def test_pde_check_kernels(ctx_factory, knl_info, order=5): dim = knl_info.kernel.dim tctx = t.ToyContext(ctx_factory(), knl_info.kernel, extra_source_kwargs=knl_info.extra_kwargs) pt_src = t.PointSources( tctx, np.random.rand(dim, 50) - 0.5, np.ones(50)) from pytools.convergence import EOCRecorder from sumpy.point_calculus import CalculusPatch eoc_rec = EOCRecorder() for h in [0.1, 0.05, 0.025]: cp = CalculusPatch(np.array([1, 0, 0])[:dim], h=h, order=order) pot = pt_src.eval(cp.points) pde = knl_info.pde_func(cp, pot) err = la.norm(pde) eoc_rec.add_data_point(h, err) print(eoc_rec) assert eoc_rec.order_estimate() > order - knl_info.nderivs + 1 - 0.1
def test_toy_p2e2p(src, ctr, tgt, expn_func, expected): src = src.reshape(3, -1) tgt = tgt.reshape(3, -1) rtol = 1e-2 if not 0 <= expected < 1 / (1 + rtol): raise ValueError() from sumpy.kernel import LaplaceKernel ctx = l3d.InterpretedToyContext(LaplaceKernel(3), l3d.L3DMultipoleExpansion, l3d.L3DLocalExpansion) src_pot = t.PointSources(ctx, src, weights=[1]) pot_actual = src_pot.eval(tgt) errors = [] for order in ORDERS: expn = expn_func(src_pot, ctr, order=order) pot_p2e2p = expn.eval(tgt) errors.append(np.abs(pot_actual - pot_p2e2p)) conv_factor = compute_approx_convergence_factor(ORDERS, errors) assert conv_factor < expected * (1 + rtol)
def main(): from sumpy.kernel import ( # noqa: F401 YukawaKernel, HelmholtzKernel, LaplaceKernel) tctx = t.ToyContext( cl.create_some_context(), #LaplaceKernel(2), YukawaKernel(2), extra_kernel_kwargs={"lam": 5}, #HelmholtzKernel(2), extra_kernel_kwargs={"k": 0.3}, ) pt_src = t.PointSources(tctx, np.random.rand(2, 50) - 0.5, np.ones(50)) fp = FieldPlotter([3, 0], extent=8) if 0 and plt is not None: t.logplot(fp, pt_src, cmap="jet") plt.colorbar() plt.show() mexp = t.multipole_expand(pt_src, [0, 0], 5) mexp2 = t.multipole_expand(mexp, [0, 0.25]) # noqa: F841 lexp = t.local_expand(mexp, [3, 0]) lexp2 = t.local_expand(lexp, [3, 1], 3) #diff = mexp - pt_src #diff = mexp2 - pt_src diff = lexp2 - pt_src print(t.l_inf(diff, 1.2, center=lexp2.center)) if 1 and plt is not None: t.logplot(fp, diff, cmap="jet", vmin=-3, vmax=0) plt.colorbar() plt.show()
def m2l_experiment(param): from sumpy.kernel import LaplaceKernel ctx = l3d.InterpretedToyContext(LaplaceKernel(3), l3d.L3DMultipoleExpansion, l3d.L3DLocalExpansion) assert param.r / param.rho < 1 mpole_center_dist = param.R + param.rho sources = param.r * SPHERE_SAMPLE sources[:, 2] += mpole_center_dist origin = np.zeros(3) mpole_center = np.array([0., 0., mpole_center_dist]) local_centers = BALL_SAMPLE * param.R error = 0 fmm_order = param.p qbx_order = param.q for src in sources: src = src.reshape(3, -1) src_pot = t.PointSources(ctx, src, weights=[1]) mpole_pot = t.multipole_expand(src_pot, mpole_center, order=fmm_order) local_pot = t.local_expand(mpole_pot, origin, order=fmm_order) for local_center in local_centers: targets = ((param.R - la.norm(local_center)) * SPHERE_SAMPLE + local_center) assert (la.norm(targets, axis=1) <= param.R * (1 + 1e-5)).all() if qbx_order == np.inf: actual = src_pot.eval(targets.T) - local_pot.eval(targets.T) else: qbx_src_pot = t.local_expand(src_pot, local_center, order=qbx_order) qbx_local_pot = t.local_expand(local_pot, local_center, order=qbx_order) diff = qbx_src_pot.with_coeffs(qbx_src_pot.coeffs - qbx_local_pot.coeffs) actual = diff.eval(targets.T) error = max(error, np.max(np.abs(actual))) return Result(param.R, param.r, param.rho, param.ratio, param.p, param.q, error)
def l2p_experiment(param): from sumpy.kernel import LaplaceKernel ctx = l3d.InterpretedToyContext(LaplaceKernel(3), l3d.L3DMultipoleExpansion, l3d.L3DLocalExpansion) assert param.r / param.rho < 1 origin = np.zeros(3) source_loc = np.array([0., 0., param.rho]) local_centers = BALL_SAMPLE * param.r fmm_order = param.p qbx_order = param.q src_pot = t.PointSources(ctx, source_loc.reshape(3, -1), weights=[1]) local_pot = t.local_expand(src_pot, origin, order=fmm_order) error = 0 for ctr in local_centers: targets = (param.r - la.norm(ctr)) * SPHERE_SAMPLE + ctr assert (la.norm(targets, axis=1) <= param.r * (1 + 1e-5)).all() if qbx_order == np.inf: actual = src_pot.eval(targets.T) - local_pot.eval(targets.T) else: qbx_src_pot = t.local_expand(src_pot, ctr, order=qbx_order) qbx_local_pot = t.local_expand(local_pot, ctr, order=qbx_order) diff = qbx_src_pot.with_coeffs(qbx_src_pot.coeffs - qbx_local_pot.coeffs) actual = diff.eval(targets.T) error = max(error, np.max(np.abs(actual))) return Result(param.R, param.r, param.rho, param.ratio, param.p, param.q, error)
if not 0 <= case.conv_factor <= 1: raise ValueError("convergence factor not in valid range: %e" % case.conv_factor) from sumpy.expansion.local import VolumeTaylorLocalExpansion from sumpy.expansion.multipole import VolumeTaylorMultipoleExpansion cl_ctx = ctx_factory() ctx = t.ToyContext(cl_ctx, LaplaceKernel(dim), VolumeTaylorMultipoleExpansion, VolumeTaylorLocalExpansion) errors = [] src_pot = t.PointSources(ctx, src, weights=np.array([1.])) pot_actual = src_pot.eval(tgt).item() for order in ORDERS_P2E2E2P: expn = case.expansion1(src_pot, case.center1, order=order) expn2 = case.expansion2(expn, case.center2, order=order) pot_p2e2e2p = expn2.eval(tgt).item() errors.append(np.abs(pot_actual - pot_p2e2e2p)) conv_factor = approx_convergence_factor(1 + np.array(ORDERS_P2E2E2P), errors) assert conv_factor <= min(1, case.conv_factor * (1 + RTOL_P2E2E2P)), \ (conv_factor, case.conv_factor * (1 + RTOL_P2E2E2P)) def test_cse_matvec():