def __init__(self, c, ambient_dim, source_f=0, flux_type="upwind", dirichlet_tag=BTAG_ALL, dirichlet_bc_f=0, neumann_tag=BTAG_NONE, radiation_tag=BTAG_NONE): assert isinstance(ambient_dim, int) self.c = c self.ambient_dim = ambient_dim self.source_f = source_f self.sign = sym.If(sym.Comparison(self.c, ">", 0), np.int32(1), np.int32(-1)) self.dirichlet_tag = dirichlet_tag self.neumann_tag = neumann_tag self.radiation_tag = radiation_tag self.dirichlet_bc_f = dirichlet_bc_f self.flux_type = flux_type
def test_map_if(actx_factory): """Test :meth:`grudge.symbolic.execution.ExecutionMapper.map_if` handling of scalar conditions. """ actx = actx_factory() dim = 2 mesh = mgen.generate_regular_rect_mesh(a=(-0.5, ) * dim, b=(0.5, ) * dim, nelements_per_axis=(8, ) * dim, order=4) discr = DiscretizationCollection(actx, mesh, order=4) sym_if = sym.If(sym.Comparison(2.0, "<", 1.0e-14), 1.0, 2.0) bind(discr, sym_if)(actx)
def test_map_if(actx_factory): """Test :meth:`grudge.symbolic.execution.ExecutionMapper.map_if` handling of scalar conditions. """ actx = actx_factory() from meshmode.mesh.generation import generate_regular_rect_mesh dim = 2 mesh = generate_regular_rect_mesh( a=(-0.5,)*dim, b=(0.5,)*dim, n=(8,)*dim, order=4) discr = DGDiscretizationWithBoundaries(actx, mesh, order=4) sym_if = sym.If(sym.Comparison(2.0, "<", 1.0e-14), 1.0, 2.0) bind(discr, sym_if)(actx)
def advection_weak_flux(flux_type, u, velocity): normal = sym.normal(u.dd, len(velocity)) v_dot_n = sym.cse(velocity.dot(normal), "v_dot_normal") flux_type = flux_type.lower() if flux_type == "central": return u.avg * v_dot_n elif flux_type == "lf": norm_v = sym.sqrt((velocity**2).sum()) return u.avg * v_dot_n + 0.5 * norm_v * (u.int - u.ext) elif flux_type == "upwind": u_upwind = sym.If( sym.Comparison(v_dot_n, ">", 0), u.int, # outflow u.ext # inflow ) return u_upwind * v_dot_n else: raise ValueError("flux `{}` is not implemented".format(flux_type))
def weak_flux(self, u): normal = sym.normal(u. dd, self.ambient_dim) v_dot_normal = sym.cse(self.v.dot(normal), "v_dot_normal") norm_v = sym.sqrt((self.v**2).sum()) if self.flux_type == "central": return u.avg*v_dot_normal elif self.flux_type == "lf": return u.avg*v_dot_normal + 0.5*norm_v*(u.int - u.ext) elif self.flux_type == "upwind": return ( v_dot_normal * sym.If( sym.Comparison(v_dot_normal, ">", 0), u.int, # outflow u.ext, # inflow )) else: raise ValueError("invalid flux type")
def f_step(x): return sym.If(sym.Comparison(dist_squared, "<", (4 * source_width)**2), 1, 0)
def main(write_output=True, order=4): cl_ctx = cl.create_some_context() queue = cl.CommandQueue(cl_ctx) actx = PyOpenCLArrayContext(queue) dims = 2 from meshmode.mesh.generation import generate_regular_rect_mesh mesh = generate_regular_rect_mesh( a=(-0.5,)*dims, b=(0.5,)*dims, nelements_per_axis=(20,)*dims) discr = DiscretizationCollection(actx, mesh, order=order) source_center = np.array([0.1, 0.22, 0.33])[:mesh.dim] source_width = 0.05 source_omega = 3 sym_x = sym.nodes(mesh.dim) sym_source_center_dist = sym_x - source_center sym_t = sym.ScalarVariable("t") c = sym.If(sym.Comparison( np.dot(sym_x, sym_x), "<", 0.15), np.float32(0.1), np.float32(0.2)) from grudge.models.wave import VariableCoefficientWeakWaveOperator from meshmode.mesh import BTAG_ALL, BTAG_NONE op = VariableCoefficientWeakWaveOperator(c, discr.dim, source_f=( sym.sin(source_omega*sym_t) * sym.exp( -np.dot(sym_source_center_dist, sym_source_center_dist) / source_width**2)), dirichlet_tag=BTAG_NONE, neumann_tag=BTAG_NONE, radiation_tag=BTAG_ALL, flux_type="upwind") from pytools.obj_array import flat_obj_array fields = flat_obj_array(discr.zeros(actx), [discr.zeros(actx) for i in range(discr.dim)]) op.check_bc_coverage(mesh) c_eval = bind(discr, c)(actx) bound_op = bind(discr, op.sym_operator()) def rhs(t, w): return bound_op(t=t, w=w) if mesh.dim == 2: dt = 0.04 * 0.3 elif mesh.dim == 3: dt = 0.02 * 0.1 dt_stepper = set_up_rk4("w", dt, fields, rhs) final_t = 1 nsteps = int(final_t/dt) print("dt=%g nsteps=%d" % (dt, nsteps)) from grudge.shortcuts import make_visualizer vis = make_visualizer(discr) step = 0 norm = bind(discr, sym.norm(2, sym.var("u"))) from time import time t_last_step = time() for event in dt_stepper.run(t_end=final_t): if isinstance(event, dt_stepper.StateComputed): assert event.component_id == "w" step += 1 print(step, event.t, norm(u=event.state_component[0]), time()-t_last_step) if step % 10 == 0: vis.write_vtk_file("fld-var-propogation-speed-%04d.vtu" % step, [ ("u", event.state_component[0]), ("v", event.state_component[1:]), ("c", c_eval), ]) t_last_step = time()
def f_step(x): return sym.If( sym.Comparison( np.dot(sym_source_center_dist, sym_source_center_dist), "<", (4 * source_width)**2), 1, 0)