Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions openmc/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class Model:
Tallies information
plots : openmc.Plots, optional
Plot information
description : str, optional
A description of the model

Attributes
----------
Expand All @@ -73,6 +75,8 @@ class Model:
Tallies information
plots : openmc.Plots
Plot information
description : str
A description of the model

"""

Expand All @@ -83,12 +87,14 @@ def __init__(
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:
Expand Down Expand Up @@ -156,6 +162,15 @@ def plots(self, plots):
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
Expand Down Expand Up @@ -365,6 +380,10 @@ def from_model_xml(cls, path: PathLike = "model.xml") -> 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)
Expand Down Expand Up @@ -701,6 +720,8 @@ def export_to_model_xml(self, path: PathLike = 'model.xml', remove_surfs: bool =
# write the XML header
fh.write("<?xml version='1.0' encoding='utf-8'?>\n")
fh.write("<model>\n")
if self.description:
fh.write(f" <description>{self.description}</description>\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,
Expand Down
18 changes: 18 additions & 0 deletions tests/unit_tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 pin cell test model"
model.export_to_model_xml()

reloaded = openmc.Model.from_model_xml()
assert reloaded.description == "PWR pin cell 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()
Expand Down
Loading