Add description attribute to Model (#3956)

Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
This commit is contained in:
Eden 2026-07-23 16:31:42 +02:00 committed by GitHub
parent 94c0defae4
commit a533fcd7f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 43 additions and 0 deletions

View file

@ -70,6 +70,8 @@ class Model:
Tallies information
plots : openmc.Plots, optional
Plot information
description : str, optional
A description of the model
Attributes
----------
@ -83,6 +85,8 @@ class Model:
Tallies information
plots : openmc.Plots
Plot information
description : str
A description of the model
"""
@ -93,12 +97,14 @@ class Model:
settings: openmc.Settings | None = None,
tallies: openmc.Tallies | None = None,
plots: openmc.Plots | None = None,
description: str = '',
):
self.geometry = openmc.Geometry() if geometry is None else geometry
self.materials = openmc.Materials() if materials is None else materials
self.settings = openmc.Settings() if settings is None else settings
self.tallies = openmc.Tallies() if tallies is None else tallies
self.plots = openmc.Plots() if plots is None else plots
self.description = description
@property
def geometry(self) -> openmc.Geometry:
@ -166,6 +172,15 @@ class Model:
for plot in plots:
self._plots.append(plot)
@property
def description(self) -> str:
return self._description
@description.setter
def description(self, description):
check_type('description', description, str)
self._description = description
@property
def bounding_box(self) -> openmc.BoundingBox:
return self.geometry.bounding_box
@ -375,6 +390,10 @@ class Model:
model = cls()
desc_elem = root.find('description')
if desc_elem is not None and desc_elem.text:
model.description = desc_elem.text
meshes = {}
model.settings = openmc.Settings.from_xml_element(
root.find('settings'), meshes)
@ -711,6 +730,12 @@ class Model:
# write the XML header
fh.write("<?xml version='1.0' encoding='utf-8'?>\n")
fh.write("<model>\n")
if self.description:
description_element = ET.Element('description')
description_element.text = self.description
fh.write(" ")
fh.write(ET.tostring(description_element, encoding="unicode"))
fh.write("\n")
# Write the materials collection to the open XML file first.
# This will write the XML header also
materials._write_xml(fh, False, level=1,

View file

@ -572,6 +572,24 @@ def test_model_xml(run_in_tmpdir):
new_model.export_to_xml()
def test_model_description(run_in_tmpdir):
model = openmc.examples.pwr_pin_cell()
model.description = "PWR fuel & water <test model>"
model.export_to_model_xml()
reloaded = openmc.Model.from_model_xml()
assert reloaded.description == "PWR fuel & water <test model>"
# Verify that an empty description is not written to XML
model2 = openmc.examples.pwr_pin_cell()
model2.export_to_model_xml('model_no_desc.xml')
with open('model_no_desc.xml') as f:
assert '<description>' not in f.read()
reloaded2 = openmc.Model.from_model_xml('model_no_desc.xml')
assert reloaded2.description == ''
def test_single_xml_exec(run_in_tmpdir):
pincell_model = openmc.examples.pwr_pin_cell()