/*******************************************************************************
*         McStas instrument definition URL=http://www.mcstas.org
*
* Instrument: Test Single Crystal output
*
* %Identification
* Written by: P. Willendrup
* Date: October 1st 2020
* Origin: DTU
* %INSTRUMENT_SITE: Tests_samples
*
* Test output of Single Crystal on a spherical monitor.
*
* %Description
* A test instrument to compare Monitor_nD output against basic 1D and 2D monitors.
*
* %Example: lambda=5 directbeam=0 Detector: Sph_mon_I=7.2e+7
* %Example: lambda=5 directbeam=0 SPLITS=5 Detector: Sph_mon_I=7.2e+7
* %Example: lambda=5 directbeam=0 SPLITS=10 Detector: Sph_mon_I=7.2e+7
* %Example: lambda=5 order=1 directbeam=0 Detector: Sph_mon_I=7.2e+7
* %Example: lambda=5 order=1 directbeam=0 SPLITS=5 Detector: Sph_mon_I=7.2e+7
* %Example: lambda=5 order=1 directbeam=0 SPLITS=10 Detector: Sph_mon_I=7.2e+7
*
* %Parameters
* lambda:     [Angs] Central wavelength emitted from source
* dlambda:    [Angs] Witdth of wavelength spectrom emitted from source
* L1:            [m] Source-sample distance
* directbeam:    [1] Suppress direct beam or not
* reflections: [str] File name for reflection list
* SPLITS:        [1] Number of SPLIT's before sample
* order:         [ ] Maximum order of multiple scattering, Single_crystal
* inc:       [barns] Incoherent cross-section
* trans:         [ ] Fraction of statistics assigned to transmitted beam
* omega:       [deg] Rotation angle of sample
* beamstop:      [ ] Toggle beamstop
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT Test_SX(lambda=5, dlambda=9.8, L1=30, int directbeam=0, beamstop=0, string reflections="YBaCuO.lau", int SPLITS=1, order=0, inc=0, trans=0.001, omega=0)

DECLARE %{
  int DirectBeam;
  #pragma acc declare create(DirectBeam)
%}

USERVARS %{
  int Type;
%}

/* The INITIALIZE section is executed when the simulation starts     */
/* (C code). You may use them as component parameter values.         */
INITIALIZE
%{
  DirectBeam = directbeam;
  #pragma acc update device(DirectBeam)
%}

/* Here comes the TRACE section, where the actual      */
/* instrument is defined as a sequence of components.  */
TRACE

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

/* source with constant flux */
REMOVABLE COMPONENT Source = Source_gen(
    radius = 0.11, dist = L1, focus_xw = 0.01, focus_yh = 0.01,
    lambda0 = lambda, dlambda = dlambda/2.0,
    T1=229.6,I1=5.32e13,T2=1102, I2=4.37e12, T3=437.1,I3=3.84e13)
  AT (0, 0, 0) RELATIVE Origin

/* Incoming beam */ 
COMPONENT PSD = PSD_monitor(filename="incoming",xwidth=0.02, yheight=0.02)
  AT (0, 0, L1-0.02) RELATIVE Origin

/* TIP: monochromator cradle */ 
COMPONENT sample_cradle = Arm()
  AT (0, 0, L1) RELATIVE Origin

SPLIT SPLITS COMPONENT SX = Single_crystal(xwidth=0.01, yheight=0.01, zdepth=0.01,reflections=reflections, mosaic=10, delta_d_d=1e-4, order=order, sigma_inc=inc, p_transmit=trans)
AT (0, 0, 0) RELATIVE sample_cradle
  ROTATED (0,omega,0) RELATIVE sample_cradle
EXTEND %{
  Type=itype;
  if (DirectBeam==0) {
    if(!SCATTERED) ABSORB;
  }
%}

COMPONENT Full_mon = PSD_monitor_4PI(nx=100,ny=100, radius=1, restore_neutron=1, filename="FULL")
  AT (0, 0, 0) RELATIVE sample_cradle

COMPONENT Sph_tun = PSD_monitor_4PI(nx=100,ny=100, radius=1, restore_neutron=1, filename="tun")
  WHEN (Type==1) AT (0, 0, 0) RELATIVE sample_cradle

COMPONENT PSDBS = PSD_monitor(filename="BS0",xwidth=0.04, yheight=0.04, restore_neutron=1)
  WHEN(beamstop) AT (0, 0, 0.5) RELATIVE sample_cradle

COMPONENT BeamStop = Beamstop(radius=0.1)
  WHEN (beamstop) AT (0,0,0.5) RELATIVE sample_cradle

COMPONENT Sph_mon = PSD_monitor_4PI(nx=100,ny=100, radius=1, restore_neutron=1, filename="Tot")
  AT (0, 0, 0) RELATIVE sample_cradle

COMPONENT Sph_coh = PSD_monitor_4PI(nx=100,ny=100, radius=1, restore_neutron=1, filename="coh")
  WHEN (Type==3) AT (0, 0, 0) RELATIVE sample_cradle

COMPONENT Sph_inc = PSD_monitor_4PI(nx=100,ny=100, radius=1, restore_neutron=1, filename="inc")
  WHEN (Type==2) AT (0, 0, 0) RELATIVE sample_cradle

/* The END token marks the instrument definition end */
END
