File indexing completed on 2026-04-18 07:41:08
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047 #if (defined WIN32) || (defined WIN64)
0048 #include <iostream> // so that namespace std becomes defined
0049 #endif
0050 #include <math.h>
0051 #include <cstdio>
0052
0053 #if !(defined WIN32) && !(defined WIN64)
0054 using namespace std;
0055 #endif
0056
0057 #include "iaea_record.h"
0058
0059 short iaea_record_type::initialize()
0060 {
0061 if(p_file == NULL) {
0062 fprintf(stderr, "\n ERROR: Failed to open Phase Space file \n");
0063 return (FAIL);
0064 }
0065
0066
0067
0068 ix = 1;
0069 iy = 1;
0070 iz = 1;
0071 iu = 1;
0072 iv = 1;
0073 iw = 1;
0074 iweight = 1;
0075
0076
0077 iextralong = 1;
0078 if( iextralong >= NUM_EXTRA_LONG)
0079 {
0080 fprintf(stderr, "\n ERROR: Increase NUM_EXTRA_LONG number in iaea_record.h\n");
0081 return (FAIL);
0082 }
0083
0084 iextrafloat = 0;
0085 if( iextrafloat >= NUM_EXTRA_FLOAT)
0086 {
0087 fprintf(stderr, "\n ERROR: Increase NUM_EXTRA_FLOAT number in iaea_record.h\n");
0088 return (FAIL);
0089 }
0090
0091 return (OK);
0092 }
0093
0094 short iaea_record_type::write_particle()
0095 {
0096 float floatArray[NUM_EXTRA_FLOAT+7];
0097 IAEA_I32 longArray[NUM_EXTRA_LONG];
0098
0099 char ishort = (char) particle;
0100 if(w < 0) ishort = -ishort;
0101
0102 if( fwrite(&ishort, sizeof(char), 1, p_file) != 1)
0103 {
0104 fprintf(stderr, "\n ERROR: write_particle: Failed to write particle type\n");
0105 return (FAIL);;
0106 }
0107
0108 int reclength = sizeof(char);
0109
0110 if(IsNewHistory > 0) energy *= (-1);
0111
0112 floatArray[0] = energy;
0113
0114 int i = 0;
0115
0116 if(ix > 0) floatArray[++i] = x;
0117 if(iy > 0) floatArray[++i] = y;
0118 if(iz > 0) floatArray[++i] = z;
0119 if(iu > 0) floatArray[++i] = u;
0120 if(iv > 0) floatArray[++i] = v;
0121 if(iweight > 0) floatArray[++i] = weight;
0122
0123 int j;
0124 for(j=0;j<iextrafloat;j++) floatArray[++i] = extrafloat[j];
0125
0126 reclength += (i+1)*sizeof(float);
0127
0128 if( fwrite(floatArray, sizeof(float), (size_t)(i+1), p_file) != (size_t) (i+1))
0129 {
0130 fprintf(stderr, "\n ERROR: write_particle: Failed to write FLOAT phsp data\n");
0131 return (FAIL);
0132 }
0133
0134 if(iextralong > 0)
0135 {
0136 for(j=0;j<iextralong;j++) longArray[j] = extralong[j];
0137 reclength += iextralong*sizeof(IAEA_I32);
0138 if( fwrite(longArray, sizeof(IAEA_I32), (size_t)iextralong, p_file) != (size_t)iextralong)
0139 {
0140 fprintf(stderr, "\n ERROR: write_particle: Failed to write LONG phsp data\n");
0141 return (FAIL);
0142 }
0143 }
0144
0145 if(reclength == 0) return(FAIL);
0146
0147 #ifdef DEBUG
0148
0149 int iaea_charge[MAX_NUM_PARTICLES]={0,-1,+1,0,+1};
0150 int charge = iaea_charge[particle - 1];
0151
0152 printf("\n Wrote a particle with a record lenght %d",reclength);
0153 printf("\n Q %d E %f X %f Y %f Z %f \n\t u %f v %f w %f W %f Part %d \n",
0154 charge, energy, x, y, z, u, v, w, weight, particle);
0155 if( iextrafloat > 0) printf(" EXTRA FLOATs:");
0156 for(j=0;j<iextrafloat;j++) printf(" F%i %f",j+1,extrafloat[j]);
0157 if( iextralong > 0) printf(" EXTRA LONGs:");
0158 for(j=0;j<iextralong;j++) printf(" L%i %d", j+1,extralong[j]);
0159 printf("\n");
0160 #endif
0161
0162 return(OK);
0163 }
0164
0165 short iaea_record_type::read_particle()
0166 {
0167 float floatArray[NUM_EXTRA_FLOAT+7];
0168 IAEA_I32 longArray[NUM_EXTRA_LONG+7];
0169
0170 int i,j,l,is,reclength;
0171 char ctmp;
0172
0173
0174
0175 if( fread(&ctmp, sizeof(char), 1, p_file) != 1)
0176 {
0177 fprintf(stderr, "\n ERROR: read_particle: Failed to read particle type\n");
0178 return (FAIL);;
0179 }
0180
0181 particle = (short) ctmp;
0182
0183 is = 1;
0184 if(particle < 0) {is = -1; particle = -particle;}
0185
0186 reclength = sizeof(char);
0187 unsigned int rec_to_read = 1;
0188
0189 if(ix > 0) rec_to_read++;
0190 if(iy > 0) rec_to_read++;
0191 if(iz > 0) rec_to_read++;
0192 if(iu > 0) rec_to_read++;
0193 if(iv > 0) rec_to_read++;
0194 if(iweight > 0) rec_to_read++;
0195 if(iextrafloat>0) rec_to_read += iextrafloat;
0196
0197 if( fread(floatArray, sizeof(float), rec_to_read, p_file) != rec_to_read)
0198 {
0199 fprintf(stderr, "\n ERROR: read_particle: Failed to read FLOATs \n");
0200 return (FAIL);;
0201 }
0202
0203 reclength += rec_to_read*sizeof(float);
0204
0205
0206 IsNewHistory = 0;
0207 if(floatArray[0]<0) IsNewHistory = 1;
0208 energy = fabs(floatArray[0]);
0209
0210 i = 0;
0211 if(ix > 0) x = floatArray[++i];
0212 if(iy > 0) y = floatArray[++i];
0213 if(iz > 0) z = floatArray[++i];
0214 if(iu > 0) u = floatArray[++i];
0215 if(iv > 0) v = floatArray[++i];
0216 if(iweight > 0) weight = floatArray[++i];
0217 for(j=0;j<iextrafloat;j++) extrafloat[j] = floatArray[++i];
0218
0219 if(iw > 0)
0220 {
0221 w = 0.f;
0222 double aux = (u*u + v*v);
0223 if (aux<=1.0) w = (float) (is * sqrt((float)(1.0 - aux)));
0224 else
0225 {
0226 aux = sqrt((float)aux);
0227 u /= (float)aux;
0228 v /= (float)aux;
0229 }
0230 }
0231
0232 if(iextralong > 0)
0233 {
0234 if( fread(longArray, sizeof(IAEA_I32), (size_t)iextralong, p_file) != (size_t)iextralong)
0235 {
0236 fprintf(stderr, "\n ERROR: read_particle: Failed to read LONGS\n");
0237 return (FAIL);
0238 }
0239
0240 for(l=0,j=0;j<iextralong;j++) extralong[j] = longArray[l++];
0241 reclength += (iextralong)*sizeof(IAEA_I32);
0242 }
0243
0244 #ifdef DEBUG
0245
0246 int iaea_charge[MAX_NUM_PARTICLES]={0,-1,+1,0,+1};
0247 int charge = iaea_charge[particle - 1];
0248
0249 printf("\n Read a particle with a record lenght %d (New History: %d)",
0250 reclength,IsNewHistory);
0251 printf("\n Q %d E %f X %f Y %f Z %f \n\t u %f v %f w %f W %f Part %d \n",
0252 charge, energy, x, y, z, u, v, w, weight, particle);
0253 if( iextrafloat > 0) printf(" EXTRA FLOATs:");
0254 for(j=0;j<iextrafloat;j++) printf(" F%i %f",j+1,extrafloat[j]);
0255 if( iextralong > 0) printf(" EXTRA LONGs:");
0256 for(j=0;j<iextralong;j++) printf(" L%i %d",j+1,extralong[j]);
0257 printf("\n");
0258 #endif
0259 return(reclength);
0260 }