Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Jan 18, 2025
1 parent 175c459 commit 69bc76b
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 18 deletions.
1 change: 1 addition & 0 deletions tests/functional/harris/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
29 changes: 18 additions & 11 deletions tests/functional/harris/harris_2d.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
import os

import numpy as np

import pyphare.pharein as ph
Expand All @@ -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,
Expand All @@ -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]
Expand Down
17 changes: 10 additions & 7 deletions tests/functional/harris/harris_2d_100_x_100.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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",
Expand All @@ -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)
Expand Down
141 changes: 141 additions & 0 deletions tests/functional/harris/harris_2d_100_x_100_slow.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 69bc76b

Please sign in to comment.