|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import numpy as np |
| 5 | +import matplotlib as mpl |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import pyphare.pharein as ph |
| 9 | +from pyphare.cpp import cpp_lib |
| 10 | +from pyphare.pharesee.run import Run |
| 11 | +from pyphare.simulator.simulator import Simulator, startMPI |
| 12 | + |
| 13 | +from tests.simulator import SimulatorTest |
| 14 | +from tools.python3 import plotting as m_plotting |
| 15 | + |
| 16 | +import harris_2d as base |
| 17 | + |
| 18 | +mpl.use("Agg") |
| 19 | + |
| 20 | +SCOPE_TIMING = os.getenv("PHARE_SCOPE_TIMING", "False").lower() in ("true", "1", "t") |
| 21 | +""" |
| 22 | + For scope timings to work |
| 23 | + The env var PHARE_SCOPE_TIMING must be == "1" (or "true") |
| 24 | + See src/phare/phare.hpp |
| 25 | + CMake must be configured with: -DwithPhlop=ON |
| 26 | + And a LOG_LEVEL must be defined via compile args: -DPHARE_LOG_LEVEL=1 |
| 27 | + Or change the default value in src/core/logger.hpp |
| 28 | + And phlop must be available on PYTHONPATH either from subprojects |
| 29 | + or install phlop via pip |
| 30 | +""" |
| 31 | + |
| 32 | +LOAD_BALANCE = os.getenv("LOAD_BALANCE", "True").lower() in ("true", "1", "t") |
| 33 | + |
| 34 | +cpp = cpp_lib() |
| 35 | +startMPI() |
| 36 | + |
| 37 | +cells = (100, 100) |
| 38 | +final_time = 50 |
| 39 | +time_step = 0.001 |
| 40 | +timestamps = np.arange(0, final_time + time_step, final_time / 5) |
| 41 | + |
| 42 | +diag_dir = "phare_outputs/harris_2d_100_x_100_slow" |
| 43 | +plot_dir = Path(f"{diag_dir}_plots") |
| 44 | +plot_dir.mkdir(parents=True, exist_ok=True) |
| 45 | + |
| 46 | + |
| 47 | +def config(): |
| 48 | + sim = ph.Simulation( |
| 49 | + time_step=time_step, |
| 50 | + final_time=final_time, |
| 51 | + cells=cells, |
| 52 | + dl=(0.40, 0.40), |
| 53 | + # refinement="tagging", |
| 54 | + # max_nbr_levels=1, |
| 55 | + nesting_buffer=1, |
| 56 | + clustering="tile", |
| 57 | + tag_buffer="1", |
| 58 | + hyper_resistivity=0.002, |
| 59 | + resistivity=0.001, |
| 60 | + diag_options={ |
| 61 | + "format": "phareh5", |
| 62 | + "options": {"dir": diag_dir, "mode": "overwrite"}, |
| 63 | + }, |
| 64 | + ) |
| 65 | + |
| 66 | + sim = base.config(sim, timestamps) |
| 67 | + |
| 68 | + for quantity in ["density", "bulkVelocity"]: |
| 69 | + ph.FluidDiagnostics(quantity=quantity, write_timestamps=timestamps) |
| 70 | + |
| 71 | + ph.FluidDiagnostics( |
| 72 | + quantity="density", write_timestamps=timestamps, population_name="protons" |
| 73 | + ) |
| 74 | + |
| 75 | + if LOAD_BALANCE: |
| 76 | + ph.LoadBalancer(active=True, auto=True, mode="nppc", tol=0.05) |
| 77 | + |
| 78 | + return sim |
| 79 | + |
| 80 | + |
| 81 | +def plot_file_for_qty(qty, time): |
| 82 | + return f"{plot_dir}/harris_{qty}_t{time}.png" |
| 83 | + |
| 84 | + |
| 85 | +def plot(diag_dir): |
| 86 | + run = Run(diag_dir) |
| 87 | + for time in timestamps: |
| 88 | + run.GetDivB(time).plot( |
| 89 | + filename=plot_file_for_qty("divb", time), |
| 90 | + plot_patches=True, |
| 91 | + vmin=1e-11, |
| 92 | + vmax=2e-10, |
| 93 | + ) |
| 94 | + run.GetRanks(time).plot( |
| 95 | + filename=plot_file_for_qty("Ranks", time), |
| 96 | + plot_patches=True, |
| 97 | + ) |
| 98 | + run.GetN(time, pop_name="protons").plot( |
| 99 | + filename=plot_file_for_qty("N", time), |
| 100 | + plot_patches=True, |
| 101 | + ) |
| 102 | + for c in ["x", "y", "z"]: |
| 103 | + run.GetB(time).plot( |
| 104 | + filename=plot_file_for_qty(f"b{c}", time), |
| 105 | + qty=f"{c}", |
| 106 | + plot_patches=True, |
| 107 | + ) |
| 108 | + run.GetJ(time).plot( |
| 109 | + filename=plot_file_for_qty("jz", time), |
| 110 | + qty="z", |
| 111 | + plot_patches=True, |
| 112 | + vmin=-2, |
| 113 | + vmax=2, |
| 114 | + ) |
| 115 | + |
| 116 | + |
| 117 | +class HarrisTest(SimulatorTest): |
| 118 | + def __init__(self, *args, **kwargs): |
| 119 | + super(HarrisTest, self).__init__(*args, **kwargs) |
| 120 | + self.simulator = None |
| 121 | + |
| 122 | + def tearDown(self): |
| 123 | + super(HarrisTest, self).tearDown() |
| 124 | + if self.simulator is not None: |
| 125 | + self.simulator.reset() |
| 126 | + self.simulator = None |
| 127 | + ph.global_vars.sim = None |
| 128 | + |
| 129 | + def test_run(self): |
| 130 | + self.register_diag_dir_for_cleanup(diag_dir) |
| 131 | + Simulator(config()).run().reset() |
| 132 | + if cpp.mpi_rank() == 0: |
| 133 | + plot(diag_dir) |
| 134 | + if SCOPE_TIMING: |
| 135 | + m_plotting.plot_run_timer_data(diag_dir, cpp.mpi_rank()) |
| 136 | + cpp.mpi_barrier() |
| 137 | + return self |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + HarrisTest().test_run().tearDown() |
0 commit comments