From b080e414ae73cbfadf198fb0e0fca829728bd7e4 Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Wed, 11 May 2022 08:55:55 -0400 Subject: [PATCH 1/2] normalize to_pole output by default --- python/damask/_orientation.py | 11 +++++++++-- python/tests/test_Orientation.py | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/python/damask/_orientation.py b/python/damask/_orientation.py index 534a9548d..0c995110c 100644 --- a/python/damask/_orientation.py +++ b/python/damask/_orientation.py @@ -786,7 +786,8 @@ class Orientation(Rotation,Crystal): def to_pole(self, *, uvw: FloatSequence = None, hkl: FloatSequence = None, - with_symmetry: bool = False) -> np.ndarray: + with_symmetry: bool = False, + normalize: bool = True) -> np.ndarray: """ Calculate lab frame vector along lattice direction [uvw] or plane normal (hkl). @@ -795,9 +796,13 @@ class Orientation(Rotation,Crystal): uvw|hkl : numpy.ndarray, shape (...,3) Miller indices of crystallographic direction or plane normal. Shape of vector blends with shape of own rotation array. - For example, a rotation array, shape (3,2) and a vector array of shape (2,4) result in (3,2,4) outputs. + For example, a rotation array of shape (3,2) and a vector array of shape (2,4) result in (3,2,4) outputs. with_symmetry : bool, optional Calculate all N symmetrically equivalent vectors. + Defaults to False. + normalize : bool, optional + Normalize output vector. + Defaults to True. Returns ------- @@ -807,6 +812,8 @@ class Orientation(Rotation,Crystal): """ v = self.to_frame(uvw=uvw,hkl=hkl) blend = util.shapeblender(self.shape,v.shape[:-1]) + if normalize: + v /= np.linalg.norm(v,axis=-1,keepdims=len(v.shape)>1) if with_symmetry: sym_ops = self.symmetry_operations shape = v.shape[:-1]+sym_ops.shape diff --git a/python/tests/test_Orientation.py b/python/tests/test_Orientation.py index 729967539..6eedd3b77 100644 --- a/python/tests/test_Orientation.py +++ b/python/tests/test_Orientation.py @@ -173,8 +173,8 @@ class TestOrientation: o = Orientation.from_directions(uvw=a,hkl=c,**kwargs) x = o.to_pole(uvw=a) z = o.to_pole(hkl=c) - assert np.isclose(np.dot(x/np.linalg.norm(x),np.array([1,0,0])),1) \ - and np.isclose(np.dot(z/np.linalg.norm(z),np.array([0,0,1])),1) + assert np.isclose(np.dot(x,np.array([1,0,0])),1) \ + and np.isclose(np.dot(z,np.array([0,0,1])),1) @pytest.mark.parametrize('function',[Orientation.from_random, Orientation.from_quaternion, From 90c3b3170d4300ebd521bb4b623fc647f5d6e577 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Fri, 13 May 2022 09:53:44 +0200 Subject: [PATCH 2/2] propagate 'normalize' option --- python/damask/_orientation.py | 6 ++++-- python/damask/_result.py | 20 +++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/python/damask/_orientation.py b/python/damask/_orientation.py index 0c995110c..dcc139b7c 100644 --- a/python/damask/_orientation.py +++ b/python/damask/_orientation.py @@ -796,7 +796,8 @@ class Orientation(Rotation,Crystal): uvw|hkl : numpy.ndarray, shape (...,3) Miller indices of crystallographic direction or plane normal. Shape of vector blends with shape of own rotation array. - For example, a rotation array of shape (3,2) and a vector array of shape (2,4) result in (3,2,4) outputs. + For example, a rotation array of shape (3,2) and a vector + array of shape (2,4) result in (3,2,4) outputs. with_symmetry : bool, optional Calculate all N symmetrically equivalent vectors. Defaults to False. @@ -807,7 +808,8 @@ class Orientation(Rotation,Crystal): Returns ------- vector : numpy.ndarray, shape (...,3) or (...,N,3) - 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. """ v = self.to_frame(uvw=uvw,hkl=hkl) diff --git a/python/damask/_result.py b/python/damask/_result.py index 253881731..7a58a1621 100644 --- a/python/damask/_result.py +++ b/python/damask/_result.py @@ -1023,26 +1023,26 @@ class Result: @staticmethod - def _add_pole(q,uvw,hkl,with_symmetry): + def _add_pole(q,uvw,hkl,with_symmetry,normalize): c = q['meta']['c/a'] if 'c/a' in q['meta'] else 1 brackets = ['[]','()','⟨⟩','{}'][(uvw is None)*1+with_symmetry*2] label = 'p^' + '{}{} {} {}{}'.format(brackets[0], *(uvw if uvw else hkl), brackets[-1],) - pole = Orientation(q['data'],lattice=q['meta']['lattice'],a=1,c=c).to_pole(uvw=uvw,hkl=hkl,with_symmetry=with_symmetry) + ori = Orientation(q['data'],lattice=q['meta']['lattice'],a=1,c=c) return { - 'data': pole, + 'data': ori.to_pole(uvw=uvw,hkl=hkl,with_symmetry=with_symmetry,normalize=normalize), 'label': label, 'meta' : { 'unit': '1', - 'description': 'lab frame vector along lattice ' \ - + ('direction' if uvw else 'plane') \ + 'description': f'{"normalized " if normalize else ""}lab frame vector along lattice ' \ + + ('direction' if uvw is not None else 'plane') \ + ('s' if with_symmetry else ''), 'creator': 'add_pole' } } - def add_pole(self,q='O',*,uvw=None,hkl=None,with_symmetry=False): + def add_pole(self,q='O',*,uvw=None,hkl=None,with_symmetry=False,normalize=True): """ Add lab frame vector along lattice direction [uvw] or plane normal (hkl). @@ -1055,9 +1055,15 @@ class Result: Miller indices of crystallographic direction or plane normal. with_symmetry : bool, optional Calculate all N symmetrically equivalent vectors. + Defaults to True. + normalize : bool, optional + Normalize output vector. + Defaults to True. """ - self._add_generic_pointwise(self._add_pole,{'q':q},{'uvw':uvw,'hkl':hkl,'with_symmetry':with_symmetry}) + self._add_generic_pointwise(self._add_pole, + {'q':q}, + {'uvw':uvw,'hkl':hkl,'with_symmetry':with_symmetry,'normalize':normalize}) @staticmethod