qtas.py 12.1 KB
Newer Older
Tobias WEBER's avatar
Tobias WEBER committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#
# tascalc gui
# @author Tobias Weber <tweber@ill.fr>
# @date 24-oct-18
# @license see 'LICENSE' file
#

import tascalc as tas

import numpy as np
import numpy.linalg as la

import PyQt5 as qt
import PyQt5.QtCore as qtc
import PyQt5.QtWidgets as qtw

import sys



app = qtw.QApplication(sys.argv)
Tobias WEBER's avatar
Tobias WEBER committed
22
app.setApplicationName("qtas")
Tobias WEBER's avatar
Tobias WEBER committed
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
app.setStyle("Fusion")

sett = qtc.QSettings("tobis_stuff", "qtas")
tabs = qtw.QTabWidget()



# -----------------------------------------------------------------------------
# variables
B = np.array([[1,0,0], [0,1,0], [0,0,1]])
orient_rlu = np.array([1,0,0])
orient2_rlu = np.array([0,1,0])
orient_up_rlu = np.array([0,0,1])
# -----------------------------------------------------------------------------



# -----------------------------------------------------------------------------
# helpers
def getfloat(str):
	try:
		return float(str)
	except ValueError:
		return 0.
# -----------------------------------------------------------------------------



# -----------------------------------------------------------------------------
# crystal tab

xtalpanel = qtw.QWidget()
xtallayout = qtw.QGridLayout(xtalpanel)
Tobias WEBER's avatar
Tobias WEBER committed
56
57
scpanel = xtalpanel
sclayout = xtallayout
Tobias WEBER's avatar
Tobias WEBER committed
58
59
60
61
62
63
64
65
66

editA = qtw.QLineEdit(xtalpanel)
editB = qtw.QLineEdit(xtalpanel)
editC = qtw.QLineEdit(xtalpanel)
editAlpha = qtw.QLineEdit(xtalpanel)
editBeta = qtw.QLineEdit(xtalpanel)
editGamma = qtw.QLineEdit(xtalpanel)
editBMat = qtw.QPlainTextEdit(xtalpanel)
editBMat.setReadOnly(True)
Tobias WEBER's avatar
Tobias WEBER committed
67
68
69
70
71
72
editAx = qtw.QLineEdit(scpanel)
editAy = qtw.QLineEdit(scpanel)
editAz = qtw.QLineEdit(scpanel)
editBx = qtw.QLineEdit(scpanel)
editBy = qtw.QLineEdit(scpanel)
editBz = qtw.QLineEdit(scpanel)
Tobias WEBER's avatar
Tobias WEBER committed
73
74
75
76
77
78
79
80
81


def xtalChanged():
	global B
	lattice = np.array([getfloat(editA.text()), getfloat(editB.text()), getfloat(editC.text())])
	angles = np.array([getfloat(editAlpha.text()), getfloat(editBeta.text()), getfloat(editGamma.text())])
	try:
		B = tas.get_B(lattice, angles/180.*np.pi)
		invB = la.inv(B)
Tobias WEBER's avatar
Tobias WEBER committed
82
		editBMat.setPlainText("B =\n%10.4f %10.4f %10.4f\n%10.4f %10.4f %10.4f\n%10.4f %10.4f %10.4f\n" \
Tobias WEBER's avatar
Tobias WEBER committed
83
			% (B[0,0],B[0,1],B[0,2], B[1,0],B[1,1],B[1,2], B[2,0],B[2,1],B[2,2]) \
Tobias WEBER's avatar
Tobias WEBER committed
84
			+"\nB^(-1) =\n%10.4f %10.4f %10.4f\n%10.4f %10.4f %10.4f\n%10.4f %10.4f %10.4f\n" \
Tobias WEBER's avatar
Tobias WEBER committed
85
86
87
88
			% (invB[0,0],invB[0,1],invB[0,2], invB[1,0],invB[1,1],invB[1,2], invB[2,0],invB[2,1],invB[2,2]))
	except la.LinAlgError:
		editBMat.setPlainText("invalid")

Tobias WEBER's avatar
Tobias WEBER committed
89
90
91
92
def planeChanged():
	QChanged()


Tobias WEBER's avatar
Tobias WEBER committed
93
94
95
96
97
98
editA.textEdited.connect(xtalChanged)
editB.textEdited.connect(xtalChanged)
editC.textEdited.connect(xtalChanged)
editAlpha.textEdited.connect(xtalChanged)
editBeta.textEdited.connect(xtalChanged)
editGamma.textEdited.connect(xtalChanged)
Tobias WEBER's avatar
Tobias WEBER committed
99
100
101
102
103
104
105
editAx.textEdited.connect(planeChanged)
editAy.textEdited.connect(planeChanged)
editAz.textEdited.connect(planeChanged)
editBx.textEdited.connect(planeChanged)
editBy.textEdited.connect(planeChanged)
editBz.textEdited.connect(planeChanged)

Tobias WEBER's avatar
Tobias WEBER committed
106
107
108
109
110
111
112

editA.setText("%.6g" % sett.value("a", 5., type=float))
editB.setText("%.6g" % sett.value("b", 5., type=float))
editC.setText("%.6g" % sett.value("c", 5., type=float))
editAlpha.setText("%.6g" % sett.value("alpha", 90., type=float))
editBeta.setText("%.6g" % sett.value("beta", 90., type=float))
editGamma.setText("%.6g" % sett.value("gamma", 90., type=float))
Tobias WEBER's avatar
Tobias WEBER committed
113
114
115
116
117
118
editAx.setText("%.6g" % sett.value("ax", 1., type=float))
editAy.setText("%.6g" % sett.value("ay", 0., type=float))
editAz.setText("%.6g" % sett.value("az", 0., type=float))
editBx.setText("%.6g" % sett.value("bx", 0., type=float))
editBy.setText("%.6g" % sett.value("by", 1., type=float))
editBz.setText("%.6g" % sett.value("bz", 0., type=float))
Tobias WEBER's avatar
Tobias WEBER committed
119
120

xtallayout.addWidget(qtw.QLabel("a (A):", xtalpanel), 0,0, 1,1)
Tobias WEBER's avatar
Tobias WEBER committed
121
xtallayout.addWidget(editA, 0,1, 1,3)
Tobias WEBER's avatar
Tobias WEBER committed
122
xtallayout.addWidget(qtw.QLabel("b (A):", xtalpanel), 1,0, 1,1)
Tobias WEBER's avatar
Tobias WEBER committed
123
xtallayout.addWidget(editB, 1,1, 1,3)
Tobias WEBER's avatar
Tobias WEBER committed
124
xtallayout.addWidget(qtw.QLabel("c (A):", xtalpanel), 2,0, 1,1)
Tobias WEBER's avatar
Tobias WEBER committed
125
xtallayout.addWidget(editC, 2,1, 1,3)
Tobias WEBER's avatar
Tobias WEBER committed
126
xtallayout.addWidget(qtw.QLabel("alpha (A):", xtalpanel), 3,0, 1,1)
Tobias WEBER's avatar
Tobias WEBER committed
127
xtallayout.addWidget(editAlpha, 3,1, 1,3)
Tobias WEBER's avatar
Tobias WEBER committed
128
xtallayout.addWidget(qtw.QLabel("beta (A):", xtalpanel), 4,0, 1,1)
Tobias WEBER's avatar
Tobias WEBER committed
129
xtallayout.addWidget(editBeta, 4,1, 1,3)
Tobias WEBER's avatar
Tobias WEBER committed
130
xtallayout.addWidget(qtw.QLabel("gamma (A):", xtalpanel), 5,0, 1,1)
Tobias WEBER's avatar
Tobias WEBER committed
131
132
133
134
135
136
137
138
139
140
xtallayout.addWidget(editGamma, 5,1, 1,3)
sclayout.addWidget(qtw.QLabel("orient 1 (rlu):", scpanel), 6,0, 1,1)
sclayout.addWidget(editAx, 6,1, 1,1)
sclayout.addWidget(editAy, 6,2, 1,1)
sclayout.addWidget(editAz, 6,3, 1,1)
sclayout.addWidget(qtw.QLabel("orient 2 (rlu):", scpanel), 7,0, 1,1)
sclayout.addWidget(editBx, 7,1, 1,1)
sclayout.addWidget(editBy, 7,2, 1,1)
sclayout.addWidget(editBz, 7,3, 1,1)
xtallayout.addWidget(editBMat, 8,0, 2,4)
Tobias WEBER's avatar
Tobias WEBER committed
141
142
143
144
145
146
147
148
149
150
151

tabs.addTab(xtalpanel, "Crystal")
# -----------------------------------------------------------------------------



# -----------------------------------------------------------------------------
# tas tab

taspanel = qtw.QWidget()
taslayout = qtw.QGridLayout(taspanel)
Tobias WEBER's avatar
Tobias WEBER committed
152
153
Qpanel = taspanel
Qlayout = taslayout
Tobias WEBER's avatar
Tobias WEBER committed
154
155
156
157
158
159
160

editA1 = qtw.QLineEdit(taspanel)
editA2 = qtw.QLineEdit(taspanel)
editA3 = qtw.QLineEdit(taspanel)
editA4 = qtw.QLineEdit(taspanel)
editA5 = qtw.QLineEdit(taspanel)
editA6 = qtw.QLineEdit(taspanel)
Tobias WEBER's avatar
Tobias WEBER committed
161

Tobias WEBER's avatar
Tobias WEBER committed
162
163
164
editDm = qtw.QLineEdit(taspanel)
editDa = qtw.QLineEdit(taspanel)

Tobias WEBER's avatar
Tobias WEBER committed
165
166
167
168
169
170
171
172
edith = qtw.QLineEdit(Qpanel)
editk = qtw.QLineEdit(Qpanel)
editl = qtw.QLineEdit(Qpanel)
editE = qtw.QLineEdit(Qpanel)

editKi = qtw.QLineEdit(Qpanel)
editKf = qtw.QLineEdit(Qpanel)

Tobias WEBER's avatar
Tobias WEBER committed
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

def TASChanged():
	a1 = getfloat(editA1.text()) / 180. * np.pi
	a2 = a1 * 2.
	a3 = getfloat(editA3.text()) / 180. * np.pi
	a4 = getfloat(editA4.text()) / 180. * np.pi
	a5 = getfloat(editA5.text()) / 180. * np.pi
	a6 = a5 * 2.
	dmono = getfloat(editDm.text())
	dana = getfloat(editDa.text())

        ki = tas.get_monok(a1, dmono)
        kf = tas.get_monok(a5, dana)
        E = tas.get_E(ki, kf)
        Qlen = tas.get_Q(ki, kf, a4)
        Qvec = tas.get_hkl(ki, kf, a3, Qlen, orient_rlu, orient_up_rlu, B)

	editA2.setText("%.6g" % (a2 / np.pi * 180.))
	editA6.setText("%.6g" % (a6 / np.pi * 180.))

	edith.setText("%.6g" % Qvec[0])
	editk.setText("%.6g" % Qvec[1])
	editl.setText("%.6g" % Qvec[2])
	editKi.setText("%.6g" % ki)
	editKf.setText("%.6g" % kf)
	editE.setText("%.6g" % E)


def A2Changed():
	a2 = getfloat(editA2.text()) / 180. * np.pi
	editA1.setText("%.6g" % (0.5*a2 / np.pi * 180.))
	TASChanged()

def A6Changed():
	a6 = getfloat(editA6.text()) / 180. * np.pi
	editA5.setText("%.6g" % (0.5*a6 / np.pi * 180.))
	TASChanged()

def DChanged():
	QChanged()

def QChanged():
	global orient_rlu, orient2_rlu, orient_up_rlu

	Q_rlu = np.array([getfloat(edith.text()), getfloat(editk.text()), getfloat(editl.text())])
	ki = getfloat(editKi.text())
	kf = getfloat(editKf.text())
	E = tas.get_E(ki, kf)

	orient_rlu = np.array([getfloat(editAx.text()), getfloat(editAy.text()), getfloat(editAz.text())])
	orient2_rlu = np.array([getfloat(editBx.text()), getfloat(editBy.text()), getfloat(editBz.text())])
	orient_up_rlu = tas.cross(orient_rlu, orient2_rlu, B)   # up vector in rlu

	[a1, a2] = tas.get_a1a2(ki, getfloat(editDm.text()))
	[a5, a6] = tas.get_a1a2(kf, getfloat(editDa.text()))
	[a3, a4] = tas.get_a3a4(ki, kf, Q_rlu, orient_rlu, orient_up_rlu, B)

	editE.setText("%.6g" % E)
	editA1.setText("%.6g" % (a1 / np.pi * 180.))
	editA2.setText("%.6g" % (a2 / np.pi * 180.))
	editA3.setText("%.6g" % (a3 / np.pi * 180.))
	editA4.setText("%.6g" % (a4 / np.pi * 180.))
	editA5.setText("%.6g" % (a5 / np.pi * 180.))
	editA6.setText("%.6g" % (a6 / np.pi * 180.))


def EChanged():
	E = getfloat(editE.text())
	kf = getfloat(editKf.text())
	ki = tas.get_ki(kf, E)
	editKi.setText("%.6g" % ki)

Tobias WEBER's avatar
Tobias WEBER committed
245
246
247
248
249
250
251
252
253

editA1.textEdited.connect(TASChanged)
editA3.textEdited.connect(TASChanged)
editA4.textEdited.connect(TASChanged)
editA5.textEdited.connect(TASChanged)
editA2.textEdited.connect(A2Changed)
editA6.textEdited.connect(A6Changed)
editDm.textEdited.connect(DChanged)
editDa.textEdited.connect(DChanged)
Tobias WEBER's avatar
Tobias WEBER committed
254
255
256
257
258
259
260
261
edith.textEdited.connect(QChanged)
editk.textEdited.connect(QChanged)
editl.textEdited.connect(QChanged)
editKi.textEdited.connect(QChanged)
editKf.textEdited.connect(QChanged)
editE.textEdited.connect(EChanged)


Tobias WEBER's avatar
Tobias WEBER committed
262
263
editDm.setText("%.6g" % sett.value("dm", 3.355, type=float))
editDa.setText("%.6g" % sett.value("da", 3.355, type=float))
Tobias WEBER's avatar
Tobias WEBER committed
264
265
266
267
268
269
270
271
edith.setText("%.6g" % sett.value("h", 1., type=float))
editk.setText("%.6g" % sett.value("k", 0., type=float))
editl.setText("%.6g" % sett.value("l", 0., type=float))
#editE.setText("%.6g" % sett.value("E", 0., type=float))
editKi.setText("%.6g" % sett.value("ki", 2.662, type=float))
editKf.setText("%.6g" % sett.value("kf", 2.662, type=float))

Qlayout.addWidget(qtw.QLabel("h (rlu):", Qpanel), 0,0, 1,1)
Tobias WEBER's avatar
Tobias WEBER committed
272
Qlayout.addWidget(edith, 0,1, 1,2)
Tobias WEBER's avatar
Tobias WEBER committed
273
Qlayout.addWidget(qtw.QLabel("k (rlu):", Qpanel), 1,0, 1,1)
Tobias WEBER's avatar
Tobias WEBER committed
274
Qlayout.addWidget(editk, 1,1, 1,2)
Tobias WEBER's avatar
Tobias WEBER committed
275
Qlayout.addWidget(qtw.QLabel("l (rlu):", Qpanel), 2,0, 1,1)
Tobias WEBER's avatar
Tobias WEBER committed
276
Qlayout.addWidget(editl, 2,1, 1,2)
Tobias WEBER's avatar
Tobias WEBER committed
277
Qlayout.addWidget(qtw.QLabel("E (meV):", Qpanel), 3,0, 1,1)
Tobias WEBER's avatar
Tobias WEBER committed
278
279
Qlayout.addWidget(editE, 3,1, 1,2)
Qlayout.addWidget(qtw.QLabel("ki/kf (1/A):", Qpanel), 4,0, 1,1)
Tobias WEBER's avatar
Tobias WEBER committed
280
Qlayout.addWidget(editKi, 4,1, 1,1)
Tobias WEBER's avatar
Tobias WEBER committed
281
282
283
284
285
286
287
288
289
290
291
292
293
294
Qlayout.addWidget(editKf, 4,2, 1,1)
taslayout.addWidget(qtw.QLabel("a1/a2 (deg):", taspanel), 5,0, 1,1)
taslayout.addWidget(editA1, 5,1, 1,1)
taslayout.addWidget(editA2, 5,2, 1,1)
taslayout.addWidget(qtw.QLabel("a3/a4 (deg):", taspanel), 6,0, 1,1)
taslayout.addWidget(editA3, 6,1, 1,1)
taslayout.addWidget(editA4, 6,2, 1,1)
taslayout.addWidget(qtw.QLabel("a5/a6 (deg):", taspanel), 7,0, 1,1)
taslayout.addWidget(editA5, 7,1, 1,1)
taslayout.addWidget(editA6, 7,2, 1,1)
taslayout.addWidget(qtw.QLabel("Mono./Ana. d (A):", taspanel), 8,0, 1,1)
taslayout.addWidget(editDm, 8,1, 1,1)
taslayout.addWidget(editDa, 8,2, 1,1)
taslayout.addItem(qtw.QSpacerItem(16,16, qtw.QSizePolicy.Minimum, qtw.QSizePolicy.Expanding), 9,0, 1,2)
Tobias WEBER's avatar
Tobias WEBER committed
295

Tobias WEBER's avatar
Tobias WEBER committed
296
tabs.addTab(taspanel, "TAS")
Tobias WEBER's avatar
Tobias WEBER committed
297
298
299
300
# -----------------------------------------------------------------------------



Tobias WEBER's avatar
Tobias WEBER committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# -----------------------------------------------------------------------------
# info/settings tab
infopanel = qtw.QWidget()
infolayout = qtw.QGridLayout(infopanel)

comboA3 = qtw.QComboBox(infopanel)
comboA3.addItems(["Takin", "NOMAD", "SICS", "NICOS"])
a3_offs = [np.pi/2., np.pi, 0., 0.]
comboA3.setCurrentIndex(sett.value("a3_conv", 1, type=int))

def comboA3ConvChanged():
	idx = comboA3.currentIndex()
	tas.set_a3_offs(a3_offs[idx])
	QChanged()

comboA3.currentIndexChanged.connect(comboA3ConvChanged)


infolayout.addWidget(qtw.QLabel("TAS Calculator.", infopanel), 0,0, 1,2)
infolayout.addWidget(qtw.QLabel("Written by Tobias Weber <tweber@ill.fr>.", infopanel), 1,0, 1,2)
infolayout.addWidget(qtw.QLabel("Date: October 24, 2018.", infopanel), 2,0, 1,2)
infolayout.addItem(qtw.QSpacerItem(16,16, qtw.QSizePolicy.Minimum, qtw.QSizePolicy.Expanding), 3,0, 1,2)
infolayout.addWidget(qtw.QLabel("A3 Convention:", infopanel), 4,0, 1,1)
infolayout.addWidget(comboA3, 4,1, 1,1)

tabs.addTab(infopanel, "Infos")
# -----------------------------------------------------------------------------


Tobias WEBER's avatar
Tobias WEBER committed
330
331
332
333
334
335
336
337
338
339

# -----------------------------------------------------------------------------
# main dialog window

dlg = qtw.QDialog()
dlg.setWindowTitle("TAS Calculator")
mainlayout = qtw.QGridLayout(dlg)
mainlayout.addWidget(tabs)

xtalChanged()
Tobias WEBER's avatar
Tobias WEBER committed
340
341
comboA3ConvChanged()
#QChanged()
Tobias WEBER's avatar
Tobias WEBER committed
342

Tobias WEBER's avatar
Tobias WEBER committed
343
dlg.show()
Tobias WEBER's avatar
Tobias WEBER committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
app.exec_()


# save settings
sett.setValue("a", getfloat(editA.text()))
sett.setValue("b", getfloat(editB.text()))
sett.setValue("c", getfloat(editC.text()))
sett.setValue("alpha", getfloat(editAlpha.text()))
sett.setValue("beta", getfloat(editBeta.text()))
sett.setValue("gamma", getfloat(editGamma.text()))
sett.setValue("ax", getfloat(editAx.text()))
sett.setValue("ay", getfloat(editAy.text()))
sett.setValue("az", getfloat(editAz.text()))
sett.setValue("bx", getfloat(editBx.text()))
sett.setValue("by", getfloat(editBy.text()))
sett.setValue("bz", getfloat(editBz.text()))
sett.setValue("dm", getfloat(editDm.text()))
sett.setValue("da", getfloat(editDa.text()))
sett.setValue("h", getfloat(edith.text()))
sett.setValue("k", getfloat(editk.text()))
sett.setValue("l", getfloat(editl.text()))
#sett.setValue("E", getfloat(editE.text()))
sett.setValue("ki", getfloat(editKi.text()))
sett.setValue("kf", getfloat(editKf.text()))
Tobias WEBER's avatar
Tobias WEBER committed
368
sett.setValue("a3_conv", comboA3.currentIndex())
Tobias WEBER's avatar
Tobias WEBER committed
369
# -----------------------------------------------------------------------------