Garfield++ 5.0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
gparticle.cpp
Go to the documentation of this file.
2/*
3Copyright (c) 2000 Igor B. Smirnov
4
5The file can be used, copied, modified, and distributed
6according to the terms of GNU Lesser General Public License version 2.1
7as published by the Free Software Foundation,
8and provided that the above copyright notice, this permission notice,
9and notices about any modifications of the original text
10appear in all copies and in supporting documentation.
11The file is provided "as is" without express or implied warranty.
12*/
13
14namespace Heed {
15
16void stvpoint::print(std::ostream& file, int l) const {
17 if (l < 0) return;
18 Ifile << "stvpoint: sb=" << sb << " s_ent=" << s_ent << " prange=" << prange
19 << " time=" << time << '\n';
20 indn.n += 2;
21 Ifile << "position:\n" << pt << ptloc;
22 Ifile << "direction:\n" << dir << dirloc;
23 Ifile << "speed=" << speed << '\n';
24 if (tid.eid.empty()) {
25 Ifile << "point is outside universe\n";
26 file.flush();
27 indn.n -= 2;
28 return;
29 }
30 tid.print(file, 1);
31 char s[100];
32 if (sb == 2) {
33 next_eid->m_chname(s);
34 Ifile << "next volume name " << s << '\n';
35 }
36 indn.n -= 2;
37 file.flush();
38}
39
40std::atomic<long> gparticle::s_counter{0L};
41
42gparticle::gparticle(manip_absvol* primvol, const point& pt, const vec& vel,
43 vfloat ftime)
44 : m_prevpos(),
45 m_nextpos() {
46 mfunname("gparticle::gparticle(...)");
47 primvol->m_find_embed_vol(pt, vel, &m_origin.tid);
48 m_origin.pt = pt;
49 if (vel == dv0) {
50 m_origin.dir = dv0;
51 m_origin.speed = 0.0;
52 } else {
53 m_origin.dir = unit_vec(vel);
54 m_origin.speed = vel.length();
55 }
56 m_origin.ptloc = m_origin.pt;
57 m_origin.tid.up_absref(&m_origin.ptloc);
58 m_origin.dirloc = m_origin.dir;
59 m_origin.tid.up_absref(&m_origin.dirloc);
60 m_origin.time = ftime;
61 m_origin.sb = 0;
62 m_origin.s_ent = 1;
63 if (m_origin.tid.eid.empty()) return;
64 m_alive = true;
67 m_nextpos.s_ent = 0;
68}
69
70void gparticle::step(std::vector<gparticle*>& secondaries) {
71 // Make step to next point and calculate new step to border.
72 mfunname("void gparticle::step()");
76 m_nstep++;
77 if (m_currpos.prange == 0) {
80 "too many zero steps, possible infinite loop\n", mcerr);
81 } else {
82 m_nzero_step = 0;
83 }
84 physics_after_new_speed(secondaries);
85 if (m_alive) {
86 if (m_prevpos.tid != m_currpos.tid) change_vol();
88 }
89}
90
91void gparticle::curvature(bool& curved, vec& frelcen, vfloat& fmrange,
92 vfloat /*prec*/) {
93 curved = false;
94 frelcen.x = 0.;
95 frelcen.y = 0.;
96 frelcen.z = 0.;
97 fmrange = max_vfloat;
98 /* The following is for debug
99 vec field(0,1,0);
100 vfloat rad = 10;
101 if (length(m_currpos.dir) > 0 && check_par(m_currpos.dir, field) == 0) {
102 curved = true;
103 vfloat coef = sin2vec(m_currpos.dir, field);
104 rad = rad / coef;
105 frelcen = unit_vec(m_currpos.dir || field) * rad;
106 }
107 */
108}
109
110void gparticle::physics_mrange(double& /*fmrange*/) {}
111
113 // Calculate next point as step to border.
114 pvecerror("stvpoint gparticle::calc_step_to_bord()");
115 if (m_currpos.sb > 0) {
116 // Just switch to new volume.
117 return switch_new_vol();
118 }
119 bool curved = false;
120 vec relcen;
121 vfloat mrange;
122 curvature(curved, relcen, mrange, m_max_straight_arange);
123 if (mrange <= 0) {
124 // Preserve current point for modification by physics.
125 stvpoint temp(m_currpos);
126 temp.s_ent = 0;
127 return temp;
128 }
129 // Change to local system.
130 m_currpos.tid.up_absref(&relcen);
131 physics_mrange(mrange);
132 trajestep ts(m_max_range, m_rad_for_straight,
133 m_max_straight_arange, m_max_circ_arange,
134 m_currpos.ptloc, m_currpos.dirloc, curved, relcen, mrange,
135 m_currpos.tid.eid.back()->Gavol()->prec);
136 if (ts.mrange <= 0) {
137 stvpoint temp(m_currpos);
138 temp.s_ent = 0;
139 return temp;
140 }
141 // Here the range is calculated:
142 int sb;
143 manip_absvol* faeid = nullptr;
144 m_currpos.volume()->range(ts, 1, sb, faeid);
145 // 1 means inside the volume and makes
146 // the program checking embraced volumes
147 if (ts.s_prec == 0) {
148 // Point is crossed.
149 return stvpoint(m_currpos, ts, sb, 0, faeid);
150 }
151 return stvpoint(m_currpos, ts, ts.mrange, sb, 0, faeid);
152}
153
154void gparticle::turn(const double ctheta, const double stheta) {
155
156 vec dir = m_currpos.dir;
157 basis temp(dir, "temp");
158 vec vturn;
159 vturn.random_round_vec();
160 vturn = vturn * stheta;
161 vec new_dir(vturn.x, vturn.y, ctheta);
162 new_dir.down(&temp);
163 m_currpos.dir = new_dir;
164 m_currpos.dirloc = m_currpos.dir;
165 m_currpos.tid.up_absref(&m_currpos.dirloc);
166}
167
169 // Generate next position in new volume.
170 mfunname("stvpoint gparticle::switch_new_vol(void)");
172 manip_absvol* eidl = nullptr;
173 stvpoint nextp = m_currpos;
174 point pth = nextp.pt;
175 // Search from primary
176 // In this case it does not necessarily switch to encountered volume
177 // namely nextp.next_eid
178 // Borders of two volumes may coincide. Thus it should look for
179 // the deepest volume at this point.
180 bool ok = false;
181 while (!ok) {
182 nextp.tid.eid[0]->m_find_embed_vol(pth, nextp.dir, &tidl);
183 if (tidl.eid.empty()) {
184 m_alive = false;
185 break;
186 }
187 // By default, assume switching to new volume.
188 int s_e = 1;
189 if (tidl == nextp.tid) {
190 // Remains in the same old volume, may be numerical error
191 // Will probably repeat attempt to switch at the same steps until ok.
192 s_e = 0;
193 double curprec = nextp.volume()->prec;
194 if (m_currpos.prange <= curprec) {
195 // very bad case, to repeat the trial.
196 vec additional_dist = nextp.dir * curprec;
197 pth = pth + additional_dist;
198 tidl = manip_absvol_treeid();
199 continue;
200 }
201 }
202 return stvpoint(pth, nextp.dir, nextp.speed, tidl, 0.0, nextp.time, 0, s_e,
203 eidl);
204 }
205 return stvpoint();
206}
207
208void gparticle::print(std::ostream& file, int l) const {
209 if (l < 0) return;
210 Ifile << "gparticle(l=" << l << "): alive=" << m_alive << " nstep=" << m_nstep
211 << " total_range_from_origin=" << m_total_range_from_origin
212 << " nzero_step=" << m_nzero_step << '\n';
213 if (l <= 1) return;
214 indn.n += 2;
215 if (l - 5 >= 0) {
216 Ifile << "origin point:\n";
217 indn.n += 2;
218 m_origin.print(file, l - 2);
219 indn.n -= 2;
220 }
221 if (l - 4 >= 0) {
222 Ifile << "previous point:\n";
223 indn.n += 2;
224 m_prevpos.print(file, l - 1);
225 indn.n -= 2;
226 }
227 if (l - 2 >= 0) {
228 Ifile << "current point:\n";
229 indn.n += 2;
230 m_currpos.print(file, l);
231 indn.n -= 2;
232 }
233 if (l - 3 >= 0) {
234 Ifile << "next point:\n";
235 indn.n += 2;
236 m_nextpos.print(file, l - 1);
237 indn.n -= 2;
238 }
239 indn.n -= 2;
240 file.flush();
241}
242}
#define check_econd12a(a, sign, b, add, stream)
#define mfunname(string)
vfloat prec
Definition volume.h:72
Basis.
Definition vec.h:313
virtual void print(std::ostream &file, int l) const
Print-out.
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
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
gparticle()=default
Default constructor.
void turn(const double ctheta, const double stheta)
Change the direction of the particle.
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.
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
static std::atomic< long > s_counter
Instance counter.
Definition gparticle.h:256
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
std::vector< manip_absvol * > eid
List of volumes.
Definition volume.h:37
Abstract base classs for volume "manipulators".
Definition volume.h:128
virtual int m_find_embed_vol(const point &fpt, const vec &fdir, manip_absvol_treeid *atid) const
Definition volume.cpp:169
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
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
vec dir
Unit vector, in the first system in the tree.
Definition gparticle.h:26
vfloat prange
Range from previous point.
Definition gparticle.h:47
vfloat speed
Longitudinal velocity.
Definition gparticle.h:32
point pt
Coordinates in the first system in the tree.
Definition gparticle.h:24
manip_absvol_treeid tid
Definition gparticle.h:33
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
vfloat x
Definition vec.h:192
vfloat length() const
Definition vec.h:196
vfloat z
Definition vec.h:194
void down(const basis *fabas)
Definition vec.cpp:168
void random_round_vec()
Generate random unit vector in plane perpendicular to z-axis.
Definition vec.cpp:229
vfloat y
Definition vec.h:193
Definition BGMesh.cpp:6
vec dv0(0, 0, 0)
Definition vec.h:308
indentation indn
Definition prstream.cpp:15
double vfloat
Definition vfloat.h:16
#define Ifile
Definition prstream.h:195
#define mcerr
Definition prstream.h:128
#define pvecerror(string)
Definition vec.h:28