feffit 0.1.0

Pure-Rust EXAFS toolkit — data reduction (pre-edge/normalize/AUTOBK), Fourier transforms, FEFF path fitting (feffit), and feff.inp build/run; a port of larch.xafs
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
//! Build a FEFF scattering cluster from an explicit unit cell, and emit it as a
//! `feff.inp`.
//!
//! # Scope: no space-group expansion
//!
//! This builder takes the **full cell contents** (every atom in the
//! conventional cell) and replicates them over the lattice to gather every atom
//! within `cluster_size` of a chosen absorber. It does **not** apply
//! space-group symmetry operators to expand an asymmetric unit — larch delegates
//! that step to `pymatgen` (`SpacegroupAnalyzer`), which is not portable from
//! the larch source. So to reproduce, e.g., the 12-neighbour fcc Cu shell you
//! must enter all four face-centred sites, not the single asymmetric-unit atom.
//! See the crate-level docs for the deferred space-group work.

use crate::feffinp::element::{symbol_to_z, z_to_symbol};
use crate::feffinp::lattice::Lattice;
use crate::feffinp::{Edge, Error};

/// One atom of the input cell, by fractional coordinate.
#[derive(Debug, Clone, PartialEq)]
pub struct Site {
    /// Element symbol (e.g. `"Cu"`); resolved to `Z` for the `POTENTIALS` card.
    pub element: String,
    /// Fractional coordinates within the cell.
    pub frac: [f64; 3],
}

impl Site {
    /// A site of `element` at fractional `(x, y, z)`.
    pub fn new(element: impl Into<String>, x: f64, y: f64, z: f64) -> Self {
        Self {
            element: element.into(),
            frac: [x, y, z],
        }
    }
}

/// A crystal: a [`Lattice`] plus the full list of cell [`Site`]s.
#[derive(Debug, Clone, PartialEq)]
pub struct Crystal {
    pub lattice: Lattice,
    pub sites: Vec<Site>,
}

/// One unique-potential declaration (a `POTENTIALS` row).
#[derive(Debug, Clone, PartialEq)]
pub struct Potential {
    pub ipot: usize,
    pub z: u32,
    pub tag: String,
}

/// One atom of the generated cluster, Cartesian (Å), absorber at the origin.
#[derive(Debug, Clone, PartialEq)]
pub struct ClusterAtom {
    pub element: String,
    pub xyz: [f64; 3],
    pub ipot: usize,
    pub tag: String,
    pub distance: f64,
}

/// A FEFF scattering cluster: the absorber-centred atom list plus the unique
/// potentials, ready to write as a `feff.inp`.
#[derive(Debug, Clone, PartialEq)]
pub struct Cluster {
    pub title: String,
    /// Absorbing element symbol.
    pub absorber: String,
    pub edge: Edge,
    pub rmax: f64,
    pub potentials: Vec<Potential>,
    /// Atoms sorted by ascending distance (the absorber, `ipot 0`, is first).
    pub atoms: Vec<ClusterAtom>,
}

/// Canonicalise an element symbol to the periodic-table capitalisation
/// (`"fe"` → `"Fe"`); returns the input trimmed if it is not a known symbol.
fn canonical(symbol: &str) -> String {
    symbol_to_z(symbol)
        .and_then(z_to_symbol)
        .map(str::to_owned)
        .unwrap_or_else(|| symbol.trim().to_owned())
}

impl Crystal {
    /// Build the absorber-centred cluster of every atom within `cluster_size`
    /// (Å) of site `absorber_index`, for the given `edge`.
    ///
    /// Returns an error for an out-of-range absorber index, a non-positive
    /// cluster size, or an unrecognised element symbol.
    pub fn cluster(
        &self,
        absorber_index: usize,
        cluster_size: f64,
        edge: Edge,
    ) -> Result<Cluster, Error> {
        if cluster_size.is_nan() || cluster_size <= 0.0 {
            return Err(Error::BadClusterSize(cluster_size));
        }
        let abs_site = self
            .sites
            .get(absorber_index)
            .ok_or(Error::AbsorberIndex(absorber_index))?;
        // Every element must resolve to a Z (needed for POTENTIALS).
        for s in &self.sites {
            if symbol_to_z(&s.element).is_none() {
                return Err(Error::UnknownElement(s.element.clone()));
            }
        }

        let p0 = self.lattice.frac_to_cart(abs_site.frac);
        let [na, nb, nc] = self.repl_counts(cluster_size);

        let mut atoms: Vec<ClusterAtom> = Vec::new();
        for i in -na..=na {
            for j in -nb..=nb {
                for k in -nc..=nc {
                    for (sidx, s) in self.sites.iter().enumerate() {
                        // The absorber is this exact site in the origin cell; its
                        // images in other cells are ordinary neighbours.
                        if sidx == absorber_index && i == 0 && j == 0 && k == 0 {
                            continue;
                        }
                        let frac = [
                            s.frac[0] + i as f64,
                            s.frac[1] + j as f64,
                            s.frac[2] + k as f64,
                        ];
                        let cart = self.lattice.frac_to_cart(frac);
                        let rel = [cart[0] - p0[0], cart[1] - p0[1], cart[2] - p0[2]];
                        let d = (rel[0] * rel[0] + rel[1] * rel[1] + rel[2] * rel[2]).sqrt();
                        if d <= cluster_size + 1e-8 {
                            atoms.push(ClusterAtom {
                                element: canonical(&s.element),
                                xyz: rel,
                                ipot: 0, // assigned below
                                tag: canonical(&s.element),
                                distance: d,
                            });
                        }
                    }
                }
            }
        }
        atoms.sort_by(|x, y| {
            x.distance
                .partial_cmp(&y.distance)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // ipot ordering (FEFF/Atoms convention): 1 = the absorber's own element
        // (if present among neighbours), then the remaining elements in order of
        // first appearance by distance.
        let abs_elem = canonical(&abs_site.element);
        let mut elem_order: Vec<String> = Vec::new();
        if atoms.iter().any(|a| a.element == abs_elem) {
            elem_order.push(abs_elem.clone());
        }
        for a in &atoms {
            if !elem_order.contains(&a.element) {
                elem_order.push(a.element.clone());
            }
        }
        for a in &mut atoms {
            // +1: ipot 0 is the absorber.
            a.ipot = elem_order.iter().position(|e| *e == a.element).unwrap() + 1;
        }

        let mut potentials = vec![Potential {
            ipot: 0,
            z: symbol_to_z(&abs_elem).unwrap(),
            tag: abs_elem.clone(),
        }];
        for (idx, elem) in elem_order.iter().enumerate() {
            potentials.push(Potential {
                ipot: idx + 1,
                z: symbol_to_z(elem).unwrap(),
                tag: elem.clone(),
            });
        }

        // The absorber itself, at the origin.
        let mut all = Vec::with_capacity(atoms.len() + 1);
        all.push(ClusterAtom {
            element: abs_elem.clone(),
            xyz: [0.0, 0.0, 0.0],
            ipot: 0,
            tag: abs_elem.clone(),
            distance: 0.0,
        });
        all.extend(atoms);

        Ok(Cluster {
            title: String::new(),
            absorber: abs_elem,
            edge,
            rmax: cluster_size,
            potentials,
            atoms: all,
        })
    }

    /// Per-axis replication counts that guarantee every atom within
    /// `cluster_size` of any cell point is generated: `⌈r / dₚ⌉ + 1`, where
    /// `dₚ` is the interplanar spacing (cell volume / opposite-face area).
    fn repl_counts(&self, cluster_size: f64) -> [i32; 3] {
        let [va, vb, vc] = self.lattice.vectors();
        let vol = self.lattice.volume().max(1e-12);
        let area = |u: [f64; 3], w: [f64; 3]| {
            let c = [
                u[1] * w[2] - u[2] * w[1],
                u[2] * w[0] - u[0] * w[2],
                u[0] * w[1] - u[1] * w[0],
            ];
            (c[0] * c[0] + c[1] * c[1] + c[2] * c[2]).sqrt()
        };
        let count = |d_perp: f64| -> i32 {
            let d = if d_perp > 1e-12 { d_perp } else { 1e-12 };
            (cluster_size / d).ceil() as i32 + 1
        };
        [
            count(vol / area(vb, vc).max(1e-12)),
            count(vol / area(va, vc).max(1e-12)),
            count(vol / area(va, vb).max(1e-12)),
        ]
    }
}

impl Cluster {
    /// Render the cluster as a `feff.inp` (FEFF8L/FEFF10 syntax).
    pub fn to_feff_inp(&self) -> String {
        let mut s = String::new();
        for line in self.title.lines() {
            s.push_str(&format!(" TITLE {line}\n"));
        }
        if self.title.is_empty() {
            s.push_str(&format!(
                " TITLE {} {} cluster (XAFSView/atoms)\n",
                self.absorber,
                self.edge.as_str()
            ));
        }
        s.push('\n');
        s.push_str(&format!(" EDGE      {}\n", self.edge.as_str()));
        s.push_str(" S02       1.0\n");
        s.push_str(" CONTROL   1 1 1 1 1 1\n");
        s.push_str(" PRINT     1 0 0 0 0 0\n");
        s.push_str(&format!(" RMAX      {:.4}\n", self.rmax));
        s.push_str(" EXCHANGE  0\n");
        s.push('\n');

        s.push_str(" POTENTIALS\n");
        s.push_str(" *    ipot   Z   tag\n");
        for p in &self.potentials {
            s.push_str(&format!("    {:5}{:5}   {:<6}\n", p.ipot, p.z, p.tag));
        }
        s.push('\n');

        s.push_str(" ATOMS\n");
        s.push_str(" *      x          y          z      ipot  tag      distance\n");
        for a in &self.atoms {
            s.push_str(&format!(
                "{:11.5}{:11.5}{:11.5}{:5}   {:<6}{:11.5}\n",
                a.xyz[0], a.xyz[1], a.xyz[2], a.ipot, a.tag, a.distance
            ));
        }
        s.push_str(" END\n");
        s
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// fcc Cu (a = 3.61): the full conventional cell is the four face-centred
    /// sites. Reproduces the canonical first shell (12 atoms at a/√2 = 2.5527 Å).
    fn fcc_cu() -> Crystal {
        Crystal {
            lattice: Lattice::cubic(3.61),
            sites: vec![
                Site::new("Cu", 0.0, 0.0, 0.0),
                Site::new("Cu", 0.5, 0.5, 0.0),
                Site::new("Cu", 0.5, 0.0, 0.5),
                Site::new("Cu", 0.0, 0.5, 0.5),
            ],
        }
    }

    #[test]
    fn fcc_first_shell() {
        let c = fcc_cu().cluster(0, 3.0, Edge::K).expect("cluster");
        // ipot 0 absorber + 12 first neighbours within 3.0 Å.
        let first: Vec<_> = c
            .atoms
            .iter()
            .filter(|a| a.ipot != 0 && a.distance < 2.6)
            .collect();
        assert_eq!(first.len(), 12, "fcc first shell is 12 neighbours");
        for a in &first {
            assert!(
                (a.distance - 3.61 / 2f64.sqrt()).abs() < 1e-6,
                "nn distance {} != a/√2",
                a.distance
            );
        }
        // Two potentials: ipot 0 (absorber Cu) and ipot 1 (Cu).
        assert_eq!(c.potentials.len(), 2);
        assert_eq!(
            c.potentials[0],
            Potential {
                ipot: 0,
                z: 29,
                tag: "Cu".into()
            }
        );
        assert_eq!(
            c.potentials[1],
            Potential {
                ipot: 1,
                z: 29,
                tag: "Cu".into()
            }
        );
        // Absorber is first, at the origin.
        assert_eq!(c.atoms[0].ipot, 0);
        assert_eq!(c.atoms[0].distance, 0.0);
    }

    /// Rocksalt FeO (a = 4.31): Fe at the corner/face centres, O at the edge/
    /// body centres. First shell is 6 O at a/2; second is 12 Fe at a/√2.
    fn rocksalt_feo() -> Crystal {
        let a = 4.3088;
        Crystal {
            lattice: Lattice::cubic(a),
            sites: vec![
                Site::new("Fe", 0.0, 0.0, 0.0),
                Site::new("Fe", 0.5, 0.5, 0.0),
                Site::new("Fe", 0.5, 0.0, 0.5),
                Site::new("Fe", 0.0, 0.5, 0.5),
                Site::new("O", 0.5, 0.0, 0.0),
                Site::new("O", 0.0, 0.5, 0.0),
                Site::new("O", 0.0, 0.0, 0.5),
                Site::new("O", 0.5, 0.5, 0.5),
            ],
        }
    }

    #[test]
    fn rocksalt_potentials_and_shells() {
        let a = 4.3088;
        let c = rocksalt_feo().cluster(0, 3.2, Edge::K).expect("cluster");
        // Potentials: 0=Fe(abs), 1=Fe, 2=O (absorber element first, then O).
        assert_eq!(
            c.potentials[0],
            Potential {
                ipot: 0,
                z: 26,
                tag: "Fe".into()
            }
        );
        assert_eq!(
            c.potentials[1],
            Potential {
                ipot: 1,
                z: 26,
                tag: "Fe".into()
            }
        );
        assert_eq!(
            c.potentials[2],
            Potential {
                ipot: 2,
                z: 8,
                tag: "O".into()
            }
        );
        // First shell: 6 O at a/2.
        let o1: Vec<_> = c.atoms.iter().filter(|x| x.element == "O").collect();
        let nearest_o = o1
            .iter()
            .filter(|x| (x.distance - a / 2.0).abs() < 1e-6)
            .count();
        assert_eq!(nearest_o, 6, "6 O at a/2");
        // Second shell: 12 Fe at a/√2.
        let fe2 = c
            .atoms
            .iter()
            .filter(|x| x.ipot == 1 && (x.distance - a / 2f64.sqrt()).abs() < 1e-6)
            .count();
        assert_eq!(fe2, 12, "12 Fe at a/√2");
    }

    #[test]
    fn feff_inp_round_trips_atom_count() {
        let c = fcc_cu().cluster(0, 4.0, Edge::K).expect("cluster");
        let text = c.to_feff_inp();
        assert!(text.contains("POTENTIALS"));
        assert!(text.contains("ATOMS"));
        assert!(text.contains(" EDGE      K"));
        // Parsing the emitted text recovers the same atom count and potentials.
        let parsed = crate::feffinp::FeffInp::parse(&text).expect("parse");
        assert_eq!(parsed.atoms.len(), c.atoms.len());
        assert_eq!(parsed.potentials.len(), c.potentials.len());
    }

    #[test]
    fn errors() {
        let c = fcc_cu();
        assert!(matches!(
            c.cluster(99, 3.0, Edge::K),
            Err(Error::AbsorberIndex(99))
        ));
        assert!(matches!(
            c.cluster(0, 0.0, Edge::K),
            Err(Error::BadClusterSize(_))
        ));
        let bad = Crystal {
            lattice: Lattice::cubic(3.0),
            sites: vec![Site::new("Zz", 0.0, 0.0, 0.0)],
        };
        assert!(matches!(
            bad.cluster(0, 3.0, Edge::K),
            Err(Error::UnknownElement(_))
        ));
    }
}