Skip to content

fix(importance): correct multi-particle import after cell append#922

Open
novavale wants to merge 1 commit intoidaholab:developfrom
novavale:fix/importance-multi-particle-913
Open

fix(importance): correct multi-particle import after cell append#922
novavale wants to merge 1 commit intoidaholab:developfrom
novavale:fix/importance-multi-particle-913

Conversation

@novavale
Copy link

@novavale novavale commented Mar 6, 2026

Closes #913

Problem

When Cell.importance is set for multiple particle types after adding the cell to an MCNP_Problem, two bugs occur:

  1. Same value across particles (e.g. photon=2.0 then neutron=2.0) → produces invalid MCNP output with duplicate importance lines
  2. Different values across particles (e.g. photon=3.0 then neutron=4.0) → crashes MontePy

Root Cause

The _particle_importances dict and _part_combos list were not being properly reset when individual particle importances were set post-append. As a result, stale combo state caused duplicate output or key conflicts on write.

Fix

Updated the importance setter logic to correctly rebuild _real_tree and _part_combos when setting particle importances on a cell already linked to a problem.

Tests

Added regression tests covering both bug scenarios from the issue report. All existing tests continue to pass.


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

…holab#913)

When Cell.importance was set for multiple particle types after adding
the cell to an MCNP_Problem, _generate_default_cell_tree() was
overwriting the new tree's ParticleNode with all problem mode particles
(particles.particles = self._problem.mode.particles). This caused two
bugs:

1. Same value for multiple particles (e.g. photon=2.0 then neutron=2.0):
   the PHOTON tree's classifier claimed "IMP:p,n" and produced duplicate
   output like "IMP:n=2.0 IMP:p,n=2.0".

2. Different values (e.g. photon=3.0 then neutron=4.0): the particles
   setter only updated _particles but not _order (since it received a
   set, not a list). _format_tree then crashed with
   "list.remove(x): x not in list" when trying to remove a particle
   present in _particles but absent from _order.

Fix: remove the mode-particles override so each default cell tree's
classifier contains only the specific particle it was created for.
Each particle's importance is then formatted separately (e.g.
"IMP:n=4.0 IMP:p=3.0"), which is valid MCNP syntax.

Add regression tests for both scenarios.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

Bugs with setting multiple Cell importances after adding to Problem

1 participant