/*******************************************************************************
*         McXtrace instrument definition URL=http://www.mcxtrace.org
*
* Instrument: Test_PowderN
*
* %Identification
* Written by: Erik B Knudsen (erkn@fysik.dtu.dk)
* Date: 2009
* Origin: DTU Physics
* Version: 1.0
* %INSTRUMENT_SITE: Tests_samples
*
* Unit-test instrument for the PowderN sample component.
*
* %Description
* Simply a model source illuminating a powder sample.
* The default sample itself is an LaB6-powder. 
* Alternatively, the Single_crystal component can also be tested in powder mode.
*
* %Example: Test_PowderN.instr reflections="Fe_bcc229_lt13deg.dat" -n1e7 TTH=13.4 d_phi=45 frac_i=0 Detector: Sph_mon_I=2.68295e-14
* %Example: Test_PowderN.instr E0=15 Detector: Sph_mon_I=6.24257e-12
* %Example: Test_PowderN.instr E0=15 d_phi=10 Detector: Sph_mon_I=6.59782e-13
* %Example: Test_PowderN.instr E0=15 directbeam=1 SPLITS=2 Detector: Sph_mon_I=7.94366e-10
* %Example: Test_PowderN.instr E0=15 directbeam=1 SPLITS=5 Detector: Sph_mon_I=7.94366e-10
* %Example: Test_PowderN.instr E0=15 directbeam=1 SPLITS=10 Detector: Sph_mon_I=7.94366e-10
* %Example: Test_PowderN.instr E0=15 directbeam=1 SPLITS=20 Detector: Sph_mon_I=7.94366e-10
*
* %Parameters
* E0:          [keV]  Source energy (width 1 keV)
* L1:          [m]    Source-sample distance
* directbeam:  [1]    Suppress direct beam (0) or not (1)
* reflections: [str]  List of powder reflections, LAU/CIF format.
* SPLITS:      [1]    Number of SPLIT's before sample
* frac_c:      [1]    Fraction of stats assigned to coherent scattering
* frac_i:      [1]    Fraction of stats assigned to incoherent scattering
* frac_t:      [1]    Fraction of stats assigned to unscattered, "direct beam"
* TTH:         [deg]  Rotation of secondary detector arm.
* d_phi:       [deg]  Angle corresponding to the vertical angular range to focus to, e.g. detector height. 0 for no focusing.
* index:       [1]    Index of the sample component to use. 1=PowderN, 2=Single_crystal
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT Test_PowderN(E0=15, L1=10, int directbeam=0, string reflections="LaB6_660b_AVID2.hkl", 
  int SPLITS=1, frac_c=0.8, frac_i=0.1, frac_t=0.1, d_phi=0, TTH=0, int index=1)

TRACE

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

COMPONENT src = Source_flat(
    yheight = 1e-3, xwidth = 1e-3, dist = 10, focus_xw = 1e-3,
    focus_yh = 1e-3, E0 = E0, dE = 1)
  AT (0, 0, 0) RELATIVE Origin

SPLIT SPLITS COMPONENT sample_cradle = Arm()
  AT (0, 0, L1) RELATIVE PREVIOUS

COMPONENT Pow = PowderN(
    radius=0.5e-4, yheight=1e-3, reflections=reflections,format={ 4,5,7,0,0,0,0,0 },
    p_inc=frac_i, p_transmit=frac_t, p_interact=frac_c,
    d_phi=d_phi, pack = 0.5, Vc = 123)
WHEN (index == 1)
AT (0, 0, 0) RELATIVE sample_cradle
EXTEND %{
  if(INSTRUMENT_GETPAR(directbeam) == 0) {
    if (!SCATTERED) {
      ABSORB;
    }
  }
%}

COMPONENT SX = Single_crystal(
    radius=0.5e-4, yheight=1e-3, reflections=reflections,powder=1,
    p_transmit=frac_t, mosaic=5, sigma_inc=-1)
WHEN (index == 2)
AT (0, 0, 0) RELATIVE sample_cradle
EXTEND %{
  if(INSTRUMENT_GETPAR(directbeam) == 0) {
    if (!SCATTERED) {
      ABSORB;
    }
  }
%}


COMPONENT Sph_mon = PSD_monitor_4PI(nx=200,ny=200, radius=1, restore_xray=1, filename="Sphere")
  AT (0, 0, 0) RELATIVE PREVIOUS

COMPONENT ttharm= Arm()
AT(0,0,0) RELATIVE Pow
ROTATED (0,TTH,0) RELATIVE Pow

COMPONENT detector2 = PSD_monitor(
    nx=200, ny=200, xwidth=2e-3, yheight=1e-2, filename="psd2",restore_xray=1
)
AT(0,0,0.1) RELATIVE ttharm

// ideal "banana" detector
COMPONENT det_angle = Monitor_nD(options="abs theta limits=[5 90]",
  radius=0.6, yheight=1e-2, bins=10000)
AT (0,0,0) RELATIVE Pow


END
