Garfield++ 3.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) {
34 Ifile << "next volume name " << s << '\n';
35 }
36 indn.n -= 2;
37 file.flush();
38}
39
40gparticle::gparticle(manip_absvol* primvol, const point& pt, const vec& vel,
41 vfloat ftime)
42 : m_prevpos(),
43 m_nextpos() {
44 // As far as I can now understand, primvol will be at m_origin.tid.eid[0]
45 mfunname("gparticle::gparticle(...)");
46 primvol->m_find_embed_vol(pt, vel, &m_origin.tid);
47 m_origin.pt = pt;
48 if (vel == dv0) {
50 m_origin.speed = 0.0;
51 } else {
52 m_origin.dir = unit_vec(vel);
53 m_origin.speed = vel.length();
54 }
59 m_origin.time = ftime;
60 m_origin.sb = 0;
61 m_origin.s_ent = 1;
62 if (m_origin.tid.eid.empty()) return;
63 m_alive = true;
66 m_nextpos.s_ent = 0;
67}
68
69void gparticle::step(std::vector<gparticle*>& secondaries) {
70 // Make step to next point and calculate new step to border.
71 mfunname("void gparticle::step()");
75 m_nstep++;
76 if (m_currpos.prange == 0) {
79 "too many zero steps, possible infinite loop\n", mcerr);
80 } else {
81 m_nzero_step = 0;
82 }
83 physics_after_new_speed(secondaries);
84 if (m_alive) {
87 }
88}
89
90void gparticle::curvature(bool& curved, vec& frelcen, vfloat& fmrange,
91 vfloat /*prec*/) {
92 curved = false;
93 frelcen.x = 0.;
94 frelcen.y = 0.;
95 frelcen.z = 0.;
96 fmrange = max_vfloat;
97 /* The following is for debug
98 vec field(0,1,0);
99 vfloat rad = 10;
100 if (length(m_currpos.dir) > 0 && check_par(m_currpos.dir, field) == 0) {
101 curved = true;
102 vfloat coef = sin2vec(m_currpos.dir, field);
103 rad = rad / coef;
104 frelcen = unit_vec(m_currpos.dir || field) * rad;
105 }
106 */
107}
108
109void gparticle::physics_mrange(double& /*fmrange*/) {}
110
112 // Calculate next point as step to border.
113 pvecerror("stvpoint gparticle::calc_step_to_bord()");
114 if (m_currpos.sb > 0) {
115 // Just switch to new volume.
116 return switch_new_vol();
117 }
118 bool curved = false;
119 vec relcen;
120 vfloat mrange;
121 curvature(curved, relcen, mrange, m_max_straight_arange);
122 if (mrange <= 0) {
123 // Preserve current point for modification by physics.
124 stvpoint temp(m_currpos);
125 temp.s_ent = 0;
126 return temp;
127 }
128 // Change to local system.
129 m_currpos.tid.up_absref(&relcen);
130 physics_mrange(mrange);
131 trajestep ts(m_max_range, m_rad_for_straight,
132 m_max_straight_arange, m_max_circ_arange,
133 m_currpos.ptloc, m_currpos.dirloc, curved, relcen, mrange,
134 m_currpos.tid.eid.back()->Gavol()->prec);
135 if (ts.mrange <= 0) {
136 stvpoint temp(m_currpos);
137 temp.s_ent = 0;
138 return temp;
139 }
140 // Here the range is calculated:
141 int sb;
142 manip_absvol* faeid = nullptr;
143 m_currpos.tid.G_lavol()->range(ts, 1, sb, faeid);
144 // 1 means inside the volume and makes
145 // the program checking embraced volumes
146 if (ts.s_prec == 0) {
147 // Point is crossed.
148 return stvpoint(m_currpos, ts, sb, 0, faeid);
149 }
150 return stvpoint(m_currpos, ts, ts.mrange, sb, 0, faeid);
151}
152
154 // Generate next position in new volume.
155 mfunname("stvpoint gparticle::switch_new_vol(void)");
157 manip_absvol* eidl = nullptr;
158 stvpoint nextp = m_currpos;
159 point pth = nextp.pt;
160 // Search from primary
161 // In this case it does not necessarily switch to encountered volume
162 // namely nextp.next_eid
163 // Borders of two volumes may coincide. Thus it should look for
164 // the deepest volume at this point.
165 bool ok = false;
166 while (!ok) {
167 nextp.tid.eid[0]->m_find_embed_vol(pth, nextp.dir, &tidl);
168 if (tidl.eid.empty()) {
169 m_alive = false;
170 break;
171 }
172 // By default, assume switching to new volume.
173 int s_e = 1;
174 if (tidl == nextp.tid) {
175 // Remains in the same old volume, may be numerical error
176 // Will probably repeat attempt to switch at the same steps until ok.
177 s_e = 0;
178 double curprec = nextp.tid.G_lavol()->prec;
179 if (m_currpos.prange <= curprec) {
180 // very bad case, to repeat the trial.
181 vec additional_dist = nextp.dir * curprec;
182 pth = pth + additional_dist;
183 tidl = manip_absvol_treeid();
184 continue;
185 }
186 }
187 return stvpoint(pth, nextp.dir, nextp.speed, tidl, 0.0, nextp.time, 0, s_e,
188 eidl);
189 }
190 return stvpoint();
191}
192
193void gparticle::print(std::ostream& file, int l) const {
194 if (l < 0) return;
195 Ifile << "gparticle(l=" << l << "): alive=" << m_alive << " nstep=" << m_nstep
196 << " total_range_from_origin=" << m_total_range_from_origin
197 << " nzero_step=" << m_nzero_step << '\n';
198 if (l == 1) {
199 file.flush();
200 return;
201 }
202 indn.n += 2;
203 if (l - 5 >= 0) {
204 Ifile << "origin point:\n";
205 indn.n += 2;
206 m_origin.print(file, l - 2);
207 indn.n -= 2;
208 }
209 if (l - 4 >= 0) {
210 Ifile << "previous point:\n";
211 indn.n += 2;
212 m_prevpos.print(file, l - 1);
213 indn.n -= 2;
214 }
215 if (l - 2 >= 0) {
216 Ifile << "current point:\n";
217 indn.n += 2;
218 m_currpos.print(file, l);
219 indn.n -= 2;
220 }
221 if (l - 3 >= 0) {
222 Ifile << "next point:\n";
223 indn.n += 2;
224 m_nextpos.print(file, l - 1);
225 indn.n -= 2;
226 }
227 indn.n -= 2;
228 file.flush();
229}
230}
#define check_econd12a(a, sign, b, add, stream)
Definition: FunNameStack.h:181
#define mfunname(string)
Definition: FunNameStack.h:45
virtual int range(trajestep &fts, int s_ext, int &sb, manip_absvol *&faeid) const
Definition: volume.cpp:99
vfloat prec
Definition: volume.h:72
virtual void print(std::ostream &file, int l) const
Print-out.
Definition: gparticle.cpp:193
virtual void physics_mrange(double &fmrange)
Definition: gparticle.cpp:109
virtual void physics_after_new_speed(std::vector< gparticle * > &)
Apply any other processes (turn the trajectory, kill the particle, ...).
Definition: gparticle.h:215
stvpoint m_nextpos
Next point.
Definition: gparticle.h:252
double m_total_range_from_origin
Range from origin to current position.
Definition: gparticle.h:245
virtual stvpoint calc_step_to_bord()
Determine next position.
Definition: gparticle.cpp:111
stvpoint m_prevpos
Previous point.
Definition: gparticle.h:248
static constexpr long m_max_qzero_step
Max. number of zero-steps allowed.
Definition: gparticle.h:238
virtual void step(std::vector< gparticle * > &secondaries)
Definition: gparticle.cpp:69
gparticle()=default
Default constructor.
stvpoint m_currpos
Current point.
Definition: gparticle.h:250
virtual void change_vol()
Move from one volume to another.
Definition: gparticle.h:197
stvpoint switch_new_vol()
Generate next position in new volume.
Definition: gparticle.cpp:153
long m_nzero_step
Number of previous steps with zero range (including this step).
Definition: gparticle.h:240
stvpoint m_origin
Original point.
Definition: gparticle.h:243
bool m_alive
Status flag whether the particle is active.
Definition: gparticle.h:233
long m_nstep
Step number.
Definition: gparticle.h:236
virtual void curvature(bool &curved, vec &frelcen, vfloat &fmrange, vfloat prec)
Definition: gparticle.cpp:90
Service class (array of manip_absvol).
Definition: volume.h:32
void print(std::ostream &file, int l) const
Definition: volume.cpp:54
void up_absref(absref *f)
Definition: volume.cpp:26
std::vector< manip_absvol * > eid
List of volumes.
Definition: volume.h:37
absvol * G_lavol() const
Get last address of volume.
Definition: volume.cpp:17
Abstract base classs for volume "manipulators".
Definition: volume.h:128
void m_chname(char *nm) const
Definition: volume.cpp:223
virtual int m_find_embed_vol(const point &fpt, const vec &fdir, manip_absvol_treeid *atid) const
Definition: volume.cpp:168
Point.
Definition: vec.h:366
Point in space, time and velocity.
Definition: gparticle.h:20
vfloat time
Definition: gparticle.h:47
void print(std::ostream &file, int l) const
Definition: gparticle.cpp:16
manip_absvol * next_eid
Definition: gparticle.h:44
vec dirloc
Unit vector, in the local system (last system in the tree).
Definition: gparticle.h:29
vec dir
Unit vector, in the first system in the tree.
Definition: gparticle.h:25
vfloat prange
Range from previous point.
Definition: gparticle.h:46
vfloat speed
Longitudinal velocity.
Definition: gparticle.h:31
point pt
Coordinates in the first system in the tree.
Definition: gparticle.h:23
manip_absvol_treeid tid
Definition: gparticle.h:32
point ptloc
Coordinates in the local system (last system in the tree).
Definition: gparticle.h:27
vfloat mrange
Maximal possible range.
Definition: trajestep.h:93
Definition: vec.h:177
vfloat x
Definition: vec.h:190
vfloat length() const
Definition: vec.h:194
vfloat z
Definition: vec.h:192
vfloat y
Definition: vec.h:191
Definition: BGMesh.cpp:6
vec dv0(0, 0, 0)
Definition: vec.h:306
indentation indn
Definition: prstream.cpp:15
double vfloat
Definition: vfloat.h:16
#define Ifile
Definition: prstream.h:196
#define mcerr
Definition: prstream.h:128
#define pvecerror(string)
Definition: vec.h:28