EGS Brachy
An egs++ user code for rapid brachytherapy calculations
Loading...
Searching...
No Matches
/Users/marc/Developer/EGSnrc/HEN_HOUSE/user_codes/egs_brachy/egs_brachy/egs_brachy.h
Go to the documentation of this file.
1/*
2################################################################################
3#
4# egs_brachy egs_brachy.h
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
44#ifndef EGS_BRACHY_
45#define EGS_BRACHY_
46
47// stdlib dependencies
48#include <map>
49#include <set>
50#include <stack>
51#include <cstdlib>
52
53// egs++ dependencies
54#include "egs_advanced_application.h"
55#include "egs_functions.h"
56#include "egs_input.h"
57#include "egs_interface2.h"
58#include "egs_interpolator.h"
59#include "egs_alias_table.h"
60#include "egs_rndm.h"
61#include "egs_run_control.h"
62#include "egs_scoring.h"
63#include "egs_transformations.h"
64#include "egs_autoenvelope/egs_autoenvelope.h" // required for Superposition mode
65#include "egs_rz/egs_rz.h"
66#include "egs_spheres/egs_spheres.h"
67
68// local dependencies
69#include "pubsub.h"
70#include "ginfo.h"
71#include "muen.h"
72#include "phantom.h"
73#include "eb_volcor.h"
74#include "spec_scoring.h"
75#include "recycle.h"
76#include "latch.h"
77#include "phsp.h"
78#include "timing.h"
79
80
81#define PRINT_PARTICLE(P) cout << "\nParticle in reg "<<P.ir << " at "<<P.x.x << " "<<P.x.y<<" "<<P.x.z << " wt "<<P.wt<< " E "<<P.E << " q "<<P.q << " latch "<<P.latch<<"\n";
82#define PRINT_PARTICLE_WITH_DIR(P) cout << "\nParticle in reg "<<P.ir << " at "<<P.x.x << " "<<P.x.y<<" "<<P.x.z << " dir "<<P.u.x << " "<<P.u.y<<" "<<P.u.z << " wt "<<P.wt<< " E "<<P.E << " q "<<P.q << " latch "<<P.latch<<"\n";
83#define NUM_STUCK_STEPS 1000
84#define SAME_POSITION_TOLERANCE 1E-10
85#define EB_EPSILON 1E-10
86
88
91class APP_EXPORT EB_Application : public EGS_AdvancedApplication {
92
93 enum RunMode {
96 RM_VC_ONLY
97 };
98
99
102
104 vector<BaseSpectrumScorer *> spectrum_scorers;
105 //SpectrumScoringControl *spec_scoring;
106
110
111 vector<EGS_Float> source_weights;
113
115
119
121
126
129
130 vector<string> output_volcor_phantoms;
132
134 vector<EGS_Vector> p_init_locs;
135
136 EGS_Vector last_position;
137 EGS_Float last_R;
138 EGS_Float cur_R;
140 EGS_I64 n_stuck;
141
142 EGS_BaseGeometry *source_envelope_geom;
143 EGS_ASwitchedEnvelope *superpos_geom;
144
145 vector<EB_Phantom *> phantom_geoms;
146
147 vector<EGS_AffineTransform *> source_transforms;
148 EGS_AffineTransform *base_transform;
149 EGS_AffineTransform *base_transform_inv;
150
151 map<int, EGS_Interpolator *> media_muen;
152 map<string, string> media_muen_names;
153
154
157
160 EGS_Float bcse_factor;
161 static const EGS_Float DEFAULT_BCSE_FACTOR;
162
163 EGS_Float flu_cutoff;
164 EGS_Float source_ecut;
165 EGS_Float source_pcut;
166 EGS_Float global_ecut;
167 EGS_Float global_pcut;
168
170 EGS_Float global_e_max_rr;
171
173 EGS_Float source_e_max_rr;
174
176
179
180 map<string, vector<int> > extra_scoring_reg;
181 map<string, vector<EGS_Float> > extra_scoring_vols;
182 map<string, vector<EGS_Float> > extra_scoring_mass;
183 map<string, EGS_ScoringArray* > extra_scoring_doses;
184 map<string, EGS_ScoringArray* > extra_scoring_doses_edep;
185
189
191
193
194 map<int, EGS_I64> steps_in_sources;
195 map<int, EGS_I64> steps_in_phantoms;
196 map<int, EGS_I64> steps_in_other;
197
200
201 static string revision;
202
203
206 int createPhantoms();
207
208
210 int correctVolumes();
211
212 /* \brief check sources for overlap using a simple MC routine */
213 int checkSourceOverlaps(EGS_Input *);
214
216 void initTrackLengthScoring(EGS_Input *);
217
219 void initMuenData(EGS_Input *);
220
222 void initOutputFiles(EGS_Input *);
223
225 void initPHSPScoring(EGS_Input *);
226
228 void initEDepScoring(EGS_Input *);
229
231 void initScatScoring(EGS_Input *);
232
234 void clearAusgabCalls();
235
237 void enableAusgabCalls(int ncalls, AusgabCall calls[]);
238
239
241 void initGCRScoring(EGS_Input *);
242
247 void initAusgabCalls();
248
250 void initDoseScaling(EGS_Input *);
251
253 void initXCCScaling(EGS_Input *);
254
256 void initSpectrumScoring(EGS_Input *);
257
258 int initCrossSections();
259
260 void discardTopParticle(int idisc=1);
261
262 void calcEffectiveHistories();
263
264 bool isStuck();
265
266public:
267
268
270 EB_Application(int argc, char **argv) :
271 EGS_AdvancedApplication(argc,argv),
272 run_mode(RM_NORMAL),
273 escoring(),
274 recycle_opts(0),
275 active_source(0),
276 phsp(0),
277 score_edep(0),
278 score_scat(0),
279 output_3ddose_files(true),
280 record_n_init(0),
281 n_stuck(0),
282 superpos_geom(0),
283 phantom_geoms(),
284 do_brem_split(false),
285 nbr_split(1),
286 do_bcse(false),
287 bcse_factor(1),
288 flu_cutoff(0),
289 ginfo(),
290 gcr_phantom(0), gcr_phantom_reg(0),
291 gz_data_out(0),
292 gz_data_in(0),
293 nsources(0),
294 effective_histories(0) {
295
296 for (int i=-1; i < 2; i++) {
297 steps_in_sources[i] = 0;
298 steps_in_phantoms[i] = 0;
299 steps_in_other[i] = 0;
300 }
301
302 pevent_pub.subscribe(&latch_control, PARTICLE_ESCAPING_SOURCE);
303 pevent_pub.subscribe(&latch_control, PARTICLE_ESCAPED_SOURCE);
304 }
305
308
309 for (vector<EB_Phantom>::size_type pp=0; pp < phantom_geoms.size(); pp++) {
310 delete phantom_geoms[pp];
311 }
312 phantom_geoms.clear();
313
314 map<int, EGS_Interpolator *>::iterator mit = media_muen.begin();
315 for (; mit != media_muen.end(); mit++) {
316 delete mit->second;
317 }
318 media_muen.clear();
319
320
321 for (vector<EGS_AffineTransform *>::size_type st=0; st < source_transforms.size(); st++) {
322 delete source_transforms[st];
323 }
324 source_transforms.clear();
325
326 for (map<string, EGS_ScoringArray* >::iterator sa=extra_scoring_doses.begin(); sa != extra_scoring_doses.end(); sa++) {
327 delete sa->second;
328 }
329 extra_scoring_doses.clear();
330
331 for (map<string, EGS_ScoringArray* >::iterator sa=extra_scoring_doses_edep.begin(); sa != extra_scoring_doses_edep.end(); sa++) {
332 delete sa->second;
333 }
334 extra_scoring_doses_edep.clear();
335
336 if (phsp) {
337 delete phsp;
338 }
339
340 if (escoring) {
341 delete escoring;
342 }
343
344 vector<BaseSpectrumScorer *>::iterator it = spectrum_scorers.begin();
345 for (; it != spectrum_scorers.end(); it++) {
346 delete *it;
347 }
348 spectrum_scorers.clear();
349
350 if (recycle_opts) {
351 delete recycle_opts;
352 }
353
354 if (base_transform_inv) {
355 delete base_transform_inv;
356 }
357
358 if (gz_data_in) {
359 delete gz_data_in;
360 }
361
362 if (gz_data_out) {
363 delete gz_data_out;
364 }
365
366 };
367
369
371
373
375 void describeUserCode() const;
376
381 void describeSimulation();
382
383 void printIncludedFiles();
384
386 int initSimulation();
387
389 int initRunControl();
390
392 int initRunMode();
393
395 int initScoring();
396
398 int initVarianceReduction();
399
401 int initRussianRoulette(EGS_Input *);
402
404 int initBCSE(EGS_Input *);
405
407 int ausgab(int iarg);
408
415 int outputData();
416
418 int outputDataHelper(ostream *);
419
421 int egsApplicationOutputData(ostream *);
422
424 int egsAdvApplicationOutputData(ostream *);
425
427 int egsBrachyOutputData(ostream *);
428
432 int readData();
433
435 int readDataHelper(istream *);
436
438 int egsApplicationReadData(istream *);
439
441 int egsAdvApplicationReadData(istream *);
442
444 int egsBrachyReadData(istream *);
445
446 int combineResults();
447
452 void resetCounter();
453
458 int addState(istream &data);
459
460
461 /* \brief output any results to console */
462 void outputResults();
463
464
466 void getCurrentResult(double &sum, double &sum2, double &norm,
467 double &count);
468
470 EB_Phantom* getPhantomByName(string name);
471
473 virtual void startNewParticle();
474 virtual void enterNewRegion();
475
476 virtual int runSimulation();
477
479 return output_volcor_format;
480 }
481
482 int howManyJobsDone();
483
484protected:
485
486 void addRecycledParticlesToStack(EGS_Particle *p, bool new_hist=false);
487 void copyParticleToSourceLoc(EGS_Particle *p, int source, bool kill_orig, bool rotate, EGS_Float new_wt);
488
489 void doPhotonSplitting(int);
490
491
492 /* \brief initialize a single shower history.
493 *
494 * Particle is initiated from source and then transformed to location
495 * of next source */
496 int simulateSingleShower();
497 int startNewShower();
498
503 int initGeometry();
504
505
506 /* \brief override default initSource to allow us to get source position weighting */
507 int initSource();
508
509
511 int initSourceTransforms();
512
513 /* \brief take an input containging multiple `transformation` input blocks
514 * and return a vector of pointers to the generated transforms.
515 */
516 vector<EGS_AffineTransform *> createTransforms(EGS_Input *input);
517
518};
519
520class EB_UniformRunControl : public EGS_RunControl {
521
522public:
523
524 EB_UniformRunControl(EB_Application *app, string egsdat_format);
526
527 void describeRCO();
528
529 int startSimulation();
530
539 int finishSimulation();
540
541protected:
542
544
546
547 int milliseconds; // time interval for checking
548 // if all jobs finished (default 1000 ms)
549
550 int check_intervals;// Number of intervals to check
551 // if all jobs done (default 5)
552
553 int njob;
554 int npar;
555 int ipar;
557 bool check_egsdat;// If true, and a 'watcher' job, produce intermediate results
558 bool watcher_job; // If true, job is a 'watcher'
559};
560
561#endif
The main egs_brachy application class. See the Main Page for full documentation.
Definition egs_brachy.h:91
vector< BaseSpectrumScorer * > spectrum_scorers
Definition egs_brachy.h:104
map< string, vector< EGS_Float > > extra_scoring_mass
Definition egs_brachy.h:182
EnergyScoringStats * escoring
Energy related scoring/stats.
Definition egs_brachy.h:103
vector< EGS_AffineTransform * > source_transforms
transforms to locations of all sources
Definition egs_brachy.h:147
map< string, vector< int > > extra_scoring_reg
Definition egs_brachy.h:180
map< int, EGS_I64 > steps_in_phantoms
Definition egs_brachy.h:195
EGS_I64 n_stuck
Definition egs_brachy.h:140
EB_TimingTree timing_blocks
Track CPU times of various functions.
Definition egs_brachy.h:192
map< string, EGS_ScoringArray * > extra_scoring_doses
Definition egs_brachy.h:183
string output_volcor_format
text or gzip
Definition egs_brachy.h:131
Publisher pevent_pub
Particle event publisher.
Definition egs_brachy.h:190
string output_egsdat_format
text or gzip
Definition egs_brachy.h:120
string run_mode_name
Definition egs_brachy.h:101
vector< string > output_volcor_phantoms
vector of phantom names to output volume correctino files for
Definition egs_brachy.h:130
string output_voxinfo_format
text or gzip
Definition egs_brachy.h:128
igzstream * gz_data_in
GZip file for outputing egsdat.
Definition egs_brachy.h:199
EGS_Float last_R
Definition egs_brachy.h:137
EGS_Float global_e_max_rr
max range rejection energy globally
Definition egs_brachy.h:170
EB_Application(int argc, char **argv)
egs_brachy constructor
Definition egs_brachy.h:270
int gcr_phantom_reg
region of phantom to use for getCurrentResult (default to 0)
Definition egs_brachy.h:178
EGS_Float effective_histories
Definition egs_brachy.h:370
bool source_i_do_rr
enable range rejection in sources
Definition egs_brachy.h:172
bool output_egsphant
true if user requests egsphant ouput
Definition egs_brachy.h:124
Latch latch_control
Definition egs_brachy.h:372
bool global_i_do_rr
enable range rejection outside of sources
Definition egs_brachy.h:169
EGS_Float source_pcut
pcut for source objects
Definition egs_brachy.h:165
EGS_Float bcse_factor
Definition egs_brachy.h:160
static string revision
the usercode revision number
Definition egs_brachy.h:201
EGS_Float global_pcut
pcut for source objects
Definition egs_brachy.h:167
bool output_3ddose_files
false if run mode is 'volume correction only'
Definition egs_brachy.h:122
map< string, EGS_ScoringArray * > extra_scoring_doses_edep
Definition egs_brachy.h:184
EB_Phantom * gcr_phantom
phantom object to use in getCurrentResult (defaults to 1st phantom)
Definition egs_brachy.h:177
static const EGS_Float DEFAULT_BCSE_FACTOR
Definition egs_brachy.h:161
map< string, string > media_muen_names
Definition egs_brachy.h:152
~EB_Application()
egs_brachy destructor
Definition egs_brachy.h:307
EGS_ASwitchedEnvelope * superpos_geom
an ASwitchedEnv cast of simulation geometry.
Definition egs_brachy.h:143
string output_dose_format
text or gzip
Definition egs_brachy.h:123
vector< EGS_Float > source_weights
Definition egs_brachy.h:111
map< string, vector< EGS_Float > > extra_scoring_vols
Definition egs_brachy.h:181
EGS_AffineTransform * base_transform
same as source_transforms[0]
Definition egs_brachy.h:148
EGS_BaseGeometry * source_envelope_geom
geometry that the sources are embedded in
Definition egs_brachy.h:142
vector< EB_Phantom * > phantom_geoms
pointers to all of the phantom objects
Definition egs_brachy.h:145
bool score_scat
true when scatter scoring is enabled
Definition egs_brachy.h:118
string output_egsphant_format
text or gzip
Definition egs_brachy.h:125
ogzstream * gz_data_out
GZip file for outputing egsdat.
Definition egs_brachy.h:198
int nbr_split
Number of times to split bremstrahlung phtons.
Definition egs_brachy.h:156
map< int, EGS_Interpolator * > media_muen
Map from medium index to muen interpolator for that medium.
Definition egs_brachy.h:151
ebvolcor::Results gen_vc_results
results from general volume correction
Definition egs_brachy.h:187
EGS_AffineTransform * base_transform_inv
same as source_transforms[0].inverse()
Definition egs_brachy.h:149
map< int, EGS_I64 > steps_in_sources
Definition egs_brachy.h:194
bool output_voxinfo
true if user requests voxel info file
Definition egs_brachy.h:127
map< int, EGS_I64 > steps_in_other
Definition egs_brachy.h:196
EGS_Vector last_position
Definition egs_brachy.h:136
PHSPControl * phsp
Definition egs_brachy.h:114
int nsources
total number of particle sources in current simulation
Definition egs_brachy.h:368
EGS_Float flu_cutoff
fluorescent photon cutoff energy
Definition egs_brachy.h:163
EGS_Float cur_R
Definition egs_brachy.h:138
@ RM_SUPERPOSITION
Superposition mode for intersource effects.
Definition egs_brachy.h:95
@ RM_NORMAL
Standard running mode.
Definition egs_brachy.h:94
GeomInfo ginfo
meta data about the geometries
Definition egs_brachy.h:175
ebvolcor::Results source_vc_results
results from source volume correctio box phantom
Definition egs_brachy.h:186
EGS_Float source_ecut
ecut for source objects
Definition egs_brachy.h:164
bool score_edep
true when energy deposition is enabled
Definition egs_brachy.h:117
EGS_Float source_e_max_rr
max range rejection energy for source objects
Definition egs_brachy.h:173
EGS_Float global_ecut
ecut for source objects
Definition egs_brachy.h:166
vector< EGS_Vector > p_init_locs
Definition egs_brachy.h:134
bool score_tlen
true when tracklength estimator is enabled
Definition egs_brachy.h:116
string getOutputVolcorFormat()
Definition egs_brachy.h:478
RunMode run_mode
Which run mode are we using (RM_NORMAL, RM_SUPERPOSITION or RM_VC_ONLY *‍/.
Definition egs_brachy.h:100
RecycleOpts * recycle_opts
Definition egs_brachy.h:107
bool single_generator
Definition egs_brachy.h:108
int record_n_init
if > 0 write initial pos of record_n_init particles to {input_file}.pinit
Definition egs_brachy.h:133
ebvolcor::FileResults file_vc_results
results from precomputed volume correction
Definition egs_brachy.h:188
A class to represent a single phantom for scoring dose in egs_brachy.
Definition phantom.h:71
EB_Application * app
Definition egs_brachy.h:543
string output_egsdat_format
text or gzip
Definition egs_brachy.h:545
int finishSimulation()
Uses 'watcher' jobs to determine if the simulation has finished.
a class to use for scoring information about total energy initialzed, escaping sources etc
a container for organizing meta data about the geometries
Definition ginfo.h:99
A class for handling latch bits relevant to egs_brachy. The Latch class listens for particle events a...
Definition latch.h:51
void subscribe(Subscriber *s, EB_Message message)
Definition pubsub.h:83
Volume correction routines for egs_brachy.
ginfo contains classes for organizing information about the geometries present in an egs_brachy simul...
handle particles latch bits for egs_brachy
Functions for loading muen data from a file.
Header file for phantom objects.
Definition of the PHSPControl object.
A simple pub/sub module to allow various egs_brachy classes to subscribe to particle events.
@ PARTICLE_ESCAPING_SOURCE
Definition pubsub.h:60
@ PARTICLE_ESCAPED_SOURCE
Definition pubsub.h:61
class definitions for recycling
Definition of spectrum scoring classes.
Struct used to collect and output results about a volume correction run.
Definition eb_volcor.h:212