forked from BoxLib-Codes/BoxLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTracerParticles.cpp
More file actions
403 lines (326 loc) · 12.2 KB
/
Copy pathTracerParticles.cpp
File metadata and controls
403 lines (326 loc) · 12.2 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
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
#include <TracerParticles.H>
//
// Uses midpoint method to advance particles using umac.
//
void
TracerParticleContainer::AdvectWithUmac (MultiFab* umac, int lev, Real dt)
{
BL_PROFILE("TracerParticleContainer::AdvectWithUmac()");
BL_ASSERT(OK(true, lev, umac[0].nGrow()-1));
BL_ASSERT(lev >= 0 && lev < m_particles.size());
D_TERM(BL_ASSERT(umac[0].nGrow() >= 1);,
BL_ASSERT(umac[1].nGrow() >= 1);,
BL_ASSERT(umac[2].nGrow() >= 1););
D_TERM(BL_ASSERT(!umac[0].contains_nan());,
BL_ASSERT(!umac[1].contains_nan());,
BL_ASSERT(!umac[2].contains_nan()););
const Real strttime = ParallelDescriptor::second();
const Geometry& geom = m_gdb->Geom(lev);
const Real* dx = geom.CellSize();
const Real* plo = geom.ProbLo();
PArray<MultiFab> umac_pointer;
// We assume that if umac[0]'s boxArray matches then the others will too...
if (OnSameGrids(lev, umac[0]))
{
umac_pointer.resize(BL_SPACEDIM, PArrayNoManage);
for (int i = 0; i < BL_SPACEDIM; i++)
umac_pointer.set(i, &umac[i]);
}
else
{
umac_pointer.resize(BL_SPACEDIM, PArrayManage);
for (int i = 0; i < BL_SPACEDIM; i++)
{
int ng = umac[i].nGrow();
umac_pointer.set(i, new MultiFab(m_gdb->ParticleBoxArray(lev),
umac[i].nComp(),
ng,
m_gdb->ParticleDistributionMap(lev),
Fab_allocate,
IntVect::TheDimensionVector(i)));
umac_pointer[i].copy(umac[i],0,0,umac[i].nComp(),ng,ng);
}
}
for (int ipass = 0; ipass < 2; ipass++)
{
PMap& pmap = m_particles[lev];
for (auto& kv : pmap)
{
const int grid = kv.first;
PBox& pbox = kv.second;
const int n = pbox.size();
FArrayBox* fab[BL_SPACEDIM] = { D_DECL(&umac_pointer[0][grid],
&umac_pointer[1][grid],
&umac_pointer[2][grid]) };
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (int i = 0; i < n; i++)
{
ParticleType& p = pbox[i];
if (p.m_id <= 0) continue;
BL_ASSERT(p.m_grid == grid);
const Real len[BL_SPACEDIM] = { D_DECL((p.m_pos[0]-plo[0])/dx[0] + Real(0.5),
(p.m_pos[1]-plo[1])/dx[1] + Real(0.5),
(p.m_pos[2]-plo[2])/dx[2] + Real(0.5)) };
const IntVect cell(D_DECL(floor(len[0]), floor(len[1]), floor(len[2])));
const Real frac[BL_SPACEDIM] = { D_DECL(len[0]-cell[0], len[1]-cell[1], len[2]-cell[2]) };
for (int d = 0; d < BL_SPACEDIM; d++)
{
IntVect ecell = cell;
ecell[d] = p.m_cell[d] + 1;
Real efrac[BL_SPACEDIM] = { D_DECL(frac[0], frac[1], frac[2]) };
efrac[d] = (p.m_pos[d]-plo[d])/dx[d] - p.m_cell[d];
for (int j = 0; j < BL_SPACEDIM; j++)
{
if (efrac[j] > 1) efrac[j] = 1;
if (efrac[j] < 0) efrac[j] = 0;
}
const Real vel = ParticleBase::InterpDoit(*fab[d], ecell, efrac, 0);
if (ipass == 0)
{
//
// Save old position and the vel & predict location at dt/2.
//
p.m_data[d] = p.m_pos[d];
p.m_pos[d] += 0.5*dt*vel;
}
else
{
//
// Update to final time using the orig position and the vel at dt/2.
//
p.m_pos[d] = p.m_data[d] + dt*vel;
// Save the velocity for use in Timestamp().
p.m_data[d] = vel;
}
}
ParticleBase::RestrictedWhere(p,m_gdb, umac[0].nGrow());
}
}
}
if (m_verbose > 1)
{
Real stoptime = ParallelDescriptor::second() - strttime;
#ifdef BL_LAZY
Lazy::QueueReduction( [=] () mutable {
#endif
ParallelDescriptor::ReduceRealMax(stoptime,ParallelDescriptor::IOProcessorNumber());
if (ParallelDescriptor::IOProcessor())
{
std::cout << "TracerParticleContainer::AdvectWithUmac() time: " << stoptime << '\n';
}
#ifdef BL_LAZY
});
#endif
}
}
//
// Uses midpoint method to advance particles using cell-centered velocity
//
void
TracerParticleContainer::AdvectWithUcc (const MultiFab& Ucc, int lev, Real dt)
{
BL_ASSERT(Ucc.nGrow() > 0);
BL_ASSERT(OK(true, lev, Ucc.nGrow()-1));
BL_ASSERT(lev >= 0 && lev < m_particles.size());
BL_ASSERT(!Ucc.contains_nan());
const Real strttime = ParallelDescriptor::second();
const Geometry& geom = m_gdb->Geom(lev);
BL_ASSERT(OnSameGrids(lev,Ucc));
int idx[BL_SPACEDIM] = {D_DECL(0,1,2)};
for (int ipass = 0; ipass < 2; ipass++)
{
PMap& pmap = m_particles[lev];
for (auto& kv : pmap)
{
const int grid = kv.first;
PBox& pbox = kv.second;
const int n = pbox.size();
const FArrayBox& fab = Ucc[grid];
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (int i = 0; i < n; i++)
{
ParticleType& p = pbox[i];
if (p.m_id <= 0) continue;
BL_ASSERT(p.m_grid == grid);
Real v[BL_SPACEDIM];
ParticleBase::Interp(p, geom, fab, idx, v, BL_SPACEDIM);
if (ipass == 0) {
//
// Save old position and the vel & predict location at dt/2.
//
for (int d = 0; d < BL_SPACEDIM; d++)
{
p.m_data[d] = p.m_pos[d];
p.m_pos[d] += 0.5*dt*v[d];
}
} else {
//
// Update to final time using the orig position and the vel at dt/2.
//
for (int d = 0; d < BL_SPACEDIM; d++)
{
p.m_pos[d] = p.m_data[d] + dt*v[d];
// Save the velocity for use in Timestamp().
p.m_data[d] = v[d];
}
}
ParticleBase::RestrictedWhere(p,m_gdb, Ucc.nGrow());
}
}
}
if (m_verbose > 1)
{
Real stoptime = ParallelDescriptor::second() - strttime;
#ifdef BL_LAZY
Lazy::QueueReduction( [=] () mutable {
#endif
ParallelDescriptor::ReduceRealMax(stoptime,ParallelDescriptor::IOProcessorNumber());
if (ParallelDescriptor::IOProcessor())
{
std::cout << "TracerParticleContainer::AdvectWithUcc() time: " << stoptime << '\n';
}
#ifdef BL_LAZY
});
#endif
}
}
void
TracerParticleContainer::Timestamp (const std::string& basename,
const MultiFab& mf,
int lev,
Real time,
const std::vector<int>& indices)
{
BL_PROFILE("TracerParticleContainer::Timestamp()");
//
// basename -> base filename for the output file
// mf -> the multifab
// lev -> level to check for particles
// time -> simulation time (will be recorded in Timestamp file)
// indices -> indices into mf that we output
//
BL_ASSERT(lev >= 0);
BL_ASSERT(time >= 0);
BL_ASSERT(!basename.empty());
BL_ASSERT(lev <= m_gdb->finestLevel());
const Real strttime = ParallelDescriptor::second();
const int MyProc = ParallelDescriptor::MyProc();
const int NProcs = ParallelDescriptor::NProcs();
// We'll spread the output over this many files.
int nOutFiles(64);
ParmParse pp("particles");
pp.query("particles_nfiles",nOutFiles);
if(nOutFiles == -1) {
nOutFiles = NProcs;
}
nOutFiles = std::max(1, std::min(nOutFiles,NProcs));
const int nSets = ((NProcs + (nOutFiles - 1)) / nOutFiles);
const int mySet = (MyProc / nOutFiles);
for (int iSet = 0; iSet < nSets; ++iSet)
{
if (mySet == iSet)
{
//
// Do we have any particles at this level that need writing?
//
bool gotwork = false;
const PMap& pmap = m_particles[lev];
for (const auto& kv : pmap) {
for (const auto& p : kv.second) {
if (p.m_id > 0) {
gotwork = true;
break;
}
}
if (gotwork) break;
}
if (gotwork)
{
std::string FileName = BoxLib::Concatenate(basename + '_', MyProc % nOutFiles, 2);
std::ofstream TimeStampFile;
VisMF::IO_Buffer io_buffer(VisMF::IO_Buffer_Size);
TimeStampFile.rdbuf()->pubsetbuf(io_buffer.dataPtr(), io_buffer.size());
TimeStampFile.open(FileName.c_str(), std::ios::out|std::ios::app|std::ios::binary);
TimeStampFile.setf(std::ios_base::scientific,std::ios_base::floatfield);
TimeStampFile.precision(10);
TimeStampFile.seekp(0, std::ios::end);
if (!TimeStampFile.good())
BoxLib::FileOpenFailed(FileName);
const int M = indices.size();
const BoxArray& ba = mf.boxArray();
std::vector<Real> vals(M);
for (const auto& kv : pmap)
{
const int grid = kv.first;
const PBox& pbox = kv.second;
const Box& bx = ba[grid];
const FArrayBox& fab = mf[grid];
for (const auto& p : pbox)
{
if (p.m_id <= 0) continue;
const IntVect& iv = ParticleBase::Index(p, m_gdb->Geom(lev));
if (!bx.contains(iv) && !ba.contains(iv)) continue;
BL_ASSERT(p.m_lev == lev);
BL_ASSERT(p.m_grid == grid);
TimeStampFile << p.m_id << ' ' << p.m_cpu << ' ';
D_TERM(TimeStampFile << p.m_pos[0] << ' ';,
TimeStampFile << p.m_pos[1] << ' ';,
TimeStampFile << p.m_pos[2] << ' ';);
TimeStampFile << time;
//
// AdvectWithUmac stores the velocity in m_data ...
//
D_TERM(TimeStampFile << ' ' << p.m_data[0];,
TimeStampFile << ' ' << p.m_data[1];,
TimeStampFile << ' ' << p.m_data[2];);
if (M > 0)
{
ParticleBase::Interp(p,m_gdb->Geom(p.m_lev),fab,&indices[0],&vals[0],M);
for (int i = 0; i < M; i++)
{
TimeStampFile << ' ' << vals[i];
}
}
TimeStampFile << '\n';
}
}
TimeStampFile.flush();
TimeStampFile.close();
}
const int iBuff = 0;
const int wakeUpPID = (MyProc + nOutFiles);
const int tag = (MyProc % nOutFiles);
if (wakeUpPID < NProcs)
ParallelDescriptor::Send(&iBuff, 1, wakeUpPID, tag);
}
if (mySet == (iSet + 1))
{
//
// Next set waits.
//
int iBuff;
const int waitForPID = (MyProc - nOutFiles);
const int tag = (MyProc % nOutFiles);
ParallelDescriptor::Recv(&iBuff, 1, waitForPID, tag);
}
}
if (m_verbose > 1)
{
Real stoptime = ParallelDescriptor::second() - strttime;
#ifdef BL_LAZY
Lazy::QueueReduction( [=] () mutable {
#endif
ParallelDescriptor::ReduceRealMax(stoptime,ParallelDescriptor::IOProcessorNumber());
if (ParallelDescriptor::IOProcessor())
{
std::cout << "TracerParticleContainer::Timestamp: lev: " << lev << " time: " << stoptime << '\n';
}
#ifdef BL_LAZY
});
#endif
}
}