Line data Source code
1 : #include "crpropa/module/ParticleCollector.h"
2 : #include "crpropa/module/TextOutput.h"
3 : #include "crpropa/Units.h"
4 :
5 : namespace crpropa {
6 :
7 14 : ParticleCollector::ParticleCollector() : nBuffer(10e6), clone(false), recursive(false) {
8 14 : container.reserve(nBuffer); // for 1e6 candidates ~ 500MB of RAM
9 14 : }
10 :
11 0 : ParticleCollector::ParticleCollector(const std::size_t nBuffer) : clone(false), recursive(false) {
12 0 : container.reserve(nBuffer);
13 0 : }
14 :
15 0 : ParticleCollector::ParticleCollector(const std::size_t nBuffer, const bool clone) : recursive(false) {
16 0 : container.reserve(nBuffer);
17 0 : }
18 :
19 0 : ParticleCollector::ParticleCollector(const std::size_t nBuffer, const bool clone, const bool recursive) {
20 0 : container.reserve(nBuffer);
21 0 : }
22 :
23 3206 : void ParticleCollector::process(Candidate *c) const {
24 6412 : #pragma omp critical(ModifyContainer)
25 : {
26 3206 : if(clone)
27 50 : container.push_back(c->clone(recursive));
28 : else
29 6362 : container.push_back(c);
30 : }
31 3206 : }
32 :
33 30 : void ParticleCollector::process(ref_ptr<Candidate> c) const {
34 30 : ParticleCollector::process((Candidate*) c);
35 30 : }
36 :
37 2 : void ParticleCollector::reprocess(Module *action) const {
38 14 : for (ParticleCollector::iterator itr = container.begin(); itr != container.end(); ++itr){
39 12 : if (clone)
40 0 : action->process((*(itr->get())).clone(false));
41 : else
42 12 : action->process(itr->get());
43 : }
44 2 : }
45 :
46 1 : void ParticleCollector::dump(const std::string &filename) const {
47 1 : TextOutput output(filename.c_str(), Output::Everything);
48 1 : reprocess(&output);
49 1 : output.close();
50 1 : }
51 :
52 1 : void ParticleCollector::load(const std::string &filename){
53 1 : TextOutput::load(filename.c_str(), this);
54 1 : }
55 :
56 22 : ParticleCollector::~ParticleCollector() {
57 14 : clearContainer();
58 22 : }
59 :
60 6 : std::size_t ParticleCollector::size() const {
61 6 : return container.size();
62 : }
63 :
64 9 : ref_ptr<Candidate> ParticleCollector::operator[](const std::size_t i) const {
65 9 : return container[i];
66 : }
67 :
68 14 : void ParticleCollector::clearContainer() {
69 : container.clear();
70 14 : }
71 :
72 2 : std::vector<ref_ptr<Candidate> >& ParticleCollector::getContainer() const {
73 2 : return container;
74 : }
75 :
76 2 : void ParticleCollector::setClone(bool b) {
77 2 : clone = b;
78 2 : }
79 :
80 0 : bool ParticleCollector::getClone() const {
81 0 : return clone;
82 : }
83 :
84 0 : std::string ParticleCollector::getDescription() const {
85 0 : return "ParticleCollector";
86 : }
87 :
88 5 : ParticleCollector::iterator ParticleCollector::begin() {
89 5 : return container.begin();
90 : }
91 :
92 0 : ParticleCollector::const_iterator ParticleCollector::begin() const {
93 0 : return container.begin();
94 : }
95 :
96 30 : ParticleCollector::iterator ParticleCollector::end() {
97 30 : return container.end();
98 : }
99 :
100 0 : ParticleCollector::const_iterator ParticleCollector::end() const {
101 0 : return container.end();
102 : }
103 :
104 1 : void ParticleCollector::getTrajectory(ModuleList* mlist, std::size_t i, Module *output) const {
105 1 : ref_ptr<Candidate> c_tmp = container[i]->clone();
106 :
107 1 : c_tmp->restart();
108 :
109 1 : mlist->add(output);
110 1 : mlist->run(c_tmp);
111 1 : mlist->remove(mlist->size()-1);
112 1 : }
113 :
114 1 : void ParticleCollector::getTrajectory(ref_ptr<ModuleList> mlist, std::size_t i, ref_ptr<Module> output) const {
115 1 : ParticleCollector::getTrajectory((ModuleList*) mlist, i, (Module*) output);
116 1 : }
117 :
118 : } // namespace crpropa
|