Skip to content

Commit cfad2a5

Browse files
author
Martin D. Weinberg
committed
Fixed a few errors in the PSPhdf5 version of the ParticleReader
1 parent 968090a commit cfad2a5

3 files changed

Lines changed: 195 additions & 98 deletions

File tree

exputil/ParticleReader.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ namespace PR {
705705
}
706706

707707

708-
PSPhdf5::PSPhdf5(const std::vector<std::string>& files, bool verbose) : ParticleReader()
708+
PSPhdf5::PSPhdf5(const std::vector<std::string>& files, bool verbose) : PSP(verbose)
709709
{
710710
_files = files;
711711
_verbose = verbose;

include/ParticleReader.H

Lines changed: 84 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -234,90 +234,6 @@ namespace PR
234234

235235
};
236236

237-
class PSPhdf5 : public ParticleReader
238-
{
239-
protected:
240-
241-
//@{
242-
//! Parameters
243-
double time;
244-
std::vector<Particle> particles;
245-
bool _verbose;
246-
std::vector<std::string> _files;
247-
248-
std::vector<double> mass;
249-
std::vector<int> Niattrib, Ndattrib;
250-
std::vector<unsigned long> npart, nptot;
251-
unsigned long totalCount;
252-
bool real4, gadget4;
253-
int ntypes, nfiles;
254-
//@}
255-
256-
//! Current file
257-
std::vector<std::string>::iterator curfile;
258-
259-
//! Components
260-
std::vector<std::string> comps;
261-
262-
//@{
263-
//! Current component and index
264-
std::string curcomp;
265-
int curindx;
266-
//@}
267-
268-
unsigned pcount;
269-
template <typename Scalar> void read_and_load();
270-
template <typename Scalar> void read_and_load_gadget4();
271-
template <typename Scalar> void read_and_load_psp();
272-
273-
void getInfo();
274-
void packParticle();
275-
bool nextFile();
276-
277-
public:
278-
279-
//! Constructor
280-
PSPhdf5(const std::vector<std::string>& file, bool verbose=false);
281-
282-
//! Select a particular particle type and reset the iterator
283-
virtual void SelectType(const std::string& type) {
284-
auto it = std::find(comps.begin(), comps.end(), type);
285-
if (it != comps.end()) {
286-
curcomp = type;
287-
curindx = it - comps.begin();
288-
}
289-
else {
290-
std::cerr << "PSPhdf5 error: could not find particle component <" << type << ">" << std::endl;
291-
std::cerr << "Available particle components are:";
292-
for (auto s : comps) std::cerr << " " << s;
293-
std::cerr << std::endl;
294-
throw std::runtime_error("PSPhdf5: non-existent component");
295-
}
296-
297-
curfile = _files.begin(); // Set to first file and open
298-
nextFile();
299-
}
300-
301-
//! Number of particles in the chosen type
302-
virtual unsigned long CurrentNumber() { return totalCount; }
303-
304-
//! Return list of particle types
305-
virtual std::vector<std::string> GetTypes() { return comps; }
306-
307-
//! Get current time
308-
virtual double CurrentTime() { return time; }
309-
310-
//! Reset to beginning of particles for this component
311-
virtual const Particle* firstParticle();
312-
313-
//! Get the next particle
314-
virtual const Particle* nextParticle();
315-
316-
//! Get file cound per snapshot
317-
virtual int NumFiles() { return nfiles; }
318-
319-
};
320-
321237
class PSPstanza
322238
{
323239
public:
@@ -646,6 +562,90 @@ namespace PR
646562
//@}
647563
};
648564

565+
class PSPhdf5 : public PSP
566+
{
567+
protected:
568+
569+
//@{
570+
//! Parameters
571+
double time;
572+
std::vector<Particle> particles;
573+
bool _verbose;
574+
std::vector<std::string> _files;
575+
576+
std::vector<double> mass;
577+
std::vector<int> Niattrib, Ndattrib;
578+
std::vector<unsigned long> npart, nptot;
579+
unsigned long totalCount;
580+
bool real4, gadget4;
581+
int ntypes, nfiles;
582+
//@}
583+
584+
//! Current file
585+
std::vector<std::string>::iterator curfile;
586+
587+
//! Components
588+
std::vector<std::string> comps;
589+
590+
//@{
591+
//! Current component and index
592+
std::string curcomp;
593+
int curindx;
594+
//@}
595+
596+
unsigned pcount;
597+
template <typename Scalar> void read_and_load();
598+
template <typename Scalar> void read_and_load_gadget4();
599+
template <typename Scalar> void read_and_load_psp();
600+
601+
void getInfo();
602+
void packParticle();
603+
bool nextFile();
604+
605+
public:
606+
607+
//! Constructor
608+
PSPhdf5(const std::vector<std::string>& file, bool verbose=false);
609+
610+
//! Select a particular particle type and reset the iterator
611+
virtual void SelectType(const std::string& type) {
612+
auto it = std::find(comps.begin(), comps.end(), type);
613+
if (it != comps.end()) {
614+
curcomp = type;
615+
curindx = it - comps.begin();
616+
}
617+
else {
618+
std::cerr << "PSPhdf5 error: could not find particle component <" << type << ">" << std::endl;
619+
std::cerr << "Available particle components are:";
620+
for (auto s : comps) std::cerr << " " << s;
621+
std::cerr << std::endl;
622+
throw std::runtime_error("PSPhdf5: non-existent component");
623+
}
624+
625+
curfile = _files.begin(); // Set to first file and open
626+
nextFile();
627+
}
628+
629+
//! Number of particles in the chosen type
630+
virtual unsigned long CurrentNumber() { return nptot[curindx]; }
631+
632+
//! Return list of particle types
633+
virtual std::vector<std::string> GetTypes() { return comps; }
634+
635+
//! Get current time
636+
virtual double CurrentTime() { return time; }
637+
638+
//! Reset to beginning of particles for this component
639+
virtual const Particle* firstParticle();
640+
641+
//! Get the next particle
642+
virtual const Particle* nextParticle();
643+
644+
//! Get file cound per snapshot
645+
virtual int NumFiles() { return nfiles; }
646+
647+
};
648+
649649
typedef std::shared_ptr<ParticleReader> PRptr;
650650
}
651651

pyEXP/ParticleReaderWrappers.cc

Lines changed: 110 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ void ParticleReaderClasses(py::module &m) {
1515
"The available particle readers are:\n"
1616
" 1. PSPout The monolithic EXP phase-space snapshot format\n"
1717
" 2. PSPspl Like PSPout, but split into multiple file chunks\n"
18-
" 3. GadgetNative The original Gadget native format\n"
19-
" 4 GadgetHDF5 The newer HDF5 Gadget format\n"
20-
" 5. TipsyNative The original Tipsy format\n"
21-
" 6. TipsyXDR The original XDR Tipsy format\n"
22-
" 7. Bonsai This is the Bonsai varient of Tipsy files\n\n"
18+
" 3. PSPhdf5 The HDF5 version of the EXP phase-space format\n"
19+
" 4. GadgetNative The original Gadget native format\n"
20+
" 5 GadgetHDF5 The newer HDF5 Gadget format\n"
21+
" 6. TipsyNative The original Tipsy format\n"
22+
" 7. TipsyXDR The original XDR Tipsy format\n"
23+
" 8. Bonsai This is the Bonsai varient of Tipsy files\n\n"
2324
"We have a helper function, getReaders, to get a list to help you\n"
2425
"remember. Try: pyEXP.read.ParticleReader.getReaders()\n\n"
2526
"Each reader can manage snapshots split into many files by parallel,\n"
@@ -216,6 +217,38 @@ void ParticleReaderClasses(py::module &m) {
216217
}
217218
};
218219

220+
class PyPSPhdf5 : public PSPhdf5
221+
{
222+
public:
223+
224+
// Inherit the constructors
225+
using PSPhdf5::PSPhdf5;
226+
227+
void SelectType(const std::string& type) override {
228+
PYBIND11_OVERRIDE(void, PSPhdf5, SelectType, type);
229+
}
230+
231+
std::vector<std::string> GetTypes() override {
232+
PYBIND11_OVERRIDE(std::vector<std::string>, PSPhdf5, GetTypes,);
233+
}
234+
235+
unsigned long CurrentNumber() override {
236+
PYBIND11_OVERRIDE(unsigned long, PSPhdf5, CurrentNumber,);
237+
}
238+
239+
double CurrentTime() override {
240+
PYBIND11_OVERRIDE(double, PSPhdf5, CurrentTime,);
241+
}
242+
243+
const Particle* firstParticle() override {
244+
PYBIND11_OVERRIDE(const Particle*, PSPhdf5, firstParticle,);
245+
}
246+
247+
const Particle* nextParticle() override {
248+
PYBIND11_OVERRIDE(const Particle*, PSPhdf5, nextParticle,);
249+
}
250+
};
251+
219252
class PyTipsy : public Tipsy
220253
{
221254
public:
@@ -401,8 +434,8 @@ void ParticleReaderClasses(py::module &m) {
401434
pr.def_static("getReaders", []()
402435
{
403436
const std::vector<std::string> formats = {
404-
"PSPout", "PSPspl", "GadgetNative", "GadgetHDF5", "TipsyNative",
405-
"TipsyXDR", "Bonsai"};
437+
"PSPout", "PSPspl", "PSPhdf5", "GadgetNative", "GadgetHDF5",
438+
"TipsyNative", "TipsyXDR", "Bonsai"};
406439

407440
return formats;
408441
},
@@ -431,7 +464,7 @@ void ParticleReaderClasses(py::module &m) {
431464
Returns
432465
-------
433466
ParticleReader
434-
)");
467+
)", py::arg("files"), py::arg("verbose")=false);
435468

436469
py::class_<GadgetNative, std::shared_ptr<GadgetNative>, PyGadgetNative, ParticleReader>(m, "GadgetNative")
437470
.def(py::init<const std::vector<std::string>&, bool>(),
@@ -448,7 +481,7 @@ void ParticleReaderClasses(py::module &m) {
448481
Returns
449482
-------
450483
ParticleReader
451-
)");
484+
)", py::arg("files"), py::arg("verbose")=false);
452485

453486
py::class_<PSP, std::shared_ptr<PSP>, PyPSP, ParticleReader>(m, "PSP")
454487
.def(py::init<bool>(), "Base class for PSP reader")
@@ -464,19 +497,67 @@ void ParticleReaderClasses(py::module &m) {
464497

465498
py::class_<PSPout, std::shared_ptr<PSPout>, PyPSPout, PSP>(m, "PSPout")
466499
.def(py::init<const std::vector<std::string>&, bool>(),
467-
"Reader for monolitic PSP format (single file written by root process)")
500+
R"(
501+
Read PSP ascii monolithic format snapshot files
502+
503+
Parameters
504+
----------
505+
files : list(str)
506+
List of files with phase-space segments comprising a single snapshot
507+
verbose : bool, default=False
508+
Verbose, diagnostic output
509+
510+
Returns
511+
-------
512+
ParticleReader
513+
)", py::arg("files"), py::arg("verbose")=false)
468514
.def("CurrentNumber", &PSPout::CurrentNumber)
469515
.def("GetTypes", &PSPout::GetTypes)
470516
.def("CurrentTime", &PSPout::CurrentTime);
471517

472518

473519
py::class_<PSPspl, std::shared_ptr<PSPspl>, PyPSPspl, PSP>(m, "PSPspl")
474520
.def(py::init<const std::vector<std::string>&, bool>(),
475-
"Reader for split PSP files (multiple files written by each process)")
521+
R"(
522+
Reader for split PSP files (multiple files written by each process)
523+
524+
Parameters
525+
----------
526+
files : list(str)
527+
List of files with phase-space segments comprising a single snapshot
528+
verbose : bool, default=False
529+
Verbose, diagnostic output
530+
531+
Returns
532+
-------
533+
ParticleReader
534+
)", py::arg("files"), py::arg("verbose")=false)
476535
.def("CurrentNumber", &PSPspl::CurrentNumber)
477536
.def("GetTypes", &PSPspl::GetTypes)
478537
.def("CurrentTime", &PSPspl::CurrentTime);
479538

539+
py::class_<PSPhdf5, std::shared_ptr<PSPhdf5>, PyPSPhdf5, PSP>(m, "PSPhdf5")
540+
.def(py::init<const std::vector<std::string>&, bool>(),
541+
R"(
542+
Reader for HDF5 PSP files (both single and multiple files)
543+
544+
Parameters
545+
----------
546+
files : list(str)
547+
List of files with phase-space segments comprising a single snapshot
548+
verbose : bool, default=False
549+
Verbose, diagnostic output
550+
551+
Returns
552+
-------
553+
ParticleReader
554+
)", py::arg("files"), py::arg("verbose")=false)
555+
.def("SelectType", &PSPhdf5::SelectType)
556+
.def("NumFiles", &PSPhdf5::NumFiles)
557+
.def("CurrentNumber", &PSPhdf5::CurrentNumber)
558+
.def("GetTypes", &PSPhdf5::GetTypes)
559+
.def("CurrentTime", &PSPhdf5::CurrentTime);
560+
480561
py::class_<Tipsy, std::shared_ptr<Tipsy>, PyTipsy, ParticleReader> tipsy(m, "Tipsy");
481562

482563
py::enum_<Tipsy::TipsyType>(tipsy, "TipsyType")
@@ -485,12 +566,28 @@ void ParticleReaderClasses(py::module &m) {
485566
.value("bonsai", Tipsy::TipsyType::bonsai)
486567
.export_values();
487568

488-
tipsy.def(py::init<const std::string&, Tipsy::TipsyType, bool>())
569+
tipsy.def(py::init<const std::string&, Tipsy::TipsyType, bool>(),
570+
R"(
571+
Read Tipsy format snapshots
572+
573+
Parameters
574+
----------
575+
file : str
576+
The Tipsy snapshot file
577+
type : TipsyType
578+
The Tipsy file type (native, xdr, bonsai)
579+
verbose : bool, default=False
580+
Verbose, diagnostic output
581+
582+
Returns
583+
-------
584+
ParticleReader
585+
586+
)", py::arg("file"), py::arg("type"), py::arg("verbose")=false)
489587
.def(py::init<const std::vector<std::string>&, Tipsy::TipsyType, bool>())
490588
.def("SelectType", &Tipsy::SelectType)
491589
.def("CurrentNumber", &Tipsy::CurrentNumber)
492590
.def("GetTypes", &Tipsy::GetTypes)
493591
.def("CurrentTime", &Tipsy::CurrentTime);
494592

495593
}
496-

0 commit comments

Comments
 (0)