use nalgebra::{Matrix3, Vector3};
fn inverse(f: &Matrix3<f64>) -> Matrix3<f64> {
f.try_inverse()
.unwrap_or_else(|| Matrix3::from_element(f64::NAN))
}
pub(crate) fn pk2_from_cauchy(sigma: &Matrix3<f64>, f: &Matrix3<f64>) -> Matrix3<f64> {
let fi = inverse(f);
f.determinant() * fi * sigma * fi.transpose()
}
pub(crate) fn pk2_from_kirchhoff(tau: &Matrix3<f64>, f: &Matrix3<f64>) -> Matrix3<f64> {
let fi = inverse(f);
fi * tau * fi.transpose()
}
pub(crate) fn pk2_from_pk1(p: &Matrix3<f64>, f: &Matrix3<f64>) -> Matrix3<f64> {
inverse(f) * p
}
pub(crate) fn cauchy_from_pk2(s: &Matrix3<f64>, f: &Matrix3<f64>) -> Matrix3<f64> {
f * s * f.transpose() / f.determinant()
}
pub(crate) fn kirchhoff_from_pk2(s: &Matrix3<f64>, f: &Matrix3<f64>) -> Matrix3<f64> {
f * s * f.transpose()
}
pub(crate) fn pk1_from_pk2(s: &Matrix3<f64>, f: &Matrix3<f64>) -> Matrix3<f64> {
f * s
}
fn log_divided_difference(a: f64, b: f64) -> f64 {
let x = (a - b) / b;
if x.abs() < 1e-6 {
(1.0 - x / 2.0 + x * x / 3.0) / b
} else {
(a / b).ln() / (a - b)
}
}
fn hencky_weights(f: &Matrix3<f64>) -> (Matrix3<f64>, Matrix3<f64>) {
let eig = (f.transpose() * f).symmetric_eigen();
let c: Vector3<f64> = eig.eigenvalues;
let theta = Matrix3::from_fn(|a, b| {
if a == b {
1.0 / c[a]
} else {
log_divided_difference(c[a], c[b])
}
});
(eig.eigenvectors, theta)
}
pub(crate) fn pk2_from_hencky(t: &Matrix3<f64>, f: &Matrix3<f64>) -> Matrix3<f64> {
let (q, theta) = hencky_weights(f);
let in_basis = q.transpose() * t * q;
q * in_basis.component_mul(&theta) * q.transpose()
}
pub(crate) fn hencky_from_pk2(s: &Matrix3<f64>, f: &Matrix3<f64>) -> Matrix3<f64> {
let (q, theta) = hencky_weights(f);
let in_basis = q.transpose() * s * q;
q * in_basis.component_div(&theta) * q.transpose()
}
pub(crate) fn polar_rotation(f: &Matrix3<f64>) -> Matrix3<f64> {
let eig = (f.transpose() * f).symmetric_eigen();
let q = eig.eigenvectors;
let u_inverse = Matrix3::from_diagonal(&eig.eigenvalues.map(|c| 1.0 / c.sqrt()));
f * q * u_inverse * q.transpose()
}
#[cfg(test)]
mod tests {
use super::*;
fn some_f() -> Matrix3<f64> {
Matrix3::new(1.2, 0.3, -0.1, 0.05, 0.9, 0.2, 0.1, -0.15, 1.1)
}
fn some_s() -> Matrix3<f64> {
Matrix3::new(10.0, 2.0, -1.0, 2.0, -4.0, 0.5, -1.0, 0.5, 3.0)
}
#[test]
fn every_measure_comes_back_to_the_same_pk2() {
let (f, s) = (some_f(), some_s());
let back = [
pk2_from_cauchy(&cauchy_from_pk2(&s, &f), &f),
pk2_from_kirchhoff(&kirchhoff_from_pk2(&s, &f), &f),
pk2_from_pk1(&pk1_from_pk2(&s, &f), &f),
pk2_from_hencky(&hencky_from_pk2(&s, &f), &f),
];
for b in back {
assert!((b - s).norm() < 1e-12 * s.norm());
}
}
#[test]
fn the_cauchy_stress_of_a_rotation_is_the_rotated_stress() {
let (c, sn) = (0.6_f64, 0.8_f64);
let r = Matrix3::new(c, -sn, 0.0, sn, c, 0.0, 0.0, 0.0, 1.0);
let s = some_s();
let sigma = cauchy_from_pk2(&s, &r);
assert!((sigma - r * s * r.transpose()).norm() < 1e-12 * s.norm());
}
#[test]
fn at_rest_and_in_rotation_the_hencky_dual_is_s() {
let (c, sn) = (0.6_f64, 0.8_f64);
let r = Matrix3::new(c, -sn, 0.0, sn, c, 0.0, 0.0, 0.0, 1.0);
let t = some_s();
for f in [Matrix3::identity(), r] {
assert!((pk2_from_hencky(&t, &f) - t).norm() < 1e-12 * t.norm());
}
}
#[test]
fn under_a_stretch_the_hencky_dual_follows_the_divided_differences() {
let f = Matrix3::new(2.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);
let t = Matrix3::new(8.0, 3.0, 0.0, 3.0, 1.0, 5.0, 0.0, 5.0, 2.0);
let s = pk2_from_hencky(&t, &f);
assert!((s[(0, 0)] - 2.0).abs() < 1e-12);
assert!((s[(0, 1)] - 3.0 * 4.0_f64.ln() / 3.0).abs() < 1e-12);
assert!((s[(1, 2)] - 5.0).abs() < 1e-12);
}
#[test]
fn the_polar_rotation_is_the_rotation_a_stretch_is_composed_with() {
let axis = nalgebra::Unit::new_normalize(Vector3::new(1.0, 2.0, 0.5));
let r0 = nalgebra::Rotation3::from_axis_angle(&axis, 0.7).into_inner();
let u0 = Matrix3::new(1.3, 0.2, -0.1, 0.2, 0.8, 0.15, -0.1, 0.15, 1.1);
let r = polar_rotation(&(r0 * u0));
assert!((r - r0).norm() < 1e-12, "{r}");
assert!((polar_rotation(&u0) - Matrix3::identity()).norm() < 1e-12);
}
}