Support for NCrystal material in from_xml_element (#2496)

Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
This commit is contained in:
Nicola Rizzi 2023-07-10 06:51:43 +02:00 committed by GitHub
parent e3cd406a9b
commit 3e1a5a552a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View file

@ -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')

View file

@ -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'