vectorized equivalent orientation calculation

This commit is contained in:
Martin Diehl 2020-06-18 22:53:04 +02:00
parent cdda556e18
commit 1648963b57
4 changed files with 42 additions and 44 deletions

View File

@ -159,7 +159,7 @@ class Symmetry:
@property
def symmetry_operations(self):
"""List (or single element) of symmetry operations as rotations."""
"""Symmetry operations as Rotations."""
if self.lattice == 'cubic':
symQuats = [
[ 1.0, 0.0, 0.0, 0.0 ],
@ -236,7 +236,7 @@ class Symmetry:
if (len(rodrigues) != 3):
raise ValueError('Input is not a Rodrigues-Frank vector.\n')
if np.any(rodrigues == np.inf): return False
if np.any(rodrigues == np.inf): return False # ToDo: MD: not sure if needed
Rabs = abs(rodrigues)

View File

@ -3,7 +3,7 @@ import numpy as np
from . import Lattice
from . import Rotation
class Orientation:
class Orientation: # make subclass or Rotation?
"""
Crystallographic orientation.
@ -39,8 +39,6 @@ class Orientation:
else:
self.rotation = Rotation.from_quaternion(rotation) # assume quaternion
# if self.rotation.quaternion.shape != (4,):
# raise NotImplementedError('Support for multiple rotations missing')
def disorientation(self,
other,
@ -94,20 +92,25 @@ class Orientation:
Rotation._qu2ro(self.rotation.as_quaternion())[l][...,:3]\
*Rotation._qu2ro(self.rotation.as_quaternion())[l][...,3])\
for l in range(self.rotation.shape[0])]
def inFZ(self):
return self.lattice.symmetry.inFZ(self.rotation.as_Rodrigues(vector=True))
def equivalent_vec(self):
"""List of orientations which are symmetrically equivalent."""
if not self.rotation.shape:
return [self.__class__(q*self.rotation,self.lattice) \
for q in self.lattice.symmetry.symmetryOperations()]
else:
return np.reshape([self.__class__(q*Rotation.from_quaternion(self.rotation.as_quaternion()[l]),self.lattice) \
for q in self.lattice.symmetry.symmetryOperations() \
for l in range(self.rotation.shape[0])], \
(len(self.lattice.symmetry.symmetryOperations()),self.rotation.shape[0]))
@property
def equivalent(self):
"""
Return orientations which are symmetrically equivalent.
One dimension (length according to symmetrically equivalent orientations)
is added to the left of the rotation array.
"""
symmetry_operations = self.lattice.symmetry.symmetry_operations
q = np.block([self.rotation.quaternion]*symmetry_operations.shape[0])
r = Rotation(q.reshape(symmetry_operations.shape+self.rotation.quaternion.shape))
return self.__class__(symmetry_operations.broadcast_to(r.shape)@r,self.lattice)
def equivalentOrientations(self,members=[]):
@ -130,7 +133,7 @@ class Orientation:
[self.__class__(o*Rotation.from_quaternion(self.rotation.as_quaternion()[l])\
,r['lattice']) for o in r['rotations'] for l in range(self.rotation.shape[0])]
,(len(r['rotations']),self.rotation.shape[0]))
def relatedOrientations(self,model):
"""List of orientations related by the given orientation relationship."""

View File

@ -129,6 +129,7 @@ class Rotation:
self.quaternion[...,1:] *= -1
return self
#@property
def inversed(self):
"""Inverse rotation/backward rotation."""
return self.copy().inverse()
@ -139,6 +140,7 @@ class Rotation:
self.quaternion[self.quaternion[...,0] < 0.0] *= -1
return self
#@property
def standardized(self):
"""Quaternion representation with positive real part."""
return self.copy().standardize()
@ -154,11 +156,12 @@ class Rotation:
Rotation to which the misorientation is computed.
"""
return other*self.inversed()
return other@self.inversed()
def broadcast_to(self,shape):
if isinstance(shape,int): shape = (shape,)
if isinstance(shape,int):
shape = (shape,)
N = np.prod(shape)//np.prod(self.shape,dtype=int)
q = np.block([np.repeat(self.quaternion[...,0:1],N).reshape(shape+(1,)),
@ -257,6 +260,7 @@ class Rotation:
"""Cubochoric vector: (c_1, c_2, c_3)."""
return Rotation._qu2cu(self.quaternion)
@property
def M(self): # ToDo not sure about the name: as_M or M? we do not have a from_M
"""
Intermediate representation supporting quaternion averaging.
@ -435,8 +439,8 @@ class Rotation:
weights = np.ones(N,dtype='i')
for i,(r,n) in enumerate(zip(rotations,weights)):
M = r.M() * n if i == 0 \
else M + r.M() * n # noqa add (multiples) of this rotation to average noqa
M = r.M * n if i == 0 \
else M + r.M * n # noqa add (multiples) of this rotation to average noqa
eig, vec = np.linalg.eig(M/N)
return Rotation.from_quaternion(np.real(vec.T[eig.argmax()]),accept_homomorph = True)
@ -461,7 +465,8 @@ class Rotation:
# for compatibility (old names do not follow convention)
asM = M
def asM(self):
return self.M
fromQuaternion = from_quaternion
fromEulers = from_Eulers
asAxisAngle = as_axis_angle

View File

@ -10,18 +10,19 @@ rot1= Rotation.from_random()
rot2= Rotation.from_random()
rot3= Rotation.from_random()
class TestOrientation_vec:
class TestOrientation_vec:
@pytest.mark.xfail
@pytest.mark.parametrize('lattice',Lattice.lattices)
def test_equivalentOrientations_vec(self,lattice):
ori0=Orientation(rot0,lattice)
ori1=Orientation(rot1,lattice)
ori2=Orientation(rot2,lattice)
ori3=Orientation(rot3,lattice)
quat=np.array([rot0.as_quaternion(),rot1.as_quaternion(),rot2.as_quaternion(),rot3.as_quaternion()])
rot_vec=Rotation.from_quaternion(quat)
ori_vec=Orientation(rot_vec,lattice)
for s in range(len(ori_vec.lattice.symmetry.symmetryOperations())):
assert all(ori_vec.equivalent_vec()[s,0].rotation.as_Eulers() == \
ori0.equivalentOrientations()[s].rotation.as_Eulers())
@ -31,7 +32,7 @@ class TestOrientation_vec:
ori2.equivalentOrientations()[s].rotation.as_Rodrigues())
assert all(ori_vec.equivalent_vec()[s,3].rotation.as_cubochoric() == \
ori3.equivalentOrientations()[s].rotation.as_cubochoric())
@pytest.mark.parametrize('lattice',Lattice.lattices)
def test_inFZ_vec(self,lattice):
ori0=Orientation(rot0,lattice)
@ -41,19 +42,19 @@ class TestOrientation_vec:
#ensure 1 of them is in FZ
ori4=ori0.reduced()
rot4=ori4.rotation
quat=np.array([rot0.as_quaternion(),rot1.as_quaternion(),\
rot2.as_quaternion(),rot3.as_quaternion(), rot4.as_quaternion()])
rot_vec=Rotation.from_quaternion(quat)
ori_vec=Orientation(rot_vec,lattice)
assert ori_vec.inFZ_vec()[0] == ori0.inFZ()
assert ori_vec.inFZ_vec()[1] == ori1.inFZ()
assert ori_vec.inFZ_vec()[2] == ori2.inFZ()
assert ori_vec.inFZ_vec()[3] == ori3.inFZ()
assert ori_vec.inFZ_vec()[4] == ori4.inFZ()
@pytest.mark.parametrize('model',['Bain','KS','GT','GT_prime','NW','Pitsch'])
@pytest.mark.parametrize('lattice',['fcc','bcc'])
def test_relatedOrientations_vec(self,model,lattice):
@ -61,11 +62,11 @@ class TestOrientation_vec:
ori1=Orientation(rot1,lattice)
ori2=Orientation(rot2,lattice)
ori3=Orientation(rot3,lattice)
quat=np.array([rot0.as_quaternion(),rot1.as_quaternion(),rot2.as_quaternion(),rot3.as_quaternion()])
rot_vec=Rotation.from_quaternion(quat)
ori_vec=Orientation(rot_vec,lattice)
for s in range(len(ori1.lattice.relationOperations(model)['rotations'])):
assert all(ori_vec.relatedOrientations_vec(model)[s,0].rotation.as_Eulers() == \
ori0.relatedOrientations(model)[s].rotation.as_Eulers())
@ -75,15 +76,4 @@ class TestOrientation_vec:
ori2.relatedOrientations(model)[s].rotation.as_Rodrigues())
assert all(ori_vec.relatedOrientations_vec(model)[s,3].rotation.as_cubochoric() == \
ori3.relatedOrientations(model)[s].rotation.as_cubochoric())