/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2008, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Instrument: Union_test_box
*
* %Identification
* Written by: Mads Bertelsen
* Date: September 2015
* Origin: University of Copenhagen
* %INSTRUMENT_SITE: Tests_union
*
* Simple test instrument for Union box component.
*
* %Description
* One of the union geometries that can be used in conjuction
* with the others to create a complex geometry. The priority needs to
* be set, and when two or more geometries overlap, the one with highest
* priority determines what material is simulated in that region.
*
* %Example: Detector: m4pi_both_I=4.8716e-10
*
* %End
*******************************************************************************/

DEFINE INSTRUMENT Test_box()

DECLARE
%{
int sample_1_index = 3;
int sample_2_index = 4;
%}

USERVARS %{
int scattered_1;
int scattered_2;
%}

TRACE

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

COMPONENT test_incoherent = Incoherent_process(sigma=20,packing_factor=1,unit_cell_volume=66.4)
AT (0,0,0) ABSOLUTE

COMPONENT test_material = Union_make_material(my_absorption=4)
AT (0,0,0) ABSOLUTE

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

COMPONENT source = Source_div(
        xwidth=0.0005, yheight=0.0005,focus_aw=3, focus_ah=2,
        E0 = 10,
        dE = 0)
  AT (0,0,0) RELATIVE a1 ROTATED (0,0,0) RELATIVE a1

// Sample position
COMPONENT beam_center = Arm()
AT (0,0,1.4) RELATIVE a1
ROTATED (0,0,0) RELATIVE a1

COMPONENT tube_point = Arm()
AT (0,0,0) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

// V1
COMPONENT box_tube1 = Union_box(xwidth=0.1, yheight=0.15, xwidth2=0.03, yheight2=0.03, zdepth=0.045, priority=5, material_string="test_material")
AT (0,0,0) RELATIVE tube_point
ROTATED (0,0,0) RELATIVE tube_point

// V2
COMPONENT cylinder_tube1 = Union_cylinder(radius = 0.013,yheight = 0.11,priority=6,material_string="Vacuum")
AT (0,0,0) RELATIVE tube_point
ROTATED (0,0,0) RELATIVE tube_point

// V3
COMPONENT cylinder_cross1 = Union_cylinder(radius = 0.003,yheight = 0.11,priority=7,material_string="test_material")
AT (0,0,0) RELATIVE tube_point
ROTATED (90,0,90) RELATIVE tube_point

// V4
COMPONENT cylinder_cross2 = Union_cylinder(radius = 0.003,yheight = 0.05,priority=8,material_string="test_material")
AT (0,0,0) RELATIVE tube_point
ROTATED (90,0,0) RELATIVE tube_point

// V5
COMPONENT box_totem1 = Union_box(xwidth=0.03,yheight=0.03,zdepth=0.03,priority=9,material_string="test_material")
AT (0,-0.01,-0.07) RELATIVE beam_center
ROTATED (45,0,45) RELATIVE beam_center

// V6
COMPONENT box_totem2 = Union_box(xwidth=0.03,yheight=0.03,zdepth=0.03,priority=10,material_string="test_material")
AT (0,0.01,-0.065) RELATIVE beam_center
ROTATED (0,0,0) RELATIVE beam_center

// V7
COMPONENT box_totem3 = Union_box(xwidth=0.03,yheight=0.03,zdepth=0.03,priority=11,material_string="test_material")
AT (0,0.03,-0.06) RELATIVE beam_center
ROTATED (45,0,45) RELATIVE beam_center

COMPONENT test_sample = Union_master()
AT(0,0,0) RELATIVE beam_center
ROTATED(0,0,0) RELATIVE beam_center
EXTEND
%{
	if (scattered_flag[sample_1_index] > 0) scattered_1 = 1; else scattered_1 = 0;
	if (scattered_flag[sample_2_index] > 0) scattered_2 = 1; else scattered_2 = 0;
%}


COMPONENT m4pi = PSD_monitor_4PI(radius=1, nx=180, ny=180, filename="Events",restore_neutron=1)
  AT (0, 0, 0) RELATIVE beam_center

COMPONENT m4pi_1 = PSD_monitor_4PI(radius=1, nx=180, ny=180, filename="Events_1",restore_neutron=1) WHEN (scattered_1)
  AT (0, 0, 0) RELATIVE beam_center

COMPONENT m4pi_2 = PSD_monitor_4PI(radius=1, nx=180, ny=180, filename="Events_2",restore_neutron=1) WHEN (scattered_2)
  AT (0, 0, 0) RELATIVE beam_center

COMPONENT arm_1 = Arm()
  AT (0, 0, 0) RELATIVE beam_center
EXTEND
%{
	if (scattered_1 + scattered_2 != 2) ABSORB;
%}

COMPONENT m4pi_both = PSD_monitor_4PI(radius=1, nx=180, ny=180, filename="Events_12") WHEN (scattered_1 && scattered_2)
  AT (0, 0, 0) RELATIVE beam_center

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


END
