/*******************************************************************************
*         McStas instrument definitio URL=http://www.mcstas.org
*
* Instrument: SEMSANS
*
* %Identification
* Written by: Morten Sales
* Date: 2014
* Origin: Copenhagen, Berlin, Delft
* %INSTRUMENT_SITE: TUDelft
*
* SEMSANS-instrument
*
* %Description
* SEMSANS instrument with 2 isosceles triangular field coils
*
* %Example: -y Detector: TOF_det_I=2.87073e-09
*
* %Parameters
* triacoil_depth: [m]   Half of the triangular coil depth in z-direction (one right triangle)
* triacoil_width: [m]   xwidth of triangular coil
* triacoil_height: [m]  yheight of triangular coil
* pol_zdepth: [m]       Depth of the polariser along z
* vcoil_zdepth: [m]     Depth along z of the v-coil flipper
* Guide1_depth: [m]     Depth of first guide/precession field section
* Guide2_depth: [m]     Depth of second guide/precession field section
* Guide3_depth: [m]     Depth of third guide/precession field section
* Guide4_depth: [m]     Depth of fourth guide/precession field section
* Bguide: [T]           Field strength of guide/precession field
* Bextra: [T]           Field strength of extra field in guide field 1 to acheive echo
* Bt2: [T]              Field in first triangular coil
* Bt1: [T]              Field in second triangular coil
* DLambda: [AA]         Wavelength spread from source (half of it)
* Lambda: [AA]          Mean wavelength
* flippos: [m]          Position (away from position set by vcoil_34_pos) of pi-flipper
* FLIP: [1]             Choose polarisation direction using v-coil pi/2-flipper (1 is down, -1 is up)
* chop1_pos: [m]        Position of first chopper
* chop2_pos: [m]        Position of second chopper
* pol_pos: [m]          Position of polariser
* analyser_pos: [m]     Position of analyser
* grating_w: [m]        Width of transmitting part of grating
* grating_a: [m]        Width of absorbing part of grating
* slit_1_pos: [m]       Position of first slit
* slit_2_pos: [m]       Position of second slit
* Guide1_pos: [m]       Position of first guide/precession field
* Guide2_pos: [m]       Position of second guide/precession field
* Guide3_pos: [m]       Position of third guide/precession field
* Guide4_pos: [m]       Position of fourth guide/precession field
* vcoil_12_pos: [m]     Position of first v-coil pair
* vcoil_34_pos: [m]     Position of second v-coil pair
* vcoil_56_pos: [m]     Position of third v-coil pair
* triacoil_1_pos: [m]   Position of first triangular coil (centre)
* triacoil_2_pos: [m]   Position of second triangular coil (centre)
* grating_pos: [m]      Position of grating
* grating_pos: [m]      Position of grating
* detector_pos: [m]     Position of detector
* tlow: [mu-s]          tmin of detector
* thigh: [mu-s]         tmax of detector
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT SEMSANS_instrument(triacoil_depth=1.935000e-01, triacoil_width=7.042824e-02, triacoil_height=3.000000e-01, pol_zdepth=9.300000e-01, vcoil_zdepth=1.500000e-01, Guide1_depth=1.650000e-02, Guide2_depth=3.315000e-01, Guide3_depth=3.315000e-01, Guide4_depth=5.650000e-02, Bguide=-5.000000e-04, Bextra=-2.120000e-02, Bt2=-4.440000e-03, Bt1=-2.560000e-03, DLambda=4.166366e+00, Lambda=4.559500e+00, flippos=3.100000e-02, FLIP=1.000000e+00, chop1_pos=5.000000e-01, chop2_pos=9.740000e-01, pol_pos=1.907000e+00, analyser_pos=6.072000e+00, grating_w=1.570000e-03, grating_a=2.930000e-03, slit_1_pos=2.957000e+00, slit_2_pos=5.437000e+00, Guide1_pos=3.307000e+00, Guide2_pos=3.710500e+00, Guide3_pos=4.342000e+00, Guide4_pos=5.060500e+00, vcoil_12_pos=3.157000e+00, vcoil_34_pos=4.192000e+00, vcoil_56_pos=5.267000e+00, triacoil_1_pos=3.517000e+00, triacoil_2_pos=4.867000e+00, grating_pos=6.757000e+00, detector_pos=6.957000e+00, tlow=2.190523040779461e+03, thigh=1.214744595341338e+04)

/* The DECLARE section allows us to declare variables or  small      */
/* functions in C syntax. These may be used in the whole instrument. */
DECLARE
%{

%}

/* The INITIALIZE section is executed when the simulation starts     */
/* (C code). You may use them as component parameter values.         */
INITIALIZE
%{

%}
/* Here comes the TRACE section, where the actual      */
/* instrument is defined as a sequence of components.  */
TRACE

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

// Source
COMPONENT source = Source_simple(
  radius=0, dist = slit_1_pos, focus_xw=20e-3, focus_yh=20e-3,
  xwidth = 2e-3, yheight = 2e-3,
  lambda0 = Lambda,dlambda = DLambda)
AT (0, 0, 0) RELATIVE Origin

// First chopper
COMPONENT chop1 = DiskChopper(radius=0.21, theta_0=20, nu=25, nslit=2, phase=-10, isfirst=1, yheight=0.21)
AT (0, 0, chop1_pos) RELATIVE Origin

// Second chopper
COMPONENT chop2 = DiskChopper(radius=0.21, theta_0=20, nu=25, nslit=2, phase=20-10, isfirst=0, yheight=0.21)
AT (0, 0, chop2_pos) RELATIVE Origin

// Polariser
COMPONENT polarizer = Set_pol(py=1)
AT (0, 0, pol_pos+0.5*pol_zdepth) RELATIVE Origin

//First Slit
COMPONENT slit_1 = Slit(
   xwidth=15e-3,yheight=15e-3)
AT (0,0,slit_1_pos) RELATIVE Origin

//First v-coil of first v-coil pair
COMPONENT vcoil1=Pol_pi_2_rotator(
    xwidth=0.15,yheight=0.15,zdepth=vcoil_zdepth,rx=0,ry=0,rz=FLIP*1)
AT (0,0,vcoil_12_pos-vcoil_zdepth) RELATIVE Origin

//Second v-coil of first v-coil pair
COMPONENT vcoil2=Pol_pi_2_rotator(
    xwidth=0.15,yheight=0.15,zdepth=vcoil_zdepth,rx=0,ry=1,rz=0)
AT (0,0,vcoil_12_pos) RELATIVE Origin


// START GUIDEFIELD 1
COMPONENT Guide_field1 = Pol_Bfield(field_type=1, xwidth=0.4, yheight=0.4, Bx=0, By=Bguide+Bextra, Bz=0)
     AT (0,0,Guide1_pos) RELATIVE Origin


// STOP GUIDE FIELD 1
COMPONENT Guide_field1_cp = Pol_Bfield_stop()
     AT (0,0,Guide1_pos+Guide1_depth) RELATIVE Origin


// TRIA COIL 1
COMPONENT triacoil_1=Pol_triafield(
    xwidth=triacoil_width,yheight=triacoil_height,zdepth=2*triacoil_depth,
    B=Bt1, Bguide=Bguide)
  AT (0,0,triacoil_1_pos-triacoil_depth) RELATIVE Origin


// START GUIDEFIELD 2
COMPONENT Guide_field2 = Pol_Bfield(field_type=1, xwidth=0.4, yheight=0.4, Bx=0, By=Bguide, Bz=0)
     AT (0,0,Guide2_pos) RELATIVE Origin


// STOP GUIDE FIELD 2
COMPONENT Guide_field2_cp = Pol_Bfield_stop()
  AT (0,0,Guide2_pos+Guide2_depth+flippos) RELATIVE Origin


//First v-coil of second v-coil pair
COMPONENT vcoil3=Pol_pi_2_rotator(
    xwidth=0.15,yheight=0.15,zdepth=vcoil_zdepth,rx=0,ry=0,rz=1)
AT (0,0,vcoil_34_pos-vcoil_zdepth+flippos) RELATIVE Origin

//Second v-coil of second v-coil pair
COMPONENT vcoil4=Pol_pi_2_rotator(
    xwidth=0.15,yheight=0.15,zdepth=vcoil_zdepth,rx=0,ry=0,rz=1)
AT (0,0,vcoil_34_pos+flippos) RELATIVE Origin


// START GUIDEFIELD 3
COMPONENT Guide_field3 = Pol_Bfield(field_type=1, xwidth=0.4, yheight=0.4, Bx=0, By=Bguide, Bz=0)
     AT (0,0,Guide3_pos+flippos) RELATIVE Origin


// STOP GUIDE FIELD 3
COMPONENT Guide_field3_cp = Pol_Bfield_stop()
     AT (0,0,Guide3_pos+Guide3_depth) RELATIVE Origin


// START TRIA COIL 2
COMPONENT triacoil_2=Pol_triafield(
    xwidth=triacoil_width,yheight=triacoil_height,zdepth=2*triacoil_depth,
    B=Bt2, Bguide=Bguide)
  AT (0,0,triacoil_2_pos-triacoil_depth) RELATIVE Origin


// START GUIDEFIELD 4
COMPONENT Guide_field4 = Pol_Bfield(field_type=1, xwidth=0.4, yheight=0.4, Bx=0, By=Bguide, Bz=0)
     AT (0,0,Guide4_pos) RELATIVE Origin


// STOP GUIDE FIELD 4
COMPONENT Guide_field4_cp = Pol_Bfield_stop()
  AT (0,0,Guide4_pos+Guide4_depth) RELATIVE Origin

//First v-coil of third v-coil pair
COMPONENT vcoil5=Pol_pi_2_rotator(
    xwidth=0.15,yheight=0.15,zdepth=vcoil_zdepth,rx=0,ry=1,rz=0)
AT (0,0,vcoil_56_pos-vcoil_zdepth) RELATIVE Origin

//Second v-coil of third v-coil pair
COMPONENT vcoil6=Pol_pi_2_rotator(
    xwidth=0.15,yheight=0.15,zdepth=vcoil_zdepth,rx=0,ry=0,rz=1)
AT (0,0,vcoil_56_pos) RELATIVE Origin

//Second slit
COMPONENT slit_2=Slit(
    xwidth=15e-3,yheight=15e-3)
AT(0,0,slit_2_pos) RELATIVE Origin

//Analyser
COMPONENT analyser=PolAnalyser_ideal(mx=0,my=1,mz=0)
     AT(0,0,analyser_pos) RELATIVE Origin


//THIS IS GRATING
		COMPONENT GratingSlit1_1 = Slit(xwidth=grating_w,yheight=5e-3)
			AT (-2.0*grating_w - 2.0*grating_a, 0, grating_pos) RELATIVE Origin
			 GROUP Grating

		COMPONENT GratingSlit1_2 = Slit(xwidth=grating_w,yheight=5e-3)
			AT (-1.0*grating_w - 1.0*grating_a, 0, grating_pos) RELATIVE Origin
                        GROUP Grating

		COMPONENT GratingSlit1_3 = Slit(xwidth=grating_w,yheight=5e-3)
			AT (0, 0, grating_pos) RELATIVE Origin
                        GROUP Grating

		COMPONENT GratingSlit1_4 = Slit(xwidth=grating_w,yheight=5e-3)
			AT (1.0*grating_w + 1.0*grating_a, 0, grating_pos) RELATIVE Origin
                        GROUP Grating

		COMPONENT GratingSlit1_5 = Slit(xwidth=grating_w,yheight=5e-3)
			AT (2.0*grating_w + 2.0*grating_a, 0, grating_pos) RELATIVE Origin
                        GROUP Grating
/*
//THIS IS DOUBLE SLIT
		COMPONENT GratingSlit2_1 = Slit(xwidth=grating_w,yheight=5e-3)
			AT (-0.5*grating_w - 0.5*grating_a, 0, grating_pos) RELATIVE Origin
			 GROUP Grating

		COMPONENT GratingSlit2_2 = Slit(xwidth=grating_w,yheight=5e-3)
			AT (0.5*grating_w + 0.5*grating_a, 0, grating_pos) RELATIVE Origin
			 GROUP Grating
*/
/*
//THIS IS SINGLE SLIT
		COMPONENT GratingSlit3_1 = Slit(xwidth=grating_w,yheight=5e-3)
			AT (0, 0, grating_pos) RELATIVE Origin
*/


COMPONENT TOF_det =  TOF_monitor(xwidth = 0.05, yheight = 0.05,
          					     nt=251, tmin=tlow, tmax=thigh, filename="TOF_det")
AT (0, 0, detector_pos) RELATIVE Origin


/* This section is executed when the simulation ends (C code). Other    */
/* optional sections are : SAVE                                         */
FINALLY
%{
%}

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