forked from duffell/Disco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.c
601 lines (527 loc) · 17.4 KB
/
misc.c
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#include "paul.h"
#include <string.h>
double get_dA( double * , double * , int );
double get_dV( double * , double * );
double get_moment_arm( double * , double * );
void clean_pi( struct domain * theDomain ){
struct cell ** theCells = theDomain->theCells;
int Nr = theDomain->Nr;
int Nz = theDomain->Nz;
int * Np = theDomain->Np;
int Npl = theDomain->Npl;
double phi_max = theDomain->phi_max;
int i,jk;
for( jk=0 ; jk<Nr*Nz ; ++jk ){
for( i=0 ; i<Np[jk] ; ++i ){
struct cell * c = &(theCells[jk][i]);
double phi = c->piph;
while( phi > phi_max ){ phi -= phi_max; }
while( phi < 0.0 ){ phi += phi_max; }
c->piph = phi;
}
}
int p;
for( p=0 ; p<Npl ; ++p ){
struct planet * pl = theDomain->thePlanets + p;
double phi = pl->phi;
while( phi > phi_max ){ phi -= phi_max; pl->RK_phi -= phi_max; }
while( phi < 0.0 ){ phi += phi_max; pl->RK_phi += phi_max; }
pl->phi = phi;
}
int Ntr = theDomain->Ntr;
int n;
for( n=0; n<Ntr; ++n){
struct tracer *tr = theDomain->theTracers + n;
double phi = tr->Phi;
while( phi > phi_max ){ phi -= phi_max; tr->RK_phi -= phi_max; }
while( phi < 0.0 ){ phi += phi_max; tr->RK_phi += phi_max; }
tr->Phi = phi;
}
}
double mindt( double * , double , double * , double * );
double getmindt( struct domain * theDomain ){
struct cell ** theCells = theDomain->theCells;
int Nr = theDomain->Nr;
int Nz = theDomain->Nz;
int * Np = theDomain->Np;
double * r_jph = theDomain->r_jph;
double * z_kph = theDomain->z_kph;
double dt = 1e100;
int i,j,k;
for( j=1 ; j<Nr-1 ; ++j ){
for( k=0 ; k<Nz ; ++k ){
int jk = j+Nr*k;
for( i=0 ; i<Np[jk] ; ++i ){
struct cell * c = &(theCells[jk][i]);
double phip = c->piph;
double phim = phip - c->dphi;
double xp[3] = {r_jph[j ] , phip , z_kph[k ]};
double xm[3] = {r_jph[j-1] , phim , z_kph[k-1]};
int im = i-1;
if( i==0 ) im = Np[jk]-1;
double wm = theCells[jk][im].wiph;
double wp = c->wiph;
double w = .5*(wm+wp);
double dt_temp = mindt( c->prim , w , xp , xm );
if( dt > dt_temp ) dt = dt_temp;
}
}
}
dt *= theDomain->theParList.CFL;
MPI_Allreduce( MPI_IN_PLACE , &dt , 1 , MPI_DOUBLE , MPI_MIN , theDomain->theComm );
return( dt );
}
void initial( double * , double * );
void prim2cons( double * , double * , double * , double );
void cons2prim( double * , double * , double * , double );
void restart( struct domain * );
/*
void clear_w( struct domain * theDomain ){
int Nr = theDomain->Nr;
int Nz = theDomain->Nz;
int * Np = theDomain->Np;
int i,jk;
for( jk=0 ; jk<Nr*Nz ; ++jk ){
for( i=0 ; i<Np[jk] ; ++i ){
theDomain->theCells[jk][i].wiph = 0.0;
}
}
}*/
double get_omega( double * , double * );
double mesh_om( double );
void set_wcell( struct domain * theDomain ){
struct cell ** theCells = theDomain->theCells;
int mesh_motion = theDomain->theParList.Mesh_Motion;
int Nr = theDomain->Nr;
int Nz = theDomain->Nz;
int * Np = theDomain->Np;
double * r_jph = theDomain->r_jph;
double * z_kph = theDomain->z_kph;
int i,j,k;
for( j=0 ; j<Nr ; ++j ){
for( k=0 ; k<Nz ; ++k ){
int jk = j+Nr*k;
double rm = r_jph[j-1];
double rp = r_jph[j];
double zm = z_kph[k-1];
double zp = z_kph[k];
for( i=0 ; i<Np[jk] ; ++i ){
struct cell * cL = &(theCells[jk][i ]);
double w = 0.0;
if( mesh_motion ){
int ip = (i+1)%Np[jk];
double phip = cL->piph;
double phim = phip-cL->dphi;
double xp[3] = {rp,phip,zp};
double xm[3] = {rm,phim,zm};
double r = get_moment_arm( xp , xm );
double x[3] = {r, 0.5*(phim+phip), 0.5*(zm+zp)};
double wL = get_omega( cL->prim , x );
struct cell * cR = &(theCells[jk][ip]);
phip = cR->piph;
phim = phip-cR->dphi;
xp[1] = phip;
xm[1] = phim;
r = get_moment_arm( xp , xm );
x[0] = r;
x[1] = 0.5*(phim+phip);
double wR = get_omega( cR->prim , x );
w = .5*(wL + wR);
}
cL->wiph = w;
}
}
}
if( mesh_motion == 3 ){
for( j=0 ; j<Nr ; ++j ){
for( k=0 ; k<Nz ; ++k ){
int jk = j+Nr*k;
double w = 0.0;
for( i=0 ; i<Np[jk] ; ++i ){
w += theCells[jk][i].wiph;
}
w /= (double)Np[jk];
for( i=0 ; i<Np[jk] ; ++i ){
theCells[jk][i].wiph = w;
}
}
}
}
if( mesh_motion == 4 ){
for( j=0 ; j<Nr ; ++j ){
double r = .5*(r_jph[j]+r_jph[j-1]);
for( k=0 ; k<Nz ; ++k ){
int jk = j+Nr*k;
for( i=0 ; i<Np[jk] ; ++i ){
theCells[jk][i].wiph = r*mesh_om(r);
}
}
}
}
}
void initial( double * , double * );
void clear_cell( struct cell * );
void regrid( struct domain * theDomain ){
/*
struct cell ** theCells = theDomain->theCells;
int Nt = theDomain->Nt;
int Np = theDomain->Np;
int * Nr = theDomain->Nr;
double * t_jph = theDomain->t_jph;
double * p_kph = theDomain->p_kph;
double jthresh = 0.1;
int i,j,k;
for( j=0 ; j<Nt ; ++j ){
for( k=0 ; k<Np ; ++k ){
int jk = j+Nt*k;
for( i=2 ; i<Nr[jk]-1 ; ++i ){
double jump = log( theCells[jk][i-1].prim[RHO]/theCells[jk][i+1].prim[RHO] );
if( fabs(jump) > jthresh ){
struct cell * c = theCells[jk]+i;
double rp = c->riph;
double rm = (c-1)->riph;
int Nsplit = (int)(fabs(jump/jthresh));
if(Nsplit>5) Nsplit=5;
if(Nsplit>3) printf("r=%.2e jump=%.2e split = %d (No Cause For Alarm)\n",theCells[jk][i].riph,jump,Nsplit);
int blocksize = (Nr[jk]-1) - i;
Nr[jk] += Nsplit;
theCells[jk] = (struct cell *) realloc( theCells[jk] , Nr[jk]*sizeof(struct cell) );
memmove( theCells[jk]+i+1+Nsplit , theCells[jk]+i+1 , blocksize*sizeof(struct cell) );
int l;
for( l=0 ; l<Nsplit+1 ; ++l ){
int m = l+i;
struct cell * cnew = theCells[jk]+m;
clear_cell( cnew );
double rlp = rm + (rp-rm)*( (double)(l+1)/(double)(Nsplit+1) );
double rlm = rm + (rp-rm)*( (double)(l )/(double)(Nsplit+1) );
double xp[3] = {rlp,t_jph[j] ,p_kph[k] };
double xm[3] = {rlm,t_jph[j-1],p_kph[k-1]};
double x[3];
int d;
for( d=0 ; d<3 ; ++d ) x[d] = .5*(xp[d]+xm[d]);
initial( cnew->prim , x );
cnew->riph = rlp;
cnew->dr = rlp-rlm;
double dV = get_dV(xp,xm);
double rr = (2./3.)*(rlp*rlp*rlp-rlm*rlm*rlm)/(rlp*rlp-rlm*rlm);
prim2cons( cnew->prim , cnew->cons , rr , dV );
}
i += Nsplit;
}
}
}
}
*/
}
void adjust_RK_cons( struct domain * theDomain , double RK ){
struct cell ** theCells = theDomain->theCells;
int Nr = theDomain->Nr;
int Nz = theDomain->Nz;
int * Np = theDomain->Np;
int i,jk,q;
for( jk=0 ; jk<Nr*Nz ; ++jk ){
for( i=0 ; i<Np[jk] ; ++i ){
struct cell * c = &(theCells[jk][i]);
for( q=0 ; q<NUM_Q ; ++q ){
c->cons[q] = (1.-RK)*c->cons[q] + RK*c->RKcons[q];
}
for( q=0 ; q<NUM_FACES ; ++q ){
c->Phi[q] = (1.-RK)*c->Phi[q] + RK*c->RK_Phi[q];
}
}
}
}
void planet_RK_adjust( struct planet * , double );
void adjust_RK_planets( struct domain * theDomain , double RK ){
int Npl = theDomain->Npl;
int p;
for( p=0 ; p<Npl ; ++p ){
planet_RK_adjust( theDomain->thePlanets+p , RK );
}
}
void tracer_RK_adjust( struct tracer * , double);
void adjust_RK_tracers( struct domain *theDomain , double RK){
int Ntr = theDomain->Ntr;
int n;
for( n=0 ; n<Ntr; ++n ){
tracer_RK_adjust( theDomain->theTracers+n , RK );
}
}
void move_cells( struct domain * theDomain , double dt){
struct cell ** theCells = theDomain->theCells;
int Nr = theDomain->Nr;
int Nz = theDomain->Nz;
int * Np = theDomain->Np;
int i,jk;
for( jk=0 ; jk<Nr*Nz ; ++jk ){
for( i=0 ; i<Np[jk] ; ++i ){
struct cell * c = &(theCells[jk][i]);
c->piph += c->wiph*dt;
}
}
}
double get_dp( double , double );
void calc_dp( struct domain * theDomain ){
struct cell ** theCells = theDomain->theCells;
int Nr = theDomain->Nr;
int Nz = theDomain->Nz;
int * Np = theDomain->Np;
int i,jk;
for( jk=0 ; jk<Nr*Nz ; ++jk ){
for( i=0 ; i<Np[jk] ; ++i ){
int im = i-1;
if( i == 0 ) im = Np[jk]-1;
double phim = theCells[jk][im].piph;
double phip = theCells[jk][i ].piph;
double dphi = get_dp(phip,phim);
theCells[jk][i].dphi = dphi;
}
}
}
void calc_prim( struct domain * theDomain ){
struct cell ** theCells = theDomain->theCells;
int Nr = theDomain->Nr;
int Nz = theDomain->Nz;
int * Np = theDomain->Np;
double * r_jph = theDomain->r_jph;
double * z_kph = theDomain->z_kph;
int i,j,k;
for( j=0 ; j<Nr ; ++j ){
double rm = r_jph[j-1];
double rp = r_jph[j];
for( k=0 ; k<Nz ; ++k ){
int jk = j+Nr*k;
for( i=0 ; i<Np[jk] ; ++i ){
struct cell * c = &(theCells[jk][i]);
double phip = c->piph;
double phim = phip-c->dphi;
double xp[3] = {rp,phip,z_kph[k] };
double xm[3] = {rm,phim,z_kph[k-1]};
double r = get_moment_arm( xp , xm );
double dV = get_dV( xp , xm );
double x[3] = {r, 0.5*(phim+phip), 0.5*(z_kph[k]+z_kph[k-1])};
cons2prim( c->cons , c->prim , x , dV );
}
}
}
}
void plm_phi( struct domain * );
void riemann_phi( struct cell * , struct cell * , double * , double );
void phi_flux( struct domain * theDomain , double dt ){
struct cell ** theCells = theDomain->theCells;
int Nr = theDomain->Nr;
int Nz = theDomain->Nz;
int * Np = theDomain->Np;
double * r_jph = theDomain->r_jph;
double * z_kph = theDomain->z_kph;
int i,j,k;
plm_phi( theDomain );
for( j=0 ; j<Nr ; ++j ){
for( k=0 ; k<Nz ; ++k ){
int jk = j+Nr*k;
for( i=0 ; i<Np[jk] ; ++i ){
int ip = (i+1)%Np[jk];
struct cell * cp = theCells[jk];
double phi = cp[i].piph;
double xp[3] = {r_jph[j] ,phi,z_kph[k] };
double xm[3] = {r_jph[j-1],phi,z_kph[k-1]};
double r = get_moment_arm(xp,xm);
double dA = get_dA(xp,xm,0);
double x[3] = {r, phi, 0.5*(z_kph[k-1]+z_kph[k])};
riemann_phi( &(cp[i]) , &(cp[ip]) , x , dA*dt );
}
}
}
}
void buildfaces( struct domain * , int , int );
void plm_trans( struct domain * , struct face * , int , int );
void riemann_trans( struct face * , double , int );
void trans_flux( struct domain * theDomain , double dt , int dim ){
int Nf;
struct face * theFaces;
if( dim==1 ){
Nf = theDomain->fIndex_r[theDomain->N_ftracks_r];
theFaces = theDomain->theFaces_1;
}else{
Nf = theDomain->fIndex_z[theDomain->N_ftracks_z];
theFaces = theDomain->theFaces_2;
}
plm_trans( theDomain , theFaces , Nf , dim );
int f;
for( f=0 ; f<Nf ; ++f ){
riemann_trans( theFaces + f , dt , dim );
}
}
void setup_faces( struct domain * theDomain , int dim ){
struct face ** theFaces;
int NN;
int * nn;
if( dim==1 ){
theFaces = &(theDomain->theFaces_1);
nn = theDomain->fIndex_r;
NN = theDomain->N_ftracks_r;
}else{
theFaces = &(theDomain->theFaces_2);
nn = theDomain->fIndex_z;
NN = theDomain->N_ftracks_z;
}
buildfaces( theDomain , dim , 0 );
int Nf = nn[NN];
*theFaces = (struct face *) malloc( Nf*sizeof(struct face) );
buildfaces( theDomain , dim , 1 );
}
void source( double * , double * , double * , double * , double );
void planet_src( struct planet * , double * , double * , double * , double * , double );
void omega_src( double * , double * , double * , double * , double );
void add_source( struct domain * theDomain , double dt ){
struct cell ** theCells = theDomain->theCells;
struct planet * thePlanets = theDomain->thePlanets;
int Nr = theDomain->Nr;
int Nz = theDomain->Nz;
int * Np = theDomain->Np;
int Npl = theDomain->Npl;
double * r_jph = theDomain->r_jph;
double * z_kph = theDomain->z_kph;
int i,j,k,p;
for( j=0 ; j<Nr ; ++j ){
for( k=0 ; k<Nz ; ++k ){
int jk = j+Nr*k;
for( i=0 ; i<Np[jk] ; ++i ){
struct cell * c = &(theCells[jk][i]);
double phip = c->piph;
double phim = c->piph - c->dphi;
double xp[3] = {r_jph[j] ,phip,z_kph[k] };
double xm[3] = {r_jph[j-1],phim,z_kph[k-1]};
double dV = get_dV(xp,xm);
source( c->prim , c->cons , xp , xm , dV*dt );
for( p=0 ; p<Npl ; ++p ){
planet_src( thePlanets+p , c->prim , c->cons , xp , xm , dV*dt );
}
omega_src( c->prim , c->cons , xp , xm , dV*dt );
}
}
}
}
void longandshort( struct domain * theDomain , double * L , double * S , int * iL , int * iS , struct cell * sweep , int j , int k ){
int Nr = theDomain->Nr;
int jk = j + Nr*k;
int Np = theDomain->Np[jk];
double * r_jph = theDomain->r_jph;
double dr = r_jph[j]-r_jph[j-1];
double r = r_jph[j];
double Long = 0.0;
double Short = 0.0;
int iLong = 0;
int iShort = 0;
int i;
for( i=0 ; i<Np ; ++i ){
double dx = dr;
double dy = r*sweep[i].dphi;
double l = dy/dx;
double s = dx/dy;
if( Long < l ){ Long = l; iLong = i; }
if( Short < s ){ Short = s; iShort = i; }
}
*L = Long;
*iL = iLong;
*S = Short;
*iS = iShort;
}
void AMRsweep( struct domain * theDomain , struct cell ** swptr , int jk ){
double * r_jph = theDomain->r_jph;
double * z_kph = theDomain->z_kph;
int Nr = theDomain->Nr;
int * Np = theDomain->Np;
int j = jk%Nr;
int k = jk/Nr;
double MaxShort = theDomain->theParList.MaxShort;
double MaxLong = theDomain->theParList.MaxLong;
struct cell * sweep = *swptr;
double L,S;
int iL=0;
int iS=0;
longandshort( theDomain , &L , &S , &iL , &iS , sweep , j , k );
//printf("Long = %e, Short = %e\n",L,S);
if( S>MaxShort ){
int iSp = (iS+1)%Np[jk];
//Possibly shift iS backwards by 1
int iSm = iS-1;
if( iSm == -1 ) iSm = Np[jk]-1;
double dpL = sweep[iSm].dphi;
double dpR = sweep[iSp].dphi;
if( dpL < dpR ){
--iS;
--iSm;
--iSp;
if( iS == -1 ) iS = Np[jk]-1;
if( iSm == -1 ) iSm = Np[jk]-1;
if( iSp == -1 ) iSp = Np[jk]-1;
}
//Remove Zone at iS+1
sweep[iS].dphi += sweep[iSp].dphi;
sweep[iS].piph = sweep[iSp].piph;
int q;
for( q=0 ; q<NUM_Q ; ++q ){
sweep[iS].cons[q] += sweep[iSp].cons[q];
sweep[iS].RKcons[q] += sweep[iSp].RKcons[q];
}
double phip = sweep[iS].piph;
double phim = phip - sweep[iS].dphi;
double xp[3] = {r_jph[j] ,phip,z_kph[k] };
double xm[3] = {r_jph[j-1],phim,z_kph[k-1]};
double r = get_moment_arm( xp , xm );
double dV = get_dV( xp , xm );
double x[3] = {r, 0.5*(phim+phip), 0.5*(z_kph[k-1]+z_kph[k])};
cons2prim( sweep[iS].cons , sweep[iS].prim , x , dV );
//Shift Memory
int blocksize = Np[jk]-iSp-1;
if( iSp != Np[jk]-1 ) memmove( sweep+iSp , sweep+iSp+1 , blocksize*sizeof(struct cell) );
Np[jk] -= 1;
*swptr = (struct cell *) realloc( sweep , Np[jk]*sizeof(struct cell) );
sweep = *swptr;
if( iS < iL ) iL--;
}
if( L>MaxLong ){
Np[jk] += 1;
*swptr = (struct cell *) realloc( sweep , Np[jk]*sizeof(struct cell) );
sweep = *swptr;
int blocksize = Np[jk]-iL-1;
memmove( sweep+iL+1 , sweep+iL , blocksize*sizeof(struct cell) );
double dphi = sweep[iL].dphi;
double phip = sweep[iL].piph;
double phim = phip - dphi;
double phi0 = .5*(phip+phim);
sweep[iL].piph = phi0;
sweep[iL].dphi = .5*dphi;
sweep[iL+1].dphi = .5*dphi;
int q;
for( q=0 ; q<NUM_Q ; ++q ){
sweep[iL].cons[q] *= .5;
sweep[iL].RKcons[q] *= .5;
sweep[iL+1].cons[q] *= .5;
sweep[iL+1].RKcons[q] *= .5;
}
double xp[3] = {r_jph[j] ,phi0,z_kph[k] };
double xm[3] = {r_jph[j-1],phim,z_kph[k-1]};
double dV = get_dV( xp , xm );
double r = get_moment_arm( xp , xm );
double x[3] = {r, 0.5*(xp[1]+xm[1]), 0.5*(xp[2]+xm[2])};
cons2prim( sweep[iL].cons , sweep[iL].prim , x , dV );
xp[1] = phip;
xm[1] = phi0;
dV = get_dV( xp , xm );
r = get_moment_arm( xp , xm );
x[0] = r;
x[1] = 0.5*(xp[1]+xm[1]);
cons2prim( sweep[iL+1].cons , sweep[iL+1].prim , x , dV );
}
}
void AMR( struct domain * theDomain ){
struct cell ** theCells = theDomain->theCells;
int Nr = theDomain->Nr;
int Nz = theDomain->Nz;
int jk;
for( jk=0 ; jk<Nr*Nz ; ++jk ){
AMRsweep( theDomain , theCells+jk , jk );
}
}