2020-11-16 03:44:46 +05:30
|
|
|
"""
|
2021-04-24 18:17:52 +05:30
|
|
|
Tensor mathematics.
|
2020-11-16 03:44:46 +05:30
|
|
|
|
2021-04-24 18:17:52 +05:30
|
|
|
All routines operate on numpy.ndarrays of shape (...,3,3).
|
2020-11-16 03:44:46 +05:30
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import numpy as _np
|
|
|
|
|
|
|
|
|
2021-11-01 03:07:54 +05:30
|
|
|
def deviatoric(T: _np.ndarray) -> _np.ndarray:
|
2020-11-19 19:08:54 +05:30
|
|
|
"""
|
|
|
|
Calculate deviatoric part of a tensor.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-12-06 12:08:40 +05:30
|
|
|
T : numpy.ndarray, shape (...,3,3)
|
2020-11-19 19:08:54 +05:30
|
|
|
Tensor of which the deviatoric part is computed.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
2021-12-06 12:08:40 +05:30
|
|
|
T' : numpy.ndarray, shape (...,3,3)
|
2020-11-19 19:08:54 +05:30
|
|
|
Deviatoric part of T.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return T - spherical(T,tensor=True)
|
|
|
|
|
|
|
|
|
2021-11-01 03:07:54 +05:30
|
|
|
def eigenvalues(T_sym: _np.ndarray) -> _np.ndarray:
|
2020-11-16 03:44:46 +05:30
|
|
|
"""
|
2020-11-16 05:31:32 +05:30
|
|
|
Eigenvalues, i.e. principal components, of a symmetric tensor.
|
2020-11-16 03:44:46 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-12-06 12:08:40 +05:30
|
|
|
T_sym : numpy.ndarray, shape (...,3,3)
|
2020-11-16 03:44:46 +05:30
|
|
|
Symmetric tensor of which the eigenvalues are computed.
|
|
|
|
|
2020-11-16 05:31:32 +05:30
|
|
|
Returns
|
|
|
|
-------
|
2021-12-06 12:08:40 +05:30
|
|
|
lambda : numpy.ndarray, shape (...,3)
|
2020-11-16 05:31:32 +05:30
|
|
|
Eigenvalues of T_sym sorted in ascending order, each repeated
|
|
|
|
according to its multiplicity.
|
|
|
|
|
2020-11-16 03:44:46 +05:30
|
|
|
"""
|
|
|
|
return _np.linalg.eigvalsh(symmetric(T_sym))
|
|
|
|
|
|
|
|
|
2022-01-27 15:15:14 +05:30
|
|
|
def eigenvectors(T_sym: _np.ndarray,
|
|
|
|
RHS: bool = False) -> _np.ndarray:
|
2020-11-16 03:44:46 +05:30
|
|
|
"""
|
2020-11-16 05:31:32 +05:30
|
|
|
Eigenvectors of a symmetric tensor.
|
2020-11-16 03:44:46 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-12-06 12:08:40 +05:30
|
|
|
T_sym : numpy.ndarray, shape (...,3,3)
|
2020-11-16 03:44:46 +05:30
|
|
|
Symmetric tensor of which the eigenvectors are computed.
|
|
|
|
RHS: bool, optional
|
2020-11-16 05:31:32 +05:30
|
|
|
Enforce right-handed coordinate system. Defaults to False.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
2021-12-06 12:08:40 +05:30
|
|
|
x : numpy.ndarray, shape (...,3,3)
|
2020-11-16 05:31:32 +05:30
|
|
|
Eigenvectors of T_sym sorted in ascending order of their
|
|
|
|
associated eigenvalues.
|
2020-11-16 03:44:46 +05:30
|
|
|
|
|
|
|
"""
|
2022-01-30 03:08:17 +05:30
|
|
|
_,v = _np.linalg.eigh(symmetric(T_sym))
|
2020-11-16 03:44:46 +05:30
|
|
|
|
2022-01-29 22:46:19 +05:30
|
|
|
if RHS: v[_np.linalg.det(v) < 0.0,:,2] *= -1.0
|
2020-11-16 03:44:46 +05:30
|
|
|
return v
|
2020-11-19 18:35:59 +05:30
|
|
|
|
|
|
|
|
2022-01-27 15:15:14 +05:30
|
|
|
def spherical(T: _np.ndarray,
|
|
|
|
tensor: bool = True) -> _np.ndarray:
|
2020-11-19 19:08:54 +05:30
|
|
|
"""
|
|
|
|
Calculate spherical part of a tensor.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-12-06 12:08:40 +05:30
|
|
|
T : numpy.ndarray, shape (...,3,3)
|
2020-11-19 19:08:54 +05:30
|
|
|
Tensor of which the spherical part is computed.
|
|
|
|
tensor : bool, optional
|
|
|
|
Map spherical part onto identity tensor. Defaults to True.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
2021-12-06 12:08:40 +05:30
|
|
|
p : numpy.ndarray, shape (...,3,3)
|
2020-11-19 19:08:54 +05:30
|
|
|
unless tensor == False: shape (...,)
|
|
|
|
Spherical part of tensor T. p is an isotropic tensor.
|
|
|
|
|
|
|
|
"""
|
|
|
|
sph = _np.trace(T,axis2=-2,axis1=-1)/3.0
|
|
|
|
return _np.einsum('...jk,...',_np.eye(3),sph) if tensor else sph
|
|
|
|
|
|
|
|
|
2021-11-01 03:07:54 +05:30
|
|
|
def symmetric(T: _np.ndarray) -> _np.ndarray:
|
2020-11-19 18:35:59 +05:30
|
|
|
"""
|
|
|
|
Symmetrize tensor.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-12-06 12:08:40 +05:30
|
|
|
T : numpy.ndarray, shape (...,3,3)
|
2020-11-19 18:35:59 +05:30
|
|
|
Tensor of which the symmetrized values are computed.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
2021-12-06 12:08:40 +05:30
|
|
|
T_sym : numpy.ndarray, shape (...,3,3)
|
2020-11-19 18:35:59 +05:30
|
|
|
Symmetrized tensor T.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return (T+transpose(T))*0.5
|
|
|
|
|
|
|
|
|
2021-11-01 03:07:54 +05:30
|
|
|
def transpose(T: _np.ndarray) -> _np.ndarray:
|
2020-11-19 18:35:59 +05:30
|
|
|
"""
|
|
|
|
Transpose tensor.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-12-06 12:08:40 +05:30
|
|
|
T : numpy.ndarray, shape (...,3,3)
|
2020-11-19 18:35:59 +05:30
|
|
|
Tensor of which the transpose is computed.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
2021-12-06 12:08:40 +05:30
|
|
|
T.T : numpy.ndarray, shape (...,3,3)
|
2020-11-19 18:35:59 +05:30
|
|
|
Transpose of tensor T.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return _np.swapaxes(T,axis2=-2,axis1=-1)
|