File: Source_multi_surfaces.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 (393 lines) | stat: -rw-r--r-- 11,609 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
/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright 1997-2006, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Component: Source_multi_surfaces
*
* %I
* Written by: Ludovic Giller, Uwe Filges
* Date: 31.10.06
* Origin: PSI/Villigen
* Modified by:
*
* Rectangular neutron source with subareas - using wavelength spectra reading
* from files
* 
* %D
* This routine is a rectangular neutron source, which aims at a square target
* centered at the beam (in order to improve MC-acceptance rate).
* The angular divergence is then given by the dimensions of the target.
* The source surface can be divided in subareas (maximum 7 in one dimension). 
* For each subarea a discret spectrum must be loaded given by its corresponding
* file. The name of the files are saved in one file and will be called with the
* parameter "filename".
* In fact the routine first reads the spectrum from files (up to 49) and it 
* selects randomly a subarea. Secondly it generates a neutron with an random
* energy, selected from the spectrum corresponding to the subsurface chosen, and
* with a random direction of propagation within the target.
* ATTENTION 1:  the files must be located in the working directory where also
*		the instrument file is located
* ATTENTION 2:  the wavelenght distribution (or binning) must be uniform 
*
* The file giving the name of sub-sources is matrix like, 
*    eg. for a 3x2 (x,y) division :
*                    spec1_1.dat spec1_2.dat spec1_3.dat
*                    spec2_1.dat spec2_2.dat spec2_3.dat(RETURN-key)
*
*                ATTENTION : The line break after the last line is important.
*			     Otherwise the files can not be read in !!!
*
*                and for example spec1_2.dat must be like, always from big to
*                small lambda :
*			#surface 1_2
*			#comments must be preceded by "#"
*			#lambda [AA] 	intensity [a.u.]
*			9.0		1.39E+08
*			8.75		9.67E+08
*			8.5		1.17E+09
*			8.25		1.50E+09
*			8.0		1.60E+09
*			7.75		1.43E+09
*			7.5		1.40E+09
*			7.25		1.36E+09
*			7.0		1.35E+09
*			6.75		1.33E+09
*			6.5		1.32E+09
*			6.25		1.31E+09
*
*
*		[a.u.] 	means that your chosen unit for the intensity influences
*			the outcoming unit. What you put in you will get out !
*			i.e. whatever is the unit for the input intensity you
*			will have the same unit for the the output.
*			Generally McStas works in neutrons per second [n/s].
*
* Usage example:
*   Source_multi_surfaces(yheight=0.16,xwidth=0.09,dist=1.18,xw=0.08,yh=0.15,xdim=4,ydim=4,
*                         Lmin=0.01,Lmax=10.0,filename="files_name.dat")
*
* %P
* yheight: [m]     Source y-height
* xwidth: [m]      Source x-width
* dist: [m]        Distance to target along z axis
* xw: [m]          Width(x) of target
* yh: [m]          Height(y) of target
* xdim: [int]      Number of subareas in the x direction
* ydim: [int]      Number of subareas in the y direction
* Lmin: [AA]       Minimum wavelength of neutrons
* Lmax: [AA]       Maximum wavelength of neutrons
* Emin: [eV]       Minimum energy of neutrons
* Emax: [eV]       Maximum energy of neutrons
* filename: [str]  Name of the file containing a list of the spectra file names
*
* %E
*******************************************************************************/
DEFINE COMPONENT Source_multi_surfaces

SETTING PARAMETERS (string filename=0, yheight=0.16,xwidth=0.09,dist=1.18,xw=0.08,yh=0.15,xdim=4,ydim=4,
Emin=0.0,Emax=0.0,Lmin=0.0,Lmax=0.0)

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

%include "read_table-lib"

char *get_token(char **src, char *token_sep)
{
        char    *tok;
        if (!src || !*src || !**src)
                return(NULL);
        while(**src && strchr(token_sep,**src))
                (*src)++;
        if (**src)
                tok = *src;
        else
                return(NULL);
        *src = strpbrk(*src,token_sep);
        if (*src)
        {
                **src = 0;
                (*src)++;
                while(**src && strchr(token_sep,**src))
                        (*src)++;
        }
        else
                *src = "";
        return(tok);
}


%}

DECLARE
%{
  double delta_lambda;
  double p_in;
  t_Table Tables[49];
%}

INITIALIZE
%{
  int n_surf=1,c_surf=1;                  	/*numbers of subsurfaces*/
  int rows_surf;                          	/*number of Source_multi_surfaces.comp rows*/
  int i, j, string_num,k;                 	/*filename_table indices*/

  char *token;
  FILE *files_name;
  char fu[50];
  struct filename_table{
       int index1;
       char spec_name[128];
       char tmp_name[512];
    };
  struct filename_table values[50];       

  for (i=0;i<50;i++)
  {
    values[i].index1=i;
    sprintf(values[i].spec_name,"empty"); 
  }
  
  if ((yheight == 0) || (xwidth == 0))
  {
    fprintf(stderr,"Source_multi_surfaces: Error: Please precise source geometry (yheight, xwidth)\n");
    exit(-1);
  }
  if (xw*yh == 0)   
  {
    fprintf(stderr,"Source_multi_surfaces: Error: Please precise source target (xw, yh)\n");
    exit(-1);
  }
  if ((xdim >= 8) || (ydim >= 8))
  {
    fprintf(stderr,"Source_multi_surfaces: Error: Number of subdivision in x or y too big (>=8)\n");
    exit(-1);
  }
  if (dist == 0)
  {
    fprintf(stderr,"Source_multi_surfaces: Error: dist = 0\n");
    exit(-1);
  }
  if ((Emin < 0) || (Emax < 0))
  {
    fprintf(stderr,"Source_multi_surfaces: Error: Energy will reach negative values (Emin or Emax < 0)\n");
    exit(-1);
  }  
  if ((Lmin < 0) || (Lmax < 0))
  {
    fprintf(stderr,"Source_multi_surfaces: Error: Wavelength will reach negative values (Lmin or Lmax < 0)\n");
    exit(-1);
  }
  
  if ((Emax != 0) && (Emin != 0))
  {
  Lmin=sqrt(81.82/Emax/1e3);             	/* wavelength in AA */
  Lmax=sqrt(81.82/Emin/1e3);  
  }

  delta_lambda=Lmax-Lmin;

  n_surf = xdim*ydim;
  
  /*read files_name.dat, generate a table with the files filename*/  

  files_name = fopen(filename, "r");

  i=0;
  rows_surf=0;  
  while(1) 
  {
    if (feof(files_name)) break;
    fgets(values[i].tmp_name,512,files_name);
    i=i+1;
    rows_surf=i;
  }                                            /*we do not consider the number of line*/
  
  j=0;i=0;k=0;
  for (j=0;j<rows_surf;j++)
  {
     token =strtok(values[j].tmp_name," ");   
     if (j>0) i=i+(7-xdim);
     sprintf(values[i].spec_name,"%s\n",token); 
     i=i+1;     
     k=k+1;
     while (token = strtok(0," "))
     {
        sprintf(values[i].spec_name,"%s\n",token);
        i=i+1;
        k=k+1;
        c_surf=k;
     }
   } 
  sprintf(values[i-1].spec_name,"%s","empty"); 
  fclose(files_name);
  rows_surf=rows_surf-1;
  
  if (n_surf!=c_surf)
  {
    fprintf(stderr,"Source_multi_surfaces: Error: Number of subdivision (xdim*ydim) and number of input file not equal (files_name.dat)\n");
    exit(-1);
  }  
  
  /*read each input file and fill in 49 arrays*/
  
  for (i=0;i<7;i++)
  {
    for (j=0;j<7;j++)
    {
      k=i*7+j;
      if (strcmp(values[k].spec_name,"empty"))
      {
        switch(j)
		{
			case 0 :
				if (xdim > 1) {
        				string_num=strlen(values[k].spec_name)-1; 
     				} else {
       				string_num=strlen(values[k].spec_name)-2; 
     				}  
				break;
			case 6 :
				if (xdim==7) {     
       				string_num=strlen(values[k].spec_name)-2; 
    				}
				break;
			default :
				if (xdim==j+1) {     
           				string_num=strlen(values[k].spec_name)-2; 
        			} else {
           				string_num=strlen(values[k].spec_name)-1;
        			}
		}
        strncpy(fu,values[k].spec_name ,string_num);
        fu[string_num] = '\0'; 
        Table_Read(&Tables[k], fu, 0); 
        if (Lmax > Table_Index(Tables[k],0,0))  {
          fprintf(stderr,"Source_multi_surfaces: Error: Lmax or Emin not present in %s",values[k].spec_name);
          fprintf(stderr,"Source_multi_surfaces: Choosen Lmax or Emin is %2.8f larger as the given Lmax-value in the file %s . \n",(Lmax-Table_Index(Tables[k],(Tables[k].rows-1),0)),values[k].spec_name);
          exit(-1);
        }       
        if (Lmin < Table_Index(Tables[k],(Tables[k].rows-1),0))  {
          fprintf(stderr,"Source_multi_surfaces: Error: Lmin or Emax not present in %s",values[k].spec_name);
          fprintf(stderr,"Source_multi_surfaces: Choosen Lmin or Emax is %2.8f smaller as the given Lmin-value in the file %s . \n",(Table_Index(Tables[k],0,0)-Lmin),values[k].spec_name);
          exit(-1);               
        } 
      } 
    }
  }

  xwidth=fabs(xwidth); yheight=fabs(yheight);
  xw = fabs(xw); yh=fabs(yh); dist=fabs(dist);

  /*generate p_in*/
  p_in = 1.0/mcget_ncount();
  
%}
TRACE
%{
  double theta0,phi0,theta1,phi1,theta,phi;
  double v,xpos,ypos;
  double intensity,lambda;
  double a,tan_v,tan_yheight;
  int i,j,k;

    z = 0;
    x = xwidth*randpm1()*0.5;         		   /*select point on the source (uniform)*/
    y = yheight*randpm1()*0.5;
   
    xpos = (x/xwidth+0.5)*xdim;       		   /*select the corresponding subsurface*/
    ypos = (y/yheight+0.5)*ydim;
    
    /* printf("xpos %f, ypos %f \n", xpos, ypos);  */
    
    theta0= -atan((x-xw/2.0)/dist);                /*Angles to aim at target*/
    phi0  = -atan((y-yh/2.0)/dist);
    theta1= -atan((x+xw/2.0)/dist);
    phi1  = -atan((y+yh/2.0)/dist);
    
    theta= theta0+(theta1- theta0)*rand01();       /*shot towards target*/
    phi  = phi0  +(phi1  - phi0)  *rand01();     
    
    lambda=Lmin+delta_lambda*rand01();		   /*select the lambda randomly*/

    intensity=0; a=0;

    /*select the correct neutron in the correct table and give its intensity*/

    /*assume a linear distribution between the values given in the files*/

    k = floor(ypos)*7+floor(xpos);

    for (i=0;i<(Tables[k].rows+1);i++)
      {
        if ((lambda <= Table_Index(Tables[k],i,0)) && (lambda > Table_Index(Tables[k],i+1,0))) 
        {        
          a=(Table_Index(Tables[k],i+1,1)-Table_Index(Tables[k],i,1))/(Table_Index(Tables[k],i+1,0)-Table_Index(Tables[k],i,0));
          intensity=a*lambda+(Table_Index(Tables[k],i,1)-a*Table_Index(Tables[k],i,0));
        }
      }
    
    /*calculate the speed*/
    
    v = K2V*(2*PI/lambda);

    /*calculate the p-value*/
    
    p = p*intensity*p_in*fabs((theta1 - theta0)*(phi1 - phi0));
    
    tan_yheight = tan(theta);
    tan_v = tan(phi);
    vz = v / sqrt(1 + tan_v*tan_v + tan_yheight*tan_yheight);
    vy = tan_v * vz;
    vx = tan_yheight * vz;    
    
    SCATTER;      

    /*printf("intensity: %f lambda: %f \n", intensity, lambda);*/
%}
FINALLY
%{
  int i;
  for (i=0;i<49;i++)
    Table_Free(&Tables[i]);
    
  fprintf(stderr,"Source_multi_surfaces: Memory cleared\n");
%}
MCDISPLAY
%{
  double xmin;
  double xmax;
  double ymin;
  double ymax;
  double xline;
  double yline;
  
  xmin = -xwidth/2; xmax = xwidth/2;
  ymin = -yheight/2; ymax = yheight/2;

  
  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);
        	   
  /*the grid*/
  
  for (xline=(xwidth/xdim)-(xwidth/2); xline < xwidth/2; xline=xline+(xwidth/xdim)) {
      line((double)xline, (double)ymin, 0.0, (double)xline, (double)ymax, 0.0);
      }
      
  for (yline=(yheight/ydim)-(yheight/2); yline < yheight/2; yline=yline+(yheight/ydim)) {
      line((double)xmin, (double)yline, 0.0, (double)xmax, (double)yline, 0.0);
      }

%}

END