qsym2/sandbox/target/real_space_function/
mod.rs

1//! Real-space functions.
2
3use std::fmt;
4
5use derive_builder::Builder;
6use itertools::Itertools;
7use nalgebra::Point3;
8use ndarray_linalg::types::Lapack;
9use num_complex::ComplexFloat;
10
11#[cfg(test)]
12mod real_space_function_tests;
13
14pub mod real_space_function_analysis;
15mod real_space_function_transformation;
16
17// ==================
18// Struct definitions
19// ==================
20
21/// Structure to manage real-space functions.
22#[derive(Builder, Clone)]
23#[builder(build_fn(validate = "Self::validate"))]
24pub struct RealSpaceFunction<T, F>
25where
26    T: ComplexFloat + Lapack,
27    F: Fn(&Point3<f64>) -> T,
28{
29    /// The grid points $`\mathbf{r}_j`$ at which the real-space function is to be evaluated.
30    grid_points: Vec<Point3<f64>>,
31
32    /// The function $`\mathbb{R}^3 \to T`$.
33    function: F,
34
35    /// A boolean indicating if action of [`Self::function`] needs to be complex-conjugated.
36    #[builder(default = "false")]
37    complex_conjugated: bool,
38}
39
40impl<T, F> RealSpaceFunctionBuilder<T, F>
41where
42    T: ComplexFloat + Lapack,
43    F: Fn(&Point3<f64>) -> T,
44{
45    fn validate(&self) -> Result<(), String> {
46        let _ = self
47            .grid_points
48            .as_ref()
49            .ok_or("No grid points found.".to_string())?;
50        let _ = self
51            .function
52            .as_ref()
53            .ok_or("No real-space function found.".to_string())?;
54        Ok(())
55    }
56}
57
58impl<T, F> RealSpaceFunction<T, F>
59where
60    T: ComplexFloat + Clone + Lapack,
61    F: Clone + Fn(&Point3<f64>) -> T,
62{
63    /// Returns a builder to construct a new [`RealSpaceFunction`].
64    pub fn builder() -> RealSpaceFunctionBuilder<T, F> {
65        RealSpaceFunctionBuilder::default()
66    }
67
68    /// Returns a vector of shared references to the grid points at which the real-space function is
69    /// evaluated.
70    pub fn grid_points(&self) -> Vec<&Point3<f64>> {
71        self.grid_points.iter().collect_vec()
72    }
73
74    /// Returns a shared reference to the function defining the [`RealSpaceFunction`].
75    pub fn function(&self) -> &F {
76        &self.function
77    }
78}
79
80// =====================
81// Trait implementations
82// =====================
83
84// -------
85// Display
86// -------
87impl<T, F> fmt::Display for RealSpaceFunction<T, F>
88where
89    T: ComplexFloat + Lapack,
90    F: Fn(&Point3<f64>) -> T,
91{
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        write!(
94            f,
95            "RealSpaceFunction{}[grid of {} {}]",
96            if self.complex_conjugated { "*" } else { "" },
97            self.grid_points.len(),
98            if self.grid_points.len() == 1 {
99                "point"
100            } else {
101                "points"
102            }
103        )?;
104        Ok(())
105    }
106}