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
#![allow(dead_code)]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct UnitCell {
a: f64,
b: f64,
c: f64,
alpha: f64,
beta: f64,
gamma: f64,
}
impl UnitCell {
pub fn new(a: f64, b: f64, c: f64, alpha: f64, beta: f64, gamma: f64) -> UnitCell {
UnitCell {
a,
b,
c,
alpha,
beta,
gamma,
}
}
pub fn a(&self) -> f64 {
self.a
}
pub fn b(&self) -> f64 {
self.b
}
pub fn c(&self) -> f64 {
self.c
}
pub fn set_a(&mut self, new_a: f64) {
if !new_a.is_finite() {
panic!("The new a value of this UnitCell is not finite");
}
self.a = new_a;
}
pub fn set_b(&mut self, new_b: f64) {
if !new_b.is_finite() {
panic!("The new b value of this UnitCell is not finite");
}
self.b = new_b;
}
pub fn set_c(&mut self, new_c: f64) {
if !new_c.is_finite() {
panic!("The new c value of this UnitCell is not finite");
}
self.c = new_c;
}
pub fn alpha(&self) -> f64 {
self.alpha
}
pub fn set_alpha(&mut self, new_alpha: f64) {
if !new_alpha.is_finite() {
panic!("The new alpha value of this UnitCell is not finite");
}
if !(0.0..360.0).contains(&new_alpha) {
panic!("The new alpha value of this UnitCell is out of bounds [0, 360).")
}
self.alpha = new_alpha;
}
pub fn beta(&self) -> f64 {
self.beta
}
pub fn set_beta(&mut self, new_beta: f64) {
if !new_beta.is_finite() {
panic!("The new beta value of this UnitCell is not finite");
}
if !(0.0..360.0).contains(&new_beta) {
panic!("The new beta value of this UnitCell is out of bounds [0, 360).")
}
self.beta = new_beta;
}
pub fn gamma(&self) -> f64 {
self.gamma
}
pub fn set_gamma(&mut self, new_gamma: f64) {
if !new_gamma.is_finite() {
panic!("The new gamma value of this UnitCell is not finite");
}
if !(0.0..360.0).contains(&new_gamma) {
panic!("The new gamma value of this UnitCell is out of bounds [0, 360).")
}
self.gamma = new_gamma;
}
pub fn size(&self) -> (f64, f64, f64) {
(self.a, self.b, self.c)
}
}
impl Default for UnitCell {
fn default() -> Self {
Self::new(0.0, 0.0, 0.0, 90.0, 90.0, 90.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn equality() {
let a = UnitCell::new(10.0, 10.0, 15.0, 90.0, 90.0, 87.0);
let b = UnitCell::new(10.0, 10.0, 15.0, 90.0, 90.0, 87.0);
let c = UnitCell::new(12.0, 10.0, 15.0, 90.0, 90.0, 87.0);
assert_eq!(a, b);
assert_ne!(a, c);
}
}