Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ MontePy Changelog
#Next Release#
--------------

**Features**

* Changed default importance value from 0.0 to 1.0 to match MCNP defaults. (:issue:`735`)

**Documentation**

* Improved documentation for NumberedObjectCollections on Slicing behavior. (:issue:`798`)

**Bugs Fixed**

* Fixed bug where lines that were the allowed length was raising a ``LineOverRunWarning`` when read by MontePy (:issue:`517`).
* Fixed bug where lines that were the allowed length was raising a ``LineOverRunWarning`` when read by MontePy (:issue:`517`).

* Added descriptive TypeError messages (:issue:`801`)

Expand Down
11 changes: 7 additions & 4 deletions doc/source/starting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,10 @@ For example:

>>> for particle in sorted(problem.mode):
... print(particle, cell.importance[particle])
neutron 0.0
photon 0.0
neutron 1.0
photon 1.0
>>> print(cell.importance[montepy.Particle.NEUTRON])
0.0
1.0

There's also a lot of convenient ways to do bulk modifications.
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.
Expand All @@ -629,7 +629,10 @@ For example:
neutron 2.0
photon 2.0

This will set the importances for the neutron and photon.
This will set the importances for the neutron and photon.

You can also delete an importance to reset it to the default value (1.0).
For example: ``del cell.importance.neutron``.

There is also the method: :func:`~montepy.cells.Cells.set_equal_importance`.
This method sets all of the cells for all particles in the problem to the same importance.
Expand Down
7 changes: 7 additions & 0 deletions montepy/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,17 @@ def importance(self):
Each particle's importance is a property of Importance.
e.g., ``cell.importance.photon = 1.0``.

Deleting an importance resets it to the default value (1.0).
e.g., ``del cell.importance.neutron``.

Returns
-------
Importance
the importance for the Cell.

.. versionchanged:: 1.2.0

Default importance value changed from 0.0 to 1.0 to match MCNP defaults.
"""
return self._importance

Expand Down
14 changes: 10 additions & 4 deletions montepy/data_inputs/importance.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(
self._particle_importances = {}
self._real_tree = {}
self._part_combos = []
self._explicitly_set = False
super().__init__(input, in_cell_block, key, value)
if self.in_cell_block:
if key:
Expand All @@ -62,6 +63,7 @@ def __init__(
raise ValueError(
f"Cell importance must be a number ≥ 0. {val.value} was given"
)
self._explicitly_set = True
self._part_combos.append(self.particle_classifiers)
for particle in self.particle_classifiers:
self._particle_importances[particle] = value
Expand All @@ -76,6 +78,7 @@ def __init__(
raise MalformedInputError(
input, f"Importances must be ≥ 0 value: {node} given"
)
self._explicitly_set = True
self._part_combos.append(self.particle_classifiers)
for particle in self.particle_classifiers:
self._particle_importances[particle] = copy.deepcopy(self._tree)
Expand All @@ -95,7 +98,7 @@ def _generate_default_cell_tree(self, particle=None):
particles.particles = self._problem.mode.particles
classifier.particles = particles
list_node = syntax_node.ListNode("imp data")
list_node.append(self._generate_default_node(float, 0.0))
list_node.append(self._generate_default_node(float, 1.0))
tree = syntax_node.SyntaxNode(
"Importance",
{
Expand Down Expand Up @@ -126,7 +129,7 @@ def _has_classifier():
@property
def has_information(self):
if self.in_cell_block:
return True
return self._explicitly_set

def merge(self, other):
if not isinstance(other, type(self)):
Expand Down Expand Up @@ -163,7 +166,7 @@ def __getitem__(self, particle):
val = self._particle_importances[particle]["data"][0]
return val.value
except KeyError:
return 0.0
return 1.0

def __setitem__(self, particle, value):
if not isinstance(particle, Particle):
Expand All @@ -175,6 +178,7 @@ def __setitem__(self, particle, value):
raise ValueError("importance must be ≥ 0")
if particle not in self._particle_importances:
self._generate_default_cell_tree(particle)
self._explicitly_set = True
self._particle_importances[particle]["data"][0].value = value

def __delitem__(self, particle):
Expand Down Expand Up @@ -213,6 +217,7 @@ def push_to_cells(self):
value = self._particle_importances[particle]["data"][i]
# force generating the default tree
cell.importance[particle] = value.value
cell.importance._explicitly_set = True
# replace default ValueNode with actual valueNode
tree = cell.importance._particle_importances[particle]
tree.nodes["classifier"] = copy.deepcopy(
Expand Down Expand Up @@ -284,6 +289,7 @@ def all(self, value):
if value < 0.0:
raise ValueError("Importance must be ≥ 0.0")
if self._problem:
self._explicitly_set = True
for particle in self._problem.mode:
self._particle_importances[particle]["data"][0].value = value

Expand Down Expand Up @@ -506,7 +512,7 @@ def __create_particle_imp_doc(particle_type):
Returns
-------
float
the importance for the particle type. If not set, defaults to 0.
the importance for the particle type. If not set, defaults to 1.0.
"""


Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ authors = [
{name = "Brenna Carbno", email="[email protected]"},
{name = "Benjaminas Marcinkevicius", email="[email protected]"},
{name = "Paul Ferney", email="[email protected]"},
{name = "Digvijay Yeware", email="[email protected]"}
{name = "Digvijay Yeware", email="[email protected]"},
{name = "Harsh Dayal", email="[email protected]"},
]
keywords = ["MCNP", "neutronics", "imcnp", "input file", "monte carlo", "radiation transport"]
license = "MIT"
Expand Down
13 changes: 9 additions & 4 deletions tests/test_importance.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def create_cell_from_input(self, in_str, block=block_type.BlockType.CELL):
"neutron": 1.0,
"photon": 1.0,
"all": None,
"alpha_particle": 0.0,
"alpha_particle": 1.0,
"in_cell_block": True,
},
None,
Expand All @@ -40,7 +40,7 @@ def create_cell_from_input(self, in_str, block=block_type.BlockType.CELL):
),
(
"1 0 -1",
{"neutron": 0.0},
{"neutron": 1.0},
None,
), # default neutron importance when nothing is set
# Error cases
Expand Down Expand Up @@ -267,9 +267,9 @@ def test_importance_deleter(self, cell_with_importance):
"""
cell = cell_with_importance
del cell.importance.neutron
assert cell.importance.neutron == pytest.approx(0.0)
assert cell.importance.neutron == pytest.approx(1.0)
del cell.importance[Particle.PHOTON]
assert cell.importance.photon == pytest.approx(0.0)
assert cell.importance.photon == pytest.approx(1.0)
with pytest.raises(TypeError):
del cell.importance[""]

Expand Down Expand Up @@ -312,3 +312,8 @@ def test_default_importance_not_implemented(self):
prob.print_in_data_block["imp"] = True
with pytest.raises(NotImplementedError):
prob.write_problem(io.StringIO())

def test_default_cell_importance(self):
"""Test that new cells have default importance of 1.0 (Issue #735)"""
cell = montepy.Cell()
assert cell.importance.neutron == 1.0
Loading