pyrucast 0.13.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
//! Parallel **scaling** harness — measures the speedup of the hot FE paths
//! (per-element assembly and per-Gauss-point behaviour integration) as the
//! thread count grows.
//!
//! Self-contained and portable: it drives the thread count **itself** (one
//! rayon pool per measurement) so a single run reports the whole curve. No need
//! to juggle `RAYON_NUM_THREADS`.
//!
//! ```text
//! cargo run --release --bin scaling [n] [reps]
//!   n     grid size → n×n QUA4 cells          (default 60)
//!   reps  timed repetitions per thread count  (default 20)
//!
//! cargo run --release --bin scaling cube [n] [reps]
//!   n     grid size → n×n×n HEX8 cells, 3-D elasticity (default 20)
//!   reps  timed repetitions of the hot assembly            (default 5)
//! ```
//!
//! Output: for each operation, a table `threads | time | speedup | efficiency`,
//! with speedup/efficiency relative to the 1-thread run. Build in **release**
//! (`--release`) — a debug build's numbers are meaningless.
//!
//! The `cube` mode answers a different question: not « does it scale across
//! cores » but « where does one assembly spend its time and its bytes ». It
//! walks the 3-D path phase by phase — material field, union of zones, cold
//! assembly (symbolic + numeric), hot assembly (sparsity cached) — and prints,
//! beside each timing, the memory each intermediate of the assembler costs at
//! that size. It is the before/after reference for the assembly work.

use pyrucast::ops::{mesh, model};
use std::hint::black_box;
use std::time::{Duration, Instant};

use pyrucast::aggregate::Aggregate;
use pyrucast::atoms::{ElementType, Node, NodeId};
use pyrucast::containers::element_field::ElementField;
use pyrucast::containers::finite_element_space::FiniteElementSpace;
use pyrucast::containers::mesh::{Mesh, SubMesh};
use pyrucast::containers::model::Model;
use pyrucast::containers::node_field::{NodeField, SubNodeField};
use pyrucast::coords::Coords;
use pyrucast::handle::Handle;
use pyrucast::models::symmetry::MaterialSymmetry;
use pyrucast::models::tensor::Kinematics;
use pyrucast::ops::element_field::material_field;
use pyrucast::ops::element_field::StrainMeasure;
use pyrucast::ops::{element_field, matrix};

/// Plane-stress elasticity on an `n × n` QUA4 grid: model, material field, and a
/// strain field ready for `element_field::behavior::integrate`.
fn build(n: usize) -> (Model, ElementField, ElementField) {
    let coords = Handle::new(Coords::new(2).unwrap());
    let mut ids: Vec<NodeId> = Vec::with_capacity((n + 1) * (n + 1));
    for j in 0..=n {
        for i in 0..=n {
            ids.push(
                Node::create_in(coords.clone(), &[i as f64, j as f64])
                    .unwrap()
                    .id(),
            );
        }
    }
    let at = |i: usize, j: usize| ids[j * (n + 1) + i];

    let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::QUA4));
    for j in 0..n {
        for i in 0..n {
            mesh.add_cell(&[at(i, j), at(i + 1, j), at(i + 1, j + 1), at(i, j + 1)])
                .unwrap();
        }
    }
    let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    let model = model::elasticity(
        &fes,
        Kinematics::PlaneStress,
        MaterialSymmetry::Isotropic,
        StrainMeasure::Linear,
    )
    .unwrap();
    let materials = material_field(&model, &[("E", 210e9), ("nu", 0.3)]).unwrap();

    let support = Handle::new(SubMesh::poi1_from_node_ids(coords.clone(), &ids).unwrap());
    let mut u = SubNodeField::from_poi1(&support, vec!["u_x".into(), "u_y".into()]).unwrap();
    for j in 0..=n {
        for i in 0..=n {
            let nid = at(i, j);
            u.set_value(nid, "u_x", 0.01 * i as f64).unwrap();
            u.set_value(nid, "u_y", -0.005 * j as f64).unwrap();
        }
    }
    let strain =
        element_field::deformation(&NodeField::from_sub(u), &fes, StrainMeasure::Linear).unwrap();
    (model, materials, strain)
}

/// Median wall time of `reps` runs of `f` executed on a `threads`-wide rayon
/// pool (one warm-up run first).
fn measure(threads: usize, reps: u32, f: &(dyn Fn() + Sync)) -> Duration {
    let pool = rayon::ThreadPoolBuilder::new()
        .num_threads(threads)
        .build()
        .unwrap();
    pool.install(f); // warm-up
    let start = Instant::now();
    pool.install(|| {
        for _ in 0..reps {
            f();
        }
    });
    start.elapsed() / reps
}

/// Thread counts to sweep: powers of two up to `max`, plus `max` itself.
fn thread_counts(max: usize) -> Vec<usize> {
    let mut out = Vec::new();
    let mut t = 1;
    while t < max {
        out.push(t);
        t *= 2;
    }
    out.push(max);
    out.dedup();
    out
}

fn run_op(name: &str, threads: &[usize], reps: u32, f: &(dyn Fn() + Sync)) {
    println!("── {name} ──");
    println!("threads        time     speedup   efficiency");
    let mut base: Option<f64> = None;
    for &t in threads {
        let secs = measure(t, reps, f).as_secs_f64();
        let b = *base.get_or_insert(secs);
        let speedup = b / secs;
        let eff = 100.0 * speedup / t as f64;
        println!(
            "{t:>5}   {:>9.3} ms   {:>5.2}x      {:>5.1}%",
            secs * 1e3,
            speedup,
            eff
        );
    }
    println!();
}

// ─── Cube mode: where one 3-D assembly spends its time and its bytes ────────

/// `b` bytes in the largest unit that keeps it readable.
fn bytes(b: usize) -> String {
    const UNITS: [&str; 5] = ["B", "kB", "MB", "GB", "TB"];
    let mut v = b as f64;
    let mut u = 0;
    while v >= 1024.0 && u + 1 < UNITS.len() {
        v /= 1024.0;
        u += 1;
    }
    format!("{v:>7.1} {}", UNITS[u])
}

/// Full 3-D elasticity on an `n × n × n` HEX8 cube: model + uniform material.
///
/// One zone, one computed block — the shape a solid mesh actually assembles in,
/// and the one whose intermediates blow up with the cell count.
fn build_cube(n: usize) -> (Model, ElementField, FiniteElementSpace) {
    let coords = Handle::new(Coords::new(3).unwrap());
    let side = n + 1;
    let mut ids: Vec<NodeId> = Vec::with_capacity(side * side * side);
    for k in 0..=n {
        for j in 0..=n {
            for i in 0..=n {
                ids.push(
                    Node::create_in(coords.clone(), &[i as f64, j as f64, k as f64])
                        .unwrap()
                        .id(),
                );
            }
        }
    }
    let at = |i: usize, j: usize, k: usize| ids[(k * side + j) * side + i];

    // HEX8: bottom face CCW (0..3), then the matching top face (4..7).
    let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::HEX8));
    for k in 0..n {
        for j in 0..n {
            for i in 0..n {
                mesh.add_cell(&[
                    at(i, j, k),
                    at(i + 1, j, k),
                    at(i + 1, j + 1, k),
                    at(i, j + 1, k),
                    at(i, j, k + 1),
                    at(i + 1, j, k + 1),
                    at(i + 1, j + 1, k + 1),
                    at(i, j + 1, k + 1),
                ])
                .unwrap();
            }
        }
    }
    let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
    let model = model::elasticity(
        &fes,
        Kinematics::Full3D,
        MaterialSymmetry::Isotropic,
        StrainMeasure::Linear,
    )
    .unwrap();
    let materials = material_field(&model, &[("E", 210e9), ("nu", 0.3)]).unwrap();
    (model, materials, fes)
}

/// Run `f` once, print its wall time under `name`, and hand back its result.
fn phase<T>(name: &str, f: impl FnOnce() -> T) -> T {
    let start = Instant::now();
    let out = f();
    println!(
        "{name:<34} {:>10.3} ms",
        start.elapsed().as_secs_f64() * 1e3
    );
    out
}

/// Chain `zones` single-cell material fields with `|`, the way a multi-material
/// model is composed. Quadratic in the zone count today, which is the point of
/// measuring it apart from everything else.
fn union_chain(zones: usize) -> usize {
    let coords = Handle::new(Coords::new(3).unwrap());
    let mut fields: Vec<ElementField> = Vec::with_capacity(zones);
    for z in 0..zones {
        let o = z as f64;
        let n: Vec<NodeId> = [
            [o, 0.0, 0.0],
            [o + 1.0, 0.0, 0.0],
            [o, 1.0, 0.0],
            [o, 0.0, 1.0],
        ]
        .iter()
        .map(|p| Node::create_in(coords.clone(), p).unwrap().id())
        .collect();
        let mut sm = SubMesh::new(coords.clone(), ElementType::TET4);
        sm.add_cell(&n).unwrap();
        let fes = FiniteElementSpace::lagrange1(&Mesh::from_submesh(sm)).unwrap();
        let m = model::elasticity(
            &fes,
            Kinematics::Full3D,
            MaterialSymmetry::Isotropic,
            StrainMeasure::Linear,
        )
        .unwrap();
        fields.push(material_field(&m, &[("E", 210e9), ("nu", 0.3)]).unwrap());
    }
    let mut acc = ElementField::empty();
    for f in &fields {
        acc = acc.union(f).unwrap();
    }
    acc.len()
}

/// Walk one 3-D assembly phase by phase, then print what each intermediate of
/// the assembler costs in memory at this size.
fn run_cube(n: usize, reps: u32) {
    let cells = n * n * n;
    let nodes = (n + 1) * (n + 1) * (n + 1);
    let dofs = 3 * nodes;
    let ke_len = 24 * 24; // HEX8 × 3 ddl
    let max = std::thread::available_parallelism()
        .map(|p| p.get())
        .unwrap_or(8);
    println!(
        "pyrucast scaling — cube {n}×{n}×{n} HEX8, élasticité 3-D \
         ({cells} mailles, {dofs} ddl), reps={reps}, cores={max}\n"
    );

    println!("── phases ──");
    let (model, materials, fespace) = phase("build_cube (maillage + EF)", || build_cube(n));
    let mat_again = phase("element_field::material_field", || {
        material_field(&model, &[("E", 210e9), ("nu", 0.3)]).unwrap()
    });
    black_box(&mat_again);
    const ZONES: usize = 256;
    let fused = phase("union en chaîne (256 zones)", || union_chain(ZONES));
    debug_assert_eq!(fused, ZONES);

    // Two component-disjoint material zones on one support, then fused: the
    // path a multi-physics material takes, and the one that walks every Gauss
    // point of the mesh.
    let deux = ElementField::new(&fespace, vec!["E".into(), "nu".into()])
        .unwrap()
        .union(&ElementField::new(&fespace, vec!["rho".into()]).unwrap())
        .unwrap();
    let une = phase("element_field::consolidate", || {
        element_field::consolidate(&deux).unwrap()
    });
    debug_assert_eq!(une.len(), 1);

    // The nodal counterpart of the union. Two zones with disjoint components on
    // one support: the merge, which walks every node. Then the same union with a
    // distinct support: there is nothing to merge, and this phase measures that
    // nothing is copied either.
    let support = fespace.get(0).unwrap().read().submesh();
    let coords = support.read().coords();
    let depl = NodeField::from_sub(
        SubNodeField::from_support(&support, vec!["UX".into(), "UY".into(), "UZ".into()]).unwrap(),
    );
    let temp = NodeField::from_sub(SubNodeField::from_support(&support, vec!["T".into()]).unwrap());
    let fusion = phase("node_field union (même support)", || {
        depl.union(&temp).unwrap()
    });
    debug_assert_eq!(fusion.len(), 1);

    let ailleurs = {
        let loin: Vec<Node> = (0..8)
            .map(|i| Node::create_in(coords.clone(), &[-1.0 - i as f64, -1.0, -1.0]).unwrap())
            .collect();
        let poi1 = mesh::poi1_from_nodes(&loin).unwrap().get(0).unwrap();
        NodeField::from_sub(SubNodeField::from_poi1(&poi1, vec!["T".into()]).unwrap())
    };
    let separees = phase("node_field union (2 supports)", || {
        depl.union(&ailleurs).unwrap()
    });
    debug_assert_eq!(separees.len(), 2);

    // The first assembly pays the symbolic phase (DOF numbering + sparsity);
    // every later one reuses the pattern memoised on the model.
    let k = phase("matrix::stiffness (à froid)", || {
        matrix::stiffness(&model, &materials).unwrap()
    });
    let nnz = k.to_csr().unwrap().nnz();
    drop(k);

    let start = Instant::now();
    for _ in 0..reps {
        black_box(matrix::stiffness(&model, &materials).unwrap());
    }
    let hot = start.elapsed().as_secs_f64() / reps as f64;
    println!(
        "{:<34} {:>10.3} ms",
        "matrix::stiffness (à chaud)",
        hot * 1e3
    );

    println!("\n── mémoire des intermédiaires (nnz = {nnz}) ──");
    let row = |name: &str, b: usize| println!("{name:<34} {}", bytes(b));
    // What the assembler really allocates today.
    row("motif : position des nœuds", 2 * cells * 8 * 4);
    row("motif : colonnes avant dédup", cells * ke_len * 4);
    row("block_slots (bases par nœud)", cells * (8 * 3 * 8) * 4);
    row("CSR : valeurs", nnz * 8);
    row("CSR : col_indices", nnz * std::mem::size_of::<usize>());
    row("tampon atomique du scatter", nnz * 8);
    // What it used to allocate, for scale.
    println!(
        "\n   (avant : paires (r,c) {} ; block_slots {} ; ke toutes matérialisées {})",
        bytes(cells * ke_len * std::mem::size_of::<(usize, usize)>()),
        bytes(cells * ke_len * std::mem::size_of::<usize>()),
        bytes(cells * ke_len * 8)
    );

    println!(
        "\nNote : « à froid » inclut la numérotation des ddl et le motif de \
         sparsité ; « à chaud » réutilise le motif mémoïsé sur le modèle et ne \
         mesure donc que la phase numérique."
    );
}

fn main() {
    let args: Vec<String> = std::env::args().collect();
    if args.get(1).map(String::as_str) == Some("cube") {
        let n: usize = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(20);
        let reps: u32 = args.get(3).and_then(|s| s.parse().ok()).unwrap_or(5);
        run_cube(n, reps);
        return;
    }
    let n: usize = args.get(1).and_then(|s| s.parse().ok()).unwrap_or(60);
    let reps: u32 = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(20);
    let max = std::thread::available_parallelism()
        .map(|p| p.get())
        .unwrap_or(8);
    let threads = thread_counts(max);

    let (model, materials, strain) = build(n);
    let cells = n * n;
    let dofs = 2 * (n + 1) * (n + 1);
    println!(
        "pyrucast scaling — {n}×{n} QUA4 ({cells} cells, {dofs} dofs), \
         reps={reps}, cores={max}\n"
    );

    let stiffness = {
        let model = &model;
        let materials = &materials;
        move || {
            black_box(matrix::stiffness(model, materials).unwrap());
        }
    };
    run_op("matrix::stiffness", &threads, reps, &stiffness);

    let integrate = {
        let model = &model;
        let materials = &materials;
        let strain = &strain;
        move || {
            black_box(
                element_field::behavior::integrate(
                    model,
                    strain,
                    &element_field::behavior::initial_state(model, materials).unwrap(),
                    materials,
                    None,
                )
                .unwrap(),
            );
        }
    };
    run_op(
        "element_field::behavior::integrate",
        &threads,
        reps,
        &integrate,
    );

    println!(
        "Note: speedup is vs the 1-thread run. Both ops are colour-parallel. \
         matrix::stiffness memoises its CSR sparsity on the model (built once, \
         reused here across reps), so the timed path is the parallel scatter; a \
         first-ever assembly also pays the one-off symbolic build."
    );
}