/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2023, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Instrument: templateSasView_Mantid
*
* %Identification
* Written by: Peter Willendrup and Torben Nielsen
* Date: 25 Jan 2016
* Origin: DTU, European Spallation Source ERIC
* Modified by: Based on templatesSANS.instr by Kim Lefmann
* %INSTRUMENT_SITE: Mantid
*
* Test instrument for the SasView_model component generating event data for Mantid. No guide / velocity selector
* etc.
*
* %Description
* Very simple test instrument for the SasView_model component.
*
* Modified to show a proof of concept method for storing a 'Mantid friendly' type of NeXus file.
*
* Example: model_index=1 Ncount=1e6 par1=4 par2=1 par3=40 par4=20 par5=400 Detector: detector_I=5e6
* Example: model_index=3 Ncount=1e6 par1=220 par2=0.06 par3=40 par4=4 par5=1 Detector: detector_I=3.3e4
* Example: model_index=47 Ncount=1e6 par1=4 par2=2 par3=35 par4=75 par5=400 Detector: detector_I=2.5e6
*
* Needed steps (McStas):
* 1) Compile your instrument with NeXus library and ISO c99 support
* 2) Generate an IDF using mcdisplay templateSANS_Mantid --format=Mantid -n0
* 3) mcrun templateSasView_Mantid -n1e6 --format=NeXus
*
* Needed steps (Mantid):
* a) Load McStas NeXus file
* b) Run Mantid algorithm: 'ConvertUnits' using the 'wavelenth' and 'elastic' options
* c) Run Mantid algorithm: 'Qxy' using the options 'MaxQxy=0.6', 'DeltaQ=0.003', 'SolidAngleWeighting=False'
*
* %Parameters
* lambda: [AA]   Mean wavelength of neutrons
* dlambda: [AA]  Wavelength spread of neutrons
* model_index: [ ] SASmodels model index (see SasView_model.comp mcdoc page)
* par1: [ ] Slot 1 in SASmodel parameter vector
* par2: [ ] Slot 2 in SASmodel parameter vector
* par3: [ ] Slot 3 in SASmodel parameter vector
* par4: [ ] Slot 4 in SASmodel parameter vector
* par5: [ ] Slot 5 in SASmodel parameter vector
* par6: [ ] Slot 6 in SASmodel parameter vector
* par7: [ ] Slot 7 in SASmodel parameter vector
* par8: [ ] Slot 8 in SASmodel parameter vector
*
* %Link
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT templateSasView_Mantid(lambda=6, dlambda=0.05, model_index=47, par1=0, par2=0, par3=0, par4=0, par5=0, par6=0, par7=0, par8=0, Ncount=0)
DEPENDENCY " @NEXUSFLAGS@ "

DECLARE %{
%}

INITIALIZE %{

  if (Ncount>0)
    mcset_ncount(Ncount);

%}

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 = 3, 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 LdetectorPRE = L_monitor(
    nL = 1000, filename = "Edet1.dat", xmin = -0.3,
    xmax = 0.3, ymin = -0.3, ymax = 0.3, Lmin = 5.5,
    Lmax = 6.5)
AT (0,0,0.05) RELATIVE coll2

SPLIT COMPONENT sampleMantid = Arm()
  AT (0, 0, 0.2) RELATIVE coll2

COMPONENT sample1= SasView_barbell(model_scale=1,model_abs=0.0,
				   sld=par1,sld_solvent=par2,radius_bell=par3,radius=par4,length=par5,
				      xwidth=0.01, yheight=0.01, zdepth=0.005,focus_xw=0.6, focus_yh=0.6, target_index=1)
WHEN (model_index==1) AT (0,0,0.2) RELATIVE coll2
EXTEND %{
  if (!SCATTERED) ABSORB;
%}

COMPONENT sample3= SasView_bcc_paracrystal(model_scale=1,model_abs=0.0,
					   dnn=par1, d_factor=par2, radius=par3, sld=par4, sld_solvent=par5,
				      xwidth=0.01, yheight=0.01, zdepth=0.005,focus_xw=0.6, focus_yh=0.6, target_index=1)
WHEN (model_index==3) AT (0,0,0.2) RELATIVE coll2
EXTEND %{
  if (!SCATTERED) ABSORB;
%}

COMPONENT sample47= SasView_parallelepiped(model_scale=1,model_abs=0.0,
				      sld=par1,sld_solvent=par2,length_a=par3,length_b=par4,length_c=par5,
				      xwidth=0.01, yheight=0.01, zdepth=0.005,focus_xw=0.6, focus_yh=0.6, target_index=1)
WHEN (model_index==47) AT (0,0,0.2) RELATIVE coll2
EXTEND %{
  if (!SCATTERED) ABSORB;
%}

COMPONENT detector = PSD_monitor(
    nx = 128, ny = 128, filename = "PSD.dat", xmin = -1.5,
    xmax = 1.5, ymin = -1.5, ymax = 1.5)
  AT (0, 0, 3) RELATIVE sampleMantid

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 sampleMantid


COMPONENT PSDrad = PSD_monitor_rad(
    filename = "psd2.dat", filename_av = "psd2_av.dat", rmax = 0.3)
  AT (0, 0, 3.02) RELATIVE sampleMantid

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

END
