/*******************************************************************************
* Instrument: PSI_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.
*
* %Example: lambda=3.4 Detector: PSD_Fermi1_I=1.44416e+07
* Example: lambda=3.4 Detector: PSD_SAMPLE_I=1.58754E+05
*
* %Parameters
* lambda: [Angs]    source energy
* chopp_ratio: [1]  Chopper radio Fermi Chopper to Disk Chopper
* DET: [deg ]       Detector angle
*
* %Link
* Written by U.Filges <uwe.filges@psi.ch> PSI, Jan 2004
* %End
*******************************************************************************/
DEFINE INSTRUMENT PSI_Focus(lambda=3.4, chopp_ratio=1, DET=-69.9)

DECLARE
%{
 double lambda;
 double chopp_ratio;
 double DET;
 double PHM, TTM;
 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));

 /* setting E and L monitors */
 LMIN = lambda - dL;
 LMAX = lambda + dL;
 EMIN = 81.81/(LMAX*LMAX);
 EMAX = 81.81/(LMIN*LMIN);

%}

TRACE

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

/* values for guide side window RNR11 */
COMPONENT csource = Source_gen(
   yheight = 0.135, xwidth = 0.08, dist=1.77, focus_xw = 0.05, focus_yh = 0.12,
   lambda0 = 3.4, dlambda = dL, T1= 296.16, I1=8.5E11,
   T2=40.68, I2=5.2E11)
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(
	xwidth = 0.05, yheight = 0.12,
	Lmin = LMIN, Lmax = LMAX, nL = 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",
          xwidth = 0.05, yheight = 0.12,
          maxdiv_h=2, maxdiv_v=2)
  AT (0, 0, 66.612) RELATIVE a1  ROTATED (0, 0, 0) RELATIVE a1

COMPONENT PSDGuideExit = PSD_monitor(
    xwidth = 0.05, yheight = 0.12,
    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 = Guide_channeled(
     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,
     nslit = 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 = DiskChopper(theta_0=0, abs_out=0, verbose=1,
				     radius = 0.70, yheight=0.70, xwidth = 0.07, nu = DISC_SPEED/2/PI, nslit = 2, delay = FO_PHA, isfirst = 1)
  AT (0, 0, 69.674) RELATIVE a1

  /*COMPONENT  DISCTOF = Monitor_nD(
    bins = 30, filename = "DISC_TOF.dat", xmin = -0.05,
    xmax = 0.05, ymin = -0.1, ymax = 0.1, options="auto, time")
    AT (0, 0, 69.718) RELATIVE a1*/

COMPONENT PSDmon1Chopper = PSD_monitor(
    xwidth = 0.1, yheight = 0.2,
    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 vacuum 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(
    xwidth = 0.1, yheight = 0.2,
    nx=60, ny=60, filename="psdmono.dat")
  AT (0, 0, 72.4) RELATIVE a1  ROTATED (0, 0, 0) RELATIVE a1

  /*COMPONENT  MONOTOF = Monitor_nD(
    bins = 30, filename = "MONO_TOF.dat", xmin = -0.05,
    xmax = 0.05, ymin = -0.1, ymax = 0.1, options="auto, time")
    AT (0, 0, 72.41) RELATIVE a1*/

COMPONENT DivMonMono = Divergence_monitor(
          nh=60, nv=60, filename="divmono.dat",
          xwidth = 0.1, yheight = 0.2,
          maxdiv_h=3, maxdiv_v=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 */

SPLIT 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 = Monitor_nD(
    bins = 30, filename = "FERMI_TOF_before.dat", xmin = -0.05,
    xmax = 0.05, ymin = -0.1, ymax = 0.1, options="auto, time")
    AT (0, 0, 0.9) RELATIVE a2*/

COMPONENT lambdaFermi = L_monitor(
	xwidth=0.1, yheight=0.2,
	Lmin = LMIN, Lmax = LMAX, nL = 60,
	filename = "lambdafermi.dat")
  AT(0, 0, 0.901) RELATIVE a2

COMPONENT EMON_Fermi = E_monitor(
	xwidth=0.06, yheight=0.1,
	Emin = EMIN, Emax = EMAX, nE = 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",
          xwidth=0.06, yheight=0.1,
          maxdiv_h=2, maxdiv_v=2)
  AT (0, 0, 0.9398) RELATIVE a2

COMPONENT PSD_Fermi1 = PSD_monitor(
    xwidth=0.06, yheight=0.11,
    nx=30, ny=30, filename="psdfermi1.dat")
  AT (0, 0, 0.9399) RELATIVE a2  ROTATED (0, 0, 0) RELATIVE a2

COMPONENT FoChopper = FermiChopper(
    radius = 0.06, nu = -FERMI_SPEED, yheight = 0.11,
    w = 0.0005, nslit = 120, alpha = 0,
    Qc = 0 , m = 0, W = 0.0001, R0 = 0, length = 0.012)
  AT (0, 0, 1.002) RELATIVE a2

COMPONENT PSD_Fermi2 = PSD_monitor(
    xwidth=0.06, yheight=0.11,
    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",
          xwidth=0.06, yheight=0.1,
          maxdiv_h=2, maxdiv_v=2)
  AT (0, 0, 1.064) RELATIVE a2

  /*COMPONENT  FERMITOF1 = Monitor_nD(
    bins = 30, filename = "FERMI_TOF1.dat.dat", xmin = -0.05,
    xmax = 0.05, ymin = -0.1, ymax = 0.1, options="auto, time")
    AT (0, 0, 1.154) RELATIVE a2*/

COMPONENT SAMPLE_SLIT = Slit(
    xwidth=0.02, yheight=0.06)
  AT (0, 0, 1.155) RELATIVE a2

  /*COMPONENT  FERMITOF2 = Monitor_nD(
    bins = 30, filename = "FERMI_TOF2.dat.dat", xmin = -0.05,
    xmax = 0.05, ymin = -0.1, ymax = 0.1, options="auto, time")
    AT (0, 0, 1.2) RELATIVE a2*/

COMPONENT PSD_SAMPLE = PSD_monitor(
    xwidth=0.02, yheight=0.1,
    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",
          xwidth=0.01, yheight=0.06,
          maxdiv_h=3, maxdiv_v=3)
  AT (0, 0, 1.451) RELATIVE a2  ROTATED (0, 0, 0) RELATIVE a2

COMPONENT EMON_SAMPLE = E_monitor(
	xwidth=0.01, yheight=0.06,
	Emin = EMIN, Emax = EMAX, nE = 60,
	filename = "emon_sample.dat")
  AT(0, 0, 1.452) RELATIVE a2 ROTATED (0,0,0) RELATIVE a2

/* sample position */

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

SPLIT COMPONENT Sample = Incoherent(
    radius = 0.008, yheight = 0.055, focus_xw=0.336, focus_yh=0.4, pack=1,
    target_index=+1)
  AT (0, 0, 0) RELATIVE PREVIOUS

  /*COMPONENT TOF_Det = Monitor_nD(
 xwidth=1.5, yheight=0.2,
 options="auto time, abs angle limits=[10 180], banana, parallel",
 bins=100)
 AT(0, 0, 0) RELATIVE PREVIOUS*/

  /*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 a3*/

COMPONENT EMON_DET = E_monitor(
	xwidth=0.4, yheight=0.4,
	Emin = EMIN, Emax = EMAX, nE = 80, filename = "emon_det.dat")
  AT(0, 0, 2.501) RELATIVE a3 ROTATED (0,DET,0) RELATIVE a3

END
