forked from hoogerheide/autonomous
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrument.py
More file actions
288 lines (216 loc) · 8.56 KB
/
instrument.py
File metadata and controls
288 lines (216 loc) · 8.56 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import numpy as np
import json
import warnings
from autorefl import q2a, a2q
from reflred.resolution import divergence
class ReflectometerBase(object):
def __init__(self) -> None:
self._L = None
self._dL = None
self.xlabel = ''
self.name = None
self.resolution = 'normal'
# assumes that the detector arm motion is the slowest component
self.topspeed = 1.0 # units of degrees / second for detector arm
self.basespeed = 0.2 # units of degrees / second for detector arm
self.acceleration = 0.5 # units of degrees / second^2 for detector arm
self.x = None # current position
# instrument geometry
self._L12 = None
self._L2S = None
self._LS3 = None
self._L34 = None
self.footprint = None
self.sample_width = None
self._S3Offset = 0.0
self._R12 = 1.0
# default t(Q) scaling parameters. Here t(Q) \propto _mon0 + _mon1 * Q^Qpow
self._mon0 = 0.0
self._mon1 = 1.0
self._Qpow = 2.0
def x2q(self, x):
pass
def x2a(self, x):
pass
def qrange2xrange(self, qmin, qmax):
pass
def intensity(self, x):
pass
def meastime(self, x, totaltime):
q = self.x2q(np.array(x))
f = self._mon0 + self._mon1 * q ** self._Qpow
return totaltime * f / sum(f)
def T(self, x):
return self.x2a(x)
def dT(self, x):
usesample = True if self.footprint > self.sample_width else False
return divergence(self.get_slits(x), self.get_slit_distances(), T=self.T(x), sample_width=self.sample_width, use_sample=usesample)
def L(self, x):
return np.array(np.ones_like(x) * self._L, ndmin=1)
def dL(self, x):
return np.array(np.ones_like(x) * self._dL, ndmin=1)
def get_slits(self, x):
x = np.array(x, ndmin=1)
sintheta = np.sin(np.radians(self.x2a(x)))
s2 = self.footprint * sintheta / ((self._R12 + 1) * self._L2S / self._L12 + 1)
s1 = self._R12 * s2
s3 = (s1 + s2) * (self._L2S + self._LS3) / self._L12 + s2 + self._S3Offset
s4 = (s1 + s2) * (self._L2S + self._LS3 + self._L34) / self._L12 + s2 + self._S3Offset
return s1, s2, s3, s4
def get_slit_distances(self):
return -(self._L12 + self._L2S), -self._L2S, self._LS3, self._LS3 + self._L34
def movetime(self, x):
if self.x is None:
return np.array([0])
else:
x = np.array(x, ndmin=1)
# convert x to angle units
newT = self.x2a(x)
curT = self.x2a(self.x)
# detector arm motion is 2 * dTheta
dx = 2 * np.abs(newT - curT)
t = np.empty_like(dx)
# total time that arm is accelerating
accel_t = (self.topspeed - self.basespeed) / self.acceleration
# total distance that can be traversed in one acceleration / deceleration cycle without achieving top speed
max_accel_dx = 2 * (0.5 * self.acceleration * accel_t ** 2 + self.basespeed * accel_t)
# select points in the acceleration only regime
accel_crit = (dx < max_accel_dx)
# top velocity reached
t[~accel_crit] = 2 * accel_t + (dx[~accel_crit] - max_accel_dx) / self.topspeed
# top velocity not reached
t[accel_crit] = 2 * self.basespeed / self.acceleration * (-1 + np.sqrt(1 + 2 * (dx[accel_crit] / 2) * self.acceleration / self.basespeed ** 2))
return t
class MAGIK(ReflectometerBase):
""" MAGIK Reflectometer
x = Q """
def __init__(self) -> None:
super().__init__()
self._L = np.array([5.0])
self._dL = 0.01648374 * self._L / 2.355
self.xlabel = r'$Q_z$ (' + u'\u212b' + r'$^{-1}$)'
self.name = 'MAGIK'
self.resolution = 'normal'
self.topspeed = 1.0
self.basespeed = 0.2
self.acceleration = 0.5
# As of 1/24/2022:
# Base: 0.2 deg / sec
# Acceleration: 0.5 deg / sec^2
# Top velocity: 1.0 deg / sec
# instrument geometry
self._L12 = 1403.
self._L2S = 330.
self._LS3 = 229.
self._L34 = 939.
self.footprint = 45.
self._S3Offset = 1.22
self._R12 = 1.0
self.sample_width = np.inf
# best practice Q scaling
self._mon0 = 30.0
self._mon1 = 1250.
self._Qpow = 2.0
# load calibration files
try:
d_intens = np.loadtxt('calibration/magik_intensity_hw106.refl')
self.p_intens = np.polyfit(d_intens[:,0], d_intens[:,1], 3, w=1/d_intens[:,2])
except OSError:
warnings.warn('MAGIK calibration files not found, using defaults')
self.p_intens = np.array([ 5.56637543e+02, 7.27944632e+04, 2.13479802e+02, -4.37052050e+01])
def x2q(self, x):
return x
def x2a(self, x):
return q2a(x, self._L)
def qrange2xrange(self, bounds):
return min(bounds), max(bounds)
def intensity(self, x):
news1 = self.get_slits(x)[0]
incident_neutrons = np.polyval(self.p_intens, news1)
return np.array(incident_neutrons, ndmin=2).T
def T(self, x):
x = np.array(x, ndmin=1)
return np.broadcast_to(self.x2a(x), (len(self._L), len(x))).T
def dT(self, x):
x = np.array(x, ndmin=1)
dTs = super().dT(x).T
return np.broadcast_to(dTs, (len(self._L), len(x))).T
def L(self, x):
x = np.array(x, ndmin=1)
return np.broadcast_to(self._L, (len(x), len(self._L)))
def dL(self, x):
x = np.array(x, ndmin=1)
return np.broadcast_to(self._dL, (len(x), len(self._L)))
class CANDOR(ReflectometerBase):
""" CANDOR Reflectometer with a single bank
x = T """
def __init__(self, bank=0) -> None:
super().__init__()
self.name = 'CANDOR'
self.xlabel = r'$\Theta$ $(\degree)$'
self.resolution = 'uniform'
self.topspeed = 2.0
self.basespeed = 0.1
self.acceleration = 0.1
# As of 1/24/2022:
# Base: 0.1 deg / sec
# Acceleration: 0.1 deg / sec^2
# Top velocity: 2.0 deg / sec
# NOTE: dominated by acceleration and base for most moves!!
# instrument geometry
self._L12 = 4000.
self._L2S = 356.
self._LS3 = 356.
self._L34 = 3000.
self.footprint = 45.
self._S3Offset = 5.0
self._R12 = 2.5
self.detector_mask = 8.0
self.sample_width = np.inf
# best practice Q scaling
self._mon0 = 20.0
self._mon1 = 20000.
self._Qpow = 3.0
# load wavelength calibration
wvcal = np.flipud(np.loadtxt(f'calibration/DetectorWavelengths_PG_integrate_sumeff_bank{bank}.csv', delimiter=',', usecols=[1, 2]))
self._L = wvcal[:,0]
self._dL = wvcal[:,1]
# load intensity calibration
with open('calibration/flowcell_d2o_r12_2_5_maxbeam_60_qoverlap0_751388_unpolarized_intensity.json', 'r') as f:
d = json.load(f)
self.intens_calib = np.squeeze(np.array(d['outputs'][0]['v']))
self.s1_intens_calib = np.squeeze(d['outputs'][0]['x'])
#ps1 = np.polynomial.polynomial.polyfit(s1, intens, 1)
def x2q(self, x):
return a2q(self.T(x), self.L(x))
def x2a(self, x):
return x
def qrange2xrange(self, qbounds):
qbounds = np.array(qbounds)
minx = q2a(min(qbounds), max(self._L))
maxx = q2a(max(qbounds), min(self._L))
return minx, maxx
def intensity(self, x):
news1 = self.get_slits(x)[0]
incident_neutrons = [np.interp(news1, self.s1_intens_calib, intens) for intens in self.intens_calib.T]
return np.array(incident_neutrons, ndmin=2).T
def meastime(self, x, totaltime):
q = a2q(np.array(x), 5.0)
f = self._mon0 + self._mon1 * q ** self._Qpow
return totaltime * f / sum(f)
def get_slits(self, x):
s1, s2, s3, _ = super().get_slits(x)
return s1, s2, s3, self.detector_mask
def T(self, x):
x = np.array(x, ndmin=1)
return np.broadcast_to(x, (len(self._L), len(x))).T
def dT(self, x):
x = np.array(x, ndmin=1)
dTs = super().dT(x).T
return np.broadcast_to(dTs, (len(self._L), len(x))).T
def L(self, x):
x = np.array(x, ndmin=1)
return np.broadcast_to(self._L, (len(x), len(self._L)))
def dL(self, x):
x = np.array(x, ndmin=1)
return np.broadcast_to(self._dL, (len(x), len(self._L)))