diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index 6d47a86c..1e73b118 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -11,6 +11,7 @@ MontePy Changelog **Bugs Fixed** * Fixed a bug where ``append_renumber`` raised a ``TypeError`` when called with an object whose ``number`` is ``None`` (e.g. an object created with no arguments) (:issue:`880`). +* Fixed a bug where the importance of cells made from scratch are usually not printed to the output file (:pull:`921`). **Feature Added** diff --git a/montepy/data_inputs/importance.py b/montepy/data_inputs/importance.py index 61e91826..52b58eaf 100644 --- a/montepy/data_inputs/importance.py +++ b/montepy/data_inputs/importance.py @@ -3,6 +3,8 @@ import copy import math import warnings + +import montepy from montepy.data_inputs.cell_modifier import CellModifierInput, InitInput from montepy.exceptions import * from montepy.constants import DEFAULT_VERSION, rel_tol, abs_tol @@ -133,7 +135,8 @@ def has_information(self): has_info = [] for part in self: has_info.append( - not math.isclose( + self._explicitly_set + or not math.isclose( self[part], self._DEFAULT_IMP, rel_tol=rel_tol, abs_tol=abs_tol ) ) @@ -244,7 +247,9 @@ def _format_tree(self): particles_printed = set() ret = "" for particle in self: - if particle in particles_printed: + if particle in particles_printed or ( + self._problem and particle not in self._problem.mode + ): continue other_particles = self._particle_importances[particle][ "classifier" @@ -299,10 +304,13 @@ def all(self, value): value = float(value) if value < 0.0: raise ValueError("Importance must be ≥ 0.0") + self._explicitly_set = True if self._problem: - self._explicitly_set = True for particle in self._problem.mode: self._particle_importances[particle]["data"][0].value = value + else: + for particle in montepy.Particle: + self[particle] = value def _clear_data(self): if not self.in_cell_block: diff --git a/tests/test_importance.py b/tests/test_importance.py index 6210691e..f5943b31 100644 --- a/tests/test_importance.py +++ b/tests/test_importance.py @@ -2,6 +2,7 @@ import montepy import os import io +from pathlib import Path import pytest from montepy.cell import Cell from montepy.particle import Particle @@ -317,3 +318,48 @@ 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 + + def test_print_explicit_default_imp(_): # based on #892 + problem = montepy.read_input(Path("tests") / "inputs" / "test.imcnp") + + # Modify an existing cell. Importance remains. + problem.cells[1].geometry &= +problem.surfaces[1000] + # Cell 2 should give "imp:n=2.0 imp:p=2.0" + c2 = montepy.Cell(number=4) + c2.geometry = -problem.surfaces[1000] & +problem.surfaces[1005] + c2.importance.all = 2.0 + problem.cells.append(c2) + # Cell 3 should give "imp:n=1.0 imp:p=1.0" + c3 = montepy.Cell(number=6) + c3.geometry = -problem.surfaces[1005] & +problem.surfaces[1010] + c3.importance.neutron = 1.0 + c3.importance.photon = 1.0 + problem.cells.append(c3) + # Cell 4 should give "imp:n=4.0 imp:p=1.0" + c4 = montepy.Cell(number=7) + c4.geometry = -problem.surfaces[1010] + c4.importance.neutron = 4.0 + problem.cells.append(c4) + # Cell 5 DOES give "imp:n=5.0 imp:p=1.0" + c5 = montepy.Cell(number=8) + c5.geometry = -problem.surfaces[1015] + c5.importance.neutron = 5.0 + c5.importance.photon = 1.0 + problem.cells.append(c5) + # Ensure that this is set: + # Ensure that this is set: + problem.print_in_data_block["imp"] = False + + stream = io.StringIO() + problem.write_problem(stream) + stream.seek(0) + new_problem = montepy.read_input(stream) + stream.close() + cells = new_problem.cells + for cell_num, imp_str in { + 4: "IMP:n=2.0 IMP:p=2.0", + 6: "IMP:n=1.0 IMP:p=1.0", + 7: "IMP:n=4.0 IMP:p=1.0", + 8: "IMP:n=5.0 IMP:p=1.0", + }.items(): + assert imp_str in cells[cell_num]._input.input_text diff --git a/tests/test_universe_integration.py b/tests/test_universe_integration.py index 8d0e0670..a662bb67 100644 --- a/tests/test_universe_integration.py +++ b/tests/test_universe_integration.py @@ -120,7 +120,7 @@ def test_fill_multi_universe_order(cells): output = cell.format_for_mcnp_input((6, 2, 0)) words = " ".join(output).split() start_idx = 6 - if "imp:n=1" in words: + if "imp" in words[3].lower(): start_idx += 1 print(output) new_universes = list(map(int, words[start_idx:]))