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
use gchemol_gut::prelude::*;
use vecfx::*;
mod mic;
mod supercell;
mod utils;
use crate::utils::*;
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
pub struct Lattice {
matrix: Matrix3f,
origin: Vector3f,
inv_matrix: Matrix3f,
}
impl Default for Lattice {
fn default() -> Self {
let matrix = Matrix3f::identity();
let inv_matrix = get_inv_matrix(&matrix);
Lattice {
matrix,
inv_matrix,
origin: Vector3f::zeros(),
}
}
}
impl Lattice {
pub fn new<T: Into<Vector3f> + Copy>(tvs: [T; 3]) -> Self {
let vectors = [tvs[0].into(), tvs[1].into(), tvs[2].into()];
let matrix = Matrix3f::from_columns(&vectors);
Self::from_matrix(matrix)
}
pub fn from_matrix<T: Into<Matrix3f>>(tvs: T) -> Self {
let matrix = tvs.into();
let inv_matrix = get_inv_matrix(&matrix);
Lattice {
matrix,
inv_matrix,
..Default::default()
}
}
pub fn from_params(a: f64, b: f64, c: f64, alpha: f64, beta: f64, gamma: f64) -> Self {
let alpha = alpha.to_radians();
let beta = beta.to_radians();
let gamma = gamma.to_radians();
let acos = alpha.cos();
let bcos = beta.cos();
let gcos = gamma.cos();
let gsin = gamma.sin();
let v = (1. - acos.powi(2) - bcos.powi(2) - gcos.powi(2) + 2.0 * acos * bcos * gcos).sqrt();
let va = [a, 0.0, 0.0];
let vb = [b * gcos, b * gsin, 0.0];
let vc = [c * bcos, c * (acos - bcos * gcos) / gsin, c * v / gsin];
Lattice::new([va, vb, vc])
}
pub fn widths(&self) -> [f64; 3] {
let volume = self.volume();
let [van, vbn, vcn] = self.lengths();
let wa = volume / (vbn * vcn);
let wb = volume / (vcn * van);
let wc = volume / (van * vbn);
[wa, wb, wc]
}
pub fn volume(&self) -> f64 {
get_cell_volume(self.matrix)
}
pub fn set_origin<T: Into<Vector3f>>(&mut self, loc: T) {
self.origin = loc.into()
}
pub fn lengths(&self) -> [f64; 3] {
get_cell_lengths(self.matrix).into()
}
pub fn angles(&self) -> [f64; 3] {
get_cell_angles(self.matrix).into()
}
pub fn scale_by(&mut self, v: f64) {
assert!(v.is_sign_positive(), "invalid scale factor: {v}");
self.matrix *= v;
self.inv_matrix = get_inv_matrix(&self.matrix);
}
pub fn scale_by_a(&mut self, v: f64) {
self.scale_by_abc(v, 0)
}
pub fn scale_by_b(&mut self, v: f64) {
self.scale_by_abc(v, 1)
}
pub fn scale_by_c(&mut self, v: f64) {
self.scale_by_abc(v, 2)
}
fn scale_by_abc(&mut self, v: f64, i: usize) {
assert!(v.is_sign_positive(), "invalid scale factor: {v}");
let mut x = self.matrix.column_mut(i);
x *= v;
self.inv_matrix = get_inv_matrix(&self.matrix);
}
pub fn origin(&self) -> Vector3f {
self.origin
}
pub fn to_frac<T: Into<Vector3f>>(&self, p: T) -> Vector3f {
self.inv_matrix * (p.into() - self.origin)
}
pub fn to_cart<T: Into<Vector3f>>(&self, p: T) -> Vector3f {
self.matrix * p.into() + self.origin
}
pub fn vector_a(&self) -> Vector3f {
self.matrix.column(0).into()
}
pub fn vector_b(&self) -> Vector3f {
self.matrix.column(1).into()
}
pub fn vector_c(&self) -> Vector3f {
self.matrix.column(2).into()
}
pub fn vectors(&self) -> [Vector3f; 3] {
[self.vector_a(), self.vector_b(), self.vector_c()]
}
pub fn matrix(&self) -> Matrix3f {
self.matrix
}
pub fn inv_matrix(&self) -> Matrix3f {
self.inv_matrix
}
pub fn is_orthorhombic(&self) -> bool {
let diag = self.matrix.diagonal();
let m = Matrix3f::from_diagonal(&diag);
m == self.matrix
}
pub fn wrap<T: Into<Vector3f>>(&self, vec: T) -> Vector3f {
let f = self.to_frac(vec);
let fcoords_wrapped = self.wrap_frac(f);
self.to_cart(fcoords_wrapped)
}
pub fn wrap_frac<T: Into<Vector3f>>(&self, f: T) -> Vector3f {
let f = f.into();
let fcoords_wrapped = [f.x - f.x.floor(), f.y - f.y.floor(), f.z - f.z.floor()];
fcoords_wrapped.into()
}
pub fn distance<T: Into<Vector3f>>(&self, pi: T, pj: T) -> f64 {
let p = pj.into() - pi.into();
let pmic = self.apply_mic(p);
pmic.norm()
}
pub fn apply_mic<T: Into<[f64; 3]>>(&self, p: T) -> Vector3f {
let p = p.into();
let v_naive = self.apply_mic_tuckerman(p);
if self.is_orthorhombic() {
v_naive
} else {
let r_max = 0.5 * self.widths().min();
if v_naive.norm() < r_max {
v_naive
} else {
self.apply_mic_brute_force(p)
}
}
}
}