def main(): logmgr = LogManager("mylog.dat", "w") # , comm=... # set a run property logmgr.set_constant("myconst", uniform(0, 1)) add_run_info(logmgr) add_general_quantities(logmgr) add_simulation_quantities(logmgr) vis_timer = IntervalTimer("t_vis", "Time spent visualizing") logmgr.add_quantity(vis_timer) logmgr.add_quantity(Fifteen("fifteen")) logmgr.add_watches(["step.max", "t_sim.max", "t_step.max"]) for istep in range(200): logmgr.tick_before() dt = uniform(0.01, 0.1) set_dt(logmgr, dt) sleep(dt) # Illustrate custom timers if istep % 10 == 0: with vis_timer.start_sub_timer(): sleep(0.05) # Illustrate warnings capture if uniform(0, 1) < 0.05: warn("Oof. Something went awry.") logmgr.tick_after() logmgr.close()
def logmgr_add_many_discretization_quantities(logmgr: LogManager, discr, dim, extract_vars_for_logging, units_for_logging): """Add default discretization quantities to the logmgr.""" for op in ["min", "max", "L2_norm"]: for quantity in ["pressure", "temperature"]: logmgr.add_quantity( DiscretizationBasedQuantity(discr, quantity, op, extract_vars_for_logging, units_for_logging)) for quantity in ["mass", "energy"]: logmgr.add_quantity( DiscretizationBasedQuantity(discr, quantity, op, extract_vars_for_logging, units_for_logging)) for d in range(dim): logmgr.add_quantity( DiscretizationBasedQuantity(discr, "momentum", op, extract_vars_for_logging, units_for_logging, axis=d))
def initialize_logmgr(enable_logmgr: bool, filename: str = None, mode: str = "wu", mpi_comm=None) -> LogManager: """Create and initialize a mirgecom-specific :class:`logpyle.LogManager`.""" if not enable_logmgr: return None logmgr = LogManager(filename=filename, mode=mode, mpi_comm=mpi_comm) add_run_info(logmgr) add_package_versions(logmgr) add_general_quantities(logmgr) add_simulation_quantities(logmgr) try: logmgr.add_quantity(PythonMemoryUsage()) except ImportError: from warnings import warn warn("psutil module not found, not tracking memory consumption." "Install it with 'pip install psutil'") return logmgr
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 logmgr_add_device_memory_usage(logmgr: LogManager, queue: cl.CommandQueue): """Add the OpenCL device memory usage to the log.""" if not (queue.device.type & cl.device_type.GPU): return logmgr.add_quantity(DeviceMemoryUsage())
add_run_info if write_output: log_file_name = "burgers.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, p=1, name="l1_u")) logmgr.add_watches(["step.max", "t_sim.max", "l1_u", "t_step.max"]) # timestep loop ----------------------------------------------------------- rhs = op.bind(discr) from grudge.timestep.runge_kutta import ODE45TimeStepper, LSRK4TimeStepper stepper = ODE45TimeStepper() stepper.add_instrumentation(logmgr) try: from grudge.timestep import times_and_steps # for visc=0.01 #stab_fac = 0.1 # RK4
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 simple_wave_entrypoint(dim=2, num_elems=256, order=4, num_steps=30, log_filename="grudge.dat"): cl_ctx = cl.create_some_context() queue = cl.CommandQueue(cl_ctx) from mpi4py import MPI comm = MPI.COMM_WORLD num_parts = comm.Get_size() n = int(num_elems ** (1./dim)) from meshmode.distributed import MPIMeshDistributor mesh_dist = MPIMeshDistributor(comm) if mesh_dist.is_mananger_rank(): from meshmode.mesh.generation import generate_regular_rect_mesh mesh = generate_regular_rect_mesh(a=(-0.5,)*dim, b=(0.5,)*dim, n=(n,)*dim) from pymetis import part_graph _, p = part_graph(num_parts, xadj=mesh.nodal_adjacency.neighbors_starts.tolist(), adjncy=mesh.nodal_adjacency.neighbors.tolist()) part_per_element = np.array(p) local_mesh = mesh_dist.send_mesh_parts(mesh, part_per_element, num_parts) else: local_mesh = mesh_dist.receive_mesh_part() vol_discr = DGDiscretizationWithBoundaries(cl_ctx, local_mesh, order=order, mpi_communicator=comm) source_center = np.array([0.1, 0.22, 0.33])[:local_mesh.dim] source_width = 0.05 source_omega = 3 sym_x = sym.nodes(local_mesh.dim) sym_source_center_dist = sym_x - source_center sym_t = sym.ScalarVariable("t") from grudge.models.wave import StrongWaveOperator from meshmode.mesh import BTAG_ALL, BTAG_NONE op = StrongWaveOperator(-0.1, vol_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 join_fields fields = join_fields(vol_discr.zeros(queue), [vol_discr.zeros(queue) for i in range(vol_discr.dim)]) from logpyle import LogManager, \ add_general_quantities, \ add_run_info, \ IntervalTimer, EventCounter # NOTE: LogManager hangs when using a file on a shared directory. logmgr = LogManager(log_filename, "w", comm) add_run_info(logmgr) add_general_quantities(logmgr) log_quantities =\ {"rank_data_swap_timer": IntervalTimer("rank_data_swap_timer", "Time spent evaluating RankDataSwapAssign"), "rank_data_swap_counter": EventCounter("rank_data_swap_counter", "Number of RankDataSwapAssign instructions evaluated"), "exec_timer": IntervalTimer("exec_timer", "Total time spent executing instructions"), "insn_eval_timer": IntervalTimer("insn_eval_timer", "Time spend evaluating instructions"), "future_eval_timer": IntervalTimer("future_eval_timer", "Time spent evaluating futures"), "busy_wait_timer": IntervalTimer("busy_wait_timer", "Time wasted doing busy wait")} for quantity in log_quantities.values(): logmgr.add_quantity(quantity) bound_op = bind(vol_discr, op.sym_operator()) def rhs(t, w): val, rhs.profile_data = bound_op(queue, profile_data=rhs.profile_data, log_quantities=log_quantities, t=t, w=w) return val rhs.profile_data = {} dt = 0.04 dt_stepper = set_up_rk4("w", dt, fields, rhs) logmgr.tick_before() for event in dt_stepper.run(t_end=dt * num_steps): if isinstance(event, dt_stepper.StateComputed): logmgr.tick_after() logmgr.tick_before() logmgr.tick_after() def print_profile_data(data): print("""execute() for rank %d: \tInstruction Evaluation: %f%% \tFuture Evaluation: %f%% \tBusy Wait: %f%% \tTotal: %f seconds""" % (comm.Get_rank(), data['insn_eval_time'] / data['total_time'] * 100, data['future_eval_time'] / data['total_time'] * 100, data['busy_wait_time'] / data['total_time'] * 100, data['total_time'])) print_profile_data(rhs.profile_data) logmgr.close()
def mpi_communication_entrypoint(): cl_ctx = cl.create_some_context() queue = cl.CommandQueue(cl_ctx) actx = PyOpenCLArrayContext(queue) from mpi4py import MPI comm = MPI.COMM_WORLD i_local_rank = comm.Get_rank() num_parts = comm.Get_size() from meshmode.distributed import MPIMeshDistributor, get_partition_by_pymetis mesh_dist = MPIMeshDistributor(comm) dim = 2 dt = 0.04 order = 4 if mesh_dist.is_mananger_rank(): from meshmode.mesh.generation import generate_regular_rect_mesh mesh = generate_regular_rect_mesh(a=(-0.5, ) * dim, b=(0.5, ) * dim, nelements_per_axis=(16, ) * dim) part_per_element = get_partition_by_pymetis(mesh, num_parts) local_mesh = mesh_dist.send_mesh_parts(mesh, part_per_element, num_parts) else: local_mesh = mesh_dist.receive_mesh_part() vol_discr = DiscretizationCollection(actx, local_mesh, order=order, mpi_communicator=comm) source_center = np.array([0.1, 0.22, 0.33])[:local_mesh.dim] source_width = 0.05 source_omega = 3 sym_x = sym.nodes(local_mesh.dim) sym_source_center_dist = sym_x - source_center sym_t = sym.ScalarVariable("t") from grudge.models.wave import WeakWaveOperator from meshmode.mesh import BTAG_ALL, BTAG_NONE op = WeakWaveOperator( 0.1, vol_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( vol_discr.zeros(actx), [vol_discr.zeros(actx) for i in range(vol_discr.dim)]) # FIXME # dt = op.estimate_rk4_timestep(vol_discr, fields=fields) # FIXME: Should meshmode consider BTAG_PARTITION to be a boundary? # Fails because: "found faces without boundary conditions" # op.check_bc_coverage(local_mesh) from logpyle import LogManager, \ add_general_quantities, \ add_run_info, \ IntervalTimer, EventCounter log_filename = None # NOTE: LogManager hangs when using a file on a shared directory. # log_filename = "grudge_log.dat" logmgr = LogManager(log_filename, "w", comm) add_run_info(logmgr) add_general_quantities(logmgr) log_quantities =\ {"rank_data_swap_timer": IntervalTimer("rank_data_swap_timer", "Time spent evaluating RankDataSwapAssign"), "rank_data_swap_counter": EventCounter("rank_data_swap_counter", "Number of RankDataSwapAssign instructions evaluated"), "exec_timer": IntervalTimer("exec_timer", "Total time spent executing instructions"), "insn_eval_timer": IntervalTimer("insn_eval_timer", "Time spend evaluating instructions"), "future_eval_timer": IntervalTimer("future_eval_timer", "Time spent evaluating futures"), "busy_wait_timer": IntervalTimer("busy_wait_timer", "Time wasted doing busy wait")} for quantity in log_quantities.values(): logmgr.add_quantity(quantity) logger.debug("\n%s", sym.pretty(op.sym_operator())) bound_op = bind(vol_discr, op.sym_operator()) def rhs(t, w): val, rhs.profile_data = bound_op(profile_data=rhs.profile_data, log_quantities=log_quantities, t=t, w=w) return val rhs.profile_data = {} dt_stepper = set_up_rk4("w", dt, fields, rhs) final_t = 4 nsteps = int(final_t / dt) logger.info("[%04d] dt %.5e nsteps %4d", i_local_rank, dt, nsteps) # from grudge.shortcuts import make_visualizer # vis = make_visualizer(vol_discr, vis_order=order) step = 0 norm = bind(vol_discr, sym.norm(2, sym.var("u"))) from time import time t_last_step = time() logmgr.tick_before() for event in dt_stepper.run(t_end=final_t): if isinstance(event, dt_stepper.StateComputed): assert event.component_id == "w" step += 1 logger.debug("[%04d] t = %.5e |u| = %.5e ellapsed %.5e", step, event.t, norm(u=event.state_component[0]), time() - t_last_step) # if step % 10 == 0: # vis.write_vtk_file("rank%d-fld-%04d.vtu" % (i_local_rank, step), # [("u", event.state_component[0]), # ("v", event.state_component[1:])]) t_last_step = time() logmgr.tick_after() logmgr.tick_before() logmgr.tick_after() def print_profile_data(data): logger.info( """execute() for rank %d:\n \tInstruction Evaluation: %g\n \tFuture Evaluation: %g\n \tBusy Wait: %g\n \tTotal: %g seconds""", i_local_rank, data["insn_eval_time"] / data["total_time"] * 100, data["future_eval_time"] / data["total_time"] * 100, data["busy_wait_time"] / data["total_time"] * 100, data["total_time"]) print_profile_data(rhs.profile_data) logmgr.close() logger.info("Rank %d exiting", i_local_rank)
def main(write_output=True): from grudge.timestep.runge_kutta import LSRK4TimeStepper from math import sqrt, pi, exp 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 c = 1 / sqrt(mu * epsilon) pml_width = 0.5 #mesh = make_mesh(a=np.array((-1,-1,-1)), b=np.array((1,1,1)), #mesh = make_mesh(a=np.array((-3,-3)), b=np.array((3,3)), mesh = make_mesh( a=np.array((-1, -1)), b=np.array((1, 1)), #mesh = make_mesh(a=np.array((-2,-2)), b=np.array((2,2)), pml_width=pml_width, max_volume=0.01) if rcon.is_head_rank: mesh_data = rcon.distribute_mesh(mesh) else: mesh_data = rcon.receive_mesh() class Current: def volume_interpolant(self, t, discr): from grudge.tools import make_obj_array result = discr.volume_zeros(kind="numpy", dtype=np.float64) omega = 6 * c if omega * t > 2 * pi: return make_obj_array([result, result, result]) x = make_obj_array(discr.nodes.T) r = np.sqrt(np.dot(x, x)) idx = r < 0.3 result[idx] = (1+np.cos(pi*r/0.3))[idx] \ *np.sin(omega*t)**3 result = discr.convert_volume(result, kind=discr.compute_kind, dtype=discr.default_scalar_type) return make_obj_array([-result, result, result]) order = 3 discr = rcon.make_discretization(mesh_data, order=order, debug=["cuda_no_plan"]) from grudge.visualization import VtkVisualizer if write_output: vis = VtkVisualizer(discr, rcon, "em-%d" % order) from grudge.mesh import BTAG_ALL, BTAG_NONE from grudge.data import GivenFunction, TimeHarmonicGivenFunction, TimeIntervalGivenFunction from grudge.models.em import MaxwellOperator from grudge.models.pml import \ AbarbanelGottliebPMLMaxwellOperator, \ AbarbanelGottliebPMLTMMaxwellOperator, \ AbarbanelGottliebPMLTEMaxwellOperator op = AbarbanelGottliebPMLTEMaxwellOperator(epsilon, mu, flux_type=1, current=Current(), pec_tag=BTAG_ALL, absorb_tag=BTAG_NONE, add_decay=True) fields = op.assemble_ehpq(discr=discr) stepper = LSRK4TimeStepper() if rcon.is_head_rank: print("order %d" % order) print("#elements=", len(mesh.elements)) # diagnostics setup --------------------------------------------------- from logpyle import LogManager, add_general_quantities, \ add_simulation_quantities, add_run_info if write_output: log_file_name = "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 logpyle 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"]) from grudge.log import LpNorm class FieldIdxGetter: def __init__(self, whole_getter, idx): self.whole_getter = whole_getter self.idx = idx def __call__(self): return self.whole_getter()[self.idx] # timestep loop ------------------------------------------------------- t = 0 pml_coeff = op.coefficients_from_width(discr, width=pml_width) rhs = op.bind(discr, pml_coeff) try: from grudge.timestep import times_and_steps step_it = times_and_steps( final_time=4 / c, 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, p, q = op.split_ehpq(fields) visf = vis.make_file("em-%d-%04d" % (order, step)) #pml_rhs_e, pml_rhs_h, pml_rhs_p, pml_rhs_q = \ #op.split_ehpq(rhs(t, fields)) j = Current().volume_interpolant(t, discr) vis.add_data( visf, [ ("e", discr.convert_volume(e, "numpy")), ("h", discr.convert_volume(h, "numpy")), ("p", discr.convert_volume(p, "numpy")), ("q", discr.convert_volume(q, "numpy")), ("j", discr.convert_volume(j, "numpy")), #("pml_rhs_e", pml_rhs_e), #("pml_rhs_h", pml_rhs_h), #("pml_rhs_p", pml_rhs_p), #("pml_rhs_q", pml_rhs_q), #("max_rhs_e", max_rhs_e), #("max_rhs_h", max_rhs_h), #("max_rhs_p", max_rhs_p), #("max_rhs_q", max_rhs_q), ], time=t, step=step) visf.close() fields = stepper(fields, t, dt, rhs) _, _, energies_data = logmgr.get_expr_dataset("W_el+W_mag") energies = [value for tick_nbr, value in energies_data] assert energies[-1] < max(energies) * 1e-2 finally: logmgr.close() if write_output: vis.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 logpyle 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 logpyle 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()
def main(): from grudge.backends import guess_run_context rcon = guess_run_context(["cuda"]) if rcon.is_head_rank: mesh = make_boxmesh() #from grudge.mesh import make_rect_mesh #mesh = make_rect_mesh( # boundary_tagger=lambda fvi, el, fn, all_v: ["inflow"]) mesh_data = rcon.distribute_mesh(mesh) else: mesh_data = rcon.receive_mesh() for order in [3]: from pytools import add_python_path_relative_to_script add_python_path_relative_to_script("..") from gas_dynamics_initials import UniformMachFlow box = UniformMachFlow(angle_of_attack=0) from grudge.models.gas_dynamics import GasDynamicsOperator op = GasDynamicsOperator(dimensions=3, gamma=box.gamma, mu=box.mu, prandtl=box.prandtl, spec_gas_const=box.spec_gas_const, bc_inflow=box, bc_outflow=box, bc_noslip=box, inflow_tag="inflow", outflow_tag="outflow", noslip_tag="noslip") discr = rcon.make_discretization(mesh_data, order=order, debug=[ #"cuda_no_plan", #"cuda_dump_kernels", #"dump_dataflow_graph", #"dump_optemplate_stages", #"dump_dataflow_graph", #"print_op_code", "cuda_no_plan_el_local", ], default_scalar_type=numpy.float32, tune_for=op.sym_operator()) from grudge.visualization import SiloVisualizer, VtkVisualizer # noqa #vis = VtkVisualizer(discr, rcon, "shearflow-%d" % order) vis = SiloVisualizer(discr, rcon) fields = box.volume_interpolant(0, discr) navierstokes_ex = op.bind(discr) max_eigval = [0] def rhs(t, q): ode_rhs, speed = navierstokes_ex(t, q) max_eigval[0] = speed return ode_rhs rhs(0, fields) if rcon.is_head_rank: print("---------------------------------------------") print("order %d" % order) print("---------------------------------------------") print("#elements=", len(mesh.elements)) from grudge.timestep import RK4TimeStepper stepper = RK4TimeStepper() # diagnostics setup --------------------------------------------------- from logpyle import LogManager, add_general_quantities, \ add_simulation_quantities, add_run_info logmgr = LogManager("navierstokes-%d.dat" % order, "w", rcon.communicator) add_run_info(logmgr) add_general_quantities(logmgr) add_simulation_quantities(logmgr) discr.add_instrumentation(logmgr) stepper.add_instrumentation(logmgr) logmgr.add_watches(["step.max", "t_sim.max", "t_step.max"]) from logpyle import LogQuantity class ChangeSinceLastStep(LogQuantity): """Records the change of a variable between a time step and the previous one""" def __init__(self, name="change"): LogQuantity.__init__(self, name, "1", "Change since last time step") self.old_fields = 0 def __call__(self): result = discr.norm(fields - self.old_fields) self.old_fields = fields return result logmgr.add_quantity(ChangeSinceLastStep()) # timestep loop ------------------------------------------------------- try: from grudge.timestep import times_and_steps step_it = times_and_steps( final_time=200, #max_steps=500, logmgr=logmgr, max_dt_getter=lambda t: op.estimate_timestep(discr, stepper=stepper, t=t, max_eigenvalue=max_eigval[0])) for step, t, dt in step_it: if step % 200 == 0: #if False: visf = vis.make_file("box-%d-%06d" % (order, step)) #rhs_fields = rhs(t, fields) vis.add_data(visf, [ ("rho", discr.convert_volume( op.rho(fields), kind="numpy")), ("e", discr.convert_volume( op.e(fields), kind="numpy")), ("rho_u", discr.convert_volume( op.rho_u(fields), kind="numpy")), ("u", discr.convert_volume( op.u(fields), kind="numpy")), # ("rhs_rho", discr.convert_volume( # op.rho(rhs_fields), kind="numpy")), # ("rhs_e", discr.convert_volume( # op.e(rhs_fields), kind="numpy")), # ("rhs_rho_u", discr.convert_volume( # op.rho_u(rhs_fields), kind="numpy")), ], expressions=[ ("p", "(0.4)*(e- 0.5*(rho_u*u))"), ], time=t, step=step ) visf.close() fields = stepper(fields, t, dt, rhs) finally: vis.close() logmgr.save() discr.close()