File: Brilliance_monitor.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 (281 lines) | stat: -rw-r--r-- 8,112 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
/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2011, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* %I
* Written by: Peter Willendrup, derived from TOF_lambda_monitor.comp
* Date: May 23, 2012
* Origin: DTU Physics
*
* Special "Brilliance" monitor.
*
* %D
* If used in the right setting, will output "instantaneous" and "mean" brilliances in units of Neutrons/cm^2/ster/AA/s. Conditions for proper units:
* <ul>
* <li>Use a with a source of area 1x1cm
* <li>The source must illuminate/focus to an area of 1x1cm a 1m distance
* <li>Parametrise the Brilliance_monitor with the frequency of the source
* <li>To not change the source TOF distribution, place the Brilliance monitor close to the source!
* </ul>
*
* with a source of area 1x1cm illuminating/focusing to an area of 1x1cm a 1m distance, this monitor will output "instantaneous" and "mean" brilliances in units of Neutrons/cm^2/ster/AA/s
*
* Here is an example of the use of the component. Note how the mentioned Unit conditions are implemented in instrument code.
*
*COMPONENT Source = ESS_moderator_long(
*    l_low = lambdamin, l_high = lambdamax, dist = 1, xw = 0.01, yh = 0.01,
*    freq = 14, T=50, tau=287e-6, tau1=0, tau2=20e-6,
*    n=20, n2=5, d=0.00286, chi2=0.9, I0=6.9e11, I2=27.6e10,
*    branch1=0, branch2=0.5, twopulses=0, size=0.01)
*  AT (0, 0, 0) RELATIVE Origin
*
*COMPONENT BRIL = Brilliance_monitor(nlam=196,nt=401,filename="bril.sim",
*	t_0=0,t_1=4000,lambda_0=lambdamin,
*	lambda_1=lambdamax, Freq=14)
*AT (0,0,0.000001) RELATIVE Source
*
* %P
* INPUT PARAMETERS:
*
* nlam: [1]             Number of bins in wavelength
* nt: [1]               Number of bins in TOF
* t_0: [us]             Minimum time
* t_1: [us]             Maximum time
* lambda_0: [AA]        Minimum wavelength detected
* lambda_1: [AA]        Maximum wavelength detected
* filename: [string]    Defines filenames for the detector images. Stored as:<br>Peak_&lt;filename&gt; and Mean_&lt;filename&gt;
* restore_neutron: [1]  If set, the monitor does not influence the neutron state
* Freq: [Hz]            Source frequency. Use freq=1 for reactor source
* srcarea:    Source area [cm^2]
* tofcuts: [1]          Flag to generate TOF-distributions as function of wavelength
* toflambda: [1]        Flag to generate TOF-lambda distribution output
* source_dist: [m]      Distance from source. Beware when transporting through neutron optics!
* xwidth: [m]           width of monitor
* yheight: [m]          height of monitor
* nowritefile: [1]      If set, monitor will skip writing to disk
*
* CALCULATED PARAMETERS:
*
* Div_N: []             Array of neutron counts
* Div_p: []             Array of neutron weight counts
* Div_p2: []            Array of second moments
*
* %E
*******************************************************************************/

DEFINE COMPONENT Brilliance_monitor



SETTING PARAMETERS (int nlam=101, int nt=1001, int nowritefile=0,
  lambda_0=0, lambda_1=20, int restore_neutron=0, Freq, int tofcuts=0,
  int toflambda=0, xwidth = 0.01, yheight=0.01, source_dist=1, string filename=0,
  t_0=0, t_1=20000, srcarea=1)



DECLARE
%{
  DArray2d BRIL_N;
  DArray2d BRIL_p;
  DArray2d BRIL_p2;

  DArray1d BRIL_mean;
  DArray1d BRIL_meanN;
  DArray1d BRIL_meanE;
  DArray1d BRIL_peak;
  DArray1d BRIL_peakN;
  DArray1d BRIL_peakE;

  DArray1d BRIL_shape;
  DArray1d BRIL_shapeN;
  DArray1d BRIL_shapeE;

  double tt_0;
  double tt_1;
  double xmin;
  double xmax;
  double ymin;
  double ymax;
  double ster;
  double prsec;
  double dlam;
  double dt;
%}

INITIALIZE
%{
  prsec = 1e-6;

  BRIL_N = create_darr2d(nt, nlam);
  BRIL_p = create_darr2d(nt, nlam);
  BRIL_p2 = create_darr2d(nt, nlam);
  BRIL_mean = create_darr1d(nlam);
  BRIL_meanN = create_darr1d(nlam);
  BRIL_meanE = create_darr1d(nlam);
  BRIL_peak = create_darr1d(nlam);
  BRIL_peakN = create_darr1d(nlam);
  BRIL_peakE = create_darr1d(nlam);

  BRIL_shape = create_darr1d(nt);
  BRIL_shapeN = create_darr1d(nt);
  BRIL_shapeE = create_darr1d(nt);


  tt_0 = t_0*prsec;
  tt_1 = t_1*prsec;
  dt = (t_1-t_0)*prsec/nt;
  dlam = (lambda_1-lambda_0)/(nlam-1);

  xmax = xwidth/2.0;
  ymax = yheight/2.0;
  xmin = -xmax;
  ymin = -ymax;

  ster = xwidth * yheight/(source_dist*source_dist);

  // Use instance name for monitor output if no input was given
  if (!strcmp(filename,"\0")) sprintf(filename,"%s",NAME_CURRENT_COMP);  
%}

TRACE
%{
  int i,j;
  double div;
  double lambda;
  double Pnorm;

  PROP_Z0;
  lambda = (2*PI/V2K)/sqrt(vx*vx + vy*vy + vz*vz);
  if (x>xmin && x<xmax && y>ymin && y<ymax &&
      lambda > lambda_0 && lambda < lambda_1) {
    if (t < tt_1 && t > tt_0) {
      i = floor((lambda - lambda_0)*nlam/(lambda_1 - lambda_0));
      j = floor((t-tt_0)*nt/(tt_1-tt_0));
      Pnorm=p/dlam/ster/srcarea;
      double Pnorm2 = Pnorm*Pnorm;
      #pragma acc atomic
      BRIL_meanN[i] = BRIL_meanN[i]+1;
      #pragma acc atomic
      BRIL_mean[i] = BRIL_mean[i]+Pnorm;
      #pragma acc atomic      
      BRIL_meanE[i] = BRIL_meanE[i]+Pnorm2;
      
      Pnorm=Pnorm/Freq/dt;
      Pnorm2=Pnorm+Pnorm;
      #pragma acc atomic      
      BRIL_N[j][i] = BRIL_N[j][i]+1;
      #pragma acc atomic
      BRIL_p[j][i] = BRIL_p[j][i]+Pnorm;
      #pragma acc atomic
      BRIL_p2[j][i] = BRIL_p2[j][i]+Pnorm2;
    }
  }
  if (restore_neutron) {
    RESTORE_NEUTRON(INDEX_CURRENT_COMP, x, y, z, vx, vy, vz, t, sx, sy, sz, p);
  }
%}

SAVE
%{
if (!nowritefile) {
  /* First, dump the 2D monitor */

  /* For each Wavelength channel, find peak brilliance */
  int i,j,jmax;
  double Pnorm;
  char ff[256];
  char tt[256];

  for (i=0; i<nlam; i++) {
    Pnorm = -1;
    jmax = -1;
    for (j=0; j<nt; j++) {
  	  if (BRIL_p[j][i]>=Pnorm) {
	      Pnorm = BRIL_p[j][i];
	      jmax=j;
	    }
  	  BRIL_shape[j] = BRIL_p[j][i];
  	  BRIL_shapeN[j] = BRIL_N[j][i];
  	  BRIL_shapeE[j] = BRIL_p2[j][i];
    }
    if (tofcuts == 1) {
    	sprintf(ff, "Shape_%s_%g",filename,lambda_0+i*dlam);
    	sprintf(tt, "Peak shape at %g AA",lambda_0+i*dlam);
    	DETECTOR_OUT_1D(
  			tt,
  			"TOF [us]",
  			"Peak Brilliance",
  			"Shape", t_0, t_1, nt,
  			&BRIL_shapeN[0],&BRIL_shape[0],&BRIL_shapeE[0],
  			ff);
    }
    BRIL_peakN[i] = BRIL_N[jmax][i];
    BRIL_peak[i] = BRIL_p[jmax][i];
    BRIL_peakE[i] = BRIL_p2[jmax][i];
  }

  sprintf(ff, "Mean_%s",filename);
  DETECTOR_OUT_1D(
  	"Mean brilliance",
    "Wavelength [AA]",
    "Mean Brilliance",
    "Mean", lambda_0, lambda_1, nlam,
    &BRIL_meanN[0],&BRIL_mean[0],&BRIL_meanE[0],
    ff);
  sprintf(ff, "Peak_%s",filename);
  DETECTOR_OUT_1D(
  	"Peak brilliance",
    "Wavelength [AA]",
    "Peak Brilliance",
    "Peak", lambda_0, lambda_1, nlam,
    &BRIL_peakN[0],&BRIL_peak[0],&BRIL_peakE[0],
    ff);

  /* MPI related NOTE: Order is important here! The 2D-data used to generate wavelength-slices and calculate
     the peak brilliance should be done LAST, otherwise we will get a factor of MPI_node_count too much as
     scatter/gather has been performed on the arrays... */
  if (toflambda == 1) {
    sprintf(ff, "TOFL_%s",filename);
    DETECTOR_OUT_2D(
      "TOF-wavelength brilliance",
      "Time-of-flight [\\gms]", "Wavelength [AA]",
      t_0, t_1, lambda_0, lambda_1,
      nt, nlam,
      &BRIL_N[0][0],&BRIL_p[0][0],&BRIL_p2[0][0],
      filename);
  }
}
%}

FINALLY
%{
  destroy_darr2d(BRIL_N);
  destroy_darr2d(BRIL_p);
  destroy_darr2d(BRIL_p2);

  destroy_darr1d(BRIL_mean);
  destroy_darr1d(BRIL_meanN);
  destroy_darr1d(BRIL_meanE);
  destroy_darr1d(BRIL_peak);
  destroy_darr1d(BRIL_peakN);
  destroy_darr1d(BRIL_peakE);

  destroy_darr1d(BRIL_shape);
  destroy_darr1d(BRIL_shapeN);
  destroy_darr1d(BRIL_shapeE);
%}

MCDISPLAY
%{
  multiline(5, (double)xmin, (double)ymin, 0.0,
               (double)xmax, (double)ymin, 0.0,
               (double)xmax, (double)ymax, 0.0,
               (double)xmin, (double)ymax, 0.0,
               (double)xmin, (double)ymin, 0.0);
%}

END