def starfish_lpot_source(queue, n_arms): from meshmode.discretization import Discretization from meshmode.discretization.poly_element import ( InterpolatoryQuadratureSimplexGroupFactory) from meshmode.mesh.generation import make_curve_mesh, NArmedStarfish mesh = make_curve_mesh(NArmedStarfish(n_arms, 0.8), np.linspace(0, 1, 1 + PANELS_PER_ARM * n_arms), TARGET_ORDER) pre_density_discr = Discretization( queue.context, mesh, InterpolatoryQuadratureSimplexGroupFactory(TARGET_ORDER)) lpot_kwargs = DEFAULT_LPOT_KWARGS.copy() lpot_kwargs.update(target_association_tolerance=0.025, _expansion_stick_out_factor=TCF, fmm_order=FMM_ORDER, qbx_order=QBX_ORDER, fmm_backend="fmmlib") from pytential.qbx import QBXLayerPotentialSource lpot_source = QBXLayerPotentialSource(pre_density_discr, OVSMP_FACTOR * TARGET_ORDER, **lpot_kwargs) lpot_source, _ = lpot_source.with_refinement() return lpot_source
def _build_qbx_discr(queue, ndim=2, nelements=30, target_order=7, qbx_order=4, curve_f=None): if curve_f is None: curve_f = NArmedStarfish(5, 0.25) if ndim == 2: mesh = make_curve_mesh(curve_f, np.linspace(0, 1, nelements + 1), target_order) elif ndim == 3: mesh = generate_torus(10.0, 2.0, order=target_order) else: raise ValueError("unsupported ambient dimension") from meshmode.discretization import Discretization from meshmode.discretization.poly_element import \ InterpolatoryQuadratureSimplexGroupFactory from pytential.qbx import QBXLayerPotentialSource density_discr = Discretization( queue.context, mesh, InterpolatoryQuadratureSimplexGroupFactory(target_order)) qbx, _ = QBXLayerPotentialSource(density_discr, fine_order=4 * target_order, qbx_order=qbx_order, fmm_order=False).with_refinement() return qbx
def test_get_qbx_center_neighborhood_sizes(ctx_factory): ctx = ctx_factory() queue = cl.CommandQueue(ctx) from meshmode.discretization import Discretization from meshmode.discretization.poly_element import ( InterpolatoryQuadratureSimplexGroupFactory) target_order = 8 n_arms = 5 nelements = 50 * n_arms mesh = make_curve_mesh( NArmedStarfish(n_arms, 0.8), np.linspace(0, 1, nelements+1), target_order) pre_density_discr = Discretization( queue.context, mesh, InterpolatoryQuadratureSimplexGroupFactory(target_order)) from pytential.qbx import QBXLayerPotentialSource lpot_source, _ = QBXLayerPotentialSource( pre_density_discr, 4 * target_order, _max_leaf_refine_weight=64, target_association_tolerance=1e-3, ).with_refinement() t_f = 0.9 result_direct = get_qbx_center_neighborhood_sizes_direct(lpot_source, 8/t_f) result_aq = get_qbx_center_neighborhood_sizes(lpot_source, 8/t_f) assert (result_direct[0] == result_aq[0]).all() assert result_direct[1] == result_aq[1] assert result_direct[2] == result_aq[2]
def curve_fn(self): from meshmode.mesh.generation import NArmedStarfish return NArmedStarfish(self.narms, self.amplitude)
def get_mesh(self, nelements, target_order): return make_curve_mesh(NArmedStarfish(self.n_arms, self.amplitude), np.linspace(0, 1, nelements + 1), target_order)
def get_starfish_mesh(n_arms, nelements, target_order): return make_curve_mesh(NArmedStarfish(n_arms, 0.8), np.linspace(0, 1, nelements + 1), target_order)
error = -np.inf for i in range(index_set.nblocks): mat_i = index_set.take(mat, i) blk_i = index_set.block_take(blk, i) error = max(error, la.norm(mat_i - blk_i) / la.norm(mat_i)) return error @pytest.mark.skipif(USE_SYMENGINE, reason="https://gitlab.tiker.net/inducer/sumpy/issues/25") @pytest.mark.parametrize("k", [0, 42]) @pytest.mark.parametrize("curve_f", [ partial(ellipse, 3), NArmedStarfish(5, 0.25)]) @pytest.mark.parametrize("lpot_id", [2, 3]) def test_matrix_build(ctx_factory, k, curve_f, lpot_id, visualize=False): cl_ctx = ctx_factory() queue = cl.CommandQueue(cl_ctx) # prevent cache 'splosion from sympy.core.cache import clear_cache clear_cache() target_order = 7 qbx_order = 4 nelements = 30 mesh = make_curve_mesh(curve_f, np.linspace(0, 1, nelements + 1), target_order)
vis = make_visualizer(actx, discr) filename = f"test_proxy_geometry_{suffix}_block_{i:04d}.vtu" vis.write_vtk_file(filename, [], overwrite=False) else: raise ValueError # }}} PROXY_TEST_CASES = [ extra.CurveTestCase(name="ellipse", target_order=7, curve_fn=partial(ellipse, 3.0)), extra.CurveTestCase(name="starfish", target_order=4, curve_fn=NArmedStarfish(5, 0.25), resolutions=[32]), extra.TorusTestCase(target_order=2, resolutions=[0]) ] # {{{ test_partition_points @pytest.mark.parametrize("tree_kind", ["adaptive", None]) @pytest.mark.parametrize("case", PROXY_TEST_CASES) def test_partition_points(actx_factory, tree_kind, case, visualize=False): """Tests that the points are correctly partitioned.""" if case.ambient_dim == 3 and tree_kind is None: pytest.skip("3d partitioning requires a tree")
def test_build_matrix_fixed_stage(ctx_factory, source_discr_stage, target_discr_stage, visualize=False): """Checks that the block builders match for difference stages.""" ctx = ctx_factory() queue = cl.CommandQueue(ctx) actx = PyOpenCLArrayContext(queue) # prevent cache explosion from sympy.core.cache import clear_cache clear_cache() case = extra.CurveTestCase( name="starfish", curve_fn=NArmedStarfish(5, 0.25), target_order=4, resolutions=[32], index_sparsity_factor=0.6, op_type="scalar", tree_kind=None, ) logger.info("\n%s", case) # {{{ geometry dd = sym.DOFDescriptor(case.name) qbx = case.get_layer_potential(actx, case.resolutions[-1], case.target_order) places = GeometryCollection( {case.name: qbx}, auto_where=(dd.copy(discr_stage=source_discr_stage), dd.copy(discr_stage=target_discr_stage))) dd = places.auto_source density_discr = places.get_discretization(dd.geometry, dd.discr_stage) # }}} # {{{ symbolic if source_discr_stage is target_discr_stage: qbx_forced_limit = -1 else: qbx_forced_limit = None sym_u, sym_op = case.get_operator(places.ambient_dim, qbx_forced_limit) from pytential.symbolic.execution import _prepare_expr sym_prep_op = _prepare_expr(places, sym_op) # }}} # {{{ check source_discr = places.get_discretization(case.name, source_discr_stage) target_discr = places.get_discretization(case.name, target_discr_stage) logger.info("nelements: %d", density_discr.mesh.nelements) logger.info("ndofs: %d", source_discr.ndofs) logger.info("ndofs: %d", target_discr.ndofs) icols = case.get_block_indices(actx, source_discr, matrix_indices=False) irows = case.get_block_indices(actx, target_discr, matrix_indices=False) index_set = MatrixBlockIndexRanges(actx.context, icols, irows) kwargs = dict( dep_expr=sym_u, other_dep_exprs=[], dep_source=places.get_geometry(case.name), dep_discr=density_discr, places=places, context=case.knl_concrete_kwargs, ) # qbx from pytential.symbolic import matrix mat = matrix.MatrixBuilder(actx, **kwargs)(sym_prep_op) blk = matrix.NearFieldBlockBuilder(actx, index_set=index_set, **kwargs)(sym_prep_op) assert mat.shape == (target_discr.ndofs, source_discr.ndofs) assert extra.max_block_error(mat, blk, index_set.get(queue)) < 1.0e-14 # p2p mat = matrix.P2PMatrixBuilder(actx, exclude_self=True, **kwargs)(sym_prep_op) blk = matrix.FarFieldBlockBuilder(actx, index_set=index_set, exclude_self=True, **kwargs)(sym_prep_op) assert mat.shape == (target_discr.ndofs, source_discr.ndofs) assert extra.max_block_error(mat, blk, index_set.get(queue)) < 1.0e-14
from pyopencl.tools import ( # noqa pytest_generate_tests_for_pyopencl as pytest_generate_tests) import extra_matrix_data as extra import logging logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) @pytest.mark.skipif(USE_SYMENGINE, reason="https://gitlab.tiker.net/inducer/sumpy/issues/25") @pytest.mark.parametrize("k", [0, 42]) @pytest.mark.parametrize( "curve_fn", [partial(ellipse, 3), NArmedStarfish(5, 0.25)]) @pytest.mark.parametrize("op_type", ["scalar_mixed", "vector"]) def test_build_matrix(ctx_factory, k, curve_fn, op_type, visualize=False): """Checks that the matrix built with `symbolic.execution.build_matrix` gives the same (to tolerance) answer as a direct evaluation. """ cl_ctx = ctx_factory() queue = cl.CommandQueue(cl_ctx) actx = PyOpenCLArrayContext(queue) # prevent cache 'splosion from sympy.core.cache import clear_cache clear_cache() case = extra.CurveTestCase(name="curve",