pyrucast 0.8.0

Finite-element library in Rust, inspired by cast3m, exposed to Python
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! Drucker-Prager plasticity — pressure-sensitive, with non-associated flow,
//! hardening, and a limiting surface.
//!
//! Soils, rocks, concrete and powders are **stronger in compression than in
//! tension**: their yielding depends on the hydrostatic pressure, which von
//! Mises ignores entirely. Drucker-Prager is the simplest surface that captures
//! it — a cone about the hydrostatic axis:
//!
//! ```text
//! f(σ) = α·I₁ + β·q − k          q = √(3 J₂),  I₁ = tr σ
//! ```
//!
//! `α` is the friction coefficient — the cone's slope, `α = 0` giving back von
//! Mises — `β` weights the deviatoric part, and `k` the cohesion. `α` travels
//! through the material field as **`friction`**: `alpha` is the thermal
//! expansion everywhere else in the crate, and a single component name cannot
//! carry two meanings.
//!
//! ## Why the flow is non-associated
//!
//! Associated flow on this cone (`g = f`) would make the material dilate under
//! shear by exactly the amount its friction implies — which for real granular
//! media is far too much. So the plastic potential carries its **own** slope,
//! the dilatancy `ψ`, and its own deviatoric weight `δ`:
//!
//! ```text
//! g(σ) = ψ·I₁ + δ·q          ψ ≤ α
//! ```
//!
//! `ψ = α, δ = β` recovers associated flow; `ψ = 0` gives isochoric plastic flow
//! with frictional strength. The price is a **non-symmetric** consistent tangent
//! — which the assembler already supports, `MatrixLayout` carrying a `symmetric`
//! flag.
//!
//! ## Hardening, and the surface it stops at
//!
//! The cohesion grows with the accumulated plastic strain, `dk = H·dp`, `H`
//! algebraic so that a negative one softens. Left alone that would grow without
//! bound, so a second **ultimate** surface bounds it:
//!
//! ```text
//! initial   α·I₁ + β·q = k
//! ultimate  α_u·I₁ + β_u·q = k_u
//! ```
//!
//! The two are read as the ends of one interpolation, driven by the hardening
//! itself:
//!
//! ```text
//! λ = clamp( H·p / (k_u − k), 0, 1 )
//! α(p) = (1−λ)α + λα_u        β(p) = (1−λ)β + λβ_u        k(p) = (1−λ)k + λk_u
//! ```
//!
//! which is worth stating plainly because it is an **interpretation**: Cast3M's
//! `PLASTIQUE DRUCKER_PRAGER` gives the two surfaces and `dK = H·dp` without
//! saying how they meet. Reading them as one interpolating surface has two
//! merits — `k(p) = k + H·p` exactly while the limit is not reached, so the
//! stated hardening law is reproduced verbatim; and the yield surface stays
//! **single**, so the return map keeps one cone and one apex instead of growing
//! a corner between two.
//!
//! ## What the parameters reduce to
//!
//! Six of the nine are optional, and their defaults are what makes the simple
//! cone the zero-configuration case:
//!
//! | Cast3M | here | défaut | ce qu'il fait |
//! |---|---|---|---|
//! | `ALFA` | `friction` | requis | pente du cône |
//! | `K` | `k` | requis | cohésion |
//! | `GAMM` | `psi` | requis | dilatance du potentiel |
//! | `BETA` | `beta` | 1 | poids déviatorique du critère |
//! | `DELT` | `delta` | 1 | poids déviatorique du potentiel |
//! | `H` | `H` | 0 | module d'écrouissage |
//! | `ETA` | `friction_ult` | `friction` | pente de la surface ultime |
//! | `MU` | `beta_ult` | `beta` | poids déviatorique ultime |
//! | `KL` | `k_ult` | `k` | cohésion ultime |
//!
//! With every default taken, `k_u = k` leaves no room to harden, the
//! interpolation is frozen at the initial surface, and what remains is the
//! perfectly plastic cone. Cast3M's **`DRUCKER_PARFAIT`** is then one more step:
//! `psi = friction` (and `delta = beta`), which makes the flow associated.
//!
//! ## The apex
//!
//! A cone has a tip, at `I₁ = k/α`, and a trial stress beyond it returns to that
//! point rather than to the cone's flank — the smooth return would otherwise
//! overshoot into a stress state with a negative equivalent stress, which has no
//! meaning. Detecting it is the one branch this law needs, and it is exactly the
//! case a naive implementation gets wrong under strong tension.

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

/// Positions in this law's required contract,
/// `["E", "nu", "friction", "k", "psi"]`.
const FRICTION: usize = 2;
const COHESION: usize = 3;
const DILATANCY: usize = 4;
/// Positions in its **optional** contract, `["alpha", "rho", "beta", "delta",
/// "H", "friction_ult", "beta_ult", "k_ult"]` — the six that turn the simple
/// cone into the general surface, plus the two an ancillary operator reads.
const OPT_BETA: usize = 2;
const OPT_DELTA: usize = 3;
const OPT_H: usize = 4;
const OPT_FRICTION_ULT: usize = 5;
const OPT_BETA_ULT: usize = 6;
const OPT_K_ULT: usize = 7;

/// The nine parameters of the general surface.
#[derive(Clone, Copy)]
struct Params {
    /// Initial surface `α·I₁ + β·q = k`.
    friction: f64,
    beta: f64,
    k: f64,
    /// Ultimate surface, the interpolation's far end.
    friction_ult: f64,
    beta_ult: f64,
    k_ult: f64,
    /// Hardening modulus, algebraic.
    hardening: f64,
    /// Flow potential `ψ·I₁ + δ·q`.
    psi: f64,
    delta: f64,
}

/// Read the nine and reject a non-physical set.
fn params(mat: &MatParams) -> Result<Params> {
    let friction = mat.get(FRICTION);
    let k = require_positive(PlasticLaw::DruckerPrager, "k", mat.get(COHESION))?;
    let psi = mat.get(DILATANCY);
    let beta = require_positive(
        PlasticLaw::DruckerPrager,
        "beta",
        mat.optional(OPT_BETA, 1.0),
    )?;
    let beta_ult = require_positive(
        PlasticLaw::DruckerPrager,
        "beta_ult",
        mat.optional(OPT_BETA_ULT, beta),
    )?;
    Ok(Params {
        friction,
        beta,
        k,
        friction_ult: mat.optional(OPT_FRICTION_ULT, friction),
        beta_ult,
        k_ult: require_positive(
            PlasticLaw::DruckerPrager,
            "k_ult",
            mat.optional(OPT_K_ULT, k),
        )?,
        hardening: mat.optional(OPT_H, 0.0),
        psi,
        delta: require_positive(
            PlasticLaw::DruckerPrager,
            "delta",
            mat.optional(OPT_DELTA, 1.0),
        )?,
    })
}

impl Params {
    /// Whether the surface can move at all — the fast path when it cannot.
    fn hardens(&self) -> bool {
        self.hardening != 0.0 && (self.k_ult - self.k).abs() > f64::EPSILON
    }

    /// The interpolation's progress at an accumulated plastic strain `p`.
    fn lambda(&self, p: f64) -> f64 {
        if !self.hardens() {
            return 0.0;
        }
        (self.hardening * p / (self.k_ult - self.k)).clamp(0.0, 1.0)
    }

    /// The surface `(α, β, k)` currently active.
    fn surface(&self, p: f64) -> (f64, f64, f64) {
        let l = self.lambda(p);
        (
            (1.0 - l) * self.friction + l * self.friction_ult,
            (1.0 - l) * self.beta + l * self.beta_ult,
            (1.0 - l) * self.k + l * self.k_ult,
        )
    }

    /// The yield function at a trial state reduced by a multiplier `dl`.
    ///
    /// `q` and `I₁` follow the flow, `p` accumulates the multiplier, and the
    /// surface is re-read at the new `p` — which is what makes the consistency
    /// condition non-linear as soon as the material hardens.
    fn residual(&self, dl: f64, q_tr: f64, i1_tr: f64, p_prev: f64, mu: f64, bulk: f64) -> f64 {
        let (a, b, c) = self.surface(p_prev + dl);
        a * (i1_tr - 9.0 * bulk * self.psi * dl) + b * (q_tr - 3.0 * mu * self.delta * dl) - c
    }
}

/// Return onto the Drucker-Prager cone, with the apex case handled separately.
///
/// ```
/// # 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::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(), "friction".into(), "k".into(), "psi".into()], &[210000.0, 0.3, 0.5, 100.0, 0.2]).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: &[] };
/// // Sensible à la pression : une traction hydrostatique plastifie, là où
/// // von Mises la laisserait passer indéfiniment. Le retour se fait sur
/// // l'**apex** du cône, à I₁ = k/α, quelle que soit l'intensité.
/// for s in [200.0, 1000.0] {
///     let pas = plasticity::drucker_prager::return_map(
///         &[s, s, s, 0.0, 0.0, 0.0], &repos, &mat)?;
///     assert!((tensor::i1(&pas.sigma) - 200.0).abs() < 1e-9);
///     // L'apex ne produit aucun écoulement **déviatorique** : `p`, qui
///     // cumule celui-ci, reste nul, tandis que ε_p gonfle en volume.
///     assert_eq!(pas.p, 0.0);
///     assert!(tensor::i1(&pas.eps_p) > 0.0);
/// }
///
/// // Hors de l'apex, `p` croît, et l'écoulement est **non associé** : la
/// // dilatance `psi` diffère du frottement, donc ε_p n'est pas isochore.
/// let pas = plasticity::drucker_prager::return_map(
///     &[400.0, 0.0, 0.0, 0.0, 0.0, 0.0], &repos, &mat)?;
/// assert!(pas.p > 0.0);
/// assert!(tensor::i1(&pas.eps_p) > 0.0);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn return_map(trial: &[f64; 6], prev: &PrevState, mat: &MatParams) -> Result<PlasticStep> {
    let p = params(mat)?;
    let (mu, bulk) = (mat.mu, mat.bulk());
    let q_tr = von_mises_stress(trial);
    let i1_tr = i1(trial);

    let (a0, b0, c0) = p.surface(prev.p);
    if a0 * i1_tr + b0 * q_tr - c0 <= 0.0 {
        return Ok(PlasticStep::elastic(trial, prev)); // elastic
    }

    let dlambda = solve_multiplier(&p, q_tr, i1_tr, prev.p, mu, bulk);
    let q_new = q_tr - 3.0 * mu * p.delta * dlambda;

    if q_new >= 0.0 && q_tr > 0.0 {
        let s_tr = deviator(trial);
        let mean_new = (i1_tr - 9.0 * bulk * p.psi * dlambda) / 3.0;
        let scale = q_new / q_tr;
        let mut sigma = [0.0; 6];
        let mut eps_p = prev.eps_p;
        for i in 0..6 {
            let s_new = s_tr[i] * scale;
            sigma[i] = if i < 3 { s_new + mean_new } else { s_new };
            // Δε_p = Δλ·(∂g/∂σ): deviatoric part δ·(3Δλ/2q)·s, plus the dilatant
            // volumetric part ψΔλ on the diagonal.
            eps_p[i] += 1.5 * p.delta * dlambda / q_tr * s_tr[i];
            if i < 3 {
                eps_p[i] += p.psi * dlambda;
            }
        }
        return Ok(PlasticStep::new(sigma, eps_p, prev.p + dlambda, &[]));
    }

    // Apex return: the flank solution would push the equivalent stress negative,
    // which is meaningless. The whole deviator is shed and the stress collapses
    // onto the tip.
    apex_return(trial, prev, mat, &p)
}

/// The plastic multiplier, from the consistency condition.
///
/// Without hardening the surface is fixed and the condition is **linear** —
/// `∂g/∂σ` maps through the elastic operator to `3μδ` on the deviator and
/// `9Kψ` on the trace — so the multiplier is a quotient and no iteration
/// happens. That is the path every non-hardening kinematics takes, unchanged.
///
/// With hardening the surface moves as the multiplier grows, and the condition
/// becomes non-linear in it. A safeguarded Newton solves it: the linear solution
/// is both the first iterate and an upper bracket, since a surface that hardens
/// (`H > 0`) can only need *less* plastic flow than a frozen one, and a
/// softening surface is caught by the bisection fallback.
fn solve_multiplier(p: &Params, q_tr: f64, i1_tr: f64, p_prev: f64, mu: f64, bulk: f64) -> f64 {
    let (a, b, c) = p.surface(p_prev);
    let slope = 3.0 * mu * b * p.delta + 9.0 * bulk * a * p.psi;
    let linear = if slope.abs() > f64::EPSILON {
        (a * i1_tr + b * q_tr - c) / slope
    } else {
        0.0
    };
    if !p.hardens() {
        return linear;
    }

    // Bracket: the residual is positive at 0 (we only got here after yielding)
    // and decreasing in `dl` for any physical set; grow the upper bound until it
    // turns, so bisection always has a sign change to work with.
    let mut hi = linear.max(1e-12);
    for _ in 0..60 {
        if p.residual(hi, q_tr, i1_tr, p_prev, mu, bulk) <= 0.0 {
            break;
        }
        hi *= 2.0;
    }
    let (mut lo, mut dl) = (0.0, hi.min(linear.max(0.0)));
    for _ in 0..50 {
        let r = p.residual(dl, q_tr, i1_tr, p_prev, mu, bulk);
        if r.abs() < 1e-12 * c.abs().max(1.0) {
            return dl;
        }
        if r > 0.0 {
            lo = dl;
        } else {
            hi = dl;
        }
        // Newton on a numerical slope, bisected whenever it leaves the bracket.
        let h = (hi - lo).max(1e-14) * 1e-6;
        let d = (p.residual(dl + h, q_tr, i1_tr, p_prev, mu, bulk) - r) / h;
        let next = if d.abs() > f64::EPSILON {
            dl - r / d
        } else {
            dl
        };
        dl = if next > lo && next < hi {
            next
        } else {
            0.5 * (lo + hi)
        };
    }
    dl
}

/// Collapse onto the cone's tip: `s = 0`, `α·I₁ = k`.
///
/// With `α = 0` the cone is a cylinder and has no apex, so this cannot be
/// reached — the flank return always succeeds there (it *is* von Mises).
///
/// The surface is read at the plastic strain the shed deviator implies, so a
/// hardening material's tip sits where its hardening has taken it.
fn apex_return(
    trial: &[f64; 6],
    prev: &PrevState,
    mat: &MatParams,
    p: &Params,
) -> Result<PlasticStep> {
    // The equivalent plastic strain shed in this step, in the same measure the
    // flank return uses (the multiplier of the deviatoric flow).
    let dp = von_mises_stress(trial) / (3.0 * mat.mu * p.delta);
    let (a, _, c) = p.surface(prev.p + dp);
    let mean_new = if a.abs() > f64::EPSILON {
        c / a / 3.0
    } else {
        i1(trial) / 3.0
    };
    let s_tr = deviator(trial);
    let mut sigma = [0.0; 6];
    let mut eps_p = prev.eps_p;
    // Everything the elastic predictor built beyond the apex becomes plastic.
    let vol_drop = (i1(trial) - 3.0 * mean_new) / (9.0 * mat.bulk());
    for i in 0..6 {
        sigma[i] = if i < 3 { mean_new } else { 0.0 };
        eps_p[i] += s_tr[i] / (2.0 * mat.mu);
        if i < 3 {
            eps_p[i] += vol_drop;
        }
    }
    Ok(PlasticStep::new(sigma, eps_p, prev.p + dp, &[]))
}

// ─── On the tangent ─────────────────────────────────────────────────────────
//
// Drucker-Prager takes its consistent tangent by finite differences, from
// `ReturnMapLawKind::consistent_tangent`.
//
// That is a deliberate reversal of an earlier hand derivation. Non-associated
// flow makes `∂σ/∂ε` pick up an `m⊗n` term with `m ≠ n`, and getting its
// engineering-Voigt weighting right is fiddly: the derivation that *looked*
// correct was 24 % off, and only the finite-difference oracle in
// `tests/plastic_laws.rs` said so. A numerical tangent cannot be mis-derived,
// costs twelve evaluations of the update, and leaves Newton's quadratic
// convergence intact. Hardening only strengthens the argument — the return is no
// longer even closed-form.
//
// The **apex** is the one case the numerical route still needs told about: the
// return pins the stress there, so `∂σ/∂ε` genuinely vanishes and the finite
// difference finds zero of its own accord. A body entirely at its apex therefore
// assembles a singular tangent — not an artefact, but the honest report that
// such a material carries no further load.

/// Drucker-Prager: pressure-sensitive, non-associated flow.
pub(crate) struct DruckerPrager;

impl ReturnMapLawKind for DruckerPrager {
    /// The general surface is nine numbers, six of which default to the simple
    /// cone: they ride the optional channel so a three-parameter kinematics stays
    /// writable in three numbers.
    fn optional_material_components(&self) -> &'static [&'static str] {
        &[
            "alpha",
            "rho",
            "beta",
            "delta",
            "H",
            "friction_ult",
            "beta_ult",
            "k_ult",
        ]
    }

    fn material_components(&self) -> &'static [&'static str] {
        &["E", "nu", "friction", "k", "psi"]
    }

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

crate::physics_operator! {
    /// [`model::drucker_prager`](crate::ops::model::drucker_prager()) — pressure-sensitive plasticity
    /// with **non-associated** flow: `f = q + α·I₁ − k`, plastic potential
    /// `g = q + ψ·I₁`. Material `E`, `nu`, `alpha` (friction), `k` (cohesion),
    /// `psi` (dilatancy).
    ///
    /// `ψ = α` recovers associated flow; `ψ < α` is the usual choice for soils
    /// and rocks, whose measured dilatancy is far below what friction alone
    /// would imply. A non-associated law has a **non-symmetric** tangent.
    /// Returns beyond the cone's apex (`I₁ = k/α`) collapse onto the tip.
    ///
    /// ```
    /// # 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::drucker_prager(&fes, Kinematics::PlaneStrain)?;
    /// assert_eq!(m.primal_vars(), vec!["u_x".to_string(), "u_y".to_string()]);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn drucker_prager(fes, kinematics: Kinematics) = crate::ops::model::plasticity_with_law, PlasticLaw::DruckerPrager;
    python: "`model.drucker_prager(fespace, kinematics)` — pressure-sensitive plasticity\nwith **non-associated** flow: `f = q + α·I₁ − k`, plastic potential\n`g = q + ψ·I₁`. Material `E`, `nu`, `alpha` (friction), `k` (cohesion),\n`psi` (dilatancy).\n\n`ψ = α` recovers associated flow; `ψ < α` is the usual choice for soils\nand rocks, whose measured dilatancy is far below what friction alone\nwould imply. A non-associated law has a **non-symmetric** tangent.\nReturns beyond the cone's apex (`I₁ = k/α`) collapse onto the tip."
}