Skip to content

Commit 3bbb34f

Browse files
authored
Merge pull request #710 from idaholab/709-surfaces-cant-be-unset-from-is_reflecting
Fixed surfaces cant be unset from is reflecting
2 parents 3f83b66 + 1a0f799 commit 3bbb34f

File tree

5 files changed

+92
-45
lines changed

5 files changed

+92
-45
lines changed

doc/source/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ MontePy Changelog
3737
* Fixed bug where setting a lattice would print as ``LAT=None``. Also switched ``CellModifier`` to print in the cell block by default (:issue:`699`).
3838
* Fixed bug that wouldn't allow cloning most surfaces (:issue:`704`).
3939
* Fixed bug that crashed when some cells were not assigned to any universes (:issue:`705`).
40+
* Fixed bug where setting ``surf.is_reflecting`` to ``False`` did not always get exported properly (:issue:`709`).
4041

4142
**Breaking Changes**
4243

montepy/input_parser/syntax_node.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,9 +1328,14 @@ def format(self):
13281328
else:
13291329
pad_str = ""
13301330
extra_pad_str = ""
1331-
buffer = "{temp:<{value_length}}{padding}".format(
1332-
temp=temp, padding=pad_str, **self._formatter
1333-
)
1331+
if not self.never_pad:
1332+
buffer = "{temp:<{value_length}}{padding}".format(
1333+
temp=temp, padding=pad_str, **self._formatter
1334+
)
1335+
else:
1336+
buffer = "{temp}{padding}".format(
1337+
temp=temp, padding=pad_str, **self._formatter
1338+
)
13341339
"""
13351340
If:
13361341
1. expanded

montepy/mcnp_object.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,16 @@ def __setattr__(self, key, value):
161161
)
162162

163163
@staticmethod
164-
def _generate_default_node(value_type: type, default, padding: str = " "):
164+
def _generate_default_node(
165+
value_type: type, default: str, padding: str = " ", never_pad: bool = False
166+
):
165167
"""Generates a "default" or blank ValueNode.
166168
167169
None is generally a safe default value to provide.
168170
171+
.. versionchanged:: 1.0.0
172+
Added ``never_pad`` argument.
173+
169174
Parameters
170175
----------
171176
value_type : Class
@@ -176,6 +181,8 @@ def _generate_default_node(value_type: type, default, padding: str = " "):
176181
padding : str, None
177182
the string to provide to the PaddingNode. If None no
178183
PaddingNode will be added.
184+
never_pad: bool
185+
Whether to never add trailing padding. True means extra padding is suppressed.
179186
180187
Returns
181188
-------
@@ -187,8 +194,8 @@ def _generate_default_node(value_type: type, default, padding: str = " "):
187194
else:
188195
padding_node = None
189196
if default is None or isinstance(default, montepy.input_parser.mcnp_input.Jump):
190-
return ValueNode(default, value_type, padding_node)
191-
return ValueNode(str(default), value_type, padding_node)
197+
return ValueNode(default, value_type, padding_node, never_pad)
198+
return ValueNode(str(default), value_type, padding_node, never_pad)
192199

193200
@property
194201
def parameters(self) -> dict[str, str]:

montepy/surfaces/surface.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __init__(
6464
elif "+" in self._number.token:
6565
self._is_white_boundary = True
6666
self._number._token = self._number.token.replace("+", "")
67-
self._modifier = self._generate_default_node(str, "+", None)
67+
self._modifier = self._generate_default_node(str, "+", None, True)
6868
self._tree["surface_num"].nodes["modifier"] = self._modifier
6969
try:
7070
assert self._number.value > 0
@@ -289,6 +289,8 @@ def _update_values(self):
289289
modifier.value = "*"
290290
elif self.is_white_boundary:
291291
modifier.value = "+"
292+
else:
293+
modifier.value = ""
292294
if self.transform is not None:
293295
self._old_transform_number.value = self.transform.number
294296
self._old_transform_number.is_negative = False

tests/test_surfaces.py

Lines changed: 70 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Copyright 2024-2025, Battelle Energy Alliance, LLC All Rights Reserved.
2+
import io
23
from unittest import TestCase
4+
from pathlib import Path
35
import pytest
46

57
import montepy
@@ -129,26 +131,6 @@ def test_validator(self):
129131
with self.assertRaises(montepy.errors.IllegalState):
130132
surf.validate()
131133

132-
def test_surface_is_reflecting_setter(self):
133-
in_str = "1 PZ 0.0"
134-
card = Input([in_str], BlockType.SURFACE)
135-
surf = Surface(card)
136-
surf.is_reflecting = True
137-
self.assertTrue(surf.is_reflecting)
138-
self.verify_export(surf)
139-
with self.assertRaises(TypeError):
140-
surf.is_reflecting = 1
141-
142-
def test_surface_is_white_bound_setter(self):
143-
in_str = "1 PZ 0.0"
144-
card = Input([in_str], BlockType.SURFACE)
145-
surf = Surface(card)
146-
surf.is_white_boundary = True
147-
self.assertTrue(surf.is_white_boundary)
148-
self.verify_export(surf)
149-
with self.assertRaises(TypeError):
150-
surf.is_white_boundary = 1
151-
152134
def test_surface_constants_setter(self):
153135
in_str = "1 PZ 0.0"
154136
card = Input([in_str], BlockType.SURFACE)
@@ -315,24 +297,36 @@ def test_cylinder_location_setter(self):
315297
with self.assertRaises(ValueError):
316298
surf.coordinates = [3, 4, 5]
317299

318-
def verify_export(_, surf):
319-
output = surf.format_for_mcnp_input((6, 3, 0))
320-
print("Surface output", output)
321-
new_surf = Surface("\n".join(output))
322-
assert surf.number == new_surf.number, "Material number not preserved."
323-
assert len(surf.surface_constants) == len(
324-
new_surf.surface_constants
325-
), "number of surface constants not kept."
326-
for old_const, new_const in zip(
327-
surf.surface_constants, new_surf.surface_constants
328-
):
329-
assert old_const == pytest.approx(new_const)
330-
assert surf.is_reflecting == new_surf.is_reflecting
331-
assert surf.is_white_boundary == new_surf.is_white_boundary
332-
if surf.old_periodic_surface:
333-
assert surf.old_periodic_surface == new_surf.old_periodic_surface
334-
if surf.old_transform_number:
335-
assert surf.old_transform_number == new_surf._old_transform_number
300+
301+
def verify_export(surf):
302+
output = surf.format_for_mcnp_input((6, 3, 0))
303+
print("Surface output", output)
304+
new_surf = Surface("\n".join(output))
305+
verify_equiv_surf(surf, new_surf)
306+
307+
308+
def verify_equiv_surf(surf, new_surf):
309+
assert surf.number == new_surf.number, "Material number not preserved."
310+
assert len(surf.surface_constants) == len(
311+
new_surf.surface_constants
312+
), "number of surface constants not kept."
313+
for old_const, new_const in zip(surf.surface_constants, new_surf.surface_constants):
314+
assert old_const == pytest.approx(new_const)
315+
assert surf.is_reflecting == new_surf.is_reflecting
316+
assert surf.is_white_boundary == new_surf.is_white_boundary
317+
if surf.old_periodic_surface:
318+
assert surf.old_periodic_surface == new_surf.old_periodic_surface
319+
if surf.old_transform_number:
320+
assert surf.old_transform_number == new_surf._old_transform_number
321+
322+
323+
def verify_prob_export(problem, surf):
324+
with io.StringIO() as fh:
325+
problem.write_problem(fh)
326+
fh.seek(0)
327+
new_problem = montepy.read_input(fh)
328+
new_surf = new_problem.surfaces[surf.number]
329+
verify_equiv_surf(surf, new_surf)
336330

337331

338332
@pytest.mark.parametrize(
@@ -345,3 +339,41 @@ def test_surface_clone(surf_str):
345339
new_surf = surf.clone()
346340
assert surf.surface_type == new_surf.surface_type
347341
assert surf.surface_constants == new_surf.surface_constants
342+
343+
344+
@pytest.fixture
345+
def simple_problem(scope="module"):
346+
return montepy.read_input(Path("tests") / "inputs" / "test.imcnp")
347+
348+
349+
@pytest.fixture
350+
def cp_simple_problem(simple_problem):
351+
return simple_problem.clone()
352+
353+
354+
@pytest.mark.parametrize(
355+
"in_str, expected",
356+
[("1 PZ 0.0", True), ("*1 PZ 0.0", False), (" *1 PZ 0.0", False)],
357+
)
358+
def test_surface_is_reflecting_setter(cp_simple_problem, in_str, expected):
359+
surf = Surface(in_str)
360+
surf.is_reflecting = expected
361+
assert surf.is_reflecting == expected
362+
cp_simple_problem.surfaces.append(surf)
363+
verify_prob_export(cp_simple_problem, surf)
364+
with pytest.raises(TypeError):
365+
surf.is_reflecting = 1
366+
367+
368+
@pytest.mark.parametrize(
369+
"in_str, expected",
370+
[("1 PZ 0.0", True), ("+1 PZ 0.0", False), (" +1 PZ 0.0", False)],
371+
)
372+
def test_surface_is_white_bound_setter(cp_simple_problem, in_str, expected):
373+
surf = Surface(in_str)
374+
surf.is_white_boundary = expected
375+
assert surf.is_white_boundary == expected
376+
cp_simple_problem.surfaces.append(surf)
377+
verify_prob_export(cp_simple_problem, surf)
378+
with pytest.raises(TypeError):
379+
surf.is_white_boundary = 1

0 commit comments

Comments
 (0)