refeff-io 0.2.0

FEFF file-format readers and writers (feff.inp, .dat/.bin handoffs, PAD encoding) for the refeff FEFF10 port
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Typed reader for FEFF `ldos.inp` module handoff files.
//!
//! `ldos.inp` combines LDOS mesh controls with FMS convergence settings and
//! per-potential angular momentum cutoffs. Keeping it typed prepares the Rust
//! LDOS module to consume normalized `rdinp` output directly.

use std::fmt::{self, Write as _};
use std::path::{Path, PathBuf};
use std::str::FromStr;

use crate::{IoError, Result};

/// Parsed contents of a FEFF `ldos.inp` file.
#[derive(Debug, Clone, PartialEq)]
pub struct LdosInput {
    /// LDOS run-control switches.
    pub control: LdosControl,
    /// LDOS energy mesh and radial grid settings.
    pub mesh: LdosMesh,
    /// FMS convergence settings reused by LDOS.
    pub fms: LdosFms,
    /// Angular-momentum cutoffs indexed by potential.
    pub lmaxph: Vec<i32>,
    /// LDOS output type selector.
    pub ldostype: i32,
}

/// First integer control line of `ldos.inp`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LdosControl {
    pub mldos: i32,
    pub lfms2: i32,
    pub ixc: i32,
    pub ispin: i32,
    pub minv: i32,
    pub neldos: i32,
    pub iscfxc: i32,
}

/// Energy and radial-grid settings from `ldos.inp`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LdosMesh {
    pub rfms2: f64,
    pub emin: f64,
    pub emax: f64,
    pub eimag: f64,
    pub rgrd: f64,
}

/// FMS convergence settings reused by the LDOS module.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LdosFms {
    pub rdirec: f64,
    pub toler1: f64,
    pub toler2: f64,
}

impl LdosInput {
    /// Parse a FEFF `ldos.inp` string.
    pub fn parse_str(source: impl Into<PathBuf>, text: &str) -> Result<Self> {
        let mut parser = LdosInputParser::new(source.into(), text);
        parser.parse()
    }
}

/// Render FEFF-compatible `ldos.inp` text.
pub fn ldos_input_string(input: &LdosInput) -> Result<String> {
    validate_ldos_input(input)?;

    let mut out = String::new();
    writeln!(out, "mldos, lfms2, ixc, ispin, minv, neldos, iscfxc")?;
    writeln!(
        out,
        "{:4}{:4}{:4}{:4}{:4} {:7} {:4}",
        input.control.mldos,
        input.control.lfms2,
        input.control.ixc,
        input.control.ispin,
        input.control.minv,
        input.control.neldos,
        input.control.iscfxc
    )?;
    writeln!(out, "rfms2, emin, emax, eimag, rgrd")?;
    write_ldos_mesh_row(
        &mut out,
        input.mesh.rfms2,
        input.mesh.emin,
        input.mesh.emax,
        input.mesh.eimag,
        input.mesh.rgrd,
    )?;
    writeln!(out, "rdirec, toler1, toler2")?;
    writeln!(
        out,
        "{:13.5}{:13.5}{:13.5}",
        input.fms.rdirec, input.fms.toler1, input.fms.toler2
    )?;
    writeln!(out, " lmaxph(0:nph)")?;
    push_i4_row(&mut out, input.lmaxph.iter().copied())?;
    writeln!(out, "ldostype")?;
    push_i4_row(&mut out, [input.ldostype])?;
    Ok(out)
}

pub(crate) fn write_ldos_mesh_row(
    out: &mut impl fmt::Write,
    rfms2: f64,
    emin: f64,
    emax: f64,
    eimag: f64,
    rgrd: f64,
) -> fmt::Result {
    write!(out, "{rfms2:13.5}{emin:13.5}")?;
    if emax < 0.0 {
        write!(out, "{emax:14.5}")?;
    } else {
        write!(out, "{emax:13.5}")?;
    }
    writeln!(out, "{eimag:13.5}{rgrd:13.5}")
}

fn validate_ldos_input(input: &LdosInput) -> Result<()> {
    validate_finite("rfms2", input.mesh.rfms2)?;
    validate_finite("emin", input.mesh.emin)?;
    validate_finite("emax", input.mesh.emax)?;
    validate_finite("eimag", input.mesh.eimag)?;
    validate_finite("rgrd", input.mesh.rgrd)?;
    validate_finite("rdirec", input.fms.rdirec)?;
    validate_finite("toler1", input.fms.toler1)?;
    validate_finite("toler2", input.fms.toler2)?;
    if input.lmaxph.is_empty() {
        return Err(IoError::Parse {
            path: "ldos.inp".into(),
            line: 0,
            message: "LDOS input requires at least one lmaxph value".to_string(),
        });
    }
    Ok(())
}

fn validate_finite(field: &'static str, value: f64) -> Result<()> {
    if value.is_finite() {
        Ok(())
    } else {
        Err(IoError::Parse {
            path: "ldos.inp".into(),
            line: 0,
            message: format!("{field} must be finite"),
        })
    }
}

fn push_i4_row(out: &mut String, values: impl IntoIterator<Item = i32>) -> Result<()> {
    for value in values {
        write!(out, "{value:4}")?;
    }
    out.push('\n');
    Ok(())
}

struct LdosInputParser<'a> {
    source: PathBuf,
    lines: std::iter::Enumerate<std::str::Lines<'a>>,
}

impl<'a> LdosInputParser<'a> {
    fn new(source: PathBuf, text: &'a str) -> Self {
        Self {
            source,
            lines: text.lines().enumerate(),
        }
    }

    fn parse(&mut self) -> Result<LdosInput> {
        let control_count = self.expect_control_header()?;
        let control_values = self.parse_values::<i32>(control_count, "LDOS control line")?;
        let control = LdosControl {
            mldos: control_values[0],
            lfms2: control_values[1],
            ixc: control_values[2],
            ispin: control_values[3],
            minv: control_values[4],
            neldos: control_values[5],
            iscfxc: control_values.get(6).copied().unwrap_or(0),
        };

        self.expect_header("rfms2, emin, emax, eimag, rgrd")?;
        let mesh_values = self.parse_values::<f64>(5, "LDOS mesh line")?;
        let mesh = LdosMesh {
            rfms2: mesh_values[0],
            emin: mesh_values[1],
            emax: mesh_values[2],
            eimag: mesh_values[3],
            rgrd: mesh_values[4],
        };

        self.expect_header("rdirec, toler1, toler2")?;
        let fms_values = self.parse_values::<f64>(3, "LDOS FMS line")?;
        let fms = LdosFms {
            rdirec: fms_values[0],
            toler1: fms_values[1],
            toler2: fms_values[2],
        };

        self.expect_header("lmaxph(0:nph)")?;
        let lmaxph = self.parse_variable_i32_line("LDOS lmaxph line")?;
        let ldostype = self.parse_optional_ldostype()?;

        Ok(LdosInput {
            control,
            mesh,
            fms,
            lmaxph,
            ldostype,
        })
    }

    fn expect_control_header(&mut self) -> Result<usize> {
        let (line_number, line) = self.next_line("LDOS control header")?;
        match line.trim() {
            "mldos, lfms2, ixc, ispin, minv, neldos, iscfxc" => Ok(7),
            "mldos, lfms2, ixc, ispin, minv, neldos" => Ok(6),
            _ => Err(self.parse_error(
                line_number,
                format!("expected LDOS control header, found {line:?}"),
            )),
        }
    }

    fn parse_optional_ldostype(&mut self) -> Result<i32> {
        let (line_number, line) = match self.next_line("ldostype") {
            Ok(line) => line,
            Err(_) => return Ok(0),
        };
        if line.trim() != "ldostype" {
            return Err(self.parse_error(
                line_number,
                format!("expected header {:?}, found {line:?}", "ldostype"),
            ));
        }
        Ok(self.parse_values::<i32>(1, "LDOS type line")?[0])
    }

    fn expect_header(&mut self, expected: &str) -> Result<()> {
        let (line_number, line) = self.next_line(expected)?;
        if line.trim() == expected {
            Ok(())
        } else {
            Err(self.parse_error(
                line_number,
                format!("expected header {expected:?}, found {line:?}"),
            ))
        }
    }

    fn parse_values<T>(&mut self, count: usize, description: &str) -> Result<Vec<T>>
    where
        T: FromStr,
    {
        let (line_number, line) = self.next_line(description)?;
        let fields: Vec<&str> = line.split_whitespace().collect();
        if fields.len() < count {
            return Err(self.parse_error(
                line_number,
                format!("{description} requires {count} fields"),
            ));
        }
        fields
            .iter()
            .take(count)
            .map(|field| parse_field(&self.source, line_number, field))
            .collect()
    }

    fn parse_variable_i32_line(&mut self, description: &str) -> Result<Vec<i32>> {
        let (line_number, line) = self.next_line(description)?;
        line.split_whitespace()
            .map(|field| parse_field(&self.source, line_number, field))
            .collect()
    }

    fn next_line(&mut self, description: &str) -> Result<(usize, &'a str)> {
        self.lines
            .next()
            .map(|(index, line)| (index + 1, line))
            .ok_or_else(|| self.parse_error(0, format!("expected {description}")))
    }

    fn parse_error(&self, line: usize, message: impl Into<String>) -> IoError {
        IoError::Parse {
            path: self.source.clone(),
            line,
            message: message.into(),
        }
    }
}

fn parse_field<T>(source: &Path, line: usize, field: &str) -> Result<T>
where
    T: FromStr,
{
    field.parse::<T>().map_err(|_| IoError::Parse {
        path: source.to_path_buf(),
        line,
        message: format!("invalid numeric field {field:?}"),
    })
}

#[cfg(test)]
mod tests {
    use crate::{FeffDocument, FeffInput, rdinp};

    use super::{LdosInput, ldos_input_string};

    #[test]
    fn parses_generated_ldos_input() -> crate::Result<()> {
        let input = FeffInput::parse_str(
            "feff.inp",
            r#"
LDOS -30.0 20.0 0.1 151 2
FMS 4.5 1 2 0.002 0.003 8.0
EXCHANGE 5 0.0 0.0
SPIN 1 0.0 0.0 1.0
POTENTIALS
0 29 Cu
1 29 Cu
ATOMS
0.0 0.0 0.0 0 Cu0
1.0 0.0 0.0 1 Cu1
END
"#,
        )?;
        let document = FeffDocument::from_input(&input)?;
        let text = rdinp::ldos_inp_string(&document)?;
        let ldos = LdosInput::parse_str("ldos.inp", &text)?;

        assert_eq!(ldos.control.mldos, 1);
        assert_eq!(ldos.control.lfms2, 1);
        assert_eq!(ldos.control.ixc, 5);
        assert_eq!(ldos.control.ispin, 1);
        assert_eq!(ldos.control.minv, 2);
        assert_eq!(ldos.control.neldos, 151);
        assert_eq!(ldos.mesh.rfms2, 4.5);
        assert_eq!(ldos.mesh.emin, -30.0);
        assert_eq!(ldos.mesh.emax, 20.0);
        assert_eq!(ldos.mesh.eimag, 0.1);
        assert_eq!(ldos.fms.rdirec, 8.0);
        assert_eq!(ldos.fms.toler1, 0.002);
        assert_eq!(ldos.fms.toler2, 0.003);
        assert_eq!(ldos.lmaxph, [3, 3]);
        assert_eq!(ldos.ldostype, 2);
        Ok(())
    }

    #[test]
    fn renders_generated_ldos_input() -> crate::Result<()> {
        let input = FeffInput::parse_str(
            "feff.inp",
            r#"
LDOS -30.0 20.0 0.1 151 2
FMS 4.5 1 2 0.002 0.003 8.0
EXCHANGE 5 0.0 0.0
SPIN 1 0.0 0.0 1.0
POTENTIALS
0 29 Cu
1 29 Cu
ATOMS
0.0 0.0 0.0 0 Cu0
1.0 0.0 0.0 1 Cu1
END
"#,
        )?;
        let document = FeffDocument::from_input(&input)?;
        let text = rdinp::ldos_inp_string(&document)?;
        let ldos = LdosInput::parse_str("ldos.inp", &text)?;

        assert_eq!(ldos_input_string(&ldos)?, text);
        Ok(())
    }

    #[test]
    fn parses_legacy_ldos_input_without_iscfxc_or_ldostype() -> crate::Result<()> {
        let ldos = LdosInput::parse_str(
            "ldos.inp",
            r#"mldos, lfms2, ixc, ispin, minv, neldos
   1   0   0   0   0     101
rfms2, emin, emax, eimag, rgrd
      4.51200    -25.00000     12.00000      0.01000      0.05000
rdirec, toler1, toler2
      9.40000      0.00100      0.00100
 lmaxph(0:nph)
   1   2   1
"#,
        )?;

        assert_eq!(ldos.control.mldos, 1);
        assert_eq!(ldos.control.neldos, 101);
        assert_eq!(ldos.control.iscfxc, 0);
        assert_eq!(ldos.lmaxph, [1, 2, 1]);
        assert_eq!(ldos.ldostype, 0);
        Ok(())
    }

    #[test]
    fn renders_negative_emax_like_feff_reference() -> crate::Result<()> {
        let input = LdosInput {
            control: super::LdosControl {
                mldos: 1,
                lfms2: 0,
                ixc: 0,
                ispin: 0,
                minv: 0,
                neldos: 3,
                iscfxc: 11,
            },
            mesh: super::LdosMesh {
                rfms2: -1.0,
                emin: -30.0,
                emax: -29.0,
                eimag: 0.1,
                rgrd: 0.05,
            },
            fms: super::LdosFms {
                rdirec: -1.0,
                toler1: 0.001,
                toler2: 0.001,
            },
            lmaxph: vec![3, 3],
            ldostype: 0,
        };

        let text = ldos_input_string(&input)?;
        assert_eq!(
            text.lines().nth(3),
            Some("     -1.00000    -30.00000     -29.00000      0.10000      0.05000")
        );
        Ok(())
    }

    #[test]
    fn rejects_invalid_ldos_rendering() {
        let input = LdosInput {
            control: super::LdosControl {
                mldos: 1,
                lfms2: 1,
                ixc: 0,
                ispin: 0,
                minv: 0,
                neldos: 101,
                iscfxc: 11,
            },
            mesh: super::LdosMesh {
                rfms2: f64::NAN,
                emin: 0.0,
                emax: 0.0,
                eimag: -1.0,
                rgrd: 0.05,
            },
            fms: super::LdosFms {
                rdirec: 10.0,
                toler1: 0.001,
                toler2: 0.001,
            },
            lmaxph: vec![3],
            ldostype: 0,
        };
        assert!(ldos_input_string(&input).is_err());

        let mut empty_lmax = input;
        empty_lmax.mesh.rfms2 = 5.0;
        empty_lmax.lmaxph.clear();
        assert!(ldos_input_string(&empty_lmax).is_err());
    }
}