/
e_beam_triple_poly.cpp
277 lines (240 loc) · 9.08 KB
/
e_beam_triple_poly.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*****************************************************************************
* e_beam_triple_poly.cpp: *
* class member functions for: *
* e_beam_triple_poly: triple electron source model, poly-energetic *
* *
* Copyright (C) 2002 Matthias Fippel *
* Abteilung fuer Medizinische Physik, *
* Universitaetsklinikum Tuebingen, Germany *
* *
* revisions: *
* initial coding MF 08.07.2002 *
* *
*****************************************************************************/
// ****************************************
// includes
// ****************************************
#include "e_beam_triple_poly.h"
// ****************************************
// declare functions and global variables
// ****************************************
real etime(void);
real trace_line(const real_3 &, const real_3 &, real &);
// ***********************************************
// member functions of class e_beam_triple_poly
// ***********************************************
// initialize energy spectrum
void e_beam_triple_poly::init_energy(void)
{
// copy auxiliary parameters to new variables
energy_X = energy_X_aux;
spectrum_A = spectrum_A_aux;
spectrum_alpha = spectrum_alpha_aux;
spectrum_beta = spectrum_beta_aux;
// initialize energy spectrum
// sampling parameters
real e1 = energy_X - energy_min;
real e2 = energy_X - energy_max;
e3 = energy_max - energy_min;
real e4 = energy_max + energy_min;
b = e1*e2,
c1 = e1*e1,
c2 = e2*e2;
d1 = energy_min*energy_min;
d2 = energy_max*energy_max;
r1 = ZERO;
r2 = 1.0e-35;;
// weights
w1 = (c1-c2)/TWO/(c1*c2);
w2 = spectrum_A*e3*e4/TWO;
w3 = spectrum_A*TWOxEMASS*e3;
real w4 = ZERO;
if (spectrum_alpha > ZERO)
{
if( spectrum_beta > ZERO)
{
real FIFTY = 50.0;
real temp1 = spectrum_beta*energy_min;
if (temp1 < FIFTY)
{
r1 = exp(-temp1);
w4 = r1*spectrum_alpha/spectrum_beta;
real temp2 = spectrum_beta*e3;
if (temp2 < FIFTY) w4 *= ONE-exp(-temp2);
real EIGHTY = 80.0;
real temp3 = spectrum_beta*energy_max;
if (temp3 < EIGHTY) r2 = exp(-temp3);
}
}
}
// normalize and print weights
real wtot = w1 + w2 + w3 + w4;
w1 /= wtot;
w2 /= wtot;
w3 /= wtot;
w4 /= wtot;
xvmc_message("Initialize energy spectrum, contributions:",1);
xvmc_message(" high energy (main): ",100.0*w1,"%",0);
xvmc_message(" linear: ",100.0*w2,"%",0);
xvmc_message(" constant: ",100.0*w3,"%",0);
xvmc_message(" low energy: ",100.0*w4,"%",0);
// mean energy
real e_mean1 = ZERO;
if (w1 > ZERO)
{
e_mean1 = ((e1-energy_min)/c1 - (e2-energy_max)/c2)/TWO/wtot;
}
real e_mean2 = ZERO;
if (w2 > ZERO)
{
e_mean2 =
spectrum_A*(pow(energy_max,THREE)-pow(energy_min,THREE))/THREE/wtot;
}
real e_mean3 = ZERO;
if (w3 > ZERO)
{
e_mean3 = spectrum_A*EMASS*e3*e4/wtot;
}
real e_mean4 = ZERO;
if (w4 > ZERO)
{
e_mean4 =
exp(-spectrum_beta*energy_min)*(ONE+spectrum_beta*energy_min);
e_mean4 -=
exp(-spectrum_beta*energy_max)*(ONE+spectrum_beta*energy_max);
e_mean4 *= spectrum_alpha/pow(spectrum_beta,TWO)/wtot;
}
energy_mean = e_mean1 + e_mean2 + e_mean3 + e_mean4;
xvmc_message(" average energy: ",energy_mean,"MeV",0);
xvmc_message(" maximum energy: ",energy_max,"MeV",0);
// cumulative weigths
w2 += w1;
w3 += w2;
}
// initialize photon background
void e_beam_triple_poly::init_photons(void)
{
// just copy auxiliary parameters to new variables
photo_a = photo_a_aux;
photo_b = photo_b_aux;
}
// add photon background to the dose distribution
bool e_beam_triple_poly::add_photons(real &cpu_time, int n_batch)
{
// measure CPU time
real cpu_start = etime();
// determine dose maximum
float dose,dose_max = ZERO;
for (register int k=0; k<dim.z; ++k)
{
for (register int j=0; j<dim.y; ++j)
{
for (register int i=0; i<dim.x; ++i)
{
dose = beam_dose->matrix[i][j][k];
if (dose > dose_max) dose_max = dose;
}
}
}
xvmc_message("Maximum dose before photon correction:",
dose_max,"10^-10 Gy cm^2",1);
// calculate scaled photon background depth parameters
real p_a = photo_a*dose_max/100.0;
real p_b = photo_b*dose_max/100.0;
real p_c = 6.0*log(20.0)/energy_max;
// calculate minimum and maximum beam angles for photon background
real tan_x_min=(app_x1 - 10.0)/app_distance;
real tan_x_max=(app_x2 + 10.0)/app_distance;
real tan_y_min=(app_y1 - 10.0)/app_distance;
real tan_y_max=(app_y2 + 10.0)/app_distance;
// calculate photon background profile parameters
real rx2 = 1.0 - (app_width_x-4.0)/42.0;
rx2 = 0.66*(rx2*app_width_x)*(rx2*app_width_x);
real ry2 = 1.0 - (app_width_y-4.0)/42.0;
ry2 = 0.66*(ry2*app_width_y)*(ry2*app_width_y);
// calculate photon background for all voxels
// (fan lines from origin point to each voxel center)
real_3 pos; // position of voxel center
real_3 dop; // difference vector from origin to voxel center
real temp;
pos.z = -voxel_size.z*ONE_HALF;
for (register int k=0; k<dim.z; ++k)
{
pos.z += voxel_size.z;
dop.z = pos.z - origin.z;
pos.y = -voxel_size.y*ONE_HALF;
for (register int j=0; j<dim.y; ++j)
{
pos.y += voxel_size.y;
dop.y = pos.y - origin.y;
pos.x = -voxel_size.x*ONE_HALF;
for (register int i=0; i<dim.x; ++i)
{
pos.x += voxel_size.x;
dop.x = pos.x - origin.x;
// origin to voxel center distance
real dist = sqrt(dop.x*dop.x + dop.y*dop.y + dop.z*dop.z);
// fan line direction vector
real_3 dir;
dir.x = dop.x/dist;
dir.y = dop.y/dist;
dir.z = dop.z/dist;
// rotate back by the table angle
temp = dir.x*sin_beta;
dir.x = dir.x*cos_beta - dir.y*sin_beta;
dir.y = temp + dir.y*cos_beta;
// rotate back by the gantry angle
temp = dir.x*sin_alpha;
dir.x = dir.x*cos_alpha + dir.z*sin_alpha;
dir.z = -temp + dir.z*cos_alpha;
// rotate back by the collimator angle
temp = dir.x*sin_gamma;
dir.x = dir.x*cos_gamma - dir.y*sin_gamma;
dir.y = temp + dir.y*cos_gamma;
// investigate fan lines
if (fabs(dir.z) > ZERO)
{
real tan_x = dir.x/fabs(dir.z);
real tan_y = dir.y/fabs(dir.z);
// the fan line must be within the beam cone
if ((tan_x > tan_x_min) && (tan_x < tan_x_max))
{
if ((tan_y > tan_y_min) && (tan_y < tan_y_max))
{
// geometrical length within the cube (dummy variable)
real length = ZERO;
// effective length in water
real eff_length = trace_line(origin, pos, length);
// profile parameters
temp = app_distance*tan_x;
real bpx = exp(-temp*temp/rx2);
temp = app_distance*tan_y;
real bpy = exp(-temp*temp/ry2);
// depth parameters
real dose = p_b*(ONE-exp(-eff_length*p_c))
+ p_a*eff_length;
// total photon dose (must be larger than 0)
dose *= bpx*bpy;
if (dose < ZERO) dose = ZERO;
// dose in vacuum or air is 0
real rho = density->matrix[i][j][k];
const real rho_min = 0.01;
dose = rho > rho_min ? dose : ZERO;
// the photon background should not contribute to the
// statistical uncertainty, the following update to the
// "beam_error->matrix" provides this functionality
real error =
(TWO*dose*beam_dose->matrix[i][j][k]+dose*dose)/n_batch;
// add photon background to beam dose and error matrices
beam_dose->matrix[i][j][k] += dose;
beam_error->matrix[i][j][k] += error;
}
}
}
}
}
}
// measure CPU time
cpu_time = etime() - cpu_start;
return(true);
}