EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_isotropic_source.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ isotropic 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: Long Zhang
27 # Frederic Tessier
28 # Reid Townson
29 # Ernesto Mainegra-Hing
30 # Hugo Bouchard
31 # Hubert Ho
32 # Marc Chamberland
33 # Reid Townson
34 #
35 ###############################################################################
36 */
37 
38 
44 #ifndef EGS_ISOTROPIC_SOURCE_
45 #define EGS_ISOTROPIC_SOURCE_
46 
47 #include "egs_vector.h"
48 #include "egs_base_source.h"
49 #include "egs_rndm.h"
50 #include "egs_shapes.h"
51 #include "egs_base_geometry.h"
52 #include "egs_math.h"
53 
54 
55 #ifdef WIN32
56 
57  #ifdef BUILD_ISOTROPIC_SOURCE_DLL
58  #define EGS_ISOTROPIC_SOURCE_EXPORT __declspec(dllexport)
59  #else
60  #define EGS_ISOTROPIC_SOURCE_EXPORT __declspec(dllimport)
61  #endif
62  #define EGS_ISOTROPIC_SOURCE_LOCAL
63 
64 #else
65 
66  #ifdef HAVE_VISIBILITY
67  #define EGS_ISOTROPIC_SOURCE_EXPORT __attribute__ ((visibility ("default")))
68  #define EGS_ISOTROPIC_SOURCE_LOCAL __attribute__ ((visibility ("hidden")))
69  #else
70  #define EGS_ISOTROPIC_SOURCE_EXPORT
71  #define EGS_ISOTROPIC_SOURCE_LOCAL
72  #endif
73 
74 #endif
75 
246 class EGS_ISOTROPIC_SOURCE_EXPORT EGS_IsotropicSource :
247  public EGS_BaseSimpleSource {
248 
249 public:
250 
253  IncludeAll = 0,
254  ExcludeAll = 1,
255  IncludeSelected = 2,
256  ExcludeSelected = 3
257  };
258 
265  EGS_BaseGeometry *geometry,
266  const string &Name="", EGS_ObjectFactory *f=0) :
267  EGS_BaseSimpleSource(Q,Spec,Name,f), shape(Shape),
268  geom(geometry), regions(0), min_theta(85.), max_theta(95.),
269  buf_1(1), buf_2(-1), min_phi(0), max_phi(2*M_PI),
270  nrs(0), gc(IncludeAll), media(0), nms(0), gcm(IncludeSelected) {
271  setUp();
272  };
273 
281  if (geom) {
282  if (!geom->deref()) {
283  delete geom;
284  }
285  }
286  if (nrs > 0 && regions) {
287  delete [] regions;
288  }
289  if (nms > 0 && media) {
290  delete [] media;
291  }
292  };
293 
295  EGS_Vector &x, EGS_Vector &u, EGS_Float &wt) {
296  bool ok = true;
297  bool ok2 = true;
298  int ntry = 0;
299  do {
300  x = shape->getRandomPoint(rndm);
301  if (geom) {
302  // Filter by region
303  if (gc == IncludeAll) {
304  ok = geom->isInside(x);
305  }
306  else if (gc == ExcludeAll) {
307  ok = !geom->isInside(x);
308  }
309  else if (gc == IncludeSelected) {
310  ok = false;
311  int ireg = geom->isWhere(x);
312  for (int j=0; j<nrs; ++j) {
313  if (ireg == regions[j]) {
314  ok = true;
315  break;
316  }
317  }
318  }
319  else if (gc == ExcludeSelected) {
320  ok = true;
321  int ireg = geom->isWhere(x);
322  for (int j=0; j<nrs; ++j) {
323  if (ireg == regions[j]) {
324  ok = false;
325  break;
326  }
327  }
328  }
329 
330  // Filter by medium
331  if (ok && nms > 0) {
332  if (gcm == IncludeSelected) {
333  ok2 = false;
334  int ireg = geom->isWhere(x);
335  int currentMedium = geom->medium(ireg);
336  for (int j=0; j<nms; ++j) {
337  if (currentMedium == media[j]) {
338  ok2 = true;
339  break;
340  }
341  }
342  }
343  else if (gcm == ExcludeSelected) {
344  ok2 = true;
345  int ireg = geom->isWhere(x);
346  for (int j=0; j<nrs; ++j) {
347  if (ireg == media[j]) {
348  ok2 = false;
349  break;
350  }
351  }
352  }
353  }
354 
355  // Combine the filtering by region and medium
356  if (ok2 == false) {
357  ok = false;
358  }
359  }
360  ntry++;
361  if (ntry > 100000) {
362  egsFatal("\nEGS_IsotropicSource::getPositionDirection:\n"
363  " For target shape %s, which is of type %s:\n"
364  " Failed to create a particle from the source after 100 000 attempts\n"
365  " Please ensure your .egsinp source definition -> source"
366  " -> shape encompasses the source region.\n",
367  shape->getObjectName().c_str(),
368  shape->getObjectType().c_str());
369  }
370  }
371  while (!ok);
372  u.z = buf_1 - rndm->getUniform()*(buf_1 - buf_2);
373  EGS_Float sinz = 1-u.z*u.z;
374  if (sinz > epsilon) {
375  sinz = sqrt(sinz);
376  EGS_Float cphi, sphi;
377  EGS_Float phi = min_phi +(max_phi - min_phi)*rndm->getUniform();
378  cphi = cos(phi);
379  sphi = sin(phi);
380  u.x = sinz*cphi;
381  u.y = sinz*sphi;
382  }
383  else {
384  u.x = 0;
385  u.y = 0;
386  }
387  wt = 1;
388  };
389 
390  EGS_Float getFluence() const {
391  return count;
392  };
393 
394  bool storeFluenceState(ostream &) const {
395  return true;
396  };
397 
398  bool setFluenceState(istream &) {
399  return true;
400  };
401 
402  bool isValid() const {
403  return (s != 0 && shape != 0);
404  };
405 
406 protected:
407 
409  EGS_BaseGeometry *geom;
410  int *regions, *media;
411 
412  void setUp();
413 
414  EGS_Float min_theta, max_theta;
415  EGS_Float buf_1, buf_2;
416  EGS_Float min_phi, max_phi;
417 
418  int nrs, nms;
419  GeometryConfinement gc, gcm;
420 };
421 
422 #endif
Base geometry class. Every geometry class must be derived from EGS_BaseGeometry.
Base shape class. All shapes in the EGSnrc C++ class library are derived from EGS_BaseShape.
Definition: egs_shapes.h:145
Base class for 'simple' particle sources.
virtual void getPositionDirection(EGS_RandomGenerator *rndm, EGS_Vector &x, EGS_Vector &u, EGS_Float &wt)=0
Sample a particle position and direction.
virtual bool isValid() const
Is this a valid source?
virtual bool storeFluenceState(ostream &data_out) const
Store the fluence state of this source to the data stream data_out.
virtual bool setFluenceState(istream &data)
Set the data related to the sampling of positions and directions to a state contained in the stream d...
EGS_I64 count
Number of statistically independent particles delivered so far.
virtual EGS_Float getFluence() const =0
Return the fluence this source has emitted so far.
Base class for energy spectra. All energy spectra in the EGSnrc C++ class library are derived from th...
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
An isotropic source.
EGS_BaseShape * shape
The shape from which particles are emitted.
EGS_IsotropicSource(int Q, EGS_BaseSpectrum *Spec, EGS_BaseShape *Shape, EGS_BaseGeometry *geometry, const string &Name="", EGS_ObjectFactory *f=0)
Constructor.
GeometryConfinement
Geometry confinement options.
EGS_Float min_phi
avoid multi-calculating cos(min_theta) and cos(max_theta)
An object factory.
static void deleteObject(EGS_Object *o)
Delete an object.
Base random number generator class. All random number generators should be derived from this class.
Definition: egs_rndm.h:90
EGS_Float getUniform()
Returns a random number uniformly distributed between zero (inclusive) and 1 (exclusive).
Definition: egs_rndm.h:126
A class representing 3D vectors.
Definition: egs_vector.h:57
EGS_Float y
y-component
Definition: egs_vector.h:62
EGS_Float z
z-component
Definition: egs_vector.h:63
EGS_Float x
x-component
Definition: egs_vector.h:61
EGS_BaseGeometry class header file.
EGS_BaseSource class header file.
Attempts to fix broken math header files.
EGS_RandomGenerator class header file.
EGS_BaseShape and shape classes header file.
EGS_Vector methods for the manipulation of 3D vectors in cartesian co-ordinates.
EGS_InfoFunction EGS_EXPORT egsFatal
Always use this function for reporting fatal errors.
const EGS_Float epsilon
The epsilon constant for floating point comparisons.
Definition: egs_functions.h:62