/*******************************************************************************
* Instrument: Test_undulator
*
* %Identification
* Written by: Erik B Knudsen <erkn@fysik.dtu.dk>
* Date: Aug 2016
* Origin: DTU Physics
* Release: McXtrace 2.3
* Version: 1.0
* %INSTRUMENT_SITE: Tests_sources
*
*  Unit test instrument for the Undulator component
*
* %Description
* A simple undulator with parameters as in Kim, 1989 (sec. 4).
*
* %Example: Test_undulator.instr minh=1 maxh=2 FAST=1 Detector: e_monitor1_I=2e+14
*
* %Parameters
* minh: [1] lowest harmonic to include in the simulation
* maxh: [1] highest harmonic to include in the simulation 
* FAST: [0/1] Use a faster integration scheme
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT Test_undulator( int minh=1, int maxh=1, FAST=0)

DECLARE
%{
    double e0;
    double de=1e-2;

    double Ee=1.5;
    double lam_u=3.65e-2;
    double K=1.0;

    double ee0;
    double dde;
%}

INITIALIZE
%{


    e0=0.95*Ee*Ee/((1+K*K/2.0)*lam_u*100.0);/*formula expects Ee in GeV and lam_u in cm (Kim 4.27).*/
    if (de>e0) de=e0;
    double emin=minh*e0-de;
    double emax=maxh*e0+de;
    dde=(emax-emin)/2.0;
    ee0=(emax+emin)/2.0;
    printf("Test_undulator: energy interval: [ %g +- %g ] = [ %g : %g ]\n",ee0,dde,ee0-dde,ee0+dde);
%}

TRACE

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


/*Undulator is set to have 5th harmonic at 15 keV*/
COMPONENT src1 = Undulator(Ee=1.5, K=1, E0=ee0, dE=dde, Ie=0.4, B=0,
	Nper=134, lu=3.65e-2, sigex=0.05367e-3, sigey=0.004e-3, focus_xw=10e-3,focus_yh=10e-3, dist=20,
        quick_integ=FAST, verbose=1)
AT(0,0,0) RELATIVE Origin

COMPONENT e_monitor = E_monitor(
    Emin=ee0-dde, Emax=ee0+dde, nE=1000, filename="emon0",
    restore_xray=1,
    xwidth=2e-2, yheight=2e-2)
AT (0, 0, 1) RELATIVE PREVIOUS

COMPONENT psd_monitor = PSD_monitor(
    filename="psd0",nx=200,ny=200, 
    restore_xray=1, xwidth=2e-4, yheight=2e-4)
AT (0, 0, 0) RELATIVE PREVIOUS

COMPONENT slit = Slit(xwidth=0.6e-3,yheight=0.6e-3)
    AT(0,0,20) RELATIVE Origin

COMPONENT e_monitor1 = COPY(e_monitor)(filename="emon1")
AT(0,0,20) RELATIVE Origin

COMPONENT psd_monitor1 = COPY(psd_monitor)(filename="psd1", xwidth=10.1e-3, yheight=10.1e-3)
AT(0,0,20) RELATIVE Origin
// insert components here (e.g. Insert -> Source -> ...)




FINALLY
%{
%}

END
