39 #include "iaea_phsp.h"
50 void IAEA_PhspSource::init() {
51 otype =
"IAEA_PhspSource";
77 void IAEA_PhspSource::openFile(
const string &phsp_file) {
86 int len = phsp_file.length();
87 char phsp_name_tmp[len+1];
88 phsp_file.copy(phsp_name_tmp,len,0);
90 phsp_name_tmp[len]=
'\0';
95 egsWarning(
"IAEA_PhspSource::openFile: no header file name supplied.\n");
99 egsWarning(
"IAEA_PhspSource::openFile: failed to open header file %s.IAEAheader\n"
100 " for reading\n",phsp_file.c_str());
104 egsWarning(
"IAEA_PhspSource::openFile: failed to open phase space file %s.IAEAphsp\n"
105 " for reading\n",phsp_file.c_str());
109 egsWarning(
"IAEA_PhspSource::openFile: failed to initialize phase space source %s\n",phsp_file.c_str());
113 egsWarning(
"IAEA_PhspSource::openFile: failed to get record contents from header file %s.IAEAheader\n",phsp_file.c_str());
117 egsWarning(
"IAEA_PhspSource::openFile: I/O error ocurred on opening phase space file %s\n",phsp_file.c_str());
126 egsWarning(
"IAEA_PhspSource::openFile: error reading file size/byte order from header %s.IAEAheader\n",phsp_file.c_str());
130 egsWarning(
"IAEA_PhspSource::openFile: byte mismatch between phase space file and machine. Will swap bytes.\n");
134 egsWarning(
"IAEA_PhspSource::openFile: mismatch between file size in header and actual file size of %s\n",phsp_file.c_str());
139 EGS_I64 n, n_photon, pinc;
142 iaea_get_max_particles(&
iaea_fileid,&iaea_type,&n);
144 egsWarning(
"IAEA_PhspSource::openFile: failed to get total no. of particles from %s.IAEAheader\n",phsp_file.c_str());
148 iaea_get_max_particles(&
iaea_fileid,&iaea_type,&n_photon);
150 egsWarning(
"IAEA_PhspSource::openFile: failed to get no. of photons from %s.IAEAheader\n",phsp_file.c_str());
156 egsWarning(
"IAEA_PhspSource::openFile: failed to get max. energy from %s.IAEAheader\n",phsp_file.c_str());
160 iaea_get_total_original_particles(&
iaea_fileid,&pinc);
162 egsWarning(
"IAEA_PhspSource::openFile: failed to get no. of original particles from %s.IAEAheader\n",phsp_file.c_str());
169 egsWarning(
"IAEA_PhspSource::openFile: failed to get no. of extra floats and longs stored in %s.IAEAphsp\n",phsp_file.c_str());
173 int extrafloat_types[MAXEXTRAS], extralong_types[MAXEXTRAS];
176 egsWarning(
"IAEA_PhspSource::openFile: failed to get Mode of data %s.IAEAheader\n",phsp_file.c_str());
182 if (extrafloat_types[i]==3) {
194 if (extrafloat_types[
i_zlast+1] == 0) {
197 egsInformation(
"IAEA_PhspSource::openFile: Time index included in data in %s.IAEAphsp\n",phsp_file.c_str());
204 if (extralong_types[i]==2) {
211 egsWarning(
"IAEA_PhspSource::openFile: LATCH is not stored in data in %s.IAEAphsp\n",phsp_file.c_str());
230 int err = input->
getInput(
"iaea phase space file",fname);
232 egsWarning(
"IAEA_PhspSource: no 'iaea phase space file' input\n");
237 egsWarning(
"IAEA_PhspSource: errors while opening the phase space file"
238 " %s\n",fname.c_str());
241 vector<EGS_Float> cutout;
242 err = input->
getInput(
"cutout",cutout);
243 if (!err && cutout.size() == 4) {
244 setCutout(cutout[0],cutout[1],cutout[2],cutout[3]);
246 vector<string> ptype;
247 ptype.push_back(
"electrons");
248 ptype.push_back(
"photons");
249 ptype.push_back(
"positrons");
250 ptype.push_back(
"all");
251 ptype.push_back(
"charged");
252 particle_type = input->
getInput(
"particle type",ptype,3)-1;
253 vector<int> the_filter;
254 vector<string> the_latch;
255 err = input->
getInput(
"filter type",the_filter);
256 int err1 = input->
getInput(
"latch setting",the_latch);
258 if (the_filter[0] >= 0 && the_filter[0] <= 3) {
259 int nbit1 = the_latch[0].size();
261 if (!the_filter[0]) {
262 nbit2 = the_latch[1].size();
264 if (nbit1 + nbit2 > 0) {
265 int *the_bits =
new int [nbit1+nbit2];
266 for (
int j=0; j<nbit1+nbit2; j++) {
268 the_bits[j]=(int)(the_latch[0][j] ==
'1');
271 the_bits[j] = (int)(the_latch[1][j-nbit1] ==
'1');
274 setFilter(the_filter[0],nbit1,nbit2,the_bits);
279 vector<EGS_Float> wwindow;
280 err = input->
getInput(
"weight window",wwindow);
281 if (!err && wwindow.size() == 2) {
286 err = input->
getInput(
"reuse photons",ntmp);
287 if (!err && ntmp > 0) {
291 err = input->
getInput(
"recycle photons",ntmp);
292 if (!err && ntmp > 0) {
296 err = input->
getInput(
"reuse electrons",ntmp);
297 if (!err && ntmp > 0) {
301 err = input->
getInput(
"recycle electrons",ntmp);
302 if (!err && ntmp > 0) {
317 int nstat,extrainttemp[MAXEXTRAS];
318 float extrafloattemp[MAXEXTRAS];
321 egsWarning(
"IAEA_PhspSource::getNextParticle(): reached the end of the "
322 "phase space file chunk (%lld)\n will start from the beginning "
323 "of the chunk (%lld) but this "
324 "implies that uncertainty estimates will be inaccurate\n",
328 egsFatal(
"IAEA_PhspSource::getNextParticle(): error restarting phase space chunk\n");
333 iaea_get_particle(&
iaea_fileid,&nstat,&p.q,&p.E,&p.wt,&p.x,&p.y,&p.z,&p.u,&p.v,&p.w,extrafloattemp,extrainttemp);
342 p.zlast = extrafloattemp[
i_zlast];
345 p.time = extrafloattemp[
i_time];
346 setTimeIndex(p.time);
370 egsFatal(
"IAEA_PhspSource::getNextParticle(): error reading particle number %i\n",
Npos);
383 egsFatal(
"IAEA_PhspSource::getNextParticle: unknown charge on particle\n");
386 if (first || nstat>0) {
407 u.
y = p.v, u.
z = p.w;
409 if (rejectParticle()) {
433 void IAEA_PhspSource::setSimulationChunk(EGS_I64 nstart, EGS_I64 nrun,
int npar,
int nchunk) {
436 EGS_I64 particlesPerChunk =
Nparticle/(npar*nchunk);
437 int ichunk = nstart/nrun;
438 if (ichunk > npar*nchunk-1) {
441 egsInformation(
"IAEA_PhspSource: Remainder of histories will reuse the last chunk of the phase space source.\n");
442 ichunk = npar*nchunk-1;
444 Nfirst = ichunk*particlesPerChunk+1;
445 if (ichunk == npar*nchunk-1) {
455 egsWarning(
"IAEA_PhspSource::setSimulationChunk(): error setting phase space chunk\n");
458 egsInformation(
"IAEA_PhspSource: using phsp portion between %lld and %lld\n",
462 bool IAEA_PhspSource::rejectParticle()
const {
463 if (particle_type < 2 && p.q != particle_type) {
466 if (particle_type == 3 && !p.q) {
469 if (p.x < Xmin || p.x > Xmax || p.y < Ymin || p.y > Ymax) {
472 if (p.wt < wmin || p.wt > wmax) {
475 if (p.latch & 2147483648UL) {
478 if (filter_type < 0) {
481 if (filter_type == 1) {
484 r1 = !(p.latch & filter1);
489 bool r2 = (p.latch & filter2);
492 if (filter_type == 1) {
493 return (p.latch & filter1);
495 int l = p.latch >> 24;
496 bool res = l & filter1;
497 return filter_type == 3 ? res : !res;
500 void IAEA_PhspSource::setFilter(
int type,
int nbit1,
int nbit2,
const int *bits) {
501 if (type < 0 || type > 3) {
502 egsWarning(
"IAEA_PhspSource::setFilter: invalid filter type %d\n",type);
510 egsWarning(
"IAEA_PhspSource::setFilter: maximum number of bits is "
511 "limited to 29, you requested %d\n",ntot);
518 if (filter_type == 0) {
522 for (i=0; i<ntot; i++) {
524 unsigned int aux = 1;
535 else if (filter_type == 1) {
537 else if (filter_type == 2) {
539 else if (filter_type == 3) {
557 createSourceTemplate<IAEA_PhspSource>(input,f,
"iaea phsp source");
Base source class. All particle sources must be derived from this class.
string description
A short source description.
string otype
The object type.
Base random number generator class. All random number generators should be derived from this class.
A class representing 3D vectors.
EGS_I64 Npos
Next record to be read.
int Nrecycle_g
Number of times to recycle a photon.
string the_file_name
The phase-space file name.
EGS_I64 Nlast
Last record this source can use.
EGS_I64 Nphoton
Number of photons in the file.
int Nrestart
Number of times the file was restarted.
int i_latch
index of latch in extra_floats array
int Nuse
Number of times current particle was used so far.
EGS_I64 Nread
Number of particles read so far.
EGS_I64 Nfirst
first record this source can use
int Nrecycle
Number of times to recycle current particle.
bool time_stored
true if time index stored
int iaea_fileid
phsp file unit no.
int n_extra_floats
no. of extra floats stored in phsp file
int i_zlast
index of zlast in extra_floats array
int i_time
index of time index in extra_floats array
EGS_Float Pinc
Number of incident particles that created the file.
EGS_I64 Nparticle
Number of particles in the file.
bool mode2
true, if a MODE2 file (i.e. storing Zlast)
int n_extra_longs
no. of extra longs stored in phsp file
IAEA_PhspSource(const string &phsp_file, const string &Name="", EGS_ObjectFactory *f=0)
Constructor.
int Nrecycle_e
Number of times to recycle a charged particle.
bool latch_stored
true if LATCH is stored in data
EGS_Float Emax
Maximum k.e. (obtained from the phsp file)
int iaea_iostat
iostat on read/write of iaea phsp file
void containsDynamic(bool &hasdynamic)
Check if the simulation source contains time indices.
Global egspp functions header file.
EGS_InfoFunction EGS_EXPORT egsInformation
Always use this function for reporting the progress of a simulation and any other type of information...
EGS_InfoFunction EGS_EXPORT egsFatal
Always use this function for reporting fatal errors.
void egsSwapBytes(int *n)
Swap the bytes of 32 bit integers.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.
const EGS_Float veryFar
A very large float.
An IAEA format phase-space file source.