EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_collimated_source.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ collimated 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: Ernesto Mainegra-Hing
27 # Reid Townson
28 # Hubert Ho
29 #
30 ###############################################################################
31 */
32 
33 
39 #ifndef EGS_COLLIMATED_SOURCE_
40 #define EGS_COLLIMATED_SOURCE_
41 
42 #include "egs_vector.h"
43 #include "egs_base_source.h"
44 #include "egs_rndm.h"
45 #include "egs_shapes.h"
46 #include "egs_functions.h"
47 
48 
49 #ifdef WIN32
50 
51  #ifdef BUILD_COLLIMATED_SOURCE_DLL
52  #define EGS_COLLIMATED_SOURCE_EXPORT __declspec(dllexport)
53  #else
54  #define EGS_COLLIMATED_SOURCE_EXPORT __declspec(dllimport)
55  #endif
56  #define EGS_COLLIMATED_SOURCE_LOCAL
57 
58 #else
59 
60  #ifdef HAVE_VISIBILITY
61  #define EGS_COLLIMATED_SOURCE_EXPORT __attribute__ ((visibility ("default")))
62  #define EGS_COLLIMATED_SOURCE_LOCAL __attribute__ ((visibility ("hidden")))
63  #else
64  #define EGS_COLLIMATED_SOURCE_EXPORT
65  #define EGS_COLLIMATED_SOURCE_LOCAL
66  #endif
67 
68 #endif
69 
159 class EGS_COLLIMATED_SOURCE_EXPORT EGS_CollimatedSource :
160  public EGS_BaseSimpleSource {
161 
162 public:
163 
170  EGS_BaseShape *sshape, EGS_BaseShape *tshape,
171  const string &Name="", EGS_ObjectFactory *f=0) :
172  EGS_BaseSimpleSource(Q,Spec,Name,f), source_shape(sshape),
173  target_shape(tshape), ctry(0), dist(1) {
174  setUp();
175  };
176 
183  EGS_Object::deleteObject(source_shape);
184  EGS_Object::deleteObject(target_shape);
185  };
186 
188  EGS_Vector &x, EGS_Vector &u, EGS_Float &wt) {
189  //x = source_shape->getPoint(rndm);
190  x = source_shape->getRandomPoint(rndm);
191  int ntry = 0;
192  do {
193  target_shape->getPointSourceDirection(x,rndm,u,wt);
194  ntry++;
195  if (ntry > 10000)
196  egsFatal("EGS_CollimatedSource::getPositionDirection:\n"
197  " my target shape %s, which is of type %s, failed to\n"
198  " return a positive weight after 10000 attempts\n",
199  target_shape->getObjectName().c_str(),
200  target_shape->getObjectType().c_str());
201  }
202  while (wt <= 0);
203  //egsInformation("got x=(%g,%g,%g) u=(%g,%g,%g) wt = %g ntry = %d\n",
204  // x.x,x.y,x.z,u.x,u.y,u.z,wt,ntry);
205  ctry += ntry;
206  };
207 
208  EGS_Float getFluence() const {
209  double res = ctry;
210  return res/(dist*dist);
211  };
212 
213  bool storeFluenceState(ostream &data) const {
214  return egsStoreI64(data,ctry);
215  };
216 
217  bool setFluenceState(istream &data) {
218  return egsGetI64(data,ctry);
219  };
220 
221  bool addFluenceData(istream &data) {
222  EGS_I64 tmp;
223  bool ok = egsGetI64(data,tmp);
224  if (!ok) {
225  return false;
226  }
227  ctry += tmp;
228  return true;
229  };
230 
231  bool isValid() const {
232  return (s != 0 && source_shape != 0 && target_shape != 0 &&
233  target_shape->supportsDirectionMethod());
234  };
235 
236  void resetFluenceCounter() {
237  ctry = 0;
238  };
239 
240 protected:
241 
242  EGS_BaseShape *source_shape,
243  *target_shape;
244  EGS_I64 ctry;
245  EGS_Float dist;
246 
247  void setUp();
248 
249 };
250 
251 #endif
virtual void getPositionDirection(EGS_RandomGenerator *rndm, EGS_Vector &x, EGS_Vector &u, EGS_Float &wt)=0
Sample a particle position and direction.
EGS_I64 ctry
number of attempts to sample a particle
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.
EGS_BaseSource class header file.
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.
A class representing 3D vectors.
Definition: egs_vector.h:56
Global egspp functions header file.
static void deleteObject(EGS_Object *o)
Delete an object.
Base shape class. All shapes in the EGSnrc C++ class library are derived from EGS_BaseShape.
Definition: egs_shapes.h:112
A collimated particle source.
Base random number generator class. All random number generators should be derived from this class...
Definition: egs_rndm.h:67
EGS_InfoFunction EGS_EXPORT egsFatal
Always use this function for reporting fatal errors.
EGS_RandomGenerator class header file.
EGS_BaseShape and shape classes header file.
An object factory.
EGS_CollimatedSource(int Q, EGS_BaseSpectrum *Spec, EGS_BaseShape *sshape, EGS_BaseShape *tshape, const string &Name="", EGS_ObjectFactory *f=0)
EGS_Float dist
source-target shape min. distance
virtual EGS_Float getFluence() const =0
Return the fluence this source has emitted so far.
virtual bool storeFluenceState(ostream &data_out) const
Store the fluence state of this source to the data stream data_out.
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
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 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...