Skip to content

Commit 724d43d

Browse files
author
Martin D. Weinberg
committed
A few additional tweaks; ready for testing
1 parent 05c788c commit 724d43d

2 files changed

Lines changed: 107 additions & 49 deletions

File tree

src/OutHDF5.H

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
@param nbeg is suffix of the first phase space %dump
1313
@param timer set to true turns on wall-clock timer for PS output
1414
@param threads number of threads for binary writes
15+
@param noids set to true turns off particle id writing
1516
1617
*/
1718
class OutHDF5 : public Output
@@ -20,13 +21,18 @@ class OutHDF5 : public Output
2021
private:
2122

2223
std::string filename;
23-
bool real4=true, real8=false, timer;
24+
bool real4=true, real8=false, ids=true, timer;
2425
int nbeg, threads;
2526
void initialize(void);
27+
std::vector<double> masses;
28+
std::vector<bool> multim;
2629

2730
//! Valid keys for YAML configurations
2831
static const std::set<std::string> valid_keys;
2932

33+
//! Check for single particle mass on the first invocation
34+
void checkParticleMasses();
35+
3036
public:
3137

3238
//! Constructor
@@ -41,6 +47,7 @@ public:
4147
indepentently of whether or not the frequency criterion is met
4248
\param timer set to true turns on wall-clock timer for PS output
4349
\param threads is the thread count for binary writes
50+
\param noids set to true turns off particle id writing
4451
*/
4552
void Run(int nstep, int mstep, bool last);
4653

src/OutHDF5.cc

Lines changed: 99 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ OutHDF5::valid_keys = {
2020
"nint",
2121
"nintsub",
2222
"nbeg",
23+
"noids",
2324
"real4",
2425
"real8",
2526
"timer",
@@ -82,6 +83,10 @@ void OutHDF5::initialize()
8283
real4 = not real8;
8384
}
8485

86+
if (Output::conf["noids"]) {
87+
ids = not Output::conf["noids"].as<bool>();
88+
}
89+
8590
if (Output::conf["timer"])
8691
timer = Output::conf["timer"].as<bool>();
8792
else
@@ -152,8 +157,8 @@ void OutHDF5::Run(int n, int mstep, bool last)
152157
std::ostringstream fname;
153158

154159
// Output name prefix
155-
fname << filename << "." << setw(5) << setfill('0') << nbeg++
156-
<< "." << myid+1;
160+
fname << filename << "." << setw(5) << setfill('0') << nbeg++;
161+
if (numprocs>1) fname << "." << myid+1;
157162

158163
// Master file name
159164
std::string path = outdir + fname.str();
@@ -177,77 +182,90 @@ void OutHDF5::Run(int n, int mstep, bool last)
177182
HighFive::File::Create);
178183
try {
179184

180-
// Create a new group for Config
181-
//
182-
HighFive::Group config = file->createGroup("Config");
183-
184-
int ncomp = comp->components.size();
185-
config.createAttribute<int>("NTYPES",
186-
HighFive::DataSpace::From(ncomp)).write(ncomp);
187-
188-
std::string gcommit(GIT_COMMIT), gbranch(GIT_BRANCH), gdate(COMPILE_TIME);
189-
config.createAttribute<std::string>("Git_commit",
190-
HighFive::DataSpace::From(gcommit)).write(gcommit);
191-
192-
config.createAttribute<std::string>("Git_branch",
193-
HighFive::DataSpace::From(gbranch)).write(gbranch);
194-
195-
config.createAttribute<std::string>("Compile_date",
196-
HighFive::DataSpace::From(gdate)).write(gdate);
197-
198185
// Create a new group for Header
199186
//
200187
HighFive::Group header = file->createGroup("Header");
201188
int dp = 1;
202-
header.createAttribute<int>("Flag_DoublePrecision",
203-
HighFive::DataSpace::From(dp)).write(dp);
189+
header.createAttribute<int>
190+
("Flag_DoublePrecision", HighFive::DataSpace::From(dp)).write(dp);
204191

205192
double hubble = 1, zero = 0;
206-
header.createAttribute<double>("HubbleParam",
207-
HighFive::DataSpace::From(hubble)).write(hubble);
193+
header.createAttribute<double>
194+
("HubbleParam", HighFive::DataSpace::From(hubble)).write(hubble);
208195

209-
header.createAttribute<double>("Omega0",
210-
HighFive::DataSpace::From(zero)).write(zero);
196+
header.createAttribute<double>
197+
("Omega0", HighFive::DataSpace::From(zero)).write(zero);
211198

212-
header.createAttribute<double>("OmegaBaryon",
213-
HighFive::DataSpace::From(zero)).write(zero);
199+
header.createAttribute<double>
200+
("OmegaBaryon", HighFive::DataSpace::From(zero)).write(zero);
214201

215-
header.createAttribute<double>("OmegaLambda",
216-
HighFive::DataSpace::From(zero)).write(zero);
202+
header.createAttribute<double>
203+
("OmegaLambda", HighFive::DataSpace::From(zero)).write(zero);
217204

218-
header.createAttribute<double>("Redshift",
219-
HighFive::DataSpace::From(zero)).write(zero);
205+
header.createAttribute<double>
206+
("Redshift", HighFive::DataSpace::From(zero)).write(zero);
220207

221-
std::vector<double> masses(ncomp, 0.0);
222-
header.createAttribute<std::vector<double>>("MassTable",
223-
HighFive::DataSpace::From(masses)).write(masses);
208+
if (masses.size()==0) checkParticleMasses();
209+
header.createAttribute
210+
<std::vector<double>>("MassTable", HighFive::DataSpace::From(masses)).write(masses);
224211

225-
header.createAttribute<int>("NumFilesPerSnapshot",
226-
HighFive::DataSpace::From(numprocs)).write(numprocs);
212+
header.createAttribute<int>
213+
("NumFilesPerSnapshot", HighFive::DataSpace::From(numprocs)).write(numprocs);
227214

228-
std::vector<unsigned> nums(ncomp);
215+
std::vector<unsigned> nums(masses.size());
229216
{
230217
int n=0;
231218
for (auto c : comp->components) nums[n++] = c->Number();
232219
}
233-
header.createAttribute<std::vector<unsigned>>("NumPart_ThisFile",
234-
HighFive::DataSpace::From(nums)).write(nums);
220+
header.createAttribute<std::vector<unsigned>>
221+
("NumPart_ThisFile", HighFive::DataSpace::From(nums)).write(nums);
235222
{
236223
int n=0;
237224
for (auto c : comp->components) nums[n++] = c->CurTotal();
238225
}
239-
header.createAttribute<std::vector<unsigned>>("NumPart_Total",
240-
HighFive::DataSpace::From(nums)).write(nums);
226+
header.createAttribute<std::vector<unsigned>>
227+
("NumPart_Total", HighFive::DataSpace::From(nums)).write(nums);
241228

242-
header.createAttribute<double>("Time",
243-
HighFive::DataSpace::From(tnow)).write(tnow);
229+
header.createAttribute<double>
230+
("Time", HighFive::DataSpace::From(tnow)).write(tnow);
244231

245232
// Create a new group for Parameters
246233
//
247234
HighFive::Group params = file->createGroup("Parameters");
248235

236+
std::string gcommit(GIT_COMMIT), gbranch(GIT_BRANCH), gdate(COMPILE_TIME);
237+
params.createAttribute<std::string>("Git_commit",
238+
HighFive::DataSpace::From(gcommit)).write(gcommit);
239+
240+
params.createAttribute<std::string>("Git_branch",
241+
HighFive::DataSpace::From(gbranch)).write(gbranch);
242+
243+
params.createAttribute<std::string>("Compile_date",
244+
HighFive::DataSpace::From(gdate)).write(gdate);
245+
246+
std::vector<std::string> names, forces, configs;
247+
for (auto c : comp->components) {
248+
names.push_back(c->name);
249+
forces.push_back(c->id);
250+
YAML::Emitter out;
251+
out << c->fconf; // where node is your YAML::Node
252+
configs.push_back(out.c_str());
253+
}
254+
255+
params.createAttribute<std::string>
256+
("ComponentNames",
257+
HighFive::DataSpace::From(names)).write(names);
258+
259+
params.createAttribute<std::string>
260+
("ForceMethods",
261+
HighFive::DataSpace::From(forces)).write(forces);
262+
263+
params.createAttribute<std::string>
264+
("ForceConfigurations",
265+
HighFive::DataSpace::From(configs)).write(configs);
266+
249267
} catch (HighFive::Exception& err) {
250-
std::string msg("Coefs::factory: error reading HDF5 file, ");
268+
std::string msg("Coefs::factory: error writing HDF5 file, ");
251269
throw std::runtime_error(msg + err.what());
252270
}
253271

@@ -257,7 +275,7 @@ void OutHDF5::Run(int n, int mstep, bool last)
257275
nOK = 1;
258276
}
259277

260-
MPI_Allreduce(0, &nOK, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
278+
MPI_Allreduce(MPI_IN_PLACE, &nOK, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
261279

262280
// Exit on file open failure
263281
//
@@ -285,9 +303,9 @@ void OutHDF5::Run(int n, int mstep, bool last)
285303
auto pgroup = file->createGroup(sout.str());
286304

287305
if (real4)
288-
c->write_HDF5<float>(pgroup, true, true);
306+
c->write_HDF5<float >(pgroup, multim[count], ids);
289307
else
290-
c->write_HDF5<double>(pgroup, true, true);
308+
c->write_HDF5<double>(pgroup, multim[count], ids);
291309
}
292310

293311
chktimer.mark();
@@ -303,3 +321,36 @@ void OutHDF5::Run(int n, int mstep, bool last)
303321
}
304322
}
305323

324+
void OutHDF5::checkParticleMasses()
325+
{
326+
for (auto c : comp->components) {
327+
// First bunch of particles
328+
int number = -1;
329+
PartPtr *p = c->get_particles(&number);
330+
331+
double minMass = std::numeric_limits<double>::max();
332+
double maxMass = 0.0;
333+
334+
// Keep going...
335+
while (p) {
336+
for (int k=0; k<number; k++) {
337+
auto &P = *p;
338+
minMass = std::min(P->mass, minMass);
339+
maxMass = std::max(P->mass, maxMass);
340+
}
341+
// Next bunch of particles
342+
p = c->get_particles(&number);
343+
}
344+
345+
MPI_Allreduce(MPI_IN_PLACE, &minMass, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
346+
MPI_Allreduce(MPI_IN_PLACE, &maxMass, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
347+
348+
if ( (maxMass - minMass)/maxMass < 1.0e-12) {
349+
masses.push_back(maxMass);
350+
multim.push_back(true);
351+
} else {
352+
masses.push_back(0.0);
353+
multim.push_back(false);
354+
}
355+
}
356+
}

0 commit comments

Comments
 (0)