EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_base_source.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ base source 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: Reid Townson
27 #
28 ###############################################################################
29 */
30 
31 
40 #ifndef EGS_BASE_SOURCE_
41 #define EGS_BASE_SOURCE_
42 
43 #include "egs_vector.h"
44 #include "egs_object_factory.h"
45 #include "egs_functions.h"
46 
47 #include <string>
48 #include <iostream>
49 #include "egs_math.h"
50 
51 using namespace std;
52 
53 class EGS_Input;
55 
81 
82 public:
83 
87  EGS_BaseSource(const string &Name="", EGS_ObjectFactory *f = 0) :
88  EGS_Object(Name,f) {};
89 
99  EGS_Object(input,f) {};
100  virtual ~EGS_BaseSource() {};
101 
107  const char *getSourceDescription() const {
108  return description.c_str();
109  };
110 
131  virtual EGS_I64 getNextParticle(EGS_RandomGenerator *rndm,
132  int &q, int &latch, // charge and latch
133  EGS_Float &E, EGS_Float &wt, // energy and weight
134  EGS_Vector &x, EGS_Vector &u) = 0; // position and direction
135 
143  virtual void setSimulationChunk(EGS_I64 nstart, EGS_I64 nrun) { };
144 
152  virtual int getCharge() const {
153  return -99;
154  };
165  virtual EGS_Float getEmax() const = 0;
166 
180  virtual EGS_Float getFluence() const = 0;
181 
182  /* A virtual function which can be implemented in derived classes
183  * to return a fractional monitor unit associated with each source
184  * particle. Currently only makes sense for IAEA_PhspSource and
185  * EGS_BeamSource.
186  */
187  virtual EGS_Float getMu() {
188  return -1;
189  };
190 
202  virtual bool storeState(ostream &data_out) const {
203  return true;
204  };
205 
216  virtual bool setState(istream &data_in) {
217  return true;
218  };
219 
232  virtual bool addState(istream &data_in) {
233  return true;
234  };
235 
247  virtual void resetCounter() {};
248 
275  static EGS_BaseSource *createSource(EGS_Input *);
276 
285  static EGS_BaseSource *getSource(const string &Name);
286 
294  static void addKnownSource(EGS_BaseSource *o);
295 
304  static void addKnownTypeId(const char *name);
305 
306 protected:
307 
313  string description;
314 
315 };
316 
335 
336 public:
337 
343  EGS_BaseSpectrum() : count(0), sum_E(0), sum_E2(0),
344  type("Unknown spectrum") {};
345 
347  virtual ~EGS_BaseSpectrum() {};
348 
355  const string &getType() const {
356  return type;
357  };
358 
365  inline EGS_Float sampleEnergy(EGS_RandomGenerator *rndm) {
366  EGS_Float e = sample(rndm);
367  count++;
368  sum_E += e;
369  sum_E2 += e*e;
370  return e;
371  };
372 
378  virtual EGS_Float maxEnergy() const = 0;
379 
385  virtual EGS_Float expectedAverage() const = 0;
386 
399  virtual bool storeState(ostream &data_out) const {
400  if (!egsStoreI64(data_out,count)) {
401  return false;
402  }
403  data_out << " " << sum_E << " " << sum_E2 << endl;
404  if (!data_out.good() || data_out.fail()) {
405  return false;
406  }
407  return true;
408  };
409 
423  virtual bool setState(istream &data_in) {
424  if (!egsGetI64(data_in,count)) {
425  return false;
426  }
427  data_in >> sum_E >> sum_E2;
428  if (data_in.eof() || !data_in.good() || data_in.fail()) {
429  return false;
430  }
431  return true;
432  };
433 
446  virtual bool addState(istream &data_in) {
447  EGS_I64 count_save = count;
448  double sum_E_save = sum_E, sum_E2_save = sum_E2;
449  if (!setState(data_in)) {
450  return false;
451  }
452  count += count_save;
453  sum_E += sum_E_save;
454  sum_E2 += sum_E2_save;
455  return true;
456  };
457 
469  virtual void resetCounter() {
470  count = 0;
471  sum_E = 0;
472  sum_E2 = 0;
473  };
474 
488  static EGS_BaseSpectrum *createSpectrum(EGS_Input *inp);
489 
495  void getSampledAverage(EGS_Float &e, EGS_Float &de) const {
496  if (count > 1) {
497  e = sum_E/count;
498  de = sum_E2/count;
499  de -= e*e;
500  if (de > 0) {
501  de = sqrt(de/(count-1));
502  }
503  }
504  };
505 
511  void reportAverageEnergy() const;
512 
513 protected:
514 
521  virtual EGS_Float sample(EGS_RandomGenerator *rndm) = 0;
522 
524  EGS_I64 count;
525 
527  double sum_E;
528 
530  double sum_E2;
531 
535  string type;
536 
537 };
538 
558 
559 public:
560 
569  const string &Name="", EGS_ObjectFactory *f=0) :
570  EGS_BaseSource(Name,f), q(Q), s(Spec), count(0) { };
571 
583 
589  if (s) {
590  delete s;
591  }
592  };
593 
604  virtual bool isValid() const {
605  return (s != 0);
606  };
607 
619  virtual EGS_I64 getNextParticle(EGS_RandomGenerator *rndm,
620  int &Q, int &latch, EGS_Float &E, EGS_Float &wt,
621  EGS_Vector &x, EGS_Vector &u) {
622  Q = q;
623  E = s->sampleEnergy(rndm);
624  getPositionDirection(rndm,x,u,wt);
625  setLatch(latch);
626  return ++count;
627  };
628 
637  virtual void getPositionDirection(EGS_RandomGenerator *rndm,
638  EGS_Vector &x, EGS_Vector &u, EGS_Float &wt) = 0;
639 
645  virtual EGS_Float getEmax() const {
646  return s->maxEnergy();
647  };
648 
653  int getCharge() const {
654  return q;
655  };
656 
667  virtual bool storeFluenceState(ostream &data_out) const {
668  return true;
669  };
670 
676  virtual bool storeState(ostream &data_out) const {
677  if (!egsStoreI64(data_out,count)) {
678  return false;
679  }
680  if (!s->storeState(data_out)) {
681  return false;
682  }
683  if (!storeFluenceState(data_out)) {
684  return false;
685  }
686  return true;
687  };
688 
695  virtual bool addState(istream &data) {
696  EGS_I64 count_save = count;
697  if (!egsGetI64(data,count)) {
698  return false;
699  }
700  if (!s->addState(data)) {
701  return false;
702  }
703  if (!addFluenceData(data)) {
704  return false;
705  }
706  count += count_save;
707  return true;
708  };
709 
716  virtual void resetCounter() {
717  count = 0;
718  s->resetCounter();
719  resetFluenceCounter();
720  };
721 
731  virtual bool addFluenceData(istream &data) {
732  return true;
733  }
734 
745  virtual void resetFluenceCounter() { };
746 
757  virtual bool setFluenceState(istream &data) {
758  return true;
759  };
760 
767  virtual bool setState(istream &data) {
768  if (!egsGetI64(data,count)) {
769  return false;
770  }
771  if (!s->setState(data)) {
772  return false;
773  }
774  if (!setFluenceState(data)) {
775  return false;
776  }
777  return true;
778  };
779 
780 protected:
781 
788  virtual void setLatch(int &latch) {
789  latch = 0;
790  };
791 
793  int q;
794 
797 
799  string type;
800 
802  EGS_I64 count;
803 
804 };
805 
815 template <class T>
817  EGS_ObjectFactory *f, const char *name) {
818  EGS_BaseSource::addKnownTypeId(typeid(T).name());
819  if (!input) {
820  egsWarning("createSource(%s): null input?\n",name);
821  return 0;
822  }
823  T *res = new T(input,f);
824  if (!res->isValid()) {
825  egsWarning("createSource(%s): the input is not "
826  "sufficient to create a valid source\n",name);
827  delete res;
828  return 0;
829  }
830  return res;
831 };
832 
833 #endif
834 
Base egspp object.
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90
virtual void setLatch(int &latch)
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.
void getSampledAverage(EGS_Float &e, EGS_Float &de) const
Get the average sampled energy and its statistical uncertainty.
virtual void setSimulationChunk(EGS_I64 nstart, EGS_I64 nrun)
Set the next simulation chunk to start at nstart and to consist of nrun particles.
EGS_BaseSimpleSource(int Q, EGS_BaseSpectrum *Spec, const string &Name="", EGS_ObjectFactory *f=0)
Constructor.
string type
A short string describing the spectrum that must be set by derived classes.
double sum_E
Sum of energies sampled so far.
virtual EGS_Float getEmax() const
Get the maximum energy of the source.
EGS_BaseSource(EGS_Input *input, EGS_ObjectFactory *f=0)
Construct a source from the input pointed to by inp.
EGS_Vector methods for the manipulation of 3D vectors in cartesian co-ordinates.
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.
EGS_BaseSource * createSourceTemplate(EGS_Input *input, EGS_ObjectFactory *f, const char *name)
A template source creation function.
virtual void resetCounter()
Reset the source to a state with zero sampled particles.
A class representing 3D vectors.
Definition: egs_vector.h:56
EGS_BaseSource(const string &Name="", EGS_ObjectFactory *f=0)
Construct a source named Name.
virtual bool addState(istream &data_in)
Add to the state of this object the data from the stream data_in.
Global egspp functions header file.
virtual bool addState(istream &data_in)
Add data from the stream data_in to the source state.
virtual void resetCounter()
Reset the source state.
virtual bool addState(istream &data)
Add the source state from the stream data to the current state.
EGS_I64 count
Number of statistically independent particles delivered so far.
EGS_Float sampleEnergy(EGS_RandomGenerator *rndm)
Sample a particle energy.
const char * getSourceDescription() const
Get a short description of this source.
virtual void resetCounter()
Reset the state of this spectrum object.
virtual bool storeState(ostream &data_out) const
Store the source state into the stream data_out.
EGS_BaseSpectrum * s
The energy spectrum of this source.
Base random number generator class. All random number generators should be derived from this class...
Definition: egs_rndm.h:67
virtual bool setState(istream &data_in)
Set the state of the spectrum object from the data in the stream data_in.
EGS_BaseSpectrum()
Constructor.
EGS_I64 count
Number of times the sampleEnergy() method was called.
virtual bool setState(istream &data_in)
Set the source state based on data from the stream data_in.
An object factory.
virtual bool setState(istream &data)
Set the source state according to the data in the stream data.
~EGS_BaseSimpleSource()
Destructor.
Attempts to fix broken math header files.
virtual bool storeState(ostream &data_out) const
Store the state of the spectrum object into the stream data_out.
virtual bool storeFluenceState(ostream &data_out) const
Store the fluence state of this source to the data stream data_out.
string type
A short description of the source type.
virtual int getCharge() const
Get the charge of the source.
EGS_Object and EGS_ObjectFactory class header file.
const string & getType() const
Get the spectrum type.
virtual ~EGS_BaseSpectrum()
Destructor. Does nothing.
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
static void addKnownTypeId(const char *name)
Add a known source object typeid to the source factory.
int getCharge() const
Get the charge of the source.
virtual bool addFluenceData(istream &data)
Add fluence data from the stream data to the current state.
virtual void resetFluenceCounter()
Reset the data related to the sampling of positions and directions to a state with zero sampled parti...
Base class for energy spectra. All energy spectra in the EGSnrc C++ class library are derived from th...
Base class for &#39;simple&#39; particle sources.
virtual bool storeState(ostream &data_out) const
Store the source state to the data stream data_out.
virtual bool isValid() const
Is this a valid source?
virtual bool setFluenceState(istream &data)
Set the data related to the sampling of positions and directions to a state contained in the stream d...
double sum_E2
Sum of energies squared sampled so far.
Base source class. All particle sources must be derived from this class.
string description
A short source description.
virtual EGS_I64 getNextParticle(EGS_RandomGenerator *rndm, int &Q, int &latch, EGS_Float &E, EGS_Float &wt, EGS_Vector &x, EGS_Vector &u)
Sample the next source particle from the source probability distribution.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.