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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//! Python wrappers for [`crate::ops::node_field`] — the operators that
//! produce a `NodeField`.
use crateAggregate;
use crateHandle;
use crate;
use cratePyMesh;
use cratePyModel;
use cratePyNodeField;
use cratePySubNodeField;
use PyTypeError;
use *;
use py_op;
/// The positions of the nodes of `mesh`, as a field at the nodes.
///
/// The bridge from geometry to fields: the result can be plotted, derived, or
/// written back after a computation with `pyrucast.coords.set`. Positions are
/// read in the active configuration of the `Coords`.
///
/// # Parameters
///
/// - `mesh`: the nodes to read.
/// - `components`: the axes to read, among `"X"`, `"Y"`, `"Z"`, one component
/// each; `None` reads every axis of the `Coords` (`["X"]` in 1-D, `["X", "Y"]`
/// in 2-D, `["X", "Y", "Z"]` in 3-D).
///
/// # Returns
///
/// A `NodeField` with one zone per submesh of `mesh`, on the distinct nodes of
/// that submesh.
///
/// # Raises
///
/// `RuntimeError` for a component other than `"X"`, `"Y"`, `"Z"`, or an axis
/// beyond the dimension of the `Coords`.
/// Restrict `field` to the nodes of `mesh`.
///
/// Each zone of the result lies on the POI1 cloud of a submesh of `mesh` — its
/// distinct nodes, built once and kept with the submesh. Two restrictions onto
/// the **same** `mesh` share that support, so they combine directly:
/// `restrict(a, mesh) - restrict(b, mesh)` is the node-by-node difference.
/// That 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.
///
/// Element operations on the region (`gradient`, `integral`, `deformation`,
/// `interp_to_gauss`) take `mesh` as a separate argument and read the field by
/// node. To land on the support of an existing field rather than a mesh, use
/// `pyrucast.node_field.restrict_like`.
///
/// # Parameters
///
/// - `field`: the field to restrict.
/// - `mesh`: the region, on the same `Coords` as `field`.
///
/// # Returns
///
/// A new `NodeField` with one zone per submesh of `mesh`, each carrying the
/// union of the components of `field`. A node of `mesh` the field does not
/// define gets `0.0`; the nodes outside `mesh` are dropped.
///
/// # Raises
///
/// `RuntimeError` if `mesh` and `field` are attached to different `Coords`.
/// Read `field` at the nodes of `target`, through the cells of `mesh`.
///
/// A node of `target` inside a cell of `mesh` takes the value the cell gives
/// there:
///
/// - **a field at the nodes** (`NodeField`, `SubNodeField`): `Σ Nᵢ(ξ) f(nodeᵢ)`,
/// the shape functions of that cell at the node (a quadratic cell
/// interpolates quadratically). The field is read first zone first, a node
/// of `mesh` it does not define reading zero.
/// - **a field at the Gauss points** (`ElementField`, `SubElementField`): the
/// value that cell's **own** Gauss points give there — their least-squares
/// fit on the cell, the one a plot colours the cell with. Nothing is
/// averaged between cells: a line of points crossing from one cell to its
/// neighbour shows the jump between them. A point on a face shared by two
/// cells is read in one of them; to follow a field along a line, put the
/// points between the nodes.
///
/// A node of `target` outside `mesh` gets zero, as a field at the nodes reads a
/// node it does not define: a patch projects onto a larger mesh. The nodes of
/// `target` are located once.
///
/// # Parameters
///
/// - `field`: the field to read. Every component is carried.
/// - `mesh`: the cells that interpolate. For a field at the nodes, on the same
/// `Coords` as `field`; for a field at the Gauss points, the field's own
/// cells, each submesh carrying a zone of the field.
/// - `target`: the nodes to read at. It may live in another `Coords` than
/// `mesh` (a remeshed part), each read in its active configuration.
/// - `tol`: how far outside a cell's reference element, without unit, a node
/// still counts as inside.
///
/// # Returns
///
/// A field at the nodes with one zone per submesh of `target`, like
/// `pyrucast.node_field.positions`. An empty `field` or `target` gives an empty
/// field; an empty `mesh` gives zero at every node of `target`.
///
/// # Raises
///
/// - `TypeError` if `field` is none of the four field types.
/// - `RuntimeError` if `mesh` and `target` differ in dimension, if a field at
/// the nodes and `mesh` hang from different `Coords`, or if a submesh of
/// `mesh` carries no zone of a field at the Gauss points.
// The four flavours of `project`, each documented for its sole receiver. Not
// registered in the module — the free function above remains the polymorphic
// entry point — they give `#[py_op]` the method of each flavour.
// `#[pyfunction]` serves only to make valid the pyo3 signature that `#[py_op]`
// copies.
/// This nodal field interpolated by the cells of `mesh` at the nodes of
/// `target`: a node of `target` inside a cell of `mesh` takes `Σ Nᵢ(ξ)
/// f(nodeᵢ)`, the shape functions of that cell at the node (quadratic cells
/// interpolate quadratically); every component is carried. A node of `target`
/// outside `mesh` — farther than `tol` outside every cell's reference element
/// — gets zero. Read first zone first, a node of `mesh` the field does not
/// define reading zero. `mesh` and `target` may live in different `Coords`.
/// One zone per submesh of `target`. Errors: the field and `mesh` on
/// different `Coords`; `mesh` and `target` in different dimensions.
/// This zone interpolated by the cells of `mesh` at the nodes of `target` —
/// as `NodeField.project`, for a field of this one zone.
/// This field at the Gauss points read at the nodes of `target`: a node of
/// `target` inside a cell of `mesh` takes the value that cell's own Gauss
/// points give there — their least-squares fit on the cell, the one a plot
/// colours the cell with. Nothing is averaged between cells, so a line of
/// points crossing from one cell to its neighbour shows the jump between
/// them; a point on a shared face is read in one of the two. `mesh` is the
/// field's own cells (each submesh must carry a zone of it). A node outside
/// `mesh` gets zero. One zone per submesh of `target`. Errors: a submesh of
/// `mesh` without a zone of the field; `mesh` and `target` in different
/// dimensions.
/// This zone at the Gauss points read at the nodes of `target` — as
/// `ElementField.project`, for a field of this one zone.
/// Read `field` on the support and the components of `target`, zone by zone.
///
/// Unlike `pyrucast.node_field.restrict`, which lands on a support built from
/// a mesh and carries every component of `field`, this reuses each zone of
/// `target` as it is — same support, same components. The result combines with
/// `target` directly by the arithmetic operators: typically, to fold an
/// increment from `pyrucast.solver.solve`, which also carries the
/// multipliers, into a current solution (`target + restrict_like(du, target)`).
///
/// # Parameters
///
/// - `field`: the field to read.
/// - `target`: the field whose zones, supports and components the result
/// takes, on the same `Coords` as `field`.
///
/// # Returns
///
/// A new `NodeField` zone for zone like `target`. A `(node, component)` pair
/// takes the value of `field` when it defines it, `0.0` otherwise; the nodes
/// and components of `field` absent from `target` are dropped.
///
/// # Raises
///
/// `RuntimeError` if `target` and `field` are attached to different `Coords`.
/// The union `a | b` of two fields at the nodes, consolidated.
///
/// The zones of both fields: those on the same support fused into one carrying
/// the union of their components, those on distinct supports side by side
/// (nothing is densified).
///
/// # Parameters
///
/// - `a`: the first field.
/// - `b`: the second field, on the same `Coords` as `a`.
///
/// # Returns
///
/// A new `NodeField`; `a` and `b` are left untouched.
///
/// # Raises
///
/// `RuntimeError` if `a` and `b` hold different values at the same
/// `(node, component)` pair, or are attached to different `Coords`.
/// Fuse the zones of `field` that lie on the same support into one.
///
/// The zones on one support become a single zone carrying the union of their
/// components; zones on distinct supports stay side by side. The union `a | b`
/// and `pyrucast.node_field.merge` end with this step.
///
/// # Parameters
///
/// - `field`: the field to consolidate.
///
/// # Returns
///
/// A new `NodeField`; `field` itself is left untouched.
///
/// # Raises
///
/// `RuntimeError` if two zones disagree on a value at a shared
/// `(node, component)` pair.
/// Weak divergence `d_i = ∫ ∇N_i · A dΩ` of a quantity at the Gauss points.
///
/// The adjoint of `pyrucast.element_field.gradient`, accumulated per node:
/// integrated quantities, not the pointwise values of `∇·A`. The tensor case
/// is the internal forces of a continuum when the quantity is a Cauchy stress,
/// but the operator knows nothing of mechanics: it needs only the geometry and
/// the names. A node shared by several subspaces receives the sum of their
/// contributions.
///
/// # Parameters
///
/// - `field`: the quantity, at the Gauss points.
/// - `prefix`: the name of the quantity, `A`. Its rank is read off the names
/// the field carries: `A_x, A_y` is a vector; `A_xx, A_xy, A_yy` a symmetric
/// tensor, read whole (non-symmetric) when it also carries `A_yx` — as the
/// nominal stress of large strains. On an axisymmetric space the tensor also
/// carries its hoop term `A_zz`.
///
/// # Returns
///
/// A `NodeField`: a single `div_A` component for a vector, one component per
/// axis for a tensor (`div_A_x`, `div_A_y`, …).
///
/// # Raises
///
/// `RuntimeError` if `field` carries no `prefix` of either rank, or both.
/// Gather a field of one point per cell at the nodes, by summation.
///
/// Each node receives, per component, the sum of the values of the cells it
/// belongs to — across zones too. It is not a smoothing: no average, no weight.
/// Applied to the cell measures of `pyrucast.element_field.cell_integral`, it
/// gives the volume each node stands for.
///
/// # Parameters
///
/// - `field`: an `ElementField` with one point per cell, built on a space with
/// the `REDUCED` quadrature.
///
/// # Returns
///
/// A `NodeField` with the components of `field`.
///
/// # Raises
///
/// `RuntimeError` if a zone of `field` has more than one point per cell.
/// Internal nodal forces of `model`, the left side of `Σ f_int = Σ f_ext`.
///
/// The nodal mirror of `pyrucast.matrix.stiffness`: every sub-model is asked
/// for its term of the residual on the internal side. A sub-model carrying a
/// behaviour applies its own `Bᵀ` (continuum solid, bar or beam), which is why
/// the operator takes a model and not a mere FE space; a sub-model with no term
/// on this side declares none and does not appear in the result.
///
/// For a linear law the result equals the assembled stiffness applied to the
/// solution (`K·u`); a non-linear law gives the exact internal forces. A law in
/// large strains is served on the reference configuration by the nominal
/// stress `P = F·S`, read whole (`P_xx, P_xy, P_yx, P_yy`):
/// `f = ∫ P·∇₀N dV₀`. Its counterpart is `pyrucast.node_field.external_forces`,
/// and the gap between the two sums is the residual.
///
/// # Parameters
///
/// - `model`: the physics and the constraints.
/// - `state`: the material state at the Gauss points, as returned by
/// `pyrucast.element_field.integrate_behavior`. Each zone is picked by its FE
/// subspace.
/// - `solution`: the current solution, multipliers included. A term linear in
/// `u` reads it directly — a boundary transfer's `∫ h·a·N` has no law to go
/// through —, and a constraint draws its force `Cᵀλ` from it, spread over the
/// constrained nodes by the relation's coefficients. That force is the
/// opposite of the **reaction**: the reaction of a model's constraints is
/// `−internal_forces(model.filter("constraint"), …)`, whichever way
/// `pyrucast.solver.solve` treated the relations.
/// - `materials`: the material data, zone by zone, as for
/// `pyrucast.matrix.stiffness`.
///
/// # Returns
///
/// A `NodeField` whose components are the dual variables of each sub-model
/// (`f_x`, … for a solid or a bar; `f_w`, `m_theta` for a beam).
///
/// # Raises
///
/// `RuntimeError` if `state` or `materials` has no zone on the FE subspace of a
/// sub-model that reads it, or lacks a component it reads.
/// External nodal forces of `model`, the right side of `Σ f_int = Σ f_ext`.
///
/// The counterpart of `pyrucast.node_field.internal_forces`. Every sub-model is
/// asked for its terms of the residual on the external side: the given data of
/// its weak form, on the right of the equals sign — the ambient of a boundary
/// transfer, a distributed flux. A physics whose term is entirely a response to
/// `u` (elasticity, conduction, a bar) has none. The terms loading one node are
/// added.
///
/// # Parameters
///
/// - `model`: the physics whose given data to integrate.
/// - `materials`: the material data, zone by zone, as for
/// `pyrucast.matrix.stiffness`; it carries the densities the terms read.
///
/// # Returns
///
/// A `NodeField` with a single zone over every loaded node. A model made only
/// of physics without such a term yields an empty field — the right answer,
/// not a failure.
///
/// # Raises
///
/// `RuntimeError` if `materials` has no zone on the FE subspace of a sub-model
/// that reads it, or lacks a component it reads.
/// A 0/1 mask of `field` against a band of values, component by component.
///
/// The result has the structure of the input — same zones, same support, same
/// components —, only the values are rewritten: `1.0` inside the band, `0.0`
/// outside. It therefore multiplies term by term with the input. There is
/// **no** AND across components: each value stands on its own. The
/// comparisons `>=`, `>`, `<=`, `<` of a field against a number build the same
/// mask. Its sibling `pyrucast.mesh.select` extracts the passing nodes
/// instead, as a `Mesh`.
///
/// # Parameters
///
/// - `field`: a `NodeField` or a `SubNodeField`.
/// - `ge`, `gt`: the lower bound, `v ≥ ge` or `v > gt` — at most one of the two.
/// - `le`, `lt`: the upper bound, `v ≤ le` or `v < lt` — at most one of the two.
/// A bound left out leaves that side open; at least one bound is given.
/// - `components`: the components to test, the others left at `1.0` (neutral
/// for the product); a zone missing a listed component is left all `1.0`.
/// `None` tests every component.
///
/// # Returns
///
/// A field of the type of `field`, with the same structure.
///
/// # Raises
///
/// - `TypeError` if `field` is neither a `NodeField` nor a `SubNodeField`.
/// - `RuntimeError` if no bound is given, if both bounds of one side are, or
/// if the lower bound exceeds the upper one.
// The two flavours of `mask`, each at its precise type. They are not registered
// in the module — the free function remains the only polymorphic entry point —
// but `#[py_op]` derives the receiver's method from them, carrying **their**
// documentation: that of one flavour, which need not mention the other.
// `#[pyfunction]` is there only to make `#[pyo3(signature = …)]` valid, which
// `#[py_op]` copies onto the method.
/// Per-component 0/1 **mask** of this field against a value band — same
/// structure as the field: 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 field.
///
/// Its sibling `select` extracts the passing *nodes* 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`.
/// Per-component 0/1 **mask** of this sub-field against a value band — same
/// structure as the sub-field: 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 sub-field.
///
/// Its sibling `select` extracts the passing *nodes* 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`.