-
Notifications
You must be signed in to change notification settings - Fork 15
/
nn-interpolation.cxx
355 lines (298 loc) · 12.2 KB
/
nn-interpolation.cxx
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
#ifdef USE_NPROF
#include <nvToolsExt.h>
#endif
#include "iostream"
#include <map>
#include <numeric>
#include <stdexcept>
#include "ANN/ANN.h"
#include "constants.hpp"
#include "parameters.hpp"
#include "barycentric-fn.hpp"
#include "mesh.hpp"
#include "utils.hpp"
#include "nn-interpolation.hpp"
namespace {
void find_nearest_neighbor(const Variables &var, ANNkd_tree &kdtree,
int_vec &idx, int_vec &is_changed)
{
#ifdef USE_NPROF
nvtxRangePush(__FUNCTION__);
#endif
double **new_center = elem_center(*var.coord, *var.connectivity);
const int k = 1;
const double eps = 1e-15;
int nn_idx[k];
double dd[k];
// Note: kdtree.annkSearch() is not thread-safe, cannot use openmp in this loop
for(int e=0; e<var.nelem; e++) {
double *q = new_center[e];
kdtree.annkSearch(q, k, nn_idx, dd, eps);
idx[e] = nn_idx[0];
is_changed[e] = (dd[0] < eps)? 0 : 1;
}
delete [] new_center[0];
delete [] new_center;
#ifdef USE_NPROF
nvtxRangePop();
#endif
}
void find_acm_elem_ratios(const Variables &var,
const Barycentric_transformation &bary,
int_vec &is_changed,
ANNkd_tree &kdtree,
std::vector<int_vec> &elems_vec,
std::vector<double_vec> &ratios_vec)
{
#ifdef USE_NPROF
nvtxRangePush(__FUNCTION__);
#endif
const int neta0 = 10; // larger neta0, more accurate mapping
const int neta1 = neta0 + 1; // different from neta0 to prevent the temporary point falling the edge of elements
const int neta2 = neta0;
const double spacing0 = 1.0 / neta0;
const double spacing1 = 1.0 / neta1;
const double spacing2 = 1.0 / neta2;
const int max_el = std::min(32, kdtree.nPoints());
const double eps = 0;
int nn_idx[32];
double dd[32];
const int nelem_changed = std::accumulate(is_changed.begin(), is_changed.end(), 0);
elems_vec.reserve(nelem_changed);
ratios_vec.reserve(nelem_changed);
std::map<int, int> elem_count;
for(int e=0; e<var.nelem; e++) {
if (is_changed[e]) {
/* Procedure:
* 1. Create a bunch of temporary points, uniformly distributed in the element.
* 2. Locate these points on old elements.
* 3. The percentage of points in each old elements is used as (approximate)
* volume weighting for the mapping.
*/
// std::cout << "Element " << e << " is changed:\n";
const int* conn = (*var.connectivity)[e];
for (int i=0; i<neta0; i++)
for (int j=0; j<neta1; j++) {
#ifdef THREED
for (int k=0; k<neta2; k++) {
double eta[4] = {(i + 0.5) * spacing0,
(j + 0.5) * spacing1,
(k + 0.5) * spacing2,
1 - (i + 0.5) * spacing0 - (j + 0.5) * spacing1 - (k + 0.5) * spacing2};
#else
double eta[3] = {(i + 0.5) * spacing0,
(j + 0.5) * spacing1,
1 - (i + 0.5) * spacing0 - (j + 0.5) * spacing1};
#endif
if (eta[NODES_PER_ELEM-1] < 0) continue;
double x[NDIMS] = {0}; // coordinate of temporary point
for (int d=0; d<NDIMS; d++)
for (int n=0; n<NODES_PER_ELEM; n++) {
x[d] += (*var.coord)[ conn[n] ][d] * eta[n];
}
// find the nearest point nn in old_center
kdtree.annkSearch(x, max_el, nn_idx, dd, eps);
// std::cout << " ";
// print(std::cout, eta, NODES_PER_ELEM);
// print(std::cout, x, NDIMS);
// find the old element that is enclosing x
double r[NDIMS];
int old_e;
for (int jj=0; jj<max_el; jj++) {
old_e = nn_idx[jj];
bary.transform(x, old_e, r);
if (bary.is_inside(r)) {
goto found;
}
}
/* not found, do nothing */
// std::cout << " not found\n";
continue;
found:
try {
++ elem_count.at(old_e);
}
catch (std::out_of_range const &exc) {
elem_count[old_e] = 1;
}
// std::cout << " found in old element " << old_e << "\n";
#ifdef THREED
}
#endif
}
// Count
int total_count = 0;
for (auto i=elem_count.begin(); i!=elem_count.end(); ++i)
total_count += i->second;
// std::cout << " has " << total_count << " points\n";
if (total_count == 0) {
// This happens when new material is added during remeshing,
// and this element is completely within the new material.
// Mark the element as unchanged instead to keep the result
// of nearest neighbor interpolation.
is_changed[e] = 0;
continue;
}
if (elem_count.size() == 1) {
// This happens when the new is completely within the old element.
// Mark the element as unchanged instead to keep the result
// of nearest neighbor interpolation.
is_changed[e] = 0;
elem_count.clear();
continue;
}
elems_vec.push_back(int_vec());
int_vec &elems = elems_vec.back();
elems.reserve(elem_count.size());
ratios_vec.push_back(double_vec());
double_vec &ratios = ratios_vec.back();
ratios.reserve(elem_count.size());
const double inv = 1.0 / total_count;
for (auto i=elem_count.begin(); i!=elem_count.end(); ++i) {
elems.push_back(i->first);
ratios.push_back(i->second * inv);
}
elem_count.clear();
}
}
#ifdef USE_NPROF
nvtxRangePop();
#endif
}
void prepare_interpolation(Variables &var,
const Barycentric_transformation &bary,
const array_t &old_coord,
const conn_t &old_connectivity,
int_vec &idx,
int_vec &is_changed,
std::vector<int_vec> &elems_vec,
std::vector<double_vec> &ratios_vec)
{
#ifdef USE_NPROF
nvtxRangePush(__FUNCTION__);
#endif
// kdtree requires the coordinate as double**
double **old_center = elem_center(old_coord, old_connectivity);
ANNkd_tree kdtree(old_center, old_connectivity.size(), NDIMS);
find_nearest_neighbor(var, kdtree, idx, is_changed);
find_acm_elem_ratios(var, bary, is_changed, kdtree, elems_vec, ratios_vec);
delete [] old_center[0];
delete [] old_center;
#ifdef USE_NPROF
nvtxRangePop();
#endif
}
void inject_field(const int_vec &idx,
const int_vec &is_changed,
const std::vector<int_vec> &elems_vec,
const std::vector<double_vec> &ratios_vec,
const double_vec &source,
double_vec &target)
{
#pragma omp parallel for default(none) \
shared(idx, source, target)
for (std::size_t i=0; i<target.size(); i++) {
int n = idx[i];
target[i] = source[n];
}
int n = 0;
for (std::size_t i=0; i<target.size(); i++) {
if (is_changed[i]) {
const int_vec &elems = elems_vec[n];
const double_vec &ratios = ratios_vec[n];
target[i] = 0;
for (std::size_t j=0; j<elems.size(); j++) {
target[i] += ratios[j] * source[ elems[j] ];
}
n ++;
}
}
}
void inject_field(const int_vec &idx,
const int_vec &is_changed,
const std::vector<int_vec> &elems_vec,
const std::vector<double_vec> &ratios_vec,
const tensor_t &source,
tensor_t &target)
{
#pragma omp parallel for default(none) \
shared(idx, source, target)
for (std::size_t i=0; i<target.size(); i++) {
int n = idx[i];
for (int d=0; d<NSTR; d++) {
target[i][d] = source[n][d];
}
}
int n = 0;
for (std::size_t i=0; i<target.size(); i++) {
if (is_changed[i]) {
const int_vec &elems = elems_vec[n];
const double_vec &ratios = ratios_vec[n];
for (int d=0; d<NSTR; d++) {
target[i][d] = 0;
for (std::size_t j=0; j<elems.size(); j++) {
target[i][d] += ratios[j] * source[ elems[j] ][d];
}
}
n ++;
}
}
}
void nn_interpolate_elem_fields(Variables &var,
const int_vec &idx,
const int_vec &is_changed,
const std::vector<int_vec> &elems_vec,
const std::vector<double_vec> &ratios_vec)
{
#ifdef USE_NPROF
nvtxRangePush(__FUNCTION__);
#endif
const int n = var.nnode;
const int e = var.nelem;
double_vec *a;
a = new double_vec(e);
inject_field(idx, is_changed, elems_vec, ratios_vec, *var.plstrain, *a);
delete var.plstrain;
var.plstrain = a;
a = new double_vec(e);
inject_field(idx, is_changed, elems_vec, ratios_vec, *var.delta_plstrain, *a);
delete var.delta_plstrain;
var.delta_plstrain = a;
tensor_t *b;
b = new tensor_t(e);
inject_field(idx, is_changed, elems_vec, ratios_vec, *var.strain, *b);
delete var.strain;
var.strain = b;
b = new tensor_t(e);
inject_field(idx, is_changed, elems_vec, ratios_vec, *var.stress, *b);
delete var.stress;
var.stress = b;
a = new double_vec(e);
inject_field(idx, is_changed, elems_vec, ratios_vec, *var.stressyy, *a);
delete var.stressyy;
var.stressyy = a;
#ifdef USE_NPROF
nvtxRangePop();
#endif
}
} // anonymous namespace
void nearest_neighbor_interpolation(Variables &var,
const Barycentric_transformation &bary,
const array_t &old_coord,
const conn_t &old_connectivity)
{
#ifdef USE_NPROF
nvtxRangePush(__FUNCTION__);
#endif
int_vec idx(var.nelem); // nearest element
int_vec is_changed(var.nelem); // is the element changed during remeshing?
std::vector<int_vec> elems_vec;
std::vector<double_vec> ratios_vec;
prepare_interpolation(var, bary, old_coord, old_connectivity, idx, is_changed, elems_vec, ratios_vec);
// print(std::cout, elems_vec);
// print(std::cout, ratios_vec);
nn_interpolate_elem_fields(var, idx, is_changed, elems_vec, ratios_vec);
#ifdef USE_NPROF
nvtxRangePop();
#endif
}