File: Source_adapt.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 (314 lines) | stat: -rw-r--r-- 10,351 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
/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright 1997-2002, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Component: Source_adapt.comp
*
* %I
* Written by: Kristian Nielsen
* Date: 1999
* Origin: Risoe
* Modified by: Revised by: <a href="mailto:percival@physics.queensu.ca">Aaron M. Percival</a>
* Modified by: 2007
* Modified by: <a href="http://www.physics.queensu.ca">Queen's University Department of Physics</a>
* Modified by: Added the option of having an initial distribution that is uniform in wavelength
*
* Neutron source with adaptive importance sampling
*
*
*
* %D
* Rectangular source with flat energy or wavelength distribution that
* uses adaptive importance sampling to improve simulation efficiency.
* Works together with the Adapt_check component.
*
* The source divides the three-dimensional phase space of (energy,
* horizontal position, horizontal divergence) into a number of
* rectangular bins. The probability for selecting neutrons from each
* bin is adjusted so that neutrons that reach the Adapt_check
* component with high weights are emitted more frequently than those
* with low weights. The adjustment is made so as to attemt to make
* the weights at the Adapt_check components equal.
*
* Focusing is achieved by only emitting neutrons towards a rectangle
* perpendicular to and placed at a certain distance along the Z axis.
* Focusing is only approximate (for simplicity); neutrons are also
* emitted to pass slightly above and below the focusing rectangle,
* more so for wider focusing.
*
* In order to prevent false learning, a parameter beta sets a
* fraction of the neutrons that are emitted uniformly, without regard
* to the adaptive distribution. The parameter alpha sets an initial
* fraction of neutrons that are emitted with low weights; this is
* done to prevent early neutrons with rare initial parameters but
* high weight to ruin the statistics before the component adapts its
* distribution to the problem at hand. Good general-purpose values
* for these parameters are alpha = beta = 0.25.
*
* %VALIDATION
* This component is not validated. It does not work properly with MPI.
*
* %P
* INPUT PARAMETERS:
*
* xmin: [m]           Left edge of rectangular source
* xmax: [m]           Right edge
* ymin: [m]           Lower edge
* ymax: [m]           Upper edge
* xwidth: [m]         Width of source
* yheight: [m]        Height of source
* target_index: [1]   relative index of component to focus at, e.g. next is +1 this is used to compute 'dist' automatically.
* dist: [m]           Distance to target rectangle along z axis
* focus_xw: [m]       Width of target
* focus_yh: [m]       Height of target
* E0: [meV]           Mean energy of neutrons
* dE: [meV]           Energy spread (energy range is from E0-dE to E0+dE)
* lambda0: [AA]       Mean wavelength of neutrons (if energy not specified)
* dlambda: [AA]       Wavelength spread half width
* flux: []            (1/(cm 2 AA st)) Absolute source flux
* N_E: [1]            Number of bins in energy (or wavelength) dimension
* N_xpos: [1]         Number of bins in horizontal position
* N_xdiv: [1]         Number of bins in horizontal divergence
* alpha: [1]          Learning cut-off factor (0 < alpha <= 1)
* beta: [1]           Aggressiveness of adaptive algorithm (0 < beta <= 1)
* filename: [string]  Optional filename for adaptive distribution output
*
* CALCULATED PARAMETERS:
*
* p_in: []            Internal, holds initial neutron weight
* y_0: []             Internal
* C: []               Internal
* r_0: []             Internal
* count: []           Internal, counts neutrons emitted
* adpt: []            Internal structure shared with the Adapt_check component
*
* %E
*******************************************************************************/
DEFINE COMPONENT Source_adapt



SETTING PARAMETERS (
  N_E=20, N_xpos=20, N_xdiv=20,
  xmin=0, xmax=0, ymin=0, ymax=0, xwidth=0, yheight=0,
  string filename=0, dist=0, focus_xw=0.05, focus_yh=0.1,
  E0=0, dE=0, lambda0=0, dlambda=0, flux=1e13,
  int target_index=+1, alpha=0.25, beta=0.25)


/* Neutron parameters: (x,y,z,vx,vy,vz,t,sx,sy,sz,p) */

SHARE
%{
%include "adapt_tree-lib"
struct source_adapt
{
struct adapt_tree *atree; /* Adaptive search tree */
int idx;                  /* Index of current bin */
double *psi, *n;          /* Arrays of weight sums, neutron counts */
double psi_tot;           /* Total weight sum */
double pi, num;           /* Initial p, number of bins in tree */
double factor;            /* Adaption quality factor */
double a_beta;            /* Adaption agression factor */
} source_adapt;

%}

DECLARE
%{
struct source_adapt adpt;
double count;                 /* Neutron counter */
double y_0, C, r_0;
double p_in;
%}

INITIALIZE
%{
int i;
double a, lambda_min, lambda_max, delta_lambda, source_area;

if (target_index && !dist)
  {
    Coords ToTarget;
    double tx,ty,tz;
    ToTarget = coords_sub(POS_A_COMP_INDEX(INDEX_CURRENT_COMP+target_index),POS_A_CURRENT_COMP);
    ToTarget = rot_apply(ROT_A_CURRENT_COMP, ToTarget);
    coords_get(ToTarget, &tx, &ty, &tz);
    dist=sqrt(tx*tx+ty*ty+tz*tz);
  }

  if (xwidth > 0) { xmin=-xwidth/2; xmax=-xmin; }
  if (yheight> 0) { ymin=-yheight/2; ymax=-ymin; }

  adpt.num = N_E*N_xpos*N_xdiv;
  adpt.a_beta = beta;

  if (E0 == 0) {
    lambda_min = lambda0 - dlambda; /* AAngstroem */
    lambda_max = lambda0 + dlambda;
    delta_lambda = 2*dlambda;
  }
  else {
    lambda_min = sqrt(81.81/(E0+dE)); /* AAngstroem */
    lambda_max = sqrt(81.81/(E0-dE));
    delta_lambda = lambda_max - lambda_min;
  }

  if (lambda_min<=0 || lambda_max <=0 || lambda_max<=lambda_min ) {
      printf("Source_adapt: %s: Error in given wavelength range!\n"
             "ERROR          Exiting\n",
           NAME_CURRENT_COMP);
      exit(0);
  }

  source_area = (xmax - xmin)*(ymax - ymin)*1e4; /* cm^2 */
  p_in = flux/mcget_ncount()*delta_lambda*source_area;
  adpt.atree = adapt_tree_init(adpt.num);
  adpt.psi = malloc(adpt.num*sizeof(*adpt.psi));
  adpt.n = malloc(adpt.num*sizeof(*adpt.n));
  if(!(adpt.psi && adpt.n))
  {
    fprintf(stderr, "Fatal error: out of memory.\n");
    exit(1);
  }
  for(i = 0; i < adpt.num; i++)
  {
    adapt_tree_add(adpt.atree, i, 1.0/adpt.num);
    adpt.psi[i] = adpt.n[i] = 0;
  }
  adpt.psi_tot = 0;
  count = 0;
  y_0 = adpt.num > 8 ? 2.0/adpt.num : 0.25;
  r_0 = 1/(double)alpha*log((1 - y_0)/y_0)/(double)mcget_ncount();
  C = 1/(1 + log(y_0 + (1 - y_0)*exp(-r_0*mcget_ncount()))/(r_0*mcget_ncount()));
%}

TRACE
%{
  double thmin,thmax,phmin,phmax,theta,phi,v,r,E,lambda;
  double new_v;
  int i_E, i_xpos, i_xdiv;

  /* Randomly select a bin in the current distribution */
  r = rand01();
  adpt.idx = adapt_tree_search(adpt.atree, adpt.atree->total*r);
  if(adpt.idx >= adpt.num)
  {
    fprintf(stderr,
            "Hm, idx is %d, num is %d, r is %g, atree->total is %g\n",
            adpt.idx, (int)adpt.num, r, adpt.atree->total);
    adpt.idx = adpt.num - 1;
  }
  /* Now find the bin coordinates. */
  i_xdiv = adpt.idx % (int)N_xdiv;
  i_xpos = (adpt.idx / (int)N_xdiv) % (int)N_xpos;
  i_E = (adpt.idx / (int)N_xdiv) / (int)N_xpos;
  /* Compute the initial neutron parameters, selecting uniformly randomly
     within each bin dimension. */
  x = xmin + (i_xpos + rand01())*((xmax - xmin)/(double)N_xpos);
  y = ymin + rand01()*(ymax - ymin);
  z=0;
  thmin = atan2(-focus_xw/2.0 - x, dist);
  thmax = atan2( focus_xw/2.0 - x, dist);
  theta = thmin + (i_xdiv + rand01())*((thmax - thmin)/(double)N_xdiv);
  phmin = atan2(-focus_yh/2.0 - y, dist);
  phmax = atan2( focus_yh/2.0 - y, dist);
  phi = phmin + rand01()*(phmax - phmin);

  if(E0 == 0) {
  	lambda = lambda0 - dlambda + (i_E + rand01())*(2.0*dlambda/(double)N_E);
	v = 3.956E3/lambda;
	vy = v*sin(phi);
  	vx = v*cos(phi)*sin(theta);
	vz = v*cos(phi)*cos(theta);

  }
  else {
  	E = E0 - dE + (i_E + rand01())*(2.0*dE/(double)N_E);
  	v = sqrt(E)*SE2V;
  	vy = v*sin(phi);
  	vx = v*cos(phi)*sin(theta);
  	vz = v*cos(phi)*cos(theta);
  }

  t = 0;
  /* Adjust neutron weight. */
  p = p_in;
  adpt.factor = y_0/(y_0 + (1 - y_0)*exp(-r_0*count));
  count++;
  p /= adpt.atree->v[adpt.idx]/(adpt.atree->total/adpt.num);
  p *= C*adpt.factor*(thmax - thmin)*(sin(phmax) - sin(phmin));
  SCATTER;
  /* Update distribution, assuming absorbtion. */
  if(adpt.n[adpt.idx] > 0)
    adpt.psi_tot -= adpt.psi[adpt.idx]/
      (adpt.n[adpt.idx]*(adpt.n[adpt.idx] + 1));
  adpt.n[adpt.idx]++;
  if(adpt.psi_tot != 0)
  {
    new_v = (1 - adpt.a_beta)*adpt.factor*adpt.psi[adpt.idx]/
                (adpt.n[adpt.idx]*adpt.psi_tot) +
            adpt.a_beta/adpt.num;
    adapt_tree_add(adpt.atree, adpt.idx, new_v - adpt.atree->v[adpt.idx]);
  }
  /* Remember initial neutron weight. */
  adpt.pi = p;
%}

FINALLY
%{
  double *p1 = NULL;
  int i;

  if(filename)
  {
    p1 = malloc(adpt.num*sizeof(double));
    if(!p1)
      fprintf(stderr, "Warning: Source_adapt: "
              "not enough memory to write distribution.\n");
  }
  if(p1)
  {
    for(i = 0; i < adpt.num; i++)
      p1[i] = adpt.atree->v[i]/adpt.atree->total;

    if(E0 == 0) {
    	DETECTOR_OUT_1D("Adaptive source Wavelength distribution",
        	    "Wavelength [AA]",
                    "Probability",
                    "lambda", lambda0 - dlambda, lambda0 + dlambda, adpt.num,
    	            NULL, p1, NULL, filename);
    }
    else {
    	DETECTOR_OUT_1D("Adaptive source energy distribution",
                    "Energy [meV]",
                    "Probability",
                    "E", E0 - dE, E0 + dE, adpt.num,
                    NULL, p1, NULL, filename);
    }

    free(p1);
  }
  adapt_tree_free(adpt.atree);
%}

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);
  if (dist) {
    dashed_line(0,0,0, -focus_xw/2,-focus_yh/2,dist, 4);
    dashed_line(0,0,0,  focus_xw/2,-focus_yh/2,dist, 4);
    dashed_line(0,0,0,  focus_xw/2, focus_yh/2,dist, 4);
    dashed_line(0,0,0, -focus_xw/2, focus_yh/2,dist, 4);
  }
%}

END