pyrucast 0.3.3

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
//! Exchange law across an **interface** between two meshes.
//!
//! Two bodies meeting along a surface do not, in general, share their nodes: an
//! imperfect contact, a coating, a joint, a membrane all let the field **jump**
//! across the interface while a flux crosses it in proportion to that jump:
//!
//! ```text
//! j·n = h · (c₁ − c₂)
//! ```
//!
//! `h` is the transfer coefficient (its inverse is the contact resistance). What
//! is transferred is the **caller's** to say: the sub-model is given
//! `(primal, dual)` pairs — the same shape [`embedded`](crate::models::embedded)
//! and [`contact`](crate::models::contact) take — and derives its coefficients
//! `h_<primal>` and its fluxes `flux_<primal>` from them. The same law describes
//! a thermal contact resistance (`("T", "q")`), a coating on a diffusion
//! (`("c_H2", "j_H2")`) and a bonded joint of finite stiffness (the three
//! displacement pairs): the mathematics is identical, only the names change.
//!
//! ## When *not* to use it on displacements
//!
//! Tying two surfaces by making `h` large is a **penalty** method, and a
//! [`Mpc`](crate::models::mpc) does it exactly, without degrading the
//! conditioning. The test is where the number comes from: if `h` comes from a
//! measurement this is physics; if `h` was chosen "large enough", it wanted a
//! constraint. See [`transfer`](crate::models::transfer), the module the two
//! exchange laws share.
//!
//! ## Four blocks, two of them off-diagonal
//!
//! The weak form of the exchange term over the interface `Γ` is
//!
//! ```text
//! ∮_Γ h (c₁ − c₂)(δc₁ − δc₂) dΓ
//! ```
//!
//! which expands into a **2×2 block structure** on the two sides' DOFs:
//!
//! ```text
//! ⎡ +K  −K ⎤          with   K_ij = h ∫_Γ N_i N_j dΓ
//! ⎣ −K  +K ⎦
//! ```
//!
//! The two diagonal blocks are ordinary [`Contribution::Computed`] blocks — rows
//! and columns on one mesh. The two off-diagonal ones have their rows on one mesh
//! and their columns on the other, which is exactly
//! [`Contribution::Coupling`]. This physics is its first user.
//!
//! The **sign** rides on the kernel, not on a factor threaded through the
//! assembler: [`SubModelKind::element_matrix`] gives `+h∫N_iN_j` and
//! [`SubModelKind::coupling_element`] gives `−h∫N_iN_j`. Because each block picks
//! its kernel from its own contribution variant, the assembler needs to know
//! nothing about interfaces.
//!
//! ## Conformité
//!
//! The two sides must be **conforming**: same element type, same cell count,
//! cell `i` facing cell `i`, and node `k` of a cell facing node `k` of its
//! counterpart. That is checked geometrically at construction — the paired nodes
//! must be co-located — and reported rather than approximated. A non-matching
//! interface is a meshing problem; papering over it with a projection would be a
//! silent source of wrong fluxes.

use crate::containers::element_field::SubElementField;
use crate::containers::finite_element_space::SubFiniteElementSpace;
use crate::containers::matrix::DofOrdering;
use crate::containers::mesh::SubMesh;
use crate::coords::Coords;
use crate::dump::DumpOptions;
use crate::error::{PyrucastError, Result};
use crate::handle::Handle;
use crate::models::transfer::{
    coefficient_indices, coefficient_name, exchange_matrix, flux_name, internal_force, jump_name,
    material_contract, physics_slice,
};
use crate::models::{
    CellGeom, Contribution, CouplingLayout, Domain, MatrixKind, MatrixLayout, Physics, SubModelKind,
};
use serde::{Deserialize, Serialize};

/// Exchange law between two conforming boundary FE subspaces.
///
/// ```
/// # 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::interface_transfer::InterfaceTransfer;
/// # use pyrucast::models::Domain;
/// // Deux bords **conformes** — ici le même, ce qui suffit à montrer le
/// // contrat ; en pratique deux faces en vis-à-vis.
/// let i = InterfaceTransfer::new(zone.clone(), zone.clone(),
///     vec![("T".into(), "q".into())], Physics::Thermal, 1e-6)?;
/// assert_eq!(i.primal_vars(), vec!["T".to_string()]);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
#[derive(Clone, Serialize, Deserialize)]
pub struct InterfaceTransfer {
    pub(crate) side_a: Handle<SubFiniteElementSpace>,
    pub(crate) side_b: Handle<SubFiniteElementSpace>,
    /// POI1 supports over each side's unique nodes.
    pub(crate) support_a: Handle<SubMesh>,
    pub(crate) support_b: Handle<SubMesh>,
    /// The transferred quantities, as `(primal, dual)` pairs.
    pub(crate) components: Vec<(String, String)>,
    /// The physics nature this exchange belongs to — what `model.filter(…)`
    /// selects it by. Free variable names cannot imply it, so it is declared.
    pub(crate) physics: Physics,
}

impl InterfaceTransfer {
    /// Exchange law across the interface between two **conforming** boundary FE
    /// subspaces. Errors unless the two sides match cell for cell and node for
    /// node, within `tol` of each other geometrically.
    ///
    /// ```
    /// # 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::interface_transfer::InterfaceTransfer;
    /// # use pyrucast::models::Domain;
    /// // Deux bords **conformes** — ici le même, ce qui suffit à montrer le
    /// // contrat ; en pratique deux faces en vis-à-vis.
    /// let i = InterfaceTransfer::new(zone.clone(), zone.clone(),
    ///     vec![("T".into(), "q".into())], Physics::Thermal, 1e-6)?;
    /// assert_eq!(i.primal_vars(), vec!["T".to_string()]);
    /// # Ok::<(), pyrucast::PyrucastError>(())
    /// ```
    pub fn new(
        side_a: Handle<SubFiniteElementSpace>,
        side_b: Handle<SubFiniteElementSpace>,
        components: Vec<(String, String)>,
        physics: Physics,
        tol: f64,
    ) -> Result<Self> {
        material_contract("InterfaceTransfer", &components)?;
        let (mesh_a, mesh_b) = (side_a.read().submesh(), side_b.read().submesh());
        check_conforming_geometry(&mesh_a, &mesh_b, tol)?;
        let support_a = mesh_a.read().to_poi1()?;
        let support_b = mesh_b.read().to_poi1()?;
        Ok(Self {
            side_a,
            side_b,
            support_a,
            support_b,
            components,
            physics,
        })
    }

    /// The layout of a diagonal block, on one side of the interface.
    fn diagonal_layout(
        &self,
        fespace: &Handle<SubFiniteElementSpace>,
        support: &Handle<SubMesh>,
    ) -> MatrixLayout {
        MatrixLayout {
            fespaces: vec![fespace.clone()],
            support: support.clone(),
            dual_vars: self.dual_vars(),
            primal_vars: self.primal_vars(),
            ordering: DofOrdering::NodesThenVars,
            symmetric: true,
        }
    }

    /// The layout of an off-diagonal block, rows on one side, columns on the other.
    fn coupling_layout(
        &self,
        row_fespace: &Handle<SubFiniteElementSpace>,
        row_support: &Handle<SubMesh>,
        col_fespace: &Handle<SubFiniteElementSpace>,
        col_support: &Handle<SubMesh>,
    ) -> CouplingLayout {
        CouplingLayout {
            fespaces: vec![row_fespace.clone()],
            col_fespaces: vec![col_fespace.clone()],
            row_support: row_support.clone(),
            col_support: col_support.clone(),
            dual_vars: self.dual_vars(),
            primal_vars: self.primal_vars(),
            ordering: DofOrdering::NodesThenVars,
        }
    }
}

impl SubModelKind for InterfaceTransfer {
    fn primal_vars(&self) -> Vec<String> {
        self.components.iter().map(|(p, _)| p.clone()).collect()
    }

    fn dual_vars(&self) -> Vec<String> {
        self.components.iter().map(|(_, d)| d.clone()).collect()
    }

    fn physics(&self) -> &'static [Physics] {
        physics_slice(self.physics)
    }

    fn as_domain(&self) -> Option<&dyn Domain> {
        Some(self)
    }

    /// The four blocks of the exchange law — two diagonal, two coupling. Nothing
    /// but the stiffness: an interface law adds no mass, no geometric stiffness.
    fn contributions(
        &self,
        kind: MatrixKind,
        _material: Option<&Handle<SubElementField>>,
    ) -> Result<Vec<Contribution>> {
        if kind != MatrixKind::Stiffness {
            return Ok(Vec::new());
        }
        Ok(vec![
            Contribution::Computed(self.diagonal_layout(&self.side_a, &self.support_a)),
            Contribution::Computed(self.diagonal_layout(&self.side_b, &self.support_b)),
            Contribution::Coupling(self.coupling_layout(
                &self.side_a,
                &self.support_a,
                &self.side_b,
                &self.support_b,
            )),
            Contribution::Coupling(self.coupling_layout(
                &self.side_b,
                &self.support_b,
                &self.side_a,
                &self.support_a,
            )),
        ])
    }

    /// A diagonal block: `+h ∫_Γ N_i N_j dΓ`.
    fn element_matrix(
        &self,
        geoms: &[CellGeom],
        material: Option<&SubElementField>,
        ke: &mut [f64],
    ) -> Result<()> {
        let geom = &geoms[0];
        let mat = material.expect("InterfaceTransfer declares a material_fespace");
        let coefficients = coefficient_indices(mat, &self.components)?;
        exchange_matrix(geom, geom, mat, &coefficients, 1.0, ke)
    }

    /// An off-diagonal block: `−h ∫_Γ N_i^row N_j^col dΓ`. The sign lives here
    /// rather than in a factor, because the two kernels are already distinct.
    fn coupling_element(
        &self,
        _kind: MatrixKind,
        row_geoms: &[CellGeom],
        col_geoms: &[CellGeom],
        material: Option<&SubElementField>,
        ke: &mut [f64],
    ) -> Result<()> {
        let mat = material.expect("InterfaceTransfer declares a material_fespace");
        let coefficients = coefficient_indices(mat, &self.components)?;
        exchange_matrix(&row_geoms[0], &col_geoms[0], mat, &coefficients, -1.0, ke)
    }

    /// Internal fluxes `q_i = ∫ N_i · flux dΓ` — weighted by `N`, not by `Bᵀ`,
    /// exactly as for convection: the interface integrand is a flux **density**,
    /// not a gradient-conjugate quantity.
    fn internal_force_element(
        &self,
        geoms: &[CellGeom],
        stress: &SubElementField,
        fe: &mut [f64],
    ) -> Result<()> {
        internal_force(&geoms[0], stress, &self.components, fe)
    }

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

    fn render(&self, _opts: &DumpOptions) -> String {
        let primal = self.primal_vars().join(", ");
        let dual = self.dual_vars().join(", ");
        let cells = self.side_a.read().cell_count().unwrap_or(0);
        format!(
            "SubModel<InterfaceTransfer>\n  primal var(s): {primal}\n  \
             dual var(s):   {dual}\n  interface: {cells} facing cell pair(s)"
        )
    }
}

impl Domain for InterfaceTransfer {
    fn material_fespace(&self) -> Handle<SubFiniteElementSpace> {
        self.side_a.clone()
    }

    /// One coefficient per transferred quantity, named after it — `h_T`,
    /// `h_c_H2`, `h_u_x`.
    fn material_components(&self) -> Option<Vec<String>> {
        Some(
            self.components
                .iter()
                .map(|(p, _)| coefficient_name(p))
                .collect(),
        )
    }

    fn behavior_fespace(&self) -> Handle<SubFiniteElementSpace> {
        self.side_a.clone()
    }

    fn behavior_output_components(&self) -> Result<Vec<String>> {
        Ok(self.components.iter().map(|(p, _)| flux_name(p)).collect())
    }

    /// The exchanged flux density `h·(a₁ − a₂)` at one Gauss point, from the jump
    /// supplied as input (`jump_<primal>`), one transferred quantity at a time.
    fn integrate_point(
        &self,
        geom: &CellGeom,
        input: &SubElementField,
        _prev: Option<&SubElementField>,
        material: Option<&SubElementField>,
        g: usize,
        _dt: Option<f64>,
        out: &mut [f64],
    ) -> Result<()> {
        let mat = material.expect("InterfaceTransfer declares a material_fespace");
        let cell = geom.cell;
        for (v, (primal, _)) in self.components.iter().enumerate() {
            let h = mat.value(cell, g, &coefficient_name(primal))?;
            out[v] = h * input.value(cell, g, &jump_name(primal))?;
        }
        Ok(())
    }
}

/// Verify that two boundary sub-meshes face each other cell for cell **and node
/// for node**, their paired nodes being co-located within `tol`.
///
/// Node-for-node matters as much as cell-for-cell: the coupling kernel pairs
/// `N_i` of one side with `N_j` of the other at a shared Gauss point, which is
/// only meaningful if local node `k` of a cell faces local node `k` of its
/// counterpart.
fn check_conforming_geometry(
    mesh_a: &Handle<SubMesh>,
    mesh_b: &Handle<SubMesh>,
    tol: f64,
) -> Result<()> {
    let (a, b) = (mesh_a.read(), mesh_b.read());
    if a.element_type() != b.element_type() {
        return Err(PyrucastError::Message(format!(
            "InterfaceTransfer: the two sides must carry the same element type — \
             {:?} facing {:?}",
            a.element_type(),
            b.element_type()
        )));
    }
    if a.cell_count() != b.cell_count() {
        return Err(PyrucastError::Message(format!(
            "InterfaceTransfer: the two sides must be conforming — {} cell(s) facing {}",
            a.cell_count(),
            b.cell_count()
        )));
    }
    let (coords_a_h, coords_b_h) = (a.coords(), b.coords());
    let (guard_a, guard_b) = (coords_a_h.read(), coords_b_h.read());
    let (coords_a, coords_b): (&Coords, &Coords) = (&guard_a, &guard_b);
    for (k, (&na, &nb)) in a.connectivity().iter().zip(b.connectivity()).enumerate() {
        let (pa, pb) = (coords_a.position(na)?, coords_b.position(nb)?);
        let d2: f64 = pa
            .iter()
            .zip(pb)
            .map(|(x, y)| (x - y) * (x - y))
            .sum::<f64>();
        if d2.sqrt() > tol {
            return Err(PyrucastError::Message(format!(
                "InterfaceTransfer: the interface is not node-conforming — the node pair at \
                 connectivity slot {k} is {:.3e} apart (tolerance {tol:.3e}). Local node `k` of \
                 a cell must face local node `k` of its counterpart.",
                d2.sqrt()
            )));
        }
    }
    Ok(())
}