-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtest_dt_utils.py
More file actions
240 lines (179 loc) · 7.71 KB
/
test_dt_utils.py
File metadata and controls
240 lines (179 loc) · 7.71 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
__copyright__ = """
Copyright (C) 2021 University of Illinois Board of Trustees
"""
__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 numpy as np
from grudge.array_context import (
PytestPyOpenCLArrayContextFactory,
PytestPytatoPyOpenCLArrayContextFactory
)
from arraycontext import pytest_generate_tests_for_array_contexts
pytest_generate_tests = pytest_generate_tests_for_array_contexts(
[PytestPyOpenCLArrayContextFactory,
PytestPytatoPyOpenCLArrayContextFactory])
from grudge import make_discretization_collection
import grudge.op as op
import pytest
import logging
logger = logging.getLogger(__name__)
from meshmode import _acf # noqa: F401
@pytest.mark.parametrize("name", ["interval", "box2d", "box3d"])
@pytest.mark.parametrize("tpe", [False, True])
def test_geometric_factors_regular_refinement(actx_factory, name, tpe):
from grudge.dt_utils import dt_geometric_factors
actx = actx_factory()
# {{{ cases
from meshmode.mesh import TensorProductElementGroup
group_cls = TensorProductElementGroup if tpe else None
if name == "interval":
from mesh_data import BoxMeshBuilder
builder = BoxMeshBuilder(ambient_dim=1, group_cls=group_cls)
elif name == "box2d":
from mesh_data import BoxMeshBuilder
builder = BoxMeshBuilder(ambient_dim=2, group_cls=group_cls)
elif name == "box3d":
from mesh_data import BoxMeshBuilder
builder = BoxMeshBuilder(ambient_dim=3, group_cls=group_cls)
else:
raise ValueError("unknown geometry name: %s" % name)
# }}}
min_factors = []
for resolution in builder.resolutions:
mesh = builder.get_mesh(resolution, builder.mesh_order)
dcoll = make_discretization_collection(actx, mesh, order=builder.order)
min_factors.append(
actx.to_numpy(
op.nodal_min(dcoll, "vol", actx.thaw(dt_geometric_factors(dcoll))))
)
# Resolution is doubled each refinement, so the ratio of consecutive
# geometric factors should satisfy: gfi+1 / gfi = 2
min_factors = np.asarray(min_factors)
ratios = min_factors[:-1] / min_factors[1:]
assert np.all(np.isclose(ratios, 2))
# Make sure it works with empty meshes
mesh = builder.get_mesh(0, builder.mesh_order)
dcoll = make_discretization_collection(actx, mesh, order=builder.order)
factors = actx.thaw(dt_geometric_factors(dcoll)) # noqa: F841
@pytest.mark.parametrize("name", ["interval", "box2d", "box3d"])
def test_non_geometric_factors(actx_factory, name):
from grudge.dt_utils import dt_non_geometric_factors
actx = actx_factory()
# {{{ cases
if name == "interval":
from mesh_data import BoxMeshBuilder
builder = BoxMeshBuilder(ambient_dim=1)
elif name == "box2d":
from mesh_data import BoxMeshBuilder
builder = BoxMeshBuilder(ambient_dim=2)
elif name == "box3d":
from mesh_data import BoxMeshBuilder
builder = BoxMeshBuilder(ambient_dim=3)
else:
raise ValueError("unknown geometry name: %s" % name)
# }}}
factors = []
degrees = list(range(1, 8))
for degree in degrees:
mesh = builder.get_mesh(1, degree)
dcoll = make_discretization_collection(actx, mesh, order=degree)
factors.append(min(dt_non_geometric_factors(dcoll)))
# Crude estimate, factors should behave like 1/N**2
factors = np.asarray(factors)
lower_bounds = 1/(np.asarray(degrees)**2)
upper_bounds = 6.295*lower_bounds
assert all(lower_bounds <= factors)
assert all(factors <= upper_bounds)
def test_build_jacobian(actx_factory):
actx = actx_factory()
import meshmode.mesh.generation as mgen
mesh = mgen.generate_regular_rect_mesh(a=[0], b=[1], nelements_per_axis=(3,))
assert mesh.dim == 1
dcoll = make_discretization_collection(actx, mesh, order=1)
def rhs(x):
return 3*x**2 + 2*x + 5
from pytools.obj_array import make_obj_array
base_state = make_obj_array([dcoll.zeros(actx)+2])
from grudge.tools import build_jacobian
mat = build_jacobian(actx, rhs, base_state, 1e-5)
assert np.array_equal(mat, np.diag(np.diag(mat)))
assert np.allclose(np.diag(mat), 3*2*2 + 2)
@pytest.mark.parametrize("dim", [1, 2])
@pytest.mark.parametrize("degree", [2, 4])
@pytest.mark.parametrize("tpe", [False, True])
def test_wave_dt_estimate(actx_factory, dim, degree, tpe, visualize=False):
actx = actx_factory()
# {{{ cases
from meshmode.mesh import TensorProductElementGroup
group_cls = TensorProductElementGroup if tpe else None
import meshmode.mesh.generation as mgen
a = [0, 0, 0]
b = [1, 1, 1]
mesh = mgen.generate_regular_rect_mesh(
a=a[:dim], b=b[:dim],
nelements_per_axis=(3,)*dim,
group_cls=group_cls)
assert mesh.dim == dim
dcoll = make_discretization_collection(actx, mesh, order=degree)
from grudge.models.wave import WeakWaveOperator
wave_op = WeakWaveOperator(dcoll, c=1)
rhs = actx.compile(
lambda w: wave_op.operator(t=0, w=w))
from pytools.obj_array import make_obj_array
fields = make_obj_array([dcoll.zeros(actx) for i in range(dim+1)])
from grudge.tools import build_jacobian
mat = build_jacobian(actx, rhs, fields, 1)
import numpy.linalg as la
eigvals = la.eigvals(mat)
assert (eigvals.real <= 1e-12).all()
from leap.rk import stability_function, RK4MethodBuilder
import sympy as sp
stab_func = sp.lambdify(*stability_function(
RK4MethodBuilder.a_explicit,
RK4MethodBuilder.output_coeffs))
dt_est = actx.to_numpy(wave_op.estimate_rk4_timestep(actx, dcoll))
if visualize:
re, im = np.mgrid[-4:1:30j, -5:5:30j]
sf_grid = np.abs(stab_func(re+1j*im))
import matplotlib.pyplot as plt
plt.contour(re, im, sf_grid, [0.25, 0.5, 0.75, 0.9, 1, 1.1])
plt.colorbar()
plt.plot(dt_est * eigvals.real, dt_est * eigvals.imag, "x")
plt.grid()
plt.show()
thresh = 1+1e-8
max_stab = np.max(np.abs(stab_func(dt_est*eigvals)))
assert max_stab < thresh, max_stab
dt_factors = 2**np.linspace(0, 4, 40)[1:]
stable_dt_factors = [
dt_factor
for dt_factor in dt_factors
if np.max(np.abs(stab_func(dt_factor*dt_est*eigvals))) < thresh]
if stable_dt_factors:
print(f"Stable timestep is {max(stable_dt_factors):.2f}x the estimate")
else:
print("Stable timestep estimate appears to be sharp")
assert not stable_dt_factors or max(stable_dt_factors) < 1.5, stable_dt_factors
# You can test individual routines by typing
# $ python test_grudge.py 'test_routine()'
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
exec(sys.argv[1])
else:
pytest.main([__file__])