Skip to content

Commit 584e1c7

Browse files
committed
port test_misc to arraycontext
1 parent 10b8849 commit 584e1c7

File tree

1 file changed

+91
-33
lines changed

1 file changed

+91
-33
lines changed

test/test_misc.py

Lines changed: 91 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,41 @@
2020
THE SOFTWARE.
2121
"""
2222

23+
import pytest
2324
import sys
2425
from dataclasses import dataclass
2526
from typing import Any, Callable
2627

2728
import numpy as np
2829
import numpy.linalg as la
2930

31+
from arraycontext import pytest_generate_tests_for_array_contexts
32+
from sumpy.array_context import ( # noqa: F401
33+
PytestPyOpenCLArrayContextFactory, _acf)
34+
3035
import sumpy.toys as t
3136
import sumpy.symbolic as sym
3237

33-
import pytest
34-
import pyopencl as cl # noqa: F401
35-
from pyopencl.tools import ( # noqa
36-
pytest_generate_tests_for_pyopencl as pytest_generate_tests)
37-
38-
from sumpy.kernel import (LaplaceKernel, HelmholtzKernel,
39-
BiharmonicKernel, YukawaKernel, StokesletKernel, StressletKernel,
40-
ElasticityKernel, LineOfCompressionKernel, ExpressionKernel)
41-
from sumpy.expansion.diff_op import (make_identity_diff_op, gradient,
42-
divergence, laplacian, concat, as_scalar_pde, curl, diff)
38+
from sumpy.kernel import (
39+
LaplaceKernel,
40+
HelmholtzKernel,
41+
BiharmonicKernel,
42+
YukawaKernel,
43+
StokesletKernel,
44+
StressletKernel,
45+
ElasticityKernel,
46+
LineOfCompressionKernel,
47+
ExpressionKernel)
48+
from sumpy.expansion.diff_op import (
49+
make_identity_diff_op, concat, as_scalar_pde, diff,
50+
gradient, divergence, laplacian, curl)
51+
52+
import logging
53+
logger = logging.getLogger(__name__)
54+
55+
pytest_generate_tests = pytest_generate_tests_for_array_contexts([
56+
PytestPyOpenCLArrayContextFactory,
57+
])
4358

4459

4560
# {{{ pde check for kernels
@@ -93,15 +108,17 @@ def nderivs(self):
93108
KernelInfo(LineOfCompressionKernel(3, 0), mu=5, nu=0.2),
94109
KernelInfo(LineOfCompressionKernel(3, 1), mu=5, nu=0.2),
95110
])
96-
def test_pde_check_kernels(ctx_factory, knl_info, order=5):
111+
def test_pde_check_kernels(actx_factory, knl_info, order=5):
112+
actx = actx_factory()
113+
97114
dim = knl_info.kernel.dim
98-
tctx = t.ToyContext(ctx_factory(), knl_info.kernel,
115+
tctx = t.ToyContext(actx.context, knl_info.kernel,
99116
extra_source_kwargs=knl_info.extra_kwargs)
100117

101-
np.random.seed(17)
118+
rng = np.random.default_rng(42)
102119
pt_src = t.PointSources(
103120
tctx,
104-
np.random.rand(dim, 50) - 0.5,
121+
rng.random(size=(dim, 50)) - 0.5,
105122
np.ones(50))
106123

107124
from pytools.convergence import EOCRecorder
@@ -117,12 +134,14 @@ def test_pde_check_kernels(ctx_factory, knl_info, order=5):
117134
err = la.norm(pde)
118135
eoc_rec.add_data_point(h, err)
119136

120-
print(eoc_rec)
137+
logger.info("eoc:\n%s", eoc_rec)
121138
assert eoc_rec.order_estimate() > order - knl_info.nderivs + 1 - 0.1
122139

123140
# }}}
124141

125142

143+
# {{{ test_pde_check
144+
126145
@pytest.mark.parametrize("dim", [1, 2, 3])
127146
def test_pde_check(dim, order=4):
128147
from sumpy.point_calculus import CalculusPatch
@@ -138,15 +157,19 @@ def test_pde_check(dim, order=4):
138157
err = la.norm(df_num-df_true)
139158
eoc_rec.add_data_point(h, err)
140159

141-
print(eoc_rec)
160+
logger.info("eoc:\n%s", eoc_rec)
142161
assert eoc_rec.order_estimate() > order-2-0.1
143162

163+
# }}}
164+
144165

166+
# {{{ test_order_finder
167+
168+
@dataclass(frozen=True)
145169
class FakeTree:
146-
def __init__(self, dimensions, root_extent, stick_out_factor):
147-
self.dimensions = dimensions
148-
self.root_extent = root_extent
149-
self.stick_out_factor = stick_out_factor
170+
dimensions: int
171+
root_extent: float
172+
stick_out_factor: float
150173

151174

152175
@pytest.mark.parametrize("knl", [
@@ -161,7 +184,7 @@ def test_order_finder(knl):
161184
orders = [
162185
ofind(knl, frozenset([("k", 5)]), tree, level)
163186
for level in range(30)]
164-
print(orders)
187+
logger.info("orders: %s", orders)
165188

166189
# Order should not increase with level
167190
assert (np.diff(orders) <= 0).all()
@@ -180,11 +203,13 @@ def test_fmmlib_order_finder(knl):
180203
orders = [
181204
ofind(knl, frozenset([("k", 5)]), tree, level)
182205
for level in range(30)]
183-
print(orders)
206+
logger.info("orders: %s", orders)
184207

185208
# Order should not increase with level
186209
assert (np.diff(orders) <= 0).all()
187210

211+
# }}}
212+
188213

189214
# {{{ expansion toys p2e2e2p test cases
190215

@@ -193,7 +218,7 @@ def approx_convergence_factor(orders, errors):
193218
return np.exp(poly[0])
194219

195220

196-
@dataclass
221+
@dataclass(frozen=True)
197222
class P2E2E2PTestCase:
198223
source: np.ndarray
199224
target: np.ndarray
@@ -243,12 +268,14 @@ def dim(self):
243268
# }}}
244269

245270

271+
# {{{ test_toy_p2e2e2p
272+
246273
ORDERS_P2E2E2P = (3, 4, 5)
247274
RTOL_P2E2E2P = 1e-2
248275

249276

250277
@pytest.mark.parametrize("case", P2E2E2P_TEST_CASES)
251-
def test_toy_p2e2e2p(ctx_factory, case):
278+
def test_toy_p2e2e2p(actx_factory, case):
252279
dim = case.dim
253280

254281
src = case.source.reshape(dim, -1)
@@ -269,8 +296,8 @@ def test_toy_p2e2e2p(ctx_factory, case):
269296

270297
from sumpy.expansion import VolumeTaylorExpansionFactory
271298

272-
cl_ctx = ctx_factory()
273-
ctx = t.ToyContext(cl_ctx,
299+
actx = actx_factory()
300+
ctx = t.ToyContext(actx.context,
274301
LaplaceKernel(dim),
275302
expansion_factory=VolumeTaylorExpansionFactory())
276303

@@ -289,6 +316,10 @@ def test_toy_p2e2e2p(ctx_factory, case):
289316
assert conv_factor <= min(1, case_conv_factor * (1 + RTOL_P2E2E2P)), \
290317
(conv_factor, case_conv_factor * (1 + RTOL_P2E2E2P))
291318

319+
# }}}
320+
321+
322+
# {{{ test_cse_matvec
292323

293324
def test_cse_matvec():
294325
from sumpy.expansion import CSEMatVecOperator
@@ -309,16 +340,21 @@ def test_cse_matvec():
309340
op = CSEMatVecOperator(input_coeffs, output_coeffs, shape=(4, 2))
310341
m = np.array([[2, 0], [6, 0], [0, 1], [30, 16]])
311342

312-
vec = np.random.random(2)
343+
rng = np.random.default_rng(42)
344+
vec = rng.random(2)
313345
expected_result = m @ vec
314346
actual_result = op.matvec(vec)
315347
assert np.allclose(expected_result, actual_result)
316348

317-
vec = np.random.random(4)
349+
vec = rng.random(4)
318350
expected_result = m.T @ vec
319351
actual_result = op.transpose_matvec(vec)
320352
assert np.allclose(expected_result, actual_result)
321353

354+
# }}}
355+
356+
357+
# {{{ test_diff_op_stokes
322358

323359
def test_diff_op_stokes():
324360
from sumpy.symbolic import symbols, Function
@@ -341,6 +377,10 @@ def test_diff_op_stokes():
341377

342378
assert expected_output == actual_output
343379

380+
# }}}
381+
382+
383+
# {{{ test_as_scalar_pde_stokes
344384

345385
def test_as_scalar_pde_stokes():
346386
diff_op = make_identity_diff_op(3, 4)
@@ -350,13 +390,17 @@ def test_as_scalar_pde_stokes():
350390

351391
# velocity components in Stokes should satisfy Biharmonic
352392
for i in range(3):
353-
print(as_scalar_pde(pde, i))
354-
print(laplacian(laplacian(u[i])))
393+
logger.info("pde\n%s", as_scalar_pde(pde, i))
394+
logger.info("\n%s", laplacian(laplacian(u[i])))
355395
assert as_scalar_pde(pde, i) == laplacian(laplacian(u[0]))
356396

357397
# pressure should satisfy Laplace
358398
assert as_scalar_pde(pde, 3) == laplacian(u[0])
359399

400+
# }}}
401+
402+
403+
# {{{ test_as_scalar_pde_maxwell
360404

361405
def test_as_scalar_pde_maxwell():
362406
from sumpy.symbolic import symbols
@@ -374,6 +418,10 @@ def test_as_scalar_pde_maxwell():
374418
assert as_scalar_pde(pde, i) == \
375419
laplacian(op[0]) - mu*epsilon*diff(diff(op[0], t), t)
376420

421+
# }}}
422+
423+
424+
# {{{ test_as_scalar_pde_elasticity
377425

378426
def test_as_scalar_pde_elasticity():
379427

@@ -408,6 +456,10 @@ def test_as_scalar_pde_elasticity():
408456
assert scalar_pde == laplacian(laplacian(diff_op[0]))
409457
assert scalar_pde.order == 4
410458

459+
# }}}
460+
461+
462+
# {{{ test_elasticity_new
411463

412464
def test_elasticity_new():
413465
from pickle import dumps, loads
@@ -424,6 +476,10 @@ def test_elasticity_new():
424476
assert not isinstance(knl, StokesletKernel)
425477
assert loads(dumps(knl)) == knl
426478

479+
# }}}
480+
481+
482+
# {{{ test_weird_kernel
427483

428484
w = make_identity_diff_op(2)
429485

@@ -457,15 +513,17 @@ def get_pde_as_diff_op(self):
457513

458514
assert fft_size == order
459515

516+
# }}}
517+
460518

461519
# You can test individual routines by typing
462-
# $ python test_misc.py 'test_p2p(cl.create_some_context)'
520+
# $ python test_misc.py 'test_pde_check_kernels(_acf,
521+
# KernelInfo(HelmholtzKernel(2), k=5), order=5)'
463522

464523
if __name__ == "__main__":
465524
if len(sys.argv) > 1:
466525
exec(sys.argv[1])
467526
else:
468-
from pytest import main
469-
main([__file__])
527+
pytest.main([__file__])
470528

471529
# vim: fdm=marker

0 commit comments

Comments
 (0)