Skip to content

fix(importance): write importances set on newly created cells#923

Closed
novavale wants to merge 3 commits intoidaholab:developfrom
novavale:fix/default-importances-892
Closed

fix(importance): write importances set on newly created cells#923
novavale wants to merge 3 commits intoidaholab:developfrom
novavale:fix/default-importances-892

Conversation

@novavale
Copy link

@novavale novavale commented Mar 6, 2026

Fixes #892

Problem

Three bugs prevented newly-created cells from having their importances serialized when writing the problem:

Bug 1: importance.all no-ops before cells.append()

When setting cell.importance.all = 2.0 before the cell is added to a problem, self._problem is None and the setter silently did nothing. The intent was lost.

Bug 2: importance.all raises KeyError for missing particles

Even after linking to a problem, all.setter would crash with KeyError for particles not yet in _particle_importances (e.g. photon on a freshly created neutron-only cell).

Bug 3: Default-value importances not written

has_information returned False when all importances equal the default (1.0), even if they were explicitly set by the user. This caused c3.importance.neutron = 1.0; c3.importance.photon = 1.0 to be silently dropped.

Fix

  • all.setter: When _problem is None, store the value in _pending_all_importance and apply it lazily in link_to_problem(). Also call _generate_default_cell_tree() for any particle not yet present (mirrors existing __setitem__ behaviour).
  • link_to_problem() override: Flushes _pending_all_importance across all mode particles once the problem is known.
  • has_information: Returns True when _explicitly_set is True, ensuring default-value importances set programmatically are still written.

Testing

4 regression tests added to tests/test_importance.py covering every scenario from the issue:

Scenario Test
importance.all set before cells.append() test_892_importance_all_before_append
importance.all set after cells.append() test_892_importance_all_after_append
Individual importances set to default value test_892_importance_individual_default_value
importance.all = 1.0 (default) still written test_892_importance_all_default_value_still_written

All 952 pre-existing tests pass (2 unrelated pre-existing failures: test_version and test_fill_multi_universe_order).


📚 Documentation preview 📚: https://montepy--923.org.readthedocs.build/en/923/

Nova Vale added 3 commits March 6, 2026 05:34
…ab#892)

Three bugs prevented newly-created cells from having their importances
serialized when writing the problem:

1. importance.all setter silently no-oped when the cell was not yet
   linked to a problem (_problem is None). The intent was lost before
   cells.append() was called. Fixed by storing the pending value in
   _pending_all_importance and applying it inside link_to_problem().

2. importance.all setter raised KeyError for particles that weren't yet
   in _particle_importances (e.g. photon on a new neutron-only cell).
   Fixed by calling _generate_default_cell_tree() when the particle is
   missing, mirroring the existing __setitem__ behaviour.

3. has_information returned False for importances set to the default
   value (1.0) even when explicitly set by the user. Fixed by also
   returning True when _explicitly_set is True.

Regression tests added for all scenarios from the issue report.
…licit importances

Previously the test hardcoded start_idx=6 with a fragile check for the
literal string 'imp:n=1', which broke when a programmatically-created
cell emits 'IMP:n=1.0' (introduced by the importance fix in this PR).

Replace with a robust FILL= token search: locate the word starting with
'FILL=', then skip 2 more range words (j, k), which always lands at the
first universe number regardless of what precedes FILL in the line.
…daholab#892

- Apply black formatting to tests/test_importance.py
- Add Bugs Fixed entry to 1.3.0 changelog for issue idaholab#892
@novavale novavale marked this pull request as ready for review March 7, 2026 17:12
@MicahGale
Copy link
Collaborator

This did not follow the PR template and the issue has already been assigned.

@MicahGale MicahGale closed this Mar 8, 2026
@novavale
Copy link
Author

novavale commented Mar 8, 2026

Thanks for the look. I can see #921 addresses the same issue — happy to close this one in favor of your implementation, or to fold any useful bits in if it helps. Let me know if there's feedback on the approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Default importances are not printed in newly created cells

2 participants