-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
353 lines (282 loc) · 7.99 KB
/
main.cpp
File metadata and controls
353 lines (282 loc) · 7.99 KB
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
/*
* main.cpp
* Part of 2d-treecodes
*
* Created and authored by Diego Rossinelli on 2015-09-25.
* Copyright 2015. All rights reserved.
*
* Users are NOT authorized
* to employ the present software for their own publications
* before getting a written permission from the author of this file.
*/
#include <unistd.h>
#include <omp.h>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cassert>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include "2d-treecodes-force.h"
#include "2d-treecodes-potential.h"
double tol = 1e-8;
void check(const double * ref, const double * res, const int N)
{
double linf = 0, l1 = 0, linf_rel = 0, l1_rel = 0;
for(int i = 0; i < N; ++i)
{
assert(!std::isnan(ref[i]));
assert(!std::isnan(res[i]));
const double err = ref[i] - res[i];
const double maxval = std::max(fabs(res[i]), fabs(ref[i]));
const double relerr = err/std::max(1e-6, maxval);
if (fabs(relerr) >= tol && fabs(err) >= tol)
printf("%d: %e ref: %e -> %e %e\n", i, res[i], ref[i], err, relerr);
assert(fabs(relerr) < tol || fabs(err) < tol);
l1 += fabs(err);
l1_rel += fabs(relerr);
linf = std::max(linf, fabs(err));
linf_rel = std::max(linf_rel, fabs(relerr));
}
printf("l-infinity errors: %.03e (absolute) %.03e (relative)\n", linf, linf_rel);
printf(" l-1 errors: %.03e (absolute) %.03e (relative)\n", l1, l1_rel);
}
void test(double theta, double tol, FILE * f = NULL, bool potential = true, bool verify = true, bool mragfile = false)
{
int NSRC = 1e5 * (0.1 + drand48());
if (f)
fread(&NSRC, sizeof(int), 1, f);
double * xsrc = new double[NSRC];
double * ysrc = new double[NSRC];
double * sources = new double[NSRC];
if (f)
{
fread(xsrc, sizeof(double), NSRC, f);
fread(ysrc, sizeof(double), NSRC, f);
fread(sources, sizeof(double), NSRC, f);
}
else
for(int i = 0; i < NSRC; ++i)
{
xsrc[i] = (drand48() - 0.25) * 10;
ysrc[i] = (drand48() - 0.75) * 20;
sources[i] = drand48() * 55;
}
int NDST = 3000 * (0.1 + drand48());
if (f)
fread(&NDST, sizeof(int), 1, f);
int NBLOCKS = 0;
const int BS2 = BLOCKSIZE * BLOCKSIZE;
double * x0s = nullptr, *y0s = nullptr, *hs = nullptr;
if (mragfile)
{
NBLOCKS = NDST;
NDST = NBLOCKS * BS2;
x0s = new double[NBLOCKS];
y0s = new double[NBLOCKS];
hs = new double[NBLOCKS];
}
double * xdst = new double[NDST];
double * ydst = new double[NDST];
double * xref = new double[NDST];
double * yref = new double[NDST];
if (f)
{
if (mragfile)
{
printf("mrag file!\n");
fread(x0s, sizeof(double), NBLOCKS, f);
fread(y0s, sizeof(double), NBLOCKS, f);
fread(hs, sizeof(double), NBLOCKS, f);
for(int b =0; b < NBLOCKS; b++)
for(int iy = 0; iy < BLOCKSIZE; ++iy)
for(int ix = 0; ix < BLOCKSIZE; ++ix)
{
xdst[ix + BLOCKSIZE * iy + BS2 * b] = x0s[b] + ix * hs[b];
ydst[ix + BLOCKSIZE * iy + BS2 * b] = y0s[b] + iy * hs[b];
}
}
else //if (!mragfile)
{
fread(xdst, sizeof(double), NDST, f);
fread(ydst, sizeof(double), NDST, f);
fread(xref, sizeof(double), NDST, f);
}
if (!potential)
{
fread(yref, sizeof(double), NDST, f);
for(int i = 0; i < NDST; ++i)
{
const double tmp0 = xref[i] * (2 * M_PI);
const double tmp1 = yref[i] * (2 * M_PI);
xref[i] = -tmp1;
yref[i] = tmp0;
}
}
}
else
{
for(int i = 0; i < NDST; ++i)
{
xdst[i] = (drand48() - 0.25) * 10;
ydst[i] = (drand48() - 0.75) * 20;
}
}
const bool mrag = mragfile || NDST % BS2 == 0;
if (mrag && !mragfile)
{
NBLOCKS = NDST / BS2;
printf("ndst: %d i have found %d blocks\n", NDST, NBLOCKS);
assert(NDST % BS2 == 0);
x0s = new double[NBLOCKS];
y0s = new double[NBLOCKS];
hs = new double[NBLOCKS];
for(int i = 0; i < NBLOCKS; ++i)
{
x0s[i] = xdst[i * BS2];
y0s[i] = ydst[i * BS2];
hs[i] = xdst[i * BS2 + 1] - xdst[i * BS2];
}
}
const double eps = std::numeric_limits<double>::epsilon() * 10;
double * xtargets = new double[NDST];
double * ytargets = new double[NDST];
printf("Testing %s with %d sources and %d targets (theta %.3e)...\n", (potential ? "POTENTIAL" : "FORCE"), NSRC, NDST, theta);
const double tstart = omp_get_wtime();
if (potential)
treecode_potential(theta, xsrc, ysrc, sources, NSRC, xdst, ydst, NDST, xtargets);
else
if (mrag)
{
printf("MRAG LAYOUT\n");
treecode_force_mrag(theta, xsrc, ysrc, sources, NSRC, x0s, y0s, hs, NBLOCKS, xtargets, ytargets);
}
else
{
printf("This file format is not support for velocities.\n");
abort();
}
const double tend = omp_get_wtime();
printf("\x1b[94msolved in %.2f ms\x1b[0m\n", (tend - tstart) * 1e3);
if (verify)
{
const int OFFSET = 7;
const int JUMP = 433;
if (potential)
#pragma omp parallel for
for(int i = OFFSET; i < NDST; i += JUMP)
{
const double xd = xdst[i];
const double yd = ydst[i];
double s = 0;
for(int j = 0; j < NSRC; ++j)
{
const double xr = xd - xsrc[j];
const double yr = yd - ysrc[j];
const double r = sqrt(xr * xr + yr * yr + eps);
s += log(r) * sources[j];
}
xref[i] = s;
}
else
#pragma omp parallel for
for(int i = OFFSET; i < NDST; i += JUMP)
{
const double xd = xdst[i];
const double yd = ydst[i];
double xs = 0, ys = 0;
for(int j = 0; j < NSRC; ++j)
{
const double xr = xd - xsrc[j];
const double yr = yd - ysrc[j];
const double factor = sources[j] / (xr * xr + yr * yr + eps);
xs += xr * factor;
ys += yr * factor;
}
xref[i] = xs;
yref[i] = ys;
}
if (verify)
{
std::vector<double> a, b, c, d;
for(int i = OFFSET; i < NDST; i += JUMP)
{
a.push_back(xref[i]);
b.push_back(xtargets[i]);
c.push_back(yref[i]);
d.push_back(ytargets[i]);
}
check(&a[0], &b[0], a.size());
if (!potential)
check(&c[0], &d[0], c.size());
}
}
if (mrag)
{
delete [] x0s;
delete [] y0s;
delete [] hs;
}
delete [] xdst;
delete [] ydst;
delete [] xtargets;
delete [] ytargets;
delete [] xref;
delete [] yref;
delete [] xsrc;
delete [] ysrc;
delete [] sources;
printf("TEST PASSED.\n");
}
int main(int argc, char ** argv)
{
srand48(1451);
double theta = 1;
bool verify = true;
if (argc > 1)
theta = atof(argv[1]);
if (argc > 2)
tol = atof(argv[2]);
if (argc > 3)
verify = strcmp(argv[3], "profile") != 0;
enum TestType { V_TEST=1, P_TEST=2, PV_TEST=3};
auto file2test = [&] (const char * filename, bool mragfile = false, TestType testt = PV_TEST)
{
if (access(filename, R_OK) == -1)
{
printf("WARNING: reference file <%s> not found, i skip this test.\n", filename);
return;
}
else
printf("reading from <%s> ...\n", filename);
FILE * fin = fopen(filename, "r");
assert(fin && sizeof(double) == sizeof(double));
if (!mragfile && (testt & P_TEST))
test(theta, tol, fin, true, verify);
fseek(fin, 0, SEEK_SET);
if (testt & V_TEST)
test(theta, tol * 100, fin, false, verify, mragfile);
fclose(fin);
};
file2test("testDiego/diegoBinaryN400", false, P_TEST);
file2test("testDiego/diegoBinaryN2000", false, P_TEST);
file2test("testDiego/diegoBinaryN12000", false, P_TEST);
file2test("diegoVel/velocityPoissonFishLmax6", false, V_TEST);
file2test("diegoVel/velocityPoissonCylUnif2048", false, V_TEST);
file2test("diegoVel/velocityPoissonFishLmax8Early", false, V_TEST);
file2test("diegoVel/velocityPoissonFishLmax8Late", false, V_TEST);
file2test("testSid/diegoSolverCylUniform", true, V_TEST);
file2test("testSid/diegoSolverAdaptiveGrid", true, V_TEST);
file2test("testSid/diegoVelTestsDec10", true, V_TEST);
file2test("testSid/diegoVelTestsDec14", true, V_TEST);
#if 0
for(int itest = 0; itest < 10; ++itest)
{
printf("test nr %d\n", itest);
test(theta, tol, NULL, true, verify);
test(theta, tol * 100, NULL, false, verify);
}
#endif
}