/********************************************************************************
* 
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2008, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
* 
* This file was written by McStasScript, which is a 
* python based McStas instrument generator written by 
* Mads Bertelsen in 2019 while employed at the 
* European Spallation Source Data Management and 
* Software Centre
* 
* Instrument powder
* 
* %Identification
* Written by: Python McStas Instrument Generator
* Date: 14:09:48 on August 12, 2025
* Origin: ESS DMSC
* %INSTRUMENT_SITE: Generated_instruments
* 
* 
* %Parameters
* 
* %End 
********************************************************************************/

DEFINE INSTRUMENT powder (
l_min = 0.5, // Minimum simulated wavelength [AA]
l_max = 4, // Maximum simulated wavelength [AA]
int n_pulses = 1, // Number of simulated pulses
guide_curve_deg = 1, 
chopper_wavelength_center = 2.5, // Center of wavelength band [AA]
double frequency_multiplier = 3, // [1] Chopper frequency as multiple of source frequency
detector_height = 1.5, 
sample_radius = 0.005, 
sample_height = 0.02, 
string sample_choice = "sample_Si" 
)

DECLARE 
%{
double chopper_position = 6.5;
double speed;
double delay;
int sample_Si_active;
int sample_LBCO_active;
int sample_Vanadium_active;
%}

USERVARS 
%{
double source_time;
double n_scattering_sample;
%}

INITIALIZE 
%{
// Start of initialize for generated powder

    speed = 2.0*PI/chopper_wavelength_center*K2V;
    delay = chopper_position/speed;
    delay += 0.5*2.86E-3;
    
if (strcmp(sample_choice, "sample_Si") == 0) {
   sample_Si_active = 1;
   sample_LBCO_active = 0;
   sample_Vanadium_active = 0;
}
if (strcmp(sample_choice, "sample_LBCO") == 0) {
   sample_Si_active = 0;
   sample_LBCO_active = 1;
   sample_Vanadium_active = 0;
}
if (strcmp(sample_choice, "sample_Vanadium") == 0) {
   sample_Si_active = 0;
   sample_LBCO_active = 0;
   sample_Vanadium_active = 1;
}
%}

TRACE 
COMPONENT Source = ESS_butterfly(
 sector = "W", beamline = 2,
 yheight = 0.03, cold_frac = 0.5,
 dist = 2, focus_xw = 0.08,
 focus_yh = 0.072, c_performance = 1,
 t_performance = 1, Lmin = l_min,
 Lmax = l_max, n_pulses = n_pulses,
 acc_power = 2)
AT (0,0,0) ABSOLUTE
EXTEND %{
source_time = t;
%}

COMPONENT feeder = Elliptic_guide_gravity(
 xwidth = 0.08, yheight = 0.06,
 l = 4.3, linxw = 4.5,
 loutxw = 0.4, linyh = 2.05,
 loutyh = 0.3, dimensionsAt = "entrance",
 alpha = 3.2, m = 3)
AT (0,0,2.0) RELATIVE Source

COMPONENT PSC_position = Arm()
AT (0,0,6.5) RELATIVE Source

COMPONENT chopper = DiskChopper(
 theta_0 = 7, radius = 0.35,
 yheight = 0.05, nu = frequency_multiplier*14.0,
 nslit = 1, delay = delay)
AT (0,0,0) RELATIVE PSC_position

COMPONENT expanding = Elliptic_guide_gravity(
 xwidth = 0.08, yheight = 0.12,
 l = 10, linxw = 1.2,
 loutxw = 11.2, linyh = 0.4,
 loutyh = 10.4, dimensionsAt = "exit",
 alpha = 3.2, m = 3)
AT (0,0,0.05) RELATIVE PSC_position

COMPONENT guide_0 = Guide_gravity(
 w1 = 0.08, h1 = 0.12,
 l = 11.1208, alpha = 3,
 m = 1.5)
AT (0,0,10.003) RELATIVE expanding
ROTATED (0,guide_curve_deg/12.0,0) RELATIVE expanding

COMPONENT guide_1 = Guide_gravity(
 w1 = 0.08, h1 = 0.12,
 l = 11.1208, alpha = 3,
 m = 1.5)
AT (0,0,11.123833333333332) RELATIVE guide_0
ROTATED (0,guide_curve_deg/12.0,0) RELATIVE guide_0

COMPONENT guide_2 = Guide_gravity(
 w1 = 0.08, h1 = 0.12,
 l = 11.1208, alpha = 3,
 m = 1.5)
AT (0,0,11.123833333333332) RELATIVE guide_1
ROTATED (0,guide_curve_deg/12.0,0) RELATIVE guide_1

COMPONENT guide_3 = Guide_gravity(
 w1 = 0.08, h1 = 0.12,
 l = 11.1208, alpha = 3,
 m = 1.5)
AT (0,0,11.123833333333332) RELATIVE guide_2
ROTATED (0,guide_curve_deg/12.0,0) RELATIVE guide_2

COMPONENT guide_4 = Guide_gravity(
 w1 = 0.08, h1 = 0.12,
 l = 11.1208, alpha = 3,
 m = 1.5)
AT (0,0,11.123833333333332) RELATIVE guide_3
ROTATED (0,guide_curve_deg/12.0,0) RELATIVE guide_3

COMPONENT guide_5 = Guide_gravity(
 w1 = 0.08, h1 = 0.12,
 l = 11.1208, alpha = 3,
 m = 1.5)
AT (0,0,11.123833333333332) RELATIVE guide_4
ROTATED (0,guide_curve_deg/12.0,0) RELATIVE guide_4

COMPONENT guide_6 = Guide_gravity(
 w1 = 0.08, h1 = 0.12,
 l = 11.1208, alpha = 3,
 m = 1.5)
AT (0,0,11.123833333333332) RELATIVE guide_5
ROTATED (0,guide_curve_deg/12.0,0) RELATIVE guide_5

COMPONENT guide_7 = Guide_gravity(
 w1 = 0.08, h1 = 0.12,
 l = 11.1208, alpha = 3,
 m = 1.5)
AT (0,0,11.123833333333332) RELATIVE guide_6
ROTATED (0,guide_curve_deg/12.0,0) RELATIVE guide_6

COMPONENT guide_8 = Guide_gravity(
 w1 = 0.08, h1 = 0.12,
 l = 11.1208, alpha = 3,
 m = 1.5)
AT (0,0,11.123833333333332) RELATIVE guide_7
ROTATED (0,guide_curve_deg/12.0,0) RELATIVE guide_7

COMPONENT guide_9 = Guide_gravity(
 w1 = 0.08, h1 = 0.12,
 l = 11.1208, alpha = 3,
 m = 1.5)
AT (0,0,11.123833333333332) RELATIVE guide_8
ROTATED (0,guide_curve_deg/12.0,0) RELATIVE guide_8

COMPONENT guide_10 = Guide_gravity(
 w1 = 0.08, h1 = 0.12,
 l = 11.1208, alpha = 3,
 m = 1.5)
AT (0,0,11.123833333333332) RELATIVE guide_9
ROTATED (0,guide_curve_deg/12.0,0) RELATIVE guide_9

COMPONENT guide_11 = Guide_gravity(
 w1 = 0.08, h1 = 0.12,
 l = 11.1208, alpha = 3,
 m = 1.5)
AT (0,0,11.123833333333332) RELATIVE guide_10
ROTATED (0,guide_curve_deg/12.0,0) RELATIVE guide_10

COMPONENT focusing = Elliptic_guide_gravity(
 xwidth = 0.08, yheight = 0.12,
 l = 10, linxw = 10.65,
 loutxw = 0.65, linyh = 10.65,
 loutyh = 0.65, dimensionsAt = "entrance",
 alpha = 3.2, m = 3)
AT (0,0,11.123833333333332) RELATIVE guide_11

COMPONENT guide_end = Arm()
AT (0,0,10) RELATIVE focusing

COMPONENT PSD_sample = PSD_monitor(
 nx = 100, ny = 100,
 filename = "sample_PSD.dat", xwidth = 0.03,
 yheight = 0.03, restore_neutron = 1)
AT (0,0,0.6) RELATIVE guide_end

COMPONENT wavelength = L_monitor(
 nL = 100, filename = "wavelength.dat",
 xwidth = 0.03, yheight = 0.03,
 Lmin = l_min, Lmax = l_max,
 restore_neutron = 1)
AT (0,0,0.6) RELATIVE guide_end

COMPONENT sample_position = Arm()
AT (0,0,0.6) RELATIVE guide_end

COMPONENT fixed_source = Arm()
AT (0,0,-160.6) RELATIVE sample_position

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

COMPONENT sample_Si_Si_inc = Incoherent_process(
 sigma = 0.256, packing_factor = 1,
 unit_cell_volume = 160.103)
AT (0,0,0) ABSOLUTE

COMPONENT sample_Si_Si_pow = Powder_process(
 reflections = "si.laz", packing_factor = 1)
AT (0,0,0) ABSOLUTE

COMPONENT sample_Si = Union_make_material(
 process_string = "sample_Si_Si_inc,sample_Si_Si_pow", my_absorption = 6.8356)
AT (0,0,0) ABSOLUTE

COMPONENT sample_LBCO_La0_5Ba0_5CoO3_inc = Incoherent_process(
 sigma = 5.7581, packing_factor = 0.95,
 unit_cell_volume = 58.9047)
AT (0,0,0) ABSOLUTE

COMPONENT sample_LBCO_La0_5Ba0_5CoO3_pow = Powder_process(
 reflections = "lbco.laz", packing_factor = 0.95)
AT (0,0,0) ABSOLUTE

COMPONENT sample_LBCO_Si_inc = Incoherent_process(
 sigma = 0.256, packing_factor = 0.05,
 unit_cell_volume = 160.103)
AT (0,0,0) ABSOLUTE

COMPONENT sample_LBCO_Si_pow = Powder_process(
 reflections = "si.laz", packing_factor = 0.05)
AT (0,0,0) ABSOLUTE

COMPONENT sample_LBCO = Union_make_material(
 process_string = "sample_LBCO_La0_5Ba0_5CoO3_inc,sample_LBCO_La0_5Ba0_5CoO3_pow,sample_LBCO_Si_inc,sample_LBCO_Si_pow", my_absorption = 68.426)
AT (0,0,0) ABSOLUTE

COMPONENT sample_Vanadium_Vanadium_inc = Incoherent_process(
 sigma = 9.88, packing_factor = 1,
 unit_cell_volume = 27.66)
AT (0,0,0) ABSOLUTE

COMPONENT sample_Vanadium = Union_make_material(
 process_string = "sample_Vanadium_Vanadium_inc", my_absorption = 36.7317)
AT (0,0,0) ABSOLUTE

COMPONENT start_union_geometries = Arm()
AT (0,0,0) ABSOLUTE

COMPONENT Sample_sample_Vanadium = Union_cylinder(
 material_string = "sample_Vanadium", priority = 7,
 radius = sample_radius, yheight = sample_height,
 number_of_activations = sample_Vanadium_active)
AT (0,0,0) RELATIVE sample_position

COMPONENT Sample_sample_LBCO = Union_cylinder(
 material_string = "sample_LBCO", priority = 6,
 radius = sample_radius, yheight = sample_height,
 number_of_activations = sample_LBCO_active)
AT (0,0,0) RELATIVE sample_position

COMPONENT Sample_sample_Si = Union_cylinder(
 material_string = "sample_Si", priority = 5,
 radius = sample_radius, yheight = sample_height,
 number_of_activations = sample_Si_active)
AT (0,0,0) RELATIVE sample_position

SPLIT 100 COMPONENT master = Union_master()
AT (0,0,0) ABSOLUTE
EXTEND %{
n_scattering_sample = scattered_flag[1];
%}

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

COMPONENT Banana_large_0 = Monitor_nD(
 user1 = "source_time", yheight = detector_height,
 restore_neutron = 1, radius = 3.5,
 options = "banana theta bins=320 limits=[10, 170] y bins=20, neutron pixel min=0, t, v, l, user1, list all neutrons", filename = "direct_event_banana_signal_large_0.dat")
AT (0,0,0.0) RELATIVE sample_position

COMPONENT Banana_large_1 = Monitor_nD(
 user1 = "source_time", yheight = detector_height,
 restore_neutron = 1, radius = 3.5,
 options = "banana theta bins=320 limits=[-170, -10] y bins=20, neutron pixel min=6410, t, v, l, user1, list all neutrons", filename = "direct_event_banana_signal_large_1.dat")
AT (0,0,0.0) RELATIVE sample_position

FINALLY 
%{
// Start of finally for generated powder
%}

END
