1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
import io
import os
import unittest.mock
from mcstasscript.interface import instr
def setup_simple_instrument():
Instr = instr.McStas_instr("integration_test_simple")
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
if Instr.mccode_version > 2:
PSD.nbins = 100
else:
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
if Instr.mccode_version > 2:
PSD = Instr.add_component("PSD_1D", "PSDlin_monitor_3")
else:
PSD = Instr.add_component("PSD_1D", "PSDlin_monitor")
PSD.set_AT([0, 0, 1], RELATIVE="source")
PSD.xwidth = 0.1
if Instr.mccode_version > 2:
PSD.nbins = 100
else:
PSD.nx = 100
PSD.yheight = 0.03
PSD.filename = "\"PSD.dat\""
PSD.restore_neutron = 1
return Instr
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
if Instr.mccode_version > 2:
PSD.nbins = 100
else:
PSD.nx = 100
PSD.yheight = 0.03
PSD.filename = "\"PSD.dat\""
PSD.restore_neutron = 1
return Instr
class TestSimpleInstrument(unittest.TestCase):
"""
Integration test of a full instrument with McStas simulation
performed by the system. The configuration file needs to be set up
correctly in order for these tests to succeed.
"""
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_simple_instrument(self, mock_stdout):
"""
Test that an instrument can run and that the results match
expectations. Here beam in small area in the middle of the
detector.
"""
CURRENT_DIR = os.getcwd()
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(THIS_DIR)
Instr = setup_simple_instrument()
data = Instr.run_full_instrument(foldername="integration_test_simple",
ncount=1E6, mpi=1,
increment_folder_name=True)
os.chdir(CURRENT_DIR)
intensity_data = data[0].Intensity
# beam should be on pixel 35 to 65
sum_outside_beam = (sum(intensity_data[0:34])
+ sum(intensity_data[66:99]))
sum_inside_beam = sum(intensity_data[35:65])
self.assertTrue(1000*sum_outside_beam < sum_inside_beam)
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_simple_instrument_input(self, mock_stdout):
"""
Test that an instrument can run and that the results match
expectations. Here beam in small area in the middle of the
detector.
"""
CURRENT_DIR = os.getcwd()
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(THIS_DIR)
Instr = setup_simple_instrument_input_path()
foldername = "integration_test_simple_input"
data = Instr.run_full_instrument(foldername=foldername,
ncount=1E6, mpi=1,
increment_folder_name=True)
os.chdir(CURRENT_DIR)
intensity_data = data[0].Intensity
# beam should be on pixel 35 to 65
sum_outside_beam = (sum(intensity_data[0:34])
+ sum(intensity_data[66:99]))
sum_inside_beam = sum(intensity_data[35:65])
self.assertTrue(1000*sum_outside_beam < sum_inside_beam)
# Check component from input_folder read
self.assertEqual(data[0].metadata.xlabel, "Test")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_simple_instrument_mpi(self, mock_stdout):
"""
Test that an instrument can run and that the results matches
expectations. Here beam in small area in the middle of the
detector. Running with mpi, 2 cores.
"""
CURRENT_DIR = os.getcwd()
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(THIS_DIR)
Instr = setup_simple_instrument()
data = Instr.run_full_instrument(foldername="integration_test_mpi",
ncount=1E6, mpi=2,
increment_folder_name=True)
os.chdir(CURRENT_DIR)
intensity_data = data[0].Intensity
# beam should be on pixel 35 to 65
sum_outside_beam = (sum(intensity_data[0:34])
+ sum(intensity_data[66:99]))
sum_inside_beam = sum(intensity_data[35:65])
self.assertTrue(1000*sum_outside_beam < sum_inside_beam)
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_slit_instrument(self, mock_stdout):
"""
Test parameters can be controlled through McStasScript. Here
a slit can be moved, but the default value of 0 should be
used.
"""
CURRENT_DIR = os.getcwd()
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(THIS_DIR)
Instr = setup_simple_slit_instrument()
foldername = "integration_test_slit"
data = Instr.run_full_instrument(foldername=foldername,
ncount=2E6, mpi=2,
increment_folder_name=True)
os.chdir(CURRENT_DIR)
intensity_data = data[0].Intensity
# beam should be on pixel 45 to 55
sum_outside_beam = (sum(intensity_data[0:44])
+ sum(intensity_data[56:99]))
sum_inside_beam = sum(intensity_data[45:55])
self.assertTrue(1000*sum_outside_beam < sum_inside_beam)
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_slit_moved_instrument(self, mock_stdout):
"""
Test parameters can be controlled through McStasScript. Here
a slit is moved to one side and the result is verified.
"""
CURRENT_DIR = os.getcwd()
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(THIS_DIR)
Instr = setup_simple_slit_instrument()
data = Instr.run_full_instrument(foldername="integration_test_slit",
ncount=2E6, mpi=2,
increment_folder_name=True,
parameters={"slit_offset": 0.03})
os.chdir(CURRENT_DIR)
intensity_data = data[0].Intensity
# beam should be on pixel 75 to 85
sum_outside_beam = (sum(intensity_data[0:74])
+ sum(intensity_data[86:99]))
sum_inside_beam = sum(intensity_data[75:85])
self.assertTrue(1000*sum_outside_beam < sum_inside_beam)
if __name__ == '__main__':
unittest.main()
|