Skip to content

Commit 5e0a047

Browse files
pshriwiseclaude
andcommitted
Make universe required in DAGMCCell.from_xml_element, remove stub
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>
1 parent 6b41684 commit 5e0a047

2 files changed

Lines changed: 13 additions & 21 deletions

File tree

openmc/dagmc.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ def create_xml_subelement(self, xml_element, memo=None):
636636
return super().create_xml_subelement(xml_element, memo)
637637

638638
@classmethod
639-
def from_xml_element(cls, elem, mats, universe=None):
639+
def from_xml_element(cls, elem, mats, universe):
640640
"""Generate a DAGMCCell from an XML <cell> override element.
641641
642642
Parameters
@@ -646,9 +646,8 @@ def from_xml_element(cls, elem, mats, universe=None):
646646
mats : dict
647647
Dictionary mapping material ID strings to
648648
:class:`openmc.Material` instances
649-
universe : DAGMCUniverse, optional
650-
Universe to add the parsed cell to. If not provided the cell is
651-
returned without being added to any universe.
649+
universe : DAGMCUniverse
650+
Universe to add the parsed cell to.
652651
653652
Returns
654653
-------
@@ -671,17 +670,6 @@ def from_xml_element(cls, elem, mats, universe=None):
671670
raise ValueError(
672671
f"DAGMC cell {cell_id} must specify a material override.")
673672

674-
# Delegate to Cell.from_xml_element. Pass the DAGMCUniverse directly
675-
# so the cell is registered there; if no universe is provided use a
676-
# no-op stub so the cell is returned without being added anywhere.
677-
if universe is not None:
678-
target = universe
679-
else:
680-
class _Stub:
681-
def add_cell(self, _):
682-
pass
683-
target = _Stub()
684-
685673
return super().from_xml_element(
686674
elem, surfaces={}, materials=mats,
687-
get_universe=lambda _: target)
675+
get_universe=lambda _: universe)

tests/unit_tests/test_cell.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,14 @@ def test_dagmccell_from_xml_element():
380380
mat = openmc.Material(1)
381381
mat.add_nuclide('U235', 1.0)
382382
mats = {'1': mat}
383+
# In practice, from_xml_element is always called from DAGMCUniverse
384+
# during XML parsing. A placeholder universe is used here so the test
385+
# can exercise the method directly without a real DAGMC model file.
386+
placeholder_univ = openmc.DAGMCUniverse('model.h5m')
383387

384388
# material + temperature + density round-trip
385389
xml = '<cell id="5" name="fuel" material="1" temperature="900.0" density="10.5"/>'
386-
cell = openmc.DAGMCCell.from_xml_element(ET.fromstring(xml), mats)
390+
cell = openmc.DAGMCCell.from_xml_element(ET.fromstring(xml), mats, placeholder_univ)
387391
assert cell.id == 5
388392
assert cell.name == 'fuel'
389393
assert cell.fill is mat
@@ -392,25 +396,25 @@ def test_dagmccell_from_xml_element():
392396

393397
# volume round-trip
394398
xml = '<cell id="6" material="1" volume="42.0"/>'
395-
cell = openmc.DAGMCCell.from_xml_element(ET.fromstring(xml), mats)
399+
cell = openmc.DAGMCCell.from_xml_element(ET.fromstring(xml), mats, placeholder_univ)
396400
assert cell.volume == 42.0
397401

398402
# forbidden: region, fill, universe
399403
for tag, val in [('region', '-1'), ('fill', '2'), ('universe', '0')]:
400404
xml = f'<cell id="7" material="1" {tag}="{val}"/>'
401405
with pytest.raises(ValueError, match=tag):
402-
openmc.DAGMCCell.from_xml_element(ET.fromstring(xml), mats)
406+
openmc.DAGMCCell.from_xml_element(ET.fromstring(xml), mats, placeholder_univ)
403407

404408
# forbidden: translation, rotation
405409
for tag, val in [('translation', '1 0 0'), ('rotation', '0 0 90')]:
406410
xml = f'<cell id="7" material="1" {tag}="{val}"/>'
407411
with pytest.raises(ValueError, match=tag):
408-
openmc.DAGMCCell.from_xml_element(ET.fromstring(xml), mats)
412+
openmc.DAGMCCell.from_xml_element(ET.fromstring(xml), mats, placeholder_univ)
409413

410414
# missing material raises
411415
xml = '<cell id="8"/>'
412416
with pytest.raises(ValueError, match='material'):
413-
openmc.DAGMCCell.from_xml_element(ET.fromstring(xml), mats)
417+
openmc.DAGMCCell.from_xml_element(ET.fromstring(xml), mats, placeholder_univ)
414418

415419

416420
def test_plot(run_in_tmpdir):

0 commit comments

Comments
 (0)