|
| 1 | +#ifndef _PHARE_AMR_DATA_INITIAZILIZERS_SAMRAI_HDF5_INITIALIZER_HPP_ |
| 2 | +#define _PHARE_AMR_DATA_INITIAZILIZERS_SAMRAI_HDF5_INITIALIZER_HPP_ |
| 3 | + |
| 4 | +#include <memory> |
| 5 | +#include <random> |
| 6 | +#include <cassert> |
| 7 | +#include <functional> |
| 8 | + |
| 9 | +#include "core/data/grid/gridlayoutdefs.hpp" |
| 10 | +#include "core/hybrid/hybrid_quantities.hpp" |
| 11 | +#include "core/utilities/types.hpp" |
| 12 | +#include "core/data/ions/particle_initializers/particle_initializer.hpp" |
| 13 | +#include "core/data/particles/particle.hpp" |
| 14 | +#include "initializer/data_provider.hpp" |
| 15 | +#include "core/utilities/point/point.hpp" |
| 16 | +#include "core/def.hpp" |
| 17 | +#include "core/logger.hpp" |
| 18 | + |
| 19 | +#include "hdf5/detail/h5/h5_file.hpp" |
| 20 | +#include "amr/utilities/box/amr_box.hpp" |
| 21 | + |
| 22 | +#include "SAMRAI/hier/PatchDataRestartManager.h" |
| 23 | + |
| 24 | + |
| 25 | +namespace PHARE::amr |
| 26 | +{ |
| 27 | + |
| 28 | +/* |
| 29 | +example paths |
| 30 | +
|
| 31 | +/PHARE_hierarchy/level_0000/level_0000-patch_0000000-block_0000000/protons##default/d_box |
| 32 | +/PHARE_hierarchy/level_0000/level_0000-patch_0000000-block_0000000/protons##default/d_ghost_box |
| 33 | +/PHARE_hierarchy/level_0000/level_0000-patch_0000000-block_0000000/protons##default/d_ghosts |
| 34 | +/PHARE_hierarchy/level_0000/level_0000-patch_0000000-block_0000000/protons##default/d_timestamp |
| 35 | +/PHARE_hierarchy/level_0000/level_0000-patch_0000000-block_0000000/protons##default/domainParticles_charge |
| 36 | +/PHARE_hierarchy/level_0000/level_0000-patch_0000000-block_0000000/protons##default/domainParticles_delta |
| 37 | +/PHARE_hierarchy/level_0000/level_0000-patch_0000000-block_0000000/protons##default/domainParticles_iCell |
| 38 | +/PHARE_hierarchy/level_0000/level_0000-patch_0000000-block_0000000/protons##default/domainParticles_v |
| 39 | +/PHARE_hierarchy/level_0000/level_0000-patch_0000000-block_0000000/protons##default/domainParticles_weight |
| 40 | +
|
| 41 | +
|
| 42 | +/PHARE_hierarchy/level_0000/level_0000-patch_0000000-block_0000000/EM_B_y##default/d_box |
| 43 | +/PHARE_hierarchy/level_0000/level_0000-patch_0000000-block_0000000/EM_B_x##default/field_EM_B_x |
| 44 | +/PHARE_hierarchy/level_0000/level_0000-patch_0000000-block_0000000/EM_B_y##default/field_EM_B_y |
| 45 | +/PHARE_hierarchy/level_0000/level_0000-patch_0000000-block_0000000/EM_B_z##default/field_EM_B_z |
| 46 | +*/ |
| 47 | + |
| 48 | + |
| 49 | +template<typename ParticleArray, typename GridLayout> |
| 50 | +class SamraiH5Interface |
| 51 | +{ |
| 52 | + struct SamraiHDF5File; // : PHARE::hdf5::h5::HighFiveFile; |
| 53 | + |
| 54 | +public: |
| 55 | + static SamraiH5Interface& INSTANCE() |
| 56 | + { |
| 57 | + static SamraiH5Interface i; |
| 58 | + return i; |
| 59 | + } |
| 60 | + |
| 61 | + void populate_from(std::string const& dir, int const& idx, int const& mpi_size); |
| 62 | + |
| 63 | + NO_DISCARD auto static getRestartFileFullPath(std::string path, int const& idx, |
| 64 | + int const& mpi_size, int const& rank) |
| 65 | + { |
| 66 | + return path // |
| 67 | + + "/restore." + SAMRAI::tbox::Utilities::intToString(idx, 6) // |
| 68 | + + "/nodes." + SAMRAI::tbox::Utilities::nodeToString(mpi_size) // |
| 69 | + + "/proc." + SAMRAI::tbox::Utilities::processorToString(rank); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +private: |
| 75 | + std::vector<std::unique_ptr<SamraiHDF5File>> restart_files; |
| 76 | + std::unordered_map<std::string, std::string> box2dataset; |
| 77 | +}; |
| 78 | + |
| 79 | +template<typename ParticleArray, typename GridLayout> |
| 80 | +struct SamraiH5Interface<ParticleArray, GridLayout>::SamraiHDF5File : public hdf5::h5::HighFiveFile |
| 81 | +{ |
| 82 | + using Super = hdf5::h5::HighFiveFile; |
| 83 | + using Box_t = core::Box<int, ParticleArray::dimension>; |
| 84 | + |
| 85 | + SamraiHDF5File(std::string const& filepath) |
| 86 | + : Super{filepath, HighFive::File::ReadOnly, /*para=*/false} |
| 87 | + { |
| 88 | + // auto box_type = box_compound_type(); |
| 89 | + // box_type.commit(file(), "d_box"); |
| 90 | + |
| 91 | + // getBoxFromPath("/PHARE_hierarchy/level_0000/level_0000-patch_0000000-block_0000000/" |
| 92 | + // "EM_B_y##default/d_box"); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + /* |
| 98 | + DATASET "d_box" { |
| 99 | + DATATYPE H5T_COMPOUND { |
| 100 | + H5T_STD_I32BE "dim"; |
| 101 | + H5T_ARRAY { [3] H5T_STD_I32BE } "lo"; |
| 102 | + H5T_ARRAY { [3] H5T_STD_I32BE } "hi"; |
| 103 | + } |
| 104 | + DATASPACE SIMPLE { ( 1 ) / ( 1 ) } |
| 105 | + DATA { |
| 106 | + (0): { |
| 107 | + 1, |
| 108 | + [ 0, 0, 0 ], |
| 109 | + [ 199, 0, 0 ] |
| 110 | + } |
| 111 | + } |
| 112 | + ATTRIBUTE "Type" { |
| 113 | + DATATYPE H5T_STD_I8BE |
| 114 | + DATASPACE SCALAR |
| 115 | + DATA { |
| 116 | + (0): 2 |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + */ |
| 121 | + |
| 122 | + struct BoxData |
| 123 | + { |
| 124 | + std::int32_t dim; |
| 125 | + std::array<std::int32_t, 3> lo; |
| 126 | + std::array<std::int32_t, 3> hi; |
| 127 | + }; |
| 128 | + |
| 129 | + // struct BoxData |
| 130 | + // { |
| 131 | + // int dim; |
| 132 | + // int lo0, lo1, lo2; |
| 133 | + // int hi0, hi1, hi2; |
| 134 | + // }; |
| 135 | + |
| 136 | + HighFive::CompoundType static box_compound_type() |
| 137 | + { |
| 138 | + return {{"dim", HighFive::create_datatype<std::int32_t>()}, |
| 139 | + {"lo", HighFive::create_datatype<std::array<std::int32_t, 3>>()}, |
| 140 | + {"hi", HighFive::create_datatype<std::array<std::int32_t, 3>>()}}; |
| 141 | + } |
| 142 | + |
| 143 | + auto getBoxFromPath(std::string const& path) const |
| 144 | + { |
| 145 | + // auto const& data = Super::read_data_set<int>(path); |
| 146 | + // PHARE_LOG_LINE_STR(data.size()); |
| 147 | + std::vector<BoxData> boxes; |
| 148 | + Super::file().getDataSet(path).read(boxes); |
| 149 | + assert(boxes.size() == 1); |
| 150 | + |
| 151 | + // auto const& bx = boxes[0]; |
| 152 | + // return Box_t{core::as_sized_array<ParticleArray::dimension>(bx.lo0, bx.lo1, bx.lo2), |
| 153 | + // core::as_sized_array<ParticleArray::dimension>(bx.hi0, bx.hi1, bx.hi2)}; |
| 154 | + |
| 155 | + return Box_t{core::sized_array<ParticleArray::dimension>(boxes[0].lo), |
| 156 | + core::sized_array<ParticleArray::dimension>(boxes[0].hi)}; |
| 157 | + } |
| 158 | +}; |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | +template<typename ParticleArray, typename GridLayout> |
| 163 | +void SamraiH5Interface<ParticleArray, GridLayout>::populate_from(std::string const& dir, |
| 164 | + int const& idx, |
| 165 | + int const& mpi_size) |
| 166 | +{ |
| 167 | + for (int rank = 0; rank < mpi_size; ++rank) |
| 168 | + { |
| 169 | + auto const hdf5_filepath = getRestartFileFullPath(dir, idx, mpi_size, rank); |
| 170 | + |
| 171 | + hdf5::h5::HighFiveFile h5File{hdf5_filepath, HighFive::File::ReadOnly, /*para=*/false}; |
| 172 | + |
| 173 | + PHARE_LOG_LINE_STR("SamraiH5Interface::populate_from"); |
| 174 | + |
| 175 | + auto groups = h5File.scan_for_groups({"level_0000", "domainParticles_charge"}); |
| 176 | + |
| 177 | + for (auto const& g : groups) |
| 178 | + { |
| 179 | + PHARE_LOG_LINE_STR(g); |
| 180 | + } |
| 181 | + |
| 182 | + restart_files.emplace_back(std::make_unique<SamraiHDF5File>(hdf5_filepath)); |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | +} // namespace PHARE::amr |
| 190 | + |
| 191 | + |
| 192 | +#endif |
0 commit comments