-
Notifications
You must be signed in to change notification settings - Fork 1
/
cxtallite.pyx
1301 lines (1113 loc) · 43.1 KB
/
cxtallite.pyx
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: UTF-8 no BOM -*-
# filename: corientation.pyx
"""
________ ___ ___________ __
/ ____/\ \/ / |/ /_ __/ | / /
/ / \ /| / / / / /| | / /
/ /___ / // | / / / ___ |/ /___
\____/ /_//_/|_|/_/ /_/ |_/_____/
Copyright (c) 2015, C. Zhang.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1) Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2) Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
DESCRIPTION
-----------
symmetry: function
provide symmetry operators in quaternion for different crystal structure
Quaternion: extension class
quaternion representation of 3D orientation
Eulers: extension class
Euler angle representation of 3D orientation
OrientationMatrix: extension class
Matrix representation of 3D orientation
Rodrigues: extension class
Rodrigue vector representation of 3D orientation
Xtallite: extension class
physically equivalent to material point
Aggregate: extension class
physically equivalent to grain
"""
import cython
import math, random, os
import numpy as np
cimport numpy as np
from libc.math cimport sin, cos, sqrt, atan2, M_PI, atan
#############################
# SETUP FOR TYPE DEFINITION #
#############################
np.import_array()
cdef extern from "numpy/npy_math.h":
double NPY_INFINITY
# Determine the right dtype for arrays of indices at compile time.
IF UNAME_MACHINE[-2:] == '64':
intp = np.int64
ELSE:
intp = np.int32
DTYPE = np.float64
# CONSTANT #
cdef list lattice_hcp = ['hexagonal', 'hex', 'hcp']
cdef list lattice_cubic = ['bcc', 'fcc', 'cubic']
cdef list lattice_tet = ['tetragonal']
cdef list lattice_orth = ['orthorhombic']
cdef list lattice_tric = ['triclinic']
cdef DTYPE_t d2r = M_PI/180.0
cdef DTYPE_t sqrt_2 = sqrt(2.0)
cdef DTYPE_t sqrt_3 = sqrt(3.0)
#----------------------#
# MODULE LEVEL CLASSES #
#----------------------#
cdef class Quaternion:
"""
DESCRIPTION
-----------
Quaternion(np.array([w,x,y,z]))
Quaternion is a set of numerics that extends from complex number,
where a imaginary space (x,y,z) is constructed to facilitate a close
set.
Particularly, the unitary quaternions correspond to the rotation
operation in 3D space, which is why many computer graphics used it
to perform fast rotation calculations.
PARAMETERS
----------
q: DTYPE[:]
Simple vector with length 4
METHODS
-------
unitary(self)
Return a unitary quaternion, useful for using quaternion to represent
rotation/orientation.
conj(self)
Return the conjugate of the quaternion
tolist(self)
Return the quaternion as a simple python list
tondarray(self)
Return the quaternion as a numpy array (preferred)
toEulers(self)
Convert a unitary quaternion into Euler Angles (np.ndarray)
toRodrigues(self)
Convert a unitary quaternion into Rodrigue vector (np.ndarray)
toOrientationMatrix(self)
Convert a unitary quaternion into Orientation Matrix (np.ndarray)
CLASSMETHOD
-----------
scale(Quaternion q, DTYPE_t scalar)
Scale a quaternion vector with given scalar.
rotate(Quaternion q, DTYPE_t[:] pt)
Rotate pt around origin by q.
average(list qs)
Return an approximation of the average quaternion (forced to unitary)
for qs (list of quaternions).
"""
def __init__(self, DTYPE_t[:] q):
cdef DTYPE_t sgn = DTYPE_sgn(q[0])
self.w = q[0] * sgn
self.x = q[1] * sgn
self.y = q[2] * sgn
self.z = q[3] * sgn
def __copy__(self):
return Quaternion([self.w,self.x,self.y,self.z])
def __richcmp__(self, Quaternion other, int op):
cdef bint flag
flag = ( abs( self.w-other.w) < 1e-8 and \
abs( self.x-other.x) < 1e-8 and \
abs( self.y-other.y) < 1e-8 and \
abs( self.z-other.z) < 1e-8) \
or \
( abs(-self.w-other.w) < 1e-8 and \
abs(-self.x-other.x) < 1e-8 and \
abs(-self.y-other.y) < 1e-8 and \
abs(-self.z-other.z) < 1e-8)
if op == 2: #__eq__
return flag
elif op == 3:
return not flag
else:
return NotImplemented
def __add__(self, Quaternion other):
cdef np.ndarray newQ = np.zeros(4, dtype=DTYPE)
newQ[0] = self.w + other.w
newQ[1] = self.x + other.x
newQ[2] = self.y + other.y
newQ[3] = self.z + other.z
return Quaternion(newQ)
def __iadd__(self, Quaternion other):
self.w = self.w + other.w
self.x = self.x + other.x
self.y = self.y + other.y
self.z = self.z + other.z
return self
def __sub__(self, Quaternion other):
cdef np.ndarray newQ = np.zeros(4, dtype=DTYPE)
newQ[0] = self.w - other.w
newQ[1] = self.x - other.x
newQ[2] = self.y - other.y
newQ[3] = self.z - other.z
return Quaternion(newQ)
def __isub__(self, Quaternion other):
self.w = self.w - other.w
self.x = self.x - other.x
self.y = self.y - other.y
self.z = self.z - other.z
return self
def __mul__(self, Quaternion other):
cdef np.ndarray newQ = np.zeros(4, dtype=DTYPE)
cdef DTYPE_t Aw = self.w
cdef DTYPE_t Ax = self.x
cdef DTYPE_t Ay = self.y
cdef DTYPE_t Az = self.z
cdef DTYPE_t Bw = other.w
cdef DTYPE_t Bx = other.x
cdef DTYPE_t By = other.y
cdef DTYPE_t Bz = other.z
newQ[0] = - Ax * Bx - Ay * By - Az * Bz + Aw * Bw
newQ[1] = + Ax * Bw + Ay * Bz - Az * By + Aw * Bx
newQ[2] = - Ax * Bz + Ay * Bw + Az * Bx + Aw * By
newQ[3] = + Ax * By - Ay * Bx + Az * Bw + Aw * Bz
return Quaternion(newQ)
def __imul__(self, Quaternion other):
cdef DTYPE_t Aw = self.w
cdef DTYPE_t Ax = self.x
cdef DTYPE_t Ay = self.y
cdef DTYPE_t Az = self.z
cdef DTYPE_t Bw = other.w
cdef DTYPE_t Bx = other.x
cdef DTYPE_t By = other.y
cdef DTYPE_t Bz = other.z
self.w = - Ax * Bx - Ay * By - Az * Bz + Aw * Bw
self.x = + Ax * Bw + Ay * Bz - Az * By + Aw * Bx
self.y = - Ax * Bz + Ay * Bw + Az * Bx + Aw * By
self.z = + Ax * By - Ay * Bx + Az * Bw + Aw * Bz
return self
def __div__(self, Quaternion other):
return self * other.conj()
def __idiv__(self, Quaternion other):
cdef Quaternion tmp = self * other.conj()
self.w = tmp.w
self.x = tmp.x
self.y = tmp.y
self.z = tmp.z
return self
def __str__(self):
tmp = "({}, <{},{},{}>)".format(self.w, self.x, self.y, self.z)
return tmp
def __repr__(self):
tmp = "Quaternion(real={:.4f}, imag=<{:.4f},{:.4f},{:.4f}>".format(self.w,
self.x,
self.y,
self.z)
return tmp
def __abs__(self):
cdef double tmp
tmp = self.w*self.w + \
self.x*self.x + \
self.y*self.y + \
self.z*self.z
tmp = sqrt(tmp)
return tmp
def __len__(self):
return 4
def __neg__(self):
self.w = -self.w
self.x = -self.x
self.y = -self.y
self.z = -self.z
return self
def unitary(self):
cdef double length = abs(self)
cdef np.ndarray newQ = np.zeros(4, dtype=DTYPE)
newQ[0] = self.w/length
newQ[1] = self.x/length
newQ[2] = self.y/length
newQ[3] = self.z/length
return Quaternion(newQ)
def conj(self):
"""
DESCRIPTION
-----------
q.conj()
Representing the inverse rotation of q, provided
q is a unitary quaternion.
"""
cdef np.ndarray newQ = np.zeros(4, dtype=DTYPE)
newQ[0] = self.w
newQ[1] = -self.x
newQ[2] = -self.y
newQ[3] = -self.z
return Quaternion(newQ)
def tolist(self):
return [self.w, self.x, self.y, self.z]
def tondarray(self):
return np.array(self.tolist())
def toEulers(self, inDegrees=True, inStandardRange=True):
"""
Conversion of ACTIVE rotation to Euler angles taken from:
Melcher, A.; Unser, A.; Reichhardt, M.; Nestler, B.; Pötschke, M.; Selzer, M.
Conversion of EBSD data by a quaternion based algorithm to be used for grain structure simulations
Technische Mechanik 30 (2010) pp 401--413
"""
cdef np.ndarray angles = np.zeros(3, dtype=DTYPE)
cdef DTYPE_t x = 0.0
cdef DTYPE_t y = 0.0
cdef DTYPE_t chi = 0.0
cdef Quaternion q = self.unitary()
if DTYPE_abs(q.x)<1e-4 and DTYPE_abs(q.y)<1e-4:
x = q.w**2.0 - q.z**2.0
y = 2.0 * q.w * q.z
angles[0] = atan2(y,x)
elif DTYPE_abs(q.w) < 1e-4 and DTYPE_abs(q.z)<1e-4:
x = q.x**2.0 - q.y**2.0
y = 2.0*q.x*q.y
angles[0] = atan2(y,x)
angles[1] = M_PI
else:
chi = sqrt((q.w**2 + q.z**2)*(q.x**2 + q.y**2))
x = (q.w * q.x - q.y * q.z)/2./chi
y = (q.w * q.y + q.x * q.z)/2./chi
angles[0] = atan2(y,x)
x = q.w**2 + q.z**2 - (q.x**2 + q.y**2)
y = 2.*chi
angles[1] = atan2(y,x)
x = (q.w * q.x + q.y * q.z)/2./chi
y = (q.z * q.x - q.y * q.w)/2./chi
angles[2] = atan2(y,x)
if inStandardRange:
angles[0] %= 2*M_PI
if angles[1] < 0.0:
angles[1] = angles[1] + M_PI
angles[2] = -angles[2]
angles[2] %= 2*M_PI
if inDegrees:
angles = np.degrees(angles)
return angles
def toRodrigues(self):
cdef np.ndarray r = np.ones(3, dtype=DTYPE)
if DTYPE_abs(self.w)<1e-6:
r = np.inf * r
else:
r[0] = self.x/self.w
r[1] = self.y/self.w
r[2] = self.z/self.w
return r
def toOrientationMatrix(self):
cdef Quaternion q = self.unitary()
cdef np.ndarray m = np.empty((3,3), dtype=DTYPE)
# this returns rotation matrix, not orientation matrix
m = np.array([[1.0-2.0*(q.y*q.y+q.z*q.z), 2.0*(q.x*q.y-q.z*q.w), 2.0*(q.x*q.z+q.y*q.w)],
[ 2.0*(q.x*q.y+q.z*q.w), 1.0-2.0*(q.x*q.x+q.z*q.z), 2.0*(q.y*q.z-q.x*q.w)],
[ 2.0*(q.x*q.z-q.y*q.w), 2.0*(q.x*q.w+q.y*q.z), 1.0-2.0*(q.x*q.x+q.y*q.y)]])
return m.T
def toAngleAxis(self):
"""
DESCRIPTION
-----------
angle, rotation_axis = q.toAngleAxis()
Return the angle-axis pair that equivalent to the rotation
represented by q.unitary().
RETURNS
-------
(angle, v) : tuple
"""
cdef np.ndarray v = np.zeros(3, dtype=DTYPE)
cdef Quaternion q = self.unitary()
cdef DTYPE_t s,x,y,angle
s = sqrt(1.0 - q.w**2)
x = 2.0*q.w**2 - 1.0
y = 2.0*q.w*s
angle = atan2(y,x)
if angle < 0.0:
angle = -angle
s = -s
if DTYPE_abs(angle) < 1e-4:
v[0] = 1.0
else:
v[0] = q.x/s
v[1] = q.y/s
v[2] = q.z/s
return (angle, v)
@classmethod
def scale(cls, Quaternion q, double s):
"""
DESCRIPTION
-----------
newQ = Quaternion.scale(q, s)
Scale a quaternion with given scalar
PARAMETERS
----------
q: Quaternion
Quaternion to scale
s: double
Scaling amount
"""
cdef np.ndarray newQ = np.zeros(4, dtype=DTYPE)
newQ[0] = q.w * s
newQ[1] = q.x * s
newQ[2] = q.y * s
newQ[3] = q.z * s
return Quaternion(newQ)
@classmethod
def rotate(cls, Quaternion q, DTYPE_t[:] pt):
"""
DESCRIPTION
-----------
newPt = Quaternion.rotate(q, pt)
active rotation of pt using quaternion q, namely
'newPt = q * pt * q.conj()'
PARAMETERS
----------
q: Quaternion
quaternion defining rotation
pt: np.ndarray
Point vector
RETURNS
-------
newPt: np.ndarray
new point vector
"""
cdef np.ndarray newPt = np.zeros(3, dtype=DTYPE)
cdef Quaternion rotQ = q.unitary()
cdef DTYPE_t w = rotQ.w
cdef DTYPE_t x = rotQ.x
cdef DTYPE_t y = rotQ.y
cdef DTYPE_t z = rotQ.z
cdef DTYPE_t Vx = pt[0]
cdef DTYPE_t Vy = pt[1]
cdef DTYPE_t Vz = pt[2]
newPt[0] = w * w * Vx + 2 * y * w * Vz - 2 * z * w * Vy + \
x * x * Vx + 2 * y * x * Vy + 2 * z * x * Vz - \
z * z * Vx - y * y * Vx
newPt[1] = 2 * x * y * Vx + y * y * Vy + 2 * z * y * Vz + \
2 * w * z * Vx - z * z * Vy + w * w * Vy - \
2 * x * w * Vz - x * x * Vy
newPt[2] = 2 * x * z * Vx + 2 * y * z * Vy + \
z * z * Vz - 2 * w * y * Vx - y * y * Vz + \
2 * w * x * Vy - x * x * Vz + w * w * Vz
return newPt
@classmethod
def average(cls, list qs):
"""
DESCRIPTION
-----------
Q_avg = Quaternion.average(listOfQuaternions)
Return the average quaternion based on algorithm published in
F. Landis Markley, Yang Cheng, John Lucas Crassidis, and Yaakov Oshman.
Averaging Quaternions,
Journal of Guidance, Control, and Dynamics,
Vol. 30, No. 4 (2007), pp. 1193-1197.
doi: 10.2514/1.28949
NOTE
----
No crystal symmetry considered at this level, just plain averaging
list of unitary quaternions. Also the results coming out of this method
is not accurate, e.g. Euler angles [10,0,0], [30,0,0], [90,0,0]
theoretical results: [43.33, 0.0, 0.0]
numerical results: [42.12, 0.0, 0.0]
"""
cdef int N = len(qs)
cdef np.ndarray M = np.zeros((4,4), dtype=DTYPE)
cdef np.ndarray eig = np.empty( 4, dtype=DTYPE)
cdef np.ndarray vec = np.empty((4,4), dtype=DTYPE)
cdef Quaternion q
cdef int i
for i in range(N):
q = qs[i].unitary()
M += np.outer(q.tondarray(), q.tondarray())
eig, vec = np.linalg.eig(M/N)
return Quaternion(np.real(vec.T[eig.argmax()]))
cdef class Eulers:
"""
DESCRIPTION
-----------
Euler angle representation of orientation.
Calculation is carries out by converting to quaternions.
PARAMETERS
----------
phi1: double
first of Euler angle
PHI: double
second of Euler angle
phi2: double
third of Euler angle
METHODS
-------
"""
def __init__(self, double phi1, double PHI, double phi2):
self.phi1 = phi1
self.PHI = PHI
self.phi2 = phi2
self.__q = Quaternion(self.__getq())
def __getq(self):
""" Return a quaternion based on given Euler Angles """
cdef np.ndarray qv = np.zeros(4, dtype=DTYPE)
cdef DTYPE_t c1,s1,c2,s2,c3,s3
c1 = cos(self.phi1 * d2r / 2.0)
s1 = sin(self.phi1 * d2r / 2.0)
c2 = cos(self.PHI * d2r / 2.0)
s2 = sin(self.PHI * d2r / 2.0)
c3 = cos(self.phi2 * d2r / 2.0)
s3 = sin(self.phi2 * d2r / 2.0)
qv[0] = c1 * c2 * c3 - s1 * c2 * s3
qv[1] = c1 * s2 * c3 + s1 * s2 * s3
qv[2] = - c1 * s2 * s3 + s1 * s2 * c3
qv[3] = c1 * c2 * s3 + s1 * c2 * c3
return qv
def toQuaternion(self):
return self.__q
def tolist(self):
return [self.phi1, self.PHI, self.phi2]
def tondarray(self):
return np.array(self.tolist())
def toRodrigues(self):
return self.__q.toRodrigues()
def toOrientationMatrix(self):
return self.__q.toOrientationMatrix()
def toAngleAxis(self):
return self.__q.toAngleAxis()
cdef class OrientationMatrix:
"""
Matrix representation of orientation, this is defined as
the transpose of the rotation matrix.
PARAMETERS
----------
METHODS
"""
def __init__(self, DTYPE_t[:,:] g):
cdef int i, j
self.g = np.zeros((3,3), dtype=DTYPE)
for i in range(3):
for j in range(3):
self.g[i,j] = g[i,j]
self.__q = self.__getq()
cdef Quaternion __getq(self):
cdef DTYPE_t trace, s, t
cdef np.ndarray qv = np.zeros(4, dtype=DTYPE)
cdef DTYPE_t w, x, y, z
trace = np.trace(self.g)
if trace > 1e-8:
s = sqrt(trace + 1.0)*2.0
qv[0] = s*0.25
qv[1] = (self.g[2,1] - self.g[1,2])/s
qv[2] = (self.g[0,2] - self.g[2,0])/s
qv[3] = (self.g[1,0] - self.g[0,1])/s
elif (self.g[0,0] > self.g[1,1]) and (self.g[0,0] > self.g[2,2]):
t = self.g[0,0] - self.g[1,1] - self.g[2,2] + 1.0
s = 2.0*sqrt(t)
qv[0] = (self.g[2,1] - self.g[1,2])/s
qv[1] = s*0.25
qv[2] = (self.g[0,1] + self.g[1,0])/s
qv[3] = (self.g[2,0] + self.g[0,2])/s
elif self.g[1,1] > self.g[2,2]:
t = -self.g[0,0] + self.g[1,1] - self.g[2,2] + 1.0
s = 2.0*sqrt(t)
qv[0] = (self.g[0,2] - self.g[2,0])/s
qv[1] = (self.g[0,1] + self.g[1,0])/s
qv[2] = s*0.25
qv[3] = (self.g[1,2] + self.g[2,1])/s
else:
t = -self.g[0,0] - self.g[1,1] + self.g[2,2] + 1.0
s = 2.0*sqrt(t)
qv[0] = (self.g[1,0] - self.g[0,1])/s
qv[1] = (self.g[2,0] + self.g[0,2])/s
qv[2] = (self.g[1,2] + self.g[2,1])/s
qv[3] = s*0.25
return Quaternion(qv)
def tondarray(self):
return self.g
def toEulers(self):
return self.__q.toEulers()
def toRodrigues(self):
return self.__q.toRodrigues()
def toQuaternion(self):
return self.__q
cdef class Rodrigues:
"""
DESCRIPTION
-----------
Rodrigues representation of orientation, a wrapper class that use
Quaternion class as engine.
"""
def __init__(self, DTYPE_t[:] v):
cdef int i
self.v = np.zeros(3, dtype=DTYPE)
for i in range(3):
self.v[i] = v[i]
self.__q = self.__getq()
def __getq(self):
cdef np.ndarray qv = np.zeros(4, dtype=DTYPE)
cdef DTYPE_t halfangle, c, s
halfangle = atan(np.linalg.norm(self.v))
c = cos(halfangle)
s = sin(halfangle)
qv[0] = s
qv[1] = c * self.v[0]
qv[2] = c * self.v[1]
qv[3] = c * self.v[2]
return Quaternion(qv)
def tolist(self):
return list(self.v)
def tondarray(self):
return np.array(self.tolist())
def toQuaternion(self):
return self.__q
def toAngleAxis(self):
return self.__q.toAngleAxis()
def toEulers(self):
return self.__q.toEulers()
def toOrientationMatrix(self):
return self.__q.toOrientationMatrix()
cdef class Xtallite:
"""
DESCRIPTION
-----------
Composite class to represent material point in general crystal plasticity
simulation.
PARAMETERS
----------
METHODS
-------
"""
def __init__(self,
eulers=np.zeros(3, dtype=DTYPE),
pt=np.zeros(3, dtype=DTYPE),
lattice='bcc',
dv=np.zeros(3, dtype=DTYPE),
stress=np.zeros((3,3), dtype=DTYPE),
strain=np.zeros((3,3), dtype=DTYPE)
):
cdef double phi1,PHI,phi2
self.eulers = np.copy(eulers)
phi1 = self.eulers[0]
PHI = self.eulers[1]
phi2 = self.eulers[2]
self.__q = Eulers(phi1, PHI, phi2).toQuaternion()
self.pt = np.copy(pt)
self.dv = np.copy(dv)
self.stress = np.copy(stress)
self.strain = np.copy(strain)
self.lattice = lattice
def setEulers(self, DTYPE_t phi1, DTYPE_t PHI, DTYPE_t phi2):
self.eulers = np.array([phi1, PHI, phi2])
self.__q = Eulers(phi1, PHI, phi2).toQuaternion()
def setLattice(self, str newLattice):
self.lattice = newLattice
def getOrientation(self, str mode='eulers'):
"""
DESCRIPTION
-----------
"""
mode = mode.lower()
if mode == 'eulers':
return self.__q.toEulers()
elif mode == 'quaternion':
return self.__q
elif mode == 'rodrigue':
return self.__q.toRodrigues()
else:
raise ValueError("Unknown mode: {}".format(mode))
def toFundamentalZone(self):
cdef Quaternion q
cdef np.ndarray symop
cdef int i
symops = symmetry(self.lattice, mode='quaternion')
for i in range(len(symops)):
q = self.__q * symops[i]
if Xtallite.inFundamentalZone(q.toRodrigues(), self.lattice):
self.__q = q
self.eulers = q.toEulers()
return self.eulers
def disorientation(self, Xtallite other, str mode='angle'):
"""
DESCRIPTION
-----------
"""
cdef Quaternion q0 = self.__q
cdef Quaternion q1 = other.getOrientation(mode='quaternion')
cdef Quaternion dq
cdef str lattice
if self.lattice.lower() != other.lattice.lower():
raise ValueError("ERROR: {}!={}".format(self.lattice, other.lattice))
else:
lattice = self.lattice.lower()
dq = self.getDq(q0, q1, lattice)
mode = mode.lower()
if mode == 'angle':
return np.degrees(dq.toAngleAxis()[0])
elif mode == 'quaternion':
return dq
elif mode == 'angleaxis':
return dq.toAngleAxis()
elif mode == 'eulers':
return dq.toEulers()
elif mode == 'axis':
return dq.toAngleAxis()[1]
else:
raise ValueError("Unknown mode: {}".format(mode))
cdef Quaternion getDq(self, Quaternion q0, Quaternion q1, str lattice):
"""
DESCRIPTION
-----------
dQ = getDq(q0, q1, lattice)
"""
cdef Quaternion deltaQ, tmp0, tmp1, tmpQ
cdef np.ndarray symop
cdef int i, j
cdef DTYPE_t angle = 360
symops = symmetry(lattice, mode='quaternion')
# use brutal force to get the smallest rotation angle
for i in range(len(symops)):
for j in range(len(symops)):
tmp0 = q0*symops[i]
tmp1 = q1*symops[j]
tmpQ = tmp0.conj() * tmp1
if angle > tmpQ.toAngleAxis()[0]:
angle = tmpQ.toAngleAxis()[0]
deltaQ = tmpQ
return deltaQ
def disorientations(self, list others):
"""
DESCRIPTION
-----------
dQs = disorientations(list ListOfXtallite)
Provide batch processing capability of disorientation
calculation. Assuming the crystallites in the list has the
same lattice structure as self.
"""
cdef Quaternion q0 = self.__q
cdef Quaternion q1, dq
cdef np.ndarray dqs = np.empty(len(others), dtype=DTYPE)
cdef int N = len(others)
cdef int i
for i in range(N):
q1 = others[i].getOrientation(mode='quaternion')
dq = self.getDq(q0,q1,self.lattice)
dqs[i] = np.degrees(dq.toAngleAxis()[0])
return dqs
@classmethod
def random(cls):
eulers = np.degrees(np.random.random(3))
return Xtallite(eulers=eulers)
@classmethod
def inDisorientationStandardZone(cls,
Quaternion deltaQ,
str lattice):
'''
DESCRIPTION
-----------
flag = Xtallite.inDisorientationStandardZone(q, lattice)
Check whether given Rodrigues vector (of misorientation) falls into standard stereographic triangle of own symmetry.
Determination of disorientations follow the work of A. Heinz and P. Neumann:
Representation of Orientation and Disorientation Data for Cubic, Hexagonal, Tetragonal and Orthorhombic Crystals
Acta Cryst. (1991). A47, 780-789
PARAMETERS
----------
'''
cdef DTYPE_t epsilon = 0.0
cdef DTYPE_t[:] R = deltaQ.toRodrigues()
if lattice in lattice_cubic:
return R[0] >= R[1]+epsilon and R[1] >= R[2]+epsilon and R[2] >= epsilon
elif lattice in lattice_hcp:
return R[0] >= math.sqrt(3)*(R[1]-epsilon) and R[1] >= epsilon and R[2] >= epsilon
elif lattice in lattice_tet:
return R[0] >= R[1]-epsilon and R[1] >= epsilon and R[2] >= epsilon
elif lattice in lattice_orth:
return R[0] >= epsilon and R[1] >= epsilon and R[2] >= epsilon
else:
return True
@classmethod
def inFundamentalZone(cls,
DTYPE_t[:] r,
str lattice):
"""
DESCRIPTION
-----------
flag = Xtallite.inFundamentalZone(r, lattice)
Check whether given Rodrigues vector is in the fundamental zone
for lattice structure
PARAMETERS
----------
r: DTYPE_t[:]
Rodrigues vector in numpy array
lattice: str
lattice structure
"""
cdef np.ndarray R = np.absolute(r)
if lattice in lattice_cubic:
return (sqrt_2 - 1.0 >= R[0]) \
and (sqrt_2 - 1.0 >= R[1]) \
and (sqrt_2 - 1.0 >= R[2]) \
and 1.0 >= R[0]+R[1]+R[2]
elif lattice in lattice_hcp:
return 1.0 >= R[0] \
and 1.0 >= R[1] \
and 1.0 >= R[2] \
and 2.0 >= sqrt_3*R[0] + R[1] \
and 2.0 >= sqrt_3*R[1] + R[0] \
and 2.0 >= sqrt_3 + R[2]
elif lattice in lattice_tet:
return 1.0 >= R[0] \
and 1.0 >= R[1] \
and sqrt_2 >= R[0] + R[1] \
and sqrt_2 >= R[2] + 1.0
elif lattice in lattice_orth:
return 1.0 >= R[0] \
and 1.0 >= R[1] \
and 1.0 >= R[2]
else:
raise ValueError("Unknown lattice structure: {}".format(lattice))
cdef class Aggregate:
"""
DESCRIPTION
-----------
grainX = Aggregate(ListOfXtallites)
A container class that holds several
"""
def __init__(self, list xtals,
INTP_t texture=0,
INTP_t gid=0):
self.xtals = xtals
self.texture = texture
self.gid = gid
def getOrientation(self, mode='eulers'):
"""
DESCRIPTION
-----------
Orientaiton_grain = grain.getOrientation()
Return the grain average orientation by averaging all the
orientations within this aggregate.
"""
cdef Quaternion Qavg
Qavg = self.__findAverageQ()
mode = mode.lower()
if mode == 'eulers':
return Qavg.toEulers()
elif mode == 'quaternion':
return Qavg
elif mode == 'rodrigue':
return Qavg.toRodrigues()
else:
raise ValueError("Unknown mode: {}".format(mode))
cdef Quaternion __findAverageQ(self):
"""
DESCRIPTION
-----------
"""
cdef INTP_t N = len(self.xtals)
cdef list qs = [None] * N
cdef str lattice = self.xtals[0].lattice
cdef list symops = symmetry(lattice, mode='quaternion')
cdef INTP_t Nsyms = len(symops)
cdef int i
cdef double angle, dA
cdef Quaternion Qavg, refQ, Qtmp, dQ, close2refQ
cdef Xtallite refXtal = self.xtals[0]
# use first xtallite as orientation reference
refXtal.toFundamentalZone()
refQ = refXtal.getOrientation(mode='quaternion')
qs[0] = refQ
for i in range(1, N):
angle = 360.0
Qtmp = self.xtals[i].getOrientation(mode='quaternion')
qs[i] = Qtmp
# find q equivalent q close to reference
for j in range(Nsyms):
dQ = refXtal.getDq(refQ, Qtmp*symops[j], lattice)
dA = np.degrees(dQ.toAngleAxis()[0])
if angle > dA:
angle = dA
qs[i] = Qtmp*symops[j]