-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_application.py
More file actions
206 lines (149 loc) · 5.41 KB
/
Copy pathtest_application.py
File metadata and controls
206 lines (149 loc) · 5.41 KB
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
from __future__ import annotations
import numpy as np
import pytest
import requests
from geant4_python_application import Application, basic_gdml
complexGdml = requests.get(
"https://raw.githubusercontent.com/rest-for-physics/restG4/dc3a8f42cea4978206a13325261fa85ec1b26261/examples/13.IAXO/geometry/setup.gdml"
).text
def test_missing_setup():
with Application() as app:
with pytest.raises(RuntimeError):
app.initialize()
def test_missing_manager():
with Application() as app:
with pytest.raises(RuntimeError):
app.setup_detector(gdml="")
def test_no_initialize():
app = Application()
with pytest.raises(RuntimeError):
app.setup_manager()
def test_run():
with Application() as app:
app.setup_manager().setup_detector(
gdml=basic_gdml
).setup_physics().setup_action()
app.initialize()
events = app.run(100)
assert len(events) == 100
def test_seed_single_thread():
with Application() as app:
app.setup_manager()
app.seed = 1100
app.setup_physics()
app.setup_detector(gdml=basic_gdml)
app.setup_action()
app.initialize()
assert app.seed == 1100
app.command("/gun/particle e-")
app.command("/gun/energy 100 MeV")
app.command("/gun/direction 0 0 -1")
app.command("/gun/position 0 0 100 cm")
# launch 100 events
events = app.run(100)
assert len(events) == 100
reference_value = [
0.00000000e00,
4.54930276e-01,
2.25814551e-01,
8.09506886e-03,
1.22345221e00,
]
energy = np.array(events.track.step.energy[0][0][0:5])
assert np.allclose(energy, reference_value, atol=1e-5)
def test_multiple_apps():
apps = [Application() for _ in range(10)]
for app in apps:
app.start()
app.setup_manager().setup_detector(
gdml=basic_gdml
).setup_physics().setup_action()
for app in apps:
app.initialize()
for app in apps:
events = app.run(10)
assert len(events) == 10
for app in apps:
app.stop()
def test_event_data_is_cleared():
with Application() as app:
app.setup_manager().setup_physics()
app.setup_detector(gdml=basic_gdml)
app.setup_action()
app.initialize()
for _ in range(10):
events = app.run(100)
assert len(events) == 100
# raising the number of threads to a high value (e.g. 1000) has a very low chance to cause a segfault. TODO: investigate
@pytest.mark.parametrize("n_threads", [0, 1, 2, 20])
def test_complex_gdml(n_threads):
with Application() as app:
app.setup_manager(n_threads=n_threads).setup_physics().setup_detector(
gdml=complexGdml
).setup_action()
app.detector.sensitive_volumes = {"gasVolume"}
app.initialize()
app.command("/gun/particle neutron")
app.command("/gun/energy 100 MeV")
app.command("/gun/direction 0 0 -1")
app.command("/gun/position 0 0 10 m")
assert len(app.detector.logical_volumes) == 22
assert len(app.detector.physical_volumes) == 309
events = app.run(1000)
def test_python_thread_safety():
import concurrent.futures
with Application() as app:
app.setup_manager().setup_physics().setup_detector(
gdml=basic_gdml
).setup_action()
app.initialize()
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
futures = [executor.submit(app.run, 10) for _ in range(10)]
for future in futures:
future.result()
def test_fields():
with Application() as app:
app.seed = 1000
app.setup_manager().setup_physics().setup_detector(
gdml=complexGdml
).setup_action()
app.detector.sensitive_volumes = {"gasVolume"}
app.initialize()
fields = app.get_event_fields_complete()
assert len(fields) == 26
app.set_event_fields(fields)
app.command("/gun/particle neutron")
app.command("/gun/energy 100 MeV")
app.command("/gun/direction 0 0 -1")
app.command("/gun/position 0 0 10 m")
events = app.run(100)
def test_no_step_fields():
with Application() as app:
app.setup_manager().setup_physics().setup_detector(
gdml=basic_gdml
).setup_action()
app.set_event_fields({"track_id"})
events = app.run(100)
assert len(events) == 100
assert set(events.fields) == {"run", "id", "track"}
assert set(events.track.fields) == {"id"}
def test_no_track_step_fields():
with Application() as app:
app.setup_manager().setup_physics().setup_detector(
gdml=basic_gdml
).setup_action()
app.set_event_fields(set())
events = app.run(100)
assert len(events) == 100
assert set(events.fields) == {"run", "id"}
def test_no_track_fields():
with Application() as app:
app.setup_manager().setup_physics().setup_detector(
gdml=basic_gdml
).setup_action()
app.set_event_fields({"step_energy"})
events = app.run(100)
assert len(events) == 100
assert set(events.fields) == {"run", "id", "track"}
assert set(events.track.fields) == {"step"}
assert set(events.track.step.fields) == {"energy"}