diff --git a/Cargo.lock b/Cargo.lock index 40a3fe6..7106458 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,53 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + [[package]] name = "cea-rs" version = "0.1.0" +dependencies = [ + "regex", +] + +[[package]] +name = "memchr" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" + +[[package]] +name = "regex" +version = "1.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" diff --git a/Cargo.toml b/Cargo.toml index 794ff49..287cf8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] +regex = "1" diff --git a/src/properties/mod.rs b/src/properties/mod.rs index 5f3105a..4d0a782 100644 --- a/src/properties/mod.rs +++ b/src/properties/mod.rs @@ -1,8 +1,8 @@ mod data; mod error; -mod thermo_db; +pub mod thermo_db; mod thermo_fit; -mod transport_db; +pub mod transport_db; mod transport_fit; mod utils; diff --git a/src/properties/thermo_db.rs b/src/properties/thermo_db.rs index 09e66b8..96cc957 100644 --- a/src/properties/thermo_db.rs +++ b/src/properties/thermo_db.rs @@ -5,7 +5,6 @@ use crate::properties::{ utils::parse_fields, }; - pub struct ThermoDB { pub products: Vec, pub reactants: Vec, @@ -136,7 +135,7 @@ fn parse_polynomial_block<'a>( let temp_hi: f64 = splits[1] .parse() - .map_err(|_| make_parse_error("temp_hi", "f64", &splits[0]))?; + .map_err(|_| make_parse_error("temp_hi", "f64", &splits[1]))?; // Now parse the first 5 coefficients let line = lines.next().ok_or(PropertiesError::InvalidFile)?; diff --git a/src/properties/transport_db.rs b/src/properties/transport_db.rs index a3860de..2ebb5f5 100644 --- a/src/properties/transport_db.rs +++ b/src/properties/transport_db.rs @@ -24,7 +24,7 @@ impl TransportDB { data.push(parse_species_transport_block(&mut lines)?); if let Some(line) = lines.peek() { - if line.contains("end") { + if line.to_lowercase().contains("end") { break; } } else { @@ -69,7 +69,7 @@ fn parse_species_header_line(line: &str) -> Result<(String, usize, usize), Prope let conductivity_count: usize = splits[4] .parse() - .map_err(|_| make_parse_error("C", "usize", &splits[2]))?; + .map_err(|_| make_parse_error("C", "usize", &splits[4]))?; Ok((name, viscosity_count, conductivity_count)) } diff --git a/src/properties/utils.rs b/src/properties/utils.rs index 7d02a05..cf8d144 100644 --- a/src/properties/utils.rs +++ b/src/properties/utils.rs @@ -1,11 +1,19 @@ +use std::sync::LazyLock; +use regex::Regex; + +// Matches Fortran D exponent (1.23D+04, 1.23D-04, 1.23D 04) and E exponent with a space +// (1.23E 04). Captures: digit before, optional sign, digit after — drops any space. +static FORTRAN_EXP: LazyLock = LazyLock::new(|| { + Regex::new(r"([0-9])[DE]([+\-]?) ?([0-9])").unwrap() +}); + pub fn parse_fields(line: &str, widths: &[usize]) -> Vec { let mut fields = Vec::new(); let mut pos = 0; for &width in widths { if let Some(field) = line.get(pos..pos + width) { - // The replace changes the fortran formatted D exponential for the normal E exponential - fields.push(field.trim().replace("D", "E").replace("E ", "E")); + fields.push(FORTRAN_EXP.replace_all(field.trim(), "${1}E${2}${3}").into_owned()); } pos += width; }