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 : /** Constructor
57 : * @param id Particle ID after the 2012 Monte Carlo nuclear code scheme, use nuleusId(A,Z) for nuclei
58 : * @param energy Particle energy
59 : * @param position Start position
60 : * @param direction Start direction
61 : * @param z Redshift
62 : * @param weight Statistical weight (important property for some modules, usally just 1)
63 : * @param tagOrigin Usually either "PRIM" for primary particle or "SEC" for secondary particle, but can be anything
64 : */
65 : Candidate(
66 : int id = 0,
67 : double energy = 0,
68 : Vector3d position = Vector3d(0, 0, 0),
69 : Vector3d direction = Vector3d(-1, 0, 0),
70 : double z = 0,
71 : double weight = 1.,
72 : std::string tagOrigin = "PRIM"
73 : );
74 :
75 : /**
76 : Creates a candidate, initializing the Candidate::source, Candidate::created,
77 : Candidate::previous and Candidate::current state with the argument.
78 : @param state ParticleState for source, created, previous and current. Makes copies.
79 : */
80 : Candidate(const ParticleState &state);
81 :
82 : /** Checks if particle is still active */
83 : bool isActive() const;
84 : /** Sets particle active or unactive
85 : * When the particle is set unactive it will first finish its current step,
86 : * only at the beginning of the next step the particle will be recognized as unactive
87 : * and the simulation for that particle will stop.
88 : * It might be possible that some modules additionally check if the particle is still active,
89 : * if that happens, it might allready be ignored in those modules during the same step,
90 : * this depends on the order the modules are added to ModuleList.
91 : * @param b Activate state of particle, false=deactivated
92 : */
93 : void setActive(bool b);
94 :
95 : /** Sets trajectory length
96 : * Mostly used by propagators over setCurrentStep, but usefull if particle should start
97 : * at a later position.
98 : * @param length Trajectory length in meter
99 : */
100 : void setTrajectoryLength(double length);
101 : /** Returns current trajectory length */
102 : double getTrajectoryLength() const;
103 :
104 : /** Returns absolute of current velocity
105 : * To get the current velocity vector you can use Candidate.current.getVelocity()
106 : */
107 : double getVelocity() const;
108 :
109 : void setRedshift(double z);
110 : double getRedshift() const;
111 :
112 : /**
113 : Sets weight of each candidate.
114 : Weights are calculated for each tracked secondary.
115 : */
116 : void setWeight(double weight);
117 : /** Updates Weight
118 : * Multiplies the current weight with the given weight
119 : */
120 : void updateWeight(double weight);
121 : double getWeight() const;
122 :
123 : /**
124 : Sets the current step and increases the trajectory length and time accordingly.
125 : Only the propagation module should use this.
126 : @param step Current step in meter
127 : */
128 : void setCurrentStep(double step);
129 : /** @return Current stepsize in meter */
130 : double getCurrentStep() const;
131 :
132 : /**
133 : Sets the proposed next step.
134 : Only the propagation module should use this.
135 : @param step Proposed next stepsize in meter
136 : */
137 : void setNextStep(double step);
138 : /** @return Proposed next stepsize in meter */
139 : double getNextStep() const;
140 :
141 : /**
142 : Sets the tagOrigin of the candidate. Can be used to trace back the interactions
143 : */
144 : void setTagOrigin(std::string tagOrigin);
145 : std::string getTagOrigin() const;
146 :
147 : /**
148 : Sets the time of the candidate.
149 : This is done automatically together with the increase of TrajectoryLength,
150 : since CRPropa assumes lightspeed in every case both TrajectoryLength and Time are equal.
151 : @param t Time in seconds
152 : */
153 : void setTime(double t);
154 : /** Returns the time of the candidate.
155 : * The time is tracked alongside TrajectoryLength by dividing the current TrajecoryLength by c
156 : * @return Current time in seconds
157 : */
158 : double getTime() const;
159 :
160 : /**
161 : Make a bid for the next step size: the lowest wins.
162 : @param step The bid in meter
163 : */
164 : void limitNextStep(double step);
165 :
166 : /** Sets a arbitrary property
167 : * This function either creates a property if it does not exist or updates it.
168 : * @param key Key to put into unordered_map
169 : * @param value Any Variant object, Variant can represent a variety of data types so
170 : * that a property can have any basic datatype
171 : */
172 : void setProperty(const std::string &key, const Variant &value);
173 : /** Returns the value of the Property with given key
174 : * This function loops through the unordered_map and returns the first property fitting to
175 : * the provided key. If no key value pair is found it throws an error
176 : * @param key Key to search for
177 : */
178 : const Variant &getProperty(const std::string &key) const;
179 : /** Tries to remove property that has given key
180 : * @return Returns true if property was removed successfully or false if not
181 : */
182 : bool removeProperty(const std::string &key);
183 : /** @return Returns true if property exists, false otherwise */
184 : bool hasProperty(const std::string &key) const;
185 :
186 : /** Add a new candidate to the list of secondaries.
187 : Adds a new candidate to the list of secondaries of this candidate.
188 : The secondaries Candidate::source and Candidate::previous state are set to the _source_ and _previous_ state of its parent.
189 : 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.
190 : Trajectory length and redshift are copied from the parent.
191 : @param c Candidate
192 : */
193 : void addSecondary(Candidate *c);
194 4 : inline void addSecondary(ref_ptr<Candidate> c) { addSecondary(c.get()); };
195 : /**
196 : Add a new candidate to the list of secondaries.
197 : @param id particle ID of the secondary after the 2012 Monte Carlo nuclear code scheme
198 : @param energy energy of the secondary
199 : @param w weight of the secondary
200 : @param tagOrigin tag of the secondary
201 : */
202 : void addSecondary(int id, double energy, double w = 1., std::string tagOrigin = "SEC");
203 : /**
204 : Add a new candidate to the list of secondaries.
205 : @param id particle ID of the secondary after the 2012 Monte Carlo nuclear code scheme
206 : @param energy energy of the secondary
207 : @param position start position of the secondary
208 : @param w weight of the secondary
209 : @param tagOrigin tag of the secondary
210 : */
211 : void addSecondary(int id, double energy, Vector3d position, double w = 1., std::string tagOrigin = "SEC");
212 : /** Clears all stored secondaries (deletes them) */
213 : void clearSecondaries();
214 :
215 : std::string getDescription() const;
216 :
217 : /** @return Returns a the unique serial number of the particle */
218 : uint64_t getSerialNumber() const;
219 : /** Sets a custom serial number
220 : * @param snr Custom serial number
221 : */
222 : void setSerialNumber(const uint64_t snr);
223 :
224 : /** @return Serial number of candidate at source*/
225 : uint64_t getSourceSerialNumber() const;
226 :
227 : /** @return Serial number of candidate at creation */
228 : uint64_t getCreatedSerialNumber() const;
229 :
230 : /** Set the next serial number to use */
231 : static void setNextSerialNumber(uint64_t snr);
232 :
233 : /** @return Get the next serial number that will be assigned */
234 : static uint64_t getNextSerialNumber();
235 :
236 : /** Create an exact clone of candidate
237 : @param recursive recursively clone and add the secondaries
238 : */
239 : ref_ptr<Candidate> clone(bool recursive = false) const;
240 :
241 : /**
242 : Copy the source particle state to the current state
243 : and activate it if inactive, e.g. restart it
244 : */
245 : void restart();
246 : };
247 :
248 : /** @}*/
249 : } // namespace crpropa
250 :
251 : #endif // CRPROPA_CANDIDATE_H
|