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
use super::Gauss;
use crate::shapes::Scratchpad;
use crate::StrError;
/// Implements the integration of a scalar field over a geometric shape
///
/// ```text
/// ⌠ → →
/// I = │ s(x(ξ)) dΩ
/// ⌡
/// Ωₑ
/// ```
///
/// The numerical integration is:
///
/// ```text
/// nip-1 → →
/// I ≈ Σ s(ιᵖ) |J|(ιᵖ) wᵖ
/// p=0
/// ```
///
/// # Input
///
/// * `pad` -- **modified** Scratchpad
/// * `ips` -- Integration points (ngauss)
/// * `fn_s` -- Function `f(p)` corresponding to `s(x(ιᵖ))` with `0 ≤ p ≤ ngauss`
///
/// # Output
///
/// * Returns `I`, the result of integration.
pub fn scalar_field<F>(pad: &mut Scratchpad, gauss: &Gauss, mut fn_s: F) -> Result<f64, StrError>
where
F: FnMut(usize) -> Result<f64, StrError>,
{
// result from integration
let mut ii = 0.0;
// loop over integration points
for p in 0..gauss.npoint() {
// ksi coordinates and weight
let iota = gauss.coords(p);
let weight = gauss.weight(p);
// calculate Jacobian
let det_jac = pad.calc_jacobian(iota)?;
// calculate s
let s = fn_s(p)?;
// loop over nodes and perform sum
ii += s * det_jac * weight;
}
Ok(ii)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod tests {
use super::scalar_field;
use crate::integ::Gauss;
use crate::recovery;
use crate::shapes::{GeoKind, Scratchpad};
use russell_lab::approx_eq;
#[allow(unused_imports)]
use plotpy::Plot;
#[test]
fn scalar_fields_over_rotated_square() {
// y (1,1)
// 2
// ^ .' `.
// | .' . `.
// | .' `.
// | .' . `.
// .' `.
// (0,0) 0 . . . . . 1 (2,0) ----> x
// '. .'
// '. . .'
// '. .'
// '. . .'
// '. .'
// '1'
// (1,-1)
let mut pad = Scratchpad::new(2, GeoKind::Qua4).unwrap();
pad.set_xx(0, 0, 0.0);
pad.set_xx(0, 1, 0.0);
pad.set_xx(1, 0, 1.0);
pad.set_xx(1, 1, -1.0);
pad.set_xx(2, 0, 2.0);
pad.set_xx(2, 1, 0.0);
pad.set_xx(3, 0, 1.0);
pad.set_xx(3, 1, 1.0);
// integration points
let class = pad.kind.class();
let selection: Vec<_> = [1, 4, 9, 16]
.iter()
.map(|n| Gauss::new_sized(class, *n).unwrap())
.collect();
// s(x) is constant = 1; i.e., the integral will result in the area of the "diamond" shape
let ii_correct = 2.0;
let tolerances = [1e-14, 1e-14, 1e-14, 1e-14];
selection.iter().zip(tolerances).for_each(|(ips, tol)| {
// println!("nip={}, tol={:.e}", ips.len(), tol);
let ii = scalar_field(&mut pad, ips, |_| Ok(1.0)).unwrap();
approx_eq(ii, ii_correct, tol);
});
// ∫∫(x²+y²) dx dy
let tolerances = [0.67, 1e-14, 1e-14, 1e-14];
let ii_correct = 8.0 / 3.0;
selection.iter().zip(tolerances).for_each(|(ips, tol)| {
// println!("nip={}, tol={:.e}", ips.len(), tol);
let x_ips = recovery::get_points_coords(&mut pad, ips).unwrap();
let ii = scalar_field(&mut pad, ips, |p| {
let x = x_ips[p][0];
let y = x_ips[p][1];
Ok(x * x + y * y)
})
.unwrap();
approx_eq(ii, ii_correct, tol);
});
// ∫∫(x³+y³) dx dy
let tolerances = [1.01, 1e-14, 1e-14, 1e-14];
let ii_correct = 3.0;
selection.iter().zip(tolerances).for_each(|(ips, tol)| {
// println!("nip={}, tol={:.e}", ips.len(), tol);
let x_ips = recovery::get_points_coords(&mut pad, ips).unwrap();
let ii = scalar_field(&mut pad, ips, |p| {
let x = x_ips[p][0];
let y = x_ips[p][1];
Ok(x * x * x + y * y * y)
})
.unwrap();
approx_eq(ii, ii_correct, tol);
});
// Mathematica code:
// region = Polygon[{{0, 0}, {1, -1}, {2, 0}, {1, 1}}];
// Graphics[{Purple, region}]
// Integrate[1, {x, y} \[Element] region]
// (* returns 2 *)
// Integrate[x^2 + y^2, {x, y} \[Element] region]
// (* returns 8/3 *)
// Integrate[x^3 + y^3, {x, y} \[Element] region]
// (* returns 3 *)
}
#[test]
fn scalar_fields_over_slanted_hex8() {
let mut pad = Scratchpad::new(3, GeoKind::Hex8).unwrap();
// node 0
pad.set_xx(0, 0, 0.0);
pad.set_xx(0, 1, 0.0);
pad.set_xx(0, 2, 0.0);
// node 1
pad.set_xx(1, 0, 1.0);
pad.set_xx(1, 1, 0.0);
pad.set_xx(1, 2, 0.0);
// node 2
pad.set_xx(2, 0, 2.0);
pad.set_xx(2, 1, 1.0);
pad.set_xx(2, 2, 0.0);
// node 3
pad.set_xx(3, 0, 1.0);
pad.set_xx(3, 1, 1.0);
pad.set_xx(3, 2, 0.0);
// node 4
pad.set_xx(4, 0, 0.0);
pad.set_xx(4, 1, 0.0);
pad.set_xx(4, 2, 1.0);
// node 5
pad.set_xx(5, 0, 1.0);
pad.set_xx(5, 1, 0.0);
pad.set_xx(5, 2, 1.0);
// node 6
pad.set_xx(6, 0, 2.0);
pad.set_xx(6, 1, 1.0);
pad.set_xx(6, 2, 1.0);
// node 7
pad.set_xx(7, 0, 1.0);
pad.set_xx(7, 1, 1.0);
pad.set_xx(7, 2, 1.0);
if false {
let mut plot = Plot::new();
pad.draw_shape(&mut plot, "", true, true).unwrap();
plot.set_equal_axes(true)
.set_figure_size_points(400.0, 400.0)
.save("/tmp/gemlab/test_scalar_fields_over_slanted_hex8.svg")
.unwrap();
}
// integration points
let class = pad.kind.class();
let selection: Vec<_> = [6, 8, 14, 27, 64]
.iter()
.map(|n| Gauss::new_sized(class, *n).unwrap())
.collect();
// s(x) is constant = 1; i.e., the integral will result in the volume
let ii_correct = 1.0;
let tolerances = [1e-14, 1e-14, 1e-14, 1e-14, 1e-14];
selection.iter().zip(tolerances).for_each(|(ips, tol)| {
// println!("nip={}, tol={:.e}", ips.len(), tol);
let ii = scalar_field(&mut pad, ips, |_| Ok(1.0)).unwrap();
approx_eq(ii, ii_correct, tol);
});
// ∫∫∫(x²+y²+z²) dx dy dz
let ii_correct = 11.0 / 6.0;
let tolerances = [1e-14, 1e-14, 1e-14, 1e-14, 1e-14];
selection.iter().zip(tolerances).for_each(|(ips, tol)| {
// println!("nip={}, tol={:.e}", ips.len(), tol);
let x_ips = recovery::get_points_coords(&mut pad, ips).unwrap();
let ii = scalar_field(&mut pad, ips, |p| {
let x = x_ips[p][0];
let y = x_ips[p][1];
let z = x_ips[p][2];
Ok(x * x + y * y + z * z)
})
.unwrap();
approx_eq(ii, ii_correct, tol);
});
// ∫∫∫(x³+y³+z³) dx dy dz
let ii_correct = 2.0;
let tolerances = [1e-14, 1e-14, 1e-14, 1e-14, 1e-14];
selection.iter().zip(tolerances).for_each(|(ips, tol)| {
// println!("nip={}, tol={:.e}", ips.len(), tol);
let x_ips = recovery::get_points_coords(&mut pad, ips).unwrap();
let ii = scalar_field(&mut pad, ips, |p| {
let x = x_ips[p][0];
let y = x_ips[p][1];
let z = x_ips[p][2];
Ok(x * x * x + y * y * y + z * z * z)
})
.unwrap();
approx_eq(ii, ii_correct, tol);
});
// mathematica code:
// region = Hexahedron[{{0, 0, 0}, {1, 0, 0}, {2, 1, 0}, {1, 1, 0}, {0, 0, 1}, {1, 0, 1}, {2, 1, 1}, {1, 1, 1}}];
// Graphics3D[{region}]
// Integrate[1, {x, y, z} \[Element] region]
// (* returns 1 *)
// Integrate[x^2 + y^2 + z^2, {x, y, z} \[Element] region]
// (* returns 11/6 *)
// Integrate[x^3 + y^3 + z^3, {x, y, z} \[Element] region]
// (* returns 2 *)
}
}