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 my_post_step(step, t, dt, state): # Logmgr needs to know about EOS, dt, dim? # imo this is a design/scope flaw if logmgr: set_dt(logmgr, dt) set_sim_state(logmgr, dim, state, eos) logmgr.tick_after() return state, dt
def my_post_step(step, t, dt, state): cv, tseed = state fluid_state = construct_fluid_state(cv, tseed) # Logmgr needs to know about EOS, dt, dim? # imo this is a design/scope flaw if logmgr: set_dt(logmgr, dt) set_sim_state(logmgr, dim, cv, gas_model.eos) logmgr.tick_after() return make_obj_array([cv, fluid_state.temperature]), dt
def main(ctx_factory=cl.create_some_context, use_logmgr=True, use_leap=False, use_profiling=False, casename=None, rst_filename=None, actx_class=PyOpenCLArrayContext): """Run the example.""" cl_ctx = cl.create_some_context() queue = cl.CommandQueue(cl_ctx) from mpi4py import MPI comm = MPI.COMM_WORLD num_parts = comm.Get_size() logmgr = initialize_logmgr(use_logmgr, filename="heat-source.sqlite", mode="wu", mpi_comm=comm) if use_profiling: queue = cl.CommandQueue( cl_ctx, properties=cl.command_queue_properties.PROFILING_ENABLE) else: queue = cl.CommandQueue(cl_ctx) actx = actx_class( queue, allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue))) from meshmode.distributed import MPIMeshDistributor, get_partition_by_pymetis mesh_dist = MPIMeshDistributor(comm) dim = 2 nel_1d = 16 t = 0 t_final = 0.0002 istep = 0 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=(nel_1d,)*dim, boundary_tag_to_face={ "dirichlet": ["+x", "-x"], "neumann": ["+y", "-y"] } ) print("%d elements" % mesh.nelements) part_per_element = get_partition_by_pymetis(mesh, num_parts) local_mesh = mesh_dist.send_mesh_parts(mesh, part_per_element, num_parts) del mesh else: local_mesh = mesh_dist.receive_mesh_part() order = 3 discr = EagerDGDiscretization(actx, local_mesh, order=order, mpi_communicator=comm) if dim == 2: # no deep meaning here, just a fudge factor dt = 0.0025/(nel_1d*order**2) else: raise ValueError("don't have a stable time step guesstimate") source_width = 0.2 from arraycontext import thaw nodes = thaw(discr.nodes(), actx) boundaries = { DTAG_BOUNDARY("dirichlet"): DirichletDiffusionBoundary(0.), DTAG_BOUNDARY("neumann"): NeumannDiffusionBoundary(0.) } u = discr.zeros(actx) if logmgr: logmgr_add_device_name(logmgr, queue) logmgr_add_device_memory_usage(logmgr, queue) logmgr.add_watches(["step.max", "t_step.max", "t_log.max"]) try: logmgr.add_watches(["memory_usage_python.max", "memory_usage_gpu.max"]) except KeyError: pass if use_profiling: logmgr.add_watches(["multiply_time.max"]) vis_timer = IntervalTimer("t_vis", "Time spent visualizing") logmgr.add_quantity(vis_timer) vis = make_visualizer(discr) def rhs(t, u): return ( diffusion_operator( discr, quad_tag=DISCR_TAG_BASE, alpha=1, boundaries=boundaries, u=u) + actx.np.exp(-np.dot(nodes, nodes)/source_width**2)) compiled_rhs = actx.compile(rhs) rank = comm.Get_rank() while t < t_final: if logmgr: logmgr.tick_before() if istep % 10 == 0: print(istep, t, actx.to_numpy(actx.np.linalg.norm(u[0]))) vis.write_vtk_file("fld-heat-source-mpi-%03d-%04d.vtu" % (rank, istep), [ ("u", u) ], overwrite=True) u = rk4_step(u, t, dt, compiled_rhs) t += dt istep += 1 if logmgr: set_dt(logmgr, dt) logmgr.tick_after() final_answer = discr.norm(u, np.inf) resid = abs(final_answer - 0.00020620711665201585) if resid > 1e-15: raise ValueError(f"Run did not produce the expected result {resid=}")
def _advance_state_stepper_func(rhs, timestepper, state, t_final, dt=0, t=0.0, istep=0, pre_step_callback=None, post_step_callback=None, logmgr=None, eos=None, dim=None): """Advance state from some time (t) to some time (t_final). Parameters ---------- rhs Function that should return the time derivative of the state. This function should take time and state as arguments, with a call with signature ``rhs(t, state)``. timestepper Function that advances the state from t=time to t=(time+dt), and returns the advanced state. Has a call with signature ``timestepper(state, t, dt, rhs)``. state: numpy.ndarray Agglomerated object array containing at least the state variables that will be advanced by this stepper t_final: float Simulated time at which to stop t: float Time at which to start dt: float Initial timestep size to use, optional if dt is adaptive istep: int Step number from which to start pre_step_callback An optional user-defined function, with signature: ``state, dt = pre_step_callback(step, t, dt, state)``, to be called before the timestepper is called for that particular step. post_step_callback An optional user-defined function, with signature: ``state, dt = post_step_callback(step, t, dt, state)``, to be called after the timestepper is called for that particular step. Returns ------- istep: int the current step number t: float the current time state: numpy.ndarray """ t = np.float64(t) if t_final <= t: return istep, t, state actx = get_container_context_recursively(state) compiled_rhs = _compile_rhs(actx, rhs) while t < t_final: state = _force_evaluation(actx, state) if logmgr: logmgr.tick_before() if pre_step_callback is not None: state, dt = pre_step_callback(state=state, step=istep, t=t, dt=dt) state = timestepper(state=state, t=t, dt=dt, rhs=compiled_rhs) t += dt istep += 1 if post_step_callback is not None: state, dt = post_step_callback(state=state, step=istep, t=t, dt=dt) if logmgr: set_dt(logmgr, dt) set_sim_state(logmgr, dim, state, eos) logmgr.tick_after() return istep, t, state
def _advance_state_stepper_func(rhs, timestepper, checkpoint, get_timestep, state, t_final, t=0.0, istep=0, logmgr=None, eos=None, dim=None): """Advance state from some time (t) to some time (t_final). Parameters ---------- rhs Function that should return the time derivative of the state. This function should take time and state as arguments, with a call with signature ``rhs(t, state)``. timestepper Function that advances the state from t=time to t=(time+dt), and returns the advanced state. Has a call with signature ``timestepper(state, t, dt, rhs)``. checkpoint Function is user-defined and can be used to preform simulation status reporting, viz, and restart i/o. A non-zero return code from this function indicates that this function should stop gracefully. get_timestep Function that should return dt for the next step. This interface allows user-defined adaptive timestepping. A negative return value indicated that the stepper should stop gracefully. Takes only state as an argument. state: numpy.ndarray Agglomerated object array containing at least the state variables that will be advanced by this stepper t_final: float Simulated time at which to stop t: float Time at which to start istep: int Step number from which to start Returns ------- istep: int the current step number t: float the current time state: numpy.ndarray """ if t_final <= t: return istep, t, state while t < t_final: if logmgr: logmgr.tick_before() dt = get_timestep(state=state) if dt < 0: return istep, t, state if checkpoint: checkpoint(state=state, step=istep, t=t, dt=dt) state = timestepper(state=state, t=t, dt=dt, rhs=rhs) t += dt istep += 1 if logmgr: set_dt(logmgr, dt) set_sim_state(logmgr, dim, state, eos) logmgr.tick_after() return istep, t, state
def _advance_state_leap(rhs, timestepper, checkpoint, get_timestep, state, t_final, component_id="state", t=0.0, istep=0, logmgr=None, eos=None, dim=None): """Advance state from some time *t* to some time *t_final* using :mod:`leap`. Parameters ---------- rhs Function that should return the time derivative of the state. This function should take time and state as arguments, with a call with signature ``rhs(t, state)``. timestepper An instance of :class:`leap.MethodBuilder`. checkpoint Function is user-defined and can be used to preform simulation status reporting, viz, and restart i/o. A non-zero return code from this function indicates that this function should stop gracefully. get_timestep Function that should return dt for the next step. This interface allows user-defined adaptive timestepping. A negative return value indicated that the stepper should stop gracefully. state: numpy.ndarray Agglomerated object array containing at least the state variables that will be advanced by this stepper t_final: float Simulated time at which to stop component_id State id (required input for leap method generation) t: float Time at which to start istep: int Step number from which to start Returns ------- istep: int the current step number t: float the current time state: numpy.ndarray """ if t_final <= t: return istep, t, state # Generate code for Leap method. dt = get_timestep(state=state) stepper_cls = generate_singlerate_leap_advancer(timestepper, component_id, rhs, t, dt, state) while t < t_final: if logmgr: logmgr.tick_before() dt = get_timestep(state=state) if dt < 0: return istep, t, state checkpoint(state=state, step=istep, t=t, dt=dt) # Leap interface here is *a bit* different. for event in stepper_cls.run(t_end=t + dt): if isinstance(event, stepper_cls.StateComputed): state = event.state_component t += dt istep += 1 if logmgr: set_dt(logmgr, dt) set_sim_state(logmgr, dim, state, eos) logmgr.tick_after() return istep, t, state
def main(use_profiling=False, use_logmgr=False, lazy_eval: bool = False): """Drive the example.""" cl_ctx = cl.create_some_context() logmgr = initialize_logmgr(use_logmgr, filename="wave.sqlite", mode="wu") if use_profiling: if lazy_eval: raise RuntimeError("Cannot run lazy with profiling.") queue = cl.CommandQueue( cl_ctx, properties=cl.command_queue_properties.PROFILING_ENABLE) actx = PyOpenCLProfilingArrayContext( queue, allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue))) else: queue = cl.CommandQueue(cl_ctx) if lazy_eval: actx = PytatoPyOpenCLArrayContext(queue) else: actx = PyOpenCLArrayContext( queue, allocator=cl_tools.MemoryPool( cl_tools.ImmediateAllocator(queue))) dim = 2 nel_1d = 16 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=(nel_1d, ) * dim) order = 3 discr = EagerDGDiscretization(actx, mesh, order=order) current_cfl = 0.485 wave_speed = 1.0 from grudge.dt_utils import characteristic_lengthscales nodal_dt = characteristic_lengthscales(actx, discr) / wave_speed from grudge.op import nodal_min dt = actx.to_numpy(current_cfl * nodal_min(discr, "vol", nodal_dt))[()] print("%d elements" % mesh.nelements) fields = flat_obj_array(bump(actx, discr), [discr.zeros(actx) for i in range(discr.dim)]) if logmgr: logmgr_add_cl_device_info(logmgr, queue) logmgr_add_device_memory_usage(logmgr, queue) logmgr.add_watches(["step.max", "t_step.max", "t_log.max"]) try: logmgr.add_watches( ["memory_usage_python.max", "memory_usage_gpu.max"]) except KeyError: pass if use_profiling: logmgr.add_watches(["multiply_time.max"]) vis_timer = IntervalTimer("t_vis", "Time spent visualizing") logmgr.add_quantity(vis_timer) vis = make_visualizer(discr) def rhs(t, w): return wave_operator(discr, c=wave_speed, w=w) compiled_rhs = actx.compile(rhs) t = 0 t_final = 1 istep = 0 while t < t_final: if logmgr: logmgr.tick_before() fields = thaw(freeze(fields, actx), actx) fields = rk4_step(fields, t, dt, compiled_rhs) if istep % 10 == 0: if use_profiling: print(actx.tabulate_profiling_data()) print(istep, t, actx.to_numpy(discr.norm(fields[0], np.inf))) vis.write_vtk_file("fld-wave-%04d.vtu" % istep, [ ("u", fields[0]), ("v", fields[1:]), ], overwrite=True) t += dt istep += 1 if logmgr: set_dt(logmgr, dt) logmgr.tick_after()
def main(snapshot_pattern="wave-mpi-{step:04d}-{rank:04d}.pkl", restart_step=None, use_profiling=False, use_logmgr=False, actx_class=PyOpenCLArrayContext): """Drive the example.""" cl_ctx = cl.create_some_context() queue = cl.CommandQueue(cl_ctx) from mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.Get_rank() num_parts = comm.Get_size() logmgr = initialize_logmgr(use_logmgr, filename="wave-mpi.sqlite", mode="wu", mpi_comm=comm) if use_profiling: queue = cl.CommandQueue(cl_ctx, properties=cl.command_queue_properties.PROFILING_ENABLE) actx = actx_class(queue, allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)), logmgr=logmgr) else: queue = cl.CommandQueue(cl_ctx) actx = actx_class(queue, allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue))) if restart_step is None: from meshmode.distributed import MPIMeshDistributor, get_partition_by_pymetis mesh_dist = MPIMeshDistributor(comm) dim = 2 nel_1d = 16 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=(nel_1d,)*dim) print("%d elements" % mesh.nelements) part_per_element = get_partition_by_pymetis(mesh, num_parts) local_mesh = mesh_dist.send_mesh_parts(mesh, part_per_element, num_parts) del mesh else: local_mesh = mesh_dist.receive_mesh_part() fields = None else: from mirgecom.restart import read_restart_data restart_data = read_restart_data( actx, snapshot_pattern.format(step=restart_step, rank=rank) ) local_mesh = restart_data["local_mesh"] nel_1d = restart_data["nel_1d"] assert comm.Get_size() == restart_data["num_parts"] order = 3 discr = EagerDGDiscretization(actx, local_mesh, order=order, mpi_communicator=comm) current_cfl = 0.485 wave_speed = 1.0 from grudge.dt_utils import characteristic_lengthscales dt = current_cfl * characteristic_lengthscales(actx, discr) / wave_speed from grudge.op import nodal_min dt = nodal_min(discr, "vol", dt) t_final = 1 if restart_step is None: t = 0 istep = 0 fields = flat_obj_array( bump(actx, discr), [discr.zeros(actx) for i in range(discr.dim)] ) else: t = restart_data["t"] istep = restart_step assert istep == restart_step restart_fields = restart_data["fields"] old_order = restart_data["order"] if old_order != order: old_discr = EagerDGDiscretization(actx, local_mesh, order=old_order, mpi_communicator=comm) from meshmode.discretization.connection import make_same_mesh_connection connection = make_same_mesh_connection(actx, discr.discr_from_dd("vol"), old_discr.discr_from_dd("vol")) fields = connection(restart_fields) else: fields = restart_fields if logmgr: logmgr_add_cl_device_info(logmgr, queue) logmgr_add_device_memory_usage(logmgr, queue) logmgr.add_watches(["step.max", "t_step.max", "t_log.max"]) try: logmgr.add_watches(["memory_usage_python.max", "memory_usage_gpu.max"]) except KeyError: pass if use_profiling: logmgr.add_watches(["multiply_time.max"]) vis_timer = IntervalTimer("t_vis", "Time spent visualizing") logmgr.add_quantity(vis_timer) vis = make_visualizer(discr) def rhs(t, w): return wave_operator(discr, c=wave_speed, w=w) compiled_rhs = actx.compile(rhs) while t < t_final: if logmgr: logmgr.tick_before() # restart must happen at beginning of step if istep % 100 == 0 and ( # Do not overwrite the restart file that we just read. istep != restart_step): from mirgecom.restart import write_restart_file write_restart_file( actx, restart_data={ "local_mesh": local_mesh, "order": order, "fields": fields, "t": t, "step": istep, "nel_1d": nel_1d, "num_parts": num_parts}, filename=snapshot_pattern.format(step=istep, rank=rank), comm=comm ) if istep % 10 == 0: print(istep, t, discr.norm(fields[0])) vis.write_parallel_vtk_file( comm, "fld-wave-mpi-%03d-%04d.vtu" % (rank, istep), [ ("u", fields[0]), ("v", fields[1:]), ], overwrite=True ) fields = thaw(freeze(fields, actx), actx) fields = rk4_step(fields, t, dt, compiled_rhs) t += dt istep += 1 if logmgr: set_dt(logmgr, dt) logmgr.tick_after() final_soln = discr.norm(fields[0]) assert np.abs(final_soln - 0.04409852463947439) < 1e-14