qsym2/target/determinant/
determinant_analysis.rs

1//! Implementation of symmetry analysis for Slater determinants.
2
3use std::fmt;
4use std::ops::Mul;
5
6use anyhow::{self, Context, ensure, format_err};
7use approx;
8use derive_builder::Builder;
9use itertools::{Itertools, izip};
10use log;
11use ndarray::{Array1, Array2, Axis, Ix2};
12use ndarray_linalg::{
13    UPLO,
14    eig::Eig,
15    eigh::Eigh,
16    solve::Determinant,
17    types::{Lapack, Scalar},
18};
19use num_complex::{Complex, ComplexFloat};
20use num_traits::{Float, ToPrimitive, Zero};
21
22use crate::analysis::{
23    EigenvalueComparisonMode, Orbit, OrbitIterator, Overlap, RepAnalysis, fn_calc_xmat_complex,
24    fn_calc_xmat_real,
25};
26use crate::angmom::spinor_rotation_3d::StructureConstraint;
27use crate::auxiliary::misc::complex_modified_gram_schmidt;
28use crate::chartab::chartab_group::CharacterProperties;
29use crate::chartab::{DecompositionError, SubspaceDecomposable};
30use crate::group::GroupType;
31use crate::io::format::{QSym2Output, log_subtitle, qsym2_output};
32use crate::symmetry::symmetry_element::symmetry_operation::SpecialSymmetryTransformation;
33use crate::symmetry::symmetry_group::SymmetryGroupProperties;
34use crate::symmetry::symmetry_transformation::{SymmetryTransformable, SymmetryTransformationKind};
35use crate::target::determinant::SlaterDeterminant;
36
37// =======
38// Overlap
39// =======
40
41impl<'a, T, SC> Overlap<T, Ix2> for SlaterDeterminant<'a, T, SC>
42where
43    T: Lapack
44        + ComplexFloat<Real = <T as Scalar>::Real>
45        + fmt::Debug
46        + Mul<<T as ComplexFloat>::Real, Output = T>,
47    <T as ComplexFloat>::Real: fmt::Debug
48        + approx::RelativeEq<<T as ComplexFloat>::Real>
49        + approx::AbsDiffEq<Epsilon = <T as Scalar>::Real>,
50    SC: StructureConstraint + Eq + fmt::Display,
51{
52    fn complex_symmetric(&self) -> bool {
53        self.complex_symmetric
54    }
55
56    /// Computes the overlap between two Slater determinants.
57    ///
58    /// Determinants with fractional electrons are currently not supported.
59    ///
60    /// When one or both of the Slater determinants have been acted on by an antiunitary operation,
61    /// the correct Hermitian or complex-symmetric metric will be chosen in the evalulation of the
62    /// overlap.
63    ///
64    /// # Arguments
65    ///
66    /// * `metric` - The atomic-orbital overlap matrix with respect to the conventional sesquilinear
67    /// inner product.
68    /// * `metric_h` - The atomic-orbital overlap matrix with respect to the bilinear inner product.
69    ///
70    /// # Panics
71    ///
72    /// Panics if `self` and `other` have mismatched spin constraints or numbers of coefficient
73    /// matrices, or if fractional occupation numbers are detected.
74    fn overlap(
75        &self,
76        other: &Self,
77        metric: Option<&Array2<T>>,
78        metric_h: Option<&Array2<T>>,
79    ) -> Result<T, anyhow::Error> {
80        ensure!(
81            self.structure_constraint == other.structure_constraint,
82            "Inconsistent structure constraints between `self` and `other`."
83        );
84        ensure!(
85            self.coefficients.len() == other.coefficients.len(),
86            "Inconsistent numbers of coefficient matrices between `self` and `other`."
87        );
88        ensure!(
89            self.baos == other.baos,
90            "Inconsistent basis angular order between `self` and `other`."
91        );
92
93        let thresh = Float::sqrt(self.threshold * other.threshold);
94        ensure!(
95            self.occupations
96                .iter()
97                .chain(other.occupations.iter())
98                .all(|occs| occs.iter().all(|&occ| approx::relative_eq!(
99                    occ,
100                    occ.round(),
101                    epsilon = thresh,
102                    max_relative = thresh
103                ))),
104            "Overlaps between determinants with fractional occupation numbers are currently not supported."
105        );
106
107        let sao = metric.ok_or_else(|| format_err!("No atomic-orbital metric found."))?;
108        let sao_h = metric_h.unwrap_or(sao);
109
110        let ov = izip!(
111            &self.coefficients,
112            &self.occupations,
113            &other.coefficients,
114            &other.occupations
115        )
116        .map(|(cw, occw, cx, occx)| {
117            let nonzero_occ_w = occw.iter().positions(|&occ| occ > thresh).collect_vec();
118            let cw_o = cw.select(Axis(1), &nonzero_occ_w);
119            let nonzero_occ_x = occx.iter().positions(|&occ| occ > thresh).collect_vec();
120            let cx_o = cx.select(Axis(1), &nonzero_occ_x);
121
122            let mo_ov_mat = if self.complex_symmetric() {
123                match (self.complex_conjugated, other.complex_conjugated) {
124                    (false, false) => cw_o.t().dot(sao_h).dot(&cx_o),
125                    (true, false) => cw_o.t().dot(sao).dot(&cx_o),
126                    (false, true) => cx_o.t().dot(sao).dot(&cw_o),
127                    (true, true) => cw_o.t().dot(&sao_h.t()).dot(&cx_o),
128                }
129            } else {
130                match (self.complex_conjugated, other.complex_conjugated) {
131                    (false, false) => cw_o.t().mapv(|x| x.conj()).dot(sao).dot(&cx_o),
132                    (true, false) => cw_o.t().mapv(|x| x.conj()).dot(sao_h).dot(&cx_o),
133                    (false, true) => cx_o
134                        .t()
135                        .mapv(|x| x.conj())
136                        .dot(sao_h)
137                        .dot(&cw_o)
138                        .mapv(|x| x.conj()),
139                    (true, true) => cw_o.t().mapv(|x| x.conj()).dot(&sao.t()).dot(&cx_o),
140                }
141            };
142            mo_ov_mat
143                .det()
144                .expect("The determinant of the MO overlap matrix could not be found.")
145        })
146        .fold(T::one(), |acc, x| acc * x);
147
148        let implicit_factor = self.structure_constraint.implicit_factor()?;
149        if implicit_factor > 1 {
150            let p_i32 = i32::try_from(implicit_factor)?;
151            Ok(ComplexFloat::powi(ov, p_i32))
152        } else {
153            Ok(ov)
154        }
155    }
156
157    /// Returns the mathematical definition of the overlap between two Slater determinants.
158    fn overlap_definition(&self) -> String {
159        let k = if self.complex_symmetric() { "κ " } else { "" };
160        format!("⟨{k}Ψ_1|Ψ_2⟩ = ∫ [{k}Ψ_1(x^Ne)]* Ψ_2(x^Ne) dx^Ne")
161    }
162}
163
164// ==============================
165// SlaterDeterminantSymmetryOrbit
166// ==============================
167
168// -----------------
169// Struct definition
170// -----------------
171
172/// Structure to manage symmetry orbits (*i.e.* orbits generated by symmetry groups) of Slater
173/// determinants.
174#[derive(Builder, Clone)]
175pub struct SlaterDeterminantSymmetryOrbit<'a, G, T, SC>
176where
177    G: SymmetryGroupProperties,
178    T: ComplexFloat + fmt::Debug + Lapack,
179    SC: StructureConstraint + fmt::Display,
180    SlaterDeterminant<'a, T, SC>: SymmetryTransformable,
181{
182    /// The generating symmetry group.
183    group: &'a G,
184
185    /// The origin Slater determinant of the orbit.
186    origin: &'a SlaterDeterminant<'a, T, SC>,
187
188    /// The threshold for determining zero eigenvalues in the orbit overlap matrix.
189    linear_independence_threshold: <T as ComplexFloat>::Real,
190
191    /// The threshold for determining if calculated multiplicities in representation analysis are
192    /// integral.
193    integrality_threshold: <T as ComplexFloat>::Real,
194
195    /// The kind of transformation determining the way the symmetry operations in `group` act on
196    /// [`Self::origin`].
197    symmetry_transformation_kind: SymmetryTransformationKind,
198
199    /// The overlap matrix between the symmetry-equivalent Slater determinants in the orbit.
200    #[builder(setter(skip), default = "None")]
201    smat: Option<Array2<T>>,
202
203    /// The eigenvalues of the overlap matrix between the symmetry-equivalent Slater determinants in
204    /// the orbit.
205    #[builder(setter(skip), default = "None")]
206    pub(crate) smat_eigvals: Option<Array1<T>>,
207
208    /// The $`\mathbf{X}`$ matrix for the overlap matrix between the symmetry-equivalent Slater
209    /// determinants in the orbit.
210    ///
211    /// See [`RepAnalysis::xmat`] for further information.
212    #[builder(setter(skip), default = "None")]
213    xmat: Option<Array2<T>>,
214
215    /// An enumerated type specifying the comparison mode for filtering out orbit overlap
216    /// eigenvalues.
217    eigenvalue_comparison_mode: EigenvalueComparisonMode,
218}
219
220// ----------------------------
221// Struct method implementation
222// ----------------------------
223
224impl<'a, G, T, SC> SlaterDeterminantSymmetryOrbit<'a, G, T, SC>
225where
226    G: SymmetryGroupProperties + Clone,
227    T: ComplexFloat + fmt::Debug + Lapack,
228    SC: StructureConstraint + Clone + fmt::Display,
229    SlaterDeterminant<'a, T, SC>: SymmetryTransformable,
230{
231    /// Returns a builder for constructing a new Slater determinant symmetry orbit.
232    pub fn builder() -> SlaterDeterminantSymmetryOrbitBuilder<'a, G, T, SC> {
233        SlaterDeterminantSymmetryOrbitBuilder::default()
234    }
235}
236
237impl<'a, G, T, SC> SlaterDeterminantSymmetryOrbit<'a, G, T, SC>
238where
239    G: SymmetryGroupProperties,
240    T: ComplexFloat + fmt::Debug + Lapack,
241    SC: StructureConstraint + fmt::Display,
242    SlaterDeterminant<'a, T, SC>: SymmetryTransformable,
243{
244    pub fn action(
245        &self,
246    ) -> fn(
247        &G::GroupElement,
248        &SlaterDeterminant<'a, T, SC>,
249    ) -> Result<SlaterDeterminant<'a, T, SC>, anyhow::Error> {
250        match self.symmetry_transformation_kind {
251            SymmetryTransformationKind::Spatial => |op, det| {
252                det.sym_transform_spatial(op).with_context(|| {
253                    format!("Unable to apply `{op}` spatially on the origin determinant")
254                })
255            },
256            SymmetryTransformationKind::SpatialWithSpinTimeReversal => |op, det| {
257                det.sym_transform_spatial_with_spintimerev(op).with_context(|| {
258                    format!("Unable to apply `{op}` spatially (with spin-including time reversal) on the origin determinant")
259                })
260            },
261            SymmetryTransformationKind::Spin => |op, det| {
262                det.sym_transform_spin(op).with_context(|| {
263                    format!("Unable to apply `{op}` spin-wise on the origin determinant")
264                })
265            },
266            SymmetryTransformationKind::SpinSpatial => |op, det| {
267                det.sym_transform_spin_spatial(op).with_context(|| {
268                    format!("Unable to apply `{op}` spin-spatially on the origin determinant")
269                })
270            },
271        }
272    }
273}
274
275impl<'a, G, SC> SlaterDeterminantSymmetryOrbit<'a, G, f64, SC>
276where
277    G: SymmetryGroupProperties,
278    SC: StructureConstraint + fmt::Display,
279    SlaterDeterminant<'a, f64, SC>: SymmetryTransformable,
280{
281    fn_calc_xmat_real!(
282        /// Calculates the $`\mathbf{X}`$ matrix for real and symmetric overlap matrix
283        /// $`\mathbf{S}`$ between the symmetry-equivalent Slater determinants in the orbit.
284        ///
285        /// The resulting $`\mathbf{X}`$ is stored in the orbit.
286        ///
287        /// # Arguments
288        ///
289        /// * `preserves_full_rank` - If `true`, when $`\mathbf{S}`$ is already of full rank, then
290        /// $`\mathbf{X}`$ is set to be the identity matrix to avoid mixing the orbit determinants.
291        /// If `false`, $`\mathbf{X}`$ also orthogonalises $`\mathbf{S}`$ even when it is already of
292        /// full rank.
293        pub calc_xmat
294    );
295}
296
297impl<'a, G, T, SC> SlaterDeterminantSymmetryOrbit<'a, G, Complex<T>, SC>
298where
299    G: SymmetryGroupProperties,
300    T: Float + Scalar<Complex = Complex<T>>,
301    Complex<T>: ComplexFloat<Real = T> + Scalar<Real = T, Complex = Complex<T>> + Lapack,
302    SC: StructureConstraint + fmt::Display,
303    SlaterDeterminant<'a, Complex<T>, SC>: SymmetryTransformable + Overlap<Complex<T>, Ix2>,
304{
305    fn_calc_xmat_complex!(
306        /// Calculates the $`\mathbf{X}`$ matrix for complex and symmetric or Hermitian overlap
307        /// matrix $`\mathbf{S}`$ between the symmetry-equivalent Slater determinants in the orbit.
308        ///
309        /// The resulting $`\mathbf{X}`$ is stored in the orbit.
310        ///
311        /// # Arguments
312        ///
313        /// * `preserves_full_rank` - If `true`, when $`\mathbf{S}`$ is already of full rank, then
314        /// $`\mathbf{X}`$ is set to be the identity matrix to avoid mixing the orbit determinants.
315        /// If `false`, $`\mathbf{X}`$ also orthogonalises $`\mathbf{S}`$ even when it is already of
316        /// full rank.
317        pub calc_xmat
318    );
319}
320
321// ---------------------
322// Trait implementations
323// ---------------------
324
325// ~~~~~
326// Orbit
327// ~~~~~
328
329impl<'a, G, T, SC> Orbit<G, SlaterDeterminant<'a, T, SC>>
330    for SlaterDeterminantSymmetryOrbit<'a, G, T, SC>
331where
332    G: SymmetryGroupProperties,
333    T: ComplexFloat + fmt::Debug + Lapack,
334    SC: StructureConstraint + fmt::Display,
335    SlaterDeterminant<'a, T, SC>: SymmetryTransformable,
336{
337    type OrbitIter = OrbitIterator<'a, G, SlaterDeterminant<'a, T, SC>>;
338
339    fn group(&self) -> &G {
340        self.group
341    }
342
343    fn origin(&self) -> &SlaterDeterminant<'a, T, SC> {
344        self.origin
345    }
346
347    fn iter(&self) -> Self::OrbitIter {
348        OrbitIterator::new(
349            self.group,
350            self.origin,
351            self.action(),
352            // match self.symmetry_transformation_kind {
353            //     SymmetryTransformationKind::Spatial => |op, det| {
354            //         det.sym_transform_spatial(op).with_context(|| {
355            //             format!("Unable to apply `{op}` spatially on the origin determinant")
356            //         })
357            //     },
358            //     SymmetryTransformationKind::SpatialWithSpinTimeReversal => |op, det| {
359            //         det.sym_transform_spatial_with_spintimerev(op).with_context(|| {
360            //             format!("Unable to apply `{op}` spatially (with spin-including time reversal) on the origin determinant")
361            //         })
362            //     },
363            //     SymmetryTransformationKind::Spin => |op, det| {
364            //         det.sym_transform_spin(op).with_context(|| {
365            //             format!("Unable to apply `{op}` spin-wise on the origin determinant")
366            //         })
367            //     },
368            //     SymmetryTransformationKind::SpinSpatial => |op, det| {
369            //         det.sym_transform_spin_spatial(op).with_context(|| {
370            //             format!("Unable to apply `{op}` spin-spatially on the origin determinant")
371            //         })
372            //     },
373            // },
374        )
375    }
376}
377
378// ~~~~~~~~~~~
379// RepAnalysis
380// ~~~~~~~~~~~
381
382impl<'a, G, T, SC> RepAnalysis<G, SlaterDeterminant<'a, T, SC>, T, Ix2>
383    for SlaterDeterminantSymmetryOrbit<'a, G, T, SC>
384where
385    G: SymmetryGroupProperties,
386    G::CharTab: SubspaceDecomposable<T>,
387    T: Lapack
388        + ComplexFloat<Real = <T as Scalar>::Real>
389        + fmt::Debug
390        + Mul<<T as ComplexFloat>::Real, Output = T>,
391    <T as ComplexFloat>::Real: fmt::Debug
392        + Zero
393        + From<u16>
394        + ToPrimitive
395        + approx::RelativeEq<<T as ComplexFloat>::Real>
396        + approx::AbsDiffEq<Epsilon = <T as Scalar>::Real>,
397    SC: StructureConstraint + Eq + fmt::Display,
398    SlaterDeterminant<'a, T, SC>: SymmetryTransformable,
399{
400    fn set_smat(&mut self, smat: Array2<T>) {
401        self.smat = Some(smat)
402    }
403
404    fn smat(&self) -> Option<&Array2<T>> {
405        self.smat.as_ref()
406    }
407
408    fn xmat(&self) -> &Array2<T> {
409        self.xmat
410            .as_ref()
411            .expect("Orbit overlap orthogonalisation matrix not found.")
412    }
413
414    fn norm_preserving_scalar_map(&self, i: usize) -> Result<fn(T) -> T, anyhow::Error> {
415        if self.origin.complex_symmetric {
416            Err(format_err!(
417                "`norm_preserving_scalar_map` is currently not implemented for complex-symmetric overlaps. This thus precludes the use of the Cayley table to speed up the computation of the orbit overlap matrix."
418            ))
419        } else {
420            if self
421                .group
422                .get_index(i)
423                .unwrap_or_else(|| panic!("Group operation index `{i}` not found."))
424                .contains_time_reversal()
425            {
426                Ok(ComplexFloat::conj)
427            } else {
428                Ok(|x| x)
429            }
430        }
431    }
432
433    fn integrality_threshold(&self) -> <T as ComplexFloat>::Real {
434        self.integrality_threshold
435    }
436
437    fn eigenvalue_comparison_mode(&self) -> &EigenvalueComparisonMode {
438        &self.eigenvalue_comparison_mode
439    }
440
441    /// Reduces the representation or corepresentation spanned by the determinants in the orbit to
442    /// a direct sum of the irreducible representations or corepresentations of the generating
443    /// symmetry group.
444    ///
445    /// # Returns
446    ///
447    /// The decomposed result.
448    ///
449    /// # Errors
450    ///
451    /// Errors if the decomposition fails, *e.g.* because one or more calculated multiplicities
452    /// are non-integral, or also because the combination of group type, transformation type, and
453    /// oddity of the number of electrons would not give sensible symmetry results. In particular,
454    /// spin or spin-spatial symmetry analysis of odd-electron systems in unitary-represented
455    /// magnetic groups is not valid.
456    fn analyse_rep(
457        &self,
458    ) -> Result<
459        <<G as CharacterProperties>::CharTab as SubspaceDecomposable<T>>::Decomposition,
460        DecompositionError,
461    > {
462        log::debug!("Analysing representation symmetry for a Slater determinant...");
463        let nelectrons_float = self.origin().nelectrons();
464        if approx::relative_eq!(
465            nelectrons_float.round(),
466            nelectrons_float,
467            epsilon = self.integrality_threshold,
468            max_relative = self.integrality_threshold
469        ) {
470            let nelectrons_usize = nelectrons_float.round().to_usize().unwrap_or_else(|| {
471                panic!(
472                    "Unable to convert the number of electrons `{nelectrons_float:.7}` to `usize`."
473                );
474            });
475            let (valid_symmetry, err_str) = if nelectrons_usize.rem_euclid(2) == 0 {
476                // Even number of electrons; always valid
477                (true, String::new())
478            } else {
479                // Odd number of electrons; validity depends on group and orbit type
480                match self.symmetry_transformation_kind {
481                    SymmetryTransformationKind::Spatial => (true, String::new()),
482                    SymmetryTransformationKind::SpatialWithSpinTimeReversal
483                        | SymmetryTransformationKind::Spin
484                        | SymmetryTransformationKind::SpinSpatial => {
485                        match self.group().group_type() {
486                            GroupType::Ordinary(_) => (true, String::new()),
487                            GroupType::MagneticGrey(_) | GroupType::MagneticBlackWhite(_) => {
488                                (!self.group().unitary_represented(),
489                                "Unitary-represented magnetic groups cannot be used for symmetry analysis of odd-electron systems where spin is treated explicitly.".to_string())
490                            }
491                        }
492                    }
493                }
494            };
495
496            if valid_symmetry {
497                let chis = self
498                    .calc_characters()
499                    .map_err(|err| DecompositionError(err.to_string()))?;
500                log::debug!("Characters calculated.");
501
502                log_subtitle("Determinant orbit characters");
503                qsym2_output!("");
504                self.characters_to_string(&chis, self.integrality_threshold)
505                    .log_output_display();
506                qsym2_output!("");
507
508                let res = self.group().character_table().reduce_characters(
509                    &chis.iter().map(|(cc, chi)| (cc, *chi)).collect::<Vec<_>>(),
510                    self.integrality_threshold(),
511                );
512                log::debug!("Characters reduced.");
513                log::debug!("Analysing representation symmetry for a Slater determinant... Done.");
514                res
515            } else {
516                Err(DecompositionError(err_str))
517            }
518        } else {
519            Err(DecompositionError(format!(
520                "Symmetry analysis for determinant with non-integer number of electrons `{nelectrons_float:.7}` (threshold = {:.3e}) not supported.",
521                self.integrality_threshold
522            )))
523        }
524    }
525}