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