Skip to main content

minimal/
minimal.rs

1// Copyright (c) d-SEAMS developers
2//! Read the GenIce sI cell through the safe seams crate.
3//!
4//! `cargo run --example minimal [dump]` reads `tests/data/genice_sI.lammpstrj`
5//! when no path is given.
6use seams::{num_threads, set_num_threads, version, System};
7use std::env;
8use std::fs::File;
9use std::io::{BufRead, BufReader};
10
11fn read_dump(path: &str) -> (Vec<f64>, [f64; 3]) {
12    let file = File::open(path).expect("open fixture");
13    let mut xyz = Vec::new();
14    let mut edges = [0.0; 3];
15    let mut tilts = [0.0; 3];
16    let mut lows = [0.0; 3];
17    let mut axis = 0;
18    let mut reading = false;
19    let mut bounds = false;
20    for line in BufReader::new(file).lines() {
21        let line = line.expect("line");
22        if line.starts_with("ITEM: BOX BOUNDS") {
23            bounds = true;
24            axis = 0;
25            reading = false;
26        } else if bounds {
27            let mut parts = line.split_whitespace();
28            let lo: f64 = parts.next().unwrap().parse().unwrap();
29            let hi: f64 = parts.next().unwrap().parse().unwrap();
30            let tilt: f64 = parts
31                .next()
32                .and_then(|item| item.parse().ok())
33                .unwrap_or(0.0);
34            // Recovered after all three rows. Store the bound span and
35            // the tilt, then convert once the third row arrives.
36            edges[axis] = hi - lo;
37            tilts[axis] = tilt;
38            lows[axis] = lo;
39            axis += 1;
40            if axis == 3 {
41                let xy = tilts[0];
42                let xz = tilts[1];
43                let yz = tilts[2];
44                let xmin = xy.min(xz).min(xy + xz).min(0.0);
45                let xmax = xy.max(xz).max(xy + xz).max(0.0);
46                let ymin = yz.min(0.0);
47                let ymax = yz.max(0.0);
48                edges[0] = edges[0] - xmax + xmin;
49                edges[1] = edges[1] - ymax + ymin;
50                bounds = false;
51                let _ = lows;
52            }
53        } else if line.starts_with("ITEM: ATOMS") {
54            reading = true;
55        } else if reading {
56            let parts: Vec<&str> = line.split_whitespace().collect();
57            if parts.len() < 5 {
58                break;
59            }
60            xyz.extend(parts[2..5].iter().map(|v| v.parse::<f64>().unwrap()));
61        }
62    }
63    (xyz, edges)
64}
65
66const FIXTURE: &str = concat!(
67    env!("CARGO_MANIFEST_DIR"),
68    "/tests/data/genice_sI.lammpstrj"
69);
70
71fn run(path: &str) -> String {
72    let (xyz, edges) = read_dump(path);
73    assert_eq!(xyz.len() / 3, 46);
74    set_num_threads(1).expect("threads");
75    assert_eq!(num_threads(), 1);
76    let mut system = System::new().expect("system");
77    system
78        .set_ortho(&xyz, edges[0], edges[1], edges[2])
79        .expect("frame");
80    assert_eq!(system.count(), 46);
81    let degree = system.neighbour_degree(3.5).expect("neighbours");
82    assert_eq!(degree.len(), 46);
83    let orders = system.q4q6(3.5).expect("q6");
84    let mean = orders.q6.iter().sum::<f64>() / orders.q6.len() as f64;
85    let ice = system
86        .chill_plus(&seams::ChillOptions::default())
87        .expect("chill+");
88    let (six, _fraction) = system.six_ring_fraction(3.5).expect("rings");
89    assert_eq!(ice.counts[4], 46);
90    assert_eq!(six, 54);
91    assert!(mean > 0.5);
92    format!("rust example ok {} q6 {:.3}", version(), mean)
93}
94
95fn main() {
96    let path = env::args().nth(1).unwrap_or_else(|| FIXTURE.to_string());
97    println!("{}", run(&path));
98}
99
100#[test]
101fn genice_si_cell() {
102    let line = run(FIXTURE);
103    assert!(line.starts_with("rust example ok "), "{line}");
104}