Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-18 07:41:08

0001 /*
0002  * Copyright (C) 2006 International Atomic Energy Agency
0003  * -----------------------------------------------------------------------------
0004  *
0005  * Permission is hereby granted, free of charge, to any person obtaining a copy
0006  * of this software and associated documentation files (the "Software"), to deal
0007  * in the Software without restriction, including without limitation the rights
0008  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0009  * copies of the Software, and to permit persons to whom the Software is furnished
0010  * to do so, subject to the following conditions:
0011  *
0012  * The above copyright notice and this permission notice shall be included in all
0013  * copies or substantial portions of the Software.
0014  *
0015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0018  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0021  * THE SOFTWARE.
0022  *
0023  *-----------------------------------------------------------------------------
0024  *
0025  *   AUTHORS:
0026  *
0027  *   Roberto Capote Noy, PhD
0028  *   e-mail: R.CapoteNoy@iaea.org (rcapotenoy@yahoo.com)
0029  *   International Atomic Energy Agency
0030  *   Nuclear Data Section, P.O.Box 100
0031  *   Wagramerstrasse 5, Vienna A-1400, AUSTRIA
0032  *   Phone: +431-260021713; Fax: +431-26007
0033  *
0034  *   Iwan Kawrakow, PhD
0035  *   e-mail iwan@irs.phy.nrc.ca
0036  *   Ionizing Radiation Standards
0037  *   Institute for National Measurement Standards
0038  *   National Research Council of Canada Ottawa, ON, K1A 0R6 Canada
0039  *   Phone: +1-613-993 2197, ext.241; Fax: +1-613-952 9865
0040  *
0041  **********************************************************************************
0042  * For documentation
0043  * see http://www-nds.iaea.org/reports-new/indc-reports/indc-nds/indc-nds-0484.pdf
0044  **********************************************************************************/
0045 //
0046 #define MAIN
0047 
0048 #if (defined WIN32) || (defined WIN64)
0049 #include <iostream>  // so that namespace std becomes defined
0050 #endif
0051 #include <cstdio>
0052 #include <cstdlib>
0053 #include <cstring>
0054 #include <cmath>
0055 #include <cctype>
0056 #include <algorithm>  // for max() and min()
0057 
0058 #include<sys/types.h>
0059 #include<sys/stat.h>
0060 
0061 #if !(defined WIN32) && !(defined WIN64)
0062 using namespace std;
0063 #endif
0064 
0065 #include "utilities.h"
0066 #include "iaea_record.h"
0067 #include "iaea_header.h"
0068 #include "iaea_phsp.h"
0069 
0070 //#define false 0  //(MACG) to silent -Wkeyword-macro build warning
0071 //#define true  1  //(MACG) to silent -Wkeyword-macro build warning
0072 
0073 // These variables are defined globally. They contain pointers
0074 // to header and record structures defined by calling iaea_new_source()
0075 // routine to maintain a list of already initialized IAEA sources.
0076 
0077 static iaea_header_type *p_iaea_header[MAX_NUM_SOURCES];
0078 static iaea_record_type *p_iaea_record[MAX_NUM_SOURCES];
0079 
0080 /************************************************************************
0081 * Initialization
0082 *
0083 * Given a file name header_file of length hf_length, initialize a
0084 * new IAEA particle source, assign an unique Id to it and return this
0085 * Id in result. Dont assume header_file is null-terminated as the
0086 * function may be called from a Fortran program.
0087 * The need for an Id arises from the fact that some applications may
0088 * want to use several IAEA sources at once. The implementation must therefore
0089 * maintain a list of already initialized sources.
0090 * If an error occures (e.g. header file does not exist, there are errors
0091 * in the header file, etc.), assign a negative number to result.
0092 * (one may want to specify a list of error codes so that the application
0093 * knows what went wrong). This function *must* be called before using
0094 * any of the following functions for a given source id.
0095 *
0096 * access = 1 => opening read-only file
0097 * access = 2 => opening file for writing
0098 * access = 3 => opening file for appending/updating
0099 *
0100 ***********************************************************************/
0101 
0102 static int __iaea_source_used[MAX_NUM_SOURCES];
0103 static int __iaea_n_source = 0;
0104 
0105 IAEA_EXTERN_C IAEA_EXPORT
0106 void iaea_new_source(IAEA_I32 *source_ID, char *header_file,
0107                      const IAEA_I32 *access, IAEA_I32 *result,
0108                      int hf_length) {
0109 
0110    if( !header_file ) {
0111        *result = 105; *source_ID = -1; return;
0112    } // null header file name
0113    if(*access != 1 && *access != 2 && *access != 3) {
0114        *result = -99 ; *source_ID = -1; return;
0115    } // Wrong access requested
0116 
0117    if( hf_length >= MAX_STR_LEN) {
0118        *result = -100 ; *source_ID = -1; return;
0119    } // Too long string
0120    if( hf_length < 1)            {
0121        *result = -101 ; *source_ID = -1; return;
0122    } // String length < 1
0123 
0124    if( !__iaea_n_source ) { // called for the first time
0125        for(int j=0; j<MAX_NUM_SOURCES; j++) __iaea_source_used[j] = false;
0126    }
0127    int sid=-1;
0128    // do we have a spare spot in the arrays ?
0129    // (e.g. because a source was destroyed)
0130    for(int j=0; j<__iaea_n_source; j++) {
0131        if( !__iaea_source_used[j] ) { sid = j; break; }
0132    }
0133    if( sid < 0 ) {
0134        // so, we don't => increase source count and check if
0135        // space left in arrays.
0136        if( ++__iaea_n_source >= MAX_NUM_SOURCES ) {
0137            *result = -98; *source_ID = -1; return;
0138        }
0139        sid = __iaea_n_source-1; *source_ID = sid;
0140    }
0141    __iaea_source_used[sid] = true;
0142 
0143    //int ilen = strlen(header_file);
0144    // the above requires a null-terminated string, which may not be
0145    // satisfied, if we are called from a fortran progrmam.
0146 
0147    int ilen = hf_length;
0148    if( header_file[hf_length-1] != 0 ) { // not null-terminated
0149        while(ilen > 0 && isspace(header_file[--ilen]) );
0150        if( ilen < hf_length-1 ) header_file[ilen+1] = '\0';
0151    }
0152 
0153    // Creating IAEA phsp header and allocating memory for it
0154    p_iaea_header[*source_ID] = (iaea_header_type *) calloc(1, sizeof(iaea_header_type));
0155    // Opening header file
0156    if(*access == 1) p_iaea_header[*source_ID]->fheader =
0157          open_file(header_file,".IAEAheader","rb");
0158    if(*access == 2) p_iaea_header[*source_ID]->fheader =
0159          open_file(header_file,".IAEAheader","wb");
0160    if(*access == 3) p_iaea_header[*source_ID]->fheader =
0161          open_file(header_file,".IAEAheader","r+b");
0162 
0163    if(p_iaea_header[*source_ID]->fheader == NULL) { *result = -96; return;} // phsp failed to open
0164 
0165    // Creating IAEA record and allocating memory for it
0166    p_iaea_record[*source_ID] = (iaea_record_type *) calloc(1, sizeof(iaea_record_type));
0167 
0168    p_iaea_header[*source_ID]->initialize_counters();
0169 
0170    switch( *access )
0171    {
0172          case 2: // writing a new phsp
0173 
0174              strcpy(p_iaea_header[*source_ID]->title,"PHASESPACE in IAEA format");
0175              // Default IAEA index
0176              *result = p_iaea_header[*source_ID]->iaea_index = 1000;
0177 
0178              p_iaea_record[*source_ID]->p_file =
0179                  open_file(header_file, ".IAEAphsp", "wb");
0180 
0181              if(p_iaea_record[*source_ID]->p_file == NULL) { *result = -94 ; return; }
0182 
0183              // Setting default i/o flags
0184              if(p_iaea_record[*source_ID]->initialize() != OK)
0185                  {*result = -1; return;}
0186 
0187              if( p_iaea_header[*source_ID]->set_record_contents(p_iaea_record[*source_ID])
0188                  == FAIL ) { *result = -95; return;}
0189 
0190              return;
0191 
0192          case 3 : // appending to the existing phsp
0193 
0194              if( p_iaea_header[*source_ID]->read_header() != OK)
0195                  { *result = -93; return;}
0196 
0197              int i;
0198              // Setting up Average Kinetic Energy counters to usable values
0199              for(i=0;i<MAX_NUM_PARTICLES;i++)
0200                  p_iaea_header[*source_ID]->averageKineticEnergy[i] *=
0201                  p_iaea_header[*source_ID]->sumParticleWeight[i];
0202 
0203              // Opening phsp file to append
0204              p_iaea_record[*source_ID]->p_file =
0205                  open_file(header_file, ".IAEAphsp", "a+b");
0206 
0207              if(p_iaea_record[*source_ID]->p_file == NULL) { *result = -94 ; return; }
0208 
0209              if(p_iaea_record[*source_ID]->initialize() != OK) {*result = -1; return;}
0210 
0211              // Get read/write logical block from the header
0212              if( p_iaea_header[*source_ID]->get_record_contents(p_iaea_record[*source_ID])
0213                  == FAIL) { *result = -91; return;}
0214 
0215              *result = p_iaea_header[*source_ID]->iaea_index; // returning IAEA index
0216 
0217              break;
0218 
0219          case 1 : // reading existing phsp
0220 
0221              if( p_iaea_header[*source_ID]->read_header() != OK) { *result = -93; return;}
0222 
0223              // Opening phsp file to read
0224              p_iaea_record[*source_ID]->p_file =
0225                  open_file(header_file, ".IAEAphsp", "rb");
0226 
0227              if(p_iaea_record[*source_ID]->p_file == NULL)
0228                  { *result = -94 ; return; }
0229 
0230              if(p_iaea_record[*source_ID]->initialize() != OK) {*result = -1; return;}
0231 
0232              // Get read/write logical block from the header
0233              if( p_iaea_header[*source_ID]->get_record_contents(p_iaea_record[*source_ID])
0234                  == FAIL) { *result = -91; return;}
0235 
0236              *result = p_iaea_header[*source_ID]->iaea_index; // returning IAEA index
0237 
0238              break;
0239    }
0240 
0241    return;
0242 }
0243 
0244 IAEA_EXTERN_C IAEA_EXPORT
0245 void iaea_new_source_(IAEA_I32 *source_ID, char *header_file,
0246                       const IAEA_I32 *access, IAEA_I32 *result,
0247                       int hf_length) {
0248     iaea_new_source(source_ID,header_file,access,result,hf_length);
0249 }
0250 IAEA_EXTERN_C IAEA_EXPORT
0251 void iaea_new_source__(IAEA_I32 *source_ID, char *header_file,
0252                       const IAEA_I32 *access, IAEA_I32 *result,
0253                       IAEA_I32 hf_length) {
0254     iaea_new_source(source_ID,header_file,access,result,hf_length);
0255 }
0256 IAEA_EXTERN_C IAEA_EXPORT
0257 void IAEA_NEW_SOURCE(IAEA_I32 *source_ID, char *header_file,
0258                       const IAEA_I32 *access, IAEA_I32 *result,
0259                       IAEA_I32 hf_length) {
0260     iaea_new_source(source_ID,header_file,access,result,hf_length);
0261 }
0262 IAEA_EXTERN_C IAEA_EXPORT
0263 void IAEA_NEW_SOURCE_(IAEA_I32 *source_ID, char *header_file,
0264                       const IAEA_I32 *access, IAEA_I32 *result,
0265                       IAEA_I32 hf_length) {
0266     iaea_new_source(source_ID,header_file,access,result,hf_length);
0267 }
0268 IAEA_EXTERN_C IAEA_EXPORT
0269 void IAEA_NEW_SOURCE__(IAEA_I32 *source_ID, char *header_file,
0270                       const IAEA_I32 *access, IAEA_I32 *result,
0271                       IAEA_I32 hf_length) {
0272     iaea_new_source(source_ID,header_file,access,result,hf_length);
0273 }
0274 
0275 /************************************************************************
0276 * Maximum number of particles
0277 *
0278 * Set n_particle to the maximum number of particle of type type the
0279 * source with Id id can return. If type<0, set n_particle to the
0280 * total number of all particles. For event generators, this function
0281 * should set n_particle to the maximum integer that can be stored in a
0282 * signed 64 bit integer. If the source with Id id does not exist,
0283 * set n_particle to a negative number.
0284 *************************************************************************/
0285 IAEA_EXTERN_C IAEA_EXPORT
0286 void iaea_get_max_particles(const IAEA_I32 *id, const IAEA_I32 *type,
0287                                   IAEA_I64 *n_particle)
0288 {
0289       // No header found
0290       if(p_iaea_header[*id]->fheader == NULL) {*n_particle = -1; return;}
0291 
0292       int file_type = p_iaea_header[*id]->file_type;
0293 
0294       if(file_type == 1) // Event generator
0295       {
0296         if( sizeof(IAEA_I64) >= 8 ) {
0297                 #if (defined WIN32) || (defined WIN64)
0298                 *n_particle = 9223372036854775807;   // 2^63 - 1
0299                 #else
0300                 *n_particle = 9223372036854775807LL; // 2^63 - 1
0301                 #endif
0302         }
0303         else *n_particle = 2147483647;           // 2^31 - 1
0304             return;
0305       }
0306       // phsp file
0307       if (*type < 0) {*n_particle = p_iaea_header[*id]->nParticles; return;}
0308       if ( (*type >= MAX_NUM_PARTICLES) || (*type ==0) ) {*n_particle = 0; return;}
0309 
0310       *n_particle = p_iaea_header[*id]->particle_number[*type-1];
0311 
0312       return;
0313 }
0314 IAEA_EXTERN_C IAEA_EXPORT
0315 void iaea_get_max_particles_(const IAEA_I32 *id,
0316                                            const IAEA_I32 *type,
0317                                                  IAEA_I64 *n_particle)
0318 { iaea_get_max_particles(id, type, n_particle); }
0319 IAEA_EXTERN_C IAEA_EXPORT
0320 void iaea_get_max_particles__(const IAEA_I32 *id,
0321                                            const IAEA_I32 *type,
0322                                                  IAEA_I64 *n_particle)
0323 { iaea_get_max_particles(id, type, n_particle); }
0324 IAEA_EXTERN_C IAEA_EXPORT
0325 void IAEA_GET_MAX_PARTICLES(const IAEA_I32 *id,
0326                                            const IAEA_I32 *type,
0327                                                  IAEA_I64 *n_particle)
0328 { iaea_get_max_particles(id, type, n_particle); }
0329 IAEA_EXTERN_C IAEA_EXPORT
0330 void IAEA_GET_MAX_PARTICLES_(const IAEA_I32 *id,
0331                                            const IAEA_I32 *type,
0332                                                  IAEA_I64 *n_particle)
0333 { iaea_get_max_particles(id, type, n_particle); }
0334 IAEA_EXTERN_C IAEA_EXPORT
0335 void IAEA_GET_MAX_PARTICLES__(const IAEA_I32 *id,
0336                                            const IAEA_I32 *type,
0337                                                  IAEA_I64 *n_particle)
0338 { iaea_get_max_particles(id, type, n_particle); }
0339 
0340 /************************************************************************
0341 * Maximum energy
0342 *
0343 * Return the maximum energy of an initialized IAEA source with Id id
0344 * in Emax. Set Emax to negative if a source with that Id does not exist.
0345 ************************************************************************/
0346 IAEA_EXTERN_C IAEA_EXPORT
0347 void iaea_get_maximum_energy(const IAEA_I32 *id, IAEA_Float *Emax)
0348 {
0349       // No header found
0350       if(p_iaea_header[*id]->fheader == NULL) {*Emax = -1.f; return;}
0351 
0352       int file_type = p_iaea_header[*id]->file_type;
0353 
0354       if(file_type == 1) {*Emax = -1.f; return;}// Event generator
0355 
0356       // phsp file
0357       *Emax = 0.f;
0358       for(int i=0;i<MAX_NUM_PARTICLES;i++)
0359       {
0360         // *Emax = (IAEA_Float) max(*Emax,p_iaea_header[*id]->maximumKineticEnergy[i]);
0361     //(MACG) using std::max from algorithm libraries
0362         *Emax = (IAEA_Float) max(*Emax,
0363                  (IAEA_Float)p_iaea_header[*id]->maximumKineticEnergy[i]);
0364       }
0365       return;
0366 }
0367 IAEA_EXTERN_C IAEA_EXPORT
0368 void iaea_get_maximum_energy_(const IAEA_I32 *id, IAEA_Float *Emax)
0369 { iaea_get_maximum_energy(id, Emax); }
0370 IAEA_EXTERN_C IAEA_EXPORT
0371 void iaea_get_maximum_energy__(const IAEA_I32 *id, IAEA_Float *Emax)
0372 { iaea_get_maximum_energy(id, Emax); }
0373 IAEA_EXTERN_C IAEA_EXPORT
0374 void IAEA_GET_MAXIMUM_ENERGY(const IAEA_I32 *id, IAEA_Float *Emax)
0375 { iaea_get_maximum_energy(id, Emax); }
0376 IAEA_EXTERN_C IAEA_EXPORT
0377 void IAEA_GET_MAXIMUM_ENERGY_(const IAEA_I32 *id, IAEA_Float *Emax)
0378 { iaea_get_maximum_energy(id, Emax); }
0379 IAEA_EXTERN_C IAEA_EXPORT
0380 void IAEA_GET_MAXIMUM_ENERGY__(const IAEA_I32 *id, IAEA_Float *Emax)
0381 { iaea_get_maximum_energy(id, Emax); }
0382 
0383 /*************************************************************************
0384 * Number of additional floats and integers returned by the source
0385 *
0386 * Return the number of additional floats in n_extra_float and the number
0387 * of additional integers in n_extra_integer for the source with Id id.
0388 * Set n_extra_integer and/or n_extra_float to be negative if such a
0389 * source does not exist.
0390 *************************************************************************/
0391 IAEA_EXTERN_C IAEA_EXPORT
0392 void iaea_get_extra_numbers(const IAEA_I32 *id, IAEA_I32 *n_extra_float,
0393                                           IAEA_I32 *n_extra_int)
0394 {
0395       // No header found
0396       if(p_iaea_header[*id]->fheader == NULL)
0397             {*n_extra_float = *n_extra_int = -1; return;}
0398 
0399       *n_extra_float = p_iaea_header[*id]->record_contents[7];
0400       *n_extra_int   = p_iaea_header[*id]->record_contents[8];
0401       return;
0402 }
0403 IAEA_EXTERN_C IAEA_EXPORT
0404 void iaea_get_extra_numbers_(const IAEA_I32 *id,
0405                                                  IAEA_I32 *n_extra_float,
0406                                                  IAEA_I32 *n_extra_int)
0407 { iaea_get_extra_numbers(id, n_extra_float,n_extra_int); }
0408 IAEA_EXTERN_C IAEA_EXPORT
0409 void iaea_get_extra_numbers__(const IAEA_I32 *id,
0410                                                  IAEA_I32 *n_extra_float,
0411                                                  IAEA_I32 *n_extra_int)
0412 { iaea_get_extra_numbers(id, n_extra_float,n_extra_int); }
0413 IAEA_EXTERN_C IAEA_EXPORT
0414 void IAEA_GET_EXTRA_NUMBERS(const IAEA_I32 *id,
0415                                                  IAEA_I32 *n_extra_float,
0416                                                  IAEA_I32 *n_extra_int)
0417 { iaea_get_extra_numbers(id, n_extra_float,n_extra_int); }
0418 IAEA_EXTERN_C IAEA_EXPORT
0419 void IAEA_GET_EXTRA_NUMBERS_(const IAEA_I32 *id,
0420                                                  IAEA_I32 *n_extra_float,
0421                                                  IAEA_I32 *n_extra_int)
0422 { iaea_get_extra_numbers(id, n_extra_float,n_extra_int); }
0423 IAEA_EXTERN_C IAEA_EXPORT
0424 void IAEA_GET_EXTRA_NUMBERS__(const IAEA_I32 *id,
0425                                                  IAEA_I32 *n_extra_float,
0426                                                  IAEA_I32 *n_extra_int)
0427 { iaea_get_extra_numbers(id, n_extra_float,n_extra_int); }
0428 
0429 /*************************************************************************
0430 * Number of additional floats and integers to be stored
0431 *
0432 * Set the number of additional floats in n_extra_float and the number
0433 * of additional integers in n_extra_integer for the source with Id id
0434 * to be stored in the corresponding file.
0435 *************************************************************************/
0436 IAEA_EXTERN_C IAEA_EXPORT
0437 void iaea_set_extra_numbers(const IAEA_I32 *id, IAEA_I32 *n_extra_float,
0438                                   IAEA_I32 *n_extra_int)
0439 {
0440       // No header found
0441       if(p_iaea_header[*id]->fheader == NULL) return;
0442 
0443       p_iaea_header[*id]->record_contents[7] = *n_extra_float;
0444       p_iaea_header[*id]->record_contents[8] = *n_extra_int;
0445 
0446     // Store read/write logical block in the PHSP header
0447     if( p_iaea_header[*id]->get_record_contents(p_iaea_record[*id])
0448          == FAIL) return;
0449 
0450       return;
0451 }
0452 IAEA_EXTERN_C IAEA_EXPORT
0453 void iaea_set_extra_numbers_(const IAEA_I32 *id,
0454                                                  IAEA_I32 *n_extra_float,
0455                                                  IAEA_I32 *n_extra_int)
0456 { iaea_set_extra_numbers(id, n_extra_float, n_extra_int); }
0457 IAEA_EXTERN_C IAEA_EXPORT
0458 void iaea_set_extra_numbers__(const IAEA_I32 *id,
0459                                                  IAEA_I32 *n_extra_float,
0460                                                  IAEA_I32 *n_extra_int)
0461 { iaea_set_extra_numbers(id, n_extra_float, n_extra_int); }
0462 IAEA_EXTERN_C IAEA_EXPORT
0463 void IAEA_SET_EXTRA_NUMBERS(const IAEA_I32 *id,
0464                                                  IAEA_I32 *n_extra_float,
0465                                                  IAEA_I32 *n_extra_int)
0466 { iaea_set_extra_numbers(id, n_extra_float, n_extra_int); }
0467 IAEA_EXTERN_C IAEA_EXPORT
0468 void IAEA_SET_EXTRA_NUMBERS_(const IAEA_I32 *id,
0469                                                  IAEA_I32 *n_extra_float,
0470                                                  IAEA_I32 *n_extra_int)
0471 { iaea_set_extra_numbers(id, n_extra_float, n_extra_int); }
0472 IAEA_EXTERN_C IAEA_EXPORT
0473 void IAEA_SET_EXTRA_NUMBERS__(const IAEA_I32 *id,
0474                                                  IAEA_I32 *n_extra_float,
0475                                                  IAEA_I32 *n_extra_int)
0476 { iaea_set_extra_numbers(id, n_extra_float, n_extra_int); }
0477 
0478 
0479 /*******************************************************************************
0480 * Set a type type of the extra long variable corresponding to the "index" number
0481 * for a corresponding header of the phsp "id". Index is running from zero.
0482 *
0483 * The current list of types for extra long variables is:
0484 *   0: User defined generic type
0485 *   1: Incremental history number (EGS,PENELOPE)
0486 *      = 0 indicates a nonprimary particle event
0487 *      > 0 indicates a primary particle. The value is equal to the number of
0488 *          primaries particles employed to get to this history after the last
0489 *          primary event was recorded.
0490 *   2: LATCH (EGS)
0491 *   3: ILB5 (PENELOPE)
0492 *   4: ILB4 (PENELOPE)
0493 *   5: ILB3 (PENELOPE)
0494 *   6: ILB2 (PENELOPE)
0495 *   7: ILB1 (PENELOPE)
0496 *   more to be defined
0497 *
0498 * Usually called before writing phsp header to set the type of extra long
0499 * variables to be stored. It must be called once for every extralong variable.
0500 *
0501 * type = -1 means the source's header file does not exist
0502 *           or source was not properly initialized (call iaea_new_...)
0503 * type = -2 means the index is out of range ( 0 <= index < NUM_EXTRA_LONG )
0504 * type = -3 means the type to be set is out of range
0505 *           ( 1 <= type < MAX_NUMB_EXTRALONG_TYPES )
0506 *******************************************************************************/
0507 IAEA_EXTERN_C IAEA_EXPORT
0508 void iaea_set_type_extralong_variable(const IAEA_I32 *id,
0509                                       const IAEA_I32 *index,
0510                                             IAEA_I32 *type)
0511 {
0512    // No header found
0513    if(p_iaea_header[*id]->fheader == NULL) {*type = -1; return;}
0514 
0515    if((*index < 0) || (*index >= NUM_EXTRA_LONG) ) {*type = -2; return;}
0516 
0517    if((*type < 0) || (*type > MAX_NUMB_EXTRALONG_TYPES) ) {*type = -3; return;}
0518 
0519    p_iaea_header[*id]->extralong_contents[*index] = *type;
0520 
0521    return;
0522 }
0523 
0524 IAEA_EXTERN_C IAEA_EXPORT
0525 void iaea_set_type_extralong_variable_(const IAEA_I32 *id,
0526                                                      const IAEA_I32 *index,
0527                                                      IAEA_I32 *type)
0528 { iaea_set_type_extralong_variable(id, index, type); }
0529 
0530 IAEA_EXTERN_C IAEA_EXPORT
0531 void iaea_set_type_extralong_variable__(const IAEA_I32 *id,
0532                                                      const IAEA_I32 *index,
0533                                                      IAEA_I32 *type)
0534 { iaea_set_type_extralong_variable(id, index, type); }
0535 
0536 IAEA_EXTERN_C IAEA_EXPORT
0537 void IAEA_SET_TYPE_EXTRALONG_VARIABLE(const IAEA_I32 *id,
0538                                                      const IAEA_I32 *index,
0539                                                      IAEA_I32 *type)
0540 { iaea_set_type_extralong_variable(id, index, type); }
0541 
0542 IAEA_EXTERN_C IAEA_EXPORT
0543 void IAEA_SET_TYPE_EXTRALONG_VARIABLE_(const IAEA_I32 *id,
0544                                                      const IAEA_I32 *index,
0545                                                      IAEA_I32 *type)
0546 { iaea_set_type_extralong_variable(id, index, type); }
0547 
0548 IAEA_EXTERN_C IAEA_EXPORT
0549 void IAEA_SET_TYPE_EXTRALONG_VARIABLE__(const IAEA_I32 *id,
0550                                                      const IAEA_I32 *index,
0551                                                      IAEA_I32 *type)
0552 { iaea_set_type_extralong_variable(id, index, type); }
0553 
0554 
0555 /********************************************************************************
0556 * Set a type type of the extra float variable corresponding to the "index" number
0557 * for a corresponding header of the phsp "id". Index is running from zero.
0558 *
0559 * The current list of types for extra float variables is:
0560 *   0: User defined generic type
0561 *   1: XLAST (x coord. of the last interaction)
0562 *   2: YLAST (y coord. of the last interaction)
0563 *   3: ZLAST (z coord. of the last interaction)
0564 *   more to be defined
0565 *
0566 * Usually called before writing phsp header to set the type of extra float
0567 * variables to be stored. It must be called once for every extra float variable.
0568 *
0569 * type = -1 means the source's header file does not exist
0570 *           or source was not properly initialized (call iaea_new_...)
0571 * type = -2 means the index is out of range ( 0 <= index < NUM_EXTRA_FLOAT )
0572 * type = -3 means the type to be set is out of range
0573 *           ( 1 <= type < MAX_NUMB_EXTRAFLOAT_TYPES )
0574 *******************************************************************************/
0575 IAEA_EXTERN_C IAEA_EXPORT
0576 void iaea_set_type_extrafloat_variable(const IAEA_I32 *id,
0577                                        const IAEA_I32 *index,
0578                                              IAEA_I32 *type)
0579 {
0580    // No header found
0581    if(p_iaea_header[*id]->fheader == NULL) {*type = -1; return;}
0582 
0583    if((*index < 0) || (*index >= NUM_EXTRA_FLOAT) ) {*type = -2; return;}
0584 
0585    if((*type < 0) || (*type > MAX_NUMB_EXTRAFLOAT_TYPES) ) {*type = -3; return;}
0586 
0587    p_iaea_header[*id]->extrafloat_contents[*index] = *type;
0588 
0589    return;
0590 }
0591 
0592 IAEA_EXTERN_C IAEA_EXPORT
0593 void iaea_set_type_extrafloat_variable_(const IAEA_I32 *id,
0594                                                      const IAEA_I32 *index,
0595                                                      IAEA_I32 *type)
0596 { iaea_set_type_extrafloat_variable(id, index, type); }
0597 
0598 IAEA_EXTERN_C IAEA_EXPORT
0599 void iaea_set_type_extrafloat_variable__(const IAEA_I32 *id,
0600                                                      const IAEA_I32 *index,
0601                                                      IAEA_I32 *type)
0602 { iaea_set_type_extrafloat_variable(id, index, type); }
0603 
0604 IAEA_EXTERN_C IAEA_EXPORT
0605 void IAEA_SET_TYPE_EXTRAFLOAT_VARIABLE(const IAEA_I32 *id,
0606                                                      const IAEA_I32 *index,
0607                                                      IAEA_I32 *type)
0608 { iaea_set_type_extrafloat_variable(id, index, type); }
0609 
0610 IAEA_EXTERN_C IAEA_EXPORT
0611 void IAEA_SET_TYPE_EXTRAFLOAT_VARIABLE_(const IAEA_I32 *id,
0612                                                      const IAEA_I32 *index,
0613                                                      IAEA_I32 *type)
0614 { iaea_set_type_extrafloat_variable(id, index, type); }
0615 
0616 IAEA_EXTERN_C IAEA_EXPORT
0617 void IAEA_SET_TYPE_EXTRAFLOAT_VARIABLE__(const IAEA_I32 *id,
0618                                                      const IAEA_I32 *index,
0619                                                      IAEA_I32 *type)
0620 { iaea_set_type_extrafloat_variable(id, index, type); }
0621 
0622 
0623 /****************************************************************************
0624 * Get a type type of all extra variables from a header of the phsp "id".
0625 *
0626 * extralong_types[] AND extrafloat_types[] must have a dimension bigger than
0627 * MAX_NUMB_EXTRALONG_TYPES and MAX_NUMB_EXTRAFLOAT_TYPES correspondingly
0628 *
0629 * The current list of types for extra long variables is:
0630 *   0: User defined generic type
0631 *   1: Incremental history number (EGS,PENELOPE)
0632 *      = 0 indicates a nonprimary particle event
0633 *      > 0 indicates a primary particle. The value is equal to the number of
0634 *          primaries particles employed to get to this history after the last
0635 *          primary event was recorded.
0636 *   2: LATCH (EGS)
0637 *   3: ILB5 (PENELOPE)
0638 *   4: ILB4 (PENELOPE)
0639 *   5: ILB3 (PENELOPE)
0640 *   6: ILB2 (PENELOPE)
0641 *   7: ILB1 (PENELOPE)
0642 *   more to be defined
0643 *
0644 * The current list of types for extra float variables is:
0645 *   0: User defined generic type
0646 *   1: XLAST (x coord. of the last interaction)
0647 *   2: YLAST (y coord. of the last interaction)
0648 *   3: ZLAST (z coord. of the last interaction)
0649 *   more to be defined
0650 *
0651 * Usually called before reading phsp header to know the type of extra long
0652 * variables to be read. It must be called once for every extra float variable.
0653 *
0654 * result = -1 means the source's header file does not exist
0655 *             or source was not properly initialized (call iaea_new_...)
0656 *******************************************************************************/
0657 IAEA_EXTERN_C IAEA_EXPORT
0658 void iaea_get_type_extra_variables(const IAEA_I32 *id, IAEA_I32 *result,
0659       IAEA_I32 extralong_types[], IAEA_I32 extrafloat_types[])
0660 {
0661       // No header found
0662       if(p_iaea_header[*id]->fheader == NULL) {*result = -1; return;}
0663 
0664       for (int i=0;i<p_iaea_header[*id]->record_contents[8];i++ )
0665           extralong_types[i] = p_iaea_header[*id]->extralong_contents[i];
0666 
0667       for (int j=0;j<p_iaea_header[*id]->record_contents[7];j++ )
0668           extrafloat_types[j] = p_iaea_header[*id]->extrafloat_contents[j];
0669 
0670       *result = +1;
0671       return;
0672 }
0673 IAEA_EXTERN_C IAEA_EXPORT
0674 void iaea_get_type_extra_variables_(const IAEA_I32 *id, IAEA_I32 *result,
0675       IAEA_I32 extralong_types[], IAEA_I32 extrafloat_types[])
0676 { iaea_get_type_extra_variables(id, result, extralong_types, extrafloat_types); }
0677 
0678 IAEA_EXTERN_C IAEA_EXPORT
0679 void iaea_get_type_extra_variables__(const IAEA_I32 *id, IAEA_I32 *result,
0680       IAEA_I32 extralong_types[], IAEA_I32 extrafloat_types[])
0681 { iaea_get_type_extra_variables(id, result, extralong_types, extrafloat_types); }
0682 
0683 IAEA_EXTERN_C IAEA_EXPORT
0684 void IAEA_GET_TYPE_EXTRA_VARIABLES(const IAEA_I32 *id, IAEA_I32 *result,
0685       IAEA_I32 extralong_types[], IAEA_I32 extrafloat_types[])
0686 { iaea_get_type_extra_variables(id, result, extralong_types, extrafloat_types); }
0687 
0688 IAEA_EXTERN_C IAEA_EXPORT
0689 void IAEA_GET_TYPE_EXTRA_VARIABLES_(const IAEA_I32 *id, IAEA_I32 *result,
0690       IAEA_I32 extralong_types[], IAEA_I32 extrafloat_types[])
0691 { iaea_get_type_extra_variables(id, result, extralong_types, extrafloat_types); }
0692 
0693 IAEA_EXTERN_C IAEA_EXPORT
0694 void IAEA_GET_TYPE_EXTRA_VARIABLES__(const IAEA_I32 *id, IAEA_I32 *result,
0695       IAEA_I32 extralong_types[], IAEA_I32 extrafloat_types[])
0696 { iaea_get_type_extra_variables(id, result, extralong_types, extrafloat_types); }
0697 
0698 /*************************************************************************
0699 * Set variable corresponding to the "index" number to a "constant" value
0700 * for a corresponding header of the phsp "id". Index is running from zero.
0701 *
0702 *       (Usually called as needed before MC loop started)
0703 *
0704 *                index  =  0 1 2 3 4 5 6
0705 *          corresponds to  x,y,z,u,v,w,wt
0706 *
0707 * Usually called before writing phsp files to set those variables which
0708 * are not going to be stored. It must be called once for every variable
0709 *
0710 * constant = -1 means the source's header file does not exist
0711 *               or source was not properly initialized (call iaea_new_...)
0712 * constant = -2 means the index is out of range ( 0 <= index < 7 )
0713 *************************************************************************/
0714 IAEA_EXTERN_C IAEA_EXPORT
0715 void iaea_set_constant_variable(const IAEA_I32 *id, const IAEA_I32 *index,
0716                                                IAEA_Float *constant)
0717 {
0718       // No header found
0719       if(p_iaea_header[*id]->fheader == NULL) {*constant = -1.f; return;}
0720 
0721       if((*index < 0) || (*index > 6) ) {*constant = -2.f; return;}
0722 
0723       p_iaea_header[*id]->record_contents[*index] = 0; // variable is constant
0724       p_iaea_header[*id]->record_constant[*index] = *constant;
0725 
0726       // Store read/write logical block changes in the PHSP header
0727       p_iaea_header[*id]->get_record_contents(p_iaea_record[*id]);
0728 
0729       return;
0730 }
0731 IAEA_EXTERN_C IAEA_EXPORT
0732 void iaea_set_constant_variable_(const IAEA_I32 *id,
0733                                                const IAEA_I32 *index,
0734                                                      IAEA_Float *constant)
0735 {iaea_set_constant_variable(id, index, constant); }
0736 IAEA_EXTERN_C IAEA_EXPORT
0737 void iaea_set_constant_variable__(const IAEA_I32 *id,
0738                                                const IAEA_I32 *index,
0739                                                      IAEA_Float *constant)
0740 {iaea_set_constant_variable(id, index, constant); }
0741 IAEA_EXTERN_C IAEA_EXPORT
0742 void IAEA_SET_CONSTANT_VARIABLE(const IAEA_I32 *id,
0743                                                const IAEA_I32 *index,
0744                                                      IAEA_Float *constant)
0745 {iaea_set_constant_variable(id, index, constant); }
0746 IAEA_EXTERN_C IAEA_EXPORT
0747 void IAEA_SET_CONSTANT_VARIABLE_(const IAEA_I32 *id,
0748                                                const IAEA_I32 *index,
0749                                                      IAEA_Float *constant)
0750 {iaea_set_constant_variable(id, index, constant); }
0751 IAEA_EXTERN_C IAEA_EXPORT
0752 void IAEA_SET_CONSTANT_VARIABLE__(const IAEA_I32 *id,
0753                                                const IAEA_I32 *index,
0754                                                      IAEA_Float *constant)
0755 {iaea_set_constant_variable(id, index, constant); }
0756 
0757 /*************************************************************************
0758 * Get value of constant corresponding to the "index" number
0759 * for a corresponding header of the phsp "id". Index is running from zero.
0760 *
0761 *                index  =  0 1 2 3 4 5 6
0762 *          corresponds to  x,y,z,u,v,w,wt
0763 *
0764 * Usually called when reading phsp header info.
0765 * It must be called once for every variable
0766 *
0767 *  result = -1 means the source's header file does not exist
0768 *               or source was not properly initialized (call iaea_new_...)
0769 *  result = -2 means the index is out of range ( 0 <= index < 7 )
0770 *  result = -3 means that the parameter indicated by index is not a constant
0771 *************************************************************************/
0772 IAEA_EXTERN_C IAEA_EXPORT
0773 void iaea_get_constant_variable(const IAEA_I32 *id, const IAEA_I32 *index,
0774                                  IAEA_Float *constant, IAEA_I32 *result)
0775 {
0776       *result=0;
0777       // No header found
0778       if(p_iaea_header[*id]->fheader == NULL) {*result = -1; return;}
0779 
0780       if((*index < 0) || (*index > 6) ) {*result = -2; return;}
0781 
0782       if( p_iaea_header[*id]->record_contents[*index] == 0) {
0783           *constant=p_iaea_header[*id]->record_constant[*index];
0784       }
0785       else { *result=-3;}
0786 
0787       return;
0788 }
0789 IAEA_EXTERN_C IAEA_EXPORT
0790 void iaea_get_constant_variable_(const IAEA_I32 *id,
0791                                         const IAEA_I32 *index,
0792                                      IAEA_Float *constant, IAEA_I32 *result)
0793 {iaea_get_constant_variable(id, index, constant, result); }
0794 IAEA_EXTERN_C IAEA_EXPORT
0795 void iaea_get_constant_variable__(const IAEA_I32 *id,
0796                                         const IAEA_I32 *index,
0797                                      IAEA_Float *constant, IAEA_I32 *result)
0798 {iaea_get_constant_variable(id, index, constant, result); }
0799 IAEA_EXTERN_C IAEA_EXPORT
0800 void IAEA_GET_CONSTANT_VARIABLE(const IAEA_I32 *id,
0801                                         const IAEA_I32 *index,
0802                                      IAEA_Float *constant, IAEA_I32 *result)
0803 {iaea_get_constant_variable(id, index, constant, result); }
0804 IAEA_EXTERN_C IAEA_EXPORT
0805 void IAEA_GET_CONSTANT_VARIABLE_(const IAEA_I32 *id,
0806                                         const IAEA_I32 *index,
0807                                      IAEA_Float *constant, IAEA_I32 *result)
0808 {iaea_get_constant_variable(id, index, constant, result); }
0809 IAEA_EXTERN_C IAEA_EXPORT
0810 void IAEA_GET_CONSTANT_VARIABLE__(const IAEA_I32 *id,
0811                                         const IAEA_I32 *index,
0812                                      IAEA_Float *constant, IAEA_I32 *result)
0813 {iaea_get_constant_variable(id, index, constant, result); }
0814 
0815 /*****************************************************************************
0816 * Get n_indep_particles number of statistically independent particles read
0817 * so far from the Source with Id id.
0818 *
0819 * Set n_indep_particles to negative if such source does not exist.
0820 ******************************************************************************/
0821 IAEA_EXTERN_C IAEA_EXPORT
0822 void iaea_get_used_original_particles(const IAEA_I32 *id, IAEA_I64 *n_indep_particles)
0823 {
0824       // No header found
0825       if(p_iaea_header[*id]->fheader == NULL) {*n_indep_particles = -1; return;}
0826 
0827       // (Number of electron histories for linacs)
0828       *n_indep_particles = p_iaea_header[*id]->read_indep_histories;
0829       return;
0830 }
0831 IAEA_EXTERN_C IAEA_EXPORT
0832 void iaea_get_used_original_particles_(const IAEA_I32 *id, IAEA_I64 *n_indep_particles)
0833 { iaea_get_used_original_particles(id, n_indep_particles); }
0834 IAEA_EXTERN_C IAEA_EXPORT
0835 void iaea_get_used_original_particles__(const IAEA_I32 *id, IAEA_I64 *n_indep_particles)
0836 { iaea_get_used_original_particles(id, n_indep_particles); }
0837 IAEA_EXTERN_C IAEA_EXPORT
0838 void IAEA_GET_USED_ORIGINAL_PARTICLES(const IAEA_I32 *id, IAEA_I64 *n_indep_particles)
0839 { iaea_get_used_original_particles(id, n_indep_particles); }
0840 IAEA_EXTERN_C IAEA_EXPORT
0841 void IAEA_GET_USED_ORIGINAL_PARTICLES_(const IAEA_I32 *id, IAEA_I64 *n_indep_particles)
0842 { iaea_get_used_original_particles(id, n_indep_particles); }
0843 IAEA_EXTERN_C IAEA_EXPORT
0844 void IAEA_GET_USED_ORIGINAL_PARTICLES__(const IAEA_I32 *id, IAEA_I64 *n_indep_particles)
0845 { iaea_get_used_original_particles(id, n_indep_particles); }
0846 
0847 /*****************************************************************************
0848 * Get Total Number of Original Particles from the Source with Id id.
0849 *
0850 * For a typical linac it should be equal to the total number of electrons
0851 * incident on the primary target.
0852 *
0853 * Set number_of_original_particles to negative if such source does not exist.
0854 ******************************************************************************/
0855 IAEA_EXTERN_C IAEA_EXPORT
0856 void iaea_get_total_original_particles(const IAEA_I32 *id,
0857                                        IAEA_I64 *number_of_original_particles)
0858 {
0859       // No header found
0860       if(p_iaea_header[*id]->fheader == NULL)
0861           {*number_of_original_particles = -1; return;}
0862 
0863       *number_of_original_particles = p_iaea_header[*id]->orig_histories;
0864 
0865       return;
0866 }
0867 IAEA_EXTERN_C IAEA_EXPORT
0868 void iaea_get_total_original_particles_(const IAEA_I32 *id,
0869                                                IAEA_I64 *number_of_original_particles)
0870 { iaea_get_total_original_particles(id, number_of_original_particles); }
0871 IAEA_EXTERN_C IAEA_EXPORT
0872 void iaea_get_total_original_particles__(const IAEA_I32 *id,
0873                                                 IAEA_I64 *number_of_original_particles)
0874 { iaea_get_total_original_particles(id, number_of_original_particles); }
0875 IAEA_EXTERN_C IAEA_EXPORT
0876 void IAEA_GET_TOTAL_ORIGINAL_PARTICLES(const IAEA_I32 *id,
0877                                               IAEA_I64 *number_of_original_particles)
0878 { iaea_get_total_original_particles(id, number_of_original_particles); }
0879 IAEA_EXTERN_C IAEA_EXPORT
0880 void IAEA_GET_TOTAL_ORIGINAL_PARTICLES_(const IAEA_I32 *id,
0881                                                IAEA_I64 *number_of_original_particles)
0882 { iaea_get_total_original_particles(id, number_of_original_particles); }
0883 IAEA_EXTERN_C IAEA_EXPORT
0884 void IAEA_GET_TOTAL_ORIGINAL_PARTICLES__(const IAEA_I32 *id,
0885                                                 IAEA_I64 *number_of_original_particles)
0886 { iaea_get_total_original_particles(id, number_of_original_particles); }
0887 
0888 /*****************************************************************************
0889 * Set Total Number of Original Particles for the Source with Id id.
0890 *
0891 * For a typical linac it should be equal to the total number of electrons
0892 * incident on the primary target.
0893 *
0894 * Set number_of_original_particles to negative if such source does not exist.
0895 ******************************************************************************/
0896 IAEA_EXTERN_C IAEA_EXPORT
0897 void iaea_set_total_original_particles(const IAEA_I32 *id,
0898                                        IAEA_I64 *number_of_original_particles)
0899 {
0900       // No header found
0901       if(p_iaea_header[*id]->fheader == NULL)
0902             {*number_of_original_particles = -1; return;}
0903 
0904       // Bug corrected, RCN, dec. 2006
0905       p_iaea_header[*id]->orig_histories = *number_of_original_particles;
0906       return;
0907 }
0908 IAEA_EXTERN_C IAEA_EXPORT
0909 void iaea_set_total_original_particles_(const IAEA_I32 *id,
0910                                                IAEA_I64 *number_of_original_particles)
0911 { iaea_set_total_original_particles(id, number_of_original_particles); }
0912 IAEA_EXTERN_C IAEA_EXPORT
0913 void iaea_set_total_original_particles__(const IAEA_I32 *id,
0914                                                 IAEA_I64 *number_of_original_particles)
0915 { iaea_set_total_original_particles(id, number_of_original_particles); }
0916 IAEA_EXTERN_C IAEA_EXPORT
0917 void IAEA_SET_TOTAL_ORIGINAL_PARTICLES(const IAEA_I32 *id,
0918                                               IAEA_I64 *number_of_original_particles)
0919 { iaea_set_total_original_particles(id, number_of_original_particles); }
0920 IAEA_EXTERN_C IAEA_EXPORT
0921 void IAEA_SET_TOTAL_ORIGINAL_PARTICLES_(const IAEA_I32 *id,
0922                                                IAEA_I64 *number_of_original_particles)
0923 { iaea_set_total_original_particles(id, number_of_original_particles); }
0924 IAEA_EXTERN_C IAEA_EXPORT
0925 void IAEA_SET_TOTAL_ORIGINAL_PARTICLES__(const IAEA_I32 *id,
0926                                                 IAEA_I64 *number_of_original_particles)
0927 { iaea_set_total_original_particles(id, number_of_original_particles); }
0928 
0929 /**************************************************************************
0930 * Partitioning for parallel runs
0931 *
0932 * i_parallel is the job number, i_chunk the calculation chunk,
0933 * n_chunk the total number of calculation chunks. This function
0934 * should divide the available phase space of source with Id id
0935 * into n_chunk equal portions and from now on deliver particles
0936 * from the i_chunk-th portion. (i_chunk must be between 1 and n_chunk)
0937 * The extra parameter i_parallel is needed
0938 * for the cases where the source is an event generator and should
0939 * be used to adjust the random number sequence.
0940 * The variable result should be set to 0 if everything went smoothly,
0941 * or to some error code if it didnt.
0942 **************************************************************************/
0943 IAEA_EXTERN_C IAEA_EXPORT
0944 //(MACG) void iaea_set_parallel(const IAEA_I32 *id, const IAEA_I32 *i_parallel,
0945 void iaea_set_parallel(const IAEA_I32 *id, const IAEA_I32* /*i_parallel*/,
0946                        const IAEA_I32 *i_chunk, const IAEA_I32 *n_chunk,
0947                                        IAEA_I32 *result)
0948 {
0949    if(p_iaea_header[*id]->fheader == NULL) {*result = -1; return;}
0950    if(*n_chunk <= 0) {*result = -2; return;}
0951    if( (*i_chunk < 1) || (*i_chunk > *n_chunk) ) {*result = -3; return;}
0952 
0953    if(p_iaea_header[*id]->file_type == 1)
0954    {
0955          // set i_parallel for event generators
0956          *result = 0;
0957          return;
0958    }
0959 
0960    IAEA_I64 nrecords =  p_iaea_header[*id]->nParticles;
0961    IAEA_I32 record_length =  p_iaea_header[*id]->record_length;
0962    // IAEA_I32 number_record_per_chunk = (IAEA_I32)nrecords/(*n_chunk); // changed, May 2011
0963    IAEA_I64 number_record_per_chunk = nrecords/(*n_chunk);
0964 
0965 
0966    // IAEA_I32 offset = ((*i_chunk)-1)*record_length * number_record_per_chunk; // changed, May 2011
0967    IAEA_I64 offset = ((*i_chunk)-1)*record_length * number_record_per_chunk;
0968    /*
0969    SEEK_CUR   Current position of file pointer
0970    SEEK_END   End of file
0971    SEEK_SET   Beginning of file
0972    stream   Pointer to FILE structure
0973    offset   Number of bytes from origin
0974    origin   Initial position
0975    */
0976    if( fseek(p_iaea_record[*id]->p_file, offset ,SEEK_SET) == 0)
0977    {
0978          // IAEA_I32 pos = ftell(p_iaea_record[*id]->p_file);  // changed, May 2011
0979          IAEA_I64 pos = (IAEA_I64)ftell(p_iaea_record[*id]->p_file);
0980      (void) pos; //(MACG) -Wunused-variable
0981          *result = 0;
0982          return;
0983    }
0984 
0985    *result = -1;
0986    return;
0987 }
0988 IAEA_EXTERN_C IAEA_EXPORT
0989 void iaea_set_parallel_(const IAEA_I32 *id,
0990                                       const IAEA_I32 *i_parallel,
0991                                       const IAEA_I32 *i_chunk,
0992                                       const IAEA_I32 *n_chunk,
0993                                             IAEA_I32 *is_ok)
0994 { iaea_set_parallel(id, i_parallel, i_chunk, n_chunk, is_ok); }
0995 IAEA_EXTERN_C IAEA_EXPORT
0996 void iaea_set_parallel__(const IAEA_I32 *id,
0997                                       const IAEA_I32 *i_parallel,
0998                                       const IAEA_I32 *i_chunk,
0999                                       const IAEA_I32 *n_chunk,
1000                                             IAEA_I32 *is_ok)
1001 { iaea_set_parallel(id, i_parallel, i_chunk, n_chunk, is_ok); }
1002 IAEA_EXTERN_C IAEA_EXPORT
1003 void IAEA_SET_PARALLEL(const IAEA_I32 *id,
1004                                       const IAEA_I32 *i_parallel,
1005                                       const IAEA_I32 *i_chunk,
1006                                       const IAEA_I32 *n_chunk,
1007                                             IAEA_I32 *is_ok)
1008 { iaea_set_parallel(id, i_parallel, i_chunk, n_chunk, is_ok); }
1009 IAEA_EXTERN_C IAEA_EXPORT
1010 void IAEA_SET_PARALLEL_(const IAEA_I32 *id,
1011                                       const IAEA_I32 *i_parallel,
1012                                       const IAEA_I32 *i_chunk,
1013                                       const IAEA_I32 *n_chunk,
1014                                             IAEA_I32 *is_ok)
1015 { iaea_set_parallel(id, i_parallel, i_chunk, n_chunk, is_ok); }
1016 IAEA_EXTERN_C IAEA_EXPORT
1017 void IAEA_SET_PARALLEL__(const IAEA_I32 *id,
1018                                       const IAEA_I32 *i_parallel,
1019                                       const IAEA_I32 *i_chunk,
1020                                       const IAEA_I32 *n_chunk,
1021                                             IAEA_I32 *is_ok)
1022 { iaea_set_parallel(id, i_parallel, i_chunk, n_chunk, is_ok); }
1023 
1024 /**************************************************************************
1025 * check that the file size equals the value of checksum in the header
1026 * and that the byte order of the machine being run on matches that of
1027 * the file
1028 *
1029 * id is the phase space file identifier.  Returns 0 if the size of the file=
1030 * checksum and the byte order = the byte order of the machine being run on.
1031 * Returns -1 if the header does not exist; -2 if the function fseek fails
1032 * for some reason; -3 if there is a file size mismatch; -4 if there is a
1033 * byte order mismatch; -5 if there is a mismatch in both
1034 **************************************************************************/
1035 IAEA_EXTERN_C IAEA_EXPORT
1036 void iaea_check_file_size_byte_order(const IAEA_I32 *id,
1037                                        IAEA_I32 *result)
1038 {
1039    if(p_iaea_header[*id]->fheader == NULL) {*result = -1; return;}
1040 
1041    int machine_byte_order = check_byte_order();
1042 
1043    #if (defined WIN32) || (defined WIN64)
1044      // IAEA_I64 size = _filelengthi64(fileno(p_iaea_record[*id]->p_file));
1045      struct _stati64 fileStatus;
1046      _fstati64(fileno(p_iaea_record[*id]->p_file),&fileStatus);
1047    #else
1048      struct stat fileStatus;
1049      fstat(fileno(p_iaea_record[*id]->p_file),&fileStatus);
1050    #endif
1051 
1052    IAEA_I64 size = fileStatus.st_size;
1053    printf(" phsp size = %llu\n",size);
1054 
1055    // bug found, changed to filelength use. May 2011
1056    // avoiding ftell and fseek use for I64 compatibility
1057    if ( size == p_iaea_header[*id]->checksum )
1058    {
1059        if (machine_byte_order==p_iaea_header[*id]->byte_order)
1060        {
1061           *result=0;
1062        }
1063        else {
1064           *result=-4;
1065        }
1066    }
1067    else {
1068        if (machine_byte_order==p_iaea_header[*id]->byte_order)
1069        {
1070           *result=-3;
1071        }
1072        else {
1073           *result=-5;
1074        }
1075    }
1076 
1077    return;
1078 }
1079 IAEA_EXTERN_C IAEA_EXPORT
1080 void iaea_check_file_size_byte_order_(const IAEA_I32 *id,
1081                                        IAEA_I32 *result)
1082 { iaea_check_file_size_byte_order(id, result);}
1083 IAEA_EXTERN_C IAEA_EXPORT
1084 void iaea_check_file_size_byte_order__(const IAEA_I32 *id,
1085                                        IAEA_I32 *result)
1086 { iaea_check_file_size_byte_order( id, result);}
1087 IAEA_EXTERN_C IAEA_EXPORT
1088 void IAEA_CHECK_FILE_SIZE_BYTE_ORDER(const IAEA_I32 *id,
1089                                        IAEA_I32 *result)
1090 { iaea_check_file_size_byte_order( id, result);}
1091 IAEA_EXTERN_C IAEA_EXPORT
1092 void IAEA_CHECK_FILE_SIZE_BYTE_ORDER_(const IAEA_I32 *id,
1093                                        IAEA_I32 *result)
1094 { iaea_check_file_size_byte_order( id, result);}
1095 IAEA_EXTERN_C IAEA_EXPORT
1096 void IAEA_CHECK_FILE_SIZE_BYTE_ORDER__(const IAEA_I32 *id,
1097                                        IAEA_I32 *result)
1098 { iaea_check_file_size_byte_order( id, result);}
1099 
1100 
1101 /**************************************************************************
1102 * setting the pointer to a user-specified record no. in the file
1103 *
1104 * record_num is the user-specified record number passed to the function.
1105 * id is the phase space file identifier.  If record_num = the number of
1106 * particles in the file + 1, then the position is set to the end of the
1107 * file.
1108 * The variable result should be set to 0 if everything went smoothly,
1109 * or to some error code if it didnt.
1110 **************************************************************************/
1111 IAEA_EXTERN_C IAEA_EXPORT
1112 void iaea_set_record(const IAEA_I32 *id, const IAEA_I64 *record_num,
1113                                        IAEA_I32 *result)
1114 {
1115    if(p_iaea_header[*id]->fheader == NULL) {*result = -1; return;}
1116    if(*record_num <= 0) {*result = -2; return;}
1117    if(*record_num > p_iaea_header[*id]->nParticles+1) {*result = -3; return;}
1118 
1119    IAEA_I32 record_length =  p_iaea_header[*id]->record_length;
1120 
1121    IAEA_I64 offset = (*record_num-1) * record_length;
1122    /*
1123    SEEK_CUR   Current position of file pointer
1124    SEEK_END   End of file
1125    SEEK_SET   Beginning of file
1126    stream   Pointer to FILE structure
1127    offset   Number of bytes from origin
1128    origin   Initial position
1129    */
1130 
1131    if( fseek(p_iaea_record[*id]->p_file, offset ,SEEK_SET) == 0)
1132    {
1133      IAEA_I32 pos = ftell(p_iaea_record[*id]->p_file);
1134      (void) pos; //(MACG)-Wunused-variable
1135      *result = 0;
1136      return;
1137    }
1138 
1139    *result = -1;
1140    return;
1141 }
1142 IAEA_EXTERN_C IAEA_EXPORT
1143 void iaea_set_record_(const IAEA_I32 *id,
1144                                       const IAEA_I64 *record_num,
1145                                             IAEA_I32 *is_ok)
1146 { iaea_set_record(id, record_num, is_ok); }
1147 IAEA_EXTERN_C IAEA_EXPORT
1148 void iaea_set_record__(const IAEA_I32 *id,
1149                                       const IAEA_I64 *record_num,
1150                                             IAEA_I32 *is_ok)
1151 { iaea_set_record(id, record_num, is_ok); }
1152 IAEA_EXTERN_C IAEA_EXPORT
1153 void IAEA_SET_RECORD(const IAEA_I32 *id,
1154                                       const IAEA_I64 *record_num,
1155                                             IAEA_I32 *is_ok)
1156 { iaea_set_record(id, record_num, is_ok); }
1157 IAEA_EXTERN_C IAEA_EXPORT
1158 void IAEA_SET_RECORD_(const IAEA_I32 *id,
1159                                       const IAEA_I64 *record_num,
1160                                             IAEA_I32 *is_ok)
1161 { iaea_set_record(id, record_num, is_ok); }
1162 IAEA_EXTERN_C IAEA_EXPORT
1163 void IAEA_SET_RECORD__(const IAEA_I32 *id,
1164                                       const IAEA_I64 *record_num,
1165                                             IAEA_I32 *is_ok)
1166 { iaea_set_record(id, record_num, is_ok); }
1167 
1168 /**************************************************************************
1169 * Get a particle
1170 *
1171 * Return the next particle from the sequence of particles from source
1172 * with Id id. Set n_stat to the number of statistically independent
1173 * events since the last call to this function (i.e. n_stat = 0, if
1174 * the particle resulted from the same incident electron, n_stat = 377
1175 * if there were 377 statistically independent events sinc the last particle
1176 * returned, etc.). If this information is not available,
1177 * simply set n_stat to 1 if the particle belongs to a new statistically
1178 * independent event. Set n_stat to -1, if a source with Id id does not
1179 * exist. Set n_stat to -2, if end of file of the phase space source reached
1180 
1181 **************************************************************************/
1182 IAEA_EXTERN_C IAEA_EXPORT
1183 void iaea_get_particle(const IAEA_I32 *id, IAEA_I32 *n_stat,
1184 IAEA_I32 *type, /* particle type */
1185 IAEA_Float *E,  /* kinetic energy in MeV */
1186 IAEA_Float *wt, /* statistical weight */
1187 IAEA_Float *x,
1188 IAEA_Float *y,
1189 IAEA_Float *z,  /* position in cartesian coordinates*/
1190 IAEA_Float *u,
1191 IAEA_Float *v,
1192 IAEA_Float *w,  /* direction in cartesian coordinates*/
1193 IAEA_Float *extra_floats,
1194 IAEA_I32 *extra_ints)
1195 {
1196       if(feof(p_iaea_record[*id]->p_file)) {
1197          *n_stat = -2;
1198          rewind (p_iaea_record[*id]->p_file);
1199          return;
1200       }
1201 
1202       if( p_iaea_record[*id]->read_particle() == FAIL ) { *n_stat = -1; return;}
1203 
1204       iaea_record_type *p = p_iaea_record[*id];
1205 
1206       // Corrected on Dec. 2006. Before n_stat was not assigned if
1207       // (p->iextralong > 0)  and (p_iaea_header[*id]->extralong_contents[j] != 1)
1208 
1209       if( p->IsNewHistory > 0) *n_stat=1;
1210       else                     *n_stat=0;
1211 
1212       if(p->iextralong > 0) {
1213           for(int j=0;j<p->iextralong ;j++) {
1214               // Looking for incremental number of histories
1215               // (Type 1 of the extralong stored variable)
1216               if(p_iaea_header[*id]->extralong_contents[j] == 1) {
1217                   *n_stat = p->extralong[j];
1218                   p->IsNewHistory = *n_stat;
1219               }
1220           }
1221       }
1222 
1223       *type  = p->particle; /* particle type */
1224       *E     = p->energy;   /* kinetic energy in MeV */
1225 
1226       if(p->ix > 0) *x = p->x;
1227       else          *x = p_iaea_header[*id]->record_constant[0];
1228 
1229       if(p->iy > 0) *y = p->y;
1230       else          *y = p_iaea_header[*id]->record_constant[1];
1231 
1232       if(p->iz > 0) *z = p->z; /* position in cartesian coordinates*/
1233       else          *z = p_iaea_header[*id]->record_constant[2];
1234 
1235       if(p->iu > 0) *u = p->u;
1236       else          *u = p_iaea_header[*id]->record_constant[3];
1237 
1238       if(p->iv > 0) *v = p->v;
1239       else          *v = p_iaea_header[*id]->record_constant[4];
1240 
1241       if(p->iw > 0) *w = p->w; /* direction in cartesian coordinates*/
1242       else          *w = p_iaea_header[*id]->record_constant[5];
1243 
1244       if(p->iweight > 0) *wt = p->weight;   /* statistical weight */
1245       else               *wt = p_iaea_header[*id]->record_constant[6];
1246 
1247       for(int k=0;k<p->iextrafloat;k++) extra_floats[k] = p->extrafloat[k];
1248       for(int j=0;j<p->iextralong ;j++) extra_ints[j] = p->extralong[j];
1249 
1250       /*
1251         Updating counters including:
1252         Min and Max Weight per particle,
1253         Total Weight per particle,
1254         Min and Max Kinetic energy per particle,
1255         Min and Max X,Y,Z coordinates,
1256         Total number of particles,
1257         Total number of each particle type
1258         Number of statistically independent histories
1259       */
1260       p_iaea_header[*id]->update_counters(p_iaea_record[*id]);
1261 
1262       return;
1263 }
1264 IAEA_EXTERN_C IAEA_EXPORT
1265 void iaea_get_particle_(const IAEA_I32 *id, IAEA_I32 *n_stat,
1266 IAEA_I32 *type, /* particle type */
1267 IAEA_Float *E,  /* kinetic energy in MeV */
1268 IAEA_Float *wt, /* statistical weight */
1269 IAEA_Float *x,
1270 IAEA_Float *y,
1271 IAEA_Float *z,  /* position in cartesian coordinates*/
1272 IAEA_Float *u,
1273 IAEA_Float *v,
1274 IAEA_Float *w,  /* direction in cartesian coordinates*/
1275 IAEA_Float *extra_floats,
1276 IAEA_I32 *extra_ints)
1277 { iaea_get_particle(id, n_stat, type,
1278                                 E, wt, x, y, z, u, v, w, extra_floats, extra_ints); }
1279 IAEA_EXTERN_C IAEA_EXPORT
1280 void iaea_get_particle__(const IAEA_I32 *id, IAEA_I32 *n_stat,
1281 IAEA_I32 *type, /* particle type */
1282 IAEA_Float *E,  /* kinetic energy in MeV */
1283 IAEA_Float *wt, /* statistical weight */
1284 IAEA_Float *x,
1285 IAEA_Float *y,
1286 IAEA_Float *z,  /* position in cartesian coordinates*/
1287 IAEA_Float *u,
1288 IAEA_Float *v,
1289 IAEA_Float *w,  /* direction in cartesian coordinates*/
1290 IAEA_Float *extra_floats,
1291 IAEA_I32 *extra_ints)
1292 { iaea_get_particle(id, n_stat, type,
1293                                 E, wt, x, y, z, u, v, w, extra_floats, extra_ints); }
1294 IAEA_EXTERN_C IAEA_EXPORT
1295 void IAEA_GET_PARTICLE(const IAEA_I32 *id, IAEA_I32 *n_stat,
1296 IAEA_I32 *type, /* particle type */
1297 IAEA_Float *E,  /* kinetic energy in MeV */
1298 IAEA_Float *wt, /* statistical weight */
1299 IAEA_Float *x,
1300 IAEA_Float *y,
1301 IAEA_Float *z,  /* position in cartesian coordinates*/
1302 IAEA_Float *u,
1303 IAEA_Float *v,
1304 IAEA_Float *w,  /* direction in cartesian coordinates*/
1305 IAEA_Float *extra_floats,
1306 IAEA_I32 *extra_ints)
1307 { iaea_get_particle(id, n_stat, type,
1308                                 E, wt, x, y, z, u, v, w, extra_floats, extra_ints); }
1309 IAEA_EXTERN_C IAEA_EXPORT
1310 void IAEA_GET_PARTICLE_(const IAEA_I32 *id, IAEA_I32 *n_stat,
1311 IAEA_I32 *type, /* particle type */
1312 IAEA_Float *E,  /* kinetic energy in MeV */
1313 IAEA_Float *wt, /* statistical weight */
1314 IAEA_Float *x,
1315 IAEA_Float *y,
1316 IAEA_Float *z,  /* position in cartesian coordinates*/
1317 IAEA_Float *u,
1318 IAEA_Float *v,
1319 IAEA_Float *w,  /* direction in cartesian coordinates*/
1320 IAEA_Float *extra_floats,
1321 IAEA_I32 *extra_ints)
1322 { iaea_get_particle(id, n_stat, type,
1323                                 E, wt, x, y, z, u, v, w, extra_floats, extra_ints); }
1324 IAEA_EXTERN_C IAEA_EXPORT
1325 void IAEA_GET_PARTICLE__(const IAEA_I32 *id, IAEA_I32 *n_stat,
1326 IAEA_I32 *type, /* particle type */
1327 IAEA_Float *E,  /* kinetic energy in MeV */
1328 IAEA_Float *wt, /* statistical weight */
1329 IAEA_Float *x,
1330 IAEA_Float *y,
1331 IAEA_Float *z,  /* position in cartesian coordinates*/
1332 IAEA_Float *u,
1333 IAEA_Float *v,
1334 IAEA_Float *w,  /* direction in cartesian coordinates*/
1335 IAEA_Float *extra_floats,
1336 IAEA_I32 *extra_ints)
1337 { iaea_get_particle(id, n_stat, type,
1338                                 E, wt, x, y, z, u, v, w, extra_floats, extra_ints); }
1339 
1340 /**************************************************************************
1341 * Write a particle
1342 * n_stat = 0 for a secondary particle
1343 * n_stat > 0 for an independent particle
1344 *
1345 * Write a particle to the source with Id id.
1346 * Set n_stat to -1, if ERROR (source with Id id does not exist).
1347 **************************************************************************/
1348 IAEA_EXTERN_C IAEA_EXPORT
1349 void iaea_write_particle(const IAEA_I32 *id, IAEA_I32 *n_stat,
1350 const IAEA_I32 *type, /* particle type */
1351 const IAEA_Float *E,  /* kinetic energy in MeV */
1352 const IAEA_Float *wt, /* statistical weight */
1353 const IAEA_Float *x,
1354 const IAEA_Float *y,
1355 const IAEA_Float *z,  /* position in cartesian coordinates*/
1356 const IAEA_Float *u,
1357 const IAEA_Float *v,
1358 const IAEA_Float *w,  /* direction in cartesian coordinates*/
1359 const IAEA_Float *extra_floats,
1360 const IAEA_I32 *extra_ints)
1361 {
1362 
1363       iaea_record_type *p = p_iaea_record[*id];
1364 
1365       if( *n_stat > 0 ) p->IsNewHistory = *n_stat;
1366       else              p->IsNewHistory = 0;
1367 
1368       p->particle = (short)*type; /* particle type */
1369       p->energy   = *E;    /* kinetic energy in MeV */
1370       if(p->iweight > 0) p->weight = *wt;   /* statistical weight */
1371       if(p->ix > 0) p->x = *x; /* position in cartesian coordinates*/
1372       if(p->iy > 0) p->y = *y;
1373       if(p->iz > 0) p->z = *z;
1374       if(p->iu > 0) p->u = *u; /* direction in cartesian coordinates*/
1375       if(p->iv > 0) p->v = *v;
1376       if(p->iw > 0) p->w = *w;
1377 
1378       for(int k=0;k<p->iextrafloat;k++) p->extrafloat[k] = extra_floats[k];
1379       for(int j=0;j<p->iextralong ;j++)  p->extralong[j] = extra_ints[j];
1380 
1381       if( p->write_particle() == FAIL ) {*n_stat = -1; return;}
1382 
1383       /*
1384         Updating counters including:
1385         Min and Max Weight per particle,
1386         Total Weight per particle,
1387         Min and Max Kinetic energy per particle,
1388         Min and Max X,Y,Z coordinates,
1389         Total number of particles,
1390         Total number of each particle type
1391         Number of statistically independent histories so far
1392       */
1393       p_iaea_header[*id]->update_counters(p_iaea_record[*id]);
1394 
1395       return;
1396 }
1397 IAEA_EXTERN_C IAEA_EXPORT
1398 void iaea_write_particle_(const IAEA_I32 *id, IAEA_I32 *n_stat,
1399 const IAEA_I32 *type, /* particle type */
1400 const IAEA_Float *E,  /* kinetic energy in MeV */
1401 const IAEA_Float *wt, /* statistical weight */
1402 const IAEA_Float *x,
1403 const IAEA_Float *y,
1404 const IAEA_Float *z,  /* position in cartesian coordinates*/
1405 const IAEA_Float *u,
1406 const IAEA_Float *v,
1407 const IAEA_Float *w,  /* direction in cartesian coordinates*/
1408 const IAEA_Float *extra_floats,
1409 const IAEA_I32 *extra_ints)
1410 { iaea_write_particle(id, n_stat, type,
1411                                 E, wt, x, y, z, u, v, w, extra_floats, extra_ints); }
1412 IAEA_EXTERN_C IAEA_EXPORT
1413 void iaea_write_particle__(const IAEA_I32 *id, IAEA_I32 *n_stat,
1414 const IAEA_I32 *type, /* particle type */
1415 const IAEA_Float *E,  /* kinetic energy in MeV */
1416 const IAEA_Float *wt, /* statistical weight */
1417 const IAEA_Float *x,
1418 const IAEA_Float *y,
1419 const IAEA_Float *z,  /* position in cartesian coordinates*/
1420 const IAEA_Float *u,
1421 const IAEA_Float *v,
1422 const IAEA_Float *w,  /* direction in cartesian coordinates*/
1423 const IAEA_Float *extra_floats,
1424 const IAEA_I32 *extra_ints)
1425 { iaea_write_particle(id, n_stat, type,
1426                                 E, wt, x, y, z, u, v, w, extra_floats, extra_ints); }
1427 IAEA_EXTERN_C IAEA_EXPORT
1428 void IAEA_WRITE_PARTICLE(const IAEA_I32 *id, IAEA_I32 *n_stat,
1429 const IAEA_I32 *type, /* particle type */
1430 const IAEA_Float *E,  /* kinetic energy in MeV */
1431 const IAEA_Float *wt, /* statistical weight */
1432 const IAEA_Float *x,
1433 const IAEA_Float *y,
1434 const IAEA_Float *z,  /* position in cartesian coordinates*/
1435 const IAEA_Float *u,
1436 const IAEA_Float *v,
1437 const IAEA_Float *w,  /* direction in cartesian coordinates*/
1438 const IAEA_Float *extra_floats,
1439 const IAEA_I32 *extra_ints)
1440 { iaea_write_particle(id, n_stat, type,
1441                                 E, wt, x, y, z, u, v, w, extra_floats, extra_ints); }
1442 IAEA_EXTERN_C IAEA_EXPORT
1443 void IAEA_WRITE_PARTICLE_(const IAEA_I32 *id, IAEA_I32 *n_stat,
1444 const IAEA_I32 *type, /* particle type */
1445 const IAEA_Float *E,  /* kinetic energy in MeV */
1446 const IAEA_Float *wt, /* statistical weight */
1447 const IAEA_Float *x,
1448 const IAEA_Float *y,
1449 const IAEA_Float *z,  /* position in cartesian coordinates*/
1450 const IAEA_Float *u,
1451 const IAEA_Float *v,
1452 const IAEA_Float *w,  /* direction in cartesian coordinates*/
1453 const IAEA_Float *extra_floats,
1454 const IAEA_I32 *extra_ints)
1455 { iaea_write_particle(id, n_stat, type,
1456                                 E, wt, x, y, z, u, v, w, extra_floats, extra_ints); }
1457 IAEA_EXTERN_C IAEA_EXPORT
1458 void IAEA_WRITE_PARTICLE__(const IAEA_I32 *id, IAEA_I32 *n_stat,
1459 const IAEA_I32 *type, /* particle type */
1460 const IAEA_Float *E,  /* kinetic energy in MeV */
1461 const IAEA_Float *wt, /* statistical weight */
1462 const IAEA_Float *x,
1463 const IAEA_Float *y,
1464 const IAEA_Float *z,  /* position in cartesian coordinates*/
1465 const IAEA_Float *u,
1466 const IAEA_Float *v,
1467 const IAEA_Float *w,  /* direction in cartesian coordinates*/
1468 const IAEA_Float *extra_floats,
1469 const IAEA_I32 *extra_ints)
1470 { iaea_write_particle(id, n_stat, type,
1471                                 E, wt, x, y, z, u, v, w, extra_floats, extra_ints); }
1472 
1473 /***************************************************************************
1474 * Destroy a source
1475 *
1476 * This function de-initializes the source with Id id, closing all open
1477 * files, deallocating memory, etc. Nothing happens if a source with that
1478 * id does not exist. Header is updated.
1479 ****************************************************************************/
1480 IAEA_EXTERN_C IAEA_EXPORT
1481 void iaea_destroy_source(const IAEA_I32 *source_ID, IAEA_I32 *result)
1482 {
1483    if(*source_ID > MAX_NUM_SOURCES) { *result = -98 ; return;} // Too big phsp ID
1484    if(*source_ID < 0)               { *result = -97 ; return;} // wrong ID number
1485 
1486    if(p_iaea_header[*source_ID]->fheader == NULL) {*result = -1; return;}
1487 
1488   /* Write an IAEA header */
1489    // For read-only files nothing happens
1490    p_iaea_header[*source_ID]->write_header();
1491 
1492    // Closing header file
1493    fclose(p_iaea_header[*source_ID]->fheader);
1494    // Deallocating IAEA phsp header
1495    free(p_iaea_header[*source_ID]);
1496 
1497    // Closing phsp file
1498    fclose(p_iaea_record[*source_ID]->p_file);
1499    // Deallocating IAEA record
1500    free(p_iaea_record[*source_ID]);
1501 
1502    __iaea_source_used[*source_ID] = false;
1503 
1504    *result = 1; // Return OK
1505 
1506    return;
1507 }
1508 IAEA_EXTERN_C IAEA_EXPORT
1509 void iaea_destroy_source_(const IAEA_I32 *source_ID, IAEA_I32 *result)
1510 { iaea_destroy_source(source_ID, result); }
1511 IAEA_EXTERN_C IAEA_EXPORT
1512 void iaea_destroy_source__(const IAEA_I32 *source_ID, IAEA_I32 *result)
1513 { iaea_destroy_source(source_ID, result); }
1514 IAEA_EXTERN_C IAEA_EXPORT
1515 void IAEA_DESTROY_SOURCE(const IAEA_I32 *source_ID, IAEA_I32 *result)
1516 { iaea_destroy_source(source_ID, result); }
1517 IAEA_EXTERN_C IAEA_EXPORT
1518 void IAEA_DESTROY_SOURCE_(const IAEA_I32 *source_ID, IAEA_I32 *result)
1519 { iaea_destroy_source(source_ID, result); }
1520 IAEA_EXTERN_C IAEA_EXPORT
1521 void IAEA_DESTROY_SOURCE__(const IAEA_I32 *source_ID, IAEA_I32 *result)
1522 { iaea_destroy_source(source_ID, result); }
1523 
1524 /***************************************************************************
1525 * Print the current header associated to source id
1526 *
1527 * result is set to negative if phsp source does not exist.
1528 ****************************************************************************/
1529 IAEA_EXTERN_C IAEA_EXPORT
1530 void iaea_print_header(const IAEA_I32 *source_ID, IAEA_I32 *result)
1531 {
1532    if(p_iaea_header[*source_ID]->fheader == NULL) {*result = -1; return;}
1533 
1534   /* Print current IAEA header for source id */
1535    p_iaea_header[*source_ID]->print_header();
1536 
1537    *result = 1; // Return OK
1538 
1539    return;
1540 }
1541 IAEA_EXTERN_C IAEA_EXPORT
1542 void iaea_print_header_(const IAEA_I32 *source_ID, IAEA_I32 *result)
1543 { iaea_print_header(source_ID, result); }
1544 IAEA_EXTERN_C IAEA_EXPORT
1545 void iaea_print_header__(const IAEA_I32 *source_ID, IAEA_I32 *result)
1546 { iaea_print_header(source_ID, result); }
1547 IAEA_EXTERN_C IAEA_EXPORT
1548 void IAEA_PRINT_HEADER(const IAEA_I32 *source_ID, IAEA_I32 *result)
1549 { iaea_print_header(source_ID, result); }
1550 IAEA_EXTERN_C IAEA_EXPORT
1551 void IAEA_PRINT_HEADER_(const IAEA_I32 *source_ID, IAEA_I32 *result)
1552 { iaea_print_header(source_ID, result); }
1553 IAEA_EXTERN_C IAEA_EXPORT
1554 void IAEA_PRINT_HEADER__(const IAEA_I32 *source_ID, IAEA_I32 *result)
1555 { iaea_print_header(source_ID, result); }
1556 //IAEA_EXTERN_C IAEA_EXPORT //(MACG) to silent -Wduplicate-decl-specifier (bug?)
1557 
1558 /***************************************************************************
1559 * Copy header of the source_id to the header of the destiny_id
1560 *
1561 * result is set to negative if phsp source or destiny do not exist.
1562 ****************************************************************************/
1563 IAEA_EXTERN_C IAEA_EXPORT
1564 void iaea_copy_header(const IAEA_I32 *source_ID,
1565                                 const IAEA_I32 *destiny_ID, IAEA_I32 *result)
1566 {
1567    if(p_iaea_header[*source_ID]->fheader == NULL) {*result = -1; return;}
1568    if(p_iaea_header[*destiny_ID]->fheader == NULL) {*result = -1; return;}
1569 
1570    // Selective copy of string variables
1571 
1572       p_iaea_header[*destiny_ID]->checksum =
1573             p_iaea_header[*source_ID]->checksum ;
1574       p_iaea_header[*destiny_ID]->record_length =
1575             p_iaea_header[*source_ID]->record_length ;
1576       p_iaea_header[*destiny_ID]->byte_order =
1577             p_iaea_header[*source_ID]->byte_order ;
1578 
1579 // ******************************************************************************
1580 // 2. Mandatory description of the phsp
1581 
1582       strcpy(p_iaea_header[*destiny_ID]->coordinate_system_description,
1583             p_iaea_header[*source_ID]->coordinate_system_description) ;
1584 
1585       int file_type = p_iaea_header[*source_ID]->file_type;
1586       if(file_type == 1)
1587       {
1588             // For event generators
1589             strcpy(p_iaea_header[*destiny_ID]->input_file_for_event_generator,
1590                   p_iaea_header[*source_ID]->input_file_for_event_generator) ;
1591             *result = 1; // Return OK
1592             return;
1593       }
1594 
1595       p_iaea_header[*destiny_ID]->orig_histories =
1596             p_iaea_header[*source_ID]->orig_histories ;
1597 
1598 // ******************************************************************************
1599 // 3. Mandatory additional information
1600       /*********************************************/
1601       strcpy(p_iaea_header[*destiny_ID]->machine_type,
1602             p_iaea_header[*source_ID]->machine_type);
1603 
1604       strcpy(p_iaea_header[*destiny_ID]->MC_code_and_version,
1605             p_iaea_header[*source_ID]->MC_code_and_version);
1606 
1607       p_iaea_header[*destiny_ID]->global_photon_energy_cutoff =
1608             p_iaea_header[*source_ID]->global_photon_energy_cutoff;
1609 
1610       p_iaea_header[*destiny_ID]->global_particle_energy_cutoff =
1611             p_iaea_header[*source_ID]->global_particle_energy_cutoff;
1612 
1613       strcpy(p_iaea_header[*destiny_ID]->transport_parameters,
1614             p_iaea_header[*source_ID]->transport_parameters);
1615 
1616 // ******************************************************************************
1617 // 4. Optional description
1618       strcpy(p_iaea_header[*destiny_ID]->beam_name,
1619             p_iaea_header[*source_ID]->beam_name);
1620       strcpy(p_iaea_header[*destiny_ID]->field_size,
1621             p_iaea_header[*source_ID]->field_size);
1622       strcpy(p_iaea_header[*destiny_ID]->nominal_SSD,
1623             p_iaea_header[*source_ID]->nominal_SSD);
1624       strcpy(p_iaea_header[*destiny_ID]->variance_reduction_techniques,
1625             p_iaea_header[*source_ID]->variance_reduction_techniques);
1626       strcpy(p_iaea_header[*destiny_ID]->initial_source_description,
1627             p_iaea_header[*source_ID]->initial_source_description);
1628 
1629       // Documentation sub-section
1630       /*********************************************/
1631       strcpy(p_iaea_header[*destiny_ID]->MC_input_filename,
1632             p_iaea_header[*source_ID]->MC_input_filename);
1633       strcpy(p_iaea_header[*destiny_ID]->published_reference,
1634             p_iaea_header[*source_ID]->published_reference);
1635       strcpy(p_iaea_header[*destiny_ID]->authors,
1636             p_iaea_header[*source_ID]->authors);
1637       strcpy(p_iaea_header[*destiny_ID]->institution,
1638             p_iaea_header[*source_ID]->institution);
1639       strcpy(p_iaea_header[*destiny_ID]->link_validation,
1640             p_iaea_header[*source_ID]->link_validation);
1641       strcpy(p_iaea_header[*destiny_ID]->additional_notes,
1642             p_iaea_header[*source_ID]->additional_notes);
1643 
1644     *result = 1; // Return OK
1645 
1646     return;
1647 
1648 }
1649 IAEA_EXTERN_C IAEA_EXPORT
1650 void iaea_copy_header_(const IAEA_I32 *source_ID,
1651                                      const IAEA_I32 *destiny_ID,
1652                                            IAEA_I32 *result)
1653 { iaea_copy_header(source_ID, destiny_ID, result); }
1654 IAEA_EXTERN_C IAEA_EXPORT
1655 void iaea_copy_header__(const IAEA_I32 *source_ID,
1656                                      const IAEA_I32 *destiny_ID,
1657                                            IAEA_I32 *result)
1658 { iaea_copy_header(source_ID, destiny_ID, result); }
1659 IAEA_EXTERN_C IAEA_EXPORT
1660 void IAEA_COPY_HEADER(const IAEA_I32 *source_ID,
1661                                      const IAEA_I32 *destiny_ID,
1662                                            IAEA_I32 *result)
1663 { iaea_copy_header(source_ID, destiny_ID, result); }
1664 IAEA_EXTERN_C IAEA_EXPORT
1665 void IAEA_COPY_HEADER_(const IAEA_I32 *source_ID,
1666                                      const IAEA_I32 *destiny_ID,
1667                                            IAEA_I32 *result)
1668 { iaea_copy_header(source_ID, destiny_ID, result); }
1669 IAEA_EXTERN_C IAEA_EXPORT
1670 void IAEA_COPY_HEADER__(const IAEA_I32 *source_ID,
1671                                      const IAEA_I32 *destiny_ID,
1672                                            IAEA_I32 *result)
1673 { iaea_copy_header(source_ID, destiny_ID, result); }
1674 
1675 
1676 /***************************************************************************
1677 * Update header of the source_id
1678 *
1679 * result is set to negative if phsp source does not exist.
1680 ****************************************************************************/
1681 IAEA_EXTERN_C IAEA_EXPORT
1682 void iaea_update_header(const IAEA_I32 *source_ID, IAEA_I32 *result)
1683 {
1684    if(p_iaea_header[*source_ID]->fheader == NULL) {*result = -1; return;}
1685 
1686   /* Write an IAEA header */
1687    // For read-only files nothing happens
1688    p_iaea_header[*source_ID]->write_header();
1689 
1690    *result = 1; // Return OK
1691    return;
1692 
1693 }
1694 IAEA_EXTERN_C IAEA_EXPORT
1695 void iaea_update_header_(const IAEA_I32 *source_ID, IAEA_I32 *result)
1696 { iaea_update_header(source_ID, result); }
1697 IAEA_EXTERN_C IAEA_EXPORT
1698 void iaea_update_header__(const IAEA_I32 *source_ID, IAEA_I32 *result)
1699 { iaea_update_header(source_ID, result); }
1700 IAEA_EXTERN_C IAEA_EXPORT
1701 void IAEA_UPDATE_HEADER(const IAEA_I32 *source_ID, IAEA_I32 *result)
1702 { iaea_update_header(source_ID, result); }
1703 IAEA_EXTERN_C IAEA_EXPORT
1704 void IAEA_UPDATE_HEADER_(const IAEA_I32 *source_ID, IAEA_I32 *result)
1705 { iaea_update_header(source_ID, result); }
1706 IAEA_EXTERN_C IAEA_EXPORT
1707 void IAEA_UPDATE_HEADER__(const IAEA_I32 *source_ID, IAEA_I32 *result)
1708 { iaea_update_header(source_ID, result); }