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
#![allow(dead_code)]
use super::unit_cell::*;
use crate::reference_tables;
use crate::transformation::*;
use std::cmp::Ordering;
#[derive(Debug, Clone)]
pub struct Symmetry {
index: usize,
}
impl Symmetry {
pub fn new(symbol: &str) -> Option<Self> {
reference_tables::get_index_for_symbol(symbol.trim()).map(|index| Symmetry { index })
}
pub fn from_index(index: usize) -> Option<Self> {
if reference_tables::get_herman_mauguin_symbol_for_index(index).is_some() {
Some(Symmetry { index })
} else {
None
}
}
pub fn herman_mauguin_symbol(&self) -> &str {
reference_tables::get_herman_mauguin_symbol_for_index(self.index)
.expect("An invalid index was present in the definition of this symmetry")
}
pub fn hall_symbol(&self) -> &str {
reference_tables::get_hall_symbol_for_index(self.index)
.expect("An invalid index was present in the definition of this symmetry")
}
#[allow(clippy::unwrap_used)]
pub fn z(&self) -> usize {
reference_tables::get_transformation(self.index)
.unwrap()
.len()
+ 1
}
pub fn index(&self) -> usize {
self.index
}
#[allow(clippy::unwrap_used)]
pub fn transformations(&self) -> Vec<TransformationMatrix> {
let matrices = reference_tables::get_transformation(self.index).unwrap();
let mut output = Vec::with_capacity(matrices.len() + 1);
output.push(TransformationMatrix::identity());
for matrix in matrices {
output.push(TransformationMatrix::from_matrix(*matrix));
}
output
}
#[allow(clippy::unwrap_used)]
pub fn transformations_absolute(&self, unit_cell: &UnitCell) -> Vec<TransformationMatrix> {
let matrices = reference_tables::get_transformation(self.index).unwrap();
let mut output = Vec::with_capacity(matrices.len() + 1);
output.push(TransformationMatrix::identity());
for matrix in matrices {
let mut ma = TransformationMatrix::from_matrix(*matrix);
ma.multiply_translation(unit_cell.size());
output.push(ma);
}
output
}
}
impl PartialEq for Symmetry {
fn eq(&self, other: &Self) -> bool {
self.index == other.index
}
}
impl Eq for Symmetry {}
impl Ord for Symmetry {
fn cmp(&self, other: &Self) -> Ordering {
self.index.cmp(&other.index)
}
}
impl PartialOrd for Symmetry {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[cfg(test)]
mod tests {
use super::Symmetry;
#[test]
#[allow(clippy::unwrap_used)]
fn both_creations() {
let a = Symmetry::new("P 21 21 21").unwrap();
let b = Symmetry::from_index(19).unwrap();
assert_eq!(a, b);
assert_eq!(a.z(), a.transformations().len());
assert_eq!(
4,
a.transformations_absolute(&crate::UnitCell::new(1.0, 1.0, 1.0, 90.0, 90.0, 90.0))
.len()
);
}
#[test]
#[allow(clippy::unwrap_used)]
fn symbol_invariant() {
let a = Symmetry::new("P 21 21 21").unwrap();
assert_eq!(a.herman_mauguin_symbol(), "P 21 21 21");
assert_eq!(a.hall_symbol(), "P 2ac 2ab");
assert_eq!(a.index(), 19);
}
}