File: MultiDiskChopper.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 (350 lines) | stat: -rw-r--r-- 13,518 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
/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2015, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Component: MultiDiskChopper
*
* %I
* Written by: Markus Appel
* Date: 2015-10-19
* Origin: ILL / FAU Erlangen-Nuernberg
* Based on DiskChopper (Revision 1.18) by Peter Willendrup (2006), 
* which in turn is based on Chopper (Philipp Bernhardt), Jitter and beamstop from work by 
* Kaspar Hewitt Klenoe (jan 2006), adjustments by Rob Bewey (march 2006)
*
* %D
* Models a disk chopper with a freely configurable slit pattern. For simple applications,
* use the DiskChopper component and see the component manual example of DiskChopper GROUPing.
* If the chopper slit pattern should be dynamically configurable or a complicated pattern
* is to be used as first chopper on a continuous source, use this component.
*
* Width and position of the slits is defined as a list in string parameters so
* they can easily be taken from instrument parameters.
* The chopper axis is located on the y axis as defined by the parameter delta_y.
* When the chopper is the first chopper after a continuous (i.e. time-independent) 
* source, the parameter isfirst should be set to 1 to increase Monte-Carlo efficiency.
* 
*
* Examples (see parameter definitions for details): 
* Two opposite slits with 10 and 20deg opening, with the 20deg slit in the beam at t=0.02:
* MultiDiskChopper(radius=0.2, slit_center="0;180", slit_width="10;20", delta_y=-0.1, 
*                  nu=302, nslits=2, phase=180, delay=0.02)
*
* First chopper on a continuous source, creating pulse trains for one additional revolution
* before and after the revolution at t=0:
* MultiDiskChopper(radius=0.2, slit_center="0;180", slit_width="10;20", delta_y=-0.1, 
*                  nu=302, nslits=2, phase=180, isfirst=1, nrev=1)
*
* %P
* INPUT PARAMETERS:
*
* slit_width: [string]  (deg)   Angular width of the slits, given as list in a string separated by space ' ', comma ',', underscore '_' or semicolon ';'. Example: "0;20;90;135;270"
* slit_center: [string] (deg)   Angular position of the slits (similar to slit_width)
* nslits: []    Number of slits to read from slit_width and slit_center
* radius: [m]   Outer radius of the disk
* delta_y: [m]  y-position of the chopper rotation axis. If the chopper is located above the guide (delta_y>0), the coordinate system will be mirrored such that the created pulse pattern in time is the same as for delta_y<0. A warning will be printed in this case.
* nu:                   [Hz]    Rotation speed of the disk, the sign determines the direction.
*
* Optional parameters:
* verbose:              [0/1]   Set to 1 to display more information during the simulation.
* phase:                [deg]   Phase angle located on top of the disk at t=delay (see below).
* delay:                [s]     Time delay of the chopper clock.
*                               NOTE: In contrast to the DiskChopper component, the effect of phase and delay are cumulative, and both can be specified.
* jitter:               [s]     Jitter in the time phase.
* abs_out:                      If 1, absorb all neutrons outside the disk diameter.
* isfirst:              [0/1]   Set to 1 for the first chopper after a continuous source. The neutron events
*                               will be shifted in time to pass the component (with adapted weight).
*
* Additional parameters when isfirst=1 (that have no effect for isfirst=0):
* equal:                [0/1]   When isfirst=1: If 0, the neutron events will be distributed between different slits proportional to the slit size. If 1, the events will be distributed such that each slit transmits the same number of events. This parameter can be used to achieve comparable simulation statistics over different pulses when simulating small and large slits together.
* nrev:                 [ ]     When isfirst=1: Number of *additional* disk revolutions before *and* after the one around t=delay to distribute events on. If set to 2 for example, there will be 2 leading, 1 central, and 2 trailing revolutions of the disk (2*nrev+1 in total).
* ratio:              [ ]       When isfirst=1: Spacing of the additional revolutions from the parameter nrev from the central revolution.
*
*
* %E
*******************************************************************************/

DEFINE COMPONENT MultiDiskChopper

SETTING PARAMETERS (string slit_center="0 180", string slit_width="10 20", nslits=2, delta_y=-0.3, nu=0, nrev=0, ratio=1, jitter=0, delay=0, isfirst=0, phase=0, radius = 0.375, equal=0, abs_out=0, verbose=0)


DECLARE
%{
double T; 
double To;
double omega;
double *dslit_center;
double *dhslit_width;
double *t0;
double *t1;
%}

INITIALIZE
%{
char *pch;
int i;
double sense;

phase = remainder(phase,360.0)*DEG2RAD;
      omega = 2.0*PI*nu; /* rad/s */
      sense = (omega<0) ? -1 : 1 ;
      
      if (isfirst && (nrev-floor(nrev)!=0) )
      { MPI_MASTER(fprintf(stderr,"MultiDiskChopper: %s: wrong First chopper revolution number, must be integer (nrev=%g)\n", NAME_CURRENT_COMP, nrev);)
        exit(-1); }
        
      if (!omega) {
        MPI_MASTER(fprintf(stderr,"MultiDiskChopper: %s WARNING: chopper frequency is 0!\n", NAME_CURRENT_COMP);)
        omega = 1e-15; /* We should actually use machine epsilon here... */
      }
      
      if (nslits<=0)
      { MPI_MASTER(fprintf(stderr,"MultiDiskChopper: %s: nslits must be > 0\n", NAME_CURRENT_COMP);
        exit(-1);) }
        
      // Read slits in array
      dslit_center = malloc(nslits*sizeof(*dslit_center));
      pch = strtok(slit_center, ";_, ");
      for (i=0; i<nslits; i++) {
         if (pch==NULL) {
            MPI_MASTER(fprintf(stderr,"MultiDiskChopper: %s: Cannot parse slit_center: Not enough values?\n", NAME_CURRENT_COMP);)
            exit(-1);
         }
         dslit_center[i] = atof(pch);
         pch = strtok(NULL,";_, ");
            
         if ( (dslit_center[i] < 0) ) {
            while (dslit_center[i] < 0)
            {
               dslit_center[i] += 360.0;
            }

            MPI_MASTER(fprintf(stderr,"MultiDiskChopper: %s: WARNING: Slit center No. %d moved to %f\n", NAME_CURRENT_COMP, i+1, dslit_center[i]);)
         }

         if ( (dslit_center[i] >= 360.0) ) {
            while (dslit_center[i] >= 360.0)
            {
               dslit_center[i] -= 360.0;
            }

            MPI_MASTER(fprintf(stderr,"MultiDiskChopper: %s: WARNING: Slit center No. %d moved to %f\n", NAME_CURRENT_COMP, i+1, dslit_center[i]);)
         }
         
         dslit_center[i] *= DEG2RAD;
      }
      
      // dhslit_width: HALF slit width
      dhslit_width = malloc(nslits*sizeof(*dhslit_width));
      pch = strtok(slit_width, ";_, ");
      for (i=0; i<nslits; i++) {
         if (pch==NULL) {
            MPI_MASTER(fprintf(stderr,"MultiDiskChopper: %s: Cannot parse slit_width: Not enough values?\n", NAME_CURRENT_COMP);)
            exit(-1);
         }
         dhslit_width[i] = 0.5 * atof(pch);
         pch = strtok(NULL,";_, ");
         if ( dhslit_width[i] <= 0 ) {
            MPI_MASTER(fprintf(stderr,"MultiDiskChopper: %s: Slit no %d has nonpositive width! \n", NAME_CURRENT_COMP, i+1);)
            exit(-1);
         }
         dhslit_width[i] *= DEG2RAD;
      }   
      
      /* Calculate delay from phase and vice versa */
      if (phase) {
        if (delay) {
          MPI_MASTER(fprintf(stderr,"MultiDiskChopper: %s WARNING: delay AND phase specified. Adding them up.\n", NAME_CURRENT_COMP);)
        }
        phase-=delay*omega;
        delay=-phase/omega;
      } else {
        phase=delay*omega; 
      } 
      
      /* Time for 1 revolution */
      T = 2.0*PI/fabs(omega);
      
      // calculate arrays of times t0 and t1 which allow for easy randomization in TRACE
      
      /* To: How long can neutrons pass the Chopper at a single point during one revolution through any slit */
            
      // generate times t1: duration of slit openings (or their cumulative sum if !equal)
      // dhslit_width is already in rad
      t1 = malloc(nslits*sizeof(*t1));
      t1[0] = 2.0 * dhslit_width[0] / fabs(omega);
      To = t1[0];    // To: Cumulated opening time in a single point during one revolution through any slit
      
      for (i=1; i<nslits; i++) {
         t1[i] = (equal ? 0 : t1[i-1]) + ( 2.0 * dhslit_width[i] / fabs(omega) );
         To += ( 2.0 * dhslit_width[i] / fabs(omega) );
      }
      
      // generate times t0 = time when slit i starts opening (at top of the disk) (minus t1[i-1] if !equal)
      t0 = malloc(nslits*sizeof(*t0));
      t0[0] = ( sense*remainder(dslit_center[0]-phase,2*PI) - dhslit_width[0] ) / fabs(omega);
      
      for (i=1; i<nslits; i++) {
         t0[i] = ( sense*remainder(dslit_center[i]-phase,2*PI) - dhslit_width[i] ) / fabs(omega) - (equal ? 0 : t1[i-1]);
      }
      
      MPI_MASTER( 
         if (verbose) {
            printf("MultiDiskChopper: %s: \n", NAME_CURRENT_COMP);
            printf("       --- frequency=%g [Hz] %g [rpm], delay=%g [s], phase=%g [deg]\n",
                   nu, nu*60, delay, phase*RAD2DEG);
            printf("       --- vertical axis offset=%g [m] To=%g [s], T=%g [s]\n", delta_y, To, T);
            
            if (isfirst && equal)
               printf("       --- first chopper distributing events equally on all slits\n");
               
            if (isfirst && !equal)
               printf("       --- first chopper distributing events proportional to slit size\n");
               
            if (isfirst)
               printf("       --- adding +-%g disk revolutions at ratio %g\n",nrev,ratio);
               
            printf("       --- Slit center [deg]:");
            for (i=0; i<nslits; i++)
               printf("  %6.2f",dslit_center[i]*RAD2DEG);
            printf("\n");
            printf("       --- Slit width  [deg]:");
            for (i=0; i<nslits; i++)
               printf("  %6.2f",2.0*dhslit_width[i]*RAD2DEG);
            printf("\n");

            // dump internal arrays for debugging
            if (verbose == 2) {
               printf("       --- Internal arrays:\n");
               printf("       --- i   t0          t1         dslit_center dhslit_width\n");
               for (i=0; i<nslits; i++) {
                  printf("       --- %02d %+.4e %+.4e %+.4e %+.4e\n",i,t0[i],t1[i],dslit_center[i],dhslit_width[i]);
               }
            }
            
         }
      )
      
 %}

TRACE
%{
   double phi;
   double xprime,yprime;
   double toff;
   int irev, islit;

   // Propagate into the chopper disk plane
   PROP_Z0;

   if ( delta_y > 0 ) { 
      // 'anormal' case, chopper above guide
      // mirror coordinate system
      xprime = -x;
      yprime = -y+delta_y;
   } else {
      // 'normal' case, chopper below guide
      xprime = x;
      yprime = y-delta_y;
   }
      
   // Is neutron transmitted/absorbed outside the disk diameter ?
   if ( ( SQR(xprime) + SQR(yprime) ) > SQR(radius) )
      if (abs_out) {
         ABSORB;
      } else {
         SCATTER;
      }
   else
   {
      if (isfirst) {
         irev = (nrev > 0 ? ratio*( floor((2*nrev+1)*rand01()) - nrev ) : 0);

         if (equal) {
            // Distribute neutrons equally over slits
            t = rand01() * nslits;
            islit = (t==nslits) ? nslits-1 : floor(t);
            t = (t-islit) * t1[islit];
            
            p *= t1[islit] / T * nslits;
         } else {
            // Distribute neutrons proportional to slit size
            t = rand01() * To;
            islit = 0;
            while (t1[islit] < t)
               islit++;
        
            /* weight correction: chopper slits transmission opening time per full revolution time */
            p *= To / T; 
         }
         
         // offset time stamp according to slit phase, neutron position and jitter
         t += t0[islit] - atan2(xprime,yprime)/omega + irev*T + ( jitter ? jitter*randnorm() : 0 );
         
      } else {
      
         // where does the neutron hit the disk ?
         phi = atan2(xprime,yprime) + omega*( t - delay - ( jitter ? jitter*randnorm() : 0 ) ) ;

         // does the neutron hit one of the slits ?
         islit=0;
         while (islit<nslits && !SCATTERED)
         {
            if ( fabs(remainder(phi-dslit_center[islit],2*PI)) < dhslit_width[islit] )
               SCATTER;
            
            islit++;
         }
         if (!SCATTERED)
            ABSORB;
      }
   }
%}

FINALLY
%{
   // clean up
   if (dslit_center)
      free(dslit_center);

   if (dhslit_width)
      free(dhslit_width);
      
   if (t0)
      free(t0);

   if (t1)
      free(t1);
%}

MCDISPLAY
%{
  int j;
  
  
  // the disk
  circle("xy", 0, delta_y, 0, radius);
  
  /* Drawing the slit(s) */
  for (j=0; j<nslits; j++) {
    /* Angular start/end of slit */
    double tmin = dslit_center[j] - dhslit_width[j] + phase;
    double tmax = tmin+2.0*dhslit_width[j];
    /* Draw lines for each slit. */
       
    line(
      radius*sin(tmin),          radius*cos(tmin)+delta_y,          0,
      0,                         delta_y,                           0
      );
    line(
      radius*sin(tmax),          radius*cos(tmax)+delta_y,          0,
      0,                         delta_y,                           0
      );
  }
%}

END