Skip to content

Commit c9b335b

Browse files
authored
Merge pull request #830 from Kaos599/develop
Update default importance values to 1.0 in Importance class and tests
2 parents 8a0e7af + a76f628 commit c9b335b

File tree

8 files changed

+66
-22
lines changed

8 files changed

+66
-22
lines changed

doc/source/changelog.rst

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@ MontePy Changelog
1818
* Allow multiple universe fills to accept 2D MNCP lattices (:issue:`719`).
1919
* Make `LatticeType.RECTANGULAR` and `LatticeType.HEXAHEDRAL` synonymous (:issue:`808`).
2020
* Allow setting ``cell.fill.universes`` with a numpy array of universe IDs (:issue:`736`).
21+
* Added Boundary condition type to the representation of a ``montepy.Surface`` (e.g., ``repr(surf)``) (:issue:`682`).
22+
* Changed default importance value from 0.0 to 1.0 to match MCNP defaults. (:issue:`735`)
2123

2224
**Bugs Fixed**
2325

2426
* Fixed bug where lines that were the allowed length was raising a ``LineOverRunWarning`` when read by MontePy (:issue:`517`).
2527
* Added descriptive TypeError messages (:issue:`801`)
2628
* Fixed a bug that caused to write an extra termination line between the data block and the cell data section in the MCNP input. (:pull:`819`) (:issue:`703`).
2729

28-
**Documentation**
29-
30-
* Improved documentation for NumberedObjectCollections on Slicing behavior. (:issue:`798`)
31-
3230
**Deprecations**
3331

3432
* Dropped support for python 3.9 through 3.11, and numpy 1.26 in order to comply with `SPEC 0 <https://scientific-python.org/specs/spec-0000/>`_ (:issue:`780`).
@@ -42,9 +40,7 @@ MontePy Changelog
4240

4341
1.1.3
4442
--------------
45-
**Features Added**
4643

47-
* Added Boundary condition type to the representation of a ``montepy.Surface`` (e.g., ``repr(surf)``) (:issue:`682`).
4844

4945
**Documentation**
5046

doc/source/starting.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -609,10 +609,10 @@ For example:
609609

610610
>>> for particle in sorted(problem.mode):
611611
... print(particle, cell.importance[particle])
612-
neutron 0.0
613-
photon 0.0
612+
neutron 1.0
613+
photon 1.0
614614
>>> print(cell.importance[montepy.Particle.NEUTRON])
615-
0.0
615+
1.0
616616

617617
There's also a lot of convenient ways to do bulk modifications.
618618
There is the :func:`~montepy.data_inputs.importance.Importance.all` property that lets you set the importance for all particles in the problem at once.
@@ -629,7 +629,10 @@ For example:
629629
neutron 2.0
630630
photon 2.0
631631

632-
This will set the importances for the neutron and photon.
632+
This will set the importances for the neutron and photon.
633+
634+
You can also delete an importance to reset it to the default value (1.0).
635+
For example: ``del cell.importance.neutron``.
633636

634637
There is also the method: :func:`~montepy.cells.Cells.set_equal_importance`.
635638
This method sets all of the cells for all particles in the problem to the same importance.

montepy/cell.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,17 @@ def importance(self):
227227
Each particle's importance is a property of Importance.
228228
e.g., ``cell.importance.photon = 1.0``.
229229
230+
Deleting an importance resets it to the default value (1.0).
231+
e.g., ``del cell.importance.neutron``.
232+
230233
Returns
231234
-------
232235
Importance
233236
the importance for the Cell.
237+
238+
.. versionchanged:: 1.2.0
239+
240+
Default importance value changed from 0.0 to 1.0 to match MCNP defaults.
234241
"""
235242
return self._importance
236243

montepy/data_inputs/importance.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class Importance(CellModifierInput):
4242
the value syntax tree from the key-value pair in a cell
4343
"""
4444

45+
_DEFAULT_IMP = 1.0
46+
4547
def __init__(
4648
self,
4749
input: InitInput = None,
@@ -52,6 +54,7 @@ def __init__(
5254
self._particle_importances = {}
5355
self._real_tree = {}
5456
self._part_combos = []
57+
self._explicitly_set = False
5558
super().__init__(input, in_cell_block, key, value)
5659
if self.in_cell_block:
5760
if key:
@@ -62,6 +65,7 @@ def __init__(
6265
raise ValueError(
6366
f"Cell importance must be a number ≥ 0. {val.value} was given"
6467
)
68+
self._explicitly_set = True
6569
self._part_combos.append(self.particle_classifiers)
6670
for particle in self.particle_classifiers:
6771
self._particle_importances[particle] = value
@@ -76,6 +80,7 @@ def __init__(
7680
raise MalformedInputError(
7781
input, f"Importances must be ≥ 0 value: {node} given"
7882
)
83+
self._explicitly_set = True
7984
self._part_combos.append(self.particle_classifiers)
8085
for particle in self.particle_classifiers:
8186
self._particle_importances[particle] = copy.deepcopy(self._tree)
@@ -95,7 +100,7 @@ def _generate_default_cell_tree(self, particle=None):
95100
particles.particles = self._problem.mode.particles
96101
classifier.particles = particles
97102
list_node = syntax_node.ListNode("imp data")
98-
list_node.append(self._generate_default_node(float, 0.0))
103+
list_node.append(self._generate_default_node(float, self._DEFAULT_IMP))
99104
tree = syntax_node.SyntaxNode(
100105
"Importance",
101106
{
@@ -125,8 +130,17 @@ def _has_classifier():
125130

126131
@property
127132
def has_information(self):
128-
if self.in_cell_block:
133+
has_info = []
134+
for part in self:
135+
has_info.append(
136+
not math.isclose(
137+
self[part], self._DEFAULT_IMP, rel_tol=rel_tol, abs_tol=abs_tol
138+
)
139+
)
140+
if any(has_info):
129141
return True
142+
if self.in_cell_block:
143+
return self.set_in_cell_block
130144

131145
def merge(self, other):
132146
if not isinstance(other, type(self)):
@@ -163,7 +177,7 @@ def __getitem__(self, particle):
163177
val = self._particle_importances[particle]["data"][0]
164178
return val.value
165179
except KeyError:
166-
return 0.0
180+
return self._DEFAULT_IMP
167181

168182
def __setitem__(self, particle, value):
169183
if not isinstance(particle, Particle):
@@ -175,6 +189,7 @@ def __setitem__(self, particle, value):
175189
raise ValueError("importance must be ≥ 0")
176190
if particle not in self._particle_importances:
177191
self._generate_default_cell_tree(particle)
192+
self._explicitly_set = True
178193
self._particle_importances[particle]["data"][0].value = value
179194

180195
def __delitem__(self, particle):
@@ -213,6 +228,7 @@ def push_to_cells(self):
213228
value = self._particle_importances[particle]["data"][i]
214229
# force generating the default tree
215230
cell.importance[particle] = value.value
231+
cell.importance._explicitly_set = True
216232
# replace default ValueNode with actual valueNode
217233
tree = cell.importance._particle_importances[particle]
218234
tree.nodes["classifier"] = copy.deepcopy(
@@ -284,6 +300,7 @@ def all(self, value):
284300
if value < 0.0:
285301
raise ValueError("Importance must be ≥ 0.0")
286302
if self._problem:
303+
self._explicitly_set = True
287304
for particle in self._problem.mode:
288305
self._particle_importances[particle]["data"][0].value = value
289306

@@ -506,7 +523,7 @@ def __create_particle_imp_doc(particle_type):
506523
Returns
507524
-------
508525
float
509-
the importance for the particle type. If not set, defaults to 0.
526+
the importance for the particle type. If not set, defaults to 1.0.
510527
"""
511528

512529

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ authors = [
1414
{name = "Brenna Carbno", email="[email protected]"},
1515
{name = "Benjaminas Marcinkevicius", email="[email protected]"},
1616
{name = "Paul Ferney", email="[email protected]"},
17-
{name = "Digvijay Yeware", email="[email protected]"}
17+
{name = "Digvijay Yeware", email="[email protected]"},
18+
{name = "Harsh Dayal", email="[email protected]"},
1819
]
1920
keywords = ["MCNP", "neutronics", "imcnp", "input file", "monte carlo", "radiation transport"]
2021
license = "MIT"

tests/test_importance.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def create_cell_from_input(self, in_str, block=block_type.BlockType.CELL):
2828
"neutron": 1.0,
2929
"photon": 1.0,
3030
"all": None,
31-
"alpha_particle": 0.0,
31+
"alpha_particle": 1.0,
3232
"in_cell_block": True,
3333
},
3434
None,
@@ -40,7 +40,7 @@ def create_cell_from_input(self, in_str, block=block_type.BlockType.CELL):
4040
),
4141
(
4242
"1 0 -1",
43-
{"neutron": 0.0},
43+
{"neutron": 1.0},
4444
None,
4545
), # default neutron importance when nothing is set
4646
# Error cases
@@ -267,9 +267,9 @@ def test_importance_deleter(self, cell_with_importance):
267267
"""
268268
cell = cell_with_importance
269269
del cell.importance.neutron
270-
assert cell.importance.neutron == pytest.approx(0.0)
270+
assert cell.importance.neutron == pytest.approx(1.0)
271271
del cell.importance[Particle.PHOTON]
272-
assert cell.importance.photon == pytest.approx(0.0)
272+
assert cell.importance.photon == pytest.approx(1.0)
273273
with pytest.raises(TypeError):
274274
del cell.importance[""]
275275

@@ -312,3 +312,8 @@ def test_default_importance_not_implemented(self):
312312
prob.print_in_data_block["imp"] = True
313313
with pytest.raises(NotImplementedError):
314314
prob.write_problem(io.StringIO())
315+
316+
def test_default_cell_importance(self):
317+
"""Test that new cells have default importance of 1.0 (Issue #735)"""
318+
cell = montepy.Cell()
319+
assert cell.importance.neutron == 1.0

tests/test_integration.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,9 +1120,15 @@ def test_alternate_encoding():
11201120
)
11211121

11221122

1123+
"""
1124+
file_path: {line_number (0-indexed): flag}
1125+
1126+
Flags:
1127+
0: skip line in both files
1128+
1: skip line in output file
1129+
2+: skip line in gold file
1130+
"""
11231131
_SKIP_LINES = {
1124-
# skip lines of added implied importances
1125-
"tests/inputs/test_universe_data.imcnp": {5: 1, 14: 1, 15: 1},
11261132
# I don't care about the edge case of shortcuts in a material def.
11271133
"tests/inputs/test_complement_edge.imcnp": {37: 0, 38: 0, 39: 0},
11281134
}
@@ -1141,6 +1147,11 @@ def test_alternate_encoding():
11411147
},
11421148
)
11431149
def test_read_write_cycle(file):
1150+
"""
1151+
Warning: this test is a Rube Goldberg machine of edge cases.
1152+
1153+
Please ask @micahgale before modifying it.
1154+
"""
11441155
print(f"Testing against {file} *********************")
11451156
if ".swp" in file.suffixes:
11461157
return

tests/test_universe_integration.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ def test_fill_multi_universe_order(cells):
119119
cell.fill.universes = unis
120120
output = cell.format_for_mcnp_input((6, 2, 0))
121121
words = " ".join(output).split()
122-
new_universes = list(map(int, words[7:]))
122+
start_idx = 6
123+
if "imp:n=1" in words:
124+
start_idx += 1
125+
print(output)
126+
new_universes = list(map(int, words[start_idx:]))
123127
assert (numbers.flatten("f") == new_universes).all()
124128

125129

0 commit comments

Comments
 (0)