def main(write_output=True) : from math import sin, cos, pi, exp, sqrt from grudge.data import TimeConstantGivenFunction, \ ConstantGivenFunction from grudge.backends import guess_run_context rcon = guess_run_context() dim = 2 def boundary_tagger(fvi, el, fn, all_v): if el.face_normals[fn][0] > 0: return ["dirichlet"] else: return ["neumann"] if dim == 2: if rcon.is_head_rank: from grudge.mesh.generator import make_disk_mesh mesh = make_disk_mesh(r=0.5, boundary_tagger=boundary_tagger) elif dim == 3: if rcon.is_head_rank: from grudge.mesh.generator import make_ball_mesh mesh = make_ball_mesh(max_volume=0.001) else: raise RuntimeError("bad number of dimensions") if rcon.is_head_rank: print("%d elements" % len(mesh.elements)) mesh_data = rcon.distribute_mesh(mesh) else: mesh_data = rcon.receive_mesh() discr = rcon.make_discretization(mesh_data, order=3, debug=["cuda_no_plan"], default_scalar_type=numpy.float64) if write_output: from grudge.visualization import VtkVisualizer vis = VtkVisualizer(discr, rcon, "fld") def u0(x, el): if la.norm(x) < 0.2: return 1 else: return 0 def coeff(x, el): if x[0] < 0: return 0.25 else: return 1 def dirichlet_bc(t, x): return 0 def neumann_bc(t, x): return 2 from grudge.models.diffusion import DiffusionOperator op = DiffusionOperator(discr.dimensions, #coeff=coeff, dirichlet_tag="dirichlet", dirichlet_bc=TimeConstantGivenFunction(ConstantGivenFunction(0)), neumann_tag="neumann", neumann_bc=TimeConstantGivenFunction(ConstantGivenFunction(1)) ) u = discr.interpolate_volume_function(u0) # diagnostics setup ------------------------------------------------------- from logpyle import LogManager, \ add_general_quantities, \ add_simulation_quantities, \ add_run_info if write_output: log_file_name = "heat.dat" else: log_file_name = None logmgr = LogManager(log_file_name, "w", rcon.communicator) add_run_info(logmgr) add_general_quantities(logmgr) add_simulation_quantities(logmgr) discr.add_instrumentation(logmgr) from grudge.log import LpNorm u_getter = lambda: u logmgr.add_quantity(LpNorm(u_getter, discr, 1, name="l1_u")) logmgr.add_quantity(LpNorm(u_getter, discr, name="l2_u")) logmgr.add_watches(["step.max", "t_sim.max", "l2_u", "t_step.max"]) # timestep loop ----------------------------------------------------------- from grudge.timestep.runge_kutta import LSRK4TimeStepper, ODE45TimeStepper from grudge.timestep.dumka3 import Dumka3TimeStepper #stepper = LSRK4TimeStepper() stepper = Dumka3TimeStepper(3, rtol=1e-6, rcon=rcon, vector_primitive_factory=discr.get_vector_primitive_factory(), dtype=discr.default_scalar_type) #stepper = ODE45TimeStepper(rtol=1e-6, rcon=rcon, #vector_primitive_factory=discr.get_vector_primitive_factory(), #dtype=discr.default_scalar_type) stepper.add_instrumentation(logmgr) rhs = op.bind(discr) try: next_dt = op.estimate_timestep(discr, stepper=LSRK4TimeStepper(), t=0, fields=u) from grudge.timestep import times_and_steps step_it = times_and_steps( final_time=0.1, logmgr=logmgr, max_dt_getter=lambda t: next_dt, taken_dt_getter=lambda: taken_dt) for step, t, dt in step_it: if step % 10 == 0 and write_output: visf = vis.make_file("fld-%04d" % step) vis.add_data(visf, [ ("u", discr.convert_volume(u, kind="numpy")), ], time=t, step=step) visf.close() u, t, taken_dt, next_dt = stepper(u, t, next_dt, rhs) #u = stepper(u, t, dt, rhs) assert discr.norm(u) < 1 finally: if write_output: vis.close() logmgr.close() discr.close()
def main(write_output=True): from grudge.data import GivenFunction, ConstantGivenFunction from grudge.backends import guess_run_context rcon = guess_run_context() dim = 2 def boundary_tagger(fvi, el, fn, points): from math import atan2, pi normal = el.face_normals[fn] if -90/180*pi < atan2(normal[1], normal[0]) < 90/180*pi: return ["neumann"] else: return ["dirichlet"] def dirichlet_boundary_tagger(fvi, el, fn, points): return ["dirichlet"] if dim == 2: if rcon.is_head_rank: from grudge.mesh.generator import make_disk_mesh mesh = make_disk_mesh(r=0.5, boundary_tagger=dirichlet_boundary_tagger, max_area=1e-3) elif dim == 3: if rcon.is_head_rank: from grudge.mesh.generator import make_ball_mesh mesh = make_ball_mesh(max_volume=0.0001, boundary_tagger=lambda fvi, el, fn, points: ["dirichlet"]) else: raise RuntimeError("bad number of dimensions") if rcon.is_head_rank: print("%d elements" % len(mesh.elements)) mesh_data = rcon.distribute_mesh(mesh) else: mesh_data = rcon.receive_mesh() discr = rcon.make_discretization(mesh_data, order=5, debug=[]) def dirichlet_bc(x, el): from math import sin return sin(10*x[0]) def rhs_c(x, el): if la.norm(x) < 0.1: return 1000 else: return 0 def my_diff_tensor(): result = numpy.eye(dim) result[0,0] = 0.1 return result try: from grudge.models.poisson import ( PoissonOperator, HelmholtzOperator) from grudge.second_order import \ IPDGSecondDerivative, LDGSecondDerivative, \ StabilizedCentralSecondDerivative k = 1 from grudge.mesh import BTAG_NONE, BTAG_ALL op = HelmholtzOperator(k, discr.dimensions, #diffusion_tensor=my_diff_tensor(), #dirichlet_tag="dirichlet", #neumann_tag="neumann", dirichlet_tag=BTAG_ALL, neumann_tag=BTAG_NONE, #dirichlet_tag=BTAG_ALL, #neumann_tag=BTAG_NONE, #dirichlet_bc=GivenFunction(dirichlet_bc), dirichlet_bc=ConstantGivenFunction(0), neumann_bc=ConstantGivenFunction(-10), scheme=StabilizedCentralSecondDerivative(), #scheme=LDGSecondDerivative(), #scheme=IPDGSecondDerivative(), ) bound_op = op.bind(discr) if False: from grudge.iterative import parallel_cg u = -parallel_cg(rcon, -bound_op, bound_op.prepare_rhs(discr.interpolate_volume_function(rhs_c)), debug=20, tol=5e-4, dot=discr.nodewise_dot_product, x=discr.volume_zeros()) else: rhs = bound_op.prepare_rhs(discr.interpolate_volume_function(rhs_c)) def compute_resid(x): return bound_op(x)-rhs from scipy.sparse.linalg import minres, LinearOperator u, info = minres( LinearOperator( (len(discr), len(discr)), matvec=bound_op, dtype=bound_op.dtype), rhs, callback=ResidualPrinter(compute_resid), tol=1e-5) print() if info != 0: raise RuntimeError("gmres reported error %d" % info) print("finished gmres") print(la.norm(bound_op(u)-rhs)/la.norm(rhs)) if write_output: from grudge.visualization import SiloVisualizer, VtkVisualizer vis = VtkVisualizer(discr, rcon) visf = vis.make_file("fld") vis.add_data(visf, [ ("sol", discr.convert_volume(u, kind="numpy")), ]) visf.close() finally: discr.close()
def main(write_output=True, flux_type_arg="upwind"): from grudge.tools import mem_checkpoint from math import sin, cos, pi, sqrt from math import floor from grudge.backends import guess_run_context rcon = guess_run_context() def f(x): return sin(pi*x) def u_analytic(x, el, t): return f((-numpy.dot(v, x)/norm_v+t*norm_v)) def boundary_tagger(vertices, el, face_nr, all_v): if numpy.dot(el.face_normals[face_nr], v) < 0: return ["inflow"] else: return ["outflow"] dim = 2 if dim == 1: v = numpy.array([1]) if rcon.is_head_rank: from grudge.mesh.generator import make_uniform_1d_mesh mesh = make_uniform_1d_mesh(0, 2, 10, periodic=True) elif dim == 2: v = numpy.array([2,0]) if rcon.is_head_rank: from grudge.mesh.generator import make_disk_mesh mesh = make_disk_mesh(boundary_tagger=boundary_tagger) elif dim == 3: v = numpy.array([0,0,1]) if rcon.is_head_rank: from grudge.mesh.generator import make_cylinder_mesh, make_ball_mesh, make_box_mesh mesh = make_cylinder_mesh(max_volume=0.04, height=2, boundary_tagger=boundary_tagger, periodic=False, radial_subdivisions=32) else: raise RuntimeError("bad number of dimensions") norm_v = la.norm(v) if rcon.is_head_rank: mesh_data = rcon.distribute_mesh(mesh) else: mesh_data = rcon.receive_mesh() if dim != 1: mesh_data = mesh_data.reordered_by("cuthill") discr = rcon.make_discretization(mesh_data, order=4) vis_discr = discr from grudge.visualization import VtkVisualizer if write_output: vis = VtkVisualizer(vis_discr, rcon, "fld") # operator setup ---------------------------------------------------------- from grudge.data import \ ConstantGivenFunction, \ TimeConstantGivenFunction, \ TimeDependentGivenFunction from grudge.models.advection import StrongAdvectionOperator, WeakAdvectionOperator op = WeakAdvectionOperator(v, inflow_u=TimeDependentGivenFunction(u_analytic), flux_type=flux_type_arg) u = discr.interpolate_volume_function(lambda x, el: u_analytic(x, el, 0)) # timestep setup ---------------------------------------------------------- from grudge.timestep.runge_kutta import LSRK4TimeStepper stepper = LSRK4TimeStepper() if rcon.is_head_rank: print("%d elements" % len(discr.mesh.elements)) # diagnostics setup ------------------------------------------------------- from logpyle import LogManager, \ add_general_quantities, \ add_simulation_quantities, \ add_run_info if write_output: log_file_name = "advection.dat" else: log_file_name = None logmgr = LogManager(log_file_name, "w", rcon.communicator) add_run_info(logmgr) add_general_quantities(logmgr) add_simulation_quantities(logmgr) discr.add_instrumentation(logmgr) stepper.add_instrumentation(logmgr) from grudge.log import Integral, LpNorm u_getter = lambda: u logmgr.add_quantity(Integral(u_getter, discr, name="int_u")) logmgr.add_quantity(LpNorm(u_getter, discr, p=1, name="l1_u")) logmgr.add_quantity(LpNorm(u_getter, discr, name="l2_u")) logmgr.add_watches(["step.max", "t_sim.max", "l2_u", "t_step.max"]) # timestep loop ----------------------------------------------------------- rhs = op.bind(discr) try: from grudge.timestep import times_and_steps step_it = times_and_steps( final_time=3, logmgr=logmgr, max_dt_getter=lambda t: op.estimate_timestep(discr, stepper=stepper, t=t, fields=u)) for step, t, dt in step_it: if step % 5 == 0 and write_output: visf = vis.make_file("fld-%04d" % step) vis.add_data(visf, [ ("u", discr.convert_volume(u, kind="numpy")), ], time=t, step=step) visf.close() u = stepper(u, t, dt, rhs) true_u = discr.interpolate_volume_function(lambda x, el: u_analytic(x, el, t)) print(discr.norm(u-true_u)) assert discr.norm(u-true_u) < 1e-2 finally: if write_output: vis.close() logmgr.close() discr.close()
def main(write_output=True): from math import sqrt, pi, exp from os.path import join from grudge.backends import guess_run_context rcon = guess_run_context() epsilon0 = 8.8541878176e-12 # C**2 / (N m**2) mu0 = 4 * pi * 1e-7 # N/A**2. epsilon = 1 * epsilon0 mu = 1 * mu0 output_dir = "maxwell-2d" import os if not os.access(output_dir, os.F_OK): os.makedirs(output_dir) from grudge.mesh.generator import make_disk_mesh mesh = make_disk_mesh(r=0.5, max_area=1e-3) if rcon.is_head_rank: mesh_data = rcon.distribute_mesh(mesh) else: mesh_data = rcon.receive_mesh() class CurrentSource: shape = (3, ) def __call__(self, x, el): return [0, 0, exp(-80 * la.norm(x))] order = 3 final_time = 1e-8 discr = rcon.make_discretization(mesh_data, order=order, debug=["cuda_no_plan"]) from grudge.visualization import VtkVisualizer if write_output: vis = VtkVisualizer(discr, rcon, join(output_dir, "em-%d" % order)) if rcon.is_head_rank: print("order %d" % order) print("#elements=", len(mesh.elements)) from grudge.mesh import BTAG_ALL, BTAG_NONE from grudge.models.em import TMMaxwellOperator from grudge.data import make_tdep_given, TimeIntervalGivenFunction op = TMMaxwellOperator(epsilon, mu, flux_type=1, current=TimeIntervalGivenFunction( make_tdep_given(CurrentSource()), off_time=final_time / 10), absorb_tag=BTAG_ALL, pec_tag=BTAG_NONE) fields = op.assemble_eh(discr=discr) from grudge.timestep import LSRK4TimeStepper stepper = LSRK4TimeStepper() from time import time last_tstep = time() t = 0 # diagnostics setup --------------------------------------------------- from pytools.log import LogManager, add_general_quantities, \ add_simulation_quantities, add_run_info if write_output: log_file_name = join(output_dir, "maxwell-%d.dat" % order) else: log_file_name = None logmgr = LogManager(log_file_name, "w", rcon.communicator) add_run_info(logmgr) add_general_quantities(logmgr) add_simulation_quantities(logmgr) discr.add_instrumentation(logmgr) stepper.add_instrumentation(logmgr) from pytools.log import IntervalTimer vis_timer = IntervalTimer("t_vis", "Time spent visualizing") logmgr.add_quantity(vis_timer) from grudge.log import EMFieldGetter, add_em_quantities field_getter = EMFieldGetter(discr, op, lambda: fields) add_em_quantities(logmgr, op, field_getter) logmgr.add_watches( ["step.max", "t_sim.max", ("W_field", "W_el+W_mag"), "t_step.max"]) # timestep loop ------------------------------------------------------- rhs = op.bind(discr) try: from grudge.timestep import times_and_steps step_it = times_and_steps( final_time=final_time, logmgr=logmgr, max_dt_getter=lambda t: op.estimate_timestep( discr, stepper=stepper, t=t, fields=fields)) for step, t, dt in step_it: if step % 10 == 0 and write_output: e, h = op.split_eh(fields) visf = vis.make_file( join(output_dir, "em-%d-%04d" % (order, step))) vis.add_data(visf, [ ("e", discr.convert_volume(e, "numpy")), ("h", discr.convert_volume(h, "numpy")), ], time=t, step=step) visf.close() fields = stepper(fields, t, dt, rhs) assert discr.norm(fields) < 0.03 finally: if write_output: vis.close() logmgr.close() discr.close()