moyo 0.9.0

Library for Crystal Symmetry in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
use itertools::iproduct;
use std::collections::{HashMap, HashSet, VecDeque};

use log::debug;
use nalgebra::{Matrix3, Vector3};

use super::{
    PrimitiveCell,
    primitive_cell::PrimitiveMagneticCell,
    solve::{
        PeriodicKdTree, pivot_site_indices, solve_correspondence,
        symmetrize_translation_from_permutation,
    },
};
use crate::base::{
    AngleTolerance, Cell, EPS, Lattice, MagneticCell, MagneticMoment, MagneticOperation,
    MagneticOperations, MoyoError, Operation, Operations, Permutation, Rotation,
    RotationMagneticMomentAction, Rotations, Transformation, is_angle_within_tolerance, traverse,
};

#[derive(Debug)]
pub struct PrimitiveSymmetrySearch {
    /// Operations in the given primitive cell
    pub operations: Operations,
    pub permutations: Vec<Permutation>,
}

impl PrimitiveSymmetrySearch {
    /// Return coset representatives of the space group w.r.t. its translation subgroup.
    /// Assume `primitive_cell` is a primitive cell and its basis vectors are Minkowski reduced.
    /// Returned operations are guaranteed to form a group.
    /// If the group closure and tolerance (symprec and angle_tolerance) are incompatible, the former is prioritized.
    /// Possible replacements for spglib/src/spacegroup.h::spa_search_spacegroup
    pub fn new(
        primitive_cell: &Cell,
        symprec: f64,
        angle_tolerance: AngleTolerance,
    ) -> Result<Self, MoyoError> {
        // Check if symprec is sufficiently small
        let minimum_basis_norm = primitive_cell.lattice.basis.column(0).norm();
        let rough_symprec = 2.0 * symprec;
        if rough_symprec > minimum_basis_norm / 2.0 {
            debug!(
                "symprec is too large compared to the basis vectors. Consider reducing symprec."
            );
            return Err(MoyoError::TooLargeToleranceError);
        }

        // Search symmetry operations
        let pkdtree = PeriodicKdTree::new(primitive_cell, rough_symprec);
        let bravais_group =
            search_bravais_group(&primitive_cell.lattice, symprec, angle_tolerance)?;
        let pivot_site_indices = pivot_site_indices(&primitive_cell.numbers);
        let mut symmetries_tmp = vec![];
        let src = pivot_site_indices[0];
        for rotation in bravais_group.iter() {
            let rotated_positions = primitive_cell
                .positions
                .iter()
                .map(|pos| rotation.map(|e| e as f64) * pos)
                .collect::<Vec<_>>();
            for dst in pivot_site_indices.iter() {
                // Try to overlap the `src`-th site to the `dst`-th site
                let translation = primitive_cell.positions[*dst] - rotated_positions[src];
                let new_positions = rotated_positions
                    .iter()
                    .map(|pos| pos + translation)
                    .collect::<Vec<_>>();

                if let Some(permutation) =
                    solve_correspondence(&pkdtree, primitive_cell, &new_positions)
                {
                    symmetries_tmp.push((*rotation, translation, permutation));
                    // Do not break here because there may be multiple translations with the rough tolerance
                }
            }
        }
        debug!(
            "Number of symmetry operation candidates: {}",
            symmetries_tmp.len()
        );

        // Purify symmetry operations by permutations
        let mut operations_and_permutations = vec![];
        for (rotation, rough_translation, permutation) in symmetries_tmp.iter() {
            let (translation, distance) = symmetrize_translation_from_permutation(
                primitive_cell,
                permutation,
                rotation,
                rough_translation,
            );
            if distance < symprec {
                operations_and_permutations
                    .push((Operation::new(*rotation, translation), permutation.clone()));
            }
        }
        if operations_and_permutations.is_empty() {
            debug!(
                "No symmetry operations are found. Consider increasing symprec and angle_tolerance."
            );
            return Err(MoyoError::TooSmallToleranceError);
        }

        // Recover operations by group multiplication
        let mut queue = VecDeque::new();
        let mut visited = HashSet::new();
        let mut operations = vec![];
        let mut permutations = vec![];
        queue.push_back((
            Operation::identity(),
            Permutation::identity(primitive_cell.num_atoms()),
        ));
        while !queue.is_empty() {
            let (ops_lhs, permutation_lhs) = queue.pop_front().unwrap();
            if visited.contains(&ops_lhs.rotation) {
                continue;
            }
            visited.insert(ops_lhs.rotation);
            operations.push(ops_lhs.clone());
            permutations.push(permutation_lhs.clone());

            for (ops_rhs, permutation_rhs) in operations_and_permutations.iter() {
                let mut new_ops = ops_lhs.clone() * ops_rhs.clone();
                new_ops.translation -= new_ops.translation.map(|e| e.round()); // Consider up to the translation subgroup
                let new_permutation = permutation_lhs.clone() * permutation_rhs.clone();
                queue.push_back((new_ops, new_permutation));
            }
        }
        if operations.len() != operations_and_permutations.len() {
            debug!(
                "Found operations do not form a group. Consider reducing symprec and angle_tolerance."
            );
            return Err(MoyoError::TooLargeToleranceError);
        }

        if !Self::check_closure(&operations, &primitive_cell.lattice, rough_symprec) {
            debug!(
                "Some centering translations are missing. Consider reducing symprec and angle_tolerance."
            );
            return Err(MoyoError::TooLargeToleranceError);
        }

        debug!("Order of point group: {}", operations.len());
        Ok(Self {
            operations,
            permutations,
        })
    }

    fn check_closure(operations: &Operations, lattice: &Lattice, symprec: f64) -> bool {
        let mut translations_map = HashMap::new();
        for operation in operations.iter() {
            translations_map.insert(operation.rotation, operation.translation);
        }
        for ops1 in operations.iter() {
            for ops2 in operations.iter() {
                let ops12 = ops1.clone() * ops2.clone();
                let diff =
                    (translations_map[&ops12.rotation] - ops12.translation).map(|e| e - e.round());
                if lattice.cartesian_coords(&diff).norm() > symprec {
                    return false;
                }
            }
        }
        true
    }
}

#[derive(Debug)]
pub struct PrimitiveMagneticSymmetrySearch {
    /// Magnetic operations in the given primitive magnetic cell
    pub magnetic_operations: MagneticOperations,
    pub permutations: Vec<Permutation>,
}

impl PrimitiveMagneticSymmetrySearch {
    /// Return coset representatives of the magnetic space group w.r.t. its translation subgroup.
    /// Assume `primitive_magnetic_cell` is a primitive magnetic cell and its basis vectors are Minkowski reduced.
    /// Returned magnetic operations are guaranteed to form a group.
    /// If the group closure and tolerance (symprec, angle_tolerance, and mag_symprec) are incompatible, the former is prioritized.
    pub fn new<M: MagneticMoment>(
        primitive_magnetic_cell: &MagneticCell<M>,
        symprec: f64,
        angle_tolerance: AngleTolerance,
        mag_symprec: f64,
        action: RotationMagneticMomentAction,
    ) -> Result<Self, MoyoError> {
        // Prepare candidate operations from primitive **nonmagnetic** cell
        let prim_nonmag_cell = PrimitiveCell::new(&primitive_magnetic_cell.cell, symprec)?;
        let prim_nonmag_symmetry =
            PrimitiveSymmetrySearch::new(&prim_nonmag_cell.cell, symprec, angle_tolerance)?;
        // Candidate operations in the primitive **magnetic** cell
        let candidate_operations =
            operations_in_cell(&prim_nonmag_cell, &prim_nonmag_symmetry.operations);

        // Find time reversal parts that keep magnetic moments
        let pkdtree = PeriodicKdTree::new(&primitive_magnetic_cell.cell, symprec);
        let mut magnetic_operations = vec![];
        let mut permutations = vec![];
        for operation in candidate_operations.iter() {
            let new_positions = primitive_magnetic_cell
                .cell
                .positions
                .iter()
                .map(|pos| operation.rotation.map(|e| e as f64) * pos + operation.translation)
                .collect::<Vec<_>>();
            if let Some(permutation) =
                solve_correspondence(&pkdtree, &primitive_magnetic_cell.cell, &new_positions)
            {
                let cartesian_rotation =
                    operation.cartesian_rotation(&primitive_magnetic_cell.cell.lattice);
                let rotated_magnetic_moments = primitive_magnetic_cell
                    .magnetic_moments
                    .iter()
                    .map(|m| m.act_rotation(&cartesian_rotation, action))
                    .collect::<Vec<_>>();

                let new_magnetic_moments = (0..primitive_magnetic_cell.num_atoms())
                    .map(|i| primitive_magnetic_cell.magnetic_moments[permutation.apply(i)].clone())
                    .collect::<Vec<_>>();

                // Find time_reversal s.t. time_reversal * rotated_magnetic_moments[:] = new_magnetic_moments[:]
                for time_reversal in [true, false] {
                    let acted_magnetic_moments = rotated_magnetic_moments
                        .iter()
                        .map(|m| m.act_time_reversal(time_reversal))
                        .collect::<Vec<_>>();
                    let take = acted_magnetic_moments
                        .iter()
                        .zip(new_magnetic_moments.iter())
                        .all(|(m1, m2)| m1.is_close(m2, mag_symprec));
                    if take {
                        magnetic_operations.push(MagneticOperation::from_operation(
                            operation.clone(),
                            time_reversal,
                        ));
                        permutations.push(permutation.clone());
                    }
                }
            }
        }

        // Check closure
        if !Self::check_closure(
            &magnetic_operations,
            &primitive_magnetic_cell.cell.lattice,
            symprec,
        ) {
            debug!(
                "Some centering translations are missing. Consider reducing symprec and angle_tolerance."
            );
            return Err(MoyoError::TooLargeToleranceError);
        }

        Ok(Self {
            magnetic_operations,
            permutations,
        })
    }

    fn check_closure(
        magnetic_operations: &MagneticOperations,
        lattice: &Lattice,
        symprec: f64,
    ) -> bool {
        let mut translations_map = HashMap::new();
        for mops in magnetic_operations.iter() {
            translations_map.insert(
                (mops.operation.rotation, mops.time_reversal),
                mops.operation.translation,
            );
        }
        for mops1 in magnetic_operations.iter() {
            for mops2 in magnetic_operations.iter() {
                let mops12 = mops1.clone() * mops2.clone();
                let Some(expected_translation) =
                    translations_map.get(&(mops12.operation.rotation, mops12.time_reversal))
                else {
                    return false;
                };
                let diff =
                    (expected_translation - mops12.operation.translation).map(|e| e - e.round());
                if lattice.cartesian_coords(&diff).norm() > symprec {
                    return false;
                }
            }
        }
        true
    }
}

pub fn operations_in_cell(prim_cell: &PrimitiveCell, prim_operations: &Operations) -> Operations {
    let input_operations =
        Transformation::from_linear(prim_cell.linear).transform_operations(prim_operations);
    let mut operations = vec![];
    for t1 in prim_cell.translations.iter() {
        for operation2 in input_operations.iter() {
            // (E, t1) (rotation, t2) = (rotation, t1 + t2)
            let t12 = (t1 + operation2.translation).map(|e| e % 1.);
            operations.push(Operation::new(operation2.rotation, t12));
        }
    }
    operations
}

pub fn magnetic_operations_in_magnetic_cell<M: MagneticMoment>(
    prim_mag_cell: &PrimitiveMagneticCell<M>,
    prim_mag_operations: &MagneticOperations,
) -> MagneticOperations {
    let input_mag_operations = Transformation::from_linear(prim_mag_cell.linear)
        .transform_magnetic_operations(prim_mag_operations);
    let mut mag_operations = vec![];
    for t1 in prim_mag_cell.translations.iter() {
        for ops2 in input_mag_operations.iter() {
            // (E, t1) (rotation, t2) theta = (rotation, t1 + t2) theta
            let t12 = (t1 + ops2.operation.translation).map(|e| e % 1.);
            mag_operations.push(MagneticOperation::new(
                ops2.operation.rotation,
                t12,
                ops2.time_reversal,
            ));
        }
    }
    mag_operations
}

/// Relevant to spglib.c/symmetry.c::get_lattice_symmetry
pub(super) fn search_bravais_group(
    minkowski_lattice: &Lattice,
    symprec: f64,
    angle_tolerance: AngleTolerance,
) -> Result<Rotations, MoyoError> {
    // Candidate column vectors for rotation matrix
    let lengths = minkowski_lattice
        .basis
        .column_iter()
        .map(|v| v.norm())
        .collect::<Vec<_>>();
    let mut candidate_lattice_points = [vec![], vec![], vec![]];
    // For a Minkowski-reduced lattice, it is sufficient to search coeffs in
    // `[-1, 0, 1]` because the third column is already in `[-1, 0, 1]^3`.
    // This bounded integer search is exact under that precondition.
    for coeffs in iproduct!(-1..=1, -1..=1, -1..=1) {
        let v = minkowski_lattice.basis
            * Vector3::new(coeffs.0 as f64, coeffs.1 as f64, coeffs.2 as f64);
        let v_length = v.norm();
        for (i, &length) in lengths.iter().enumerate() {
            if (v_length - length).abs() < symprec {
                candidate_lattice_points[i].push(coeffs);
            }
        }
    }

    let mut rotations = vec![];
    for (c0, c1) in iproduct!(
        candidate_lattice_points[0].iter(),
        candidate_lattice_points[1].iter()
    ) {
        let v0 = minkowski_lattice.basis * Vector3::new(c0.0 as f64, c0.1 as f64, c0.2 as f64);
        let v1 = minkowski_lattice.basis * Vector3::new(c1.0 as f64, c1.1 as f64, c1.2 as f64);

        // Check the (0, 1)-th element of metric tensor beforehand
        if !compare_nondiagonal_matrix_tensor_element(
            &minkowski_lattice.basis,
            &v0,
            &v1,
            0,
            1,
            symprec,
            angle_tolerance,
        ) {
            continue;
        }

        for c2 in candidate_lattice_points[2].iter() {
            let v2 = minkowski_lattice.basis * Vector3::new(c2.0 as f64, c2.1 as f64, c2.2 as f64);

            // Check determinant
            let rotation = Rotation::from_columns(&[
                Vector3::new(c0.0, c0.1, c0.2),
                Vector3::new(c1.0, c1.1, c1.2),
                Vector3::new(c2.0, c2.1, c2.2),
            ]);

            if relative_ne!(
                rotation.map(|e| e as f64).determinant().abs(),
                1.0,
                epsilon = EPS
            ) {
                continue;
            }

            // Check the (1, 2)th elements of metric tensor
            if !compare_nondiagonal_matrix_tensor_element(
                &minkowski_lattice.basis,
                &v1,
                &v2,
                1,
                2,
                symprec,
                angle_tolerance,
            ) {
                continue;
            }
            // Check the (2, 0)th elements of metric tensor
            if !compare_nondiagonal_matrix_tensor_element(
                &minkowski_lattice.basis,
                &v2,
                &v0,
                2,
                0,
                symprec,
                angle_tolerance,
            ) {
                continue;
            }

            rotations.push(rotation);
        }
    }

    // 48 for Oh
    if rotations.is_empty() || (48 % rotations.len() != 0) {
        debug!(
            "Found automorphisms for the lattice do not form a group. Consider reducing symprec and angle_tolerance."
        );
        return Err(MoyoError::TooLargeToleranceError);
    }

    let complemented_rotations = traverse(&rotations);
    if complemented_rotations.len() != rotations.len() {
        debug!(
            "Found automorphisms for the lattice do not form a group. Consider reducing symprec and angle_tolerance."
        );
        return Err(MoyoError::TooLargeToleranceError);
    }
    debug!("Order of Bravais group: {}", complemented_rotations.len());
    Ok(complemented_rotations)
}

/// Compare (basis.column(col1), basis.column(col2)) and (b1, b2)
/// Return true if these pairs of vectors are close enough
fn compare_nondiagonal_matrix_tensor_element(
    basis: &Matrix3<f64>,
    b1: &Vector3<f64>,
    b2: &Vector3<f64>,
    col1: usize,
    col2: usize,
    symprec: f64,
    angle_tolerance: AngleTolerance,
) -> bool {
    let theta_org = basis.column(col1).angle(&basis.column(col2));
    let theta_new = b1.angle(b2);
    let len_u = (basis.column(col1).norm() + b1.norm()) / 2.0;
    let len_v = (basis.column(col2).norm() + b2.norm()) / 2.0;
    is_angle_within_tolerance(
        theta_new - theta_org,
        len_u,
        len_v,
        symprec,
        angle_tolerance,
    )
}

#[cfg(test)]
mod tests {
    use std::vec;

    use nalgebra::{Matrix3, Vector3, matrix};
    use test_log::test;

    use super::{PrimitiveMagneticSymmetrySearch, search_bravais_group};
    use crate::base::{
        AngleTolerance, Collinear, Lattice, MagneticCell, NonCollinear,
        RotationMagneticMomentAction,
    };

    #[test]
    fn test_search_bravais_group() {
        let symprec = 1e-4;

        {
            // m-3m
            let lattice = Lattice::new(matrix![
                0.0, 0.5, 0.5;
                0.5, 0.0, 0.5;
                0.5, 0.5, 0.0;
            ]);
            let rotations =
                search_bravais_group(&lattice, symprec, AngleTolerance::Radian(1e-2)).unwrap();
            assert_eq!(rotations.len(), 48);
        }

        {
            // 6/mmm
            let lattice = Lattice::new(matrix![
                1.0, 0.0, 0.0;
                -0.5, f64::sqrt(3.0) / 2.0, 0.0;
                0.0, 0.0, 1.0;
            ]);
            let rotations =
                search_bravais_group(&lattice, symprec, AngleTolerance::default()).unwrap();
            assert_eq!(rotations.len(), 24);
        }

        {
            let lattice = Lattice::new(matrix![
                0.5, 0.0, 0.5;
                0.5, 0.5, 0.0;
                0.0, 0.5, 0.5;
            ]);
            let rotations =
                search_bravais_group(&lattice, symprec, AngleTolerance::default()).unwrap();
            assert_eq!(rotations.len(), 48);
        }
    }

    #[test]
    fn test_primitive_magnetic_symmetry_search() {
        let symprec = 1e-4;
        let angle_tolerance = AngleTolerance::default();
        let mag_symprec = 1e-4;

        {
            // AFM bcc (type 4)
            let prim_mag_cell = MagneticCell::new(
                Lattice::new(Matrix3::identity()),
                vec![Vector3::new(0.0, 0.0, 0.0), Vector3::new(0.5, 0.5, 0.5)],
                vec![0, 0],
                vec![Collinear(1.0), Collinear(-1.0)],
            );
            let result = PrimitiveMagneticSymmetrySearch::new(
                &prim_mag_cell,
                symprec,
                angle_tolerance,
                mag_symprec,
                RotationMagneticMomentAction::Axial,
            )
            .unwrap();
            assert_eq!(result.magnetic_operations.len(), 96);
            assert_eq!(result.permutations.len(), result.magnetic_operations.len());
            assert_eq!(
                result
                    .magnetic_operations
                    .iter()
                    .filter(|mops| mops.time_reversal)
                    .count(),
                48
            );
        }
        {
            // Primitive fcc (type 2)
            let prim_mag_cell = MagneticCell::new(
                Lattice::new(matrix![
                    0.0, 0.5, 0.5;
                    0.5, 0.0, 0.5;
                    0.5, 0.5, 0.0;
                ]),
                vec![Vector3::new(0.0, 0.0, 0.0)],
                vec![0],
                vec![NonCollinear(Vector3::new(0.0, 0.0, 0.0))],
            );
            let result = PrimitiveMagneticSymmetrySearch::new(
                &prim_mag_cell,
                symprec,
                angle_tolerance,
                mag_symprec,
                RotationMagneticMomentAction::Axial,
            )
            .unwrap();
            assert_eq!(result.magnetic_operations.len(), 96);
            assert_eq!(result.permutations.len(), result.magnetic_operations.len());
            assert_eq!(
                result
                    .magnetic_operations
                    .iter()
                    .filter(|mops| mops.time_reversal)
                    .count(),
                48
            );
        }
    }
}