Expand description
Russell - Rust Scientific Library
russell_tensor: Tensor analysis, calculus, and functions for continuum mechanics
Important: This crate depends on external libraries (non-Rust). Thus, please check the Installation Instructions on the GitHub Repository.
§Introduction
This library implements structures and functions for tensor analysis and calculus, with focus on applications in engineering and Continuum Mechanics. The essential functionality for the targeted applications includes first-order, second-order, third-order, and fourth-order tensors, scalar “invariants,” and derivatives.
§Capabilities
- Tensor1 — First-order tensors (vectors) in R³. Includes operations such as the dot and cross products
- Tensor2 — Second-order tensors in R³×R³. Allows symmetric specialization. Includes functions such as the determinant, inverse, norm, and invariants (principal, deviatoric, Lode, octahedral, …)
- Tensor3 — Third-order tensors R³×R³×R³. Allows minor-symmetric specialization. Includes functions such as permutation (Levi-Civita) tensor
- Tensor4 — Fourth-order tensors R³×R³×R³×R³. Allows minor-symmetric specialization. Includes functions to generate isotropic tensors.
- EigenValuesT2, EigenProjsT2, EigenProjDerivsT2 — Eigenvalues, eigenprojectors, and the derivatives of the eigenprojectors for symmetric second-order tensors.
- LinElasticity — The linear elasticity equations for small-strain problems (Generalized Hooke’s law)
- analysis::PiezoDatabase — A database of piezoelectric materials (dielectric permittivity, piezoelectric, and stiffness tensors) loaded from JSON.
- Polar decomposition — Computes the polar decomposition
F = R U = V Rof a general Tensor2 using the classic Eigen/SVD algorithms, the iterative Brannon algorithm, or the quaternion-based Higham & Noferini algorithm (see PolarAlgo and polar_decomp_mx). - Constants — Includes identity, transposition, and projector tensors, as well as the ADD/SET operation selectors.
- Operations between tensors — Includes addition, single and double contractions (dot and ddot), and dyadic products. Most operations support both overwriting (SET) and accumulation (ADD) semantics.
- Derivatives — Implements first and second derivatives of invariants and tensor functions (e.g., the inverse and squared tensors)
- Display — The tensors implement the
Displaytrait (with configurable precision) and provide ascientificmethod returning a scientific-notation string for printing or logging.
§Kelvin-Mandel notation
Internally, tensors are stored in the Kelvin-Mandel basis (Kelvin-Mandel notation).
In the Kelvin-Mandel basis, a second-order tensor is mapped to a column matrix (vector), a third-order tensor is mapped to a rectangular matrix, and a fourth-order tensor is mapped to a square matrix. The √2 factors make the mapping isometric; thus the tensor norm is preserved and standard matrix/vector operations can be used directly.
The dimension — the const generic N of Tensor2/Tensor4, and M/N of
Tensor3 — selects the representation:
9— all components (general): 9×1 / 9×3 / 3×9 / 9×96— symmetric Tensor2 / minor-symmetric Tensor3/Tensor4: 6×1 / 6×3 / 3×6 / 6×64— symmetric Tensor2 / minor-symmetric Tensor3/Tensor4 (generalized plane): 4×1 / 4×3 / 3×4 / 4×4
The dimensions above correspond to Tensor2 (vector), Tensor3 (Case A / Case B rectangular matrix), and Tensor4 (square matrix), respectively.
§Reduced dimension and truncation (chop) strategy
The N = 4 case is the four-dimensional subspace {00, 11, 22, 01} of
symmetric tensors—the out-of-plane normal component T₂₂ is kept,
while only the out-of-plane shears are set to zero
(T₁₂ = T₀₂ = 0, Kelvin-Mandel components 4 and 5).
The tensor operators considered here (e.g., ssd_fn, qsd_fn, and the second
derivatives of the invariants) are polynomial in the components, and for an
input in this subspace they are block diagonal with respect to the
{0,1,2,3} and {4,5} partitions: every off-diagonal block is proportional
to the (zero) out-of-plane shears. Consequently, restricting both the input and
the output to N components is exact — not an approximation: the reduced
N × N operator is precisely the corresponding block of the full 6 × 6
operator.
A Tensor3 is stored as a rectangular Kelvin-Mandel matrix with dimensions (M, N)
set by const generics. Two cases are considered, where DIM (the leading dimension)
is one of 4, 6, or 9:
- Case A
(DIM, 3)—M = DIM,N = 3: the Tensor3 acts on a Tensor1 (vector) yielding a Tensor2 (T = H · u) - Case B
(3, DIM)—M = 3,N = DIM: the Tensor3 acts on a Tensor2 yielding a Tensor1 (vector) (v = M : S)
§Standard vs Kelvin-Mandel components
The tensor accessors follow a naming convention that distinguishes the standard (Cartesian) components Tᵢⱼ / Hᵢⱼₖ / Dᵢⱼₖₗ from the Kelvin-Mandel components stored internally:
- Accessors dealing with standard components carry the
stdqualifier in their names:- Tensor2 — Tensor2::set_std_matrix, Tensor2::from_std_matrix, Tensor2::get_std, Tensor2::as_std_matrix, Tensor2::to_std_matrix, Tensor2::as_std_matrix_2d, Tensor2::sym_set_std, Tensor2::sym_add_std
- Tensor3 — Tensor3::from_std_array, Tensor3::from_std_matrix, Tensor3::get_std, Tensor3::as_std_array, Tensor3::to_std_array, Tensor3::as_std_matrix, Tensor3::to_std_matrix, Tensor3::sym_set_std
- Tensor4 — Tensor4::from_std_array, Tensor4::from_std_matrix, Tensor4::get_std, Tensor4::as_std_array, Tensor4::to_std_array, Tensor4::as_std_matrix, Tensor4::to_std_matrix, Tensor4::sym_set_std
- Accessors dealing directly with the Kelvin-Mandel components carry no qualifier:
Note: Tensor1 stores the three standard components directly (there is no Kelvin-Mandel mapping for first-order tensors); access them with Tensor1::get and Tensor1::set.
§Optional features
The following (Rust) features are available:
intel_mkl— use Intel MKL instead of OpenBLAS
§Examples
use russell_tensor::*;
fn main() -> Result<(), StrError> {
// Allocate a symmetric second-order tensor given the standard components
let a = Tensor2::<6>::from_std_matrix(&[
[1.0, 2.0, 3.0],
[2.0, 2.0, 4.0],
[3.0, 4.0, 3.0],
])?;
// Compute the principal invariants
let ii1 = a.invariant_ii1();
let ii2 = a.invariant_ii2();
let ii3 = a.invariant_ii3();
println!("I1 = {:.6}", ii1);
println!("I2 = {:.6}", ii2);
println!("I3 = {:.6}", ii3);
Ok(())
}Modules§
- analysis
- This module contains auxiliary analysis tools
- z_
reference_ loop_ fns - Loop-based reference implementations of the tensor operations used by the
benchmarks. These are simple, obviously-correct implementations (using the
M_TO_IJ/MN_TO_IJKLindex mappings) that are used in tests to cross-check the optimized (unrolled) production implementations.
Structs§
- Eigen
Proj Derivs T2 - Assists in calculating the derivatives of the eigenprojectors
- Eigen
Projs T2 - Assists in calculating the eigenprojectors of a symmetric second-order tensor
- Eigen
Values T2 - Assists in calculating the eigenvalues of a symmetric second-order tensor
- LinElasticity
- Implements the linear elasticity equations for small-strain problems
- Sample
Tensor2 - Collects values related to a sample Tensor2
- Samples
Tensor2 - Holds second-order tensor samples
- Samples
Tensor3 - Holds third-order tensor samples
- Samples
Tensor4 - Holds fourth-order tensor samples
- Tensor1
- Defines a first-order tensor (vector) in R³
- Tensor2
- Defines a second-order tensor in R³×R³
- Tensor3
- Defines a third-order tensor in R³×R³×R³
- Tensor4
- Defines a fourth-order tensor in R³×R³×R³×R³
- Workspace
Deriv2 Lode - Sets a workspace with temporary variables to calculate the second derivative of the Lode angle
Enums§
- Eigen
ValMethod - Defines the method to calculate the eigenvalues
- Polar
Algo - Specifies the polar decomposition algorithm
Constants§
- ADD
- Identifies an operation as an addition (update)
- IDENTIT
Y2 - Second-order identity tensor in Kelvin-Mandel basis (I)
- IDENTIT
Y4 - Fourth-order identity tensor in Kelvin-Mandel basis (II)
- IJKL_
TO_ MN - Maps (i,j,k,l) of Tensor4 to the (m,n)-th position in the matrix representation
- IJKL_
TO_ MN_ SYM - Maps (i,j,k,l) of Tensor4 to the (m,n)-th position in the matrix representation (minor-symmetric version)
- IJK_
TO_ MN_ CASE_ A - Maps (i,j,k) of Tensor3 to the (m,n)-th position in the matrix representation (Case A)
- IJK_
TO_ MN_ CASE_ B - Maps (i,j,k) of Tensor3 to the (m,n)-th position in the matrix representation (Case B)
- IJK_
TO_ MN_ SYM_ CASE_ A - Maps (i,j,k) of Tensor3 to the (m,n)-th position in the matrix representation (minor-symmetric; Case A)
- IJK_
TO_ MN_ SYM_ CASE_ B - Maps (i,j,k) of Tensor3 to the (m,n)-th position in the matrix representation (minor-symmetric; Case B)
- IJ_TO_M
- Maps (i,j) of Tensor2 to the m-th position in the vector representation
- IJ_
TO_ M_ SYM - Maps (i,j) of Tensor2 to the m-th position in the vector representation (symmetric version)
- MN_
TO_ IJKL - Maps the (m,n)-th position in the matrix representation to (i,j,k,l) of Tensor4
- MN_
TO_ IJK_ CASE_ A - Maps the (m,n)-th position in the matrix representation to (i,j,k) of Tensor3 (Case A)
- MN_
TO_ IJK_ CASE_ B - Maps the (m,n)-th position in the matrix representation to (i,j,k) of Tensor3 (Case B)
- M_TO_IJ
- Maps the m-th position in the vector representation to the index (i,j) of Tensor2
- OK_
EIGENPROJ_ RULES - Indicates that a set of three Tensor2 satisfy the eigenprojectors rules
- ONE_
BY_ 3 - 1/3
- P_DEV
- Fourth-order deviatoric making projector (Pdev)
- P_ISO
- Fourth-order isotropic making projector (Piso)
- P_SKEW
- Fourth-order skew making projector (Pskew)
- P_SYM
- Fourth-order symmetric making projector (Psym)
- P_
SYMDEV - Fourth-order symmetric-deviatoric projector in Kelvin-Mandel basis
- SET
- Identifies an operation as a setting
- SQRT_2
- sqrt(2) https://oeis.org/A002193
- SQRT_3
- sqrt(3) https://oeis.org/A002194
- SQRT_6
- sqrt(6) https://oeis.org/A010464
- SQRT_
2_ BY_ 3 - sqrt(2/3) https://oeis.org/A157697
- SQRT_
3_ BY_ 2 - sqrt(3/2) https://oeis.org/A115754
- TOL_J2
- Tolerance to avoid zero division with the J2 invariant
- TRACE_
PROJECTION - Fourth-order trace-projection tensor (JJ)
- TRANSPOSITION
- Fourth-order transposition tensor in Kelvin-Mandel basis (TT)
- TWO_
BY_ 3 - 2/3
Functions§
- deriv1_
invariant_ d - Calculates the first derivative of d w.r.t. the symmetric tensor
- deriv1_
invariant_ ii2 - Calculates the first derivative of the I2 invariant w.r.t. its defining tensor
- deriv1_
invariant_ ii3 - Calculates the first derivative of the I3 invariant w.r.t. its defining tensor
- deriv1_
invariant_ jj2 - Calculates the first derivative of the J2 invariant w.r.t. the symmetric tensor
- deriv1_
invariant_ jj3 - Calculates the first derivative of the J3 invariant w.r.t. the symmetric tensor
- deriv1_
invariant_ lode - Calculates the first derivative of the Lode invariant w.r.t. the symmetric tensor
- deriv1_
invariant_ p - Calculates the first derivative of p w.r.t. the symmetric tensor
- deriv1_
invariant_ q - Calculates the first derivative of q (von Mises) w.r.t. the symmetric tensor
- deriv1_
invariant_ r - Calculates the first derivative of r w.r.t. the symmetric tensor
- deriv1_
norm - Calculates the first derivative of the norm w.r.t. the defining Tensor2
- deriv2_
invariant_ ii2 - Calculates the second derivative of the I2 invariant w.r.t. its defining tensor
- deriv2_
invariant_ ii3 - Calculates the second derivative of the I3 invariant w.r.t. its defining tensor
- deriv2_
invariant_ jj2 - Calculates the second derivative of the J2 invariant w.r.t. the symmetric tensor
- deriv2_
invariant_ jj3 - Calculates the second derivative of the J3 invariant w.r.t. the symmetric tensor
- deriv2_
invariant_ lode - Calculates the second derivative of the Lode invariant w.r.t. the symmetric tensor
- deriv2_
invariant_ q - Calculates the second derivative of the deviatoric invariant (von Mises) w.r.t. the symmetric tensor
- deriv2_
invariant_ r - Calculates the second derivative of r w.r.t. the symmetric tensor
- deriv_
inverse_ tensor - Calculates the derivative of the inverse tensor w.r.t. the defining Tensor2
- deriv_
inverse_ tensor_ sym - Calculates the derivative of the inverse tensor w.r.t. a symmetric Tensor2
- deriv_
squared_ tensor - Calculates the derivative of the squared tensor w.r.t. a Tensor2
- deriv_
squared_ tensor_ sym - Calculates the derivative of the squared tensor w.r.t. a symmetric Tensor2
- dsd_fn
- Performs the duo-sum-dyadic (dsd) operation with two Tensor2 yielding a minor-symmetric Tensor4
- eigen_
octahedral - Rotates the eigenvalues to the principal values space
- eigenprojector_
rules - Checks the properties (rules) of a set of candidate eigenprojectors
- polar_
decomp - Performs the polar decomposition F = R U = V R (using the default method)
- polar_
decomp_ mx - Performs the polar decomposition F = R U = V R (selectable method version)
- qsd_fn
- Performs the quad-sum-dyadic (qsd) operation with two Tensor2 yielding a minor-symmetric Tensor4
- ssd_fn
- Performs the self-sum-dyadic (ssd) operation with a Tensor2 yielding a minor-symmetric Tensor4
- t1_
dot_ t2 - Performs the single dot operation between a vector and a Tensor2
- t1_
dot_ t3 - Performs the single-dot operation between a Tensor3 and a vector resulting in a Tensor2 (Case B)
- t1_
dyad_ t1 - Performs the dyadic product between two vectors resulting in a second-order tensor
- t2_add
- Adds two second-order tensors
- t2_
ddot_ t2 - Performs the double-dot (ddot) operation between two Tensor2 (inner product)
- t2_
ddot_ t3 - Performs the double-dot operation between a Tensor2 and a Tensor3 resulting in a Tensor1 (Case A)
- t2_
ddot_ t4 - Performs the double-dot (ddot) operation between a Tensor2 and a Tensor4
- t2_
ddot_ t4_ ddot_ t2 - Computes Tensor2 double-dot Tensor4 double-dot Tensor2
- t2_
dot_ t1 - Performs the single dot operation between a Tensor2 and a vector
- t2_
dyad_ t2 - Performs the dyadic product between two Tensor2 resulting a Tensor4
- t2_
matmul - Performs the matrix multiplication between two Tensor2
- t2_
matmulx - Performs a triple matrix multiplication: C = α A · B · Aᵀ or C = α Aᵀ · B · A
- t2_
odyad_ t2 - Performs the overbar dyadic product between two Tensor2 resulting in a (general) Tensor4
- t2_
udyad_ t2 - Performs the underbar dyadic product between two Tensor2 resulting in a (general) Tensor4
- t3_add
- Adds two third-order tensors
- t3_
ddot_ t2 - Performs the double-dot operation between a Tensor3 and a Tensor2 resulting in a vector (Case B)
- t3_
dot_ t1 - Performs the single-dot operation between a Tensor3 and a Tensor1 resulting in a Tensor2 (Case A)
- t4_add
- Adds two fourth-order tensors
- t4_
ddot_ t2 - Performs the double-dot (ddot) operation between a Tensor4 and a Tensor2
- t4_
ddot_ t4 - Performs the double-dot (ddot) operation between two Tensor4
- t4_
ddot_ t2_ dyad_ t2_ ddot_ t4 - Computes Tensor4 double-dot Tensor2 dyadic Tensor2 double-dot Tensor4
Type Aliases§
- StrError
- Defines the error type as a static string