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 #
31 ###############################################################################
32 */
33 
34 
40 #ifndef EGS_APPLICATION_
41 #define EGS_APPLICATION_
42 
43 #include "egs_base_geometry.h"
44 #include "egs_base_source.h"
45 #include "egs_simple_container.h"
46 #include "egs_interpolator.h"
47 
48 #include <string>
49 #include <iostream>
50 using namespace std;
51 
52 class EGS_Input;
53 class EGS_BaseSource;
55 class EGS_RunControl;
57 class EGS_AusgabObject;
58 class EGS_Interpolator;
59 //template <class T> class EGS_SimpleContainer;
60 
64 struct EGS_Particle {
65  int q;
66  int latch;
67  int ir;
68  EGS_Float E;
69  EGS_Float wt;
72 };
73 
268 
269 public:
270 
281  EGS_Application(int argc, char **argv);
282 
288  virtual ~EGS_Application();
289 
313  virtual int initSimulation();
314 
323  virtual void setSimulationChunk(EGS_I64 nstart, EGS_I64 nrun);
324 
346  virtual int runSimulation();
347 
356  virtual int finishSimulation();
357 
364  virtual void describeSimulation();
365 
380  virtual int simulateSingleShower();
381 
387  virtual void getCurrentResult(double &sum, double &sum2, double &norm,
388  double &count) {
389  sum = 0;
390  sum2 = 0;
391  norm = 1;
392  count = 0;
393  };
394 
400  virtual void analyzeResults() {};
401 
407  virtual void outputResults() {};
408 
422  virtual int combineResults();
423 
430  virtual int combinePartialResults();
431 
439  int howManyJobsDone();
440 
441 
457  virtual int outputData();
458 
473  virtual int readData();
474 
479  return input;
480  };
481 
483  const string &getAppName() const {
484  return app_name;
485  };
486 
488  const string &getEgsHome() const {
489  return egs_home;
490  };
491 
493  const string &getHenHouse() const {
494  return hen_house;
495  };
496 
498  const string &getOutputFile() const {
499  return output_file;
500  };
501 
510  const string &getFinalOutputFile() const {
511  return final_output_file;
512  };
513 
521  string constructIOFileName(const char *extension, bool with_run_dir) const;
522 
524  const string &getAppDir() const {
525  return app_dir;
526  };
527 
529  const string &getRunDir() const {
530  return run_dir;
531  };
532 
534  const string &getWorkDir() const {
535  return run_dir;
536  };
537 
539  enum AusgabCall {
540  BeforeTransport = 0,
541  EgsCut = 1,
542  PegsCut = 2,
543  UserDiscard = 3,
544  ExtraEnergy = 4,
546  AfterTransport = 5,
547  BeforeBrems = 6,
548  AfterBrems = 7,
549  BeforeMoller = 8,
550  AfterMoller = 9,
551  BeforeBhabha = 10,
552  AfterBhabha = 11,
553  BeforeAnnihFlight = 12,
554  AfterAnnihFlight = 13,
555  BeforeAnnihRest = 28,
556  AfterAnnihRest = 14,
557  BeforePair = 15,
558  AfterPair = 16,
559  BeforeCompton = 17,
560  AfterCompton = 18,
561  BeforePhoto = 19,
562  AfterPhoto = 20,
563  EnteringUphi = 21,
564  LeavingUphi = 22,
565  BeforeRayleigh = 23,
567  AfterRayleigh = 24,
568  FluorescentEvent = 25,
569  CosterKronigEvent = 26,
570  AugerEvent = 27,
571  BeforePhotoNuc = 29,
572  AfterPhotoNuc = 30,
573  BeforeEII = 31,
574  AfterEII = 32,
575  AfterSubPhoton = 33,
576  AfterSubElectron = 34,
577  UnknownCall = 35
578  };
579 
585  virtual void setAusgabCall(AusgabCall call, bool on_or_off) {
586  ausgab_flag[call] = on_or_off;
587  };
588 
596  int getNparallel() const {
597  return n_parallel;
598  };
599 
606  int getIparallel() const {
607  return i_parallel;
608  };
609 
616  int getFirstParallel() const {
617  return first_parallel;
618  };
619 
624  inline int howfar(int ireg, const EGS_Vector &x, const EGS_Vector &u,
625  EGS_Float &t, int *newmed) {
626 
627  geometry->resetErrorFlag();
628  EGS_Float twant = t;
629  int inew = geometry->howfar(ireg,x,u,t,newmed);
630  storeGeometryStep(ireg,inew,x,u,twant,t);
631  if (geometry->getLastError()) {
632  reportGeometryError();
633  }
634  return inew;
635 
636  //return geometry->howfar(ireg,x,u,t,newmed);
637  };
638 
643  inline EGS_Float hownear(int ireg,const EGS_Vector &x) {
644  return geometry->hownear(ireg,x);
645  };
646 
648  inline int getMedium(int ireg) {
649  return geometry->medium(ireg);
650  };
651 
659  bool isRealRegion(int ireg) {
660  return geometry->isRealRegion(ireg);
661  }
662  int isWhere(EGS_Vector &r) {
663  return geometry->isWhere(r);
664  }
665 
681  void getNumberRegions(const string &str, vector<int> &regs) {
682  geometry->getNumberRegions(str, regs);
683  }
684 
693  void getLabelRegions(const string &str, vector<int> &regs) {
694  geometry->getLabelRegions(str, regs);
695  }
696 
705  EGS_Float getMU() {
706  return source->getMu();
707  }
708 
715  int userScoring(int iarg, int ir=-1);
716 
722  virtual int ausgab(int) {
723  return 0;
724  };
725 
736  virtual void startNewParticle() { };
737 
756  virtual void enterNewRegion() { };
757 
766  virtual void fillRandomArray(int n, EGS_Float *rns);
767 
784  static EGS_Application *activeApplication();
785 
795  static void setActiveApplication(EGS_Application *);
796 
804  virtual EGS_I64 randomNumbersUsed() const;
805 
814  virtual void getElectronSteps(double &ch_steps, double &all_steps) const {
815  ch_steps = 0;
816  all_steps = 0;
817  };
818 
831  virtual int addState(istream &data);
832 
842  virtual void resetCounter();
843 
851  virtual void describeUserCode() const {};
852 
855  virtual void appInformation(const char *);
856 
859  virtual void appWarning(const char *);
860 
863  virtual void appFatal(const char *);
864 
867  void checkDeviceFull(FILE *);
868 
878  static bool getArgument(int &argc, char **argv,
879  const char *name1, const char *name2, string &arg);
880 
887  static void checkEnvironmentVar(int &argc, char **argv, const char *env,
888  const char *n1, const char *n2, string &var);
889 
890 protected:
891 
905  virtual int initGeometry();
906 
920  virtual int initSource();
921 
936  virtual int initCrossSections() {
937  return 0;
938  };
939 
947  virtual int initScoring() {
948  return 0;
949  };
950 
967  virtual int initRunControl();
968 
980  virtual int initRNG();
981 
995  virtual int initEGSnrcBackEnd() {
996  return 0;
997  };
998 
1006  void initAusgabObjects();
1007 
1009  void addAusgabObject(EGS_AusgabObject *o);
1010 
1019  virtual int startNewShower();
1020 
1028  virtual int finishShower() {
1029  return 0;
1030  };
1031 
1041  virtual int shower() {
1042  return 0;
1043  };
1044 
1045  virtual void finishRun() { };
1046 
1047  void storeGeometryStep(int ireg, int inew, const EGS_Vector &x,
1048  const EGS_Vector &u, EGS_Float twant, EGS_Float t);
1049 
1050  void reportGeometryError();
1051 
1057 
1058  bool ausgab_flag[UnknownCall];
1059 
1060  string app_name;
1061  string egs_home;
1062  string hen_house;
1063  string app_dir;
1064  string run_dir;
1065  string egs_config;
1066  string input_file;
1067  string output_file;
1069  string pegs_file;
1070  string abs_pegs_file;
1071 
1073  i_parallel,
1074  first_parallel;
1075  bool batch_run;
1076  bool simple_run;
1079 
1082  EGS_I64 current_case;
1083  EGS_I64 last_case;
1084 
1090  ostream *data_out;
1091 
1097  istream *data_in;
1098 
1103 
1106 
1109 
1110  EGS_GeometryHistory *ghistory;
1111 
1112 private:
1113 
1114  static int n_apps;
1115 
1116 public:
1117 
1119  int Np;
1120  //************************************************************
1121  // Utility functions for use with ausgab dose scoring objects
1122  //************************************************************
1123  EGS_Float getFluence() {
1124  return source->getFluence();
1125  };
1126  int getnRegions() {
1127  return geometry->regions();
1128  };
1129  int getnMedia() {
1130  return geometry->nMedia();
1131  };
1132  const char *getMediumName(int ind) {
1133  return geometry->getMediumName(ind);
1134  };
1135  virtual EGS_Float getMediumRho(int ind) {
1136  return -1.0;
1137  };
1138  virtual EGS_Float getEdep() {
1139  return 0.0;
1140  };
1141  virtual void setEdep(EGS_Float edep) {};
1142  virtual EGS_Float getEcut() {
1143  return 0.0;
1144  };
1145  virtual EGS_Float getPcut() {
1146  return 0.0;
1147  };
1148  virtual EGS_Float getRM() {
1149  return -1.0;
1150  };
1151  virtual void setRadiativeSplitting(const EGS_Float &nsplit) {};
1152 
1153  //************************************************************
1154  // Utility functions for use with ausgab fluence scoring objects
1155  //************************************************************
1156  virtual EGS_Float getTVSTEP() {
1157  return 0.0;
1158  };
1159 
1160  virtual EGS_Interpolator *getDEDX(const int &imed, const int &iq) {
1161  return 0;
1162  };
1163 
1164  string sourceType() {
1165  return source->getObjectType();
1166  }
1167 
1168  int sourceCharge() {
1169  return source->getCharge();
1170  }
1171 
1172  int sourceEmax() {
1173  return source->getEmax();
1174  }
1175 
1176  virtual void setLatch(const int &ip, const int &latch) {};
1177 
1178  virtual void incLatch(const int &ip, const int &increment) {};
1179 
1180  virtual int getNp() {
1181  return 0;
1182  };
1183 
1184  virtual int getNpOld() {
1185  return 0;
1186  };
1187 
1188  //************************************************************
1189  // Utility function for ausgab phase space scoring objects
1190  //************************************************************
1191  virtual void setLatch(int latch) {};
1192 
1193 };
1194 
1195 #define APP_MAIN(app_name) \
1196  int main(int argc, char **argv) { \
1197  app_name app(argc,argv); \
1198  int err = app.initSimulation(); \
1199  if( err ) return err; \
1200  err = app.runSimulation(); \
1201  if( err < 0 ) return err; \
1202  return app.finishSimulation(); \
1203  }
1204 
1205 #define APP_SIMPLE_MAIN(app_name) \
1206  int main(int argc, char **argv) { \
1207  app_name app(argc,argv); \
1208  app.run(); \
1209  app.reportResults(); \
1210  app.finish(); \
1211  return 0; \
1212  }
1213 
1214 #define APP_LIB(app_name) \
1215  extern "C" {\
1216  APP_EXPORT EGS_Application* createApplication(int argc, char **argv) {\
1217  return new app_name(argc,argv);\
1218  }\
1219  }
1220 
1221 
1222 #endif
string app_dir
The user code directory.
const string & getFinalOutputFile() const
Returns the base name of the final output file(s)
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90
EGS_Input * input
the input to this simulation.
virtual int initScoring()
Initialize the scoring of quantities of interest.
EGS_Float hownear(int ireg, const EGS_Vector &x)
Calculates nearest distance to a boundary in any direction.
EGS_I64 current_case
The current case as returned from the source.
virtual void getElectronSteps(double &ch_steps, double &all_steps) const
Get the number of electron steps taken.
EGS_Float E
particle energy in MeV
A simple run control object for advanced EGSnrc C++ applications.
virtual void describeUserCode() const
Describe the user code.
void getNumberRegions(const string &str, vector< int > &regs)
Gets numbers out of str and pushes them onto regs.
EGS_BaseSource class header file.
void getLabelRegions(const string &str, vector< int > &regs)
Gets the regions for the labels in str and pushes onto regs.
int n_parallel
Number of parallel jobs.
int q
particle charge
AusgabCall
Possible calls to the user scoring function ausgab().
virtual void outputResults()
Output the simulation results.
virtual int ausgab(int)
User scoring function.
A class representing 3D vectors.
Definition: egs_vector.h:56
A structure holding the information of one particle.
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.
int getIparallel() const
Returns the job number in a parallel run.
bool batch_run
Interactive or batch run.
EGS_Vector x
position
Base geometry class. Every geometry class must be derived from EGS_BaseGeometry.
virtual int finishShower()
Called just after the shower() function.
int getNparallel() const
Returns the number of parallel jobs executing.
bool simple_run
Use a simple run control object for parallel runs.
bool isRealRegion(int ireg)
Returns true if ireg is a real region, false otherwise.
Base random number generator class. All random number generators should be derived from this class...
Definition: egs_rndm.h:67
EGS_RandomGenerator * rndm
the random number generator
virtual int initEGSnrcBackEnd()
Initialize the EGSnrc backend.
const string & getRunDir() const
Returns the name of the working directory.
string run_dir
The working directory during the run.
virtual void getCurrentResult(double &sum, double &sum2, double &norm, double &count)
Report the current result.
const string & getAppName() const
Returns the application name.
int Np
The index of the top particle on the stack.
virtual int shower()
Simulate a single shower.
ostream * data_out
data output stream
bool is_pegsless
set to true if a pegsless run
virtual void analyzeResults()
Analyze the simulation results.
EGS_SimpleContainer template class.
A class for fast run-time interpolations.
EGS_RunControl * run
the run control object.
istream * data_in
data input stream
virtual int initCrossSections()
Initialize the EGSnrc cross sections and cross section/transport options.
int getMedium(int ireg)
Returns the medium index in region ireg using C-style indexing.
int ir
particle region index
string app_name
The application name.
string hen_house
The HEN_HOUSE directory.
string egs_config
The EGSnrc config.
const string & getEgsHome() const
Returns the EGS_HOME directory.
int app_index
the index of this application.
int getFirstParallel() const
Returns the first job number in a parallel run.
int latch
latch variable (useful as a flag on many occasions)
EGS_Vector u
direction
const string & getHenHouse() const
Returns the HEN_HOUSE directory.
virtual void startNewParticle()
Start the transport of a new particle.
EGS_Float getMU()
Returns the value of the mu synchronization parameter.
EGS_SimpleContainer< EGS_AusgabObject * > a_objects_list
The ausgab objects.
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
string output_file
The output file name (no extension)
EGS_I64 last_case
The last case simulated.
virtual void enterNewRegion()
Particle enters new region.
EGS_Input * getInput()
Returns a pointer to the EGS_Input object containing the user input to the application found in the i...
const string & getAppDir() const
Returns the absolute path to the user code directory.
string pegs_file
The pegs file name.
string input_file
The input file name.
EGS_Particle top_p
The top particle on the stack (i.e., the particle being transported)
virtual void setAusgabCall(AusgabCall call, bool on_or_off)
Turns on or off a call to the user scoring function ausgab.
EGS_BaseGeometry class header file.
string abs_pegs_file
The pegs file name including absolute path.
string egs_home
The EGS_HOME directory.
EGS_Float wt
statistical weight
const string & getWorkDir() const
Returns the name of the working directory.
EGS_Interpolator class header file.
EGS_BaseGeometry * geometry
the geometry of this simulation
EGS_BaseSource * source
the particle source
bool uniform_run
Use a uniform run control object for parallel runs.
EGS_SimpleContainer< EGS_AusgabObject * > * a_objects
The ausgab objects for the various ausgab calls.
Base class for advanced EGSnrc C++ applications.
const string & getOutputFile() const
Returns the base name of the output file(s)
Base source class. All particle sources must be derived from this class.
string final_output_file
The final output file name.