Skip to main content

nucleide_material/
compendium.rs

1//! DOE/PNNL Materials Compendium ingestion (Revision 2, 411 materials).
2//!
3//! Parses the official `MaterialsCompendium.json` of the DOE/PNNL
4//! Materials Compendium, Revision 2 (companion license vendored under
5//! `fixtures/data/`) into typed entries convertible to [`Material`]s with
6//! full isotopic weight fractions.
7//!
8//! Schema notes (verified against the dataset):
9//! - top level `{siteVersion: String, data: [entry; 411]}`;
10//! - every entry carries `Elements[].Isotopes[]` — no degenerate cases;
11//! - `Isotopes[].WeightFraction` is the material-level mass fraction
12//!   (element-level fractions live in `IsotopicWeightFraction`);
13//! - `ZAID` is numeric (`1001`, `95242`-style metastables absent here);
14//! - names and MatNum values are unique across all 411 entries.
15
16use std::collections::BTreeMap;
17use std::path::Path;
18
19use serde::Deserialize;
20
21use crate::material::Material;
22use nucleide_nuclei::NuclideId;
23
24/// Errors from compendium loading.
25#[derive(Debug, Clone, PartialEq)]
26pub enum Error {
27    /// Reading the compendium file failed.
28    Io(String),
29    /// The text was not valid compendium JSON.
30    Json(String),
31}
32
33impl std::fmt::Display for Error {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        match self {
36            Error::Io(m) => write!(f, "io error: {m}"),
37            Error::Json(m) => write!(f, "compendium JSON error: {m}"),
38        }
39    }
40}
41impl std::error::Error for Error {}
42
43/// Accept either a JSON string or an array of strings (the upstream dataset
44/// is inconsistent: most entries use arrays, some a single bare string).
45fn string_or_vec<'de, D>(de: D) -> Result<Vec<String>, D::Error>
46where
47    D: serde::Deserializer<'de>,
48{
49    struct V;
50    impl<'de2> serde::de::Visitor<'de2> for V {
51        type Value = Vec<String>;
52        fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53            f.write_str("string or list of strings")
54        }
55        fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {
56            Ok(vec![v.to_string()])
57        }
58        fn visit_seq<S: serde::de::SeqAccess<'de2>>(
59            self,
60            mut seq: S,
61        ) -> Result<Self::Value, S::Error> {
62            let mut out = Vec::new();
63            while let Some(s) = seq.next_element::<String>()? {
64                out.push(s);
65            }
66            Ok(out)
67        }
68    }
69    de.deserialize_any(V)
70}
71
72/// One isotope row of an element inside a compendium material.
73#[derive(Debug, Clone, Deserialize)]
74pub struct CompendiumIsotope {
75    /// Isotope name as given in the dataset (`"H1"`-style).
76    #[serde(rename = "Isotope")]
77    pub isotope: String,
78    /// MCNP-style ZAID; serialized as a JSON string upstream ("1001").
79    #[serde(rename = "ZAID")]
80    pub zaid: String,
81    /// Natural abundance of the isotope within its element (isotopic atom
82    /// fraction as tabulated upstream).
83    #[serde(rename = "Abundance")]
84    pub abundance: f64,
85    /// Mass fraction within the parent element.
86    #[serde(rename = "IsotopicWeightFraction")]
87    pub isotopic_weight_fraction: f64,
88    /// Mass fraction of the whole material.
89    #[serde(rename = "WeightFraction")]
90    pub weight_fraction: f64,
91}
92
93/// One elemental constituent.
94#[derive(Debug, Clone, Deserialize)]
95pub struct CompendiumElement {
96    /// Element symbol (`"H"`, `"Fe"`).
97    #[serde(rename = "Element")]
98    pub element: String,
99    /// Atom fraction of the element in the whole material.
100    #[serde(rename = "AtomFraction")]
101    pub atom_fraction: f64,
102    /// Isotope rows of this element.
103    #[serde(rename = "Isotopes")]
104    pub isotopes: Vec<CompendiumIsotope>,
105}
106
107/// One compendium material entry.
108#[derive(Debug, Clone, Deserialize)]
109pub struct CompendiumEntry {
110    /// Display name (`"Bone Equivalent Plastic, B-110"`).
111    #[serde(rename = "Name")]
112    pub name: String,
113    /// Acronym(s) of the material; the upstream field mixes bare strings and
114    /// arrays, so this is always a vector.
115    #[serde(rename = "Acronym", default, deserialize_with = "string_or_vec")]
116    pub acronym: Vec<String>,
117    /// Compendium material number (unique across the dataset).
118    #[serde(rename = "MatNum")]
119    pub mat_num: u32,
120    /// Nominal density [g/cm³].
121    #[serde(rename = "Density")]
122    pub density: f64,
123    /// Nominal atom density [atoms/barn·cm]; 0.0 when the field is absent.
124    #[serde(rename = "MaterialAtomDensity", default)]
125    pub atom_density: f64,
126    /// Provenance string (e.g. `"PNNL"`); empty when absent.
127    #[serde(rename = "Source", default)]
128    pub source: String,
129    /// Free-form comment lines; upstream mixes bare strings and arrays, so
130    /// this is always a vector.
131    #[serde(rename = "Comment", default, deserialize_with = "string_or_vec")]
132    pub comment: Vec<String>,
133    /// Elemental constituents of the material.
134    #[serde(rename = "Elements")]
135    pub elements: Vec<CompendiumElement>,
136}
137
138impl CompendiumEntry {
139    /// All (isotope ZAID → material-level weight fraction) pairs.
140    pub fn weight_fractions(&self) -> BTreeMap<u32, f64> {
141        let mut out = BTreeMap::new();
142        for el in &self.elements {
143            for iso in &el.isotopes {
144                let zaid: u32 = match iso.zaid.parse() {
145                    Ok(v) => v,
146                    Err(_) => continue,
147                };
148                *out.entry(zaid).or_insert(0.0) += iso.weight_fraction;
149            }
150        }
151        out
152    }
153
154    /// Convert to a [`Material`] whose composition holds relative masses
155    /// equal to the isotopic weight fractions (normalized to 1 g total).
156    ///
157    /// Density and provenance are attached as metadata; set the real mass or
158    /// density separately when building transport inputs.
159    pub fn to_material(&self) -> Result<Material, Error> {
160        use nucleide_nuclei::dialects;
161        let mut mat = Material::new();
162        for (zaid, wf) in self.weight_fractions() {
163            if wf <= 0.0 {
164                continue;
165            }
166            // Natural-element zaids (AAA == 0) cannot map to a single
167            // nuclide; keep them as placeholder ground-state ids like the
168            // mcnp-io inp convention.
169            let id = dialects::from_zaid(zaid)
170                .unwrap_or_else(|_| NuclideId::from_nucid((zaid / 1000) * 10_000_000));
171            mat.add_nuclide(id, wf);
172        }
173        mat.set_metadata(Some(serde_json::json!({
174            "source": "DOE/PNNL Materials Compendium Rev.2",
175            "acronym": self.acronym,
176            "mat_num": self.mat_num,
177            "density_g_cm3": self.density,
178            "atom_density": self.atom_density,
179            "reference": self.source,
180        })));
181        Ok(mat)
182    }
183}
184
185/// The full parsed compendium with fast lookups.
186#[derive(Debug, Clone)]
187pub struct MaterialsLibrary {
188    /// Dataset version tag (top-level `siteVersion`, e.g. `"0.1.1"`).
189    pub site_version: String,
190    /// All material entries in file order.
191    pub entries: Vec<CompendiumEntry>,
192    by_name: BTreeMap<String, usize>,
193    by_matnum: BTreeMap<u32, usize>,
194}
195
196impl MaterialsLibrary {
197    /// Parse compendium JSON text.
198    pub fn from_json(text: &str) -> Result<Self, Error> {
199        #[derive(Deserialize)]
200        struct Top {
201            #[serde(rename = "siteVersion")]
202            site_version: String,
203            data: Vec<CompendiumEntry>,
204        }
205        let top: Top = serde_json::from_str(text).map_err(|e| Error::Json(e.to_string()))?;
206        let mut lib = Self {
207            site_version: top.site_version,
208            entries: top.data,
209            by_name: BTreeMap::new(),
210            by_matnum: BTreeMap::new(),
211        };
212        for (i, e) in lib.entries.iter().enumerate() {
213            lib.by_name.insert(e.name.to_ascii_lowercase(), i);
214            lib.by_matnum.insert(e.mat_num, i);
215        }
216        Ok(lib)
217    }
218
219    /// Read and parse a compendium JSON file.
220    pub fn from_file(path: impl AsRef<Path>) -> Result<Self, Error> {
221        let path = path.as_ref();
222        let text = std::fs::read_to_string(path)
223            .map_err(|e| Error::Io(format!("{}: {}", path.display(), e)))?;
224        Self::from_json(&text)
225    }
226
227    /// Number of materials.
228    pub fn len(&self) -> usize {
229        self.entries.len()
230    }
231
232    /// True when the library holds no entries.
233    pub fn is_empty(&self) -> bool {
234        self.entries.is_empty()
235    }
236
237    /// Case-insensitive lookup by display name ("Air (dry, near sea level)").
238    pub fn get(&self, name: &str) -> Option<&CompendiumEntry> {
239        self.by_name
240            .get(&name.to_ascii_lowercase())
241            .map(|&i| &lib_entries(self)[i])
242    }
243
244    /// Lookup by compendium material number (1..=411).
245    pub fn get_by_matnum(&self, num: u32) -> Option<&CompendiumEntry> {
246        self.by_matnum.get(&num).map(|&i| &lib_entries(self)[i])
247    }
248
249    /// All display names in file order.
250    pub fn names(&self) -> Vec<&str> {
251        self.entries.iter().map(|e| e.name.as_str()).collect()
252    }
253}
254
255// Borrow helper keeping the index maps and storage in one struct without
256// split borrows surfacing in the public API.
257fn lib_entries(lib: &MaterialsLibrary) -> &Vec<CompendiumEntry> {
258    &lib.entries
259}
260
261#[cfg(test)]
262mod tests {
263    use super::*;
264
265    fn library() -> MaterialsLibrary {
266        let path = concat!(
267            env!("CARGO_MANIFEST_DIR"),
268            "/../../fixtures/data/MaterialsCompendium.json"
269        );
270        MaterialsLibrary::from_file(path).unwrap()
271    }
272
273    #[test]
274    fn loads_all_411_materials() {
275        let lib = library();
276        assert_eq!(lib.len(), 411);
277        assert!(!lib.is_empty());
278        assert!(!lib.site_version.is_empty());
279    }
280
281    #[test]
282    fn names_unique_and_ordered() {
283        let lib = library();
284        let names = lib.names();
285        let unique: std::collections::BTreeSet<&str> = names.iter().copied().collect();
286        assert_eq!(unique.len(), 411);
287        // Dataset order: B-110 Bone Equivalent Plastic is entry zero.
288        assert_eq!(names[0], "Bone Equivalent Plastic, B-110");
289    }
290
291    #[test]
292    fn lookup_by_name_case_insensitive() {
293        let lib = library();
294        let air = lib.get("air (DRY, near sea level)").expect("air present");
295        assert_eq!(air.mat_num, 4); // early compendium entry
296        assert!(air.density > 0.001);
297    }
298
299    #[test]
300    fn lookup_by_matnum() {
301        let lib = library();
302        assert!(lib.get_by_matnum(1).is_some());
303        assert!(lib.get_by_matnum(412).is_none());
304    }
305
306    #[test]
307    fn weight_fractions_sum_near_one() {
308        let lib = library();
309        for entry in &lib.entries {
310            let total: f64 = entry.weight_fractions().values().sum();
311            assert!(
312                (total - 1.0).abs() < 1e-2,
313                "material `{}` fractions sum to {total}",
314                entry.name
315            );
316        }
317    }
318
319    #[test]
320    fn bone_plastic_h1_spot_value() {
321        // From the dataset: B-110 Bone Equivalent Plastic, H1 wf = 0.035491
322        let lib = library();
323        let bone = lib.get("Bone Equivalent Plastic, B-110").unwrap();
324        let wf = bone.weight_fractions();
325        assert!((wf[&1_001] - 0.035491).abs() < 1e-6);
326    }
327
328    #[test]
329    fn to_material_builds_named_composition_with_metadata() {
330        let lib = library();
331        let entry = lib.get("Acetone").unwrap();
332        let mat = entry.to_material().unwrap();
333        assert!(!mat.comp.is_empty());
334        let meta = mat.metadata().expect("metadata attached");
335        assert_eq!(meta["mat_num"], entry.mat_num);
336        // Fractions are relative masses; atom_fractions must resolve via AME2020.
337        let af = mat.atom_fractions(&crate::Ame2020).unwrap();
338        assert_eq!(af.len(), mat.comp.len());
339    }
340
341    #[test]
342    fn missing_file_errors() {
343        let err = MaterialsLibrary::from_file("/nonexistent/compendium.json");
344        assert!(matches!(err, Err(Error::Io(_))));
345    }
346}