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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//! Parser for FEFF `dmdw.out` reports.

use crate::error::Result;

use super::common::{parse_error, parse_error_value};
use super::types::{
    DmdwOutData, DmdwOutEinstein, DmdwOutHeader, DmdwOutMoment, DmdwOutPole, DmdwOutSection,
    DmdwOutSubject, DmdwOutTemperature, DmdwOutTemperatureValue,
};
use super::validate::validate_dmdw_out;

/// Parse FEFF `dmdw.out` text.
pub fn parse_dmdw_out(text: &str) -> Result<DmdwOutData> {
    if text.trim().is_empty() {
        return Ok(DmdwOutData {
            header: None,
            mass_enhancement_header: false,
            sections: Vec::new(),
        });
    }

    let lines = text
        .lines()
        .enumerate()
        .map(|(index, raw)| (index + 1, raw.trim()))
        .collect::<Vec<_>>();
    let mut position = 0;
    skip_blank_lines(&lines, &mut position);
    let header = parse_header(&lines, &mut position)?;
    let mass_enhancement_header = parse_mass_enhancement_header(&lines, &mut position);

    let mut sections = Vec::new();
    while position < lines.len() {
        skip_section_gap(&lines, &mut position);
        if position >= lines.len() {
            break;
        }
        sections.push(parse_section(&lines, &mut position)?);
    }

    let data = DmdwOutData {
        header: Some(header),
        mass_enhancement_header,
        sections,
    };
    validate_dmdw_out(&data)?;
    Ok(data)
}

fn parse_header(lines: &[(usize, &str)], position: &mut usize) -> Result<DmdwOutHeader> {
    let (order_line, order_text) = next_line(lines, position, "Lanczos recursion order")?;
    let order = parse_prefixed_usize(order_line, order_text, "# Lanczos recursion order:")?;

    let (temperature_line, temperature_text) = next_line(lines, position, "temperature")?;
    let temperature_value =
        parse_prefixed_tail(temperature_line, temperature_text, "# Temperature:")?;
    let temperature = if temperature_value.eq_ignore_ascii_case("(See list Below)") {
        DmdwOutTemperature::ListedBelow
    } else {
        DmdwOutTemperature::Single(parse_f64(
            temperature_line,
            "temperature",
            temperature_value,
        )?)
    };

    let (dym_line, dym_text) = next_line(lines, position, "dynamical matrix file")?;
    let dynamical_matrix_file =
        parse_prefixed_tail(dym_line, dym_text, "# Dynamical matrix file:")?.to_owned();

    Ok(DmdwOutHeader {
        lanczos_recursion_order: order,
        temperature,
        dynamical_matrix_file,
    })
}

fn parse_mass_enhancement_header(lines: &[(usize, &str)], position: &mut usize) -> bool {
    if *position >= lines.len() {
        return false;
    }
    if lines[*position].1 == "Mass Enchancement Factor" {
        *position += 1;
        true
    } else {
        false
    }
}

fn parse_section(lines: &[(usize, &str)], position: &mut usize) -> Result<DmdwOutSection> {
    let (line_number, line) = lines[*position];
    let subject = parse_subject(line_number, line)?;
    *position += 1;

    let mut section = DmdwOutSection::new(subject);
    if matches!(section.subject, DmdwOutSubject::AtomIndex { .. }) {
        parse_optional_direction(lines, position, &mut section)?;
    }

    while *position < lines.len() {
        let (current_line, current_text) = lines[*position];
        if current_text.is_empty() || is_separator_line(current_text) {
            *position += 1;
            continue;
        }
        if is_section_start(current_text) {
            break;
        }

        if current_text.starts_with("PDOS Poles:") {
            *position += 1;
            section.pdos_poles = parse_pdos_poles(lines, position)?;
        } else if current_text.starts_with("PDOS Einstein freq") {
            *position += 1;
            section.einstein = Some(parse_einstein(lines, position)?);
        } else if current_text.starts_with("pDOS n Moments") {
            *position += 1;
            section.moments = parse_moments(lines, position)?;
        } else if current_text.starts_with("Path Red. Mass (AMU):") {
            section.reduced_mass_amu = Some(parse_single_value_after_colon(
                current_line,
                current_text,
                "reduced_mass_amu",
            )?);
            *position += 1;
        } else if current_text.starts_with("Path Length (Ang), s^2") {
            let values = parse_values_after_colon(current_line, current_text)?;
            if values.len() != 2 {
                return parse_error(
                    current_line,
                    format!(
                        "path length/sigma2 row has {} value(s), expected 2",
                        values.len()
                    ),
                );
            }
            section.path_length_angstrom = Some(values[0]);
            section.sigma2_1e_minus_3_angstrom2 = Some(values[1]);
            *position += 1;
        } else if current_text.starts_with("Path Length (Ang):") {
            section.path_length_angstrom = Some(parse_single_value_after_colon(
                current_line,
                current_text,
                "path_length_angstrom",
            )?);
            *position += 1;
            skip_blank_lines(lines, position);
            if *position < lines.len() && lines[*position].1.starts_with("Temp (K)") {
                *position += 1;
                section.sigma2_by_temperature =
                    parse_temperature_values(lines, position, "sigma2")?;
            }
        } else if current_text.starts_with("VFE (eV):") {
            section.vibrational_free_energy_ev = Some(parse_single_value_after_colon(
                current_line,
                current_text,
                "vibrational_free_energy_ev",
            )?);
            *position += 1;
        } else if current_text.starts_with("Temp (K)") && current_text.contains("VFE") {
            *position += 1;
            section.vibrational_free_energy_by_temperature =
                parse_temperature_values(lines, position, "vibrational_free_energy_ev")?;
        } else if current_text.starts_with("u^2 (1e-3 Ang^2):") {
            section.u2_1e_minus_3_angstrom2 = Some(parse_single_value_after_colon(
                current_line,
                current_text,
                "u2_1e_minus_3_angstrom2",
            )?);
            *position += 1;
        } else if current_text.starts_with("Temp (K)") && current_text.contains("u^2") {
            *position += 1;
            section.u2_by_temperature = parse_temperature_values(lines, position, "u2")?;
        } else if current_text.starts_with("Projected DOS component computed.") {
            section.projected_dos_component_computed = true;
            *position += 1;
        } else {
            return parse_error(
                current_line,
                format!("unexpected DMDW output line {current_text:?}"),
            );
        }
    }

    Ok(section)
}

fn parse_subject(line_number: usize, line: &str) -> Result<DmdwOutSubject> {
    if let Some(tail) = line.strip_prefix("Path Indices:") {
        return Ok(DmdwOutSubject::PathIndices(parse_index_list(
            line_number,
            tail,
        )?));
    }
    if let Some(tail) = line.strip_prefix("Atom Index:") {
        return Ok(DmdwOutSubject::AtomIndex {
            indices: parse_index_list(line_number, tail)?,
            direction: None,
        });
    }
    if line == "Total PDOS results:" {
        return Ok(DmdwOutSubject::TotalPdos);
    }
    if line == "Total VFE for the paths requested:" {
        return Ok(DmdwOutSubject::TotalVfe);
    }
    parse_error(
        line_number,
        format!("expected DMDW section header, found {line:?}"),
    )
}

fn parse_optional_direction(
    lines: &[(usize, &str)],
    position: &mut usize,
    section: &mut DmdwOutSection,
) -> Result<()> {
    if *position >= lines.len() {
        return Ok(());
    }
    let (line_number, line) = lines[*position];
    if !line.contains("Direction") {
        return Ok(());
    }
    let mut tokens = line.split_whitespace();
    let direction = tokens
        .find(|token| token.eq_ignore_ascii_case("Direction"))
        .and_then(|_| tokens.next())
        .filter(|token| token.chars().any(|ch| ch != '-'))
        .map(ToOwned::to_owned)
        .ok_or_else(|| parse_error_value(line_number, "missing atom perturbation direction"))?;
    if let DmdwOutSubject::AtomIndex {
        direction: slot, ..
    } = &mut section.subject
    {
        *slot = Some(direction);
    }
    *position += 1;
    Ok(())
}

fn parse_pdos_poles(lines: &[(usize, &str)], position: &mut usize) -> Result<Vec<DmdwOutPole>> {
    skip_blank_lines(lines, position);
    if *position < lines.len() && lines[*position].1.starts_with("Freq.") {
        *position += 1;
    }

    let mut poles = Vec::new();
    while *position < lines.len() {
        let (line_number, line) = lines[*position];
        if line.is_empty() {
            *position += 1;
            break;
        }
        if is_section_start(line) || is_section_gap_or_label(line) {
            break;
        }

        let tokens = line.split_whitespace().collect::<Vec<_>>();
        if tokens.len() != 2 {
            return parse_error(
                line_number,
                format!("PDOS pole row has {} token(s), expected 2", tokens.len()),
            );
        }
        poles.push(DmdwOutPole {
            frequency_thz: parse_f64(line_number, "pole_frequency_thz", tokens[0])?,
            weight: parse_f64(line_number, "pole_weight", tokens[1])?,
        });
        *position += 1;
    }
    Ok(poles)
}

fn parse_einstein(lines: &[(usize, &str)], position: &mut usize) -> Result<DmdwOutEinstein> {
    skip_blank_lines(lines, position);
    if *position < lines.len() && lines[*position].1.starts_with("Freq (THz)") {
        *position += 1;
    }
    let (line_number, line) = next_data_line(lines, position, "Einstein row")?;
    let tokens = line.split_whitespace().collect::<Vec<_>>();
    if tokens.len() != 3 {
        return parse_error(
            line_number,
            format!("Einstein row has {} token(s), expected 3", tokens.len()),
        );
    }
    Ok(DmdwOutEinstein {
        frequency_thz: parse_f64(line_number, "einstein_frequency_thz", tokens[0])?,
        temperature_kelvin: parse_f64(line_number, "einstein_temperature_kelvin", tokens[1])?,
        effective_force_constant_n_per_m: parse_f64(
            line_number,
            "einstein_effective_force_constant_n_per_m",
            tokens[2],
        )?,
    })
}

fn parse_moments(lines: &[(usize, &str)], position: &mut usize) -> Result<Vec<DmdwOutMoment>> {
    skip_blank_lines(lines, position);
    if *position < lines.len() && lines[*position].1.starts_with('n') {
        *position += 1;
    }

    let mut moments = Vec::new();
    while *position < lines.len() {
        let (line_number, line) = lines[*position];
        if line.is_empty() {
            *position += 1;
            break;
        }
        if is_section_start(line) || is_section_gap_or_label(line) {
            break;
        }

        let tokens = line.split_whitespace().collect::<Vec<_>>();
        if tokens.len() < 2 || tokens.len() > 5 {
            return parse_error(
                line_number,
                format!("moment row has {} token(s), expected 2 to 5", tokens.len()),
            );
        }
        moments.push(DmdwOutMoment {
            order: parse_i32(line_number, "moment_order", tokens[0])?,
            moment_thz_power_n: parse_f64(line_number, "moment_thz_power_n", tokens[1])?,
            frequency_thz: parse_optional_f64(
                line_number,
                "moment_frequency_thz",
                tokens.get(2).copied(),
            )?,
            temperature_kelvin: parse_optional_f64(
                line_number,
                "moment_temperature_kelvin",
                tokens.get(3).copied(),
            )?,
            effective_force_constant_n_per_m: parse_optional_f64(
                line_number,
                "moment_effective_force_constant_n_per_m",
                tokens.get(4).copied(),
            )?,
        });
        *position += 1;
    }
    Ok(moments)
}

fn parse_temperature_values(
    lines: &[(usize, &str)],
    position: &mut usize,
    value_field: &'static str,
) -> Result<Vec<DmdwOutTemperatureValue>> {
    let mut rows = Vec::new();
    while *position < lines.len() {
        let (line_number, line) = lines[*position];
        if line.is_empty() {
            *position += 1;
            break;
        }
        if is_section_start(line) || is_section_gap_or_label(line) {
            break;
        }

        let tokens = line.split_whitespace().collect::<Vec<_>>();
        if tokens.len() != 2 {
            return parse_error(
                line_number,
                format!(
                    "temperature result row has {} token(s), expected 2",
                    tokens.len()
                ),
            );
        }
        rows.push(DmdwOutTemperatureValue {
            temperature_kelvin: parse_f64(line_number, "temperature_kelvin", tokens[0])?,
            value: parse_f64(line_number, value_field, tokens[1])?,
        });
        *position += 1;
    }
    Ok(rows)
}

fn next_data_line<'a>(
    lines: &'a [(usize, &'a str)],
    position: &mut usize,
    description: &'static str,
) -> Result<(usize, &'a str)> {
    while *position < lines.len() {
        let (line_number, line) = lines[*position];
        *position += 1;
        if line.is_empty() {
            continue;
        }
        if is_section_start(line) || is_section_gap_or_label(line) {
            return parse_error(line_number, format!("missing {description}"));
        }
        return Ok((line_number, line));
    }
    parse_error(0, format!("missing {description}"))
}

fn parse_prefixed_usize(line: usize, text: &str, prefix: &'static str) -> Result<usize> {
    let token = parse_prefixed_tail(line, text, prefix)?;
    parse_usize(line, "lanczos_recursion_order", token)
}

fn parse_prefixed_tail<'a>(line: usize, text: &'a str, prefix: &'static str) -> Result<&'a str> {
    text.strip_prefix(prefix)
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .ok_or_else(|| parse_error_value(line, format!("expected {prefix:?} line")))
}

fn parse_index_list(line: usize, tail: &str) -> Result<Vec<usize>> {
    let indices = tail
        .split_whitespace()
        .map(|token| parse_usize(line, "index", token))
        .collect::<Result<Vec<_>>>()?;
    if indices.is_empty() {
        return parse_error(line, "section index list must not be empty");
    }
    if indices.contains(&0) {
        return parse_error(line, "section indices must be positive");
    }
    Ok(indices)
}

fn parse_single_value_after_colon(line: usize, text: &str, field: &'static str) -> Result<f64> {
    let values = parse_values_after_colon(line, text)?;
    if values.len() != 1 {
        return parse_error(
            line,
            format!("{field} line has {} value(s), expected 1", values.len()),
        );
    }
    Ok(values[0])
}

fn parse_values_after_colon(line: usize, text: &str) -> Result<Vec<f64>> {
    let (_, tail) = text
        .split_once(':')
        .ok_or_else(|| parse_error_value(line, "expected ':' separator"))?;
    let values = tail
        .split_whitespace()
        .map(|token| parse_f64(line, "value", token))
        .collect::<Result<Vec<_>>>()?;
    if values.is_empty() {
        return parse_error(line, "expected numeric value after ':'");
    }
    Ok(values)
}

fn next_line<'a>(
    lines: &'a [(usize, &'a str)],
    position: &mut usize,
    description: &'static str,
) -> Result<(usize, &'a str)> {
    let Some(line) = lines.get(*position).copied() else {
        return parse_error(0, format!("missing {description} line"));
    };
    *position += 1;
    Ok(line)
}

fn skip_blank_lines(lines: &[(usize, &str)], position: &mut usize) {
    while *position < lines.len() && lines[*position].1.is_empty() {
        *position += 1;
    }
}

fn skip_section_gap(lines: &[(usize, &str)], position: &mut usize) {
    while *position < lines.len() {
        let line = lines[*position].1;
        if line.is_empty() || is_separator_line(line) {
            *position += 1;
        } else {
            break;
        }
    }
}

fn is_section_start(line: &str) -> bool {
    line.starts_with("Path Indices:")
        || line.starts_with("Atom Index:")
        || line == "Total PDOS results:"
        || line == "Total VFE for the paths requested:"
}

fn is_section_gap_or_label(line: &str) -> bool {
    is_separator_line(line)
        || line.starts_with("PDOS Poles:")
        || line.starts_with("PDOS Einstein freq")
        || line.starts_with("pDOS n Moments")
        || line.starts_with("Path Red. Mass (AMU):")
        || line.starts_with("Path Length (Ang)")
        || line.starts_with("VFE (eV):")
        || line.starts_with("Temp (K)")
        || line.starts_with("u^2 (1e-3 Ang^2):")
        || line.starts_with("Projected DOS component computed.")
}

fn is_separator_line(line: &str) -> bool {
    let trimmed = line.trim();
    trimmed.len() >= 3 && trimmed.chars().all(|ch| ch == '-')
}

fn parse_optional_f64(
    line: usize,
    field: &'static str,
    token: Option<&str>,
) -> Result<Option<f64>> {
    match token {
        Some(value) if is_placeholder(value) => Ok(None),
        Some(value) => parse_f64(line, field, value).map(Some),
        None => Ok(None),
    }
}

fn is_placeholder(token: &str) -> bool {
    !token.is_empty() && token.chars().all(|ch| ch == '-')
}

fn parse_f64(line: usize, field: &'static str, token: &str) -> Result<f64> {
    token
        .replace(['D', 'd'], "E")
        .parse::<f64>()
        .map_err(|_| parse_error_value(line, format!("could not parse {field} from {token:?}")))
}

fn parse_i32(line: usize, field: &'static str, token: &str) -> Result<i32> {
    token
        .parse::<i32>()
        .map_err(|_| parse_error_value(line, format!("could not parse {field} from {token:?}")))
}

fn parse_usize(line: usize, field: &'static str, token: &str) -> Result<usize> {
    token
        .parse::<usize>()
        .map_err(|_| parse_error_value(line, format!("could not parse {field} from {token:?}")))
}