castep-model-core 0.2.9

The core module to parse, read, edit, and write 3D lattice models for castep and compatible with Materials Studio.
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
use crate::{error::InvalidIndex, model_type::ModelInfo, CellModel, MsiModel, Transformation};
use std::{cmp::Ordering, ops::Add};

use na::Point3;

mod atom_builder;
pub mod visitor;

pub use atom_builder::AtomCollectionBuilder;

use self::visitor::VisitCollection;
#[derive(Debug, Clone)]
/// Struct that defines an atom.
pub struct Atom<T: ModelInfo> {
    /// The symbol of the element.
    element_symbol: String,
    /// The atomic number of the element in periodic table.
    atomic_number: u8,
    /// The cartesian coordinate of the atom.
    xyz: Point3<f64>,
    /// The fractional coordinate of the atom in a lattice.
    /// Optional since it relies on lattice vectors.
    fractional_xyz: Option<Point3<f64>>,
    /// The id of the atom in the parsed model.
    atom_id: u32,
    /// Format type
    format_type: T,
}

pub struct AtomView<'a, T: ModelInfo> {
    element_symbol: &'a str,
    atomic_number: &'a u8,
    xyz: &'a Point3<f64>,
    fractional_xyz: Option<&'a Point3<f64>>,
    atom_id: &'a u32,
    format_type: T,
}

impl<'a, T: ModelInfo> AtomView<'a, T> {
    pub fn xyz(&self) -> &Point3<f64> {
        self.xyz
    }

    pub fn element_symbol(&self) -> &str {
        self.element_symbol
    }

    pub fn atomic_number(&self) -> &u8 {
        self.atomic_number
    }

    pub fn fractional_xyz(&self) -> Option<&Point3<f64>> {
        self.fractional_xyz
    }

    pub fn atom_id(&self) -> &u32 {
        self.atom_id
    }
}

impl<'a, T: ModelInfo> From<AtomView<'a, T>> for Atom<T> {
    fn from(src: AtomView<'a, T>) -> Self {
        Self {
            element_symbol: src.element_symbol().into(),
            atomic_number: *src.atomic_number(),
            xyz: src.xyz().to_owned(),
            fractional_xyz: src.fractional_xyz().copied(),
            atom_id: *src.atom_id(),
            format_type: T::default(),
        }
    }
}

#[derive(Debug, Clone, Default)]
/// Struct of `Atom` as data-driven design.
pub struct AtomCollection<T: ModelInfo> {
    element_symbols: Vec<String>,
    atomic_nums: Vec<u8>,
    xyz_coords: Vec<Point3<f64>>,
    fractional_xyz: Vec<Option<Point3<f64>>>,
    atom_ids: Vec<u32>,
    size: usize,
    format_type: T,
}

impl<T: ModelInfo> AtomCollection<T> {
    /// Update the `element_symbol` at the given index.
    /// # Errors
    /// This function will return an error if the index is out of bounds.
    pub fn update_symbol_at(&mut self, index: usize, new_symbol: &str) -> Result<(), InvalidIndex> {
        *self.element_symbols.get_mut(index).ok_or(InvalidIndex)? = new_symbol.into();
        Ok(())
    }
    /// Update the `element_id` at the given index.
    /// # Errors
    /// This function will return an error if the index is out of bounds.
    pub fn update_elm_id_at(&mut self, index: usize, new_elm_id: u8) -> Result<(), InvalidIndex> {
        *self.atomic_nums.get_mut(index).ok_or(InvalidIndex)? = new_elm_id;
        Ok(())
    }
    /// Update the `xyz` at the given index.
    /// # Errors
    /// This function will return an error if the index is out of bounds.
    pub fn update_xyz_at(
        &mut self,
        index: usize,
        new_xyz: Point3<f64>,
    ) -> Result<(), InvalidIndex> {
        *self.xyz_coords.get_mut(index).ok_or(InvalidIndex)? = new_xyz;
        Ok(())
    }
    /// Update the `fractional_xyz` at the given index.
    /// # Errors
    /// This function will return an error if the index is out of bounds.
    pub fn update_frac_xyz_at(
        &mut self,
        index: usize,
        new_frac: Option<Point3<f64>>,
    ) -> Result<(), InvalidIndex> {
        *self.fractional_xyz.get_mut(index).ok_or(InvalidIndex)? = new_frac;
        Ok(())
    }
    /// Update the `atom_id` at the given index.
    /// # Errors
    /// This function will return an error if the index is out of bounds.
    pub fn update_atom_id_at(
        &mut self,
        index: usize,
        new_atom_id: u32,
    ) -> Result<(), InvalidIndex> {
        *self.atom_ids.get_mut(index).ok_or(InvalidIndex)? = new_atom_id;
        Ok(())
    }
    /// Update the whole atom at the given index.
    /// # Errors
    /// This function will return an error if the index is out of bounds.
    pub fn update_atom_at(&mut self, index: usize, new_atom: Atom<T>) -> Result<(), InvalidIndex> {
        let Atom {
            element_symbol,
            atomic_number: element_id,
            xyz,
            fractional_xyz,
            atom_id,
            format_type: _,
        } = new_atom;
        self.update_symbol_at(index, &element_symbol)?;
        self.update_elm_id_at(index, element_id)?;
        self.update_xyz_at(index, xyz)?;
        self.update_frac_xyz_at(index, fractional_xyz)?;
        self.update_atom_id_at(index, atom_id)?;
        Ok(())
    }

    pub fn element_symbols(&self) -> &[String] {
        self.element_symbols.as_ref()
    }

    pub fn atomic_nums(&self) -> &[u8] {
        self.atomic_nums.as_ref()
    }

    pub fn xyz_coords(&self) -> &[Point3<f64>] {
        self.xyz_coords.as_ref()
    }

    pub fn xyz_coords_mut(&mut self) -> &mut [Point3<f64>] {
        self.xyz_coords.as_mut()
    }

    pub fn fractional_xyz(&self) -> &[Option<Point3<f64>>] {
        self.fractional_xyz.as_ref()
    }

    pub fn fractional_xyz_mut(&mut self) -> &mut [Option<Point3<f64>>] {
        self.fractional_xyz.as_mut()
    }

    pub fn atom_ids(&self) -> &[u32] {
        self.atom_ids.as_ref()
    }

    pub fn size(&self) -> usize {
        self.size
    }
}

impl<T: ModelInfo> From<Vec<Atom<T>>> for AtomCollection<T> {
    fn from(src: Vec<Atom<T>>) -> Self {
        let atom_num = src.len();
        let mut output = AtomCollection {
            element_symbols: Vec::with_capacity(atom_num),
            atomic_nums: Vec::with_capacity(atom_num),
            xyz_coords: Vec::with_capacity(atom_num),
            fractional_xyz: Vec::with_capacity(atom_num),
            atom_ids: Vec::with_capacity(atom_num),
            size: atom_num,
            format_type: T::default(),
        };
        for atom in src.into_iter() {
            output.element_symbols.push(atom.element_symbol);
            output.atomic_nums.push(atom.atomic_number);
            output.xyz_coords.push(atom.xyz);
            output.fractional_xyz.push(atom.fractional_xyz);
            output.atom_ids.push(atom.atom_id);
        }
        output
    }
}

impl<'a, T: ModelInfo> From<&'a AtomCollection<T>> for Vec<AtomView<'a, T>> {
    fn from(src: &'a AtomCollection<T>) -> Self {
        (0..src.size)
            .into_iter()
            .map(|i| src.view_atom_at_index(i).unwrap())
            .collect()
    }
}

impl<T: ModelInfo> From<AtomCollection<T>> for Vec<Atom<T>> {
    fn from(src: AtomCollection<T>) -> Self {
        (0..src.size)
            .into_iter()
            .map(|i| -> Atom<T> {
                let view = src.view_atom_at_index(i).unwrap();
                view.into()
            })
            .collect()
    }
}

impl<T: ModelInfo> Add for AtomCollection<T> {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        let size_self = self.size;
        let size_rhs = rhs.size;
        let new_size = size_self + size_rhs;
        AtomCollection {
            element_symbols: vec![self.element_symbols, rhs.element_symbols].concat(),
            atomic_nums: vec![self.atomic_nums, rhs.atomic_nums].concat(),
            xyz_coords: vec![self.xyz_coords, rhs.xyz_coords].concat(),
            fractional_xyz: vec![self.fractional_xyz, rhs.fractional_xyz].concat(),
            atom_ids: vec![self.atom_ids, rhs.atom_ids].concat(),
            size: new_size,
            format_type: T::default(),
        }
    }
}

impl<T> Atom<T>
where
    T: ModelInfo,
{
    /// Creates a new [`Atom`].
    pub fn new(element_symbol: String, atomic_number: u8, xyz: Point3<f64>, atom_id: u32) -> Self {
        Self {
            element_symbol,
            atomic_number,
            xyz,
            fractional_xyz: None,
            atom_id,
            format_type: T::default(),
        }
    }

    /// Returns a reference to the element symbol of this [`Atom<Format>`].
    pub fn element_symbol(&self) -> &str {
        self.element_symbol.as_ref()
    }
    /// Sets the element symbol of this [`Atom<Format>`].
    pub fn set_element_symbol(&mut self, element_symbol: String) {
        self.element_symbol = element_symbol;
    }

    /// Returns the element id of this [`Atom<Format>`].
    pub fn element_id(&self) -> u8 {
        self.atomic_number
    }
    /// Sets the element id of this [`Atom<Format>`].
    pub fn set_element_id(&mut self, atomic_number: u8) {
        self.atomic_number = atomic_number;
    }

    /// Returns a reference to the xyz of this [`Atom<Format>`].
    pub fn xyz(&self) -> &Point3<f64> {
        &self.xyz
    }

    /// Sets the xyz of this [`Atom<Format>`].
    pub fn set_xyz(&mut self, xyz: Point3<f64>) {
        self.xyz = xyz;
    }

    /// Returns the atom id of this [`Atom<Format>`].
    pub fn atom_id(&self) -> u32 {
        self.atom_id
    }
    /// Sets the atom id of this [`Atom<Format>`].
    pub fn set_atom_id(&mut self, atom_id: u32) {
        self.atom_id = atom_id;
    }

    pub fn fractional_xyz(&self) -> Option<&Point3<f64>> {
        self.fractional_xyz.as_ref()
    }

    pub fn set_fractional_xyz(&mut self, fractional_xyz: Option<Point3<f64>>) {
        self.fractional_xyz = fractional_xyz;
    }
}

// impl Export for Vec<Atom> {
//     fn format_output(&self) -> String {
//         let atom_strings: Vec<String> = self.iter().map(|atom| atom.format_output()).collect();
//         atom_strings.concat()
//     }
// }

impl<T> Transformation for Atom<T>
where
    T: ModelInfo,
{
    fn rotate(&mut self, rotate_quatd: &na::UnitQuaternion<f64>) {
        self.set_xyz(rotate_quatd.transform_point(self.xyz()))
    }

    fn translate(&mut self, translate_matrix: &na::Translation<f64, 3>) {
        self.set_xyz(translate_matrix.transform_point(self.xyz()))
    }
}

impl<T> Transformation for AtomCollection<T>
where
    T: ModelInfo,
{
    fn rotate(&mut self, rotate_quatd: &na::UnitQuaternion<f64>) {
        self.xyz_coords
            .iter_mut()
            .for_each(|point| *point = rotate_quatd.transform_point(point))
    }

    fn translate(&mut self, translate_matrix: &na::Translation<f64, 3>) {
        self.xyz_coords
            .iter_mut()
            .for_each(|point| *point = translate_matrix.transform_point(point))
    }
}

impl From<AtomCollection<MsiModel>> for AtomCollection<CellModel> {
    fn from(src: AtomCollection<MsiModel>) -> Self {
        let AtomCollection {
            element_symbols,
            atomic_nums: element_ids,
            xyz_coords,
            fractional_xyz,
            atom_ids,
            size,
            format_type: _,
        } = src;
        Self {
            element_symbols,
            atomic_nums: element_ids,
            xyz_coords,
            fractional_xyz,
            atom_ids,
            size,
            format_type: CellModel::default(),
        }
    }
}

impl<T: ModelInfo> AsRef<AtomCollection<T>> for &AtomCollection<T> {
    fn as_ref(&self) -> &AtomCollection<T> {
        self
    }
}

impl<T> Ord for Atom<T>
where
    T: ModelInfo,
{
    fn cmp(&self, other: &Self) -> Ordering {
        self.atom_id.cmp(&other.atom_id)
    }
}

impl<T> PartialOrd for Atom<T>
where
    T: ModelInfo,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<T> PartialEq for Atom<T>
where
    T: ModelInfo,
{
    fn eq(&self, other: &Self) -> bool {
        self.atom_id == other.atom_id
    }
}

impl<T> Eq for Atom<T> where T: ModelInfo {}