Skip to content

Commit fa64e08

Browse files
committed
++
1 parent 2f190ca commit fa64e08

File tree

4 files changed

+110
-171
lines changed

4 files changed

+110
-171
lines changed

tests/functional/harris/harris_2d.py

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
from pyphare.simulator.simulator import Simulator
88
from pyphare.simulator.simulator import startMPI
99

10+
from tests.simulator import SimulatorTest
11+
1012

1113
ph.NO_GUI()
1214
cpp = cpp_lib()
1315

1416

15-
1617
diag_outputs = "phare_outputs/test/harris/2d"
1718
time_step_nbr = 1000
1819
time_step = 0.001
@@ -23,6 +24,7 @@ def default_timestamps():
2324
dt = 10 * time_step
2425
nt = final_time / dt + 1
2526
timestamps = dt * np.arange(nt)
27+
return timestamps
2628

2729

2830
def default_setup():
@@ -48,7 +50,7 @@ def default_setup():
4850
)
4951

5052

51-
def config(sim = None, timestamps = None, seed = 12334):
53+
def config(sim=None, timestamps=None, seed=12334):
5254
if sim is None:
5355
sim = default_setup()
5456
if timestamps is None:
@@ -154,10 +156,70 @@ def vthz(x, y):
154156
return sim
155157

156158

157-
def main():
158-
Simulator(config()).run()
159-
159+
def plot_file_for_qty(plot_dir, qty, time):
160+
return f"{plot_dir}/harris_{qty}_t{time}.png"
161+
162+
163+
class HarrisTest(SimulatorTest):
164+
def __init__(self, *args, **kwargs):
165+
super(HarrisTest, self).__init__(*args, **kwargs)
166+
self.simulator = None
167+
168+
def tearDown(self):
169+
super(HarrisTest, self).tearDown()
170+
if self.simulator is not None:
171+
self.simulator.reset()
172+
self.simulator = None
173+
ph.global_vars.sim = None
174+
175+
def test_run(self, diag_dir=None, sim=None):
176+
diag_dir = diag_dir if diag_dir else diag_outputs
177+
sim = sim if sim else config()
178+
self.register_diag_dir_for_cleanup(diag_dir)
179+
Simulator(sim).run().reset()
180+
return self
181+
182+
def plot(self, timestamps, diag_dir, plot_dir):
183+
run = self.run(diag_dir)
184+
for time in timestamps:
185+
run.GetDivB(time).plot(
186+
filename=plot_file_for_qty(plot_dir, "divb", time),
187+
plot_patches=True,
188+
vmin=1e-11,
189+
vmax=2e-10,
190+
)
191+
run.GetRanks(time).plot(
192+
filename=plot_file_for_qty(plot_dir, "Ranks", time),
193+
plot_patches=True,
194+
)
195+
run.GetN(time, pop_name="protons").plot(
196+
filename=plot_file_for_qty(plot_dir, "N", time),
197+
plot_patches=True,
198+
)
199+
for c in ["x", "y", "z"]:
200+
run.GetB(time).plot(
201+
filename=plot_file_for_qty(plot_dir, f"b{c}", time),
202+
qty=f"{c}",
203+
plot_patches=True,
204+
)
205+
run.GetJ(time).plot(
206+
filename=plot_file_for_qty(plot_dir, "jz", time),
207+
qty="z",
208+
plot_patches=True,
209+
vmin=-2,
210+
vmax=2,
211+
)
212+
213+
def scope_timing(self, diag_dir):
214+
try:
215+
from tools.python3 import plotting as m_plotting
216+
217+
m_plotting.plot_run_timer_data(diag_dir, cpp.mpi_rank())
218+
except ImportError:
219+
print("Phlop not found - install with: `pip install phlop`")
220+
except FileNotFoundError:
221+
print("Phlop installed but not active")
160222

161223

162224
if __name__ == "__main__":
163-
main()
225+
HarrisTest().test_run().tearDown()

tests/functional/harris/harris_2d_100_x_100.py

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77

88
import pyphare.pharein as ph
99
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
10+
from pyphare.simulator.simulator import startMPI
1411

1512
import harris_2d as base
1613

@@ -49,8 +46,6 @@ def config():
4946
final_time=final_time,
5047
cells=cells,
5148
dl=(0.40, 0.40),
52-
# refinement="tagging",
53-
# max_nbr_levels=1,
5449
nesting_buffer=1,
5550
clustering="tile",
5651
tag_buffer="1",
@@ -77,69 +72,17 @@ def config():
7772
return sim
7873

7974

80-
def plot_file_for_qty(qty, time):
81-
return f"{plot_dir}/harris_{qty}_t{time}.png"
82-
83-
84-
def plot(diag_dir):
85-
run = Run(diag_dir)
86-
for time in timestamps:
87-
run.GetDivB(time).plot(
88-
filename=plot_file_for_qty("divb", time),
89-
plot_patches=True,
90-
vmin=1e-11,
91-
vmax=2e-10,
92-
)
93-
run.GetRanks(time).plot(
94-
filename=plot_file_for_qty("Ranks", time),
95-
plot_patches=True,
96-
)
97-
run.GetN(time, pop_name="protons").plot(
98-
filename=plot_file_for_qty("N", time),
99-
plot_patches=True,
100-
)
101-
for c in ["x", "y", "z"]:
102-
run.GetB(time).plot(
103-
filename=plot_file_for_qty(f"b{c}", time),
104-
qty=f"{c}",
105-
plot_patches=True,
106-
)
107-
run.GetJ(time).plot(
108-
filename=plot_file_for_qty("jz", time),
109-
qty="z",
110-
plot_patches=True,
111-
vmin=-2,
112-
vmax=2,
113-
)
114-
115-
116-
class HarrisTest(SimulatorTest):
75+
class HarrisTest(base.HarrisTest):
11776
def __init__(self, *args, **kwargs):
11877
super(HarrisTest, self).__init__(*args, **kwargs)
119-
self.simulator = None
120-
121-
def tearDown(self):
122-
super(HarrisTest, self).tearDown()
123-
if self.simulator is not None:
124-
self.simulator.reset()
125-
self.simulator = None
126-
ph.global_vars.sim = None
12778

12879
def test_run(self):
129-
self.register_diag_dir_for_cleanup(diag_dir)
130-
Simulator(config()).run().reset()
80+
super(HarrisTest, self).test_run(diag_dir, config())
13181
if cpp.mpi_rank() == 0:
132-
plot(diag_dir)
82+
self.plot(timestamps, diag_dir, plot_dir)
13383

13484
if SCOPE_TIMING:
135-
try:
136-
from tools.python3 import plotting as m_plotting
137-
138-
m_plotting.plot_run_timer_data(diag_dir, cpp.mpi_rank())
139-
except ImportError:
140-
print("Phlop not found - install with: `pip install phlop`")
141-
142-
cpp.mpi_barrier()
85+
self.scope_timing(diag_dir)
14386
return self
14487

14588

tests/functional/harris/harris_2d_100_x_100_slow.py

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
from pathlib import Path
77

88
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
129

13-
from tests.simulator import SimulatorTest
10+
from pyphare.cpp import cpp_lib
11+
from pyphare.simulator.simulator import startMPI
1412

1513
import harris_2d as base
1614

@@ -77,69 +75,17 @@ def config():
7775
return sim
7876

7977

80-
def plot_file_for_qty(qty, time):
81-
return f"{plot_dir}/harris_{qty}_t{time}.png"
82-
83-
84-
def plot(diag_dir):
85-
run = Run(diag_dir)
86-
for time in timestamps:
87-
run.GetDivB(time).plot(
88-
filename=plot_file_for_qty("divb", time),
89-
plot_patches=True,
90-
vmin=1e-11,
91-
vmax=2e-10,
92-
)
93-
run.GetRanks(time).plot(
94-
filename=plot_file_for_qty("Ranks", time),
95-
plot_patches=True,
96-
)
97-
run.GetN(time, pop_name="protons").plot(
98-
filename=plot_file_for_qty("N", time),
99-
plot_patches=True,
100-
)
101-
for c in ["x", "y", "z"]:
102-
run.GetB(time).plot(
103-
filename=plot_file_for_qty(f"b{c}", time),
104-
qty=f"{c}",
105-
plot_patches=True,
106-
)
107-
run.GetJ(time).plot(
108-
filename=plot_file_for_qty("jz", time),
109-
qty="z",
110-
plot_patches=True,
111-
vmin=-2,
112-
vmax=2,
113-
)
114-
115-
116-
class HarrisTest(SimulatorTest):
78+
class HarrisTest(base.HarrisTest):
11779
def __init__(self, *args, **kwargs):
11880
super(HarrisTest, self).__init__(*args, **kwargs)
119-
self.simulator = None
120-
121-
def tearDown(self):
122-
super(HarrisTest, self).tearDown()
123-
if self.simulator is not None:
124-
self.simulator.reset()
125-
self.simulator = None
126-
ph.global_vars.sim = None
12781

12882
def test_run(self):
129-
self.register_diag_dir_for_cleanup(diag_dir)
130-
Simulator(config()).run().reset()
83+
super(HarrisTest, self).test_run(diag_dir, config())
13184
if cpp.mpi_rank() == 0:
132-
plot(diag_dir)
85+
self.plot(timestamps, diag_dir, plot_dir)
13386

13487
if SCOPE_TIMING:
135-
try:
136-
from tools.python3 import plotting as m_plotting
137-
138-
m_plotting.plot_run_timer_data(diag_dir, cpp.mpi_rank())
139-
except ImportError:
140-
print("Phlop not found - install with: `pip install phlop`")
141-
142-
cpp.mpi_barrier()
88+
self.scope_timing(diag_dir)
14389
return self
14490

14591

tests/simulator/__init__.py

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import os
22
import unittest
3+
import numpy as np
4+
import pyphare.pharein as ph
5+
6+
from functools import wraps
37
from datetime import datetime
4-
import pyphare.pharein as ph, numpy as np
58
from pyphare.pharein import ElectronModel
9+
from pyphare.pharesee import run
610

711

812
def parse_cli_args(pop_from_sys=True):
@@ -192,53 +196,34 @@ def _diff(slice0):
192196
return boxes
193197

194198

195-
#
196-
#
197-
198-
199-
def caliper_func_times_json(data_dir, mpi_rank=0):
200-
return f"{os.path.join(data_dir, f'func_times.{mpi_rank}.json')}"
201-
202-
203-
def caliper_recorder_cali(data_dir, mpi_rank=0):
204-
return f"{os.path.join(data_dir, f'recorder.{mpi_rank}.cali')}"
205-
206-
207-
CALIPER_MODES = [
208-
# "callpath:event:recorder:trace",
209-
"report,event,trace,timestamp,recorder", # light
210-
"alloc,aggregate,cpuinfo,memusage,debug,env,event,loop_monitor,region_monitor,textlog,io,pthread,sysalloc,recorder,report,timestamp,statistics,spot,trace,validator,mpi,mpireport,mpiflush", # heavy
211-
]
199+
class SimulatorTestRunInterop(run.Run):
200+
"""
201+
Intercept calls to Run and catch FileNotFound errors for when
202+
diags are not active/etc
203+
"""
212204

205+
def __init__(self, *args, **kwargs):
206+
super().__init__(*args, **kwargs)
207+
self.dummies = {}
213208

214-
def activate_caliper(data_dir, mode_idx=0):
215-
from pyphare.cpp import cpp_lib
216-
217-
rank = cpp_lib().mpi_rank()
218-
env = os.environ
219-
220-
# env["CALI_SERVICES_ENABLE"] = "event,trace,timer,report"
221-
# env["CALI_REPORT_CONFIG"] = "format json"
222-
# env["CALI_REPORT_FILENAME"] = "trace.json"
223-
224-
# env[
225-
# "CALI_CONFIG"
226-
# ] = "hatchet-region-profile,topdown-counters.all,output.format=json"
227-
228-
# # env["CALI_CONFIG_PROFILE"] = "callstack-trace"
229-
# env["CALI_SERVICES_ENABLE"] = CALIPER_MODES[mode_idx]
230-
231-
# env["CALI_CONFIG"] = "hatchet-region-profile"
209+
@staticmethod
210+
def _wrapper(func, func_name):
211+
@wraps(func)
212+
def wrapped(*args, **kwargs):
213+
try:
214+
return func(*args, **kwargs)
215+
except FileNotFoundError:
216+
print("File not found, maybe diagnostic is not active")
232217

233-
# # env["CALI_CALLPATH_USE_NAME"] = "true"
218+
return wrapped
234219

235-
# env["CALI_REPORT_FILENAME"] = caliper_func_times_json(data_dir, rank)
236-
# env[
237-
# "CALI_REPORT_CONFIG"
238-
# ] = "SELECT function,time.duration ORDER BY time.duration FORMAT json"
239-
# env["CALI_RECORDER_FILENAME"] = caliper_recorder_cali(data_dir, rank)
220+
def __getattribute__(self, name):
221+
import types
240222

241-
# print("os.environ", os.environ)
223+
attr = super(SimulatorTestRunInterop, self).__getattribute__(name)
224+
if isinstance(attr, types.MethodType) and not name.startswith("_"):
225+
attr = SimulatorTestRunInterop._wrapper(attr, name)
226+
return attr
242227

243228

244229
class SimulatorTest(unittest.TestCase):
@@ -308,3 +293,6 @@ def clean_up_diags_dirs(self):
308293
if os.path.exists(diag_dir):
309294
shutil.rmtree(diag_dir)
310295
cpp_lib().mpi_barrier()
296+
297+
def run(self, diag_dir):
298+
return SimulatorTestRunInterop(diag_dir)

0 commit comments

Comments
 (0)