-
Notifications
You must be signed in to change notification settings - Fork 0
/
evolve.c
349 lines (283 loc) · 8.4 KB
/
evolve.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
/*
noisy:
Generate a gaussian random field in 2D with
locally anisotropic correlation function,
locally varying correlation time.
Follows the technique of
Lindgren, Rue, and Lindstr\:om 2011, J.R. Statist. Soc. B 73, pp 423-498.
https://rss.onlinelibrary.wiley.com/doi/10.1111/j.1467-9868.2011.00777.x
in particular, implements eq. 17, which has power spectrum given by eq. 18.
Based on work by
David Daeyoung Lee
Charles Gammie
on applications in disk turbulence.
CFG 22 Dec 2019
*/
#include "noisy.h"
/* grid functions necessary for diffusive and advective evolution */
void grid_function_calc(
double F_coeff_gradx[][N][4],
double F_coeff_grady[][N][4],
double v[][N][4][2],
double T[][N],
double *Kmax,
double *Vmax
) {
void principal_axis_func(double x, double y, double *e1x, double *e1y, double *e2x, double *e2y);
void advection_velocity(double x, double y, double va[2]);
void ij_to_xy(int i,int j,double *x,double *y);
/* preparatory work: calculate some grid functions */
int i,j ;
double x,y,dx,dy;
double e1x,e1y,e2x,e2y;
double K1,K2;
double diffusion_coefficient(double x, double y);
double correlation_time(double x, double y);
dx = PARAM_FOV/N;
dy = PARAM_FOV/N;
*Kmax = 0.;
*Vmax = 0.;
for(i=0;i<N;i++)
for(j=0;j<N;j++) {
ij_to_xy(i,j,&x,&y);
principal_axis_func(x+0.5*dx, y, &e1x,&e1y,&e2x,&e2y);
K1 = diffusion_coefficient(x+0.5*dx, y);
K2 = PARAM_RAT*K1;
F_coeff_gradx[i][j][0] = K1*e1x*e1x + K2*e2x*e2x ;
F_coeff_grady[i][j][0] = K1*e1x*e1y + K2*e2x*e2y ;
advection_velocity(x+0.5*dx, y, v[i][j][0]);
principal_axis_func(x, y+0.5*dy, &e1x,&e1y,&e2x,&e2y);
K1 = diffusion_coefficient(x, y+0.5*dy);
K2 = PARAM_RAT*K1;
F_coeff_gradx[i][j][1] = K1*e1y*e1x + K2*e2y*e2x ;
F_coeff_grady[i][j][1] = K1*e1y*e1y + K2*e2y*e2y ;
advection_velocity(x, y+0.5*dy, v[i][j][1]);
principal_axis_func(x-0.5*dx, y, &e1x,&e1y,&e2x,&e2y);
K1 = diffusion_coefficient(x-0.5*dx, y);
K2 = PARAM_RAT*K1;
F_coeff_gradx[i][j][2] = K1*e1x*e1x + K2*e2x*e2x ;
F_coeff_grady[i][j][2] = K1*e1x*e1y + K2*e2x*e2y ;
advection_velocity(x-0.5*dx, y, v[i][j][2]);
principal_axis_func(x, y-0.5*dy, &e1x,&e1y,&e2x,&e2y);
K1 = diffusion_coefficient(x, y-0.5*dy);
K2 = PARAM_RAT*K1;
F_coeff_gradx[i][j][3] = K1*e1y*e1x + K2*e2y*e2x ;
F_coeff_grady[i][j][3] = K1*e1y*e1y + K2*e2y*e2y ;
advection_velocity(x, y-0.5*dy, v[i][j][3]);
T[i][j] = correlation_time(x, y) + SMALL;
/* for timestep */
double Ktot = K1+K2;
if(Ktot > *Kmax) *Kmax = Ktot;
double Vtot = fabs( v[i][j][0][0] ) + fabs( v[i][j][0][1] ) ;
if(Vtot > *Vmax) *Vmax = Vtot;
//fprintf(stderr,"%d %d %g %g %g\n",i,j,*Kmax,K1,K2);
/*
fprintf(stderr,"%d %d %g %g %g %g\n",i,j,
F_coeff_gradx[i][j][0],
F_coeff_grady[i][j][0],
F_coeff_gradx[i][j][1],
F_coeff_grady[i][j][1]);
*/
}
}
void evolve_diffusion(double del[][N], double F_coeff_gradx[][N][4], double F_coeff_grady[][N][4],
double dt)
{
double ddel[N][N];
double gradx,grady,Fxp,Fxm,Fyp,Fym,deldiff;
double dx=PARAM_FOV/N;
double dy=PARAM_FOV/N;
int i,j,ip,jp,im,jm;
#pragma omp parallel \
shared ( del, ddel, dx, dy, F_coeff_gradx, F_coeff_grady ) \
private (i, j, ip, im, jp, jm, gradx, grady, \
Fxp, Fxm, Fyp, Fym, deldiff)
{
#pragma omp for
for(i=0;i<N;i++)
for(j=0;j<N;j++) {
ip = (i+N+1)%N ;
im = (i+N-1)%N ;
jp = (j+N+1)%N ;
jm = (j+N-1)%N ;
/* F = -K1 e1 (e1 . grad) - K2 e2 (e2 . grad) */
/* gradient, centered at ... */
/* upper x face */
gradx = (del[ip][j] - del[i][j])/dx;
grady = 0.5*(
(del[i][jp] - del[i][jm])/(2.*dy) +
(del[ip][jp] - del[ip][jm])/(2.*dy)
);
Fxp = -(
F_coeff_gradx[i][j][0]*gradx +
F_coeff_grady[i][j][0]*grady
);
/* upper y face */
gradx = 0.5*(
(del[ip][j] - del[im][j])/(2.*dx) +
(del[ip][jp] - del[im][jp])/(2.*dx)
);
grady = (del[i][jp] - del[i][j])/dy;
Fyp = -(
F_coeff_gradx[i][j][1]*gradx +
F_coeff_grady[i][j][1]*grady
);
/* lower x face */
gradx = (del[i][j] - del[im][j])/dx;
grady = 0.5*(
(del[i][jp] - del[i][jm])/(2.*dy) +
(del[im][jp] - del[im][jm])/(2.*dy)
);
Fxm = -(
F_coeff_gradx[i][j][2]*gradx +
F_coeff_grady[i][j][2]*grady
);
/* lower y face */
gradx = 0.5*(
(del[ip][j] - del[im][j])/(2.*dx) +
(del[ip][jm] - del[im][jm])/(2.*dx)
);
grady = (del[i][j] - del[i][jm])/dy;
Fym = -(
F_coeff_gradx[i][j][3]*gradx +
F_coeff_grady[i][j][3]*grady
);
deldiff = -(Fxp - Fxm)/dx - (Fyp - Fym)/dy;
ddel[i][j] = deldiff ;
}
}
#pragma omp parallel \
shared ( del, ddel, dt ) \
private (i, j )
{
#pragma omp for
/* update del */
for(i=0;i<N;i++)
for(j=0;j<N;j++) {
del[i][j] += dt*ddel[i][j] ;
}
}
}
void linear_mc(double x1, double x2, double x3, double *lout, double *rout)
{
double Dqm,Dqp,Dqc,s;
Dqm = 2. * (x2 - x1);
Dqp = 2. * (x3 - x2);
Dqc = 0.5 * (x3 - x1);
s = Dqm * Dqp;
if (s <= 0.)
s = 0.;
else {
if (fabs(Dqm) < fabs(Dqp) && fabs(Dqm) < fabs(Dqc))
s = Dqm;
else if (fabs(Dqp) < fabs(Dqc))
s = Dqp;
else
s = Dqc;
}
/* reconstruct left, right */
*lout = x2 - 0.5*s;
*rout = x2 + 0.5*s;
}
void reconstruct_lr(double d0, double d1, double d2, double d3, double *d_left, double *d_right)
{
void linear_mc(double x1, double x2, double x3, double *lout, double *rout);
double lout,rout;
linear_mc(d0,d1,d2,&lout,&rout);
*d_left = rout ;
linear_mc(d1,d2,d3,&lout,&rout);
*d_right = lout ;
}
double lr_to_flux(double d_left, double d_right, double v)
{
double F;
F = 0.5*(d_left*v + d_right*v) - fabs(v)*(d_right - d_left) ;
return(F);
}
void evolve_advection(double del[][N], double v[][N][4][2], double dt)
{
double ddel[N][N],Fxp,Fyp,Fxm,Fym,deladv;
int i,j,im,jm,ip,jp,imm,jmm;
double Fx[N][N];
double Fy[N][N];
double delr, dell;
double dx = PARAM_FOV/N;
double dy = PARAM_FOV/N;
#pragma omp parallel \
shared ( del, v, Fx, Fy ) \
private (i, j, imm, jmm, im, jm, ip, jp, dell, delr )
{
#pragma omp for
for(i=0;i<N;i++)
for(j=0;j<N;j++) {
ip = (i+N+1)%N ;
im = (i+N-1)%N ;
imm = (i+N-2)%N ;
jp = (j+N+1)%N ;
jm = (j+N-1)%N ;
jmm = (j+N-2)%N ;
reconstruct_lr(del[imm][j],del[im][j],del[i][j],del[ip][j], &dell, &delr);
Fx[i][j] = lr_to_flux(dell, delr, v[i][j][2][0]);
reconstruct_lr(del[i][jmm],del[i][jm],del[i][j],del[i][jp], &dell, &delr);
Fy[i][j] = lr_to_flux(dell, delr, v[i][j][3][1]);
}
}
#pragma omp parallel \
shared ( ddel, Fx, Fy ) \
private (i, j, ip, jp, Fxp, Fyp, Fxm, Fym, deladv )
{
#pragma omp for
for(i=0;i<N;i++)
for(j=0;j<N;j++) {
ip = (i+N+1)%N ;
jp = (j+N+1)%N ;
Fxp = Fx[ip][j];
Fyp = Fy[i][jp];
Fxm = Fx[i][j];
Fym = Fy[i][j];
deladv = -(Fxp - Fxm)/dx - (Fyp - Fym)/dy;
ddel[i][j] = deladv ;
}
}
#pragma omp parallel \
shared ( del, ddel, dt ) \
private (i, j )
{
#pragma omp for
/* update del */
for(i=0;i<N;i++)
for(j=0;j<N;j++) {
del[i][j] += dt*ddel[i][j] ;
}
}
}
void evolve_noise(double del[][N], double dt)
{
int i,j;
double del_noise[N][N];
void noise_model(double del_noise[][N], double dt);
noise_model(del_noise, dt);
/* update del */
for(i=0;i<N;i++)
for(j=0;j<N;j++) {
del[i][j] += del_noise[i][j];
}
}
void evolve_decay(double del[][N], double T[][N], double dt)
{
int i,j;
double Tdec;
#pragma omp parallel \
shared ( del, dt ) \
private (i, j, Tdec )
{
#pragma omp for
/* update del */
for(i=0;i<N;i++)
for(j=0;j<N;j++) {
Tdec = T[i][j] + 2.*dt ;
//del[i][j] *= (1. - 0.5*dt/Tdec)/(1. + 0.5*dt/Tdec) ;
del[i][j] += -dt*del[i][j]/Tdec;
}
}
}