gchemol_lattice/
supercell.rs

1// base
2
3// [[file:~/Workspace/Programming/gchemol-rs/lattice/lattice.note::*base][base:1]]
4use crate::Lattice;
5use gchemol_gut::itertools::*;
6use vecfx::Vector3f;
7
8impl Lattice {
9    /// Create a supercell along three cell directions.
10    pub fn replicate(
11        &self,
12        ra: impl Iterator<Item = isize> + Clone,
13        rb: impl Iterator<Item = isize> + Clone,
14        rc: impl Iterator<Item = isize> + Clone,
15    ) -> impl Iterator<Item = Vector3f> {
16        iproduct!(ra, rb, rc).map(|(i, j, k)| Vector3f::from([i as f64, j as f64, k as f64]))
17    }
18}
19
20// #[cfg(feature = "adhoc")]
21// /// Helper struct for periodic image
22// #[derive(Clone, Copy, Debug)]
23// pub struct Image(pub(crate) isize, pub(crate) isize, pub(crate) isize);
24
25// #[cfg(feature = "adhoc")]
26// impl Image {
27//     /// Return fractional translation vector for moving a point into this image.
28//     pub fn translation_vector(&self) -> Vector3f {
29//         [self.0 as f64, self.1 as f64, self.2 as f64].into()
30//     }
31
32//     /// Return image location relative to origin cell.
33//     pub fn location(&self) -> [isize; 3] {
34//         [self.0, self.1, self.2]
35//     }
36// }
37// base:1 ends here
38
39// test
40
41// [[file:~/Workspace/Programming/gchemol-rs/lattice/lattice.note::*test][test:1]]
42#[test]
43fn test_supercell() {
44    // Setup lattice
45    // a = b = c = 4, alpha = beta = gamma = 60
46    let cell = [
47        [4.00000000, 0.00000000, 0.00000000],
48        [2.00000000, 3.46410162, 0.00000000],
49        [2.00000000, 1.15470054, 3.26598632],
50    ];
51
52    let lattice = Lattice::new(cell);
53    let cell_images = lattice.replicate(-1..=1, -1..=1, -1..=1);
54    assert_eq!(cell_images.count(), 27);
55}
56// test:1 ends here