def test_mass_operator_inverse(actx_factory, name): actx = actx_factory() # {{{ cases import mesh_data if name == "2-1-ellipse": # curve builder = mesh_data.EllipseMeshBuilder(radius=3.1, aspect_ratio=2.0) elif name == "spheroid": # surface builder = mesh_data.SpheroidMeshBuilder() elif name.startswith("warped_rect"): builder = mesh_data.WarpedRectMeshBuilder(dim=int(name[-1])) else: raise ValueError("unknown geometry name: %s" % name) # }}} # {{{ inv(m) @ m == id from pytools.convergence import EOCRecorder eoc = EOCRecorder() for resolution in builder.resolutions: mesh = builder.get_mesh(resolution, builder.mesh_order) dcoll = DiscretizationCollection(actx, mesh, order=builder.order) volume_discr = dcoll.discr_from_dd(dof_desc.DD_VOLUME) logger.info("ndofs: %d", volume_discr.ndofs) logger.info("nelements: %d", volume_discr.mesh.nelements) # {{{ compute inverse mass def f(x): return actx.np.cos(4.0 * x[0]) dd = dof_desc.DD_VOLUME x_volm = thaw(volume_discr.nodes(), actx) f_volm = f(x_volm) f_inv = op.inverse_mass(dcoll, op.mass(dcoll, dd, f_volm)) inv_error = actx.to_numpy( op.norm(dcoll, f_volm - f_inv, 2) / op.norm(dcoll, f_volm, 2)) # }}} # compute max element size from grudge.dt_utils import h_max_from_volume h_max = h_max_from_volume(dcoll) eoc.add_data_point(h_max, inv_error) logger.info("inverse mass error\n%s", str(eoc)) # NOTE: both cases give 1.0e-16-ish at the moment, but just to be on the # safe side, choose a slightly larger tolerance assert eoc.max_error() < 1.0e-14
def wave_operator(dcoll, c, w): u = w[0] v = w[1:] dir_u = op.project(dcoll, "vol", BTAG_ALL, u) dir_v = op.project(dcoll, "vol", BTAG_ALL, v) dir_bval = flat_obj_array(dir_u, dir_v) dir_bc = flat_obj_array(-dir_u, dir_v) dd_quad = DOFDesc("vol", DISCR_TAG_QUAD) c_quad = op.project(dcoll, "vol", dd_quad, c) w_quad = op.project(dcoll, "vol", dd_quad, w) u_quad = w_quad[0] v_quad = w_quad[1:] dd_allfaces_quad = DOFDesc("all_faces", DISCR_TAG_QUAD) return (op.inverse_mass( dcoll, flat_obj_array(-op.weak_local_div(dcoll, dd_quad, c_quad * v_quad), -op.weak_local_grad(dcoll, dd_quad, c_quad * u_quad)) + # noqa: W504 op.face_mass( dcoll, dd_allfaces_quad, wave_flux(dcoll, c=c, w_tpair=op.interior_trace_pair(dcoll, w)) + wave_flux(dcoll, c=c, w_tpair=TracePair( BTAG_ALL, interior=dir_bval, exterior=dir_bc)))))
def operator(self, t, u): from grudge.dof_desc import DOFDesc, DD_VOLUME, DTAG_VOLUME_ALL from meshmode.discretization.connection import FACE_RESTR_ALL face_dd = DOFDesc(FACE_RESTR_ALL, self.quad_tag) quad_dd = DOFDesc(DTAG_VOLUME_ALL, self.quad_tag) dcoll = self.dcoll def flux(tpair): return op.project(dcoll, tpair.dd, face_dd, self.flux(tpair)) def to_quad(arg): return op.project(dcoll, DD_VOLUME, quad_dd, arg) quad_v = to_quad(self.v) quad_u = to_quad(u) return (op.inverse_mass( dcoll, sum( op.weak_local_d_dx(dcoll, quad_dd, d, quad_u * quad_v[d]) for d in range(dcoll.ambient_dim)) - op.face_mass( dcoll, face_dd, sum( flux(quad_tpair) for quad_tpair in to_quad_int_tpairs( dcoll, u, self.quad_tag)))))
def operator(self, t, w): dcoll = self.dcoll u = w[0] v = w[1:] actx = u.array_context # boundary conditions ------------------------------------------------- # dirichlet BCs ------------------------------------------------------- dir_u = op.project(dcoll, "vol", self.dirichlet_tag, u) dir_v = op.project(dcoll, "vol", self.dirichlet_tag, v) if self.dirichlet_bc_f: # FIXME from warnings import warn warn("Inhomogeneous Dirichlet conditions on the wave equation " "are still having issues.") dir_g = self.dirichlet_bc_f dir_bc = flat_obj_array(2 * dir_g - dir_u, dir_v) else: dir_bc = flat_obj_array(-dir_u, dir_v) # neumann BCs --------------------------------------------------------- neu_u = op.project(dcoll, "vol", self.neumann_tag, u) neu_v = op.project(dcoll, "vol", self.neumann_tag, v) neu_bc = flat_obj_array(neu_u, -neu_v) # radiation BCs ------------------------------------------------------- rad_normal = thaw(dcoll.normal(dd=self.radiation_tag), actx) rad_u = op.project(dcoll, "vol", self.radiation_tag, u) rad_v = op.project(dcoll, "vol", self.radiation_tag, v) rad_bc = flat_obj_array( 0.5 * (rad_u - self.sign * np.dot(rad_normal, rad_v)), 0.5 * rad_normal * (np.dot(rad_normal, rad_v) - self.sign * rad_u)) # entire operator ----------------------------------------------------- def flux(tpair): return op.project(dcoll, tpair.dd, "all_faces", self.flux(tpair)) result = (op.inverse_mass( dcoll, flat_obj_array(-self.c * op.weak_local_div(dcoll, v), -self.c * op.weak_local_grad(dcoll, u)) - op.face_mass( dcoll, sum( flux(tpair) for tpair in op.interior_trace_pairs(dcoll, w)) + flux(op.bv_trace_pair(dcoll, self.dirichlet_tag, w, dir_bc)) + flux(op.bv_trace_pair(dcoll, self.neumann_tag, w, neu_bc)) + flux(op.bv_trace_pair(dcoll, self.radiation_tag, w, rad_bc))))) result[0] = result[0] + self.source_f(actx, dcoll, t) return result
def operator(self, t, u): from grudge.dof_desc import DOFDesc, DD_VOLUME, DTAG_VOLUME_ALL from meshmode.mesh import BTAG_ALL from meshmode.discretization.connection import FACE_RESTR_ALL face_dd = DOFDesc(FACE_RESTR_ALL, self.quad_tag) boundary_dd = DOFDesc(BTAG_ALL, self.quad_tag) quad_dd = DOFDesc(DTAG_VOLUME_ALL, self.quad_tag) dcoll = self.dcoll def flux(tpair): return op.project(dcoll, tpair.dd, face_dd, self.flux(tpair)) def to_quad(arg): return op.project(dcoll, DD_VOLUME, quad_dd, arg) if self.inflow_u is not None: inflow_flux = flux( op.bv_trace_pair(dcoll, boundary_dd, interior=u, exterior=self.inflow_u(t))) else: inflow_flux = 0 quad_v = to_quad(self.v) quad_u = to_quad(u) return (op.inverse_mass( dcoll, sum( op.weak_local_d_dx(dcoll, quad_dd, d, quad_u * quad_v[d]) for d in range(dcoll.ambient_dim)) - op.face_mass( dcoll, face_dd, sum( flux(quad_tpair) for quad_tpair in to_quad_int_tpairs( dcoll, u, self.quad_tag)) + inflow_flux # FIXME: Add support for inflow/outflow tags # + flux(op.bv_trace_pair(dcoll, # self.inflow_tag, # interior=u, # exterior=bc_in)) # + flux(op.bv_trace_pair(dcoll, # self.outflow_tag, # interior=u, # exterior=bc_out)) )))
def wave_operator(dcoll, c, w): u = w[0] v = w[1:] dir_u = op.project(dcoll, "vol", BTAG_ALL, u) dir_v = op.project(dcoll, "vol", BTAG_ALL, v) dir_bval = flat_obj_array(dir_u, dir_v) dir_bc = flat_obj_array(-dir_u, dir_v) return (op.inverse_mass( dcoll, flat_obj_array(-c * op.weak_local_div(dcoll, v), -c * op.weak_local_grad(dcoll, u)) + # noqa: W504 op.face_mass( dcoll, wave_flux(dcoll, c=c, w_tpair=op.interior_trace_pair(dcoll, w)) + wave_flux(dcoll, c=c, w_tpair=TracePair( BTAG_ALL, interior=dir_bval, exterior=dir_bc)))))
def wave_operator(dcoll, c, w): u = w[0] v = w[1:] dir_u = op.project(dcoll, "vol", BTAG_ALL, u) dir_v = op.project(dcoll, "vol", BTAG_ALL, v) dir_bval = flat_obj_array(dir_u, dir_v) dir_bc = flat_obj_array(-dir_u, dir_v) dd_quad = DOFDesc("vol", DISCR_TAG_QUAD) c_quad = op.project(dcoll, "vol", dd_quad, c) w_quad = op.project(dcoll, "vol", dd_quad, w) u_quad = w_quad[0] v_quad = w_quad[1:] dd_allfaces_quad = DOFDesc("all_faces", DISCR_TAG_QUAD) return ( op.inverse_mass( dcoll, flat_obj_array( -op.weak_local_div(dcoll, dd_quad, c_quad*v_quad), -op.weak_local_grad(dcoll, dd_quad, c_quad*u_quad) \ # pylint: disable=invalid-unary-operand-type ) + op.face_mass( dcoll, dd_allfaces_quad, wave_flux( dcoll, c=c, w_tpair=op.bdry_trace_pair(dcoll, BTAG_ALL, interior=dir_bval, exterior=dir_bc) ) + sum( wave_flux(dcoll, c=c, w_tpair=tpair) for tpair in op.interior_trace_pairs(dcoll, w) ) ) ) )
def operator(self, t, w): """The full operator template - the high level description of the Maxwell operator. Combines the relevant operator templates for spatial derivatives, flux, boundary conditions etc. """ from grudge.tools import count_subset elec_components = count_subset(self.get_eh_subset()[0:3]) mag_components = count_subset(self.get_eh_subset()[3:6]) if self.fixed_material: # need to check this material_divisor = ([self.epsilon] * elec_components + [self.mu] * mag_components) tags_and_bcs = [ (self.pec_tag, self.pec_bc(w)), (self.pmc_tag, self.pmc_bc(w)), (self.absorb_tag, self.absorbing_bc(w)), (self.incident_tag, self.incident_bc(w)), ] dcoll = self.dcoll def flux(pair): return op.project(dcoll, pair.dd, "all_faces", self.flux(pair)) return (-self.local_derivatives(w) - op.inverse_mass( dcoll, op.face_mass( dcoll, sum( flux(tpair) for tpair in op.interior_trace_pairs(dcoll, w)) + sum( flux(op.bv_trace_pair(dcoll, tag, w, bc)) for tag, bc in tags_and_bcs)))) / material_divisor
def wave_operator(dcoll, c, w): u = w.u v = w.v dir_w = op.project(dcoll, "vol", BTAG_ALL, w) dir_u = dir_w.u dir_v = dir_w.v dir_bval = WaveState(u=dir_u, v=dir_v) dir_bc = WaveState(u=-dir_u, v=dir_v) return (op.inverse_mass( dcoll, WaveState(u=-c * op.weak_local_div(dcoll, v), v=-c * op.weak_local_grad(dcoll, u)) + op.face_mass( dcoll, wave_flux(dcoll, c=c, w_tpair=op.bdry_trace_pair( dcoll, BTAG_ALL, interior=dir_bval, exterior=dir_bc)) + sum( wave_flux(dcoll, c=c, w_tpair=tpair) for tpair in op.interior_trace_pairs(dcoll, w)))))
def operator(self, t, u): from meshmode.mesh import BTAG_ALL dcoll = self.dcoll def flux(tpair): return op.project(dcoll, tpair.dd, "all_faces", self.flux(tpair)) if self.inflow_u is not None: inflow_flux = flux( op.bv_trace_pair(dcoll, BTAG_ALL, interior=u, exterior=self.inflow_u(t))) else: inflow_flux = 0 return (op.inverse_mass( dcoll, np.dot(self.v, op.weak_local_grad(dcoll, u)) - op.face_mass( dcoll, sum( flux(tpair) for tpair in op.interior_trace_pairs(dcoll, u)) + inflow_flux # FIXME: Add support for inflow/outflow tags # + flux(op.bv_trace_pair(dcoll, # self.inflow_tag, # interior=u, # exterior=bc_in)) # + flux(op.bv_trace_pair(dcoll, # self.outflow_tag, # interior=u, # exterior=bc_out)) )))
lift = op.face_mass(dcoll, # left boundary weak-flux terms op.project(dcoll, left_bndry, "all_faces", flux(dcoll, lbnd_tpair)) # right boundary weak-flux terms + op.project(dcoll, right_bndry, "all_faces", flux(dcoll, rbnd_tpair)) # interior weak-flux terms + op.project(dcoll, FACE_RESTR_INTERIOR, "all_faces", flux(dcoll, interior_tpair))) duh_by_dt = op.inverse_mass(dcoll, np.dot([2 * np.pi], Su) - lift) # forward euler time step uh = uh + dt * duh_by_dt t += dt # ENDEXAMPLE # Plot the solution: def u_exact(x, t): return actx.np.sin(x[0] - 2 * np.pi * t) assert op.norm(dcoll, uh - u_exact(x_vol, t_final), p=2) <= 0.1
def inverse_mass(self, vec): return op.inverse_mass(self, vec)
def test_gradient(actx_factory, form, dim, order, vectorize, nested, visualize=False): actx = actx_factory() from pytools.convergence import EOCRecorder eoc_rec = EOCRecorder() for n in [4, 6, 8]: mesh = mgen.generate_regular_rect_mesh(a=(-1, ) * dim, b=(1, ) * dim, nelements_per_axis=(n, ) * dim) dcoll = DiscretizationCollection(actx, mesh, order=order) def f(x): result = dcoll.zeros(actx) + 1 for i in range(dim - 1): result = result * actx.np.sin(np.pi * x[i]) result = result * actx.np.cos(np.pi / 2 * x[dim - 1]) return result def grad_f(x): result = make_obj_array( [dcoll.zeros(actx) + 1 for _ in range(dim)]) for i in range(dim - 1): for j in range(i): result[i] = result[i] * actx.np.sin(np.pi * x[j]) result[i] = result[i] * np.pi * actx.np.cos(np.pi * x[i]) for j in range(i + 1, dim - 1): result[i] = result[i] * actx.np.sin(np.pi * x[j]) result[i] = result[i] * actx.np.cos(np.pi / 2 * x[dim - 1]) for j in range(dim - 1): result[dim - 1] = result[dim - 1] * actx.np.sin(np.pi * x[j]) result[dim - 1] = result[dim - 1] * (-np.pi / 2 * actx.np.sin(np.pi / 2 * x[dim - 1])) return result x = thaw(dcoll.nodes(), actx) if vectorize: u = make_obj_array([(i + 1) * f(x) for i in range(dim)]) else: u = f(x) def get_flux(u_tpair): dd = u_tpair.dd dd_allfaces = dd.with_dtag("all_faces") normal = thaw(dcoll.normal(dd), actx) u_avg = u_tpair.avg if vectorize: if nested: flux = make_obj_array( [u_avg_i * normal for u_avg_i in u_avg]) else: flux = np.outer(u_avg, normal) else: flux = u_avg * normal return op.project(dcoll, dd, dd_allfaces, flux) dd_allfaces = DOFDesc("all_faces") if form == "strong": grad_u = ( op.local_grad(dcoll, u, nested=nested) # No flux terms because u doesn't have inter-el jumps ) elif form == "weak": grad_u = op.inverse_mass( dcoll, -op.weak_local_grad(dcoll, u, nested=nested) # pylint: disable=E1130 + # noqa: W504 op.face_mass( dcoll, dd_allfaces, # Note: no boundary flux terms here because u_ext == u_int == 0 sum( get_flux(utpair) for utpair in op.interior_trace_pairs(dcoll, u)))) else: raise ValueError("Invalid form argument.") if vectorize: expected_grad_u = make_obj_array([(i + 1) * grad_f(x) for i in range(dim)]) if not nested: expected_grad_u = np.stack(expected_grad_u, axis=0) else: expected_grad_u = grad_f(x) if visualize: from grudge.shortcuts import make_visualizer vis = make_visualizer(dcoll, vis_order=order if dim == 3 else dim + 3) filename = ( f"test_gradient_{form}_{dim}_{order}" f"{'_vec' if vectorize else ''}{'_nested' if nested else ''}.vtu" ) vis.write_vtk_file(filename, [ ("u", u), ("grad_u", grad_u), ("expected_grad_u", expected_grad_u), ], overwrite=True) rel_linf_err = actx.to_numpy( op.norm(dcoll, grad_u - expected_grad_u, np.inf) / op.norm(dcoll, expected_grad_u, np.inf)) eoc_rec.add_data_point(1. / n, rel_linf_err) print("L^inf error:") print(eoc_rec) assert (eoc_rec.order_estimate() >= order - 0.5 or eoc_rec.max_error() < 1e-11)