-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.cpp
More file actions
433 lines (380 loc) · 14.4 KB
/
setup.cpp
File metadata and controls
433 lines (380 loc) · 14.4 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include <algorithm>
#include <math.h>
#include "idefix.hpp"
#include "setup.hpp"
#include "planet.hpp"
real wkzMinGlob;
real wkzMaxGlob;
real wkzDampingGlob;
real sigma0Glob;
real sigmaSlopeGlob;
real h0Glob;
real flaringIndexGlob;
real alphaGlob;
real densityFloorGlob;
real masstaperGlob;
real omegaGlob;
/*********************************************/
/**
Customized random number generator
Allow one to have consistent random numbers
generators on different architectures.
**/
/*********************************************/
real randm(void) {
const int a = 16807;
const int m = 2147483647;
static int in0 = 13763 + 2417*idfx::prank;
int q;
/* find random number */
q= (int) fmod((double) a * in0, m);
in0=q;
return((real) ((double) q/(double)m));
}
void MySoundSpeed(DataBlock &data, const real t, IdefixArray3D<real> &cs) {
real h0 = h0Glob;
real flaringIndex = flaringIndexGlob;
IdefixArray1D<real> x1=data.x[IDIR];
idefix_for("MySoundSpeed",0,data.np_tot[KDIR],0,data.np_tot[JDIR],0,data.np_tot[IDIR],
KOKKOS_LAMBDA (int k, int j, int i) {
real R = x1(i);
cs(k,j,i) = h0*pow(R,flaringIndex-0.5);
});
}
void Damping(Hydro *hydro, const real t, const real dtin) {
auto *data = hydro->data;
IdefixArray4D<real> Vc = hydro->Vc;
IdefixArray4D<real> Uc = hydro->Uc;
IdefixArray1D<real> x1 = data->x[IDIR];
IdefixArray1D<real> x2 = data->x[JDIR];
real h0 = h0Glob;
real sigma0 = sigma0Glob;
real sigmaSlope = sigmaSlopeGlob;
real flaringIndex = flaringIndexGlob;
real omega = omegaGlob;
real gamma = hydro->eos->GetGamma();
real dt = dtin;
bool isFargo = data->haveFargo;
real rmin{data->mygrid->xbeg[0]};
real wkzMin{wkzMinGlob};
real rmax{data->mygrid->xend[0]};
real wkzMax{wkzMaxGlob};
real wkzDamping{wkzDampingGlob};
idefix_for("Damping",
0, data->np_tot[KDIR],
0, data->np_tot[JDIR],
0, data->np_tot[IDIR],
KOKKOS_LAMBDA (int k, int j, int i) {
real R = x1(i);
real Vk = 1.0/sqrt(R);
/*
// Cooling function
real cs2 = h0*h0*pow(R,2*flaringIndex-1.0);
// cooling time in local Keplerian units
real tau = 0.1*pow(R,1.5);
real Ptarget = cs2*Vc(RHO,k,j,i);
Uc(ENG,k,j,i) += -dt*(Vc(PRS,k,j,i)-Ptarget)/(tau*(gamma-1.0));
*/
real lambda = 0.0;
// Damping function for poloidal velocity field in damping zones
// Damp whatever is at R<wkzMin and R>wkzMax
if (R<wkzMin) {
lambda = 1.0/(wkzDamping*2.0*M_PI*pow(rmin,1.5))*(1.0 - pow(sin(M_PI*( (R-rmin) / (wkzMin-rmin) )/2.0),2.0));
}
if (R>wkzMax) {
lambda = 1.0/(wkzDamping*2.0*M_PI*pow(rmax,1.5))*pow(sin(M_PI*( (R-wkzMax) / (rmax-wkzMax) )/2.0),2.0);
}
real rhoTarget = sigma0*pow(R,-sigmaSlope) ;
real vx2Target = 0.0;
if(!isFargo) {
vx2Target = Vk*sqrt(1.0-(1.0+sigmaSlope-2*flaringIndex)*h0*h0*pow(R,2*flaringIndex)) - omega * R;
}
// relaxation
real drho = lambda*(Vc(RHO,k,j,i)-rhoTarget);
real dvx1 = lambda*Vc(RHO,k,j,i)*Vc(VX1,k,j,i);
real dvx2 = lambda*Vc(RHO,k,j,i)*(Vc(VX2,k,j,i)-vx2Target);
// real dvx3 = lambda*Vc(RHO,k,j,i)*Vc(VX3,k,j,i);
real dmx1 = dvx1 + Vc(VX1,k,j,i) * drho;
real dmx2 = dvx2 + Vc(VX2,k,j,i) * drho;
// real dmx3 = dvx3 + Vc(VX3,k,j,i) * drho;
// Kinetic energy fluctuations due to above damping
// must be compensated in total energy conservation
/*
real deng = 0.5*(Vc(VX1,k,j,i)*Vc(VX1,k,j,i)
+Vc(VX2,k,j,i)*Vc(VX2,k,j,i))*drho
+Vc(VX3,k,j,i)*Vc(VX3,k,j,i))*drho
+ Vc(RHO,k,j,i) * (
Vc(VX1,k,j,i)*dvx1
+ Vc(VX2,k,j,i)*dvx2
+ Vc(VX3,k,j,i)*dvx3
);
*/
Uc(RHO,k,j,i) += -drho*dt;
Uc(MX1,k,j,i) += -dmx1*dt;
Uc(MX2,k,j,i) += -dmx2*dt;
// Uc(MX3,k,j,i) += -dmx3*dt;
// Uc(ENG,k,j,i) += -deng*dt;
});
}
// User-defined boundaries
void UserdefBoundary(Hydro *hydro, int dir, BoundarySide side, real t) {
auto *data = hydro->data;
IdefixArray4D<real> Vc = hydro->Vc;
IdefixArray1D<real> x1 = data->x[IDIR];
real sigmaSlope=sigmaSlopeGlob;
real omega = omegaGlob;
if(dir==IDIR) {
int ighost,ibeg,iend,sign;
if(side == left) {
ighost = data->beg[IDIR];
ibeg = 0;
iend = data->beg[IDIR];
sign=-1;
//return;
}
else if(side==right) {
ighost = data->end[IDIR]-1;
ibeg=data->end[IDIR];
iend=data->np_tot[IDIR];
sign=1;
}
idefix_for("UserDefBoundary",
0, data->np_tot[KDIR],
0, data->np_tot[JDIR],
ibeg, iend,
KOKKOS_LAMBDA (int k, int j, int i) {
real R=x1(i);
real R0=x1(ighost);
real Vk = 1.0/sqrt(R);
Vc(RHO,k,j,i) = Vc(RHO,k,j,ighost)*pow(R/R0,-sigmaSlope) ;
Vc(VX1,k,j,i) = - Vc(VX1,k,j,2*ighost-i+sign);
Vc(VX2,k,j,i) = Vc(VX2,k,j,ighost)*pow(R/R0,-0.5) - omega*R ;
// Vc(VX3,k,j,i) = Vc(VX3,k,j,2*ighost-i+sign);
});
}
}
void FargoVelocity(DataBlock &data, IdefixArray2D<real> &Vphi) {
IdefixArray1D<real> x1 = data.x[IDIR];
real omega = omegaGlob;
idefix_for("FargoVphi",0,data.np_tot[KDIR], 0, data.np_tot[IDIR],
KOKKOS_LAMBDA (int k, int i) {
real R = x1(i);
Vphi(k,i) = 1.0/sqrt(R) - omega*R;
});
}
// Analyse data to produce an ascii output
void Analysis(DataBlock & data) {
// Mirror data on Host
DataBlockHost d(data);
// Sync it
d.SyncFromDevice();
for(int ip=0; ip < data.planetarySystem->nbp ; ip++) {
// Get the orbital parameters
real timeStep = data.dt;
real xp = data.planetarySystem->planet[ip].getXp();
real yp = data.planetarySystem->planet[ip].getYp();
real zp = data.planetarySystem->planet[ip].getZp();
real vxp = data.planetarySystem->planet[ip].getVxp();
real vyp = data.planetarySystem->planet[ip].getVyp();
real vzp = data.planetarySystem->planet[ip].getVzp();
real qp = data.planetarySystem->planet[ip].getMp();
real time = data.t;
std::string planetName, tqwkName;
std::stringstream pName, tName;
pName << "planet" << ip << ".dat";
tName << "tqwk" << ip << ".dat";
planetName = pName.str();
tqwkName = tName.str();
// Write the data in ascii to our file
if(idfx::prank==0) {
std::ofstream f;
f.open(planetName,std::ios::app);
f.precision(10);
f << std::scientific << timeStep << " " << xp << " " << yp << " " << zp << " " << vxp << " " << vyp << " " << vzp << " " << qp << " " << time << std::endl;
f.close();
}
// Force force = {{0.0,0.0,0.0},{0.0,0.0,0.0},{0.0,0.0,0.0},{0.0,0.0,0.0}};
Force &force = data.planetarySystem->planet[ip].m_force;
bool isp = true;
data.planetarySystem->planet[ip].computeForce(data,isp);
// Get the torque and work
// real fxi = force.f_inner[0];
// real fyi = force.f_inner[1];
// real fzi = force.f_inner[2];
// real fxo = force.f_outer[0];
// real fyo = force.f_outer[1];
// real fzo = force.f_outer[2];
// real fxhi = force.f_ex_inner[0];
// real fyhi = force.f_ex_inner[1];
// real fzhi = force.f_ex_inner[2];
// real fxho = force.f_ex_outer[0];
// real fyho = force.f_ex_outer[1];
// real fzho = force.f_ex_outer[2];
real tq_inner = xp*force.f_inner[1]-yp*force.f_inner[0];
real tq_outer = xp*force.f_outer[1]-yp*force.f_outer[0];
real tq_ex_inner = xp*force.f_ex_inner[1]-yp*force.f_ex_inner[0];
real tq_ex_outer = xp*force.f_ex_outer[1]-yp*force.f_ex_outer[0];
real wk_inner = vxp*force.f_inner[0]+vyp*force.f_inner[1];
real wk_outer = vxp*force.f_outer[0]+vyp*force.f_outer[1];
real wk_ex_inner = vxp*force.f_ex_inner[0]+vyp*force.f_ex_inner[1];
real wk_ex_outer = vxp*force.f_ex_outer[0]+vyp*force.f_ex_outer[1];
// Write the data in ascii to our file
if(idfx::prank==0) {
std::ofstream ft;
ft.open(tqwkName,std::ios::app);
ft.precision(10);
// ft << std::scientific << timeStep << " " << fxi << " " << fyi << " " << fzi << " " << fxo << " " << fyo << " " << fzo << " " << fxhi << " " << fyhi << " " << fzhi << " " << fxho << " " << fyho << " " << fzho << " " << time << std::endl;
ft << std::scientific << timeStep << " " << tq_inner << " " << tq_outer << " " << tq_ex_inner << " " << tq_ex_outer << " " << wk_inner << " " << wk_outer << " " << wk_ex_inner << " " << wk_ex_outer << " " << time << std::endl;
ft.close();
}
}
}
// Compute user variables which will be written in vtk files
void ComputeUserVars(DataBlock & data, UserDefVariablesContainer &variables) {
// Mirror data on Host
real h0=h0Glob;
real flaringIndex=flaringIndexGlob;
DataBlockHost d(data);
// Sync it
d.SyncFromDevice();
// The model being isothermal, we define ourselves PRS
#undef PRS
// Make references to the user-defined arrays (variables is a container of IdefixHostArray3D)
// Note that the labels should match the variable names in the input file
IdefixHostArray3D<real> PRS = variables["PRS"];
for(int k = 0; k < d.np_tot[KDIR] ; k++) {
for(int j = 0; j < d.np_tot[JDIR] ; j++) {
for(int i = 0; i < d.np_tot[IDIR] ; i++) {
real R = d.x[IDIR](i);
real cs2 = h0*h0*pow(R,2*flaringIndex-1.);
PRS(k,j,i) = cs2*d.Vc(RHO,k,j,i);
}
}
}
}
// Default constructor
// Initialisation routine. Can be used to allocate
// Arrays or variables which are used later on
Setup::Setup(Input &input, Grid &grid, DataBlock &data, Output &output)// : m_planet(0)//, Planet &planet)
{
// Set the function for userdefboundary
data.hydro->EnrollUserDefBoundary(&UserdefBoundary);
data.hydro->EnrollUserSourceTerm(&Damping);
data.hydro->EnrollIsoSoundSpeed(&MySoundSpeed);
if(data.haveFargo)
data.fargo->EnrollVelocity(&FargoVelocity);
if(data.hydro->haveRotation) {
omegaGlob = data.hydro->OmegaZ;
} else {
omegaGlob = 0.0;
}
// Enroll the analysis function
output.EnrollAnalysis(&Analysis);
// Enroll our user-defined variables
output.EnrollUserDefVariables(&ComputeUserVars);
wkzMinGlob = input.Get<real>("Setup","wkzMin",0);
wkzMaxGlob = input.Get<real>("Setup","wkzMax",0);
wkzDampingGlob = input.Get<real>("Setup","wkzDamping",0);
sigma0Glob = input.Get<real>("Setup","sigma0",0);
sigmaSlopeGlob = input.Get<real>("Setup","sigmaSlope",0);
h0Glob = input.Get<real>("Setup","h0",0);
flaringIndexGlob = input.Get<real>("Setup","flaringIndex",0);
densityFloorGlob = input.Get<real>("Setup","densityFloor",0);
masstaperGlob = input.Get<real>("Planet","masstaper",0);
// delete file planet0.dat at initialization if we do not restart the simulation.
for(int ip=0; ip < data.planetarySystem->nbp ; ip++) {
std::string planetName, tqwkName;
std::stringstream pName, tName;
pName << "planet" << ip << ".dat";
tName << "tqwk" << ip << ".dat";
planetName = pName.str();
tqwkName = tName.str();
if (!(input.restartRequested)) {
if(idfx::prank==0) {
std::ofstream f;
f.open(planetName,std::ios::out);
f.close();
std::ofstream ft;
ft.open(tqwkName,std::ios::out);
ft.close();
}
}
/*
else {
if(idfx::prank==0) {
char ch;
int nline = input.restartFileNumber*input.Get<real>("Output","dmp",0)/input.Get<real>("Output","analysis",0);
int line = 1;
// planet0.dat
std::ifstream fin(planetName);
std::ofstream fout;
fout.open("temp.dat", std::ios::out);
while(fin.get(ch))
{
if(ch == '\n')
line++;
if(line <= nline) // content not to be deleted
fout<<ch;
}
fout << std::endl;
fout.close();
fin.close();
std::remove(planetName.c_str());
std::rename("temp.dat",planetName.c_str());
// tqwk0.dat
std::ifstream fint(tqwkName);
std::ofstream foutt;
foutt.open("tempt.dat", std::ios::out);
nline = input.restartFileNumber*input.Get<real>("Output","dmp",0)/input.Get<real>("Output","analysis",0);
line = 1;
while(fint.get(ch))
{
if(ch == '\n')
line++;
if(line <= nline) // content not to be deleted
foutt<<ch;
}
foutt << std::endl;
foutt.close();
fint.close();
std::remove(tqwkName.c_str());
std::rename("tempt.dat",tqwkName.c_str());
}
}
*/
}
}
// This routine initialize the flow
// Note that data is on the device.
// One can therefore define locally
// a datahost and sync it, if needed
void Setup::InitFlow(DataBlock &data) {
// Create a host copy
DataBlockHost d(data);
real h0=h0Glob;
real sigma0=sigma0Glob;
real sigmaSlope=sigmaSlopeGlob;
real flaringIndex=flaringIndexGlob;
real omega=omegaGlob;
for(int k = 0; k < d.np_tot[KDIR] ; k++) {
for(int j = 0; j < d.np_tot[JDIR] ; j++) {
for(int i = 0; i < d.np_tot[IDIR] ; i++) {
real R = d.x[IDIR](i);
real z = d.x[KDIR](k);
real Vk = 1.0/sqrt(R);
d.Vc(RHO,k,j,i) = sigma0*pow(R,-sigmaSlope) ;
d.Vc(VX1,k,j,i) = 0.0;
d.Vc(VX2,k,j,i) = Vk*sqrt(1.0-(1.0+sigmaSlope-2*flaringIndex)*h0*h0*pow(R,2*flaringIndex)) - omega * R;
// d.Vc(VX3,k,j,i) = 0.0;
if(d.Vc(RHO,k,j,i) < densityFloorGlob) {
d.Vc(RHO,k,j,i) = densityFloorGlob;
}
}
}
}
// Send it all, if needed
d.SyncToDevice();
}
// Analyse data to produce an output
void MakeAnalysis(DataBlock & data) {
}