|
| 1 | +#ifndef PHARE_DIAGNOSTIC_DETAIL_VTK_H5_TYPE_WRITER_HPP |
| 2 | +#define PHARE_DIAGNOSTIC_DETAIL_VTK_H5_TYPE_WRITER_HPP |
| 3 | + |
| 4 | +#include <string> |
| 5 | +#include <unordered_map> |
| 6 | + |
| 7 | +#include "core/utilities/mpi_utils.hpp" |
| 8 | + |
| 9 | +#include "diagnostic/diagnostic_writer.hpp" |
| 10 | + |
| 11 | +#include "hdf5/detail/h5/h5_file.hpp" |
| 12 | + |
| 13 | + |
| 14 | +namespace PHARE::diagnostic::vtkh5 |
| 15 | +{ |
| 16 | +using namespace hdf5::h5; |
| 17 | + |
| 18 | +template<typename Writer> |
| 19 | +class H5TypeWriter : public PHARE::diagnostic::TypeWriter |
| 20 | +{ |
| 21 | +public: |
| 22 | + using Attributes = typename Writer::Attributes; |
| 23 | + static constexpr auto dimension = Writer::dimension; |
| 24 | + |
| 25 | + H5TypeWriter(Writer& h5Writer) |
| 26 | + : h5Writer_{h5Writer} |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + //------ defined by each concrete H5TypeWriter--------------------------- |
| 31 | + // virtual void createFiles(DiagnosticProperties& diagnostic) = 0; |
| 32 | + |
| 33 | + // virtual void getDataSetInfo(DiagnosticProperties& diagnostic, std::size_t iLevel, |
| 34 | + // std::string const& patchID, Attributes& patchAttributes) |
| 35 | + // = 0; |
| 36 | + |
| 37 | + // virtual void |
| 38 | + // initDataSets(DiagnosticProperties& diagnostic, |
| 39 | + // std::unordered_map<std::size_t, std::vector<std::string>> const& patchIDs, |
| 40 | + // Attributes& patchAttributes, std::size_t maxLevel) |
| 41 | + // = 0; |
| 42 | + |
| 43 | + // virtual void writeAttributes( |
| 44 | + // DiagnosticProperties&, Attributes&, |
| 45 | + // std::unordered_map<std::size_t, std::vector<std::pair<std::string, Attributes>>>&, |
| 46 | + // std::size_t maxLevel) |
| 47 | + // = 0; |
| 48 | + //------------------------------------------------------------------------ |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + //------ valid for all h5type writers ------------------------------------- |
| 53 | + void finalize(DiagnosticProperties& diagnostic) |
| 54 | + { |
| 55 | + // // we close the file by removing the associated file |
| 56 | + // // from the map. This is done only at flush time otherwise |
| 57 | + // ++diagnostic.dumpIdx; |
| 58 | + |
| 59 | + // assert(diagnostic.params.contains("flush_every")); |
| 60 | + |
| 61 | + // std::size_t flushEvery = diagnostic.param<std::size_t>("flush_every"); |
| 62 | + |
| 63 | + // if (flushEvery != Writer::flush_never and diagnostic.dumpIdx % flushEvery == 0) |
| 64 | + // { |
| 65 | + // fileData_.erase(diagnostic.quantity); |
| 66 | + // assert(fileData_.count(diagnostic.quantity) == 0); |
| 67 | + // } |
| 68 | + } |
| 69 | + //------------------------------------------------------------------------ |
| 70 | + |
| 71 | + |
| 72 | +protected: |
| 73 | + template<typename InitPatch> |
| 74 | + void initDataSets_(std::unordered_map<std::size_t, std::vector<std::string>> const& patchIDs, |
| 75 | + Attributes& patchAttributes, std::size_t maxLevel, InitPatch&& initPatch) |
| 76 | + { |
| 77 | + // for (std::size_t lvl = h5Writer_.minLevel; lvl <= maxLevel; lvl++) |
| 78 | + // { |
| 79 | + // auto& lvlPatches = patchIDs.at(lvl); |
| 80 | + // std::size_t patchNbr = lvlPatches.size(); |
| 81 | + // std::size_t maxPatches = core::mpi::max(patchNbr); |
| 82 | + // for (std::size_t i = 0; i < patchNbr; ++i) |
| 83 | + // initPatch(lvl, patchAttributes[std::to_string(lvl) + "_" + lvlPatches[i]], |
| 84 | + // lvlPatches[i]); |
| 85 | + // for (std::size_t i = patchNbr; i < maxPatches; i++) |
| 86 | + // initPatch(lvl, patchAttributes); |
| 87 | + // } |
| 88 | + } |
| 89 | + |
| 90 | + void writeAttributes_( |
| 91 | + DiagnosticProperties& diagnostic, HighFiveFile& file, Attributes& fileAttributes, |
| 92 | + std::unordered_map<std::size_t, std::vector<std::pair<std::string, Attributes>>>& |
| 93 | + patchAttributes, |
| 94 | + std::size_t maxLevel, Attributes defaultPatchAttributes = {}) |
| 95 | + { |
| 96 | + // for (std::size_t lvl = h5Writer_.minLevel; lvl <= maxLevel; lvl++) |
| 97 | + // { |
| 98 | + // auto& lvlPatches = patchAttributes.at(lvl); |
| 99 | + // std::size_t patchNbr = lvlPatches.size(); |
| 100 | + // std::size_t maxPatches = core::mpi::max(patchNbr); |
| 101 | + // for (auto const& [patch, attr] : lvlPatches) |
| 102 | + // h5Writer_.writeAttributeDict(file, attr, |
| 103 | + // h5Writer_.getPatchPathAddTimestamp(lvl, patch)); |
| 104 | + // for (std::size_t i = patchNbr; i < maxPatches; i++) |
| 105 | + // h5Writer_.writeAttributeDict( |
| 106 | + // file, h5Writer_.modelView().getEmptyPatchProperties(defaultPatchAttributes), |
| 107 | + // ""); |
| 108 | + // } |
| 109 | + |
| 110 | + // if (diagnostic.nAttributes > 0) |
| 111 | + // h5Writer_.writeAttributeDict(file, diagnostic.fileAttributes, "/py_attrs"); |
| 112 | + |
| 113 | + // h5Writer_.writeGlobalAttributeDict(file, fileAttributes, "/"); |
| 114 | + } |
| 115 | + |
| 116 | + template<typename ParticlePopulation> |
| 117 | + void writeIonPopAttributes_(HighFiveFile& file, ParticlePopulation const& pop) |
| 118 | + { |
| 119 | + // auto& h5Writer = this->h5Writer_; |
| 120 | + |
| 121 | + // Attributes popAttributes; |
| 122 | + // popAttributes["pop_mass"] = pop.mass(); |
| 123 | + // h5Writer.writeGlobalAttributeDict(file, popAttributes, "/"); |
| 124 | + } |
| 125 | + |
| 126 | + void writeGhostsAttr_(HighFiveFile& file, std::string path, std::size_t ghosts, bool null) |
| 127 | + { |
| 128 | + // Attributes dsAttr; |
| 129 | + // dsAttr["ghosts"] = ghosts; |
| 130 | + // h5Writer_.writeAttributeDict(file, dsAttr, null ? "" : path); |
| 131 | + } |
| 132 | + |
| 133 | + template<typename FileMap, typename... Quantities> |
| 134 | + void checkCreateFileFor_(DiagnosticProperties const& diagnostic, FileMap& fileData, |
| 135 | + std::string const tree, Quantities const... vars) |
| 136 | + { |
| 137 | + core::apply(std::forward_as_tuple(vars...), [&](auto const& var) { |
| 138 | + if (diagnostic.quantity == tree + var and !fileData.count(diagnostic.quantity)) |
| 139 | + fileData.emplace(diagnostic.quantity, this->h5Writer_.makeFile(diagnostic)); |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + auto& h5FileForQuantity(DiagnosticProperties& diagnostic) |
| 145 | + { |
| 146 | + if (!fileData_.count(diagnostic.quantity)) |
| 147 | + throw std::runtime_error("Unknown Diagnostic Quantity: " + diagnostic.quantity); |
| 148 | + |
| 149 | + return *fileData_.at(diagnostic.quantity); |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | + Writer& h5Writer_; |
| 154 | + std::unordered_map<std::string, std::unique_ptr<HighFiveFile>> fileData_; |
| 155 | +}; |
| 156 | + |
| 157 | +} // namespace PHARE::diagnostic::vtkh5 |
| 158 | + |
| 159 | +#endif // PHARE_DIAGNOSTIC_DETAIL_VTK_H5_TYPE_WRITER_HPP |
0 commit comments