Skip to content

Preserve user material names in convert_to_multigroup and avoid overwriting material with same name bug fix#3984

Merged
jtramm merged 2 commits into
openmc-dev:developfrom
shimwell:convert-mgxs-preserve-material-names
Jun 30, 2026
Merged

Preserve user material names in convert_to_multigroup and avoid overwriting material with same name bug fix#3984
jtramm merged 2 commits into
openmc-dev:developfrom
shimwell:convert-mgxs-preserve-material-names

Conversation

@shimwell

Copy link
Copy Markdown
Member

Summary

convert_to_multigroup overwrote each material's name with a sanitised, HDF5-safe version, because the name was used both as the key for the material's XSdata entry in the MGXS library and as the macroscopic the material reads back. This silently changed user-assigned names, and two distinct materials whose names collided were written as a single cross section (one overwriting the other).

This keeps material.name untouched and instead keys the library by a separate name + id string (sanitised to alphanumeric/underscore; unique because the material ID is unique). The macroscopic reads its data under that library name. As a result:

  • user material names are preserved (a material with no name stays unnamed),
  • distinct materials that share a name no longer collapse — each gets its own cross section.

Verified end-to-end: a model with two distinct same-named materials converts and runs in multigroup mode, the library containing one entry per material.

Tests

Added a unit test asserting names are preserved and same-named materials map to distinct library entries.

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format (version 18) on any C++ source files (if applicable)
  • I have followed the style guidelines for Python source files (if applicable)
  • I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

convert_to_multigroup overwrote each material's name with a sanitised,
HDF5-safe version, because the name is used as the key for the material's
XSdata entry in the MGXS library and for the macroscopic that reads it back.
This silently changed user-assigned names, and two distinct materials whose
names collided were written as a single cross section (one overwriting the
other).

Use a unique library name (the sanitised name plus the material ID) for the
library entry and macroscopic, then restore each material's original name at
the end, so the user's names are preserved and same-named materials no longer
collapse.
@shimwell shimwell requested a review from jtramm June 25, 2026 16:22
@jon-proximafusion jon-proximafusion force-pushed the convert-mgxs-preserve-material-names branch 2 times, most recently from 34498bd to 75699d4 Compare June 25, 2026 16:32
convert_to_multigroup now keeps the user's original material name in the
exported materials.xml and keys each macroscopic / MGXS-library entry by
sanitize(name) + "_{id}". Regenerate the inputs_true.dat references for the
random_ray_auto_convert* and random_ray_diagonal_stabilization tests to match.
Only dataset labels change; the MGXS values and results are unaffected.
@shimwell shimwell force-pushed the convert-mgxs-preserve-material-names branch from b86ff79 to dc8af9b Compare June 25, 2026 20:56
@shimwell shimwell changed the title Preserve user material names in convert_to_multigroup Preserve user material names in convert_to_multigroup and avoid overwriting material with same name bug fix Jun 25, 2026
@shimwell

Copy link
Copy Markdown
Member Author

I just updated the regression test files to accommodate the new names in the mgxs and now the CI is green.

What do you think @jtramm is this PR small and contained enough to merge in before the next release

@jtramm jtramm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for putting this together!

@jtramm jtramm merged commit d06fdee into openmc-dev:develop Jun 30, 2026
19 checks passed
jon-proximafusion pushed a commit to shimwell/openmc that referenced this pull request Jun 30, 2026
Add method="material_cell_wise" to Model.convert_to_multigroup: like
material_wise, but it tallies multigroup cross sections per CELL instead of per
material, so each material-filled cell gets its own spatially-resolved cross
sections (each cell is assigned a distinct macroscopic). This captures
intra-material spectral variation that material_wise averages away when one
material spans a strong gradient (e.g. a thick shield modeled as one material).

Reuses the entire material_wise run/tally/collapse path via a domain_type
parameter threaded through _auto_generate_mgxs_lib and _isothermal_materialwise_mgxs
(openmc.mgxs.Library already supports domain_type="cell"); the only genuinely new
code is the per-cell macroscopic assignment in convert_to_multigroup. A shared
_mgxs_domain_key helper keys each cell by sanitized name + id.

Stacked on openmc-dev#3984 (name+id library keying): per-cell macroscopics for cells that
share a material would otherwise collide in the library. Retarget to develop once
openmc-dev#3984 merges. Adds a unit test that two cells sharing a material get distinct
macroscopics.
jon-proximafusion pushed a commit to shimwell/openmc that referenced this pull request Jun 30, 2026
Add method="material_cell_wise" to Model.convert_to_multigroup: like
material_wise, but it tallies multigroup cross sections per CELL instead of per
material, so each material-filled cell gets its own spatially-resolved cross
sections (each cell is assigned a distinct macroscopic). This captures
intra-material spectral variation that material_wise averages away when one
material spans a strong gradient (e.g. a thick shield modeled as one material).

Reuses the entire material_wise run/tally/collapse path via a domain_type
parameter threaded through _auto_generate_mgxs_lib and _isothermal_materialwise_mgxs
(openmc.mgxs.Library already supports domain_type="cell"); the only genuinely new
code is the per-cell macroscopic assignment in convert_to_multigroup. A shared
_mgxs_domain_key helper keys each cell by sanitized name + id.

Stacked on openmc-dev#3984 (name+id library keying): per-cell macroscopics for cells that
share a material would otherwise collide in the library. Retarget to develop once
openmc-dev#3984 merges. Adds a unit test that two cells sharing a material get distinct
macroscopics.
jon-proximafusion pushed a commit to shimwell/openmc that referenced this pull request Jun 30, 2026
Add method="material_cell_wise" to Model.convert_to_multigroup: like
material_wise, but gives each cell its own multigroup cross sections. The
material in every material-filled cell is cloned (each clone gets a unique id),
then the standard per-material generation runs, so per material becomes per
cell. This captures the intra-material spatial-spectrum variation that
material_wise averages away when one material spans a strong gradient.

The implementation reuses the material_wise path entirely; the only new code is
the per-cell cloning step in convert_to_multigroup plus the dispatch entry. Adds
a unit test (two cells sharing a material get distinct macroscopics) and a user
guide entry in the MGXS methods table.

Builds on the name+id library keying from openmc-dev#3984 (now in develop).
jon-proximafusion pushed a commit to shimwell/openmc that referenced this pull request Jun 30, 2026
Add method="material_cell_wise" to Model.convert_to_multigroup: like
material_wise, but gives each cell its own multigroup cross sections. The
material in every material-filled cell is cloned (each clone gets a unique id),
then the standard per-material generation runs, so per material becomes per
cell. This captures the intra-material spatial-spectrum variation that
material_wise averages away when one material spans a strong gradient.

The implementation reuses the material_wise path entirely; the only new code is
the per-cell cloning step in convert_to_multigroup plus the dispatch entry. Adds
a unit test (two cells sharing a material get distinct macroscopics) and a user
guide entry in the MGXS methods table.

Builds on the name+id library keying from openmc-dev#3984 (now in develop).
jon-proximafusion added a commit to shimwell/openmc that referenced this pull request Jun 30, 2026
Add method="material_cell_wise" to Model.convert_to_multigroup: like
material_wise, but gives each cell its own multigroup cross sections. The
material in every material-filled cell is cloned (each clone gets a unique id),
then the standard per-material generation runs, so per material becomes per
cell. This captures the intra-material spatial-spectrum variation that
material_wise averages away when one material spans a strong gradient.

The implementation reuses the material_wise path entirely; the only new code is
the per-cell cloning step in convert_to_multigroup plus the dispatch entry. Adds
a unit test (two cells sharing a material get distinct macroscopics) and a user
guide entry in the MGXS methods table.

Builds on the name+id library keying from openmc-dev#3984 (now in develop).

Co-authored-by: jon-proxima <jon@proximafusion.com>
jon-proximafusion added a commit to shimwell/openmc that referenced this pull request Jul 1, 2026
Add method="cell_wise" to Model.convert_to_multigroup: like material_wise, but
gives each cell its own multigroup cross sections. The material in every
material-filled cell is cloned (each clone gets a unique id), then the standard
per-material generation runs, so per material becomes per cell. This captures the
intra-material spatial-spectrum variation that material_wise averages away when
one material spans a strong gradient.

The implementation reuses the material_wise path entirely; the only new code is
the per-cell cloning step in convert_to_multigroup plus the dispatch entry. Adds
unit tests (CSG and DAGMC: two cells sharing a material get distinct
macroscopics) and a user guide entry in the MGXS methods table.

Builds on the name+id library keying from openmc-dev#3984 (now in develop).

Co-authored-by: jon-proxima <jon@proximafusion.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants