def test_read_component_JUMP(self): """ Check a JUMP and GROUP statement is read correctly """ set_dummy_dir() blockPrint() Instr = instr.McStas_instr("test_instrument") enablePrint() InstrReader = setup_standard(Instr) components = Instr.component_list test_component = None for component in components: if component.name == "armB": test_component = component self.assertEqual(test_component.component_name, "Arm") self.assertEqual(test_component.GROUP, "arms") self.assertEqual(test_component.JUMP, "myself 2") self.assertEqual(test_component.AT_data, ["0", "0", "0"]) self.assertEqual(test_component.AT_relative, "ABSOLUTE")
def test_read_component_1(self): set_dummy_dir() blockPrint() Instr = instr.McStas_instr("test_instrument") enablePrint() InstrReader = setup_standard(Instr) components = Instr.component_list test_component = None for component in components: if component.name == "Al": test_component = component self.assertEqual(test_component.component_name, "Union_make_material") val = getattr(test_component, "my_absorption") #self.assertEqual(val, "\"100*4*0.231/66.4\"") self.assertEqual(val, "100*4*0.231/66.4") val = getattr(test_component, "process_string") self.assertEqual(val, "\"Al_incoherent,Al_powder\"") self.assertEqual(test_component.AT_data, ["0", "0", "0"]) self.assertEqual(test_component.AT_relative, "ABSOLUTE") self.assertEqual(test_component.ROTATED_data, [0, 0, 0]) self.assertEqual(test_component.ROTATED_relative, "ABSOLUTE")
def test_read_component_2(self): set_dummy_dir() blockPrint() Instr = instr.McStas_instr("test_instrument") enablePrint() InstrReader = setup_standard(Instr) components = Instr.component_list test_component = None for component in components: if component.name == "sample_holder3": test_component = component self.assertEqual(test_component.component_name, "Union_box") val = getattr(test_component, "xwidth") self.assertEqual(val, "0.0098") val = getattr(test_component, "priority") self.assertEqual(val, "52") self.assertEqual(test_component.AT_data, ["0", "-0.03", "-0.03*0.35-0.004"]) self.assertEqual(test_component.AT_relative, "RELATIVE sample_rod_bottom") self.assertEqual(test_component.ROTATED_data, ["-25", "0", "0"]) self.assertEqual(test_component.ROTATED_relative, "RELATIVE sample_rod_bottom")
def setup_simple_slit_instrument(): Instr = instr.McStas_instr("integration_test_simple") source = Instr.add_component("source", "Source_div") source.xwidth = 0.1 source.yheight = 0.01 source.focus_aw = 0.01 source.focus_ah = 0.01 source.E0 = 81.81 source.dE = 1.0 source.flux = 1E10 Instr.add_parameter("slit_offset", value=0) Slit = Instr.add_component("slit", "Slit") Slit.set_AT(["slit_offset", 0, 0.5], RELATIVE="source") Slit.xwidth = 0.01 Slit.yheight = 0.03 PSD = Instr.add_component("PSD_1D", "PSDlin_monitor") PSD.set_AT([0, 0, 1], RELATIVE="source") PSD.xwidth = 0.1 PSD.nx = 100 PSD.yheight = 0.03 PSD.filename = "\"PSD.dat\"" PSD.restore_neutron = 1 return Instr
def setup_simple_instrument_input_path(): THIS_DIR = os.path.dirname(os.path.abspath(__file__)) input_path = os.path.join(THIS_DIR, "test_input_folder") Instr = instr.McStas_instr("integration_test_simple_input", input_path=input_path) source = Instr.add_component("source", "Source_div") source.xwidth = 0.03 source.yheight = 0.01 source.focus_aw = 0.01 source.focus_ah = 0.01 source.E0 = 81.81 source.dE = 1.0 source.flux = 1E10 PSD = Instr.add_component("PSD_1D", "PSDlin_monitor") PSD.set_AT([0, 0, 1], RELATIVE="source") PSD.xwidth = 0.1 PSD.nx = 100 PSD.yheight = 0.03 PSD.filename = "\"PSD.dat\"" PSD.restore_neutron = 1 return Instr
def test_read_declare_parameter(self): set_dummy_dir() blockPrint() Instr = instr.McStas_instr("test_instrument") enablePrint() InstrReader = setup_standard(Instr) self.assertEqual(Instr.declare_list[0].name, "sample_1_index") self.assertEqual(Instr.declare_list[0].type, "int") self.assertEqual(Instr.declare_list[0].value, 27) self.assertEqual(Instr.declare_list[8].name, "array") self.assertEqual(Instr.declare_list[8].type, "double") self.assertEqual(Instr.declare_list[8].vector, 3) self.assertEqual(Instr.declare_list[8].value, [0.1, 0.2, 0.3]) self.assertEqual(Instr.declare_list[9].name, "I_array") self.assertEqual(Instr.declare_list[9].type, "int") self.assertEqual(Instr.declare_list[9].vector, 4) self.assertEqual(Instr.declare_list[10].name, "T_array") self.assertEqual(Instr.declare_list[10].type, "int") self.assertEqual(Instr.declare_list[10].vector, 5) self.assertEqual(Instr.declare_list[10].value, [1, 2, 3, 4, 5]) self.assertEqual(Instr.declare_list[11].name, "home") self.assertEqual(Instr.declare_list[11].type, "char") self.assertEqual(Instr.declare_list[11].vector, 20) self.assertEqual(Instr.declare_list[11].value, "\"\\\"test_string\\\"\"")
def test_read_component_WHEN(self): set_dummy_dir() blockPrint() Instr = instr.McStas_instr("test_instrument") enablePrint() InstrReader = setup_standard(Instr) components = Instr.component_list test_component = None for component in components: if component.name == "outer_cryostat_vacuum": test_component = component self.assertEqual(test_component.component_name, "Union_cylinder") val = getattr(test_component, "radius") self.assertEqual(val, "0.09") val = getattr(test_component, "priority") self.assertEqual(val, "11") self.assertEqual(test_component.WHEN, "WHEN (necessary == 1 )") self.assertEqual(test_component.AT_data, ["0", "0.01", "0"]) self.assertEqual(test_component.AT_relative, "RELATIVE beam_center") self.assertEqual(test_component.ROTATED_data, ["0", "0", "0"]) self.assertEqual(test_component.ROTATED_relative, "RELATIVE beam_center")
def setup_standard_auto_instr(): set_dummy_dir() blockPrint() Instr = instr.McStas_instr("test_instrument") enablePrint() return setup_standard(Instr)
def test_read_initialize_line(self): set_dummy_dir() blockPrint() Instr = instr.McStas_instr("test_instrument") enablePrint() InstrReader = setup_standard(Instr) self.assertEqual(Instr.initialize_section, "// Start of initialize for generated test_instrument\n" + "I_array[2] = 8;\n" + "printf(\"Hello world\\n\");\n")
def test_read_instrument_name(self): """ Check if the instrument name is read correctly """ set_dummy_dir() blockPrint() Instr = instr.McStas_instr("test_instrument") enablePrint() filename = "Union_demonstration_test.instr" InstrReader = control.InstrumentReader(filename) InstrReader.add_to_instr(Instr) self.assertEqual(InstrReader.instr_name, "Union_demonstration")
def test_read_component_EXTEND(self): set_dummy_dir() blockPrint() Instr = instr.McStas_instr("test_instrument") enablePrint() setup_standard(Instr) components = Instr.component_list test_component = None for component in components: if component.name == "test_sample": test_component = component self.assertEqual(test_component.component_name, "Union_master") val = getattr(test_component, "history_limit") self.assertEqual(val, "1000000") lines = test_component.EXTEND.split("\n") line0 = ("if (scattered_flag[sample_1_index] > 0) scattered_1 = 1;" + " else scattered_1 = 0;") line1 = ("if (scattered_flag[sample_2_index] > 0) scattered_2 = 1;" + " else scattered_2 = 0;") line2 = ("if (scattered_flag[sample_3_index] > 0) scattered_3 = 1;" + " else scattered_3 = 0;") line3 = ("if (scattered_flag[sample_4_index] > 0) scattered_4 = 1;" + " else scattered_4 = 0;") self.assertEqual(lines[0], line0) self.assertEqual(lines[1], line1) self.assertEqual(lines[2], line2) self.assertEqual(lines[3], line3) self.assertEqual(test_component.AT_data, ["0", "0", "0"]) self.assertEqual(test_component.AT_relative, "RELATIVE beam_center") self.assertEqual(test_component.ROTATED_data, ["0", "0", "0"]) self.assertEqual(test_component.ROTATED_relative, "RELATIVE beam_center")
def test_read_component_SPLIT(self): set_dummy_dir() blockPrint() Instr = instr.McStas_instr("test_instrument") enablePrint() InstrReader = setup_standard(Instr) components = Instr.component_list test_component = None for component in components: if component.name == "sample_4_container": test_component = component self.assertEqual(test_component.component_name, "Union_cylinder") self.assertEqual(test_component.AT_data, ["0", "0", "0"]) self.assertEqual(test_component.AT_relative, "RELATIVE sample_4")
def test_read_input_parameter(self): set_dummy_dir() blockPrint() Instr = instr.McStas_instr("test_instrument") enablePrint() InstrReader = setup_standard(Instr) self.assertEqual(Instr.parameter_list[0].name, "stick_displacement") # space in type inserted for easier writing by McStas_Instr class self.assertEqual(Instr.parameter_list[0].type, "double ") self.assertEqual(Instr.parameter_list[0].value, 0) self.assertEqual(Instr.parameter_list[1].name, "test_int") # space in type inserted for easier writing by McStas_Instr class self.assertEqual(Instr.parameter_list[1].type, "int ") self.assertEqual(Instr.parameter_list[1].value, 3) self.assertEqual(Instr.parameter_list[2].name, "test_str") # space in type inserted for easier writing by McStas_Instr class self.assertEqual(Instr.parameter_list[2].type, "string ") self.assertEqual(Instr.parameter_list[2].value, "\"\\\"hurray\\\"\"")
def test_read_component_GROUP(self): set_dummy_dir() blockPrint() Instr = instr.McStas_instr("test_instrument") enablePrint() InstrReader = setup_standard(Instr) components = Instr.component_list test_component = None for component in components: if component.name == "armA": test_component = component self.assertEqual(test_component.component_name, "Arm") self.assertEqual(test_component.GROUP, "arms") self.assertEqual(test_component.AT_data, ["0", "0", "0"]) self.assertEqual(test_component.AT_relative, "ABSOLUTE")
# check path to McStas simulation data assert os.path.isdir(dataconfig.data_mcstas), \ 'The folder which should contain the outputs of McStas simulation does not exist' # read instrument file model_to_open = os.path.join(dataconfig.data_mcstas, "V20_config6.instr") assert os.path.isfile( model_to_open ), 'There is an issue with the McStas .instr file to be opened.' InstrReader = reader.McStas_file(model_to_open) # check McStas instrument file for the type of sample McStasInstrument = instr.McStas_instr("McStasInstrument") InstrReader.add_to_instr(McStasInstrument) McStasInstrument.print_components() McStasInstrument.print_component('NAK') # access data stored after McStas simulations data_to_plot = functions.load_data(dataconfig.data_mcstas) # Create a dictionary containing filenames, shape of output data, x, y labels and # position of component dict_mcstas_files = {} for item in data_to_plot: file_key = item.metadata.info['filename'].rstrip() xlabel = item.metadata.info['xlabel'].rstrip()
# load McStas instrument file. assert os.path.exists( dataconfig.data_mcstas), 'The path to McStas files does not exist.' model_to_open = os.path.join(dataconfig.data_mcstas, 'V20_config6.instr') assert os.path.isfile( model_to_open ), 'There is an issue with the McStas .instr file to be opened.' InstrReader = reader.McStas_file(model_to_open) InstrReader.write_python_file('generated_mode.py', force=True) demoV20 = instr.McStas_instr('V20diffraction') InstrReader.add_to_instr(demoV20) demoV20.print_components() # Display parameters for a selected component demoV20.get_component('source').show_parameters_simple() demoV20.get_component('FOC_2_f').show_parameters_simple() demoV20.print_component('FOC_2_f') # Display all parameters for defined McStas model demoV20.show_parameters() # name of the folder where the output data from the simulation will be written
def setup_complex_instrument(): """ Sets up guide system with two guides that are placed next to one another with separate entrances but converge at the end. It attempts to use as many McStas keywords and features as possible. """ Instr = instr.McStas_instr("integration_test_complex", author="test_suite", origin="integration tests") Instr.add_parameter("guide_width", value=0.03) Instr.add_parameter("guide_length", value=8.0) source = Instr.add_component("source", "Source_simple") source.xwidth = 0.1 source.yheight = 0.01 source.dist = 1.5 source.focus_xw = "3*guide_width" source.focus_yh = 0.05 source.E0 = 5.0 source.dE = 1.0 source.flux = 1E10 Instr.add_declare_var("int", "guide_choice") Instr.add_declare_var("double", "source_to_guide_end") Instr.append_initialize("source_to_guide_end = 1.5 + guide_length;") after_guide = Instr.add_component("after_guide", "Arm", AT=[0, 0, "source_to_guide_end"], RELATIVE="source") after_guide.append_EXTEND("guide_choice = -1;") # Add first slit with component methods slit1 = Instr.add_component("slit1", "Slit") slit1.set_AT(["1.3*guide_width", 0, 1.5], RELATIVE="source") slit1.xwidth = "guide_width" slit1.yheight = 0.05 slit1.append_EXTEND("if (SCATTERED) {") slit1.append_EXTEND(" guide_choice = 1;") slit1.append_EXTEND("}") slit1.set_GROUP("entrance_slits") # Add second slit with instr methods Instr.add_component("slit2", "Slit") Instr.set_component_AT("slit2", ["-1.3*guide_width", 0, 1.5]) Instr.set_component_RELATIVE("slit2", "source") Instr.set_component_parameter("slit2", { "xwidth": "guide_width", "yheight": 0.05 }) Instr.append_component_EXTEND("slit2", "if (SCATTERED) {") Instr.append_component_EXTEND("slit2", " guide_choice = 2;") Instr.append_component_EXTEND("slit2", "}") Instr.set_component_GROUP("slit2", "entrance_slits") select1 = Instr.add_component("select1", "Arm", RELATIVE="after_guide") select1.set_JUMP("select2 WHEN guide_choice == 2") guide1 = Instr.add_component("guide1", "Guide_gravity") guide1.set_AT([0, 0, 0.1], RELATIVE="slit1") guide1.set_ROTATED([0, "-RAD2DEG*atan(0.5*guide_width/guide_length)", 0], RELATIVE="slit1") guide1.w1 = "guide_width" guide1.w2 = "1.3*guide_width" guide1.h1 = 0.05 guide1.h2 = 0.05 guide1.l = "guide_length" guide1.m = 4 guide1.G = -9.82 select2 = Instr.add_component("select2", "Arm", RELATIVE="after_guide") select2.set_JUMP("done WHEN guide_choice == 1") guide2 = Instr.add_component("guide2", "Guide_gravity") guide2.set_AT([0, 0, 0.1], RELATIVE="slit2") guide2.set_ROTATED([0, "RAD2DEG*atan(0.5*guide_width/guide_length)", 0], RELATIVE="slit2") guide2.w1 = "guide_width" guide2.w2 = "1.3*guide_width" guide2.h1 = 0.05 guide2.h2 = 0.05 guide2.l = "guide_length" guide2.m = 4 guide2.G = -9.82 guide2.set_SPLIT = 2 Instr.add_component("done", "Arm", RELATIVE="after_guide") PSD1 = Instr.add_component("PSD_1D_1", "PSDlin_monitor") PSD1.set_AT([0, 0, 0.2], RELATIVE="after_guide") PSD1.xwidth = 0.1 PSD1.nx = 100 PSD1.yheight = 0.03 PSD1.filename = "\"PSD1.dat\"" PSD1.restore_neutron = 1 PSD1.set_WHEN("guide_choice == 1") PSD2 = Instr.add_component("PSD_1D_2", "PSDlin_monitor") PSD2.set_AT([0, 0, 0.2], RELATIVE="after_guide") PSD2.xwidth = 0.1 PSD2.nx = 100 PSD2.yheight = 0.03 PSD2.filename = "\"PSD2.dat\"" PSD2.restore_neutron = 1 PSD2.set_WHEN("guide_choice == 2") PSD = Instr.add_component("PSD_1D", "PSDlin_monitor") PSD.set_AT([0, 0, 0.2], RELATIVE="after_guide") PSD.xwidth = 0.1 PSD.nx = 100 PSD.yheight = 0.03 PSD.filename = "\"PSD_all.dat\"" PSD.restore_neutron = 1 Instr.append_finally("guide_choice = -1;") return Instr
import McStasCalculator import McStasParameters from mcstasscript.interface import instr, plotter instrument = instr.McStas_instr("mcstas_instrument_name") instrument.add_parameter("energy") src = instrument.add_component("Source", "Source_simple") src.xwidth = 0.1 src.yheight = 0.1 src.E0 = "energy" src.dist = 2.0 src.focus_xw = 0.03 src.focus_yh = 0.03 det = instrument.add_component("Detector", "PSD_monitor") det.xwidth = 0.03 det.yheight = 0.03 det.filename = "\"psd.dat\"" det.set_AT([0, 0, 2.0], RELATIVE="Source") pars = McStasParameters.McStasParameters(instrument=instrument, pars={"energy": 10}, ncount=1E7, mpi=4, increment_folder_name=True) calculator = McStasCalculator.McStasCalculator(parameters=pars, input_path='.', output_path='test_output') data = calculator.backengine() plotter.make_sub_plot(data)