diff --git a/openmc/material.py b/openmc/material.py index 5f28535dba..4e3fdac815 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -1452,6 +1452,11 @@ class Material(IDManagerMixin): """ mat_id = int(elem.get('id')) + # Add NCrystal material from cfg string + if "cfg" in elem.attrib: + cfg = elem.get("cfg") + return Material.from_ncrystal(cfg, material_id=mat_id) + mat = cls(mat_id) mat.name = elem.get('name') diff --git a/tests/regression_tests/ncrystal/test.py b/tests/regression_tests/ncrystal/test.py index 03994e0d38..e1c1e5ed6f 100644 --- a/tests/regression_tests/ncrystal/test.py +++ b/tests/regression_tests/ncrystal/test.py @@ -1,4 +1,6 @@ from math import pi +import filecmp +from difflib import unified_diff import numpy as np import openmc @@ -78,3 +80,26 @@ def test_ncrystal(): test = pencil_beam_model(cfg, E0, n_particles) harness = NCrystalTest('statepoint.10.h5', model=test) harness.main() + + +def test_cfg_from_xml(): + """Make sure the cfg string is read by from_xml method""" + n_particles = 100000 + E0 = 0.012 # eV + cfg = 'Al_sg225.ncmat' + model = pencil_beam_model(cfg, E0, n_particles) + #export the original material generated with cfg string + model.materials.export_to_xml('materials.xml.orig') + expected = open('materials.xml.orig', 'r').readlines() + #read back the original material + mats_from_xml = openmc.Materials.from_xml('materials.xml.orig') + #export again + mats_from_xml.export_to_xml('materials.xml.after') + actual = open('materials.xml.after', 'r').readlines() + compare = filecmp.cmp('materials.xml.orig','materials.xml.after') + if not compare: + diff = unified_diff(expected, actual, 'materials.xml.orig', + 'materials.xml.after') + print('Input differences:') + print(''.join(diff)) + assert compare, 'Materials not read correctly from XML'