From 588e7bbab0d1f5fcb07aa6aea63e75ebd671f209 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Wed, 31 Aug 2022 12:37:06 -0500 Subject: [PATCH 1/2] Add XML roundtrip test for openmc.Source --- tests/unit_tests/test_source.py | 43 ++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/tests/unit_tests/test_source.py b/tests/unit_tests/test_source.py index dbcf49efc..76dcbcdc8 100644 --- a/tests/unit_tests/test_source.py +++ b/tests/unit_tests/test_source.py @@ -1,6 +1,10 @@ +from math import pi + import openmc import openmc.stats -from math import pi +import numpy as np +from pytest import approx + def test_source(): space = openmc.stats.Point() @@ -39,8 +43,8 @@ def test_spherical_uniform(): phis, origin) - assert isinstance(sph_indep_function, openmc.stats.SphericalIndependent) - + assert isinstance(sph_indep_function, openmc.stats.SphericalIndependent) + def test_source_file(): filename = 'source.h5' @@ -59,3 +63,36 @@ def test_source_dlopen(): elem = src.to_xml_element() assert 'library' in elem.attrib + + +def test_source_xml_roundtrip(): + # Create a source and write to an XML element + space = openmc.stats.Box([-5., -5., -5.], [5., 5., 5.]) + energy = openmc.stats.Discrete([1.0e6, 2.0e6, 5.0e6], [0.3, 0.5, 0.2]) + angle = openmc.stats.PolarAzimuthal( + mu=openmc.stats.Uniform(0., 1.), + phi=openmc.stats.Uniform(0., 2*pi), + reference_uvw=(0., 1., 0.) + ) + src = openmc.Source( + space=space, angle=angle, energy=energy, + particle='photon', strength=100.0 + ) + elem = src.to_xml_element() + + # Read from XML element and make sure data is preserved + new_src = openmc.Source.from_xml_element(elem) + assert isinstance(new_src.space, openmc.stats.Box) + np.testing.assert_allclose(new_src.space.lower_left, src.space.lower_left) + np.testing.assert_allclose(new_src.space.upper_right, src.space.upper_right) + assert isinstance(new_src.energy, openmc.stats.Discrete) + np.testing.assert_allclose(new_src.energy.x, src.energy.x) + np.testing.assert_allclose(new_src.energy.p, src.energy.p) + assert isinstance(new_src.angle, openmc.stats.PolarAzimuthal) + assert new_src.angle.mu.a == src.angle.mu.a + assert new_src.angle.mu.b == src.angle.mu.b + assert new_src.angle.phi.a == src.angle.phi.a + assert new_src.angle.phi.b == src.angle.phi.b + np.testing.assert_allclose(new_src.angle.reference_uvw, src.angle.reference_uvw) + assert new_src.particle == src.particle + assert new_src.strength == approx(src.strength) From eb1dc015e5026f3083e7e0ad1e313a26cccd1a19 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Tue, 30 Aug 2022 21:15:58 -0500 Subject: [PATCH 2/2] Fix reading of reference_uvw during from_xml --- openmc/stats/multivariate.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/openmc/stats/multivariate.py b/openmc/stats/multivariate.py index f34bf0d1f..8bb319aa4 100644 --- a/openmc/stats/multivariate.py +++ b/openmc/stats/multivariate.py @@ -148,9 +148,9 @@ class PolarAzimuthal(UnitSphere): """ mu_phi = cls() - params = get_text(elem, 'parameters') - if params is not None: - mu_phi.reference_uvw = [float(x) for x in params.split()] + uvw = get_text(elem, 'reference_uvw') + if uvw is not None: + mu_phi.reference_uvw = [float(x) for x in uvw.split()] mu_phi.mu = Univariate.from_xml_element(elem.find('mu')) mu_phi.phi = Univariate.from_xml_element(elem.find('phi')) return mu_phi @@ -242,9 +242,9 @@ class Monodirectional(UnitSphere): """ monodirectional = cls() - params = get_text(elem, 'parameters') - if params is not None: - monodirectional.reference_uvw = [float(x) for x in params.split()] + uvw = get_text(elem, 'reference_uvw') + if uvw is not None: + monodirectional.reference_uvw = [float(x) for x in uvw.split()] return monodirectional