From 358f64c53ac7dfd1ab11fb3a078bea2b2b2796a4 Mon Sep 17 00:00:00 2001 From: Olaf Schumann Date: Mon, 11 Oct 2021 21:20:30 +0200 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Paul Romano --- docs/source/io_formats/settings.rst | 2 +- include/openmc/distribution.h | 14 ++++++++------ openmc/stats/univariate.py | 17 ++++++++++------- src/distribution.cpp | 4 ++-- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/docs/source/io_formats/settings.rst b/docs/source/io_formats/settings.rst index 313d064a7..82c45f3ee 100644 --- a/docs/source/io_formats/settings.rst +++ b/docs/source/io_formats/settings.rst @@ -641,7 +641,7 @@ variable and whose sub-elements/attributes are as follows: For a "rational" 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)=x^n` + the probability distribution :math:`p(x)=c x^n` For a "discrete" or "tabular" distribution, ``parameters`` provides the :math:`(x,p)` pairs defining the discrete/tabular distribution. All :math:`x` diff --git a/include/openmc/distribution.h b/include/openmc/distribution.h index 48c167810..5d67f2a8a 100644 --- a/include/openmc/distribution.h +++ b/include/openmc/distribution.h @@ -87,7 +87,9 @@ private: class Rational : public Distribution { public: explicit Rational(pugi::xml_node node); - Rational(double a, double b, double n) : offset_{std::pow(a, n+1)}, span_{std::pow(b, n+1) - offset_}, ninv_{1/(n+1)} {}; + Rational(double a, double b, double n) + : offset_ {std::pow(a, n + 1)}, span_ {std::pow(b, n + 1) - offset_}, + ninv_ {1/(n+1)} {}; //! Sample a value from the distribution //! \param seed Pseudorandom number seed pointer @@ -95,12 +97,12 @@ public: double sample(uint64_t* seed) const; double a() const { return std::pow(offset_, ninv_); } - double b() const { return std::pow(offset_+span_, ninv_); } - double n() const { return 1/ninv_-1; } + double b() const { return std::pow(offset_ + span_, ninv_); } + double n() const { return 1 / ninv_ - 1; } private: - double offset_; //!< Lower bound of distribution - double span_; //!< Upper bound of distribution - double ninv_; //!< inverse Exponent of distribution + 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 cfbd0535b..64b94045c 100644 --- a/openmc/stats/univariate.py +++ b/openmc/stats/univariate.py @@ -246,6 +246,8 @@ class Uniform(Univariate): class Rational(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`. Parameters ---------- @@ -254,7 +256,8 @@ class Rational(Univariate): b : float, optional Upper bound of the sampling interval. Defaults to unity. n : float, optional - power law exponent. Defaults to zero, which is eqivalent to an uniform distribution + Power law exponent. Defaults to zero, which is equivalent to a uniform + distribution. Attributes ---------- @@ -289,17 +292,17 @@ class Rational(Univariate): @a.setter def a(self, a): - cv.check_type('Uniform a', a, Real) + cv.check_type('interval lower bound', a, Real) self._a = a @b.setter def b(self, b): - cv.check_type('Uniform b', b, Real) + cv.check_type('interval upper bound', b, Real) self._b = b @n.setter def n(self, n): - cv.check_type('Uniform n', n, Real) + cv.check_type('power law exponent', n, Real) self._n = n def to_xml_element(self, element_name): @@ -313,12 +316,12 @@ class Rational(Univariate): Returns ------- element : xml.etree.ElementTree.Element - XML element containing uniform distribution data + XML element containing distribution data """ element = ET.Element(element_name) element.set("type", "rational") - element.set("parameters", '{} {} {}'.format(self.a, self.b, self.n)) + element.set("parameters", f'{self.a} {self.b} {self.n}') return element @classmethod @@ -333,7 +336,7 @@ class Rational(Univariate): Returns ------- openmc.stats.Rational - Uniform distribution generated from XML element + Distribution generated from XML element """ params = get_text(elem, 'parameters').split() diff --git a/src/distribution.cpp b/src/distribution.cpp index 54bd67d80..5b33d4a7e 100644 --- a/src/distribution.cpp +++ b/src/distribution.cpp @@ -101,9 +101,9 @@ Rational::Rational(pugi::xml_node node) const double b = params.at(1); const double n = params.at(2); - offset_ = std::pow(a, n+1); + offset_ = std::pow(a, n + 1); span_ = std::pow(b, n + 1) - offset_; - ninv_ = 1/(n+1); + ninv_ = 1 / (n + 1); } double Rational::sample(uint64_t* seed) const