From 69bc76bb704f0d9af95110966047b88f9cd6ece0 Mon Sep 17 00:00:00 2001 From: dekken Date: Sat, 18 Jan 2025 17:32:50 +0100 Subject: [PATCH] ++ --- tests/functional/harris/CMakeLists.txt | 1 + tests/functional/harris/harris_2d.py | 29 ++-- .../functional/harris/harris_2d_100_x_100.py | 17 ++- .../harris/harris_2d_100_x_100_slow.py | 141 ++++++++++++++++++ 4 files changed, 170 insertions(+), 18 deletions(-) create mode 100644 tests/functional/harris/harris_2d_100_x_100_slow.py diff --git a/tests/functional/harris/CMakeLists.txt b/tests/functional/harris/CMakeLists.txt index d6708cac0..1cbb8723f 100644 --- a/tests/functional/harris/CMakeLists.txt +++ b/tests/functional/harris/CMakeLists.txt @@ -14,6 +14,7 @@ if(HighFive AND testMPI) if(testMPI) phare_mpi_python3_exec(11 4 harris_2d_100_x_100 harris_2d_100_x_100.py ${CMAKE_CURRENT_BINARY_DIR}) + phare_mpi_python3_exec(11 4 harris_2d_100_x_100_slow harris_2d_100_x_100_slow.py ${CMAKE_CURRENT_BINARY_DIR}) endif(testMPI) diff --git a/tests/functional/harris/harris_2d.py b/tests/functional/harris/harris_2d.py index 8c824c96b..9ffd88576 100644 --- a/tests/functional/harris/harris_2d.py +++ b/tests/functional/harris/harris_2d.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -import os + import numpy as np import pyphare.pharein as ph @@ -8,21 +8,26 @@ from pyphare.simulator.simulator import startMPI -def default_setup(): +ph.NO_GUI() +cpp = cpp_lib() - ph.NO_GUI() - cpp = cpp_lib() - startMPI() - diag_outputs = "phare_outputs/test/harris/2d" - time_step_nbr = 1000 - time_step = 0.001 - final_time = time_step * time_step_nbr +diag_outputs = "phare_outputs/test/harris/2d" +time_step_nbr = 1000 +time_step = 0.001 +final_time = time_step * time_step_nbr + + +def default_timestamps(): dt = 10 * time_step nt = final_time / dt + 1 timestamps = dt * np.arange(nt) + +def default_setup(): + startMPI() + return ph.Simulation( smallest_patch_size=15, largest_patch_size=25, @@ -43,9 +48,11 @@ def default_setup(): ) -def config(sim = None, seed = 12334): - if not sim: +def config(sim = None, timestamps = None, seed = 12334): + if sim is None: sim = default_setup() + if timestamps is None: + timestamps = default_timestamps() def density(x, y): L = sim.simulation_domain()[1] diff --git a/tests/functional/harris/harris_2d_100_x_100.py b/tests/functional/harris/harris_2d_100_x_100.py index 0c638f525..041155be8 100644 --- a/tests/functional/harris/harris_2d_100_x_100.py +++ b/tests/functional/harris/harris_2d_100_x_100.py @@ -35,14 +35,10 @@ startMPI() cells = (100, 100) -time_steps = 1000 +final_time = 50 time_step = 0.005 -final_time = time_step * time_steps timestamps = np.arange(0, final_time + time_step, final_time / 5) -if cpp.mpi_rank() == 0: - print(LOAD_BALANCE, "diag timestamps:", timestamps) - diag_dir = "phare_outputs/harris_2d_100_x_100" plot_dir = Path(f"{diag_dir}_plots") plot_dir.mkdir(parents=True, exist_ok=True) @@ -55,7 +51,7 @@ def config(): cells=cells, dl=(0.40, 0.40), # refinement="tagging", - max_nbr_levels=1, + # max_nbr_levels=1, nesting_buffer=1, clustering="tile", tag_buffer="1", @@ -67,7 +63,14 @@ def config(): }, ) - sim = base.config(sim) + sim = base.config(sim, timestamps) + + for quantity in ["density", "bulkVelocity"]: + ph.FluidDiagnostics(quantity=quantity, write_timestamps=timestamps) + + ph.FluidDiagnostics( + quantity="density", write_timestamps=timestamps, population_name="protons" + ) if LOAD_BALANCE: ph.LoadBalancer(active=True, auto=True, mode="nppc", tol=0.05) diff --git a/tests/functional/harris/harris_2d_100_x_100_slow.py b/tests/functional/harris/harris_2d_100_x_100_slow.py new file mode 100644 index 000000000..3da5869d3 --- /dev/null +++ b/tests/functional/harris/harris_2d_100_x_100_slow.py @@ -0,0 +1,141 @@ +#!/usr/bin/env python3 + +import os +import numpy as np +import matplotlib as mpl +from pathlib import Path + +import pyphare.pharein as ph +from pyphare.cpp import cpp_lib +from pyphare.pharesee.run import Run +from pyphare.simulator.simulator import Simulator, startMPI + +from tests.simulator import SimulatorTest +from tools.python3 import plotting as m_plotting + +import harris_2d as base + +mpl.use("Agg") + +SCOPE_TIMING = os.getenv("PHARE_SCOPE_TIMING", "False").lower() in ("true", "1", "t") +""" + For scope timings to work + The env var PHARE_SCOPE_TIMING must be == "1" (or "true") + See src/phare/phare.hpp + CMake must be configured with: -DwithPhlop=ON + And a LOG_LEVEL must be defined via compile args: -DPHARE_LOG_LEVEL=1 + Or change the default value in src/core/logger.hpp + And phlop must be available on PYTHONPATH either from subprojects + or install phlop via pip +""" + +LOAD_BALANCE = os.getenv("LOAD_BALANCE", "True").lower() in ("true", "1", "t") + +cpp = cpp_lib() +startMPI() + +cells = (100, 100) +final_time = 50 +time_step = 0.001 +timestamps = np.arange(0, final_time + time_step, final_time / 5) + +diag_dir = "phare_outputs/harris_2d_100_x_100_slow" +plot_dir = Path(f"{diag_dir}_plots") +plot_dir.mkdir(parents=True, exist_ok=True) + + +def config(): + sim = ph.Simulation( + time_step=time_step, + final_time=final_time, + cells=cells, + dl=(0.40, 0.40), + # refinement="tagging", + # max_nbr_levels=1, + nesting_buffer=1, + clustering="tile", + tag_buffer="1", + hyper_resistivity=0.002, + resistivity=0.001, + diag_options={ + "format": "phareh5", + "options": {"dir": diag_dir, "mode": "overwrite"}, + }, + ) + + sim = base.config(sim, timestamps) + + for quantity in ["density", "bulkVelocity"]: + ph.FluidDiagnostics(quantity=quantity, write_timestamps=timestamps) + + ph.FluidDiagnostics( + quantity="density", write_timestamps=timestamps, population_name="protons" + ) + + if LOAD_BALANCE: + ph.LoadBalancer(active=True, auto=True, mode="nppc", tol=0.05) + + return sim + + +def plot_file_for_qty(qty, time): + return f"{plot_dir}/harris_{qty}_t{time}.png" + + +def plot(diag_dir): + run = Run(diag_dir) + for time in timestamps: + run.GetDivB(time).plot( + filename=plot_file_for_qty("divb", time), + plot_patches=True, + vmin=1e-11, + vmax=2e-10, + ) + run.GetRanks(time).plot( + filename=plot_file_for_qty("Ranks", time), + plot_patches=True, + ) + run.GetN(time, pop_name="protons").plot( + filename=plot_file_for_qty("N", time), + plot_patches=True, + ) + for c in ["x", "y", "z"]: + run.GetB(time).plot( + filename=plot_file_for_qty(f"b{c}", time), + qty=f"{c}", + plot_patches=True, + ) + run.GetJ(time).plot( + filename=plot_file_for_qty("jz", time), + qty="z", + plot_patches=True, + vmin=-2, + vmax=2, + ) + + +class HarrisTest(SimulatorTest): + def __init__(self, *args, **kwargs): + super(HarrisTest, self).__init__(*args, **kwargs) + self.simulator = None + + def tearDown(self): + super(HarrisTest, self).tearDown() + if self.simulator is not None: + self.simulator.reset() + self.simulator = None + ph.global_vars.sim = None + + def test_run(self): + self.register_diag_dir_for_cleanup(diag_dir) + Simulator(config()).run().reset() + if cpp.mpi_rank() == 0: + plot(diag_dir) + if SCOPE_TIMING: + m_plotting.plot_run_timer_data(diag_dir, cpp.mpi_rank()) + cpp.mpi_barrier() + return self + + +if __name__ == "__main__": + HarrisTest().test_run().tearDown()