From 61fdaed53c07da46e25cec5c426a0f7f1d4cfef8 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Thu, 2 Jan 2025 13:58:01 -0600 Subject: [PATCH] Adding XML roundtrip test for PhongPlot --- openmc/plots.py | 24 +++++++++++++++++++++++- tests/unit_tests/test_plots.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/openmc/plots.py b/openmc/plots.py index 1f492b4a386..ab64b109602 100644 --- a/openmc/plots.py +++ b/openmc/plots.py @@ -1478,6 +1478,24 @@ def to_xml_element(self): return element + def _read_phong_attributes(self, elem): + """Read attributes specific to the Phong plot + from an XML element + Returns + ------- + None + """ + if elem.find('light_position') is not None: + self.light_position = get_elem_tuple(elem, 'light_position', float) + + diffuse_fraction = elem.find('diffuse_fraction') + if diffuse_fraction is not None: + self.diffuse_fraction = float(diffuse_fraction.text) + + if elem.find('opaque_ids') is not None: + self.opaque_domains = list(get_elem_tuple(elem, 'opaque_ids', int)) + + @classmethod def from_xml_element(cls, elem): """Generate plot object from an XML element @@ -1495,16 +1513,20 @@ def from_xml_element(cls, elem): """ plot_id = int(elem.get("id")) - plot = cls(plot_id) + plot_name = get_text(elem, 'name', '') + plot = cls(plot_id, plot_name) plot.type = "phong" plot._read_xml_attributes(elem) + plot._read_phong_attributes(elem) # Set plot colors for color_elem in elem.findall("color"): uid = color_elem.get("id") plot.colors[uid] = get_elem_tuple(color_elem, "rgb") + return plot + class Plots(cv.CheckedList): """Collection of Plots used for an OpenMC simulation. diff --git a/tests/unit_tests/test_plots.py b/tests/unit_tests/test_plots.py index a1e15016a2e..50c1dc1112d 100644 --- a/tests/unit_tests/test_plots.py +++ b/tests/unit_tests/test_plots.py @@ -223,6 +223,39 @@ def test_voxel_plot_roundtrip(): assert new_plot.color_by == plot.color_by +def test_phone_plot_roundtrip(): + plot = openmc.PhongPlot(name='my phong plot') + plot.id = 2300 + plot.filename = 'phong1' + plot.pixels = (50, 50) + plot.look_at = (11., 12., 13.) + plot.camera_position = (22., 23., 24.) + plot.diffuse_fraction = 0.5 + plot.horizontal_field_of_view = 90.0 + plot.color_by = 'material' + plot.light_position = (8., 9., 10.) + plot.opaque_domains = [6, 7, 8] + + elem = plot.to_xml_element() + + new_plot = openmc.PhongPlot.from_xml_element(elem) + + assert new_plot.name == plot.name + assert new_plot.id == plot.id + assert new_plot.filename == plot.filename + assert new_plot.pixels == plot.pixels + assert new_plot.look_at == plot.look_at + assert new_plot.camera_position == plot.camera_position + assert new_plot.diffuse_fraction == plot.diffuse_fraction + assert new_plot.horizontal_field_of_view == plot.horizontal_field_of_view + assert new_plot.color_by == plot.color_by + assert new_plot.light_position == plot.light_position + assert new_plot.opaque_domains == plot.opaque_domains + + # ensure the new object is valid to re-write to XML + new_elem = new_plot.to_xml_element() + + def test_plot_directory(run_in_tmpdir): pwr_pin = openmc.examples.pwr_pin_cell()