pyrucast 0.3.2

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
use crate::aggregate::Aggregate;
use crate::containers::field::{Field, SubField};
use crate::containers::mesh::Mesh;
use crate::containers::node_field::{NodeField, NodeFieldView, SubNodeField};
use crate::error::{PyrucastError, Result};
use crate::handle::Handle;

/// Fill `sub` (already on its target support) with the values of `source`,
/// component by component, node by node: a `(node, component)` pair that
/// `source` covers is copied, one it does not is left at `0.0`. Shared by
/// [`restrict`] and [`restrict_like`].
fn fill_from(sub: &mut SubNodeField, source: &NodeFieldView) -> Result<()> {
    let components = sub.components().to_vec();
    for nid in sub.nodes().to_vec() {
        for comp in &components {
            if let Some(v) = source.value_opt(nid, comp) {
                sub.set_value(nid, comp, v)?;
            }
        }
    }
    Ok(())
}

/// Restrict `field` to the nodes used by `mesh`.
///
/// Returns a new [`NodeField`] with one zone per submesh of `mesh`. Each zone
/// is supported on the submesh's **canonical POI1 node cloud** — its distinct
/// nodes, via [`SubMesh::to_poi1`](crate::containers::mesh::SubMesh::to_poi1),
/// which is materialised once and cached. Two restrictions onto the **same**
/// `mesh` therefore land on the **same support slot** and combine directly by
/// the arithmetic operators: `restrict(a, mesh) - restrict(b, mesh)` is the
/// node-by-node difference. That cached support is also the one a stiffness
/// block over `mesh` uses, so `&K * &restrict(f, mesh)` and
/// `solve(K, f) - restrict(g, mesh)` line up too.
///
/// Each zone carries the union of `field`'s components. Values are read
/// through the aggregate (first zone of `field` defining the pair wins); a
/// node of `mesh` that `field` does not cover is assigned `0.0`; a node of
/// `field` absent from `mesh` is dropped.
///
/// Element operations on the region — [`gradient`](fn@crate::ops::element_field::gradient),
/// [`integral`](fn@crate::ops::measure::integral),
/// [`deformation`](fn@crate::ops::element_field::deformation),
/// [`interp_to_gauss`](fn@crate::ops::element_field::interp_to_gauss) — take the mesh /
/// finite-element space as a **separate argument** and read the field by node
/// id. To land instead on the *exact* support of an **existing field** (rather
/// than a mesh), use [`restrict_like`].
///
/// Errors if `mesh` is attached to a different `Coords` than
/// `field`.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{Band, 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::node_field::NodeField;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::ops::{coords as ops_coords, element_field, field, measure, mesh, node_field};
/// # 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 support = mesh::poi1_from_nodes(&n).unwrap();
/// # let champ = |noms: Vec<String>| {
/// #     NodeField::from_submesh(&support.get(0).unwrap(), noms).unwrap()
/// # };
/// # let temp = champ(vec!["T".into()]);
/// # {
/// #     let mut z = temp.get(0).unwrap().write();
/// #     z.set_value(n[0].id(), "T", 10.0).unwrap();
/// #     z.set_value(n[1].id(), "T", 50.0).unwrap();
/// #     z.set_value(n[2].id(), "T", 90.0).unwrap();
/// # }
/// // Le champ ramené aux nœuds d'un maillage — un bord, une sélection.
/// let bout = mesh::poi1_from_nodes(&n[..1])?;
/// let r = node_field::restrict(&temp, &bout)?;
/// assert_eq!(r.node_count()?, 1);
/// assert_eq!(r.get(0)?.read().value(n[0].id(), "T")?, 10.0);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn restrict(field: &NodeField, mesh: &Mesh) -> Result<NodeField> {
    let mesh_coords = mesh.coords()?;
    let field_coords = field.coords()?;
    if !mesh_coords.same_object(&field_coords) {
        return Err(PyrucastError::Message(
            "restrict: mesh is not attached to the same Coords".into(),
        ));
    }

    let components = Field::components(field)?;
    let view = field.view()?;
    let mut out = NodeField::default();
    for sm in mesh {
        let mut sub = SubNodeField::from_support(sm, components.clone())?;
        fill_from(&mut sub, &view)?;
        out.add_sub(Handle::new(sub))?;
    }
    Ok(out)
}

/// Reproject `field` onto the **exact support and components of `target`**,
/// zone by zone.
///
/// Unlike [`restrict`] (which lands on a *fresh* support materialised from a
/// mesh, carrying the union of `field`'s components), this reuses each zone of
/// `target` **as-is** — the same support object, the same component list — so the result is
/// on the very same support as `target` and can be combined with it directly by
/// the arithmetic operators (`&target + &field.restrict_like(target)`). Each
/// `(node, component)` pair is filled from `field` when it covers it, `0.0`
/// otherwise; nodes and components of `field` absent from `target` are dropped.
///
/// The typical use is folding a solver increment back into a running solution:
/// a `solve` result carries the primal *and* dual (Lagrange multiplier) DOFs on
/// a fresh support, whereas the running field carries only the primal DOFs —
/// `restrict_like` keeps exactly the latter, on the running field's own support.
///
/// Errors if `target` is attached to a different `Coords` than `field`.
///
/// ```
/// # use pyrucast::aggregate::Aggregate;
/// # use pyrucast::atoms::{Band, 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::node_field::NodeField;
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::ops::{coords as ops_coords, element_field, field, measure, mesh, node_field};
/// # 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 support = mesh::poi1_from_nodes(&n).unwrap();
/// # let champ = |noms: Vec<String>| {
/// #     NodeField::from_submesh(&support.get(0).unwrap(), noms).unwrap()
/// # };
/// # let temp = champ(vec!["T".into()]);
/// # {
/// #     let mut z = temp.get(0).unwrap().write();
/// #     z.set_value(n[0].id(), "T", 10.0).unwrap();
/// #     z.set_value(n[1].id(), "T", 50.0).unwrap();
/// #     z.set_value(n[2].id(), "T", 90.0).unwrap();
/// # }
/// // La même chose en visant le support d'un **autre champ** : le résultat
/// // s'apparie avec lui sous `same_support`, ce qui est ce qu'exige une
/// // opération terme à terme.
/// # let bout = mesh::poi1_from_nodes(&n[..2])?;
/// let cible = NodeField::from_submesh(&bout.get(0)?, vec!["q".into()])?;
/// let r = node_field::restrict_like(&temp, &cible)?;
/// assert!(r.get(0)?.read().same_support(&cible.get(0)?.read()));
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn restrict_like(field: &NodeField, target: &NodeField) -> Result<NodeField> {
    let target_coords = target.coords()?;
    let field_coords = field.coords()?;
    if !target_coords.same_object(&field_coords) {
        return Err(PyrucastError::Message(
            "restrict_like: target is not attached to the same Coords".into(),
        ));
    }

    let view = field.view()?;
    let mut out = NodeField::default();
    for h in target.iter() {
        let zone = h.read();
        // `from_support` on the zone's own support handle shares its slot (POI1
        // supports are shared as-is), so the output pairs with `target` under
        // `same_support` — the precondition of the field operators.
        let mut sub = SubNodeField::from_support(&zone.support(), zone.components().to_vec())?;
        fill_from(&mut sub, &view)?;
        out.add_sub(Handle::new(sub))?;
    }
    Ok(out)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::atoms::ElementType;
    use crate::atoms::Node;
    use crate::containers::mesh::SubMesh;
    use crate::coords::Coords;
    use crate::handle::Handle;

    /// Build a single-zone POI1 field on `n` fresh 1-D nodes;
    /// returns (coords, nodes, field).
    fn poi1_field(
        n: usize,
        components: Vec<String>,
    ) -> (crate::handle::Handle<Coords>, Vec<Node>, NodeField) {
        let coords = Handle::new(Coords::new(1).unwrap());
        let nodes: Vec<Node> = (0..n)
            .map(|i| Node::create_in(coords.clone(), &[i as f64]).unwrap())
            .collect();
        let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
        for nd in &nodes {
            sm.add_cell(&[nd.id()]).unwrap();
        }
        let field =
            NodeField::from_sub(SubNodeField::from_poi1(&Handle::new(sm), components).unwrap());
        (coords, nodes, field)
    }

    #[test]
    fn restrict_subset() {
        let (coords, nodes, f) = poi1_field(3, vec!["T".into(), "P".into()]);
        {
            let mut s = f.get(0).unwrap().write();
            s.set(0, 0, 1.0).unwrap();
            s.set(1, 0, 2.0).unwrap();
            s.set(2, 0, 3.0).unwrap();
        }

        // Mesh with only nodes[0] and nodes[2].
        let mut m = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::POI1));
        m.add_cell(&[nodes[0].id()]).unwrap();
        m.add_cell(&[nodes[2].id()]).unwrap();

        let r = restrict(&f, &m).unwrap();
        assert_eq!(r.len(), 1);
        assert_eq!(r.node_count().unwrap(), 2);
        assert_eq!(Field::components(&r).unwrap(), vec!["T", "P"]);
        assert_eq!(r.value(nodes[0].id(), "T").unwrap(), 1.0);
        assert_eq!(r.value(nodes[2].id(), "T").unwrap(), 3.0);
        assert_eq!(r.value(nodes[0].id(), "P").unwrap(), 0.0); // absent → 0
        assert_eq!(r.value_opt(nodes[1].id(), "T").unwrap(), None); // dropped
    }

    #[test]
    fn restrict_node_absent_from_field_gives_zero() {
        let (coords, nodes, f) = poi1_field(1, vec!["T".into()]);
        f.get(0).unwrap().write().set(0, 0, 7.0).unwrap();
        let nb = Node::create_in(coords.clone(), &[1.0]).unwrap();

        // Mesh contains nb which is NOT in the field.
        let mut m = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::POI1));
        m.add_cell(&[nodes[0].id()]).unwrap();
        m.add_cell(&[nb.id()]).unwrap();

        let r = restrict(&f, &m).unwrap();
        assert_eq!(r.node_count().unwrap(), 2);
        assert_eq!(r.value(nodes[0].id(), "T").unwrap(), 7.0);
        assert_eq!(r.value(nb.id(), "T").unwrap(), 0.0);
    }

    #[test]
    fn restrict_one_zone_per_mesh_submesh() {
        let (coords, nodes, f) = poi1_field(2, vec!["T".into()]);
        let mut mesh = Mesh::empty();
        for nd in &nodes {
            let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
            sm.add_cell(&[nd.id()]).unwrap();
            mesh.add_sub(Handle::new(sm)).unwrap();
        }
        let r = restrict(&f, &mesh).unwrap();
        assert_eq!(r.len(), 2, "one zone per submesh of the target mesh");
    }

    #[test]
    fn restrict_like_lands_on_target_support_and_components() {
        let coords = Handle::new(Coords::new(1).unwrap());
        let n0 = Node::create_in(coords.clone(), &[0.0]).unwrap();
        let n1 = Node::create_in(coords.clone(), &[1.0]).unwrap();
        let n2 = Node::create_in(coords.clone(), &[2.0]).unwrap(); // only in source

        // Target: POI1 on {n0, n1}, components [u_x, u_y].
        let mut sm_t = SubMesh::new(coords.clone(), ElementType::POI1);
        sm_t.add_cell(&[n0.id()]).unwrap();
        sm_t.add_cell(&[n1.id()]).unwrap();
        let target = NodeField::from_sub(
            SubNodeField::from_poi1(&Handle::new(sm_t), vec!["u_x".into(), "u_y".into()]).unwrap(),
        );

        // Source (`du`-like): superset nodes, extra `lambda` component.
        let mut sm_s = SubMesh::new(coords.clone(), ElementType::POI1);
        for n in [&n0, &n1, &n2] {
            sm_s.add_cell(&[n.id()]).unwrap();
        }
        let mut ss = SubNodeField::from_poi1(
            &Handle::new(sm_s),
            vec!["u_x".into(), "u_y".into(), "lambda".into()],
        )
        .unwrap();
        ss.set_value(n0.id(), "u_x", 1.0).unwrap();
        ss.set_value(n1.id(), "u_y", 2.0).unwrap();
        ss.set_value(n2.id(), "u_x", 9.0).unwrap(); // node dropped
        ss.set_value(n0.id(), "lambda", 7.0).unwrap(); // component dropped
        let source = NodeField::from_sub(ss);

        let out = restrict_like(&source, &target).unwrap();
        assert_eq!(Field::components(&out).unwrap(), vec!["u_x", "u_y"]);
        assert_eq!(out.node_count().unwrap(), 2); // n2 dropped
        assert_eq!(out.value(n0.id(), "u_x").unwrap(), 1.0);
        assert_eq!(out.value(n1.id(), "u_y").unwrap(), 2.0);
        assert_eq!(out.value(n1.id(), "u_x").unwrap(), 0.0); // absent → 0

        // Lands on `target`'s own support ⇒ the arithmetic operator applies.
        let sum = (&target + &out).unwrap();
        assert_eq!(sum.value(n0.id(), "u_x").unwrap(), 1.0);
        assert_eq!(sum.value(n1.id(), "u_y").unwrap(), 2.0);
    }

    #[test]
    fn restrict_like_incompatible_cfg_errors() {
        let (_c1, _n1, f) = poi1_field(1, vec!["T".into()]);
        let (_c2, _n2, target) = poi1_field(1, vec!["T".into()]); // different Coords
        assert!(restrict_like(&f, &target).is_err());
    }

    #[test]
    fn restrict_incompatible_cfg_errors() {
        let (_cfg1, _nodes1, f) = poi1_field(1, vec!["T".into()]);
        // A mesh attached to a *different* Coords.
        let cfg2 = Handle::new(Coords::new(1).unwrap());
        let n2 = Node::create_in(cfg2.clone(), &[0.0]).unwrap();
        let mut m2 = Mesh::from_submesh(SubMesh::new(cfg2.clone(), ElementType::POI1));
        m2.add_cell(&[n2.id()]).unwrap();
        assert!(restrict(&f, &m2).is_err());
    }

    /// Two restrictions onto the **same element mesh** land on its cached POI1
    /// companion (one shared slot), so they subtract node-by-node instead of
    /// passing through as two disjoint zones.
    #[test]
    fn restrict_twice_to_element_mesh_shares_support_and_subtracts() {
        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();

        // A genuine element mesh (TRI3), not a POI1 cloud.
        let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
        sm.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
        let mesh = Mesh::from_submesh(sm);

        // Two source node fields carrying "v" on the same nodes.
        let mk = |va: f64, vb: f64, vc: f64| {
            let mut psm = SubMesh::new(coords.clone(), ElementType::POI1);
            for nd in [&a, &b, &c] {
                psm.add_cell(&[nd.id()]).unwrap();
            }
            let mut f = SubNodeField::from_poi1(&Handle::new(psm), vec!["v".into()]).unwrap();
            f.set_value(a.id(), "v", va).unwrap();
            f.set_value(b.id(), "v", vb).unwrap();
            f.set_value(c.id(), "v", vc).unwrap();
            NodeField::from_sub(f)
        };
        let a2 = restrict(&mk(1.0, 2.0, 3.0), &mesh).unwrap();
        let b2 = restrict(&mk(0.5, 0.5, 0.5), &mesh).unwrap();

        // Same canonical support slot ⇒ the operators pair the zones.
        let sa = a2.get(0).unwrap().read().support();
        let sb = b2.get(0).unwrap().read().support();
        assert!(
            sa.same_object(&sb),
            "both restricts share the cached POI1 support"
        );

        let diff = (&a2 - &b2).unwrap();
        assert_eq!(diff.len(), 1, "one fused zone, not two pass-through zones");
        assert_eq!(diff.value(a.id(), "v").unwrap(), 0.5);
        assert_eq!(diff.value(b.id(), "v").unwrap(), 1.5);
        assert_eq!(diff.value(c.id(), "v").unwrap(), 2.5);
    }

    /// Regression: restricting a field whose support **is** the mesh's cached
    /// POI1 companion must not deadlock. `restrict` holds a `view` (read guard)
    /// over that support across the loop, while `from_poi1` re-`seal`s it — and
    /// `seal`'s already-sealed read fast-path is what stops that from
    /// write-locking against the held reader. Before the fix this hung.
    #[test]
    fn restrict_onto_own_cached_companion_does_not_deadlock() {
        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 sm = SubMesh::new(coords.clone(), ElementType::TRI3);
        sm.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
        let mesh = Mesh::from_submesh(sm);

        let mut psm = SubMesh::new(coords.clone(), ElementType::POI1);
        for nd in [&a, &b, &c] {
            psm.add_cell(&[nd.id()]).unwrap();
        }
        let mut f = SubNodeField::from_poi1(&Handle::new(psm), vec!["v".into()]).unwrap();
        f.set_value(a.id(), "v", 5.0).unwrap();
        let base = NodeField::from_sub(f);

        // `on_companion` lives on the mesh's cached POI1 companion.
        let on_companion = restrict(&base, &mesh).unwrap();
        // Restricting it onto the same mesh reuses that very support slot.
        let again = restrict(&on_companion, &mesh).unwrap();
        assert_eq!(again.value(a.id(), "v").unwrap(), 5.0);

        let s1 = on_companion.get(0).unwrap().read().support();
        let s2 = again.get(0).unwrap().read().support();
        assert!(
            s1.same_object(&s2),
            "both land on the shared cached companion"
        );
    }
}