Skip to content

DAGMC Cell Override Updates#3888

Open
pshriwise wants to merge 15 commits intoopenmc-dev:developfrom
pshriwise:dagmc-cell-overrides
Open

DAGMC Cell Override Updates#3888
pshriwise wants to merge 15 commits intoopenmc-dev:developfrom
pshriwise:dagmc-cell-overrides

Conversation

@pshriwise
Copy link
Contributor

Description

This takes a different approach to material overrides by reusing the current XML cell specification. The goal is to provide better support for not only material overrides but other cell properties as well (e.g. temperatures, densities). Later, with support for lattice/universe fills of DAGMC cells additional properties like translation and rotation will become useful as well.

Things to consider:

  • I've made a hard transition here, removing support for the old override format right away as it hasn't been with us long. Old XML files using that format can still be read into Python or C++, however.
  • I've refactored some code for reading temperatures and materials from an XML cell node, which may be a little overkill in terms of indirection to reduce duplicate code.

Claude's PR Description

Summary

This PR replaces the ad-hoc <material_overrides> XML schema for DAGMC universes with a
generalized <cell> sub-element approach that reuses OpenMC's standard cell XML schema.
The new format supports material, temperature, density, and volume overrides on a per-cell
basis, with full parity between the Python and C++ interfaces.

Motivation

The previous <material_overrides> schema was limited to material assignment only and used
a bespoke XML structure not shared with the rest of OpenMC's geometry input. This PR:

  • Aligns DAGMC cell overrides with the standard <cell> XML schema
  • Adds support for temperature, density, and volume overrides
  • Consolidates duplicated XML parsing logic into shared free functions
  • Provides backward compatibility for existing inputs using the old format
  • Removes the deprecated material_overrides property, replace_material_assignment
    method, and related internal state from DAGMCUniverse

<cell> Sub-element Specification

A <cell> element nested inside a <dagmc_universe> overrides properties of the DAGMC
volume with the matching id.

Supported Attributes

Attribute Required Type Description
id Yes integer DAGMC cell/volume ID to override
name No string Optional label for the cell
material Yes string or space-separated list Material ID(s). Use void for vacuum. Multiple IDs specify a distribmat fill. May also be a <material> child element.
temperature No float or space-separated list Temperature(s) in Kelvin. Must be ≥ 0. May also be a <temperature> child element.
density No float or space-separated list Density in g/cm³. Must be > 0. Requires a non-void material fill. May also be a <density> child element.
volume No float Cell volume in cm³. Note: DAGMC can compute volumes exactly from the triangulated mesh. Specifying a manual volume overrides that capability and risks inconsistency.

Unsupported Attributes

The following standard <cell> attributes are not valid inside <dagmc_universe> and will
raise an error:

Attribute Reason
region DAGMC cells have no CSG region definition
fill Only material fills are supported (universe fills are not)
universe Cannot specify a containing universe
translation Not yet supported
rotation Not yet supported

Example

<dagmc_universe id="1" filename="model.h5m">
  <!-- Single material + temperature override -->
  <cell id="3" material="10" temperature="900.0"/>

  <!-- Distribmat override (two materials) -->
  <cell id="5">
    <material>20 21</material>
  </cell>

  <!-- Void fill -->
  <cell id="7" material="void"/>

  <!-- Density + volume override (use volume with caution) -->
  <cell id="9" material="10" density="10.5" volume="42.0"/>
</dagmc_universe>

Backward Compatibility

The old <material_overrides> format is deprecated but still accepted in both Python
and C++. A DeprecationWarning (Python) or warning() (C++) is emitted, and the overrides
are converted to the new <cell> representation at parse time. It is an error to mix both
formats on the same universe.

<!-- DEPRECATED — use <cell> sub-elements instead -->
<dagmc_universe id="1" filename="model.h5m">
  <material_overrides>
    <cell_override id="3"><material_ids>10</material_ids></cell_override>
  </material_overrides>
</dagmc_universe>

Implementation Details

C++ (src/cell.cpp, include/openmc/cell.h)

Three free functions are added to centralize <cell> XML parsing, shared by both
CSGCell::CSGCell and DAGUniverse:

  • parse_cell_material_xml(node, cell_id) — parses material attribute/element, handles "void"
  • parse_cell_temperature_xml(node, cell_id) — parses temperature, validates ≥ 0
  • parse_cell_density_xml(node, cell_id) — parses density, validates > 0

DAGUniverse uses intermediate maps (MaterialOverrides, TemperatureOverrides,
DensityOverrides) rather than applying values directly, because DAGMC Cell objects do
not exist until the .h5m file is loaded during init_geometry().

Python (openmc/dagmc.py)

  • DAGMCCell.from_xml_element is added as a classmethod that delegates to
    Cell.from_xml_element via super(). It accepts an optional universe parameter; when
    called from _parse_cell_overrides the owning DAGMCUniverse is passed directly so the
    cell is registered in one step without a separate add_cell call.
  • DAGMCUniverse._parse_legacy_material_overrides handles the deprecated XML format.
  • The material_overrides property is retained but both the getter and setter now raise
    AttributeError with a message directing users to the DAGMCCell-based API.
  • The replace_material_assignment method, _get_cell_material_overrides helper, and
    the _material_overrides internal attribute are removed entirely.

Tests

  • tests/unit_tests/test_cell.pytest_dagmccell_from_xml_element: round-trip of
    material, temperature, density, and volume; rejection of forbidden attributes; missing
    material error.
  • tests/unit_tests/dagmc/test_model.py:
    • test_dagmc_xml_legacy_single_material_compat — old format, single material
    • test_dagmc_xml_legacy_distribmat_compat — old format, multiple material IDs
    • test_dagmc_xml_legacy_void_compat — old format, void material
    • test_dagmc_xml_legacy_both_raises — error when both formats present on same universe
    • test_dagmc_xml_legacy_deprecation_warning — confirms DeprecationWarning is emitted
    • test_dagmc_xml_legacy_roundtrip — old format parsed → new <cell> format emitted

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)

pshriwise and others added 15 commits March 3, 2026 13:37
Extract parse_cell_material_xml() and parse_cell_temperature_xml() from
CSGCell::CSGCell() into free functions in cell.h/cell.cpp so that
DAGUniverse can reuse the same logic when parsing <cell> override
sub-elements. Both handle void/material-id conversion, empty-array
validation, and non-negative temperature validation. Each call site
retains its own higher-level policy (fill/material exclusivity in
CSGCell, forbidden-attribute guards and duplicate detection in
DAGUniverse).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Replace the TypeError stub in DAGMCCell.from_xml_element with a proper
implementation. Validates DAGMC-unsupported attributes (region, fill,
universe, density, translation, rotation, volume) and required material,
then delegates to Cell.from_xml_element for common id/name/material/
temperature parsing via a no-op universe stub. DAGMCUniverse._parse_cell_
overrides is simplified to loop over <cell> elements and call
DAGMCCell.from_xml_element, removing all duplicated parsing logic.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Density overrides:
- Add parse_cell_density_xml() free function (cell.h/cell.cpp), used by
  both CSGCell::CSGCell (replacing inline parsing) and DAGUniverse XML
  constructor
- Add DensityOverrides type alias and thread it through initialize() and
  init_geometry() in DAGUniverse; apply density_mult_ in init_geometry()
  using the same deferred-multiplier convention as CSG cells
- Remove density from the forbidden-attribute block in the DAGMC XML
  constructor and from DAGMCCell validation

Volume overrides:
- Volume is Python-only (not stored in C++ Cell); no C++ changes needed
- Remove volume from forbidden-attribute checks in DAGMCCell
- Add a Notes section to DAGMCCell docstring warning that manually
  specifying volume may be inconsistent with DAGMC's triangulated-surface
  geometry, which can support exact volume computation

translation and rotation remain unsupported and are rejected explicitly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Covers: material/temperature/density/volume parsing, forbidden attribute
rejection (region, fill, universe, translation, rotation), and missing
material error. Added to test_cell.py alongside existing Cell XML tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…hema

Both Python and C++ now accept the old <material_overrides> format with
a DeprecationWarning/warning() instead of a hard error, converting each
<cell_override> into an equivalent DAGMCCell or MaterialOverrides entry.

- Python: replace the ValueError in DAGMCUniverse.from_xml_element with
  a DeprecationWarning; add _parse_legacy_material_overrides() which
  builds DAGMCCell objects from <cell_override id> / <material_ids> text
- C++: replace the fatal_error in DAGUniverse XML constructor with a
  warning() + conversion loop into material_overrides map
- Both sides raise an error if <material_overrides> and <cell> appear
  together on the same <dagmc_universe>
- Replace test_dagmc_xml_reject_legacy_material_overrides with six new
  tests covering single-material, distribmat, void, both-raises,
  deprecation-warning, and round-trip-to-new-format scenarios

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Removes DAGMCUniverse.material_overrides (getter/setter),
replace_material_assignment, _get_cell_material_overrides, and the
associated _material_overrides internal attribute. Cell material
assignments are now managed directly via DAGMCCell objects.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…stub

DAGMCCell.from_xml_element now accepts an optional universe parameter.
When called from _parse_cell_overrides the actual DAGMCUniverse is passed,
so Cell.from_xml_element registers the cell directly rather than requiring
a separate add_cell call. Remove tests for the deleted material_overrides
setter, replace_material_assignment, and stale error message patterns.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Replace the deprecated <material_overrides> documentation with the new
<cell> sub-element spec covering id, name, material, temperature, density,
and volume. Include unsupported attribute list and a deprecation note for
the old format.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Both the getter and setter raise AttributeError with a message directing
users to the DAGMCCell-based API.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
universe is now a required parameter since from_xml_element is always
called from a DAGMCUniverse context. The test uses a clearly named
placeholder_univ with a comment explaining the direct call is only
for testing purposes.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@pshriwise pshriwise changed the title Dagmc cell overrides DAGMC Cell Override Updates Mar 18, 2026
@pshriwise
Copy link
Contributor Author

@bam241 I'd especially like to hear your thoughts on this one if you have the time.

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.

1 participant