File: MCPL_input.comp

package info (click to toggle)
mccode 3.5.19%2Bds5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,113,256 kB
  • sloc: ansic: 40,697; python: 25,137; yacc: 8,438; sh: 5,405; javascript: 4,596; lex: 1,632; cpp: 742; perl: 296; lisp: 273; makefile: 226; fortran: 132
file content (432 lines) | stat: -rw-r--r-- 13,969 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*******************************************************************************
*
* McXtrace, x-ray tracing package
*         Copyright, All rights reserved
*         DTU Physics, Kgs. Lyngby, Denmark
*         Synchrotron SOLEIL, Saint-Aubin, France
*
* Component: MCPL_input
*
* %Identification
* Written by: Erik B Knudsen
* Date: Aug 2016
* Origin: DTU Physics
*
* Source-like component that reads photon state parameters from an mcpl-file.
*
* %Description
* Source-like component that reads photon state parameters from a binary mcpl-file.
*
* MCPL is short for Monte Carlo Particle List, and is a new format for sharing events
* between e.g. MCNP(X), Geant-4 and McXtrace .
*
* When used with MPI, the --ncount given on the commandline is overwritten by 
* #MPI nodes x #events in the file.
*
* Example: MCPL_input(filename=voutput.mcpl,verbose=1,repeat_count=1,E_smear=0.1,pos_smear=0.001,dir_smear=0.01)
*
* %Parameters
* INPUT PARAMETERS
*
* filename: [str]       Name of photon mcpl file to read.
* verbose: [ ]          Print debugging information for first 10 particles read.
* polarisationuse: [ ]  If !=0 read polarisation vectors from file.
* Emin: [keV]           Lower energy bound. Particles found in the MCPL-file below the limit are skipped.
* Emax: [keV]           Upper energy bound. Particles found in the MCPL-file above the limit are skipped.
* repeat_count: [1]     Repeat contents of the MCPL file this number of times. NB: When running MPI, repeating is implicit and is taken into account by integer division. Should be combined sith the _smear options!
* E_smear: [1]          When repeating events, make a Gaussian MC choice within E_smear*E around particle energy E
* pos_smear: [m]        When repeating events, make a flat MC choice of position within pos_smear around particle starting position
* dir_smear: [deg]      When repeating events, make a Gaussian MC choice of direction within dir_smear around particle direction
*
* %End
*******************************************************************************/

DEFINE COMPONENT MCPL_input

SETTING PARAMETERS (string filename=0, polarisationuse=1,verbose=1, Emin=0, Emax=FLT_MAX, int repeat_count=1, E_smear=0, pos_smear=0, dir_smear=0)

DEPENDENCY "-Wl,-rpath,CMD(mcpl-config --show libdir) -LCMD(mcpl-config --show libdir) -lmcpl -ICMD(mcpl-config --show includedir)"

SHARE
%{
#include <mcpl.h>
%}

DECLARE
%{
mcpl_file_t inputfile;
long long nparticles;
long long read_photons;
long long used_photons;
int repeat_cnt;
int repeating;
int ismpislave;
#ifdef OPENACC
DArray1d X;
DArray1d Y;
DArray1d Z;
DArray1d KX;
DArray1d KY;
DArray1d KZ;
DArray1d EX;
DArray1d EY;
DArray1d EZ;
DArray1d T;
DArray1d P;
#endif
%}

INITIALIZE
%{
    char line[256];
    long long ncount;

    if(Emax<Emin){
        fprintf(stderr,"Warning(%s): Nonsensical energy interval: E=[%g,%g]. Aborting.\n",NAME_CURRENT_COMP,Emin,Emax);
        exit(-1);
    }
    /* No need to check if the file opens correctly since mcpl will
     * abort internally if it cannot open the file.*/
    inputfile = mcpl_open_file(filename);

  
    if ( !(nparticles=mcpl_hdr_nparticles(inputfile)) ) {
        fprintf(stderr,"Warning(%s): MCPL-file reports no present particles. Foolishly trying to go on.\n",NAME_CURRENT_COMP);
        #ifndef OPENACC
        nparticles=ncount;
        #endif
    }else{
        printf("Message(%s): MCPL file (%s) produced with %s.\n",NAME_CURRENT_COMP,filename,mcpl_hdr_srcname(inputfile));
        printf("Message(%s): MCPL file (%s) contains %lu particles.\n",NAME_CURRENT_COMP,filename,(long unsigned)nparticles); 
    }
    mcset_ncount(nparticles);

    if(repeat_count>1 && E_smear==0 && pos_smear==0 && dir_smear==0) {
      fprintf(stdout, "\n\n WARNING: You have requested a repeat_count=%i but have left all *_smear parameters at 0!\n --> This is not allowed! Resetting to repeat_count=1!\n",repeat_count);
      fprintf(stderr, "\n\n WARNING: You have requested a repeat_count=%i but have left all *_smear parameters at 0!\n --> This is not allowed! Resetting to repeat_count=1!\n",repeat_count);
      repeat_count=1;
      sleep(10);
    }

    if(repeat_count==0) repeat_count=1;
    repeat_cnt = repeat_count;
    ismpislave=0;
#if defined (USE_MPI)
    repeat_cnt = ceil(1.0*repeat_cnt/mpi_node_count);
    ismpislave = mpi_node_rank;
    MPI_MASTER(
#endif
	     fprintf(stdout, "\n\n Warning: You are using MCPL_input with a repeat_count of %lu:\n - Minimum photon count requested is %lu x %lu <= %lu",
	       (long unsigned)repeat_count,(long unsigned)nparticles,
	       (long unsigned)repeat_count,(long unsigned)repeat_cnt*nparticles); 
#if defined (USE_MPI)
  fprintf(stdout, " x %i MPI nodes = %lu photons total\n",
    mpi_node_count,(long unsigned)mpi_node_count*repeat_cnt*nparticles);
     );
  mcset_ncount(mpi_node_count*repeat_cnt*nparticles);
#else
  fprintf(stdout, " photons total\n\n");
  mcset_ncount(repeat_cnt*nparticles);
#endif

    ncount=mcget_ncount();
    fprintf(stdout,"Initialize ncount is %lu\n",(long unsigned)ncount);
    read_photons=0;
    used_photons=0;

#if defined (USE_MPI)   
    MPI_MASTER(
#endif

      if (verbose==1) {
        printf("MCPL_input verbose mode - outputting data on the 10 first read photons in MCPL units:\n");
      }

#if defined (USE_MPI) 
    );
#endif
    repeating = 0;
#ifdef OPENACC
      X = create_darr1d(nparticles);
      Y = create_darr1d(nparticles);
      Z = create_darr1d(nparticles);
      KX = create_darr1d(nparticles);
      KY = create_darr1d(nparticles);
      KZ = create_darr1d(nparticles);
      if(polarisationuse){
	EX = create_darr1d(nparticles);
      	EY = create_darr1d(nparticles);
      	EZ = create_darr1d(nparticles);
      }
      T = create_darr1d(nparticles);
      P = create_darr1d(nparticles);
      printf("Initiating file read...\n");
      int loop;
      for (loop=0; loop < nparticles ; loop++) {
	const mcpl_particle_t *particle;
	particle=mcpl_read(inputfile);
	if (particle) {
	  if (particle->pdgcode==22) {
	    if (verbose && read_photons<11) {
	      printf("id=%ld pdg=22\tekin=%g MeV\tx=%g cm\ty=%g cm\tz=%g cm\tux=%g\tuy=%g\tuz=%g\tt=%g ms\tweight=%g\tpolx=%g\tpoly=%g\tpolz=%g\n",
		     (long unsigned)read_photons, particle->ekin, particle->position[0], particle->position[1], particle->position[2],
		     particle->direction[0], particle->direction[1], particle->direction[2], particle->time, particle->weight,
		     particle->polarisation[0], particle->polarisation[1], particle->polarisation[2]);
	    }
	    /* check energy range*/
	    if ( particle->ekin>Emin*1e-9 || particle->ekin<Emax*1e-9 ) {
	      /* Particle energy in range */
	      /*positions are in cm*/
	      X[read_photons]=particle->position[0]/100;
	      Y[read_photons]=particle->position[1]/100;
	      Z[read_photons]=particle->position[2]/100;

	      if(polarisationuse){
		EX[read_photons]=(double)particle->polarisation[0];
		EY[read_photons]=(double)particle->polarisation[1];
		EZ[read_photons]=(double)particle->polarisation[2];
	      }else{
		EX[read_photons]=0;
		EY[read_photons]=0;
		EZ[read_photons]=0;
	      }
	      double nrm;
              nrm = particle->ekin *1e3*E2K;
              nrm = sqrt(nrm);

	      double d0=particle->direction[0],d1=particle->direction[1],d2=particle->direction[2];

	      KX[read_photons]=d0*nrm;
	      KY[read_photons]=d1*nrm;
	      KZ[read_photons]=d2*nrm;

	      /*time in ms:*/
	      T[read_photons] = particle->time*1e-3;
	      /*weight in unspecified units:*/
	      P[read_photons] = particle->weight;
	      read_photons++;
	    }
	  }
	}
      }
      printf("Done reading MCPL file, found %ld photons\n",(long unsigned)read_photons);
      mcpl_close_file(inputfile);
      fprintf(stdout, "\n\n Warning: You are using MCPL_input with a repeat_count of %lu:\n - Minimum photon count requested is %lu x %lu <= %lu",
	      (long unsigned)repeat_count,(long unsigned)read_photons,
	      (long unsigned)repeat_count,(long unsigned)repeat_cnt*read_photons);
      fprintf(stdout, " photons total\n\n");
      mcset_ncount(repeat_cnt*read_photons);

      ncount=mcget_ncount();
      fprintf(stdout,"Initialize ncount is %lu\n",(long unsigned)ncount);
#endif
%}

TRACE
%{
#ifndef OPENACC
    long long ncount;

    double nrm;
    const mcpl_particle_t *particle;// = (mcpl_particle_t *) calloc(sizeof(mcpl_particle_t),1);
    particle = mcpl_read(inputfile);

    ncount=mcget_ncount();
    // fprintf(stdout,"Trace ncount is %ld\n",ncount);      
    if (!particle) {
      if(repeat_cnt>1) {
	/* Trigger rewind of the file and ABSORB to get the first photon "again" */
	repeating++;
	mcpl_rewind(inputfile);
	particle = mcpl_read(inputfile);
#if defined (USE_MPI)   
	MPI_MASTER(
#endif
       printf("MCPL inputfile %s rewound %i time(s)\n",filename,repeating);
#if defined (USE_MPI) 
    );
#endif
      } else
	ABSORB;
    }
    if (particle->pdgcode!=22) {
        /*Either no particle read, particle is not a photon, or it has invalid energy - terminate to trigger next ray*/
       ABSORB;
    }
    read_photons++;

    /* check energy range*/
    if ( particle->ekin<Emin*1e-3 || particle->ekin>Emax*1e-3 ) {
        /*Particle energy out of range - terminate to trigger next ray*/
        ABSORB;
    }
    used_photons++;

#if defined (USE_MPI)   
  MPI_MASTER(
#endif

    if (verbose && used_photons<11) {
      printf("id=%ld\tpdg=22\tekin=%g MeV\tx=%g cm\ty=%g cm\tz=%g cm\tux=%g\tuy=%g\tuz=%g\tt=%g ms\tweight=%g\tpolx=%g\tpoly=%g\tpolz=%g\n",
	     read_photons, particle->ekin, particle->position[0], particle->position[1], particle->position[2],
	     particle->direction[0], particle->direction[1], particle->direction[2], particle->time, particle->weight,
	     particle->polarisation[0], particle->polarisation[1], particle->polarisation[2]);
    }

#if defined (USE_MPI)   
	    );
#endif    

    /*positions are in cm*/
    x=particle->position[0]/100;
    y=particle->position[1]/100;
    z=particle->position[2]/100;
    
    if (ismpislave || repeating) {
      double tmpx,tmpy,tmpz;
      // Position-MC:
      randvec_target_circle(&tmpx, &tmpy, &tmpz, NULL, 0, 0, 1, 0);
      NORM(tmpx,tmpy,tmpz);
      tmpx *= pos_smear*rand01(); tmpy *= pos_smear*rand01(); tmpz *= pos_smear*rand01();
      x+=tmpx; y+=tmpy; z+=tmpz;
    }
    
    if(polarisationuse){
        Ex=particle->polarisation[0];
        Ey=particle->polarisation[1];
        Ez=particle->polarisation[2];
    }else{
        Ex=Ey=Ez=0;
    }
    nrm = particle->ekin *1e3*E2K;
    nrm = sqrt(nrm);
    if (ismpislave || repeating) {
      // Energy-MC:
      double tmp=(1.0+E_smear*randpm1());
      //printf("Adjusting energy from %g to",nrm);
      nrm *= (1+E_smear*randpm1());
      //printf(" to %g\n",nrm);
    }
    double d0=particle->direction[0],d1=particle->direction[1],d2=particle->direction[2];
    
    if (ismpislave || repeating) {
      // Direction-MC:
      double tmpx,tmpy,tmpz;
      // Position-MC:
      randvec_target_circle(&d0, &d1, &d2, NULL, particle->direction[0], particle->direction[1], particle->direction[2], sin(dir_smear*DEG2RAD));
      NORM(d0,d1,d2);
    }
    
    kx=d0*nrm;
    ky=d1*nrm;
    kz=d2*nrm;   

    /*time in ms:*/
    t=particle->time*1e-3;
    /*weight in unspecified units:*/
    p=particle->weight;

    /* Correct for repetition, by repeat_count and/or MPI */
    p /= repeat_cnt;
#if defined (USE_MPI)   
    p /= mpi_node_count;
#endif
#else
  unsigned long long i=_particle->_uid;
  if (i>=nparticles) {
    repeating=1;
    i = i % nparticles;
  }
  x=X[i];
  y=Y[i];
  z=Z[i];
  kx=KX[i];
  ky=KY[i];
  kz=KZ[i];
  Ex=EX[i];
  Ey=EY[i];
  Ez=EZ[i];
  t=T[i];
  /*MCPL does not store photon phase information.*/
  phi=0;
  p=P[i];
  if (repeat_cnt>1) p = p /repeat_cnt;

  if (repeating) {
    /* Position smearing */
    double tmpx,tmpy,tmpz;
    // Position-MC:
    randvec_target_circle(&tmpx, &tmpy, &tmpz, NULL, 0, 0, 1, 0);
    NORM(tmpx,tmpy,tmpz);
    tmpx *= pos_smear*rand01(); tmpy *= pos_smear*rand01(); tmpz *= pos_smear*rand01();
    x+=tmpx; y+=tmpy; z+=tmpz;

    double d0=kx,d1=ky,d2=kz;
    /* Direction smearing: */
    randvec_target_circle(&d0, &d1, &d2, NULL, kx, ky, kz, sin(dir_smear*DEG2RAD));
    NORM(d0,d1,d2);

    /* Energy smearing: */
    double tmpe=(1.0+E_smear*randpm1());
    double nrm=K2E*sqrt(kx*kx+ky*ky+kz*kz);
    nrm *= (1.0+E_smear*randpm1());
    nrm = K2E*nrm;

    kx=nrm*d0;
    ky=nrm*d1;
    kz=nrm*d2;
  }
#endif
  SCATTER;
%}

SAVE
%{
    #ifndef OPENACC
    mcpl_close_file(inputfile);
    #endif
%}

FINALLY
%{
    long long ncount;

    ncount=mcget_ncount();

    if (used_photons!=read_photons){
        fprintf(stdout,"Message(%s): You have used %lu of %lu photons available in the MCPL file.\n",NAME_CURRENT_COMP,
          (long unsigned)used_photons,(long unsigned)read_photons);
    }
    if (ncount != used_photons){
        fprintf(stderr,"Warning (%s): You requested %ld photons from a file which contains %lu particles in general, of which only %lu are photons (within the wanted energy interval).\n"
                "Please examine the recorded intensities carefully.\n",
          NAME_CURRENT_COMP,(long unsigned)ncount,(long unsigned)nparticles,(long unsigned)used_photons);
    }
    
    destroy_darr1d(X);
    destroy_darr1d(Y);
    destroy_darr1d(Z);
    destroy_darr1d(KX);
    destroy_darr1d(KY);
    destroy_darr1d(KZ);
    if(polarisationuse){
      destroy_darr1d(EX);
      destroy_darr1d(EY);
      destroy_darr1d(EZ);
    }
    destroy_darr1d(T);
    destroy_darr1d(P);
%}

MCDISPLAY
%{
    multiline(5, 0.2,0.2,0.0, -0.2,0.2,0.0, -0.2,-0.2,0.0, 0.2,-0.2,0.0, 0.2,0.2,0.0);
    /*M*/
    multiline(5,-0.085,-0.085,0.0, -0.085,0.085,0.0, -0.045,-0.085,0.0, -0.005,0.085,0.0, -0.005,-0.085,0.0);
    /*I*/
    line(0.045,-0.085,0, 0.045, 0.085,0);    
    line(0.005, 0.085,0, 0.085, 0.085,0);    
    line(0.005,-0.085,0, 0.085,-0.085,0);    
%}

END