Skip to content

Commit

Permalink
ParticleCollector::load clean-up, TextOutput::load introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
adundovi committed Dec 11, 2016
1 parent 1d3d47c commit 2f81224
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 80 deletions.
2 changes: 2 additions & 0 deletions include/crpropa/module/TextOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CRPROPA_TEXTOUTPUT_H

#include "crpropa/module/Output.h"
#include "crpropa/module/ParticleCollector.h"

#include <fstream>

Expand Down Expand Up @@ -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;
};

Expand Down
6 changes: 6 additions & 0 deletions python/2_headers.i
Original file line number Diff line number Diff line change
Expand Up @@ -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();"
Expand Down Expand Up @@ -137,6 +139,7 @@
return PyString_FromString( value.c_str() );
}
};

%template(CandidateVector) std::vector< crpropa::ref_ptr<crpropa::Candidate> >;
%template(CandidateRefPtr) crpropa::ref_ptr<crpropa::Candidate>;
%include "crpropa/Candidate.h"
Expand Down Expand Up @@ -184,6 +187,9 @@
%inline %{
class RangeError {};
%}

%template(ParticleCollectorRefPtr) crpropa::ref_ptr<crpropa::ParticleCollector>;

%include "crpropa/module/ParticleCollector.h"
%include "crpropa/module/HDF5Output.h"
%include "crpropa/module/OutputShell.h"
Expand Down
76 changes: 1 addition & 75 deletions src/module/ParticleCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@
#include "crpropa/module/TextOutput.h"
#include "crpropa/Units.h"

#include <fstream>
#include <stdexcept>
#include <kiss/string.h>

#ifdef CRPROPA_HAVE_ZLIB
#include <izstream.hpp>
#endif

namespace crpropa {

ParticleCollector::ParticleCollector() : nBuffer(10e6), clone(false), recursive(false) {
Expand All @@ -29,8 +21,6 @@ ParticleCollector::ParticleCollector(const std::size_t nBuffer, const bool clone
container.reserve(nBuffer);
}

#include <iostream>

void ParticleCollector::process(Candidate *c) const {
#pragma omp critical
{
Expand Down Expand Up @@ -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<Candidate> 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() {
Expand Down
81 changes: 76 additions & 5 deletions src/module/TextOutput.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "crpropa/module/TextOutput.h"
#include "crpropa/module/ParticleCollector.h"
#include "crpropa/Units.h"

#include <stdio.h>
Expand All @@ -7,6 +8,7 @@
#include <kiss/string.h>

#ifdef CRPROPA_HAVE_ZLIB
#include <izstream.hpp>
#include <ozstream.hpp>
#endif

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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<Candidate> 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";
}
Expand Down

0 comments on commit 2f81224

Please sign in to comment.