Skip to content

Commit

Permalink
??
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Sep 9, 2024
1 parent bdf8576 commit 1be5b34
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 109 deletions.
12 changes: 10 additions & 2 deletions pyphare/pyphare/core/phare_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,18 @@ def is_fp32(item):
return isinstance(item, float)


def assert_fp_any_all_close(a, b, atol=1e-16, rtol=0, atol_fp32=None):
def any_fp_tol(a, b, atol=1e-16, rtol=0, atol_fp32=None):
if any([is_fp32(el) for el in [a, b]]):
atol = atol_fp32 if atol_fp32 else atol * 1e8
np.testing.assert_allclose(a, b, atol=atol, rtol=rtol)
return dict(atol=atol, rtol=rtol)


def fp_any_all_close(a, b, atol=1e-16, rtol=0, atol_fp32=None):
return np.allclose(a, b, **any_fp_tol(a, b, atol, rtol, atol_fp32))


def assert_fp_any_all_close(a, b, atol=1e-16, rtol=0, atol_fp32=None):
np.testing.assert_allclose(a, b, **any_fp_tol(a, b, atol, rtol, atol_fp32))


def decode_bytes(input, errors="ignore"):
Expand Down
12 changes: 5 additions & 7 deletions pyphare/pyphare/pharesee/hierarchy/hierarchy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,9 @@ class EqualityReport:
def __bool__(self):
return self.ok

def __repr__(self):
return self.reason


def hierarchy_compare(this, that):
if not isinstance(this, PatchHierarchy) or not isinstance(that, PatchHierarchy):
Expand Down Expand Up @@ -599,12 +602,7 @@ def hierarchy_compare(this, that):
patch_data_cmp = patch_cmp.patch_datas[patch_data_key]

if patch_data_cmp != patch_data_ref:
return EqualityReport(
False,
"data mismatch: "
+ type(patch_data_cmp).__name__
+ " "
+ type(patch_data_ref).__name__,
)
msg = f"data mismatch: {patch_data_key} {type(patch_data_cmp).__name__} {type(patch_data_ref).__name__}"
return EqualityReport(False, msg)

return EqualityReport(True, "OK")
10 changes: 8 additions & 2 deletions pyphare/pyphare/pharesee/hierarchy/patchdata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from ...core.phare_utilities import deep_copy
from ...core.phare_utilities import deep_copy, fp_any_all_close
from ...core import box as boxm
from ...core.box import Box

Expand Down Expand Up @@ -80,8 +80,14 @@ def __str__(self):
def __repr__(self):
return self.__str__()

def compare(self, that, atol=1e-16):
return fp_any_all_close(self.dataset[:], that.dataset[:], atol)

def __eq__(self, that):
return self.field_name == that.field_name and self.dataset[:] == that.dataset[:]
return self.field_name == that.field_name and self.compare(that)

def __ne__(self, that):
return not (self == that)

def select(self, box):
"""
Expand Down
61 changes: 47 additions & 14 deletions src/amr/data/field/initializers/samrai_hdf5_field_initializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
#include <cassert>
#include <functional>

#include "core/def.hpp"
#include "core/logger.hpp"
#include "core/data/grid/gridlayoutdefs.hpp"
#include "core/data/ndarray/ndarray_vector.hpp"
#include "core/hybrid/hybrid_quantities.hpp"
#include "core/utilities/types.hpp"
#include "core/data/ions/particle_initializers/particle_initializer.hpp"
#include "core/data/particles/particle.hpp"
#include "initializer/data_provider.hpp"
#include "core/utilities/point/point.hpp"
#include "core/def.hpp"
#include "core/logger.hpp"

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

Expand Down Expand Up @@ -45,6 +46,7 @@ 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>;

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

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

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

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

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

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
auto const& overlaps = SamraiH5Interface<GridLayout>::INSTANCE().box_intersections(dest_box);

PHARE_LOG_LINE_STR(layout.AMRBox());
// PHARE_LOG_LINE_STR(layout.AMRBox());

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
for (auto const& [h5FilePtr, pdataptr] : overlaps)
{
auto& h5File = *h5FilePtr;
auto& pdata = *pdataptr;
PHARE_LOG_LINE_STR(pdata.box);
auto const src_box = grow(pdata.box, GridLayout::nbrGhosts());
// PHARE_LOG_LINE_STR(pdata.box);

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

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());

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

core::Box<std::uint32_t, GridLayout::dimension> const lcl_src_box{
core::Point{core::ConstArray<std::uint32_t, GridLayout::dimension>()},
core::Point{
core::for_N<GridLayout::dimension, core::for_N_R_mode::make_array>([&](auto i) {
return static_cast<std::uint32_t>(src_box.upper[i] - src_box.lower[i] + 1);
return static_cast<std::uint32_t>(
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());

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

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)
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);

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

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

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

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(*src_iter);
// PHARE_LOG_LINE_STR(*dst_iter);

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

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

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
}
}
}
Expand Down
74 changes: 41 additions & 33 deletions src/amr/data/initializers/samrai_hdf5_initializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ class SamraiH5Interface
std::unordered_map<std::string, std::string> box2dataset;
};


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

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

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 Down Expand Up @@ -129,64 +151,49 @@ struct SamraiH5Interface<GridLayout>::SamraiHDF5File : public hdf5::h5::HighFive
}
*/

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.



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

// struct BoxData
// {
// int dim;
// int lo0, lo1, lo2;
// int hi0, hi1, hi2;
// };

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

auto getBoxFromPath(std::string const& path) const
{
// auto const& data = Super::read_data_set<int>(path);

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
// PHARE_LOG_LINE_STR(data.size());
std::vector<BoxData> boxes;
Super::file().getDataSet(path).read(boxes);
assert(boxes.size() == 1);
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])};

// auto const& bx = boxes[0];
// 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>(boxes[0].lo),
core::sized_array<GridLayout::dimension>(boxes[0].hi)};
return Box_t{core::sized_array<GridLayout::dimension>(data[0].lo),
core::sized_array<GridLayout::dimension>(data[0].hi)};
}

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)
{
Box_t const mock{{0}, {99}};
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"}))
{
auto const em_path = group.substr(0, group.rfind("/"));
h5File.patches.emplace_back(mock, em_path.substr(0, em_path.rfind("/")));
h5File.patches.emplace_back(/*h5File.getBoxFromPath(em_path + "/d_box")*/ mock,
em_path.substr(0, em_path.rfind("/")));
}
}
}
Expand All @@ -195,5 +202,6 @@ 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
Loading

0 comments on commit 1be5b34

Please sign in to comment.