Skip to content

Commit 704f30d

Browse files
pshriwiseclaude
andcommitted
Add DAGMCCell.from_xml_element, reuse Cell parsing logic
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>
1 parent 0b7075b commit 704f30d

1 file changed

Lines changed: 44 additions & 46 deletions

File tree

openmc/dagmc.py

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -496,53 +496,12 @@ def _parse_cell_overrides(self, elem, mats):
496496
"no materials were provided to populate the "
497497
"mapping.")
498498

499-
self._material_overrides = {}
500499
for cell_elem in elem.findall('cell'):
501-
cell_id = int(get_text(cell_elem, 'id'))
502-
name = get_text(cell_elem, 'name')
503-
504-
if get_text(cell_elem, 'region') is not None:
505-
raise ValueError("DAGMC cell overrides cannot include a region.")
506-
if get_text(cell_elem, 'fill') is not None:
507-
raise ValueError("DAGMC cell overrides currently only support "
508-
"material fills.")
509-
if get_text(cell_elem, 'universe') is not None:
510-
raise ValueError("DAGMC cell overrides cannot specify a "
511-
"universe.")
512-
for tag in ('density', 'translation', 'rotation', 'volume'):
513-
if get_text(cell_elem, tag) is not None:
514-
raise ValueError(
515-
"DAGMC cell overrides currently only support material "
516-
f"fills (found unsupported '{tag}' for cell {cell_id}).")
517-
518-
mat_ids = get_elem_list(cell_elem, 'material', str)
519-
if mat_ids is None:
520-
raise ValueError(
521-
f"DAGMC cell {cell_id} must specify a material override.")
522-
523-
mat_objs = [mats[mat_id] for mat_id in mat_ids]
524-
if len(mat_objs) == 1:
525-
fill = mat_objs[0]
526-
else:
527-
fill = mat_objs
528-
529-
temperature = get_elem_list(cell_elem, 'temperature', float)
530-
if temperature is not None:
531-
if len(temperature) > 1:
532-
cell_temp = temperature
533-
else:
534-
cell_temp = temperature[0]
535-
else:
536-
cell_temp = None
537-
538-
if cell_id in self.cells:
500+
cell = DAGMCCell.from_xml_element(cell_elem, mats)
501+
if cell.id in self.cells:
539502
raise ValueError(
540-
f"Duplicate DAGMC cell override specified for cell {cell_id}.")
541-
cell = openmc.DAGMCCell(cell_id=cell_id, name=name or '', fill=fill)
542-
if cell_temp is not None:
543-
cell.temperature = cell_temp
503+
f"Duplicate DAGMC cell override specified for cell {cell.id}.")
544504
self.add_cell(cell)
545-
self._material_overrides[cell_id] = mat_objs
546505

547506
def _partial_deepcopy(self):
548507
"""Clone all of the openmc.DAGMCUniverse object's attributes except for
@@ -705,5 +664,44 @@ def create_xml_subelement(self, xml_element, memo=None):
705664
return super().create_xml_subelement(xml_element, memo)
706665

707666
@classmethod
708-
def from_xml_element(cls, elem, surfaces, materials, get_universe):
709-
raise TypeError("from_xml_element is not available for DAGMC cells.")
667+
def from_xml_element(cls, elem, mats):
668+
"""Generate a DAGMCCell from an XML <cell> override element.
669+
670+
Parameters
671+
----------
672+
elem : lxml.etree._Element
673+
`<cell>` element containing a DAGMC cell property override
674+
mats : dict
675+
Dictionary mapping material ID strings to
676+
:class:`openmc.Material` instances
677+
678+
Returns
679+
-------
680+
DAGMCCell
681+
DAGMCCell instance
682+
"""
683+
cell_id = int(get_text(elem, 'id'))
684+
685+
# Validate attributes that are unsupported for DAGMC cell overrides
686+
for tag in ('region', 'fill', 'universe'):
687+
if get_text(elem, tag) is not None:
688+
raise ValueError(
689+
f"DAGMC cell {cell_id} override cannot specify '{tag}'.")
690+
for tag in ('density', 'translation', 'rotation', 'volume'):
691+
if get_text(elem, tag) is not None:
692+
raise ValueError(
693+
f"DAGMC cell {cell_id} override currently only supports "
694+
f"material fills (found unsupported '{tag}').")
695+
if get_elem_list(elem, 'material', str) is None:
696+
raise ValueError(
697+
f"DAGMC cell {cell_id} must specify a material override.")
698+
699+
# Delegate to Cell.from_xml_element for common parsing. Supply a
700+
# no-op universe since DAGMC cells are not placed in a CSG universe.
701+
class _NullUniverse:
702+
def add_cell(self, _):
703+
pass
704+
705+
return super().from_xml_element(
706+
elem, surfaces={}, materials=mats,
707+
get_universe=lambda _: _NullUniverse())

0 commit comments

Comments
 (0)