Line data Source code
1 : #ifndef CRPROPA_CANDIDATE_H
2 : #define CRPROPA_CANDIDATE_H
3 :
4 : #include "crpropa/ParticleState.h"
5 : #include "crpropa/Referenced.h"
6 : #include "crpropa/AssocVector.h"
7 : #include "crpropa/Variant.h"
8 :
9 : #include <vector>
10 : #include <map>
11 : #include <sstream>
12 : #include <stdint.h>
13 :
14 : namespace crpropa {
15 : /**
16 : * \addtogroup Core
17 : * @{
18 : */
19 :
20 : /**
21 : @class Candidate Candidate.h include/crpropa/Candidate.h
22 : @brief All information about the cosmic ray.
23 :
24 : The Candidate is a passive object, that holds the information about the state
25 : of the cosmic ray and the simulation itself.
26 : */
27 : class Candidate: public Referenced {
28 : public:
29 : ParticleState source; /**< Particle state at the source */
30 : ParticleState created; /**< Particle state of parent particle at the time of creation */
31 : ParticleState current; /**< Current particle state */
32 : ParticleState previous; /**< Particle state at the end of the previous step */
33 :
34 : std::vector<ref_ptr<Candidate> > secondaries; /**< Secondary particles from interactions */
35 :
36 : typedef Loki::AssocVector<std::string, Variant> PropertyMap;
37 : PropertyMap properties; /**< Map of property names and their values. */
38 :
39 : /** Parent candidate. 0 if no parent (initial particle). Must not be a ref_ptr to prevent circular referencing. */
40 : Candidate *parent;
41 :
42 : private:
43 : bool active; /**< Active status */
44 : double weight; /**< Weight of the candidate */
45 : double redshift; /**< Current simulation time-point in terms of redshift z */
46 : double trajectoryLength; /**< Comoving distance [m] the candidate has traveled so far */
47 : double currentStep; /**< Size of the currently performed step in [m] comoving units */
48 : double nextStep; /**< Proposed size of the next propagation step in [m] comoving units */
49 : std::string tagOrigin; /**< Name of interaction/source process which created this candidate*/
50 : double time; /**< Time [s] that has passed in the laboratory frame of reference */
51 :
52 : static uint64_t nextSerialNumber;
53 : uint64_t serialNumber;
54 :
55 : public:
56 : Candidate(
57 : int id = 0,
58 : double energy = 0,
59 : Vector3d position = Vector3d(0, 0, 0),
60 : Vector3d direction = Vector3d(-1, 0, 0),
61 : double z = 0,
62 : double weight = 1.,
63 : std::string tagOrigin = "PRIM"
64 : );
65 :
66 : /**
67 : Creates a candidate, initializing the Candidate::source, Candidate::created,
68 : Candidate::previous and Candidate::current state with the argument.
69 : */
70 : Candidate(const ParticleState &state);
71 :
72 : bool isActive() const;
73 : void setActive(bool b);
74 :
75 : void setTrajectoryLength(double length);
76 : double getTrajectoryLength() const;
77 :
78 : double getVelocity() const;
79 :
80 : void setRedshift(double z);
81 : double getRedshift() const;
82 :
83 : /**
84 : Sets weight of each candidate.
85 : Weights are calculated for each tracked secondary.
86 : */
87 : void setWeight(double weight);
88 : void updateWeight(double weight);
89 : double getWeight() const;
90 :
91 : /**
92 : Sets the current step and increases the trajectory length accordingly.
93 : Only the propagation module should use this.
94 : */
95 : void setCurrentStep(double step);
96 : double getCurrentStep() const;
97 :
98 : /**
99 : Sets the proposed next step.
100 : Only the propagation module should use this.
101 : */
102 : void setNextStep(double step);
103 : double getNextStep() const;
104 :
105 : /**
106 : Sets the tagOrigin of the candidate. Can be used to trace back the interactions
107 : */
108 : void setTagOrigin(std::string tagOrigin);
109 : std::string getTagOrigin() const;
110 :
111 : /**
112 : Sets the time of the candidate.
113 : */
114 : void setTime(double t);
115 : double getTime() const;
116 :
117 : /**
118 : Make a bid for the next step size: the lowest wins.
119 : */
120 : void limitNextStep(double step);
121 :
122 : void setProperty(const std::string &name, const Variant &value);
123 : const Variant &getProperty(const std::string &name) const;
124 : bool removeProperty(const std::string &name);
125 : bool hasProperty(const std::string &name) const;
126 :
127 : /**
128 : Add a new candidate to the list of secondaries.
129 : @param c Candidate
130 :
131 : Adds a new candidate to the list of secondaries of this candidate.
132 : The secondaries Candidate::source and Candidate::previous state are set to the _source_ and _previous_ state of its parent.
133 : The secondaries Candidate::created and Candidate::current state are set to the _current_ state of its parent, except for the secondaries current energy and particle id.
134 : Trajectory length and redshift are copied from the parent.
135 : */
136 : void addSecondary(Candidate *c);
137 4 : inline void addSecondary(ref_ptr<Candidate> c) { addSecondary(c.get()); };
138 : /**
139 : Add a new candidate to the list of secondaries.
140 : @param id particle ID of the secondary
141 : @param energy energy of the secondary
142 : @param w weight of the secondary
143 : @param tagOrigin tag of the secondary
144 : */
145 : void addSecondary(int id, double energy, double w = 1., std::string tagOrigin = "SEC");
146 : /**
147 : Add a new candidate to the list of secondaries.
148 : @param id particle ID of the secondary
149 : @param energy energy of the secondary
150 : @param position start position of the secondary
151 : @param w weight of the secondary
152 : @param tagOrigin tag of the secondary
153 : */
154 : void addSecondary(int id, double energy, Vector3d position, double w = 1., std::string tagOrigin = "SEC");
155 : void clearSecondaries();
156 :
157 : std::string getDescription() const;
158 :
159 : /** Unique (inside process) serial number (id) of candidate */
160 : uint64_t getSerialNumber() const;
161 : void setSerialNumber(const uint64_t snr);
162 :
163 : /** Serial number of candidate at source*/
164 : uint64_t getSourceSerialNumber() const;
165 :
166 : /** Serial number of candidate at creation */
167 : uint64_t getCreatedSerialNumber() const;
168 :
169 : /** Set the next serial number to use */
170 : static void setNextSerialNumber(uint64_t snr);
171 :
172 : /** Get the next serial number that will be assigned */
173 : static uint64_t getNextSerialNumber();
174 :
175 : /**
176 : Create an exact clone of candidate
177 : @param recursive recursively clone and add the secondaries
178 : */
179 : ref_ptr<Candidate> clone(bool recursive = false) const;
180 :
181 : /**
182 : Copy the source particle state to the current state
183 : and activate it if inactive, e.g. restart it
184 : */
185 : void restart();
186 : };
187 :
188 : /** @}*/
189 : } // namespace crpropa
190 :
191 : #endif // CRPROPA_CANDIDATE_H
|