Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 22 additions & 39 deletions tests/simulator/test_diagnostic_timestamps.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,62 +101,43 @@ def tearDown(self):
def ddt_test_id(self):
return self._testMethodName.split("_")[-1]

def test_dump_diags_timestamps(self):
print("test_dump_diags dim/interp:{}/{}".format(1, 1))

simulation = ph.Simulation(**simArgs.copy())
sim = simulation

dump_every = 1
timestamps = np.arange(
0, sim.final_time + sim.time_step, dump_every * sim.time_step
)
setup_model(10)

for quantity in ["B"]:
ElectromagDiagnostics(
quantity=quantity,
write_timestamps=timestamps,
flush_every=ElectromagDiagnostics.h5_flush_never,
)

Simulator(simulation).run()

def make_time(stamp):
return "{:.10f}".format(stamp)

for diagname, diagInfo in ph.global_vars.sim.diagnostics.items():
h5_filename = os.path.join(out, h5_filename_from(diagInfo))
self.assertTrue(os.path.exists(h5_filename))

h5_file = h5py.File(h5_filename, "r")

for timestamp in timestamps:
self.assertIn(make_time(timestamp), h5_file[h5_time_grp_key])

@data(
({"L0": {"B0": Box1D(10, 14), "B1": Box1D(15, 19)}}),
)
def test_hierarchy_timestamp_cadence(self, refinement_boxes):
"""
this test checks diagnostics are dumped at the correct timestamps

- only B is dumped
- we dump every 2dt, or every 3dt
- we check:
- diag file exists
- expected number of times in diag h5
- expected values of times in diag h5
- that we can skip init dump (t=t0)


This test only runs in 1D. other Dims are unnecessary
"""
dim = refinement_boxes["L0"]["B0"].ndim

time_step = 0.001
# time_step_nbr chosen to force diagnostics dumping double imprecision cadence calculations accuracy testing
time_step_nbr = 101
final_time = time_step * time_step_nbr

for trailing in [0, 1]: # 1 = skip init dumps
for i in [2, 3]:
for skip_init in [0, 1]: # 1 = skip init dumps
for diag_cadence in [2, 3]:
simInput = simArgs.copy()
diag_outputs = f"phare_outputs_hierarchy_timestamp_cadence_{dim}_{self.ddt_test_id()}_{i}"
diag_outputs = f"phare_outputs_hierarchy_timestamp_cadence_{dim}_{self.ddt_test_id()}_{diag_cadence}"
simInput["diag_options"]["options"]["dir"] = diag_outputs
simInput["time_step_nbr"] = time_step_nbr

ph.global_vars.sim = None
simulation = ph.Simulation(**simInput)
setup_model(10)

timestamps = np.arange(0, final_time, time_step * i)[trailing:]
timestamps = np.arange(0, final_time, time_step * i)[skip_init:]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix undefined variable error.

The variable i is undefined. It should be diag_cadence to match the loop variable name.

Apply this diff to fix the undefined variable:

-                timestamps = np.arange(0, final_time, time_step * i)[skip_init:]
+                timestamps = np.arange(0, final_time, time_step * diag_cadence)[skip_init:]
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
timestamps = np.arange(0, final_time, time_step * i)[skip_init:]
timestamps = np.arange(0, final_time, time_step * diag_cadence)[skip_init:]
🧰 Tools
🪛 Ruff (0.8.2)

140-140: Undefined name i

(F821)

for quantity in ["B"]:
ElectromagDiagnostics(
quantity=quantity,
Expand All @@ -175,8 +156,10 @@ def test_hierarchy_timestamp_cadence(self, refinement_boxes):
time_hier_keys = list(hier.time_hier.keys())
self.assertEqual(len(time_hier_keys), len(timestamps))

for i, timestamp in enumerate(time_hier_keys):
self.assertEqual(format_timestamp(timestamps[i]), timestamp)
for diag_cadence, timestamp in enumerate(time_hier_keys):
self.assertEqual(
format_timestamp(timestamps[diag_cadence]), timestamp
)


if __name__ == "__main__":
Expand Down