File: Shell_c.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 (343 lines) | stat: -rw-r--r-- 12,550 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
/*******************************************************************************
*
* McXtrace, x-ray tracing package
*         Copyright, All rights reserved
*         DTU Physics, Kgs. Lyngby, Denmark
*         Synchrotron SOLEIL, Saint-Aubin, France
*
* Component: Shell_c
*
* %Identification
*
* Written by: Erik B Knudsen and Desiree D. M. Ferreira
* Date: Feb. 2016
* Version: 1.0
* Release: McXtrace 1.2
* Origin: DTU Physics, DTU Space
*
* Single conical shell as part of a Wolter optic.
*
* %Description
* A single shell is simulated. The top and bottom are curved cylindrically
* azimuthally, whereas they are straight sagitally. The primary parameter specifies whether this is a
* primary or secondary mirror.
* The azimuthal curvature is defined by the parameter radius. This refers to the top plate of the shell. I.e the top
* and bottom plates have radius of curvature <radius> and <radius-yheight> respectively.
*
* To intersect the Wolter I plates we take advatage of the azimuthal symmetry and only consider the radial component
* of the photon's wavevector.
*
* Example: Shell_c( radius_m=0.535532,Z0=12,yheight=1e-2,length=0.5,primary=0, R_d=1)
*
* %Parameters
* Input parameters:
* radius_m: [m] Ring radius of the upper (reflecting) plate of the shell at the optic centre.
* yheight:  [m] Height of the shell.
* chamferwidth:  [m] Width of side walls.
* gap:      [m] Gap between the plate and the intersection plane with the hyperbolic section. (currently ignored)
* Z0:       [m] Distance between optics centre plane and focal spot (essentially focal length).
* mirror_reflec: [ ] Data file containing reflectivities of the reflector surface (TOP).
* bottom_reflec: [ ]  Data file containing reflectivities of the bottom surface (BOTTOM).
* R_d:      [ ] Default reflectivity value to use if no reflectivity file is given. Useful f.i. is one surface is reflecting and the others absorbing.
* primary:  [ ] If non-zero, the shell is considered a primary reflector, and extends towards negative z. I.e. the entry plane is behind the z=0-plane. If zero, the shell is considered secondary
*               and extends from the z=0-plane and towards positive z.
* dalpha:   [deg] Offset to the alpha angle computed from the focal length. Useful for targeting the modified conical geometry (currently ignored).
* waviness: [rad] Waviness of the shell reflecting surface. The slope error is assumed to be uniformly distributed in the interval [-waviness:waviness].
* longw:    [ ] If non-zero, waviness is 1D and along the shell axis.
* %End
*******************************************************************************/

DEFINE COMPONENT Shell_c
SETTING PARAMETERS (radius_m, Z0, yheight, gap=0, chamferwidth=0, length=0, string mirror_reflec="", string bottom_reflec="", R_d=1, primary=1, dalpha=0, waviness=0, longw=0)

SHARE
%{
#ifndef MCSPO_INTERSECT_CONE
#define MCSPO_INTERSECT_CONE 1

    int intersect_cone(double *l0, double x, double y, double z, double kx, double ky, double kz, double alpha, double radius, double *nx, double *ny, double *nz){
        double kxn=kx,kyn=ky,kzn=kz;
        NORM(kxn,kyn,kzn);
        double c=tan(alpha);
        double z0=radius/c;
        double c2=c*c;
        double A,B,C;
        A=kxn*kxn + kyn*kyn - c2*kzn*kzn;
        B=2*(kxn*x + kyn*y  - c2*kzn*(z-z0));
        C=x*x + y*y - c2*(z-z0)*(z-z0);

        int status;
        double l1;
        if ( (status=solve_2nd_order(l0,&l1,A,B,C))==0 ){
            /*note that if l1->NULL only the smallest positive solution is returned*/
            //fprintf(stderr,"Error(%s): No solution to second order eq.\n","Shell_c");
            return status;
        }
        /*compute normal vector here*/
        x+=kxn* (*l0);
        y+=kyn* (*l0);
        z+=kzn* (*l0);

        double vn=sqrt(x*x+y*y);
        *nx=x/vn;
        *ny=y/vn;

        *nz=1;

        *nx *= cos(alpha);
        *ny *= cos(alpha);
        *nz *= sin(alpha);

        return status;
    }
#endif
%}

DECLARE
%{
    double nExit[3];
    double wExit[3];
    double nEntry[3];
    double wEntry[3];
    double nTop[3];
    double nBottom[3];
    double alpha;
    double radius_1;
    double radius_2;
    double e_min[2];
    double e_step[2];
    double e_max[2];
    double theta_min[2];
    double theta_step[2];
    double theta_max[2];

    double zentry;
    double zexit;

    t_Table reflec_top_table;
    t_Table reflec_bottom_table;
%}

INITIALIZE
%{
    /*read data from files into tables using read_table-lib*/
    char *filenames[2]={mirror_reflec,bottom_reflec};
    t_Table *ref_tables[2]={&reflec_top_table,&reflec_bottom_table};
    int i;

    /*read data from files into tables using read_table-lib*/
    for (i=0;i<2;i++){
        char *reflec=filenames[i];
        t_Table *tp=ref_tables[i];
        if (reflec && strlen(reflec)) {
            char **header_parsed;

            /* read 1st block data from file into tp */
            if (Table_Read(tp, reflec, 1) <= 0)
            {
                exit(fprintf(stderr,"Error(%s): can not read file %s\n",NAME_CURRENT_COMP, reflec));
            }
            header_parsed = Table_ParseHeader(tp->header,
                    "e_min=","e_max=","e_step=","theta_min=","theta_max=","theta_step=",NULL);
            if (header_parsed[0] && header_parsed[1] && header_parsed[2] &&
                    header_parsed[3] && header_parsed[4] && header_parsed[5])
            {
                e_min[i]=strtod(header_parsed[0],NULL);
                e_max[i]=strtod(header_parsed[1],NULL);
                e_step[i]=strtod(header_parsed[2],NULL);
                theta_min[i]=strtod(header_parsed[3],NULL);
                theta_max[i]=strtod(header_parsed[4],NULL);
                theta_step[i]=strtod(header_parsed[5],NULL);
            } else {
                exit(fprintf(stderr,"Error (%s): wrong/missing header line(s) in file %s\n", NAME_CURRENT_COMP, reflec));
            }
            if (!((int)(e_max[i]-e_min[i]) == (int)((tp->rows-1)*e_step[i])))
            {
                exit(fprintf(stderr,"Error (%s): e_step does not match e_min and e_max in file %s\n",NAME_CURRENT_COMP, reflec));
            }
            if (!((int)(theta_max[i]-theta_min[i]) == (int)((tp->columns-1)*theta_step[i])))
            {
                exit(fprintf(stderr,"Error (%s): theta_step does not match theta_min and theta_max in file %s\n",NAME_CURRENT_COMP, reflec));
            }
        }else{
            /*mark the table as unread by setting "rows" to -1
              This will trigger the default reflectivity.*/
            tp->rows=-1;
        }
    }

    /* compute some pore parameters*/
    alpha=0.25*atan(radius_m/Z0);

    double D,Z0p;
    if (primary){
        Z0p=radius_m/tan(alpha);
        D=sqrt(radius_m*radius_m + Z0p*Z0p);
        zentry=Z0p*(1-(D+length)/D);
        zexit=0;
        radius_1=(D+length)/D*radius_m;
        radius_2=radius_m;
    }else{
        alpha*=3.0;
        Z0p=radius_m/tan(alpha);
        D=sqrt(radius_m*radius_m + Z0p*Z0p);
        zentry=0;
        zexit=Z0p*(1-(D-length)/D);
        radius_1=radius_m;
        radius_2=(D-length)/D*radius_m;
    }
    nEntry[0]=nEntry[1]=0; nEntry[2]=-1;
    wEntry[0]=wEntry[1]=0; wEntry[2]=zentry;

    nExit[0]=nExit[1]=0; nExit[2]=1;
    wExit[0]=wExit[1]=0; wExit[2]=zexit;
%}

TRACE
%{
    enum {LEFT, RIGHT, TOP, BOTTOM, EXIT, NONE} wall;
    t_Table *reflec_table=NULL;
    int hit_shell, hit_chamfer;
    double R;

    /*Moving photon to z=-zentry. This odd way of writing this is to handle phase and time automatically.
      Note that zentry is z<0, hence the sign convention.*/
    z-=zentry;
    ALLOW_BACKPROP;
    PROP_Z0;
    z+=zentry;

    hit_shell= ( ( x*x + y*y < radius_1*radius_1 ) && ( x*x + y*y >(radius_1-yheight)*(radius_1-yheight) ) ) ;
    hit_chamfer=0;
    if(hit_shell){
        SCATTER;
        int exit=0;
        int intersections[5]={0,0,0,0,0};
        int i_small;
        double l[5]={100000.0, 100000.0, 100000.0, 100000.0, 100000.0};
        double l_small;

        double nx,ny,nz;

        while (!exit){
            l_small=DBL_MAX;
            wall=NONE;
            double nx,ny,nz;
            double wx,wy,wz;
            int prm_idx;

            intersections[EXIT]=plane_intersect(l+EXIT,x,y,z,kx,ky,kz,nExit[0],nExit[1],nExit[2],wExit[0],wExit[1],wExit[2]);
            if (intersections[EXIT] && l[EXIT]>DBL_EPSILON && l[EXIT]<l_small) {l_small=l[EXIT];i_small=intersections[EXIT];wall=EXIT;}
            /*top surface - the real reflecting surface*/
            intersections[TOP]=intersect_cone((l+TOP),x,y,z,kx,ky,kz,alpha,radius_m,&(nTop[0]),&(nTop[1]),&(nTop[2]));
            if (intersections[TOP] && l[TOP]>DBL_EPSILON && l[TOP]<l_small) {l_small=l[TOP];i_small=intersections[TOP];wall=TOP;}
            /*bottom surface*/
            intersections[BOTTOM]=intersect_cone((l+BOTTOM),x,y,z,kx,ky,kz,alpha,radius_m-yheight,&(nBottom[0]),&(nBottom[1]),&(nBottom[2]));
            if (intersections[BOTTOM] && l[BOTTOM]>DBL_EPSILON && l[BOTTOM]<l_small) {l_small=l[BOTTOM];i_small=intersections[BOTTOM];wall=BOTTOM;}

            switch (wall){
                case TOP:
                    /*handle top wall reflection*/
                    reflec_table=&reflec_top_table;
                    nx=nTop[0];ny=nTop[1];nz=nTop[2];
                    prm_idx=0;
                    break;
                case BOTTOM:
                    /*handle bottom wall "reflection"*/
                    reflec_table=&reflec_bottom_table;
                    nx=nBottom[0];ny=nBottom[1];nz=nBottom[2];
                    prm_idx=1;
                    break;
                case EXIT:
                    /*photon will exit pore*/
                    exit=1;
                    break;
            }
            if(exit){
                continue;
            }
            PROP_DL(l_small);

            double kix=kx,kiy=ky,kiz=kz;
            double k=sqrt(kx*kx+ ky*ky + kz*kz);
            double e=K2E*k;
            double s=scalar_prod(kx,ky,kz,nx,ny,nz);
            double theta=RAD2DEG*(M_PI_2-acos(s/k)); /*pi_2 since theta is supposed to be the grazing angle*/

            /*if we have waviness alter the normal vector slightly*/
            if(waviness!=0){
                /*assuming theta to be small we might disregard atan*/
                if(longw){
                    double dtheta;
                    if(theta<waviness){
                        dtheta=rand01()*(theta+waviness)-theta;
                    }else{
                        dtheta=randpm1()*waviness;
                    }
                    double tx,ty,tz;
                    vec_prod(tx,ty,tz,0,0,1,nx,ny,nz);
                    rotate(nx,ny,nz, nx,ny,nz, dtheta, tx,ty,tz);
                }else{
                    /*waviness is also transversal but isotropic*/
                    double radius;
                    if(theta<waviness){
                        radius=atan(waviness);
                        randvec_target_circle(&nx,&ny,&nz,NULL,nx,ny,nx,radius);
                    }else{
                        radius=(atan(theta)+atan(waviness))/2.0;
                        randvec_target_circle(&nx,&ny,&nz,NULL,nx,ny,nx+radius-atan(theta),radius);
                    }
                    NORM(nx,ny,nz);
                }
                /*recompute theta*/
                theta=RAD2DEG*0.5*acos(scalar_prod(kx,ky,kz,kix,kiy,kiz)/k/k);
            }
            /*reflect the photon through the surface normal*/
            if(s!=0){
                kx-=2*s*nx;
                ky-=2*s*ny;
                kz-=2*s*nz;
            }
            SCATTER;

            if(reflec_table==NULL || reflec_table->rows==-1){
                R=R_d;
            }else{
                R=Table_Value2d(*reflec_table,(e-e_min[prm_idx])/e_step[prm_idx], (theta-theta_min[prm_idx])/theta_step[prm_idx]);
            }
            p*=R;
        }
    }else if (hit_chamfer){
        ABSORB;
    }else{
        /*no hit*/
        ABSORB;
    }
%}

MCDISPLAY
%{
    int k;
    double zo,zm,radius_o;
    magnify("");
    zm=0;

    double D,Z0p;
    Z0p=radius_m/tan(alpha);
    D=sqrt(radius_m*radius_m + Z0p*Z0p);
    if (primary){
        zo=Z0p*(1-(D+length)/D);
        radius_o=(Z0p-zo)/Z0p*radius_m;
    }else{
        zo=Z0p*(1-(D-length)/D);
        radius_o=(Z0p-zexit)/Z0p*radius_m;
    }

    circle("xy",0,0,zm,radius_m);
    circle("xy",0,0,zm,radius_m-yheight);

    circle("xy",0,0,zo,radius_o);
    circle("xy",0,0,zo,radius_o-yheight);
%}

END