/*******************************************************************************
*         McStas instrument definition URL=http://www.mcstas.org
*
* Instrument: test Test_Scatter_log_losses
*
* %Identification
* Written by: Erik B Knudsen (erkn@fysik.dtu.dk)
* Date: 2013
* Origin: DTU Fysik
* %INSTRUMENT_SITE: Templates
*
* Example instrument of Scatter_logger feature usage for detection of lost instensity
*
* %Description
*
* This instrument is an example of how to use thet newly developed (and experimental)
* Scatter_logger family of components in McStas. In this example the neutrons deposited in the
* guide wall of a LENGTH m long straight guide are monitored bytwo instances of Monitor_nD
* The first (mnd1) stretches the full length of the guide and simply monitors the intensity lost in
* the guide walls, the latter (mnd2) shows the lost intensity binned by neutron wavelength in the last
* 1m of guide.
*
* Also include (but commented out) is a code-snippet that would dump the lost neutrons to stdout
*
* Example: mcrun Test_Scatter_log_losses LENGTH=10
*
* %Parameters
* LENGTH: [m]  The length of the guide to be modelled
*
* %Link
* <a href="http://orbit.dtu.dk/files/57025387/prod11375088187360.NO_P_v8.pdf">Esben Klinkby talk at NOP&D 2013</a>
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT Test_Scatter_log_losses(LENGTH=10)


/* 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.1, dist = 1, focus_xw = 0.1, focus_yh = 0.1, lambda0=5, dlambda=4.9)
AT (0, 0, 0) RELATIVE Origin

COMPONENT psd0=PSD_monitor(
    xwidth=0.1, yheight=0.1, filename="psd0")
AT(0,0,0.5) RELATIVE PREVIOUS

COMPONENT s1=Scatter_logger()
AT(0,0,1) RELATIVE src

COMPONENT guide_simple = Guide(
    w1 = 0.1, h1 = 0.1, w2 = 0.1, h2 = 0.1, l = LENGTH, R0 = 0.99,
    Qc = 0.0219, alpha = 6.07, m = 2, W = 0.003)
AT (0, 0, 1) RELATIVE src

COMPONENT s2=Scatter_logger_stop(logger="s1")
AT(0,0,0) RELATIVE PREVIOUS

/*The iterator test code*/

COMPONENT a0=Arm()
AT(0,0,0) ABSOLUTE

COMPONENT iter1 = Scatter_log_iterator()
AT(0,0,0) ABSOLUTE

COMPONENT mnd1=Monitor_nD (
  restore_neutron=1, yheight=LENGTH, radius=M_SQRT2*0.1,options="cylinder previous no slit y", filename="mnd1.dat")
AT(0,0,LENGTH/2.0) RELATIVE guide_simple
ROTATED (90,0,0) RELATIVE guide_simple

COMPONENT mnd2=Monitor_nD (
  restore_neutron=1, yheight=1, radius=M_SQRT2*0.1,options="cylinder previous no slit y,l limits=[0 10]", filename="mnd2.dat")
AT(0,0,LENGTH-0.5) RELATIVE guide_simple
ROTATED (90,0,0) RELATIVE guide_simple

/*COMPONENT printout = Arm()*/
/*AT(0,0,0) ABSOLUTE*/
/*EXTEND*/
/*%{*/
  /*print the neutron state*/
/*  printf("SCATTERLOG_ITERATOR: %llu %g %g %g  %g %g %g  %g  %g %g %g  %g  %d\n", \*/
/*	     mcget_run_num(),x,y,z, vx, vy, vz, t, \*/
/*	     sx, sy, sz, p, INDEX_CURRENT_COMP);*/
/*%}*/
/**/

COMPONENT iter2 = Scatter_log_iterator_stop(iterator="iter1")
AT(0,0,0) RELATIVE iter1

COMPONENT a1 = Arm()
AT (0,0,0) ABSOLUTE
JUMP a0 WHEN(COMP_GETPAR(iter2, loop))

COMPONENT axxx=Arm()
At(0,0,12) ABSOLUTE

END
