2020-04-22 12:28:43 +05:30
|
|
|
import pytest
|
2019-11-22 00:30:28 +05:30
|
|
|
import numpy as np
|
2020-04-22 12:28:43 +05:30
|
|
|
|
2020-11-16 03:44:46 +05:30
|
|
|
from damask import tensor
|
2019-11-22 00:30:28 +05:30
|
|
|
from damask import mechanics
|
2020-11-20 03:06:19 +05:30
|
|
|
from damask import Rotation
|
2019-11-22 00:30:28 +05:30
|
|
|
|
2020-11-18 03:26:22 +05:30
|
|
|
def stress_Cauchy(P,F):
|
2020-06-09 17:47:31 +05:30
|
|
|
sigma = 1.0/np.linalg.det(F) * np.dot(P,F.T)
|
2020-11-16 03:44:46 +05:30
|
|
|
return symmetric(sigma)
|
2020-06-09 17:47:31 +05:30
|
|
|
|
|
|
|
|
|
|
|
def eigenvalues(T_sym):
|
|
|
|
return np.linalg.eigvalsh(symmetric(T_sym))
|
|
|
|
|
|
|
|
|
|
|
|
def maximum_shear(T_sym):
|
|
|
|
w = eigenvalues(T_sym)
|
|
|
|
return (w[0] - w[2])*0.5
|
|
|
|
|
|
|
|
|
2020-11-18 03:26:22 +05:30
|
|
|
def equivalent_strain_Mises(epsilon):
|
|
|
|
return equivalent_Mises(epsilon,2.0/3.0)
|
2020-06-09 17:47:31 +05:30
|
|
|
|
|
|
|
|
2020-11-18 03:26:22 +05:30
|
|
|
def equivalent_stress_Mises(sigma):
|
|
|
|
return equivalent_Mises(sigma,3.0/2.0)
|
2020-06-09 17:47:31 +05:30
|
|
|
|
|
|
|
|
2020-11-18 03:26:22 +05:30
|
|
|
def stress_second_Piola_Kirchhoff(P,F):
|
2020-06-09 17:56:22 +05:30
|
|
|
S = np.dot(np.linalg.inv(F),P)
|
2020-06-09 17:47:31 +05:30
|
|
|
return symmetric(S)
|
|
|
|
|
|
|
|
|
2020-11-20 03:16:52 +05:30
|
|
|
def rotation(T):
|
2020-06-09 17:47:31 +05:30
|
|
|
return polar_decomposition(T,'R')[0]
|
|
|
|
|
2020-11-16 03:44:46 +05:30
|
|
|
|
2020-11-16 05:42:23 +05:30
|
|
|
def strain(F,t,m):
|
2020-11-16 03:44:46 +05:30
|
|
|
|
2020-06-09 17:47:31 +05:30
|
|
|
if t == 'V':
|
2020-11-16 03:44:46 +05:30
|
|
|
B = np.matmul(F,F.T)
|
2020-06-09 17:47:31 +05:30
|
|
|
w,n = np.linalg.eigh(B)
|
|
|
|
elif t == 'U':
|
2020-11-16 03:44:46 +05:30
|
|
|
C = np.matmul(F.T,F)
|
2020-06-09 17:47:31 +05:30
|
|
|
w,n = np.linalg.eigh(C)
|
|
|
|
|
|
|
|
if m > 0.0:
|
2020-11-16 03:44:46 +05:30
|
|
|
eps = 1.0/(2.0*abs(m)) * (+ np.matmul(n,np.einsum('j,kj->jk',w**m,n))
|
|
|
|
- np.eye(3))
|
2020-06-09 17:47:31 +05:30
|
|
|
elif m < 0.0:
|
2020-11-16 03:44:46 +05:30
|
|
|
eps = 1.0/(2.0*abs(m)) * (- np.matmul(n,np.einsum('j,kj->jk',w**m,n))
|
|
|
|
+ np.eye(3))
|
2020-06-09 17:47:31 +05:30
|
|
|
else:
|
2020-11-16 03:44:46 +05:30
|
|
|
eps = np.matmul(n,np.einsum('j,kj->jk',0.5*np.log(w),n))
|
2020-06-09 17:47:31 +05:30
|
|
|
|
2020-11-16 03:44:46 +05:30
|
|
|
return eps
|
2020-06-09 17:47:31 +05:30
|
|
|
|
|
|
|
|
2020-11-16 05:31:32 +05:30
|
|
|
def stretch_left(T):
|
|
|
|
return polar_decomposition(T,'V')[0]
|
|
|
|
|
|
|
|
|
|
|
|
def stretch_right(T):
|
|
|
|
return polar_decomposition(T,'U')[0]
|
|
|
|
|
|
|
|
|
2020-06-09 17:47:31 +05:30
|
|
|
def symmetric(T):
|
2020-11-16 03:44:46 +05:30
|
|
|
return (T+T.T)*0.5
|
2020-06-09 17:47:31 +05:30
|
|
|
|
|
|
|
|
|
|
|
def polar_decomposition(T,requested):
|
|
|
|
u, s, vh = np.linalg.svd(T)
|
|
|
|
R = np.dot(u,vh)
|
|
|
|
|
|
|
|
output = []
|
|
|
|
if 'R' in requested:
|
|
|
|
output.append(R)
|
|
|
|
if 'V' in requested:
|
2020-11-16 03:44:46 +05:30
|
|
|
output.append(np.dot(T,R.T))
|
2020-06-09 17:47:31 +05:30
|
|
|
if 'U' in requested:
|
|
|
|
output.append(np.dot(R.T,T))
|
|
|
|
|
|
|
|
return tuple(output)
|
|
|
|
|
2020-11-18 03:26:22 +05:30
|
|
|
def equivalent_Mises(T_sym,s):
|
2020-11-19 19:08:54 +05:30
|
|
|
return np.sqrt(s*(np.sum(deviatoric(T_sym)**2.0)))
|
2020-06-09 17:47:31 +05:30
|
|
|
|
2020-11-19 19:08:54 +05:30
|
|
|
def deviatoric(T):
|
|
|
|
return T - np.eye(3)*np.trace(T)/3.0
|
2020-06-09 17:47:31 +05:30
|
|
|
|
2019-11-22 00:30:28 +05:30
|
|
|
class TestMechanics:
|
2020-02-15 18:26:15 +05:30
|
|
|
|
2019-11-22 01:31:01 +05:30
|
|
|
n = 1000
|
2019-11-22 00:30:28 +05:30
|
|
|
c = np.random.randint(n)
|
2020-02-15 18:26:15 +05:30
|
|
|
|
2020-11-19 19:08:54 +05:30
|
|
|
@pytest.mark.parametrize('vectorized,single',[(mechanics.maximum_shear, maximum_shear),
|
2020-11-18 03:26:22 +05:30
|
|
|
(mechanics.equivalent_stress_Mises, equivalent_stress_Mises),
|
|
|
|
(mechanics.equivalent_strain_Mises, equivalent_strain_Mises),
|
|
|
|
(mechanics.stretch_left, stretch_left),
|
|
|
|
(mechanics.stretch_right, stretch_right),
|
2020-11-16 03:44:46 +05:30
|
|
|
])
|
2020-06-09 17:47:31 +05:30
|
|
|
def test_vectorize_1_arg(self,vectorized,single):
|
|
|
|
epsilon = np.random.rand(self.n,3,3)
|
|
|
|
epsilon_vec = np.reshape(epsilon,(self.n//10,10,3,3))
|
|
|
|
for i,v in enumerate(np.reshape(vectorized(epsilon_vec),vectorized(epsilon).shape)):
|
|
|
|
assert np.allclose(single(epsilon[i]),v)
|
2020-04-22 12:28:43 +05:30
|
|
|
|
2020-11-20 03:06:19 +05:30
|
|
|
def test_vectorize_rotation(self):
|
|
|
|
epsilon = Rotation.from_random(self.n).as_matrix()
|
|
|
|
epsilon_vec = np.reshape(epsilon,(self.n//10,10,3,3))
|
2020-11-20 03:16:52 +05:30
|
|
|
for i,v in enumerate(np.reshape(mechanics.rotation(epsilon_vec).as_matrix(),
|
|
|
|
mechanics.rotation(epsilon).as_matrix().shape)):
|
|
|
|
assert np.allclose(rotation(epsilon[i]),v)
|
2020-11-20 03:06:19 +05:30
|
|
|
|
|
|
|
|
2020-11-18 03:26:22 +05:30
|
|
|
@pytest.mark.parametrize('vectorized,single',[(mechanics.stress_Cauchy, stress_Cauchy),
|
|
|
|
(mechanics.stress_second_Piola_Kirchhoff, stress_second_Piola_Kirchhoff)
|
2020-06-09 17:47:31 +05:30
|
|
|
])
|
|
|
|
def test_vectorize_2_arg(self,vectorized,single):
|
|
|
|
P = np.random.rand(self.n,3,3)
|
|
|
|
F = np.random.rand(self.n,3,3)
|
2020-06-10 01:03:13 +05:30
|
|
|
P_vec = np.reshape(P,(self.n//10,10,3,3))
|
|
|
|
F_vec = np.reshape(F,(self.n//10,10,3,3))
|
2020-06-09 17:47:31 +05:30
|
|
|
for i,v in enumerate(np.reshape(vectorized(P_vec,F_vec),vectorized(P,F).shape)):
|
|
|
|
assert np.allclose(single(P[i],F[i]),v)
|
|
|
|
|
|
|
|
|
2020-11-16 05:42:23 +05:30
|
|
|
@pytest.mark.parametrize('vectorized,single',[(mechanics.strain,strain)])
|
|
|
|
def test_vectorize_strain(self,vectorized,single):
|
2020-06-09 17:47:31 +05:30
|
|
|
F = np.random.rand(self.n,3,3)
|
2020-06-10 01:03:13 +05:30
|
|
|
F_vec = np.reshape(F,(self.n//10,10,3,3))
|
2020-06-09 17:47:31 +05:30
|
|
|
t = ['V','U'][np.random.randint(0,2)]
|
|
|
|
m = np.random.random()*10.0 -5.0
|
|
|
|
for i,v in enumerate(np.reshape(vectorized(F_vec,t,m),vectorized(F,t,m).shape)):
|
2020-06-09 18:09:17 +05:30
|
|
|
assert np.allclose(single(F[i],t,m),v)
|
2019-11-22 00:30:28 +05:30
|
|
|
|
2020-11-18 03:26:22 +05:30
|
|
|
@pytest.mark.parametrize('function',[mechanics.stress_Cauchy,
|
|
|
|
mechanics.stress_second_Piola_Kirchhoff,
|
2020-05-01 18:23:40 +05:30
|
|
|
])
|
|
|
|
def test_stress_measures(self,function):
|
|
|
|
"""Ensure that all stress measures are equivalent for no deformation."""
|
2020-04-22 12:28:43 +05:30
|
|
|
P = np.random.rand(self.n,3,3)
|
2020-11-16 03:44:46 +05:30
|
|
|
assert np.allclose(function(P,np.broadcast_to(np.eye(3),(self.n,3,3))),tensor.symmetric(P))
|
2019-11-22 00:30:28 +05:30
|
|
|
|
2019-12-23 15:55:07 +05:30
|
|
|
def test_polar_decomposition(self):
|
2020-02-16 13:45:12 +05:30
|
|
|
"""F = RU = VR."""
|
2020-04-22 12:28:43 +05:30
|
|
|
F = np.broadcast_to(np.eye(3),[self.n,3,3])*np.random.rand(self.n,3,3)
|
2020-11-20 03:16:52 +05:30
|
|
|
R = mechanics.rotation(F).as_matrix()
|
2020-11-16 05:31:32 +05:30
|
|
|
V = mechanics.stretch_left(F)
|
|
|
|
U = mechanics.stretch_right(F)
|
2020-02-16 13:45:12 +05:30
|
|
|
assert np.allclose(np.matmul(R,U),
|
|
|
|
np.matmul(V,R))
|
2020-02-15 18:26:15 +05:30
|
|
|
|
2020-05-28 11:35:02 +05:30
|
|
|
@pytest.mark.parametrize('m',[0.0,np.random.random()*10.,np.random.random()*-10.])
|
2020-11-16 05:42:23 +05:30
|
|
|
def test_strain_no_rotation(self,m):
|
2020-02-16 13:45:12 +05:30
|
|
|
"""Ensure that left and right stretch give same results for no rotation."""
|
2020-04-22 12:28:43 +05:30
|
|
|
F = np.broadcast_to(np.eye(3),[self.n,3,3])*np.random.rand(self.n,3,3)
|
2020-11-16 05:42:23 +05:30
|
|
|
assert np.allclose(mechanics.strain(F,'U',m),
|
|
|
|
mechanics.strain(F,'V',m))
|
2020-02-15 18:26:15 +05:30
|
|
|
|
2020-05-28 11:35:02 +05:30
|
|
|
@pytest.mark.parametrize('m',[0.0,np.random.random()*2.5,np.random.random()*-2.5])
|
2020-11-16 05:42:23 +05:30
|
|
|
def test_strain_rotation_equivalence(self,m):
|
2020-02-16 13:45:12 +05:30
|
|
|
"""Ensure that left and right strain differ only by a rotation."""
|
2020-04-22 12:28:43 +05:30
|
|
|
F = np.broadcast_to(np.eye(3),[self.n,3,3]) + (np.random.rand(self.n,3,3)*0.5 - 0.25)
|
2020-11-16 05:42:23 +05:30
|
|
|
assert np.allclose(np.linalg.det(mechanics.strain(F,'U',m)),
|
|
|
|
np.linalg.det(mechanics.strain(F,'V',m)))
|
2019-11-22 00:30:28 +05:30
|
|
|
|
2020-05-28 11:35:02 +05:30
|
|
|
@pytest.mark.parametrize('m',[0.0,np.random.random(),np.random.random()*-1.])
|
|
|
|
@pytest.mark.parametrize('t',['V','U'])
|
2020-11-16 05:42:23 +05:30
|
|
|
def test_strain_rotation(self,m,t):
|
2020-02-16 13:45:12 +05:30
|
|
|
"""Ensure that pure rotation results in no strain."""
|
2020-11-20 03:06:19 +05:30
|
|
|
F = Rotation.from_random(self.n).as_matrix()
|
2020-11-16 05:42:23 +05:30
|
|
|
assert np.allclose(mechanics.strain(F,t,m),
|
2020-02-16 13:45:12 +05:30
|
|
|
0.0)
|
2020-02-15 18:26:15 +05:30
|
|
|
|
2019-12-23 15:55:07 +05:30
|
|
|
def test_rotation_determinant(self):
|
2020-02-16 13:45:12 +05:30
|
|
|
"""
|
|
|
|
Ensure that the determinant of the rotational part is +- 1.
|
2019-12-23 15:55:07 +05:30
|
|
|
|
2020-02-16 13:45:12 +05:30
|
|
|
Should be +1, but random F might contain a reflection.
|
|
|
|
"""
|
2020-04-22 12:28:43 +05:30
|
|
|
x = np.random.rand(self.n,3,3)
|
2020-11-20 03:06:19 +05:30
|
|
|
assert np.allclose(np.abs(np.linalg.det(mechanics._polar_decomposition(x,'R')[0])),
|
2020-02-16 13:45:12 +05:30
|
|
|
1.0)
|
2019-12-23 15:55:07 +05:30
|
|
|
|
|
|
|
def test_deviatoric_Mises(self):
|
2020-02-16 13:45:12 +05:30
|
|
|
"""Ensure that Mises equivalent stress depends only on deviatoric part."""
|
2020-04-22 12:28:43 +05:30
|
|
|
x = np.random.rand(self.n,3,3)
|
2020-11-18 03:26:22 +05:30
|
|
|
full = mechanics.equivalent_stress_Mises(x)
|
2020-11-19 19:08:54 +05:30
|
|
|
dev = mechanics.equivalent_stress_Mises(tensor.deviatoric(x))
|
2020-02-16 13:45:12 +05:30
|
|
|
assert np.allclose(full,
|
|
|
|
dev)
|
2019-12-23 15:55:07 +05:30
|
|
|
|
2020-11-19 19:08:54 +05:30
|
|
|
@pytest.mark.parametrize('Mises_equivalent',[mechanics.equivalent_strain_Mises,
|
|
|
|
mechanics.equivalent_stress_Mises])
|
|
|
|
def test_spherical_Mises(self,Mises_equivalent):
|
|
|
|
"""Ensure that Mises equivalent strain/stress of spherical strain is 0."""
|
2020-04-22 12:28:43 +05:30
|
|
|
x = np.random.rand(self.n,3,3)
|
2020-11-19 19:08:54 +05:30
|
|
|
assert np.allclose(Mises_equivalent(tensor.spherical(x,True)),
|
2020-02-16 13:45:12 +05:30
|
|
|
0.0)
|
2019-11-22 00:30:28 +05:30
|
|
|
|
2019-11-22 01:31:01 +05:30
|
|
|
|
|
|
|
def test_Mises(self):
|
2020-02-16 13:45:12 +05:30
|
|
|
"""Ensure that equivalent stress is 3/2 of equivalent strain."""
|
2020-04-22 12:28:43 +05:30
|
|
|
x = np.random.rand(self.n,3,3)
|
2020-11-18 03:26:22 +05:30
|
|
|
assert np.allclose(mechanics.equivalent_stress_Mises(x)/mechanics.equivalent_strain_Mises(x),
|
2020-02-16 13:45:12 +05:30
|
|
|
1.5)
|
2020-02-15 18:26:15 +05:30
|
|
|
|
2020-02-15 21:25:12 +05:30
|
|
|
def test_spherical_no_shear(self):
|
2020-02-16 13:45:12 +05:30
|
|
|
"""Ensure that sherical stress has max shear of 0.0."""
|
2020-11-19 19:08:54 +05:30
|
|
|
A = tensor.spherical(tensor.symmetric(np.random.rand(self.n,3,3)),True)
|
2020-02-16 13:45:12 +05:30
|
|
|
assert np.allclose(mechanics.maximum_shear(A),0.0)
|
2020-11-20 03:06:19 +05:30
|
|
|
|
|
|
|
def test_invalid_decomposition(self):
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
mechanics._polar_decomposition(np.random.rand(10,3,3),'A')
|
2023-02-21 01:14:40 +05:30
|
|
|
|
|
|
|
def test_invalid_strain(self):
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
mechanics.strain(np.random.rand(10,3,3),'A',0)
|