Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Sep 10, 2024
1 parent 1be5b34 commit 2e4e861
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 269 deletions.
8 changes: 4 additions & 4 deletions pyphare/pyphare/pharesee/hierarchy/hierarchy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def overlap_mask_2d(x, y, dl, level, qty):
return is_overlaped


def flat_finest_field(hierarchy, qty, time=None):
def flat_finest_field(hierarchy, qty, time=None, neghosts=1):
"""
returns 2 flattened arrays containing the data (with shape [Npoints])
and the coordinates (with shape [Npoints, Ndim]) for the given
Expand All @@ -311,7 +311,7 @@ def flat_finest_field(hierarchy, qty, time=None):
dim = hierarchy.ndim

if dim == 1:
return flat_finest_field_1d(hierarchy, qty, time)
return flat_finest_field_1d(hierarchy, qty, time, neghosts)
elif dim == 2:
return flat_finest_field_2d(hierarchy, qty, time)
elif dim == 3:
Expand All @@ -321,7 +321,7 @@ def flat_finest_field(hierarchy, qty, time=None):
raise ValueError("the dim of a hierarchy should be 1, 2 or 3")


def flat_finest_field_1d(hierarchy, qty, time=None):
def flat_finest_field_1d(hierarchy, qty, time=None, neghosts=1):
lvl = hierarchy.levels(time)

for ilvl in range(hierarchy.finest_level(time) + 1)[::-1]:
Expand All @@ -333,7 +333,7 @@ def flat_finest_field_1d(hierarchy, qty, time=None):
# all but 1 ghost nodes are removed in order to limit
# the overlapping, but to keep enough point to avoid
# any extrapolation for the interpolator
needed_points = pdata.ghosts_nbr - 1
needed_points = pdata.ghosts_nbr - neghosts

# data = pdata.dataset[patch.box] # TODO : once PR 551 will be merged...
data = pdata.dataset[needed_points[0] : -needed_points[0]]
Expand Down
5 changes: 3 additions & 2 deletions pyphare/pyphare/pharesee/particles.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,9 @@ def single_patch_per_level_per_pop_from(hier, only_keep_L0=True): # dragons
particles[key] = []

for patch in patch_level.patches:
for patch_data_key in patch.patch_datas.keys():
particles[key] += [patch[patch_data_key].dataset]
for key in patch.patch_datas.keys():
if key in particles:
particles[key] += [patch[key].dataset]

for key in particles.keys():
if particles[key]:
Expand Down
73 changes: 10 additions & 63 deletions src/amr/data/field/initializers/samrai_hdf5_field_initializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ void SamraiHDF5FieldInitializer<Field_t, GridLayout>::load(Field_t& field,
GridLayout const& layout) const
{
bool static constexpr c_ordering = false;

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable c_ordering is not used.
// using Viewer = core::NdArrayViewer<Field_t::dimension>;

auto const local_cell = [&](auto const& box, auto const& point) {
core::Point<std::uint32_t, dimension> localPoint;
Expand All @@ -56,33 +55,16 @@ void SamraiHDF5FieldInitializer<Field_t, GridLayout>::load(Field_t& field,
return localPoint;
};

// PHARE_LOG_LINE_STR("SamraiHDF5FieldInitializer::load");

auto const& dest_box = layout.AMRBox();
auto const& centering = layout.centering(field.physicalQuantity());
auto const& dest_box = layout.AMRBox(); // grow(layout.AMRBox(), GridLayout::nbrGhosts());
auto const& overlaps = SamraiH5Interface<GridLayout>::INSTANCE().box_intersections(dest_box);

// PHARE_LOG_LINE_STR(layout.AMRBox());
for (auto const& [h5FilePtr, pdataptr] : overlaps)
for (auto const& [overlap_box, h5FilePtr, pdataptr] : overlaps)
{
auto& h5File = *h5FilePtr;
auto& pdata = *pdataptr;
// PHARE_LOG_LINE_STR(pdata.box);

auto const src_box = [&]() {
auto copy = pdata.box;
// for (std::size_t i = 0; i < Field_t::dimension; ++i)
// copy.upper[i] += centering[i] == core::QtyCentering::primal ? 1 : 0;
return copy;
}();

std::vector<double> data;
std::string const fieldpath
= pdata.base_path + "/" + field.name() + "##default/field_" + field.name();
h5File.file().getDataSet(fieldpath).read(data);

// PHARE_LOG_LINE_STR(data.size());

auto& h5File = *h5FilePtr;
auto& pdata = *pdataptr;
auto const src_box = pdata.box;
auto const data = h5File.template read_data_set_flat<double>(
pdata.base_path + "/" + field.name() + "##default/field_" + field.name());
core::Box<std::uint32_t, GridLayout::dimension> const lcl_src_box{
core::Point{core::ConstArray<std::uint32_t, GridLayout::dimension>()},
core::Point{
Expand All @@ -91,45 +73,9 @@ void SamraiHDF5FieldInitializer<Field_t, GridLayout>::load(Field_t& field,
src_box.upper[i] - src_box.lower[i] + (GridLayout::nbrGhosts() * 2)
+ (centering[i] == core::QtyCentering::primal ? 1 : 0));
})}};

// PHARE_LOG_LINE_STR(lcl_src_box.size());

assert(data.size() == lcl_src_box.size());

auto data_view = core::make_array_view<c_ordering>(data.data(), *lcl_src_box.shape());
auto dst_iter = dest_box.begin();
auto src_iter = src_box.begin();
for (; src_iter != src_box.end(); ++src_iter)
{
// for (auto const& el : **src_iter)
// if (el < 0)
// continue;
// if (core::any(*src_iter, [](auto const& el) { el < 0; }))
// continue; // skip ghosts

if (isIn(core::Point{*src_iter}, dest_box))
{
while (*dst_iter != *src_iter and dst_iter != dest_box.end())
++dst_iter;

if (dst_iter == dest_box.end() or not isIn(core::Point{*dst_iter}, dest_box))
break;

// auto src_idx = Viewer::idx(lcl_src_box.shape(), *src_iter);
// auto dst_idx = Viewer::idx(dest_box.shape(), *dst_iter);

// PHARE_LOG_LINE_STR(src_idx);
// PHARE_LOG_LINE_STR(dst_idx);

field(local_cell(dest_box, *dst_iter)) = data_view(local_cell(src_box, *src_iter));

// PHARE_LOG_LINE_STR(*src_iter);
// PHARE_LOG_LINE_STR(*dst_iter);

// PHARE_LOG_LINE_STR(field(local_cell(dest_box, *dst_iter)));
// PHARE_LOG_LINE_STR(data_view(local_cell(src_box, *src_iter)));
}
}
for (auto const& point : overlap_box)
field(local_cell(dest_box, point)) = data_view(local_cell(src_box, point));
}
}

Expand All @@ -138,4 +84,5 @@ void SamraiHDF5FieldInitializer<Field_t, GridLayout>::load(Field_t& field,
} // namespace PHARE::amr



#endif
105 changes: 24 additions & 81 deletions src/amr/data/initializers/samrai_hdf5_initializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include "core/logger.hpp"

#include "hdf5/detail/h5/h5_file.hpp"

#include "SAMRAI/tbox/HDFDatabase.h"

#include "amr/utilities/box/amr_box.hpp"

#include "SAMRAI/hier/PatchDataRestartManager.h"
Expand Down Expand Up @@ -57,7 +60,8 @@ class SamraiH5Interface
return i;
}

void populate_from(std::string const& dir, int const& idx, int const& mpi_size);
void populate_from(std::string const& dir, int const& idx, int const& mpi_size,
std::string const& field_name = "field_EM_B_x");

NO_DISCARD auto static getRestartFileFullPath(std::string path, int const& idx,
int const& mpi_size, int const& rank)
Expand All @@ -70,13 +74,13 @@ class SamraiH5Interface

auto box_intersections(Box_t const& box)
{
using Pair = std::pair<SamraiHDF5File const* const,
using Tup = std::tuple<Box_t, SamraiHDF5File const* const,
SamraiH5PatchDataInfo<GridLayout::dimension> const* const>;
std::vector<Pair> overlaps;
std::vector<Tup> overlaps;
for (auto const& h5File : restart_files)
for (auto const& patch : h5File->patches)
if (auto const intersection = box * patch.box)
overlaps.emplace_back(h5File.get(), &patch);
overlaps.emplace_back(*intersection, h5File.get(), &patch);
return overlaps;
}

Expand All @@ -88,26 +92,6 @@ class SamraiH5Interface
};


// struct BoxData
// {
// int dim;
// std::array<int, 3> lo;
// std::array<int, 3> hi;
// };

typedef struct
{
int dim;
std::array<int, 3> lo;
std::array<int, 3> hi;
} BoxData;

HighFive::CompoundType static box_compound_type()
{
return {{"dim", HighFive::create_datatype<int>()},
{"lo", HighFive::create_datatype<std::array<int, 3>>()},
{"hi", HighFive::create_datatype<std::array<int, 3>>()}};
}

template<typename GridLayout>
struct SamraiH5Interface<GridLayout>::SamraiHDF5File : public hdf5::h5::HighFiveFile
Expand All @@ -116,83 +100,43 @@ struct SamraiH5Interface<GridLayout>::SamraiHDF5File : public hdf5::h5::HighFive

SamraiHDF5File(std::string const& filepath)
: Super{filepath, HighFive::File::ReadOnly, /*para=*/false}
, filepath_{filepath}
{
// auto box_type = box_compound_type();
// box_type.commit(file(), "d_box");

// getBoxFromPath("/PHARE_hierarchy/level_0000/level_0000-patch_0000000-block_0000000/"
// "EM_B_y##default/d_box");
}



/*
DATASET "d_box" {
DATATYPE H5T_COMPOUND {
H5T_STD_I32BE "dim";
H5T_ARRAY { [3] H5T_STD_I32BE } "lo";
H5T_ARRAY { [3] H5T_STD_I32BE } "hi";
}
DATASPACE SIMPLE { ( 1 ) / ( 1 ) }
DATA {
(0): {
1,
[ 0, 0, 0 ],
[ 199, 0, 0 ]
}
}
ATTRIBUTE "Type" {
DATATYPE H5T_STD_I8BE
DATASPACE SCALAR
DATA {
(0): 2
}
}
}
*/

auto getBoxFromPath(std::string const& path) const
{
// auto const& data = Super::read_data_set<int>(path);
PHARE_LOG_LINE_STR(path);
std::vector<BoxData> data;
Super::file().getDataSet(path).read(data);
assert(data.size() == 1);

// return Box_t{core::as_sized_array<GridLayout::dimension>(data[1], data[2], data[3]),
// core::as_sized_array<GridLayout::dimension>(data[4], data[5], data[6])};

// return Box_t{core::as_sized_array<GridLayout::dimension>(bx.lo0, bx.lo1, bx.lo2),
// core::as_sized_array<GridLayout::dimension>(bx.hi0, bx.hi1, bx.hi2)};

return Box_t{core::sized_array<GridLayout::dimension>(data[0].lo),
core::sized_array<GridLayout::dimension>(data[0].hi)};
std::size_t constexpr samrai_dim = 3; // always 3!

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable samrai_dim is not used.
auto constexpr _to_std_array = [](auto& i) {
return core::sized_array<GridLayout::dimension>(
*reinterpret_cast<std::array<int, samrai_dim> const*>(&i));
};

SAMRAI::tbox::HDFDatabase db{"db"};
db.open(filepath_);
auto const boxes = db.getDatabaseBoxVector(path);
return Box_t{_to_std_array(boxes[0].d_data.d_lo), _to_std_array(boxes[0].d_data.d_hi)};
}

std::string const filepath_;
std::vector<SamraiH5PatchDataInfo<GridLayout::dimension>> patches;
};



template<typename GridLayout>
void SamraiH5Interface<GridLayout>::populate_from(std::string const& dir, int const& idx,
int const& mpi_size)
int const& mpi_size,
std::string const& field_name)
{
auto upstr = core::get_env("PHARE_RESTART_BUP", "199");
int up = 199;
std::stringstream ss{upstr};
ss >> up;


Box_t const mock{{0}, {up}};
for (int rank = 0; rank < mpi_size; ++rank)
{
auto const hdf5_filepath = getRestartFileFullPath(dir, idx, mpi_size, rank);
auto& h5File = *restart_files.emplace_back(std::make_unique<SamraiHDF5File>(hdf5_filepath));
for (auto const& group : h5File.scan_for_groups({"level_0000", "field_EM_B_x"}))
for (auto const& group : h5File.scan_for_groups({"level_0000", field_name}))
{
auto const em_path = group.substr(0, group.rfind("/"));
h5File.patches.emplace_back(/*h5File.getBoxFromPath(em_path + "/d_box")*/ mock,
h5File.patches.emplace_back(h5File.getBoxFromPath(em_path + "/d_box"),
em_path.substr(0, em_path.rfind("/")));
}
}
Expand All @@ -202,6 +146,5 @@ void SamraiH5Interface<GridLayout>::populate_from(std::string const& dir, int co

} // namespace PHARE::amr

HIGHFIVE_REGISTER_TYPE(PHARE::amr::BoxData, PHARE::amr::box_compound_type)

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,10 @@ void SamraiHDF5ParticleInitializer<ParticleArray, GridLayout>::loadParticles(
ParticleArray& particles, GridLayout const& layout, std::string const& popname) const
{
using Packer = core::ParticlePacker<ParticleArray::dimension>;
PHARE_LOG_LINE_STR("SamraiHDF5ParticleInitializer::loadParticles");
PHARE_LOG_LINE_SS(popname << " " << layout.AMRBox());

auto const& dest_box = layout.AMRBox();

auto const& overlaps = SamraiH5Interface<GridLayout>::INSTANCE().box_intersections(dest_box);
for (auto const& [h5FilePtr, pdataptr] : overlaps)
auto const& overlaps
= SamraiH5Interface<GridLayout>::INSTANCE().box_intersections(layout.AMRBox());
for (auto const& [overlap_box, h5FilePtr, pdataptr] : overlaps)
{
auto& h5File = *h5FilePtr;
auto& pdata = *pdataptr;
Expand All @@ -69,19 +66,13 @@ void SamraiHDF5ParticleInitializer<ParticleArray, GridLayout>::loadParticles(
std::size_t part_idx = 0;
core::apply(soa.as_tuple(), [&](auto& arg) {
auto const datapath = poppath + Packer::keys()[part_idx++];
PHARE_LOG_LINE_STR("SamraiHDF5ParticleInitializer::loadParticles");
PHARE_LOG_LINE_STR(datapath);
h5File.file().getDataSet(datapath).read(arg);
PHARE_LOG_LINE_STR("SamraiHDF5ParticleInitializer::loadParticles");
});
}

for (std::size_t i = 0; i < soa.size(); ++i)
if (auto const p = soa.copy(i); core::isIn(core::Point{p.iCell}, dest_box))
if (auto const p = soa.copy(i); core::isIn(core::Point{p.iCell}, overlap_box))
particles.push_back(p);

PHARE_LOG_LINE_STR("SamraiHDF5ParticleInitializer::loadParticles");
PHARE_LOG_LINE_STR(particles.size());
}
}

Expand Down
11 changes: 8 additions & 3 deletions tests/simulator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ add_no_mpi_python3_test(periodicity test_init_periodicity.py ${CMAKE_CURRENT_BIN

if(HighFive)
## These test use dump diagnostics so require HighFive!
phare_python3_exec(9 diagnostics test_diagnostics.py ${CMAKE_CURRENT_BINARY_DIR}) # serial or n = 2
phare_python3_exec(9 restarts test_restarts.py ${CMAKE_CURRENT_BINARY_DIR}) # serial or n = 2
phare_python3_exec(9 run test_run.py ${CMAKE_CURRENT_BINARY_DIR}) # serial or n = 2
phare_python3_exec(9 diagnostics test_diagnostics.py ${CMAKE_CURRENT_BINARY_DIR}) # serial or n = 2
phare_python3_exec(9 restarts test_restarts.py ${CMAKE_CURRENT_BINARY_DIR}) # serial or n = 2
phare_python3_exec(9 run test_run.py ${CMAKE_CURRENT_BINARY_DIR}) # serial or n = 2

# phare_python3_exec(9 tagging test_tagging.py ${CMAKE_CURRENT_BINARY_DIR}) # serial or n = 2

if(testMPI)
Expand All @@ -28,6 +29,10 @@ if(HighFive)
endif(testMPI)

phare_python3_exec(11, test_diagnostic_timestamps test_diagnostic_timestamps.py ${CMAKE_CURRENT_BINARY_DIR})

if(NOT lowResourceTests)
phare_python3_exec(9 initing test_init_from_restart.py ${CMAKE_CURRENT_BINARY_DIR})
endif()
endif()

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.py ${CMAKE_CURRENT_BINARY_DIR}/config.py @ONLY)
Expand Down
Loading

0 comments on commit 2e4e861

Please sign in to comment.