Garfield++ 5.0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
gparticle.h
Go to the documentation of this file.
1#ifndef GPARTICLE_H
2#define GPARTICLE_H
3#include <atomic>
5
6/*
7Copyright (c) 2000 Igor B. Smirnov
8
9The file can be used, copied, modified, and distributed
10according to the terms of GNU Lesser General Public License version 2.1
11as published by the Free Software Foundation,
12and provided that the above copyright notice, this permission notice,
13and notices about any modifications of the original text
14appear in all copies and in supporting documentation.
15The file is provided "as is" without express or implied warranty.
16*/
17
18namespace Heed {
19
20/// Point in space, time and velocity
21class stvpoint {
22 public:
23 /// Coordinates in the first system in the tree.
25 /// Unit vector, in the first system in the tree.
27 /// Coordinates in the local system (last system in the tree).
29 /// Unit vector, in the local system (last system in the tree).
31 /// Longitudinal velocity
34
35 /// Position flag
36 /// 0 - inside volume, or unknown
37 /// 1 - on the border of the volume
38 /// 2 - on the border of an embraced volume
39 int sb = 0;
40 /// "Entering flag".
41 /// 1 - entering new volume, 0 otherwise.
42 /// Embraced volume is also considered new.
43 int s_ent = 0;
44
45 manip_absvol* next_eid = nullptr; // if nextpos.sb==2
46 /// Range from previous point.
49
50 /// Default constructor.
51 stvpoint() = default;
52 /// Constructor.
53 stvpoint(const point& fpt, const vec& fdir, vfloat fspeed,
54 manip_absvol_treeid& ftid, vfloat fprange, vfloat ftime, int fsb,
55 int fs_ent, manip_absvol* faeid)
56 : pt(fpt),
57 dir(unit_vec(fdir)),
58 speed(fspeed),
59 tid(ftid),
60 sb(fsb),
61 s_ent(fs_ent),
62 next_eid(faeid),
63 prange(fprange),
64 time(ftime) {
65 ptloc = pt;
66 tid.up_absref(&ptloc);
67 dirloc = dir;
68 tid.up_absref(&dirloc);
69 }
70 /** Constructor.
71 * \param pstv previous point
72 * \param ts trajectory step (in the local system)
73 * \param mrange step length (may be less than the one in ts)
74 * \param fsb position flag
75 * \param fs_ent "entering" flag
76 * \param faeid next volume
77 **/
78 stvpoint(const stvpoint& pstv, const trajestep& ts, vfloat mrange,
79 int fsb, int fs_ent, manip_absvol* faeid)
80 : pt(),
81 dir(),
82 ptloc(),
83 dirloc(),
84 speed(pstv.speed),
85 tid(pstv.tid),
86 sb(fsb),
87 s_ent(fs_ent),
88 next_eid(faeid),
89 prange(mrange) {
90 if (pstv.speed == 0) {
91 time = pstv.time; // just to put anything
92 } else {
93 // If speed is changed, this time is to be corrected in derivative class
94 time = pstv.time + mrange / pstv.speed;
95 }
96 ts.Gnextpoint(mrange, ptloc, dirloc);
97 pt = ptloc;
98 tid.down_absref(&pt);
99 dir = dirloc;
100 tid.down_absref(&dir);
101 }
102 /** Constructor.
103 * \param pstv previous point
104 * \param ts trajectory step (in the local system)
105 * \param fsb position flag
106 * \param fs_ent "entering" flag
107 * \param faeid next volume
108 **/
109 stvpoint(const stvpoint& pstv, const trajestep& ts,
110 int fsb, int fs_ent, manip_absvol* faeid)
111 : pt(),
112 dir(),
113 ptloc(),
114 dirloc(),
115 speed(pstv.speed),
116 tid(pstv.tid),
117 sb(fsb),
118 s_ent(fs_ent),
119 next_eid(faeid),
120 prange(ts.mrange) {
121 if (pstv.speed == 0) {
122 time = pstv.time; // just to put anything
123 } else {
124 // If speed is changed, this time is to be corrected in derivative class
125 time = pstv.time + ts.mrange / pstv.speed;
126 }
127 ptloc = ts.mpoint;
128 point temp;
129 ts.Gnextpoint(ts.mrange, temp, dirloc);
130 pt = ptloc;
131 tid.down_absref(&pt);
132 dir = dirloc;
133 tid.down_absref(&dir);
134 }
135 /// Copy constructor.
136 stvpoint(const stvpoint&) = default;
137 /// Copy assignment operator
138 stvpoint& operator=(const stvpoint&) = default;
139 void print(std::ostream& file, int l) const;
140 absvol* volume() { return tid.G_lavol(); }
141};
142
143/// "Geometric particle" (particle that does not interact with materials).
144/// It moves along a polyline or circle from one volume to another.
145/// Classes for interacting particles should be derived from this base class.
146
148 public:
149 /// Default constructor.
150 gparticle() = default;
151 /// Constructor.
152 gparticle(manip_absvol* primvol, const point& pt, const vec& vel,
153 vfloat time);
154 /// Destructor.
155 virtual ~gparticle() {}
156
157 /// Transport the particle.
158 virtual void fly(std::vector<gparticle*>& secondaries) {
159 mfunname("virtual void gparticle::fly()");
160 while (m_alive) {
161 step(secondaries);
162 physics(secondaries);
163 }
164 }
165
166 virtual void fly(std::vector<gparticle*>& secondaries, const bool one_step) {
167 mfunname("virtual void gparticle::fly()");
168 int nstep = 0;
169 while (m_alive) {
170 step(secondaries);
171 physics(secondaries);
172 ++nstep;
173 if (one_step && nstep >= 2) break;
174 }
175 }
176
177 /// Set limits/parameters for trajectory steps.
178 void set_step_limits(const vfloat fmax_range,
179 const vfloat frad_for_straight,
180 const vfloat fmax_straight_arange,
181 const vfloat fmax_circ_arange) {
182 m_max_range = fmax_range;
183 m_rad_for_straight = frad_for_straight;
184 m_max_straight_arange = fmax_straight_arange;
185 m_max_circ_arange = fmax_circ_arange;
186 }
187
188 /// Get the current position of the particle.
189 const vec& position() const { return m_currpos.pt.v; }
190 /// Get the current time of the particle.
191 vfloat time() const { return m_currpos.time; }
192 /// Get the current direction of the particle.
193 const vec& direction() const { return m_currpos.dir; }
194 /// Alive?
195 bool alive() const { return m_alive; }
196
197 /// Reset the counter.
198 static void reset_counter() { s_counter = 0L; }
199
200 /// Print-out.
201 virtual void print(std::ostream& file, int l) const;
202 /// Clone the particle.
203 virtual gparticle* copy() const { return new gparticle(*this); }
204
205 protected:
206 /// Assign prevpos = currpos and currpos = nextpos,
207 /// call change_vol if necessary and update nextpos = calc_step_to_bord().
208 /// Derived versions can also recalculate the direction at currpos
209 /// right after updating currpos = nextpos.
210 /// This is especially important in the case when the motion is approximated
211 /// by straight-line steps, but there is a (magnetic) field which slightly
212 /// deflects the trajectory. In this case, the velocity is corrected
213 /// at the end point of each interval, but the position is not.
214 virtual void step(std::vector<gparticle*>& secondaries);
215
216 /// Move from one volume to another.
217 virtual void change_vol() { m_currpos.volume()->income(this); }
218
219 /** Set curvature. Can also change the direction at the current position.
220 * \param curved
221 * flag whether the trajectory is curved
222 * \param frelcen
223 * position of the centre of rotation relative to currpos.
224 * \param fmrange
225 * step range
226 * \param prec
227 * tolerance for checking if the force is parallel or antiparallel to
228 * dir. In the latter case, the range is restricted by the end point.
229 * In calc_step_to_bord() it is set to m_max_straight_arange.
230 */
231 virtual void curvature(bool& curved, vec& frelcen, vfloat& fmrange,
232 vfloat prec);
233
234 /// Apply any other processes (turn the trajectory, kill the particle, ...).
235 virtual void physics_after_new_speed(std::vector<gparticle*>& /*secondaries*/) {}
236
237 /// Apply any other processes (turn the trajectory, kill the particle, ...).
238 virtual void physics(std::vector<gparticle*>& /*secondaries*/) {}
239
240 /// Reduce the maximal possible range due to continuous processes.
241 /// Called from calc_step_to_bord after the call of curvature.
242 /// but before considering the crossing with volumes.
243 /// Therefore mrange may be reduced after this.
244 virtual void physics_mrange(double& fmrange);
245
246 /// Determine next position.
247 virtual stvpoint calc_step_to_bord();
248
249 /// Change the direction of the particle.
250 void turn(const double ctheta, const double stheta);
251
252 /// Generate next position in new volume.
254
255 /// Instance counter.
256 static std::atomic<long> s_counter;
257
258 /// Status flag whether the particle is active.
259 bool m_alive = false;
260
261 /// Step number.
262 long m_nstep = 0;
263 /// Max. number of zero-steps allowed.
264 static constexpr long m_max_qzero_step = 100;
265 /// Number of previous steps with zero range (including this step).
266 long m_nzero_step = 0;
267
268 /// Original point.
270 /// Range from origin to current position.
272
273 /// Previous point.
275 /// Current point.
277 /// Next point.
279
280 private:
281 /// Max. length of trajectory steps.
282 vfloat m_max_range = 100. * CLHEP::cm;
283 /// Bending radius beyond which to use straight-line steps.
284 vfloat m_rad_for_straight = 1000. * CLHEP::cm;
285 /// Angular step limit when using straight-line approximation.
286 vfloat m_max_straight_arange = 0.1 * CLHEP::rad;
287 /// Angular step limit for curved lines.
288 vfloat m_max_circ_arange = 0.2 * CLHEP::rad;
289};
290}
291
292#endif
#define mfunname(string)
virtual void print(std::ostream &file, int l) const
Print-out.
virtual ~gparticle()
Destructor.
Definition gparticle.h:155
static void reset_counter()
Reset the counter.
Definition gparticle.h:198
virtual void physics_mrange(double &fmrange)
virtual void physics_after_new_speed(std::vector< gparticle * > &)
Apply any other processes (turn the trajectory, kill the particle, ...).
Definition gparticle.h:235
virtual void fly(std::vector< gparticle * > &secondaries, const bool one_step)
Definition gparticle.h:166
stvpoint m_nextpos
Next point.
Definition gparticle.h:278
double m_total_range_from_origin
Range from origin to current position.
Definition gparticle.h:271
virtual stvpoint calc_step_to_bord()
Determine next position.
stvpoint m_prevpos
Previous point.
Definition gparticle.h:274
static constexpr long m_max_qzero_step
Max. number of zero-steps allowed.
Definition gparticle.h:264
virtual void step(std::vector< gparticle * > &secondaries)
Definition gparticle.cpp:70
bool alive() const
Alive?
Definition gparticle.h:195
vfloat time() const
Get the current time of the particle.
Definition gparticle.h:191
gparticle()=default
Default constructor.
void turn(const double ctheta, const double stheta)
Change the direction of the particle.
const vec & position() const
Get the current position of the particle.
Definition gparticle.h:189
stvpoint m_currpos
Current point.
Definition gparticle.h:276
virtual void change_vol()
Move from one volume to another.
Definition gparticle.h:217
stvpoint switch_new_vol()
Generate next position in new volume.
virtual void physics(std::vector< gparticle * > &)
Apply any other processes (turn the trajectory, kill the particle, ...).
Definition gparticle.h:238
virtual gparticle * copy() const
Clone the particle.
Definition gparticle.h:203
long m_nzero_step
Number of previous steps with zero range (including this step).
Definition gparticle.h:266
stvpoint m_origin
Original point.
Definition gparticle.h:269
bool m_alive
Status flag whether the particle is active.
Definition gparticle.h:259
long m_nstep
Step number.
Definition gparticle.h:262
const vec & direction() const
Get the current direction of the particle.
Definition gparticle.h:193
static std::atomic< long > s_counter
Instance counter.
Definition gparticle.h:256
virtual void fly(std::vector< gparticle * > &secondaries)
Transport the particle.
Definition gparticle.h:158
void set_step_limits(const vfloat fmax_range, const vfloat frad_for_straight, const vfloat fmax_straight_arange, const vfloat fmax_circ_arange)
Set limits/parameters for trajectory steps.
Definition gparticle.h:178
virtual void curvature(bool &curved, vec &frelcen, vfloat &fmrange, vfloat prec)
Definition gparticle.cpp:91
Service class (array of manip_absvol).
Definition volume.h:32
Abstract base classs for volume "manipulators".
Definition volume.h:128
Point.
Definition vec.h:368
Point in space, time and velocity.
Definition gparticle.h:21
void print(std::ostream &file, int l) const
Definition gparticle.cpp:16
stvpoint & operator=(const stvpoint &)=default
Copy assignment operator.
absvol * volume()
Definition gparticle.h:140
manip_absvol * next_eid
Definition gparticle.h:45
vec dirloc
Unit vector, in the local system (last system in the tree).
Definition gparticle.h:30
stvpoint(const point &fpt, const vec &fdir, vfloat fspeed, manip_absvol_treeid &ftid, vfloat fprange, vfloat ftime, int fsb, int fs_ent, manip_absvol *faeid)
Constructor.
Definition gparticle.h:53
stvpoint(const stvpoint &pstv, const trajestep &ts, int fsb, int fs_ent, manip_absvol *faeid)
Definition gparticle.h:109
vec dir
Unit vector, in the first system in the tree.
Definition gparticle.h:26
stvpoint(const stvpoint &)=default
Copy constructor.
vfloat prange
Range from previous point.
Definition gparticle.h:47
vfloat speed
Longitudinal velocity.
Definition gparticle.h:32
stvpoint(const stvpoint &pstv, const trajestep &ts, vfloat mrange, int fsb, int fs_ent, manip_absvol *faeid)
Definition gparticle.h:78
point pt
Coordinates in the first system in the tree.
Definition gparticle.h:24
manip_absvol_treeid tid
Definition gparticle.h:33
stvpoint()=default
Default constructor.
point ptloc
Coordinates in the local system (last system in the tree).
Definition gparticle.h:28
vfloat mrange
Maximal possible range.
Definition trajestep.h:93
void Gnextpoint(vfloat frange, point &fpos, vec &fdir) const
Move to the next point.
Definition trajestep.cpp:80
Definition BGMesh.cpp:6
double vfloat
Definition vfloat.h:16