/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2008, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Instrument: incoherent_validation
*
* %Identification
* Written by: Mads Bertelsen
* Date: May 2016
* Origin: Johns Hopkins University, Baltimore
* %INSTRUMENT_SITE: Union_demos
*
* %Description
*  Validation of Union single crystal against standard single_crystal.
*
* %Example: comp_select=1 Detector: Banana_monitor_I=9.5e5
* %Example: comp_select=1 Detector: PSDlin_transmission_scattered_I=793256
* %Example: comp_select=2 Detector: PSDlin_transmission_scattered_I=795548
*
* %Parameters
* comp_select: [1] 1: Union components, 2: Single crystal
* material_data_file: [string] Sample material defintion
* sigma_inc: [barns] Sample incoherent cross-section       
* my_absorption_union: [m^-1] Sample mass-absorption coefficient     
* delta_d_d: [] Sample latttice spacing variation
* mosaic: [arcmin] Sample mosaicity 
* lam0: [AA] Source mean wavelength                    
* dlam: [AA] Source wavelength spread
* xwidth: [m] Sample x-dimension 
* yheight: [m] Sample y-dimension       
* zdepth: [m] Sample z-dimension                  
* unit_cell_volume: [AA] Sample unit cell volume       
* sigma_abs_sc: [banrs] Sample absorption cross-section
* x_rotation_geometry: [deg] Rotates union geometry component around x
* y_rotation_geometry: [deg] Rotates union geometry component around y
* x_rotation_geometry_ref: [deg] Rotates reference component around x
* y_rotation_geometry_ref: [deg] Rotates reference component around y
* x_rotation_process: [deg] Rotates crystal process (crystal orientation), x    
* y_rotation_process: [deg] Rotates crystal process (crystal orientation), y
* geometry_interact: [deg] Rotates crystal process (crystal orientation), z
* PG: [ ] Sample PG-mode                      
* powder: [] Sample powder-mode      
*
* %End
*******************************************************************************/

/* Change name of instrument and input parameters with default values */
DEFINE INSTRUMENT Union_single_crystal_validation(comp_select=1,string material_data_file="YBaCuO.lau",sigma_inc=2.105, my_absorption_union=8.55, delta_d_d=1e-4, mosaic=5, lam0=7, dlam=5 , xwidth=0.01, yheight=0.01, zdepth=0.01,unit_cell_volume=173.28,sigma_abs_sc=0,sigma_inc=2.105,x_rotation_geometry=0,y_rotation_geometry=0,x_rotation_geometry_ref=0,y_rotation_geometry_ref=0,x_rotation_process=0,y_rotation_process=0,geometry_interact=0,PG=0,powder=0)

USERVARS
%{
int scattered_flag_instr;
%}

TRACE

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

COMPONENT Single_crystal_test_process = Single_crystal_process(
          delta_d_d=delta_d_d, mosaic=mosaic,
          ax = 3.8186, ay = 0,      az = 0,
          bx = 0,      by = 3.8843, bz = 0,
          cx = 0,      cy = 0,      cz = 11.6777,
          reflections=material_data_file, barns=0, packing_factor=1, powder=powder,PG=PG)
AT (0,0,0) ABSOLUTE
ROTATED (x_rotation_process, y_rotation_process, 0) ABSOLUTE

  COMPONENT Incoherent_process = Incoherent_process(sigma=sigma_inc, packing_factor=1, unit_cell_volume=173.28, interact_fraction=-1)
    AT (0,0,0) ABSOLUTE

// With manual linking the result depend on the order in the process string
COMPONENT test_material = Union_make_material(my_absorption=my_absorption_union,
  process_string="Incoherent_process,Single_crystal_test_process")
AT (0,0,0) ABSOLUTE


// Try automatic linking instead, same result, depends on order of definitions
/* COMPONENT test_material = Union_make_material(my_absorption=my_absorption_union) */
/* AT (0,0,0) ABSOLUTE */



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

COMPONENT source = Source_simple(
  radius=0.02, focus_xw=0.01, focus_yh=0.01,
  lambda0=lam0, dlambda=dlam, flux=1e12)
AT (0,0,0) ABSOLUTE

COMPONENT slit = Slit(
  xwidth=0.01, yheight=0.01)
AT (0,0,5) RELATIVE source

COMPONENT cylinder_sample_union = Union_cylinder(radius=xwidth,yheight=yheight, priority=1, material_string="test_material",p_interact=geometry_interact)
AT (0,0,0.1) RELATIVE slit
ROTATED (x_rotation_geometry,y_rotation_geometry,0) RELATIVE slit


// Insert loggers to be tested here
COMPONENT Union_logger_2DQ_crystal = Union_logger_2DQ(
    Q_direction_1="z", Q1_min=-5.0, Q1_max=5.0, n1=200,
    Q_direction_2="x", Q2_min=-5.0, Q2_max=5.0, n2=200,
    filename="Union_logger_2DQ_crystal.dat",
    target_geometry="cylinder_sample_union",
    target_process="Single_crystal_test_process")
    AT(0,0,0.1) RELATIVE slit

COMPONENT Union_logger_2DQ_incoherent = Union_logger_2DQ(
    Q_direction_1="z", Q1_min=-5.0, Q1_max=5.0, n1=200,
    Q_direction_2="x", Q2_min=-5.0, Q2_max=5.0, n2=200,
    filename="Union_logger_2DQ_incoherent.dat",
    target_geometry="cylinder_sample_union",
    target_process="Incoherent_process")
AT(0,0,0.1) RELATIVE slit

COMPONENT test_sample = Union_master()
WHEN (comp_select == 1)
AT(0,0,0.1) RELATIVE slit
EXTEND
%{
if (number_of_scattering_events == 0) scattered_flag_instr=0;
else scattered_flag_instr=1;
%}

COMPONENT sample = Single_crystal(
          radius=xwidth, yheight=yheight,
          delta_d_d=delta_d_d, mosaic=mosaic,
          ax = 3.8186, ay = 0,      az = 0,
          bx = 0,      by = 3.8843, bz = 0,
          cx = 0,      cy = 0,      cz = 11.6777,
          reflections="YBaCuO.lau",
	  order=10000,barns=0,
          powder=powder,PG=PG)
WHEN (comp_select == 2)
AT (0,0,0.10) RELATIVE slit
ROTATED (x_rotation_geometry_ref,y_rotation_geometry_ref,0) RELATIVE slit
EXTEND %{
if (SCATTERED) scattered_flag_instr=1;
else scattered_flag_instr=0;
%}

COMPONENT det= PSD_monitor_4PI(radius=1, nx=360,ny=180,filename="psd",restore_neutron=1)
WHEN (scattered_flag_instr==1)
AT (0,0,0.1) RELATIVE slit
ROTATED (0,0,0) RELATIVE slit

COMPONENT Banana_monitor = Monitor_nD(radius=1, yheight=0.1, options="banana, theta limits=[20,170], bins=200",filename="banana.dat",restore_neutron=1)
AT (0,0,0.1) RELATIVE slit
ROTATED (0,0,0) RELATIVE slit

COMPONENT PSDlin_transmission_scattered = PSDlin_monitor(xwidth=0.15, yheight=0.01, nbins=100, filename="Output_transmission_lin_scattered.psd",restore_neutron = 1)
WHEN (scattered_flag_instr==1)
AT (0,0,0.5) RELATIVE slit

COMPONENT PSDlin_transmission_transmitted = PSDlin_monitor(xwidth=0.15, yheight=0.01, nbins=100, filename="Output_transmission_lin_transmitted.psd",restore_neutron = 1)
WHEN (scattered_flag_instr==0)
AT (0,0,0.5) RELATIVE slit

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

END
