forked from inducer/pytential
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tools.py
More file actions
180 lines (137 loc) · 5.77 KB
/
Copy pathtest_tools.py
File metadata and controls
180 lines (137 loc) · 5.77 KB
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
__copyright__ = "Copyright (C) 2013 Andreas Kloeckner"
__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 pytest
from functools import partial
import numpy as np
import numpy.linalg as la
from arraycontext import thaw
from meshmode import _acf # noqa: F401
from arraycontext import pytest_generate_tests_for_array_contexts
from meshmode.array_context import PytestPyOpenCLArrayContextFactory
import logging
logger = logging.getLogger(__name__)
pytest_generate_tests = pytest_generate_tests_for_array_contexts([
PytestPyOpenCLArrayContextFactory,
])
def test_gmres():
n = 200
A = ( # noqa
n * (np.eye(n) + 2j * np.eye(n))
+ np.random.randn(n, n) + 1j * np.random.randn(n, n))
true_sol = np.random.randn(n) + 1j * np.random.randn(n)
b = np.dot(A, true_sol)
A_func = lambda x: np.dot(A, x) # noqa
A_func.shape = A.shape
A_func.dtype = A.dtype
from pytential.solve import gmres, ResidualPrinter
tol = 1e-6
sol = gmres(A_func, b, callback=ResidualPrinter(),
maxiter=5*n, tol=tol).solution
assert la.norm(true_sol - sol) / la.norm(sol) < tol
def test_interpolatory_error_reporting(actx_factory):
logging.basicConfig(level=logging.INFO)
actx = actx_factory()
h = 0.2
from meshmode.mesh.io import generate_gmsh, FileSource
mesh = generate_gmsh(
FileSource("circle.step"), 2, order=4, force_ambient_dim=2,
other_options=["-string", "Mesh.CharacteristicLengthMax = %g;" % h],
target_unit="mm",
)
logger.info("%d elements" % mesh.nelements)
# {{{ discretizations and connections
from meshmode.discretization import Discretization
from meshmode.discretization.poly_element import \
QuadratureSimplexGroupFactory
vol_discr = Discretization(actx, mesh,
QuadratureSimplexGroupFactory(5))
vol_x = thaw(vol_discr.nodes(), actx)
# }}}
from pytential import integral
one = 1 + 0*vol_x[0]
from meshmode.discretization import NoninterpolatoryElementGroupError
with pytest.raises(NoninterpolatoryElementGroupError):
print("AREA", integral(vol_discr, one), 0.25**2*np.pi)
def test_geometry_collection_caching(actx_factory):
# NOTE: checks that the on-demand caching works properly in
# the `GeometryCollection`. This is done by constructing a few separated
# spheres, putting a few `QBXLayerPotentialSource`s on them and requesting
# the `nodes` on each `discr_stage`.
actx = actx_factory()
ndim = 2
nelements = 1024
target_order = 7
qbx_order = 4
ngeometry = 3
# construct discretizations
from meshmode.mesh.generation import ellipse, make_curve_mesh
from meshmode.mesh.processing import affine_map
from meshmode.discretization import Discretization
from meshmode.discretization.poly_element import \
InterpolatoryQuadratureSimplexGroupFactory
discrs = []
radius = 1.0
for k in range(ngeometry):
if k == 0:
mesh = make_curve_mesh(partial(ellipse, radius),
np.linspace(0.0, 1.0, nelements + 1),
target_order)
else:
mesh = affine_map(discrs[0].mesh,
b=np.array([3 * k * radius, 0]))
discr = Discretization(actx, mesh,
InterpolatoryQuadratureSimplexGroupFactory(target_order))
discrs.append(discr)
# construct qbx source
from pytential.qbx import QBXLayerPotentialSource
lpots = [
QBXLayerPotentialSource(density_discr,
fine_order=2 * target_order,
qbx_order=qbx_order,
fmm_order=False)
for density_discr in discrs
]
sources = [f"source_{k}" for k in range(ngeometry)]
# construct a geometry collection
from pytential import GeometryCollection
places = GeometryCollection(dict(zip(sources, lpots)), auto_where=sources[0])
print(places.places)
# check on-demand refinement
from pytential import bind, sym
discr_stages = [sym.QBX_SOURCE_STAGE1,
sym.QBX_SOURCE_STAGE2,
sym.QBX_SOURCE_QUAD_STAGE2]
for k in range(ngeometry):
for discr_stage in discr_stages:
with pytest.raises(KeyError):
discr = places._get_discr_from_cache(sources[k], discr_stage)
dofdesc = sym.DOFDescriptor(sources[k], discr_stage=discr_stage)
bind(places, sym.nodes(ndim, dofdesc=dofdesc))(actx)
discr = places._get_discr_from_cache(sources[k], discr_stage)
assert discr is not None
# You can test individual routines by typing
# $ python test_tools.py 'test_routine()'
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
exec(sys.argv[1])
else:
from pytest import main
main([__file__])
# vim: fdm=marker