re-introduced multiple inheritance

This commit is contained in:
Martin Diehl 2021-06-06 19:49:29 +02:00
parent 0722f4f754
commit 41e5f0c06c
4 changed files with 76 additions and 81 deletions

View File

@ -29,7 +29,8 @@ lattice_symmetries = {
class Lattice(LatticeFamily): class Lattice(LatticeFamily):
"""Lattice.""" """Lattice."""
def __init__(self, def __init__(self,*,
family = None,
lattice = None, lattice = None,
a = None,b = None,c = None, a = None,b = None,c = None,
alpha = None,beta = None,gamma = None, alpha = None,beta = None,gamma = None,
@ -57,10 +58,11 @@ class Lattice(LatticeFamily):
Angles are given in degrees. Defaults to False. Angles are given in degrees. Defaults to False.
""" """
super().__init__(lattice_symmetries[lattice]) super().__init__(family = lattice_symmetries[lattice] if family is None else family)
self.lattice = lattice self.lattice = lattice
if self.lattice is not None:
self.a = 1 if a is None else a self.a = 1 if a is None else a
self.b = b self.b = b
self.c = c self.c = c
@ -97,6 +99,9 @@ class Lattice(LatticeFamily):
if np.any([np.roll([self.alpha,self.beta,self.gamma],r)[0] if np.any([np.roll([self.alpha,self.beta,self.gamma],r)[0]
> np.sum(np.roll([self.alpha,self.beta,self.gamma],r)[1:]) for r in range(3)]): > np.sum(np.roll([self.alpha,self.beta,self.gamma],r)[1:]) for r in range(3)]):
raise ValueError ('Each lattice angle must be less than sum of others') raise ValueError ('Each lattice angle must be less than sum of others')
else:
self.a = self.b = self.c = None
self.alpha = self.beta = self.gamma = None
def __eq__(self,other): def __eq__(self,other):

View File

@ -4,7 +4,6 @@ import copy
import numpy as np import numpy as np
from . import Rotation from . import Rotation
from . import LatticeFamily
from . import Lattice from . import Lattice
from . import util from . import util
from . import tensor from . import tensor
@ -56,7 +55,7 @@ _parameter_doc = \
""" """
class Orientation(Rotation): class Orientation(Rotation,Lattice):
""" """
Representation of crystallographic orientation as combination of rotation and either crystal family or Bravais lattice. Representation of crystallographic orientation as combination of rotation and either crystal family or Bravais lattice.
@ -131,19 +130,9 @@ class Orientation(Rotation):
Defaults to no rotation. Defaults to no rotation.
""" """
super().__init__(rotation) Rotation.__init__(self,rotation)
Lattice.__init__(self,family=family, lattice=lattice,
if family in set(lattice_symmetries.values()) and lattice is None: a=a,b=b,c=c, alpha=alpha,beta=beta,gamma=gamma, degrees=degrees)
self.family = family
self.lattice = None
self.structure = LatticeFamily(self.family)
self.related = self.Schmid = self.to_pole = None
elif lattice in lattice_symmetries:
self.family = lattice_symmetries[lattice]
self.lattice = lattice
self.structure = Lattice(self.lattice, a,b,c, alpha,beta,gamma, degrees)
else:
raise KeyError(f'no valid family or lattice')
def __repr__(self): def __repr__(self):
@ -173,8 +162,9 @@ class Orientation(Rotation):
Orientation to check for equality. Orientation to check for equality.
""" """
matching_type = self.structure == other.structure if hasattr(other,'structure') else \ matching_type = self.family == other.family and \
False self.lattice == other.lattice and \
self.parameters == other.parameters
return np.logical_and(matching_type,super(self.__class__,self.reduced).__eq__(other.reduced)) return np.logical_and(matching_type,super(self.__class__,self.reduced).__eq__(other.reduced))
def __ne__(self,other): def __ne__(self,other):
@ -211,8 +201,9 @@ class Orientation(Rotation):
Mask indicating where corresponding orientations are close. Mask indicating where corresponding orientations are close.
""" """
matching_type = self.structure == other.structure if hasattr(other,'structure') else \ matching_type = self.family == other.family and \
False self.lattice == other.lattice and \
self.parameters == other.parameters
return np.logical_and(matching_type,super(self.__class__,self.reduced).isclose(other.reduced)) return np.logical_and(matching_type,super(self.__class__,self.reduced).isclose(other.reduced))
@ -385,8 +376,8 @@ class Orientation(Rotation):
""" """
o = cls(**kwargs) o = cls(**kwargs)
x = o.structure.to_frame(uvw=uvw) x = o.to_frame(uvw=uvw)
z = o.structure.to_frame(hkl=hkl) z = o.to_frame(hkl=hkl)
om = np.stack([x,np.cross(z,x),z],axis=-2) om = np.stack([x,np.cross(z,x),z],axis=-2)
return o.copy(rotation=Rotation.from_matrix(tensor.transpose(om/np.linalg.norm(om,axis=-1,keepdims=True)))) return o.copy(rotation=Rotation.from_matrix(tensor.transpose(om/np.linalg.norm(om,axis=-1,keepdims=True))))
@ -400,7 +391,7 @@ class Orientation(Rotation):
is added to the left of the Rotation array. is added to the left of the Rotation array.
""" """
sym_ops = self.structure.symmetry_operations sym_ops = self.symmetry_operations
o = sym_ops.broadcast_to(sym_ops.shape+self.shape,mode='right') o = sym_ops.broadcast_to(sym_ops.shape+self.shape,mode='right')
return self.copy(rotation=o*Rotation(self.quaternion).broadcast_to(o.shape,mode='left')) return self.copy(rotation=o*Rotation(self.quaternion).broadcast_to(o.shape,mode='left'))
@ -677,21 +668,21 @@ class Orientation(Rotation):
if not isinstance(vector,np.ndarray) or vector.shape[-1] != 3: if not isinstance(vector,np.ndarray) or vector.shape[-1] != 3:
raise ValueError('input is not a field of three-dimensional vectors') raise ValueError('input is not a field of three-dimensional vectors')
if self.structure.basis is None: # direct exit for no symmetry if self.basis is None: # direct exit for no symmetry
return np.ones_like(vector[...,0],bool) return np.ones_like(vector[...,0],bool)
if proper: if proper:
components_proper = np.around(np.einsum('...ji,...i', components_proper = np.around(np.einsum('...ji,...i',
np.broadcast_to(self.structure.basis['proper'], vector.shape+(3,)), np.broadcast_to(self.basis['proper'], vector.shape+(3,)),
vector), 12) vector), 12)
components_improper = np.around(np.einsum('...ji,...i', components_improper = np.around(np.einsum('...ji,...i',
np.broadcast_to(self.structure.basis['improper'], vector.shape+(3,)), np.broadcast_to(self.basis['improper'], vector.shape+(3,)),
vector), 12) vector), 12)
return np.all(components_proper >= 0.0,axis=-1) \ return np.all(components_proper >= 0.0,axis=-1) \
| np.all(components_improper >= 0.0,axis=-1) | np.all(components_improper >= 0.0,axis=-1)
else: else:
components = np.around(np.einsum('...ji,...i', components = np.around(np.einsum('...ji,...i',
np.broadcast_to(self.structure.basis['improper'], vector.shape+(3,)), np.broadcast_to(self.basis['improper'], vector.shape+(3,)),
np.block([vector[...,:2],np.abs(vector[...,2:3])])), 12) np.block([vector[...,:2],np.abs(vector[...,2:3])])), 12)
return np.all(components >= 0.0,axis=-1) return np.all(components >= 0.0,axis=-1)
@ -732,15 +723,15 @@ class Orientation(Rotation):
vector_ = self.to_SST(vector,proper) if in_SST else \ vector_ = self.to_SST(vector,proper) if in_SST else \
self @ np.broadcast_to(vector,self.shape+(3,)) self @ np.broadcast_to(vector,self.shape+(3,))
if self.structure.basis is None: # direct exit for no symmetry if self.basis is None: # direct exit for no symmetry
return np.zeros_like(vector_) return np.zeros_like(vector_)
if proper: if proper:
components_proper = np.around(np.einsum('...ji,...i', components_proper = np.around(np.einsum('...ji,...i',
np.broadcast_to(self.structure.basis['proper'], vector_.shape+(3,)), np.broadcast_to(self.basis['proper'], vector_.shape+(3,)),
vector_), 12) vector_), 12)
components_improper = np.around(np.einsum('...ji,...i', components_improper = np.around(np.einsum('...ji,...i',
np.broadcast_to(self.structure.basis['improper'], vector_.shape+(3,)), np.broadcast_to(self.basis['improper'], vector_.shape+(3,)),
vector_), 12) vector_), 12)
in_SST = np.all(components_proper >= 0.0,axis=-1) \ in_SST = np.all(components_proper >= 0.0,axis=-1) \
| np.all(components_improper >= 0.0,axis=-1) | np.all(components_improper >= 0.0,axis=-1)
@ -748,7 +739,7 @@ class Orientation(Rotation):
components_proper,components_improper) components_proper,components_improper)
else: else:
components = np.around(np.einsum('...ji,...i', components = np.around(np.einsum('...ji,...i',
np.broadcast_to(self.structure.basis['improper'], vector_.shape+(3,)), np.broadcast_to(self .basis['improper'], vector_.shape+(3,)),
np.block([vector_[...,:2],np.abs(vector_[...,2:3])])), 12) np.block([vector_[...,:2],np.abs(vector_[...,2:3])])), 12)
in_SST = np.all(components >= 0.0,axis=-1) in_SST = np.all(components >= 0.0,axis=-1)
@ -781,9 +772,9 @@ class Orientation(Rotation):
Lab frame vector (or vectors if with_symmetry) along [uvw] direction or (hkl) plane normal. Lab frame vector (or vectors if with_symmetry) along [uvw] direction or (hkl) plane normal.
""" """
sym_ops = self.structure.symmetry_operations sym_ops = self.symmetry_operations
# ToDo: simplify 'with_symmetry' # ToDo: simplify 'with_symmetry'
v = self.structure.to_frame(uvw=uvw,hkl=hkl) v = self.to_frame(uvw=uvw,hkl=hkl)
if with_symmetry: if with_symmetry:
v = sym_ops.broadcast_to(sym_ops.shape+v.shape[:-1],mode='right') \ v = sym_ops.broadcast_to(sym_ops.shape+v.shape[:-1],mode='right') \
@ np.broadcast_to(v,sym_ops.shape+v.shape) @ np.broadcast_to(v,sym_ops.shape+v.shape)
@ -820,8 +811,8 @@ class Orientation(Rotation):
""" """
try: try:
d = self.structure.to_frame(uvw=self.structure.kinematics(mode)['direction']) d = self.to_frame(uvw=self.kinematics(mode)['direction'])
p = self.structure.to_frame(hkl=self.structure.kinematics(mode)['plane']) p = self.to_frame(hkl=self.kinematics(mode)['plane'])
except KeyError: except KeyError:
raise (f'"{mode}" not defined for lattice "{self.lattice}"') raise (f'"{mode}" not defined for lattice "{self.lattice}"')
P = np.einsum('...i,...j',d/np.linalg.norm(d,axis=-1,keepdims=True), P = np.einsum('...i,...j',d/np.linalg.norm(d,axis=-1,keepdims=True),
@ -839,15 +830,14 @@ class Orientation(Rotation):
is added to the left of the Rotation array. is added to the left of the Rotation array.
""" """
lattice,o = self.structure.relation_operations(model) lattice,o = self.relation_operations(model)
target = Lattice(lattice=lattice) target = Lattice(lattice=lattice)
struct = self.structure
o = o.broadcast_to(o.shape+self.shape,mode='right') o = o.broadcast_to(o.shape+self.shape,mode='right')
return Orientation(rotation=o*Rotation(self.quaternion).broadcast_to(o.shape,mode='left'), return Orientation(rotation=o*Rotation(self.quaternion).broadcast_to(o.shape,mode='left'),
lattice=lattice, lattice=lattice,
b = struct.b if target.ratio['b'] is None else struct.a*target.ratio['b'], b = self.b if target.ratio['b'] is None else self.a*target.ratio['b'],
c = struct.c if target.ratio['c'] is None else struct.a*target.ratio['c'], c = self.c if target.ratio['c'] is None else self.a*target.ratio['c'],
alpha = None if 'alpha' in target.immutable else struct.alpha, alpha = None if 'alpha' in target.immutable else self.alpha,
beta = None if 'beta' in target.immutable else struct.beta, beta = None if 'beta' in target.immutable else self.beta,
gamma = None if 'gamma' in target.immutable else struct.gamma, gamma = None if 'gamma' in target.immutable else self.gamma,
) )

View File

@ -6,12 +6,12 @@ from damask import Lattice
class TestLattice: class TestLattice:
def test_double_to_lattice(self): def test_double_to_lattice(self):
L = Lattice('cF') L = Lattice(lattice='cF')
with pytest.raises(KeyError): with pytest.raises(KeyError):
L.to_lattice(direction=np.ones(3),plane=np.ones(3)) L.to_lattice(direction=np.ones(3),plane=np.ones(3))
def test_double_to_frame(self): def test_double_to_frame(self):
L = Lattice('cF') L = Lattice(lattice='cF')
with pytest.raises(KeyError): with pytest.raises(KeyError):
L.to_frame(uvw=np.ones(3),hkl=np.ones(3)) L.to_frame(uvw=np.ones(3),hkl=np.ones(3))

View File

@ -409,7 +409,7 @@ class TestOrientation:
**dict(zip(['a','b','c'],lengths)), **dict(zip(['a','b','c'],lengths)),
**dict(zip(['alpha','beta','gamma'],np.arccos(cosines))), **dict(zip(['alpha','beta','gamma'],np.arccos(cosines))),
) )
assert np.allclose(o.structure.to_frame(uvw=np.eye(3)),basis), 'Lattice basis disagrees with initialization' assert np.allclose(o.to_frame(uvw=np.eye(3)),basis), 'Lattice basis disagrees with initialization'
@pytest.mark.parametrize('lattice,a,b,c,alpha,beta,gamma', @pytest.mark.parametrize('lattice,a,b,c,alpha,beta,gamma',
@ -436,7 +436,7 @@ class TestOrientation:
a=a,b=b,c=c, a=a,b=b,c=c,
alpha=alpha,beta=beta,gamma=gamma) alpha=alpha,beta=beta,gamma=gamma)
assert o.to_pole(**{kw:vector,'with_symmetry':with_symmetry}).shape \ assert o.to_pole(**{kw:vector,'with_symmetry':with_symmetry}).shape \
== o.shape + (o.structure.symmetry_operations.shape if with_symmetry else ()) + vector.shape == o.shape + (o.symmetry_operations.shape if with_symmetry else ()) + vector.shape
@pytest.mark.parametrize('lattice',['hP','cI','cF']) @pytest.mark.parametrize('lattice',['hP','cI','cF'])
def test_Schmid(self,update,ref_path,lattice): def test_Schmid(self,update,ref_path,lattice):