Skip to content

Commit aa881ed

Browse files
authored
Support keywords on material cards (#61)
1 parent 5926b50 commit aa881ed

3 files changed

Lines changed: 75 additions & 6 deletions

File tree

src/openmc_mcnp_adapter/parse.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,28 @@ def parse_data(section):
241241
for line in lines:
242242
if _MATERIAL_RE.match(line):
243243
g = _MATERIAL_RE.match(line).groups()
244-
spec = g[1].split()
244+
spec_text = g[1]
245+
246+
# Extract keywords (key=value) and remove them from spec
247+
keyword_pattern = re.compile(r'(\S+)\s*=\s*(\S+)')
248+
keywords = {}
249+
def extract_keyword(match):
250+
keywords[match.group(1).lower()] = match.group(2)
251+
return ''
252+
spec_text = keyword_pattern.sub(extract_keyword, spec_text)
253+
254+
# Parse remaining nuclide-density pairs
255+
spec = spec_text.split()
245256
try:
246257
nuclides = list(zip(spec[::2], map(float_, spec[1::2])))
247258
except Exception:
248259
raise ValueError('Invalid material specification?')
260+
249261
uid = int(g[0])
250-
data['materials'][uid].update({'id': uid, 'nuclides': nuclides})
262+
material_data = {'id': uid, 'nuclides': nuclides}
263+
if keywords:
264+
material_data['keywords'] = keywords
265+
data['materials'][uid].update(material_data)
251266
elif _SAB_RE.match(line):
252267
g = _SAB_RE.match(line).groups()
253268
uid = int(g[0])

tests/test_geometry.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,19 @@ def test_likenbut():
106106
(1.0, 0.0, 0.2),
107107
],
108108
),
109-
param(
109+
(
110110
"1 0 -1 TRCL=(0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 -1)",
111111
("1 rpp -0.5 0.5 -0.25 0.25 -0.1 0.1",),
112-
[],
113-
[],
114-
marks=mark.xfail(reason="13-parameter TRCL not yet supported"),
112+
[
113+
(-0.4, 0.1, 0.0),
114+
(0.0, 0.0, 0.0),
115+
(0.49, -0.24, -0.05),
116+
],
117+
[
118+
(-0.6, 0.0, 0.0),
119+
(0.0, 0.5, 0.0),
120+
(0.0, 0.0, 0.2),
121+
],
115122
),
116123
],
117124
)

tests/test_material.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,50 @@ def test_density_no_whitespace():
109109
model = mcnp_str_to_model(mcnp_model)
110110
m = model.materials[0]
111111
assert m.get_mass_density() == approx(4.5)
112+
113+
114+
def test_material_keywords_at_beginning():
115+
"""Test material card with keywords at the beginning"""
116+
mat_card = "m1 NLIB=70c PLIB=04p 92235.70c 1.0 92238.70c 0.5"
117+
m = convert_material(mat_card, -1.0)
118+
nd = m.get_nuclide_densities()
119+
assert 'U235' in nd and nd['U235'].percent == approx(1.0)
120+
assert 'U238' in nd and nd['U238'].percent == approx(0.5)
121+
122+
123+
def test_material_keywords_in_middle():
124+
"""Test material card with keywords interspersed with nuclide pairs"""
125+
mat_card = "m1 92235.70c 1.0 NLIB=70c 92238.70c 0.5 PLIB=04p 92234.70c 0.3"
126+
m = convert_material(mat_card, -1.0)
127+
nd = m.get_nuclide_densities()
128+
assert 'U235' in nd and nd['U235'].percent == approx(1.0)
129+
assert 'U238' in nd and nd['U238'].percent == approx(0.5)
130+
assert 'U234' in nd and nd['U234'].percent == approx(0.3)
131+
132+
133+
def test_material_keywords_with_spaces():
134+
"""Test material card with keywords that have spaces around equals sign"""
135+
mat_card = "m1 92235.70c 1.5 NLIB = 80c 92238.70c 0.5 GAS = 0"
136+
m = convert_material(mat_card, -2.0)
137+
nd = m.get_nuclide_densities()
138+
assert 'U235' in nd and nd['U235'].percent == approx(1.5)
139+
assert 'U238' in nd and nd['U238'].percent == approx(0.5)
140+
assert m.get_mass_density() == approx(2.0)
141+
142+
143+
def test_material_keywords_at_end():
144+
"""Test material card with keywords at the end"""
145+
mat_card = "m1 92235.70c 2.0 92238.70c 1.0 NLIB=70c PLIB=04p"
146+
m = convert_material(mat_card, -1.0)
147+
nd = m.get_nuclide_densities()
148+
assert 'U235' in nd and nd['U235'].percent == approx(2.0)
149+
assert 'U238' in nd and nd['U238'].percent == approx(1.0)
150+
151+
152+
def test_material_without_keywords():
153+
"""Test that material cards without keywords still work correctly"""
154+
mat_card = "m1 92235.70c 1.0 92238.70c 0.5"
155+
m = convert_material(mat_card, -1.0)
156+
nd = m.get_nuclide_densities()
157+
assert 'U235' in nd and nd['U235'].percent == approx(1.0)
158+
assert 'U238' in nd and nd['U238'].percent == approx(0.5)

0 commit comments

Comments
 (0)