pyrucast 0.11.0

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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! Python wrappers for [`crate::ops::element_field`] — the operators that
//! produce an `ElementField`.

use crate::handle::Handle;
use crate::ops::element_field::{StrainMeasure, StressMeasure};
use crate::py::element_field::{PyElementField, PySubElementField};
use crate::py::finite_element_space::PyFiniteElementSpace;
use crate::py::model::{PyModel, PySubModel};
use crate::py::node_field::PyNodeField;
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
use pyrucast_macros::py_op;

/// Fuse the zones of an element `field` sharing the same `FiniteElementSpace`
/// support into a single zone carrying the union of their components.
///
/// The counterpart of `|`, which leaves component-disjoint zones side by side:
/// this is how per-physics material zones built on one shared fespace become a
/// single material field readable by every physics. Components carried by two
/// zones must agree value by value, else it errors. `field` itself is left
/// untouched.
#[py_op(method_on = PyElementField)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(name = "consolidate_element")]
pub fn consolidate(field: PyRef<PyElementField>) -> PyResult<PyElementField> {
    Ok(PyElementField {
        inner: crate::ops::element_field::consolidate(&field.inner)?,
    })
}

/// Gradient `∇f` of a node `field` at the Gauss points of `fespace`.
///
/// Geometric and physics-agnostic: each component of `field` is
/// differentiated w.r.t. every spatial axis, giving an `ElementField` with
/// one component `grad_<name>_<axis>` per (input component, axis) pair
/// (`grad_T_x`, …). Feed the result to `integrate_behavior`.
#[py_op(method_on = PyNodeField)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn gradient(
    field: PyRef<PyNodeField>,
    fespace: PyRef<PyFiniteElementSpace>,
) -> PyResult<PyElementField> {
    let ef = crate::ops::element_field::gradient(&field.inner, &fespace.inner)?;
    Ok(PyElementField { inner: ef })
}

/// Deformation of a displacement field `u` at the Gauss points of `fespace`.
///
/// `u` must carry exactly `space_dim` components, taken in order as the
/// displacement along x, y, z. `measure` chooses the deformation, with
/// `H = ∇u` and `F = I + H`:
///
/// | measure | formula | components |
/// |---|---|---|
/// | `"linear"` (default) | `ε = ½(H + Hᵀ)` | `eps_xx`, `eps_xy`, … (tensor convention) |
/// | `"gradient"` (`"F"`) | `F = I + H` | every `F_ij`, row-major |
/// | `"green_lagrange"` | `E = ½(FᵀF − I)` | `E_xx`, `E_xy`, … |
/// | `"right_cauchy_green"` (`"C"`) | `C = FᵀF` | `C_xx`, … |
/// | `"left_cauchy_green"` (`"b"`) | `b = FFᵀ` | `b_xx`, … |
/// | `"hencky"` (`"log"`) | `½ ln C` | `H_xx`, … |
/// | `"euler_almansi"` (`"almansi"`) | `e = ½(I − b⁻¹)` | `e_xx`, … |
///
/// On an axisymmetric space, the hoop entry `<prefix>_zz` is appended
/// (`H_θθ = u_r / r`).
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (u, fespace, measure=StrainMeasure::Linear))]
pub fn deformation(
    u: PyRef<PyNodeField>,
    fespace: PyRef<PyFiniteElementSpace>,
    measure: StrainMeasure,
) -> PyResult<PyElementField> {
    let ef = crate::ops::element_field::deformation(&u.inner, &fespace.inner, measure)?;
    Ok(PyElementField { inner: ef })
}

/// Interpolate a nodal `field` to the Gauss points of `fespace`
/// (`f(ξ_g) = Σ_i f_i N_i(ξ_g)`), turning a per-node `NodeField` into a
/// per-element `ElementField` with the same component names. Cast3M `CHAN`
/// (nodes → Gauss).
#[py_op(method_on = PyNodeField)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn interp_to_gauss(
    field: PyRef<PyNodeField>,
    fespace: PyRef<PyFiniteElementSpace>,
) -> PyResult<PyElementField> {
    let ef = crate::ops::element_field::interp_to_gauss(&field.inner, &fespace.inner)?;
    Ok(PyElementField { inner: ef })
}

/// Thermal (free-dilation) strain `ε_th = α·(T − t_ref)` at the Gauss points of
/// `fespace` — Cast3M `EPTH`. `temperature` is a per-element field carrying
/// `"T"` (e.g. from `interp_to_gauss`); `materials` carries `"alpha"` (supplied
/// via `material_field`). Returns the strain tensor in the same layout as
/// `deformation`, so `deformation(u, fespace) - thermal_strain(...)` is the
/// mechanical strain. Backbone of uncoupled thermomechanics: assemble the
/// thermal load with `internal_forces(integrate_behavior(model, ε_th,
/// materials))` and recover `σ = D:(ε − ε_th)`.
///
/// `measure` is the strain the law reads (`"linear"`, `"green_lagrange"`,
/// `"hencky"`): the dilation of stretch `1 + α·ΔT` is given exactly in it —
/// `α·ΔT`, `½((1 + α·ΔT)² − 1)`, `ln(1 + α·ΔT)` — under its names (`eps_*`,
/// `E_*`, `H_*`).
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (temperature, materials, fespace, t_ref, measure=StrainMeasure::Linear))]
pub fn thermal_strain(
    temperature: PyRef<PyElementField>,
    materials: PyRef<PyElementField>,
    fespace: PyRef<PyFiniteElementSpace>,
    t_ref: f64,
    measure: StrainMeasure,
) -> PyResult<PyElementField> {
    let ef = crate::ops::element_field::thermal_strain(
        &temperature.inner,
        &materials.inner,
        &fespace.inner,
        t_ref,
        measure,
    )?;
    Ok(PyElementField { inner: ef })
}

/// `stress` converted to the measure `target`, point by point, with the
/// deformation gradient `gradient` — the field `deformation(u, fespace,
/// "gradient")` produces (`F_xx, F_xy, …`).
///
/// `target` is `"cauchy"` (`sigma_*`, the true stress), `"kirchhoff"`
/// (`tau_* = J sigma_*`), `"pk1"` (`P_*`, the nominal stress `F S`, not
/// symmetric), `"pk2"` (`S_*`, dual of Green–Lagrange) or `"hencky"` (`T_*`,
/// dual of the Hencky strain, what a law reading Hencky writes). The measure of
/// `stress` is recognised by its names, one per zone. This is how the
/// Lagrangian stress a law writes in large transformations becomes the first
/// Piola–Kirchhoff `internal_forces` reads, or the Cauchy stress of the
/// results — the stress half of Cast3M `CONF`.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn stress_measure(
    stress: PyRef<PyElementField>,
    gradient: PyRef<PyElementField>,
    target: StressMeasure,
) -> PyResult<PyElementField> {
    let ef = crate::ops::element_field::stress_measure(&stress.inner, &gradient.inner, target)?;
    Ok(PyElementField { inner: ef })
}

/// `field` with its material axes turned with the part: `V1*` (and `V2*` in
/// 3-D) turned by the rotation `R` of the polar decomposition `F = R U` at each
/// point, `F` read from `gradient` — the field `deformation(u, fespace,
/// "gradient")` produces, computed on the **reference** configuration. Every
/// other component is copied as it is: the material is the same, only its axes
/// follow the rotation, `k = R k₀ Rᵀ`. This is what a conduction or a diffusion
/// computed on the deformed part reads.
///
/// A zone without axes (an isotropic material) comes back as it is, shared. A
/// zone carrying part of the axes only, or a gradient missing on a zone's FE
/// space, is an error.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn rotate_frame(
    field: PyRef<PyElementField>,
    gradient: PyRef<PyElementField>,
) -> PyResult<PyElementField> {
    let ef = crate::ops::element_field::rotate_frame(&field.inner, &gradient.inner)?;
    Ok(PyElementField { inner: ef })
}

/// Elastic stress `σ = D:ε` of a strain field at the Gauss points of every
/// continuum sub-model of `model` — Cast3M `ELAS` applied to a strain.
///
/// `D` is the operator each law predicts with: the Hooke matrix of an
/// elasticity, in its symmetry; the isotropic `D` behind a plasticity's elastic
/// predictor; the **undamaged** `D` a damage law degrades. `strain` is in
/// tensor convention, the layout of `deformation` and `thermal_strain`;
/// `materials` needs only each law's elastic constants (`E`, `nu` when
/// isotropic). One zone per continuum sub-model, its components the stresses
/// the law writes; a sub-model with no elastic operator (conduction, bar, beam,
/// constraint) contributes none.
#[py_op(method_on = PyModel)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn elastic_stress(
    model: PyRef<PyModel>,
    strain: PyRef<PyElementField>,
    materials: PyRef<PyElementField>,
) -> PyResult<PyElementField> {
    let ef =
        crate::ops::element_field::elastic_stress(&model.inner, &strain.inner, &materials.inner)?;
    Ok(PyElementField { inner: ef })
}

/// Elastic strain `ε = D⁻¹:σ` of a stress field at the Gauss points of every
/// continuum sub-model of `model` — the inverse of `elastic_stress` (Cast3M
/// `ELAS` applied to a stress).
///
/// `stress` carries the stresses each law writes (`sigma_xx`, …; the state
/// returned by `integrate_behavior` will do); `materials` the elastic
/// constants. The result is in tensor convention, the layout of `deformation`,
/// so it subtracts from a strain component for component; in plane stress the
/// out-of-plane `eps_zz` is not written. Raises when `D` is singular at a point
/// (a zero modulus).
#[py_op(method_on = PyModel)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn elastic_strain(
    model: PyRef<PyModel>,
    stress: PyRef<PyElementField>,
    materials: PyRef<PyElementField>,
) -> PyResult<PyElementField> {
    let ef =
        crate::ops::element_field::elastic_strain(&model.inner, &stress.inner, &materials.inner)?;
    Ok(PyElementField { inner: ef })
}

/// The input of every law of `model`, produced from the nodal `solution` — the
/// model's `B`, of which `internal_forces` is the transpose (Cast3M `EPSI`,
/// extended to every physics).
///
/// One zone per sub-model with a law, on its behaviour space, carrying what
/// that law reads: the small-strain tensor `eps_*` of a continuum or a bar, the
/// gradient `grad_T_*` of conduction, `T` itself for radiation, the section
/// strains of a beam, the generalized strains of a shell. Each law reads its
/// own unknowns of `solution` by name, so one solution — displacement,
/// temperature, multipliers — serves the whole model; sub-models without a law
/// produce nothing. `materials` is read by the beams alone (their `Φ`). The
/// result feeds `integrate_behavior` as is.
#[py_op(method_on = PyModel)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn strain(
    model: PyRef<PyModel>,
    solution: PyRef<PyNodeField>,
    materials: PyRef<PyElementField>,
) -> PyResult<PyElementField> {
    let ef = crate::ops::element_field::strain(&model.inner, &solution.inner, &materials.inner)?;
    Ok(PyElementField { inner: ef })
}

/// Integral of every component of `field` over **each cell** (Cast3M
/// `INTG 'ELEM'`), with the field's own quadrature: `Σ_g f(g)·|J|·w_g`, the
/// `2πr` included on a body of revolution. The result lives on `target`, a
/// space of the same mesh with **one point per cell** —
/// `FiniteElementSpace(mesh, quadrature="REDUCED")`, a single point at each
/// centroid. Integrating `1` gives every cell's measure. Raises when `target`
/// has several points per cell or does not cover the field's mesh.
#[py_op(method_on = PyElementField)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn cell_integral(
    field: PyRef<PyElementField>,
    target: PyRef<PyFiniteElementSpace>,
) -> PyResult<PyElementField> {
    let ef = crate::ops::element_field::cell_integral(&field.inner, &target.inner)?;
    Ok(PyElementField { inner: ef })
}

/// Generalised **section strains** of a beam at the Gauss points, in whichever
/// configuration the mesh puts it — the geometric producer of the behaviour
/// input for `timoshenko` and `bernoulli`.
///
/// | `Coords` | DOFs read | components produced |
/// |---|---|---|
/// | 1-D | `w, theta` | `kappa, gamma` |
/// | 2-D | `u_x, u_y, r_z` | `eps, kappa, gamma` |
/// | 3-D | six | `eps, kappa_y, kappa_z, torsion, gamma_y, gamma_z` |
///
/// For an oriented element it builds the local axes, rotates the nodal triples
/// into them, then evaluates the strains from the local DOFs. This replaces
/// `frame_deformation`, which was the same operator for the 2-D and 3-D cases.
///
/// The **material** is required, and that is the honest signature: the
/// element's interpolation depends on `Φ = 12EI/(G·A_s·L²)`, so its curvature
/// distribution cannot be recovered without knowing the shear stiffness. An
/// operator that took no material could only ever report a mean.
///
/// Feed the result to `integrate_behavior` to obtain the section forces
/// (`N = E·A·eps`, `M = E·I·kappa`, `V = G·A_s·gamma`). The moment then varies
/// along the element, as `M' = V` requires, while the shear stays constant.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn beam_deformation(
    field: PyRef<PyNodeField>,
    fespace: PyRef<PyFiniteElementSpace>,
    material: PyRef<PyElementField>,
) -> PyResult<PyElementField> {
    let ef =
        crate::ops::element_field::beam_deformation(&field.inner, &fespace.inner, &material.inner)?;
    Ok(PyElementField { inner: ef })
}

/// Generalised **strains of a shell** at the Gauss points — the geometric
/// producer of the behaviour input for `shell`.
///
/// `shell_deformation(field, fespace, formulation)` with `formulation`
/// (`"thick"` or `"kirchhoff"`), which decides the bending rows and whether a
/// transverse shear is reported at all:
///
/// | formulation | components produced |
/// |---|---|
/// | `thick` | `eps_xx…eps_xy`, `kappa_xx…kappa_xy`, `drill`, `gamma_xz, gamma_yz` |
/// | `kirchhoff` | the same, without the two shear strains |
///
/// All in the element's **local** frame. The transverse shear is
/// element-constant, sampled at the reduced point the stiffness integrates it
/// at — anywhere else would report a strain the element does not carry.
///
/// Feed the result to `integrate_behavior` to obtain the generalised forces
/// (`N = D_m·eps`, `M = D_b·kappa`, `Q = D_s·gamma`, and the drilling moment).
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn shell_deformation(
    field: PyRef<PyNodeField>,
    fespace: PyRef<PyFiniteElementSpace>,
    formulation: &str,
) -> PyResult<PyElementField> {
    let formulation =
        <crate::models::shell::ShellFormulation as crate::named::Named>::parse(formulation)?;
    let ef =
        crate::ops::element_field::shell_deformation(&field.inner, &fespace.inner, formulation)?;
    Ok(PyElementField { inner: ef })
}

/// Build the material `SubElementField` of one sub-model.
///
/// `sub_material_field(sub_model, [("k", 1.0), ...])` — fresh
/// SubElementField on the sub-model's FE subspace, pre-filled with the
/// given uniform value per declared component. Errors for physics that
/// need no material (e.g. Dirichlet).
#[py_op(method_on = PySubModel, name = "material_field")]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn sub_material_field(
    sub_model: PyRef<PySubModel>,
    components_and_values: Vec<(String, f64)>,
) -> PyResult<PySubElementField> {
    let pairs: Vec<(&str, f64)> = components_and_values
        .iter()
        .map(|(c, v)| (c.as_str(), *v))
        .collect();
    let sub = crate::ops::element_field::sub_material_field(&sub_model.handle.read(), &pairs)?;
    Ok(PySubElementField {
        handle: Handle::new(sub),
    })
}

/// Build a material `ElementField` applying the same uniform
/// `(component, value)` pairs to every material-hungry sub-model of
/// `model`. Sub-models that need no material (Dirichlet, …) are skipped.
#[py_op(method_on = PyModel)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn material_field(
    model: PyRef<PyModel>,
    components_and_values: Vec<(String, f64)>,
) -> PyResult<PyElementField> {
    let pairs: Vec<(&str, f64)> = components_and_values
        .iter()
        .map(|(c, v)| (c.as_str(), *v))
        .collect();
    let ef = crate::ops::element_field::material_field(&model.inner, &pairs)?;
    Ok(PyElementField { inner: ef })
}

/// Build a material `ElementField` where each sub-model gets its own
/// `(component, value)` list. The outer list length must equal
/// `model.len()`. An empty inner list **skips** the matching
/// sub-model (typical for Dirichlet).
#[py_op(method_on = PyModel)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn material_field_per_sub_model(
    model: PyRef<PyModel>,
    components_and_values_per_sub_model: Vec<Vec<(String, f64)>>,
) -> PyResult<PyElementField> {
    // Materialise each inner Vec<(String, f64)> into a Vec<(&str, f64)>,
    // then collect slices into a Vec<&[(&str, f64)]>.
    let owned: Vec<Vec<(&str, f64)>> = components_and_values_per_sub_model
        .iter()
        .map(|v| v.iter().map(|(c, x)| (c.as_str(), *x)).collect())
        .collect();
    let slices: Vec<&[(&str, f64)]> = owned.iter().map(|v| v.as_slice()).collect();
    let ef = crate::ops::element_field::material_field_per_sub_model(&model.inner, &slices)?;
    Ok(PyElementField { inner: ef })
}

/// Integrate the constitutive law of `model` (Cast3m `COMP`), stepping A → B.
///
/// `deformation` is the **end-of-step** behaviour input ε(B) (from
/// `gradient(field, fespace)` or `deformation(u, fespace)`); `prev` is the
/// **converged output of the previous step** (the state at A — stress,
/// internal variables and start-of-step strain), or `None` on the first step;
/// `materials` supplies the per-zone material data; `dt` is the time increment
/// (`None` if the law is rate-independent). Returns the material-state field at
/// B (dual flux/stress + updated internal variables) of every behaviour-bearing
/// sub-model — feed it back as `prev` at the next step.
///
/// For a linear law the result is consistent with the assembled stiffness
/// (`∫ Bᵀ·flux = K·u`); a non-linear law is the exact response.
#[py_op(method_on = PyModel)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (model, deformation, materials, prev=None, dt=None))]
pub fn integrate_behavior(
    model: PyRef<PyModel>,
    deformation: PyRef<PyElementField>,
    materials: PyRef<PyElementField>,
    prev: Option<PyRef<PyElementField>>,
    dt: Option<f64>,
) -> PyResult<PyElementField> {
    let prev_inner = prev.as_ref().map(|p| &p.inner);
    let ef = crate::ops::element_field::behavior::integrate(
        &model.inner,
        &deformation.inner,
        prev_inner,
        &materials.inner,
        dt,
    )?;
    Ok(PyElementField { inner: ef })
}

/// Per-component 0/1 **mask** of a field against a value band — same structure
/// as the input (Cast3M `MASQUE`): same zones, same support, same components,
/// only the values are rewritten (`1.0` inside the band, `0.0` outside). The
/// result is therefore multipliable term by term with the input.
///
/// `field` is an `ElementField` or a `SubElementField`, and the result is of
/// the same kind. Its sibling `pyrucast.mesh.select` extracts the passing
/// *support* instead, and produces a `Mesh`.
///
/// The band is set by the four comparison bounds `ge` (`≥`), `gt` (`>`),
/// `le` (`≤`), `lt` (`<`). There is **no** AND across components: each value
/// stands on its own. `components=None` tests every component; a `components`
/// list tests only those, leaving the others at `1.0` (identity for the
/// product), and a zone missing a listed component is left all-`1.0`.
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(name = "mask_element")]
#[pyo3(signature = (field, ge=None, gt=None, le=None, lt=None, components=None))]
#[allow(clippy::too_many_arguments)]
pub fn mask(
    py: Python<'_>,
    field: &Bound<'_, PyAny>,
    ge: Option<f64>,
    gt: Option<f64>,
    le: Option<f64>,
    lt: Option<f64>,
    components: Option<Vec<String>>,
) -> PyResult<Py<PyAny>> {
    if let Ok(f) = field.extract::<PyRef<PyElementField>>() {
        Ok(Py::new(py, mask_element_field(f, ge, gt, le, lt, components)?)?.into_any())
    } else if let Ok(f) = field.extract::<PyRef<PySubElementField>>() {
        Ok(Py::new(py, mask_sub_element_field(f, ge, gt, le, lt, components)?)?.into_any())
    } else {
        Err(PyTypeError::new_err(
            "expected a PyElementField or PySubElementField",
        ))
    }
}

// The two flavours of `mask`: same setup as in `node_field` — unregistered, they
// serve only to give `#[py_op]` a method at the precise type, with its own
// documentation.

/// Per-component 0/1 **mask** of this field against a value band — same
/// structure as the field (Cast3M `MASQUE`): same zones, same support, same
/// components, same Gauss points, only the values are rewritten (`1.0` inside
/// the band, `0.0` outside). The result is therefore multipliable term by term
/// with the field.
///
/// Its sibling `select` extracts the passing *cells* instead, and produces a
/// `Mesh`.
///
/// The band is set by the four comparison bounds `ge` (`≥`), `gt` (`>`),
/// `le` (`≤`), `lt` (`<`). There is **no** AND across components: each value
/// stands on its own. `components=None` tests every component; a `components`
/// list tests only those, leaving the others at `1.0` (identity for the
/// product), and a zone missing a listed component is left all-`1.0`.
#[py_op(method_on = PyElementField, name = "mask")]
#[pyfunction]
#[pyo3(signature = (field, ge=None, gt=None, le=None, lt=None, components=None))]
pub fn mask_element_field(
    field: PyRef<PyElementField>,
    ge: Option<f64>,
    gt: Option<f64>,
    le: Option<f64>,
    lt: Option<f64>,
    components: Option<Vec<String>>,
) -> PyResult<PyElementField> {
    let band = crate::atoms::Band::new(ge, gt, le, lt)?;
    Ok(PyElementField {
        inner: crate::ops::element_field::mask(&field.inner, &band, components)?,
    })
}

/// Per-component 0/1 **mask** of this sub-field against a value band — same
/// structure as the sub-field (Cast3M `MASQUE`): same support, same
/// components, same Gauss points, only the values are rewritten (`1.0` inside
/// the band, `0.0` outside). The result is therefore multipliable term by term
/// with the sub-field.
///
/// Its sibling `select` extracts the passing *cells* instead, and produces a
/// `Mesh`.
///
/// The band is set by the four comparison bounds `ge` (`≥`), `gt` (`>`),
/// `le` (`≤`), `lt` (`<`). There is **no** AND across components: each value
/// stands on its own. `components=None` tests every component; a `components`
/// list tests only those, leaving the others at `1.0` (identity for the
/// product). A listed component the sub-field does not carry leaves it
/// all-`1.0`.
#[py_op(method_on = PySubElementField, name = "mask")]
#[pyfunction]
#[pyo3(signature = (field, ge=None, gt=None, le=None, lt=None, components=None))]
pub fn mask_sub_element_field(
    field: PyRef<PySubElementField>,
    ge: Option<f64>,
    gt: Option<f64>,
    le: Option<f64>,
    lt: Option<f64>,
    components: Option<Vec<String>>,
) -> PyResult<PySubElementField> {
    let band = crate::atoms::Band::new(ge, gt, le, lt)?;
    let out = crate::ops::element_field::mask_sub(&field.handle.read(), &band, components);
    Ok(PySubElementField {
        handle: Handle::new(out),
    })
}