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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
use std::fmt::Write as _;

/// A single fixed-width Fortran column format, as used by FEFF's column
/// layouts (e.g. `E13.6`, `F11.4`, `G15.6`).
///
/// Column layouts are the thing golden byte-for-byte compatibility depends
/// on; naming them with [`FortranField`] turns invisible magic numbers such
/// as `write_fortran_exp(out, *value, 13, 6)` into a documented constant such
/// as `const CHI_ROW_VALUE: FortranField = FortranField::E { width: 13,
/// precision: 6 };`.
///
/// Each variant mirrors one of this module's free formatting functions:
/// [`FortranField::E`] is [`write_fortran_exp`], [`FortranField::F`] is a
/// plain Rust fixed-point field (matching Fortran's `Fw.d`),
/// [`FortranField::G`] is [`write_fortran_g`], and
/// [`FortranField::ZeroScaledE`] is
/// [`write_fortran_zero_scaled_exp_with_exponent_width`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FortranField {
    /// Fortran `Ew.d` scientific field with a two-digit exponent, e.g.
    /// `E13.6`. See [`write_fortran_exp`].
    E {
        /// Total field width in characters.
        width: usize,
        /// Digits after the decimal point.
        precision: usize,
    },
    /// Fortran `Fw.d` fixed-point field, e.g. `F11.4`.
    F {
        /// Total field width in characters.
        width: usize,
        /// Digits after the decimal point.
        precision: usize,
    },
    /// Fortran `Gw.d` field that switches between fixed and scientific
    /// notation, e.g. `G15.6`. See [`write_fortran_g`].
    G {
        /// Total field width in characters.
        width: usize,
        /// Digits after the decimal point.
        precision: usize,
    },
    /// Canonical zero-scaled Fortran `Ew.dEe` field with a configurable
    /// exponent width. See
    /// [`write_fortran_zero_scaled_exp_with_exponent_width`].
    ZeroScaledE {
        /// Total field width in characters.
        width: usize,
        /// Digits after the decimal point.
        precision: usize,
        /// Minimum digits in the exponent.
        exp_width: usize,
    },
}

impl FortranField {
    /// Append `value` to `out` using this field's Fortran format.
    pub fn write(self, out: &mut impl std::fmt::Write, value: f64) -> std::fmt::Result {
        match self {
            FortranField::E { width, precision } => write_fortran_exp(out, value, width, precision),
            FortranField::F { width, precision } => write!(out, "{value:width$.precision$}"),
            FortranField::G { width, precision } => write_fortran_g(out, value, width, precision),
            FortranField::ZeroScaledE {
                width,
                precision,
                exp_width,
            } => write_fortran_zero_scaled_exp_with_exponent_width(
                out, value, width, precision, exp_width,
            ),
        }
    }
}

/// Write one row of Fortran fields and their values in order, joining
/// consecutive fields with the literal `separator` (FEFF's column gap, such
/// as a single space for a `1x` descriptor or an empty string for
/// back-to-back fields). No separator is written before the first field.
pub fn write_fortran_row(
    out: &mut impl std::fmt::Write,
    separator: &str,
    fields: impl IntoIterator<Item = (FortranField, f64)>,
) -> std::fmt::Result {
    let mut first = true;
    for (field, value) in fields {
        if first {
            first = false;
        } else {
            out.write_str(separator)?;
        }
        field.write(out, value)?;
    }
    Ok(())
}

/// FEFF frequently writes fixed-width integer lists using Fortran formats such
/// as `20(1x,i4)`. This helper produces the repeated `1x,iN` form.
pub fn repeated_ints(values: impl IntoIterator<Item = i64>, width: usize) -> String {
    let mut out = String::new();
    for value in values {
        let _ = write!(out, " {value:>width$}");
    }
    out
}

/// Format a float using a Fortran-like scientific notation.
///
/// Rust prints exponents as `e+00`; Fortran output in FEFF is generally
/// accepted with either `e` or `E`, so this keeps lowercase `e` while preserving
/// explicit sign and exponent width.
pub fn exp(value: f64, width: usize, precision: usize) -> String {
    format!("{value:>width$.precision$e}")
}

/// Format a float like a Fortran `Ew.d` field with a two-digit exponent.
#[must_use]
pub fn fortran_exp(value: f64, width: usize, precision: usize) -> String {
    let mut out = String::new();
    let _ = write_fortran_exp(&mut out, value, width, precision);
    out
}

/// Append a float like a Fortran `Ew.d` field with a two-digit exponent.
pub fn write_fortran_exp(
    out: &mut impl std::fmt::Write,
    value: f64,
    width: usize,
    precision: usize,
) -> std::fmt::Result {
    let raw = format!("{value:.precision$E}");
    let Some((mantissa, exponent)) = raw.split_once('E') else {
        return write!(out, "{raw:>width$}");
    };
    let (sign, digits) = match exponent.as_bytes().first() {
        Some(b'-') => ('-', &exponent[1..]),
        Some(b'+') => ('+', &exponent[1..]),
        _ => ('+', exponent),
    };
    let exponent_width = digits.len().max(2);
    let field_width = mantissa.len() + 2 + exponent_width;
    for _ in 0..width.saturating_sub(field_width) {
        out.write_char(' ')?;
    }
    out.write_str(mantissa)?;
    out.write_char('E')?;
    out.write_char(sign)?;
    for _ in 0..exponent_width.saturating_sub(digits.len()) {
        out.write_char('0')?;
    }
    out.write_str(digits)
}

/// Format a float like a canonical Fortran `Ew.d` field.
///
/// This form keeps the mantissa in `[0.1, 1.0)` for non-zero values, matching
/// Fortran output such as `0.3809984030E-01`.
#[must_use]
pub fn fortran_zero_scaled_exp(value: f64, width: usize, precision: usize) -> String {
    let mut out = String::new();
    let _ = write_fortran_zero_scaled_exp(&mut out, value, width, precision);
    out
}

/// Append a float like a canonical Fortran `Ew.d` field.
pub fn write_fortran_zero_scaled_exp(
    out: &mut impl std::fmt::Write,
    value: f64,
    width: usize,
    precision: usize,
) -> std::fmt::Result {
    write_fortran_zero_scaled_exp_with_exponent_width(out, value, width, precision, 2)
}

/// Append a canonical Fortran `Ew.dEe` field with a requested exponent width.
pub fn write_fortran_zero_scaled_exp_with_exponent_width(
    out: &mut impl std::fmt::Write,
    value: f64,
    width: usize,
    precision: usize,
    min_exponent_width: usize,
) -> std::fmt::Result {
    let exponent = if value == 0.0 {
        0
    } else {
        value.abs().log10().floor() as i32 + 1
    };
    // Preserve IEEE-754 signed zero. FEFF/gfortran emits `-0.000...` for a
    // negative zero, and dropping that sign breaks byte-exact handoff
    // roundtrips even though the numeric values compare equal.
    let mantissa = if value == 0.0 {
        value
    } else {
        value / 10.0_f64.powi(exponent)
    };
    let mantissa = format!("{mantissa:.precision$}");
    let sign = if exponent < 0 { '-' } else { '+' };
    let exponent_digits = exponent.abs().to_string();
    let exponent_width = exponent_digits.len().max(min_exponent_width);
    let field_width = mantissa.len() + 2 + exponent_width;
    for _ in 0..width.saturating_sub(field_width) {
        out.write_char(' ')?;
    }
    out.write_str(&mantissa)?;
    out.write_char('E')?;
    out.write_char(sign)?;
    for _ in 0..exponent_width.saturating_sub(exponent_digits.len()) {
        out.write_char('0')?;
    }
    out.write_str(&exponent_digits)
}

/// Format a float like a Fortran `Gw.d` field.
///
/// This covers the FEFF-compatible fixed/scientific switch used by output such
/// as `G14.6`: fixed notation for magnitudes in `[0.1, 10^d)` and canonical
/// zero-scaled scientific notation otherwise.
#[must_use]
pub fn fortran_g(value: f64, width: usize, precision: usize) -> String {
    let mut out = String::new();
    let _ = write_fortran_g(&mut out, value, width, precision);
    out
}

/// Append a float like a Fortran `Gw.d` field.
pub fn write_fortran_g(
    out: &mut impl std::fmt::Write,
    value: f64,
    width: usize,
    precision: usize,
) -> std::fmt::Result {
    let magnitude = value.abs();
    let fixed_upper = 10.0_f64.powi(precision as i32);
    if value == 0.0 || ((0.1..fixed_upper).contains(&magnitude)) {
        let digits_before_decimal = if magnitude >= 1.0 {
            magnitude.log10().floor() as usize + 1
        } else {
            0
        };
        let decimals = if value == 0.0 {
            precision.saturating_sub(1)
        } else if magnitude < 1.0 {
            precision
        } else {
            precision.saturating_sub(digits_before_decimal)
        };
        let mut fixed = format!("{value:.decimals$}");
        if decimals == 0 && !fixed.contains('.') {
            fixed.push('.');
        }
        let fixed_width = width.saturating_sub(4);
        for _ in 0..fixed_width.saturating_sub(fixed.len()) {
            out.write_char(' ')?;
        }
        out.write_str(&fixed)?;
        for _ in 0..width.saturating_sub(fixed_width) {
            out.write_char(' ')?;
        }
        Ok(())
    } else {
        write_fortran_zero_scaled_exp(out, value, width, precision)
    }
}

pub fn repeated_exp(
    values: impl IntoIterator<Item = f64>,
    width: usize,
    precision: usize,
) -> String {
    let mut out = String::new();
    for value in values {
        out.push(' ');
        out.push_str(&exp(value, width, precision));
    }
    out
}

/// Format one `f64` as gfortran writes a double precision value in a
/// list-directed `WRITE(unit,*)` record.
///
/// FEFF uses this implicit format for a few scalar handoff files. gfortran
/// emits each double in a 26-character field, using fixed notation for
/// magnitudes in `[0.1, 1e17)` and scientific notation otherwise.
#[must_use]
pub fn fortran_list_directed_f64(value: f64) -> String {
    let magnitude = value.abs();
    if magnitude != 0.0 && !(0.1..1.0e17).contains(&magnitude) {
        let scientific = fortran_list_directed_exponent(value);
        format!("{scientific:>26}")
    } else {
        let decimals = list_directed_decimal_places(magnitude);
        let mut fixed = format!("{value:.decimals$}");
        if decimals == 0 {
            fixed.push('.');
        }
        format!("{fixed:>21}     ")
    }
}

/// Format one `f64` as the 24-column list-directed record used by older FEFF
/// modules such as COMPTON and CRPA.
///
/// Those routines write double precision values with 15 significant digits.
/// Fixed-form values occupy the first 19 columns and leave five trailing
/// blanks; scientific values occupy the full 24-column field with a
/// three-digit exponent.
#[must_use]
pub fn fortran_list_directed_g15_f64(value: f64) -> String {
    let magnitude = value.abs();
    if magnitude != 0.0 && (0.1..1.0e15).contains(&magnitude) {
        let decimals = list_directed_g15_decimal_places(magnitude);
        let mut fixed = format!("{value:.decimals$}");
        if decimals == 0 && !fixed.contains('.') {
            fixed.push('.');
        }
        format!("{fixed:>19}     ")
    } else {
        let scientific = fortran_list_directed_g15_exponent(value);
        format!("{scientific:>24}")
    }
}

fn list_directed_decimal_places(magnitude: f64) -> usize {
    if magnitude == 0.0 {
        16
    } else if magnitude < 1.0 {
        17
    } else {
        let digits_before_decimal = magnitude.log10().floor() as usize + 1;
        17_usize.saturating_sub(digits_before_decimal)
    }
}

fn list_directed_g15_decimal_places(magnitude: f64) -> usize {
    if magnitude < 1.0 {
        15
    } else {
        let digits_before_decimal = magnitude.log10().floor() as usize + 1;
        15_usize.saturating_sub(digits_before_decimal)
    }
}

fn fortran_list_directed_exponent(value: f64) -> String {
    let raw = format!("{value:.16E}");
    let Some((mantissa, exponent)) = raw.split_once('E') else {
        return raw;
    };
    let (sign, digits) = match exponent.as_bytes().first() {
        Some(b'-') => ('-', &exponent[1..]),
        Some(b'+') => ('+', &exponent[1..]),
        _ => ('+', exponent),
    };
    match digits.parse::<u32>() {
        Ok(exponent) => format!("{mantissa}E{sign}{exponent:03}"),
        Err(_) => raw,
    }
}

fn fortran_list_directed_g15_exponent(value: f64) -> String {
    let raw = format!("{value:.15E}");
    let Some((mantissa, exponent)) = raw.split_once('E') else {
        return raw;
    };
    let (sign, digits) = match exponent.as_bytes().first() {
        Some(b'-') => ('-', &exponent[1..]),
        Some(b'+') => ('+', &exponent[1..]),
        _ => ('+', exponent),
    };
    match digits.parse::<u32>() {
        Ok(exponent) => format!("{mantissa}E{sign}{exponent:03}"),
        Err(_) => raw,
    }
}

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

    #[test]
    fn fortran_field_write_matches_free_functions() {
        let mut e_field = String::new();
        assert!(
            FortranField::E {
                width: 13,
                precision: 6
            }
            .write(&mut e_field, -18.69)
            .is_ok()
        );
        assert_eq!(e_field, fortran_exp(-18.69, 13, 6));

        let mut f_field = String::new();
        assert!(
            FortranField::F {
                width: 11,
                precision: 4
            }
            .write(&mut f_field, 0.0)
            .is_ok()
        );
        assert_eq!(f_field, format!("{:11.4}", 0.0_f64));

        let mut g_field = String::new();
        assert!(
            FortranField::G {
                width: 14,
                precision: 6
            }
            .write(&mut g_field, 8979.41)
            .is_ok()
        );
        assert_eq!(g_field, fortran_g(8979.41, 14, 6));

        let mut zero_scaled_field = String::new();
        assert!(
            FortranField::ZeroScaledE {
                width: 20,
                precision: 10,
                exp_width: 3
            }
            .write(&mut zero_scaled_field, -0.293_644_216_9)
            .is_ok()
        );
        let mut expected = String::new();
        assert!(
            write_fortran_zero_scaled_exp_with_exponent_width(
                &mut expected,
                -0.293_644_216_9,
                20,
                10,
                3,
            )
            .is_ok()
        );
        assert_eq!(zero_scaled_field, expected);
    }

    #[test]
    fn write_fortran_row_joins_fields_with_separator() {
        let mut out = String::new();
        let field = FortranField::E {
            width: 13,
            precision: 6,
        };
        assert!(
            write_fortran_row(&mut out, " ", [(field, 1.5), (field, -2.5), (field, 0.0)]).is_ok()
        );
        assert_eq!(
            out,
            format!(
                "{} {} {}",
                fortran_exp(1.5, 13, 6),
                fortran_exp(-2.5, 13, 6),
                fortran_exp(0.0, 13, 6)
            )
        );
    }

    #[test]
    fn write_fortran_row_supports_empty_separator() {
        let mut out = String::new();
        assert!(
            write_fortran_row(
                &mut out,
                "",
                [
                    (
                        FortranField::F {
                            width: 12,
                            precision: 3
                        },
                        11076.317
                    ),
                    (
                        FortranField::F {
                            width: 11,
                            precision: 3
                        },
                        -40.0
                    ),
                ]
            )
            .is_ok()
        );
        assert_eq!(out, format!("{:12.3}{:11.3}", 11076.317_f64, -40.0_f64));
    }

    #[test]
    fn formats_repeated_ints_like_fortran_list() {
        assert_eq!(repeated_ints([1, -2, 345], 4), "    1   -2  345");
    }

    #[test]
    fn formats_fixed_width_exponents() {
        assert_eq!(exp(12.5, 14, 7), "   1.2500000e1");
    }

    #[test]
    fn formats_fortran_style_e_fields() {
        assert_eq!(fortran_exp(1.5073e-4, 12, 4), "  1.5073E-04");
        assert_eq!(fortran_exp(-0.7625, 12, 4), " -7.6250E-01");
        assert_eq!(fortran_exp(0.0, 12, 4), "  0.0000E+00");
        assert_eq!(fortran_exp(12_345.0, 12, 4), "  1.2345E+04");
        let mut out = String::from("prefix");
        assert!(write_fortran_exp(&mut out, -18.69, 11, 4).is_ok());
        assert_eq!(out, "prefix-1.8690E+01");
    }

    #[test]
    fn formats_zero_scaled_fortran_e_fields() {
        assert_eq!(
            fortran_zero_scaled_exp(0.038_099_840_3, 20, 10),
            "    0.3809984030E-01"
        );
        assert_eq!(fortran_zero_scaled_exp(-4.3629, 13, 5), " -0.43629E+01");
        assert_eq!(fortran_zero_scaled_exp(0.0, 13, 5), "  0.00000E+00");
        assert_eq!(fortran_zero_scaled_exp(-0.0, 13, 5), " -0.00000E+00");
        let mut explicit = String::new();
        assert!(
            write_fortran_zero_scaled_exp_with_exponent_width(
                &mut explicit,
                -0.293_644_216_9,
                20,
                10,
                3,
            )
            .is_ok()
        );
        assert_eq!(explicit, "  -0.2936442169E+000");
    }

    #[test]
    fn formats_fortran_g_fields() {
        assert_eq!(fortran_g(8979.41, 14, 6), "   8979.41    ");
        assert_eq!(fortran_g(273.822, 14, 6), "   273.822    ");
        assert_eq!(fortran_g(276.260, 14, 6), "   276.260    ");
        assert_eq!(fortran_g(100.0, 14, 6), "   100.000    ");
        assert_eq!(fortran_g(0.0, 14, 6), "   0.00000    ");
        assert_eq!(fortran_g(0.1, 14, 6), "  0.100000    ");
        assert_eq!(fortran_g(999_999.0, 14, 6), "   999999.    ");
        assert_eq!(fortran_g(1.0e6, 14, 6), "  0.100000E+07");
        assert_eq!(fortran_g(0.308_730e-7, 14, 6), "  0.308730E-07");
    }

    #[test]
    fn formats_list_directed_double_fields_like_gfortran() {
        assert_eq!(fortran_list_directed_f64(0.0), "   0.0000000000000000     ");
        assert_eq!(
            fortran_list_directed_f64(330.319_156_029_843_7),
            "   330.31915602984373     "
        );
        assert_eq!(
            fortran_list_directed_f64(6.354_647_093_099_486e-2),
            "   6.3546470930994858E-002"
        );
        assert_eq!(
            fortran_list_directed_f64(-7.729_278_779_143_69),
            "  -7.7292787791436899     "
        );
        assert_eq!(
            fortran_list_directed_f64(1.0e20),
            "   1.0000000000000000E+020"
        );
        assert_eq!(
            fortran_list_directed_f64(1.0e16),
            "   10000000000000000.     "
        );
        assert_eq!(fortran_list_directed_f64(0.1), "  0.10000000000000001     ");
    }

    #[test]
    fn formats_legacy_g15_list_directed_double_fields_like_feff() {
        assert_eq!(
            fortran_list_directed_g15_f64(0.0),
            "  0.000000000000000E+000"
        );
        assert_eq!(
            fortran_list_directed_g15_f64(5.005_004_815_757_275e-3),
            "  5.005004815757275E-003"
        );
        assert_eq!(
            fortran_list_directed_g15_f64(2.744_767_348_503_43),
            "   2.74476734850343     "
        );
        assert_eq!(
            fortran_list_directed_g15_f64(-0.167_258_739_984_332),
            " -0.167258739984332     "
        );
        assert_eq!(
            fortran_list_directed_g15_f64(1.0),
            "   1.00000000000000     "
        );
    }
}