/* Instrument: FOCUS
 *
 * %Identification
 * Written by: <a href="mailto:Uwe.Filges@psi.ch">Uwe Filges</a>
 * Date: Jan 2004
 * Origin: <a href="http://www.psi.ch">PSI</a>
 *
 * %INSTRUMENT_SITE: PSI
 *
 * The FOCUS Spectrometer at PSI (Paul Scherrer Institute,Switzerland)
 *
 * %Description
 * This instrument is a model of the FOCUS Spectrometer at PSI, Villigen, CH.
 *
 * input parameters :
 *    lambda      : source energy                               [Angs]
 *    chopp_ratio : chopper radio Fermi Chopper to Disk Chopper [1]
 *    DET         : Detector angle                              [deg]        
 *
 * building : 
 *     mcstas focus_psi.instr 
 *     gcc -O -o  focus_psi.out  focus_psi.c -lm
 *
 * Example: mcrun focus_psi.instr -n 1e6 lambda=3.4 chopp_ratio=1 DET=-69.9
 * 
 * %Parameters
 * INPUT PARAMETERS:
 *    lambda      : source energy                               [Angs]
 *    chopp_ratio : chopper radio Fermi Chopper to Disk Chopper [1]
 *    DET         : Detector angle                              [deg]    
 *
 * %Link
 * Written by U.Filges <uwe.filges@psi.ch> PSI, Jan 2004
 * %End 
*/ 


DEFINE INSTRUMENT FOCUS(lambda=3.4, chopp_ratio=1, DET=-69.9)
    
DECLARE
%{
 double lambda;
 double chopp_ratio;
 double DET;
 double PHM, TTM;
 double fo_x, fo_z;
 double RV_2, RH_2;
 double FO_PHA;
 double DISC_SPEED, FERMI_SPEED;
 /* mono d-spacing */
 double monodd = 3.355; 
 /* time constant [mus/A/m] */
 double c = 252.78;
 /* distance sample - detector */
 double dsd = 2.5;
 /* distance guide - monochromator */
 double dgm = 2.996;
 /* distance fermi-chopper - sample */
 double dfcs = 0.4997;
 /* distance disk chopper - monochromator */
 double ddcm = 2.944;
 /* distance  monochromator - fermi-chopper */
 double mfc = 1.002;
 /* distance  monochromator - sample */
 double dms = 1.4997;
 /* setting range of sources */ 
 double dL = 0.7;
 double LMIN, LMAX;
 double EMIN, EMAX;
 
%}

INITIALIZE
%{
  
 /* setting theta and 2Theta */
 PHM = asin(lambda/2/monodd)*180/PI;
 TTM = 2*PHM;

 /* setting speed of Fermi chopper, Disk chopper */
 FERMI_SPEED=1.0e6/(PI*lambda*c*(1./tan(PHM*PI/180))*(dsd*pow((1),1.5)+dfcs)*(1.0-(dms/dgm)));   
 DISC_SPEED = FERMI_SPEED/chopp_ratio;
 
 /* calculate the phase between Fermi and Disk chopper */
 FO_PHA = (c*lambda*(ddcm+mfc))/1e6;

 /* setting monochromator curvature */ 
 RV_2 = 2*dms*(sin(DEG2RAD*PHM)); 
 RH_2 = 2*dms/(sin(DEG2RAD*PHM));
 
 /* focusing neutrons on detector */
 fo_x = 2.5*sin(DET*PI/180);
 fo_z = 2.5*cos(DET*PI/180);
 
 /* setting E and L monitors */
 LMIN = lambda - dL;
 LMAX = lambda + dL;
 EMIN = 81.81/(LMAX*LMAX);
 EMAX = 81.81/(LMIN*LMIN); 

%}

TRACE

COMPONENT a1 = Arm()
  AT (0,0,0) ABSOLUTE

/* values for guide side window RNR11 */
COMPONENT csource = Source_gen(
   radius = 0.08,xw = 0.05, yh = 0.14, dist = 1.5,
   Lambda0 = 3.4, dLambda = dL,
   T1=150.42,I1=5.98e11,
   T2=37.23,I2=5.637e11,T3=14.9,I3=0.962e11)
AT (0,0,0) RELATIVE a1  ROTATED (0,0,0) RELATIVE a1

/* guide segment 1, m=2, 4.66 m */
 COMPONENT guide1 = Guide(w1=0.05, h1=0.12, w2=0.05, h2=0.12, 
                         l=4.66, R0=0.995, Qc=0.0217, alpha = 5.76, 
                         m = 2.0, W = 0.0033) 
    AT (0,0,1.5) RELATIVE a1 ROTATED (0,0,0) RELATIVE a1

/* guide segment 2, curved, m=2, 24.5 m */
COMPONENT guide2 = Bender(
    w = 0.05, h = 0.12, r = 1445, R0a = 0.995, Qca = 0.0217,
    alphaa = 5.76, ma = 2, Wa = 0.0033, R0i = 0.995, Qci = 0.0217,
    alphai = 5.76, mi = 2, Wi = 0.0033, R0s = 0.995, Qcs = 0.0217,
    alphas = 5.76, ms = 2, Ws = 0.0033, l = 24.5)
   AT (0,0,6.16) RELATIVE a1 ROTATED (0,0,0) RELATIVE a1

/* bunker wall, m=2, 3.0 m */
 COMPONENT bunker = Guide(w1=0.05, h1=.12, w2=0.05, h2=.12, 
        l=3.0, R0=0.995, Qc=0.0217, alpha = 5.76, m = 2.0, W = 0.0033) 
    AT (0,0,30.66) RELATIVE a1 ROTATED (0,0,0) RELATIVE a1 

/* guide segment 3, m=2, 32.95 m */
 COMPONENT guide3 = Guide(w1=0.05, h1=.12, w2=0.05, h2=.12, 
        l=32.95, R0=0.995,Qc=0.0217, alpha = 5.76, m = 2.0, W = 0.0033) 
    AT (0,0,33.66) RELATIVE a1 ROTATED (0,0,0) RELATIVE a1 

/* guide exit at 66.61 m - measured flux 2.53 10e8 cm-2 s-1 mA-1 */

COMPONENT LambdaGuideExit = L_monitor(
	xmin = -0.025, xmax = 0.025, ymin = -0.06, ymax = 0.06,
	Lmin = LMIN, Lmax = LMAX, nchan = 60,
	filename = "lambdaguide.dat")
  AT(0, 0, 66.611) RELATIVE a1 ROTATED (0,0,0) RELATIVE a1

COMPONENT DivMonGuideExit = Divergence_monitor(
          nh=60, nv=60, filename="divguide.dat",
          xmin=-0.025, xmax=0.025, ymin=-0.06, ymax=0.06, 
          h_maxdiv=2, v_maxdiv=2)
  AT (0, 0, 66.612) RELATIVE a1  ROTATED (0, 0, 0) RELATIVE a1

COMPONENT PSDGuideExit = PSD_monitor(
    xmin=-0.025, xmax=0.025, ymin=-0.06, ymax=0.06,
    nx=60, ny=60, filename="psdguide.dat")
  AT (0, 0, 66.613) RELATIVE a1  ROTATED (0, 0, 0) RELATIVE a1

/* Shutter position - distance neutron guide to FOCUS trumpet 0.06 m */

COMPONENT FOCUSguide = Channeled_guide(
     w1 = 0.05, h1 = 0.12,
     w2 = 0.05, h2 = 0.095,
     l = 2.95, R0 = 0.995, Qcx = 0.0217, Qcy = 0.0217,
     alphax = 5.76, alphay = 5.64, W = 0.0033, 
     k = 1, d = 0.002, mx = 2.4, my = 3)
  AT (0, 0, 66.67) RELATIVE a1 ROTATED (0, 0, 0) RELATIVE a1 

/* distance guide exit to chopper center 0.054 m */

COMPONENT FirstChopper = Chopper(
    R = 0.35, w = 0.07, f = DISC_SPEED, n = 2, pha = -FO_PHA, IsFirst = 1)
  AT (0, -0.3, 69.674) RELATIVE a1

COMPONENT  DISCTOF = TOF_monitor(
    nchan = 30, filename = "DISC_TOF.dat", xmin = -0.05,
    xmax = 0.05, ymin = -0.1, ymax = 0.1, dt = 100)
  AT (0, 0, 69.718) RELATIVE a1

COMPONENT PSDmon1Chopper = PSD_monitor(
    xmin=-0.05, xmax=0.05, ymin=-0.1, ymax=0.1,
    nx=60, ny=60, filename="psdchopper.dat")
  AT (0, 0, 69.719) RELATIVE a1  ROTATED (0, 0, 0) RELATIVE a1

/* Be-filter optinal; distance chopper center to Be-Filter entry 0.06 m, 
   Filter length 0.17 m */

/*
COMPONENT BeFilter = Filter(
    xmin = -0.035, xmax = 0.035, ymin = -0.06, ymax = 0.06,
    len = 0.17, T0 = 0.9, T1 = 0.1, Emin = 0.5, Emax = 20.0)
  AT (0, 0, 69.717) RELATIVE a1
*/

/* distance chopper center to vakuum tube entry 0.398 m */

/* vacuum tube part 1,  0.535 m */
 
COMPONENT VacuumTube1entry = Slit(radius = 0.085)
  AT (0, 0, 70.072) RELATIVE a1

COMPONENT VacuumTube1exit = Slit(radius = 0.085)
  AT (0, 0, 70.607) RELATIVE a1

/* vacuum tube part 2,  0.795 m */

COMPONENT VacuumTube2entry = Slit(radius = 0.1)
  AT (0, 0, 70.607) RELATIVE a1

COMPONENT VacuumTube2exit = Slit(radius = 0.1)
  AT (0, 0, 71.402) RELATIVE a1

/* vacuum tube part 3,  0.605 m */

COMPONENT VacuumTube3entry = Slit(radius = 0.1335)
  AT (0, 0, 71.402) RELATIVE a1

COMPONENT VacuumTube3exit = Slit(radius = 0.1335)
  AT (0, 0, 72.007) RELATIVE a1

/* distance vakuum tube exit to monochromator center 0.610 m */

COMPONENT PSDmonMono = PSD_monitor(
    xmin=-0.05, xmax=0.05, ymin=-0.1, ymax=0.1,
    nx=60, ny=60, filename="psdmono.dat")
  AT (0, 0, 72.4) RELATIVE a1  ROTATED (0, 0, 0) RELATIVE a1

COMPONENT  MONOTOF = TOF_monitor(
    nchan = 30, filename = "MONO_TOF.dat", xmin = -0.05,
    xmax = 0.05, ymin = -0.1, ymax = 0.1, dt = 1000)
  AT (0, 0, 72.41) RELATIVE a1

COMPONENT DivMonMono = Divergence_monitor(
          nh=60, nv=60, filename="divmono.dat",
          xmin=-0.05, xmax=0.05, ymin=-0.1, ymax=0.1, 
          h_maxdiv=3, v_maxdiv=3)
  AT (0, 0, 72.42) RELATIVE a1  ROTATED (0, 0, 0) RELATIVE a1


COMPONENT focus_mono = Arm()
   AT (0, 0, 72.617) RELATIVE a1 ROTATED (0, PHM, 0) RELATIVE a1

/* PG002=3.355 AA */

COMPONENT mono = Monochromator_2foc(
          zwidth = 0.019, yheight = 0.025, gap = 0.001,
          NH = 9, NV =  7,
          mosaich = 48, mosaicv = 48,
          r0 = 0.99,
          Q = 1.873,
          RV = RV_2, RH = RH_2)  
  AT (0, 0, 0) RELATIVE focus_mono

COMPONENT a2 = Arm()
  AT (0,0,0) RELATIVE focus_mono ROTATED (0, TTM, 0) RELATIVE a1

COMPONENT  FERMITOF_before = TOF_monitor(
    nchan = 30, filename = "FERMI_TOF_before.dat", xmin = -0.03,
    xmax = 0.03, ymin = -0.05, ymax = 0.05, dt = 1000)
  AT (0, 0, 0.9) RELATIVE a2

COMPONENT LambdaFermi = L_monitor(
	xmin = -0.05, xmax = 0.05, ymin = -0.1, ymax = 0.1,
	Lmin = LMIN, Lmax = LMAX, nchan = 60,
	filename = "lambdafermi.dat")
  AT(0, 0, 0.901) RELATIVE a2

COMPONENT EMON_Fermi = E_monitor(
	xmin = -0.03, xmax = 0.03, ymin = -0.05, ymax = 0.05,
	Emin = EMIN, Emax = EMAX, nchan = 60,
	filename = "emon_fermi.dat")
  AT(0, 0, 0.9397) RELATIVE a2 ROTATED (0,0,0) RELATIVE a2

COMPONENT DivMonfermi1 = Divergence_monitor(
          nh=30, nv=30, filename="divfermi1.dat",
          xmin=-0.03, xmax=0.03, ymin=-0.05, ymax=0.05, 
          h_maxdiv=2, v_maxdiv=2)
  AT (0, 0, 0.9398) RELATIVE a2  

COMPONENT PSD_Fermi1 = PSD_monitor(
    xmin=-0.03, xmax=0.03, ymin=-0.055, ymax=0.055,
    nx=30, ny=30, filename="psdfermi1.dat")
  AT (0, 0, 0.9399) RELATIVE a2  ROTATED (0, 0, 0) RELATIVE a2

COMPONENT FoChopper = FermiChopper(
    dist = 0, rad = 0.06, nu = FERMI_SPEED, ymin = -0.055, 
    ymax = 0.055, w = 0.0005, Nslit = 120, alpham = 0, 
    Qc = 0 , m = 0, Wi = 0.0001, R0 = 0, Vi = 0.0001, slit = 0.012)
  AT (0, 0, 1.002) RELATIVE a2

COMPONENT PSD_Fermi2 = PSD_monitor(
    xmin=-0.03, xmax=0.03, ymin=-0.055, ymax=0.055,
    nx=30, ny=30, filename="psdfermi2.dat")
  AT (0, 0, 1.063) RELATIVE a2  ROTATED (0, 0, 0) RELATIVE a2

COMPONENT DivMonfermi2 = Divergence_monitor(
          nh=30, nv=30, filename="divfermi2.dat",
          xmin=-0.03, xmax=0.03, ymin=-0.05, ymax=0.05, 
          h_maxdiv=2, v_maxdiv=2)
  AT (0, 0, 1.064) RELATIVE a2 

COMPONENT  FERMITOF1 = TOF_monitor(
    nchan = 30, filename = "FERMI_TOF1.dat", xmin = -0.03,
    xmax = 0.03, ymin = -0.05, ymax = 0.05, dt = 1000)
  AT (0, 0, 1.154) RELATIVE a2

COMPONENT SAMPLE_SLIT = Slit(
    xmin = -0.01, xmax = 0.01, ymin = -0.03, ymax = 0.03)
  AT (0, 0, 1.155) RELATIVE a2

COMPONENT  FERMITOF2 = TOF_monitor(
    nchan = 30, filename = "FERMI_TOF2.dat", xmin = -0.05,
    xmax = 0.05, ymin = -0.1, ymax = 0.1, dt = 100)
  AT (0, 0, 1.2) RELATIVE a2

COMPONENT  FERMITOF3 = TOF_monitor(
    nchan = 30, filename = "FERMI_TOF3.dat", xmin = -0.05,
    xmax = 0.05, ymin = -0.1, ymax = 0.1, dt = 100)
  AT (0, 0, 1.3) RELATIVE a2

COMPONENT  FERMITOF5 = TOF_monitor(
    nchan = 30, filename = "FERMI_TOF5.dat", xmin = -0.05,
    xmax = 0.05, ymin = -0.1, ymax = 0.1, dt = 100)
  AT (0, 0, 1.4) RELATIVE a2

COMPONENT PSD_SAMPLE = PSD_monitor(
    xmin=-0.01, xmax=0.01, ymin=-0.05, ymax=0.05,
    nx=30, ny=30, filename="psdsample.dat")
  AT (0, 0, 1.45) RELATIVE a2  ROTATED (0, 0, 0) RELATIVE a2

COMPONENT DivMon_Sample = Divergence_monitor(
          nh=30, nv=30, filename="div2.dat",
          xmin=-0.005, xmax=0.005, ymin=-0.03, ymax=0.03,
          h_maxdiv=3, v_maxdiv=3)
  AT (0, 0, 1.451) RELATIVE a2  ROTATED (0, 0, 0) RELATIVE a2

COMPONENT EMON_SAMPLE = E_monitor(
	xmin = -0.005, xmax = 0.005, ymin = -0.03, ymax = 0.03,
	Emin = EMIN, Emax = EMAX, nchan = 60,
	filename = "emon_sample.dat")
  AT(0, 0, 1.452) RELATIVE a2 ROTATED (0,0,0) RELATIVE a2

/* sample position */

COMPONENT FoSample = V_sample(
    radius_i = 0.0, radius_o = 0.008, h = 0.055, focus_r=0.4, pack=1,
    target_x=fo_x, target_y=0, target_z=fo_z)
  AT (0, 0, 1.5) RELATIVE a2

COMPONENT a3 = Arm()
  AT (0,0,1.5) RELATIVE a2 ROTATED (0,DET,0) RELATIVE a2

COMPONENT FoDet = Monitor_nD(
  xwidth = 0.336, yheight = 0.4,
  options="t auto file=TofDet.dat")
AT (0, 0, 2.5) RELATIVE a3 ROTATED (0,DET,0) RELATIVE a2

COMPONENT EMON_DET = E_monitor(
	xmin = -0.2, xmax = 0.2, ymin = -0.2, ymax = 0.2,
	Emin = EMIN, Emax = EMAX, nchan = 80, filename = "emon_det.dat")
  AT(0, 0, 2.501) RELATIVE a3 ROTATED (0,DET,0) RELATIVE a2   

END



























