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
//! Lattice data structure

use super::util::Axis;
use crate::alloy::Alloy;
use crate::error::VegasLatticeError;
use crate::mask::Mask;
use crate::site::Site;
use crate::vertex::Vertex;
use rand::thread_rng;
use serde::{Deserialize, Serialize};
use std::iter::repeat;
use std::str::FromStr;

/// A lattice is a collection of sites and vertices.
///
/// For now it only supports rectangular lattices. This is Orthorombic, Tetragonal and Cubic
/// Bravais lattices. We assume the lattice vectors are aligned with the cartesian axes. While you
/// can choose the lattice parameters _a_, _b_, and _c_ to be different.
#[derive(Debug, Serialize, Deserialize)]
pub struct Lattice {
    size: (f64, f64, f64),
    sites: Vec<Site>,
    vertices: Vec<Vertex>,
}

impl Lattice {
    /// Create a new lattice with the given size
    pub fn new(size: (f64, f64, f64)) -> Self {
        Lattice {
            size,
            sites: Vec::new(),
            vertices: Vec::new(),
        }
    }

    /// Create a simple cubic lattice with the given size _a_
    pub fn sc(a: f64) -> Self {
        let sites = vec![Site::new("A")];
        let vertices = vec![
            Vertex::new(0, 0, (1, 0, 0)),
            Vertex::new(0, 0, (0, 1, 0)),
            Vertex::new(0, 0, (0, 0, 1)),
        ];
        Lattice {
            size: (a, a, a),
            sites,
            vertices,
        }
    }

    /// Create a body centered cubic lattice with the given size _a_
    pub fn bcc(a: f64) -> Self {
        let sites = vec![
            Site::new("A"),
            Site::new("B").with_position((0.5 * a, 0.5 * a, 0.5 * a)),
        ];
        let vertices = vec![
            Vertex::new(0, 1, (0, 0, 0)),
            Vertex::new(0, 1, (0, -1, 0)),
            Vertex::new(0, 1, (-1, 0, 0)),
            Vertex::new(0, 1, (-1, -1, 0)),
            Vertex::new(0, 1, (0, 0, -1)),
            Vertex::new(0, 1, (0, -1, -1)),
            Vertex::new(0, 1, (-1, 0, -1)),
            Vertex::new(0, 1, (-1, -1, -1)),
        ];
        Lattice {
            size: (a, a, a),
            sites,
            vertices,
        }
    }

    /// Create a face centered cubic lattice with lattice parameter _a_
    pub fn fcc(a: f64) -> Self {
        let sites = vec![
            Site::new("A"),
            Site::new("B").with_position((0.5 * a, 0.5 * a, 0.0)),
            Site::new("C").with_position((0.5 * a, 0.0, 0.5 * a)),
            Site::new("D").with_position((0.0, 0.5 * a, 0.5 * a)),
        ];
        let vertices = vec![
            // xy plane
            Vertex::new(0, 1, (0, 0, 0)),
            Vertex::new(0, 1, (-1, 0, 0)),
            Vertex::new(0, 1, (-1, -1, 0)),
            Vertex::new(0, 1, (0, -1, 0)),
            // xz plane
            Vertex::new(0, 2, (0, 0, 0)),
            Vertex::new(0, 2, (-1, 0, 0)),
            Vertex::new(0, 2, (-1, 0, -1)),
            Vertex::new(0, 2, (0, 0, -1)),
            // yz plane
            Vertex::new(0, 3, (0, 0, 0)),
            Vertex::new(0, 3, (0, -1, 0)),
            Vertex::new(0, 3, (0, -1, -1)),
            Vertex::new(0, 3, (0, 0, -1)),
        ];
        Lattice {
            size: (a, a, a),
            sites,
            vertices,
        }
    }

    /// Get the size of the lattice
    pub fn size(&self) -> (f64, f64, f64) {
        self.size
    }

    /// Get the sites of the lattice
    pub fn sites(&self) -> &[Site] {
        &self.sites
    }

    /// Get the vertices of the lattice
    pub fn vertices(&self) -> &[Vertex] {
        &self.vertices
    }

    /// Changes the size of the lattice
    pub fn with_size(mut self, size: (f64, f64, f64)) -> Self {
        self.size = size;
        self
    }

    /// Changes the sites of the lattice
    pub fn with_sites(mut self, sites: Vec<Site>) -> Self {
        self.sites = sites;
        self
    }

    /// Changes the vertices of the lattice
    pub fn with_vertices(mut self, vertices: Vec<Vertex>) -> Self {
        self.vertices = vertices;
        self
    }

    fn are_vertices_consistent(&self) -> bool {
        self.vertices
            .iter()
            .map(|vertex| vertex.source())
            .chain(self.vertices.iter().map(|vertex| vertex.target()))
            .all(|id| id < self.sites.len())
    }

    /// Validates the lattice
    pub fn validate(self) -> Result<Self, VegasLatticeError> {
        if !self.are_vertices_consistent() {
            return Err(VegasLatticeError::InconsistentVertices);
        }
        if self.size.0 < 0.0 || self.size.1 < 0.0 || self.size.2 < 0.0 {
            return Err(VegasLatticeError::NegativeSize);
        }
        Ok(self)
    }

    /// Drops all the vertices that are periodic along the given axis
    fn drop_along(mut self, axis: Axis) -> Self {
        self.vertices.retain(|v| {
            let delta = v.delta();
            match axis {
                Axis::X => delta.0 == 0,
                Axis::Y => delta.1 == 0,
                Axis::Z => delta.2 == 0,
            }
        });
        self
    }

    /// Drop periodic boundary conditions along the x axis
    pub fn drop_x(self) -> Self {
        self.drop_along(Axis::X)
    }

    /// Drop periodic boundary conditions along the y axis
    pub fn drop_y(self) -> Self {
        self.drop_along(Axis::Y)
    }

    /// Drop periodic boundary conditions along the z axis
    pub fn drop_z(self) -> Self {
        self.drop_along(Axis::Z)
    }

    /// Drop periodic boundary conditions along all axes
    pub fn drop_all(self) -> Self {
        self.drop_x().drop_y().drop_z()
    }

    #[inline]
    fn size_along(&self, axis: Axis) -> f64 {
        match axis {
            Axis::X => self.size.0,
            Axis::Y => self.size.1,
            Axis::Z => self.size.2,
        }
    }

    /// Expands the lattice along the given axis
    fn expand_along(mut self, axis: Axis, amount: usize) -> Self {
        let size = self.size_along(axis);
        let n_sites = self.sites.len();
        let n_vertices = self.vertices.len();

        self.sites = (0..amount)
            .flat_map(|i| repeat(i).take(n_sites))
            .zip(self.sites().iter().cycle())
            .map(|(index, site)| site.clone().move_along(axis, (index as f64) * size))
            .collect();

        self.vertices = (0..amount)
            .flat_map(|i| repeat(i).take(n_vertices))
            .zip(self.vertices.iter().cycle())
            .map(|(index, vertex)| match axis {
                Axis::X => vertex.clone().move_x(index, n_sites, amount),
                Axis::Y => vertex.clone().move_y(index, n_sites, amount),
                Axis::Z => vertex.clone().move_z(index, n_sites, amount),
            })
            .collect();

        match axis {
            Axis::X => self.size.0 *= amount as f64,
            Axis::Y => self.size.1 *= amount as f64,
            Axis::Z => self.size.2 *= amount as f64,
        }

        self
    }

    /// Expand lattice along the x axis
    pub fn expand_x(self, amount: usize) -> Self {
        self.expand_along(Axis::X, amount)
    }

    /// Expand lattice along the y axis
    pub fn expand_y(self, amount: usize) -> Self {
        self.expand_along(Axis::Y, amount)
    }

    /// Expand lattice along the z axis
    pub fn expand_z(self, amount: usize) -> Self {
        self.expand_along(Axis::Z, amount)
    }

    /// Expand lattice by the same ammount along all axes
    pub fn expand_all(self, amount: usize) -> Self {
        self.expand_x(amount).expand_y(amount).expand_z(amount)
    }

    /// Expand lattice by the given ammount along all axes
    pub fn expand(self, x: usize, y: usize, z: usize) -> Self {
        self.expand_x(x).expand_y(y).expand_z(z)
    }

    /// Removes sites from the lattice according to the given mask
    ///
    /// TODO: This only removes points in the xy plane, and it should be generalized
    pub fn apply_mask(mut self, mask: Mask) -> Self {
        let mut rng = thread_rng();
        let site_mask: Vec<_> = self
            .sites
            .iter()
            .map(|s| {
                let (x, y, _) = s.position();
                mask.keep(x, y, &mut rng)
            })
            .collect();
        let mut counter = 0;
        let new_indices: Vec<_> = (0..self.sites.len())
            .map(|i| {
                if site_mask[i] {
                    counter += 1;
                    counter - 1
                } else {
                    i
                }
            })
            .collect();
        self.sites = self
            .sites
            .into_iter()
            .enumerate()
            .filter(|&(i, ref _s)| site_mask[i])
            .map(|(_i, s)| s)
            .collect();
        self.vertices = self
            .vertices
            .into_iter()
            .filter(|v| site_mask[v.source()] && site_mask[v.target()])
            .map(|v| v.reindex(&new_indices))
            .collect();
        self
    }

    /// Replaces the sites labeled as `source` with sites in the `target` alloy
    pub fn alloy_sites(mut self, source: &str, target: Alloy) -> Self {
        let mut rng = thread_rng();
        self.sites = self
            .sites
            .into_iter()
            .map(|site| {
                if site.kind() != source {
                    site
                } else {
                    site.with_kind(target.pick(&mut rng))
                }
            })
            .collect();
        self
    }
}

impl FromStr for Lattice {
    type Err = VegasLatticeError;
    fn from_str(source: &str) -> Result<Lattice, Self::Err> {
        let lattice: Lattice = serde_json::from_str(source)?;
        lattice.validate()
    }
}

#[cfg(test)]
mod test {
    use crate::{Lattice, Site, Vertex};

    #[test]
    fn drop_example() {
        let lattice = Lattice::new((1.0, 1.0, 1.0))
            .with_sites(vec![Site::new("Fe")])
            .with_vertices(vec![Vertex::new(0, 0, (0, 0, 1))]);
        let lattice = lattice.drop_x();
        assert!(lattice.vertices.len() == 1);
    }

    #[test]
    fn drop_example_actually_dropping() {
        let lattice = Lattice::new((1.0, 1.0, 1.0))
            .with_sites(vec![Site::new("Fe")])
            .with_vertices(vec![Vertex::new(0, 0, (0, 0, 1))]);
        let lattice = lattice.drop_z();
        assert!(lattice.vertices.is_empty());
    }

    #[test]
    fn single_lattice_expansion_1d() {
        let lattice = Lattice::new((1.0, 1.0, 1.0)).with_sites(vec![Site::new("Fe")]);
        let output = lattice.expand_x(2);
        assert_eq!(output.sites.len(), 2);
        assert!((output.sites[1].position().0 - 1.0).abs() < 1e-10);
    }

    #[test]
    fn double_lattice_expansion_1d() {
        let lattice = Lattice::new((1.0, 1.0, 1.0)).with_sites(vec![Site::new("Fe")]);
        let lattice = lattice.expand_x(2);
        let output = lattice.expand_x(2);
        assert_eq!(output.sites.len(), 4);
        assert!((output.sites[1].position().0 - 1.0).abs() < 1e-10);
        assert!((output.sites[2].position().0 - 2.0).abs() < 1e-10);
        assert!((output.sites[3].position().0 - 3.0).abs() < 1e-10);
    }

    #[test]
    fn single_lattice_expansion_1d_vertices() {
        let lattice = Lattice::new((1.0, 1.0, 1.0))
            .with_sites(vec![Site::new("Fe")])
            .with_vertices(vec![Vertex::new(0, 0, (1, 0, 0))]);
        let output = lattice.expand_x(2);
        assert_eq!(output.vertices.len(), 2);
        assert_eq!(output.vertices[0].source(), 0);
        assert_eq!(output.vertices[0].target(), 1);
        assert_eq!(output.vertices[0].delta().0, 0);
        assert_eq!(output.vertices[1].source(), 1);
        assert_eq!(output.vertices[1].target(), 0);
        assert_eq!(output.vertices[1].delta().0, 1);
    }

    #[test]
    fn single_lattice_expansion_1d_negative_vertices() {
        let lattice = Lattice::new((1.0, 1.0, 1.0))
            .with_sites(vec![Site::new("Fe")])
            .with_vertices(vec![Vertex::new(0, 0, (-1, 0, 0))]);
        let output = lattice.expand_x(2);
        assert_eq!(output.vertices.len(), 2);
        assert_eq!(output.vertices[0].source(), 0);
        assert_eq!(output.vertices[0].target(), 1);
        assert_eq!(output.vertices[0].delta().0, -1);
        assert_eq!(output.vertices[1].source(), 1);
        assert_eq!(output.vertices[1].target(), 0);
        assert_eq!(output.vertices[1].delta().0, 0);
    }

    #[test]
    fn test_lattice_can_be_read_from_string() {
        let lattice = r#"{
            "size": [1.0, 1.0, 1.0],
            "sites": [
                {
                    "kind": "Fe",
                    "position": [0.0, 0.0, 0.0]
                }
            ],
            "vertices": [
                {
                    "source": 0,
                    "target": 0,
                    "delta": [0, 0, 1]
                }
            ]
        }"#;
        let lattice: Lattice = lattice.parse().unwrap();
        assert_eq!(lattice.size(), (1.0, 1.0, 1.0));
        assert_eq!(lattice.sites().len(), 1);
        assert_eq!(lattice.vertices().len(), 1);
    }
}