Skip to content

Commit 243a994

Browse files
authored
Fix comment card removal (#62)
1 parent aa881ed commit 243a994

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/openmc_mcnp_adapter/parse.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,9 @@ def sanitize(section: str) -> str:
377377
# Remove end-of-line comments
378378
section = re.sub(r'\$.*$', '', section, flags=re.MULTILINE)
379379

380-
# Remove comment cards
381-
section = re.sub('^[ \t]*?[cC].*?$\n?', '', section, flags=re.MULTILINE)
380+
# Remove comment cards: 'c' in first 5 columns followed by at least one
381+
# blank, or 'c' as the only character on the line
382+
section = re.sub(r'^[ \t]{0,4}[cC](?:[ \t]+.*)?$\n?', '', section, flags=re.MULTILINE)
382383

383384
# Turn continuation lines into single line
384385
section = re.sub('&.*\n', ' ', section)

tests/test_material.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,27 @@ def test_material_without_keywords():
156156
nd = m.get_nuclide_densities()
157157
assert 'U235' in nd and nd['U235'].percent == approx(1.0)
158158
assert 'U238' in nd and nd['U238'].percent == approx(0.5)
159+
160+
def test_material_with_code_keyword():
161+
"""Test that keywords starting with 'c' are not treated as comment cards"""
162+
# This tests the case where continuation line has keyword starting with 'c'
163+
# The 'code=0' line should not be treated as a comment card
164+
mcnp_model = textwrap.dedent("""
165+
title
166+
1 1 -1.0 1
167+
2 2 -1.0 -1
168+
169+
1 px 0.0
170+
171+
m1 1001.70c 1.0 &
172+
code=0
173+
m2 1001.70c 1.0
174+
""")
175+
model = mcnp_str_to_model(mcnp_model)
176+
# Should have two distinct materials
177+
assert len(model.materials) == 2
178+
# Both materials should have H1
179+
for mat in model.materials:
180+
nd = mat.get_nuclide_densities()
181+
assert 'H1' in nd
182+
assert nd['H1'].percent == approx(1.0)

0 commit comments

Comments
 (0)