56 #include <sys/types.h>
68 #define EGS_ACCESS ::_access
72 #define EGS_ACCESS ::access
73 #include <sys/statvfs.h>
76 #define MAXIMUM_JOB_NUMBER 8192 // GPSC1: 256 nodes with 16 cores (32 threads)
79 static char __egs_app_msg1[] =
"EGS_Application::EGS_Application(int,char**):";
80 static char __egs_app_msg2[] =
"EGS_Application::initSimulation():";
81 static char __egs_app_msg3[] =
"EGS_Application::runSimulation():";
83 static EGS_LOCAL
bool __egs_find_pegsfile(
const vector<string> &paths,
84 const string &pegs_file,
string &abs_pegs_file) {
85 string pfile = pegs_file;
86 if (pfile.find(
".pegs4dat") == string::npos) {
89 for (
unsigned int j=0; j<paths.size(); j++) {
90 string this_pegs =!paths[j].empty() ?
egsJoinPath(paths[j],pfile) : pfile;
91 if (!EGS_ACCESS(this_pegs.c_str(),R_OK)) {
92 abs_pegs_file = this_pegs;
101 int EGS_Application::n_apps = 0;
104 return active_egs_application;
108 active_egs_application = a;
113 int early_return = 0;
114 for (
int j=0; j<a_objects[iarg].size(); ++j) {
117 res = a_objects[iarg][j]->processEvent((
AusgabCall)iarg, ir);
120 res = a_objects[iarg][j]->processEvent((
AusgabCall)iarg);
129 if (early_return > 0) {
142 const char *n1,
const char *n2,
const char *env,
string &var) {
143 char *aux = getenv(env);
150 getArgument(argc,argv,n1,n2,var);
151 if (!var.size())
egsFatal(
"%s\n the environment variable %s is not set"
152 " and it was not passed as argument\n",__egs_app_msg1,env);
153 int n = var.size()-1;
154 if (var[n] !=
'/' && var[n] != fs) {
167 EGS_Float Twant, EGS_Float T) : x(X), u(U), twant(Twant), t(T),
168 ireg(Ireg), inew(Inew) {};
170 egsWarning(
"old region=%d, position=(%g,%g,%g), "
171 "direction=(%g,%g,%g) intended step=%g, new region=%d, "
172 "step=%g\n",ireg,x.x,x.y,x.z,u.x,u.y,u.z,twant,inew,t);
181 void addStep(
int ireg,
int inew,
const EGS_Vector &x,
182 const EGS_Vector &u, EGS_Float twant, EGS_Float t) {
183 steps[ns++] = Step(ireg,inew,x,u,twant,t);
189 void reportHistory()
const {
190 int nhave = wrap ? nsize : ns;
191 egsWarning(
"\n************** Last %d geometry steps:\n",nhave);
193 for (
int j=ns; j<nsize; j++) {
197 for (
int j=0; j<ns; j++) {
208 void EGS_Application::reportGeometryError() {
209 ghistory->reportHistory();
212 run->geomErrorCount++;
213 if (run->geomErrorCount > run->geomErrorMax) {
214 egsFatal(
"\n\n******** Encountered %d geometry errors (maximum allowed is %d). \n \
215 You can change this limit with the 'geometry error limit' run control input key. \n \
216 Quitting now.\n", run->geomErrorCount, run->geomErrorMax);
220 void EGS_Application::storeGeometryStep(
int ireg,
int inew,
222 ghistory->addStep(ireg,inew,x,u,twant,t);
226 source(0), rndm(0), run(0), simple_run(false), uniform_run(false), current_case(0),
227 last_case(0), data_out(0), data_in(0), a_objects(0),
232 if (!active_egs_application) {
233 active_egs_application =
this;
253 size_t exePos =
app_name.rfind(
".exe");
254 if (exePos != string::npos) {
259 "name from %s or command line arguments\n",__egs_app_msg1,argv[0]);
284 if (ext != std::string::npos) {
300 if (!
getArgument(argc,argv,
"-p",
"--pegs-file",pegs_file)) {
308 if (pegs_file.size() > 0) {
310 vector<string> pegs_paths;
311 pegs_paths.push_back(
"");
314 if (!__egs_find_pegsfile(pegs_paths,pegs_file,abs_pegs_file))
315 egsFatal(
"%s\n the pegs file %s does not exist or is not "
316 "readable\n",__egs_app_msg1,pegs_file.c_str());
325 if (ifile.find(
".egsinp") == string::npos) {
328 if (EGS_ACCESS(ifile.c_str(),R_OK))
329 egsFatal(
"%s\n the input file %s does not exist or is not "
330 "readable\n",__egs_app_msg1,ifile.c_str());
346 for (
int j=1; j<argc; j++) {
347 string tmp = argv[j];
348 if (tmp ==
"-b" || tmp ==
"--batch") {
358 string npar, ipar, ifirst;
361 bool have_np =
getArgument(argc,argv,
"-P",
"--parallel",npar);
362 bool have_ip =
getArgument(argc,argv,
"-j",
"--job",ipar);
363 bool have_first =
getArgument(argc,argv,
"-f",
"--first-job",ifirst);
364 if (have_np && have_ip) {
370 egsWarning(
"%s\n invalid -f argument %d\n",__egs_app_msg1,
377 egsWarning(
"%s\n invalid -P argument %d\n",__egs_app_msg1,
381 if (i_parallel < 0) {
382 egsWarning(
"%s\n invalid -j argument %d\n",__egs_app_msg1,
388 egsWarning(
"%s\n job number (%d) can not be larger than number"
389 " of parallel jobs(%d). Turning off parallel option\n",
395 else if (have_np && !have_ip) {
404 else if (!have_np && have_ip) {
405 egsWarning(
"\n%s\n to specify a parallel run you need both,"
406 " the -P and -j command line options\n\n",__egs_app_msg1);
412 for (
int j=1; j<argc; j++) {
413 string tmp = argv[j];
414 if (tmp ==
"-s" || tmp ==
"--simple-run") {
426 for (
int j=1; j<argc; j++) {
427 string tmp = argv[j];
428 if (tmp ==
"-u" || tmp ==
"--urc") {
447 if (ifile.size() > 0) {
449 egsFatal(
"%s\n error while reading input file %s\n",
450 __egs_app_msg1,ifile.c_str());
456 const char *name1,
const char *name2,
string &arg) {
457 string n1(name1), n2(name2);
458 for (
int j=1; j<argc-1; j++) {
459 if (n1 == argv[j] || n2 == argv[j]) {
470 bool with_run_dir)
const {
490 data_out =
new ofstream(ofile.c_str());
492 egsWarning(
"EGS_Application::outputData: failed to open %s "
493 "for writing\n",ofile.c_str());
526 data_in =
new ifstream(ifile.c_str());
528 egsWarning(
"EGS_Application::readData: failed to open %s "
529 "for reading\n",ifile.c_str());
557 rndm->resetCounter();
566 if (!
run->addState(data)) {
575 if (!
rndm->addState(data)) {
588 bool fileExists(
const string &name) {
590 return (stat(name.c_str(), &buffer) == 0);
601 if (fileExists(dfile)) {
623 "\n Suming the following .egsdat files:\n"
624 "=======================================================================\n");
627 EGS_Float last_cpu = 0;
628 EGS_I64 last_ncase = 0;
642 ifstream data(dfile.c_str());
647 EGS_I64 ncase =
run->getNdone();
648 EGS_Float cpu =
run->getCPUTime();
650 ndat,buf,ncase-last_ncase,cpu-last_cpu);
656 egsWarning(
"%2d %-30s error %d\n",ndat,buf,err);
662 "=======================================================================\n");
663 egsInformation(
"%40s%-14lld cpu=%-11.2f\n\n",
"Total ncase=",last_ncase,
720 run = EGS_RunControl::getRunControlObject(
this);
740 egsWarning(
"EGS_Application::initRNG(): using default RNG\n");
744 egsWarning(
"EGS_Application::initRNG(): got null RNG?\n");
757 egsWarning(
"\n\n%s geometry initialization failed\n",__egs_app_msg2);
762 egsWarning(
"\n\n%s source initialization failed\n",__egs_app_msg2);
767 egsWarning(
"\n\n%s RNG initialization failed\n",__egs_app_msg2);
772 egsWarning(
"\n\n%s run control initialization failed\n",__egs_app_msg2);
780 egsWarning(
"\n\n%s back-end initialization failed\n",__egs_app_msg2);
786 egsWarning(
"\n\n%s cross section initialization failed\n",__egs_app_msg2);
791 egsWarning(
"\n\n%s scoring initialization failed with status %d\n",
816 "\n========================================="
827 egsInformation(
"The following ausgab objects are included in the simulation\n");
828 egsInformation(
"===========================================================\n\n");
839 egsWarning(
"%s no geometry\n",__egs_app_msg3);
851 egsWarning(
"%s no run control object\n",__egs_app_msg3);
860 if (start_status < 0) {
861 egsWarning(
"\n%s failed to start the simulation\n\n",__egs_app_msg3);
867 bool next_chunk =
true;
873 if (
run->getCombinedResult(f,df)) {
876 " runs: %lg +/- %7.3lf%c\n\n",f,df,c);
882 EGS_I64 ncase_per_batch = ncase/nbatch;
883 if (!ncase_per_batch) {
887 for (
int ibatch=0; ibatch<nbatch; ibatch++) {
893 for (EGS_I64 icase=0; icase<ncase_per_batch; icase++) {
896 "loop termination\n");
923 egsWarning(
"EGS_Application::simulateSingleShower(): no particle"
924 " from the source has entered the geometry after 100000"
975 active_egs_application =
this;
978 active_egs_application = app;
995 if (active_egs_application ==
this) {
996 active_egs_application = 0;
1009 egsFatal(
"error: only one ausgab object of type "
1010 "'EGS_TrackScoring' is allowed\n");
1088 fstat(fileno(stream), &tmp);
1091 if (S_ISREG(tmp.st_mode)) {
1092 struct statvfs fstats;
1093 fstatvfs(fileno(stdout), &fstats);
1094 if (fstats.f_bavail*fstats.f_bsize < 1048576) {
1095 egsFatal(
"\n\n******** Space left on file system is below 1 MB. Quitting now.\n");
1104 fprintf(stdout,
"%s",msg);
1112 fprintf(stderr,
"%s",msg);
1120 fprintf(stderr,
"%s",msg);
string app_dir
The user code directory.
virtual int runSimulation()
Runs an EGSnrc simulation.
#define EGS_EXPORT
Export symbols from the egspp library.
static EGS_BaseSource * createSource(EGS_Input *)
Create sources from the information pointed to by input.
EGS_Input * input
the input to this simulation.
string egsStripPath(const string &aname)
Strip the path from a file name and return the result.
virtual int initScoring()
Initialize the scoring of quantities of interest.
virtual int startSimulation()
Starts the simulation.
EGS_I64 current_case
The current case as returned from the source.
virtual EGS_I64 getNextChunk()
Returns the number of histories to run in the next simulation chunk.
virtual int readData()
Read intermediate results.
bool EGS_EXPORT egsGetI64(istream &data, EGS_I64 &n)
Reads a 64 bit integer from the stream data and assigns it to n. Returns true on success, false on failure.
int deref()
Decrease the reference count to this geometry.
static int nObjects()
Returns the number of ausgab objects in the internal list.
EGS_Float E
particle energy in MeV
virtual void setSimulationChunk(EGS_I64 nstart, EGS_I64 nrun)
Set the next simulation chunk to start at nstart and to consist of nrun particles.
A simple run control object for advanced EGSnrc C++ applications.
virtual void printInfo() const
Print information about this geometry.
virtual void describeSimulation()
Describe the simulation.
int egsGetPid()
Get the process id.
EGS_AusgabObject interface class header file.
virtual int initSimulation()
Initializes the EGSnrc application.
virtual int combineResults()
Combine results from parallel runs.
virtual void describeUserCode() const
Describe the user code.
EGS_BaseSource class header file.
virtual void resetCounter()
Reset the application to a 'pristine' state.
int n_parallel
Number of parallel jobs.
static bool getArgument(int &argc, char **argv, const char *name1, const char *name2, string &arg)
Finds a command line argument.
bool storeState(ostream &data)
Functions for storing, seting and reseting the state of a RNG object.
static EGS_AusgabObject * getObject(int j)
Returns the j'th ausgab object in the internal list.
int first_parallel
first parallel job number
bool EGS_EXPORT egsStoreI64(ostream &data, EGS_I64 n)
Writes the 64 bit integer n to the output stream data and returns true on success, false on failure.
AusgabCall
Possible calls to the user scoring function ausgab().
virtual int combinePartialResults()
Combine intermediate results from parallel runs.
virtual void outputResults()
Output the simulation results.
A class representing 3D vectors.
string egsJoinPath(const string &first, const string &second)
Join two path variables (or a path and a file name) using the platform specific directory separator a...
static void setActiveApplication(EGS_Application *)
Set the active EGS_Application class.
Global egspp functions header file.
virtual bool addState(istream &data_in)
Add data from the stream data_in to the source state.
virtual int initRunControl()
Construct the run control object.
virtual int isWhere(const EGS_Vector &x)=0
In which region is poisition x?
bool batch_run
Interactive or batch run.
static void deleteObject(EGS_Object *o)
Delete an object.
virtual void resetCounter()
Reset the source state.
virtual int howfar(int ireg, const EGS_Vector &x, const EGS_Vector &u, EGS_Float &t, int *newmed=0, EGS_Vector *normal=0)=0
Calculate the distance to a boundary from x along the direction u.
const EGS_Float veryFar
A very large float.
virtual void setSimulationChunk(EGS_I64 nstart, EGS_I64 nrun)
Set the simulation chunk.
virtual void appInformation(const char *)
Write an information message.
const char * getSourceDescription() const
Get a short description of this source.
virtual void appFatal(const char *)
Write a warning message and exit.
bool ausgab_flag[UnknownCall]
on/off flags for ausgab calls
EGS_I64 numbersUsed() const
Returns the number of random numbers used so far.
virtual void fillArray(int n, EGS_Float *array)=0
Fill the array of n elements pointed to by array with random numbers.
virtual int finishShower()
Called just after the shower() function.
virtual bool storeState(ostream &data_out) const
Store the source state into the stream data_out.
virtual bool needsCall(EGS_Application::AusgabCall iarg) const
Is the ausgab call iarg relevant for this object?
bool simple_run
Use a simple run control object for parallel runs.
int getNbatch() const
Returns the number of batches per simulation chunk.
static void setActiveGeometryList(int list)
Set the currently active geometry list.
EGS_RandomGenerator * rndm
the random number generator
virtual int initEGSnrcBackEnd()
Initialize the EGSnrc backend.
EGS_InfoFunction EGS_EXPORT egsFatal
Always use this function for reporting fatal errors.
string run_dir
The working directory during the run.
EGS_RandomGenerator class header file.
virtual int shower()
Simulate a single shower.
virtual int startNewShower()
Called just before the shower() function.
ostream * data_out
data output stream
bool is_pegsless
set to true if a pegsless run
int ref()
Increase the reference count to this object.
virtual bool setState(istream &data_in)
Set the source state based on data from the stream data_in.
virtual bool finishBatch()
Finish a batch.
EGS_InfoFunction EGS_EXPORT egsInformation
Always use this function for reporting the progress of a simulation and any other type of information...
EGS_SimpleContainer template class.
EGS_RunControl * run
the run control object.
virtual int initRNG()
Initialize the random number generator.
istream * data_in
data input stream
virtual void fillRandomArray(int n, EGS_Float *rns)
Fill an array with random numbers using the application's RNG.
virtual int initCrossSections()
Initialize the EGSnrc cross sections and cross section/transport options.
virtual bool startBatch(int, EGS_I64)
Start a new batch.
static EGS_Application * activeApplication()
Get the active application.
void initAusgabObjects()
Initialize ausgab objects.
int i_parallel
Job index in parallel runs.
string constructIOFileName(const char *extension, bool with_run_dir) const
Constructs and returns the name of an input/output file.
int ir
particle region index
string app_name
The application name.
virtual void describeRNG() const
Describe this RNG.
virtual int outputData()
Output intermediate results.
void addAusgabObject(EGS_AusgabObject *o)
Adds an ausgab object to the list of ausgab objects.
string hen_house
The HEN_HOUSE directory.
static void createAusgabObjects(EGS_Input *)
Create ausgab objects from the information pointed to by input.
EGS_Application(int argc, char **argv)
Construct an EGSnrc application.
const string & getObjectType() const
Get the object type.
int app_index
the index of this application.
virtual int finishSimulation()
Analyze and output the results.
virtual EGS_I64 randomNumbersUsed() const
Returns the number of random numbers used.
int ref()
Increase the reference count to this geometry.
static EGS_BaseGeometry * createGeometry(EGS_Input *)
Create a geometry (or geometries) from a given input.
last element in the enumeration
static EGS_RandomGenerator * createRNG(EGS_Input *inp, int sequence=0)
Create a RNG object from the information pointed to by inp and return a pointer to it...
virtual int addState(istream &data)
Add data from a parallel job.
int latch
latch variable (useful as a flag on many occasions)
virtual int initGeometry()
Initialize the simulation geometry.
int userScoring(int iarg, int ir=-1)
User scoring function for accumulation of results and VRT implementation.
EGS_SimpleContainer< EGS_AusgabObject * > a_objects_list
The ausgab objects.
virtual int simulateSingleShower()
Simulates a single particle shower.
virtual void appWarning(const char *)
Write a warning message.
string output_file
The output file name (no extension)
EGS_I64 last_case
The last case simulated.
static EGS_RandomGenerator * defaultRNG(int sequence=0)
Returns a pointer to the default egspp RNG.
virtual EGS_I64 getNextParticle(EGS_RandomGenerator *rndm, int &q, int &latch, EGS_Float &E, EGS_Float &wt, EGS_Vector &x, EGS_Vector &u)=0
Sample the next source particle from the source probability distribution.
string input_file
The input file name.
virtual void setAusgabCall(AusgabCall call, bool on_or_off)
Turns on or off a call to the user scoring function ausgab.
string egs_home
The EGS_HOME directory.
EGS_Float wt
statistical weight
EGS_BaseGeometry * geometry
the geometry of this simulation
virtual void setApplication(EGS_Application *App)
Set the application this object belongs to.
EGS_Application class header file.
void checkDeviceFull(FILE *)
Check if a device holding a given stream is full.
EGS_BaseSource * source
the particle source
bool uniform_run
Use a uniform run control object for parallel runs.
virtual int finishSimulation()
Finish the simulation.
EGS_SimpleContainer< EGS_AusgabObject * > * a_objects
The ausgab objects for the various ausgab calls.
virtual ~EGS_Application()
Destruct the EGSnrc application.
Base class for advanced EGSnrc C++ applications.
string egsHostName()
Get the name of the host the program is running on.
static void checkEnvironmentVar(int &argc, char **argv, const char *env, const char *n1, const char *n2, string &var)
Finds a command line argument.
EGS_RunControl and EGS_JCFControl class header file.
int howManyJobsDone()
Counts how many *.egsdat files in app folder.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.
string final_output_file
The final output file name.
virtual int initSource()
Initialize the particle source.