Skip to content

Commit

Permalink
Merge pull request #40 from spglib/operation-iterator
Browse files Browse the repository at this point in the history
Refactor to add `Operations.iter()`
  • Loading branch information
lan496 authored Dec 27, 2024
2 parents 4d22e6b + d411929 commit dea96f4
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 69 deletions.
6 changes: 6 additions & 0 deletions moyo/src/base/operation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::{HashSet, VecDeque};
use std::iter::Zip;
use std::ops::Mul;
use std::slice::Iter;

use nalgebra::base::{Matrix3, Vector3};

Expand Down Expand Up @@ -43,6 +45,10 @@ impl Operations {
.map(|r| lattice.basis * r.map(|e| e as f64) * inv_basis)
.collect()
}

pub fn iter(&self) -> Zip<Iter<Rotation>, Iter<Translation>> {
self.rotations.iter().zip(self.translations.iter())
}
}

#[derive(Debug, PartialEq, Eq, Clone, Hash)]
Expand Down
18 changes: 3 additions & 15 deletions moyo/src/base/transformation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ impl UnimodularTransformation {
pub fn transform_operations(&self, operations: &Operations) -> Operations {
let mut new_rotations = vec![];
let mut new_translations = vec![];
for (rotation, translation) in operations
.rotations
.iter()
.zip(operations.translations.iter())
{
for (rotation, translation) in operations.iter() {
let new_rotation = self.linear_inv * rotation * self.linear;
let new_translation = self.linear_inv.map(|e| e as f64)
* (rotation.map(|e| e as f64) * self.origin_shift + translation
Expand Down Expand Up @@ -141,11 +137,7 @@ impl Transformation {
pub fn transform_operations(&self, operations: &Operations) -> Operations {
let mut new_rotations = vec![];
let mut new_translations = vec![];
for (rotation, translation) in operations
.rotations
.iter()
.zip(operations.translations.iter())
{
for (rotation, translation) in operations.iter() {
if let Some((new_rotation, new_translation)) = transform_operation_as_f64(
rotation,
translation,
Expand All @@ -165,11 +157,7 @@ impl Transformation {
pub fn inverse_transform_operations(&self, operations: &Operations) -> Operations {
let mut new_rotations = vec![];
let mut new_translations = vec![];
for (rotation, translation) in operations
.rotations
.iter()
.zip(operations.translations.iter())
{
for (rotation, translation) in operations.iter() {
if let Some((new_rotation, new_translation)) = transform_operation_as_f64(
rotation,
translation,
Expand Down
7 changes: 1 addition & 6 deletions moyo/src/data/hall_symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,7 @@ impl HallSymbol {
rotations.push(rotation_lhs);
translations.push(translation_lhs);

for (rotation_rhs, translation_rhs) in self
.generators
.rotations
.iter()
.zip(self.generators.translations.iter())
{
for (rotation_rhs, translation_rhs) in self.generators.iter() {
let new_rotation = rotation_lhs * rotation_rhs;
let new_translation =
rotation_lhs.map(|e| e as f64) * translation_rhs + translation_lhs;
Expand Down
31 changes: 5 additions & 26 deletions moyo/src/identify/space_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,7 @@ fn match_origin_shift(
let new_prim_operations =
UnimodularTransformation::from_linear(*trans_mat).transform_operations(prim_operations);
let mut hm_translations = HashMap::new();
for (rotation, translation) in new_prim_operations
.rotations
.iter()
.zip(new_prim_operations.translations.iter())
{
for (rotation, translation) in new_prim_operations.iter() {
hm_translations.insert(*rotation, *translation);
}

Expand All @@ -186,12 +182,7 @@ fn match_origin_shift(
// <-> (R - E) * s = t_db - t_target (mod 1)
let mut a = OMatrix::<i32, Dyn, U3>::zeros(3 * db_prim_generators.rotations.len());
let mut b = OVector::<f64, Dyn>::zeros(3 * db_prim_generators.rotations.len());
for (k, (rotation, other_translation)) in db_prim_generators
.rotations
.iter()
.zip(db_prim_generators.translations.iter())
.enumerate()
{
for (k, (rotation, other_translation)) in db_prim_generators.iter().enumerate() {
// Correction transformation matrix may not be normalizer of the point group. For example, mm2 -> 2mm
let target_translation = hm_translations.get(rotation)?;

Expand Down Expand Up @@ -297,11 +288,7 @@ mod tests {
UnimodularTransformation::from_linear(*corr).transform_operations(&prim_operations);

let mut hm_translations = HashMap::new();
for (rotation, translation) in corr_prim_operations
.rotations
.iter()
.zip(corr_prim_operations.translations.iter())
{
for (rotation, translation) in corr_prim_operations.iter() {
hm_translations.insert(rotation.clone(), translation.clone());
}
let r = matrix![
Expand Down Expand Up @@ -349,11 +336,7 @@ mod tests {
.inverse_transform_operations(&matched_operations);

let mut hm_translations = HashMap::new();
for (rotation, translation) in matched_prim_operations
.rotations
.iter()
.zip(matched_prim_operations.translations.iter())
{
for (rotation, translation) in matched_prim_operations.iter() {
hm_translations.insert(rotation, translation);
}

Expand All @@ -365,11 +348,7 @@ mod tests {
matched_prim_operations.rotations.len(),
transformed_prim_operations.rotations.len()
);
for (rotation, translation) in transformed_prim_operations
.rotations
.iter()
.zip(transformed_prim_operations.translations.iter())
{
for (rotation, translation) in transformed_prim_operations.iter() {
assert!(hm_translations.contains_key(rotation));
let mut diff = *hm_translations.get(rotation).unwrap() - translation;
diff -= diff.map(|e| e.round()); // in [-0.5, 0.5]
Expand Down
6 changes: 1 addition & 5 deletions moyo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,7 @@ fn operations_in_cell(prim_cell: &PrimitiveCell, prim_operations: &Operations) -
let input_operations =
Transformation::from_linear(prim_cell.linear).transform_operations(prim_operations);
for t1 in prim_cell.translations.iter() {
for (rotation, t2) in input_operations
.rotations
.iter()
.zip(input_operations.translations.iter())
{
for (rotation, t2) in input_operations.iter() {
// (E, t1) (rotation, t2) = (rotation, t1 + t2)
rotations.push(*rotation);
let t12 = (t1 + t2).map(|e| e % 1.);
Expand Down
8 changes: 3 additions & 5 deletions moyo/src/symmetrize/standardize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,9 @@ fn symmetrize_positions(
(0..cell.num_atoms())
.map(|i| {
let mut acc = Vector3::zeros();
for (inv_perm, rotation, translation) in izip!(
inverse_permutations.iter(),
operations.rotations.iter(),
operations.translations.iter(),
) {
for (inv_perm, (rotation, translation)) in
inverse_permutations.iter().zip(operations.iter())
{
let mut frac_displacements =
rotation.map(|e| e as f64) * cell.positions[inv_perm.apply(i)] + translation
- cell.positions[i];
Expand Down
7 changes: 1 addition & 6 deletions moyo/tests/test_moyo_dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ fn assert_dataset(
}
}

for (rotation, translation) in dataset
.operations
.rotations
.iter()
.zip(dataset.operations.translations.iter())
{
for (rotation, translation) in dataset.operations.iter() {
// Check if operation induces permutation
let permutation = permutation_from_operation(cell, rotation, translation).unwrap();

Expand Down
8 changes: 2 additions & 6 deletions moyopy/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn operations_from_number(

let lattice_points = hs.centering.lattice_points();
for t1 in lattice_points.iter() {
for (r2, t2) in coset.rotations.iter().zip(coset.translations.iter()) {
for (r2, t2) in coset.iter() {
// (E, t1) (r2, t2) = (r2, t1 + t2)
rotations.push(*r2);
let t12 = (t1 + t2).map(|e| e % 1.);
Expand All @@ -58,11 +58,7 @@ mod tests {

fn unique_sites(position: &Position, operations: &Operations) -> Vec<Position> {
let mut sites: Vec<Position> = vec![];
for (rotation, translation) in operations
.rotations
.iter()
.zip(operations.translations.iter())
{
for (rotation, translation) in operations.iter() {
let new_site = rotation.map(|e| e as f64) * position + translation;
let mut overlap = false;
for site in sites.iter() {
Expand Down

0 comments on commit dea96f4

Please sign in to comment.