/*******************************************************************************
* McXtrace, x-ray tracing package
*         Copyright, All rights reserved
*         DTU Physics, Kgs. Lyngby, Denmark
*         Synchrotron SOLEIL, Saint-Aubin, France
*
* Component: Pore_p
*
* %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 Pore as part of the Silicon Pore Optics (SPO) as envisioned for the ATHENA+ space telescope.
*
* %Description
* A single pore is simulated, which may have thick walls. 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. If primary they mirror extends backwards.
* The azimuthal curvature is defined by the parameter radius. This refers to the center of the pore. I.e the top
* and bottom plates have radius of curvature <radius+yheight/2> and <radius-yheight/2> 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.
*
* %Parameters
* Input parameters:
* radius_m: [m] Ring radius of the upper (reflecting) plate of the pore at the optic centre. 
* yheight:  [m] Height of the pore.
* xwidth:   [m] Width of the pore.
* 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).
* side_reflec:   [ ] Data file containing reflectivities of the side walls (LEFT and RIGHT).
* 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 pore is considered a primary reflector, and extends towards negative z. I.e. the entry plane is behind the z=0-plane. If zero, the pore 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 pore 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 pore axis.
* %End
*******************************************************************************/

DEFINE COMPONENT Pore_c
DEFINITION PARAMETERS ()
SETTING PARAMETERS (radius_m, Z0, xwidth, yheight, gap=0, chamferwidth=0, length=0, string mirror_reflec="", string bottom_reflec="", string side_reflec="", R_d=1, primary=1, dalpha=0, waviness=0, longw=0)
OUTPUT PARAMETERS (nLeft, nRight, nExit, nTop, nBottom, wLeft, wRight, wExit, wall, zentry, zexit, alpha,radius_1, radius_2,reflec_top_table, reflec_bottom_table,reflec_side_table,
        ref_prms)
/* X-ray parameters: (x,y,z,kx,ky,kz,phi,t,Ex,Ey,Ez,p) */ 

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","Pore_c");*/
            return status;
        }
        /*compute normal vector*/
        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 nRight[3];
    double wRight[3];
    double nLeft[3];
    double wLeft[3];
    double radius_1;
    double radius_2;
    double e_min[3];
    double e_step[3];
    double e_max[3];
    double theta_min[3];
    double theta_step[3];
    double theta_max[3];

    double zentry;
    double zexit;

    t_Table reflec_top_table;
    t_Table reflec_bottom_table;
    t_Table reflec_side_table;
    t_Table wave_table[1024];
%}

INITIALIZE
%{
    char *filenames[]={mirror_reflec,bottom_reflec,side_reflec};
    t_Table *ref_tables[]={&reflec_top_table,&reflec_bottom_table,&reflec_side_table};
    int i;

    /*read data from files into tables using read_table-lib*/
    for (i=0;i<3;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*/
    double 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;
    }

    double cosa=cos(xwidth/2.0/radius_m);
    double sina=sin(xwidth/2.0/radius_m);
    /*side wall, entry, and exit planes*/
    nLeft[0]= cosa;
    nLeft[1]=-sina;
    nLeft[2]=0;
    wLeft[0]=radius_m*(sina);
    wLeft[1]=radius_m*(1-cosa);
    wLeft[2]=0;

    nRight[0]=-cosa;
    nRight[1]=-sina;
    nRight[2]=0;
    wRight[0]=-radius_m*(sina);
    wRight[1]=-radius_m*(1-cosa);
    wRight[2]=0;

    nEntry[0]=0;
    nEntry[1]=0;
    nEntry[2]=-1;
    wExit[0]=wExit[1]=0;wExit[2]=zentry;

    nExit[0]=0;
    nExit[1]=0;
    nExit[2]=1;
    wExit[0]=wExit[1]=wExit[2]=0;

%}

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

    /*first do a test prop to see if the photon will enter the pore*/
    double tmpt,tmpx,tmpy, dl;
    tmpt=(zentry-z)/kz;
    tmpx=x+kx*tmpt;
    tmpy=y+ky*tmpt;

    double phi_max,phi_min,phi;
    phi_min=-xwidth*0.5/radius_m;
    phi_max= xwidth*0.5/radius_m;
    phi=atan2(tmpx,tmpy+radius_m);

    hit_pore= ( ( tmpx*tmpx + (tmpy+radius_m)*(tmpy+radius_m) < radius_1*radius_1 ) && ( tmpx*tmpx + (tmpy+radius_m)*(tmpy+radius_m) >(radius_1-yheight)*(radius_1-yheight) ) && (phi>phi_min && phi<phi_max)) ;
    hit_chamfer=0;

    if(hit_pore){
        /*Moving photon to z=zentry. This odd way of writing this, is to handle phase and time automatically.*/
        if(zentry){
            z-=zentry;
            ALLOW_BACKPROP;
            PROP_Z0;
            z+=zentry;
        }

        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;/*index indicating which table parameter set to choose*/
            /*left wall*/            
            intersections[LEFT]=plane_intersect(l+LEFT,x,y,z,kx,ky,kz,nLeft[0],nLeft[1],nLeft[2],wLeft[0],wLeft[1],wLeft[2]);
            if (intersections[LEFT] && l[LEFT]>DBL_EPSILON && l[LEFT]<l_small) {l_small=l[LEFT];i_small=intersections[LEFT];wall=LEFT;}
            /*right wall*/            
            intersections[RIGHT]=plane_intersect(l+RIGHT,x,y,z,kx,ky,kz,nRight[0],nRight[1],nRight[2],wRight[0],wRight[1],wRight[2]);
            if (intersections[RIGHT] && l[RIGHT]>DBL_EPSILON && l[RIGHT]<l_small) {l_small=l[RIGHT];i_small=intersections[RIGHT];wall=RIGHT;}
            /*exit plane*/
            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+radius_m,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+radius_m,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;}

            /*sort intersections ot find the smallest positive one*/
            switch (wall){
                case LEFT:
                    /*handle left wall "reflection"*/
                    reflec_table=&reflec_side_table;
                    nx=nLeft[0];ny=nLeft[1];nz=nLeft[2];
                    prm_idx=2;
                    break;
                case RIGHT:
                    /*handle right wall "reflection"*/
                    reflec_table=&reflec_side_table;
                    nx=nRight[0];ny=nRight[1];nz=nRight[2];
                    prm_idx=2;
                    break;
                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 dth,theta,t0,t1,inner_m,inner_o, radius_o, zo,zm;
    const int N=20;

    theta=xwidth/2.0/radius_m;
    inner_m=radius_m-yheight;

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

    if(primary){
        radius_o=(Z0p-zo)/Z0p*radius_m;
    }else{
        radius_o=(Z0p-zexit)/Z0p*radius_m;
    }
    inner_o=radius_o-yheight;

    magnify("");
    line(0,0,zm, 0,radius_o-radius_m,zo); /*this extra line indicates the reflecting surface*/
    line( sin(theta)*radius_m, (cos(theta)-1)*radius_m, zm,  sin(theta)*radius_o, cos(theta)*radius_o-radius_m, zo);
    line(-sin(theta)*radius_m, (cos(theta)-1)*radius_m, zm, -sin(theta)*radius_o, cos(theta)*radius_o-radius_m, zo);   
 
    line( sin(theta)*inner_m, cos(theta)*inner_m-radius_m, zm,  sin(theta)*inner_o, cos(theta)*inner_o-radius_m, zo);
    line(-sin(theta)*inner_m, cos(theta)*inner_m-radius_m, zm, -sin(theta)*inner_o, cos(theta)*inner_o-radius_m, zo);

    line( sin(theta)*radius_m, (cos(theta)-1)*radius_m, zm,  sin(theta)*inner_m, cos(theta)*inner_m-radius_m, zm);
    line(-sin(theta)*radius_m, (cos(theta)-1)*radius_m, zm, -sin(theta)*inner_m, cos(theta)*inner_m-radius_m, zm);
    line( sin(theta)*radius_o, cos(theta)*radius_o-radius_m, zo,  sin(theta)*inner_o, cos(theta)*inner_o-radius_m, zo);
    line(-sin(theta)*radius_o, cos(theta)*radius_o-radius_m, zo, -sin(theta)*inner_o, cos(theta)*inner_o-radius_m, zo);

    dth=2*theta/N;
    for (k=1;k<N+1;k++){
        t0=-theta+(k-1)*dth;
        t1=-theta+k*dth;
        line( sin(t0)*radius_m, cos(t0)*radius_m-radius_m, zm, sin(t1)*radius_m, cos(t1)*radius_m-radius_m, zm);
        line( sin(t0)*inner_m, cos(t0)*inner_m-radius_m, zm, sin(t1)*inner_m, cos(t1)*inner_m-radius_m, zm);
    
        line( sin(t0)*radius_o, cos(t0)*radius_o-radius_m, zo, sin(t1)*radius_o, cos(t1)*radius_o-radius_m, zo);
        line( sin(t0)*inner_o, cos(t0)*inner_o-radius_m, zo, sin(t1)*inner_o, cos(t1)*inner_o-radius_m, zo);
    }

%}

END
