From 0a066b12a90f89967bdf344f3c38526da89da384 Mon Sep 17 00:00:00 2001 From: "Micah D. Gale" Date: Thu, 5 Mar 2026 11:30:43 -0600 Subject: [PATCH 1/8] Lazily replicated bug #892 as a test case. --- tests/test_importance.py | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/test_importance.py b/tests/test_importance.py index 6210691e..00fa2210 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 From 5dad2f5fcfb98b2a6108faa2d01b9a2f71641e4f Mon Sep 17 00:00:00 2001 From: Micah Gale Date: Sat, 7 Mar 2026 22:24:36 -0600 Subject: [PATCH 2/8] Fixed bug that explicitly set was ignored. --- montepy/data_inputs/importance.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/montepy/data_inputs/importance.py b/montepy/data_inputs/importance.py index 61e91826..43f18e56 100644 --- a/montepy/data_inputs/importance.py +++ b/montepy/data_inputs/importance.py @@ -133,7 +133,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 ) ) From 01cc4a1689df5f6d07367286a026399ed69394bd Mon Sep 17 00:00:00 2001 From: Micah Gale Date: Sat, 7 Mar 2026 22:25:50 -0600 Subject: [PATCH 3/8] Allowed importance.all without a problem. --- montepy/data_inputs/importance.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/montepy/data_inputs/importance.py b/montepy/data_inputs/importance.py index 43f18e56..a9222e30 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 @@ -300,10 +302,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: From b1b99e5c64f41e91f79ecfba073ee28339f39b67 Mon Sep 17 00:00:00 2001 From: Micah Gale Date: Sat, 7 Mar 2026 22:33:03 -0600 Subject: [PATCH 4/8] Ignore default implicit importance, and change to caps in tests. --- tests/test_importance.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_importance.py b/tests/test_importance.py index 00fa2210..a9d2c7a2 100644 --- a/tests/test_importance.py +++ b/tests/test_importance.py @@ -335,7 +335,7 @@ def test_print_explicit_default_imp(_): # based on #892 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" + # Cell 4 should give "imp:n=4.0" c4 = montepy.Cell(number=7) c4.geometry = -problem.surfaces[1010] c4.importance.neutron = 4.0 @@ -357,9 +357,9 @@ def test_print_explicit_default_imp(_): # based on #892 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", + 4: "IMP:n=2.0 IMP:p=2.0", + 6: "IMP:n=1.0 IMP:p=1.0", + 7: "IMP:n=4.0", + 8: "IMP:n=5.0 IMP:p=1.0", }.items(): assert imp_str in cells[cell_num]._input.input_text From 7f1254f09da7a9d6a051b05a0104b3fa35a428dd Mon Sep 17 00:00:00 2001 From: Micah Gale Date: Sat, 7 Mar 2026 22:33:25 -0600 Subject: [PATCH 5/8] Filter printing by particles in the problem. --- montepy/data_inputs/importance.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/montepy/data_inputs/importance.py b/montepy/data_inputs/importance.py index a9222e30..52b58eaf 100644 --- a/montepy/data_inputs/importance.py +++ b/montepy/data_inputs/importance.py @@ -247,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" From b7381f52ea62296c636d5777e69ed132de07043e Mon Sep 17 00:00:00 2001 From: Micah Gale Date: Sat, 7 Mar 2026 22:41:57 -0600 Subject: [PATCH 6/8] Updated test for default importance printing change. --- tests/test_universe_integration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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:])) From 7a947014fe85b6ba21ad77e8b03be1ce395ce7d6 Mon Sep 17 00:00:00 2001 From: Micah Gale Date: Sat, 7 Mar 2026 22:44:55 -0600 Subject: [PATCH 7/8] Added #892 to the changelog. --- doc/source/changelog.rst | 1 + 1 file changed, 1 insertion(+) 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** From 90165d819378e4a97a3d38add3351d891feb73ba Mon Sep 17 00:00:00 2001 From: Micah Gale Date: Tue, 10 Mar 2026 10:18:14 -0500 Subject: [PATCH 8/8] Added photon importance back in with feedback. --- tests/test_importance.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_importance.py b/tests/test_importance.py index a9d2c7a2..f5943b31 100644 --- a/tests/test_importance.py +++ b/tests/test_importance.py @@ -335,7 +335,7 @@ def test_print_explicit_default_imp(_): # based on #892 c3.importance.neutron = 1.0 c3.importance.photon = 1.0 problem.cells.append(c3) - # Cell 4 should give "imp:n=4.0" + # 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 @@ -359,7 +359,7 @@ def test_print_explicit_default_imp(_): # based on #892 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", + 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