From 4bb039e3c81f394e9cdf107df4f7f4318e6bd319 Mon Sep 17 00:00:00 2001 From: deegan Date: Fri, 2 Feb 2024 14:40:41 +0100 Subject: [PATCH] add serialized simulation to diagnostics files --- pyphare/pyphare/pharein/__init__.py | 8 +++++++- pyphare/pyphare/pharein/diagnostics.py | 8 ++------ pyphare/pyphare/pharesee/run.py | 1 - tests/simulator/test_diagnostics.py | 9 ++++++++- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/pyphare/pyphare/pharein/__init__.py b/pyphare/pyphare/pharein/__init__.py index 22b40c163..ee2449bb3 100644 --- a/pyphare/pyphare/pharein/__init__.py +++ b/pyphare/pyphare/pharein/__init__.py @@ -251,9 +251,14 @@ def as_paths(rb): addInitFunction(maginit_path + "y_component", fn_wrapper(modelDict["by"])) addInitFunction(maginit_path + "z_component", fn_wrapper(modelDict["bz"])) + serialized_sim = serialize_sim(simulation) + #### adding diagnostics + diag_path = "simulation/diagnostics/" for diag in list(simulation.diagnostics.values()): + diag.attributes["serialized_simulation"] = serialized_sim + type_path = diag_path + diag.type + "/" name_path = type_path + diag.name add_string(name_path + "/" + "type", diag.type) @@ -265,6 +270,7 @@ def as_paths(rb): pp.add_array_as_vector( name_path + "/" + "compute_timestamps", diag.compute_timestamps ) + add_size_t(name_path + "/" + "n_attributes", len(diag.attributes)) for attr_idx, attr_key in enumerate(diag.attributes): add_string(name_path + "/" + f"attribute_{attr_idx}_key", attr_key) @@ -343,7 +349,7 @@ def as_paths(rb): restarts_path + "write_timestamps", restart_options["timestamps"] ) - add_string(restarts_path + "serialized_simulation", serialize_sim(simulation)) + add_string(restarts_path + "serialized_simulation", serialized_sim) #### restarts added #### adding electrons diff --git a/pyphare/pyphare/pharein/diagnostics.py b/pyphare/pyphare/pharein/diagnostics.py index 47727e9a0..415b5d00f 100644 --- a/pyphare/pyphare/pharein/diagnostics.py +++ b/pyphare/pyphare/pharein/diagnostics.py @@ -1,3 +1,5 @@ +import numpy as np + from ..core import phare_utilities from . import global_vars @@ -45,8 +47,6 @@ def wrapper(diagnostics_object, name, **kwargs): return wrapper -import numpy as np - # ------------------------------------------------------------------------------ def validate_timestamps(clazz, key, **kwargs): sim = global_vars.sim @@ -173,7 +173,6 @@ def __init__(self, **kwargs): ) def _setSubTypeAttributes(self, **kwargs): - if kwargs["quantity"] not in ElectromagDiagnostics.em_quantities: error_msg = "Error: '{}' not a valid electromag diagnostics : " + ", ".join( ElectromagDiagnostics.em_quantities @@ -201,7 +200,6 @@ def population_in_model(population): class FluidDiagnostics_(Diagnostics): - fluid_quantities = [ "density", "mass_density", @@ -327,7 +325,6 @@ def _setSubTypeAttributes(self, **kwargs): self.quantity = "/ions/pop/" + self.population_name + "/" + self.quantity def space_box(self, **kwargs): - if "extent" not in kwargs and self.quantity == "space_box": raise ValueError( "Error: missing 'extent' parameter required by 'space_box' the ParticleDiagnostics type" @@ -352,7 +349,6 @@ def to_dict(self): class MetaDiagnostics(Diagnostics): - meta_quantities = ["tags"] type = "info" diff --git a/pyphare/pyphare/pharesee/run.py b/pyphare/pyphare/pharesee/run.py index 0da598e1b..988479df9 100644 --- a/pyphare/pyphare/pharesee/run.py +++ b/pyphare/pyphare/pharesee/run.py @@ -111,7 +111,6 @@ def _divB2D(Bx, By, xBx, yBy): def _compute_divB(patchdatas, **kwargs): - reference_pd = patchdatas["Bx"] # take Bx as a reference, but could be any other ndim = reference_pd.box.ndim diff --git a/tests/simulator/test_diagnostics.py b/tests/simulator/test_diagnostics.py index 6f863e10e..eeff619de 100644 --- a/tests/simulator/test_diagnostics.py +++ b/tests/simulator/test_diagnostics.py @@ -160,7 +160,7 @@ def _test_dump_diags(self, dim, **simInput): simInput["refinement_boxes"] = {"L0": {"B0": b0}} py_attrs = [f"{dep}_version" for dep in ["samrai", "highfive", "pybind"]] - py_attrs += ["git_hash"] + py_attrs += ["git_hash", "serialized_simulation"] for interp in range(1, 4): print("test_dump_diags dim/interp:{}/{}".format(dim, interp)) @@ -203,6 +203,13 @@ def _test_dump_diags(self, dim, **simInput): for py_attr in py_attrs: self.assertIn(py_attr, h5_py_attrs) + assert ( + ph.simulation.deserialize( + h5_file["py_attrs"].attrs["serialized_simulation"] + ).electrons.closure.Te + == 0.12 + ) + hier = hierarchy_from(h5_filename=h5_filepath) if h5_filepath.endswith("domain.h5"): particle_files += 1