From 5e512e06d3591792bb726c63dda6da596951a209 Mon Sep 17 00:00:00 2001 From: Alex Fikl Date: Thu, 10 May 2018 20:07:44 -0500 Subject: [PATCH 1/3] tests: parametrize test_matrix_build for more cases --- test/test_matrix.py | 46 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/test/test_matrix.py b/test/test_matrix.py index 5485c502..689fd3a9 100644 --- a/test/test_matrix.py +++ b/test/test_matrix.py @@ -40,7 +40,9 @@ from pyopencl.tools import ( # noqa @pytest.mark.skipif(USE_SYMENGINE, reason="https://gitlab.tiker.net/inducer/sumpy/issues/25") -def test_matrix_build(ctx_factory): +@pytest.mark.parametrize(("k", "layer_pot_id"), + [(0, 1), (0, 2), (0, 3)]) +def test_matrix_build(ctx_factory, k, layer_pot_id, plot=False): cl_ctx = ctx_factory() queue = cl.CommandQueue(cl_ctx) @@ -53,8 +55,6 @@ def test_matrix_build(ctx_factory): nelements = 30 curve_f = partial(ellipse, 3) - k = 1 - from sumpy.kernel import LaplaceKernel, HelmholtzKernel if k: knl = HelmholtzKernel(2) @@ -64,22 +64,21 @@ def test_matrix_build(ctx_factory): knl_kwargs = {} from pytools.obj_array import make_obj_array, is_obj_array - - if 1: + if layer_pot_id == 1: + u_sym = sym.var("u") + op = sym.Sp(knl, u_sym, **knl_kwargs) + elif layer_pot_id == 2: u_sym = sym.make_sym_vector("u", 2) u0_sym, u1_sym = u_sym op = make_obj_array([ - sym.Sp(knl, u0_sym, **knl_kwargs) - + sym.D(knl, u1_sym, **knl_kwargs), + sym.Sp(knl, u0_sym, **knl_kwargs) + + sym.D(knl, u1_sym, **knl_kwargs), - sym.S(knl, 0.4*u0_sym, **knl_kwargs) - + 0.3*sym.D(knl, u0_sym, **knl_kwargs) + sym.S(knl, 0.4 * u0_sym, **knl_kwargs) + + 0.3 * sym.D(knl, u0_sym, **knl_kwargs) ]) - elif 0: - u_sym = sym.var("u") - op = sym.Sp(knl, u_sym, **knl_kwargs) - else: + elif layer_pot_id == 3: k0 = 3 k1 = 2.9 beta = 2.5 @@ -97,9 +96,11 @@ def test_matrix_build(ctx_factory): u_sym = pde_op.make_unknown("u") op = pde_op.operator(u_sym) + else: + raise ValueError("Unknown layer_pot_id: {}".format(layer_pot_id)) mesh = make_curve_mesh(curve_f, - np.linspace(0, 1, nelements+1), + np.linspace(0, 1, nelements + 1), target_order) from meshmode.discretization import Discretization @@ -110,35 +111,32 @@ def test_matrix_build(ctx_factory): cl_ctx, mesh, InterpolatoryQuadratureSimplexGroupFactory(target_order)) - qbx, _ = QBXLayerPotentialSource(pre_density_discr, 4*target_order, + qbx, _ = QBXLayerPotentialSource(pre_density_discr, 4 * target_order, qbx_order, # Don't use FMM for now fmm_order=False).with_refinement() - density_discr = qbx.density_discr - bound_op = bind(qbx, op) from pytential.symbolic.execution import build_matrix mat = build_matrix(queue, qbx, op, u_sym).get() - if 0: + if plot: from sumpy.tools import build_matrix as build_matrix_via_matvec mat2 = build_matrix_via_matvec(bound_op.scipy_op(queue, "u")) + print(la.norm((mat - mat2).real, "fro") / la.norm(mat2.real, "fro"), + la.norm((mat - mat2).imag, "fro") / la.norm(mat2.imag, "fro")) - print( - la.norm((mat-mat2).real, "fro")/la.norm(mat2.real, "fro"), - la.norm((mat-mat2).imag, "fro")/la.norm(mat2.imag, "fro")) import matplotlib.pyplot as pt pt.subplot(121) - pt.imshow(np.log10(np.abs(1e-20+(mat-mat2).real))) + pt.imshow(np.log10(np.abs(1.0e-20 + (mat - mat2).real))) pt.colorbar() pt.subplot(122) - pt.imshow(np.log10(np.abs(1e-20+(mat-mat2).imag))) + pt.imshow(np.log10(np.abs(1.0e-20 + (mat - mat2).imag))) pt.colorbar() pt.show() - if 0: + if plot: import matplotlib.pyplot as pt pt.subplot(121) pt.imshow(mat.real) -- GitLab From 717125cd142924322917d505f06e714f30f11943 Mon Sep 17 00:00:00 2001 From: Alex Fikl Date: Thu, 10 May 2018 20:23:24 -0500 Subject: [PATCH 2/3] tests: fix test_matrix test --- pytential/symbolic/execution.py | 11 ++++------- test/test_matrix.py | 21 ++------------------- 2 files changed, 6 insertions(+), 26 deletions(-) diff --git a/pytential/symbolic/execution.py b/pytential/symbolic/execution.py index c0fe0153..f658a6aa 100644 --- a/pytential/symbolic/execution.py +++ b/pytential/symbolic/execution.py @@ -495,18 +495,15 @@ def build_matrix(queue, places, expr, input_exprs, domains=None, from pytools.obj_array import is_obj_array, make_obj_array if not is_obj_array(expr): expr = make_obj_array([expr]) - - from pytential.symbolic.primitives import DEFAULT_SOURCE - domains = _domains_default(len(input_exprs), places, domains, - DEFAULT_SOURCE) - try: - iter(input_exprs) + input_exprs = list(input_exprs) except TypeError: # not iterable, wrap in a list input_exprs = [input_exprs] - input_exprs = list(input_exprs) + from pytential.symbolic.primitives import DEFAULT_SOURCE + domains = _domains_default(len(input_exprs), places, domains, + DEFAULT_SOURCE) nblock_rows = len(expr) nblock_columns = len(input_exprs) diff --git a/test/test_matrix.py b/test/test_matrix.py index 689fd3a9..7c626e57 100644 --- a/test/test_matrix.py +++ b/test/test_matrix.py @@ -41,7 +41,8 @@ from pyopencl.tools import ( # noqa @pytest.mark.skipif(USE_SYMENGINE, reason="https://gitlab.tiker.net/inducer/sumpy/issues/25") @pytest.mark.parametrize(("k", "layer_pot_id"), - [(0, 1), (0, 2), (0, 3)]) + [(0, 1), (0, 2), + (42, 1), (42, 2)]) def test_matrix_build(ctx_factory, k, layer_pot_id, plot=False): cl_ctx = ctx_factory() queue = cl.CommandQueue(cl_ctx) @@ -78,24 +79,6 @@ def test_matrix_build(ctx_factory, k, layer_pot_id, plot=False): sym.S(knl, 0.4 * u0_sym, **knl_kwargs) + 0.3 * sym.D(knl, u0_sym, **knl_kwargs) ]) - elif layer_pot_id == 3: - k0 = 3 - k1 = 2.9 - beta = 2.5 - - from pytential.symbolic.pde.scalar import ( # noqa - DielectricSRep2DBoundaryOperator as SRep, - DielectricSDRep2DBoundaryOperator as SDRep) - pde_op = SDRep( - mode="tem", - k_vacuum=1, - interfaces=((0, 1, sym.DEFAULT_SOURCE),), - domain_k_exprs=(k0, k1), - beta=beta, - use_l2_weighting=False) - - u_sym = pde_op.make_unknown("u") - op = pde_op.operator(u_sym) else: raise ValueError("Unknown layer_pot_id: {}".format(layer_pot_id)) -- GitLab From af96eb2609c85b2c528aa7e253fded04fb535969 Mon Sep 17 00:00:00 2001 From: Alex Fikl Date: Thu, 10 May 2018 20:29:24 -0500 Subject: [PATCH 3/3] rename variable to match other tests --- test/test_matrix.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_matrix.py b/test/test_matrix.py index 7c626e57..1d04789d 100644 --- a/test/test_matrix.py +++ b/test/test_matrix.py @@ -43,7 +43,7 @@ from pyopencl.tools import ( # noqa @pytest.mark.parametrize(("k", "layer_pot_id"), [(0, 1), (0, 2), (42, 1), (42, 2)]) -def test_matrix_build(ctx_factory, k, layer_pot_id, plot=False): +def test_matrix_build(ctx_factory, k, layer_pot_id, visualize=False): cl_ctx = ctx_factory() queue = cl.CommandQueue(cl_ctx) @@ -104,7 +104,7 @@ def test_matrix_build(ctx_factory, k, layer_pot_id, plot=False): from pytential.symbolic.execution import build_matrix mat = build_matrix(queue, qbx, op, u_sym).get() - if plot: + if visualize: from sumpy.tools import build_matrix as build_matrix_via_matvec mat2 = build_matrix_via_matvec(bound_op.scipy_op(queue, "u")) print(la.norm((mat - mat2).real, "fro") / la.norm(mat2.real, "fro"), @@ -119,7 +119,7 @@ def test_matrix_build(ctx_factory, k, layer_pot_id, plot=False): pt.colorbar() pt.show() - if plot: + if visualize: import matplotlib.pyplot as pt pt.subplot(121) pt.imshow(mat.real) -- GitLab