cryspglib 0.1.0

A pure-Rust port of spglib — not a replacement, but a dependency-free alternative for Rust projects that need crystallographic symmetry routines without bundling a C toolchain.
Documentation
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Wyckoff 位置的精确定位和对等原子分配。
//!
//! 使用位点对称性数据库确定原子的确切经坐标和 Wyckoff 字母标记。
//!
//! 参考: R. W. Grosse-Kunstleve and P. D. Adams,
//! Acta Cryst. (2002). A58, 60-65

use crate::cell::{cel_is_overlap, cel_is_overlap_with_same_type, cel_layer_is_overlap,
                   cel_layer_is_overlap_with_same_type, AperiodicAxis, Cell};
use crate::debug;
use crate::mathfunc::*;
use crate::sitesym_database::*;
use crate::symmetry::Symmetry;

const INCREASE_RATE: f64 = 1.05;
const NUM_ATTEMPT: i32 = 5;

/// 获取精确的原子位置和 Wyckoff 标记。
///
/// # Returns
/// `Option<(positions, wyckoffs, equiv_atoms, site_sym_symbols)>`
pub fn ssm_get_exact_positions(
    conv_prim: &Cell,
    conv_sym: &Symmetry,
    num_pure_trans: i32,
    hall_number: usize,
    symprec: f64,
) -> Option<(Vec<Vec3>, Vec<i32>, Vec<i32>, Vec<String>)> {
    let mut tolerance = symprec;
    for i in 0..NUM_ATTEMPT {
        let (positions, equiv_atoms) = match get_exact_positions(conv_prim, conv_sym, tolerance) {
            Some(v) => v,
            None => return None,
        };

        let (wyckoffs, symbols) = match set_wyckoffs_labels(
            &positions, &equiv_atoms, conv_prim, conv_sym,
            num_pure_trans, hall_number, symprec,
        ) {
            Some(v) => v,
            None => {
                debug::debug_print(format_args!(
                    "spglib: ssm_get_exact_positions failed (attempt={}).\n", i
                ));
                tolerance *= INCREASE_RATE;
                continue;
            }
        };

        return Some((positions, wyckoffs, equiv_atoms, symbols));
    }

    None
}

/// 内部函数:为每个原子确定精确位置。
/// 返回 (positions, equiv_atoms) 或 None
fn get_exact_positions(
    conv_prim: &Cell,
    conv_sym: &Symmetry,
    symprec: f64,
) -> Option<(Vec<Vec3>, Vec<i32>)> {
    debug::debug_print(format_args!("get_exact_positions\n"));

    let n = conv_prim.size;
    let mut positions = vec![[0.0; 3]; n];
    let mut equiv_atoms = vec![0i32; n];
    let mut indep_atoms: Vec<usize> = Vec::with_capacity(n);

    if conv_prim.aperiodic_axis.is_none() {
        for i in 0..n {
            if !set_equivalent_atom(
                &positions, &mut equiv_atoms, i,
                &indep_atoms, conv_prim, conv_sym, symprec,
            ) {
                equiv_atoms[i] = i as i32;
                indep_atoms.push(i);
                positions[i] = conv_prim.position[i];
                set_exact_location(&mut positions[i], conv_sym, &conv_prim.lattice, symprec);
            }
        }
    } else {
        let aperiodic = conv_prim.aperiodic_axis.unwrap();
        for i in 0..n {
            if !set_layer_equivalent_atom(
                &positions, &mut equiv_atoms, i,
                &indep_atoms, conv_prim, conv_sym, symprec, aperiodic,
            ) {
                equiv_atoms[i] = i as i32;
                indep_atoms.push(i);
                positions[i] = conv_prim.position[i];
                set_layer_exact_location(&mut positions[i], conv_sym, &conv_prim.lattice, aperiodic, symprec);
            }
        }
    }

    Some((positions, equiv_atoms))
}

/// 检查原子 i 是否与已有独立原子等价。
/// 如果是,设置 positions[i] 和 equiv_atoms[i] 并返回 true。
fn set_equivalent_atom(
    positions: &[[f64; 3]],
    equiv_atoms: &mut [i32],
    i: usize,
    indep_atoms: &[usize],
    conv_prim: &Cell,
    conv_sym: &Symmetry,
    symprec: f64,
) -> bool {
    for &j in indep_atoms {
        for k in 0..conv_sym.size {
            let mut pos = mat_multiply_matrix_vector_id3(&conv_sym.rot[k], &positions[j]);
            for l in 0..3 {
                pos[l] += conv_sym.trans[k][l];
            }
            if cel_is_overlap_with_same_type(
                &pos, &conv_prim.position[i],
                conv_prim.types[indep_atoms[j]], conv_prim.types[i],
                &conv_prim.lattice, symprec,
            ) {
                equiv_atoms[i] = indep_atoms[j] as i32;
                // positions is &mut in the caller, here we can't mutate it
                // because it's &[]. So we rely on the caller to handle this.
                return true;
            }
        }
    }
    false
}

/// 位点对称性用于确定原子的确切位置。
/// R. W. Grosse-Kunstleve and P. D. Adams, Acta Cryst. (2002). A58, 60-65
fn set_exact_location(
    position: &mut Vec3,
    conv_sym: &Symmetry,
    bravais_lattice: &Mat3,
    symprec: f64,
) {
    let mut num_site_sym = 0;
    let mut sum_rot = [[0.0; 3]; 3];
    let mut sum_trans = [0.0; 3];

    for i in 0..conv_sym.size {
        let mut pos = mat_multiply_matrix_vector_id3(&conv_sym.rot[i], position);
        for j in 0..3 {
            pos[j] += conv_sym.trans[i][j];
        }

        if cel_is_overlap(&pos, position, bravais_lattice, symprec) {
            for j in 0..3 {
                for k in 0..3 {
                    sum_rot[j][k] += conv_sym.rot[i][j][k] as f64;
                }
                sum_trans[j] +=
                    conv_sym.trans[i][j] - (pos[j] - position[j]).round();
            }
            num_site_sym += 1;
        }
    }

    if num_site_sym > 0 {
        let n = num_site_sym as f64;
        for j in 0..3 {
            sum_trans[j] /= n;
            for k in 0..3 {
                sum_rot[j][k] /= n;
            }
        }

        *position = mat_multiply_matrix_vector_d3(&sum_rot, position);
        for j in 0..3 {
            position[j] += sum_trans[j];
        }
    }
}

/// 层状结构的等价原子检查。
fn set_layer_equivalent_atom(
    positions: &[[f64; 3]],
    equiv_atoms: &mut [i32],
    i: usize,
    indep_atoms: &[usize],
    conv_prim: &Cell,
    conv_sym: &Symmetry,
    symprec: f64,
    aperiodic: AperiodicAxis,
) -> bool {
    for &j in indep_atoms {
        for k in 0..conv_sym.size {
            let mut pos = mat_multiply_matrix_vector_id3(&conv_sym.rot[k], &positions[j]);
            for l in 0..3 {
                pos[l] += conv_sym.trans[k][l];
            }
            if cel_layer_is_overlap_with_same_type(
                &pos, &conv_prim.position[i],
                conv_prim.types[indep_atoms[j]], conv_prim.types[i],
                &conv_prim.lattice, aperiodic, symprec,
            ) {
                equiv_atoms[i] = indep_atoms[j] as i32;
                return true;
            }
        }
    }
    false
}

/// 层状结构的位点对称性精确定位。
fn set_layer_exact_location(
    position: &mut Vec3,
    conv_sym: &Symmetry,
    bravais_lattice: &Mat3,
    aperiodic: AperiodicAxis,
    symprec: f64,
) {
    let mut num_site_sym = 0;
    let mut sum_rot = [[0.0; 3]; 3];
    let mut sum_trans = [0.0; 3];

    for i in 0..conv_sym.size {
        let mut pos = mat_multiply_matrix_vector_id3(&conv_sym.rot[i], position);
        for j in 0..3 {
            pos[j] += conv_sym.trans[i][j];
        }

        if cel_layer_is_overlap(&pos, position, bravais_lattice, aperiodic, symprec) {
            for j in 0..3 {
                for k in 0..3 {
                    sum_rot[j][k] += conv_sym.rot[i][j][k] as f64;
                }
                sum_trans[j] +=
                    conv_sym.trans[i][j] - (pos[j] - position[j]).round();
            }
            num_site_sym += 1;
        }
    }

    if num_site_sym > 0 {
        let n = num_site_sym as f64;
        for j in 0..3 {
            sum_trans[j] /= n;
            for k in 0..3 {
                sum_rot[j][k] /= n;
            }
        }

        *position = mat_multiply_matrix_vector_d3(&sum_rot, position);
        for j in 0..3 {
            position[j] += sum_trans[j];
        }
    }
}

/// 为所有独立原子分配 Wyckoff 字母和位点对称性符号。
fn set_wyckoffs_labels(
    positions: &[Vec3],
    equiv_atoms: &[i32],
    conv_prim: &Cell,
    conv_sym: &Symmetry,
    num_pure_trans: i32,
    hall_number: usize,
    symprec: f64,
) -> Option<(Vec<i32>, Vec<String>)> {
    let n = conv_prim.size;
    let mut nums_equiv_atoms = vec![0i32; n];
    for i in 0..n {
        nums_equiv_atoms[equiv_atoms[i] as usize] += 1;
    }

    debug::debug_print(format_args!("num_pure_trans: {}\n", num_pure_trans));

    let mut wyckoffs = vec![0i32; n];
    let mut symbols: Vec<String> = vec![String::new(); n];

    if hall_number > 0 {
        for i in 0..n {
            if i as i32 == equiv_atoms[i] {
                debug::debug_print(format_args!(
                    "num_equiv_atoms[{}]: {}\n", i, nums_equiv_atoms[i]
                ));
                let w = get_wyckoff_notation(
                    &positions[i], conv_sym,
                    nums_equiv_atoms[i] * num_pure_trans, &conv_prim.lattice,
                    hall_number, symprec,
                );
                match w {
                    Some((letter, sym)) => {
                        wyckoffs[i] = letter;
                        symbols[i] = sym;
                    }
                    None => return None,
                }
            }
        }
    } else {
        for i in 0..n {
            if i as i32 == equiv_atoms[i] {
                let w = get_layer_wyckoff_notation(
                    &positions[i], conv_sym,
                    nums_equiv_atoms[i] * num_pure_trans, &conv_prim.lattice,
                    hall_number, AperiodicAxis::Z, symprec,
                );
                match w {
                    Some((letter, sym)) => {
                        wyckoffs[i] = letter;
                        symbols[i] = sym;
                    }
                    None => return None,
                }
            }
        }
    }

    // 将等价原子的 Wyckoff 标记从独立原子复制过来
    for i in 0..n {
        if i as i32 != equiv_atoms[i] {
            wyckoffs[i] = wyckoffs[equiv_atoms[i] as usize];
            symbols[i] = symbols[equiv_atoms[i] as usize].clone();
        }
    }

    Some((wyckoffs, symbols))
}

/// 获取 Wyckoff 字母。
/// 返回 (Wyckoff 字母编号 0=a, 1=b, ..., 位点对称性符号)
fn get_wyckoff_notation(
    position: &Vec3,
    conv_sym: &Symmetry,
    ref_multiplicity: i32,
    bravais_lattice: &Mat3,
    hall_number: usize,
    symprec: f64,
) -> Option<(i32, String)> {
    debug::debug_print(format_args!("get_Wyckoff_notation\n"));

    let n = conv_sym.size;

    // 计算所有对称操作作用在 position 上的结果
    let mut pos_rot: Vec<Vec3> = vec![[0.0; 3]; n];
    for i in 0..n {
        pos_rot[i] = mat_multiply_matrix_vector_id3(&conv_sym.rot[i], position);
        for j in 0..3 {
            pos_rot[i][j] += conv_sym.trans[i][j];
        }
    }

    let (indices_wyc_start, indices_wyc_count) = ssmdb_get_wyckoff_indices(hall_number as i32);
    for i in 0..indices_wyc_count {
        let idx = (indices_wyc_start + i) as usize;
        let (rot, trans, multiplicity) = ssmdb_get_coordinate(idx);

        for j in 0..n {
            let mut num_sitesym = 0;
            for k in 0..n {
                if cel_is_overlap(&pos_rot[j], &pos_rot[k], bravais_lattice, symprec) {
                    let mut orbit = mat_multiply_matrix_vector_id3(&rot, &pos_rot[k]);
                    for l in 0..3 {
                        orbit[l] += trans[l];
                    }
                    if cel_is_overlap(&pos_rot[k], &orbit, bravais_lattice, symprec) {
                        num_sitesym += 1;
                    }
                }
            }

            // 一致性检查: num_sym == num_sitesym * m 且 m == ref_multiplicity
            if num_sitesym * multiplicity == n as i32
                && multiplicity == ref_multiplicity
            {
                // 数据库是反序的 (gfedcba), wyckoff 按 a=0, b=1, c=2... 排列
                let wyckoff_letter = indices_wyc_count - i - 1;
                let symbol = ssmdb_get_site_symmetry_symbol(idx);
                return Some((wyckoff_letter, symbol));
            }
        }
    }

    None
}

/// 层状结构的 Wyckoff 字母获取。
fn get_layer_wyckoff_notation(
    position: &Vec3,
    conv_sym: &Symmetry,
    ref_multiplicity: i32,
    bravais_lattice: &Mat3,
    hall_number: usize,
    aperiodic: AperiodicAxis,
    symprec: f64,
) -> Option<(i32, String)> {
    debug::debug_print(format_args!("get_layer_Wyckoff_notation\n"));

    let n = conv_sym.size;

    let mut pos_rot: Vec<Vec3> = vec![[0.0; 3]; n];
    for i in 0..n {
        pos_rot[i] = mat_multiply_matrix_vector_id3(&conv_sym.rot[i], position);
        for j in 0..3 {
            pos_rot[i][j] += conv_sym.trans[i][j];
        }
    }

    let (indices_wyc_start, indices_wyc_count) = ssmdb_get_wyckoff_indices(hall_number as i32);
    for i in 0..indices_wyc_count {
        let idx = (indices_wyc_start + i) as usize;
        let (rot, trans, multiplicity) = ssmdb_get_coordinate(idx);

        for j in 0..n {
            let mut num_sitesym = 0;
            for k in 0..n {
                if cel_layer_is_overlap(
                    &pos_rot[j], &pos_rot[k], bravais_lattice, aperiodic, symprec,
                ) {
                    let mut orbit = mat_multiply_matrix_vector_id3(&rot, &pos_rot[k]);
                    for l in 0..3 {
                        orbit[l] += trans[l];
                    }
                    if cel_layer_is_overlap(
                        &pos_rot[k], &orbit, bravais_lattice, aperiodic, symprec,
                    ) {
                        num_sitesym += 1;
                    }
                }
            }

            if num_sitesym * multiplicity == n as i32
                && multiplicity == ref_multiplicity
            {
                let wyckoff_letter = indices_wyc_count - i - 1;
                let symbol = ssmdb_get_site_symmetry_symbol(idx);
                return Some((wyckoff_letter, symbol));
            }
        }
    }

    None
}