Skip to content
1 change: 1 addition & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
14 changes: 11 additions & 3 deletions montepy/data_inputs/importance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
46 changes: 46 additions & 0 deletions tests/test_importance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion tests/test_universe_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]))
Expand Down
Loading