-
Notifications
You must be signed in to change notification settings - Fork 8
/
fvmhd3d.cpp
396 lines (331 loc) · 12.1 KB
/
fvmhd3d.cpp
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
#include <sys/stat.h>
#include "fvmhd3d.h"
#ifndef __MACOSX_
#define __LINUX__
#endif
#ifdef __MACOSX__
#include <Accelerate/Accelerate.h>
#include <xmmintrin.h>
inline void fpe_catch() {
_mm_setcsr( _MM_MASK_MASK &~
(_MM_MASK_OVERFLOW|_MM_MASK_INVALID|_MM_MASK_DIV_ZERO) );
}
#elif defined __LINUX__
#include <fenv.h>
void fpe_catch(void) {
/* Enable some exceptions. At startup all exceptions are masked. */
feenableexcept(FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW);
}
#else
crap
void fpe_catch(void) {}
#endif
namespace fvmhd3d
{
CProxy_Main mainProxy;
CProxy_System systemProxy;
CProxy_globalDomains globalDomainsProxy;
int numElements;
char problem_string[256];
char problem_path [256];
boundary global_domain;
double dtWall_doLB;
double dtWall_doCheckPoint;
vec3 global_domain_size;
#if 0
CProxy_CkCacheManager cachePtcl;
CProxy_CkCacheManager cacheFluid;
#endif
Main::Main(CkMigrateMessage* msg)
{
IsRestarting = true;
}
Main::Main(CkArgMsg *m)
{
IsRestarting = false;
fpe_catch();
const int numElements_min = 8;
if (m->argc <= 2)
{
CkPrintf("Usage: %s numElements output_path \n", m->argv[0]);
CkExit();
}
assert(m->argc > 2);
numElements = atoi(m->argv[1]);
sprintf(problem_path, "%s", m->argv[2]);
CkPrintf(" [main] numElements= %d \n", numElements);
CkPrintf(" [main] problem_path= %s \n", problem_path);
if (numElements < numElements_min)
{
CkPrintf("FATAL: numElements must be >= %d \n", numElements_min);
CkExit();
}
assert(numElements >= numElements_min);
dtWall_doLB = m->argc > 3 ? atof(m->argv[3]) : 120.0;
dtWall_doCheckPoint = m->argc > 4 ? atof(m->argv[4]) : 420.0;
CkPrintf(" [main] loadBalancer will be called every %g sec \n", dtWall_doLB);
CkPrintf(" [main] checkPoint will be called every %g sec \n", dtWall_doCheckPoint);
CkPrintf(" [main] execline: ");
for (int i = 0; i < m->argc; i++)
CkPrintf("%s ", m->argv[i]);
CkPrintf("\n");
Problem_set_global_domain();
CProxy_globalDomains _globalDomainsProxy = CProxy_globalDomains::ckNew();
globalDomainsProxy = _globalDomainsProxy;
systemProxy = CProxy_System::ckNew(numElements);
#if 0
const int cacheSize = 128;
cachePtcl = CProxy_CkCacheManager::ckNew(cacheSize, systemProxy.ckLocMgr()->getGroupID());
cacheFluid = CProxy_CkCacheManager::ckNew(cacheSize, systemProxy.ckLocMgr()->getGroupID());
#endif
thisProxy.startSimulation();
}
void Main::startSimulation()
{
/**************** generate Geometry ***************/
CkPrintf(" [main::startSimulation] generate_geometry\n");
int nRelax = 0;
{
CkReductionMsg *msg;
systemProxy.generateGeometry(1, CkCallbackResumeThread((void*&)msg));
nRelax = std::max(nRelax, *(int*)msg->getData());
delete msg;
}
CkPrintf(" [main::startSimulation] relaxing geometry in %d Lloyd's iterations \n", nRelax);
/**************** relax Geometry ***************/
for (int iter = 0; iter < nRelax + 1; iter++)
{
CkPrintf(" [main::startSimulation] domainDecomposition\n");
mainProxy.domainDecomposition(CkCallbackResumeThread());
CkPrintf(" [main::startSimulation] setDomains\n");
globalDomainsProxy.setDomains(proc_domains, CkCallbackResumeThread());
CkPrintf(" [main::startSimulation] move particles around \n");
systemProxy.moveParticles(CkCallbackResumeThread());
const double t0 = CkWallTimer();
CkPrintf(" [main::startSimulation] globalMesh_build \n");
CkReductionMsg *msg;
systemProxy.globalMesh_build(iter < nRelax, CkCallbackResumeThread((void*&)msg));
const real v0 = global_domain_size.x*global_domain_size.y*global_domain_size.z;
const real v1 = *(double*)msg->getData();
delete msg;
CkPrintf(" [main::startSimulation] volume: exact= %g compute= %g diff= %g [ %g ] \n",
v0, v1, v1 - v0, (v1-v0)/v0);
CkPrintf(" [main::startSimulation] done in %g sec \n", CkWallTimer() - t0);
{
const double t0 = CkWallTimer();
CkPrintf(" *** System::loadBalancer() call \n");
systemProxy.loadBalancer(CkCallbackResumeThread());
CkPrintf(" *** System::loadBalancer() done in %g sec \n", CkWallTimer() - t0);
}
}
/**************** generate IC ***************/
{
CkPrintf(" [main::startSimulation] generate_IC\n");
const double t0 = CkWallTimer();
CkReductionMsg *msg;
systemProxy.generateIC(1, CkCallbackResumeThread((void*&)msg));
CkPrintf(" [main::startSimulation] 1 done in %g sec :: \n", CkWallTimer() - t0);
const real v0 = global_domain_size.x*global_domain_size.y*global_domain_size.z;
const real v1 = (*(double*)msg->getData());
delete msg;
CkPrintf(" [main::startSimulation]1 volume: exact= %g compute= %g diff= %g [ %g ] \n \n",
v0, v1, v1 - v0, (v1-v0)/v0);
}
/************** dump IC into a file *************/
E0 = computeEnergy();
E0.print_mass (" M1: ");
E0.print_energy (" E0: ");
E0.print_momentum(" M0: ");
{
double dt_restart;
{
CkReductionMsg *msg;
systemProxy.get_info(CkCallbackResumeThread((void*&)msg));
t_global = ((double*)msg->getData())[0];
t_end = ((double*)msg->getData())[1];
dt_snap = ((double*)msg->getData())[2];
dt_restart = ((double*)msg->getData())[3];
iteration = (int)((double*)msg->getData())[4];
global_n = (int)((double*)msg->getData())[5];
delete msg;
}
{
CkVec<char> filename(256);
char filepath[256];
sprintf(filepath, "%s/init", problem_path);
mkdir(filepath, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IWOTH);
sprintf(&filename[0], "%s/", filepath);
systemProxy.dump_binary(filename, global_n, CkCallbackResumeThread());
}
CkPrintf(" [main::startSimulation] :: start_iteration= %d global_n= %d t_global= %g t_end= %g dt_snap= %g dt_restart = %g \n",
iteration, global_n, t_global, t_end, dt_snap, dt_restart);
t_snap = t_global + dt_snap;
}
nmoved = 0;
nfac = 5.2;
dtWall_lastLB = -1.0;
checkpoint_flag = true;
tWall0 = CkWallTimer();
thisProxy.doIterations();
}
void Main::doIterations()
{
if (dtWall_lastLB == -1.0)
{
const double t0 = CkWallTimer();
CkPrintf(" *** System::loadBalancer() call \n");
systemProxy.loadBalancer(CkCallbackResumeThread());
CkPrintf(" *** System::loadBalancer() done in %g sec \n", CkWallTimer() - t0);
dtWall_lastLB = 0.0;
}
/**************** start Simulation **********/
double dtWall_lastCheckPoint = 0.0;
while (t_global < t_end)
{
const double t0 = CkWallTimer();
CkReductionMsg *msg;
#if 0
systemProxy.Iterate(CkCallbackResumeThread((void*&)msg));
#else
const double t00 = CkWallTimer();
systemProxy.GetActiveList (CkCallbackResumeThread());
const double t10 = CkWallTimer();
systemProxy.BuildLocalMesh(CkCallbackResumeThread());
const double t20 = CkWallTimer();
systemProxy.RefineDerefine(CkCallbackResumeThread());
const double t30 = CkWallTimer();
systemProxy.ComputeFluidUpdate(CkCallbackResumeThread());
const double t40 = CkWallTimer();
systemProxy.CompleteIteration(CkCallbackResumeThread((void*&)msg));
const double t50 = CkWallTimer();
#if 0
CkPrintf(" dt_all= %g :: Active= %g Mesh= %g RefDeref= %g Fluid= %g Complete= %g \n",
t50 - t00, t10-t00, t20-t10, t30-t20, t40-t30, t50-t40);
#endif
#endif
t_global = ((double*)msg->getData())[0];
iteration = ((double*)msg->getData())[2];
const double dt_global = ((double*)msg->getData())[1];
const int n_active = ((double*)msg->getData())[3];
const int n_global = ((double*)msg->getData())[4];
global_n = n_global;
delete msg;
const double dtWall = CkWallTimer() - t0;
dtWall_lastLB += dtWall;
dtWall_lastCheckPoint += dtWall;
CkPrintf("iter= %d :: t= %g dt= %g Nact= %d Nglb= %d [ %g ] done in %g sec :: runtime= %g h [ %g %g %g ] \n",
iteration, t_global, dt_global, n_active, n_global, 1.0*n_active/n_global,
dtWall, (CkWallTimer() - tWall0)/3600.0,
n_global/dtWall, n_active/dtWall, n_active/dtWall/CkNumPes());
assert(n_active > 0);
assert(n_global > 0);
nmoved += n_active;
if (n_active == n_global)
{
const Energy E1 = computeEnergy();
const Energy dE = (E1 - E0)/E0.abs();
E1.print_energy (" E1: ");
dE.print_energy (" dE: ");
E1.print_mass (" M1: ");
dE.print_mass (" dM: ");
E1.print_momentum(" Mom1: ");
dE.print_momentum(" dMom: ");
}
// if (n_active == n_global)
if (t_global >= t_snap)
{
assert(n_active == n_global);
CkVec<char> filename(256);
char filepath[256];
sprintf(filepath, "%s/iter%.6d", problem_path, (int)((t_snap+0.001*dt_snap)/dt_snap));
mkdir(filepath, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IWOTH);
sprintf(&filename[0], "%s/", filepath);
CkPrintf(" >>> Writing snapshot to %s \n", filepath);
systemProxy.dump_binary(filename, n_global, CkCallbackResumeThread());
t_snap += dt_snap;
}
if ((nmoved > (unsigned int)(nfac*n_global)) && (n_active == n_global))
// if(false)
// if (t_global >= t_snap)
{
nmoved = 0;
CkPrintf(" [main::startSimulation] domainDecomposition\n");
mainProxy.domainDecomposition(CkCallbackResumeThread());
CkPrintf(" [main::startSimulation] setDomains\n");
globalDomainsProxy.setDomains(proc_domains, CkCallbackResumeThread());
CkPrintf(" [main::startSimulation] move particles around \n");
systemProxy.moveParticles(CkCallbackResumeThread());
const double t0 = CkWallTimer();
CkPrintf(" [main::startSimulation] globalMesh_build \n");
CkReductionMsg *msg;
systemProxy.globalMesh_build(false, CkCallbackResumeThread((void*&)msg));
const real v0 = global_domain_size.x*global_domain_size.y*global_domain_size.z;
const real v1 = *(double*)msg->getData();
delete msg;
CkPrintf(" [main::startSimulation] volume: exact= %g compute= %g diff= %g [ %g ] \n",
v0, v1, v1 - v0, (v1-v0)/v0);
CkPrintf(" [main::startSimulation] done in %g sec \n", CkWallTimer() - t0);
}
if (dtWall_lastLB > dtWall_doLB)
{
dtWall_lastLB = 0.0;
const double t0 = CkWallTimer();
CkPrintf(" *** System::loadBalancer() call \n");
systemProxy.loadBalancer(CkCallbackResumeThread());
CkPrintf(" *** System::loadBalancer() done in %g sec \n", CkWallTimer() - t0);
}
if (dtWall_lastCheckPoint > dtWall_doCheckPoint)
{
char filepath[256];
if (checkpoint_flag)
{
sprintf(filepath, "%s/checkpoint1", problem_path);
checkpoint_flag = false;
}
else
{
sprintf(filepath, "%s/checkpoint2", problem_path);
checkpoint_flag = true;
}
CkStartCheckpoint(filepath, CkCallback(CkIndex_Main::restart(), thisProxy));
return;
}
}
CkExit();
}
void Main::restart()
{
if (IsRestarting)
{
CkPrintf(" --- restarting --- \n");
dtWall_lastLB = -1.0;
IsRestarting = false;
CkVec<char> filename(256);
char filepath[256];
sprintf(filepath, "%s/restart", problem_path);
mkdir(filepath, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IWOTH);
sprintf(&filename[0], "%s/", filepath);
CkPrintf(" >>> Writing snapshot to %s \n", filepath);
systemProxy.dump_binary(filename, global_n, CkCallbackResumeThread());
tWall0 = CkWallTimer();
}
thisProxy.doIterations();
}
void Main::pup(PUP::er &p)
{
CBase_Main::pup(p);
p|proc_domains;
p|dtWall_lastLB;
p|t_global;
p|t_end;
p|t_snap;
p|dt_snap;
p|iteration;
p|nfac;
p|nmoved;
p|E0;
p|checkpoint_flag;
p|global_n;
}
}
#include "fvmhd3d.def.h"