/*******************************************************************************
*         McStas instrument definition URL=http://www.mcstas.org
*
* Instrument: Test_Pol_Tabled (rename also the example and DEFINE lines below)
*
* %Identification
* Written by: Your name (email)
* Date: Current Date
* Origin: Your institution
* %INSTRUMENT_SITE: Tests_polarization
*
* Test the tabled magnetic field option
*
* %Description
* A test instrument to check that supplying a magnetic field as a vector field point cloud
* is working. The field referred to by the default input parameter MF is 10 \mu T along the z-axis
* at entry and flips to l0 \mu T along the negative z-axis halfway through
*
* %Example: -n1e4 interpol_method=default MF=flipfield.dat Detector: pol_6_I=1.0
* %Example: -n1e4 interpol_method=default MF=constfield.dat Detector: polx_6_I=0.05
*
* %Parameters
* MF:              [string] Field definition file
* zdepth:               [m] Z-dimension of field
* interpol_method: [string] Choice of interpolation method "kdtree" (default on CPU) / "regular" (default on GPU)
*
* %Link
* A reference/HTML link for more information
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT Test_Pol_Tabled(string MF="flipfield.dat", zdepth=1, string interpol_method="default")

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

/* 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 src = Source_simple(
    radius = 0.02, dist = 5, focus_xw = 0.1, focus_yh = 0.1,
    lambda0 = 5, dlambda = 4.99)
  AT (0, 0, 0) RELATIVE Origin

COMPONENT setpol = Set_pol(
    px = 0, py = 1, pz = 0)
  AT (0, 0, 1) RELATIVE src


COMPONENT pol_0 = Pol_monitor(
    restore_neutron=1,xwidth=0.2, yheight=0.2, mx=0, my=1, mz=0)
AT(0,0,1) RELATIVE setpol

COMPONENT polx_0 = PolLambda_monitor(nL=100,npol=101,
    xwidth = 0.2, yheight = 0.2, restore_neutron = 1, mx = 1,
    my = 0, mz = 0, Lmin = 0, Lmax = 10, filename="polx_0")
AT(0,0,0) RELATIVE setpol
COMPONENT poly_0 = COPY (polx_0)(mx=0,my=1,mz=0,filename="poly_0")
AT(0,0,0) RELATIVE PREVIOUS
COMPONENT polz_0 = COPY (polx_0)(mx=0,my=0,mz=1,filename="polz_0")
AT(0,0,0) RELATIVE PREVIOUS
COMPONENT lmon_0 = L_monitor(nL=100,Lmin=0, Lmax=10,xwidth=0.2, yheight=0.2, filename="lmon_0")
AT(0,0,0) RELATIVE PREVIOUS


COMPONENT field = Pol_tabled_field(
    xwidth=0.1, yheight=0.1, zdepth=zdepth, filename=MF, interpol_method=interpol_method)
AT(0,0,0.5) RELATIVE PREVIOUS


COMPONENT polx_6 = COPY(polx_0)(filename="polx_6")
AT(0,0,1) RELATIVE field
/*COMPONENT poly_6 = COPY(poly_0)(filename="poly_6")
AT(0,0,0) RELATIVE PREVIOUS
 COMPONENT polz_6 = COPY(polz_0)(restore_neutron=0,filename="polz_6")
AT(0,0,0) RELATIVE PREVIOUS
*/
  COMPONENT pol_6 = COPY(pol_0)
AT(0,0,0) RELATIVE PREVIOUS

COMPONENT lmon_6 = COPY(lmon_0)(filename="lmon_6")
AT(0,0,0) RELATIVE PREVIOUS

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