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
use crate::{DelaunayReduceError, NiggliReduceError, StandardizeError};
use spglib_sys as ffi;
use std::os::raw::c_int;
#[derive(Clone, Debug)]
pub enum StandardizeOps {
Ideal,
Primitive,
Both,
}
#[derive(Clone, Debug, Default)]
pub struct Cell {
pub lattice: [[f64; 3]; 3],
pub positions: Vec<[f64; 3]>,
pub types: Vec<i32>,
}
impl Cell {
pub fn new(lattice: [[f64; 3]; 3], positions: &[[f64; 3]], types: &[i32]) -> Cell {
if positions.len() != types.len() {
panic!("number of positions must match number of types")
}
Cell {
lattice,
positions: positions.to_owned(),
types: types.to_owned(),
}
}
pub fn standardize(
&mut self,
std_ops: StandardizeOps,
symprec: f64,
) -> Result<(), StandardizeError> {
let (to_primitive, no_idealize) = match std_ops {
StandardizeOps::Ideal => (0, 0),
StandardizeOps::Primitive => (1, 1),
StandardizeOps::Both => (1, 0),
};
let res = unsafe {
ffi::spg_standardize_cell(
self.lattice.as_ptr() as *mut [f64; 3],
self.positions.as_ptr() as *mut [f64; 3],
self.types.as_ptr() as *mut c_int,
self.positions.len() as c_int,
to_primitive,
no_idealize,
symprec,
)
};
if res == 0 {
return Err(StandardizeError);
}
Ok(())
}
pub fn delaunay_reduce(&mut self, symprec: f64) -> Result<(), DelaunayReduceError> {
let res =
unsafe { ffi::spg_delaunay_reduce(self.lattice.as_ptr() as *mut [f64; 3], symprec) };
if res == 0 {
return Err(DelaunayReduceError);
}
Ok(())
}
pub fn niggli_reduce(&mut self, symprec: f64) -> Result<(), NiggliReduceError> {
let res =
unsafe { ffi::spg_niggli_reduce(self.lattice.as_ptr() as *mut [f64; 3], symprec) };
if res == 0 {
return Err(NiggliReduceError);
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use crate::cell::StandardizeOps;
use crate::internal::{bcc_cell, corrundum_cell, distorted_bcc_cell, distorted_corrundum_cell};
#[test]
fn find_primitive() {
let symprec = 1e-5;
let mut cell = bcc_cell();
cell.standardize(StandardizeOps::Primitive, symprec)
.unwrap();
let mut cell = corrundum_cell();
cell.standardize(StandardizeOps::Primitive, symprec)
.unwrap();
}
#[test]
fn find_ideal() {
let symprec = 1e-5;
let mut cell = distorted_bcc_cell();
cell.standardize(StandardizeOps::Ideal, symprec).unwrap();
let mut cell = distorted_corrundum_cell();
cell.standardize(StandardizeOps::Ideal, symprec).unwrap();
}
#[test]
fn standardize() {
let symprec = 1e-5;
let mut cell = distorted_bcc_cell();
cell.standardize(StandardizeOps::Both, symprec).unwrap();
let mut cell = distorted_corrundum_cell();
cell.standardize(StandardizeOps::Both, symprec).unwrap()
}
}