/*******************************************************************************
*
* McStas, version 1.2 released February 2000
*         Maintained by Kristian Nielsen and Kim Lefmann,
*         Risoe National Laboratory, Roskilde, Denmark
*
* %IDENTIFICATION
*
* Written by: Jonas Okkels Birk (based upon Filteg_Graphite by Thomas C Hansen (2000))
* Date: 6 December 2006
* Origin: PSI
*
* Sapphire filter at 300K
*
* %DESCRIPTION
*
* This sapphire filter, defined by two identical rectangular opening apertures,
* is based upon an absorption function absorp=exp(L*A+C*(exp(-(B/L^2+D/L^4))))
* decribed by Freund (1983) Nucl. Instrum. Methods, A278, 379-401. The
* defaultvalues is for Sapphire at 300K as found by measurement by (Mildner &
* Lamaze (1998) J. Appl. Cryst. 31,835-840
* ( http://journals.iucr.org/j/issues/1998/06/00/hw0070/hw0070bdy.html#BB4)). It
* is possble to ajust the formular to other materials or temperatures by typing in
* other parameters.
* The transmission in sapphire is only lightly demependt on temperature, but
* The formular is only cheked at wavelenths between 0.5 and 14 AA (0.4 meV 0.3
* eV).
* The filter is for example used in the Eiger beamline at PSI to cut of high
* energies.
*
* %PARAMETERS
*
* INPUT PARAMETERS:
*
* xmin: [m]        Lower x bound
* xmax: [m]        Upper x bound
* ymin: [m]        Lower y bound
* ymax: [m]        Upper y bound
* len: [m]         Thickness of filter
* A: [m^-1 AA^-1]  
* B: [AA^2]        
* C: [m^-1]        
* D: [AA^4]        
*
* %LINKS
* %END
*******************************************************************************/

DEFINE COMPONENT Sapphire_Filter

SETTING PARAMETERS (xmin=-0.16, xmax=0.16, ymin=-0.16, ymax=0.16, len=0.1,A=0.8116,B=0.1618, C=21.24,D=0.1291)

/* STATE PARAMETERS (x,y,z,vx,vy,vz,t,s1,s2,p) */

TRACE
%{
  double L,L2,Filt_T;
  double dt;
  PROP_Z0;
  L = (2*PI/V2K)/sqrt(vx*vx + vy*vy + vz*vz);
    if (x<xmin || x>xmax || y<ymin || y>ymax) ABSORB;
    dt = len/vz;
    PROP_DT(dt);
    L2=L*L;
    Filt_T=(A*L+C*(1-exp(-B/L2-D/L2/L2)));
    Filt_T = exp(-Filt_T*len);
    p*=Filt_T;
%}

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);
  multiline(5, (double)xmin, (double)ymin, (double)len,
               (double)xmax, (double)ymin, (double)len,
               (double)xmax, (double)ymax, (double)len,
               (double)xmin, (double)ymax, (double)len,
               (double)xmin, (double)ymin, (double)len);
  line(xmin, ymin, 0.0, xmin, ymin, len);
  line(xmax, ymin, 0.0, xmax, ymin, len);
  line(xmin, ymax, 0.0, xmin, ymax, len);
  line(xmax, ymax, 0.0 , xmax, ymax, len);
%}
END
