/*******************************************************************************
* Instrument: Unittest_SPLIT
*
* %I
* Written by: Peter Willendrup
* Date: Oct 28th, 2021
* Origin: DTU
* %INSTRUMENT_SITE: Tests_grammar
*
* SPLIT unittest including samples
*
* %D
* SPLIT unittest.
* 
* One unit of intensity is emitted from a 1x1 m.
*
*
* %Example: SAMPLE=1 SPLITS=1 Detector: PSD_I=1
* %Example: SAMPLE=1 SPLITS=10 Detector: PSD_I=10
* %Example: SAMPLE=1 SPLITS=100 Detector: PSD_I=100
* %Example: SAMPLE=2 SPLITS=1 Detector: PSD_I=1
* %Example: SAMPLE=2 SPLITS=10 Detector: PSD_I=10
* %Example: SAMPLE=2 SPLITS=100 Detector: PSD_I=100
* %Example: SAMPLE=3 SPLITS=1 Detector: PSD_I=0.999001
* %Example: SAMPLE=3 SPLITS=1 Detector: PSD_transmission_I=0.00071068
* %Example: SAMPLE=3 SPLITS=1 Detector: PSD_scattered_I=0.00182805
* %Example: SAMPLE=3 SPLITS=10 Detector: PSD_I=9.99001
* %Example: SAMPLE=3 SPLITS=10 Detector: PSD_transmission_I=0.007122
* %Example: SAMPLE=3 SPLITS=10 Detector: PSD_scattered_I=0.0183156
* %Example: SAMPLE=3 SPLITS=100 Detector: PSD_I=99.9001
* %Example: SAMPLE=3 SPLITS=100 Detector: PSD_transmission_I=0.070845
* %Example: SAMPLE=3 SPLITS=100 Detector: PSD_scattered_I=0.182056
*
* %P
* SPLITS: [1] Number of SPLIT to do
* Pp0:   [1] Dummy input parameter used internally
*
* %L
* <reference/HTML link>
*
* %E
*******************************************************************************/
DEFINE INSTRUMENT Unittest_SPLIT_sample(int SAMPLE=1, int SPLITS=10, Pp0=1)

DECLARE
%{
%}

USERVARS
%{
%}

INITIALIZE
%{
  Pp0=1.0/mcget_ncount();
%}

TRACE

COMPONENT Arm = Arm()
  AT (0,0,0) ABSOLUTE
EXTEND %{
  // position x in one of 100 bins
  x = randpm1()*0.0005;
  y = randpm1()*0.0005;
  if ( INSTRUMENT_GETPAR(SAMPLE) == 3) {
    vz=200+rand01()*4400;
  } else
    vz= 2200;
  p = 1; 
%}

SPLIT SPLITS COMPONENT Slit = Arm()
  AT (0,0,0) ABSOLUTE
 

COMPONENT Incoh = Incoherent(radius=0.01,focus_r=1, target_z=1)
  WHEN (SAMPLE==1) AT (0,0,0.1) RELATIVE Slit
EXTEND %{
  if(!SCATTERED) ABSORB;
  p = INSTRUMENT_GETPAR(Pp0);
%}

COMPONENT Powder = PowderN(radius=0.01,reflections="Al.lau",p_inc=0.01,p_transmit=0.01)
  WHEN (SAMPLE==2) AT (0,0,0.1) RELATIVE Slit
EXTEND %{
  if(!SCATTERED) ABSORB;
  p = INSTRUMENT_GETPAR(Pp0);
%}

COMPONENT SX = Single_crystal(radius=0.01,reflections="Al.lau",mosaic=20,order=1)
  WHEN (SAMPLE==3) AT (0,0,0.1) RELATIVE Slit
EXTEND %{
  if(!SCATTERED) ABSORB;
  p = INSTRUMENT_GETPAR(Pp0);
%}


COMPONENT PSD = PSD_monitor_4PI(radius=1, filename="splits", nx=100, ny=100, restore_neutron=1)
 AT (0,0,0) RELATIVE Incoh
	 
COMPONENT PSD_transmission = PSD_monitor(xwidth=0.01, yheight=0.01, filename="splits_transmission", nx=100, ny=100, restore_neutron=1)
 AT (0,0,0.1) RELATIVE Incoh
	  
COMPONENT detector_direction_1 = Arm()
 AT (0,0,0) RELATIVE Incoh
 ROTATED (0, 29.65, 0) RELATIVE Incoh
	   
COMPONENT detector_direction_2 = Arm()
 AT (0,0,0) RELATIVE Incoh
 ROTATED (26.3, 0, 0) RELATIVE detector_direction_1
	   
COMPONENT PSD_scattered = PSD_monitor(xwidth=0.02, yheight=0.02, filename="splits_scattered", nx=100, ny=100, restore_neutron=1)
 AT (0,0,0.5) RELATIVE detector_direction_2
  
FINALLY
%{
%}

END
