-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmedium.cpp
335 lines (260 loc) · 9.28 KB
/
medium.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "debug.h"
#include "vector3D.h"
#include "layer.h"
#include "medium.h"
#include "detector.h"
#include <cmath>
#include <cassert>
#include <cstdlib>
#undef DEBUG
Medium::Medium()
{
cout << "Error: Medium::Medium() called, must give default values\n";
}
Medium::Medium(const double x, const double y, const double z)
{
this->z_bound = z;
this->y_bound = y;
this->x_bound = x;
this->initCommon();
}
Medium::~Medium()
{
// If there were any absorbers in the medium, write out their data.
for (vector<Layer *>::iterator it = p_layers.begin(); it != p_layers.end(); it++)
{
(*it)->writeAbsorberData();
delete *it;
}
}
void Medium::initCommon(void)
{
radial_size = 3.0; // Total range in which bins are extended (cm).
num_radial_pos = MAX_BINS-1; // Set the number of bins.
radial_bin_size = radial_size / num_radial_pos;
Cplanar = NULL; // Planar detector array.
}
void Medium::setPlanarArray(double *array)
{
Cplanar = array;
// Initialize all the bins to zero since they will serve as accumulators.
int i;
for (i = 0; i < MAX_BINS; i++) {
Cplanar[i] = 0;
}
}
// Add the layer to the medium by pushing it onto the vector container.
void Medium::addLayer(Layer *layer)
{
p_layers.push_back(layer);
}
void Medium::addDetector(Detector *detector)
{
p_detectors.push_back(detector);
}
void Medium::absorbEnergy(const double z, const double energy)
{
#ifdef DEBUG
cout << "Updating bin...\n";
#endif
boost::mutex::scoped_lock lock(m_sensor_mutex);
double r = fabs(z);
int ir = (r/radial_size);
Cplanar[ir] += energy;
}
void Medium::absorbEnergy(const double *energy_array)
{
int i;
// Grab the lock to ensure a single thread has access
// to update the global array.
boost::mutex::scoped_lock lock(m_sensor_mutex);
for (i = 0; i < MAX_BINS; i++) {
// Grab the lock to serialize threads when updating
// the global planar detection array in the Medium.
Cplanar[i] += energy_array[i];
}
}
// See if photon has crossed the detector plane.
int Medium::photonHitDetectorPlane(const boost::shared_ptr<Vector3d> p0)
{
bool hitDetectorNumTimes = 0;
// Free the memory for layers that were added to the medium.
for (vector<Detector *>::iterator it = p_detectors.begin(); it != p_detectors.end(); it++)
{
if ((*it)->photonHitDetector(p0))
hitDetectorNumTimes++;
}
return hitDetectorNumTimes;
}
Layer * Medium::getLayerAboveCurrent(Layer *currentLayer)
{
// Ensure that the photon's z-axis coordinate is sane. That is,
// it has not left the medium.
assert(currentLayer != NULL);
// If we have only one layer, no need to iterate through the vector.
// And we should return NULL since there is no layer above us.
if (p_layers.size() == 1)
return NULL;
// Otherwise we walk the vector and return 'trailer' since it is the
// one before the current layer (i.e. 'it').
vector<Layer *>::iterator it;
vector<Layer *>::iterator trailer;
it = p_layers.begin(); // Get the first layer from the array.
// If we are at the top of the medium there is no layer above, so return NULL;
if (currentLayer == (*it))
return NULL;
while(it != p_layers.end()) {
trailer = it; // Assign the trailer to the current layer.
it++; // Advance the iterator to the next layer.
// Find the layer we are in within the medium based on the depth (i.e. z)
// that was passed in. Break from the loop when we find the correct layer
// because trailer will be pointing to the previous layer in the medium.
//if ((*it)->getDepthStart() <= z && (*it)->getDepthEnd() >= z)
if ((*it) == currentLayer)
break;
}
// Sanity check. If the trailer has made it to the end, which means
// the iterator made it past the end, then there
// was no previous layer found, and something went wrong.
if (trailer == p_layers.end())
return NULL;
// If we make it here, we have found the previous layer.
return *trailer;
}
Layer * Medium::getLayerBelowCurrent(double z)
{
// Ensure that the photon's z-axis coordinate is sane. That is,
// it has not left the medium.
assert(z >= 0 && z <= z_bound);
// If we have only one layer, no need to iterate through the vector.
// And we should return NULL since there is no layer below us.
if (p_layers.size() == 1)
return NULL;
// The case where there is no layer below is since we are at the bottom of the
// medium.
if (z == z_bound)
return NULL;
vector<Layer *>::iterator it;
for (it = p_layers.begin(); it != p_layers.end(); it++) {
// Find the layer we are in within the medium based on the depth (i.e. z)
// that was passed in. Break from the loop when we find the correct layer.
if ((*it)->getDepthStart() <= z && (*it)->getDepthEnd() >= z) {
return *(++it);
}
}
// If the above loop never returned a layer it means we made it through the list
// so there is no layer below us, therefore we return null.
return NULL;
}
// Return the layer in the medium at the passed in depth 'z'.
// We iterate through the vector which contains pointers to the layers.
// When the correct layer is found from the depth we return the layer object.
Layer * Medium::getLayerFromDepth(double z)
{
// Ensure that the photon's z-axis coordinate is sane. That is,
// it has not left the medium.
assert(z >= 0 && z <= z_bound);
vector<Layer *>::iterator it;
for (it = p_layers.begin(); it != p_layers.end(); it++) {
// Find the layer we are in within the medium based on the depth (i.e. z)
// that was passed in. Break from the loop when we find the correct layer.
if ((*it)->getDepthStart() <= z && (*it)->getDepthEnd() >= z)
break;
}
// Return layer based on the depth passed in.
return *it;
}
double Medium::getLayerAbsorptionCoeff(double z)
{
// Ensure that the photon's z-axis coordinate is sane. That is,
// it has not left the medium.
assert(z >= 0 && z <= z_bound);
double absorp_coeff = -1;
vector<Layer *>::iterator it;
for (it = p_layers.begin(); it != p_layers.end(); it++) {
// Find the layer we are it in the medium based on the depth (i.e. z)
// that was passed in. Break from the loop when we find the correct layer.
if ((*it)->getDepthStart() <= z && (*it)->getDepthEnd() >= z) {
absorp_coeff = (*it)->getAbsorpCoeff();
break;
}
}
// If not found, report error.
assert(absorp_coeff != 0);
// If not found, fail.
// If not found, report error.
assert(absorp_coeff != -1);
// Return the absorption coefficient value.
return absorp_coeff;
}
double Medium::getLayerScatterCoeff(double z)
{
// Ensure that the photon's z-axis coordinate is sane. That is,
// it has not left the medium.
assert(z >= 0 && z <= z_bound);
double scatter_coeff = -1;
vector<Layer *>::iterator it;
for (it = p_layers.begin(); it != p_layers.end(); it++) {
// Find the layer we are it in the medium based on the depth (i.e. z)
// that was passed in. Break from the loop when we find the correct layer.
if ((*it)->getDepthStart() <= z && (*it)->getDepthEnd() >= z) {
scatter_coeff = (*it)->getScatterCoeff();
break;
}
}
// If not found, report error.
assert(scatter_coeff != 0);
// If not found, fail.
// If not found, report error.
assert(scatter_coeff != -1);
// Return the scattering coefficient for the layer that resides at depth 'z'.
return scatter_coeff;
}
double Medium::getAnisotropyFromDepth(double z)
{
// Ensure that the photon's z-axis coordinate is sane. That is,
// it has not left the medium.
assert(z >= 0 && z <= z_bound);
double anisotropy = -1;
vector<Layer *>::iterator it;
for (it = p_layers.begin(); it != p_layers.end(); it++) {
// Find the layer we are it in the medium based on the depth (i.e. z)
// that was passed in. Break from the loop when we find the correct layer.
if ((*it)->getDepthStart() <= z && (*it)->getDepthEnd() >= z) {
anisotropy = (*it)->getAnisotropy();
break;
}
}
// If not found, report error.
assert(anisotropy != 0);
// If not found, fail.
// If not found, report error.
assert(anisotropy != -1);
// Return the anisotropy value for the layer that resides at depth 'z'.
return anisotropy;
}
void Medium::printGrid(const int numPhotons)
{
// Open the file we will write to.
ofstream output;
output.open("fluences.txt");
// Print the header information to the file.
//output << "r [cm] \t Fsph [1/cm2] \t Fcyl [1/cm2] \t Fpla [1/cm2]\n";
//output << "r [cm] \t Fplanar[1/cm^2]\n";
double mu_a = p_layers[0]->getAbsorpCoeff();
double fluencePlanar = 0;
double r = 0;
double shellVolume = 0;
for (int ir = 0; ir <= num_radial_pos; ir++) {
r = (ir + 0.5)*radial_bin_size;
shellVolume = radial_bin_size;
fluencePlanar = Cplanar[ir]/numPhotons/shellVolume/mu_a;
// Print to file with the value for 'r' in fixed notation and having a
// precision of 5 decimal places, followed by the fluence in scientific
// notation with a precision of 3 decimal places.
output << fixed << setprecision(5) << r << "\t \t";
output << scientific << setprecision(3) << fluencePlanar << "\n";
}
// close the file.
output.close();
}