EGS Brachy
An egs++ user code for rapid brachytherapy calculations
Loading...
Searching...
No Matches
eb_iaeaphsp_source.cpp
Go to the documentation of this file.
1/*
2################################################################################
3#
4# egs_brachy eb_iaeaphsp_source.cpp
5# Copyright (C) 2016 Rowan Thomson, Dave Rogers, Randle Taylor, and Marc
6# Chamberland
7#
8# This file is part of egs_brachy
9#
10# egs_brachy is free software: you can redistribute it and/or modify it
11# under the terms of the GNU Affero General Public License as published
12# by the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# egs_brachy is distributed in the hope that it will be useful, but
16# WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18# Affero General Public License for more details:
19# <http://www.gnu.org/licenses/>.
20#
21################################################################################
22#
23# When egs_brachy is used for publications, please cite our paper:
24# M. J. P. Chamberland, R. E. P. Taylor, D. W. O. Rogers, and R. M. Thomson,
25# egs brachy: a versatile and fast Monte Carlo code for brachytherapy,
26# Phys. Med. Biol. 61, 8214-8231 (2016).
27#
28################################################################################
29#
30# Author: Randle Taylor, 2016
31#
32# Contributors: Marc Chamberland
33# Dave Rogers
34# Rowan Thomson
35#
36################################################################################
37*/
38
42#include <sstream>
43#include "eb_iaeaphsp_source.h"
44#include "egs_application.h"
45#include "egs_input.h"
46#include "egs_functions.h"
47
48
49
51const string EB_IAEASource::iaea_header_ext = ".IAEAheader";
52
53EB_IAEASource::EB_IAEASource(EGS_Input *input, EGS_ObjectFactory *f) :
54 EGS_BaseSource(input,f) {
55
56 otype = "EB_IAEAPHSPSource";
57
58 int err = input->getInput("header file", phsp_file_name);
59 if (err) {
60 egsWarning("EB_IAEASource: no 'header file' input\n");
61 return;
62 }
63
64 size_t e_sz = iaea_header_ext.size();
65 size_t p_sz = phsp_file_name.size();
66 if ((p_sz > e_sz) && (phsp_file_name.substr(p_sz - e_sz) == iaea_header_ext)) {
67 phsp_file_name = phsp_file_name.substr(0,p_sz - e_sz);
68 }
69
72
73 EGS_Application *app = EGS_Application::activeApplication();
74
75 i_parallel = app->getIparallel();
76 n_parallel = app->getNparallel();
77
78 Nread = 0;
79 count = 0;
80 Npos = 0;
81 Nlast = 0;
82
84
85 if (!isValid()) {
86 egsWarning(
87 "EB_IAEASource: errors while opening the phase space file %s\n",
88 phsp_file_name.c_str()
89 );
90 return;
91 }
92
94
95 count = 0;
96
97 stringstream ss;
98 ss << "Phase space source from " + phsp_file_name << endl;
99 ss << " Number of original particles = " << Nincident << endl;
100 ss << " Number of particles in phsp = " << Nparticle << endl;
101 ss << " Max energy of particles = " << Emax << " MeV" << endl;
102 description = ss.str();
103
104}
105
106
108 if (isValid()) {
109 IAEA_I32 result;
110 iaea_destroy_source(p_source_id, &result);
111 }
112
113}
114
115
117
118 IAEA_I32 access = 1; // read only
119 IAEA_I32 result;
120 char *hname = new char[phsp_file_name.size()+1];
121 copy(phsp_file_name.begin(), phsp_file_name.end(), hname);
122 hname[phsp_file_name.size()] = '\0';
123
124 iaea_new_source(p_source_id, hname, &access, &result, phsp_file_name.size());
125
126 is_valid = result > 0;
127
128
129}
130
132
133 IAEA_Float ftmp;
134 IAEA_I64 i64tmp;
135
136 Emin = 0;
137
138 iaea_get_maximum_energy(p_source_id, &ftmp);
139 Emax = (EGS_Float)ftmp;
140
141 iaea_get_total_original_particles(p_source_id, &i64tmp);
142 Nincident = (IAEA_I64)i64tmp;
143
144 IAEA_I32 type = -1;
145 iaea_get_max_particles(p_source_id, &type, &i64tmp);
146 Nparticle = (IAEA_I64)i64tmp;
147
148 iaea_get_used_original_particles(p_source_id, &i64tmp);
149 Nused = (IAEA_I64)i64tmp;
150
151 Npos = 0;
153 Nfirst = 1;
154
155}
156
157IAEA_I64 EB_IAEASource::getNextParticle(EGS_RandomGenerator *rndm, int &q,
158 int &latch, EGS_Float &E, EGS_Float &wt, EGS_Vector &x, EGS_Vector &u) {
159
160 IAEA_I32 n_stat, type, extra_ints;
161 IAEA_Float Ei, wti, xi, yi, zi, ui, vi, wi, extra_floats;
162
163 iaea_get_particle(
164 p_source_id, &n_stat,
165 &type, &Ei, &wti,
166 &xi, &yi, &zi,
167 &ui, &vi, &wi,
168 &extra_floats, &extra_ints
169 );
170 Nread++;
171
172 if (n_stat < 0) {
173 egsInformation(
174 "\nEB_IAEASource:: Error reading particle from phsp file (n_stat=%d).\n"
175 "Rewinding to first record (may cause statistical biases)\n",
176 n_stat
177 );
178 IAEA_I32 result;
179 iaea_set_record(p_source_id, &Nfirst, &result);
180 return getNextParticle(rndm, q, latch, E, wt, x, u);
181 }
182
183
184 if (type == 1) {
185 q = 0;
186 } else if (type == 2) {
187 q = -1;
188 } else if (type == 3) {
189 q = 1;
190 }
191
192
193 latch = 0;
194 E = Ei;
195 wt = wti;
196 x.x = xi;
197 x.y = yi;
198 x.z = zi;
199 u.x = ui;
200 u.y = vi;
201 u.z = wi;
202 count++;
203
204 return count;
205}
206
207
208void EB_IAEASource::setSimulationChunk(IAEA_I64 nstart, IAEA_I64 nrun) {
209
210 if (nstart < 0 || nrun < 1 || nstart + nrun > Nparticle) {
211 egsWarning(
212 "EB_IAEASource::setSimulationChunk(): illegal attempt "
213 "to set the simulation chunk between %lld and %lld ignored\n",
214 nstart, nstart + nrun - 1);
215 return;
216 }
217
218 Nfirst = nstart + 1;
219 Nlast = Nfirst + nrun;
220 Npos = Nfirst;
221
222 IAEA_I32 res;
223 iaea_set_record(p_source_id, &Npos, &res);
224
225 if (res !=0) {
226 egsFatal("EB_IAEASource:: failed to set record to %d", Npos);
227 }
228
229}
230
231
232EGS_Float EB_IAEASource::getEmax() const {
233 return Emax;
234};
235
237 return is_valid;
238}
239
240EGS_Float EB_IAEASource::getFluence() const {
241 double aux = ((double) count)/((double) Nparticle);
242 return Nincident*aux;
243}
244
245bool EB_IAEASource::storeState(ostream &data) const {
246
247 data << endl;
248
249 bool res = egsStoreI64(data, Nread);
250 if (!res) {
251 return res;
252 }
253 data << " ";
254
255 res = egsStoreI64(data, Nfirst);
256 if (!res) {
257 return res;
258 }
259 data << " ";
260
261 res = egsStoreI64(data, Nlast);
262 if (!res) {
263 return res;
264 }
265 data << " ";
266
267 res = egsStoreI64(data, Npos);
268 if (!res) {
269 return res;
270 }
271 data << " ";
272
273 res = egsStoreI64(data, count);
274 if (!res) {
275 return res;
276 }
277 data << " ";
278
279 return true;
280}
281
282bool EB_IAEASource::setState(istream &data) {
283
284
285 bool res = egsGetI64(data,Nread);
286 if (!res) {
287 return res;
288 }
289
290 res = egsGetI64(data,Nfirst);
291 if (!res) {
292 return res;
293 }
294
295 res = egsGetI64(data,Nlast);
296 if (!res) {
297 return res;
298 }
299
300 res = egsGetI64(data,Npos);
301 if (!res) {
302 return res;
303 }
304
305
306 IAEA_I32 ires;
307 iaea_set_record(p_source_id, &Npos, &ires);
308 if (ires !=0) {
309 egsInformation("Failed to find record %d\n", ires);
310 return false;
311 }
312
313 res = egsGetI64(data, count);
314
315 return res;
316
317};
318
319bool EB_IAEASource::addState(istream &data) {
320
321 IAEA_I64 tmp_Nread = Nread;
322 IAEA_I64 tmp_count = count;
323 bool res = setState(data);
324 Nread += tmp_Nread;
325 count += tmp_count;
326
327 return res;
328};
329
331 Nread = 0;
332 count = 0;
333}
334
335extern "C" {
336
337 EB_IAEA_SOURCE_EXPORT EGS_BaseSource *createSource(EGS_Input *input, EGS_ObjectFactory *f) {
338 return createSourceTemplate<EB_IAEASource>(input,f,"iaea phsp source");
339 }
340
341}
static IAEA_I32 next_source_id
static const string iaea_header_ext
EGS_Float Emax
Maximum energy (obtained from the phsp file)
string phsp_file_name
The phase space file name.
IAEA_I64 Nfirst
first record this source can use
bool addState(istream &data)
EGS_Float getFluence() const
EGS_Float Nincident
Number of incident particles that created the file.
IAEA_I64 Nlast
Last record this source can use.
IAEA_I64 count
Particles delivered so far.
EB_IAEASource(EGS_Input *, EGS_ObjectFactory *f=0)
Constructor.
IAEA_I64 Nparticle
Number of particles in the file.
IAEA_I64 Npos
Next record to be read.
bool storeState(ostream &data) const
EGS_Float getEmax() const
EGS_I64 getNextParticle(EGS_RandomGenerator *rndm, int &q, int &latch, EGS_Float &E, EGS_Float &wt, EGS_Vector &x, EGS_Vector &u)
IAEA_I64 Nread
Number of particles read from file so far.
IAEA_I64 Nused
Number of particles used so far.
void setSimulationChunk(EGS_I64 nstart, EGS_I64 nrun)
EGS_Float Emin
Minimum energy (obtained from the phsp file)
bool setState(istream &data)
EB_IAEA_SOURCE_EXPORT EGS_BaseSource * createSource(EGS_Input *input, EGS_ObjectFactory *f)
a minimal IAEA phase space source for egs_brachy
#define EB_IAEA_SOURCE_EXPORT