def energies(path, kkind="iso"): """ This loops over all times of a given run and return the magnetic and kinetic energy as a function of time. """ r = Run(path) times = get_times_from_h5(r.path+"/EM_B.h5") Bnrj = np.zeros_like(times) K = np.zeros_like(times) for it,t in enumerate(times): B = r.GetB(t) protons = r.GetParticles(t, "protons") Bnrj[it] = mag_energy(B) K[it] = total_kinetic(protons,kind=kkind) return r, Bnrj, K, times
def test_restarts(self, dim, interp, simInput): print(f"test_restarts dim/interp:{dim}/{interp}") simput = copy.deepcopy(simInput) for key in ["cells", "dl", "boundary_types"]: simput[key] = [simput[key]] * dim if "refinement" not in simput: b0 = [[10 for i in range(dim)], [19 for i in range(dim)]] simput["refinement_boxes"] = {"L0": {"B0": b0}} else: # https://github.com/LLNL/SAMRAI/issues/199 # tagging can handle more than one timestep as it does not # appear subject to regridding issues, so we make more timesteps # to confirm simulations are still equivalent simput["time_step_nbr"] = 10 # if restart time exists it "loads" from restart file # otherwise just saves restart files based on timestamps assert "restart_time" not in simput["restart_options"] simput["interp_order"] = interp time_step = simput["time_step"] time_step_nbr = simput["time_step_nbr"] restart_idx = 4 restart_time = time_step * restart_idx timestamps = [time_step * restart_idx, time_step * time_step_nbr] # first simulation local_out = f"{out}/test/{dim}/{interp}/mpi_n/{cpp.mpi_size()}/id{self.ddt_test_id()}" simput["restart_options"]["dir"] = local_out simput["diag_options"]["options"]["dir"] = local_out ph.global_vars.sim = None ph.global_vars.sim = ph.Simulation(**simput) assert "restart_time" not in ph.global_vars.sim.restart_options model = setup_model() dump_all_diags(model.populations, timestamps=np.array(timestamps)) Simulator(ph.global_vars.sim).run().reset() self.register_diag_dir_for_cleanup(local_out) diag_dir0 = local_out # second restarted simulation local_out = f"{local_out}_n2" simput["diag_options"]["options"]["dir"] = local_out simput["restart_options"]["restart_time"] = restart_time ph.global_vars.sim = None ph.global_vars.sim = ph.Simulation(**simput) assert "restart_time" in ph.global_vars.sim.restart_options model = setup_model() dump_all_diags(model.populations, timestamps=np.array(timestamps)) Simulator(ph.global_vars.sim).run().reset() self.register_diag_dir_for_cleanup(local_out) diag_dir1 = local_out def check(qty0, qty1, checker): checks = 0 for ilvl, lvl0 in qty0.patch_levels.items(): patch_level1 = qty1.patch_levels[ilvl] for p_idx, patch0 in enumerate(lvl0): patch1 = patch_level1.patches[p_idx] for pd_key, pd0 in patch0.patch_datas.items(): pd1 = patch1.patch_datas[pd_key] self.assertNotEqual(id(pd0), id(pd1)) checker(pd0, pd1) checks += 1 return checks def check_particles(qty0, qty1): return check( qty0, qty1, lambda pd0, pd1: self.assertEqual(pd0.dataset, pd1.dataset)) def check_field(qty0, qty1): return check( qty0, qty1, lambda pd0, pd1: np.testing.assert_equal( pd0.dataset[:], pd1.dataset[:])) def count_levels_and_patches(qty): n_levels = len(qty.patch_levels) n_patches = 0 for ilvl, lvl in qty.patch_levels.items(): n_patches += len(qty.patch_levels[ilvl].patches) return n_levels, n_patches n_quantities_per_patch = 20 pops = model.populations for time in timestamps: checks = 0 run0 = Run(diag_dir0) run1 = Run(diag_dir1) checks += check_particles(run0.GetParticles(time, pops), run1.GetParticles(time, pops)) checks += check_field(run0.GetB(time), run1.GetB(time)) checks += check_field(run0.GetE(time), run1.GetE(time)) checks += check_field(run0.GetNi(time), run1.GetNi(time)) checks += check_field(run0.GetVi(time), run1.GetVi(time)) for pop in pops: checks += check_field(run0.GetFlux(time, pop), run1.GetFlux(time, pop)) checks += check_field(run0.GetN(time, pop), run1.GetN(time, pop)) n_levels, n_patches = count_levels_and_patches(run0.GetB(time)) self.assertEqual(n_levels, 2) # at least 2 levels self.assertGreaterEqual(n_patches, n_levels) # at least one patch per level self.assertEqual(checks, n_quantities_per_patch * n_patches)