-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdensefield.hpp
500 lines (417 loc) · 17.1 KB
/
densefield.hpp
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
/*
* field.hpp
*
* Sparse storage in HCS
*
* This is a sparse storage class for the H coordinate system.
* - dedicated refinement / coarsening
* - only complete "H"s exist
* - lower-level coords always exist, but top-level get marked as such. (Top-Level-Coordinate TLC)
* - iterator class that allows fast iteration over all top-level or all existing coords or
* existing coords of a specific level
* - bi-linear interpolation of non-existing coords, providing coefficients for TLC
* - Arbitrary data type that needs to support some basic arithmetic
* - Field supplies basic arithmetic operators
* - A bracket operator for coordinates is implemented, with adjustable behavior for non-existing coords.
* - The performance of exists() relies on STL's map::lower_bound O(log) complexity
* - The center coordinate (1) always exists
* - boundary conditions can be implemented as lambdas
*
*/
using namespace std;
using namespace hcs;
template <typename DTYPE, typename HCSTYPE>
class DenseField : public Field<DTYPE, HCSTYPE> {
public:
DenseField(HCSTYPE hcs_) : Field<DTYPE, HCSTYPE>(hcs_), max_level(1), max_coord(1) {
clear();
hcs = hcs_;
}
DenseField() : DenseField(HCSTYPE()) {}
DenseField(level_t level) : DenseField(HCSTYPE()) {
createEntireLevel(level);
}
// The copy constructor, to make quick copies of the field and its structure
// Field<??> a = b; Or Field<??> a(b);
DenseField(const DenseField<DTYPE, HCSTYPE> &f) {
//cout << "FCOPY\n"; // debug hint, this is an expensive op and can happen when you least expect it
this->max_level = f.max_level;
this->max_coord = f.max_coord;
this->hcs = f.hcs;
hcs = this->hcs;
this->bracket_behavior = f.bracket_behavior;
this->data = f.data;
this->boundary_propagate = f.boundary_propagate;
for (int i = 0; i < 64; i++)
this->boundary[i] = this->boundary_propagate[i] ? f.boundary[i] : nullptr;
}
~DenseField() {}
// Any other type of Field is a friend.
template <typename U, typename V>
friend class DenseField;
// Any other type of Field is a friend.
template <typename U, typename V>
friend class Field;
private:
// The actual data is stored linear to coord for efficiency. Data storage is _not_ sparse!
vector<DTYPE> data;
// Indicates the current level in data.
level_t max_level;
coord_t max_coord;
public:
HCSTYPE hcs;
// C++ goodies, with this operator you can iterate over all existing coords in a field
class DenseIterator : public Field<DTYPE, HCSTYPE>::CustomIterator {
public:
DenseIterator(DenseField<DTYPE, HCSTYPE>* field, bool top_only = false, int only_level = -1) : current(1), field(field), only_level(only_level), top_only(top_only), end_coord(1), current_pair(0, intermediate), current_idx(0) {
if (field == NULL)
return;
hcs = field->hcs;
if (top_only && only_level >= 0)
throw range_error("Field iterator can only be top_only or only_level, not both.");
this->at_end = (only_level > field->max_level);
if (this->at_end)
return;
end_coord = hcs.CreateMaxLevel(field->max_level);
if (only_level >= 0) {
current = hcs.CreateMinLevel(only_level);
end_coord = hcs.CreateMaxLevel(only_level);
} else if (top_only)
current = hcs.CreateMinLevel(field->max_level);
else
current = 1;
current_idx = hcs.coord2index(current);
this->currentCoord = current;
this->currentValPtr = &field->data[current_idx];
}
virtual pair<coord_t, DTYPE&>* getCurrentPairPtr() {
if (this->at_end)
throw range_error("Iterator reached end and was queried for value!");
current_pair.~pair<coord_t, DTYPE&>();
new(¤t_pair) pair<coord_t, DTYPE&>(current, field->data[current_idx]);
return ¤t_pair;
}
virtual void increment() {
if (hcs.inc(current))
this->at_end = current > end_coord;
current_idx++;
this->currentCoord = current;
this->currentValPtr = &field->data[current_idx];
}
DenseIterator* clone() {
DenseIterator* result = new DenseIterator(field, this->top_only, only_level);
result->current = current;
result->end_coord = end_coord;
return result;
}
private:
DTYPE intermediate;
pair<coord_t, DTYPE&> current_pair;
DenseField<DTYPE, HCSTYPE>* field;
coord_t current, end_coord;
size_t current_idx;
int only_level;
bool top_only;
HCSTYPE hcs;
};
// Iterator methods & class
//iterator dummy = iterator(this);
typename Field<DTYPE, HCSTYPE>::Iterator begin(bool top_only = false, int only_level = -1) {
return typename Field<DTYPE, HCSTYPE>::Iterator(new DenseIterator(this, top_only, only_level));
}
typename Field<DTYPE, HCSTYPE>::Iterator end() { // Just dummy, the begin iterator determines termination
return NULL;
}
// Returns the number of available elements for this field
size_t nElements() {
return this->data.size();
}
// Returns the number of top-level elements for this field
size_t nElementsTop() {
return hcs.CreateMaxLevel(max_level) - hcs.CreateMinLevel(max_level);
}
// Read-write access to existing coords. For (probably) non-existing, use get() and retrieve interpolated values.
// the value set to bracket_behavior applies.
DTYPE& operator[](coord_t coord) {
if (!this->exists(coord)) {
switch (this->bracket_behavior) {
case Field<DTYPE,HCSTYPE>::BR_THROW:
throw range_error("[]: Coord does not exist");
case Field<DTYPE,HCSTYPE>::BR_INTERP:
this->intermediate = get(coord);
return this->intermediate;
case Field<DTYPE,HCSTYPE>::BR_REFINE:
throw bad_function_call();
case Field<DTYPE,HCSTYPE>::BR_NOTHING:
return this->intermediate;
}
}
return data[hcs.coord2index(coord)];
}
bool exists(coord_t coord) {
return coord <= max_coord;
}
// Does not query coefficients, throws if coord does not exist
DTYPE& getDirect(coord_t coord) {
//if (!this->exists(coord))
// throw range_error("[]: Coord does not exist");
return data[hcs.coord2index(coord)];
}
// Returns value for coord, if not present, interpolates.
// if it is not TLC, return value anyway. To retrieve proper values from non-TLC
// call propagate() first
DTYPE get(coord_t coord, bool use_non_top = true) {
DTYPE result = 0;
Field<DTYPE, HCSTYPE>::get(coord, result, use_non_top);
return result;
}
// Do coordinates exist in a higher level?
bool isTop(coord_t coord) {
return hcs.GetLevel(coord) == max_level;
}
// Average all non-top coords from top-level
void propagate() {
uint32_t parts = hcs.parts;
data_t inv_parts = 1. / data_t(parts);
size_t idx = data.size() - 1;
coord_t c = hcs.index2coord(idx);
c -= c % parts;
idx = hcs.coord2index(c);
while (c > parts) {
DTYPE sum = 0;
for (size_t j = idx; j < idx + parts; j++)
sum += data[j];
sum *= inv_parts;
data[hcs.coord2index(hcs.ReduceLevel(c))] += sum;
hcs.decParts(c);
idx -= parts;
}
}
// Average all non-top coords from top-level and subtract
void pack() {
uint32_t parts = hcs.parts;
data_t inv_parts = 1. / data_t(parts);
size_t idx = data.size() - 1;
coord_t c = hcs.index2coord(idx);
c -= c % parts;
idx = hcs.coord2index(c);
while (c > parts) {
DTYPE sum = 0;
for (size_t j = idx; j < idx + parts; j++)
sum += data[j];
sum *= inv_parts;
for (size_t j = idx; j < idx + parts; j++)
data[j] -= sum / 2.;
data[hcs.coord2index(hcs.ReduceLevel(c))] += sum / 2.;
hcs.decParts(c);
idx -= parts;
}
}
// Opposite of pack
void unpack() {
uint32_t parts = hcs.parts;
data_t inv_parts = 1. / data_t(parts);
size_t idx = 0;
coord_t c = 1;
while (idx < data.size() - parts) {
DTYPE sum = data[idx];
for (size_t j = idx; j < idx + parts; j++)
sum += data[j];
sum *= inv_parts;
for (size_t j = idx; j < idx + parts; j++)
data[j] -= sum;
data[hcs.coord2index(hcs.ReduceLevel(c))] += sum;
hcs.decParts(c);
idx -= parts;
}
}
// .. and all levels below.
// This routine DELETES everything in the field and is meant as an initializer. Fills with DTYPE(0)
void createEntireLevel(level_t level) {
max_level = level;
max_coord = hcs.CreateMaxLevel(level);
size_t level_end_idx = hcs.coord2index(max_coord);
data.resize(level_end_idx + 1, DTYPE(0));
}
// Return highest stored coord-level. Could be faster.
level_t getHighestLevel() {
return max_level;
}
// Assignment operator requires equal structure, dirty-check with data.size()
// isTop is not copied because of assumption of equal structure
DenseField &operator=(const DenseField& f){
//cout << "XCOPY\n";
assert(("= Operator would alter structure. if this is intended, call takeStructure(x) first!",
data.size() == f.data.size()));
data = f.data;
this->boundary_propagate = f.boundary_propagate;
for (int i = 0; i < 64; i++)
this->boundary[i] = this->boundary_propagate[i] ? f.boundary[i] : nullptr;
return *this;
};
// Assignment operator requires equal structure, dirty-check with data.size()
// isTop is not copied because of assumption of equal structure
Field<DTYPE, HCSTYPE> &operator=(const Field<DTYPE, HCSTYPE>& f){
this->operator =(dynamic_cast<DenseField<DTYPE, HCSTYPE> >(f));
return *this;
};
DenseField &operator=(const DTYPE& f){
fill(data.begin(), data.end(), f);
//Field<DTYPE,HCSTYPE>::operator =(f);
return *this;
}
DenseField<DTYPE, HCSTYPE> operator-() const { DenseField<DTYPE, HCSTYPE> result = *this; for (auto e : result) e.second = -e.second; return result;}
// Clears the field and takes the same coordinate structure as the provided field, without copying their
// values. The provided field may have a different DTYPE. The newly created coords are initialized with zero.
template <typename DTYPE2>
void takeStructure(DenseField<DTYPE2, HCSTYPE> &f) {
if (sameStructure(f))
return;
data.resize(f.data.size(), DTYPE(0));
max_level = f.max_level;
max_coord = f.max_coord;
}
// Tests if the provided field has the same structure.
// The provided field may have a different DTYPE. The newly created coords are initialized with zero.
template <typename DTYPE2>
bool sameStructure(const DenseField<DTYPE2, HCSTYPE> &f) {
return f.data.size() == data.size();
}
// Empties all data
void clear() {
data.clear();
//data.resize(2, DTYPE(0));
max_level = 0;
max_coord = 0;
}
Field<DTYPE, HCSTYPE>& operator*= (const DenseField<DTYPE, HCSTYPE>& rhs) {
if (sameStructure(rhs)) {
for (size_t i = 0; i < data.size(); i++)
data[i] *= rhs.data[i];
} else
return Field<DTYPE,HCSTYPE>::operator *=(rhs);
return *this;
}
Field<DTYPE, HCSTYPE>& operator*= (const DTYPE& rhs) {
for (size_t i = 0; i < data.size(); i++)
data[i] *= rhs;
return *this;
}
Field<DTYPE, HCSTYPE>& operator+= (const DenseField<DTYPE, HCSTYPE>& rhs) {
if (sameStructure(rhs)) {
for (size_t i = 0; i < data.size(); i++)
data[i] += rhs.data[i];
} else
return Field<DTYPE,HCSTYPE>::operator +=(rhs);
return *this;
}
Field<DTYPE, HCSTYPE>& operator+= (const Field<DTYPE, HCSTYPE>& rhs) {
return Field<DTYPE,HCSTYPE>::operator +=(rhs);
}
Field<DTYPE, HCSTYPE>& operator+= (const DTYPE& rhs) {
for (size_t i = 0; i < data.size(); i++)
data[i] += rhs;
return *this;
}
Field<DTYPE, HCSTYPE>& operator-= (const DenseField<DTYPE, HCSTYPE>& rhs) {
if (sameStructure(rhs)) {
for (size_t i = 0; i < data.size(); i++)
data[i] -= rhs.data[i];
} else
return Field<DTYPE,HCSTYPE>::operator -=(rhs);
return *this;
}
Field<DTYPE, HCSTYPE>& operator-= (const Field<DTYPE, HCSTYPE>& rhs) {
return Field<DTYPE,HCSTYPE>::operator -=(rhs);
}
Field<DTYPE, HCSTYPE>& operator-= (const DTYPE& rhs) {
for (size_t i = 0; i < data.size(); i++)
data[i] -= rhs;
return *this;
}
Field<DTYPE, HCSTYPE>& operator/= (const DenseField<DTYPE, HCSTYPE>& rhs) {
if (sameStructure(rhs)) {
for (size_t i = 0; i < data.size(); i++)
data[i] /= rhs.data[i];
} else
return Field<DTYPE,HCSTYPE>::operator /=(rhs);
return *this;
}
Field<DTYPE, HCSTYPE>& operator/= (const DTYPE& rhs) {
for (size_t i = 0; i < data.size(); i++)
data[i] /= rhs;
return *this;
}
};
// Other non-member arithmetic ops
template <typename DTYPE, typename HCSTYPE> DenseField<DTYPE, HCSTYPE> operator* (const DenseField<DTYPE, HCSTYPE>& lhs, const Field<DTYPE, HCSTYPE>& rhs) {
DenseField<DTYPE, HCSTYPE> result = lhs;
result *= rhs;
return result;
};
template <typename DTYPE, typename HCSTYPE> DenseField<DTYPE, HCSTYPE> operator* (const DenseField<DTYPE, HCSTYPE>& lhs, const DenseField<DTYPE, HCSTYPE>& rhs) {
DenseField<DTYPE, HCSTYPE> result = lhs;
result *= rhs;
return result;
};
template <typename DTYPE, typename HCSTYPE> DenseField<DTYPE, HCSTYPE> operator* (const DTYPE& val, const DenseField<DTYPE, HCSTYPE>& rhs) {
DenseField<DTYPE, HCSTYPE> result = rhs;
result *= val;
return result;
};
template <typename DTYPE, typename HCSTYPE> DenseField<DTYPE, HCSTYPE> operator* (const DenseField<DTYPE, HCSTYPE>& lhs, const DTYPE& val) {
DenseField<DTYPE, HCSTYPE> result = lhs;
result *= val;
return result;
};
template <typename DTYPE, typename HCSTYPE> DenseField<DTYPE, HCSTYPE> operator/ (const DenseField<DTYPE, HCSTYPE>& lhs, const Field<DTYPE, HCSTYPE>& rhs) {
DenseField<DTYPE, HCSTYPE> result = lhs;
result /= rhs;
return result;
};
template <typename DTYPE, typename HCSTYPE> DenseField<DTYPE, HCSTYPE> operator/ (const DTYPE& val, const DenseField<DTYPE, HCSTYPE>& rhs) {
DenseField<DTYPE, HCSTYPE> result = rhs;
for (auto e : result)
e.second = val / e.second;
return result;
}
template <typename DTYPE, typename HCSTYPE> DenseField<DTYPE, HCSTYPE> operator/ (const DenseField<DTYPE, HCSTYPE>& lhs, const DTYPE& val) {
DenseField<DTYPE, HCSTYPE> result = lhs;
result /= val;
return result;
}
template <typename DTYPE, typename HCSTYPE> DenseField<DTYPE, HCSTYPE> operator+ (const DenseField<DTYPE, HCSTYPE>& lhs, const Field<DTYPE, HCSTYPE>& rhs) {
DenseField<DTYPE, HCSTYPE> result = lhs;
result += rhs;
return result;
};
template <typename DTYPE, typename HCSTYPE> DenseField<DTYPE, HCSTYPE> operator+ (const DenseField<DTYPE, HCSTYPE>& lhs, const DenseField<DTYPE, HCSTYPE>& rhs) {
DenseField<DTYPE, HCSTYPE> result = lhs;
result += rhs;
return result;
};
template <typename DTYPE, typename HCSTYPE> DenseField<DTYPE, HCSTYPE> operator+ (const DTYPE& val, const DenseField<DTYPE, HCSTYPE>& rhs) {
DenseField<DTYPE, HCSTYPE> result = rhs;
result += val;
return result;
}
template <typename DTYPE, typename HCSTYPE> DenseField<DTYPE, HCSTYPE> operator+ (const DenseField<DTYPE, HCSTYPE>& lhs, const DTYPE& val) {
DenseField<DTYPE, HCSTYPE> result = lhs;
result += val;
return result;
}
template <typename DTYPE, typename HCSTYPE> DenseField<DTYPE, HCSTYPE> operator- (const DenseField<DTYPE, HCSTYPE>& lhs, const Field<DTYPE, HCSTYPE>& rhs) {
DenseField<DTYPE, HCSTYPE> result = lhs;
result -= rhs;
return result;
};
template <typename DTYPE, typename HCSTYPE> DenseField<DTYPE, HCSTYPE> operator- (const DTYPE& val, const DenseField<DTYPE, HCSTYPE>& rhs) {
DenseField<DTYPE, HCSTYPE> result = -rhs;
result += val;
return result;
}
template <typename DTYPE, typename HCSTYPE> DenseField<DTYPE, HCSTYPE> operator- (const DenseField<DTYPE, HCSTYPE>& lhs, const DTYPE& val) {
DenseField<DTYPE, HCSTYPE> result = lhs;
result -= val;
return result;
}