EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_dynamic_source.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ dynamic 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: Blake Walters, 2017
25 #
26 # Contributors: Reid Townson
27 # Alexandre Demelo
28 #
29 ###############################################################################
30 */
31 
32 
38 #ifndef EGS_DYNAMIC_SOURCE_
39 #define EGS_DYNAMIC_SOURCE_
40 
41 #include "egs_vector.h"
42 #include "egs_base_source.h"
43 #include "egs_rndm.h"
44 #include "egs_shapes.h"
45 #include <string>
46 #include <sstream>
47 
48 
49 #ifdef WIN32
50 
51  #ifdef BUILD_DYNAMIC_SOURCE_DLL
52  #define EGS_DYNAMIC_SOURCE_EXPORT __declspec(dllexport)
53  #else
54  #define EGS_DYNAMIC_SOURCE_EXPORT __declspec(dllimport)
55  #endif
56  #define EGS_DYNAMIC_SOURCE_LOCAL
57 
58 #else
59 
60  #ifdef HAVE_VISIBILITY
61  #define EGS_DYNAMIC_SOURCE_EXPORT __attribute__ ((visibility ("default")))
62  #define EGS_DYNAMIC_SOURCE_LOCAL __attribute__ ((visibility ("hidden")))
63  #else
64  #define EGS_DYNAMIC_SOURCE_EXPORT
65  #define EGS_DYNAMIC_SOURCE_LOCAL
66  #endif
67 
68 #endif
69 
167 class EGS_DYNAMIC_SOURCE_EXPORT EGS_DynamicSource :
168  public EGS_BaseSource {
169 
170 public:
171 
172 
174 
175  EGS_Vector iso; //isocentre position
176  EGS_Float dsource; //source-isocentre distance
177  EGS_Float theta; //angle of rotation about Y-axis
178  EGS_Float phi; //angle of rotation about Z-axis
179  EGS_Float phicol; //angle of rotation in source plane
180  EGS_Float time; //monitor unit index for control point
181  };
182 
187  EGS_DynamicSource(EGS_BaseSource *Source, vector<EGS_ControlPoint> cpts,
188  const string &Name="", EGS_ObjectFactory *f=0) :
189  EGS_BaseSource(Name,f), source(Source), valid(true) {
190  //do some checks on cpts
191  if (cpts.size()<2) {
192  egsWarning("EGS_DynamicSource: not enough or missing control points.\n");
193  valid = false;
194  }
195  else {
196  if (cpts[0].time > 0.0) {
197  egsWarning("EGS_DynamicSource: time index of control point 1 > 0.0. This will generate many warning messages.\n");
198  }
199  int npts = cpts.size();
200  for (int i=0; i<npts; i++) {
201  if (i>0 && cpts[i].time < cpts[i-1].time) {
202  egsWarning("EGS_DynamicSource: time index of control point %i < time index of control point %i\n",i,i-1);
203  valid = false;
204  }
205  if (cpts[i].time<0.0) {
206  egsWarning("EGS_DynamicSource: time index of control point %i < 0.0\n",i);
207  valid = false;
208  }
209  }
210  //normalize time values
211  for (int i=0; i<npts-1; i++) {
212  cpts[i].time /= cpts[npts-1].time;
213  }
214  }
215  setUp();
216  };
217 
220 
221  ~EGS_DynamicSource() {
222  EGS_Object::deleteObject(source);
223  };
224 
225  EGS_I64 getNextParticle(EGS_RandomGenerator *rndm,
226  int &q, int &latch, EGS_Float &E, EGS_Float &wt,
227  EGS_Vector &x, EGS_Vector &u) {
228  int err = 1;
229  EGS_ControlPoint ipt;
230  EGS_I64 c;
231  while (err) {
232  c = source->getNextParticle(rndm,q,latch,E,wt,x,u);
233  if (sync) {
234  ptime = source->getTimeIndex();
235  if (ptime<0) {
236  egsWarning("EGS_DynamicSource: You have selected synchronization of dynamic source with %s\n",source->getObjectName().c_str());
237  egsWarning("However, this source does not return time values for each particle. Will turn off synchronization.\n");
238  sync = false;
239  }
240  }
241  if (!sync) {
242  ptime = rndm->getUniform();
243  }
244  setTimeIndex(ptime); //this is added for the storing of time index in a single location. Technically stored as a basesource attribute not dynamic source
245  err = getCoord(ptime,ipt);
246  }
247 
248  //translate source in Z
249  x.z=x.z-ipt.dsource;
250  //get the rotation matrices
251  ipt.phicol *= M_PI/180;
252  ipt.theta *= M_PI/180;
253  ipt.phi *= M_PI/180;
255  EGS_RotationMatrix Rtheta(EGS_RotationMatrix::rotY(ipt.theta));
257  //apply rotations in specific order and then translate relative
258  //to the isocentre
259  u=Rphi*Rtheta*Rcol*u;
260  x=Rphi*Rtheta*Rcol*x + ipt.iso;
261  return c;
262  };
263  EGS_Float getEmax() const {
264  return source->getEmax();
265  };
266  EGS_Float getFluence() const {
267  return source->getFluence();
268  };
269  bool storeState(ostream &data) const {
270  return source->storeState(data);
271  };
272  bool setState(istream &data) {
273  return source->setState(data);
274  };
275  bool addState(istream &data_in) {
276  return source->addState(data_in);
277  };
278  void resetCounter() {
279  source->resetCounter();
280  };
281 
282  bool isValid() const {
283  return (valid && source != 0);
284  };
285 
286  void setSimulationChunk(EGS_I64 nstart, EGS_I64 nrun, int npar, int nchunk) {
287  source->setSimulationChunk(nstart, nrun, npar, nchunk);
288  };
289 
290  void containsDynamic(bool &hasdynamic);
291 
292 protected:
293 
295 
296  vector<EGS_ControlPoint> cpts; //control point
297 
298  int ncpts; //no. of control points
299 
300  bool valid; //is this a valid source
301 
302  bool sync; //set to true if source motion synched with time read from
303  //iaea phsp or beam simulation source
304 
305  int getCoord(const EGS_Float rand, EGS_ControlPoint &ipt);
306 
307  EGS_Float ptime; //time index corresponding to particle
308  //could just be a random number.
309 
310  void setUp();
311 
312 };
313 
314 #endif
Base source class. All particle sources must be derived from this class.
virtual bool addState(istream &data_in)
Add data from the stream data_in to the source state.
virtual EGS_Float getEmax() const =0
Return the maximum energy of this source.
virtual EGS_Float getFluence() const =0
Return the fluence this source has emitted so far.
virtual void resetCounter()
Reset the source state.
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.
virtual bool setState(istream &data_in)
Set the source state based on data from the stream data_in.
virtual void setSimulationChunk(EGS_I64 nstart, EGS_I64 nrun, int npar, int nchunk)
Set the next simulation chunk to start at nstart and to consist of nrun particles.
virtual bool storeState(ostream &data_out) const
Store the source state into the stream data_out.
A source with time-varying rotations/translations.
EGS_BaseSource * source
The source being rotated.
EGS_DynamicSource(EGS_BaseSource *Source, vector< EGS_ControlPoint > cpts, const string &Name="", EGS_ObjectFactory *f=0)
Construct a dynamic source using Source as the source and cpts as the control points....
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 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:67
EGS_Float getUniform()
Returns a random number uniformly distributed between zero (inclusive) and 1 (exclusive).
Definition: egs_rndm.h:103
A class for vector rotations.
static EGS_RotationMatrix rotY(EGS_Float cphi, EGS_Float sphi)
Returns a rotation around the y-axis by the angle with cphi, sphi = .
static EGS_RotationMatrix rotZ(EGS_Float cphi, EGS_Float sphi)
Returns a rotation around the z-axis by the angle with cphi, sphi = .
A class representing 3D vectors.
Definition: egs_vector.h:57
EGS_Float z
z-component
Definition: egs_vector.h:63
EGS_BaseSource class header file.
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 egsWarning
Always use this function for reporting warnings.