pyrucast 0.10.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
//! Internal nodal forces `f = ∫ Bᵀ σ dΩ` of a `Model` (Cast3m `BSIG`) — the
//! transpose of the deformation operator `B`, applied to the stress.
//!
//! This is the mechanical generalisation of
//! [`crate::ops::node_field::divergence`](fn@crate::ops::node_field::divergence): where
//! `divergence` scatters `Bᵀ q` of a scalar transport flux to the nodes,
//! `internal_forces` does the same for every behaviour-bearing sub-model, one
//! output component per dual DOF. It is the exact transpose of the geometric
//! deformation producer each physics uses
//! ([`crate::ops::element_field::deformation`](fn@crate::ops::element_field::deformation),
//! [`crate::ops::element_field::beam_deformation`](fn@crate::ops::element_field::beam_deformation)),
//! so continuum solids, bars and beams are all handled — each physics supplies
//! its own `Bᵀ` kernel ([`crate::models::SubModelKind::internal_force_element`]), and
//! this layer only orchestrates the per-zone pairing and aggregation.
//!
//! The state read here is the material-state aggregate produced by
//! [`crate::ops::element_field::behavior::integrate`] (`COMP`). For a **linear** law the result
//! equals `K·u` (the assembled stiffness applied to the solution); a non-linear
//! law departs from that tangent — which is the point of forming the residual
//! from the exact stresses.
//!
//! This is the **internal** half of the balance `Σ f_int = Σ f_ext`; its
//! counterpart is [`crate::ops::node_field::external_forces`](fn@crate::ops::node_field::external_forces),
//! and the gap between the two sums is the residual. Every sub-model is asked
//! for its term — a physics with none on this side simply declares none.

use crate::aggregate::Aggregate;
use crate::atoms::{ElementType, NodeId};
use crate::containers::element_field::ElementField;
use crate::containers::field::SubField;
use crate::containers::mesh::SubMesh;
use crate::containers::model::Model;
use crate::containers::node_field::NodeField;
use crate::error::{PyrucastError, Result};
use crate::handle::Handle;
use crate::models::kernel;
use crate::models::ResidualContribution;

/// Internal nodal forces of `model` — the left side of `Σ f_int = Σ f_ext`
/// (Cast3m `BSIG` for a continuum).
///
/// The nodal mirror of [`crate::ops::matrix::stiffness`](fn@crate::ops::matrix::stiffness):
/// where that one asks every sub-model for its blocks of `∂r/∂u`, this asks
/// every sub-model for its term of `r` on the internal side, through
/// [`SubModelKind::internal_force_contribution`](crate::models::SubModelKind::internal_force_contribution).
/// A behaviour-bearing domain
/// pairs its state sub-field by FE subspace and integrates its `Bᵀ` kernel
/// cell-by-cell; a sub-model with no term here declares none and is absent from
/// the result. Returns a [`NodeField`] with one zone per contributing
/// sub-model, in model order, its components that sub-model's dual variables.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{ElementType, Node};
/// # use pyrucast::containers::element_field::ElementField;
/// # use pyrucast::containers::field::SubField;
/// # use pyrucast::containers::finite_element_space::FiniteElementSpace;
/// # use pyrucast::containers::mesh::{Mesh, SubMesh};
/// # use pyrucast::containers::model::Model;
/// # use pyrucast::containers::node_field::NodeField;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::tensor::Kinematics;
/// # use pyrucast::ops::{element_field, geom, mesh, node_field};
/// # use pyrucast::ops::model;
/// # let coords = Handle::new(Coords::new(2).unwrap());
/// # let n: Vec<Node> = [[0.0, 0.0], [2.0, 0.0], [0.0, 2.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 maillage = Mesh::from_submesh(sm);
/// # let fes = FiniteElementSpace::lagrange1(&maillage).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let support = mesh::poi1_from_nodes(&n).unwrap();
/// // The form that goes through the **model**: every sub-model brings its own
/// // operator there, a bar not having a continuum's Bᵀ.
/// # let modele = model::elasticity(&fes, Kinematics::PlaneStress)?;
/// # let mut s = ElementField::new(&fes,
/// #     vec!["sigma_xx".into(), "sigma_yy".into(), "sigma_xy".into()])?;
/// # s.get(0)?.write().set_uniform("sigma_xx", 100.0)?;
/// # let mat = element_field::material_field(&modele, &[("E", 1.0), ("nu", 0.3)])?;
/// # let u = NodeField::empty();
/// let f = node_field::internal_forces(&modele, &s, &u, &mat)?;
/// assert_eq!(f.get(0)?.read().components(),
///            &["f_x".to_string(), "f_y".to_string()]);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn internal_forces(
    model: &Model,
    state: &ElementField,
    solution: &NodeField,
    materials: &ElementField,
) -> Result<NodeField> {
    let mut out = NodeField::empty();
    for sub_h in model {
        // Build the zone(s) under a read guard, then drop it before `add_sub`,
        // which takes the aggregate's write lock.
        let built = {
            let sub = sub_h.read();
            let kind = sub.as_kind();
            let mut zones = Vec::new();
            for c in kind.internal_force_contribution() {
                match c {
                    ResidualContribution::Literal(field) => {
                        zones.push(field);
                    }
                    ResidualContribution::Relations => {
                        zones.push(reaction(kind, solution)?);
                    }
                    ResidualContribution::Computed(layout) => {
                        // The operator resolves what the term reads, exactly as
                        // `assemble_kind` resolves the material before asking
                        // for a block. The sub-model resolved nothing, which is
                        // why its declaration could not fail.
                        let domain = kind.as_domain().ok_or_else(|| {
                            PyrucastError::Message(format!(
                                "{}: declares a computed internal force but no material \
                                 subspace to integrate it on",
                                kind.label()
                            ))
                        })?;
                        let material = materials.sub_for_fespace_with(
                            &domain.material_fespace(),
                            &domain.material_components(),
                        )?;
                        // What the term applies `Bᵀ` to: the law's output when
                        // there is a law, the primal itself at the Gauss points
                        // when there is none. A boundary transfer is of the
                        // second kind — its `∫ h·a·N` reads `a`, not a stress.
                        let per_point = match kind.as_behavior() {
                            Some(b) => state.sub_for_fespace(&b.behavior_fespace())?,
                            None => kind.residual_input(&layout.fespaces[0], solution)?,
                        };
                        let guard = per_point.read();
                        let mat_guard = material.read();
                        // Resolved once for the zone, before the parallel region.
                        let names = kind.internal_force_reads();
                        let refs: Vec<&str> = names.iter().map(String::as_str).collect();
                        let lay = guard.resolve_components(&refs, "state")?;
                        let mat_names = domain.material_components();
                        let mat_refs: Vec<&str> = mat_names.iter().map(String::as_str).collect();
                        let mat = mat_guard.resolve_components(&mat_refs, "material")?;
                        let zone = kernel::scatter_to_nodes(
                            &layout.fespaces,
                            &layout.support,
                            layout.dual_vars,
                            |geoms, fe| {
                                kind.internal_force_element(
                                    geoms, &guard, &lay, &mat_guard, &mat, fe,
                                )
                            },
                        )?;
                        let mut one = NodeField::empty();
                        one.add_sub(Handle::new(zone))?;
                        zones.push(one);
                    }
                }
            }
            zones
        };
        for zone in built {
            // `r = Σ rᵢ` is a **sum**, not a stack: two terms may load the same node in
            // the same component, and an aggregate view would pick one instead of adding
            // them.
            out = (&out + &zone)?;
        }
    }
    Ok(out)
}

/// A constraint's reaction, `Cᵀ λ`, spread over the constrained nodes: term `k`
/// of a relation carries `aₖ · λ` into the dual row of its own variable.
///
/// This is the thing the multiplier alone does not give. For a Dirichlet with a
/// single term of coefficient 1, λ *is* the nodal reaction — which is why
/// nothing ever seemed to be missing. For a relation with several terms, λ is
/// **one scalar** and the reaction is `aₖ·λ`, node by node.
fn reaction(kind: &dyn crate::models::SubModelKind, solution: &NodeField) -> Result<NodeField> {
    let constraint = kind.as_constraint().ok_or_else(|| {
        PyrucastError::Message(format!(
            "{}: declares a reaction but imposes no relation",
            kind.label()
        ))
    })?;
    let view = solution.view()?;
    let relations = constraint.relations()?;
    let primals = kind.primal_vars();
    let duals = kind.dual_vars();
    let mut values: std::collections::HashMap<(NodeId, String), f64> =
        std::collections::HashMap::new();
    let mut nodes: Vec<NodeId> = Vec::new();
    for rel in &relations {
        // The multiplier is an unknown of the augmented system, so it is read
        // where every other unknown is: in the solution.
        // The multiplier's own name: a constraint's primal, at the index of
        // the dual this relation writes into.
        let slot = duals
            .iter()
            .position(|d| *d == rel.imposed_value)
            .ok_or_else(|| {
                PyrucastError::Message(format!(
                    "{}: relation writes `{}`, which is not one of its duals",
                    kind.label(),
                    rel.imposed_value
                ))
            })?;
        let lambda = view
            .value_opt(rel.multiplier_node, &primals[slot])
            .unwrap_or(0.0);
        // `C·u` on the constraint's own row: its half of `C·u = g`, whose
        // other half is the imposed value, on the external side.
        let mut cu = 0.0;
        for term in &rel.terms {
            let slot = (term.node, term.target_dual.clone());
            if !values.contains_key(&slot) {
                nodes.push(term.node);
            }
            *values.entry(slot).or_insert(0.0) += term.coefficient * lambda;
            cu += term.coefficient * view.value_opt(term.node, &term.variable).unwrap_or(0.0);
        }
        let row = (rel.multiplier_node, rel.imposed_value.clone());
        if !values.contains_key(&row) {
            nodes.push(rel.multiplier_node);
        }
        *values.entry(row).or_insert(0.0) += cu;
    }
    nodes.sort_unstable();
    nodes.dedup();
    // The coords come from the constraint's own mesh, not from the solution: a
    // residual asked before anything is solved has an empty solution, and the
    // reaction is then legitimately zero rather than an error.
    let coords = constraint.multiplier_mesh().coords()?;
    let mut support = SubMesh::new(coords, ElementType::POI1);
    for id in &nodes {
        support.add_cell(&[*id])?;
    }
    let support = Handle::new(support);
    let components: Vec<String> = {
        let mut c: Vec<String> = values.keys().map(|(_, d)| d.clone()).collect();
        c.sort_unstable();
        c.dedup();
        c
    };
    let out = NodeField::from_submesh(&support, components)?;
    {
        let mut zone = out.get(0)?.write();
        for ((node, dual), v) in &values {
            zone.set_value(*node, dual, *v)?;
        }
    }
    Ok(out)
}

// ─── Unit tests ────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::atoms::{ElementType, Node};
    use crate::containers::finite_element_space::FiniteElementSpace;
    use crate::containers::mesh::{Mesh, SubMesh};
    use crate::containers::model::SubModel;
    use crate::containers::node_field::{NodeField, SubNodeField};
    use crate::coords::Coords;
    use crate::models::tensor::Kinematics;
    use crate::ops::element_field::behavior::integrate;
    use crate::ops::element_field::deformation;
    use crate::ops::element_field::material_field;

    /// Elasticity on a single TRI3: for a linear law the internal forces equal
    /// `K·u` applied to the (linear) displacement solution.
    #[test]
    fn elasticity_internal_forces_match_k_times_u() {
        let coords = Handle::new(Coords::new(2).unwrap());
        let a = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
        let b = Node::create_in(coords.clone(), &[1.0, 0.0]).unwrap();
        let c = Node::create_in(coords.clone(), &[0.0, 1.0]).unwrap();
        let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::TRI3));
        mesh.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
        let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();

        let mut model = Model::empty();
        model
            .add_sub(Handle::new(
                SubModel::elasticity(fes.get(0).unwrap(), Kinematics::PlaneStress).unwrap(),
            ))
            .unwrap();
        let materials = material_field(&model, &[("E", 210.0), ("nu", 0.3)]).unwrap();

        // Arbitrary linear displacement field.
        let support =
            Handle::new(SubMesh::poi1_from_nodes(&[a.clone(), b.clone(), c.clone()]).unwrap());
        let mut u = SubNodeField::from_poi1(&support, vec!["u_x".into(), "u_y".into()]).unwrap();
        for (n, x, y) in [(&a, 0.0, 0.0), (&b, 1.0, 0.0), (&c, 0.0, 1.0)] {
            u.set_value(n.id(), "u_x", 0.02 * x - 0.01 * y).unwrap();
            u.set_value(n.id(), "u_y", 0.005 * x + 0.03 * y).unwrap();
        }
        let u = NodeField::from_sub(u);

        // f_int = ∫ Bᵀ σ  vs  K·u.
        let strain = deformation(&u, &fes).unwrap();
        let stress = integrate(&model, &strain, None, &materials, None).unwrap();
        let f_int = internal_forces(&model, &stress, &u, &materials).unwrap();

        let k = crate::ops::matrix::stiffness(&model, &materials).unwrap();
        let ku = (&k * &u).unwrap();

        let fv = f_int.view().unwrap();
        let kv = ku.view().unwrap();
        let tol = 1e-9;
        for nid in [a.id(), b.id(), c.id()] {
            for comp in ["f_x", "f_y"] {
                let got = fv.value(nid, comp).unwrap();
                let want = kv.value(nid, comp).unwrap();
                assert!((got - want).abs() < tol, "{comp}@{nid:?}: {got} ≠ {want}");
            }
        }
    }

    /// A solid's internal forces **are** the divergence of its stress tensor
    /// contraintes : l'opérateur de modèle et l'opérateur purement géométrique
    /// give the same numbers, node by node. That is what justifies only one of the
    /// two knowing the mechanics.
    #[test]
    fn internal_forces_are_the_divergence_of_the_stress() {
        let coords = Handle::new(Coords::new(2).unwrap());
        let a = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
        let b = Node::create_in(coords.clone(), &[1.0, 0.0]).unwrap();
        let c = Node::create_in(coords.clone(), &[0.0, 1.0]).unwrap();
        let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::TRI3));
        mesh.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
        let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();

        let mut model = Model::empty();
        model
            .add_sub(Handle::new(
                SubModel::elasticity(fes.get(0).unwrap(), Kinematics::PlaneStrain).unwrap(),
            ))
            .unwrap();
        let materials = material_field(&model, &[("E", 70.0), ("nu", 0.25)]).unwrap();

        let support =
            Handle::new(SubMesh::poi1_from_nodes(&[a.clone(), b.clone(), c.clone()]).unwrap());
        let mut u = SubNodeField::from_poi1(&support, vec!["u_x".into(), "u_y".into()]).unwrap();
        for (n, x, y) in [(&a, 0.0, 0.0), (&b, 1.0, 0.0), (&c, 0.0, 1.0)] {
            u.set_value(n.id(), "u_x", 0.03 * x + 0.01 * y).unwrap();
            u.set_value(n.id(), "u_y", -0.02 * x + 0.04 * y).unwrap();
        }
        let u = NodeField::from_sub(u);

        let strain = deformation(&u, &fes).unwrap();
        let stress = integrate(&model, &strain, None, &materials, None).unwrap();

        let via_model = internal_forces(&model, &stress, &u, &materials).unwrap();
        let via_divergence = crate::ops::node_field::divergence(&stress, "sigma").unwrap();

        let m = via_model.view().unwrap();
        let d = via_divergence.view().unwrap();
        let tol = 1e-12;
        for nid in [a.id(), b.id(), c.id()] {
            for (force, div) in [("f_x", "div_sigma_x"), ("f_y", "div_sigma_y")] {
                assert!((m.value(nid, force).unwrap() - d.value(nid, div).unwrap()).abs() < tol);
            }
        }
    }

    /// Bar (truss): a pure axial stretch gives the equilibrating end forces
    /// `f_A = −N c`, `f_B = +N c` with `N = E·A·ε` along the bar axis.
    #[test]
    fn truss_internal_forces_are_equilibrating_end_forces() {
        let (e, area, dx, dy) = (100.0, 3.0, 3.0, 4.0);
        let coords = Handle::new(Coords::new(2).unwrap());
        let a = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
        let b = Node::create_in(coords.clone(), &[dx, dy]).unwrap();
        let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::SEG2));
        mesh.add_cell(&[a.id(), b.id()]).unwrap();
        let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();

        let mut model = Model::empty();
        model
            .add_sub(Handle::new(SubModel::truss(fes.get(0).unwrap()).unwrap()))
            .unwrap();
        let materials = material_field(&model, &[("E", e), ("A", area)]).unwrap();

        let len = (dx * dx + dy * dy).sqrt();
        let c = [dx / len, dy / len];
        // u = ε·L along the axis at B, zero at A ⇒ axial strain ε.
        let eps = 0.01;
        let support = Handle::new(SubMesh::poi1_from_nodes(&[a.clone(), b.clone()]).unwrap());
        let mut u = SubNodeField::from_poi1(&support, vec!["u_x".into(), "u_y".into()]).unwrap();
        u.set_value(a.id(), "u_x", 0.0).unwrap();
        u.set_value(a.id(), "u_y", 0.0).unwrap();
        u.set_value(b.id(), "u_x", eps * len * c[0]).unwrap();
        u.set_value(b.id(), "u_y", eps * len * c[1]).unwrap();
        let u = NodeField::from_sub(u);

        let strain = deformation(&u, &fes).unwrap();
        let stress = integrate(&model, &strain, None, &materials, None).unwrap();
        let f_int = internal_forces(&model, &stress, &u, &materials).unwrap();

        let n = e * area * eps; // axial force
        let fv = f_int.view().unwrap();
        let tol = 1e-9;
        assert!((fv.value(a.id(), "f_x").unwrap() + n * c[0]).abs() < tol);
        assert!((fv.value(a.id(), "f_y").unwrap() + n * c[1]).abs() < tol);
        assert!((fv.value(b.id(), "f_x").unwrap() - n * c[0]).abs() < tol);
        assert!((fv.value(b.id(), "f_y").unwrap() - n * c[1]).abs() < tol);
    }
}