/************************************************
*
* McStas instrument for simulating IN5-TYPE (cold chopper) multi-frame spectrometer at ESS LPTS
*
* %Identification
* Written by: Kim Lefmann (kim.lefmann@risoe.dk), Helmuth Schober, Feri Mezei
* Date: September 20, 2006
* Origin:ESS
* Release: McStas 1.12
* Version: $Revision: 1.1 $
* %INSTRUMENT_SITE: ESS
*
* %Description
* McStas instrument for simulating IN5-TYPE (cold chopper) multi-frame spectrometer at ESS LPTS
*
* %Parameters
*	Lam_min:                [AA]    Minimum wavelength emitted by the moderator
*       Lam_max:	        [AA] 	Maximum wavelength emitted by the moderator
*	Lam_target:		[AA]  	Target wavelength for the monochromating crystal
*	Pulse_width:		[s]	Length of the long pulse. Default to 2 ms.
*	Twopulses:		[1]	Possibility of emitting neutrons from a second pulse. Default 0.
*
*	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-H4:                  [m]     Height of guide elements
*       W1-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]	2T (or a4) scattering angle
*	RES_DE:			[meV]	Adjustable zoom for energy monitor at detector position
*
* %Link
* The <a href="http://www.ill.fr/Computing/ESS-LP/">ESS-Rencurel</a> website.
*
* %End
***************************************************/

DEFINE INSTRUMENT ESS_IN5_reprate( Lam_min=4.9, Lam_max=5.1, Lam_target=5, Pulse_width=0.002, Twopulses=0, GUI_start=1.5, 
			       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)
     

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

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

TRACE

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

COMPONENT source = ESS_moderator_long(
    size = 0.12, l_low = Lam_min, l_high = Lam_max, dist = GUI_start, xw = GUI_w,
    yh = GUI_h, freq = FREQ, T = 50, tau = 287e-6, tau1 = 0,
    tau2 = 20e-6, d = Pulse_width, n = 20, n2 = 5, chi2 = 0.9,
    I0 = 6.9e11, I2 = 27.6e10, branch1 = 1, branch2 = 0.5,
    branch_tail = 0.1, twopulses = Twopulses)
  AT (0, 0, 0) RELATIVE Origin

COMPONENT TOFmoderator_zoom = TOF_monitor(
    nchan = 1000, filename = "TOFmoderator_zoom.dat", xwidth = 0.12,
    yheight = 0.12, t0 = 0, t1 = 5000)
  AT (0, 0, 1E-6) RELATIVE source

COMPONENT TOFmoderator = TOF_monitor(
    nchan = 1000, filename = "TOFmoderator.dat", xwidth = 0.12,
    yheight = 0.12, t0 = 0, t1 = 3.0e5)
  AT (0, 0, 1E-6) RELATIVE source

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

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

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 source

COMPONENT Lmonslow1 = L_monitor(
    nchan = 200, filename = "Lmonslow1.dat", xwidth = 0.06,
    yheight = 0.21, Lmin = 0, Lmax = Lam_max+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(
    theta_0 = SLOW1_THETA, h=0.20, R = 0.6, omega = F_slow1*2*PI, n = 1, t_0 = t_FO1)
  AT (0, 0, FO1_DIST) RELATIVE source

COMPONENT TOFLmon1 = TOFLambda_monitor(
    nlam = 200, nt = 200, filename = "TOFLmon1.dat", t_0 = 0,
    t_1 = 3e5, xwidth = 0.05, yheight = 0.21, lambda_0 = 0,
    lambda_1 = Lam_max+1)
  AT (0, 0, 1e-6) RELATIVE FOchop1

COMPONENT Lmon_afterslow1 = L_monitor(
    nchan = 200, filename = "Lmon_afterslow1.dat", xwidth = 0.06,
    yheight = 0.21, Lmin = 0, Lmax = Lam_max+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 source

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

COMPONENT FOchop2 = DiskChopper(
     theta_0 = 155, R=0.6, omega = 2*PI*F_slow2, n = 1, t_0 = t_FO2)
  AT (0, 0.1, Length/2) RELATIVE source

COMPONENT Fastchop1 = DiskChopper(
    theta_0 = THETA_fast1, R = 0.5, omega = 2*PI*F_fast1, n = N_fast, t_0 = t_fast1)
  AT (0, 0.1, Length/2+GUI_GAP) RELATIVE source

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(
    nchan = 200, filename = "Lmon_afterslow2.dat", xwidth = 0.06,
    yheight = 0.21, Lmin = 0, Lmax = Lam_max+1)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT TOFL_afterslow2 = TOFLambda_monitor(
    nlam = 200, nt = 200, filename = "TOFL_afterslow2.dat", t_0 = 0,
    t_1 = 3.0e5, xwidth = 0.05, yheight = 0.21, lambda_0 = 0,
    lambda_1 = Lam_max+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(
    nchan = 200, filename = "Lmon_before_ballistic.dat", xwidth = 0.06,
    yheight = 0.18, Lmin = 0, Lmax = Lam_max+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(
    nchan = 200, filename = "Lmonfast2.dat", xwidth = 0.06,
    yheight = 0.18, Lmin = 0, Lmax = Lam_max+1)
  AT (0, 0, L_ballistic_end+1e-6) RELATIVE PREVIOUS

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

COMPONENT TOFLfast2 = TOFLambda_monitor(
    nlam = 200, nt = 200, filename = "TOFLfast2.dat", t_0 = 0,
    t_1 = 3.0e5, xwidth = 0.05, yheight = 0.12, lambda_0 = 0,
    lambda_1 = Lam_max+1)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT TOFLfast2zoom = TOFLambda_monitor(
    nlam = 200, nt = 200, filename = "TOFLfast2_zoom.dat", t_0 = t0_zoom,
    t_1 = t1_zoom, xwidth = 0.05, yheight = 0.12, lambda_0 = Lam_target-0.2,
    lambda_1 = Lam_target+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(
    theta_0 = FAST_THETA, R = 0.35, omega = 2*PI*F_fast2, n = N_fast, t_0 = t_fast2)
  AT (0, 0.04, Length) RELATIVE source

COMPONENT Fastchop2counter = DiskChopper(
    theta_0 = FAST_THETA, R = 0.35, omega = -2*PI*F_fast2, n = N_fast, t_0 = -t_fast2a)
  AT (0, 0.04, Length+GUI_GAP) RELATIVE source

COMPONENT FOchop3 = DiskChopper(
    theta_0 = 2*FAST_THETA, R = 0.35, omega = 2*PI*F_fast2/FO3, n = N_fast, t_0 = t_fast3)
  AT (0, 0.04, Length+2*GUI_GAP) RELATIVE source

COMPONENT TOFfast2_zoom = TOF_monitor(
    nchan = 100, filename = "TOF_fast2.dat", xwidth = 0.1,
    yheight = 0.2, t0 = 1e6*t_fast3-2e2, t1 = 1e6*t_fast3+2e2)
  AT (0, -0.04, 1e-6) RELATIVE PREVIOUS

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

COMPONENT TOFL_afterfast2 = TOFLambda_monitor(
    nlam = 200, nt = 200, filename = "TOF_afterfast2.dat", t_0 = 0,
    t_1 = 3.0e5, xwidth = 0.05, yheight = 0.12, lambda_0 = 0,
    lambda_1 = Lam_max+1)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT TOFL_afterfast2_zoom = TOFLambda_monitor(
    nlam = 200, nt = 200, filename = "TOFL_afterfast2_zoom.dat", t_0 = t0_zoom,
    t_1 = t1_zoom, xwidth = 0.05, yheight = 0.12, lambda_0 = Lam_target-0.2,
    lambda_1 = Lam_target+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(
    nchan = 1000, filename = "Lmon_guideend.dat", xwidth = W_end+0.01,
    yheight = H_end+0.01, Lmin = 0, Lmax = Lam_max+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(
    nchan = 500, filename = "TOF_sample.dat", xwidth = 0.02,
    yheight = 0.04, t0 = 1e6*t_sample-5e4, t1 = 1e6*t_sample+5e4)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT Esample = E_monitor(
    nchan = 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(
    nchan = 100, filename = "LMON_sample_zoom.dat", xwidth = 0.02,
    yheight = 0.04, Lmin = Lam_target-0.1, Lmax = Lam_target+0.1)
  AT (0, 0, 1e-6) RELATIVE PREVIOUS

COMPONENT sample = Tunneling_sample(
    radius_i = V_HOLE, radius_o = 0.01, h = 0.04, frac = 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(
    nchan = 512, filename = "TOF.dat", xwidth = 0.015,
    yheight = 0.015, t0 = 0, t1 = 2e5)
  AT (0, 0, DETECTOR_DIST) RELATIVE detectorarm

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

COMPONENT Edetector = E_monitor(
    nchan = 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(
    nchan = 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

FINALLY
%{
%}

END










