-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlattice.py
632 lines (551 loc) · 20.6 KB
/
lattice.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
"""
Lattice calcs
Authors/Modifications:
-----------------------
* Tom Trainor ([email protected])
* convert matlab to python, Kunal Tanwar
* Convert classes, TPT
Notes on Convention and Generalized Transforms
----------------------------------------------
The set of basis vectors that defines the real lattice are
covarient quantities, while the set of indicies that define
real space vectors are contravarient:
|a|
v = (x,y,z)* |b| = x*a + y*b + z*c
|c|
If A is an arbitrary matrix that transforms the contravarient
vector indicies, its assumed that the indicies left multiply
(x',y',z') = (x,y,z) * A
Note that the usual convention is to express covarient quantities
as column vectors and contravarient quantities as row vectors
(subscripts and superscripts respectivley in tensor notation)
However, in the notes below we'll just take 1-D arrays to be
column vectors to simplify the notation. Therefore, if A is defined
as above, the matrix vector multiplications will need to be changed to:
|x'| |x|
|y'| = transpose(A)* |y|
|z'| |z|
We can express the same in numpy given a vector v and matrix A
dot(v,A) = dot(transpose(A),v)
where v is multiplied as a row vector on the lhs and a column
vector on the rhs; ie the transpose of v bewteen column and
row vecor is implied by its position.
Assume there is a matrix that transforms the covarient basis
(via rotation and/or dilation) to a new set of basis vectors:
|a'| |a| a' = (F11*a + F12*b + F13*c)
|b'| = F* |b| or b' = (F21*a + F22*b + F23*c)
|c'| |c| c' = (F31*a + F32*b + F33*c)
with a well defined inverse:
|a| |a'|
|b| = inv(F)* |b'|
|c| |c'|
The covarient/contravarient relationship bewteen basis vectors
and the vector indicies implies that the indicies of a (stationary)
vector in the new (') basis are obtained via the inverse of the
F matrix:
|x'| |x|
|y'| = M* |y|
|z'| |z|
where
M = transpose(inv(F)) = inv(transpose(F))
The reciprocal relation is:
|x| |x'|
|y| = N* |y'|
|z| |z'|
where
N = inv(M) = transpose(F)
With respect to the real lattice the recip lattice basis vectors
are contravarient and the vector indicies are covarient:
|h|
vr = (ar,br,cr)* |k| = h*ar + k*br + l*cr
|l|
Given the generalized basis transform F above, the covarient recip
lattice indicies transform in the same way as the lattice.
Therefore:
|h'| |h|
|k'| = F* |k|
|l'| |l|
The reciprocal relation is:
|h| |k'|
|k| = G* |k'|
|l| |l'|
where
G = inv(F) = transpose(M)
The metric tensor
------------------
The metric tensor is defined in terms of the vector dot product
for a general real space system
dot(u,v) = u*g*v
where
|a*a, a*b, a*c|
g = |b*a, b*b, b*c|
|c*a, c*b, c*c|
Therefore, (with a,b,c, correspoding to vectors magnitudes):
g = [ [ a*a, a*b*cosd(gam), a*c*cosd(bet) ],
[ b*a*cosd(gam), b*b, b*c*cosd(alp) ],
[ c*a*cosd(bet), c*b*cosd(alp), c*c ] ]
The metric tensor may also be defined in terms of a lattice transform
ie the real space basis vectors can be expressed in terms of the
recip space basis vectors via
|a| |ar|
|b| = g* |br|
|c| |cr|
therefore
|ar| |a|
|br| = gr* |b|
|cr| |c|
where gr = inv(g). Hence the recip lattice vectors
can be plotted in the real lattice via
ar = gr11*a + gr12*b + gr13*c
br = gr21*a + gr22*b + gr23*c
cr = gr31*a + gr32*b + gr33*c
and visa versa
Using the above relations for lattice transforms the
indicies of real lattice vectors can be calc in the recip
lattice and visa-versa via:
|h| |x| |x|
|k| = (x,y,z)*g = transpose(g)* |y| = M* |y|
|l| |z| |z|
and
|x| |h| |h|
|y| = inv(transpose(g))* |k| = N* |k|
|z| |l| |l|
e.g. in above we take recip lattice as primed basis and
real lattice as the unprimed basis, therefore equate:
F = gr
G = inv(F) = inv(gr) = g
M = transpose(inv(F)) = transpose(g)
N = inv(M) = transpose(F) = inv(transpose(g)) = transpose(gr)
Note that g and gr are always symmetric, therefore
g = transpose(g) and gr = transpose(gr).
F = N = gr
G = M = g
"""
##########################################################################
import numpy as num
from mathutil import cosd, sind, tand
from mathutil import arccosd, arcsind, arctand
##########################################################################
class Lattice:
"""
Class that defines a lattice and various
operations within the lattice
"""
def __init__(self,a=10.,b=10.,c=10.,alpha=90.,beta=90.,gamma=90.,lam=1.5406):
"""
Initialize
Parameters:
-----------
* a,b,c in angstroms
* alpha, beta, gamma in degrees,
* lambda in angstroms (default lambda is Cu Ka1)
"""
self.update(a=a,b=b,c=c,alpha=alpha,beta=beta,gamma=gamma,lam=lam)
def __repr__(self,):
""" display """
lout = "a=%6.5f, b=%6.5f, c=%6.5f" % (self.a, self.b, self.c)
lout = "%s, alpha=%6.5f,beta=%6.5f,gamma=%6.5f\n" % (lout,
self.alpha,
self.beta,
self.gamma)
lout = "%sar=%6.5f, br=%6.5f, cr=%6.5f" % (lout,self.ar, self.br, self.cr)
lout = "%s, alphar=%6.5f,betar=%6.5f,gammar=%6.5f\n" % (lout,
self.alphar,
self.betar,
self.gammar)
lout = "%sDefault wavelength for angle calculations=%6.5f\n" % (lout,
self.lam)
return lout
def update(self,a=None,b=None,c=None,alpha=None,beta=None,gamma=None,lam=None):
"""
Update lattice parameters
"""
if a != None: self.a = float(a)
if b != None: self.b = float(b)
if c != None: self.c = float(c)
if alpha != None: self.alpha = float(alpha)
if beta != None: self.beta = float(beta)
if gamma != None: self.gamma = float(gamma)
if lam != None: self.lam = float(lam)
# update calc quantities
self._calc_g()
def cell(self):
"""
Return array of real lattice cell parameters
"""
return num.array([self.a,self.b,self.c,
self.alpha,self.beta,self.gamma],dtype=float)
def rcell(self):
"""
Return array of reciprocal lattice cell parameters
"""
return num.array([self.ar,self.br,self.cr,
self.alphar,self.betar,self.gammar],dtype=float)
def _calc_g(self):
"""
Calculate the metric tensors and recip lattice params
self.g = real space metric tensor
self.gr = recip space metric tensor
"""
(a,b,c,alp,bet,gam) = self.cell()
# real metric tensor
self.g = num.array([ [ a*a, a*b*cosd(gam), a*c*cosd(bet) ],
[ b*a*cosd(gam), b*b, b*c*cosd(alp) ],
[ c*a*cosd(bet), c*b*cosd(alp), c*c ] ])
# recip lattice metric tensor
# and recip lattice params
self.gr = num.linalg.inv(self.g)
self.ar = num.sqrt(self.gr[0,0])
self.br = num.sqrt(self.gr[1,1])
self.cr = num.sqrt(self.gr[2,2])
self.alphar = arccosd(self.gr[1,2]/(self.br*self.cr))
self.betar = arccosd(self.gr[0,2]/(self.ar*self.cr))
self.gammar = arccosd(self.gr[0,1]/(self.ar*self.br))
def vol(self,recip=False):
"""
Calculate the cell volume.
Parameters:
-----------
* If recip == False, this is the real space vol in ang**3
If recip == True, this is the recip space vol in ang**-3
Notes:
------
V_real = sqrt(determiant(g))
V_recip = sqrt(determiant(inv(g)))
"""
if recip == True: g = self.gr
else: g = self.g
det = num.linalg.det(g)
if det > 0:
return num.sqrt(det)
else:
return 0.0
def dot(self,u,v,recip=False):
"""
Calculate dot product of two vectors (u,v)
Notes:
------
* If u and v are real space vectors, use recip = False
* If u and v are recip space vectors, use recip = True
Note u and v are assumed to be normal numpy arrays
(ie not matrix objects)
The generalized dot product for real space vectors
may be defined in terms of the metric tensor (g):
dot(u,v) = u*g*v
If u and v are recip lattice vectors replace g with
gr = inv(g)
"""
if recip == True: g = self.gr
else: g = self.g
u = num.array(u,dtype=float)
v = num.array(v,dtype=float)
return num.dot(u,num.dot(g,v))
def mag(self,v,recip=False):
"""
Calculate the norm of a vector (v)
Notes:
------
* If v is real space vector, use recip = False
* If v is recip space vector, use recip = True
Note v is assumed to be normal numpy array
(ie not matrix objects)
"""
m = num.sqrt(self.dot(v,v,recip=recip))
return m
def angle(self,u,v,recip=False):
"""
Calculate the angle between two vectors (u,v)
Notes:
------
* If u and v are real space vectors, use recip = False
* If u and v are recip space vectors, use recip = True
Note u and v are assumed to be normal numpy arrays
(ie not matrix objects)
"""
uv = self.dot(u,v,recip=recip)
um = self.mag(u,recip=recip)
vm = self.mag(v,recip=recip)
arg = uv/(um*vm)
if num.fabs(arg) > 1.0:
arg = arg / num.fabs(arg)
alpha = arccosd(arg)
return alpha
def angle_rr(self,x,h):
"""
Calculate the angle between a real vector x = [x,y,z]
and a recip vector h = [h,k,l]
"""
x = num.array(x,dtype=float)
h = num.array(h,dtype=float)
hx = num.sum(x*h)
xm = self.mag(x,recip=False)
hm = self.mag(h,recip=True)
arg = hx/(hm*xm)
if num.fabs(arg) > 1.0:
arg = arg / num.fabs(arg)
alpha = arccosd(arg)
return alpha
def d(self,hkl):
"""
Calculate d space for given [h,k,l]
"""
if len(hkl)!=3:
print "Error need an array of [h,k,l]"
return 0.
h = self.mag(hkl,recip=True)
if h == 0.:
#print "Error [h,k,l] magnitude is zero:", hkl
return 0.0
d = 1./h
return d
def tth(self,hkl,lam=None):
"""
Calculate 2Theta for given [h,k,l] and wavelength
Notes:
------
If lam is None the default lambda will be used (Cu Ka1)
If you pass in lam, this will change the default for
subsequent calls
"""
if lam != None: self.lam = float(lam)
d = self.d(hkl)
if d == 0.: return 0.
r = self.lam/(2.*d)
if num.fabs(r) > 1.0:
r = r/num.fabs(r)
tth = 2.*arcsind(r)
return tth
def dvec(self,hkl):
"""
Calculate the real space vector d
which has a magnitude of the d spacing
and is normal to the plane hkl = [h,k,l]
"""
# convert hkl vector to real space indicies
#hkl = num.array(hkl,dtype=float)
#dvec = num.dot(self.gr,hkl)
dvec = self.recip_to_real(hkl)
dspc = self.d(hkl)
if dspc == 0: return num.array([0.,0.,0.])
dvec = (dspc**2.)*dvec
return dvec
def recip_to_real(self,hkl):
"""
Given recip vector hkl = [h,k,l] calculate the
vectors indicies in the real lattice
"""
hkl = num.array(hkl,dtype=float)
#v = num.dot(self.gr.transpose(),hkl)
v = num.dot(self.gr,hkl)
return v
def real_to_recip(self,v):
"""
Given real vector v = [x,y,z] calculate the
vectors indicies in the reciprocal lattice
"""
v = num.array(v,dtype=float)
hkl = num.dot(v,self.g)
return hkl
##########################################################################
class LatticeTransform:
"""
Generalized lattice transformations
"""
def __init__(self,lattice,Va=None,Vb=None,Vc=None,shift=None):
"""
Initialize
Parameters:
----------
* lattice is a Lattice instance.
All transforms are defined with respect to this
lattice, ie this is the original unprimed lattice
* Va, Vb, Vc are the lattice vectors that define the
a',b',c' coordinate system (rotation and contraction)
* shift is the lattice shift vector
* (V's and shift are defined in the unprimed system)
"""
self.lattice = lattice
self.Va = num.array([1.,0.,0.])
self.Vb = num.array([0.,1.,0.])
self.Vc = num.array([0.,0.,1.])
self.shift = num.array([0.,0.,0.])
self._update(Va=Va,Vb=Vb,Vc=Vc,shift=shift)
def basis(self,Va=None,Vb=None,Vc=None,shift=None):
"""
Define new basis vectors.
Parameters:
----------
* Va, Vb and Vc should define the a',b',c' lattice
vectors of the new basis (rotation/dialation part).
* Shift describes an origin shift of the new lattice
(ie take the new basis defined by Va,Vb,Vc then
apply the shift vector)
Notes:
------
All the vectors should be defined in terms of fractional
coordinate indicies in the original basis, for example:
a' = x1*a + y1*b + z1*c
b' = x2*a + y2*b + z2*c
c' = x3*a + y3*b + z3*c
shift = x4*a + y4*b + z4*c
and
Va = [x1,y1,y1]
Vb = [x2,y2,y2]
Vc = [x3,y3,y3]
shift = [x4,y4,z4]
"""
self._update(Va=Va,Vb=Vb,Vc=Vc,shift=shift)
def _update(self,Va=None,Vb=None,Vc=None,shift=None):
""" update """
if Va != None: self.Va = num.array(Va,dtype=float)
if Vb != None: self.Vb = num.array(Vb,dtype=float)
if Vc != None: self.Vc = num.array(Vc,dtype=float)
if shift != None: self.shift = num.array(shift,dtype=float)
F = [self.Va,self.Vb,self.Vc]
self.F = num.array(F, dtype=float)
self.G = num.linalg.inv(self.F)
self.M = self.G.transpose()
self.N = self.F.transpose()
def cartesian(self,shift=[0.,0.,0.]):
"""
Calculates a cartesian basis using:
Va = a' is parallel to a
Vb = b' is perpendicular to a' and in the a/b plane
Vc = c' is perpendicular to the a'/c' plane
A shift vector may be specified to shift the origin
of the cartesian lattice relative to the original lattice
origin (specify shift in fractional coordinates of
the original lattice)
"""
(a,b,c,alp,bet,gam) = self.lattice.cell()
(ar,br,cr,alpr,betr,gamr) = self.lattice.rcell()
Va = [1./a, 0. , 0.]
Vb = [-1./(a*tand(gam)), 1./(b*sind(gam)), 0.]
Vc = [ar*cosd(betr), br*cosd(alpr), cr]
self.basis(Va=Va,Vb=Vb,Vc=Vc,shift=shift)
def xp(self,x):
"""
Given x = [x,y,z] in the original lattice
compute the indicies of the vector in the new basis
xp = M*(x - shift)
"""
x = num.array(x,dtype=float)
if self.shift.sum() != 0.:
x = x - self.shift
xp = num.dot(self.M,x)
return xp
def x(self,xp):
"""
Given xp = [x',y',z'] in the primed basis
compute the indicies of the vector in the original basis
x = N*(xp + shiftp) = N*xp + shift
"""
xp = num.array(xp,dtype=float)
x = num.dot(self.N,xp)
if self.shift.sum() != 0.:
x = x + self.shift
return x
def hp(self,h):
"""
Given h = [h,k,l] in the original (recip) basis
compute the indicies of the vector in the primed
(recip) basis
"""
h = num.array(h,dtype=float)
return num.dot(self.F,h)
def h(self,hp):
"""
Given hp = [h',k',l'] in the primed (recip) basis
compute the indicies of the vector in the original
(recip) basis
"""
hp = num.array(hp,dtype=float)
return num.dot(self.G,hp)
def plat(self):
"""
Return a Lattice instance for the primed basis
"""
a = self.lattice.mag(self.Va)
b = self.lattice.mag(self.Vb)
c = self.lattice.mag(self.Vc)
alp = self.lattice.angle(self.Vb,self.Vc)
bet = self.lattice.angle(self.Va,self.Vc)
gam = self.lattice.angle(self.Va,self.Vb)
return Lattice(a,b,c,alp,bet,gam)
##########################################################################
##########################################################################
def test_lattice():
# create a new lattice instance
cell = Lattice(5.6,5.6,13,90,90,120)
# show lattice parameters and real
# recip lattice cell volumes
print cell
print cell.vol()
print cell.vol(recip=True)
# what the hkl = [0,0,1] dspace
# and 2 theta value for lambda = 1 ang.
print cell.d([0,0,1])
print cell.tth([0,0,1],lam=1.)
# create a real space vector perpendicular
# to hkl = [0,0,1], with magnitude = dspace(001)
dv = cell.dvec([0,0,1])
print dv
print cell.mag(dv)
# compute the angle between some vectors
# recip lattice [1,0,0] and [0,1,0]
print cell.angle([1,0,0],[0,1,0],recip=True)
# recip lattice [1,0,0] and [0,0,1]
print cell.angle([1,0,0],[0,0,1],recip=True)
# recip lattice [0,0,1] and [1,1,1]
print cell.angle([0,0,1],[1,1,1],recip=True)
# real lattice [1,0,0] and [0,1,0]
print cell.angle([1,0,0],[0,1,0])
# real lattice [1,0,0] and [0,0,1]
print cell.angle([1,0,0],[0,0,1])
# real lattice [0,0,1] and [1,1,1]
print cell.angle([0,0,1],[1,1,1])
# real [0,0,1] and recip [0,0,1]
print cell.angle_rr([0,0,1],[0,0,1])
# real [1,1,1] and recip [0,0,1]
print cell.angle_rr([1,1,1],[0,0,1])
# real dv and recip [0,0,1]
print cell.angle_rr(dv,[0,0,1])
# real dv and recip [1,1,0]
print cell.angle_rr(dv,[1,1,0])
return cell
def test_transform():
# create a new hexagonal lattice instance
cell = Lattice(5.6,5.6,13,90,90,120)
# test lattice transform
# below defines rhombohedral basis vectors
# in terms of the heaxagonal basis vectors
Va_rhom = [0.6667, 0.3333, 0.3333]
Vb_rhom = [-0.3333, 0.3333, 0.3333]
Vc_rhom = [-0.3333, -0.6667, 0.3333]
t = LatticeTransform(cell,Va=Va_rhom,Vb=Vb_rhom,Vc=Vc_rhom)
# create an instance of the rhombohedral lattice
# shows the rhomb lattice params
print t.plat()
# given the hkl = [001] in hex calc hkl in rhom
print t.hp([0,0,1])
# given the hkl = [111] in rhom calc hkl in hex
print t.h([1,1,1])
# create a cartesian representation
t.cartesian()
cart = t.plat()
print cart
# convert a [1,1,0] vector in hex lattice to cartesian
vc = t.xp([1,1,0])
print vc
# check that the vector is the same length
print cell.mag([1,1,0])
print cart.mag(vc)
return t
##########################################################################
##########################################################################
if __name__ == "__main__":
"""
test
"""
test_lattice()
test_transform()