From 041e3e7cbb5bf74ebb60d319cbc5a9f07111b3bd Mon Sep 17 00:00:00 2001 From: Zhuoran Han Date: Thu, 23 Aug 2018 11:01:37 -0500 Subject: [PATCH 1/4] Check hash value for equality --- openmc/filter_expansion.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/openmc/filter_expansion.py b/openmc/filter_expansion.py index 7debebd6b9..67a9117a9a 100644 --- a/openmc/filter_expansion.py +++ b/openmc/filter_expansion.py @@ -19,7 +19,7 @@ class ExpansionFilter(Filter): if type(self) is not type(other): return False else: - return self.bins == other.bins + return hash(self) == hash(other) @property def order(self): @@ -390,6 +390,9 @@ class ZernikeFilter(ExpansionFilter): def __hash__(self): string = type(self).__name__ + '\n' string += '{: <16}=\t{}\n'.format('\tOrder', self.order) + string += '{: <16}=\t{}\n'.format('\tX', self.x) + string += '{: <16}=\t{}\n'.format('\tY', self.y) + string += '{: <16}=\t{}\n'.format('\tR', self.r) return hash(string) def __repr__(self): From be075618d995d1b2cbd643cba5c7e7f4e85b23e6 Mon Sep 17 00:00:00 2001 From: Zhuoran Han Date: Thu, 23 Aug 2018 11:50:12 -0500 Subject: [PATCH 2/4] Fix code style --- openmc/filter_expansion.py | 8 ++++---- openmc/polynomial.py | 10 +++++++--- tests/unit_tests/test_polynomials.py | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/openmc/filter_expansion.py b/openmc/filter_expansion.py index 67a9117a9a..59457b6857 100644 --- a/openmc/filter_expansion.py +++ b/openmc/filter_expansion.py @@ -325,8 +325,8 @@ class ZernikeFilter(ExpansionFilter): This filter allows scores to be multiplied by Zernike polynomials of the particle's position normalized to a given unit circle, up to a - user-specified order. The standard Zernike polynomials follow the definition by - Born and Wolf, *Principles of Optics* and are defined as + user-specified order. The standard Zernike polynomials follow the + definition by Born and Wolf, *Principles of Optics* and are defined as .. math:: Z_n^m(\rho, \theta) = R_n^m(\rho) \cos (m\theta), \quad m > 0 @@ -342,7 +342,7 @@ class ZernikeFilter(ExpansionFilter): \frac{n+m}{2} - k)! (\frac{n-m}{2} - k)!} \rho^{n-2k}. With this definition, the integral of :math:`(Z_n^m)^2` over the unit disk - is :math:`\frac{\epsilon_m\pi}{2n+2}` for each polynomial where + is :math:`\frac{\epsilon_m\pi}{2n+2}` for each polynomial where :math:`\epsilon_m` is 2 if :math:`m` equals 0 and 1 otherwise. Specifying a filter with order N tallies moments for all :math:`n` from 0 @@ -488,7 +488,7 @@ class ZernikeRadialFilter(ZernikeFilter): is :math:`\frac{\pi}{n+1}`. If there is only radial dependency, the polynomials are integrated over - the azimuthal angles. The only terms left are :math:`Z_n^{0}(\rho, \theta) + the azimuthal angles. The only terms left are :math:`Z_n^{0}(\rho, \theta) = R_n^{0}(\rho)`. Note that :math:`n` could only be even orders. Therefore, for a radial Zernike polynomials up to order of :math:`n`, there are :math:`\frac{n}{2} + 1` terms in total. The indexing is from the diff --git a/openmc/polynomial.py b/openmc/polynomial.py index b120659976..3552819f6b 100644 --- a/openmc/polynomial.py +++ b/openmc/polynomial.py @@ -1,9 +1,10 @@ import numpy as np import openmc import openmc.capi as capi +from collections import Iterable -def legendre_from_expcoef(coef, domain= (-1,1)): +def legendre_from_expcoef(coef, domain=(-1, 1)): """Return a Legendre series object based on expansion coefficients. Given a list of coefficients from FET tally and a array of down, return @@ -73,5 +74,8 @@ class ZernikeRadial(Polynomial): return self._order def __call__(self, r): - zn_rad = capi.calc_zn_rad(self.order, r) - return np.sum(self._norm_coef * zn_rad) + if isinstance(r, Iterable): + return [np.sum(self._norm_coef * capi.calc_zn_rad(self.order, r_i)) + for r_i in r] + else: + return np.sum(self._norm_coef * capi.calc_zn_rad(self.order, r)) diff --git a/tests/unit_tests/test_polynomials.py b/tests/unit_tests/test_polynomials.py index 52c30bb000..82b400b3b8 100644 --- a/tests/unit_tests/test_polynomials.py +++ b/tests/unit_tests/test_polynomials.py @@ -27,3 +27,21 @@ def test_zernike_radial(): test_vals = zn_rad(rho) assert ref_vals == test_vals + + rho = [0.2, 0.5] + # Reference solution from running the Fortran implementation + raw_zn1 = np.array([ + 1.00000000e+00, -9.20000000e-01, 7.69600000e-01, + -5.66720000e-01, 3.35219200e-01, -1.01747000e-01]) + raw_zn2 = np.array([ + 1.00000000e+00, -5.00000000e-01, -1.25000000e-01, + 4.37500000e-01, -2.89062500e-01, -8.98437500e-02]) + + ref_vals = [np.sum(norm_coeff * raw_zn1), np.sum(norm_coeff * raw_zn2)] + + test_vals = zn_rad(rho) + + print(ref_vals) + print(test_vals) + + assert np.allclose(ref_vals, test_vals) From 57674ce33b4bb5c3c597a5567d4be624e0d04793 Mon Sep 17 00:00:00 2001 From: Zhuoran Han Date: Thu, 23 Aug 2018 12:12:04 -0500 Subject: [PATCH 3/4] Delete leftovers --- tests/unit_tests/test_polynomials.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/unit_tests/test_polynomials.py b/tests/unit_tests/test_polynomials.py index 82b400b3b8..f6ac2711fd 100644 --- a/tests/unit_tests/test_polynomials.py +++ b/tests/unit_tests/test_polynomials.py @@ -41,7 +41,4 @@ def test_zernike_radial(): test_vals = zn_rad(rho) - print(ref_vals) - print(test_vals) - assert np.allclose(ref_vals, test_vals) From d4a6e8d827afe5b110747702e7808b296439f2c6 Mon Sep 17 00:00:00 2001 From: Zhuoran Han Date: Thu, 23 Aug 2018 15:33:19 -0500 Subject: [PATCH 4/4] Update --- openmc/polynomial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openmc/polynomial.py b/openmc/polynomial.py index 3552819f6b..8bcb74f68a 100644 --- a/openmc/polynomial.py +++ b/openmc/polynomial.py @@ -1,7 +1,7 @@ import numpy as np import openmc import openmc.capi as capi -from collections import Iterable +from collections.abc import Iterable def legendre_from_expcoef(coef, domain=(-1, 1)):