Merge branch 'rotation-numpy-coupling' into 'development'

numpy uses __array__ for casting

See merge request damask/DAMASK!345
This commit is contained in:
Philip Eisenlohr 2021-02-23 01:45:22 +00:00
commit fdb182b40d
3 changed files with 20 additions and 3 deletions

View File

@ -248,7 +248,7 @@ class ConfigMaterial(Config):
Examples Examples
-------- --------
>>> import damask >>> import damask
>>> O = damask.Rotation.from_random(3).as_quaternion() >>> O = damask.Rotation.from_random(3)
>>> phase = ['Aluminum','Steel','Aluminum'] >>> phase = ['Aluminum','Steel','Aluminum']
>>> m = damask.ConfigMaterial().material_add(constituents={'phase':phase,'O':O}, >>> m = damask.ConfigMaterial().material_add(constituents={'phase':phase,'O':O},
... homogenization='SX') ... homogenization='SX')

View File

@ -125,9 +125,18 @@ class Rotation:
return np.logical_not(self==other) return np.logical_not(self==other)
def __array__(self):
"""Initializer for numpy."""
return self.quaternion
@property
def size(self):
return self.quaternion[...,0].size
@property @property
def shape(self): def shape(self):
return self.quaternion.shape[:-1] return self.quaternion[...,0].shape
def __len__(self): def __len__(self):

View File

@ -689,6 +689,10 @@ class TestRotation:
with pytest.raises(TypeError): with pytest.raises(TypeError):
Rotation(np.ones(3)) Rotation(np.ones(3))
def test_to_numpy(self):
r = Rotation.from_random(np.random.randint(0,10,4))
assert np.all(r.as_quaternion() == np.array(r))
@pytest.mark.parametrize('degrees',[True,False]) @pytest.mark.parametrize('degrees',[True,False])
def test_Eulers(self,set_of_rotations,degrees): def test_Eulers(self,set_of_rotations,degrees):
for rot in set_of_rotations: for rot in set_of_rotations:
@ -804,7 +808,11 @@ class TestRotation:
r = Rotation.from_random() r = Rotation.from_random()
assert r == ~~r assert r == ~~r
@pytest.mark.parametrize('shape',[None,1,(1,),(4,2),(1,1,1)]) @pytest.mark.parametrize('shape',[1,(1,),(4,2),(1,1,1),tuple(np.random.randint(0,10,4))])
def test_size(self,shape):
assert Rotation.from_random(shape).size == np.prod(shape)
@pytest.mark.parametrize('shape',[None,1,(1,),(4,2),(1,1,1),tuple(np.random.randint(0,10,4))])
def test_shape(self,shape): def test_shape(self,shape):
r = Rotation.from_random(shape=shape) r = Rotation.from_random(shape=shape)
assert r.shape == (shape if isinstance(shape,tuple) else (shape,) if shape else ()) assert r.shape == (shape if isinstance(shape,tuple) else (shape,) if shape else ())