diff --git a/docs/source/io_formats/settings.rst b/docs/source/io_formats/settings.rst index 82c45f3ee..ac2fda458 100644 --- a/docs/source/io_formats/settings.rst +++ b/docs/source/io_formats/settings.rst @@ -638,7 +638,7 @@ variable and whose sub-elements/attributes are as follows: numbers :math:`a` and :math:`b` that define the interval :math:`[a,b]` over which random variates are sampled. - For a "rational" distribution, ``parameters`` should be given as three real + For a "power law" distribution, ``parameters`` should be given as three real numbers :math:`a` and :math:`b` that define the interval :math:`[a,b]` over which random variates are sampled and :math:`n` that defines the exponent of the probability distribution :math:`p(x)=c x^n` diff --git a/docs/source/pythonapi/stats.rst b/docs/source/pythonapi/stats.rst index 55f18f45d..1d4ef0302 100644 --- a/docs/source/pythonapi/stats.rst +++ b/docs/source/pythonapi/stats.rst @@ -15,7 +15,7 @@ Univariate Probability Distributions openmc.stats.Univariate openmc.stats.Discrete openmc.stats.Uniform - openmc.stats.Rational + openmc.stats.PowerLaw openmc.stats.Maxwell openmc.stats.Watt openmc.stats.Tabular diff --git a/include/openmc/distribution.h b/include/openmc/distribution.h index 5d67f2a8a..8403f2c67 100644 --- a/include/openmc/distribution.h +++ b/include/openmc/distribution.h @@ -81,15 +81,15 @@ private: }; //============================================================================== -//! Rational distribution over the interval [a,b] with exponent n +//! PowerLaw distribution over the interval [a,b] with exponent n : p(x)=c x^n //============================================================================== -class Rational : public Distribution { +class PowerLaw : public Distribution { public: - explicit Rational(pugi::xml_node node); - Rational(double a, double b, double n) + explicit PowerLaw(pugi::xml_node node); + PowerLaw(double a, double b, double n) : offset_ {std::pow(a, n + 1)}, span_ {std::pow(b, n + 1) - offset_}, - ninv_ {1/(n+1)} {}; + ninv_ {1 / (n + 1)} {}; //! Sample a value from the distribution //! \param seed Pseudorandom number seed pointer @@ -100,6 +100,7 @@ public: double b() const { return std::pow(offset_ + span_, ninv_); } double n() const { return 1 / ninv_ - 1; } private: + //! Store processed values in object to allow for faster sampling double offset_; //!< a^(n+1) double span_; //!< b^(n+1) - a^(n+1) double ninv_; //!< 1/(n+1) diff --git a/openmc/stats/univariate.py b/openmc/stats/univariate.py index 64b94045c..8d10104fd 100644 --- a/openmc/stats/univariate.py +++ b/openmc/stats/univariate.py @@ -42,8 +42,8 @@ class Univariate(EqualityMixin, ABC): return Discrete.from_xml_element(elem) elif distribution == 'uniform': return Uniform.from_xml_element(elem) - elif distribution == 'rational': - return Rational.from_xml_element(elem) + elif distribution == 'powerlaw': + return PowerLaw.from_xml_element(elem) elif distribution == 'maxwell': return Maxwell.from_xml_element(elem) elif distribution == 'watt': @@ -244,7 +244,7 @@ class Uniform(Univariate): params = get_text(elem, 'parameters').split() return cls(*map(float, params)) -class Rational(Univariate): +class PowerLaw(Univariate): """Distribution with power law probability over a finite interval [a,b] The power law distribution has density function :math:`p(x) dx = c x^n dx`. @@ -306,7 +306,7 @@ class Rational(Univariate): self._n = n def to_xml_element(self, element_name): - """Return XML representation of the rational distribution + """Return XML representation of the power law distribution Parameters ---------- @@ -320,13 +320,13 @@ class Rational(Univariate): """ element = ET.Element(element_name) - element.set("type", "rational") + element.set("type", "powerlaw") element.set("parameters", f'{self.a} {self.b} {self.n}') return element @classmethod def from_xml_element(cls, elem): - """Generate rational distribution from an XML element + """Generate power law distribution from an XML element Parameters ---------- @@ -335,7 +335,7 @@ class Rational(Univariate): Returns ------- - openmc.stats.Rational + openmc.stats.PowerLaw Distribution generated from XML element """ diff --git a/src/distribution.cpp b/src/distribution.cpp index 5b33d4a7e..901852e0f 100644 --- a/src/distribution.cpp +++ b/src/distribution.cpp @@ -86,14 +86,14 @@ double Uniform::sample(uint64_t* seed) const } //============================================================================== -// Rational implementation +// PowerLaw implementation //============================================================================== -Rational::Rational(pugi::xml_node node) +PowerLaw::PowerLaw(pugi::xml_node node) { auto params = get_node_array(node, "parameters"); if (params.size() != 3) { - fatal_error("Rational distribution must have three " + fatal_error("PowerLaw distribution must have three " "parameters specified."); } @@ -106,7 +106,7 @@ Rational::Rational(pugi::xml_node node) ninv_ = 1 / (n + 1); } -double Rational::sample(uint64_t* seed) const +double PowerLaw::sample(uint64_t* seed) const { return std::pow(offset_ + prn(seed) * span_, ninv_); } @@ -373,8 +373,8 @@ UPtrDist distribution_from_xml(pugi::xml_node node) UPtrDist dist; if (type == "uniform") { dist = UPtrDist {new Uniform(node)}; - } else if (type == "rational") { - dist = UPtrDist{new Rational(node)}; + } else if (type == "powerlaw") { + dist = UPtrDist {new PowerLaw(node)}; } else if (type == "maxwell") { dist = UPtrDist {new Maxwell(node)}; } else if (type == "watt") { diff --git a/tests/regression_tests/source/inputs_true.dat b/tests/regression_tests/source/inputs_true.dat index 7dac4a535..0568b6543 100644 --- a/tests/regression_tests/source/inputs_true.dat +++ b/tests/regression_tests/source/inputs_true.dat @@ -101,7 +101,7 @@ - + 0.7853981633974483 1.5707963267948966 2.356194490192345 0.3 0.4 0.3 @@ -124,7 +124,7 @@ - + -2.0 0.0 2.0 0.2 0.3 0.2 diff --git a/tests/regression_tests/source/test.py b/tests/regression_tests/source/test.py index 3a194f577..41cd25856 100644 --- a/tests/regression_tests/source/test.py +++ b/tests/regression_tests/source/test.py @@ -30,8 +30,8 @@ class SourceTestHarness(PyAPITestHarness): y_dist = openmc.stats.Discrete([-4., -1., 3.], [0.2, 0.3, 0.5]) z_dist = openmc.stats.Tabular([-2., 0., 2.], [0.2, 0.3, 0.2]) r_dist = openmc.stats.Uniform(2., 3.) - r_dist1 = openmc.stats.Rational(2., 3., 1.) - r_dist2 = openmc.stats.Rational(2., 3., 2.) + r_dist1 = openmc.stats.PowerLaw(2., 3., 1.) + r_dist2 = openmc.stats.PowerLaw(2., 3., 2.) theta_dist = openmc.stats.Discrete([pi/4, pi/2, 3*pi/4], [0.3, 0.4, 0.3]) phi_dist = openmc.stats.Uniform(0.0, 2*pi) diff --git a/tests/unit_tests/test_stats.py b/tests/unit_tests/test_stats.py index f741a9e5b..bbcb12ff1 100644 --- a/tests/unit_tests/test_stats.py +++ b/tests/unit_tests/test_stats.py @@ -42,12 +42,12 @@ def test_uniform(): assert t.p == [1/(b-a), 1/(b-a)] assert t.interpolation == 'histogram' -def test_rational(): +def test_powerlaw(): a, b, n = 10.0, 20.0, 2.0 - d = openmc.stats.Rational(a, b, n) + d = openmc.stats.PowerLaw(a, b, n) elem = d.to_xml_element('distribution') - d = openmc.stats.Rational.from_xml_element(elem) + d = openmc.stats.PowerLaw.from_xml_element(elem) assert d.a == a assert d.b == b assert d.n == n