From b1ad8197f2a35f24a653fb0da7a6b857ba4f1f17 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Thu, 29 Apr 2021 19:44:21 +0200 Subject: [PATCH] object oriented approach Orientation (of special lattice) should not have generic attributes Also, import at the beginning of the file --- python/damask/__init__.py | 1 - python/damask/{lattice.py => _lattice.py} | 0 python/damask/_orientation.py | 84 +++++++++++------------ python/setup.py | 2 +- python/tests/test_Orientation.py | 39 ++++++----- 5 files changed, 61 insertions(+), 65 deletions(-) rename python/damask/{lattice.py => _lattice.py} (100%) diff --git a/python/damask/__init__.py b/python/damask/__init__.py index ad46d454f..060c5e295 100644 --- a/python/damask/__init__.py +++ b/python/damask/__init__.py @@ -14,7 +14,6 @@ from . import tensor # noqa from . import mechanics # noqa from . import solver # noqa from . import grid_filters # noqa -from . import lattice # noqa #Modules that contain only one class (of the same name), are prefixed by a '_'. #For example, '_colormap' containsa class called 'Colormap' which is imported as 'damask.Colormap'. from ._rotation import Rotation # noqa diff --git a/python/damask/lattice.py b/python/damask/_lattice.py similarity index 100% rename from python/damask/lattice.py rename to python/damask/_lattice.py diff --git a/python/damask/_orientation.py b/python/damask/_orientation.py index 00e74d36c..6d4b09d5b 100644 --- a/python/damask/_orientation.py +++ b/python/damask/_orientation.py @@ -5,6 +5,35 @@ import numpy as np from . import Rotation from . import util from . import tensor +from . import _lattice + +_crystal_families = ['triclinic', + 'monoclinic', + 'orthorhombic', + 'tetragonal', + 'hexagonal', + 'cubic'] + +_lattice_symmetries = { + 'aP': 'triclinic', + + 'mP': 'monoclinic', + 'mS': 'monoclinic', + + 'oP': 'orthorhombic', + 'oS': 'orthorhombic', + 'oI': 'orthorhombic', + 'oF': 'orthorhombic', + + 'tP': 'tetragonal', + 'tI': 'tetragonal', + + 'hP': 'hexagonal', + + 'cP': 'cubic', + 'cI': 'cubic', + 'cF': 'cubic', + } _parameter_doc = \ """lattice : str @@ -33,7 +62,7 @@ class Orientation(Rotation): """ Representation of crystallographic orientation as combination of rotation and either crystal family or Bravais lattice. - The crystal family is one of Orientation.crystal_families: + The crystal family is one of: - triclinic - monoclinic @@ -45,7 +74,7 @@ class Orientation(Rotation): and enables symmetry-related operations such as "equivalent", "reduced", "disorientation", "IPF_color", or "to_SST". - The Bravais lattice is one of Orientation.lattice_symmetries: + The Bravais lattice is given in the Pearson notation: - triclinic - aP : primitive @@ -85,35 +114,6 @@ class Orientation(Rotation): """ - crystal_families = ['triclinic', - 'monoclinic', - 'orthorhombic', - 'tetragonal', - 'hexagonal', - 'cubic'] - - lattice_symmetries = { - 'aP': 'triclinic', - - 'mP': 'monoclinic', - 'mS': 'monoclinic', - - 'oP': 'orthorhombic', - 'oS': 'orthorhombic', - 'oI': 'orthorhombic', - 'oF': 'orthorhombic', - - 'tP': 'tetragonal', - 'tI': 'tetragonal', - - 'hP': 'hexagonal', - - 'cP': 'cubic', - 'cI': 'cubic', - 'cF': 'cubic', - } - - @util.extend_docstring(_parameter_doc) def __init__(self, rotation = None, @@ -132,12 +132,10 @@ class Orientation(Rotation): Defaults to no rotation. """ - from damask.lattice import kinematics - Rotation.__init__(self) if rotation is None else Rotation.__init__(self,rotation=rotation) - if ( lattice not in self.lattice_symmetries - and lattice not in self.crystal_families): + if ( lattice not in _lattice_symmetries + and lattice not in _crystal_families): raise KeyError(f'Lattice "{lattice}" is unknown') self.family = None @@ -150,8 +148,8 @@ class Orientation(Rotation): self.gamma = None self.kinematics = None - if lattice in self.lattice_symmetries: - self.family = self.lattice_symmetries[lattice] + if lattice in _lattice_symmetries: + self.family = _lattice_symmetries[lattice] self.lattice = lattice self.a = 1 if a is None else a self.b = b @@ -190,15 +188,15 @@ class Orientation(Rotation): > 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') - if self.lattice in kinematics: - master = kinematics[self.lattice] + if self.lattice in _lattice.kinematics: + master = _lattice.kinematics[self.lattice] self.kinematics = {} for m in master: self.kinematics[m] = {'direction':master[m][:,0:3],'plane':master[m][:,3:6]} \ if master[m].shape[-1] == 6 else \ {'direction':self.Bravais_to_Miller(uvtw=master[m][:,0:4]), 'plane': self.Bravais_to_Miller(hkil=master[m][:,4:8])} - elif lattice in self.crystal_families: + elif lattice in _crystal_families: self.family = lattice @@ -676,11 +674,9 @@ class Orientation(Rotation): https://doi.org/10.1016/j.actamat.2004.11.021 """ - from damask.lattice import relations - - if model not in relations: + if model not in _lattice.relations: raise KeyError(f'Orientation relationship "{model}" is unknown') - r = relations[model] + r = _lattice.relations[model] if self.lattice not in r: raise KeyError(f'Relationship "{model}" not supported for lattice "{self.lattice}"') diff --git a/python/setup.py b/python/setup.py index e590a1197..5d66d3c88 100644 --- a/python/setup.py +++ b/python/setup.py @@ -12,7 +12,7 @@ setuptools.setup( author='The DAMASK team', author_email='damask@mpie.de', description='DAMASK library', - long_description='Python library for pre and post processing of DAMASK simulations', + long_description='Python library for managing DAMASK simulations', url='https://damask.mpie.de', packages=setuptools.find_packages(), include_package_data=True, diff --git a/python/tests/test_Orientation.py b/python/tests/test_Orientation.py index dce0cf40f..3c4e5bbff 100644 --- a/python/tests/test_Orientation.py +++ b/python/tests/test_Orientation.py @@ -5,9 +5,10 @@ from itertools import permutations from damask import Rotation from damask import Orientation from damask import Table -from damask import lattice from damask import util from damask import grid_filters +from damask import _lattice as lattice +from damask._orientation import _crystal_families as crystal_families @pytest.fixture @@ -22,7 +23,7 @@ def set_of_rodrigues(set_of_quaternions): class TestOrientation: - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) @pytest.mark.parametrize('shape',[None,5,(4,6)]) def test_equal(self,lattice,shape): R = Rotation.from_random(shape) @@ -30,14 +31,14 @@ class TestOrientation: (Orientation(R,lattice) == Orientation(R,lattice)).all() - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) @pytest.mark.parametrize('shape',[None,5,(4,6)]) def test_unequal(self,lattice,shape): R = Rotation.from_random(shape) assert not ( Orientation(R,lattice) != Orientation(R,lattice) if shape is None else \ (Orientation(R,lattice) != Orientation(R,lattice)).any()) - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) @pytest.mark.parametrize('shape',[None,5,(4,6)]) def test_close(self,lattice,shape): R = Orientation.from_random(lattice=lattice,shape=shape) @@ -182,14 +183,14 @@ class TestOrientation: with pytest.raises(ValueError): Orientation(lattice='aP',a=1,b=2,c=3,alpha=45,beta=45,gamma=90.0001,degrees=True) # noqa - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) @pytest.mark.parametrize('angle',[10,20,30,40]) def test_average(self,angle,lattice): o = Orientation.from_axis_angle(lattice=lattice,axis_angle=[[0,0,1,10],[0,0,1,angle]],degrees=True) avg_angle = o.average().as_axis_angle(degrees=True,pair=True)[1] assert np.isclose(avg_angle,10+(angle-10)/2.) - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) def test_reduced_equivalent(self,lattice): i = Orientation(lattice=lattice) o = Orientation.from_random(lattice=lattice) @@ -197,7 +198,7 @@ class TestOrientation: FZ = np.argmin(abs(eq.misorientation(i.broadcast_to(len(eq))).as_axis_angle(pair=True)[1])) assert o.reduced == eq[FZ] - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) @pytest.mark.parametrize('N',[1,8,32]) def test_disorientation(self,lattice,N): o = Orientation.from_random(lattice=lattice,shape=N) @@ -215,7 +216,7 @@ class TestOrientation: .misorientation(p[n].equivalent[ops[n][1]]) .as_quaternion()) - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) @pytest.mark.parametrize('a,b',[ ((2,3,2),(2,3,2)), ((2,2),(4,4)), @@ -230,20 +231,20 @@ class TestOrientation: assert o[tuple(loc[:len(o.shape)])].disorientation(p[tuple(loc[-len(p.shape):])]) \ .isclose(o.disorientation(p)[tuple(loc)]) - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) def test_disorientation360(self,lattice): o_1 = Orientation(Rotation(),lattice) o_2 = Orientation.from_Euler_angles(lattice=lattice,phi=[360,0,0],degrees=True) assert np.allclose((o_1.disorientation(o_2)).as_matrix(),np.eye(3)) - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) @pytest.mark.parametrize('shape',[(1),(2,3),(4,3,2)]) def test_reduced_vectorization(self,lattice,shape): o = Orientation.from_random(lattice=lattice,shape=shape) for r, theO in zip(o.reduced.flatten(),o.flatten()): assert r == theO.reduced - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) def test_reduced_corner_cases(self,lattice): # test whether there is always a sym-eq rotation that falls into the FZ N = np.random.randint(10,40) @@ -253,7 +254,7 @@ class TestOrientation: assert evenly_distributed.shape == evenly_distributed.reduced.shape - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) @pytest.mark.parametrize('shape',[(1),(2,3),(4,3,2)]) @pytest.mark.parametrize('vector',np.array([[1,0,0],[1,2,3],[-1,1,-1]])) @pytest.mark.parametrize('proper',[True,False]) @@ -262,7 +263,7 @@ class TestOrientation: for r, theO in zip(o.to_SST(vector=vector,proper=proper).reshape((-1,3)),o.flatten()): assert np.allclose(r,theO.to_SST(vector=vector,proper=proper)) - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) @pytest.mark.parametrize('shape',[(1),(2,3),(4,3,2)]) @pytest.mark.parametrize('vector',np.array([[1,0,0],[1,2,3],[-1,1,-1]])) @pytest.mark.parametrize('proper',[True,False]) @@ -272,7 +273,7 @@ class TestOrientation: for r, theO in zip(o.IPF_color(vector,in_SST=in_SST,proper=proper).reshape((-1,3)),o.flatten()): assert np.allclose(r,theO.IPF_color(vector,in_SST=in_SST,proper=proper)) - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) @pytest.mark.parametrize('a,b',[ ((2,3,2),(2,3,2)), ((2,2),(4,4)), @@ -300,7 +301,7 @@ class TestOrientation: assert np.allclose(np.array(color['RGB']), cube.IPF_color(vector=np.array(direction),proper=proper)) - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) @pytest.mark.parametrize('proper',[True,False]) def test_IPF_equivalent(self,set_of_quaternions,lattice,proper): direction = np.random.random(3)*2.0-1.0 @@ -308,13 +309,13 @@ class TestOrientation: color = o.IPF_color(vector=direction,proper=proper) assert np.allclose(np.broadcast_to(color[0,...],color.shape),color) - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) def test_in_FZ_vectorization(self,set_of_rodrigues,lattice): result = Orientation.from_Rodrigues_vector(rho=set_of_rodrigues.reshape((-1,4,4)),lattice=lattice).in_FZ.reshape(-1) for r,rho in zip(result,set_of_rodrigues[:len(result)]): assert r == Orientation.from_Rodrigues_vector(rho=rho,lattice=lattice).in_FZ - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) def test_in_disorientation_FZ_vectorization(self,set_of_rodrigues,lattice): result = Orientation.from_Rodrigues_vector(rho=set_of_rodrigues.reshape((-1,4,4)), lattice=lattice).in_disorientation_FZ.reshape(-1) @@ -322,7 +323,7 @@ class TestOrientation: assert r == Orientation.from_Rodrigues_vector(rho=rho,lattice=lattice).in_disorientation_FZ @pytest.mark.parametrize('proper',[True,False]) - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) def test_in_SST_vectorization(self,lattice,proper): vecs = np.random.rand(20,4,3) result = Orientation(lattice=lattice).in_SST(vecs,proper).flatten() @@ -393,7 +394,7 @@ class TestOrientation: a=a,b=b,c=c, alpha=alpha,beta=beta,gamma=gamma).related(relation) # noqa - @pytest.mark.parametrize('lattice',Orientation.crystal_families) + @pytest.mark.parametrize('lattice',crystal_families) @pytest.mark.parametrize('proper',[True,False]) def test_in_SST(self,lattice,proper): assert Orientation(lattice=lattice).in_SST(np.zeros(3),proper)