/***************************************************************************
*         McStas instrument definition URL=http://www.mcstas.org
*
* Instrument: Test_Magnetic_Rotation
*
* %Identification
* Written by: Peter Christiansen and Peter Willendrup
* Date: August 2006
* Origin: RISOE
* %INSTRUMENT_SITE: Tests_polarization
*
* This instrument demonstrates how to use the Pol_constBfield
* component.
*
* %Description
* This instrument demonstrates how to use the component
* Pol_constBfield to make a Mezei Spin flipper.
*
* See:
* Seeger et al. NIM A: Volume 457, Issues 1-2 , 11 January 2001, Pages 338-346
*
* %Example: -n1e5 RESTORENEUT=1 Detector: MPLMonNum2Y_I=3.54525
*
* %Parameters
* RESTORENEUT: [] RESTORE_NEUTRON flag for last monitor within field region.
*
* %Link
*
* %End
****************************************************************************/
DEFINE INSTRUMENT Test_Magnetic_Rotation(RESTORENEUT=1)

DECLARE
%{
  const double magnetLength =  0.2;
  double Bmagnitude;


  %}

/* The INITIALIZE section is executed when the simulation starts     */
/* (C code). You may use them as component parameter values.         */
INITIALIZE
%{
  // we want to set Bmagnitude to the value that the adiabicity
  // E=2/PI(-gamma_n*Bmagnitude)*deltaT
  // for a 10AA neutron is 20 to compare with Figure 1 in the
  // paper by Seeger and Daemen on the NISP algorithm.
  double vel = 2*PI/10*K2V;
  double deltaT =  magnetLength/vel;

  printf("deltaT = %f\n", deltaT);

  Bmagnitude = PI/2*20/deltaT/183.247e6; // Tesla
  printf("Bmagnitude = %f T\n", Bmagnitude);
  %}

/* 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.01, dist = 1.0, focus_xw = 0.01, focus_yh = 0.01, lambda0 = 5,
	      dlambda = 4.99, flux = 1)
     AT (0, 0, 0) RELATIVE Origin

COMPONENT polSetter =
Set_pol(py=1)
     AT (0, 0, 0.5) RELATIVE Origin

COMPONENT pollambdaMonitor1x =
PolLambda_monitor(xwidth=0.1, yheight=0.1,
		  nL=100, Lmin = 0.0, Lmax = 10.0, npol=21,
		  mx=1, my=0, mz=0, filename="pollambdaMon1x.data")
     AT (0, 0, 0.75) RELATIVE Origin

COMPONENT pollambdaMonitor1y =
PolLambda_monitor(xwidth=0.1, yheight=0.1,
		  nL=100, Lmin = 0.0, Lmax = 10.0, npol=21,
		  mx=0, my=1, mz=0, filename="pollambdaMon1y.data")
     AT (0, 0, 0.75) RELATIVE Origin

COMPONENT pollambdaMonitor1z =
PolLambda_monitor(xwidth=0.1, yheight=0.1,
		  nL=100, Lmin = 0.0, Lmax = 10.0, npol=21,
		  mx=0, my=0, mz=1, filename="pollambdaMon1z.data")
     AT (0, 0, 0.75) RELATIVE Origin

COMPONENT magnetNum =
Pol_Bfield(field_type=2, xwidth=0.08, yheight=0.08, zdepth=magnetLength,
	   Bx = 0, By = Bmagnitude, Bz = 0, concentric=1)
     AT (0, 0, 1.0) RELATIVE Origin
/*
COMPONENT MPLMonNum2X_1 =
MeanPolLambda_monitor(xwidth=0.1, yheight=0.1,
		  nL=100, Lmin = 0.0, Lmax = 10.0,
		  mx=1, my=0, mz=0, filename="MPLMonNum2X_1.data")
     AT (0, 0, magnetLength/2) RELATIVE magnetNum
*/

COMPONENT MPLMonNum2Y_1 =
MeanPolLambda_monitor(xwidth=0.1, yheight=0.1,
		  nL=100, Lmin = 0.0, Lmax = 10.0,
		  mx=0, my=1, mz=0, filename="MPLMonNum2Y_1.data",restore_neutron=RESTORENEUT)
     AT (0, 0, magnetLength/2) RELATIVE magnetNum
/*
COMPONENT MPLMonNum2Z_1 =
MeanPolLambda_monitor(xwidth=0.1, yheight=0.1,
		  nL=100, Lmin = 0.0, Lmax = 10.0,
		  mx=0, my=0, mz=1, filename="MPLMonNum2Z_1.data")
     AT (0, 0, magnetLength/2) RELATIVE magnetNum
*/
COMPONENT magnetNumCp =
Pol_Bfield_stop()
  AT(0,0,magnetLength) RELATIVE magnetNum

COMPONENT MPLMonNum2X =
MeanPolLambda_monitor(xwidth=0.1, yheight=0.1,
		  nL=100, Lmin = 0.0, Lmax = 10.0,
		  mx=1, my=0, mz=0, filename="MPLMonNum2X.data")
     AT (0, 0, 1.5) RELATIVE Origin

COMPONENT MPLMonNum2Y =
MeanPolLambda_monitor(xwidth=0.1, yheight=0.1,
		  nL=100, Lmin = 0.0, Lmax = 10.0,
		  mx=0, my=1, mz=0, filename="MPLMonNum2Y.data")
     AT (0, 0, 1.5) RELATIVE Origin

COMPONENT MPLMonNum2Z =
MeanPolLambda_monitor(xwidth=0.1, yheight=0.1,
		  nL=100, Lmin = 0.0, Lmax = 10.0,
		  mx=0, my=0, mz=1, filename="MPLMonNum2Z.data")
     AT (0, 0, 1.5) 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
