only vectorized version needed

use single point/simple versions only for testing
This commit is contained in:
Martin Diehl 2020-05-27 18:05:08 +02:00
parent f9c33d9210
commit 56afc03f3c
2 changed files with 21 additions and 14 deletions

View File

@ -28,12 +28,12 @@ def deviatoric_part(T):
Parameters Parameters
---------- ----------
T : numpy.ndarray of shape (:,3,3) or (3,3) T : numpy.ndarray of shape (...,3,3)
Tensor of which the deviatoric part is computed. Tensor of which the deviatoric part is computed.
""" """
return T - _np.eye(3)*spherical_part(T) if _np.shape(T) == (3,3) else \ return T - _np.einsum('...ij,...->...ij',_np.eye(3),spherical_part(T))
T - _np.einsum('...ij,...->...ij',_np.eye(3),spherical_part(T))
def eigenvalues(T_sym): def eigenvalues(T_sym):
""" """
@ -180,22 +180,14 @@ def spherical_part(T,tensor=False):
Parameters Parameters
---------- ----------
T : numpy.ndarray of shape (:,3,3) or (3,3) T : numpy.ndarray of shape (...,3,3)
Tensor of which the hydrostatic part is computed. Tensor of which the hydrostatic part is computed.
tensor : bool, optional tensor : bool, optional
Map spherical part onto identity tensor. Default is false Map spherical part onto identity tensor. Default is false
""" """
if T.shape == (3,3): sph = _np.trace(T,axis2=-2,axis1=-1)/3.0
sph = _np.trace(T)/3.0 return _np.einsum('...jk,...->...jk',_np.eye(3),sph) if tensor else sph
return sph if not tensor else _np.eye(3)*sph
else:
sph = _np.trace(T,axis2=-2,axis1=-1)/3.0
if not tensor:
return sph
else:
#return _np.einsum('ijk,i->ijk',_np.broadcast_to(_np.eye(3),(T.shape[0],3,3)),sph)
return _np.einsum('...jk,...->...jk',_np.eye(3),sph)
def strain_tensor(F,t,m): def strain_tensor(F,t,m):

View File

@ -3,12 +3,27 @@ import numpy as np
from damask import mechanics from damask import mechanics
def deviatoric_part(T):
return T - np.eye(3)*spherical_part(T)
def spherical_part(T,tensor=False):
sph = np.trace(T)/3.0
return sph if not tensor else np.eye(3)*sph
class TestMechanics: class TestMechanics:
n = 1000 n = 1000
c = np.random.randint(n) c = np.random.randint(n)
@pytest.mark.parametrize('vectorized,single',[(mechanics.deviatoric_part, deviatoric_part),
(mechanics.spherical_part, spherical_part)
])
def test_vectorize_1_arg_(self,vectorized,single):
test_data = np.random.rand(self.n,3,3)
for i,v in enumerate(vectorized(test_data)):
assert np.allclose(single(test_data[i]),v)
@pytest.mark.parametrize('function',[mechanics.deviatoric_part, @pytest.mark.parametrize('function',[mechanics.deviatoric_part,
mechanics.eigenvalues, mechanics.eigenvalues,
mechanics.eigenvectors, mechanics.eigenvectors,