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
#[cfg(test)]
extern crate tempfile;
#[macro_use]
extern crate failure;
extern crate lazy_init;
pub mod trajectory;
pub mod xdr;
use std::fmt;
use failure::Fail;
#[derive(Debug, Fail, Clone)]
#[fail(display="Trajectory Error while {}: {}", operation, code)]
pub struct TrajectoryError {
pub operation: String,
pub code: u32,
}
#[derive(Clone)]
pub struct Frame {
pub num_atoms: u32,
pub step: u32,
pub time: f32,
pub box_vector: [[f32; 3usize]; 3usize],
pub coords: Vec<[f32; 3usize]>,
}
impl Default for Frame {
fn default() -> Frame {
return Frame {
num_atoms: 0,
step: 0,
time: 0.0,
box_vector: [[0.0; 3]; 3],
coords: Vec::with_capacity(0)
};
}
}
impl fmt::Debug for Frame {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Frame {{ atoms: {}, step: {}, time: {}, \
box: {:?}, coords: {:?} }}", self.num_atoms, self.step, self.time,
self.box_vector, self.coords)
}
}
impl Frame {
pub fn new() -> Frame {
return Frame{ ..Default::default() };
}
pub fn with_capacity(num_atoms: u32) -> Frame {
return Frame {
num_atoms: num_atoms,
coords: vec![[0.0, 0.0, 0.0]; num_atoms as usize],
..Default::default()
};
}
pub fn filter_coords(self: &mut Frame, indeces: &[usize]) {
self.coords = self.coords.iter()
.map(|elem| elem.clone())
.enumerate()
.filter(|&(i, _)| indeces.contains(&i))
.map(|(_, elem)| elem)
.collect();
self.num_atoms = self.coords.len() as u32;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_frame_with_capacity() {
let frame = Frame::with_capacity(10);
println!("{:?}", frame.coords);
assert_eq!(frame.coords.len(), 10);
}
#[test]
fn test_frame_default_capacity() {
let frame = Frame::new();
assert_eq!(frame.coords.len(), 0);
}
#[test]
fn test_frame_filter_atoms() {
let mut frame = Frame::with_capacity(3);
frame.coords[0] = [1.0, 2.0, 3.0];
frame.coords[1] = [4.0, 5.0, 6.0];
frame.coords[2] = [7.0, 8.0, 9.0];
let filter: Vec<usize> = vec![1,2];
let mut frame_new = frame.clone();
frame_new.filter_coords(&filter);
assert!(frame_new.num_atoms as usize == filter.len());
assert!(frame_new.coords[0] == frame.coords[1]);
assert!(frame_new.coords[1] == frame.coords[2]);
}
}