Skip to content

Commit

Permalink
Adding XML roundtrip test for PhongPlot
Browse files Browse the repository at this point in the history
  • Loading branch information
pshriwise committed Jan 2, 2025
1 parent c24e37a commit 61fdaed
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
24 changes: 23 additions & 1 deletion openmc/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
33 changes: 33 additions & 0 deletions tests/unit_tests/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 61fdaed

Please sign in to comment.