File: Monitor_Optimizer.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 (271 lines) | stat: -rw-r--r-- 9,183 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
/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright 1997-2002, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Component: Monitor_Optimizer
*
* %Identification
* Written by: <a href="mailto:farhi@ill.fr">Emmanuel Farhi</a>
* Date: 17 Sept 1999
* Origin: <a href="http://www.ill.fr">ILL (France)</a>
* Modified by: EF, (Feb 2000)
* Modified by: EF, Mar 10th, 2000 : now uses struct
* Modified by: EF, Oct 12th, 2000 ; optim divergence (0.08)
*
* To be used after the <b>Source_Optimizer</b> component
*
* %Description
* A component that optimizes the neutron flux passing through the
* <b>Source_Optimizer</b> in order to have the maximum flux at the
* Monitor_Optimizer position(s).
* <b>Source_optimizer</b> should be placed just after the source.
* Monitor_Optimizer should be placed at the position to optimize.
* I prefer to put one just before the sample.
*
* See <a href="Source_Optimizer.html">Source_Optimizer</a> for
* usage example and additional informations.
*
* %Parameters
* INPUT PARAMETERS:
*
* xmin: [m]       Lower x bound of monitor opening
* xmax: [m]       Upper x bound of monitor opening
* ymin: [m]       Lower y bound of monitor opening
* ymax: [m]       Upper y bound of monitor opening
* optim_comp: [str]  name of the Source_Optimizer component in the instrument definition. Do not use quotes (no quotes)
* xwidth: [m]     Width of monitor. Overrides xmin,xmax.
* yheight: [m]    Height of monitor. Overrides ymin,ymax.
*
* CALCULATED PARAMETERS:
*
* none (see <b>Source_Optimizer.comp</b>)
*
* %Link
* <a href="Source_Optimizer.html">Source_Optimizer</a>
*
* %End
*******************************************************************************/

DEFINE COMPONENT Monitor_Optimizer

SETTING PARAMETERS (
  string optim_comp,
  xmin=-0.1, xmax=0.1, ymin=-0.1, ymax=0.1, xwidth=0, yheight=0)

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

DECLARE
%{
#ifndef Source_Optimizer_Here
#error McStas : Source_Optimizer component has to be used before Monitor_Optimizer
#endif

int This_Monitor_id;
%}

INITIALIZE
%{

  struct Optim_Variables *Vars = 
    (struct Optim_Variables *)(COMP_GETPAR3(Source_Optimizer, optim_comp, Vars));

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

  Vars->Monitor_Number++;
  This_Monitor_id = Vars->Monitor_Number;
%}

TRACE
%{
  struct Optim_Defines *DEFS   = 
    (struct Optim_Defines *)(COMP_GETPAR3(Source_Optimizer, optim_comp, DEFS));
  struct Optim_Variables *Vars = 
    (struct Optim_Variables *)(COMP_GETPAR3(Source_Optimizer, optim_comp, Vars));

  PROP_Z0;
  if ((Vars->Phase != DEFS->PHASE_UNACTIVATE)
&& (x>xmin && x<xmax && y>ymin && y<ymax))
  {

    Vars->cur_p = p;

    Vars->Monitor_Counts++; /* initialized to 0 in DEFS->PHASE_SET_REF */
    if (Vars->Flag_Smooth )
    { /* to evaluate correctly the un-optimized flux, we restore original weight in these phases */
      if ((Vars->Phase == DEFS->PHASE_GET_REF) || (Vars->Phase == DEFS->PHASE_GET_LIMITS))
             Vars->cur_p /= Vars->dkeep;
    }
    if (This_Monitor_id == 1)  /* only count flux once !!  */
    {
      Vars->Monitor_Flux += Vars->cur_p;
    }

    Vars->Total_Monitor_Counts++;

    if (Vars->Flag_Auto
    && (Vars->Phase == DEFS->PHASE_GET_REF))
      {
      if (((Vars->Reference_Counts > (double)mcget_ncount()*Vars->dstep) && (Vars->Monitor_Counts >= Vars->nbins*10))
      || (Vars->Monitor_Counts >= Vars->nbins*100) || (Vars->Reference_Counts > 2.0*mcget_ncount()*Vars->dstep))
      {
        Vars->Phase_Counts_R = 0;        /* enough counts on monitor */
        if (Vars->Flag_Verbose)
        {
          printf(">> AUTO monitor has reached %.1f counts (non optimized",Vars->Monitor_Counts);
          if (Vars->Flag_Smooth) printf(", smoothed");
          printf(")\n");
        }
        Vars->Phase_Counts = (Vars->Reference_Counts+Vars->Limits_Counts);
        Vars->dstep = (double)Vars->Phase_Counts/(double)mcget_ncount();

/*        Vars->dkeep = Vars->nbins*10/Vars->Monitor_Counts;
        if (Vars->dkeep < Vars->dkeep_target) Vars->dkeep = Vars->dkeep_target;
        if (Vars->dkeep > .9) Vars->dkeep = 0.9; */

        if (Vars->Monitor_Counts < 3*Vars->nbins)
        {
          printf("Source_Optimizer: monitor only reached %.1f counts. \n", Vars->Monitor_Counts);
          printf("* WARNING *       You'd better unactivate it or increase number of neutrons\n");
        }
      }
    }

   if ((Vars->Phase == DEFS->PHASE_GET_REF)
    || ((Vars->Phase == DEFS->PHASE_OPTIM) && (Vars->Flag_Continuous) ))        /* build the Optimized Source distributions */
   {
     if (Vars->vx_max-Vars->vx_min)
       Vars->index = (int)rint(Vars->nbins * (Vars->dvx -Vars->vx_min)/(Vars->vx_max-Vars->vx_min));
     else
       Vars->index = 0;
     if (Vars->index < 0)     Vars->index = 0;
     if (Vars->index >= Vars->nbins) Vars->index = Vars->nbins - 1;
     Vars->New_Source_vx[Vars->index]++;

     if (Vars->vy_max-Vars->vy_min)
       Vars->index = (int)rint(Vars->nbins * (Vars->dvy -Vars->vy_min)/(Vars->vy_max-Vars->vy_min));
     else
       Vars->index = 0;
     if (Vars->index < 0)     Vars->index = 0;
     if (Vars->index >= Vars->nbins) Vars->index = Vars->nbins - 1;
     Vars->New_Source_vy[Vars->index]++;

     if (Vars->vz_max-Vars->vz_min)
       Vars->index = (int)rint(Vars->nbins * (Vars->dvz -Vars->vz_min)/(Vars->vz_max-Vars->vz_min));
     else
       Vars->index = 0;
     if (Vars->index < 0)     Vars->index = 0;
     if (Vars->index >= Vars->nbins) Vars->index = Vars->nbins - 1;
     Vars->New_Source_vz[Vars->index]++;

     if (Vars->x_max-Vars->x_min)
       Vars->index = (int)rint(Vars->nbins * (Vars->cur_x -Vars->x_min)/(Vars->x_max-Vars->x_min));
     else
       Vars->index = 0;
     if (Vars->index < 0)     Vars->index = 0;
     if (Vars->index >= Vars->nbins) Vars->index = Vars->nbins - 1;
     Vars->New_Source_x[Vars->index]++;

     if (Vars->y_max-Vars->y_min)
       Vars->index = (int)rint(Vars->nbins * (Vars->cur_y -Vars->y_min)/(Vars->y_max-Vars->y_min));
     else
       Vars->index = 0;
     if (Vars->index < 0)     Vars->index = 0;
     if (Vars->index >= Vars->nbins) Vars->index = Vars->nbins - 1;
     Vars->New_Source_y[Vars->index]++;

     if (Vars->sx_max-Vars->sx_min)
       Vars->index = (int)rint(Vars->nbins * (Vars->dsx -Vars->sx_min)/(Vars->sx_max-Vars->sx_min));
     else
       Vars->index = 0;
     if (Vars->index < 0)     Vars->index = 0;
     if (Vars->index >= Vars->nbins) Vars->index = Vars->nbins - 1;
     Vars->New_Source_sx[Vars->index]++;

     if (Vars->sy_max-Vars->sy_min)
       Vars->index = (int)rint(Vars->nbins * (Vars->dsy -Vars->sy_min)/(Vars->sy_max-Vars->sy_min));
     else
       Vars->index = 0;
     if (Vars->index < 0)     Vars->index = 0;
     if (Vars->index >= Vars->nbins) Vars->index = Vars->nbins - 1;
     Vars->New_Source_sy[Vars->index]++;

     if (Vars->sz_max-Vars->sz_min)
       Vars->index = (int)rint(Vars->nbins * (Vars->dsz -Vars->sz_min)/(Vars->sz_max-Vars->sz_min));
     else
       Vars->index = 0;
     if (Vars->index < 0)     Vars->index = 0;
     if (Vars->index >= Vars->nbins) Vars->index = Vars->nbins - 1;
     Vars->New_Source_sz[Vars->index]++;

   } /* end if Vars->Phase */

  } /* end if xy in optimizer */
/* end trace */
%}

FINALLY
%{
  struct Optim_Variables *Vars = 
    (struct Optim_Variables *)(COMP_GETPAR3(Source_Optimizer, optim_comp, Vars));

  if (Vars->Monitor_Number == 0)
  {
  /* initial Reference distribution arrays (for weights) */
    free(Vars->Reference_x);
    free(Vars->Reference_y);
    free(Vars->Reference_vx);
    free(Vars->Reference_vy);
    free(Vars->Reference_vz);
    free(Vars->Reference_sx);
    free(Vars->Reference_sy);
    free(Vars->Reference_sz);

  /* optimized Source distribution arrays (to reach) */
    free(Vars->Source_x);
    free(Vars->Source_y);
    free(Vars->Source_vx);
    free(Vars->Source_vy);
    free(Vars->Source_vz);
    free(Vars->Source_sx);
    free(Vars->Source_sy);
    free(Vars->Source_sz);

  /* optimized New_Source distribution arrays (to reach in next step, passed to Source) */
    free(Vars->New_Source_x);
    free(Vars->New_Source_y);
    free(Vars->New_Source_vx);
    free(Vars->New_Source_vy);
    free(Vars->New_Source_vz);
    free(Vars->New_Source_sx);
    free(Vars->New_Source_sy);
    free(Vars->New_Source_sz);

  /* Passing distribution arrays (should grow to reach Source) */
    free(Vars->Passing_x);
    free(Vars->Passing_y);
    free(Vars->Passing_vx);
    free(Vars->Passing_vy);
    free(Vars->Passing_vz);
    free(Vars->Passing_sx);
    free(Vars->Passing_sy);
    free(Vars->Passing_sz);

    Vars->Monitor_Number = 1;  /* to only free once */
  }
%}

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