From 2f81224e048aa2c188c46c46e324147a77fc0cf1 Mon Sep 17 00:00:00 2001 From: Andrej Dundovic Date: Sun, 11 Dec 2016 03:33:43 +0000 Subject: [PATCH] ParticleCollector::load clean-up, TextOutput::load introduced --- include/crpropa/module/TextOutput.h | 2 + python/2_headers.i | 6 +++ src/module/ParticleCollector.cpp | 76 +-------------------------- src/module/TextOutput.cpp | 81 +++++++++++++++++++++++++++-- 4 files changed, 85 insertions(+), 80 deletions(-) diff --git a/include/crpropa/module/TextOutput.h b/include/crpropa/module/TextOutput.h index 734227456..a67f8d618 100644 --- a/include/crpropa/module/TextOutput.h +++ b/include/crpropa/module/TextOutput.h @@ -2,6 +2,7 @@ #define CRPROPA_TEXTOUTPUT_H #include "crpropa/module/Output.h" +#include "crpropa/module/ParticleCollector.h" #include @@ -32,6 +33,7 @@ class TextOutput: public Output { void gzip(); void process(Candidate *candidate) const; + static void load(const std::string &filename, ParticleCollector *collector); std::string getDescription() const; }; diff --git a/python/2_headers.i b/python/2_headers.i index 5bf780953..db3e68941 100644 --- a/python/2_headers.i +++ b/python/2_headers.i @@ -98,6 +98,8 @@ %ignore operator crpropa::Module*; %ignore operator crpropa::ModuleList*; %ignore operator crpropa::MagneticField*; +%ignore operator crpropa::ParticleCollector*; +%ignore crpropa::TextOutput::load; %feature("ref") crpropa::Referenced "$this->addReference();" %feature("unref") crpropa::Referenced "$this->removeReference();" @@ -137,6 +139,7 @@ return PyString_FromString( value.c_str() ); } }; + %template(CandidateVector) std::vector< crpropa::ref_ptr >; %template(CandidateRefPtr) crpropa::ref_ptr; %include "crpropa/Candidate.h" @@ -184,6 +187,9 @@ %inline %{ class RangeError {}; %} + +%template(ParticleCollectorRefPtr) crpropa::ref_ptr; + %include "crpropa/module/ParticleCollector.h" %include "crpropa/module/HDF5Output.h" %include "crpropa/module/OutputShell.h" diff --git a/src/module/ParticleCollector.cpp b/src/module/ParticleCollector.cpp index 4afd27a6d..7bfab44a7 100644 --- a/src/module/ParticleCollector.cpp +++ b/src/module/ParticleCollector.cpp @@ -2,14 +2,6 @@ #include "crpropa/module/TextOutput.h" #include "crpropa/Units.h" -#include -#include -#include - -#ifdef CRPROPA_HAVE_ZLIB -#include -#endif - namespace crpropa { ParticleCollector::ParticleCollector() : nBuffer(10e6), clone(false), recursive(false) { @@ -29,8 +21,6 @@ ParticleCollector::ParticleCollector(const std::size_t nBuffer, const bool clone container.reserve(nBuffer); } -#include - void ParticleCollector::process(Candidate *c) const { #pragma omp critical { @@ -59,71 +49,7 @@ void ParticleCollector::dump(const std::string &filename) const { } void ParticleCollector::load(const std::string &filename){ - - std::string line; - std::istream *in; - std::ifstream infile(filename.c_str()); - - double lengthScale = Mpc; // default Mpc - double energyScale = EeV; // default EeV - - if (!infile.good()) - throw std::runtime_error( - "crpropa::ParticleCollector: could not open file " + filename); - in = &infile; - - if (kiss::ends_with(filename, ".gz")){ -#ifdef CRPROPA_HAVE_ZLIB - in = new zstream::igzstream(*in); -#else - throw std::runtime_error("CRPropa was build without Zlib compression!"); -#endif - } - - while (std::getline(*in,line)) { - std::stringstream stream(line); - if (stream.peek() == '#') - continue; - - ref_ptr c = new Candidate(); - double val_d; int val_i; - double x, y, z; - stream >> val_d; - c->setTrajectoryLength(val_d*lengthScale); // D - stream >> val_d; - c->setRedshift(val_d); // z - stream >> val_i; - c->setSerialNumber(val_i); // SN - stream >> val_i; - c->current.setId(val_i); // ID - stream >> val_d; - c->current.setEnergy(val_d*energyScale); // E - stream >> x >> y >> z; - c->current.setPosition(Vector3d(x, y, z)*lengthScale); // X, Y, Z - stream >> x >> y >> z; - c->current.setDirection(Vector3d(x, y, z)*lengthScale); // Px, Py, Pz - stream >> val_i; // SN0 (TODO: Reconstruct the parent-child relationship) - stream >> val_i; - c->source.setId(val_i); // ID0 - stream >> val_d; - c->source.setEnergy(val_d*energyScale); // E0 - stream >> x >> y >> z; - c->source.setPosition(Vector3d(x, y, z)*lengthScale); // X0, Y0, Z0 - stream >> x >> y >> z; - c->source.setDirection(Vector3d(x, y, z)*lengthScale); // P0x, P0y, P0z - stream >> val_i; // SN1 - stream >> val_i; - c->created.setId(val_i); // ID1 - stream >> val_d; - c->created.setEnergy(val_d*energyScale); // E1 - stream >> x >> y >> z; - c->created.setPosition(Vector3d(x, y, z)*lengthScale); // X1, Y1, Z1 - stream >> x >> y >> z; - c->created.setDirection(Vector3d(x, y, z)*lengthScale); // P1x, P1y, P1z - - process(c); - } - infile.close(); + TextOutput::load(filename.c_str(), this); } ParticleCollector::~ParticleCollector() { diff --git a/src/module/TextOutput.cpp b/src/module/TextOutput.cpp index 5f8753eb6..4e188d65c 100644 --- a/src/module/TextOutput.cpp +++ b/src/module/TextOutput.cpp @@ -1,4 +1,5 @@ #include "crpropa/module/TextOutput.h" +#include "crpropa/module/ParticleCollector.h" #include "crpropa/Units.h" #include @@ -7,6 +8,7 @@ #include #ifdef CRPROPA_HAVE_ZLIB +#include #include #endif @@ -34,13 +36,14 @@ TextOutput::TextOutput(const std::string &filename) : Output(), outfile(filenam } TextOutput::TextOutput(const std::string &filename, - OutputType outputtype) : Output(outputtype), outfile(filename.c_str(), - std::ios::binary), out(&outfile), filename( - filename) { - if (kiss::ends_with(filename, ".gz")) - gzip(); + OutputType outputtype) : Output(outputtype), outfile(filename.c_str(), + std::ios::binary), out(&outfile), filename( + filename) { + if (kiss::ends_with(filename, ".gz")) + gzip(); } + void TextOutput::printHeader() const { *out << "#"; if (fields.test(TrajectoryLengthColumn)) @@ -216,6 +219,74 @@ void TextOutput::process(Candidate *c) const { } +void TextOutput::load(const std::string &filename, ParticleCollector *collector){ + + std::string line; + std::istream *in; + std::ifstream infile(filename.c_str()); + + double lengthScale = Mpc; // default Mpc + double energyScale = EeV; // default EeV + + if (!infile.good()) + throw std::runtime_error( + "crpropa::TextOutput: could not open file " + filename); + in = &infile; + + if (kiss::ends_with(filename, ".gz")){ +#ifdef CRPROPA_HAVE_ZLIB + in = new zstream::igzstream(*in); +#else + throw std::runtime_error("CRPropa was build without Zlib compression!"); +#endif + } + + while (std::getline(*in,line)) { + std::stringstream stream(line); + if (stream.peek() == '#') + continue; + + ref_ptr c = new Candidate(); + double val_d; int val_i; + double x, y, z; + stream >> val_d; + c->setTrajectoryLength(val_d*lengthScale); // D + stream >> val_d; + c->setRedshift(val_d); // z + stream >> val_i; + c->setSerialNumber(val_i); // SN + stream >> val_i; + c->current.setId(val_i); // ID + stream >> val_d; + c->current.setEnergy(val_d*energyScale); // E + stream >> x >> y >> z; + c->current.setPosition(Vector3d(x, y, z)*lengthScale); // X, Y, Z + stream >> x >> y >> z; + c->current.setDirection(Vector3d(x, y, z)*lengthScale); // Px, Py, Pz + stream >> val_i; // SN0 (TODO: Reconstruct the parent-child relationship) + stream >> val_i; + c->source.setId(val_i); // ID0 + stream >> val_d; + c->source.setEnergy(val_d*energyScale); // E0 + stream >> x >> y >> z; + c->source.setPosition(Vector3d(x, y, z)*lengthScale); // X0, Y0, Z0 + stream >> x >> y >> z; + c->source.setDirection(Vector3d(x, y, z)*lengthScale); // P0x, P0y, P0z + stream >> val_i; // SN1 + stream >> val_i; + c->created.setId(val_i); // ID1 + stream >> val_d; + c->created.setEnergy(val_d*energyScale); // E1 + stream >> x >> y >> z; + c->created.setPosition(Vector3d(x, y, z)*lengthScale); // X1, Y1, Z1 + stream >> x >> y >> z; + c->created.setDirection(Vector3d(x, y, z)*lengthScale); // P1x, P1y, P1z + + collector->process(c); + } + infile.close(); +} + std::string TextOutput::getDescription() const { return "TextOutput"; }