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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Global assembly by **scatter into a fixed CSR pattern**.
//!
//! The historical path turned every block into global `(row, col, value)`
//! triplets and rebuilt a CSR from them (a counting-sort with serial
//! histogram / scatter / dedup passes — the assembly bottleneck). This module
//! splits that into two phases.
//!
//! **Symbolic** ([`build_pattern`]): the global CSR sparsity (`row_offsets` +
//! `col_indices`) from the blocks' topology alone — no kernel eval. It is a
//! function of the model structure only, so it can be built once and reused
//! across assemblies (materials change, sparsity does not).
//!
//! **Numeric** ([`scatter_serial`]): evaluate each block's contribution and add
//! it into the CSR values at its precomputed slot.
//!
//! [`scatter_serial`] visits blocks — and, within a block, cells / COO entries —
//! in the same order the triplet stream had, accumulating each slot in that
//! order, so its result is **bit-for-bit** identical to the triplet path. The
//! parallel colour-driven scatter (the actual speed-up) builds on this same
//! pattern.

use crate::containers::matrix::{AssemblyPattern, BlockSlots, Matrix, NamedDof};
use crate::error::{PyrucastError, Result};
use crate::models::kernel;
use crate::ops::coloring;
use crate::parallel::*;
use nalgebra_sparse::CsrMatrix;
use std::collections::HashMap;
use std::sync::atomic::AtomicU64;

/// Build the global CSR sparsity [`AssemblyPattern`] for `k` from its blocks'
/// topology alone — no kernel evaluation. Each block contributes its global
/// `(row, col)` entries (a computed block via [`kernel::element_block_pattern`],
/// a literal block via its stored COO indices); columns are then sorted and
/// deduplicated per row.
///
/// ```
/// # 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::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::MatrixKind;
/// # use pyrucast::ops::{element_field, matrix, mesh, scatter};
/// # 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 modele = Model::heat_conduction(&fes).unwrap();
/// # let materiaux = element_field::material_field(&modele,
/// #     &[("k", 1.0), ("rho", 2.0), ("cp", 3.0)]).unwrap();
/// // Le motif est **matériau-indépendant** : il se bâtit une fois, puis
/// // les deux phases numériques le remplissent — la série, référence
/// // bit-à-bit, et la parallèle par coloriage.
/// let k = matrix::stiffness(&modele, &materiaux)?;
/// let motif = scatter::build_pattern(&k)?;
/// let serie = scatter::scatter_serial(&k, &motif)?;
/// let para = scatter::scatter_parallel(&k, &motif)?;
/// assert_eq!(serie.nnz(), motif.nnz());
/// assert_eq!(para.nnz(), serie.nnz());
/// // Mêmes valeurs, à l'ordre de sommation des couleurs près.
/// for (a, b) in serie.values().iter().zip(para.values()) {
///     assert!((a - b).abs() < 1e-12);
/// }
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn build_pattern(k: &Matrix) -> Result<AssemblyPattern> {
    let row_dofs = k.row_dofs()?;
    let col_dofs = k.col_dofs()?;
    let row_map: HashMap<NamedDof, usize> = row_dofs
        .iter()
        .cloned()
        .enumerate()
        .map(|(i, d)| (d, i))
        .collect();
    let col_map: HashMap<NamedDof, usize> = col_dofs
        .iter()
        .cloned()
        .enumerate()
        .map(|(i, d)| (d, i))
        .collect();

    let nrows = row_dofs.len();
    // Per row, the columns it touches (with duplicates; deduped below).
    let mut row_cols: Vec<Vec<usize>> = vec![Vec::new(); nrows];
    // Per block (in `subs` order), its global entries as `(r, c)` in the exact
    // order the numeric scatter emits them — kept so the second pass can map
    // each to its CSR slot once, and cache the result on the pattern.
    enum BlockEntries {
        /// One `(r, c)` list per cell (computed block).
        Computed(Vec<Vec<(usize, usize)>>),
        /// One `(r, c)` per COO entry (literal block).
        Literal(Vec<(usize, usize)>),
    }
    let mut block_entries: Vec<BlockEntries> = Vec::new();
    for blk_h in k {
        let blk = blk_h.read();
        let trow: Vec<usize> = blk.row_dofs().iter().map(|d| row_map[d]).collect();
        let tcol: Vec<usize> = blk.col_dofs().iter().map(|d| col_map[d]).collect();
        match blk.recipe() {
            Some(recipe) => {
                // A non-empty `col_fespaces` marks an inter-mesh block: rows and
                // columns are walked on two facing connectivities instead of one.
                let (_, _, per_cell) = match recipe.col_fespaces.first() {
                    Some(col_fe) => kernel::coupling_block_pattern(
                        &recipe.fespaces[0],
                        col_fe,
                        blk.row_support(),
                        blk.col_support(),
                        blk.dual_vars().len(),
                        blk.primal_vars().len(),
                        blk.ordering(),
                    )?,
                    None => kernel::element_block_pattern(
                        &recipe.fespaces[0],
                        blk.row_support(),
                        blk.col_support(),
                        blk.dual_vars().len(),
                        blk.primal_vars().len(),
                        blk.ordering(),
                    )?,
                };
                let cells: Vec<Vec<(usize, usize)>> = per_cell
                    .iter()
                    .map(|cell| {
                        cell.iter()
                            .map(|&(ri, ci)| {
                                let (gr, gc) = (trow[ri], tcol[ci]);
                                row_cols[gr].push(gc);
                                (gr, gc)
                            })
                            .collect()
                    })
                    .collect();
                block_entries.push(BlockEntries::Computed(cells));
            }
            None => {
                let (lr, lc, _) = blk.local_coo_arrays();
                let entries: Vec<(usize, usize)> = (0..lr.len())
                    .map(|k| {
                        let (gr, gc) = (trow[lr[k]], tcol[lc[k]]);
                        row_cols[gr].push(gc);
                        (gr, gc)
                    })
                    .collect();
                block_entries.push(BlockEntries::Literal(entries));
            }
        }
    }

    // Sort + dedup each row's columns independently → parallel across rows.
    row_cols
        .par_iter_mut()
        .with_min_len(MIN_PARALLEL_LEN)
        .for_each(|cols| {
            cols.sort_unstable();
            cols.dedup();
        });
    // Concatenate into the CSR arrays (serial: O(nnz) appends + prefix sum).
    let mut row_offsets = vec![0usize; nrows + 1];
    let mut col_indices: Vec<usize> = Vec::new();
    for r in 0..nrows {
        col_indices.extend_from_slice(&row_cols[r]);
        row_offsets[r + 1] = col_indices.len();
    }

    let mut pattern = AssemblyPattern {
        row_dofs,
        col_dofs,
        row_offsets,
        col_indices,
        block_slots: Vec::new(),
    };

    // Second pass: map every block entry to its CSR value slot once, so the
    // numeric scatter reads slots directly instead of binary-searching per
    // entry on every assembly. Parallel across blocks (independent output).
    // No `with_min_len` here: an item is a whole block (all its cells, all
    // their entries), so blocks are few and each is heavy — the grain policy
    // counts leaf items and would serialise this loop outright.
    pattern.block_slots = block_entries
        .into_par_iter()
        .map(|be| match be {
            BlockEntries::Computed(cells) => BlockSlots::Computed(
                cells
                    .into_iter()
                    .map(|cell| cell.iter().map(|&(r, c)| pattern.slot(r, c)).collect())
                    .collect(),
            ),
            BlockEntries::Literal(entries) => {
                BlockSlots::Literal(entries.iter().map(|&(r, c)| pattern.slot(r, c)).collect())
            }
        })
        .collect();

    Ok(pattern)
}

/// Assemble `k` into a CSR by scattering each block's contribution into
/// `pattern`'s value slots, **serially in block order** (and, within a block,
/// in cell / COO order). Because every slot accumulates in the same order the
/// triplet stream had, the result is bit-for-bit identical to the triplet path.
/// This is the reference numeric phase; the colour-parallel scatter reuses the
/// same pattern and slots.
///
/// ```
/// # 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::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::MatrixKind;
/// # use pyrucast::ops::{element_field, matrix, mesh, scatter};
/// # 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 modele = Model::heat_conduction(&fes).unwrap();
/// # let materiaux = element_field::material_field(&modele,
/// #     &[("k", 1.0), ("rho", 2.0), ("cp", 3.0)]).unwrap();
/// // Le motif est **matériau-indépendant** : il se bâtit une fois, puis
/// // les deux phases numériques le remplissent — la série, référence
/// // bit-à-bit, et la parallèle par coloriage.
/// let k = matrix::stiffness(&modele, &materiaux)?;
/// let motif = scatter::build_pattern(&k)?;
/// let serie = scatter::scatter_serial(&k, &motif)?;
/// let para = scatter::scatter_parallel(&k, &motif)?;
/// assert_eq!(serie.nnz(), motif.nnz());
/// assert_eq!(para.nnz(), serie.nnz());
/// // Mêmes valeurs, à l'ordre de sommation des couleurs près.
/// for (a, b) in serie.values().iter().zip(para.values()) {
///     assert!((a - b).abs() < 1e-12);
/// }
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn scatter_serial(k: &Matrix, pattern: &AssemblyPattern) -> Result<CsrMatrix<f64>> {
    let nrows = pattern.row_dofs.len();
    let ncols = pattern.col_dofs.len();

    let mut values = vec![0.0f64; pattern.nnz()];
    for (bi, blk_h) in k.into_iter().enumerate() {
        let blk = blk_h.read();
        match blk.recipe() {
            Some(recipe) => {
                // Read the sub-model once (not per cell) so the element kernel
                // stays lock-free while it runs in parallel over cells.
                let sm = recipe.submodel.read();
                let phys = sm.as_kind();
                // Per-cell triplets so the value stream lines up cell-for-cell
                // with the precomputed slots (same `(li,di,lj,pj)` order).
                let (_, _, per_cell) = if recipe.col_fespaces.is_empty() {
                    kernel::element_block_triplets_per_cell(
                        &recipe.fespaces,
                        blk.row_support(),
                        blk.col_support(),
                        blk.dual_vars().len(),
                        blk.primal_vars().len(),
                        blk.ordering(),
                        recipe.material.as_ref(),
                        recipe.state.as_ref(),
                        |geoms, m, state, ke| phys.matrix_element(recipe.kind, geoms, m, state, ke),
                    )?
                } else {
                    kernel::coupling_block_triplets_per_cell(
                        &recipe.fespaces,
                        &recipe.col_fespaces,
                        blk.row_support(),
                        blk.col_support(),
                        blk.dual_vars().len(),
                        blk.primal_vars().len(),
                        blk.ordering(),
                        recipe.material.as_ref(),
                        |row_geoms, col_geoms, m, ke| {
                            phys.coupling_element(recipe.kind, row_geoms, col_geoms, m, ke)
                        },
                    )?
                };
                let slots = match &pattern.block_slots[bi] {
                    BlockSlots::Computed(s) => s,
                    BlockSlots::Literal(_) => {
                        return Err(PyrucastError::Message(
                            "scatter_serial: pattern/block kind mismatch (computed block)".into(),
                        ))
                    }
                };
                let factor = blk.factor();
                for (cell, cell_slots) in per_cell.iter().zip(slots) {
                    for (&(_, _, v), &slot) in cell.iter().zip(cell_slots) {
                        values[slot] += v * factor;
                    }
                }
            }
            None => {
                let (_, _, lv) = blk.local_coo_arrays();
                let factor = blk.factor();
                let slots = match &pattern.block_slots[bi] {
                    BlockSlots::Literal(s) => s,
                    BlockSlots::Computed(_) => {
                        return Err(PyrucastError::Message(
                            "scatter_serial: pattern/block kind mismatch (literal block)".into(),
                        ))
                    }
                };
                for (&slot, &v) in slots.iter().zip(lv) {
                    values[slot] += v * factor;
                }
            }
        }
    }

    CsrMatrix::try_from_csr_data(
        nrows,
        ncols,
        pattern.row_offsets.clone(),
        pattern.col_indices.clone(),
        values,
    )
    .map_err(|e| PyrucastError::Message(format!("scatter_serial: invalid CSR: {e}")))
}

/// Assemble `k` into a CSR by scattering each block's contribution into
/// `pattern`'s value slots **in parallel**, colour by colour. A computed
/// block's element matrices are evaluated in parallel
/// ([`kernel::element_block_triplets_per_cell`]); then, for each colour of the
/// block's cell colouring (cached on its FE subspace), that colour's cells —
/// which touch pairwise-disjoint DOFs — scatter concurrently into disjoint
/// slots. Literal blocks scatter serially. The colouring is deterministic, so
/// the assembled values are reproducible regardless of thread count (though the
/// per-slot summation order differs from the serial path, hence not bit-for-bit
/// with it).
///
/// ```
/// # 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::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # use pyrucast::models::MatrixKind;
/// # use pyrucast::ops::{element_field, matrix, mesh, scatter};
/// # 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 modele = Model::heat_conduction(&fes).unwrap();
/// # let materiaux = element_field::material_field(&modele,
/// #     &[("k", 1.0), ("rho", 2.0), ("cp", 3.0)]).unwrap();
/// // Le motif est **matériau-indépendant** : il se bâtit une fois, puis
/// // les deux phases numériques le remplissent — la série, référence
/// // bit-à-bit, et la parallèle par coloriage.
/// let k = matrix::stiffness(&modele, &materiaux)?;
/// let motif = scatter::build_pattern(&k)?;
/// let serie = scatter::scatter_serial(&k, &motif)?;
/// let para = scatter::scatter_parallel(&k, &motif)?;
/// assert_eq!(serie.nnz(), motif.nnz());
/// assert_eq!(para.nnz(), serie.nnz());
/// // Mêmes valeurs, à l'ordre de sommation des couleurs près.
/// for (a, b) in serie.values().iter().zip(para.values()) {
///     assert!((a - b).abs() < 1e-12);
/// }
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub fn scatter_parallel(k: &Matrix, pattern: &AssemblyPattern) -> Result<CsrMatrix<f64>> {
    let nrows = pattern.row_dofs.len();
    let ncols = pattern.col_dofs.len();

    // f64 values held as bits so the colour-parallel scatter can write them
    // through shared references (see `add_atomic`).
    let values: Vec<AtomicU64> = (0..pattern.nnz()).map(|_| AtomicU64::new(0)).collect();

    for (bi, blk_h) in k.into_iter().enumerate() {
        let blk = blk_h.read();
        match blk.recipe() {
            Some(recipe) => {
                let sm = recipe.submodel.read();
                let phys = sm.as_kind();
                // Element matrices, evaluated in parallel, one triplet list per
                // cell (grouping needed for the colour-driven scatter).
                let (_, _, per_cell) = if recipe.col_fespaces.is_empty() {
                    kernel::element_block_triplets_per_cell(
                        &recipe.fespaces,
                        blk.row_support(),
                        blk.col_support(),
                        blk.dual_vars().len(),
                        blk.primal_vars().len(),
                        blk.ordering(),
                        recipe.material.as_ref(),
                        recipe.state.as_ref(),
                        |geoms, m, state, ke| phys.matrix_element(recipe.kind, geoms, m, state, ke),
                    )?
                } else {
                    kernel::coupling_block_triplets_per_cell(
                        &recipe.fespaces,
                        &recipe.col_fespaces,
                        blk.row_support(),
                        blk.col_support(),
                        blk.dual_vars().len(),
                        blk.primal_vars().len(),
                        blk.ordering(),
                        recipe.material.as_ref(),
                        |row_geoms, col_geoms, m, ke| {
                            phys.coupling_element(recipe.kind, row_geoms, col_geoms, m, ke)
                        },
                    )?
                };
                let slots = match &pattern.block_slots[bi] {
                    BlockSlots::Computed(s) => s,
                    BlockSlots::Literal(_) => {
                        return Err(PyrucastError::Message(
                            "scatter_parallel: pattern/block kind mismatch (computed block)".into(),
                        ))
                    }
                };

                let factor = blk.factor();
                if recipe.col_fespaces.is_empty() {
                    // Cell colouring (cached on the primary FE subspace): two cells
                    // sharing a node conflict, so one colour's cells touch disjoint DOFs.
                    let fe = recipe.fespaces[0].read();
                    let submesh = fe.submesh();
                    let submesh_g = submesh.read();
                    let conn = submesh_g.connectivity();
                    let n_cells = fe.cell_count()?;
                    let keys_per_cell = conn.len().checked_div(n_cells).unwrap_or(0);
                    let coloring =
                        fe.coloring(|| coloring::greedy_color(n_cells, keys_per_cell, conn));

                    // Scatter colour by colour: within a colour, cells write disjoint
                    // slots ⇒ the parallel atomic stores never race. Slots are
                    // precomputed (indexed cell-for-cell, entry-for-entry).
                    for color in coloring {
                        color
                            .par_iter()
                            .with_min_len(MIN_PARALLEL_LEN)
                            .for_each(|&cell| {
                                for (&(_, _, v), &slot) in per_cell[cell].iter().zip(&slots[cell]) {
                                    add_atomic(&values[slot], v * factor);
                                }
                            });
                    }
                } else {
                    // An inter-mesh block writes rows on one mesh and columns on
                    // another: a colouring of *one* connectivity no longer proves
                    // the slots disjoint. Its element matrices are still evaluated
                    // in parallel above; only the scatter is serial, as for a
                    // literal block. An interface carries a boundary mesh's worth
                    // of cells, so this costs nothing worth a two-sided colouring.
                    for (cell_trips, cell_slots) in per_cell.iter().zip(slots) {
                        for (&(_, _, v), &slot) in cell_trips.iter().zip(cell_slots) {
                            add_atomic(&values[slot], v * factor);
                        }
                    }
                }
            }
            None => {
                let (_, _, lv) = blk.local_coo_arrays();
                let factor = blk.factor();
                let slots = match &pattern.block_slots[bi] {
                    BlockSlots::Literal(s) => s,
                    BlockSlots::Computed(_) => {
                        return Err(PyrucastError::Message(
                            "scatter_parallel: pattern/block kind mismatch (literal block)".into(),
                        ))
                    }
                };
                for (&slot, &v) in slots.iter().zip(lv) {
                    add_atomic(&values[slot], v * factor);
                }
            }
        }
    }

    let vals: Vec<f64> = values
        .into_iter()
        .map(|a| f64::from_bits(a.into_inner()))
        .collect();
    CsrMatrix::try_from_csr_data(
        nrows,
        ncols,
        pattern.row_offsets.clone(),
        pattern.col_indices.clone(),
        vals,
    )
    .map_err(|e| PyrucastError::Message(format!("scatter_parallel: invalid CSR: {e}")))
}