/*******************************************************************************
*         McXtrace instrument definition URL=http://www.mcxtrace.org
*
* Instrument: Test_ML_elliptic (rename also the example and DEFINE lines below)
*
* %Identification
* Written by: Your name (email)
* Date: Current Date
* Origin: Your institution
* Release: McXtrace 1.0
* Version: 0.2
* %INSTRUMENT_SITE: Tests_optics
*
* Unit test instrument for Multilayer_elliptic
*
* %Description
* Tests the correct working of the mulitlyer_elliptic component both using a
* reflectivity file and the kinematical approximation. 
*
* %Example: Test_ML_elliptic.instr S1=1 S2=2 gamma=1.2 fromfile=0 Detector: emon1_I=6.91956e-21
* %Example: Test_ML_elliptic.instr S1=1 S2=2 gamma=1.2 fromfile=1 Detector: emon1_I=4.61321e-21
* %Example: Test_ML_elliptic.instr S1=1 S2=2 gamma=1.2 fromfile=1 fxw=2e-2 fyh=1e-3 Detector: emon1_I=9.22372e-08
*
* %Parameters
* S1: [m] Distance from source focus to multilayer centre.
* S1: [m] Distance from multilayer centre to focal point
* gamma: [deg] Design glancing angle at centre.
* fromfile: [ ] If nonzero read reflectivity number from a datafile ("reflectivity.txt")
* fxw: [m] width of the beam
* fxh: [m] height of the beam
*
* %End
*******************************************************************************/

/* Change name of instrument and input parameters with default values */
DEFINE INSTRUMENT Test_ML_elliptic(S1=1, S2=2, gamma=1.2, fromfile=1, fxw=1e-9, fyh=1e-9)

/* 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 an xray source !    */
/* Progress_bar is an Arm displaying simulation progress.               */
COMPONENT Origin = Progress_bar()
  AT (0,0,0) ABSOLUTE

COMPONENT source = Source_flat(
    radius = 1e-6, dist = 1, focus_xw = fxw, focus_yh = fyh,
    E0 = 20.1, dE = 20.0)
AT (0, 0, 0) RELATIVE Origin

COMPONENT psd0 = PSD_monitor(
	filename="psd0",xwidth=.3, yheight=.1, nx=201, ny=201,
	restore_xray=1)
AT(0,0,1) RELATIVE source

COMPONENT emon0 = E_monitor(
    filename="emon0",xwidth=.3, yheight=.1, nE=201,restore_xray=1, Emin=0, Emax=50)
AT(0,0,1) RELATIVE source

COMPONENT a1=Arm()
AT(0,0,1) RELATIVE source
ROTATED (gamma,0,0) RELATIVE source

COMPONENT ml0 = Multilayer_elliptic(
    coating = "Ref_W_B4C.txt", theta = gamma,
    s1 = S1, s2 = S2, length = 0.1, width = 0.1, R0 = 1,
    Emin=7, Emax=10, Estep=0.05)
WHEN (fromfile) AT (0, 0, 0) RELATIVE a1

COMPONENT ml1 = Multilayer_elliptic(
    coating = "NULL", theta = gamma,
    s1 = S1, s2 = S2, length = 0.1, width = 0.1, R0 = 1,
    Emin=0, Emax=14.2, Estep=0.05,
    Gamma=0.2, Lambda=50, N=10, rho_AB=3.9817)
WHEN (!fromfile) AT (0, 0, 0) RELATIVE a1

COMPONENT a2 = Arm()
AT(0,0,0) RELATIVE a1
ROTATED (gamma,0,0) RELATIVE a1

COMPONENT psd1=COPY(psd0)(filename="psd1")
AT(0,0,0.1) RELATIVE a2
COMPONENT emon1=COPY(emon0)(filename="emon1")
AT(0,0,0) RELATIVE PREVIOUS

COMPONENT psdd=COPY(psd0)(filename="psdd")
AT(0,0,2) RELATIVE a2 
COMPONENT emond=COPY(emon0)(filename="emond")
AT(0,0,0) RELATIVE PREVIOUS

COMPONENT emonsingle=COPY(emon0)(filename="emonsingle",nE=20,Emin=7.9,Emax=8.1)
AT(0,0,0) RELATIVE PREVIOUS

COMPONENT psd6=COPY(psd1)(filename="psd6")
AT(0,0,3.2) RELATIVE a2 
COMPONENT emon6=COPY(emon0)(filename="emon6")
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

