/*******************************************************************************
*         McStas instrument definition URL=http://www.mcstas.org
*
* Instrument: Bispectral
*
* %Identification
* Written by: Mads Bertelsen (mads.bertelsen@ess.eu)
* Date: October 2025
* Origin: ESS
* %INSTRUMENT_SITE: ESS
*
* Example of bispectral extraction on ESS Butterfly moderator using Union components
*
* %Description
* Bispectral switch conisting of Si blades with supermirror coating and Cu walls,
* with different coatings. Materials are simulated using NCrystal.
*
* Example: mcrun Bispectral.instr l_min=0.5 l_max=6.0
*
* %Parameters
*
* l_min: [AA]  Shortest simulated wavelength
* l_max: [AA]  Longest simulated wavelength
*
* %Example: l_min=0.5 l_max=6.0 Detector: monitor_L_I=5.10776e+11
*
* %Link
* A reference/HTML link for more information
*
* %End
*******************************************************************************/

DEFINE INSTRUMENT Bispectral(
l_min = 0.5, 
l_max = 6.0
)

DECLARE 
%{
%}

INITIALIZE 
%{
%}

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

COMPONENT Source = ESS_butterfly(
 sector = "S", beamline = 2,
 yheight = 0.03, cold_frac = 0.5, 
 target_index = 2,
 focus_xw = 0.07, focus_yh = 0.04, 
 t_performance = 1, c_performance = 1, 
 Lmin = l_min, Lmax = l_max, 
 n_pulses = 1, acc_power = 5)
AT (0,0,0) RELATIVE Origin

COMPONENT Start_of_bi = Arm()
AT (0.026,0,2.0306) RELATIVE Source
	 
COMPONENT focus_center = Arm()
AT (-0.008,0,0) RELATIVE Start_of_bi

COMPONENT init = Union_init()
AT (0,0,0) ABSOLUTE

// Union materials, always placed relative to ABSOLUTE 
COMPONENT Cu_total = NCrystal_process(
 cfg = "Cu_sg225.ncmat;temp=10C")
AT (0,0,0) ABSOLUTE

COMPONENT Cu = Union_make_material(
 process_string = "Cu_total", my_absorption = 32.0067,
 refraction_SLD = 6.53513E-06)
AT (0,0,0) ABSOLUTE

COMPONENT Si_total = NCrystal_process(
 cfg = "Si_sg227.ncmat;dir1=@crys_hkl:1,0,0@lab:1,0,0;dir2=@crys_hkl:0,1,0@lab:0,1,0;mos=5.0arcmin;temp=10C")
AT (0,0,0) ABSOLUTE

COMPONENT Si = Union_make_material(
 process_string = "Si_total", my_absorption = 0.85412,
 refraction_SLD = 2.07241E-06)
AT (0,0,0) ABSOLUTE

// Union surfaces 
COMPONENT m2_mirror = Mirror_surface(
 m = 2, alpha=3.0)
AT (0,0,0) ABSOLUTE

COMPONENT m4_mirror = Mirror_surface(
 m = 4, alpha=3.0)
AT (0,0,0) ABSOLUTE

// Union geometries 
COMPONENT Cu_box = Union_box(
  material_string = "Cu", priority = 100,
  xwidth = 0.075, xwidth2 = 0.06,
  yheight = 0.042, zdepth = 0.31)
AT (-0.0037,0,0.1485) RELATIVE Start_of_bi
ROTATED (0,1.4,0) RELATIVE Start_of_bi
	  
COMPONENT Vacuum_box = Union_box(
  material_string = "Vacuum", priority = 101,
  xwidth = 0.075 - 0.016, xwidth2 = 0.06 - 0.016, // Subtracting double wall thickness
  yheight = 0.034, zdepth = 0.31+1E-5, // Slight extra depth to avoid parallel faces
  plus_x_surface = "m4_mirror", // Super-mirror coating on left wall, but not right (right wall can still reflect below critical angle of Vacuum - Cu interface)
  plus_y_surface="m2_mirror", minus_y_surface="m2_mirror") // Super-mirror coating on top/bottom
AT (0,0,0) RELATIVE Cu_box

// Array of Si blades at slight angle, rays from hot source are mainly transmitted, rays from cold source are reflected 
COMPONENT si_blade_0 = Union_box(
 material_string = "Si", priority = 110,
 xwidth = 0.0005, yheight = 0.035,
 zdepth = 0.3, minus_x_surface = "m4_mirror")
AT (-0.021,0,0.1485) RELATIVE Start_of_bi
ROTATED (0,0.74,0) RELATIVE Start_of_bi

COMPONENT si_blade_1 = Union_box(
 material_string = "Si", priority = 111,
 xwidth = 0.0005, yheight = 0.035,
 zdepth = 0.3, minus_x_surface = "m4_mirror")
AT (-0.017625,0,0.1485) RELATIVE Start_of_bi
ROTATED (0,0.74,0) RELATIVE Start_of_bi

COMPONENT si_blade_2 = Union_box(
 material_string = "Si", priority = 112,
 xwidth = 0.0005, yheight = 0.035,
 zdepth = 0.3, minus_x_surface = "m4_mirror")
AT (-0.014250,0,0.1485) RELATIVE Start_of_bi
ROTATED (0,0.74,0) RELATIVE Start_of_bi

COMPONENT si_blade_3 = Union_box(
 material_string = "Si", priority = 113,
 xwidth = 0.0005, yheight = 0.035,
 zdepth = 0.3, minus_x_surface = "m4_mirror")
AT (-0.010875,0,0.1485) RELATIVE Start_of_bi
ROTATED (0,0.74,0) RELATIVE Start_of_bi

COMPONENT si_blade_4 = Union_box(
 material_string = "Si", priority = 114,
 xwidth = 0.0005, yheight = 0.035,
 zdepth = 0.3, minus_x_surface = "m4_mirror")
AT (-0.007500,0,0.1485) RELATIVE Start_of_bi
ROTATED (0,0.74,0) RELATIVE Start_of_bi

COMPONENT si_blade_5 = Union_box(
 material_string = "Si", priority = 115,
 xwidth = 0.0005, yheight = 0.035,
 zdepth = 0.3, minus_x_surface = "m4_mirror")
AT (-0.004125,0,0.1485) RELATIVE Start_of_bi
ROTATED (0,0.74,0) RELATIVE Start_of_bi

COMPONENT si_blade_6 = Union_box(
 material_string = "Si", priority = 116,
 xwidth = 0.0005, yheight = 0.035,
 zdepth = 0.3, minus_x_surface = "m4_mirror")
AT (-0.000750,0,0.1485) RELATIVE Start_of_bi
ROTATED (0,0.74,0) RELATIVE Start_of_bi

COMPONENT si_blade_7 = Union_box(
 material_string = "Si", priority = 117,
 xwidth = 0.0005, yheight = 0.035,
 zdepth = 0.3, minus_x_surface = "m4_mirror")
AT (0.0026250,0,0.1485) RELATIVE Start_of_bi
ROTATED (0,0.74,0) RELATIVE Start_of_bi

COMPONENT si_blade_8 = Union_box(
 material_string = "Si", priority = 118,
 xwidth = 0.0005, yheight = 0.035,
 zdepth = 0.3, minus_x_surface = "m4_mirror")
AT (0.006,0,0.1485) RELATIVE Start_of_bi
ROTATED (0,0.74,0) RELATIVE Start_of_bi

COMPONENT si_blade_9 = Union_box(
 material_string = "Si", priority = 119,
 xwidth = 0.0005, yheight = 0.035,
 zdepth = 0.3, minus_x_surface = "m4_mirror")
AT (0.009375,0,0.1485) RELATIVE Start_of_bi
ROTATED (0,0.74,0) RELATIVE Start_of_bi

COMPONENT si_blade_10 = Union_box(
 material_string = "Si", priority = 120,
 xwidth = 0.0005, yheight = 0.035,
 zdepth = 0.3, minus_x_surface = "m4_mirror")
AT (0.012750,0,0.1485) RELATIVE Start_of_bi
ROTATED (0,0.74,0) RELATIVE Start_of_bi

COMPONENT si_blade_11 = Union_box(
 material_string = "Si", priority = 121,
 xwidth = 0.0005, yheight = 0.035,
 zdepth = 0.3, minus_x_surface = "m4_mirror")
AT (0.016125,0,0.1485) RELATIVE Start_of_bi
ROTATED (0,0.74,0) RELATIVE Start_of_bi

COMPONENT si_blade_12 = Union_box(
 material_string = "Si", priority = 122,
 xwidth = 0.0005, yheight = 0.035,
 zdepth = 0.3, minus_x_surface = "m4_mirror")
AT (0.0195,0,0.1485) RELATIVE Start_of_bi
ROTATED (0,0.74,0) RELATIVE Start_of_bi

COMPONENT master = Union_master(
 enable_refraction=1, enable_reflection=1) // control parameters to enable/disable refraction and reflection effects
AT (0,0,0) ABSOLUTE

COMPONENT stop = Union_stop()
AT (0,0,0) ABSOLUTE

COMPONENT monitor_Div_L = DivLambda_monitor(
 xwidth = 0.045, yheight = 0.04,	  
 maxdiv_h = 2.5, nh = 300,
 Lmin = l_min, Lmax = l_max, nL = 300,
 restore_neutron = 1)
AT (0,0,0.32) RELATIVE Start_of_bi
	 
COMPONENT monitor_L = L_monitor(
 xwidth = 0.045, yheight = 0.04,	  
 Lmin = l_min, Lmax = l_max, nL = 500,
 restore_neutron = 1)
AT (0,0,0.32) RELATIVE Start_of_bi

COMPONENT monitor_PSD = PSD_monitor(
 nx = 300, ny = 300,
 xwidth = 0.06, yheight = 0.06,
 restore_neutron = 1)
AT (0,0,0.32) RELATIVE Start_of_bi

FINALLY 
%{
%}

END
