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
use std::{collections::HashSet, fmt::Display};

use super::custom_data_type::FractionalCoordRange;
use super::Atom;

mod lattice_vectors;
mod reciprocal_space;

pub use lattice_vectors::LatticeVectors;

#[derive(Debug, Clone)]
pub struct BasicLatticeModel {
    pub(crate) lattice_vectors: Option<LatticeVectors>,
    pub(crate) atoms: Vec<Atom>,
}

impl BasicLatticeModel {
    pub fn new(lattice_vectors: &Option<LatticeVectors>, atoms: &[Atom]) -> Self {
        BasicLatticeModel {
            lattice_vectors: lattice_vectors.clone(),
            atoms: atoms.to_vec(),
        }
    }
    pub fn number_of_atoms(&self) -> usize {
        self.atoms.len()
    }
    // pub fn builder() -> LatticeModelBuilder<T, Pending> {
    //     LatticeModelBuilder::<T, Pending>::new()
    // }
    pub fn lattice_vectors(&self) -> Option<&LatticeVectors> {
        self.lattice_vectors.as_ref()
    }

    pub fn atoms(&self) -> &[Atom] {
        &self.atoms
    }

    pub fn atoms_mut(&mut self) -> &mut Vec<Atom> {
        &mut self.atoms
    }

    pub fn append_atom(&mut self, atoms: &mut Vec<Atom>) {
        self.atoms.append(atoms)
    }

    pub fn lattice_vectors_mut(&mut self) -> &mut Option<LatticeVectors> {
        &mut self.lattice_vectors
    }

    pub fn set_lattice_vectors(&mut self, lattice_vectors: Option<LatticeVectors>) {
        self.lattice_vectors = lattice_vectors;
    }

    pub fn set_atoms(&mut self, atoms: Vec<Atom>) {
        self.atoms = atoms;
    }
    pub fn element_list(&self) -> Vec<String> {
        let all_atom_elements = self
            .atoms
            .iter()
            .map(|atom| (atom.atomic_number(), atom.symbol()))
            .collect::<Vec<(u8, &str)>>()
            .drain(..)
            .collect::<HashSet<(u8, &str)>>();
        let mut element_list: Vec<(u8, &str)> = all_atom_elements.into_iter().collect();
        element_list.sort_by(|a, b| a.0.cmp(&b.0));
        element_list
            .iter()
            .map(|(_, symbol)| symbol.to_string())
            .collect()
    }
    /// Select atoms by the given ranges of x, y, z in fractional coordinates
    pub fn xyz_range_filter(
        &self,
        x_range: FractionalCoordRange,
        y_range: FractionalCoordRange,
        z_range: FractionalCoordRange,
    ) -> Vec<Atom> {
        self.atoms
            .iter()
            .filter(|&atom| {
                let frac_coord = self.lattice_vectors.as_ref().unwrap().mat_cart_to_frac()
                    * atom.cartesian_coord();
                if x_range.is_in_range(frac_coord.x)
                    && y_range.is_in_range(frac_coord.y)
                    && z_range.is_in_range(frac_coord.z)
                {
                    true
                } else {
                    false
                }
            })
            .cloned()
            .collect::<Vec<Atom>>()
    }
}

impl Display for BasicLatticeModel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let lattice_vector_disp = if let Some(v) = &self.lattice_vectors {
            format!("{}", v)
        } else {
            "N/A".to_string()
        };
        let atoms_text_vec: Vec<String> = self.atoms.iter().map(|atom| format!("{atom}")).collect();
        let atoms_text = atoms_text_vec.join("\n");
        write!(
            f,
            "Lattice Vectors: {}\n{}",
            lattice_vector_disp, atoms_text
        )
    }
}