1+ from math import pi
2+
13import openmc
24import openmc .stats
3- from math import pi
5+ import numpy as np
6+ from pytest import approx
7+
48
59def test_source ():
610 space = openmc .stats .Point ()
@@ -39,8 +43,8 @@ def test_spherical_uniform():
3943 phis ,
4044 origin )
4145
42- assert isinstance (sph_indep_function , openmc .stats .SphericalIndependent )
43-
46+ assert isinstance (sph_indep_function , openmc .stats .SphericalIndependent )
47+
4448
4549def test_source_file ():
4650 filename = 'source.h5'
@@ -59,3 +63,36 @@ def test_source_dlopen():
5963
6064 elem = src .to_xml_element ()
6165 assert 'library' in elem .attrib
66+
67+
68+ def test_source_xml_roundtrip ():
69+ # Create a source and write to an XML element
70+ space = openmc .stats .Box ([- 5. , - 5. , - 5. ], [5. , 5. , 5. ])
71+ energy = openmc .stats .Discrete ([1.0e6 , 2.0e6 , 5.0e6 ], [0.3 , 0.5 , 0.2 ])
72+ angle = openmc .stats .PolarAzimuthal (
73+ mu = openmc .stats .Uniform (0. , 1. ),
74+ phi = openmc .stats .Uniform (0. , 2 * pi ),
75+ reference_uvw = (0. , 1. , 0. )
76+ )
77+ src = openmc .Source (
78+ space = space , angle = angle , energy = energy ,
79+ particle = 'photon' , strength = 100.0
80+ )
81+ elem = src .to_xml_element ()
82+
83+ # Read from XML element and make sure data is preserved
84+ new_src = openmc .Source .from_xml_element (elem )
85+ assert isinstance (new_src .space , openmc .stats .Box )
86+ np .testing .assert_allclose (new_src .space .lower_left , src .space .lower_left )
87+ np .testing .assert_allclose (new_src .space .upper_right , src .space .upper_right )
88+ assert isinstance (new_src .energy , openmc .stats .Discrete )
89+ np .testing .assert_allclose (new_src .energy .x , src .energy .x )
90+ np .testing .assert_allclose (new_src .energy .p , src .energy .p )
91+ assert isinstance (new_src .angle , openmc .stats .PolarAzimuthal )
92+ assert new_src .angle .mu .a == src .angle .mu .a
93+ assert new_src .angle .mu .b == src .angle .mu .b
94+ assert new_src .angle .phi .a == src .angle .phi .a
95+ assert new_src .angle .phi .b == src .angle .phi .b
96+ np .testing .assert_allclose (new_src .angle .reference_uvw , src .angle .reference_uvw )
97+ assert new_src .particle == src .particle
98+ assert new_src .strength == approx (src .strength )
0 commit comments