/*******************************************************************************
*         McStas instrument definition URL=http://www.mcstas.org
*
* Instrument: ESS_Testbeamline_HZB_V20
*
* %I
* Written by: <a href="mailto:alaa.falahat@gmail.com">Ala'a Al-Falahat</a>
* Date: March 17, 2017
* Modified: 2018 by Jochen Fenske, Robin Woracek, Peter Willendrup.
* Origin: HZB
* Release: McStas 2.5
* Version: TBL
* %INSTRUMENT_SITE: ESS
*
* McStas model of the ESS testbeamline V20 at HZB in Berlin
*
* %D
* McStas model of the ESS testbeamline V20 at HZB in Berlin. Please note that not
* all geometrical sizes, distances and parameters have been fully validated.
*
* %Example: ESS_Testbeamline_HZB_V20 lambda_min=1 frequency=14 Detector: time_lambda_FOC2_I=1.2e+08
*
* %P
* lambda_min:              [AA] Minimum wavelength emitted by the source  
* lambda_max:              [AA] Maximum wavelength emitted by the source                 
* frequency:               [Hz] Frequency of the choppers 
* L_g1:                     [m] Length of the guide number 1
* w_g1:                     [m] Width of the guide number 1
* h_g1:                     [m] Height of the guide number 1
* Offset_deg_SC1:         [deg] Phase source chopper phase disk 1
* Offset_deg_SC2:         [deg] Phase source chopper phase disk 2
* Offset_deg_BC1:         [deg] Phase bandwidth chopper phase disk 1
* Offset_deg_BC2:         [deg] Phase bandwidth chopper phase disk 2
* Offset_deg_WFM1:        [deg] Phase wavelength frame maltiplication chopper disk 1 
* Offset_deg_WFM2:        [deg] Phase wavelength frame maltiplication chopper disk 2
* Offset_deg_FOC1:        [deg] Phase frame overlap chopper disk 1
* Offset_deg_FOC2:        [deg] Phase frame overlap chopper disk 2
* Z:                        [m] Distance between to Wavelength frame multiplication chopper disks 0.1 - 0.5
* s1_x:                     [m] slit 1 width
* s1_y:	                    [m] slit 1 height
* s2_x:                     [m] slit 1 width
* s2_y:	                    [m] slit 1 height
* s3_x:                     [m] slit 1 width
* s3_y:	                    [m] slit 1 height
* sp:                       [m] sample position
* npulses:                  [1] Number of pulses to simulate
* Choppers_WFM1_in:         [1] Flag to indicate if WFM1 is active
* Choppers_WFM2_in:         [1] Flag to indicate if WFM2 is active
* Choppers_FOC1_in:         [1] Flag to indicate if FOC1 is active
* Choppers_FOC2_in:         [1] Flag to indicate if FOC2 is active
* emulate_reactor_emmision: [1] Flag to emulate all emission times from the reactor
*
* %L
* <a href="https://www.helmholtz-berlin.de/pubbin/igama_output?modus=einzel&gid=1845">V20 page</a> at the HZB website
* <a href="https://europeanspallationsource.se/workshops-facilities#v20-test-beamline">V20 info section</a> at the ESS website
* %E
*******************************************************************************/
/* instrument definition: name, parameters & default values */
DEFINE INSTRUMENT ESS_Testbeamline_HZB_V20(lambda_min=1, lambda_max=15,frequency=14,Choppers_WFM1_in=1,Choppers_WFM2_in=1,Choppers_FOC1_in=1, Choppers_FOC2_in=1,
						s1_x=0.05, s1_y=0.1, s2_x=0.02, s2_y=0.04, s3_x=0.0138, s3_y=0.033, Offset_deg_SC1=0,Offset_deg_SC2=0,Offset_deg_BC1=-25,
						Offset_deg_BC2=57,Offset_deg_WFM1=47.1,Offset_deg_WFM2=76.76,Offset_deg_FOC1=62.4,Offset_deg_FOC2=12.27,
			     Z=0.28, sp=50.6, npulses=1, emulate_reactor_emmision=0)

/* declare variables and functions in C syntax */
DECLARE
%{


 double L_g1,L_g2,L_g3,L_g4,L_g5,L_g6,L_g7,L_g8,L_g9,L_g10,L_g11,L_g12;
 double w_g1,w_g2,w_g3,w_g4,w_g5,w_g6,w_g7,w_g8,w_g9,w_g10,w_g11,w_g12;
 double h_g1,h_g2, h_g3, h_g4, h_g5, h_g6, h_g7, h_g8, h_g9, h_g10, h_g11, h_g12; 

%}

/* any C code here is executed when the simulation starts */
INITIALIZE
%{

/* guide lengths  */
L_g1=1.87;
L_g2=1.53;
L_g3=5.50;
L_g4=7.50;
L_g5=3.00;
L_g6=0.45;
L_g7=6.50;
L_g8=1.5;
L_g9=1;
L_g10=5.75;
L_g11=3.5;
L_g12=5;

/* guide widths */
w_g1=0.06;
w_g2=0.06;
w_g3=0.06;
w_g4=0.06;
w_g5=0.06;
w_g6=0.06;
w_g7=0.06;
w_g8=0.06;
w_g9=0.06;
w_g10=0.06;
w_g11=0.06;
w_g12=0.06;

/*guide heights */
h_g1=0.125;
h_g2=0.125;
h_g3=0.125;
h_g4=0.125;
h_g5=0.125;
h_g6=0.125;
h_g7=0.125;
h_g8=0.125;
h_g9=0.125;
h_g10=0.125;
h_g11=0.125;
h_g12=0.125;
%}

/* in the TRACE section, the instrument is defined as a sequence of components. */
TRACE

/* Progress_bar() is an Arm() displaying simulation progress */
COMPONENT origin = Progress_bar()
AT (0, 0, 0) ABSOLUTE


/*****************************************Source*******************************************************/


COMPONENT source = Source_Maxwell_3(size = 0.155, Lmin =  lambda_min, Lmax =  lambda_max, dist = 1.53,
focus_xw = 0.06, focus_yh = 0.125, T1 = 43.7, T2 = 137.2, I1 = 1.4e12, I2 =2.08e12 )
AT (0, 0, 0) RELATIVE origin
EXTEND %{
  if (INSTRUMENT_GETPAR(emulate_reactor_emmision)) t=rand01();
%}


/*****************************************Monitors****************************************************/

COMPONENT lmon_source = L_monitor(
    nL = 1000, filename = "L_Monitor_Source.dat",   xwidth = 0.06, yheight = 0.125, Lmin =  lambda_min, Lmax =  lambda_max)
  AT (0, 0, 1.5290) RELATIVE  origin

COMPONENT psd_source = PSD_monitor(
    nx = 150, ny = 150, filename = "PSD_Monitor_Source.dat",  xwidth = 0.15, yheight = 0.15)
  AT (0, 0, 1.5291) RELATIVE  origin

COMPONENT time_lambda_source= TOFLambda_monitor(
   nL = 1000, nt = 1000, filename = "Time_Lambda_Source.dat",
 xwidth = 0.06, yheight = 0.125,  tmin = 0, tmax =72000, Lmin = 0, Lmax = 10  )
 AT (0, 0, 1.5292) RELATIVE origin

COMPONENT tofmon_source = TOF_monitor(
 nt = 1000,  filename = "TOF_Monitor_Source.dat",  xwidth =0.06,  yheight = 0.125,
  tmin = 0,  tmax = 72000,  restore_neutron = 1)
 AT (0, 0,1.52921) RELATIVE origin 


/***************************************** Elementes*************************************************/

COMPONENT g1 = Guide(
    w1 = w_g1, h1 = h_g1, w2 = w_g1, h2 = h_g1, l = L_g1, alpha = 6.49, m = 3, W = 0.00333)
  AT (0, 0,1.53) RELATIVE origin


COMPONENT g2_channeled = Guide_channeled(
    w1 = w_g2, h1 = h_g2, w2 = w_g2, h2 = h_g2, l = L_g2, alpha = 6.49, m = 3, nslit = 6, W = 0.00333)
  AT (0, 0, 3.47) RELATIVE origin


COMPONENT g3 = Bender(
    w = w_g3, h = h_g3, r = 1500, l = L_g3, ma = 3,  alphaa = 6.49 ,Wa=0.00333)
  AT (0, 0, 5) RELATIVE origin


COMPONENT g4 = Bender(
    w = w_g4, h = h_g4, r = 2300, l = L_g4 , ma = 3,alphaa = 6.49,Wa=0.00333)
  AT (0, 0, 10.706) RELATIVE origin

COMPONENT g5 = Bender(
    w = w_g5, h = h_g5, r = 2300, l = L_g5 ,ma = 3 , alphaa = 6.49,Wa=0.00333)
  AT (0, 0, 18.206) RELATIVE origin

COMPONENT g6 = Bender(
    w = w_g6, h = h_g6, r = 2300, l = L_g6 , ma = 3 , alphaa = 6.49,Wa=0.00333)
  AT (0, 0, 21.206) RELATIVE origin



/************************************Start of  Choppers *********************************************/



/*********************************Source Choppers 1 and 2 ***************************************/


COMPONENT Source_Chopper_1 = DiskChopper(
    theta_0 = 23, radius = 0.35, yheight = 0.13, nu = -1*frequency,
    nslit = 1, isfirst = 1, n_pulse = npulses, abs_out = 1,verbose=1, phase = Offset_deg_SC1 +191.5-180)
    AT (0, 0, 21.729) RELATIVE origin
  ROTATED (0, 0, 180) RELATIVE origin


COMPONENT Source_Chopper_2 = DiskChopper(
    theta_0 = 50, radius = 0.35, yheight = 0.13, nu = 3*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase = Offset_deg_SC2+205)
    AT (0, 0.57, 21.759) RELATIVE origin

/*-------------------------------------------------End of Source Chopper -------------------------------------------------*/


COMPONENT lmon_source_chopper = L_monitor(
    nL = 1000, filename = "L_Monitor_SourceChopper.dat", 
     xwidth = 0.06, yheight = 0.125, Lmin =  lambda_min,Lmax =  lambda_max)
  AT (0, 0,21.770) RELATIVE  origin

COMPONENT psd_source_chopper = PSD_monitor(
    nx = 150, ny = 150, filename = "PSD_Monitor_SourceChopper.dat",
   xwidth = 0.15, yheight = 0.15)
  AT (0, 0, 21.771) RELATIVE  origin

COMPONENT tofmon_source_chopper = TOF_monitor(
 nt = 1000,  filename = "TOF_Monitor_SourceChopper.dat",  xwidth =0.06,  yheight = 0.125,
  tmin = 0,  tmax = 72000,  restore_neutron = 1)
 AT (0, 0,21.772) RELATIVE origin 

COMPONENT time_lambda_SC= TOFLambda_monitor(
    nL = 1000, nt = 1000, filename = "Time_Lambda_SourceChopper.dat",
 xwidth = 0.06, yheight = 0.125, tmin = 0, tmax =72000, Lmin =  lambda_min, Lmax =  lambda_max  )
 AT (0, 0, 21.773) RELATIVE origin

COMPONENT g7 = Bender(
    w = w_g7, h = h_g7, r = 2300, l = L_g7 , ma = 3, alphaa = 6.49,Wa=0.00333)
  AT (0, 0, 21.831) RELATIVE origin


/************************************Start of WFM-1 Chopper ********************************************/

COMPONENT WFMC_1_a = DiskChopper(
    theta_0 = 10.9872, radius = 0.3, yheight = 0.135, nu = -5*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1, verbose=1,phase = Offset_deg_WFM1+89.208+180+14.8428)
  WHEN (Choppers_WFM1_in) AT (0, 0.465, 28.594-Z*0.5) RELATIVE origin
  GROUP Choppers

COMPONENT WFMC_1_b = DiskChopper(
    theta_0 = 15.2964, radius = 0.3, yheight = 0.135, nu = -5*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1,phase = Offset_deg_WFM1+148.1382+180+14.8428)
  WHEN (Choppers_WFM1_in) AT (0, 0.465, 28.594-Z*0.5) RELATIVE origin
  GROUP Choppers

COMPONENT WFMC_1_c = DiskChopper(
    theta_0 = 19.3032, radius = 0.3, yheight = 0.135, nu = -5*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1, verbose=1,phase = Offset_deg_WFM1+202.9104+180+14.8428)
  WHEN (Choppers_WFM1_in) AT (0, 0.465, 28.594-Z*0.5) RELATIVE origin
  GROUP Choppers

COMPONENT WFMC_1_d = DiskChopper(
    theta_0 = 23.0076, radius = 0.3, yheight = 0.135, nu = -5*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase =  Offset_deg_WFM1+253.827+180+14.8428)
  WHEN (Choppers_WFM1_in) AT (0, 0.465,28.594-Z*0.5) RELATIVE origin
  GROUP Choppers

COMPONENT WFMC_1_e = DiskChopper(
    theta_0 = 26.46, radius = 0.3, yheight = 0.135, nu = -5*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase =  Offset_deg_WFM1+ 301.14+180+14.8428)
  WHEN (Choppers_WFM1_in) AT (0, 0.465, 28.594-Z*0.5) RELATIVE origin
  GROUP Choppers

COMPONENT WFMC_1_f = DiskChopper(
    theta_0 = 29.6856, radius = 0.3, yheight = 0.135, nu = -5*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase = Offset_deg_WFM1+345.1392+180+14.8428)
  WHEN (Choppers_WFM1_in) AT (0, 0.465, 28.594-Z*0.5) RELATIVE origin
  GROUP Choppers



COMPONENT WFMC_1_dummy = Arm()
  AT (0, 0.465, 28.594-Z*0.5) RELATIVE origin
 GROUP Choppers
EXTEND %{
  if (INSTRUMENT_GETPAR(Choppers_WFM1_in==0)) SCATTER;
%}

/*----------------------------------------End of WFM-1 Chopper -------------------------------------------------------*/

/*********************************************Monitors*************************************************/

COMPONENT lmon_WFMC_1 = L_monitor(
    nL = 1000, filename = "L_Monitor_WFMC1.dat", 
  xwidth = 0.06, yheight = 0.125, Lmin =  lambda_min,    Lmax =  lambda_max)
  AT (0, 0, 28.592) RELATIVE  origin

COMPONENT time_lambda_WFM1= TOFLambda_monitor(
   nL = 1000, nt = 1000, filename = "Time_Lambda_WFMC1.dat",
 xwidth = 0.06, yheight = 0.125, tmin = 0, tmax =72000,
 Lmin = 0, Lmax = 10  )
 AT (0, 0, 28.593) RELATIVE origin



COMPONENT slit_1 = Slit(
xwidth = s1_x, yheight = s1_y)
 AT (0, 0, 28.594) RELATIVE origin

/*******************************Start of WFM-2 Chopper ************************************************/

COMPONENT WFMC_2_a = DiskChopper(
    theta_0 = 10.9872, radius = 0.3, yheight = 0.135, nu = -5*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1, verbose=1,phase = Offset_deg_WFM2+70.5348+180+14.8428)
  WHEN (Choppers_WFM2_in) AT (0, 0.465, 28.594+Z*0.5) RELATIVE origin
  GROUP Choppers2

COMPONENT WFMC_2_b = DiskChopper(
    theta_0 = 15.2964, radius = 0.3, yheight = 0.135, nu = -5*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1, verbose=1,phase = Offset_deg_WFM2+133.749+180+14.8428)
  WHEN (Choppers_WFM2_in) AT (0, 0.465,28.594+Z*0.5) RELATIVE origin
  GROUP Choppers2

COMPONENT WFMC_2_c = DiskChopper(
    theta_0 = 19.3032, radius = 0.3, yheight = 0.135, nu = -5*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase = Offset_deg_WFM2+192.528+180+14.8428)
  WHEN (Choppers_WFM2_in) AT (0, 0.465, 28.594+Z*0.5) RELATIVE origin
  GROUP Choppers2

COMPONENT WFMC_2_d = DiskChopper(
    theta_0 = 19.3032, radius = 0.3, yheight = 0.135, nu = -5*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase = Offset_deg_WFM2+245.322+180+14.8428)
  WHEN (Choppers_WFM2_in) AT (0, 0.465, 28.594+Z*0.5) RELATIVE origin
  GROUP Choppers2

COMPONENT WFMC_2_e = DiskChopper(
    theta_0 = 23.0076, radius = 0.3, yheight = 0.135, nu = -5*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase = Offset_deg_WFM2+296.2386+180+14.8428)
  WHEN (Choppers_WFM2_in) AT (0, 0.465,28.594+Z*0.5) RELATIVE origin
  GROUP Choppers2 

COMPONENT WFMC_2_f = DiskChopper(
    theta_0 = 29.6856, radius = 0.3, yheight = 0.135, nu = -5*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase = Offset_deg_WFM2+ 345.1644+180+14.8428)
  WHEN (Choppers_WFM2_in) AT (0, 0.465, 28.594+Z*0.5) RELATIVE origin
  GROUP Choppers2

COMPONENT WFMC_2_dummy = Arm()
 AT (0, 0.465, 28.594+Z*0.5) RELATIVE origin
  GROUP Choppers2
EXTEND %{
  if (INSTRUMENT_GETPAR(Choppers_WFM2_in==0)) SCATTER;
%}



/*----------------------------------------End of WFM-2 Chopper ----------------------------------------------------*/


/**********************************Monitors**********************************************************/

COMPONENT lmon_WFMC_2 = L_monitor(
    nL = 1000, filename = "L_Monitor_WFMC2.dat",  
 xwidth = 0.06, yheight = 0.125, Lmin =  lambda_min,    Lmax =  lambda_max)
  AT (0, 0, 28.8441) RELATIVE  origin

COMPONENT psd_WFMC_2 = PSD_monitor(
    nx = 150, ny = 150, filename = "PSD_Monitor_WFMC2.dat",
     xwidth = 0.15, yheight = 0.15)
  AT (0, 0, 28.8442) RELATIVE  origin

COMPONENT time_lambda_WFM2= TOFLambda_monitor(
    nL = 1000, nt = 1000, filename = "Time_Lambda_WFMC2.dat",
 xwidth = 0.06, yheight = 0.125, tmin = 0, tmax =72000, Lmin = 0, Lmax = 10  )
 AT (0, 0, 28.8443) RELATIVE origin



COMPONENT g8 = Bender(
    w = w_g8, h = h_g8, r = 2300, l = L_g8 , ma = 3 , alphaa = 6.49 , Wa=0.00333)
  AT (0, 0, 28.894) RELATIVE origin

/***************************************Start of FOC-1 Chopper *****************************************/


COMPONENT FOC_1_a = DiskChopper(
    theta_0 = 20.64, radius = 0.3, yheight = 0.135, nu = -4*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase = Offset_deg_FOC1+74.67+180+12.235)
   WHEN (Choppers_FOC1_in) AT (0, 0.465, 30.444) RELATIVE origin
  GROUP Choppers3

COMPONENT FOC_1_b = DiskChopper(
    theta_0 = 23.24, radius = 0.3, yheight = 0.135, nu = -4*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase =  Offset_deg_FOC1+136.67+180+12.235)
   WHEN (Choppers_FOC1_in) AT (0, 0.465, 30.444) RELATIVE origin
  GROUP Choppers3

COMPONENT FOC_1_c = DiskChopper(
    theta_0 = 21.81, radius = 0.3, yheight = 0.135, nu = -4*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase =  Offset_deg_FOC1+194.315+180+12.235)
  WHEN (Choppers_FOC1_in)  AT (0, 0.465, 30.444) RELATIVE origin
  GROUP Choppers3

COMPONENT FOC_1_d = DiskChopper(
    theta_0 = 17.87, radius = 0.3, yheight = 0.135, nu = -4*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase =  Offset_deg_FOC1+245.335+180+12.235)
 WHEN (Choppers_FOC1_in)   AT (0, 0.465, 30.444) RELATIVE origin
  GROUP Choppers3

COMPONENT FOC_1_e = DiskChopper(
    theta_0 = 15.76, radius = 0.3, yheight = 0.135, nu = -4*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase = Offset_deg_FOC1+ 294.92+180+12.235)
   WHEN (Choppers_FOC1_in) AT (0, 0.465, 30.444) RELATIVE origin
  GROUP Choppers3

COMPONENT FOC_1_f = DiskChopper(
    theta_0 = 24.47, radius = 0.3, yheight = 0.135, nu = -4*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase =  Offset_deg_FOC1+347.765+180+12.235)
   WHEN (Choppers_FOC1_in) AT (0, 0.465, 30.444) RELATIVE origin
  GROUP Choppers3

COMPONENT FOC_1_dummy = Arm()
  AT (0, 0.465, 30.444) RELATIVE origin
  GROUP Choppers3
EXTEND %{
  if (INSTRUMENT_GETPAR(Choppers_FOC1_in==0)) SCATTER;
%}


/*----------------------------------------End of FOC-1 Chopper ----------------------------------------------------*/


/************************************Monitors********************************************************/


COMPONENT lmon_FOC_1 = L_monitor(
    nL = 1000, filename = "L_Monitor_FOC1.dat",   
xwidth = 0.06, yheight = 0.125, Lmin =  lambda_min, Lmax =  lambda_max)
  AT (0, 0, 30.4441) RELATIVE  origin


COMPONENT psd_FOC_1 = PSD_monitor(
    nx = 150, ny = 150, filename = "PSD_Monitor_FOC1.dat",
    xwidth = 0.15, yheight = 0.15)
 AT (0, 0, 30.4442) RELATIVE  origin

COMPONENT time_lambda_FOC1= TOFLambda_monitor(
   nL = 1000, nt = 1000, filename = "Time_Lambda_FOC1.dat",
 xwidth = 0.06, yheight = 0.125,  tmin = 0, tmax =72000,
 Lmin =  lambda_min, Lmax =  lambda_max  )
 AT (0, 0, 30.4443) RELATIVE origin


COMPONENT slit_fix_1 = Slit(
 xwidth = 0.05, yheight = 0.1)
AT (0, 0, 30.543) RELATIVE origin


COMPONENT g9 = Bender(
    w = w_g9, h = h_g9, r = 2300, l = L_g9 , ma = 3 , alphaa = 6.49 , Wa=0.00333)
  AT (0, 0, 30.543) RELATIVE origin

/************************************   Band Chopper  1 and 2 ***************************************/
COMPONENT t_before_BC = TOF_monitor(
    nt=1000, filename="t_before_BC", xwidth=0.06, yheight=0.125, tmax=72000, restore_neutron=1)
AT (0, 0, 31.650) RELATIVE origin


COMPONENT BC_1 = DiskChopper(
    theta_0 = 140, radius = 0.35, yheight = 0.13, nu = -1*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase = -Offset_deg_BC1+250)
     AT (0, 0.57, 31.653) RELATIVE origin


COMPONENT BC_2 = DiskChopper(
    theta_0 = 202, radius = 0.35, yheight = 0.13, nu = -1*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase = -Offset_deg_BC2+281)
   AT (0, 0.57, 31.683) RELATIVE origin

/*********************************************Monitors******************************************************/



COMPONENT lmon_Band_Chopper = L_monitor(
    nL = 1000, filename = "L_Monitor_BandChopper.dat",  
xwidth = 0.06, yheight = 0.125, Lmin =  lambda_min, Lmax =  lambda_max)
  AT (0, 0, 31.6831) RELATIVE  origin

COMPONENT psd_Band_Chopper = PSD_monitor(
    nx = 150, ny = 150, filename = "PSD_Monitor_BandChopper.dat",
     xwidth = 0.15, yheight = 0.15)
  AT (0, 0, 31.6832) RELATIVE  origin


COMPONENT tofmon_BC = TOF_monitor(
  nt = 1000,  filename = "TOF_Monitor_BandChopper.dat",
  xwidth =0.06,  yheight = 0.125,  tmin = 0,  tmax = 72000)
AT (0, 0, 31.6833) RELATIVE origin 


COMPONENT time_lambda_BC= TOFLambda_monitor(
    nL = 1000, nt = 1000,filename = "Time_Lambda_BandChopper.dat",
 xwidth = 0.06, yheight = 0.125, tmin = 0, tmax =72000, Lmin =  lambda_min, Lmax =  lambda_max  )
 AT (0, 0, 31.6834) RELATIVE origin

COMPONENT g10= Bender(
    w = w_g10, h = h_g10 , r = 2300, l=L_g10 , ma = 3 , alphaa = 6.49, Wa=0.00333)
  AT (0, 0, 31.793) RELATIVE origin


/***********************************Start of FOC_2 Chopper ******************************************/


COMPONENT FOC_2_a = DiskChopper(
    theta_0 = 36.6, radius = 0.3, yheight = 0.135, nu = -2*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase =  Offset_deg_FOC2+98.06+180+14.555)
  WHEN (Choppers_FOC2_in) AT (0, 0.465,37.544) RELATIVE origin
  GROUP Choppers4

COMPONENT FOC_2_b = DiskChopper(
    theta_0 = 36.06, radius = 0.3, yheight = 0.135, nu = -2*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1, verbose=1,phase =  Offset_deg_FOC2+154.44+180+14.555)
  WHEN (Choppers_FOC2_in) AT (0, 0.465, 37.544) RELATIVE origin
  GROUP Choppers4

COMPONENT FOC_2_c = DiskChopper(
    theta_0 = 30.21, radius = 0.3, yheight = 0.135, nu = -2*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase =  Offset_deg_FOC2+206.835+180+14.555)
  WHEN (Choppers_FOC2_in) AT (0, 0.465, 37.544) RELATIVE origin
  GROUP Choppers4

COMPONENT FOC_2_d = DiskChopper(
    theta_0 = 26.88, radius = 0.3, yheight = 0.135, nu = -2*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase =  Offset_deg_FOC2+254.25+180+14.555)
  WHEN (Choppers_FOC2_in) AT (0, 0.465, 37.544) RELATIVE origin
  GROUP Choppers4

COMPONENT FOC_2_e = DiskChopper(
    theta_0 = 24.56, radius = 0.3, yheight = 0.135, nu = -2*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase =  Offset_deg_FOC2+299.41+180+14.555)
  WHEN (Choppers_FOC2_in) AT (0, 0.465, 37.544) RELATIVE origin
  GROUP Choppers4

COMPONENT FOC_2_f = DiskChopper(
    theta_0 = 29.11, radius = 0.3, yheight = 0.135, nu = -2*frequency,
    nslit = 1, isfirst = 0, n_pulse = 1, abs_out = 1,verbose=1, phase =  Offset_deg_FOC2+345.445+180+14.555)
  WHEN (Choppers_FOC2_in) AT (0, 0.465, 37.544) RELATIVE origin
  GROUP Choppers4

COMPONENT FOC_2_dummy = Arm()
  AT (0, 0.465, 37.544) RELATIVE origin
  GROUP Choppers4
EXTEND %{
  if (INSTRUMENT_GETPAR(Choppers_FOC2_in==0)) SCATTER;
%}



/*----------------------------------------End of FOC Chopper  -------------------------------------------------------------*/



/********************************************End of all Choppers  *****************************************/


/***********************************************Monitors**************************************************/


COMPONENT lmon_FOC_2 = L_monitor(
    nL = 1000, filename = "L_Monitor_FOC2.dat",
  xwidth = 0.06, yheight = 0.125, Lmin =  lambda_min,  Lmax =  lambda_max)
  AT (0, 0, 37.5441) RELATIVE  origin


COMPONENT psd_FOC_2 = PSD_monitor(
    nx = 150, ny = 150, filename = "PSD_Monitor_FOC2.dat",
    xwidth = 0.15, yheight = 0.15)
  AT (0, 0, 37.5442) RELATIVE  origin

COMPONENT time_lambda_FOC2= TOFLambda_monitor(
    nL = 1000, nt = 1000, filename = "Time_Lambda_FOC2.dat",
 xwidth = 0.06, yheight = 0.125, tmin = 0, tmax =72000, Lmin =  lambda_min, Lmax =  lambda_max  )
 AT (0, 0, 37.5443) RELATIVE origin


COMPONENT slit_fix_2 = Slit(
  xwidth = 0.02, yheight = 0.1)
  AT (0, 0, 37.594) RELATIVE origin

COMPONENT g11= Bender(
    w = w_g11, h = h_g11 , r = 2300, l=L_g11 , ma = 3 , alphaa = 6.49 , Wa=0.00333)
  AT (0, 0, 37.594) RELATIVE origin

COMPONENT g12 = Guide(
    w1 = w_g12, h1 = h_g12, w2 = w_g12, h2 = h_g12, l = L_g12,
    alpha = 6.49, m = 3, W = 0.00333)
  AT (0, 0,41.094) RELATIVE origin

/*
COMPONENT Polarizing_S_bender = Pol_bender(
  xwidth = 0.06, yheight = 0.125, length = 0.50, radius = 10)
 AT (0, 0, 46.094)  RELATIVE PREVIOUS
*/
/********************end of concrete shielding  at 46.594m ***************************************/

COMPONENT slit_2 = Slit(
  xwidth = s2_x, yheight = s2_y)
  AT (0, 0, 47.3) RELATIVE origin

COMPONENT tofmon_V20 = TOF_monitor(
  nt = 1000,  filename = "TOF_Monitor_V20.dat",
  xwidth =0.06,  yheight = 0.125,  tmin = 0,  tmax = 72000)
 AT (0, 0, 47.4) RELATIVE origin  

COMPONENT lmon_exit = L_monitor(
    nL = 1000, filename = "L_Monitor_ExitGuide.dat", xwidth = 0.06,
    yheight = 0.125, Lmin =  lambda_min, Lmax =  lambda_max)
  AT (0, 0, 47.4001) RELATIVE origin


COMPONENT psd_exit= PSD_monitor(
    nx = 150, ny = 150, filename = "PSD_Monitor_ExitGuide.dat",
    xwidth = 0.15,    yheight = 0.15)
  AT (0, 0, 47.4002) RELATIVE  origin

COMPONENT tofmon_exit = TOF_monitor(
  nt = 1000,  filename = "TOF_Monitor_ExitGuide.dat",
  xwidth =0.06,  yheight = 0.125,  tmin = 10000,  tmax = 90000)
 AT (0, 0, 47.4003) RELATIVE origin  


COMPONENT time_lambda_exit= TOFLambda_monitor(
    nL = 1000, nt = 2000, filename = "Time_Lambda_ExitGuide.dat",
 xwidth = 0.06, yheight = 0.125, tmin = 10000, tmax =90000, Lmin =  lambda_min, Lmax =  lambda_max )
 AT (0, 0, 47.4004) RELATIVE origin


COMPONENT tofmon_V20_2 = TOF_monitor(
  nt = 1000,  filename = "TOF_Monitor_V20_2.dat",
  xwidth =0.06,  yheight = 0.125,  tmin = 0,  tmax = 72000)
 AT (0, 0, 50.12) RELATIVE origin

/********************************optional_slit**********************************/
COMPONENT slit_3 = Slit(
  xwidth = s3_x, yheight = s3_y)
  AT (0, 0, 50.25) RELATIVE origin

/*************************************************Samples***********************************************************/

COMPONENT arm_sample = Arm()
AT (0, 0, sp) RELATIVE origin
ROTATED (0, 0, 0) RELATIVE origin
/********************************Fe*****************************************/

SPLIT 100
COMPONENT Fealpha = PowderN(
  reflections = "Fe.laz", radius=0.006, yheight=0.08,
 Vc =  2.867*2.867*2.867, p_inc=0.1,  Strain=0,
  barns = 1, d_phi=10, p_transmit=0.1)
 AT (0, 0, 0) RELATIVE arm_sample


/****************************************PG002*****************************/
/*
COMPONENT arm_mono = Arm()
AT (0, 0, 50.6) RELATIVE origin
ROTATED (0, 45, 0) RELATIVE origin

COMPONENT monochromator_flat = Monochromator_flat(
    zwidth=0.1, 
    yheight=0.1, 
    mosaich=30, 
    mosaicv=30, 
    r0=0.8, 
    DM=3.355)
AT (0, 0, 0) RELATIVE arm_mono
*/


/**********************************detector*****************************/


COMPONENT arm_detector = Arm()
AT (0, 0, 0) RELATIVE arm_sample
ROTATED (0, 90, 0) RELATIVE origin

COMPONENT tofmon_detector_position2 = TOF_monitor(
  nt = 500,  filename = "TOF_Monitor_DetecorPosition_2.dat",
  xwidth =0.025,  yheight = 0.3,  tmin = 0,  tmax = 72000, restore_neutron =1  )
 AT (0, 0, 0.37001 ) RELATIVE arm_detector

COMPONENT x_t = Monitor_nD(
    xmin=-0.15, xmax=0.15, yheight=0.3, bins=500, 
    options="time limits=[0, 0.072], x limits=[-0.15,0.15]", 
    filename="x_t.dat")
AT (0, 0, 0.5) RELATIVE arm_detector


/* any C code here is executed when the simulation ends */
FINALLY
%{
%}

/* The END token marks the instrument definition end */
END




