pyrucast 0.7.1

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
//! Dirichlet constraint via Lagrange multipliers.
//!
//! A Dirichlet sub-model imposes `u = u_d` on a set of constrained nodes. It
//! is a *constraint*, not a volumetric physics: it carries no material and no
//! constitutive law. It is built from **two meshes supplied by the user** — it
//! creates no node and never mutates the
//! [`Coords`](crate::coords::Coords):
//!
//! - `imposed_mesh`  — POI1 (for now): the constrained nodes (shared with the
//!   target physics), one node per cell;
//! - `multiplier_mesh` — POI1: the support of the Lagrange multipliers, paired
//!   element-for-element with `imposed_mesh` (same submesh structure, same
//!   per-submesh cell count). Typically built from `imposed_mesh` with the
//!   generic [`barycenter`](crate::ops::mesh::barycenter()) mesher (colocated fresh nodes),
//!   but the user is free to colocate, offset, or even reuse the constrained
//!   nodes themselves.
//!
//! # Variables
//!
//! A Dirichlet sub-model imposes the **primal of another model** yet owns its
//! own pair of variables; the names keep the two apart:
//!
//! - `imposed_variable` (e.g. `"T"`) — the constrained primal of the **target**
//!   physics (a *column* of the target's stiffness);
//! - `target_dual` (e.g. `"q"`) — the **target's** dual variable; the row into
//!   which the constraint reaction `Cᵀ` is added (it cannot be derived from
//!   `imposed_variable` — the `T → q` map is target-specific);
//! - `multiplier` (default `lambda_<imposed_variable>`) — this sub-model's own
//!   **primal**: the Lagrange multiplier, an unknown of the augmented system,
//!   whose solved value is the **reaction**;
//! - `imposed_value` (default `imposed_<imposed_variable>`) — this sub-model's
//!   own **dual**: the constraint-equation row, and the **slot** at which the
//!   user writes the imposed value `u_d` in the load `SubNodeField` (on the
//!   multiplier node).
//!
//! `multiplier` and `imposed_value` are derived from `imposed_variable` but can
//! be overridden by hand.

use crate::aggregate::Aggregate;
use crate::atoms::{ElementType, NodeId};
use crate::containers::element_field::SubElementField;
use crate::containers::mesh::Mesh;
use crate::dump::DumpOptions;
use crate::error::{PyrucastError, Result};
use crate::models::{
    constraint_block_pair, Constraint, ConstraintTerm, Contribution, MatrixKind, Physics, Relation,
    RelationSense, SubModelKind,
};
use serde::{Deserialize, Serialize};

/// Default multiplier (primal) name for a constrained variable: `lambda_<v>`.
///
/// ```
/// # 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::{Constraint, Physics, RelationSense, SubModelKind};
/// # use pyrucast::ops::mesh;
/// # 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 maillage = Mesh::from_submesh(sm);
/// # let fes = FiniteElementSpace::lagrange1(&maillage).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let impose = mesh::poi1_from_nodes(&n[..1]).unwrap();
/// # let mult = mesh::barycenter(&impose).unwrap();
/// # use pyrucast::models::dirichlet;
/// // Dérivés plutôt que tabulés : `multiplier` et `imposed_value` laissés
/// // à `None` prennent ces valeurs.
/// assert_eq!(
///     (dirichlet::default_multiplier("T").as_str(),
///      dirichlet::default_imposed_value("T").as_str()),
///     ("lambda_T", "imposed_T"));
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn default_multiplier(imposed_variable: &str) -> String {
    format!("lambda_{imposed_variable}")
}

/// Default imposed-value (dual) name for a constrained variable: `imposed_<v>`.
///
/// ```
/// # 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::{Constraint, Physics, RelationSense, SubModelKind};
/// # use pyrucast::ops::mesh;
/// # 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 maillage = Mesh::from_submesh(sm);
/// # let fes = FiniteElementSpace::lagrange1(&maillage).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let impose = mesh::poi1_from_nodes(&n[..1]).unwrap();
/// # let mult = mesh::barycenter(&impose).unwrap();
/// # use pyrucast::models::dirichlet;
/// // Dérivés plutôt que tabulés : `multiplier` et `imposed_value` laissés
/// // à `None` prennent ces valeurs.
/// assert_eq!(
///     (dirichlet::default_multiplier("T").as_str(),
///      dirichlet::default_imposed_value("T").as_str()),
///     ("lambda_T", "imposed_T"));
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn default_imposed_value(imposed_variable: &str) -> String {
    format!("imposed_{imposed_variable}")
}

/// Dirichlet constraint imposed via Lagrange multipliers.
///
/// See the module documentation for the meaning of the four variable names and
/// the two meshes. The imposed value `u_d` is **not** stored here: the user
/// supplies it through the load `SubNodeField` at the multiplier node's
/// `imposed_value` component.
///
/// ```
/// # 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::{Constraint, Physics, RelationSense, SubModelKind};
/// # use pyrucast::ops::mesh;
/// # 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 maillage = Mesh::from_submesh(sm);
/// # let fes = FiniteElementSpace::lagrange1(&maillage).unwrap();
/// # let zone = fes.get(0).unwrap();
/// # let impose = mesh::poi1_from_nodes(&n[..1]).unwrap();
/// # let mult = mesh::barycenter(&impose).unwrap();
/// # use pyrucast::models::dirichlet::{self, Dirichlet};
/// # let cible = pyrucast::ops::model::heat_conduction(&fes).unwrap();
/// // Un appui : `u = u_d` aux nœuds imposés, par multiplicateur de Lagrange.
/// let d = Dirichlet::new(&cible, "T", &impose, &mult,
///                        RelationSense::Equality)?;
/// // Sans noms donnés, les défauts se dérivent de la variable imposée.
/// assert_eq!(d.relations()?.len(), 1);
/// assert_eq!(d.relations()?[0].imposed_value, dirichlet::default_imposed_value("T"));
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
#[derive(Serialize, Deserialize)]
pub struct Dirichlet {
    /// Constrained primal of the target physics (e.g. `"T"`).
    pub(crate) imposed_variable: String,
    /// Target physics's dual; row where the reaction `Cᵀ` lands (e.g. `"q"`).
    pub(crate) target_dual: String,
    /// This sub-model's primal — the Lagrange multiplier (e.g. `"lambda_T"`).
    pub(crate) multiplier: String,
    /// This sub-model's dual — constraint row + imposed-value slot (e.g. `"imposed_T"`).
    pub(crate) imposed_value: String,
    /// POI1 mesh of the constrained nodes (one node per cell), as given by the
    /// user. Shares the submeshes (refcounts keep the nodes alive).
    pub(crate) imposed_mesh: Mesh,
    /// POI1 mesh of the multiplier nodes, paired element-for-element with
    /// `imposed_mesh` (same submesh structure, same per-submesh cell count).
    pub(crate) multiplier_mesh: Mesh,
    /// Equality (default) or unilateral inequality (`u ≥ u_d` / `u ≤ u_d`),
    /// solved by the active-set operator.
    #[serde(default)]
    pub(crate) sense: RelationSense,
}

impl Dirichlet {
    /// Build a Dirichlet constraint imposing `imposed_variable = u_d` on the
    /// nodes of `imposed_mesh`, with multipliers living on `multiplier_mesh`.
    ///
    /// `target_dual` is the dual variable of the target physics (e.g. `"q"`).
    /// `multiplier` / `imposed_value` default to `lambda_<imposed_variable>` /
    /// `imposed_<imposed_variable>` when `None`. Both meshes must be POI1 (for
    /// now), share one [`Coords`](crate::coords::Coords),
    /// and pair element-for-element (same number of submeshes, same cell count
    /// per submesh).
    ///
    /// `sense` turns the constraint unilateral: `GreaterEqual` imposes
    /// `u ≥ u_d` (`LessEqual`: `u ≤ u_d`), enforced only while in contact — such
    /// a model is solved by the active-set operator
    /// [`unilateral`](crate::ops::solver::unilateral).
    ///
    /// ```
    /// # 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::{Constraint, Physics, RelationSense, SubModelKind};
    /// # use pyrucast::ops::mesh;
    /// # 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 maillage = Mesh::from_submesh(sm);
    /// # let fes = FiniteElementSpace::lagrange1(&maillage).unwrap();
    /// # let zone = fes.get(0).unwrap();
    /// # let impose = mesh::poi1_from_nodes(&n[..1]).unwrap();
    /// # let mult = mesh::barycenter(&impose).unwrap();
    /// # use pyrucast::models::dirichlet::{self, Dirichlet};
    /// # let cible = pyrucast::ops::model::heat_conduction(&fes).unwrap();
    /// // Un appui : `u = u_d` aux nœuds imposés, par multiplicateur de Lagrange.
    /// let d = Dirichlet::new(&cible, "T", &impose, &mult,
    ///                        RelationSense::Equality)?;
    /// // Sans noms donnés, les défauts se dérivent de la variable imposée.
    /// assert_eq!(d.relations()?.len(), 1);
    /// assert_eq!(d.relations()?[0].imposed_value, dirichlet::default_imposed_value("T"));
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn new(
        target: &crate::containers::model::Model,
        variable: &str,
        imposed_mesh: &Mesh,
        multiplier_mesh: &Mesh,
        sense: RelationSense,
    ) -> Result<Self> {
        // The target's own names, read from the target rather than retyped: the
        // `T → q` map is its business, and a mistyped one used to build a
        // constraint on a variable nobody assembles — a singular matrix, with
        // nothing to point at.
        let imposed_variable = variable.to_string();
        let target_dual = target.dual_of(variable).ok_or_else(|| {
            PyrucastError::Message(format!(
                "Dirichlet: `{variable}` is not a primal of the model it constrains — it \
                 declares {:?}",
                target.primal_vars()
            ))
        })?;
        if imposed_mesh.cell_count() == 0 {
            return Err(PyrucastError::Message(
                "Dirichlet: imposed_mesh must constrain at least one node".into(),
            ));
        }
        let n_sub = imposed_mesh.len();
        if multiplier_mesh.len() != n_sub {
            return Err(PyrucastError::Message(format!(
                "Dirichlet: imposed_mesh has {} submesh(es) but multiplier_mesh has {}",
                n_sub,
                multiplier_mesh.len()
            )));
        }
        // NodeIds are Coords-relative: both meshes must share it.
        let coords_i = imposed_mesh.coords()?;
        let coords_m = multiplier_mesh.coords()?;
        if !coords_i.same_object(&coords_m) {
            return Err(PyrucastError::Message(
                "Dirichlet: imposed_mesh and multiplier_mesh must share a Coords".into(),
            ));
        }
        // POI1 (for now) + element-for-element pairing (equal cell counts).
        for i in 0..n_sub {
            let imp = imposed_mesh.get(i)?;
            let mult = multiplier_mesh.get(i)?;
            let (iet, icount) = {
                let s = imp.read();
                (s.element_type(), s.cell_count())
            };
            let (met, mcount) = {
                let s = mult.read();
                (s.element_type(), s.cell_count())
            };
            if iet != ElementType::POI1 {
                return Err(PyrucastError::Message(format!(
                    "Dirichlet: imposed_mesh submesh {i} must be POI1, got {iet}"
                )));
            }
            if met != ElementType::POI1 {
                return Err(PyrucastError::Message(format!(
                    "Dirichlet: multiplier_mesh submesh {i} must be POI1, got {met}"
                )));
            }
            if icount != mcount {
                return Err(PyrucastError::Message(format!(
                    "Dirichlet: submesh {i} has {icount} constrained node(s) but \
                     {mcount} multiplier(s)"
                )));
            }
        }

        let multiplier = default_multiplier(&imposed_variable);
        let imposed_value = default_imposed_value(&imposed_variable);

        Ok(Self {
            imposed_variable,
            target_dual,
            multiplier,
            imposed_value,
            imposed_mesh: share(imposed_mesh)?,
            multiplier_mesh: share(multiplier_mesh)?,
            sense,
        })
    }
}

impl SubModelKind for Dirichlet {
    fn primal_vars(&self) -> Vec<String> {
        vec![self.multiplier.clone()]
    }

    fn dual_vars(&self) -> Vec<String> {
        vec![self.imposed_value.clone()]
    }

    /// Its relations, applied to the solution: the reaction `Cᵀ λ` on the
    /// constrained nodes, and `C·u` on its own row.
    fn internal_force_contribution(&self) -> Vec<crate::models::ResidualContribution> {
        vec![crate::models::ResidualContribution::Relations]
    }

    fn as_constraint(&self) -> Option<&dyn Constraint> {
        Some(self)
    }

    /// Dirichlet contributes its **literal** C / Cᵀ blocks directly — it has no
    /// [`stiffness_layout`](SubModelKind::stiffness_layout) (nothing is
    /// integrated on a cell), so it bypasses the layout-driven default and
    /// returns a single [`Contribution::Literal`] carrying the filled blocks.
    /// This is *the* seam that keeps the assembler free of any Dirichlet-specific
    /// special case.
    fn contributions(
        &self,
        kind: MatrixKind,
        _material: Option<&crate::handle::Handle<SubElementField>>,
    ) -> Result<Vec<Contribution>> {
        // A constraint only enters the global (stiffness) matrix — no
        // mass/geometric/tangent term.
        if kind != MatrixKind::Stiffness {
            return Ok(Vec::new());
        }
        // One C / Cᵀ pair per submesh pair — the user's submeshes are used
        // directly as row/col supports (no flattening), so the submesh structure
        // carried through `barycenter` is preserved. Dirichlet is the single-term
        // relation with coefficient 1, so it defers to the shared block builder.
        let mut blocks = Vec::with_capacity(self.imposed_mesh.len() * 2);
        for i in 0..self.imposed_mesh.len() {
            let imp_sm = self.imposed_mesh.get(i)?;
            let mult_sm = self.multiplier_mesh.get(i)?;
            let (c_block, ct_block) = constraint_block_pair(
                &mult_sm,
                &imp_sm,
                &self.imposed_variable,
                &self.target_dual,
                &self.multiplier,
                &self.imposed_value,
                1.0,
            )?;
            blocks.push(c_block);
            blocks.push(ct_block);
        }
        Ok(vec![Contribution::Literal(blocks)])
    }

    fn physics(&self) -> &'static [Physics] {
        &[Physics::Constraint]
    }

    fn label(&self) -> &'static str {
        "Dirichlet"
    }

    fn display(&self) -> String {
        let n = self.imposed_mesh.cell_count();
        format!(
            "SubModel<Dirichlet({})>: {} constrained node(s)",
            self.imposed_variable, n
        )
    }

    fn render(&self, _opts: &DumpOptions) -> String {
        let nc = self.imposed_mesh.cell_count();
        let nm = self.multiplier_mesh.cell_count();
        format!(
            "SubModel<Dirichlet({imposed_variable})>\n  primal var(s): {multiplier} \
             (multiplier)\n  dual var(s):   {imposed_value} (imposed value)\n  \
             targets primary dual: {target_dual}\n  sense: {sense}\n  \
             constrained: {nc} node(s)\n  multipliers: {nm} node(s)",
            imposed_variable = self.imposed_variable,
            multiplier = self.multiplier,
            imposed_value = self.imposed_value,
            target_dual = self.target_dual,
            sense = self.sense,
        )
    }
}

impl Constraint for Dirichlet {
    fn multiplier_mesh(&self) -> &Mesh {
        &self.multiplier_mesh
    }

    /// Dirichlet is a single-term relation `1·u ⋈ u_d` per constrained node
    /// (`⋈` is the sub-model's sense, `=` by default).
    fn relations(&self) -> Result<Vec<Relation>> {
        let mut relations = Vec::with_capacity(self.imposed_mesh.cell_count());
        for i in 0..self.imposed_mesh.len() {
            let imposed_nodes: Vec<NodeId> =
                self.imposed_mesh.get(i)?.read().connectivity().to_vec();
            let multiplier_nodes: Vec<NodeId> =
                self.multiplier_mesh.get(i)?.read().connectivity().to_vec();
            for (imp, mult) in imposed_nodes.iter().zip(multiplier_nodes.iter()) {
                relations.push(Relation {
                    multiplier_node: *mult,
                    imposed_value: self.imposed_value.clone(),
                    terms: vec![ConstraintTerm {
                        node: *imp,
                        variable: self.imposed_variable.clone(),
                        target_dual: self.target_dual.clone(),
                        coefficient: 1.0,
                    }],
                    sense: self.sense,
                });
            }
        }
        Ok(relations)
    }
}

/// Clone a mesh by sharing its submeshes — increfs each submesh handle so the
/// nodes stay alive for the lifetime of the sub-model.
fn share(mesh: &Mesh) -> Result<Mesh> {
    let mut out = Mesh::empty();
    for sm in mesh {
        out.add_sub(sm.clone())?;
    }
    Ok(out)
}