diff --git a/openmc/plots.py b/openmc/plots.py index 5bc8a2e6e..6afe3ea33 100644 --- a/openmc/plots.py +++ b/openmc/plots.py @@ -11,7 +11,7 @@ import openmc import openmc.checkvalue as cv from openmc.checkvalue import PathLike -from ._xml import clean_indentation, get_elem_tuple, reorder_attributes +from ._xml import clean_indentation, get_elem_tuple, reorder_attributes, get_text from .mixin import IDManagerMixin _BASES = ['xy', 'xz', 'yz'] @@ -482,6 +482,9 @@ class PlotBase(IDManagerMixin): """ element = ET.Element("plot") + element.set("id", str(self._id)) + if len(self._name) > 0: + element.set("name", str(self.name)) if self._filename is not None: element.set("filename", self._filename) element.set("color_by", self._color_by) @@ -782,7 +785,6 @@ class Plot(PlotBase): """ element = super().to_xml_element() - element.set("id", str(self._id)) element.set("type", self._type) if self._type == 'slice': @@ -843,12 +845,14 @@ class Plot(PlotBase): """ plot_id = int(elem.get("id")) - plot = cls(plot_id) + name = get_text(elem, 'name', '') + plot = cls(plot_id, name) if "filename" in elem.keys(): plot.filename = elem.get("filename") plot.color_by = elem.get("color_by") plot.type = elem.get("type") - plot.basis = elem.get("basis") + if plot.type == 'slice': + plot.basis = elem.get("basis") plot.origin = get_elem_tuple(elem, "origin", float) plot.width = get_elem_tuple(elem, "width", float) @@ -1176,7 +1180,6 @@ class ProjectionPlot(PlotBase): """ element = super().to_xml_element() - element.set("id", str(self._id)) element.set("type", "projection") subelement = ET.SubElement(element, "camera_position") @@ -1276,7 +1279,7 @@ class ProjectionPlot(PlotBase): tmp = elem.find("orthographic_width") if tmp is not None: - self.orthographic_width = float(tmp) + plot.orthographic_width = float(tmp) plot.pixels = get_elem_tuple(elem, "pixels") plot.camera_position = get_elem_tuple(elem, "camera_position", float) diff --git a/tests/unit_tests/test_plots.py b/tests/unit_tests/test_plots.py index 5e5e1f22c..922e4d9dc 100644 --- a/tests/unit_tests/test_plots.py +++ b/tests/unit_tests/test_plots.py @@ -199,3 +199,25 @@ def test_plots(run_in_tmpdir): assert new_plots[0].colors == p1.colors assert new_plots[0].mask_components == p1.mask_components assert new_plots[1].origin == p2.origin + + +def test_voxel_plot_roundtrip(): + # Define a voxel plot and create XML element + plot = openmc.Plot(name='my voxel plot') + plot.type = 'voxel' + plot.filename = 'voxel1' + plot.pixels = (50, 50, 50) + plot.origin = (0., 0., 0.) + plot.width = (75., 75., 75.) + plot.color_by = 'material' + elem = plot.to_xml_element() + + # Read back from XML and make sure it hasn't changed + new_plot = plot.from_xml_element(elem) + assert new_plot.name == plot.name + assert new_plot.filename == plot.filename + assert new_plot.type == plot.type + assert new_plot.pixels == plot.pixels + assert new_plot.origin == plot.origin + assert new_plot.width == plot.width + assert new_plot.color_by == plot.color_by