diff --git a/docs/source/io_formats/settings.rst b/docs/source/io_formats/settings.rst index c14767e46..3a5e347f9 100644 --- a/docs/source/io_formats/settings.rst +++ b/docs/source/io_formats/settings.rst @@ -423,16 +423,19 @@ attributes/sub-elements: :type: The type of spatial distribution. Valid options are "box", "fission", - "point", "cartesian", and "spherical". A "box" spatial distribution has - coordinates sampled uniformly in a parallelepiped. A "fission" spatial - distribution samples locations from a "box" distribution but only - locations in fissionable materials are accepted. A "point" spatial - distribution has coordinates specified by a triplet. An "cartesian" - spatial distribution specifies independent distributions of x-, y-, and - z-coordinates. A "spherical" spatial distribution specifies independent - distributions of r-, theta-, and phi-coordinates where theta is the angle - with respect to the z-axis, phi is the azimuthal angle, and the sphere is - centered on the coordinate (x0,y0,z0). + "point", "cartesian", "cylindrical", and "spherical". A "box" spatial + distribution has coordinates sampled uniformly in a parallelepiped. A + "fission" spatial distribution samples locations from a "box" + distribution but only locations in fissionable materials are accepted. + A "point" spatial distribution has coordinates specified by a triplet. + A "cartesian" spatial distribution specifies independent distributions of + x-, y-, and z-coordinates. A "cylindrical" spatial distribution specifies + independent distributions of r-, phi-, and z-coordinates where phi is the + azimuthal angle and the origin for the cylindrical coordinate system is + specified by origin. A "spherical" spatial distribution specifies + independent distributions of r-, theta-, and phi-coordinates where theta + is the angle with respect to the z-axis, phi is the azimuthal angle, and + the sphere is centered on the coordinate (x0,y0,z0). *Default*: None @@ -449,6 +452,9 @@ attributes/sub-elements: For an "cartesian" distribution, no parameters are specified. Instead, the ``x``, ``y``, and ``z`` elements must be specified. + + For a "cylindrical" distribution, no parameters are specified. Instead, + the ``r``, ``phi``, ``z``, and ``origin`` elements must be specified. For a "spherical" distribution, no parameters are specified. Instead, the ``r``, ``theta``, ``phi``, and ``origin`` elements must be specified. @@ -468,15 +474,16 @@ attributes/sub-elements: :ref:`univariate`). :z: - For an "cartesian" distribution, this element specifies the distribution - of z-coordinates. The necessary sub-elements/attributes are those of a - univariate probability distribution (see the description in - :ref:`univariate`). + For both "cartesian" and "cylindrical" distributions, this element + specifies the distribution of z-coordinates. The necessary + sub-elements/attributes are those of a univariate probability + distribution (see the description in :ref:`univariate`). :r: - For a "spherical" distribution, this element specifies the distribution - of r-coordinates. The necessary sub-elements/attributes are those of a - univariate probability distribution (see the description in + For "cylindrical" and "spherical" distributions, this element specifies + the distribution of r-coordinates (cylindrical radius and spherical + radius, respectively). The necessary sub-elements/attributes are those + of a univariate probability distribution (see the description in :ref:`univariate`). :theta: @@ -486,14 +493,14 @@ attributes/sub-elements: :ref:`univariate`). :phi: - For a "spherical" distribution, this element specifies the distribution - of phi-coordinates. The necessary sub-elements/attributes are those of a - univariate probability distribution (see the description in - :ref:`univariate`). + For "cylindrical" and "spherical" distributions, this element specifies + the distribution of phi-coordinates. The necessary + sub-elements/attributes are those of a univariate probability + distribution (see the description in :ref:`univariate`). :origin: - For a "spherical" distribution, this element specifies the coordinates of - the center of the sphere. + For "cylindrical and "spherical" distributions, this element specifies + the coordinates for the origin of the coordinate system. :angle: An element specifying the angular distribution of source sites. This element diff --git a/docs/source/pythonapi/stats.rst b/docs/source/pythonapi/stats.rst index cacbab31b..d14aff558 100644 --- a/docs/source/pythonapi/stats.rst +++ b/docs/source/pythonapi/stats.rst @@ -46,6 +46,7 @@ Spatial Distributions openmc.stats.Spatial openmc.stats.CartesianIndependent + openmc.stats.CylindricalIndependent openmc.stats.SphericalIndependent openmc.stats.Box openmc.stats.Point diff --git a/include/openmc/distribution_spatial.h b/include/openmc/distribution_spatial.h index 53857fac3..f3c7bb67d 100644 --- a/include/openmc/distribution_spatial.h +++ b/include/openmc/distribution_spatial.h @@ -38,6 +38,26 @@ private: UPtrDist z_; //!< Distribution of z coordinates }; +//============================================================================== +//! Distribution of points specified by cylindrical coordinates r,phi,z +//============================================================================== + +class CylindricalIndependent : public SpatialDistribution { +public: + explicit CylindricalIndependent(pugi::xml_node node); + + //! Sample a position from the distribution + //! \param seed Pseudorandom number seed pointer + //! \return Sampled position + Position sample(uint64_t* seed) const; +private: + UPtrDist r_; //!< Distribution of r coordinates + UPtrDist phi_; //!< Distribution of phi coordinates + UPtrDist z_; //!< Distribution of z coordinates + Position origin_; //!< Cartesian coordinates of the cylinder center +}; + + //============================================================================== //! Distribution of points specified by spherical coordinates r,theta,phi //============================================================================== diff --git a/openmc/stats/multivariate.py b/openmc/stats/multivariate.py index 6a97e6192..3d258ee35 100644 --- a/openmc/stats/multivariate.py +++ b/openmc/stats/multivariate.py @@ -272,6 +272,8 @@ class Spatial(metaclass=ABCMeta): distribution = get_text(elem, 'type') if distribution == 'cartesian': return CartesianIndependent.from_xml_element(elem) + elif distribution == 'cylindrical': + return CylindricalIndependent.from_xml_element(elem) elif distribution == 'spherical': return SphericalIndependent.from_xml_element(elem) elif distribution == 'box' or distribution == 'fission': @@ -377,7 +379,7 @@ class CartesianIndependent(Spatial): class SphericalIndependent(Spatial): - """Spatial distribution represented in spherical coordinates. + r"""Spatial distribution represented in spherical coordinates. This distribution allows one to specify coordinates whose :math:`r`, :math:`\theta`, and :math:`\phi` components are sampled independently from @@ -386,26 +388,31 @@ class SphericalIndependent(Spatial): Parameters ---------- r : openmc.stats.Univariate - Distribution of r-coordinates + Distribution of r-coordinates in a reference frame specified by + the origin parameter theta : openmc.stats.Univariate - Distribution of theta-coordinates (angle relative to the z-axis) + Distribution of theta-coordinates (angle relative to the z-axis) in a + reference frame specified by the origin parameter phi : openmc.stats.Univariate - Distribution of phi-coordinates (azimuthal angle) + Distribution of phi-coordinates (azimuthal angle) in a reference frame + specified by the origin parameter origin: Iterable of float, optional - coordinates (x0, y0, z0) of the center of the sphere. Defaults to - (0.0, 0.0, 0.0) + coordinates (x0, y0, z0) of the center of the spherical reference frame + for the source. Defaults to (0.0, 0.0, 0.0) Attributes ---------- r : openmc.stats.Univariate - Distribution of r-coordinates + Distribution of r-coordinates in the local reference frame theta : openmc.stats.Univariate - Distribution of theta-coordinates (angle relative to the z-axis) + Distribution of theta-coordinates (angle relative to the z-axis) in the + local reference frame phi : openmc.stats.Univariate - Distribution of phi-coordinates (azimuthal angle) + Distribution of phi-coordinates (azimuthal angle) in the local + reference frame origin: Iterable of float, optional - coordinates (x0, y0, z0) of the center of the sphere. Defaults to - (0.0, 0.0, 0.0) + coordinates (x0, y0, z0) of the center of the spherical reference + frame. Defaults to (0.0, 0.0, 0.0) """ @@ -491,6 +498,126 @@ class SphericalIndependent(Spatial): origin = [float(x) for x in elem.get('origin').split()] return cls(r, theta, phi, origin=origin) +class CylindricalIndependent(Spatial): + """Spatial distribution represented in cylindrical coordinates. + + This distribution allows one to specify coordinates whose :math:`r`, + :math:`\phi`, and :math:`z` components are sampled independently from + one another and in a reference frame whose origin is specified by the + coordinates (x0, y0, z0). + + Parameters + ---------- + r : openmc.stats.Univariate + Distribution of r-coordinates in a reference frame specified by the + origin parameter + phi : openmc.stats.Univariate + Distribution of phi-coordinates (azimuthal angle) in a reference frame + specified by the origin parameter + z : openmc.stats.Univariate + Distribution of z-coordinates in a reference frame specified by the + origin parameter + origin: Iterable of float, optional + coordinates (x0, y0, z0) of the center of the cylindrical reference + frame. Defaults to (0.0, 0.0, 0.0) + + Attributes + ---------- + r : openmc.stats.Univariate + Distribution of r-coordinates in the local reference frame + phi : openmc.stats.Univariate + Distribution of phi-coordinates (azimuthal angle) in the local + reference frame + z : openmc.stats.Univariate + Distribution of z-coordinates in the local reference frame + origin: Iterable of float, optional + coordinates (x0, y0, z0) of the center of the cylindrical reference + frame. Defaults to (0.0, 0.0, 0.0) + + """ + + def __init__(self, r, phi, z, origin=(0.0, 0.0, 0.0)): + super().__init__() + self.r = r + self.phi = phi + self.z = z + self.origin = origin + + @property + def r(self): + return self._r + + @property + def phi(self): + return self._phi + + @property + def z(self): + return self._z + + @property + def origin(self): + return self._origin + + @r.setter + def r(self, r): + cv.check_type('r coordinate', r, Univariate) + self._r = r + + @phi.setter + def phi(self, phi): + cv.check_type('phi coordinate', phi, Univariate) + self._phi = phi + + @z.setter + def z(self, z): + cv.check_type('z coordinate', z, Univariate) + self._z = z + + @origin.setter + def origin(self, origin): + cv.check_type('origin coordinates', origin, Iterable, Real) + origin = np.asarray(origin) + self._origin = origin + + def to_xml_element(self): + """Return XML representation of the spatial distribution + + Returns + ------- + element : xml.etree.ElementTree.Element + XML element containing spatial distribution data + + """ + element = ET.Element('space') + element.set('type', 'cylindrical') + element.append(self.r.to_xml_element('r')) + element.append(self.phi.to_xml_element('phi')) + element.append(self.z.to_xml_element('z')) + element.set("origin", ' '.join(map(str, self.origin))) + return element + + @classmethod + def from_xml_element(cls, elem): + """Generate spatial distribution from an XML element + + Parameters + ---------- + elem : xml.etree.ElementTree.Element + XML element + + Returns + ------- + openmc.stats.CylindricalIndependent + Spatial distribution generated from XML element + + """ + r = Univariate.from_xml_element(elem.find('r')) + phi = Univariate.from_xml_element(elem.find('phi')) + z = Univariate.from_xml_element(elem.find('z')) + origin = [float(x) for x in elem.get('origin').split()] + return cls(r, phi, z, origin=origin) + class Box(Spatial): """Uniform distribution of coordinates in a rectangular cuboid. diff --git a/src/distribution_spatial.cpp b/src/distribution_spatial.cpp index a5c45e4e9..12432baf7 100644 --- a/src/distribution_spatial.cpp +++ b/src/distribution_spatial.cpp @@ -51,6 +51,72 @@ Position CartesianIndependent::sample(uint64_t* seed) const return {x_->sample(seed), y_->sample(seed), z_->sample(seed)}; } +//============================================================================== +// CylindricalIndependent implementation +//============================================================================== + +CylindricalIndependent::CylindricalIndependent(pugi::xml_node node) +{ + // Read distribution for r-coordinate + if (check_for_node(node, "r")) { + pugi::xml_node node_dist = node.child("r"); + r_ = distribution_from_xml(node_dist); + } else { + // If no distribution was specified, default to a single point at r=0 + double x[] {0.0}; + double p[] {1.0}; + r_ = std::make_unique(x, p, 1); + } + + // Read distribution for phi-coordinate + if (check_for_node(node, "phi")) { + pugi::xml_node node_dist = node.child("phi"); + phi_ = distribution_from_xml(node_dist); + } else { + // If no distribution was specified, default to a single point at phi=0 + double x[] {0.0}; + double p[] {1.0}; + phi_ = std::make_unique(x, p, 1); + } + + // Read distribution for z-coordinate + if (check_for_node(node, "z")) { + pugi::xml_node node_dist = node.child("z"); + z_ = distribution_from_xml(node_dist); + } else { + // If no distribution was specified, default to a single point at z=0 + double x[] {0.0}; + double p[] {1.0}; + z_ = std::make_unique(x, p, 1); + } + + // Read cylinder center coordinates + if (check_for_node(node, "origin")) { + auto origin = get_node_array(node, "origin"); + if (origin.size() == 3) { + origin_ = origin; + } else { + std::stringstream err_msg; + err_msg << "Origin for cylindrical source distribution must be length 3"; + fatal_error(err_msg); + } + } else { + // If no coordinates were specified, default to (0, 0, 0) + origin_ = {0.0, 0.0, 0.0}; + } + +} + +Position CylindricalIndependent::sample(uint64_t* seed) const +{ + double r = r_->sample(seed); + double phi = phi_->sample(seed); + double x = r*cos(phi) + origin_.x; + double y = r*sin(phi) + origin_.y; + double z = z_->sample(seed) + origin_.z; + return {x, y, z}; +} + //============================================================================== // SphericalIndependent implementation //============================================================================== diff --git a/src/source.cpp b/src/source.cpp index 628631fdb..d2e13a173 100644 --- a/src/source.cpp +++ b/src/source.cpp @@ -86,6 +86,8 @@ SourceDistribution::SourceDistribution(pugi::xml_node node) type = get_node_value(node_space, "type", true, true); if (type == "cartesian") { space_ = UPtrSpace{new CartesianIndependent(node_space)}; + } else if (type == "cylindrical") { + space_ = UPtrSpace{new CylindricalIndependent(node_space)}; } else if (type == "spherical") { space_ = UPtrSpace{new SphericalIndependent(node_space)}; } else if (type == "box") { diff --git a/tests/regression_tests/source/inputs_true.dat b/tests/regression_tests/source/inputs_true.dat index 6f05eb3a7..de81a4179 100644 --- a/tests/regression_tests/source/inputs_true.dat +++ b/tests/regression_tests/source/inputs_true.dat @@ -63,4 +63,17 @@ 1.0 1.3894954943731377 1.93069772888325 2.6826957952797255 3.72759372031494 5.17947467923121 7.196856730011519 10.0 13.894954943731374 19.306977288832496 26.826957952797247 37.2759372031494 51.7947467923121 71.96856730011518 100.0 138.94954943731375 193.06977288832496 268.26957952797244 372.7593720314938 517.9474679231207 719.6856730011514 1000.0 1389.4954943731375 1930.6977288832495 2682.6957952797247 3727.593720314938 5179.474679231207 7196.856730011514 10000.0 13894.95494373136 19306.977288832495 26826.95795279722 37275.93720314938 51794.74679231213 71968.56730011514 100000.0 138949.5494373136 193069.77288832495 268269.5795279722 372759.3720314938 517947.4679231202 719685.6730011514 1000000.0 1389495.494373136 1930697.7288832497 2682695.7952797217 3727593.720314938 5179474.679231202 7196856.730011513 10000000.0 0.0 2.9086439299358713e-08 5.80533561806147e-08 8.67817193689187e-08 1.1515347785771536e-07 1.4305204600565115e-07 1.7036278261198208e-07 1.9697346200185813e-07 2.227747351856934e-07 2.4766057919761985e-07 2.715287327665956e-07 2.9428111652990295e-07 3.1582423606228735e-07 3.360695660646056e-07 3.549339141332686e-07 3.723397626156721e-07 3.882155871468592e-07 4.024961505584776e-07 4.151227709522976e-07 4.260435628367196e-07 4.3521365033538783e-07 4.4259535159179273e-07 4.4815833361210174e-07 4.5187973690993757e-07 4.5374426944091084e-07 4.5374426944091084e-07 4.5187973690993757e-07 4.4815833361210174e-07 4.4259535159179273e-07 4.352136503353879e-07 4.2604356283671966e-07 4.1512277095229767e-07 4.0249615055847764e-07 3.8821558714685926e-07 3.723397626156722e-07 3.5493391413326864e-07 3.360695660646057e-07 3.158242360622874e-07 2.942811165299031e-07 2.715287327665957e-07 2.4766057919762e-07 2.2277473518569352e-07 1.9697346200185819e-07 1.7036278261198226e-07 1.4305204600565126e-07 1.1515347785771556e-07 8.678171936891881e-08 5.805335618061493e-08 2.9086439299358858e-08 5.559621115282002e-23 + + + + + + -2.0 0.0 2.0 0.2 0.3 0.2 + + + + + 1.0 1.3894954943731377 1.93069772888325 2.6826957952797255 3.72759372031494 5.17947467923121 7.196856730011519 10.0 13.894954943731374 19.306977288832496 26.826957952797247 37.2759372031494 51.7947467923121 71.96856730011518 100.0 138.94954943731375 193.06977288832496 268.26957952797244 372.7593720314938 517.9474679231207 719.6856730011514 1000.0 1389.4954943731375 1930.6977288832495 2682.6957952797247 3727.593720314938 5179.474679231207 7196.856730011514 10000.0 13894.95494373136 19306.977288832495 26826.95795279722 37275.93720314938 51794.74679231213 71968.56730011514 100000.0 138949.5494373136 193069.77288832495 268269.5795279722 372759.3720314938 517947.4679231202 719685.6730011514 1000000.0 1389495.494373136 1930697.7288832497 2682695.7952797217 3727593.720314938 5179474.679231202 7196856.730011513 10000000.0 0.0 2.9086439299358713e-08 5.80533561806147e-08 8.67817193689187e-08 1.1515347785771536e-07 1.4305204600565115e-07 1.7036278261198208e-07 1.9697346200185813e-07 2.227747351856934e-07 2.4766057919761985e-07 2.715287327665956e-07 2.9428111652990295e-07 3.1582423606228735e-07 3.360695660646056e-07 3.549339141332686e-07 3.723397626156721e-07 3.882155871468592e-07 4.024961505584776e-07 4.151227709522976e-07 4.260435628367196e-07 4.3521365033538783e-07 4.4259535159179273e-07 4.4815833361210174e-07 4.5187973690993757e-07 4.5374426944091084e-07 4.5374426944091084e-07 4.5187973690993757e-07 4.4815833361210174e-07 4.4259535159179273e-07 4.352136503353879e-07 4.2604356283671966e-07 4.1512277095229767e-07 4.0249615055847764e-07 3.8821558714685926e-07 3.723397626156722e-07 3.5493391413326864e-07 3.360695660646057e-07 3.158242360622874e-07 2.942811165299031e-07 2.715287327665957e-07 2.4766057919762e-07 2.2277473518569352e-07 1.9697346200185819e-07 1.7036278261198226e-07 1.4305204600565126e-07 1.1515347785771556e-07 8.678171936891881e-08 5.805335618061493e-08 2.9086439299358858e-08 5.559621115282002e-23 + + diff --git a/tests/regression_tests/source/results_true.dat b/tests/regression_tests/source/results_true.dat index 8f24644f0..eda1a6597 100644 --- a/tests/regression_tests/source/results_true.dat +++ b/tests/regression_tests/source/results_true.dat @@ -1,2 +1,2 @@ k-combined: -3.004769E-01 3.944044E-03 +2.999424E-01 9.520402E-03 diff --git a/tests/regression_tests/source/test.py b/tests/regression_tests/source/test.py index 5fd06286b..91316acc6 100644 --- a/tests/regression_tests/source/test.py +++ b/tests/regression_tests/source/test.py @@ -38,7 +38,10 @@ class SourceTestHarness(PyAPITestHarness): spatial3 = openmc.stats.Point([1.2, -2.3, 0.781]) spatial4 = openmc.stats.SphericalIndependent(r_dist, theta_dist, phi_dist, - origin=(1.0, 1.0, 0.0)) + origin=(1., 1., 0.)) + spatial5 = openmc.stats.CylindricalIndependent(r_dist, phi_dist, + z_dist, + origin=(1., 1., 0.)) mu_dist = openmc.stats.Discrete([-1., 0., 1.], [0.5, 0.25, 0.25]) phi_dist = openmc.stats.Uniform(0., 6.28318530718) @@ -57,12 +60,13 @@ class SourceTestHarness(PyAPITestHarness): source2 = openmc.Source(spatial2, angle2, energy2, strength=0.3) source3 = openmc.Source(spatial3, angle3, energy3, strength=0.1) source4 = openmc.Source(spatial4, angle3, energy3, strength=0.1) + source5 = openmc.Source(spatial5, angle3, energy3, strength=0.1) settings = openmc.Settings() settings.batches = 10 settings.inactive = 5 settings.particles = 1000 - settings.source = [source1, source2, source3, source4] + settings.source = [source1, source2, source3, source4, source5] settings.export_to_xml()