/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2008, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Instrument: ESS_IN5_reprate
*
* %Identification
* Written by: Kim Lefmann (kim.lefmann@risoe.dk), Helmuth Schober, Feri Mezei
* Date: September 20, 2006
* Origin:ESS
* %INSTRUMENT_SITE: ESS
*
* An IN5 type (cold chopper) multi-frame spectrometer at ESS LPTS
*
* %Description
* McStas instrument for simulating IN5-TYPE (cold chopper) multi-frame spectrometer at ESS LPTS.
* The sample is incoherent with an inelastic tunneling line.
*
* %Example: Lmin=4.9 Detector: PSDfast2_I=1.18335e+10
* %Example: Lmin=4.9 Detector: PSDsample_I=1.8583e+07
*
* %Parameters
* Lmin: [AA]              Minimum wavelength emitted by the moderator
* Lmax: [AA]              Maximum wavelength emitted by the moderator
* lambda0: [AA]           Target wavelength for the monochromating crystal
* Pulse_width: [s]        Length of the long pulse. Default to 2 ms.
* Num_pulses: [1]         Number of pulses to simulate. Default 1.
* GUI_start: [m]          Distance from moderator to guide start
* FO1_DIST: [m]           Distance from moderator to FO1 chopper
* L_ballistic_begin: [m]  Length of expanding ballistic section in the beginning of the guide
* L_ballistic_end: [m]    Length of compressing ballistic section in the end of the guide
* Length: [m]             Total length of spectrometer (moderator to F2)
* SAMPLE_DIST: [m]        Distance from F2 to sample
* DETECTOR_DIST: [m]      Length sample-detector
* GUI_h: [m]              Height of guide opening at entry
* GUI_w: [m]              Width of guide opening at entry
* H1: [m]                 Height of guide elements
* H2: [m]                 Height of guide elements
* H3: [m]                 Height of guide elements
* H4: [m]                 Height of guide elements
* W1: [m]                 Width of guide elements
* W2: [m]                 Width of guide elements
* W3: [m]                 Width of guide elements
* W4: [m]                 Width of guide elements
* GUI_GAP: [m]            Gap between guides due to chopper hardware
* H_chop: [m]             Height at F2 chopper position
* W_chop: [m]             Width at F2 chopper position
* H_end: [m]              Height of guide opening at exit
* W_end: [m]              Width of guide opening at exit
* ALPHA: [m]              Parameter for supermirrors in guides, Swiss Neutronics best at 3.4
* M: [1]                  Parameter for supermirrors in guides, Swiss Neutronics best at 4
*
* F_slow1: [Hz]           Frequency of the FO1 chopper
* F_slow2: [Hz]           Frequency of the FO2 chopper
* F_fast1: [Hz]           Frequency of the F1 chopper
* F_fast2: [Hz]           Frequency of the F2 chopper
* N_fast: [1]             Number of symmetric openings in the fast F choppers
* SLOW1_THETA: [deg]      Opening angle of the FO1 chopper
* FO3: [1]                Inverse frequency of FO3 chopper (if 1, same as F2; if 2, half of F2, etc.)
* THETA_fast1: [deg]      Opening angle of the F1 chopper
* FAST_THETA: [deg]       Opening angle of the F2 chopper
* Gamma: [meV]            Width of sample quasielastic scattering (0=elastic)
* Etun: [meV]             Tunneling energy
* V_HOLE: [m]             Radius of hole in V sample (must be smaller than the outer radius: 0.01)
* FRAC_QUASIEL: [1]       Fraction of the sample that scatters quasielastically
* TT: [deg]               2Theta (or a4) scattering angle
* RES_DE: [meV]           Adjustable zoom for energy monitor at detector position
* FRAC_TUNNEL: [ ]        Fraction of the sample that scatters on the tunneling peaks   
* cold: [ ]               Fraction of neutrons emitted from the cold moderatorx
*
* %Link
* The <a href="http://wwwold.ill.fr/Computing/links/meetings/ESS-LP/">ESS-Rencurel</a> website.
* %Link
* <a href="http://www.ill.fr/in5">The IN5@ILL Yellow Pages</a>
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT ESS_IN5_reprate(Lmin=4.9, Lmax=5.1, lambda0=5, Pulse_width=2.857e-3, Num_pulses=1, GUI_start=2.0,  FO1_DIST=6, L_ballistic_begin=19.5, L_ballistic_end=17, Length=100, SAMPLE_DIST=1.2,  DETECTOR_DIST=4, GUI_h=0.105, GUI_w=0.1, GUI_GAP=0.05, H1=0.167, W1=0.116, H2=0.185, W2=0.15, H3=0.19, W3=0.15, H4=0.213, W4=0.14, H_chop=0.075, W_chop=0.03, H_end=0.042,  W_end=0.0215, ALPHA=3.4, M=3.5, F_slow1=16.6667, F_slow2=0, F_fast1=0, F_fast2=200,  N_fast=1, SLOW1_THETA=120, FO3=1, THETA_fast1=180, FAST_THETA=5, Gamma=0, Etun=1,  V_HOLE=0, FRAC_QUASIEL=0, FRAC_TUNNEL=0, TT=50, RES_DE=0.5, cold=0.95)

DECLARE
%{
        double FREQ;
        double t_FO1, t_FO2, t_fast1, t_fast2, t_fast2a, t_fast3, t_sample, t_detector;
        double tmin_zoom, tmax_zoom, t_offset;
        double E_target;
%}

INITIALIZE
%{
        FREQ = ESS_SOURCE_FREQUENCY;
        t_offset=Pulse_width/2.0+170e-6;
        t_FO1 = FO1_DIST*lambda0/(2*PI*K2V)+t_offset;
        t_FO2 = Length/2*lambda0/(2*PI*K2V)+t_offset;
        t_fast1 = (Length/2+0.1)*lambda0/(2*PI*K2V)+t_offset;
        t_fast2 = Length*lambda0/(2*PI*K2V)+t_offset;
        t_fast2a = (Length+GUI_GAP)*lambda0/(2*PI*K2V)+t_offset;
        t_fast3 = (Length+2*GUI_GAP)*lambda0/(2*PI*K2V)+t_offset;
        t_sample = (Length+SAMPLE_DIST)*lambda0/(2*PI*K2V)+t_offset;
        t_detector = (Length+SAMPLE_DIST+DETECTOR_DIST)*lambda0/(2*PI*K2V)+t_offset;
        tmin_zoom = t_fast2*1e6-1e3;
        tmax_zoom = t_fast2*1e6+1e3;
        E_target = VS2E*(K2V*2*PI/lambda0)*(K2V*2*PI/lambda0);
%}

TRACE


/* Displaced wrt Origin - as it has a volume... */
COMPONENT source = ESS_butterfly(sector="W",beamline=5,Lmin=Lmin,Lmax=Lmax,dist=GUI_start,target_index=6,cold_frac=cold,focus_xw = GUI_w, focus_yh = GUI_h, n_pulses=Num_pulses)
  AT (0, 0, -0.1) ABSOLUTE


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

COMPONENT TOFmoderator_zoom = TOF_monitor(
    nt = 1000, filename = "TOFmoderator_zoom.dat", xwidth = 0.12,
    yheight = 0.12, tmin = 0, tmax = 5000, restore_neutron=1)
    AT (0, 0, 1E-6) RELATIVE Origin

COMPONENT TOFmoderator = TOF_monitor(
    nt = 1000, filename = "TOFmoderator.dat", xwidth = 0.12,
    yheight = 0.12, tmin = 0, tmax = 3.0e5, restore_neutron=1)
  AT (0, 0, 1E-6) RELATIVE Origin

COMPONENT Lmon_guistart = L_monitor(
    nL = 1000, filename = "Lmon_guistart.dat", xwidth = GUI_w+0.01,
    yheight = GUI_h+0.01, Lmin = 0, Lmax = Lmax+1)
  AT (0, 0, GUI_start-2e-6) RELATIVE Origin

COMPONENT Lmon_normalize = L_monitor(
    nL = 2880, filename = "Lmon_guistart_normalize.dat", xwidth = 0.10,
    yheight = 0.10, Lmin = 0, Lmax = 20)
  AT (0, 0, GUI_start-1e-6) RELATIVE Origin



COMPONENT Guide1 = Guide(
    w1 = GUI_w, h1 = GUI_h, w2 = W1, h2 = H1, l = FO1_DIST-GUI_start-GUI_GAP/2, R0 = 1,
    alpha = ALPHA, m = M, W = 0.003)
  AT (0, 0, GUI_start) RELATIVE Origin

COMPONENT Lmonslow1 = L_monitor(
    nL = 200, filename = "Lmonslow1.dat", xwidth = 0.06,
    yheight = 0.21, Lmin = 0, Lmax = Lmax+1)
  AT (0, 0, FO1_DIST-2e-6) RELATIVE Origin

COMPONENT PSDslow1 = PSD_monitor(
    filename = "PSDslow1.dat", xwidth = 0.1, yheight = 0.25)
  AT (0, 0, FO1_DIST-1e-6) RELATIVE Origin

COMPONENT FOchop1 = DiskChopper(
    yheight=0.20, theta_0 = SLOW1_THETA, radius = 0.6, nu = F_slow1, nslit = 1, delay = t_FO1)
  AT (0, 0, FO1_DIST) RELATIVE Origin

COMPONENT TOFLmon1 = TOFLambda_monitor(
    nL = 200, nt = 200, filename = "TOFLmon1.dat", tmin = 0,
    tmax = 3e5, xwidth = 0.05, yheight = 0.21, Lmin = 0,
    Lmax = Lmax+1)
  AT (0, 0, 1e-6) RELATIVE FOchop1

COMPONENT Lmon_afterslow1 = L_monitor(
    nL = 200, filename = "Lmon_afterslow1.dat", xwidth = 0.06,
    yheight = 0.21, Lmin = 0, Lmax = Lmax+1)
  AT (0, 0, 2e-6) RELATIVE FOchop1

COMPONENT PSD_afterslow1 = PSD_monitor(
    filename = "PSD_afterslow1.dat", xwidth = 0.1, yheight = 0.25)
  AT (0, 0, 3e-6) RELATIVE FOchop1

COMPONENT Guidelong1 = Guide(
    w1 = W1, h1 = H1, w2 = W2, h2 = H2,
    l = L_ballistic_begin+GUI_start-FO1_DIST-GUI_GAP/2,
    R0 = 1, alpha = ALPHA, m = M, W = 0.003)
  AT (0, 0, GUI_GAP/2) RELATIVE FOchop1

COMPONENT Guidelong1b = Guide(
    w1 = W2, h1 = H2, w2 = W3, h2 = H3, l = Length/2-L_ballistic_begin-GUI_start-GUI_GAP/2,
    R0 = 1, alpha = ALPHA, m = M, W = 0.003)
  AT (0, 0, L_ballistic_begin+GUI_start) RELATIVE Origin

COMPONENT Lmon_slow2 = L_monitor(
    nL = 200, filename = "Lmon_slow2.dat", xwidth = 0.06,
    yheight = 0.21, Lmin = 0, Lmax = Lmax+1)
  AT (0, 0, Length/2-1e-6) RELATIVE Origin

COMPONENT FOchop2 = DiskChopper(
     yheight=0.6, theta_0 = 155, radius=0.6, nu = F_slow2, nslit = 1, delay = t_FO2)
  AT (0, 0.1, Length/2) RELATIVE Origin

COMPONENT Fastchop1 = DiskChopper(
    yheight=0.5, theta_0 = THETA_fast1, radius = 0.5, nu = F_fast1, nslit = N_fast, delay = t_fast1)
  AT (0, 0.1, Length/2+GUI_GAP) RELATIVE Origin

COMPONENT PSD_afterslow2 = PSD_monitor(
    filename = "PSD_afterslow2.dat", xwidth = 0.1, yheight = 0.25)
  AT (0, -0.1, 1e-6) RELATIVE Fastchop1

COMPONENT Lmon_afterslow2 = L_monitor(
    nL = 200, filename = "Lmon_afterslow2.dat", xwidth = 0.06,
    yheight = 0.21, Lmin = 0, Lmax = Lmax+1)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT TOFL_afterslow2 = TOFLambda_monitor(
    nL = 200, nt = 200, filename = "TOFL_afterslow2.dat", tmin = 0,
    tmax = 3.0e5, xwidth = 0.05, yheight = 0.21, Lmin = 0,
    Lmax = Lmax+1)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT Guidelong2 = Guide(
    w1 = W3, h1 = H3, w2 = W4, h2 = H4, l = Length/2-2*GUI_GAP-L_ballistic_end,
    R0 = 1, alpha = ALPHA, m = M, W = 0.003)
  AT (0, -0.1, GUI_GAP/2) RELATIVE Fastchop1

COMPONENT Lmon_beforeballistic = L_monitor(
    nL = 200, filename = "Lmon_before_ballistic.dat", xwidth = 0.06,
    yheight = 0.18, Lmin = 0, Lmax = Lmax+1)
  AT (0, 0, Length/2-2*GUI_GAP-L_ballistic_end+1e-6) RELATIVE Guidelong2

COMPONENT PSD_beforeballistic = PSD_monitor(
    filename = "PSD_beforeballistic.dat", xwidth = 0.1, yheight = 0.25)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT Guidelong2a = Guide(
    w1 = W4, h1 = H4, w2 = W_chop, h2 = H_chop, l = L_ballistic_end,
    R0 = 1, alpha = ALPHA, m = M, W = 0.003)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT Lmonfast2 = L_monitor(
    nL = 200, filename = "Lmonfast2.dat", xwidth = 0.06,
    yheight = 0.18, Lmin = 0, Lmax = Lmax+1)
  AT (0, 0, L_ballistic_end+1e-6) RELATIVE PREVIOUS

COMPONENT Lmonfast2_zoom = L_monitor(
    nL = 200, filename = "Lmonfast2_zoom.dat", xwidth = 0.06,
    yheight = 0.18, Lmin = lambda0-0.2, Lmax = lambda0+0.2)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT TOFLfast2 = TOFLambda_monitor(
    nL = 200, nt = 200, filename = "TOFLfast2.dat", tmin = 0,
    tmax = 3.0e5, xwidth = 0.05, yheight = 0.12, Lmin = 0,
    Lmax = Lmax+1)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT TOFLfast2zoom = TOFLambda_monitor(
    nL = 200, nt = 200, filename = "TOFLfast2_zoom.dat", tmin = tmin_zoom,
    tmax = tmax_zoom, xwidth = 0.05, yheight = 0.12, Lmin = lambda0-0.2,
    Lmax = lambda0+0.2)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT PSDfast2 = PSD_monitor(
    filename = "PSDfast2.dat", xwidth = 0.1, yheight = 0.25)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT Fastchop2 = DiskChopper(
    yheight=0.35, theta_0 = FAST_THETA, radius = 0.35, nu = F_fast2, nslit = N_fast, delay = t_fast2)
  AT (0, 0.04, Length) RELATIVE Origin

COMPONENT Fastchop2counter = DiskChopper(
    yheight=0.35, theta_0 = FAST_THETA, radius = 0.35, nu = -F_fast2, nslit = N_fast, delay = t_fast2a)
  AT (0, 0.04, Length+GUI_GAP) RELATIVE Origin

COMPONENT FOchop3 = DiskChopper(
    yheight=0.35, theta_0 = 2*FAST_THETA, radius = 0.35, nu = F_fast2/FO3, nslit = N_fast, delay = t_fast3)
  AT (0, 0.04, Length+2*GUI_GAP) RELATIVE Origin

COMPONENT TOFfast2_zoom = TOF_monitor(
    nt = 100, filename = "TOF_fast2.dat", xwidth = 1.1,
    yheight = 1.2, tmin = 1e6*t_fast3-2e2, tmax = 1e6*t_fast3+2e2)
  AT (0, -0.04, 1e-6) RELATIVE PREVIOUS

COMPONENT Lmon_afterfast2 = L_monitor(
    nL = 500, filename = "Lmon_afterfast2.dat", xwidth = 0.06,
    yheight = 0.18, Lmin = 0, Lmax = Lmax+1)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT TOFL_afterfast2 = TOFLambda_monitor(
    nL = 200, nt = 200, filename = "TOF_afterfast2.dat", tmin = 0,
    tmax = 3.0e5, xwidth = 0.05, yheight = 0.12, Lmin = 0,
    Lmax = Lmax+1)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT TOFL_afterfast2_zoom = TOFLambda_monitor(
    nL = 200, nt = 200, filename = "TOFL_afterfast2_zoom.dat", tmin = tmin_zoom,
    tmax = tmax_zoom, xwidth = 0.05, yheight = 0.12, Lmin = lambda0-0.2,
    Lmax = lambda0+0.2)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT PSD_afterfast2 = PSD_monitor(
    filename = "PSD_afterfast2.dat", xwidth = 0.1, yheight = 0.25)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT Guidesample = Guide(
    w1 = W_chop, h1 = H_chop, w2 = W_end, h2 = H_end, l = SAMPLE_DIST-4*GUI_GAP,
    R0 = 1, alpha = ALPHA, m = M, W = 0.003)
  AT (0, 0, GUI_GAP/2) RELATIVE PREVIOUS

COMPONENT Lmon_guideend = L_monitor(
    nL = 1000, filename = "Lmon_guideend.dat", xwidth = W_end+0.01,
    yheight = H_end+0.01, Lmin = 0, Lmax = Lmax+1)
  AT (0, 0, SAMPLE_DIST-4*GUI_GAP+1e-6) RELATIVE PREVIOUS

COMPONENT PSDsample = PSD_monitor(
    filename = "PSDsample.dat", xwidth = 0.1, yheight = 0.25)
  AT (0, 0, SAMPLE_DIST-3*GUI_GAP+1e-6) RELATIVE PREVIOUS

COMPONENT TOFsample_zoom = TOF_monitor(
    nt = 500, filename = "TOF_sample.dat", xwidth = 0.02,
    yheight = 0.04, tmin = 1e6*t_sample-5e4, tmax = 1e6*t_sample+5e4)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT Esample = E_monitor(
    nE = 400, filename = "Esample", xwidth = 0.02,
    yheight = 0.04, Emin = E_target-RES_DE, Emax = E_target+RES_DE)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT Lmon_sample_zoom = L_monitor(
    nL = 100, filename = "LMON_sample_zoom.dat", xwidth = 0.02,
    yheight = 0.04, Lmin = lambda0-0.1, Lmax = lambda0+0.1)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT sample = Tunneling_sample(
    thickness = 0.01-V_HOLE, radius = 0.01, yheight = 0.04, p_interact = 1,
    f_QE = FRAC_QUASIEL, f_tun=FRAC_TUNNEL, gamma = Gamma, E_tun = Etun,
    focus_xw = 0.015, focus_yh = 0.015, target_index = 2)
  AT (0, -0.04, SAMPLE_DIST) RELATIVE Fastchop2

COMPONENT detectorarm = Arm(
    )
  AT (0, 0, 0) RELATIVE sample
  ROTATED (0,TT,0) RELATIVE sample

COMPONENT TOFdetector = TOF_monitor(
    nt = 512, filename = "TOF.dat", xwidth = 0.015,
    yheight = 0.015, tmin = 0, tmax = 2e5)
  AT (0, 0, DETECTOR_DIST) RELATIVE detectorarm

COMPONENT TOFdetector_zoom = TOF_monitor(
    nt = 100, filename = "TOF_zoom.dat", xwidth = 0.015,
    yheight = 0.015, tmin = 1e6*t_detector-10e2, tmax = 1e6*t_detector+10e2)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT Edetector = E_monitor(
    nE = 400, filename = "Edet", xwidth = 0.015,
    yheight = 0.015, Emin = E_target-RES_DE, Emax = E_target+RES_DE)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT TOF2Edetector = TOF2E_monitor(
    nE = 200, filename = "TOF2E.dat", xwidth = 0.015,
    yheight = 0.015, Emin=E_target-RES_DE, Emax=E_target+RES_DE, T_zero = t_sample, L_flight = DETECTOR_DIST)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

END
