EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_application.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ application headers
5 # Copyright (C) 2015 National Research Council Canada
6 #
7 # This file is part of EGSnrc.
8 #
9 # EGSnrc is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU Affero General Public License as published by the
11 # Free Software Foundation, either version 3 of the License, or (at your
12 # option) any later version.
13 #
14 # EGSnrc is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
17 # more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ###############################################################################
23 #
24 # Author: Iwan Kawrakow, 2005
25 #
26 # Contributors: Frederic Tessier
27 # Ernesto Mainegra-Hing
28 # Blake Walters
29 # Reid Townson
30 # Alexandre Demelo
31 #
32 ###############################################################################
33 */
34 
35 
41 #ifndef EGS_APPLICATION_
42 #define EGS_APPLICATION_
43 
44 #include "egs_base_geometry.h"
45 #include "egs_base_source.h"
46 #include "egs_simple_container.h"
47 #include "egs_interpolator.h"
48 
49 #include <string>
50 #include <iostream>
51 using namespace std;
52 
53 class EGS_Input;
54 class EGS_BaseSource;
56 class EGS_RunControl;
58 class EGS_AusgabObject;
59 class EGS_Interpolator;
60 //template <class T> class EGS_SimpleContainer;
61 
65 struct EGS_Particle {
66  int q;
67  int latch;
68  int ir;
69  EGS_Float E;
70  EGS_Float wt;
73 };
74 
269 
270 public:
271 
282  EGS_Application(int argc, char **argv);
283 
289  virtual ~EGS_Application();
290 
314  virtual int initSimulation();
315 
325  virtual void setSimulationChunk(EGS_I64 nstart, EGS_I64 nrun, int npar, int nchunk);
326 
348  virtual int runSimulation();
349 
358  virtual int finishSimulation();
359 
366  virtual void describeSimulation();
367 
382  virtual int simulateSingleShower();
383 
389  virtual void getCurrentResult(double &sum, double &sum2, double &norm,
390  double &count) {
391  sum = 0;
392  sum2 = 0;
393  norm = 1;
394  count = 0;
395  };
396 
402  virtual void analyzeResults() {};
403 
409  virtual void outputResults() {};
410 
424  virtual int combineResults();
425 
432  virtual int combinePartialResults();
433 
441  int howManyJobsDone();
442 
443 
459  virtual int outputData();
460 
475  virtual int readData();
476 
481  return input;
482  };
483 
485  const string &getAppName() const {
486  return app_name;
487  };
488 
490  const string &getEgsHome() const {
491  return egs_home;
492  };
493 
495  const string &getHenHouse() const {
496  return hen_house;
497  };
498 
500  const string &getOutputFile() const {
501  return output_file;
502  };
503 
512  const string &getFinalOutputFile() const {
513  return final_output_file;
514  };
515 
523  string constructIOFileName(const char *extension, bool with_run_dir) const;
524 
526  const string &getAppDir() const {
527  return app_dir;
528  };
529 
531  const string &getRunDir() const {
532  return run_dir;
533  };
534 
536  const string &getWorkDir() const {
537  return run_dir;
538  };
539 
541  enum AusgabCall {
542  BeforeTransport = 0,
543  EgsCut = 1,
544  PegsCut = 2,
545  UserDiscard = 3,
546  ExtraEnergy = 4,
548  AfterTransport = 5,
549  BeforeBrems = 6,
550  AfterBrems = 7,
551  BeforeMoller = 8,
552  AfterMoller = 9,
553  BeforeBhabha = 10,
554  AfterBhabha = 11,
555  BeforeAnnihFlight = 12,
556  AfterAnnihFlight = 13,
557  BeforeAnnihRest = 28,
558  AfterAnnihRest = 14,
559  BeforePair = 15,
560  AfterPair = 16,
561  BeforeCompton = 17,
562  AfterCompton = 18,
563  BeforePhoto = 19,
564  AfterPhoto = 20,
565  EnteringUphi = 21,
566  LeavingUphi = 22,
568  BeforeRayleigh = 23,
569  AfterRayleigh = 24,
570  FluorescentEvent = 25,
571  CosterKronigEvent = 26,
572  AugerEvent = 27,
573  BeforePhotoNuc = 29,
574  AfterPhotoNuc = 30,
575  BeforeEII = 31,
576  AfterEII = 32,
577  AfterSubPhoton = 33,
578  AfterSubElectron = 34,
579  UnknownCall = 35
580  };
581 
587  virtual void setAusgabCall(AusgabCall call, bool on_or_off) {
588  ausgab_flag[call] = on_or_off;
589  };
590 
598  int getNparallel() const {
599  return n_parallel;
600  };
601 
608  int getIparallel() const {
609  return i_parallel;
610  };
611 
618  int getFirstParallel() const {
619  return first_parallel;
620  };
621 
626  inline int howfar(int ireg, const EGS_Vector &x, const EGS_Vector &u,
627  EGS_Float &t, int *newmed) {
628 
629  geometry->resetErrorFlag();
630  EGS_Float twant = t;
631  int inew = geometry->howfar(ireg,x,u,t,newmed);
632  storeGeometryStep(ireg,inew,x,u,twant,t);
633  if (geometry->getLastError()) {
634  reportGeometryError();
635  }
636  return inew;
637 
638  //return geometry->howfar(ireg,x,u,t,newmed);
639  };
640 
645  inline EGS_Float hownear(int ireg,const EGS_Vector &x) {
646  return geometry->hownear(ireg,x);
647  };
648 
650  inline int getMedium(int ireg) {
651  return geometry->medium(ireg);
652  };
653 
661  bool isRealRegion(int ireg) {
662  return geometry->isRealRegion(ireg);
663  }
664  int isWhere(EGS_Vector &r) {
665  return geometry->isWhere(r);
666  }
667 
683  void getNumberRegions(const string &str, vector<int> &regs) {
684  geometry->getNumberRegions(str, regs);
685  }
686 
695  void getLabelRegions(const string &str, vector<int> &regs) {
696  geometry->getLabelRegions(str, regs);
697  }
698 
707  EGS_Float getTimeIndex() {
708  return source->getTimeIndex();
709  }
710 
714  void setTimeIndex(EGS_Float temp_time) {
715  source->setTimeIndex(temp_time);
716  }
717 
724  int userScoring(int iarg, int ir=-1);
725 
731  virtual int ausgab(int) {
732  return 0;
733  };
734 
745  virtual void startNewParticle() { };
746 
765  virtual void enterNewRegion() { };
766 
775  virtual void fillRandomArray(int n, EGS_Float *rns);
776 
793  static EGS_Application *activeApplication();
794 
804  static void setActiveApplication(EGS_Application *);
805 
813  virtual EGS_I64 randomNumbersUsed() const;
814 
823  virtual void getElectronSteps(double &ch_steps, double &all_steps) const {
824  ch_steps = 0;
825  all_steps = 0;
826  };
827 
840  virtual int addState(istream &data);
841 
851  virtual void resetCounter();
852 
860  virtual void describeUserCode() const {};
861 
864  virtual void appInformation(const char *);
865 
868  virtual void appWarning(const char *);
869 
872  virtual void appFatal(const char *);
873 
876  void checkDeviceFull(FILE *);
877 
887  static bool getArgument(int &argc, char **argv,
888  const char *name1, const char *name2, string &arg);
889 
896  static void checkEnvironmentVar(int &argc, char **argv, const char *env,
897  const char *n1, const char *n2, string &var);
898 
899 protected:
900 
914  virtual int initGeometry();
915 
929  virtual int initSource();
930 
945  virtual int initCrossSections() {
946  return 0;
947  };
948 
956  virtual int initScoring() {
957  return 0;
958  };
959 
976  virtual int initRunControl();
977 
989  virtual int initRNG();
990 
1004  virtual int initEGSnrcBackEnd() {
1005  return 0;
1006  };
1007 
1015  void initAusgabObjects();
1016 
1018  void addAusgabObject(EGS_AusgabObject *o);
1019 
1028  virtual int startNewShower();
1029 
1037  virtual int finishShower() {
1038  return 0;
1039  };
1040 
1050  virtual int shower() {
1051  return 0;
1052  };
1053 
1054  virtual void finishRun() { };
1055 
1056  void storeGeometryStep(int ireg, int inew, const EGS_Vector &x,
1057  const EGS_Vector &u, EGS_Float twant, EGS_Float t);
1058 
1059  void reportGeometryError();
1060 
1066 
1067  bool ausgab_flag[UnknownCall];
1068 
1069  string app_name;
1070  string egs_home;
1071  string hen_house;
1072  string app_dir;
1073  string run_dir;
1074  string egs_config;
1075  string input_file;
1076  string output_file;
1078  string pegs_file;
1079  string abs_pegs_file;
1080 
1084  bool batch_run;
1085  bool simple_run;
1088 
1091  EGS_I64 current_case;
1092  EGS_I64 last_case;
1093 
1099  ostream *data_out;
1100 
1106  istream *data_in;
1107 
1112 
1115 
1118 
1119  EGS_GeometryHistory *ghistory;
1120 
1121 private:
1122 
1123  static int n_apps;
1124 
1125 public:
1126 
1128  int Np;
1129  //************************************************************
1130  // Utility functions for use with ausgab dose scoring objects
1131  //************************************************************
1132  EGS_Float getFluence() {
1133  return source->getFluence();
1134  };
1135  int getnRegions() {
1136  return geometry->regions();
1137  };
1138  int getnMedia() {
1139  return geometry->nMedia();
1140  };
1141  const char *getMediumName(int ind) {
1142  return geometry->getMediumName(ind);
1143  };
1144  virtual EGS_Float getMediumRho(int ind) {
1145  return -1.0;
1146  };
1147  virtual EGS_Float getEdep() {
1148  return 0.0;
1149  };
1150  virtual void setEdep(EGS_Float edep) {};
1151  virtual EGS_Float getEcut() {
1152  return 0.0;
1153  };
1154  virtual EGS_Float getPcut() {
1155  return 0.0;
1156  };
1157  virtual EGS_Float getRM() {
1158  return -1.0;
1159  };
1160 
1161  //************************************************
1162  // For use with ausgab radiative splitting objects
1163  //************************************************
1164  virtual void setRadiativeSplitting(const EGS_Float &nsplit) {};
1165  virtual void setRussianRoulette(const EGS_Float &iSwitchRR) {};
1166  virtual void splitTopParticleIsotropically(const EGS_Float &fsplit) {}
1167 
1168  //************************************************************
1169  // Utility functions for use with ausgab fluence scoring objects
1170  //************************************************************
1171  virtual EGS_Float getTVSTEP() {
1172  return 0.0;
1173  };
1174 
1175  virtual EGS_Interpolator *getDEDX(const int &imed, const int &iq) {
1176  return 0;
1177  };
1178 
1179  string sourceType() {
1180  return source->getObjectType();
1181  }
1182 
1183  int sourceCharge() {
1184  return source->getCharge();
1185  }
1186 
1187  int sourceEmax() {
1188  return source->getEmax();
1189  }
1190 
1191  virtual void setLatch(const int &ip, const int &latch) {};
1192 
1193  virtual void incLatch(const int &ip, const int &increment) {};
1194 
1195  virtual int getNp() {
1196  return 0;
1197  };
1198 
1199  virtual int getNpOld() {
1200  return 0;
1201  };
1202 
1203  //************************************************************
1204  // Utility function for ausgab phase space scoring objects
1205  //************************************************************
1206  virtual void setLatch(int latch) {};
1207 
1208  bool containsDynamic() {
1209  bool hasDynamic = false;
1210  geometry->containsDynamic(hasDynamic);
1211  if (!hasDynamic) {
1212  source->containsDynamic(hasDynamic);
1213  }
1214  return hasDynamic;
1215  }
1216 };
1217 
1218 #define APP_MAIN(app_name) \
1219  int main(int argc, char **argv) { \
1220  app_name app(argc,argv); \
1221  int err = app.initSimulation(); \
1222  if( err ) return err; \
1223  err = app.runSimulation(); \
1224  if( err < 0 ) return err; \
1225  return app.finishSimulation(); \
1226  }
1227 
1228 #define APP_SIMPLE_MAIN(app_name) \
1229  int main(int argc, char **argv) { \
1230  app_name app(argc,argv); \
1231  app.run(); \
1232  app.reportResults(); \
1233  app.finish(); \
1234  return 0; \
1235  }
1236 
1237 #define APP_LIB(app_name) \
1238  extern "C" {\
1239  APP_EXPORT EGS_Application* createApplication(int argc, char **argv) {\
1240  return new app_name(argc,argv);\
1241  }\
1242  }
1243 
1244 
1245 #endif
Base class for advanced EGSnrc C++ applications.
string app_dir
The user code directory.
bool uniform_run
Use a uniform run control object for parallel runs.
string run_dir
The working directory during the run.
int Np
The index of the top particle on the stack.
bool simple_run
Use a simple run control object for parallel runs.
EGS_RandomGenerator * rndm
the random number generator
bool is_pegsless
set to true if a pegsless run
int n_parallel
Number of parallel jobs.
void getLabelRegions(const string &str, vector< int > &regs)
Gets the regions for the labels in str and pushes onto regs.
EGS_SimpleContainer< EGS_AusgabObject * > a_objects_list
The ausgab objects.
int getMedium(int ireg)
Returns the medium index in region ireg using C-style indexing.
string input_file
The input file name.
string output_file
The output file name (no extension)
virtual void analyzeResults()
Analyze the simulation results.
virtual void setAusgabCall(AusgabCall call, bool on_or_off)
Turns on or off a call to the user scoring function ausgab.
EGS_I64 last_case
The last case simulated.
int howfar(int ireg, const EGS_Vector &x, const EGS_Vector &u, EGS_Float &t, int *newmed)
Calculates distance to a boundary along the current direction.
virtual void startNewParticle()
Start the transport of a new particle.
string pegs_file
The pegs file name.
int getNparallel() const
Returns the number of parallel jobs executing.
virtual int initScoring()
Initialize the scoring of quantities of interest.
EGS_Input * input
the input to this simulation.
EGS_Input * getInput()
Returns a pointer to the EGS_Input object containing the user input to the application found in the i...
virtual int ausgab(int)
User scoring function.
string app_name
The application name.
virtual void outputResults()
Output the simulation results.
const string & getRunDir() const
Returns the name of the working directory.
virtual void getElectronSteps(double &ch_steps, double &all_steps) const
Get the number of electron steps taken.
const string & getEgsHome() const
Returns the EGS_HOME directory.
const string & getAppName() const
Returns the application name.
bool isRealRegion(int ireg)
Returns true if ireg is a real region, false otherwise.
virtual void getCurrentResult(double &sum, double &sum2, double &norm, double &count)
Report the current result.
string final_output_file
The final output file name.
EGS_BaseGeometry * geometry
the geometry of this simulation
AusgabCall
Possible calls to the user scoring function ausgab().
int getFirstParallel() const
Returns the first job number in a parallel run.
EGS_Float getTimeIndex()
Returns the value of the time synchronization parameter.
EGS_I64 current_case
The current case as returned from the source.
const string & getOutputFile() const
Returns the base name of the output file(s)
bool batch_run
Interactive or batch run.
int i_parallel
Job index in parallel runs.
istream * data_in
data input stream
string egs_home
The EGS_HOME directory.
int first_parallel
first parallel job number
int app_index
the index of this application.
virtual int initCrossSections()
Initialize the EGSnrc cross sections and cross section/transport options.
const string & getFinalOutputFile() const
Returns the base name of the final output file(s)
virtual void describeUserCode() const
Describe the user code.
virtual void enterNewRegion()
Particle enters new region.
string abs_pegs_file
The pegs file name including absolute path.
virtual int finishShower()
Called just after the shower() function.
const string & getAppDir() const
Returns the absolute path to the user code directory.
ostream * data_out
data output stream
EGS_Float hownear(int ireg, const EGS_Vector &x)
Calculates nearest distance to a boundary in any direction.
EGS_RunControl * run
the run control object.
EGS_SimpleContainer< EGS_AusgabObject * > * a_objects
The ausgab objects for the various ausgab calls.
const string & getWorkDir() const
Returns the name of the working directory.
void getNumberRegions(const string &str, vector< int > &regs)
Gets numbers out of str and pushes them onto regs.
string egs_config
The EGSnrc config.
string hen_house
The HEN_HOUSE directory.
virtual int initEGSnrcBackEnd()
Initialize the EGSnrc backend.
void setTimeIndex(EGS_Float temp_time)
const string & getHenHouse() const
Returns the HEN_HOUSE directory.
int getIparallel() const
Returns the job number in a parallel run.
EGS_BaseSource * source
the particle source
EGS_Particle top_p
The top particle on the stack (i.e., the particle being transported)
virtual int shower()
Simulate a single shower.
Base geometry class. Every geometry class must be derived from EGS_BaseGeometry.
static int nMedia()
Get the number of media registered so far by all geometries.
int regions() const
Returns the number of local regions in this geometry.
static const char * getMediumName(int ind)
Get the name of medium with index ind.
Base source class. All particle sources must be derived from this class.
virtual int getCharge() const
Get the charge of the source.
virtual EGS_Float getEmax() const =0
Return the maximum energy of this source.
virtual EGS_Float getFluence() const =0
Return the fluence this source has emitted so far.
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
A class for fast run-time interpolations.
const string & getObjectType() const
Get the object type.
Base random number generator class. All random number generators should be derived from this class.
Definition: egs_rndm.h:67
A simple run control object for advanced EGSnrc C++ applications.
A class representing 3D vectors.
Definition: egs_vector.h:57
EGS_BaseGeometry class header file.
EGS_BaseSource class header file.
EGS_Interpolator class header file.
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90
EGS_SimpleContainer template class.
A structure holding the information of one particle.
EGS_Vector x
position
EGS_Float E
particle energy in MeV
EGS_Vector u
direction
int ir
particle region index
int latch
latch variable (useful as a flag on many occasions)
EGS_Float wt
statistical weight
int q
particle charge