EGS Brachy
An egs++ user code for rapid brachytherapy calculations
Loading...
Searching...
No Matches
/Users/marc/Developer/clrp/EGSnrc-eb/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
43
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
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;
151
152 map<int, EGS_Interpolator *> media_muen;
153 map<string, string> media_muen_names;
154
155
158
161 EGS_Float bcse_factor;
162 static const EGS_Float DEFAULT_BCSE_FACTOR;
163
164 EGS_Float flu_cutoff;
165 EGS_Float source_ecut;
166 EGS_Float source_pcut;
167 EGS_Float global_ecut;
168 EGS_Float global_pcut;
169
171 EGS_Float global_e_max_rr;
172
174 EGS_Float source_e_max_rr;
175
177
180
181 map<string, vector<int> > extra_scoring_reg;
182 map<string, vector<EGS_Float> > extra_scoring_vols;
183 map<string, vector<EGS_Float> > extra_scoring_mass;
184 map<string, EGS_ScoringArray* > extra_scoring_doses;
185 map<string, EGS_ScoringArray* > extra_scoring_doses_edep;
186
190
192
194
195 map<int, EGS_I64> steps_in_sources;
196 map<int, EGS_I64> steps_in_phantoms;
197 map<int, EGS_I64> steps_in_other;
198
201
202 static string revision;
203
204
207 int createPhantoms();
208
209
211 int correctVolumes();
212
213 /* \brief check sources for overlap using a simple MC routine */
214 int checkSourceOverlaps(EGS_Input *);
215
217 void initTrackLengthScoring(EGS_Input *);
218
220 void initMuenData(EGS_Input *);
221
223 void initOutputFiles(EGS_Input *);
224
226 void initPHSPScoring(EGS_Input *);
227
229 void initEDepScoring(EGS_Input *);
230
232 void initScatScoring(EGS_Input *);
233
235 void clearAusgabCalls();
236
238 void enableAusgabCalls(int ncalls, AusgabCall calls[]);
239
240
242 void initGCRScoring(EGS_Input *);
243
248 void initAusgabCalls();
249
251 void initDoseScaling(EGS_Input *);
252
254 void initXCCScaling(EGS_Input *);
255
257 void initSpectrumScoring(EGS_Input *);
258
259 int initCrossSections();
260
261 void discardTopParticle(int idisc=1);
262
264
265 bool isStuck();
266
267public:
268
269
271 EB_Application(int argc, char **argv) :
272 EGS_AdvancedApplication(argc,argv),
274 escoring(),
275 recycle_opts(0),
276 active_source(0),
277 phsp(0),
278 score_edep(0),
279 score_scat(0),
281 record_n_init(0),
282 n_stuck(0),
283 superpos_geom(0),
285 do_brem_split(false),
286 nbr_split(1),
287 do_bcse(false),
288 bcse_factor(1),
289 flu_cutoff(0),
290 ginfo(),
292 gz_data_out(0),
293 gz_data_in(0),
294 nsources(0),
296
297 for (int i=-1; i < 2; i++) {
298 steps_in_sources[i] = 0;
299 steps_in_phantoms[i] = 0;
300 steps_in_other[i] = 0;
301 }
302
305 }
306
309
310 for (vector<EB_Phantom>::size_type pp=0; pp < phantom_geoms.size(); pp++) {
311 delete phantom_geoms[pp];
312 }
313 phantom_geoms.clear();
314
315 map<int, EGS_Interpolator *>::iterator mit = media_muen.begin();
316 for (; mit != media_muen.end(); mit++) {
317 delete mit->second;
318 }
319 media_muen.clear();
320
321
322 for (vector<EGS_AffineTransform *>::size_type st=0; st < source_transforms.size(); st++) {
323 delete source_transforms[st];
324 }
325 source_transforms.clear();
326
327 for (map<string, EGS_ScoringArray* >::iterator sa=extra_scoring_doses.begin(); sa != extra_scoring_doses.end(); sa++) {
328 delete sa->second;
329 }
330 extra_scoring_doses.clear();
331
332 for (map<string, EGS_ScoringArray* >::iterator sa=extra_scoring_doses_edep.begin(); sa != extra_scoring_doses_edep.end(); sa++) {
333 delete sa->second;
334 }
336
337 if (phsp) {
338 delete phsp;
339 }
340
341 if (escoring) {
342 delete escoring;
343 }
344
345 vector<BaseSpectrumScorer *>::iterator it = spectrum_scorers.begin();
346 for (; it != spectrum_scorers.end(); it++) {
347 delete *it;
348 }
349 spectrum_scorers.clear();
350
351 if (recycle_opts) {
352 delete recycle_opts;
353 }
354
355 if (base_transform_inv) {
356 delete base_transform_inv;
357 }
358
359 if (gz_data_in) {
360 delete gz_data_in;
361 }
362
363 if (gz_data_out) {
364 delete gz_data_out;
365 }
366
367 };
368
370
372
374
376 void describeUserCode() const;
377
382 void describeSimulation();
383
384 void printIncludedFiles();
385
387 int initSimulation();
388
390 int initRunControl();
391
393 int initRunMode();
394
396 int initScoring();
397
400
402 int initRussianRoulette(EGS_Input *);
403
405 int initBCSE(EGS_Input *);
406
408 int ausgab(int iarg);
409
416 int outputData();
417
419 int outputDataHelper(ostream *);
420
422 int egsApplicationOutputData(ostream *);
423
425 int egsAdvApplicationOutputData(ostream *);
426
428 int egsBrachyOutputData(ostream *);
429
433 int readData();
434
436 int readDataHelper(istream *);
437
439 int egsApplicationReadData(istream *);
440
442 int egsAdvApplicationReadData(istream *);
443
445 int egsBrachyReadData(istream *);
446
447 int combineResults();
448
453 void resetCounter();
454
459 int addState(istream &data);
460
461
462 /* \brief output any results to console */
463 void outputResults();
464
465
467 void getCurrentResult(double &sum, double &sum2, double &norm,
468 double &count);
469
471 EB_Phantom* getPhantomByName(string name);
472
474 virtual void startNewParticle();
475 virtual void enterNewRegion();
476
477 virtual int runSimulation();
478
481 }
482
483 int howManyJobsDone();
484
485protected:
486
487 void addRecycledParticlesToStack(EGS_Particle *p, bool new_hist=false);
488 void copyParticleToSourceLoc(EGS_Particle *p, int source, bool kill_orig, bool rotate, EGS_Float new_wt);
489
490 void doPhotonSplitting(int);
491
492
493 /* \brief initialize a single shower history.
494 *
495 * Particle is initiated from source and then transformed to location
496 * of next source */
497 int simulateSingleShower();
498 int startNewShower();
499
504 int initGeometry();
505
506
507 /* \brief override default initSource to allow us to get source position weighting */
508 int initSource();
509
510
514 int initSourceTransforms();
515
516 /* \brief take an input containging multiple `transformation` input blocks
517 * and return a vector of pointers to the generated transforms.
518 */
519 vector<EGS_AffineTransform *> createTransforms(EGS_Input *input);
520
521};
522
523class EB_UniformRunControl : public EGS_RunControl {
524
525public:
526
527 EB_UniformRunControl(EB_Application *app, string egsdat_format);
529
530 void describeRCO();
531
532 int startSimulation();
533
542 int finishSimulation();
543
544protected:
545
547
549
550 int milliseconds; // time interval for checking
551 // if all jobs finished (default 1000 ms)
552
553 int check_intervals;// Number of intervals to check
554 // if all jobs done (default 5)
555
556 int njob;
557 int npar;
558 int ipar;
560 bool check_egsdat;// If true, and a 'watcher' job, produce intermediate results
561 bool watcher_job; // If true, job is a 'watcher'
562};
563
564#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:183
int initRunControl()
egs_brachy specific run control initialization
void initPHSPScoring(EGS_Input *)
set up phsp scoring
int checkSourceOverlaps(EGS_Input *)
EnergyScoringStats * escoring
Energy related scoring/stats.
Definition egs_brachy.h:103
int initSimulation()
set the run mode and then call EGS_AdvancedApplication::initSimulation
void initEDepScoring(EGS_Input *)
energy deposition scoring initialization
int outputData()
Output intermediate results. The egs_brachy version outputs the standard egs++ data along with egs_br...
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:181
void initDoseScaling(EGS_Input *)
Initialize dose scaling factor if requested.
map< int, EGS_I64 > steps_in_phantoms
Definition egs_brachy.h:196
EGS_I64 n_stuck
Definition egs_brachy.h:140
int outputDataHelper(ostream *)
helper function for outputData
EB_TimingTree timing_blocks
Track CPU times of various functions.
Definition egs_brachy.h:193
int initRussianRoulette(EGS_Input *)
Initialize Russian roulette variance reduction if requested.
map< string, EGS_ScoringArray * > extra_scoring_doses
Definition egs_brachy.h:184
string output_volcor_format
text or gzip
Definition egs_brachy.h:131
int createPhantoms()
set up Phantom objects for any geometries that user has requested scoring for
Publisher pevent_pub
Particle event publisher.
Definition egs_brachy.h:191
virtual void startNewParticle()
Set source ecut/pcut if different from global ecut/pcut.
void describeUserCode() const
Print information about the egs_brachy user code.
int initRunMode()
Get run mode from the input file.
void describeSimulation()
Describe the simulation.
void enableAusgabCalls(int ncalls, AusgabCall calls[])
enable an array of ausgab calls
void initAusgabCalls()
setup any required ausgab calls
void initMuenData(EGS_Input *)
load muen data for requested media
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:200
EGS_Float last_R
Definition egs_brachy.h:137
EGS_Float global_e_max_rr
max range rejection energy globally
Definition egs_brachy.h:171
EB_Application(int argc, char **argv)
egs_brachy constructor
Definition egs_brachy.h:271
int egsAdvApplicationOutputData(ostream *)
helper function for outputData
void discardTopParticle(int idisc=1)
int gcr_phantom_reg
region of phantom to use for getCurrentResult (default to 0)
Definition egs_brachy.h:179
EGS_Float effective_histories
Definition egs_brachy.h:371
int initBCSE(EGS_Input *)
Initialize BCSE variance reduction if requested.
bool source_i_do_rr
enable range rejection in sources
Definition egs_brachy.h:173
bool output_egsphant
true if user requests egsphant ouput
Definition egs_brachy.h:124
Latch latch_control
Definition egs_brachy.h:373
void printIncludedFiles()
bool global_i_do_rr
enable range rejection outside of sources
Definition egs_brachy.h:170
EGS_Float source_pcut
pcut for source objects
Definition egs_brachy.h:166
EGS_Float bcse_factor
Definition egs_brachy.h:161
int egsBrachyReadData(istream *)
helper function for outputData
void initTrackLengthScoring(EGS_Input *)
track length scoring initialization
static string revision
the usercode revision number
Definition egs_brachy.h:202
EGS_Float global_pcut
pcut for source objects
Definition egs_brachy.h:168
void getCurrentResult(double &sum, double &sum2, double &norm, double &count)
Reports the current results for this batch of the simulation.
int correctVolumes()
run the volume correction routines
bool output_3ddose_files
false if run mode is 'volume correction only'
Definition egs_brachy.h:122
void initSpectrumScoring(EGS_Input *)
Initialize all spectrum scoring objects.
map< string, EGS_ScoringArray * > extra_scoring_doses_edep
Definition egs_brachy.h:185
EB_Phantom * gcr_phantom
phantom object to use in getCurrentResult (defaults to 1st phantom)
Definition egs_brachy.h:178
EB_Phantom * getPhantomByName(string name)
takes a phantom geometry name and returns the EB_Phantom object
static const EGS_Float DEFAULT_BCSE_FACTOR
Definition egs_brachy.h:162
map< string, string > media_muen_names
Definition egs_brachy.h:153
~EB_Application()
egs_brachy destructor
Definition egs_brachy.h:308
int readDataHelper(istream *)
helper function for outputData
void initScatScoring(EGS_Input *)
energy deposition scoring initialization
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
virtual void enterNewRegion()
map< string, vector< EGS_Float > > extra_scoring_vols
Definition egs_brachy.h:182
void resetCounter()
Reset the application to a 'pristine' state. Adapted from egs_application.cpp to allow combining in t...
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
void calcEffectiveHistories()
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
int egsBrachyOutputData(ostream *)
helper function for outputData
ogzstream * gz_data_out
GZip file for outputing egsdat.
Definition egs_brachy.h:199
int initCrossSections()
int nbr_split
Number of times to split bremstrahlung phtons.
Definition egs_brachy.h:157
map< int, EGS_Interpolator * > media_muen
Map from medium index to muen interpolator for that medium.
Definition egs_brachy.h:152
ebvolcor::Results gen_vc_results
results from general volume correction
Definition egs_brachy.h:188
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:195
bool output_voxinfo
true if user requests voxel info file
Definition egs_brachy.h:127
bool applied_source_coord_transform
true if a source coordinate transform was composed in
Definition egs_brachy.h:150
map< int, EGS_I64 > steps_in_other
Definition egs_brachy.h:197
EGS_Vector last_position
Definition egs_brachy.h:136
PHSPControl * phsp
Definition egs_brachy.h:114
int ausgab(int iarg)
user scoring of dose, spectra etc
int readData()
Read data required for restarting simulations.
int egsApplicationReadData(istream *)
helper function for outputData
int nsources
total number of particle sources in current simulation
Definition egs_brachy.h:369
EGS_Float flu_cutoff
fluorescent photon cutoff energy
Definition egs_brachy.h:164
EGS_Float cur_R
Definition egs_brachy.h:138
@ RM_VC_ONLY
Run volume correction routines then quit.
Definition egs_brachy.h:96
@ RM_SUPERPOSITION
Superposition mode for intersource effects.
Definition egs_brachy.h:95
@ RM_NORMAL
Standard running mode.
Definition egs_brachy.h:94
int egsAdvApplicationReadData(istream *)
helper function for outputData
int egsApplicationOutputData(ostream *)
helper function for outputData
GeomInfo ginfo
meta data about the geometries
Definition egs_brachy.h:176
void clearAusgabCalls()
disable all ausgab calls
ebvolcor::Results source_vc_results
results from source volume correctio box phantom
Definition egs_brachy.h:187
void initXCCScaling(EGS_Input *)
Initialize cross section scaling if requested.
EGS_Float source_ecut
ecut for source objects
Definition egs_brachy.h:165
bool score_edep
true when energy deposition is enabled
Definition egs_brachy.h:117
void initGCRScoring(EGS_Input *)
setup which phantom/region will be used for getCurrentResult
EGS_Float source_e_max_rr
max range rejection energy for source objects
Definition egs_brachy.h:174
int initScoring()
initialze all scoring and variance reduction parameters
int addState(istream &data)
Add data from a parallel job. Add standard egs++ data as well as egs_brachy specific scoring informat...
EGS_Float global_ecut
ecut for source objects
Definition egs_brachy.h:167
void initOutputFiles(EGS_Input *)
set up whether to output extra info files
int initVarianceReduction()
initialze all variance reduction parameters
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:479
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:189
virtual int runSimulation()
A class to represent a single phantom for scoring dose in egs_brachy.
Definition phantom.h:71
EB_UniformRunControl(EB_Application *app, string egsdat_format)
EB_Application * app
Definition egs_brachy.h:546
string output_egsdat_format
text or gzip
Definition egs_brachy.h:548
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
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