Add tests for XML roundtripping

This commit is contained in:
Paul Romano 2022-01-20 22:21:54 -06:00
parent 5bb4b4bbdf
commit d57c14e06f
3 changed files with 98 additions and 5 deletions

View file

@ -1,5 +1,4 @@
from math import sqrt, pi
import numpy as np
import openmc
from pytest import fixture, approx
@ -38,6 +37,11 @@ def test_cell_instance():
assert all(x == c1.id for x in bins[:6:2])
assert all(x == c2.id for x in bins[6::2])
# from_xml_element()
new_f = openmc.Filter.from_xml_element(elem)
assert new_f.id == f.id
assert np.all(new_f.bins == f.bins)
# get_pandas_dataframe()
df = f.get_pandas_dataframe(f.num_bins, 1)
cells = df['cellinstance', 'cell']
@ -61,6 +65,11 @@ def test_collision():
assert elem.tag == 'filter'
assert elem.attrib['type'] == 'collision'
# from_xml_element()
new_f = openmc.Filter.from_xml_element(elem)
assert new_f.id == f.id
assert np.all(new_f.bins == f.bins)
def test_legendre():
n = 5
@ -79,6 +88,11 @@ def test_legendre():
assert elem.attrib['type'] == 'legendre'
assert elem.find('order').text == str(n)
# from_xml_element()
new_f = openmc.Filter.from_xml_element(elem)
assert new_f.id == f.id
assert new_f.bins, f.bins
def test_spatial_legendre():
n = 5
@ -102,6 +116,12 @@ def test_spatial_legendre():
assert elem.find('order').text == str(n)
assert elem.find('axis').text == str(axis)
# from_xml_element()
new_f = openmc.Filter.from_xml_element(elem)
assert new_f.id == f.id
assert new_f.order == f.order
assert new_f.axis == f.axis
def test_spherical_harmonics():
n = 3
@ -122,6 +142,12 @@ def test_spherical_harmonics():
assert elem.attrib['cosine'] == f.cosine
assert elem.find('order').text == str(n)
# from_xml_element()
new_f = openmc.Filter.from_xml_element(elem)
assert new_f.id == f.id
assert new_f.order == f.order
assert new_f.cosine == f.cosine
def test_zernike():
n = 4
@ -140,6 +166,12 @@ def test_zernike():
assert elem.attrib['type'] == 'zernike'
assert elem.find('order').text == str(n)
# from_xml_element()
new_f = openmc.Filter.from_xml_element(elem)
for attr in ('id', 'order', 'x', 'y', 'r'):
assert getattr(new_f, attr) == getattr(f, attr)
def test_zernike_radial():
n = 4
f = openmc.ZernikeRadialFilter(n, 0., 0., 1.)
@ -157,6 +189,11 @@ def test_zernike_radial():
assert elem.attrib['type'] == 'zernikeradial'
assert elem.find('order').text == str(n)
# from_xml_element()
new_f = openmc.Filter.from_xml_element(elem)
for attr in ('id', 'order', 'x', 'y', 'r'):
assert getattr(new_f, attr) == getattr(f, attr)
def test_first_moment(run_in_tmpdir, box_model):
plain_tally = openmc.Tally()

View file

@ -1,3 +1,4 @@
import numpy as np
import openmc
import openmc.examples
import pytest
@ -12,8 +13,8 @@ def myplot():
plot.filename = 'myplot'
plot.type = 'slice'
plot.basis = 'yz'
plot.background = (0, 0, 0)
plot.background = 'black'
plot.background = (0, 0, 0)
plot.color_by = 'material'
m1, m2 = openmc.Material(), openmc.Material()
@ -21,8 +22,8 @@ def myplot():
plot.colors = {m1: 'green', m2: 'blue'}
plot.mask_components = [openmc.Material()]
plot.mask_background = (255, 255, 255)
plot.mask_background = 'white'
plot.mask_background = (255, 255, 255)
plot.overlap_color = (255, 211, 0)
plot.overlap_color = 'yellow'
@ -70,7 +71,7 @@ def test_highlight_domains():
plots.highlight_domains(model.geometry, mats)
def test_to_xml_element(myplot):
def test_xml_element(myplot):
elem = myplot.to_xml_element()
assert 'id' in elem.attrib
assert 'color_by' in elem.attrib
@ -80,10 +81,19 @@ def test_to_xml_element(myplot):
assert elem.find('pixels') is not None
assert elem.find('background').text == '0 0 0'
newplot = openmc.Plot.from_xml_element(elem)
attributes = ('id', 'color_by', 'filename', 'type', 'basis', 'level',
'meshlines', 'show_overlaps', 'origin', 'width', 'pixels',
'background', 'mask_background')
for attr in attributes:
assert getattr(newplot, attr) == getattr(myplot, attr), attr
def test_plots(run_in_tmpdir):
p1 = openmc.Plot(name='plot1')
p1.origin = (5., 5., 5.)
p2 = openmc.Plot(name='plot2')
p2.origin = (-3., -3., -3.)
plots = openmc.Plots([p1, p2])
assert len(plots) == 2
@ -92,3 +102,9 @@ def test_plots(run_in_tmpdir):
assert len(plots) == 3
plots.export_to_xml()
# from_xml
new_plots = openmc.Plots.from_xml()
assert len(plots)
assert plots[0].origin == p1.origin
assert plots[1].origin == p2.origin

View file

@ -0,0 +1,40 @@
import numpy as np
import openmc
def test_xml_roundtrip(run_in_tmpdir):
# Create a tally with all possible gizmos
mesh = openmc.RegularMesh()
mesh.lower_left = (-10., -10., -10.)
mesh.upper_right = (10., 10., 10.,)
mesh.dimension = (5, 5, 5)
mesh_filter = openmc.MeshFilter(mesh)
tally = openmc.Tally()
tally.filters = [mesh_filter]
tally.nuclides = ['U235', 'I135', 'Li6']
tally.scores = ['total', 'fission', 'heating']
tally.derivative = openmc.TallyDerivative(
variable='nuclide_density', material=1, nuclide='Li6'
)
tally.triggers = [openmc.Trigger('rel_err', 0.025)]
tally.triggers[0].scores = ['total', 'fission']
tallies = openmc.Tallies([tally])
# Roundtrip through XML and make sure we get what we started with
tallies.export_to_xml()
new_tallies = openmc.Tallies.from_xml()
assert len(new_tallies) == 1
new_tally = new_tallies[0]
assert new_tally.id == tally.id
assert len(new_tally.filters) == 1
assert isinstance(new_tally.filters[0], openmc.MeshFilter)
assert np.allclose(new_tally.filters[0].mesh.lower_left, mesh.lower_left)
assert new_tally.nuclides == tally.nuclides
assert new_tally.scores == tally.scores
assert new_tally.derivative.variable == tally.derivative.variable
assert new_tally.derivative.material == tally.derivative.material
assert new_tally.derivative.nuclide == tally.derivative.nuclide
assert len(new_tally.triggers) == 1
assert new_tally.triggers[0].trigger_type == tally.triggers[0].trigger_type
assert new_tally.triggers[0].threshold == tally.triggers[0].threshold
assert new_tally.triggers[0].scores == tally.triggers[0].scores