12 #ifndef CD_DischargeIOImplem_H
13 #define CD_DischargeIOImplem_H
22 #include <CD_NamespaceHeader.H>
24 template <
size_t M,
size_t N>
28 const std::vector<std::string> a_realVars,
29 const std::vector<std::string> a_vectVars,
30 const RealVect a_shift,
31 const Real a_time) noexcept
34 CH_TIME(
"DischargeIO::writeH5Part");
36 CH_assert(a_realVars.size() == 0 || a_realVars.size() == M);
37 CH_assert(a_vectVars.size() == 0 || a_vectVars.size() == N);
39 std::vector<std::string> realVariables(M);
40 std::vector<std::string> vectVariables(N);
42 if (a_realVars.size() == M) {
43 realVariables = a_realVars;
45 for (
int i = 0; i < M; i++) {
46 if (realVariables[i] ==
"") {
47 realVariables[i] =
"real-" + std::to_string(i);
52 for (
int i = 0; i < M; i++) {
53 realVariables[i] =
"real-" + std::to_string(i);
57 if (a_vectVars.size() == N) {
58 vectVariables = a_vectVars;
60 for (
int i = 0; i < N; i++) {
61 if (vectVariables[i] ==
"") {
62 vectVariables[i] =
"vect-" + std::to_string(i);
67 for (
int i = 0; i < N; i++) {
68 vectVariables[i] =
"vect-" + std::to_string(i);
73 const unsigned long long numParticlesLocal = a_particles.getNumberOfValidParticlesLocal();
74 const unsigned long long numParticlesGlobal = a_particles.getNumberOfValidParticlesGlobal();
76 std::vector<unsigned long long> particlesPerRank;
78 particlesPerRank.resize(numProc(), 0ULL);
80 std::vector<int> recv(numProc(), 1);
81 std::vector<int> displ(numProc(), 0);
82 for (
int i = 0; i < numProc(); i++) {
85 MPI_Allgatherv(&numParticlesLocal,
87 MPI_UNSIGNED_LONG_LONG,
91 MPI_UNSIGNED_LONG_LONG,
94 particlesPerRank.resize(1);
95 particlesPerRank[0] = numParticlesGlobal;
101 fileAccess = H5Pcreate(H5P_FILE_ACCESS);
102 H5Pset_fapl_mpio(fileAccess, Chombo_MPI::comm, MPI_INFO_NULL);
105 hid_t fileID = H5Fcreate(a_filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, fileAccess);
106 H5Pclose(fileAccess);
109 hid_t grp = H5Gcreate2(fileID,
"Step#0", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
112 hid_t scal = H5Screate(H5S_SCALAR);
113 hid_t attr = H5Acreate(grp,
"time", H5T_NATIVE_DOUBLE, scal, H5P_DEFAULT, H5P_DEFAULT);
114 herr_t err = H5Awrite(attr, H5T_NATIVE_DOUBLE, &a_time);
121 dims[0] = numParticlesGlobal;
123 hid_t fileSpaceID = H5Screate_simple(1, dims,
nullptr);
127 memDims[0] = numParticlesLocal;
128 hid_t memSpaceID = H5Screate_simple(1, memDims,
nullptr);
131 hsize_t fileStart[1];
132 hsize_t fileCount[1];
139 for (
int i = 0; i < procID(); i++) {
140 fileStart[0] += particlesPerRank[i];
142 fileCount[0] = particlesPerRank[procID()];
143 memCount[0] = particlesPerRank[procID()];
145 H5Sselect_hyperslab(fileSpaceID, H5S_SELECT_SET, fileStart,
nullptr, fileCount,
nullptr);
146 H5Sselect_hyperslab(memSpaceID, H5S_SELECT_SET, memStart,
nullptr, memCount,
nullptr);
149 hid_t datasetID = H5Dcreate2(grp,
"id", H5T_NATIVE_ULLONG, fileSpaceID, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
150 hid_t datasetX = H5Dcreate2(grp,
"x", H5T_NATIVE_DOUBLE, fileSpaceID, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
151 hid_t datasetY = H5Dcreate2(grp,
"y", H5T_NATIVE_DOUBLE, fileSpaceID, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
153 hid_t datasetZ = H5Dcreate2(grp,
"z", H5T_NATIVE_DOUBLE, fileSpaceID, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
157 std::vector<unsigned long long> id;
158 std::vector<double> x;
159 std::vector<double> y;
160 std::vector<double> z;
162 for (
int lvl = 0; lvl <= a_particles.getFinestLevel(); lvl++) {
164 const DisjointBoxLayout& dbl = a_particles.getGrids()[lvl];
165 const DataIterator& dit = dbl.dataIterator();
167 const int nbox = dit.size();
169 for (
int mybox = 0; mybox < nbox; mybox++) {
170 const DataIndex& din = dit[mybox];
172 const List<GenericParticle<M, N>>& particles = a_particles[lvl][din].listItems();
175 id.push_back(procID());
176 x.push_back(lit().
position()[0] - a_shift[0]);
177 y.push_back(lit().
position()[1] - a_shift[1]);
179 z.push_back(lit().
position()[2] - a_shift[2]);
185 H5Dwrite(datasetID, H5Dget_type(datasetID), memSpaceID, fileSpaceID, H5P_DEFAULT, &
id[0]);
186 H5Dwrite(datasetX, H5Dget_type(datasetX), memSpaceID, fileSpaceID, H5P_DEFAULT, &x[0]);
187 H5Dwrite(datasetY, H5Dget_type(datasetY), memSpaceID, fileSpaceID, H5P_DEFAULT, &y[0]);
189 H5Dwrite(datasetZ, H5Dget_type(datasetY), memSpaceID, fileSpaceID, H5P_DEFAULT, &z[0]);
206 for (
int curVar = 0; curVar < M; curVar++) {
207 hid_t dataset = H5Dcreate2(grp,
208 realVariables[curVar].c_str(),
215 std::vector<double> ds;
217 for (
int lvl = 0; lvl <= a_particles.getFinestLevel(); lvl++) {
219 const DisjointBoxLayout& dbl = a_particles.getGrids()[lvl];
220 const DataIterator& dit = dbl.dataIterator();
222 const int nbox = dit.size();
224 for (
int mybox = 0; mybox < nbox; mybox++) {
225 const DataIndex& din = dit[mybox];
227 const List<GenericParticle<M, N>>& particles = a_particles[lvl][din].listItems();
230 ds.push_back(lit().getReals()[curVar]);
236 H5Dwrite(dataset, H5Dget_type(dataset), memSpaceID, fileSpaceID, H5P_DEFAULT, &ds[0]);
241 for (
int curVar = 0; curVar < N; curVar++) {
242 for (
int dir = 0; dir < SpaceDim; dir++) {
245 varName = vectVariables[curVar] +
"-x";
248 varName = vectVariables[curVar] +
"-y";
251 varName = vectVariables[curVar] +
"-z";
254 hid_t dataset = H5Dcreate2(grp,
262 std::vector<double> ds;
264 for (
int lvl = 0; lvl <= a_particles.getFinestLevel(); lvl++) {
266 const DisjointBoxLayout& dbl = a_particles.getGrids()[lvl];
267 const DataIterator& dit = dbl.dataIterator();
269 const int nbox = dit.size();
271 for (
int mybox = 0; mybox < nbox; mybox++) {
272 const DataIndex& din = dit[mybox];
274 const List<GenericParticle<M, N>>& particles = a_particles[lvl][din].listItems();
277 const RealVect var = lit().getVects()[curVar];
279 ds.push_back(var[dir]);
285 H5Dwrite(dataset, H5Dget_type(dataset), memSpaceID, fileSpaceID, H5P_DEFAULT, &ds[0]);
296 #include <CD_NamespaceFooter.H>
Silly, but useful functions that override standard Chombo HDF5 IO.
A generic particle class, holding the position and a specified number of real and vector values.
Definition: CD_GenericParticle.H:33
Templated class for holding particles on an AMR hierarchy with particle remapping.
Definition: CD_ParticleContainer.H:50
void writeH5Part(const std::string a_filename, const ParticleContainer< GenericParticle< M, N >> &a_particles, const std::vector< std::string > a_realVars=std::vector< std::string >(), const std::vector< std::string > a_vectVars=std::vector< std::string >(), const RealVect a_shift=RealVect::Zero, const Real a_time=0.0) noexcept
A shameless copy of Chombo's writeEBHDF5 but including the lower-left corner of the physical domain a...
Definition: CD_DischargeIOImplem.H:26
RealVect position(const Location::Cell a_location, const VolIndex &a_vof, const EBISBox &a_ebisbox, const Real &a_dx)
Compute the position (ignoring the "origin) of a Vof.
Definition: CD_LocationImplem.H:20