pyrucast 0.15.0

Finite-element library in Rust, inspired by cast3m, exposed to Python
Documentation
//! von Mises (J2) plasticity — perfect, or with linear isotropic hardening.
//!
//! The archetypal metal-plasticity law: yielding depends on the **deviator**
//! alone, so it is insensitive to hydrostatic pressure and plastic flow is
//! isochoric. The yield surface is a cylinder about the hydrostatic axis,
//!
//! ```text
//! f(σ, p) = q − σ_y(p)          q = √(3 J₂)
//! ```
//!
//! with `σ_y(p) = σ_y + H·p`. `H = 0` is the perfect law — one code path serves
//! both, which is why they share this file rather than duplicating a return map
//! that differs by a single term.
//!
//! ## The closed-form return
//!
//! Associated flow on a cylinder means the return is **radial**: the deviator
//! is scaled, its direction untouched, and the hydrostatic part is left alone.
//! Consistency gives the multiplier in one step, with no iteration:
//!
//! ```text
//! Δp = (q_trial − σ_y(p_A)) / (3μ + H)
//! ```
//!
//! That closed form is why von Mises does not go through the cutting plane: an
//! exact answer beats a converged one.

use super::law::ReturnMapLawKind;
use crate::error::Result;
use crate::models::continuum::elastic::elastic_tangent;
use crate::models::plasticity::law::PlasticLaw;
use crate::models::plasticity::law::{MatParams, PlasticStep, PrevState};
use crate::models::tensor::Kinematics;
use crate::models::tensor::{deviator, von_mises_stress};

/// Positions in this law's material contract, `["E", "nu", "sigma_y"(, "H")]` —
/// the order the law declares, and therefore the order the zone layout resolves.
const SIGMA_Y: usize = 2;
/// The hardening modulus, which only the hardening variant carries.
const H: usize = 3;

/// Radial return onto `q = σ_y + H·p`.
///
/// `hardening` is `H`; pass `0.0` for the perfect law. Returns the updated
/// `(σ, ε_p, p)`, all full 3-D.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::SubElementField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::plasticity;
/// # use pyrucast::models::continuum::elastic;
/// # use pyrucast::models::tensor;
/// # use pyrucast::models::plasticity::law::{self, MatParams, PlasticLaw, PrevState};
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let materiau = SubElementField::from_uniform_per_component(
/// #     fes.get(0).unwrap(), vec!["E".into(), "nu".into(), "sigma_y".into()], &[210000.0, 0.3, 250.0]).unwrap();
/// # let idx_mat: Vec<u32> = (0..materiau.point_values(0, 0).unwrap().len() as u32).collect();
/// # let opt_mat = [pyrucast::containers::field::ABSENT_COMPONENT; 8];
/// # let mat = MatParams::new(materiau.point_values(0, 0).unwrap(), &idx_mat, &opt_mat);
/// # let repos = PrevState { eps: [0.0; 6], sigma: [0.0; 6], eps_p: [0.0; 6], p: 0.0,
/// #                         vars: &[] };
/// // Without hardening (`hardening = 0`), the projection brings q back
/// // **exactly** onto σ_y, and the plastic strain is purely deviatoric.
/// let trial = [400.0, 0.0, 0.0, 0.0, 0.0, 0.0];
/// let pas = plasticity::von_mises::return_map(&trial, &repos, &mat, 0.0);
/// assert!((tensor::von_mises_stress(&pas.sigma) - 250.0).abs() < 1e-6);
/// assert!(tensor::i1(&pas.eps_p).abs() < 1e-12); // écoulement isochore
///
/// // With isotropic hardening, the threshold rises by H·p: the stress
/// // retained is **larger**.
/// let dur = plasticity::von_mises::return_map(&trial, &repos, &mat, 20_000.0);
/// assert!(tensor::von_mises_stress(&dur.sigma) > 250.0);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn return_map(
    trial: &[f64; 6],
    prev: &PrevState,
    mat: &MatParams,
    hardening: f64,
) -> PlasticStep {
    let sigma_y0 = mat.get(SIGMA_Y);
    let q = von_mises_stress(trial);
    let yield_now = sigma_y0 + hardening * prev.p;
    let f = q - yield_now;
    if f <= 0.0 || q == 0.0 {
        return PlasticStep::elastic(trial, prev); // elastic
    }
    // Consistency: q − 3μΔp = σ_y + H(p + Δp).
    let dp = f / (3.0 * mat.mu + hardening);
    let s_trial = deviator(trial);
    let mean = (trial[0] + trial[1] + trial[2]) / 3.0;
    let scale = (yield_now + hardening * dp) / q;
    // Flow direction n = (3/2)·s/q, so Δε_p = Δp·n = (3Δp/2q)·s — a tensor, so
    // the off-diagonals take the same factor with no engineering doubling.
    let factor = 1.5 * dp / q;

    let mut sigma = [0.0; 6];
    let mut eps_p = prev.eps_p;
    for i in 0..6 {
        let s_new = s_trial[i] * scale;
        sigma[i] = if i < 3 { s_new + mean } else { s_new };
        eps_p[i] += factor * s_trial[i];
    }
    PlasticStep::new(sigma, eps_p, prev.p + dp, &[])
}

/// The consistent tangent `D_alg = ∂σ(B)/∂ε(B)` of the radial return, evaluated
/// at the trial stress — the classical algorithmic modulus,
///
/// ```text
/// D = K·1⊗1 + 2μθ·I_dev − 2μθ̄·n̂⊗n̂
/// θ = σ_y(p+Δp)/q_trial          θ̄ = 3μ/(3μ + H) − (1 − θ)
/// ```
/// # use pyrucast::models::continuum::elastic;
///
/// with `n̂ = s_trial/‖s_trial‖` the **unit** deviatoric direction. For `H = 0`
/// the two coefficients collapse (`θ̄ = θ`) and this is exactly the perfect-J2
/// tangent — which is why hardening costs one extra term rather than a second
/// derivation.
///
/// Note the difference from the **continuum** elastoplastic modulus: `θ` here
/// accounts for the *finite* step, and dropping it would cost Newton its
/// quadratic convergence.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::SubElementField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::continuum::elastic;
/// # use pyrucast::models::plasticity;
/// # use pyrucast::models::plasticity::law::{self, MatParams, PlasticLaw, PrevState};
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]
/// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
/// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
/// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
/// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
/// # let materiau = SubElementField::from_uniform_per_component(
/// #     fes.get(0).unwrap(), vec!["E".into(), "nu".into(), "sigma_y".into()], &[210000.0, 0.3, 250.0]).unwrap();
/// # let idx_mat: Vec<u32> = (0..materiau.point_values(0, 0).unwrap().len() as u32).collect();
/// # let opt_mat = [pyrucast::containers::field::ABSENT_COMPONENT; 8];
/// # let mat = MatParams::new(materiau.point_values(0, 0).unwrap(), &idx_mat, &opt_mat);
/// # let repos = PrevState { eps: [0.0; 6], sigma: [0.0; 6], eps_p: [0.0; 6], p: 0.0,
/// #                         vars: &[] };
/// // Below the threshold, the consistent tangent **is** the elastic tangent.
/// let sous = [100.0, 0.0, 0.0, 0.0, 0.0, 0.0];
/// let d = plasticity::von_mises::tangent(&sous, &mat, 0.0, 0.0);
/// assert_eq!(d, elastic::elastic_tangent(mat.lambda, mat.mu));
///
/// // Beyond it, it softens: the apparent modulus drops.
/// let au_dela = [400.0, 0.0, 0.0, 0.0, 0.0, 0.0];
/// let dp = plasticity::von_mises::tangent(&au_dela, &mat, 0.0, 0.0);
/// assert!(dp[0][0] < d[0][0]);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn tangent(trial: &[f64; 6], mat: &MatParams, hardening: f64, p_prev: f64) -> [[f64; 6]; 6] {
    let (lambda, mu) = (mat.lambda, mat.mu);
    let sigma_y0 = mat.get(SIGMA_Y);
    let q = von_mises_stress(trial);
    let yield_now = sigma_y0 + hardening * p_prev;
    if q <= yield_now || q == 0.0 {
        return elastic_tangent(lambda, mu);
    }
    let dp = (q - yield_now) / (3.0 * mu + hardening);
    let theta = (yield_now + hardening * dp) / q;
    let theta_bar = 3.0 * mu / (3.0 * mu + hardening) - (1.0 - theta);

    let k = lambda + 2.0 * mu / 3.0;
    let coef = 2.0 * mu * theta;
    let mut d = [[0.0_f64; 6]; 6];
    // K·1⊗1 on the normal (top-left 3×3) block.
    for row in d.iter_mut().take(3) {
        for e in row.iter_mut().take(3) {
            *e += k;
        }
    }
    // 2μθ · I_dev (engineering: normal block ⅔/−⅓, shear diagonal ½).
    for (i, row) in d.iter_mut().enumerate().take(3) {
        for (j, e) in row.iter_mut().enumerate().take(3) {
            *e += coef * if i == j { 2.0 / 3.0 } else { -1.0 / 3.0 };
        }
    }
    for i in 3..6 {
        d[i][i] += coef * 0.5;
    }
    // − 2μθ̄ · n̂⊗n̂, with `n̂` unit in the Frobenius sense (off-diagonals of the
    // deviator counted twice, as `s:s` does).
    let s = deviator(trial);
    let s_norm =
        (s[0] * s[0] + s[1] * s[1] + s[2] * s[2] + 2.0 * (s[3] * s[3] + s[4] * s[4] + s[5] * s[5]))
            .sqrt();
    if s_norm > 0.0 {
        let nv: [f64; 6] = std::array::from_fn(|i| s[i] / s_norm);
        let c = 2.0 * mu * theta_bar;
        for i in 0..6 {
            for j in 0..6 {
                d[i][j] -= c * nv[i] * nv[j];
            }
        }
    }
    d
}

/// The perfect von Mises law — no hardening.
pub(crate) struct Perfect;

impl ReturnMapLawKind for Perfect {
    fn material_components(&self) -> &'static [&'static str] {
        &["E", "nu", "sigma_y"]
    }

    fn return_map(
        &self,
        trial: &[f64; 6],
        prev: &PrevState,
        mat: &MatParams,
        _dt: f64,
    ) -> Result<PlasticStep> {
        Ok(return_map(trial, prev, mat, 0.0))
    }

    fn tangent_source(&self) -> crate::models::TangentSource {
        crate::models::TangentSource::Analytic
    }

    fn analytic_tangent(
        &self,
        trial: &[f64; 6],
        prev: &PrevState,
        mat: &MatParams,
    ) -> Option<Result<[[f64; 6]; 6]>> {
        Some(Ok(tangent(trial, mat, 0.0, prev.p)))
    }
}

/// von Mises with **linear isotropic hardening**, `σ_y(p) = σ_y + H·p`.
pub(crate) struct Isotropic;

impl ReturnMapLawKind for Isotropic {
    fn material_components(&self) -> &'static [&'static str] {
        &["E", "nu", "sigma_y", "H"]
    }

    fn return_map(
        &self,
        trial: &[f64; 6],
        prev: &PrevState,
        mat: &MatParams,
        _dt: f64,
    ) -> Result<PlasticStep> {
        Ok(return_map(trial, prev, mat, mat.get(H)))
    }

    fn tangent_source(&self) -> crate::models::TangentSource {
        crate::models::TangentSource::Analytic
    }

    fn analytic_tangent(
        &self,
        trial: &[f64; 6],
        prev: &PrevState,
        mat: &MatParams,
    ) -> Option<Result<[[f64; 6]; 6]>> {
        Some(Ok(tangent(trial, mat, mat.get(H), prev.p)))
    }
}

crate::physics_operator! {
    /// [`model::plasticity_perfect`](crate::ops::model::plasticity_perfect()) — **perfect** (non-hardening)
    /// von Mises elastoplasticity spanning every subspace of `fespace`. `kinematics`
    /// is `"plane_stress"` / `"plane_strain"` / `"axisymmetric"` (2-D) or
    /// `"full_3d"` (3-D). Same DOFs as elasticity (`u_x, u_y(, u_z)`); material
    /// (`E`, `nu`, `sigma_y`) is supplied at assembly / integration time. The
    /// behaviour integration (`COMP`) carries the plastic-strain +
    /// cumulated-`p` internal state (`VAR0`→`VAR1`) and emits the consistent
    /// tangent `D_alg`.
    ///
    /// ```
    /// # use pyrucast::ops::element_field::StrainMeasure;
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::tensor::Kinematics;
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// let m = model::plasticity_perfect(&fes, Kinematics::PlaneStrain, StrainMeasure::Linear)?;
    /// assert_eq!(m.primal_vars(), vec!["u_x".to_string(), "u_y".to_string()]);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn plasticity_perfect(fespace, kinematics: Kinematics) = crate::ops::model::plasticity, PlasticLaw::Perfect;
    python: "Perfect (non-hardening) von Mises elastoplasticity.\n\nSame unknowns as `pyrucast.model.elasticity`. Material `E`, `nu`, `sigma_y`.\nThe state carries the plastic strain and the cumulated plastic strain `p`;\nthe law gives its consistent tangent to `pyrucast.matrix.tangent`."
}

crate::physics_operator! {
    /// [`model::plasticity_isotropic`](crate::ops::model::plasticity_isotropic()) — von Mises with **linear
    /// isotropic hardening**, `σ_y(p) = σ_y + H·p`. Material `E`, `nu`,
    /// `sigma_y`, `H`; everything else as `plasticity_perfect` (`H = 0` would
    /// give it back exactly).
    ///
    /// ```
    /// # use pyrucast::ops::element_field::StrainMeasure;
    /// # use pyrucast::aggregate::Aggregate;
    /// # use pyrucast::atoms::{ElementType, Node};
    /// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
    /// # use pyrucast::containers::mesh::{Mesh, SubMesh};
    /// # use pyrucast::coords::Coords;
    /// # use pyrucast::handle::Handle;
    /// # use pyrucast::models::tensor::Kinematics;
    /// # use pyrucast::ops::model;
    /// # let coords = Handle::new(Coords::new(2).unwrap());
    /// # let n: Vec<Node> = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]
    /// #     .iter().map(|p| Node::create_in(coords.clone(), p).unwrap()).collect();
    /// # let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
    /// # sm.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
    /// # let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
    /// let m = model::plasticity_isotropic(&fes, Kinematics::PlaneStrain, StrainMeasure::Linear)?;
    /// assert_eq!(m.primal_vars(), vec!["u_x".to_string(), "u_y".to_string()]);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn plasticity_isotropic(fespace, kinematics: Kinematics) = crate::ops::model::plasticity, PlasticLaw::Isotropic;
    python: "Von Mises plasticity with linear isotropic hardening, `σ_y(p) = σ_y + H·p`.\n\nMaterial `E`, `nu`, `sigma_y`, `H`; everything else as\n`pyrucast.model.plasticity_perfect`, which `H = 0` gives back exactly."
}