/*******************************************************************************
*
* Instrument: ISIS_IMAT_proto
* %Identification
* Written by: Genoveva Burca (Genoveva.Burca@stfc.ac.uk)
* Date: October 2017
* Version: 2017_01 
* Origin: ISIS
* %INSTRUMENT_SITE: ISIS
*
* IMAT: McStas instrument file of the imaging and diffraction parts of the IMAT instrument.
*
* %Description
*
* McStas instrument for simulating the IMAT - imaging and diffraction
* New McStas moderator file (G. Skoro, ISIS Facility) was added
* \\ISIS\Shares\NeutronicsTeam\TS-2\HydroMod_Upgrade\McStas_TS2_HydroMod_Base_newVirtMod_350mm\Let_Base.mcstas
* Components positions were extracted from IMAT final design (March 2016) 
* Choppers were not added to this model
* Diffraction detectors are
*
* IMPORTANT NOTES:
* 1. For reference use G. Burca et al., Modelling of an imaging beamline at the ISIS pulsed neutron source, 
* Journal of Instrumentation, 8 (2013), no 10, http://dx.doi.org/10.1088/1748-0221/8/10/P10001
* 2. Results from calculations published in the previous paper were obtained using McStas ISIS_moderator component (Face="W5")  
* 
*
* %Parameters
* INPUT PARAMETERS:
* l_min: [AA]   Low wavelength sampled.
* l_max: [AA]   High wavelength sampled.
* t_min: [t]    Low detector time limit.
* t_max: [t]    High detector time limit.
* theta: [deg]  Angle that the detector banks are set at.
*
* %Link 
* <a href="http://www.isis.stfc.ac.uk/instruments/imat">
* %End

*******************************************************************************/

DEFINE INSTRUMENT ISIS_IMAT(l_min=1, l_max=10, t_min=0, t_max=0.2, theta=90)

DECLARE
%{ 
	double e_min, e_max, SDD,SDI; 
%}

INITIALIZE 
%{ 
	e_min=81.799/l_max/l_max;
	e_max=81.799/l_min/l_min;
        /*sample-detector distance diffraction= 2 m.*/
        SDD=2;
%}

TRACE

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

/* New McStas IMAT moderator component (L-H2)*/

COMPONENT moderator=ViewModISIS(Face="TS2.imat", E0 = e_min, E1 = e_max,
  modPosition=0, xw=0.004, yh = 0.01, dist = 56)  
AT (0.0, 0.0, 0.0) RELATIVE Origin

/*define the source_position for mantid*/
COMPONENT sourceMantid = Arm()
AT(0,0,0) RELATIVE moderator

/* Old version of McStas IMAT moderator L-H2/S-Ch4 (W5 port on TS-2) */
/*COMPONENT moderator = ISIS_moderator(Face ="w5", Emin = e_min, Emax = e_max, dist = 1.688,
    focus_xw = 0.11, focus_yh = 0.11, xwidth=0.11, yheight=0.11, CAngle = 0, SAC = 1, Lmin=l_min, Lmax=l_max)
  AT (0.0, 0.0, 0.00) RELATIVE Origin*/

COMPONENT lmon_s = L_monitor(xwidth=0.1, yheight=0.1, filename="lmon_s",Lmin=l_min, Lmax=l_max, nL=100)
AT(0,0,1e-3) RELATIVE Origin

COMPONENT shutter = Guide(
    w1 = 0.10, h1 = 0.10, w2 = 0.10, h2 = 0.10, l = 1.948, m = 3)
  AT (0.0, 0.0, 1.688) RELATIVE Origin
COMPONENT lmon1 = COPY(lmon_s)(filename="lmon1")
AT(0,0,1.948+1e-3) RELATIVE PREVIOUS

COMPONENT guide1 = Guide(
    w1 = 0.095, h1 = 0.095, w2 = 0.095, h2 = 0.095, l = 7.8125, m = 3)
  AT (0.0, 0.0, 3.7222) RELATIVE Origin

/* 2nd piece of guide before the 1st double disk chopper */  
COMPONENT guide2 = Guide(
    w1 = 0.095, h1 = 0.095, w2 = 0.095, h2 = 0.095, l = 0.4866, m = 3)
  AT (0.0, 0.0, 11.6797) RELATIVE Origin

/* 3rd piece of guide before T0 chopper */  
COMPONENT guide3 = Guide(
    w1 = 0.095, h1 = 0.095, w2 = 0.095, h2 = 0.095, l = 0.4556, m = 3)
  AT (0.0, 0.0, 12.2333) RELATIVE Origin

COMPONENT guide4 = Guide(
    w1 = 0.095, h1 = 0.095, w2 = 0.095, h2 = 0.095, l =6.757, m = 3)
  AT (0.0, 0.0, 13.0259) RELATIVE Origin

/* 5th piece of guide before the 2nd double disk chopper */
COMPONENT guide5 = Guide(
    w1 = 0.095, h1 = 0.095, w2 = 0.095, h2 = 0.095, l =0.52, m = 3)
  AT (0.0, 0.0, 19.8459) RELATIVE Origin

COMPONENT guide6 = Guide(
    w1 = 0.095, h1 = 0.095, w2 = 0.095, h2 = 0.095, l =0.428, m = 3)
  AT (0.0, 0.0, 20.4339) RELATIVE Origin
  
COMPONENT guide7 = Guide(
    w1 = 0.095, h1 = 0.095, w2 = 0.095, h2 = 0.095, l =24.582, m = 3)
  AT (0.0, 0.0, 21.0069) RELATIVE Origin

/* Pinhole selector could be one of 5, 10, 20, 40 or 80 mm for imaging or fully open position (e.g. 100mm) for diffraction*/
COMPONENT pinhole = Slit(radius = 0.05)
  AT (0.0, 0.0, 46) RELATIVE Origin

/* Five pairs of jaws for shaping and defining the beam size */  

COMPONENT slit1 = Slit(xwidth = 0.089882, yheight = 0.089882)
  AT (0.0, 0.0, 1.55731) RELATIVE pinhole

COMPONENT slit2 = Slit(xwidth = 0.08088, yheight = 0.08088)
  AT (0.0, 0.0, 2.94097) RELATIVE pinhole
  
COMPONENT slit3 = Slit(xwidth = 0.07168, yheight = 0.07168)
  AT (0.0, 0.0, 4.3562) RELATIVE pinhole

COMPONENT slit4 = Slit(xwidth = 0.06012, yheight = 0.06012)
  AT (0.0, 0.0, 6.13597) RELATIVE pinhole

COMPONENT slit5 = Slit(xwidth = 0.048549, yheight = 0.048549)
  AT (0.0, 0.0, 7.91614) RELATIVE pinhole

/* Slit 6 is used for diffraction measurements being possible to move it close to the sample */

COMPONENT slit6 = Slit(xwidth = 0.004, yheight = 0.15)
AT (0.0, 0.0, 55.996) RELATIVE Origin

/* Sample placed at 10m after the pinhole */

COMPONENT PSDpre = PSD_monitor(nx = 1024, ny = 1024, filename = "PSDpre.dat", xmin =-0.1, xmax =0.1, ymin = -0.1, ymax =0.1, restore_neutron=1)
AT (0.0, 0.0, 10.00) RELATIVE pinhole

// we can not use anymore the Lazy define with cogen3, as the vector assignment 
// must here be static, but the grammar tests for a '{' as fisrt char. The 'Lazy'
// #define is indeed a {value}, but not replaced before the code generation takes
// place.
COMPONENT sample = PowderN(reflections="Na2Ca3Al2F14.laz", radius = 0.002, yheight = 0.01,
        Vc = 1079.1, sigma_abs = 2.9464e-3, sigma_inc = 3.4176, d_phi=60, format={17,6,0,0,0,0,0,13,0 })
AT (0.0, 0.0, 56) RELATIVE Origin
EXTEND
%{
    if(!SCATTERED) ABSORB;
%}

/*define mantid sample point*/
COMPONENT sampleMantid = Arm()
AT(0,0,0) RELATIVE sample


/* Position sensitive detector for imaging setup; maximum field-of-view of 200 x 200 mm2 */
COMPONENT PSDdet = PSD_monitor(nx = 1024, ny = 1024, filename = "PSD.dat", xmin =-0.1, xmax =0.1, ymin = -0.1, ymax =0.1, restore_neutron=1)
AT (0.0, 0.0, 10.01) RELATIVE pinhole
GROUP imaging

COMPONENT catchall_PSDdet = Arm()
AT(0,0,0) RELATIVE PREVIOUS
GROUP imaging
EXTEND
%{
    SCATTER;
%}

COMPONENT psd4pi = PSD_monitor_4PI(radius=0.5, restore_neutron=1, filename="psd4pi")
AT(0,0,0) RELATIVE sample

COMPONENT north_a = Arm()
AT(0,0,0) RELATIVE sample
ROTATED (0, theta, 0) RELATIVE sample

COMPONENT south_a = Arm()
AT(0,0,0) RELATIVE sample
ROTATED (0,-theta,0) RELATIVE sample

/* Diffraction setup; pixel size of 4mm width x 100 mm height */
/*north bank - 2 flat detector modules*/
COMPONENT nD_Mantid_1 = Monitor_nD(
	xwidth=400e-3, yheight=100e-3, filename="north_1.dat",restore_neutron=1,
	options="mantid square x limits=[-200e-3,200e-3] bins=100, y limits=[-50e-3,50e-3] bins=2 neutron pixel t, list all neutrons")
AT(0,-(5e-3+50e-3),SDD) RELATIVE north_a 
GROUP detectors

COMPONENT nD_Mantid_2 = COPY(nD_Mantid_1)(filename="north_2.dat",
	options="mantid square x limits=[-200e-3,200e-3] bins=100, y limits=[-50e-3,50e-3] bins=2 neutron pixel min 200 t, list all neutrons")
AT(0,+(5e-3+50e-3),SDD) RELATIVE north_a
GROUP detectors

/*south bank - 2 flat detector modules*/
COMPONENT nD_Mantid_3 = Monitor_nD(
	xwidth=400e-3, yheight=100e-3, filename="south_1.dat",restore_neutron=1,
	options="mantid square x limits=[-200e-3,200e-3] bins=100, y limits=[-50e-3,50e-3] bins=2 neutron pixel min 400 t, list all neutrons")
AT(0,-(5e-3+50e-3),SDD) RELATIVE south_a 
GROUP detectors

COMPONENT nD_Mantid_4 = COPY(nD_Mantid_1)(filename="south_2.dat",
	options="mantid square x limits=[-200e-3,200e-3] bins=100, y limits=[-50e-3,50e-3] bins=2 neutron pixel min 600 t, list all neutrons")
AT(0,+(5e-3+50e-3),SDD) RELATIVE south_a
GROUP detectors

END
