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
use crate::const_generic::storage::{GridPoint, PointIterator, SparseGridData};
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum RefinementMode
{
#[default]
Isotropic,
Anisotropic,
}
#[derive(Default, Debug, Clone)]
pub struct RefinementOptions
{
pub threshold: f64,
pub refinement_mode: RefinementMode,
pub level_limits: Option<Vec<u8>>,
}
impl RefinementOptions
{
pub fn new(threshold: f64) -> Self
{
Self { threshold, ..Default::default() }
}
}
///
/// This trait defines operations used for refinement or coarsening. These
/// two operations are never done simulataneously, but provide a common
/// interface to allow user-specified constraints to control either operation.
///
pub trait RefinementFunctor<const D: usize, const DIM_OUT: usize> : Send + Sync
{
///
/// Return criteria for determining refinement threshold
/// `alpha` represents the surplus coefficients for each point
/// `values` represents the values at each point
/// returns the error estimate at each node. A common choice is
/// to just use the absolute value of the surplus.
///
fn eval(&self, points: PointIterator<D>, alpha: &[[f64; DIM_OUT]], values: &[[f64; DIM_OUT]]) -> Vec<f64>;
///
/// Return per-dimension error estimates for anisotropic refinement
/// `alpha` represents the surplus coefficients for each point
/// `values` represents the values at each point
/// returns a vector where each element is a Vec<f64> of length D
/// containing the error estimate for each dimension at that node.
/// Default implementation returns uniform errors (same as eval() for all dimensions)
///
fn eval_per_dimension(&self, points: PointIterator<D>, alpha: &[[f64; DIM_OUT]], values: &[[f64; DIM_OUT]]) -> Vec<Vec<f64>>
{
let error_estimate = self.eval(points, alpha, values);
error_estimate.iter().map(|&err| vec![err; D]).collect()
}
///
/// Returns the maximum number of points to be refined. If
/// set to none there is no limit to the maximum number of points.
///
fn max_num_refined(&self) -> Option<usize>
{
None
}
///
/// Returns the maximum number of points that may be removed
///
fn max_num_removed(&self) -> Option<usize>
{
None
}
}
fn iterate_refinable_points<const D: usize, Op: FnMut((usize, &GridPoint<D>))>(storage: &SparseGridData<D>, operation: &mut Op, level_limits: Option<Vec<u8>>)
{
let level_limits = if let Some(level_limits) = level_limits.as_ref()
{
if level_limits.len() != D
{
panic!("level limits must have length equal to D");
}
std::array::from_fn(|i| level_limits[i])
}
else
{
[u8::MAX; D]
};
for (seq, point) in storage.nodes().iter().enumerate()
{
let parent = point;
let mut point = *point;
#[allow(clippy::needless_range_loop)]
for d in 0..D
{
if point.level[d] >= level_limits[d]
{
continue; // skip this point, it is too deep
}
let index = point.index[d];
let level = point.level[d];
if level == 0
{
point.level[d] = 1;
point.index[d] = 1;
if !storage.contains(&point)
{
operation((seq, parent));
break;
}
}
else
{
// check if left child exists
point.index[d] = 2 * index - 1;
point.level[d] = level + 1;
// Child doesn't exist. we can refine this node.
if !storage.contains(&point)
{
operation((seq, parent));
break;
}
// check if right child exists
point.index[d] = 2 * index + 1;
// Child doesn't exist. we can refine this node.
if !storage.contains(&point)
{
operation((seq, parent));
break;
}
point.index[d] = index;
point.level[d] = level;
}
point.level[d] = level;
point.index[d] = index;
}
}
}
///
/// Base refinement struct. Boolean determines whether or not boundaries are enabled...
///
pub struct BaseRefinement<const D: usize, const DIM_OUT: usize>(pub bool);
impl<const D: usize, const DIM_OUT: usize> BaseRefinement<D, DIM_OUT>
{
fn create_point(&self, storage: &mut SparseGridData<D>, point: GridPoint<D>)
{
for dim in 0..D
{
if !self.0 // no boundaries
{
self.create_point_1d(point, storage, dim);
}
else // has boundaries
{
self.create_point_1d_with_boundary(point, storage, dim);
}
}
storage.insert_point(point);
// deal with boundaries
if self.0
{
self.create_gridpoint_level_zero_consistency(storage, point);
}
}
fn create_gridpoint_level_zero_consistency(&self, storage: &mut SparseGridData<D>, mut point: GridPoint<D>)
{
if D == 1 // only needed for D > 1
{
return;
}
for dim in 0..D
{
let level = point.level[dim];
let index = point.index[dim];
if level == 0
{
point.level[dim] = 0;
// loop through left, right boundaries
for i in 0..2
{
point.index[dim] = i;
if storage.contains(&point)
{
let leaf_l = point.is_leaf();
// check the boundary not being evaluated
point.index[dim] = if i == 0 { 1 } else { 0 };
if !storage.contains(&point)
{
let leaf_r = point.is_leaf();
point.set_is_leaf(leaf_l);
self.create_point(storage, point);
point.set_is_leaf(leaf_r);
}
else if let Some(point) = storage.get_mut(&point)
{
point.set_is_leaf(leaf_l);
}
}
}
point.level[dim] = level;
point.index[dim] = index;
}
}
}
fn create_gridpoint_internal(&self, storage: &mut SparseGridData<D>, mut point: GridPoint<D>)
{
if let Some(point) = storage.get_mut(&point)
{
point.set_is_leaf(false);
}
else
{
point.set_is_leaf(false);
self.create_point(storage, point);
}
}
fn create_point_1d_with_boundary(&self, mut point: GridPoint<D>, storage: &mut SparseGridData<D>, dim: usize)
{
let level = point.level[dim];
let index = point.index[dim];
// stuff for boundaries...
if level == 1 && D > 1
{
// check if we need some additional points on the boundaries,
// only needed on a N dim grid
// test if there are boundaries in every dimension for this grid point
// left boundary
point.index[dim] = 0;
point.level[dim] = 0;
self.create_gridpoint_internal(storage, point);
// right boundary
point.level[dim] = 0;
point.index[dim] = 1;
self.create_gridpoint_internal(storage, point);
// restore values
point.level[dim] = level;
point.index[dim] = index;
}
self.create_point_1d(point, storage, dim);
}
fn create_point_1d(&self, mut point: GridPoint<D>, storage: &mut SparseGridData<D>, dim: usize)
{
let level = point.level[dim];
let index = point.index[dim];
if level > 1
{
if index.div_ceil(2) % 2 == 1
{
point.index[dim] = index.div_ceil(2);
point.level[dim] = level - 1;
}
else
{
point.index[dim] = (index - 1) / 2;
point.level[dim] = level - 1;
}
self.create_gridpoint_internal(storage, point);
}
}
fn refine_gridpoint(&self, storage: &mut SparseGridData<D>, index: usize, level_limits: Option<Vec<u8>>, dim_errors: Option<&[f64]>, threshold: f64)
{
let level_limits = level_limits.unwrap_or(vec![u8::MAX; D]);
let point = storage[index];
storage[index].set_is_leaf(false);
for dim in 0..D
{
if point.level[dim] == level_limits[dim]
{
continue; // skip this dimension, it is too deep
}
// For anisotropic refinement, only refine dimensions with high error
if let Some(errors) = dim_errors && errors[dim] <= threshold
{
continue; // skip this dimension, error is too low
}
self.refine_1d(storage, point, dim);
}
}
}
impl<const D: usize, const DIM_OUT: usize> BaseRefinement<D, DIM_OUT>
{
///
/// Refine grid based on criteria computed using functor
///
pub fn refine(&self, storage: &mut SparseGridData<D>, alpha: &[[f64; DIM_OUT]], values: &[[f64; DIM_OUT]], functor: &dyn RefinementFunctor<D, DIM_OUT>, options: RefinementOptions) -> Vec<usize> {
let mut refinable_nodes = Vec::new();
let original_number = storage.len();
// For anisotropic refinement, use per-dimension errors
let use_anisotropic = options.refinement_mode == RefinementMode::Anisotropic;
if use_anisotropic
{
let dim_errors = functor.eval_per_dimension(storage.points(), alpha, values);
iterate_refinable_points(storage, &mut |(seq, _point)|
{
// Check if any dimension exceeds threshold
if dim_errors[seq].iter().any(|&err| err > options.threshold)
{
refinable_nodes.push(seq);
}
}, options.level_limits.clone());
for seq in refinable_nodes
{
self.refine_gridpoint(storage, seq, options.level_limits.clone(), Some(&dim_errors[seq]), options.threshold);
}
}
else // Isotropic refinement
{
let error_estimate = functor.eval(storage.points(), alpha, values);
iterate_refinable_points(storage, &mut |(seq, _point)|
{
if error_estimate[seq] > options.threshold
{
refinable_nodes.push(seq);
}
}, options.level_limits.clone());
for seq in refinable_nodes
{
self.refine_gridpoint(storage, seq, options.level_limits.clone(), None, options.threshold);
}
}
(original_number..storage.len()).collect()
}
///
/// Returns the number of grid points that can be refined.
///
pub fn get_num_refinable_points(&self, storage: &SparseGridData<D>, level_limits: Option<Vec<u8>>) -> usize
{
let mut count = 0;
iterate_refinable_points(storage, &mut |_point| { count += 1; }, level_limits);
count
}
///
/// Refine a grid point along a single direction
///
pub fn refine_1d(&self, storage: &mut SparseGridData<D>, mut point: GridPoint<D>, dim: usize)
{
let index = point.index[dim];
let level = point.level[dim];
if level == 0
{
point.level[dim] = 1;
point.index[dim] = 1;
if !storage.contains(&point)
{
point.set_is_leaf(true);
self.create_point(storage, point);
}
}
else
{
point.level[dim] = level + 1;
point.index[dim] = 2 * index - 1;
if !storage.contains(&point)
{
point.set_is_leaf(true);
self.create_point(storage, point);
}
point.level[dim] = level + 1;
point.index[dim] = index * 2 + 1;
if !storage.contains(&point)
{
point.set_is_leaf(true);
self.create_point(storage, point);
}
}
}
}