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
//! Value-range masking — turn a field into a 0/1 indicator of the **same
//! shape**, testing each value against a `[lower, upper]` band, component by
//! component (Cast3M's `MASQUE`).
//!
//! Unlike [`select`](crate::ops::mesh::select_nodes), which extracts the passing part of the
//! support into a `Mesh`, `mask` keeps the field's exact structure (same
//! zones, same support, same components) and only rewrites the values:
//! `1.0` where the band holds, `0.0` where it does not. The result is
//! therefore multipliable term by term with the input — `field *
//! mask(field, ge=0)` zeroes the out-of-band values, component by
//! component.
//!
//! There is **no** AND across components here (that is [`select`](crate::ops::mesh::select_nodes)'s job):
//! each value stands on its own, so the mask is per component.
//!
//! # Component filter
//!
//! - `components = None` ⇒ every component is tested (gets a real 0/1);
//! - `components = Some(list)` ⇒ only those components are tested; the
//! others are left at `1.0` (identity for the product), so masking a
//! subset of components leaves the rest untouched. A zone that does not
//! carry a listed component is left all-`1.0` (the filter cannot apply).
//!
//! # Bounds
//!
//! The `[lower, upper]` band comes from a shared [`Band`],
//! built from the four comparison bounds `ge` / `gt` / `le` / `lt` — each
//! side open, inclusive, or strict.
use crate;
use crate;
use crateResult;
use crateBand;
use cratecomponents_to_test;
/// Rewrite `sub` into its 0/1 mask: each **tested** component becomes `1.0`
/// inside the band and `0.0` outside; untested components stay `1.0`.
///
/// Components are the innermost storage axis for both field flavours, so a
/// single flat pass with `idx % ncomp` handles nodes and Gauss points alike.
/// Per-component 0/1 mask of `field` against `band`, zone by zone. Returns a
/// `NodeField` with the **same structure** (zones, support, components) as
/// the input. See the [module documentation](self) for the component-filter
/// semantics.
/// Per-component 0/1 mask of `field` against `band`, zone by zone. Returns a
/// `NodeField` with the **same structure** (zones, support, components) as
/// the input. See the [module documentation](self) for the component-filter
/// semantics.
///
/// ```
/// # 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();
/// # }
/// // Un **indicateur 0/1** de même forme — mêmes zones, même support,
/// // mêmes composantes — là où `select_nodes` extrait un maillage.
/// let bande = Band::new(Some(40.0), None, None, None)?;
/// let m = node_field::mask(&temp, &bande, None)?;
/// assert_eq!(m.node_count()?, temp.node_count()?);
/// assert_eq!(m.get(0)?.read().value(n[0].id(), "T")?, 0.0); // 10 < 40
/// assert_eq!(m.get(0)?.read().value(n[2].id(), "T")?, 1.0); // 90 ≥ 40
/// // D'où sa vocation : multipliable terme à terme, il annule les valeurs
/// // hors bande sans toucher au support.
/// let filtre = (&temp * &m)?;
/// assert_eq!(filtre.get(0)?.read().value(n[2].id(), "T")?, 90.0);
/// assert_eq!(filtre.get(0)?.read().value(n[0].id(), "T")?, 0.0);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
/// Single-zone [`mask`].
///
/// ```
/// # 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 forme mono-zone, sans `Result` : rien ne peut échouer une fois la
/// // bande construite.
/// let bande = Band::new(Some(40.0), None, None, None)?;
/// let m = node_field::mask_sub(&temp.get(0)?.read(), &bande, None);
/// assert_eq!((m.value(n[0].id(), "T")?, m.value(n[2].id(), "T")?), (0.0, 1.0));
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```