-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_l3d.py
executable file
·192 lines (142 loc) · 6.14 KB
/
test_l3d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python
"""Tests for translation operators."""
__copyright__ = "Copyright (C) 2018 Matt Wala"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import l3d
import numpy as np
import numpy.linalg as la
import sumpy.toys as t
import pytest
from sumpy.kernel import LaplaceKernel
ORDERS = [2, 4, 9]
SCALING = 1 / (4 * np.pi)
def compute_approx_convergence_factor(orders, errors):
poly = np.polyfit(orders, np.log(errors), deg=1)
return np.exp(poly[0])
_LSRC = np.array([3., 4., 5.])
_LCTR = np.array([1., 0., 0.])
_LCTR2 = np.array([1., 3., 0.])
_LTGT = np.array([1., 1., 1.])
_LCONV = la.norm(_LTGT - _LCTR) / la.norm(_LSRC - _LCTR)
_MSRC = _LTGT
_MCTR = _LCTR
_MCTR2 = _LCTR2
_MTGT = _LSRC
_MCONV = la.norm(_MSRC - _MCTR) / la.norm(_MTGT - _MCTR)
_MCONV2 = la.norm(_MSRC - _MCTR2) / la.norm(_MTGT - _MCTR2)
_M2LSRC = np.array([-3., 4., 5.])
_M2LCTR = np.array([-2., 5., 3.])
_M2LCTR2 = np.array([1., 0., 2.])
_M2LTGT = np.array([0., 0., -1])
_M2LCONV = la.norm(_M2LTGT - _M2LCTR2) / (la.norm(_M2LCTR2 - _M2LCTR) -
la.norm(_M2LCTR - _M2LSRC))
@pytest.mark.parametrize("src, ctr, tgt, expn_class, expected",
[(_LSRC, _LCTR, _LTGT, l3d.L3DLocalExpansion, _LCONV),
(_MSRC, _MCTR, _MTGT, l3d.L3DMultipoleExpansion, _MCONV)])
def test_p2e2p(src, ctr, tgt, expn_class, expected):
errors = []
rtol = 1e-2
if not 0 <= expected < 1 / (1 + rtol):
raise ValueError()
pot_actual = SCALING / la.norm(tgt - src)
for order in ORDERS:
expn = expn_class(LaplaceKernel(3), order)
coeffs = expn.coefficients_from_source(ctr - src)
pot_p2e2p = expn.evaluate(coeffs, tgt - ctr)
errors.append(np.abs(pot_actual - pot_p2e2p))
conv_factor = compute_approx_convergence_factor(ORDERS, errors)
assert conv_factor < expected * (1 + rtol)
@pytest.mark.parametrize("src, ctr, tgt, expn_func, expected",
[(_LSRC, _LCTR, _LTGT, t.local_expand, _LCONV),
(_MSRC, _MCTR, _MTGT, t.multipole_expand, _MCONV)])
def test_toy_p2e2p(src, ctr, tgt, expn_func, expected):
src = src.reshape(3, -1)
tgt = tgt.reshape(3, -1)
rtol = 1e-2
if not 0 <= expected < 1 / (1 + rtol):
raise ValueError()
from sumpy.kernel import LaplaceKernel
ctx = l3d.InterpretedToyContext(
LaplaceKernel(3), l3d.L3DMultipoleExpansion, l3d.L3DLocalExpansion)
src_pot = t.PointSources(ctx, src, weights=[1])
pot_actual = src_pot.eval(tgt)
errors = []
for order in ORDERS:
expn = expn_func(src_pot, ctr, order=order)
pot_p2e2p = expn.eval(tgt)
errors.append(np.abs(pot_actual - pot_p2e2p))
conv_factor = compute_approx_convergence_factor(ORDERS, errors)
assert conv_factor < expected * (1 + rtol)
@pytest.mark.parametrize(
"src, ctr, ctr2, tgt, from_expn_class, to_expn_class, expected",
[(_LSRC, _LCTR, _LCTR2, _LTGT,
l3d.L3DLocalExpansion, l3d.L3DLocalExpansion, _LCONV),
(_MSRC, _MCTR, _MCTR2, _MTGT,
l3d.L3DMultipoleExpansion, l3d.L3DMultipoleExpansion, _MCONV2),
(_M2LSRC, _M2LCTR, _M2LCTR2, _M2LTGT,
l3d.L3DMultipoleExpansion, l3d.L3DLocalExpansion, _M2LCONV)])
def test_p2e2e2p(src, ctr, ctr2, tgt, from_expn_class, to_expn_class, expected):
errors = []
rtol = 1e-2
if not 0 <= expected < 1 / (1 + rtol):
raise ValueError()
pot_actual = SCALING / la.norm(tgt - src)
for order in ORDERS:
expn = from_expn_class(LaplaceKernel(3), order)
coeffs = expn.coefficients_from_source(ctr - src)
expn2 = to_expn_class(LaplaceKernel(3), order)
coeffs2 = expn2.translate_from(expn, coeffs, ctr2 - ctr)
pot_p2e2e2p = expn2.evaluate(coeffs2, tgt - ctr2)
errors.append(np.abs(pot_actual - pot_p2e2e2p))
conv_factor = compute_approx_convergence_factor(ORDERS, errors)
assert conv_factor < expected * (1 + rtol)
@pytest.mark.parametrize(
"src, ctr, ctr2, tgt, from_expn_func, to_expn_func, expected",
[(_LSRC, _LCTR, _LCTR2, _LTGT,
t.local_expand, t.local_expand, _LCONV),
(_MSRC, _MCTR, _MCTR2, _MTGT,
t.multipole_expand, t.multipole_expand, _MCONV2),
(_M2LSRC, _M2LCTR, _M2LCTR2, _M2LTGT,
t.multipole_expand, t.local_expand, _M2LCONV)])
def test_toy_p2e2e2p(src, ctr, ctr2, tgt, from_expn_func, to_expn_func, expected):
src = src.reshape(3, -1)
tgt = tgt.reshape(3, -1)
rtol = 1e-2
if not 0 <= expected < 1 / (1 + rtol):
raise ValueError()
from sumpy.kernel import LaplaceKernel
ctx = l3d.InterpretedToyContext(
LaplaceKernel(3), l3d.L3DMultipoleExpansion, l3d.L3DLocalExpansion)
errors = []
src_pot = t.PointSources(ctx, src, weights=[1])
pot_actual = src_pot.eval(tgt)
for order in ORDERS:
expn = from_expn_func(src_pot, ctr, order=order)
expn2 = to_expn_func(expn, ctr2, order=order)
pot_p2e2e2p = expn2.eval(tgt)
errors.append(np.abs(pot_actual - pot_p2e2e2p))
conv_factor = compute_approx_convergence_factor(ORDERS, errors)
assert conv_factor < expected * (1 + rtol)
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
exec(sys.argv[1])
else:
import py.test
py.test.cmdline.main([__file__])