Skip to content

Commit 52d7c65

Browse files
pshriwiseclaude
andcommitted
Pass DAGMCUniverse to DAGMCCell.from_xml_element, drop _NullUniverse 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>
1 parent 49ac308 commit 52d7c65

2 files changed

Lines changed: 31 additions & 84 deletions

File tree

openmc/dagmc.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,11 @@ def _parse_cell_overrides(self, elem, mats):
442442
"mapping.")
443443

444444
for cell_elem in elem.findall('cell'):
445-
cell = DAGMCCell.from_xml_element(cell_elem, mats)
446-
if cell.id in self.cells:
445+
cell_id = int(get_text(cell_elem, 'id'))
446+
if cell_id in self.cells:
447447
raise ValueError(
448-
f"Duplicate DAGMC cell override specified for cell {cell.id}.")
449-
self.add_cell(cell)
448+
f"Duplicate DAGMC cell override specified for cell {cell_id}.")
449+
DAGMCCell.from_xml_element(cell_elem, mats, self)
450450

451451
def _partial_deepcopy(self):
452452
"""Clone all of the openmc.DAGMCUniverse object's attributes except for
@@ -622,7 +622,7 @@ def create_xml_subelement(self, xml_element, memo=None):
622622
return super().create_xml_subelement(xml_element, memo)
623623

624624
@classmethod
625-
def from_xml_element(cls, elem, mats):
625+
def from_xml_element(cls, elem, mats, universe=None):
626626
"""Generate a DAGMCCell from an XML <cell> override element.
627627
628628
Parameters
@@ -632,6 +632,9 @@ def from_xml_element(cls, elem, mats):
632632
mats : dict
633633
Dictionary mapping material ID strings to
634634
:class:`openmc.Material` instances
635+
universe : DAGMCUniverse, optional
636+
Universe to add the parsed cell to. If not provided the cell is
637+
returned without being added to any universe.
635638
636639
Returns
637640
-------
@@ -654,12 +657,17 @@ def from_xml_element(cls, elem, mats):
654657
raise ValueError(
655658
f"DAGMC cell {cell_id} must specify a material override.")
656659

657-
# Delegate to Cell.from_xml_element for common parsing. Supply a
658-
# no-op universe since DAGMC cells are not placed in a CSG universe.
659-
class _NullUniverse:
660-
def add_cell(self, _):
661-
pass
660+
# Delegate to Cell.from_xml_element. Pass the DAGMCUniverse directly
661+
# so the cell is registered there; if no universe is provided use a
662+
# no-op stub so the cell is returned without being added anywhere.
663+
if universe is not None:
664+
target = universe
665+
else:
666+
class _Stub:
667+
def add_cell(self, _):
668+
pass
669+
target = _Stub()
662670

663671
return super().from_xml_element(
664672
elem, surfaces={}, materials=mats,
665-
get_universe=lambda _: _NullUniverse())
673+
get_universe=lambda _: target)

tests/unit_tests/dagmc/test_model.py

Lines changed: 12 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -70,30 +70,6 @@ def model(request):
7070
openmc.reset_auto_ids()
7171

7272

73-
def test_dagmc_replace_material_assignment(model):
74-
mats = {}
75-
76-
mats["foo"] = openmc.Material(name="foo")
77-
mats["foo"].add_nuclide("H1", 2.0)
78-
mats["foo"].add_element("O", 1.0)
79-
mats["foo"].set_density("g/cm3", 1.0)
80-
mats["foo"].add_s_alpha_beta("c_H_in_H2O")
81-
82-
for univ in model.geometry.get_all_universes().values():
83-
if not isinstance(univ, openmc.DAGMCUniverse):
84-
break
85-
86-
cells_with_41 = []
87-
for cell in univ.cells.values():
88-
if cell.fill is None:
89-
continue
90-
if cell.fill.name == "41":
91-
cells_with_41.append(cell.id)
92-
univ.replace_material_assignment("41", mats["foo"])
93-
for cell_id in cells_with_41:
94-
assert univ.cells[cell_id].fill == mats["foo"]
95-
96-
9773
def test_dagmc_sync_cell_names(model):
9874
dag_univ = None
9975
for univ in model.geometry.get_all_universes().values():
@@ -190,57 +166,20 @@ def test_model_differentiate_with_dagmc(model):
190166
assert len(model.materials) == 4*2 + 4
191167

192168

193-
def test_bad_override_cell_id(model):
194-
for univ in model.geometry.get_all_universes().values():
195-
if isinstance(univ, openmc.DAGMCUniverse):
196-
break
197-
with pytest.raises(ValueError, match="Cell ID '1' not found in DAGMC universe"):
198-
univ.material_overrides = {1: model.materials[0]}
199-
200-
201-
def test_bad_override_type(model):
202-
not_a_dag_cell = openmc.Cell()
203-
for univ in model.geometry.get_all_universes().values():
204-
if isinstance(univ, openmc.DAGMCUniverse):
205-
break
206-
with pytest.raises(ValueError, match="Unrecognized key type. Must be an integer or openmc.DAGMCCell object"):
207-
univ.material_overrides = {not_a_dag_cell: model.materials[0]}
208-
209-
210-
def test_bad_replacement_mat_name(model):
211-
for univ in model.geometry.get_all_universes().values():
212-
if isinstance(univ, openmc.DAGMCUniverse):
213-
break
214-
with pytest.raises(ValueError, match="No material with name 'not_a_mat' found in the DAGMC universe"):
215-
univ.replace_material_assignment("not_a_mat", model.materials[0])
216-
217-
218169
def test_dagmc_xml(model):
219-
# Set the environment
220-
mats = {}
221-
mats["no-void fuel"] = openmc.Material(1, name="no-void fuel")
222-
mats["no-void fuel"].add_nuclide("U235", 0.03)
223-
mats["no-void fuel"].add_nuclide("U238", 0.97)
224-
mats["no-void fuel"].add_nuclide("O16", 2.0)
225-
mats["no-void fuel"].set_density("g/cm3", 10.0)
226-
227-
mats[5] = openmc.Material(name="41")
228-
mats[5].add_nuclide("H1", 2.0)
229-
mats[5].add_element("O", 1.0)
230-
mats[5].set_density("g/cm3", 1.0)
231-
mats[5].add_s_alpha_beta("c_H_in_H2O")
170+
override_mat = openmc.Material(name="41")
171+
override_mat.add_nuclide("H1", 2.0)
172+
override_mat.add_element("O", 1.0)
173+
override_mat.set_density("g/cm3", 1.0)
174+
override_mat.add_s_alpha_beta("c_H_in_H2O")
175+
model.materials.append(override_mat)
232176

233177
for univ in model.geometry.get_all_universes().values():
234178
if isinstance(univ, openmc.DAGMCUniverse):
235179
dag_univ = univ
236180
break
237181

238-
for k, v in mats.items():
239-
if isinstance(k, int):
240-
dag_univ.add_material_override(k, v)
241-
model.materials.append(v)
242-
elif isinstance(k, str):
243-
dag_univ.replace_material_assignment(k, v)
182+
dag_univ.add_material_override(5, override_mat)
244183

245184
# Tesing the XML subelement generation
246185
root = ET.Element('dagmc_universe')
@@ -304,7 +243,7 @@ def test_dagmc_xml_reject_fill_override():
304243
'<cell id="1" fill="2"/>'
305244
'</dagmc_universe>'
306245
)
307-
with pytest.raises(ValueError, match="only support material fills"):
246+
with pytest.raises(ValueError, match="cannot specify 'fill'"):
308247
openmc.DAGMCUniverse.from_xml_element(elem, mats)
309248

310249

@@ -315,7 +254,7 @@ def test_dagmc_xml_reject_region_override():
315254
'<cell id="1" material="1" region="-1"/>'
316255
'</dagmc_universe>'
317256
)
318-
with pytest.raises(ValueError, match="cannot include a region"):
257+
with pytest.raises(ValueError, match="cannot specify 'region'"):
319258
openmc.DAGMCUniverse.from_xml_element(elem, mats)
320259

321260

@@ -413,14 +352,14 @@ def test_dagmc_xml_temperature_roundtrip():
413352

414353
dag_univ = openmc.DAGMCUniverse.from_xml_element(elem, mats)
415354
assert dag_univ.cells[7].fill.id == 1
416-
assert dag_univ.cells[7].temperature == pytest.approx(825.0)
355+
assert dag_univ.cells[7].temperature == pytest.approx([825.0])
417356

418357
root = ET.Element('geometry')
419358
dag_univ.create_xml_subelement(root)
420359
dagmc_elem = root.find('dagmc_universe')
421360
xml_cell = dagmc_elem.find('cell')
422-
assert xml_cell.get('temperature') == '825.0'
361+
assert xml_cell.find('temperature').text == '825.0'
423362

424363
dag_univ_roundtrip = openmc.DAGMCUniverse.from_xml_element(dagmc_elem, mats)
425364
assert dag_univ_roundtrip.cells[7].fill.id == 1
426-
assert dag_univ_roundtrip.cells[7].temperature == pytest.approx(825.0)
365+
assert dag_univ_roundtrip.cells[7].temperature == pytest.approx([825.0])

0 commit comments

Comments
 (0)