Skip to content

Commit

Permalink
update python diags
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasaunai committed Nov 9, 2023
1 parent fcfb6da commit f23733f
Show file tree
Hide file tree
Showing 14 changed files with 788 additions and 513 deletions.
217 changes: 130 additions & 87 deletions pyphare/pyphare/pharein/__init__.py

Large diffs are not rendered by default.

312 changes: 206 additions & 106 deletions pyphare/pyphare/pharein/diagnostics.py

Large diffs are not rendered by default.

670 changes: 394 additions & 276 deletions pyphare/pyphare/pharein/simulation.py

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion pyphare/pyphare/pharesee/hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@ def __init__(self, layout, field_name, data, **kwargs):

self.dataset = data


def meshgrid(self, select=None):
def grid():
if self.ndim == 1:
return [self.x]
if self.ndim == 2:
return np.meshgrid(self.x, self.y, indexing="ij")
return np.meshgrid(self.x, self.y, self.z, indexing="ij")

mesh = grid()
if select is not None:
return tuple(g[select] for g in mesh)
Expand Down Expand Up @@ -1496,6 +1496,12 @@ def isFieldQty(qty):
"flux_x",
"flux_y",
"flux_z",
"momentumTensor_xx",
"momentumTensor_xy",
"momentumTensor_xz",
"momentumTensor_yy",
"momentumTensor_yz",
"momentumTensor_zz",
)


Expand Down
7 changes: 4 additions & 3 deletions src/core/data/tensorfield/tensorfield.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ namespace PHARE::core
{


template<typename NdArrayImpl, typename PhysicalQuantity, std::size_t rank = 1>
template<typename NdArrayImpl, typename PhysicalQuantity, std::size_t rank_ = 1>
class TensorField
{
private:
constexpr static std::size_t dimFromRank()
{
if constexpr (rank == 1) // Vector field
if constexpr (rank_ == 1) // Vector field
return 3;
else if constexpr (rank == 2) // symmetric 3x3 tensor field
else if constexpr (rank_ == 2) // symmetric 3x3 tensor field
return 6;
}

Expand All @@ -37,6 +37,7 @@ class TensorField

using value_type = typename NdArrayImpl::type;
static constexpr std::size_t dimension = NdArrayImpl::dimension;
static constexpr std::size_t rank = rank_;
using tensor_t = typename PhysicalQuantity::template TensorType<rank>;

TensorField(std::string const& name, tensor_t physQty)
Expand Down
3 changes: 2 additions & 1 deletion src/core/data/vecfield/vecfield_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ namespace core
inline std::unordered_map<std::string, Component> const Components::vecComponentMap{
{"x", Component::X}, {"y", Component::Y}, {"z", Component::Z}};
inline std::unordered_map<std::string, Component> const Components::tensComponentMap{
{"x", Component::X}, {"y", Component::Y}, {"z", Component::Z}};
{"xx", Component::XX}, {"xy", Component::XY}, {"xz", Component::XZ},
{"yy", Component::YY}, {"yz", Component::YZ}, {"zz", Component::ZZ}};

} // namespace core
} // namespace PHARE
Expand Down
9 changes: 5 additions & 4 deletions src/diagnostic/detail/h5writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ class H5Writer



template<typename VecField>
static void writeVecFieldAsDataset(HighFiveFile& h5, std::string path, VecField& vecField)
template<typename TensorField>
static void writeTensorFieldAsDataset(HighFiveFile& h5, std::string path,
TensorField& tensorField)
{
for (auto& [id, type] : core::Components::componentMap())
for (auto& [id, type] : core::Components::componentMap<TensorField::rank>())
h5.write_data_set_flat<dimension>(path + "_" + id,
&(*vecField.getComponent(type).begin()));
&(*tensorField.getComponent(type).begin()));
}

auto& modelView() { return modelView_; }
Expand Down
6 changes: 3 additions & 3 deletions src/diagnostic/detail/types/electromag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ void ElectromagDiagnosticWriter<H5Writer>::write(DiagnosticProperties& diagnosti

for (auto* vecField : h5Writer.modelView().getElectromagFields())
if (diagnostic.quantity == "/" + vecField->name())
h5Writer.writeVecFieldAsDataset(*fileData_.at(diagnostic.quantity),
h5Writer.patchPath() + "/" + vecField->name(),
*vecField);
h5Writer.writeTensorFieldAsDataset(*fileData_.at(diagnostic.quantity),
h5Writer.patchPath() + "/" + vecField->name(),
*vecField);
}


Expand Down
28 changes: 16 additions & 12 deletions src/diagnostic/detail/types/fluid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ void FluidDiagnosticWriter<H5Writer>::initDataSets(
for (auto& [id, type] : core::Components::componentMap())
initDS(path, attr, key + "_" + id, null);
};
auto initTF = [&](auto& path, auto& attr, std::string key, auto null) {
for (auto& [id, type] : core::Components::componentMap<2>())
initDS(path, attr, key + "_" + id, null);
};

auto initPatch = [&](auto& lvl, auto& attr, std::string patchID = "") {
bool null = patchID.empty();
Expand All @@ -261,17 +265,17 @@ void FluidDiagnosticWriter<H5Writer>::initDataSets(
initDS(path, attr[popId], "density", null);
if (isActiveDiag(diagnostic, tree, "flux"))
initVF(path, attr[popId], "flux", null);
// if (isActiveDiag(diagnostic, tree, "momentum_tensor"))
// initTF(path, attr[popId], "momentum_tensor", null);
if (isActiveDiag(diagnostic, tree, "momentum_tensor"))
initTF(path, attr[popId], "momentum_tensor", null);
}

std::string tree{"/ions/"};
if (isActiveDiag(diagnostic, tree, "density"))
initDS(path, attr["ion"], "density", null);
if (isActiveDiag(diagnostic, tree, "bulkVelocity"))
initVF(path, attr["ion"], "bulkVelocity", null);
// if (isActiveDiag(diagnostic, tree, "momentum_tensor"))
// initTF(path, attr["ion"], "momentum_tensor", null);
if (isActiveDiag(diagnostic, tree, "momentum_tensor"))
initTF(path, attr["ion"], "momentum_tensor", null);
};

initDataSets_(patchIDs, patchAttributes, maxLevel, initPatch);
Expand All @@ -288,8 +292,8 @@ void FluidDiagnosticWriter<H5Writer>::write(DiagnosticProperties& diagnostic)
auto writeDS = [&](auto path, auto& field) {
h5file.template write_data_set_flat<GridLayout::dimension>(path, &(*field.begin()));
};
auto writeVF
= [&](auto path, auto& vecF) { h5Writer.writeVecFieldAsDataset(h5file, path, vecF); };
auto writeTF
= [&](auto path, auto& vecF) { h5Writer.writeTensorFieldAsDataset(h5file, path, vecF); };

std::string path = h5Writer.patchPath() + "/";
for (auto& pop : ions)
Expand All @@ -298,19 +302,19 @@ void FluidDiagnosticWriter<H5Writer>::write(DiagnosticProperties& diagnostic)
if (isActiveDiag(diagnostic, tree, "density"))
writeDS(path + "density", pop.density());
if (isActiveDiag(diagnostic, tree, "flux"))
writeVF(path + "flux", pop.flux());
// if (isActiveDiag(diagnostic, tree, "momentum_tensor"))
// writeTF(path + "momentum_tensor", momentum_tensor_[pop.name()]);
writeTF(path + "flux", pop.flux());
if (isActiveDiag(diagnostic, tree, "momentum_tensor"))
writeTF(path + "momentum_tensor", pop.momentumTensor());
}

std::string tree{"/ions/"};
auto& density = ions.density();
if (isActiveDiag(diagnostic, tree, "density"))
writeDS(path + "density", density);
if (isActiveDiag(diagnostic, tree, "bulkVelocity"))
writeVF(path + "bulkVelocity", ions.velocity());
// if (isActiveDiag(diagnostic, tree, "momentum_tensor"))
// writeTF(path + "momentum_tensor", momentum_tensor_["ion"]);
writeTF(path + "bulkVelocity", ions.velocity());
if (isActiveDiag(diagnostic, tree, "momentum_tensor"))
writeTF(path + "momentum_tensor", ions.momentumTensor());
}


Expand Down
27 changes: 14 additions & 13 deletions tests/diagnostic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@


def timestamps_with_step(sim, dump_step):
import numpy as np

nbr_dump_step = sim.final_time / dump_step
return dump_step * np.arange(nbr_dump_step)


def all_timestamps(sim):
import numpy as np

nbr_dump_step = int(sim.final_time / sim.time_step) + 1
return sim.time_step * np.arange(nbr_dump_step)

Expand All @@ -25,7 +26,7 @@ def dump_all_diags(pops=[], flush_every=100, timestamps=None):
compute_timestamps=timestamps,
)

for quantity in ["density", "bulkVelocity"]:
for quantity in ["density", "bulkVelocity", "pressure_tensor"]:
ph.FluidDiagnostics(
quantity=quantity,
write_timestamps=timestamps,
Expand All @@ -35,21 +36,21 @@ def dump_all_diags(pops=[], flush_every=100, timestamps=None):

for pop in pops:
for quantity in ["density", "flux"]:
ph.FluidDiagnostics(
quantity=quantity,
write_timestamps=timestamps,
compute_timestamps=timestamps,
flush_every=flush_every,
population_name=pop
)

for quantity in ['domain', 'levelGhost', 'patchGhost']:
ph.FluidDiagnostics(
quantity=quantity,
write_timestamps=timestamps,
compute_timestamps=timestamps,
flush_every=flush_every,
population_name=pop,
)

for quantity in ["domain", "levelGhost", "patchGhost"]:
ph.ParticleDiagnostics(
quantity=quantity,
compute_timestamps=timestamps,
write_timestamps=timestamps,
flush_every=flush_every,
population_name=pop
population_name=pop,
)

for quantity in ["E", "B"]:
Expand Down
4 changes: 2 additions & 2 deletions tests/simulator/test_diagnostic_timestamps.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_dump_diags_timestamps(self):
def make_time(stamp):
return "{:.10f}".format(stamp)

for diagInfo in ph.global_vars.sim.diagnostics:
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))

Expand Down Expand Up @@ -166,7 +166,7 @@ def test_hierarchy_timestamp_cadence(self, refinement_boxes):

Simulator(simulation).run()

for diagInfo in simulation.diagnostics:
for diagname, diagInfo in simulation.diagnostics.items():
h5_filename = os.path.join(diag_outputs, h5_filename_from(diagInfo))
self.assertTrue(os.path.exists(h5_filename))

Expand Down
4 changes: 2 additions & 2 deletions tests/simulator/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ def _test_dump_diags(self, dim, **simInput):
any(
[
diagInfo.quantity.endswith("domain")
for diagInfo in ph.global_vars.sim.diagnostics
for diagname, diagInfo in ph.global_vars.sim.diagnostics.items()
]
)
)

particle_files = 0
for diagInfo in ph.global_vars.sim.diagnostics:
for diagname, diagInfo in ph.global_vars.sim.diagnostics.items():
h5_filepath = os.path.join(local_out, h5_filename_from(diagInfo))
self.assertTrue(os.path.exists(h5_filepath))

Expand Down
2 changes: 1 addition & 1 deletion tests/simulator/test_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ def _test_patch_ghost_on_refined_level_case(self, dim, has_patch_ghost, **kwargs
any(
[
diagInfo.quantity.endswith("patchGhost")
for diagInfo in ph.global_vars.sim.diagnostics
for diagname, diagInfo in ph.global_vars.sim.diagnostics.items()
]
)
)
Expand Down
4 changes: 2 additions & 2 deletions tests/simulator/test_tagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ def _test_dump_diags(self, dim, **simInput):
any(
[
diagInfo.quantity.endswith("tags")
for diagInfo in ph.global_vars.sim.diagnostics
for diagname, diagInfo in ph.global_vars.sim.diagnostics.items()
]
)
)

checks = 0
found = 0
for diagInfo in ph.global_vars.sim.diagnostics:
for diagname, diagInfo in ph.global_vars.sim.diagnostics.items():
h5_filepath = os.path.join(local_out, h5_filename_from(diagInfo))
self.assertTrue(os.path.exists(h5_filepath))

Expand Down

0 comments on commit f23733f

Please sign in to comment.