/**************************************************************************
*         McStas instrument definition URL=http://www.mcstas.org
*
* Instrument: Test_Pol_TripleAxis
*
* %Identification
* Written by: Peter Christiansen
* Date: July 2006.
* Origin: RISOE
* %INSTRUMENT_SITE: Tests_polarization
*
* Based on Emmanuel Farhi's thermal H8 triple-axis spectrometer from
* Brookhaven reactor
*
* %Description
* This instrument is a simple model of the thermal H8 triple-axis
* spectrometer from former Brookhaven reactor. It has an (polarizing)
* monochromator and a (polarizaing) analyzer. The sample is a vanadium
* cylinder.
* Three cases can be done:
* -1) Spin flip (flipper is inserted): Up->Down
*  0) No polarization: Up/Down->Up/Down
*  1) No spin flip: Up->Up
* The Vanadium sample should give I(0)=2*(I(-1)+I(1)) and I(-1)=2*I(1).
*
* %Example: OPTION=-1 Detector: pollambdaMonitorDet_I=5.63101e-12
*
* %Parameters
* OPTION:        [1] See above           
* LAMBDA:     [Angs] Source wavelength   
* MOZ: [Arc minutes] Mosaicity           
*
* %Link
*
* %End
*******************************************************************************/

DEFINE INSTRUMENT Test_Pol_TripleAxis(int OPTION=0, LAMBDA=2.0, MOZ=40)

DECLARE
%{
  double DM  = 3.3539; /* Monochromator d-spacing in Angs */
                       /* PG002 Orders : 1st 3.355 2e 1.6775, 3e 1.1183 */

  /* to compute */
  double A1,A2;
  double A3,A4;
  double A5,A6;
  double mono_q, Ei;

  /* This variable helps us switch on and of the spin flipper*/
  double filterProb;
%}
/* end of DECLARE */

INITIALIZE
%{
  int    ORDER = 1;
  double vi, Ki, Kf;
  int    SM, SS, SA;

  if(OPTION<-1 || OPTION>1) {

    printf("OPTION %d not permitted.", OPTION);
    exit(1);
  }

  /* SM : scattering at mono to the right (-1)/left(+1) */
  /* SS : scattering at sample to the right (-1)/left(+1) */
  /* SA : scattering at analyser to the right (-1)/left(+1) */
  SM = 1; SS = -1; SA = 1;

  mono_q = 2*PI*ORDER/DM;  /* Q mono in Angs-1 */

  Ki = 2*PI/LAMBDA;
  vi = K2V*fabs(Ki);
  Ei = VS2E*vi*vi;

  Kf=Ki;

  A2 = asin(mono_q/2/Ki)*RAD2DEG*2;
  A6 = asin(mono_q/2/Kf)*RAD2DEG*2;
  A4 = A2;

  A2 *= SM;   /* A1 : mono theta (crystal) */
  A1 = A2/2;  /* A2 : mono 2 theta (arm to sample) */
  A4 *= SS;   /* A3 : sample theta */
  A3 = A4/2;  /* A4 : sample 2 theta (arm to analyser) */
  A6 *= SA;   /* A5 : analyser theta (crystal) */
  A5 = A6/2;  /* A6 : analyser 2 theta (arm to Dector) */
  %}
/* end of INITIALIZE */

TRACE
/* Source description */

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

/* a flat constant source */
COMPONENT Source =
Source_simple(radius  = 0.01, dist = 5.0,
	      focus_xw = 0.02, focus_yh = 0.02,
	      E0 = Ei, dE = 0.5)
     AT (0,0,0) RELATIVE Origin

COMPONENT Mono_Arm = Arm()
     AT (0, 0, 5.0) RELATIVE Source ROTATED (0, A1, 0) RELATIVE Source

COMPONENT Mono_Out = Arm()
     AT (0, 0, 0) RELATIVE Mono_Arm ROTATED (0, A2, 0) RELATIVE Source

COMPONENT Mono =
Monochromator_flat(zwidth = 0.10, yheight = 0.08,
		   mosaich = MOZ, mosaicv = MOZ,
		   r0 = 1.0, Q=mono_q)
     WHEN (OPTION==0) AT (0, 0, 0) RELATIVE Mono_Arm

COMPONENT Mono_pol =
Monochromator_pol(zwidth = 0.10, yheight = 0.08,
		  mosaic = MOZ, dspread = 0.0,
		  Rup = 1.0, Rdown = 0.0, Q=mono_q, debug = 0)
     WHEN (OPTION==-1 || OPTION==1) AT (0, 0, 0) RELATIVE Mono_Arm


COMPONENT pollambdaMonitorMono =
PolLambda_monitor(xwidth=0.1, yheight=0.1,
		  nL=40, Lmin = 0.97*LAMBDA, Lmax = 1.03*LAMBDA, npol=11,
		  my=1, filename="pollambdaMonMono.data")
     AT (0, 0, 0.5) RELATIVE Mono_Out

COMPONENT Sample_Arm = Arm()
     AT (0, 0, 1.0) RELATIVE Mono_Out ROTATED (0, A3, 0) RELATIVE Mono_Out

COMPONENT Sample_Out = Arm()
     AT (0, 0, 0) RELATIVE Sample_Arm ROTATED (0, A4, 0) RELATIVE Mono_Out

COMPONENT Sample =
V_sample(radius = 0.024, yheight = 0.0254,
	 target_x= 0, target_y=0, target_z= 1.0,
	 focus_xw=0.01, focus_yh = 0.01, pack = 1)
     AT (0, 0, 0) RELATIVE Sample_Out

COMPONENT pollambdaMonitorSample =
PolLambda_monitor(xwidth=0.10, yheight=0.10,
		  nL=40, Lmin = 0.97*LAMBDA, Lmax = 1.03*LAMBDA, npol=11,
		  my=1, filename="pollambdaMonSample.data")
     AT (0, 0, 0.25) RELATIVE Sample_Out

COMPONENT msf =
Pol_constBfield(xwidth=0.10, yheight=0.10, zdepth=0.2, fliplambda=LAMBDA)
     WHEN (OPTION==-1) AT (0, 0, 0.4) RELATIVE Sample_Out
ROTATED (0, 0, 90) RELATIVE Sample_Out

COMPONENT pollambdaMonitorMSF =
PolLambda_monitor(xwidth=0.1, yheight=0.1,
		  nL=40, Lmin = 0.97*LAMBDA, Lmax = 1.03*LAMBDA, npol=11,
		  my=1, filename="pollambdaMonMsf.data")
     AT (0, 0, 0.75) RELATIVE Sample_Out

COMPONENT Ana_Arm = Arm()
     AT (0, 0, 1.0) RELATIVE Sample_Out ROTATED (0, A5, 0) RELATIVE Sample_Out

COMPONENT Ana_Out = Arm() /*        this is the sample-ana axis */
     AT (0, 0, 0) RELATIVE Ana_Arm ROTATED (0, A6, 0) RELATIVE Sample_Out

COMPONENT Ana =
Monochromator_flat(zwidth = 0.10, yheight = 0.08,
		   mosaich = MOZ, mosaicv = MOZ,
		   r0 = 1.0, Q=mono_q)
     WHEN (OPTION==0) AT (0, 0, 0) RELATIVE Ana_Arm

COMPONENT Ana_pol =
Monochromator_pol(zwidth = 0.10, yheight = 0.08,
		  mosaic = MOZ, dspread = 0.0,
		  Rup = 1.0, Rdown = 0.0, Q=mono_q, debug = 0)
     WHEN (OPTION==-1 || OPTION==1) AT (0, 0, 0) RELATIVE Ana_Arm


COMPONENT pollambdaMonitorDet =
PolLambda_monitor(xwidth=0.1, yheight=0.1,
		  nL=40, Lmin = 0.97*LAMBDA, Lmax = 1.03*LAMBDA, npol=11,
		  my=1, filename="pollambdaMonDet.data")
     AT (0, 0, 0.15) RELATIVE Ana_Out

END
