Skip to content

Commit

Permalink
Merge pull request #39 from spglib/fix-primitive-symmetry-search
Browse files Browse the repository at this point in the history
Restart ToleranceHandler when the maximum number of symmetry search trials is reached
  • Loading branch information
lan496 authored Dec 24, 2024
2 parents c5707e1 + efe05fc commit c0a0480
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,5 @@ docs/note/
# python-interface docs
_build/
apidocs/

debug.py
54 changes: 35 additions & 19 deletions moyo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub use base::{
};
pub use data::{HallNumber, Number, Setting};

use log::debug;
use nalgebra::Matrix3;

use crate::base::{ToleranceHandler, Transformation};
Expand Down Expand Up @@ -229,36 +230,51 @@ impl MoyoDataset {
}

const MAX_SYMMETRY_SEARCH_TRIALS: usize = 16;
const MAX_TOLERANCE_HANDLER_TRIALS: usize = 4;

fn iterative_symmetry_search(
cell: &Cell,
symprec: f64,
angle_tolerance: AngleTolerance,
) -> Result<(PrimitiveCell, PrimitiveSymmetrySearch, f64, AngleTolerance), MoyoError> {
let mut tolerance_handler = ToleranceHandler::new(symprec, angle_tolerance);

for _ in 0..MAX_SYMMETRY_SEARCH_TRIALS {
match PrimitiveCell::new(cell, tolerance_handler.symprec) {
Ok(prim_cell) => {
match PrimitiveSymmetrySearch::new(
&prim_cell.cell,
tolerance_handler.symprec,
tolerance_handler.angle_tolerance,
) {
Ok(symmetry_search) => {
return Ok((
prim_cell,
symmetry_search,
tolerance_handler.symprec,
tolerance_handler.angle_tolerance,
));
let mut current_symprec = symprec;
let mut current_angle_tolerance = angle_tolerance;

for _ in 0..MAX_TOLERANCE_HANDLER_TRIALS {
let mut tolerance_handler = ToleranceHandler::new(current_symprec, current_angle_tolerance);

for _ in 0..MAX_SYMMETRY_SEARCH_TRIALS {
match PrimitiveCell::new(cell, tolerance_handler.symprec) {
Ok(prim_cell) => {
match PrimitiveSymmetrySearch::new(
&prim_cell.cell,
tolerance_handler.symprec,
tolerance_handler.angle_tolerance,
) {
Ok(symmetry_search) => {
return Ok((
prim_cell,
symmetry_search,
tolerance_handler.symprec,
tolerance_handler.angle_tolerance,
));
}
Err(err) => tolerance_handler.update(err),
}
Err(err) => tolerance_handler.update(err),
}
Err(err) => tolerance_handler.update(err),
}
Err(err) => tolerance_handler.update(err),
}

// When the maximum number of symmetry search trials is reached, restart ToleranceHandler to try larger strides
current_symprec = tolerance_handler.symprec;
current_angle_tolerance = tolerance_handler.angle_tolerance;
debug!(
"Restart ToleranceHandler with symprec={}, angle_tolerance={:?}",
current_symprec, current_angle_tolerance
);
}
debug!("Reach the maximum number of symmetry search trials");
Err(MoyoError::PrimitiveSymmetrySearchError)
}

Expand Down
1 change: 1 addition & 0 deletions moyo/tests/assets/wbm-1-29497.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"lattice":{"basis":[3.9654072417,-0.0006472088,-0.7686595013,-0.2799924824,5.5110023687,-1.4490818386,0.0130490884,-0.1425390523,10.6864239498]},"positions":[[0.8004699061,0.8000010486,0.6000019079000001],[0.4007723449,0.39999603850000004,0.7999922155],[0.1995300549,0.1999990969,0.399998091],[0.5992276144,0.6000038303,0.2000077513],[-1.32e-8,1.234e-7,3.48e-8],[0.8005351034,0.3000024112,0.6000047708],[0.40081083100000003,0.8999938615,0.7999881145000001],[0.5991891318,0.1000058037,0.2000119394],[0.1994649555,0.6999974685,0.3999952432],[-5e-8,0.5000000182000001,-6.7e-8],[0.30046467720000003,0.5499962967,0.5999901493],[0.0992753086,0.3499964238,0.2000087642],[0.49999993730000003,0.2500220433,-5.91e-8],[0.3004646111,0.0499938896,0.5999901164],[0.0992752335,0.8500124103000001,0.2000087669],[0.6995353506,0.4500038052,0.4000098636],[0.900724735,0.650003536,0.7999912864000001],[0.6995354726,0.9500062277,0.40000992160000004],[0.9007247138000001,0.1499877499,0.7999912396000001],[0.5000000815,0.7499779168,-5.04e-8]],"numbers":[25,25,25,25,25,31,31,31,31,31,27,27,27,27,27,27,27,27,27,27]}
15 changes: 15 additions & 0 deletions moyo/tests/test_moyo_dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,18 @@ fn test_niggli_reduction_corner_cases() {
assert_dataset(&dataset.prim_std_cell, symprec, angle_tolerance, setting);
}
}

#[test]
fn test_primitive_symmetry_search_corner_case() {
// https://github.com/spglib/moyo/issues/38
let path = Path::new("tests/assets/wbm-1-29497.json");
let cell: Cell = serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();

let symprec = 1e-2;
let angle_tolerance = AngleTolerance::Default;
let setting = Setting::Standard;

let dataset = assert_dataset(&cell, symprec, angle_tolerance, setting);
assert_dataset(&dataset.std_cell, symprec, angle_tolerance, setting);
assert_dataset(&dataset.prim_std_cell, symprec, angle_tolerance, setting);
}

0 comments on commit c0a0480

Please sign in to comment.