/*******************************************************************************
*
* McStas, neutron ray-tracing package
*
* Instrument: Test_MCViNE_SANS2D_ongrid
*
* %Identification
* Written by: Fahima Islam
* Date: 2026-09-29
* Origin: MCViNE (https://github.com/mcvine/mcvine) kernels ported to McStas
* %INSTRUMENT_SITE: Tests_samples
*
* Test of the MCViNE SANS2D_ongrid kernel (SANS from a 2D S(Qx,Qy) map), as a Union process and as a standalone sample
*
* %Description
* Monochromatic beam on a small sample described by the MCViNE SANS2D_ongrid kernel.
* comp_select=1 uses the Union process MCViNE_SANS2D_ongrid_process (geometry, absorption
* and multiple scattering by Union_master); comp_select=2 uses the standalone
* component MCViNE_SANS2D_ongrid with the same geometry, absorption and up to 20 orders of
* scattering. The two should give the same scattered intensity (see the two
* example lines below).
*
* %Example: comp_select=1 Detector: detector_I=9.72217e-07
* %Example: comp_select=2 Detector: detector_I=9.42522e-07
*
* %Parameters
* comp_select: [1] 1: Union process MCViNE_SANS2D_ongrid_process in Union_master, 2: standalone MCViNE_SANS2D_ongrid
* Ei: [meV] Incident energy
* sample_rotation: [deg] Rotation of the sample frame about the vertical axis
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT Test_MCViNE_SANS2D_ongrid(comp_select=1, Ei=60, sample_rotation=0)

DECLARE
%{
  double Ttgt;
%}

INITIALIZE
%{
  /* arrival time at a target 3 m from the sample for Ef = 40 meV (used by DGSSXRes) */
  Ttgt = 5.0 / (SE2V * sqrt (Ei)) + 3.0 / (SE2V * sqrt (40.0));
%}

TRACE

COMPONENT sample_process = MCViNE_SANS2D_ongrid_process(S_QxQy="MCViNE/SANS2D_example.dat", scattering_coefficient=10)
  AT (0, 0, 0) ABSOLUTE ROTATED (0, sample_rotation, 0) ABSOLUTE

COMPONENT sample_material = Union_make_material(my_absorption=2, process_string="sample_process")
  AT (0, 0, 0) ABSOLUTE

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

COMPONENT source = Source_simple(radius=0.005, dist=5, focus_xw=0.01, focus_yh=0.01, E0=Ei, dE=0.01*Ei)
  AT (0, 0, 0) ABSOLUTE

COMPONENT sample_union = Union_box(xwidth=0.01, yheight=0.01, zdepth=0.01, priority=1, material_string="sample_material", p_interact=0.9)
  WHEN (comp_select == 1)
  AT (0, 0, 5) ABSOLUTE

COMPONENT master = Union_master()
  WHEN (comp_select == 1)
  AT (0, 0, 5) ABSOLUTE

COMPONENT sample_standalone = MCViNE_SANS2D_ongrid(S_QxQy="MCViNE/SANS2D_example.dat", scattering_coefficient=10, absorption_coefficient=2, order=20, xwidth=0.01, yheight=0.01, zdepth=0.01)
  WHEN (comp_select == 2)
  AT (0, 0, 5) ABSOLUTE ROTATED (0, sample_rotation, 0) ABSOLUTE


COMPONENT beamstop = Beamstop(radius=0.02)
  AT (0, 0, 9-0.01) ABSOLUTE
COMPONENT detector = Monitor_nD(options="x bins=100, y bins=100", xwidth=0.6, yheight=0.6, restore_neutron=1, filename="sans_xy")
  AT (0, 0, 9) ABSOLUTE

END
