/*******************************************************************************
* Instrument: ISIS_TOSCA_preupgrade_Mantid
*
* %I
* Written by: Peter Willendrup (DTU/ESS), Sanghamitra Mukhopadhyay (ISIS STFC)
* Date: September 2018
* Origin: DTU/ESS/ISIS
* %INSTRUMENT_SITE: Mantid
*
* TOSCA (pre-upgrade) at ISIS with Mantid support.
*
* %D
* The model at hand is adapted from an earlier model by M.Zanetti and represents the
* TOSCA instrument as it looked prior to the recent upgrade. The model at hand was
* adapted during the MDANSE2018 workshop (http://mdanse2018.essworkshop.org) and by
* default uses a model of the incoherent scattering from Benzene, created using 
* CASTEP (http://www.castep.org ) and converted to Sqw format iFit (http://ifit.mcode.org)
*
* The model is naturally very challenging to run because of the indirect TOF geometry
* and would benefit from an adaptive sampling approach.
*
* Please follow instructions at https://github.com/McStasMcXtrace/McCode/wiki/McStas-and-Mantid
* to compile with NeXus and generate a Mantid-IDF before attempting to use with Mantid.
*
* To generate the right flightpath length in Mantid, this version of the instrument has 
* been created with transmitting rather than reflecting analysers, and the detectors are hence
* also positioned differently than in the physical instrument.
*
* Example: ISIS_TOSCA_preupgrade_Mantid.instr inc=Benzene_inc_CASTEP_MDANSE2018.sqw eventmode=1 Detector: Sphere_I=0.686792
* Example: ISIS_TOSCA_preupgrade_Mantid.instr inc=Benzene_inc_CASTEP_MDANSE2018.sqw eventmode=1 --format=NeXus Detector: Sphere_I=0.686792 
*
* %P
* inc:       [filename] Incoherent scattering model to use
* zdepth:    [m]        Thickness of sample
* Emin:      [meV]      Minimum energy to generate at the source
* Emax:      [meV]      Maximum energy to generate at the source
* eventmode: [boolean]  Flag to enable event monitors
*
* %L
* <a href="https://www.isis.stfc.ac.uk/Pages/tosca.aspx">TOSCA instrument webpage</a>
* <a href="https://http://mdanse2018.essworkshop.org">MDANSE 2018 school webpage</a>
* %E
*******************************************************************************/
DEFINE INSTRUMENT ISIS_TOSCA_preupgrade_Mantid(string inc="Benzene_inc_CASTEP_MDANSE2018.sqw", zdepth=0.002, Emin=0.01, Emax=500, eventmode=0)
DEPENDENCY " @NEXUSFLAGS@ "

DECLARE %{ 
double v_foc, c_h1, c_h2, slit_curv, num_slits; 
double nu_chop = 0, phase_chop = 28, E_min = 1, E_max = 1000;
double lam, deg_phase, phase_time; 
double phase;
double monitor_dimx = 0.10; 
double monitor_dimy = 0.10; 
double L0=16.997;
double mneu  =1.6749544*1E-27;
double meV2J =1.6021892*1E-22;
double Emon_min = 2;
double Emon_max = 500;
double L_min = 0.1;
double L_max = 5;
double t_min = 10;
double t_max = 40000;
double L_minE,L_maxE,t_minE,t_maxE;
#pragma acc declare create(t_minE)
int myrank;
FILE *fp;
%}

USERVARS %{
  double t_prime;
  double vix;
  double viy;
  double viz;
%}

INITIALIZE
%{
L_minE= 10.0*sqrt(0.81787/(E_max*1.15));
L_maxE= 10.0*sqrt(0.81787/(E_min*0.85));
t_maxE = L0/sqrt(2/mneu*meV2J*(E_min*0.85))*1E6;
t_minE = L0/sqrt(2/mneu*meV2J*(E_max*1.15))*1E6;
printf("Found t_minE at %g \n",t_minE);
#pragma acc update device(t_minE)

%}

TRACE
// ------- ref point
COMPONENT arm1 = Progress_bar() AT (0,0,0) ABSOLUTE

// MODERATOR
COMPONENT moder=ISIS_moderator(Face="tosca", Emin = 0.01, Emax = 500.0,
          xwidth=0.12, yheight = 0.115,  focus_xw = 0.04, focus_yh = 0.04, dist = 17)  

AT (0, 0, 0.00001) RELATIVE arm1

  COMPONENT sourceMantid = Arm()
  AT (0,0,0) RELATIVE moder

COMPONENT OldShutter = Guide_channeled( 
				w1 = 0.0845, 		//Width at the guide entry 
				h1 = 0.080, 		//Height at the guide entry 
				w2 = 0.0735, 		//Width at the guide exit   0
				h2 = 0.073, 		//Height at the guide exit  0
				l = 1.940, 		//Length of guide
				mx =0, 	//m-value of material for left and right vertical mirrors in each channel. Zero means completely absorbing. 	1
				my =0    	//m-value of material for top and bottom mirrors. Zero means completely absorbing. 				1
) 
AT (0, 0,1.626) RELATIVE arm1

COMPONENT Slit=Slit(xwidth = 0.043, yheight = 0.043)
AT (0, 0, 15.761) RELATIVE arm1

COMPONENT Bmon = TOF_monitor( tmin = 0, tmax = 26000, nt = 4900,
yheight = 0.04, xwidth =0.04 , restore_neutron = 1, filename="Bmon.tof")
AT (0, 0, 15.791) RELATIVE arm1

COMPONENT Emon = E_monitor(Emin = Emin, Emax = Emax, nE = 100,
yheight = 0.04, xwidth =0.04, filename="Emon")
AT (0, 0, 15.792) RELATIVE arm1


COMPONENT TOF_sample0=Monitor_nD(options="t limits=[0 1] bins=1000,previous")
  AT(0,0,0) RELATIVE PREVIOUS
EXTEND %{
  t_prime = t-t_minE/1e6;
%}


COMPONENT TOF_sample=Monitor_nD(user1="t_prime", options="user1 limits=[0 0.026] bins=1000,previous")
  AT(0,0,0) RELATIVE PREVIOUS
EXTEND %{
  vix=vx,viy=vy,viz=vz;
%}

COMPONENT arm_sample = Arm() AT (0, 0, 17.0) RELATIVE arm1 ROTATED (0, 180, 0) ABSOLUTE
COMPONENT arm_bank1 = Arm() AT (0,0,0) RELATIVE arm_sample ROTATED (0, 0*180, -180+0*60) RELATIVE arm1
COMPONENT arm_bank2 = Arm() AT (0,0,0) RELATIVE arm_sample ROTATED (0, 0*180, -180+1*60) RELATIVE arm1
COMPONENT arm_bank3 = Arm() AT (0,0,0) RELATIVE arm_sample ROTATED (0, 0*180, -180+2*60) RELATIVE arm1
COMPONENT arm_bank4 = Arm() AT (0,0,0) RELATIVE arm_sample ROTATED (0, 0*180, -180+3*60) RELATIVE arm1
COMPONENT arm_bank5 = Arm() AT (0,0,0) RELATIVE arm_sample ROTATED (0, 0*180, -180+4*60) RELATIVE arm1
							//rotate Z by n*60 deg for different bank postions; rotate Y 180 deg for forward scattering
COMPONENT arm_bank6 = Arm() AT (0,0,0) RELATIVE arm_sample ROTATED (0, 180, -180+0*60) RELATIVE arm1
COMPONENT arm_bank7 = Arm() AT (0,0,0) RELATIVE arm_sample ROTATED (0, 180, -180+2*60) RELATIVE arm1
COMPONENT arm_bank8 = Arm() AT (0,0,0) RELATIVE arm_sample ROTATED (0, 180, -180+3*60) RELATIVE arm1
COMPONENT arm_bank9 = Arm() AT (0,0,0) RELATIVE arm_sample ROTATED (0, 180, -180+4*60) RELATIVE arm1
COMPONENT arm_bank10 = Arm() AT (0,0,0) RELATIVE arm_sample ROTATED (0, 180, -180+5*60) RELATIVE arm1

// SAMPLE

SPLIT 1000 COMPONENT powder=Isotropic_Sqw(
  thickness=0, xwidth=0.04, yheight = 0.04, zdepth=zdepth,T=-1,
  Sqw_coh="NULL", Sqw_inc=inc, p_interact=0.9, d_phi=105
) AT (0, 0, 0) RELATIVE arm_sample
EXTEND
%{
  if (!SCATTERED) ABSORB;
%}

COMPONENT sampleMantid = Arm()
AT (0,0,0) RELATIVE powder


COMPONENT sqw = Sqw_monitor(nq=316,qmin=0, qmax=18, nE=501, Emin=-100, Emax=100, vix="vix",viy="viy",viz="viz",filename="sqw")
AT (0,0,0) RELATIVE powder

SPLIT 100 COMPONENT Repeat = Arm()
AT (0,0,0) RELATIVE PREVIOUS

//BACKWARD BANKS
COMPONENT monocr_b1 = Monochromator_curved( order=1, mosaich = 150,
mosaicv = 150, width = 0.10, height = 0.12, DM = 3.354, reflect="HOPG.rfl",
NH = 15, NV = 10, RH = 0, gap = 0.001)
AT (0, 0.23, -0.2625) RELATIVE arm_bank1 ROTATED (0, 90, 0) RELATIVE arm_bank1
GROUP Monos
EXTEND %{
    // For calculation of the correct flightpath, these analysers
    // transmit instead of reflect, and the detectors are placed differently than
    // in the physical TOSCA instrument
    if (SCATTERED) vx=-vx;
%} 
 
COMPONENT monocr_b2 = Monochromator_curved( order=1, mosaich = 150,
mosaicv = 150, width = 0.10, height = 0.12, DM = 3.354, reflect="HOPG.rfl",
NH = 15, NV = 10, RH = 0, gap = 0.001)
AT (0, 0.23, -0.2625) RELATIVE arm_bank2 ROTATED (0, 90, 0) RELATIVE arm_bank2
GROUP Monos
EXTEND %{
    if (SCATTERED) vx=-vx;
%} 

COMPONENT monocr_b3 = Monochromator_curved( order=1, mosaich = 150,
mosaicv = 150, width = 0.10, height = 0.12, DM = 3.354, reflect="HOPG.rfl",
NH = 15, NV = 10, RH = 0, gap = 0.001)
AT (0, 0.23, -0.2625) RELATIVE arm_bank3 ROTATED (0, 90, 0) RELATIVE arm_bank3
GROUP Monos
EXTEND %{
    if (SCATTERED) vx=-vx;
%} 

COMPONENT monocr_b4 = Monochromator_curved( order=1, mosaich = 150,
mosaicv = 150, width = 0.10, height = 0.12, DM = 3.354, reflect="HOPG.rfl",
NH = 15, NV = 10, RH = 0, gap = 0.001)
AT (0, 0.23, -0.2625) RELATIVE arm_bank4 ROTATED (0, 90, 0) RELATIVE arm_bank4
GROUP Monos
EXTEND %{
    if (SCATTERED) vx=-vx;
%} 

COMPONENT monocr_b5 = Monochromator_curved( order=1, mosaich = 150,
mosaicv = 150, width = 0.10, height = 0.12, DM = 3.354, reflect="HOPG.rfl",
NH = 15, NV = 10, RH = 0, gap = 0.001)
AT (0, 0.23, -0.2625) RELATIVE arm_bank5 ROTATED (0, 90, 0) RELATIVE arm_bank5
GROUP Monos
EXTEND %{
    if (SCATTERED) vx=-vx;
%} 


COMPONENT monocr_f6 = Monochromator_curved( order=1, mosaich = 150,
mosaicv = 150, width = 0.10, height = 0.12, DM = 3.354, reflect="HOPG.rfl",
NH = 15, NV = 10, RH = 0, gap = 0.001)
AT (0, 0.23, -0.2625) RELATIVE arm_bank6 ROTATED (0, 90, 0) RELATIVE arm_bank6
GROUP Monos
EXTEND %{
    if (SCATTERED) vx=-vx;
%} 

COMPONENT monocr_f7 = Monochromator_curved( order=1, mosaich = 150,
mosaicv = 150, width = 0.10, height = 0.12, DM = 3.354, reflect="HOPG.rfl",
NH = 15, NV = 10, RH = 0, gap = 0.001)
AT (0, 0.23, -0.2625) RELATIVE arm_bank7 ROTATED (0, 90, 0) RELATIVE arm_bank7
GROUP Monos
EXTEND %{
    if (SCATTERED) vx=-vx;
%} 

COMPONENT monocr_f8 = Monochromator_curved( order=1, mosaich = 150,
mosaicv = 150, width = 0.10, height = 0.12, DM = 3.354, reflect="HOPG.rfl",
NH = 15, NV = 10, RH = 0, gap = 0.001)
AT (0, 0.23, -0.2625) RELATIVE arm_bank8 ROTATED (0, 90, 0) RELATIVE arm_bank8
GROUP Monos
EXTEND %{
    if (SCATTERED) vx=-vx;
%} 

COMPONENT monocr_f9 = Monochromator_curved( order=1, mosaich = 150,
mosaicv = 150, width = 0.10, height = 0.12, DM = 3.354, reflect="HOPG.rfl",
NH = 15, NV = 10, RH = 0, gap = 0.001)
AT (0, 0.23, -0.2625) RELATIVE arm_bank9 ROTATED (0, 90, 0) RELATIVE arm_bank9
GROUP Monos
EXTEND %{
    if (SCATTERED) vx=-vx;
%} 

COMPONENT monocr_f10 = Monochromator_curved( order=1, mosaich = 150,
mosaicv = 150, width = 0.10, height = 0.12, DM = 3.354, reflect="HOPG.rfl",
NH = 15, NV = 10, RH = 0, gap = 0.001)
AT (0, 0.23, -0.2625) RELATIVE arm_bank10 ROTATED (0, 90, 0) RELATIVE arm_bank10
GROUP Monos
EXTEND %{
    if (SCATTERED) vx=-vx;
%} 

COMPONENT sqw2 = Sqw_monitor(nq=316,qmin=0, qmax=18, nE=501, Emin=-100, Emax=100, vix="vix",viy="viy",viz="viz",filename="swq2")
  AT (0,0,0) RELATIVE powder

COMPONENT Eabs = Monitor_nD(radius=2, options="energy limits=[-100 100] bins=501, q limits=[-18 18] bins=361, parallel")
  AT (0,0,0) RELATIVE powder


COMPONENT Sphere = PSD_monitor_4PI(filename="outgoing", radius=2.1, restore_neutron=1)
  AT (0,0,0) RELATIVE powder

COMPONENT Histo1 = Monitor_nD(xwidth=0.2, yheight=0.1762, user1="t_prime", options="y limits=[-0.0881 0.0881] user1 limits=[0 0.026] bins=1000")
 AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank1  GROUP detectors2

COMPONENT Histo2 = Monitor_nD(xwidth=0.2, yheight=0.1762, user1="t_prime", options="y limits=[-0.0881 0.0881] user1 limits=[0 0.026] bins=1000")
 AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank2  GROUP detectors2

COMPONENT Histo3 = Monitor_nD(xwidth=0.2, yheight=0.1762, user1="t_prime", options="y limits=[-0.0881 0.0881] user1 limits=[0 0.026] bins=1000")
  AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank3  GROUP detectors2

COMPONENT Histo4 = Monitor_nD(xwidth=0.2, yheight=0.1762, user1="t_prime", options="y limits=[-0.0881 0.0881] user1 limits=[0 0.026] bins=1000")
  AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank4  GROUP detectors2

COMPONENT Histo5 = Monitor_nD(xwidth=0.2, yheight=0.1762, user1="t_prime", options="y limits=[-0.0881 0.0881] user1 limits=[0 0.026] bins=1000")
  AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank5  GROUP detectors2

COMPONENT Histo6 = Monitor_nD(xwidth=0.2, yheight=0.1762, user1="t_prime", options="y limits=[-0.0881 0.0881] user1 limits=[0 0.026] bins=1000")
  WHEN (eventmode==0) AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank6  GROUP detectors2

COMPONENT Histo7 = Monitor_nD(xwidth=0.2, yheight=0.1762, user1="t_prime", options="y limits=[-0.0881 0.0881] user1 limits=[0 0.026] bins=1000")
  AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank7  GROUP detectors2

COMPONENT Histo8 = Monitor_nD(xwidth=0.2, yheight=0.1762, user1="t_prime", options="y limits=[-0.0881 0.0881] user1 limits=[0 0.026] bins=1000")
  AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank8  GROUP detectors2

COMPONENT Histo9 = Monitor_nD(xwidth=0.2, yheight=0.1762, user1="t_prime", options="y limits=[-0.0881 0.0881] user1 limits=[0 0.026] bins=1000")
  AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank9  GROUP detectors2

COMPONENT Histo10 = Monitor_nD(xwidth=0.2, yheight=0.1762, user1="t_prime", options="y limits=[-0.0881 0.0881] user1 limits=[0 0.026] bins=1000")
  AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank10  GROUP detectors2
  COMPONENT ALL_TOF_B = Monitor_nD(xwidth=1.5, yheight=1.5, user1="t_prime", options="user1 limits=[0 0.08] bins=81 parallel")
    AT (0,0,-0.46548) RELATIVE powder GROUP alltof

  COMPONENT ALL_TOF_F = Monitor_nD(xwidth=1.5, yheight=1.5, user1="t_prime", options="user1 limits=[0 0.08] bins=81 parallel")
    AT (0,0,0.46548) RELATIVE powder GROUP alltof

COMPONENT CalcT = Arm()
WHEN (eventmode==1) AT (0,0,0) relative powder
GROUP detectors
EXTEND %{
  t = t-t_minE/1e6;
%}



COMPONENT nD_Mantid_1 = Monitor_nD(xwidth=0.2, yheight=0.1762, options="mantid square x limits=[-0.1 0.1] bins=2 y limits=[-0.0881 0.0881] bins=13, neutron pixel user1, list all neutrons",
				   filename="Bbank", nowritefile=(!eventmode))
  WHEN (eventmode==1) AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank1  GROUP detectors

COMPONENT TOF2 = Monitor_nD(xwidth=0.2, yheight=0.1762, options="square x limits=[-0.1 0.1] bins=2 y limits=[-0.0881 0.0881] bins=13, neutron pixel user1, list all neutrons",
			       filename="Bbank", nowritefile=(!eventmode))
  WHEN (eventmode==1) AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank2  GROUP detectors

COMPONENT TOF_3 = Monitor_nD(xwidth=0.2, yheight=0.1762, options="square x limits=[-0.1 0.1] bins=2 y limits=[-0.0881 0.0881] bins=13, neutron pixel user1, list all neutrons",
			       filename="Bbank", nowritefile=(!eventmode))
  WHEN (eventmode==1) AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank3  GROUP detectors

COMPONENT TOF_4 = Monitor_nD(xwidth=0.2, yheight=0.1762, options="square x limits=[-0.1 0.1] bins=2 y limits=[-0.0881 0.0881] bins=13, neutron pixel user1, list all neutrons",
			       filename="Bbank", nowritefile=(!eventmode))
  WHEN (eventmode==1) AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank4  GROUP detectors

COMPONENT TOF_5 = Monitor_nD(xwidth=0.2, yheight=0.1762, options="square x limits=[-0.1 0.1] bins=2 y limits=[-0.0881 0.0881] bins=13, neutron pixel user1, list all neutrons",
			       filename="Bbank", nowritefile=(!eventmode))
  WHEN (eventmode==1) AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank5  GROUP detectors

COMPONENT nD_Mantid_2 = Monitor_nD(xwidth=0.2, yheight=0.1762, options="mantid square x limits=[-0.1 0.1] bins=2 y limits=[-0.0881 0.0881] bins=13, neutron pixel min=100 user1, list all neutrons",
			       filename="Fbank", nowritefile=(!eventmode))
  WHEN (eventmode==1) AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank6  GROUP detectors

COMPONENT TOF_7 = Monitor_nD(xwidth=0.2, yheight=0.1762, options="square x limits=[-0.1 0.1] bins=2 y limits=[-0.0881 0.0881] bins=13, neutron pixel min=100 user1, list all neutrons",
			       filename="Fbank", nowritefile=(!eventmode))
  WHEN (eventmode==1) AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank7  GROUP detectors

COMPONENT TOF_8 = Monitor_nD(xwidth=0.2, yheight=0.1762, options="square x limits=[-0.1 0.1] bins=2 y limits=[-0.0881 0.0881] bins=13, neutron pixel min=100 user1, list all neutrons",
			       filename="Fbank", nowritefile=(!eventmode))
  WHEN (eventmode==1)AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank8  GROUP detectors

COMPONENT TOF_9 = Monitor_nD(xwidth=0.2, yheight=0.1762, options="square x limits=[-0.1 0.1] bins=2 y limits=[-0.0881 0.0881] bins=13, neutron pixel min=100 user1, list all neutrons",
			       filename="Fbank", nowritefile=(!eventmode))
  WHEN (eventmode==1) AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank9  GROUP detectors

COMPONENT TOF_10 = Monitor_nD(xwidth=0.2, yheight=0.1762, options="square x limits=[-0.1 0.1] bins=2 y limits=[-0.0881 0.0881] bins=13, neutron pixel min=100 user1, list all neutrons",
			       filename="Fbank", nowritefile=(!eventmode))
  WHEN (eventmode==1) AT (0, 0.4165+0.09-0.18/13*7, -0.46548) RELATIVE arm_bank10  GROUP detectors

  COMPONENT Catchall = Arm()
  WHEN (eventmode==0)  AT (0,0,0) relative powder
GROUP detectors
  EXTEND %{
  if(INSTRUMENT_GETPAR(eventmode)==0) SCATTER;
  %}


END
