/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2008, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Instrument: templateSANS2_Mantid
*
* %Identification
* Written by: Peter Willendrup, Wim G. Bouwman
* Date: Dec 20190
* Origin: <a href="http://www.fysik.dtu.dk">DTU</a>
* %INSTRUMENT_SITE: Mantid
*
* Test instrument for the SANS_spheres2 component. No guide / velocity selector.
*
* %Description
* Very simple test instrument for the SANS_spheres2 component, derived / simplified from 
* H. Frielinghaus SANS_benchmark2
*
* %Example: lambda=6 Detector: detectorSANS_I=0.201795
* %Example: lambda=6 frac_dir=0.4 Detector: detectorDB_I=66.6933
* %Example: lambda=6 --format=NeXus Detector: detectorSANS_I=0.201795
* %Example: lambda=6 frac_dir=0.4 --format=NeXus Detector: detectorDB_I=66.6933
*
* %Parameters
* lambda:       [AA]  Mean wavelength of neutrons
* dlambda:      [AA]  Wavelength spread of neutrons
* r:            [AA]  Radius of scattering hard spheres
* PHI:           [1]  Particle volume fraction
* Delta_Rho: [cm^-2]  Scattering length density
* frac_dir:      [1]  Fraction of statistics for direct beam
* frac_inc:      [1]  Fraction of statistics for incoherent scattering in scattered beam
*
* %Link
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT templateSANS2_Mantid(lambda=6, dlambda=0.05, r=150, PHI=1e-3, Delta_Rho=6e10, frac_dir=0.03, frac_inc=0.01)
DEPENDENCY " @NEXUSFLAGS@ "

USERVARS %{
  int was_scattered;
%}
  
TRACE

COMPONENT a1 = Progress_bar()
  AT (0,0,0) ABSOLUTE

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

COMPONENT sourceMantid = Source_simple(
    radius = 0.02, dist = 6, focus_xw = 0.01, focus_yh = 0.01,
    lambda0 = lambda, dlambda = dlambda, flux = 1e8)
  AT (0, 0, 0) RELATIVE arm

COMPONENT coll1 = Slit(
    radius = 0.005)
  AT (0, 0, 3) RELATIVE arm

COMPONENT coll2 = Slit(
    radius = 0.005)
  AT (0, 0, 6) RELATIVE arm

COMPONENT sample_position = Arm()
AT (0,0,0.2) RELATIVE coll2

COMPONENT Sample_in = PSD_monitor(xwidth=0.02,yheight=0.02, filename="SamplePos",restore_neutron=1)
  AT(0,0,0) RELATIVE PREVIOUS  

SPLIT COMPONENT sampleMantid = SANS_spheres2(xwidth=0.02, yheight=0.02, zthick=0.001, sc_aim=(1-frac_dir), sans_aim=(1-frac_inc), R=r)
   AT (0,0,0.2) RELATIVE coll2
EXTEND %{
  was_scattered=SCATTERED;
%}

/* PSD, scattered and direct beam */
COMPONENT detector = PSD_monitor(
    nx = 128, ny = 128, filename = "PSD.dat", xmin = -0.3,
    xmax = 0.3, ymin = -0.3, ymax = 0.3)
  AT (0, 0, 3) RELATIVE sample_position

/* PSD, scattered beam only */
COMPONENT detectorSANS = PSD_monitor(
    nx = 128, ny = 128, filename = "SANS.dat", xmin = -0.3,
    xmax = 0.3, ymin = -0.3, ymax = 0.3)
  WHEN (was_scattered)  AT (0, 0, 3) RELATIVE sample_position
  
/* PSD, direct beam only */
COMPONENT detectorDB = PSD_monitor(
    nx = 128, ny = 128, filename = "Direct.dat", xmin = -0.3,
    xmax = 0.3, ymin = -0.3, ymax = 0.3)
  WHEN (!was_scattered)  AT (0, 0, 3) RELATIVE sample_position

/* WL monitor */
COMPONENT Ldetector = L_monitor(
    nL = 1000, filename = "Edet.dat", xmin = -0.3,
    xmax = 0.3, ymin = -0.3, ymax = 0.3, Lmin = 5.5,
    Lmax = 6.5)
  AT (0, 0, 3.01) RELATIVE sample_position

/* Radial average, scattered beam only */
COMPONENT PSDrad = PSD_monitor_rad(
    filename = "psd2.dat", filename_av = "psd2_av.dat", rmax = 0.3)
  WHEN(was_scattered)  AT (0, 0, 3.02) RELATIVE sample_position

/* Radial average, scattered and direct beam */
COMPONENT PSDrad_full = PSD_monitor_rad(
    filename = "psd2_full.dat", filename_av = "psd2_av_full.dat", rmax = 0.3)
  AT (0, 0, 3.02) RELATIVE sample_position

COMPONENT nD_Mantid_1 = Monitor_nD(
    options ="mantid square x limits=[-0.3 0.3] bins=128 y limits=[-0.3 0.3] bins=128, neutron pixel min=0 t, list all neutrons",
    xmin = -0.3,
    xmax = 0.3,
    ymin = -0.3,
    ymax = 0.3,
    restore_neutron = 1,
    filename = "bank01_events.dat")
AT (0, 0, 3.2) RELATIVE sampleMantid

END
