/******************************************************************************
*         McStas instrument definition URL=http://www.mcstas.org
*
* Instrument: Test_Pol_Bender
*
* %Identification
* Written by: Peter Christiansen
* Date: July 2006
* Origin: RISOE
* %INSTRUMENT_SITE: Tests_polarization
*
* Test Pol_bender.
*
* %Description
* Test that Pol_bender polarizes an unpolarized beam, and allows one
* to test the different options.
*
* %Example: GUIDELENGTH=1 Detector: psdBender_I=0.703658
*
* %Parameters
* GUIDELENGTH: [m] Length of bender
* GUIDERADIUS: [m] Radius of curvature of bender
* ENDOPTION:    [] Setting for parallel start/end planes of bender 
* NSLITS:       [] Number of channels in bender 
* WSPACER:     [m] Spacer dimension  
* DRAWOPTION:   [] Bender drawing option 1: fine(all slits/90 points per arc), 2: normal (max 20/40), 3: rough (max 5/10) 
*
* %Link
*
* %End
******************************************************************************/

/* Change name of instrument and input parameters with default values */
DEFINE INSTRUMENT Test_Pol_Bender(GUIDELENGTH=1.0, GUIDERADIUS=10.0,
				  int ENDOPTION=0, int NSLITS=5, WSPACER=0.005,
				  int DRAWOPTION=1)

/* The DECLARE section allows us to declare variables or  small      */
/* functions in C syntax. These may be used in the whole instrument. */
DECLARE
%{
  double calcAlpha(double length, double radius) {
    // calculate angle of arm after curved guide
    return RAD2DEG * length/radius;
  }

  double calcX(double length, double radius) {
    // calculate position and angle of arm after curved guide
    double alpha = DEG2RAD * calcAlpha(length, radius);
    return radius*(1.0-cos(alpha));
  }

  double calcZ(double length, double radius) {
    // calculate position and angle of arm after curved guide
    double alpha = DEG2RAD * calcAlpha(length, radius);
    return radius*sin(alpha);
  }
  %}

/* 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

/* The Arm() class component defines reference points and orientations  */
/* in 3D space. Every component instance must have a unique name. Here, */
/* Origin is used. This Arm() component is set to define the origin of  */
/* our global coordinate system (AT (0,0,0) ABSOLUTE). It may be used   */
/* for further RELATIVE reference, Other useful keywords are : ROTATED  */
/* EXTEND GROUP PREVIOUS. Also think about adding a neutron source !    */
/* Progress_bar is an Arm displaying simulation progress.               */
COMPONENT Origin = Progress_bar()
     AT (0,0,0) ABSOLUTE

COMPONENT source =
Source_simple(radius = 0.02, dist = 1.2, focus_xw = 0.08, focus_yh = 0.08, lambda0 = 15,
	      dlambda = 14.5, flux = 1)
     AT (0, 0, 0) RELATIVE Origin

COMPONENT lamStart =
L_monitor(nL = 30, filename = "lambdaStart.dat",
	  xwidth = 0.10, yheight = 0.10,
	  Lmin = 0.0, Lmax = 30)
     AT (0, 0, 0.1) RELATIVE Origin

COMPONENT psdStart =
PSD_monitor(xwidth=0.10, yheight=0.10,
	    nx=40, ny=40, filename="psdStart.dat")
     AT (0, 0, 1.15) RELATIVE Origin

COMPONENT bender =
Pol_bender(xwidth = 0.08, yheight = 0.08, length = GUIDELENGTH, radius= GUIDERADIUS,
	   nslit=NSLITS, d=WSPACER,
	   endFlat=ENDOPTION, drawOption=DRAWOPTION,
	   rTopUpPar={0.99, 0.0219, 6.07, 3.0, 0.003},
	   rTopDownPar={0.99, 0.0219, 6.07, 2.0, 0.003})
     AT (0, 0, 1.2) RELATIVE Origin

COMPONENT armlambdaStop = Arm()
     AT (calcX(GUIDELENGTH, GUIDERADIUS), 0, calcZ(GUIDELENGTH, GUIDERADIUS)+1.2)
     RELATIVE Origin
ROTATED (0, calcAlpha(GUIDELENGTH, GUIDERADIUS), 0) RELATIVE Origin

COMPONENT lamStopBender =
L_monitor(nL = 30, filename = "lambdaStopBender.dat",
	  xwidth = 0.10, yheight = 0.10,
	  Lmin = 0.0, Lmax = 30)
     AT (0, 0, 0.001) RELATIVE armlambdaStop

COMPONENT psdBender =
PSD_monitor(xwidth=0.10, yheight=0.10,
	    nx=80, ny=80, filename="psdBender.dat")
     AT (0, 0, 0.001) RELATIVE armlambdaStop

COMPONENT pollambdaMonitorY =
PolLambda_monitor(xwidth=0.1, yheight=0.1,
		  nL=30, Lmin = 0, Lmax = 30, npol=41,
		  my=1, filename="pollambdaMonY.data")
     AT (0, 0, 0.05) RELATIVE armlambdaStop

COMPONENT meanpollambdaMonitorY =
MeanPolLambda_monitor(xwidth=0.1, yheight=0.1,
      nL=30, Lmin = 0, Lmax = 30,
      my=1, filename="meanpollambdaMonY.data")
     AT (0, 0, 0.05) RELATIVE armlambdaStop

/* 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
