-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathdataBlock.cpp
More file actions
406 lines (338 loc) · 11.9 KB
/
dataBlock.cpp
File metadata and controls
406 lines (338 loc) · 11.9 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
404
405
406
// ***********************************************************************************
// Idefix MHD astrophysical code
// Copyright(C) Geoffroy R. J. Lesur <geoffroy.lesur@univ-grenoble-alpes.fr>
// and other code contributors
// Licensed under CeCILL 2.1 License, see COPYING for more information
// ***********************************************************************************
#include <algorithm>
#include <memory>
#include <string>
#include "idefix.hpp"
#include "dataBlock.hpp"
#include "fluid.hpp"
#include "gravity.hpp"
#include "planetarySystem.hpp"
#include "vtk.hpp"
#include "dump.hpp"
#ifdef WITH_HDF5
#include "xdmf.hpp"
#endif
DataBlock::DataBlock(Grid &grid, Input &input) {
idfx::pushRegion("DataBlock::DataBlock");
this->mygrid=&grid;
// Make a local copy of the grid for future usage.
GridHost gridHost(grid);
gridHost.SyncFromDevice();
// Get the number of points from the parent grid object
for(int dir = 0 ; dir < 3 ; dir++) {
nghost[dir] = grid.nghost[dir];
// Domain decomposition: decompose the full domain size in grid by the number of processes
// in that direction
np_int[dir] = grid.np_int[dir]/grid.nproc[dir];
np_tot[dir] = np_int[dir]+2*nghost[dir];
// Boundary conditions
if (grid.xproc[dir]==0) {
lbound[dir] = grid.lbound[dir];
if(lbound[dir]==axis) this->haveAxis = true;
} else {
lbound[dir] = internal;
}
if (grid.xproc[dir] == grid.nproc[dir]-1) {
rbound[dir] = grid.rbound[dir];
if(rbound[dir]==axis) this->haveAxis = true;
} else {
rbound[dir] = internal;
}
beg[dir] = grid.nghost[dir];
end[dir] = grid.nghost[dir]+np_int[dir];
// Where does this datablock starts and end in the grid?
// This assumes even distribution of points between procs
gbeg[dir] = grid.nghost[dir] + grid.xproc[dir]*np_int[dir];
gend[dir] = grid.nghost[dir] + (grid.xproc[dir]+1)*np_int[dir];
// Local start and end of current datablock
xbeg[dir] = gridHost.xl[dir](gbeg[dir]);
xend[dir] = gridHost.xr[dir](gend[dir]-1);
}
// Allocate the required fields
std::string label;
for(int dir = 0 ; dir < 3 ; dir++) {
label = "DataBlock_x" + std::to_string(dir);
x[dir] = IdefixArray1D<real>(label, np_tot[dir]);
label = "DataBlock_xr" + std::to_string(dir);
xr[dir] = IdefixArray1D<real>(label,np_tot[dir]);
label = "DataBlock_xl" + std::to_string(dir);
xl[dir] = IdefixArray1D<real>(label,np_tot[dir]);
label = "DataBlock_dx" + std::to_string(dir);
dx[dir] = IdefixArray1D<real>(label,np_tot[dir]);
label = "DataBlock_xgc" + std::to_string(dir);
xgc[dir] = IdefixArray1D<real>(label,np_tot[dir]);
label = "DataBlock_A" + std::to_string(dir);
A[dir] = IdefixArray3D<real>(label,
np_tot[KDIR]+KOFFSET, np_tot[JDIR]+JOFFSET, np_tot[IDIR]+IOFFSET);
}
dV = IdefixArray3D<real>("DataBlock_dV",np_tot[KDIR],np_tot[JDIR],np_tot[IDIR]);
#if GEOMETRY == SPHERICAL
rt = IdefixArray1D<real>("DataBlock_rt",np_tot[IDIR]);
sinx2m = IdefixArray1D<real>("DataBlock_sinx2m",np_tot[JDIR]);
tanx2m = IdefixArray1D<real>("DataBlock_tanx2m",np_tot[JDIR]);
sinx2 = IdefixArray1D<real>("DataBlock_sinx2",np_tot[JDIR]);
tanx2 = IdefixArray1D<real>("DataBlock_tanx2",np_tot[JDIR]);
dmu = IdefixArray1D<real>("DataBlock_dmu",np_tot[JDIR]);
#endif
// Initialize our sub-domain
this->ExtractSubdomain();
// Initialize the geometry
this->MakeGeometry();
// Initialise the state containers
// (by default, datablock only initialise the current state, which is a reference
// to arrays in the daughter object
this->states["current"] = StateContainer();
// Initialize the Dump object
this->dump = std::make_unique<Dump>(input, this);
// Initialize the VTK object
this->vtk = std::make_unique<Vtk>(input, this, "data");
// Init XDMF objects for HDF5 outputs
#ifdef WITH_HDF5
this->xdmf= std::make_unique<Xdmf>(input,this);
#endif
// Initialize the hydro object attached to this datablock
this->hydro = std::make_unique<Fluid<DefaultPhysics>>(grid, input, this);
// Initialise Fargo if needed
if(input.CheckBlock("Fargo")) {
this->fargo = std::make_unique<Fargo>(input, DefaultPhysics::nvar, this);
this->haveFargo = true;
}
// initialise planets if needed
if(input.CheckBlock("Planet")) {
this->planetarySystem = std::make_unique<PlanetarySystem>(input, this);
this->haveplanetarySystem = true;
}
// Initialise gravity if needed (automatically if planets are present)
if(input.CheckBlock("Gravity") || haveplanetarySystem) {
this->gravity = std::make_unique<Gravity>(input, this);
this->haveGravity = true;
}
// Initialise dust grains if needed
if(input.CheckBlock("Dust")) {
haveDust = true;
int nSpecies = input.Get<int>("Dust","nSpecies",0);
for(int i = 0 ; i < nSpecies ; i++) {
dust.emplace_back(std::make_unique<Fluid<DustPhysics>>(grid, input, this, i));
}
}
// Register variables that need to be saved in case of restart dump
dump->RegisterVariable(&t, "time");
dump->RegisterVariable(&dt, "dt");
idfx::popRegion();
}
/**
* @brief Construct a new Data Block as a subgrid
*
* @param subgrid : subgrid from which the local datablock should be extracted from
*/
DataBlock::DataBlock(SubGrid *subgrid) {
idfx::pushRegion("DataBlock:DataBlock(SubGrid)");
Grid *grid = subgrid->parentGrid;
this->mygrid = subgrid->grid.get();
// Make a local copy of the grid for future usage.
GridHost gridHost(*grid);
gridHost.SyncFromDevice();
// Get the number of points from the parent grid object
for(int dir = 0 ; dir < 3 ; dir++) {
nghost[dir] = grid->nghost[dir];
// Domain decomposition: decompose the full domain size in grid by the number of processes
// in that direction
np_int[dir] = grid->np_int[dir]/grid->nproc[dir];
np_tot[dir] = np_int[dir]+2*nghost[dir];
// Boundary conditions
if (grid->xproc[dir]==0) {
lbound[dir] = grid->lbound[dir];
if(lbound[dir]==axis) this->haveAxis = true;
} else {
lbound[dir] = internal;
}
if (grid->xproc[dir] == grid->nproc[dir]-1) {
rbound[dir] = grid->rbound[dir];
if(rbound[dir]==axis) this->haveAxis = true;
} else {
rbound[dir] = internal;
}
beg[dir] = grid->nghost[dir];
end[dir] = grid->nghost[dir]+np_int[dir];
// Where does this datablock starts and end in the grid?
// This assumes even distribution of points between procs
gbeg[dir] = grid->nghost[dir] + grid->xproc[dir]*np_int[dir];
gend[dir] = grid->nghost[dir] + (grid->xproc[dir]+1)*np_int[dir];
// Local start and end of current datablock
xbeg[dir] = gridHost.xl[dir](gbeg[dir]);
xend[dir] = gridHost.xr[dir](gend[dir]-1);
}
// Next we erase the info the slice direction
int refdir = subgrid->direction;
nghost[refdir] = 0;
np_int[refdir] = 1;
np_tot[refdir] = 1;
beg[refdir] = 0;
end[refdir] = 1;
gbeg[refdir] = subgrid->index;
gend[refdir] = subgrid->index+1;
xbeg[refdir] = gridHost.x[refdir](subgrid->index);
xend[refdir] = gridHost.x[refdir](subgrid->index);
// Reset gbeg/gend so that they don't refer anymore to the parent grid
gbeg[refdir] = 0;
gend[refdir] = 1;
// Allocate the required fields (only a limited set for a datablock from a subgrid)
std::string label;
for(int dir = 0 ; dir < 3 ; dir++) {
label = "DataBlock_x" + std::to_string(dir);
x[dir] = IdefixArray1D<real>(label, np_tot[dir]);
label = "DataBlock_xr" + std::to_string(dir);
xr[dir] = IdefixArray1D<real>(label,np_tot[dir]);
label = "DataBlock_xl" + std::to_string(dir);
xl[dir] = IdefixArray1D<real>(label,np_tot[dir]);
label = "DataBlock_dx" + std::to_string(dir);
dx[dir] = IdefixArray1D<real>(label,np_tot[dir]);
}
// Initialize our sub-domain
this->ExtractSubdomain();
idfx::popRegion();
}
void DataBlock::ResetStage() {
this->hydro->ResetStage();
if(haveDust) {
for(int i = 0 ; i < dust.size() ; i++) {
dust[i]->ResetStage();
}
}
}
void DataBlock::ConsToPrim() {
this->hydro->ConvertConsToPrim();
if(haveDust) {
for(int i = 0 ; i < dust.size() ; i++) {
dust[i]->ConvertConsToPrim();
}
}
}
void DataBlock::PrimToCons() {
this->hydro->ConvertPrimToCons();
if(haveDust) {
for(int i = 0 ; i < dust.size() ; i++) {
dust[i]->ConvertPrimToCons();
}
}
}
// Set the boundaries of the data structures in this datablock
void DataBlock::SetBoundaries() {
if(haveGridCoarsening) {
ComputeGridCoarseningLevels();
hydro->CoarsenFlow(hydro->Vc);
#if MHD==YES
hydro->CoarsenMagField(hydro->Vs);
#endif
if(haveDust) {
for(int i = 0 ; i < dust.size() ; i++) {
dust[i]->CoarsenFlow(dust[i]->Vc);
}
}
}
if(haveDust) {
for(int i = 0 ; i < dust.size() ; i++) {
dust[i]->boundary->SetBoundaries(t);
}
}
hydro->boundary->SetBoundaries(t);
}
void DataBlock::ShowConfig() {
if(idfx::psize>1) {
idfx::cout << "DataBlock: this process grid size is " << std::endl;
for(int dir = 0 ; dir < DIMENSIONS ; dir++) {
idfx::cout << "\t Direction X" << (dir+1) << ": " << xbeg[dir] << "...." << np_int[dir]
<< "...." << xend[dir] << std::endl;
}
}
hydro->ShowConfig();
if(haveFargo) fargo->ShowConfig();
if(haveplanetarySystem) planetarySystem->ShowConfig();
if(haveGravity) gravity->ShowConfig();
if(haveUserStepFirst) idfx::cout << "DataBlock: User's first step has been enrolled."
<< std::endl;
if(haveUserStepLast) idfx::cout << "DataBlock: User's last step has been enrolled."
<< std::endl;
if(haveDust) {
idfx::cout << "DataBlock: evolving " << dust.size() << " dust species." << std::endl;
// Only show the config the first dust specie
dust[0]->ShowConfig();
/*
for(int i = 0 ; i < dust.size() ; i++) {
dust[i]->ShowConfig();
}*/
}
}
real DataBlock::ComputeTimestep() {
// Compute the timestep using all of the enabled modules in the current dataBlock
// First with the hydro block
auto InvDt = hydro->InvDt;
real dt;
idefix_reduce("Timestep_reduction",
beg[KDIR], end[KDIR],
beg[JDIR], end[JDIR],
beg[IDIR], end[IDIR],
KOKKOS_LAMBDA (int k, int j, int i, real &dtmin) {
dtmin=FMIN(ONE_F/InvDt(k,j,i),dtmin);
},
Kokkos::Min<real>(dt));
if(haveDust) {
for(int n = 0 ; n < dust.size() ; n++) {
real dtDust;
auto InvDt = dust[n]->InvDt;
idefix_reduce("Timestep_reduction_dust",
beg[KDIR], end[KDIR],
beg[JDIR], end[JDIR],
beg[IDIR], end[IDIR],
KOKKOS_LAMBDA (int k, int j, int i, real &dtmin) {
dtmin=FMIN(ONE_F/InvDt(k,j,i),dtmin);
},
Kokkos::Min<real>(dtDust));
dt = std::min(dt,dtDust);
}
}
Kokkos::fence();
return(dt);
}
// Recompute magnetic fields from vector potential in dedicated fluids
void DataBlock::DeriveVectorPotential() {
if constexpr(DefaultPhysics::mhd) {
#ifdef EVOLVE_VECTOR_POTENTIAL
hydro->emf->EnforceVectorPotentialBoundary(hydro->Ve);
hydro->emf->ComputeMagFieldFromA(hydro->Ve, hydro->Vs);
#endif
}
}
void DataBlock::LaunchUserStepLast() {
if(haveUserStepLast) {
idfx::pushRegion("User::UserStepLast");
if(userStepLast != nullptr)
userStepLast(*this, this->t, this->dt);
else
IDEFIX_ERROR("UserStepLast not properly initialized");
idfx::popRegion();
}
}
void DataBlock::LaunchUserStepFirst() {
if(haveUserStepFirst) {
idfx::pushRegion("User::UserStepFirst");
if(userStepFirst != nullptr)
userStepFirst(*this, this->t, this->dt);
else
IDEFIX_ERROR("UserStepLast not properly initialized");
idfx::popRegion();
}
}
void DataBlock::EnrollUserStepLast(StepFunc func) {
haveUserStepLast = true;
userStepLast = func;
}
void DataBlock::EnrollUserStepFirst(StepFunc func) {
haveUserStepFirst = true;
userStepFirst = func;
}