EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_source_collection.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ source collection 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 # Marc Chamberland
28 #
29 ###############################################################################
30 */
31 
32 
38 #ifndef EGS_SOURCE_COLLECTION_
39 #define EGS_SOURCE_COLLECTION_
40 
41 #include "egs_vector.h"
42 #include "egs_base_source.h"
43 #include "egs_rndm.h"
44 #include "egs_alias_table.h"
45 
46 #ifdef WIN32
47 
48  #ifdef BUILD_SOURCE_COLLECTION_DLL
49  #define EGS_SOURCE_COLLECTION_EXPORT __declspec(dllexport)
50  #else
51  #define EGS_SOURCE_COLLECTION_EXPORT __declspec(dllimport)
52  #endif
53  #define EGS_SOURCE_COLLECTION_LOCAL
54 
55 #else
56 
57  #ifdef HAVE_VISIBILITY
58  #define EGS_SOURCE_COLLECTION_EXPORT __attribute__ ((visibility ("default")))
59  #define EGS_SOURCE_COLLECTION_LOCAL __attribute__ ((visibility ("hidden")))
60  #else
61  #define EGS_SOURCE_COLLECTION_EXPORT
62  #define EGS_SOURCE_COLLECTION_LOCAL
63  #endif
64 
65 #endif
66 
120 class EGS_SOURCE_COLLECTION_EXPORT EGS_SourceCollection :
121  public EGS_BaseSource {
122 
123 public:
124 
130  EGS_SourceCollection(const vector<EGS_BaseSource *> &S,
131  const vector<EGS_Float> &prob,
132  const string &Name="", EGS_ObjectFactory *f=0) :
133  EGS_BaseSource(Name,f), nsource(0), count(0) {
134  setUp(S,prob);
135  };
136 
143  if (nsource > 0) {
144  for (int j=0; j<nsource; j++) {
145  EGS_Object::deleteObject(sources[j]);
146  }
147  delete [] sources;
148  delete table;
149  delete [] p;
150  delete [] last_cases;
151  }
152  };
153 
154  EGS_I64 getNextParticle(EGS_RandomGenerator *rndm,
155  int &q, int &latch, EGS_Float &E, EGS_Float &wt,
156  EGS_Vector &x, EGS_Vector &u) {
157  int j = table->sample(rndm);
158  EGS_I64 this_case = sources[j]->getNextParticle(rndm,q,latch,E,wt,x,u);
159  count += this_case - last_cases[j];
160  last_cases[j] = this_case;
161  return count;
162  };
163  EGS_Float getEmax() const {
164  return Emax;
165  };
166  EGS_Float getFluence() const {
167  EGS_Float flu = 0;
168  for (int j=0; j<nsource; j++) {
169  flu += sources[j]->getFluence();
170  }
171  return flu;
172  };
173  bool storeState(ostream &data) const {
174  bool res = EGS_BaseSource::storeState(data);
175  if (!res) {
176  return res;
177  }
178  res = egsStoreI64(data,count);
179  if (!res) {
180  return res;
181  }
182  data << " ";
183  for (int j=0; j<nsource; j++) {
184  res = egsStoreI64(data,last_cases[j]);
185  if (!res) {
186  return res;
187  }
188  data << " ";
189  if (!sources[j]->storeState(data)) {
190  return false;
191  }
192  }
193  return true;
194  };
195  bool setState(istream &data) {
196  bool res = EGS_BaseSource::setState(data);
197  if (!res) {
198  return res;
199  }
200  res = egsGetI64(data,count);
201  if (!res) {
202  return res;
203  }
204  for (int j=0; j<nsource; j++) {
205  res = egsGetI64(data,last_cases[j]);
206  if (!res) {
207  return res;
208  }
209  if (!sources[j]->setState(data)) {
210  return false;
211  }
212  }
213  return true;
214  }
215 
216  void resetCounter() {
218  count = 0;
219  for (int j=0; j<nsource; ++j) {
220  last_cases[j] = 0;
221  sources[j]->resetCounter();
222  }
223  };
224 
225  virtual bool addState(istream &data_in) {
226  EGS_I64 tmp;
227  bool res = EGS_BaseSource::addState(data_in);
228  if (!res) {
229  return res;
230  }
231  res = egsGetI64(data_in,tmp);
232  if (!res) {
233  return res;
234  }
235  count += tmp;
236  for (int j=0; j<nsource; j++) {
237  res = egsGetI64(data_in,tmp);
238  if (!res) {
239  return res;
240  }
241  last_cases[j] += tmp;
242  if (!sources[j]->addState(data_in)) {
243  return false;
244  }
245  }
246  return true;
247  };
248 
249  bool isValid() const {
250  return (nsource > 0);
251  };
252 
253  void setSimulationChunk(EGS_I64 nstart, EGS_I64 nrun) {
254  for (int j=0; j<nsource; j++) {
255  sources[j]->setSimulationChunk(nstart, nrun);
256  }
257  };
258 
259 protected:
260 
261  int nsource;
264  EGS_I64 *last_cases;
265  EGS_Float *p;
266  EGS_Float Emax;
267  EGS_I64 count;
268 
269  void setUp(const vector<EGS_BaseSource *> &S, const vector<EGS_Float> &);
270 
271 };
272 
273 #endif
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.
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_AliasTable class header file.
EGS_I64 count
Independent particles delivered.
EGS_Float Emax
Maximum energy (max of s[j]-&gt;getEmax()).
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
virtual bool addState(istream &data_in)
Add data from the stream data_in to the source state.
static void deleteObject(EGS_Object *o)
Delete an object.
virtual void resetCounter()
Reset the source state.
virtual bool storeState(ostream &data_out) const
Store the source state into the stream data_out.
EGS_SimpleAliasTable * table
Alias table for randomly picking a source.
Base random number generator class. All random number generators should be derived from this class...
Definition: egs_rndm.h:67
EGS_RandomGenerator class header file.
EGS_SourceCollection(const vector< EGS_BaseSource * > &S, const vector< EGS_Float > &prob, const string &Name="", EGS_ObjectFactory *f=0)
Constructor.
virtual bool setState(istream &data_in)
Set the source state based on data from the stream data_in.
A class for sampling random bins from a given probability distribution using the alias table techniqu...
An object factory.
EGS_BaseSource ** sources
The sources in the collection.
virtual EGS_Float getFluence() const =0
Return the fluence this source has emitted so far.
EGS_I64 * last_cases
Last case returned from each source.
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
EGS_Float * p
The probabilities.
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 EGS_Float getEmax() const =0
Return the maximum energy of this source.
Base source class. All particle sources must be derived from this class.
A source collection.