EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_phsp_source.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ phase-space 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: Frederic Tessier
27 # Reid Townson
28 #
29 ###############################################################################
30 */
31 
32 
38 #ifndef EGS_PHSP_SOURCE_
39 #define EGS_PHSP_SOURCE_
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 #include <fstream>
47 using namespace std;
48 
49 #ifdef WIN32
50 
51  #ifdef BUILD_PHSP_SOURCE_DLL
52  #define EGS_PHSP_SOURCE_EXPORT __declspec(dllexport)
53  #else
54  #define EGS_PHSP_SOURCE_EXPORT __declspec(dllimport)
55  #endif
56  #define EGS_PHSP_SOURCE_LOCAL
57 
58 #else
59 
60  #ifdef HAVE_VISIBILITY
61  #define EGS_PHSP_SOURCE_EXPORT __attribute__ ((visibility ("default")))
62  #define EGS_PHSP_SOURCE_LOCAL __attribute__ ((visibility ("hidden")))
63  #else
64  #define EGS_PHSP_SOURCE_EXPORT
65  #define EGS_PHSP_SOURCE_LOCAL
66  #endif
67 
68 #endif
69 
127 class EGS_PHSP_SOURCE_EXPORT EGS_PhspSource : public EGS_BaseSource {
128 
129 public:
130 
136  EGS_PhspSource(const string &phsp_file,
137  const string &Name="", EGS_ObjectFactory *f=0);
138 
144  ~EGS_PhspSource() { };
145 
146  EGS_I64 getNextParticle(EGS_RandomGenerator *rndm,
147  int &q, int &latch, EGS_Float &E, EGS_Float &wt,
148  EGS_Vector &x, EGS_Vector &u);
149  void setSimulationChunk(EGS_I64 nstart, EGS_I64 nrun);
150  EGS_Float getEmax() const {
151  return Emax;
152  };
153  EGS_Float getFluence() const {
154  double aux = ((double) Nread)/((double) Nparticle);
155  return Pinc*aux;
156  };
157  bool storeState(ostream &data) const {
158  data << endl;
159  bool res = egsStoreI64(data,Nread);
160  if (!res) {
161  return res;
162  }
163  data << " ";
164  res = egsStoreI64(data,Nfirst);
165  if (!res) {
166  return res;
167  }
168  data << " ";
169  res = egsStoreI64(data,Nlast);
170  if (!res) {
171  return res;
172  }
173  data << " ";
174  res = egsStoreI64(data,Npos);
175  if (!res) {
176  return res;
177  }
178  data << " ";
179  res = egsStoreI64(data,count);
180  if (!res) {
181  return res;
182  }
183  data << " ";
184  return res;
185  };
186  bool setState(istream &data) {
187  first = false;
188  bool res = egsGetI64(data,Nread);
189  if (!res) {
190  return res;
191  }
192  res = egsGetI64(data,Nfirst);
193  if (!res) {
194  return res;
195  }
196  res = egsGetI64(data,Nlast);
197  if (!res) {
198  return res;
199  }
200  res = egsGetI64(data,Npos);
201  if (!res) {
202  return res;
203  }
204  the_file.seekg((Npos+1)*recl,ios::beg);
205  res = egsGetI64(data,count);
206  return res;
207  };
208  bool addState(istream &data) {
209  EGS_I64 tmp_Nread = Nread, tmp_count = count;
210  bool res = setState(data);
211  Nread += tmp_Nread;
212  count += tmp_count;
213  return res;
214  };
215  void resetCounter() {
216  Nread = 0;
217  count = 0;
218  };
219 
220  bool isValid() const {
221  return is_valid;
222  };
223 
224  void setCutout(EGS_Float xmin, EGS_Float xmax, EGS_Float ymin,
225  EGS_Float ymax) {
226  Xmin = xmin;
227  Xmax = xmax;
228  Ymin = ymin;
229  Ymax = ymax;
230  };
231  void setFilter(int, int, int, const int *);
232 
233 protected:
234 
235  bool is_valid;
236  string the_file_name;
237  ifstream the_file;
238  int recl;
239  bool mode2;
240  bool swap_bytes;
242  char *record;
243  EGS_Float Emax,
244  Emin,
245  Pinc;
246  EGS_I64 Nparticle,
247  Nphoton,
248  Nread,
249  Npos,
250  Nfirst,
251  Nlast,
252  count;
254  int Nrestart;
257  int Nrecycle;
258  int Nuse;
259 
260  bool first;
261 
262  // filters
263  int particle_type;
264  int filter_type;
265  unsigned long filter1, filter2;
266  EGS_Float Xmin, Xmax, Ymin, Ymax;
267  EGS_Float wmin, wmax; // weight window
268 
269  void openFile(const string &);
270  void init();
271 
272 #ifndef SKIP_DOXYGEN
273  struct EGS_LOCAL BeamParticle {
274  int latch, q;
275  float E, u, v, x, y, wt;
276  };
277  BeamParticle p;
278 #endif
279 
280  inline void readParticle();
281  inline bool rejectParticle() const;
282 
283 };
284 
285 #endif
ifstream the_file
Phase space data stream.
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_AliasTable class header file.
EGS_BaseSource class header file.
EGS_Float Pinc
Number of incident particles that created the file.
int Nrestart
Number of times the file was restarted.
char * record
Memory to read a particle into.
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
bool mode2
true, if a MODE2 file
EGS_I64 Nread
Number of particles read so far.
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.
An object factory.
string the_file_name
The phase-space file name.
int Nrecycle
Number of times to recycle current particle.
A phase-space file source.
int Nrecycle_g
Number of times to recycle a photon.
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
int Nrecycle_e
Number of times to recycle a charged particle.
int recl
The particle record length.
int Nuse
Number of times current particle was used so far.
Base source class. All particle sources must be derived from this class.