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    #[allow(clippy::type_complexity)]
245    pub fn action(
246        &self,
247    ) -> fn(
248        &G::GroupElement,
249        &SlaterDeterminant<'a, T, SC>,
250    ) -> Result<SlaterDeterminant<'a, T, SC>, anyhow::Error> {
251        match self.symmetry_transformation_kind {
252            SymmetryTransformationKind::Spatial => |op, det| {
253                det.sym_transform_spatial(op).with_context(|| {
254                    format!("Unable to apply `{op}` spatially on the origin determinant")
255                })
256            },
257            SymmetryTransformationKind::SpatialWithSpinTimeReversal => |op, det| {
258                det.sym_transform_spatial_with_spintimerev(op).with_context(|| {
259                    format!("Unable to apply `{op}` spatially (with spin-including time reversal) on the origin determinant")
260                })
261            },
262            SymmetryTransformationKind::Spin => |op, det| {
263                det.sym_transform_spin(op).with_context(|| {
264                    format!("Unable to apply `{op}` spin-wise on the origin determinant")
265                })
266            },
267            SymmetryTransformationKind::SpinSpatial => |op, det| {
268                det.sym_transform_spin_spatial(op).with_context(|| {
269                    format!("Unable to apply `{op}` spin-spatially on the origin determinant")
270                })
271            },
272        }
273    }
274}
275
276impl<'a, G, SC> SlaterDeterminantSymmetryOrbit<'a, G, f64, SC>
277where
278    G: SymmetryGroupProperties,
279    SC: StructureConstraint + fmt::Display,
280    SlaterDeterminant<'a, f64, SC>: SymmetryTransformable,
281{
282    fn_calc_xmat_real!(
283        /// Calculates the $`\mathbf{X}`$ matrix for real and symmetric overlap matrix
284        /// $`\mathbf{S}`$ between the symmetry-equivalent Slater determinants in the orbit.
285        ///
286        /// The resulting $`\mathbf{X}`$ is stored in the orbit.
287        ///
288        /// # Arguments
289        ///
290        /// * `preserves_full_rank` - If `true`, when $`\mathbf{S}`$ is already of full rank, then
291        /// $`\mathbf{X}`$ is set to be the identity matrix to avoid mixing the orbit determinants.
292        /// If `false`, $`\mathbf{X}`$ also orthogonalises $`\mathbf{S}`$ even when it is already of
293        /// full rank.
294        pub calc_xmat
295    );
296}
297
298impl<'a, G, T, SC> SlaterDeterminantSymmetryOrbit<'a, G, Complex<T>, SC>
299where
300    G: SymmetryGroupProperties,
301    T: Float + Scalar<Complex = Complex<T>>,
302    Complex<T>: ComplexFloat<Real = T> + Scalar<Real = T, Complex = Complex<T>> + Lapack,
303    SC: StructureConstraint + fmt::Display,
304    SlaterDeterminant<'a, Complex<T>, SC>: SymmetryTransformable + Overlap<Complex<T>, Ix2>,
305{
306    fn_calc_xmat_complex!(
307        /// Calculates the $`\mathbf{X}`$ matrix for complex and symmetric or Hermitian overlap
308        /// matrix $`\mathbf{S}`$ between the symmetry-equivalent Slater determinants in the orbit.
309        ///
310        /// The resulting $`\mathbf{X}`$ is stored in the orbit.
311        ///
312        /// # Arguments
313        ///
314        /// * `preserves_full_rank` - If `true`, when $`\mathbf{S}`$ is already of full rank, then
315        /// $`\mathbf{X}`$ is set to be the identity matrix to avoid mixing the orbit determinants.
316        /// If `false`, $`\mathbf{X}`$ also orthogonalises $`\mathbf{S}`$ even when it is already of
317        /// full rank.
318        pub calc_xmat
319    );
320}
321
322// ---------------------
323// Trait implementations
324// ---------------------
325
326// ~~~~~
327// Orbit
328// ~~~~~
329
330impl<'a, G, T, SC> Orbit<G, SlaterDeterminant<'a, T, SC>>
331    for SlaterDeterminantSymmetryOrbit<'a, G, T, SC>
332where
333    G: SymmetryGroupProperties,
334    T: ComplexFloat + fmt::Debug + Lapack,
335    SC: StructureConstraint + fmt::Display,
336    SlaterDeterminant<'a, T, SC>: SymmetryTransformable,
337{
338    type OrbitIter = OrbitIterator<'a, G, SlaterDeterminant<'a, T, SC>>;
339
340    fn group(&self) -> &G {
341        self.group
342    }
343
344    fn origin(&self) -> &SlaterDeterminant<'a, T, SC> {
345        self.origin
346    }
347
348    fn iter(&self) -> Self::OrbitIter {
349        OrbitIterator::new(
350            self.group,
351            self.origin,
352            self.action(),
353            // match self.symmetry_transformation_kind {
354            //     SymmetryTransformationKind::Spatial => |op, det| {
355            //         det.sym_transform_spatial(op).with_context(|| {
356            //             format!("Unable to apply `{op}` spatially on the origin determinant")
357            //         })
358            //     },
359            //     SymmetryTransformationKind::SpatialWithSpinTimeReversal => |op, det| {
360            //         det.sym_transform_spatial_with_spintimerev(op).with_context(|| {
361            //             format!("Unable to apply `{op}` spatially (with spin-including time reversal) on the origin determinant")
362            //         })
363            //     },
364            //     SymmetryTransformationKind::Spin => |op, det| {
365            //         det.sym_transform_spin(op).with_context(|| {
366            //             format!("Unable to apply `{op}` spin-wise on the origin determinant")
367            //         })
368            //     },
369            //     SymmetryTransformationKind::SpinSpatial => |op, det| {
370            //         det.sym_transform_spin_spatial(op).with_context(|| {
371            //             format!("Unable to apply `{op}` spin-spatially on the origin determinant")
372            //         })
373            //     },
374            // },
375        )
376    }
377}
378
379// ~~~~~~~~~~~
380// RepAnalysis
381// ~~~~~~~~~~~
382
383impl<'a, G, T, SC> RepAnalysis<G, SlaterDeterminant<'a, T, SC>, T, Ix2>
384    for SlaterDeterminantSymmetryOrbit<'a, G, T, SC>
385where
386    G: SymmetryGroupProperties,
387    G::CharTab: SubspaceDecomposable<T>,
388    T: Lapack
389        + ComplexFloat<Real = <T as Scalar>::Real>
390        + fmt::Debug
391        + Mul<<T as ComplexFloat>::Real, Output = T>,
392    <T as ComplexFloat>::Real: fmt::Debug
393        + Zero
394        + From<u16>
395        + ToPrimitive
396        + approx::RelativeEq<<T as ComplexFloat>::Real>
397        + approx::AbsDiffEq<Epsilon = <T as Scalar>::Real>,
398    SC: StructureConstraint + Eq + fmt::Display,
399    SlaterDeterminant<'a, T, SC>: SymmetryTransformable,
400{
401    fn set_smat(&mut self, smat: Array2<T>) {
402        self.smat = Some(smat)
403    }
404
405    fn smat(&self) -> Option<&Array2<T>> {
406        self.smat.as_ref()
407    }
408
409    fn xmat(&self) -> &Array2<T> {
410        self.xmat
411            .as_ref()
412            .expect("Orbit overlap orthogonalisation matrix not found.")
413    }
414
415    fn norm_preserving_scalar_map(&self, i: usize) -> Result<fn(T) -> T, anyhow::Error> {
416        if self.origin.complex_symmetric {
417            Err(format_err!(
418                "`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."
419            ))
420        } else 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    fn integrality_threshold(&self) -> <T as ComplexFloat>::Real {
433        self.integrality_threshold
434    }
435
436    fn eigenvalue_comparison_mode(&self) -> &EigenvalueComparisonMode {
437        &self.eigenvalue_comparison_mode
438    }
439
440    /// Reduces the representation or corepresentation spanned by the determinants in the orbit to
441    /// a direct sum of the irreducible representations or corepresentations of the generating
442    /// symmetry group.
443    ///
444    /// # Returns
445    ///
446    /// The decomposed result.
447    ///
448    /// # Errors
449    ///
450    /// Errors if the decomposition fails, *e.g.* because one or more calculated multiplicities
451    /// are non-integral, or also because the combination of group type, transformation type, and
452    /// oddity of the number of electrons would not give sensible symmetry results. In particular,
453    /// spin or spin-spatial symmetry analysis of odd-electron systems in unitary-represented
454    /// magnetic groups is not valid.
455    fn analyse_rep(
456        &self,
457    ) -> Result<
458        <<G as CharacterProperties>::CharTab as SubspaceDecomposable<T>>::Decomposition,
459        DecompositionError,
460    > {
461        log::debug!("Analysing representation symmetry for a Slater determinant...");
462        let nelectrons_float = self.origin().nelectrons();
463        if approx::relative_eq!(
464            nelectrons_float.round(),
465            nelectrons_float,
466            epsilon = self.integrality_threshold,
467            max_relative = self.integrality_threshold
468        ) {
469            let nelectrons_usize = nelectrons_float.round().to_usize().unwrap_or_else(|| {
470                panic!(
471                    "Unable to convert the number of electrons `{nelectrons_float:.7}` to `usize`."
472                );
473            });
474            let (valid_symmetry, err_str) = if nelectrons_usize.rem_euclid(2) == 0 {
475                // Even number of electrons; always valid
476                (true, String::new())
477            } else {
478                // Odd number of electrons; validity depends on group and orbit type
479                match self.symmetry_transformation_kind {
480                    SymmetryTransformationKind::Spatial => (true, String::new()),
481                    SymmetryTransformationKind::SpatialWithSpinTimeReversal
482                        | SymmetryTransformationKind::Spin
483                        | SymmetryTransformationKind::SpinSpatial => {
484                        match self.group().group_type() {
485                            GroupType::Ordinary(_) => (true, String::new()),
486                            GroupType::MagneticGrey(_) | GroupType::MagneticBlackWhite(_) => {
487                                (!self.group().unitary_represented(),
488                                "Unitary-represented magnetic groups cannot be used for symmetry analysis of odd-electron systems where spin is treated explicitly.".to_string())
489                            }
490                        }
491                    }
492                }
493            };
494
495            if valid_symmetry {
496                let chis = self
497                    .calc_characters()
498                    .map_err(|err| DecompositionError(err.to_string()))?;
499                log::debug!("Characters calculated.");
500
501                log_subtitle("Determinant orbit characters");
502                qsym2_output!("");
503                self.characters_to_string(&chis, self.integrality_threshold)
504                    .log_output_display();
505                qsym2_output!("");
506
507                let res = self.group().character_table().reduce_characters(
508                    &chis.iter().map(|(cc, chi)| (cc, *chi)).collect::<Vec<_>>(),
509                    self.integrality_threshold(),
510                );
511                log::debug!("Characters reduced.");
512                log::debug!("Analysing representation symmetry for a Slater determinant... Done.");
513                res
514            } else {
515                Err(DecompositionError(err_str))
516            }
517        } else {
518            Err(DecompositionError(format!(
519                "Symmetry analysis for determinant with non-integer number of electrons `{nelectrons_float:.7}` (threshold = {:.3e}) not supported.",
520                self.integrality_threshold
521            )))
522        }
523    }
524}