-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubmatrix.py
583 lines (468 loc) · 22.4 KB
/
ubmatrix.py
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
289
290
291
292
293
294
295
296
297
298
299
300
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
'''
Author: Alex Yee
Edit History
See Research Journal
'''
import sys
import numpy as N
import numpy as np
#import scipy
#import scipy.optimize
#import OpenOpt
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
a, b, c, alpha, beta, gamma, h1, k1, l1, omega1, chi1, phi1, h2, k2, l2, omega2, chi2, phi2=3.9091,3.9091,3.9091,90,90,90,1,0,0,0,0,17.59,0,0,1,0,90,17.59
def genout(s):
myerr=open('/tmp/calcerr.txt','w')
myerr.write(s)
myerr.close()
def star(a,b,c,alpha,beta,gamma):
"Calculate unit cell volume, reciprocal cell volume, reciprocal lattice parameters"
alpha=N.radians(alpha)
beta=N.radians(beta)
gamma=N.radians(gamma)
V=2*a*b*c*\
N.sqrt(N.sin((alpha+beta+gamma)/2)*\
N.sin((-alpha+beta+gamma)/2)*\
N.sin((alpha-beta+gamma)/2)*\
N.sin((alpha+beta-gamma)/2))
Vstar=(2*N.pi)**3/V;
astar=2*N.pi*b*c*N.sin(alpha)/V
bstar=2*N.pi*a*c*N.sin(beta)/V
cstar=2*N.pi*b*a*N.sin(gamma)/V
alphastar=N.arccos((N.cos(beta)*N.cos(gamma)-\
N.cos(alpha))/ \
(N.sin(beta)*N.sin(gamma)))
betastar= N.arccos((N.cos(alpha)*N.cos(gamma)-\
N.cos(beta))/ \
(N.sin(alpha)*N.sin(gamma)))
gammastar=N.arccos((N.cos(alpha)*N.cos(beta)-\
N.cos(gamma))/ \
(N.sin(alpha)*N.sin(beta)))
V=V #wut
alphastar=N.degrees(alphastar)
betastar=N.degrees(betastar)
gammastar=N.degrees(gammastar)
return astar,bstar,cstar,alphastar,betastar,gammastar
def calcB(astar,bstar,cstar,alphastar,betastar,gammastar,c, alpha):
"Calculates the B matrix using the crystal dimensions calculated in the 'star' method"
# print((astar,bstar,cstar,alphastar,betastar,gammastar,c, alpha))
alphastar = N.radians(alphastar)
betastar = N.radians(betastar)
gammastar = N.radians(gammastar)
alpha = N.radians(alpha)
Bmatrix=N.array([[astar, bstar*N.cos(gammastar), cstar*N.cos(betastar)],
[0, bstar*N.sin(gammastar), -cstar*N.sin(betastar)*N.cos(alpha)],
[0, 0, cstar]],'Float64') #check the third element
#"cstarN.sin(betastar)*N.sin(alpha)" for third element is equivalent
return Bmatrix
def calcU(h1, k1, l1, h2, k2, l2, omega1, chi1, phi1, omega2, chi2, phi2, Bmatrix):
"Calculates the UB matrix using 2 sets of observations (h#, k#, l#) and their respective angle measurements in degrees (omega#, chi#, phi#)"
#Convertiung angles given in degrees to radians
omega1 = N.radians(omega1)
chi1 = N.radians(chi1)
phi1 = N.radians(phi1)
omega2 = N.radians(omega2)
chi2 = N.radians(chi2)
phi2 = N.radians(phi2)
hmatrix1 = N.array([h1, k1, l1])
hmatrix2 = N.array([h2, k2, l2])
h1c = N.dot(Bmatrix, hmatrix1)
h2c = N.dot(Bmatrix, hmatrix2)
h3c = N.cross(h1c, h2c)
''' Making the orthogonal unit-vectors t#c:
t1c is parallel to h1c
t3c is orthogonal to both t1c and t2c, and thus is parallel to h3c
t2c must be orthogonal to both t1c and t2c # <--- typo im guessing(supposed to be t3c)
'''
t1c = h1c / N.sqrt(N.power(h1c[0], 2) + N.power(h1c[1], 2) + N.power(h1c[2], 2))
t3c = h3c / N.sqrt(N.power(h3c[0], 2) + N.power(h3c[1], 2) + N.power(h3c[2], 2))
t2c = N.cross(t3c, t1c)
Tc = N.array([t1c, t2c, t3c]).T
#realU=N.array([[ -5.28548868e-01, 8.65056241e-17, -6.63562568e-16],
# [ -0.00000000e+00, 4.86909792e-01, 2.90030482e-16],
# [ 0.00000000e+00, 0.00000000e+00, -1.26048191e-01]])
#calculating u_phi
u1p = N.array([N.cos(omega1)*N.cos(chi1)*N.cos(phi1) - N.sin(omega1)*N.sin(phi1),
N.cos(omega1)*N.cos(chi1)*N.sin(phi1) + N.sin(omega1)*N.cos(phi1),
N.cos(omega1)*N.sin(chi1)],'Float64')
u2p = N.array([N.cos(omega2)*N.cos(chi2)*N.cos(phi2) - N.sin(omega2)*N.sin(phi2),
N.cos(omega2)*N.cos(chi2)*N.sin(phi2) + N.sin(omega2)*N.cos(phi2),
N.cos(omega2)*N.sin(chi2)],'Float64')
u3p = N.cross(u1p, u2p)
''' Making orthogonal unit-vectors t#p
Tp should be exactaly superimposed on Tc
t#p is created the same way t#c was, except using u#p instead of h#c
'''
t1p = u1p / N.sqrt(u1p[0]**2 + u1p[1]**2 + u1p[2]**2)
t3p = u3p / N.sqrt(u3p[0]**2 + u3p[1]**2 + u3p[2]**2)
t2p = N.cross(t3p, t1p)
Tp = N.array([t1p, t2p, t3p],'Float64').T
#calculating the UB matrix
Umatrix = N.dot(Tp, Tc.T)
#UBmatrix = N.dot(Umatrix, Bmatrix)
return Umatrix
def calcUB (*args):
Umatrix = calcU(*args)
UBmatrix = N.dot(Umatrix, args[-1])
return UBmatrix
# ******************************* STAR - METHODS FOR REFINING THE UB MATRIX *******************************
def calcRefineUB(observations, wavelength):
#observations are an array of dictionaries for each observed reflection
hvectors = []
Uv = []
for i in range(0, len(observations)):
h = [observations[i]['h'], observations[i]['k'], observations[i]['l']]
hvectors.append(h)
'''
sys.stderr.write('i %3.4f \n'%(observations[i]['h'],))
sys.stderr.write('i %3.4f \n'%(observations[i]['k'],))
sys.stderr.write('i %3.4f \n'%(observations[i]['l'],))
sys.stderr.write('i %3.4f \n'%(observations[i]['twotheta'],))
sys.stderr.write('i %3.4f \n'%(observations[i]['theta'],))
sys.stderr.write('i %3.4f \n'%(observations[i]['chi'],))
sys.stderr.write('i %3.4f \n'%(observations[i]['phi'],))
'''
theta = N.radians(observations[i]['theta'])
chi = N.radians(observations[i]['chi'])
phi = N.radians(observations[i]['phi'])
twotheta = N.radians(observations[i]['twotheta'])
omega = theta - twotheta/2.0
print(("TwoTheta", twotheta, "omage", omega))
u1p = N.cos(omega)*N.cos(chi)*N.cos(phi) - N.sin(omega)*N.sin(phi)
u2p = N.cos(omega)*N.cos(chi)*N.sin(phi) + N.sin(omega)*N.cos(phi)
u3p = N.cos(omega)*N.sin(chi)
uv1p = 2.0*N.sin(twotheta/2) / wavelength * u1p
uv2p = 2.0*N.sin(twotheta/2) / wavelength * u2p
uv3p = 2.0*N.sin(twotheta/2) / wavelength * u3p
Uv.append(uv1p)
Uv.append(uv2p)
Uv.append(uv3p)
x0 = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
p = NLSP(UBRefinementEquations, x0, args=(hvectors, Uv))
r = p.solve('nlp:ralg')
a = r.xf[0]
b = r.xf[1]
c = r.xf[2]
d = r.xf[3]
e = r.xf[4]
f = r.xf[5]
g = r.xf[6]
h = r.xf[7]
j = r.xf[8]
UB = [[a, b, c], [d, e, f], [g, h, j]]
return UB
def UBRefinementEquations(x, h, Uv):
#x vector are the intial estimates
#UB matrix will be:
''' (a d e)
(d b f)
(e f c)
'''
#h is an array of h vectors (arrays): [[h,k,l], [h,k,l],...]
#Uv is an array of all of the u_phi vectors' elements sequentially: [up11, up12, up13, up21,...upn1, upn2, upn3]
a = x[0]
b = x[1]
c = x[2]
d = x[3]
e = x[4]
f = x[5]
g = x[6]
j = x[7]
k = x[8]
UB = [[a, b, c], [d, e, f], [g, j, k]]
outvec = []
for i in range(0, len(h)):
#sys.stderr.write('i %d\n'%(i,))
#sys.stdout.write('i %d\n'%(i,))
outvec.append(N.dot(UB[0], h[i]) - Uv[3*i])
outvec.append(N.dot(UB[1], h[i]) - Uv[3*i + 1])
outvec.append(N.dot(UB[2], h[i]) - Uv[3*i + 2])
return outvec
# ******************************* END - METHODS FOR REFINING THE UB MATRIX *******************************
# **************************** START - METHOD FOR OBTAINING LATTICE PARAMETERS FROM UB ****************************
def calculateLatticeParameters(UBmatrix):
G = N.linalg.inv(N.dot(UBmatrix.T, UBmatrix))
abc = N.sqrt(N.diag(G))
a = abc[0]
b = abc[1]
c = abc[2]
alpha = N.degrees(N.arccos(G[1, 2]/b/c))
beta = N.degrees(N.arccos(G[0, 2]/a/c))
gamma = N.degrees(N.arccos(G[0, 1]/a/b))
latticeParameters = {'a': a, 'b': b, 'c': c, 'alpha': alpha, 'beta': beta, 'gamma': gamma}
return latticeParameters
# **************************** END - METHOD FOR OBTAINING LATTICE PARAMETERS FROM UB ****************************
def calcTwoTheta(h, stars, wavelength):
"Calculates the twotheta value for a vector h with lattice parameters given. Useful for finding observed reflections."
q = calcq (h[0], h[1], h[2], stars)
twotheta = N.degrees(2 * N.arcsin(wavelength * q / 4 / N.pi))
print(("twoTheta: ", twotheta))
return twotheta
# *********************************** START - calculations for bisecting mode ***********************************
def calcIdealAngles(h, UBmatrix, Bmatrix, wavelength, stars):
"Calculates the remaining angles with omega given as 0"
"Returns (twotheta, theta, omega, chi, phi)"
'''myUBmatrix=N.array([[ -0.8495486120866541,0.8646150711829229,-1.055554030805845],
[-0.7090402876860106,0.7826211792587279,1.211714627203656],
[1.165768160358335,1.106088263216144,-0.03224481098900243]],'Float64')
'''
hp = N.dot(UBmatrix, h)
phi = N.degrees(N.arctan2(hp[1], hp[0]))
chi = N.degrees(N.arctan2(hp[2], N.sqrt(hp[0]**2 + hp[1]**2)))
q = calcq (h[0], h[1], h[2], stars)
twotheta = N.degrees(2 * N.arcsin(wavelength * q / 4 / N.pi))
# print(("TwoTheta", twotheta))
theta = twotheta / 2
omega = 0
#print 'chi',chi, 180-chi
#print 'phi',phi, 180+phi
return twotheta, theta, omega, chi, phi
# *********************************** END - calculations for bisecting mode ***********************************
# ********************************* START - calculations for scattering plane mode *********************************
def calcIdealAngles2 (desiredh, chi, phi, UBmatrix, wavelength, stars):
"Calculates the twotheta, theta, and omega values for a desired h vector. Uses chi and phi from calcScatteringPlane."
#Accepts the desired h vector, chi, phi, the UB matrix, the wavelength, and the stars dictionary
desiredhp = N.dot(UBmatrix, desiredh)
#Old code (scipy.optimize.fsolve) produced inaccurate results with far-off estimates
#solutions = scipy.optimize.fsolve(equations, x0, args=(h1p, h2p, wavelength))
q = calcq (desiredh[0], desiredh[1], desiredh[2], stars)
twotheta = 2.0 * N.arcsin(wavelength * q / 4.0 / N.pi)
print(("TwoTheta", twotheta))
x0 = [0.0]
p = NLSP(secondequations, x0, args=(desiredhp, chi, phi, wavelength, twotheta))
r = p.solve('nlp:ralg')
omega = r.xf[0]
theta = twotheta/2.0 + omega # ------ ALTERNATE SOLUTION FOR THETA ------
#theta = r.xf[1] # ------ SOLVER POTENTIALLY INACCURATE FOR THETA ------
solutions = [twotheta, theta, omega]
return N.degrees(solutions) #% 360
#returns an array of 3 angles [twotheta, theta, omega]
def calcScatteringPlane (h1, h2, UBmatrix, wavelength,stars):
"Calculates the chi and phi for the scattering plane defined by h1 and h2. Used with calcIdealAngles2."
#Accepts two scattering plane vectors, h1 and h2, the UB matrix, and the wavelength
h1p = N.dot(UBmatrix, h1)
h2p = N.dot(UBmatrix, h2)
x0 = [0.0, 0.0, 0.0, 0.0]
q = calcq (h1[0], h1[1], h1[2], stars)
twotheta1 = 2.0 * N.arcsin(wavelength * q / 4.0 / N.pi)
q = calcq (h2[0], h2[1], h2[2], stars)
twotheta2 = 2.0 * N.arcsin(wavelength * q / 4.0 / N.pi)
s1='before NLSP\n'
genout(s1)
p0 = NLSP(scatteringEquations, x0, args=(h1p, h2p, wavelength, twotheta1, twotheta2))
s1=s1+'after NLSP\n'
genout(s1)
r0 = p0.solve('nlp:ralg')
s1=s1+'after solve\n'
genout(s1)
chi = N.degrees(r0.xf[0]) #xf is the final array, xf[0] = chi
phi = N.degrees(r0.xf[1]) # xf[1] = phi
print(("TwoTheta", twotheta1, twotheta2))
return chi, phi
def scatteringEquations(x, h1p, h2p, wavelength,tth1,tth2):
#x vector are the intial estimates
chi = x[0]
phi = x[1]
omega1 = x[2]
omega2 = x[3]
theta1 = tth1/2
theta2 = tth2/2
print(("TwoTheta", tth1))
outvec=[h1p[0] - 2.0/wavelength * N.sin(theta1) * (N.cos(omega1)*N.cos(chi)*N.cos(phi) - N.sin(omega1)*N.sin(phi)),
h1p[1] - 2.0/wavelength * N.sin(theta1) * (N.cos(omega1)*N.cos(chi)*N.sin(phi) + N.sin(omega1)*N.cos(phi)),
h1p[2] - 2.0/wavelength * N.sin(theta1) * N.cos(omega1)*N.sin(chi),
h2p[0] - 2.0/wavelength * N.sin(theta2) * (N.cos(omega2)*N.cos(chi)*N.cos(phi) - N.sin(omega2)*N.sin(phi)),
h2p[1] - 2.0/wavelength * N.sin(theta2) * (N.cos(omega2)*N.cos(chi)*N.sin(phi) + N.sin(omega2)*N.cos(phi)),
h2p[2] - 2.0/wavelength * N.sin(theta2) * N.cos(omega2)*N.sin(chi)]
return outvec
def secondequations(x, hp, chi, phi, wavelength,tth):
#theta = x[0]
#omega = x[1]
omega=x[0]
theta=tth/2
outvec=[hp[0] - 2.0/wavelength * N.sin(theta) * (N.cos(omega)*N.cos(chi)*N.cos(phi) - N.sin(omega)*N.sin(phi)),
hp[1] - 2.0/wavelength * N.sin(theta) * (N.cos(omega)*N.cos(chi)*N.sin(phi) + N.sin(omega)*N.cos(phi)),
hp[2] - 2.0/wavelength * N.sin(theta) * N.cos(omega)*N.sin(chi)]
return outvec
# *********************************** END - calculations for scattering plane mode ***********************************
# *********************************** START - calculations for phi fixed mode ***********************************
def calcIdealAngles3 (h, UBmatrix, wavelength, phi, stars):
"Calculates the chi and theta for a desired vector h in the fixed phi mode."
#Accepts ta desired vector h, the UB matrix, the wavelength, and the fixed phi
hp = N.dot(UBmatrix, h)
q = calcq (h[0], h[1], h[2], stars)
twotheta = 2 * N.arcsin(wavelength * q / 4 / N.pi)
x0 = [0.0, 0.0]
p0 = NLSP(phiEquations, x0, args=(hp, wavelength, phi,twotheta))
r0 = p0.solve('nlp:ralg')
#r0.xf is the final array
twotheta=N.degrees(twotheta)
chi = N.degrees(r0.xf[0]) # xf[0] = chi
omega = N.degrees(r0.xf[1]) # xf[1] = omega
theta = twotheta/2.0 + omega
return twotheta, theta, omega, chi
def phiEquations(x, h1p, wavelength, phi,tth):
#x vector are the intial estimates
chi = x[0]
omega1 = x[1]
theta1=tth/2
outvec=[h1p[0] - 2.0/wavelength * N.sin(theta1) * (N.cos(omega1)*N.cos(chi)*N.cos(phi) - N.sin(omega1)*N.sin(phi)),
h1p[1] - 2.0/wavelength * N.sin(theta1) * (N.cos(omega1)*N.cos(chi)*N.sin(phi) + N.sin(omega1)*N.cos(phi)),
h1p[2] - 2.0/wavelength * N.sin(theta1) * N.cos(omega1)*N.sin(chi)]
return outvec
# *********************************** END - calculations for phi fixed mode ***********************************
def isInPlane(h1, h2, v):
"Checks if vector v lies in the plane formed by vectors h1 and h2 by calculating the determinate."
determinate = (h1[0]*h2[1]*v[2] - h1[0]*v[1]*h2[2] - h1[1]*h2[0]*v[2] + h1[1]*v[0]*h2[2] + h1[2]*h2[0]*v[1] - h1[2]*v[0]*h2[1])
if determinate == 0:
return True
else:
return False
# ******************************* START - METHODS FOR CALCULATING Q *******************************
def scalar(x1, y1, z1, x2, y2, z2, stars):
"calculates scalar product of two vectors"
a = stars['astar']
b = stars['bstar']
c = stars['cstar']
alpha = N.radians(stars['alphastar'])
beta = N.radians(stars['betastar'])
gamma = N.radians(stars['gammastar'])
s=x1*x2*a**2+y1*y2*b**2+z1*z2*c**2+(x1*y2+x2*y1)*a*b*N.cos(gamma)+(x1*z2+x2*z1)*a*c*N.cos(beta)+(z1*y2+z2*y1)*c*b*N.cos(alpha)
return s
def modvec(x, y, z, stars):
"Calculates modulus of a vector defined by its fraction cell coordinates"
"or Miller indexes"
m=N.sqrt(scalar(x, y, z, x, y, z, stars))
return m
def calcq(H, K, L, stars):
"Given reciprocal-space coordinates of a vector, calculate its coordinates in the Cartesian space."
q = modvec(H, K, L, stars);
return q
# ******************************* END - METHODS FOR CALCULATING Q *******************************
# **************************************** UB MATRIX TESTING CODE ****************************************
def UBtestrun():
"Test method to calculate UB matrix given input"
#a, b, c, alpha, beta, gamma, h1, k1, l1, omega1, chi1, phi1, h2, k2, l2, omega2, chi2, phi2 = input('enter data: ')
#Test data:
#3.9091,3.9091,3.9091,90,90,90,1,1,0,0,89.62,.001,0,0,1,0,-1.286,131.063
#maybe: 3.9091,3.9091,3.9091,90,90,90,1,1,0,-1.855,89.62,.001,0,0,1,-.0005,-1.286,131.063
#Should yield:
# a, b, c, alpha, beta, gamma, h1, k1, l1, omega1, chi1, phi1, h2, k2, l2, omega2, chi2, phi2=3.9,3.9,3.9,90,90,90,1,0,0,17.59,0,0,1,1,0,25.3,0,0
# a, b, c, alpha, beta, gamma, h1, k1, l1, omega1, chi1, phi1, h2, k2, l2, omega2, chi2, phi2=3.9091,3.9091,3.9091,90,90,90,1,1,0,0,0,0,1,-1,0,0,0,90
# a, b, c, alpha, beta, gamma, h1, k1, l1, omega1, chi1, phi1, h2, k2, l2, omega2, chi2, phi2=3.9,3.9,3.9,90,90,90,1,0,0,0,0,17.59,1,1,0,0,0,25.3
# a, b, c, alpha, beta, gamma, h1, k1, l1, omega1, chi1, phi1, h2, k2, l2, omega2, chi2, phi2=3.9091,3.9091,3.9091,90,90,90,1,0,0,0,0,17.59,0,0,1,0,90,17.59
'''
UB["0"] = -0.8495486120866541
UB["1"] = 0.8646150711829229
UB["2"] = -1.055554030805845
UB["3"] = -0.7090402876860106
UB["4"] = 0.7826211792587279
UB["5"] = 1.211714627203656
UB["6"] = 1.165768160358335
UB["7"] = 1.106088263216144
UB["8"] = -0.03224481098900243
'''
# astar, bstar, cstar, alphastar, betastar, gammastar = star(a, b, c, alpha, beta, gamma)
# stars = astar, bstar, cstar, alphastar, betastar, gammastar
# Bmatrix = calcB(astar, bstar, cstar, alphastar, betastar, gammastar, c, alpha)
stars = star(a, b, c, alpha, beta, gamma)
Bmatrix = calcB(*(list(stars)+[c, alpha]))
U = calcU(h1, k1, l1, h2, k2, l2, omega1, chi1, phi1, omega2, chi2, phi2, Bmatrix)
UB=calcUB(h1, k1, l1, h2, k2, l2, omega1, chi1, phi1, omega2, chi2, phi2, Bmatrix)
stars_dict = dict(list(zip(('astar','bstar','cstar','alphastar','betastar','gammastar'),
stars)))
onehkl = lambda i: calcIdealAngles(N.array(i,'Float64'), UB,Bmatrix, 2.35916, stars_dict)
allhkl = lambda hkl: [onehkl(i) for i in hkl]
#h, UBmatrix, Bmatrix, wavelength, stars
#result = calcIdealAngles2(hv1, hv2, UB, 2.35916)
print((calcIdealAngles([1,0,0], UB, Bmatrix, 2.35916, stars_dict)))
print((calcIdealAngles([0,1,0], UB, Bmatrix, 2.35916, stars_dict)))
print((calcIdealAngles([0,0,1], UB, Bmatrix, 2.35916, stars_dict)))
print((calcIdealAngles([1,1,1], UB, Bmatrix, 2.35916, stars_dict)))
print((calcIdealAngles([1,1,0], UB, Bmatrix, 2.35916, stars_dict)))
print("-------------------------------------------------------")
print((calcIdealAngles([1,1,1], UB, Bmatrix, 2.35916, stars_dict)))
print((calcIdealAngles([1,1,-1], UB, Bmatrix, 2.35916, stars_dict)))
print((calcIdealAngles([1,-1,1], UB, Bmatrix, 2.35916, stars_dict)))
print((calcIdealAngles([1,-1,-1], UB, Bmatrix, 2.35916, stars_dict)))
print((calcIdealAngles([-1,1,1], UB, Bmatrix, 2.35916, stars_dict)))
print((calcIdealAngles([-1,1,-1], UB, Bmatrix, 2.35916, stars_dict)))
print((calcIdealAngles([-1,-1,1], UB, Bmatrix, 2.35916, stars_dict)))
print((calcIdealAngles([-1,-1,-1], UB, Bmatrix, 2.35916, stars_dict)))
print("hello")
soa = N.array([N.append([0,0,0], N.matmul(UB, [1,0,0])), N.append([0,0,0], N.matmul(UB, [1,1,0])), N.append([0,0,0], N.matmul(UB, [0,0,1]))])
X, Y, Z, U, V, W = list(zip(*soa))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.quiver(X, Y, Z, U, V, W)
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
ax.set_zlim([0, 1])
plt.show()
return allhkl
#print result
#print 'chi',(180-result[0])%360
#print 'phi',(result[1]+180)%360
# **************************************** END OF UB MATRIX TESTING CODE ****************************************
# ***************************** ADDED CODE FOR THE ALIGNMENT MACHINE LEARNING PROGRAM *****************************
def equivalence(Q):
for i in range(len(Q)):
Q[i] = float(Q[i])
q = np.sqrt(Q[0]**2 + Q[1]**2 + Q[2]**2)
epsilon = 0.01
if (1-epsilon < q < 1+epsilon):
return [[1,0,0],[0,1,0],[0,0,1],[-1,0,0],[0,-1,0],[0,0,-1]]
elif (np.sqrt(2)-epsilon < q < (np.sqrt(2)+epsilon)):
return [[1,1,0],[1,0,1],[0,1,1],[1,-1,0],[1,0,-1],[-1,1,0],[-1,0,1],[-1,-1,0],[-1,0,-1],[0,-1,1],[0,-1,-1],[0,1,-1]]
elif (np.sqrt(3)-epsilon < q < np.sqrt(3)+epsilon):
return [[1,1,1],[1,1,-1],[1,-1,1],[1,-1,-1],[-1,1,1],[-1,1,-1],[-1,-1,1],[-1,-1,-1]]
def initial(u1, u2, theta, B):
"""
Finds two reflections (100) and (010) for the crystal so the UB matrix can be calculated
"""
# Currently only works for crystals with the 100 facing the z axis. NEED TO FIX THIS SO IT WORKS FOR ANY CRYSTAL
phi1 = theta
chi1 = 90
if u2[0] == 0 and u2[1] > 0:
phi2 = 90 + theta
elif u2[0] == 0 and u2[1] < 0:
phi2 = -90+theta
else:
phi2 = theta + np.rad2deg(np.arctan2(float(u2[1]),u2[0]))
chi2 = 0
return [1, 0, 0, 0, 1, 0, 0, chi1, phi1, 0, chi2, phi2, B]
def motor_pos(v):
"""
returns the motor positons of the equivalent HKLs. Assumes 100 faces x axis and 010 faces y axis.
"""
stars = star(a, b, c, alpha, beta, gamma)
stars_dict = dict(list(zip(('astar','bstar','cstar','alphastar','betastar','gammastar'),stars)))
l = []
Bmatrix = calcB(*(list(stars)+[c, alpha]))
eq = equivalence(v)
for i in range(len(eq)):
l.append(eq[i])
pre = initial([0,1,0], [0,0,1], 17.59, Bmatrix)
UB = calcUB(*pre)
angles = []
for i in range(len(l)):
# print(calcIdealAngles(l[i], UB, Bmatrix, 2.35916, stars_dict)[-2:])
angles.append([int(round(calcIdealAngles(l[i], UB, Bmatrix, 2.35916, stars_dict)[-2]))%360, int(round(calcIdealAngles(l[i], UB, Bmatrix, 2.35916, stars_dict)[-1]))%360])
# angles.append([round(int(calcIdealAngles(l[i], UB, Bmatrix, 2.35916, stars_dict)[-2])%360), round(int(calcIdealAngles(l[i], UB, Bmatrix, 2.35916, stars_dict)[-1]%360))])
# print(angles, "a")
# angles[i]=round(angles[i])
# return np.array_split(angles, len(l))
return angles
# ***************************** END OF MACHINE LEARNING PROGRAM CODE *****************************
if __name__=="__main__":
pi=N.pi
a=3.9; b=3.9; c=3.9
alpha=90; beta=90; gamma=90
recip=star(a,b,c,alpha,beta,gamma)
# print(a,b,c,alpha,beta,gamma)
print (recip)
UBtestrun()
print('done!')
print(motor_pos([1,1,1]))
# print(twotheta)