-
Notifications
You must be signed in to change notification settings - Fork 0
/
2D_hydro.c
372 lines (332 loc) · 13 KB
/
2D_hydro.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
double gam = 5./3.;//adiabatic index
double thet = 1.5;//slope limiter
typedef struct Vars{//conserved variables
double mass;
double xvelocity;
double yvelocity;
double energy;
double press;
} Vars;
typedef struct Flux{
double rhov;
double momx;
double momy;
double energy;
} Flux;
void Print(int Nx, int Ny, Vars * U, double dx, double dy);
double Et(double rho, double xvel, double yvel, double P){//calculates the energy from pressure and other conserved variables
return(.5*rho*(pow(xvel/rho,2)+pow(yvel/rho,2)) + P/(gam-1.));
}
double sgn(double x){//returns sign of the number
if(x < 0) return(-1.);
else if(x >0) return(1.);
else return(0.);
}
double min(double x, double y, double z){//returns minium of set
double num = x;
if(num > y) num = y;
if(num > z) num = z;
return(num);
}
double minmod(double x, double y, double z){//slope limiter
double minm = .25*fabs(sgn(x) + sgn(y))*(sgn(x) + sgn(z))*min(fabs(x), fabs(y), fabs(z));
return(minm);
}
Vars U_L(Vars Ui, Vars Uimo, Vars Uipo){//interpolates the conserved variables at interface for the left state
Vars UL;
UL.mass = Ui.mass + 0.5*minmod(thet*(Ui.mass - Uimo.mass), 0.5*(Uipo.mass - Uimo.mass),thet*(Uipo.mass-Ui.mass));
UL.xvelocity = Ui.xvelocity + 0.5*minmod(thet*(Ui.xvelocity - Uimo.xvelocity), 0.5*(Uipo.xvelocity - Uimo.xvelocity),thet*(Uipo.xvelocity-Ui.xvelocity));
UL.yvelocity = Ui.yvelocity + 0.5*minmod(thet*(Ui.yvelocity - Uimo.yvelocity), 0.5*(Uipo.yvelocity - Uimo.yvelocity),thet*(Uipo.yvelocity-Ui.yvelocity));
UL.press = Ui.press + 0.5*minmod(thet*(Ui.press - Uimo.press), 0.5*(Uipo.press - Uimo.press),thet*(Uipo.press-Ui.press));
UL.energy = Et(UL.mass, UL.xvelocity, UL.yvelocity, UL.press);
return(UL);
}
Vars U_R(Vars Ui, Vars Uipo, Vars Uipt){//does U_L for the right state
Vars UR;
UR.mass = Uipo.mass - 0.5*minmod(thet*(Uipo.mass-Ui.mass),0.5*(Uipt.mass-Ui.mass),thet*(Uipt.mass-Uipo.mass));
UR.xvelocity = Uipo.xvelocity - 0.5*minmod(thet*(Uipo.xvelocity-Ui.xvelocity),.5*(Uipt.xvelocity-Ui.xvelocity),thet*(Uipt.xvelocity-Uipo.xvelocity));
UR.yvelocity = Uipo.yvelocity - 0.5*minmod(thet*(Uipo.yvelocity-Ui.yvelocity),.5*(Uipt.yvelocity-Ui.yvelocity),thet*(Uipt.yvelocity-Uipo.yvelocity));
UR.press = Uipo.press - 0.5*minmod(thet*(Uipo.press-Ui.press), 0.5*(Uipt.press-Ui.press), thet*(Uipt.press-Uipo.press));
UR.energy = Et(UR.mass, UR.xvelocity, UR.yvelocity, UR.press);
return(UR);
}
void init_sys(int Nx, int Ny, Vars * U, double dx, double dy){//various initial conditions
int i, j;
for(i=0;i<Nx;++i){
for(j=0;j<Ny;++j){
if(fabs(-.5+j*dy) < .2){
U[i*Ny+j].mass = 2.;
U[i*Ny+j].xvelocity = 0.5*U[i*Ny+j].mass;
U[i*Ny+j].yvelocity = 0.;
U[i*Ny+j].press = 2.5;
}
else{
U[i*Ny+j].mass = 1.;
U[i*Ny+j].xvelocity = -0.5*U[i*Ny+j].mass;
U[i*Ny+j].yvelocity = 0.;
U[i*Ny+j].press = 2.5;
}
if(fabs(-.5+dy*j) > .195 && fabs(-.5+dy*j) < .20) U[i*Ny+j].yvelocity = .001*sin(i*dx*8);
//if(i*dx == .5) printf("here %f\n", U[i*Ny+j].mass);
}
//if(i*dx == .5) Print(Nx, Ny, U, dx, dy);
}
for(i=0;i<Nx;++i){
for(j=0;j<Ny;++j){
U[i*Ny+j].energy = Et(U[i*Ny+j].mass, U[i*Ny+j].xvelocity, U[i*Ny+j].yvelocity, U[i*Ny+j].press);
//if(i*dx == .5) printf("%f\n", U[i*Ny+j].mass);
}
}
//Print(Nx, Ny, U, dx, dy);
}
double lambda(double vel, Vars U, double pm){
double c = sqrt(gam*U.press/U.mass);
return(vel + pm*c);
}
double MAX(double a, double b, double c){
double max = a;
if(max < b) max = b;
if(max < c) max = c;
return(max);
}
double alpha(Vars UL, Vars UR, double pm, int xy){
double alph;
if(xy == 1){// x integration
alph = MAX(0., pm*lambda(UR.xvelocity, UR, pm), pm*lambda(UL.xvelocity, UL,pm));
}
else if(xy == -1){// y integration
alph = MAX(0., pm*lambda(UR.yvelocity, UR, pm), pm*lambda(UL.yvelocity, UL,pm));
}
return(alph);
}
Flux get_F(Vars U){//get the flux from the conserved variables
Flux F;
F.rhov = U.xvelocity;
F.momx = pow(U.xvelocity,2)/U.mass + U.press;
F.momy = U.xvelocity*U.yvelocity/U.mass;
F.energy = (U.energy + U.press)*U.xvelocity/U.mass;
return(F);
}
Flux get_G(Vars U){
Flux G;
G.rhov = U.yvelocity;
G.momx = U.yvelocity*U.xvelocity/U.mass;
G.momy = pow(U.yvelocity,2)/U.mass + U.press;
G.energy = (U.energy + U.press)*U.yvelocity/U.mass;
return(G);
}
double Maxalpha(Vars Uimo, Vars Ui, Vars Uipo, Vars Uipt, int type){//calculates maxalpha for stability condition
double maxalph = 0.;
Vars UL = U_L(Ui, Uimo, Uipo); Vars UR = U_R(Ui, Uipo, Uipt); //Calculates the conserved variables at the interfaces
double alphap = alpha(UL, UR, 1., type);
double alpham = alpha(UL, UR, -1., type);
maxalph = MAX(maxalph, alphap, alpham);
return(maxalph);
}
Flux Fhll( Vars Uimo, Vars Ui, Vars Uipo, Vars Uipt){//calculates the HLL flux for a given interface in x
Flux F_HLL;
Vars UL = U_L(Ui, Uimo, Uipo); Vars UR = U_R(Ui, Uipo, Uipt); //Calculates the conserved variables at the interfaces
double alphap = alpha(UL, UR, 1., 1);
double alpham = alpha(UL, UR, -1., 1);
Flux FL = get_F(UL); Flux FR = get_F(UR); //Calculates the Fluxes from the left and right at the interface
F_HLL.rhov = (alphap*FL.rhov + alpham*FR.rhov - alphap*alpham*(UR.mass - UL.mass))/(alphap + alpham);
F_HLL.momx = (alphap*FL.momx + alpham*FR.momx - alphap*alpham*(UR.xvelocity - UL.xvelocity))/(alphap + alpham);
F_HLL.momy = (alphap*FL.momy + alpham*FR.momy - alphap*alpham*(UR.yvelocity - UL.yvelocity))/(alphap + alpham);
F_HLL.energy = (alphap*FL.energy + alpham*FR.energy - alphap*alpham*(UR.energy - UL.energy))/(alphap + alpham);
return(F_HLL);
}
Flux Ghll( Vars Uimo, Vars Ui, Vars Uipo, Vars Uipt){//calculates the HLL flux for a given interface in y
Flux G_HLL;
Vars UL = U_L(Ui, Uimo, Uipo); Vars UR = U_R(Ui, Uipo, Uipt); //Calculates the conserved variables at the interfaces
double alphap = alpha(UL, UR, 1., -1);
double alpham = alpha(UL, UR, -1., -1);
Flux FL = get_G(UL); Flux FR = get_G(UR); //Calculates the Fluxes from the left and right at the interface
G_HLL.rhov = (alphap*FL.rhov + alpham*FR.rhov - alphap*alpham*(UR.mass - UL.mass))/(alphap + alpham);
G_HLL.momx = (alphap*FL.momx + alpham*FR.momx - alphap*alpham*(UR.xvelocity - UL.xvelocity))/(alphap + alpham);
G_HLL.momy = (alphap*FL.momy + alpham*FR.momy - alphap*alpham*(UR.yvelocity - UL.yvelocity))/(alphap + alpham);
G_HLL.energy = (alphap*FL.energy + alpham*FR.energy - alphap*alpham*(UR.energy - UL.energy))/(alphap + alpham);
return(G_HLL);
}
Flux L(Flux FL, Flux FR, Flux GL, Flux GR, double dy, double dx){
Flux LU;
LU.rhov = -1.*(FR.rhov - FL.rhov)/dx - 1.*(GR.rhov - GL.rhov)/dy;
LU.momx = -1.*(FR.momx - FL.momx)/dx - 1.*(GR.momx - GL.momx)/dy;
LU.momy = -1.*(FR.momy - FL.momy)/dx - 1.*(GR.momy - GL.momy)/dy;
LU.energy = -1.*(FR.energy - FL.energy)/dx - 1.*(GR.energy - GL.energy)/dy;
return(LU);
}
Vars make_U1(double dt, Vars U, Flux LU){
Vars U1;
U1.mass = U.mass + dt*LU.rhov;
U1.xvelocity = U.xvelocity + dt*LU.momx;
U1.yvelocity = U.yvelocity + dt*LU.momy;
U1.energy = U.energy + dt*LU.energy;
U1.press = (gam-1.)*(U1.energy - .5*(pow(U1.yvelocity,2) + pow(U1.xvelocity,2))/U1.mass);
return(U1);
}
Vars make_U2(double dt, Vars U, Vars U1, Flux LU1){
Vars U2;
U2.mass = 0.75*U.mass + 0.25*U1.mass + 0.25*dt*LU1.rhov;
U2.xvelocity = 0.75*U.xvelocity + 0.25*U1.xvelocity + 0.25*dt*LU1.momx;
U2.yvelocity = 0.75*U.yvelocity + 0.25*U1.yvelocity + 0.25*dt*LU1.momy;
U2.energy = 0.75*U.energy + 0.25*U1.energy + 0.25*dt*LU1.energy;
U2.press = (gam-1.)*(U2.energy - .5*(pow(U2.yvelocity,2) + pow(U2.xvelocity,2))/U2.mass);
return(U2);
}
Vars make_UN(double dt, Vars U, Vars U2, Flux LU2){
Vars UN;
UN.mass = U.mass/3. + 2.*U2.mass/3. + 2.*dt*LU2.rhov/3.;
UN.xvelocity = U.xvelocity/3. + 2.*U2.xvelocity/3. + 2.*dt*LU2.momx/3.;
UN.yvelocity = U.yvelocity/3. + 2.*U2.yvelocity/3. + 2.*dt*LU2.momy/3.;
UN.energy = U.energy/3. + 2.*U2.energy/3. + 2.*dt*LU2.energy/3.;
UN.press = (gam-1.)*(UN.energy - .5*(pow(UN.yvelocity,2) + pow(UN.xvelocity,2))/UN.mass);
return(UN);
}
void Set_B2A(int Nx, int Ny, Vars * A, Vars * B){
int i, j;
for(i=0;i<Nx;++i){
for(j=0;j<Ny;++j){
B[i*Ny+j] = A[i*Ny+j];
}
}
}
void set_boundary(Vars * U, int Nx, int Ny, int type){
int i,j;
if(type == 0){
for(j=0;j<Ny;++j){//periodic boundary conditions for symmetry about x=1/2
U[1*Ny+j] = U[0*Ny+j];
U[0*Ny+j] = U[(Nx-1)*Ny+j];
U[(Nx-1)*Ny+j] = U[(Nx-2)*Ny+j];
U[(Nx-2)*Ny+j] = U[(Nx-3)*Ny+j];
}
for(i=0;i<Nx;++i){
U[i*Ny+1] = U[i*Ny+0];
U[i*Ny+Nx-1] = U[i*Ny+Nx-2];
U[i*Ny+0] = U[i*Ny+Nx-1];
U[i*Ny+Nx-2] = U[i*Ny+Nx-3];
/*U[i*Ny+1].yvelocity = 0.;
U[i*Ny+Nx-1].yvelocity = 0;
U[i*Ny+0].yvelocity = 0.;
U[i*Ny+Nx-2].yvelocity = 0.;*/
}
}
else if(type == 1){
for(i=0;i<Nx;++i){//periodic boundary conditions for symmetry about y=1/2
U[i*Ny+1] = U[i*Ny+0];
U[i*Ny+0] = U[i*Ny+Nx-1];
U[i*Ny+Nx-1] = U[i*Ny+Nx-2];
U[i*Ny+Nx-2] = U[i*Ny+Nx-3];
}
}
}
double advance_system(int Nx, int Ny, Vars * U, double dx, double dy, double fac){
//double maxalpha = Maxalpha(Nx, Ny, U);//calculate the maxalpha
double dt = 0.0001;//0.5*dx/maxalpha;//calculate the time step
Vars * U_n, * Un; int i, j;
U_n = malloc(Nx*Ny*sizeof(Vars)); Un = malloc(Nx*Ny*sizeof(Vars));
double maxalphax, maxalphay, maxalpha;
maxalpha = 0.;
for(i=2;i<Nx-1;++i){
for(j=2;j<Ny-1;++j){
maxalphax = Maxalpha(U[(i-2)*Ny+j],U[(i-1)*Ny+j],U[i*Ny+j],U[(i+1)*Ny+j], 1);
maxalphay = Maxalpha(U[(i-2)*Ny+j],U[(i-1)*Ny+j],U[i*Ny+j],U[(i+1)*Ny+j], -1);
maxalpha = MAX(maxalpha, maxalphax, maxalphay);
}
}
dt = fac*dx/maxalpha;
Flux FL, FR, GL, GR;
Set_B2A(Nx, Ny, U, U_n);
for(i=2;i<Nx-2;++i){
for(j=2;j<Ny-2;++j){
FL = Fhll(U[(i-2)*Ny+j],U[(i-1)*Ny+j],U[i*Ny+j],U[(i+1)*Ny+j]);
FR = Fhll(U[(i-1)*Ny+j],U[i*Ny+j],U[(i+1)*Ny+j],U[(i+2)*Ny+j]);
GL = Ghll(U[i*Ny+j-2],U[i*Ny+j-1],U[i*Ny+j],U[i*Ny+j+1]);
GR = Ghll(U[i*Ny+j-1],U[i*Ny+j],U[i*Ny+j+1],U[i*Ny+j+2]);
U_n[i*Ny+j] = make_U1(dt, U[i*Ny+j], L(FL, FR, GL, GR, dy, dx));
}
}
set_boundary(U_n, Nx, Ny, 0);
Set_B2A(Nx, Ny, U_n, Un);
for(i=2;i<Nx-2;++i){
for(j=2;j<Ny-2;++j){
FL = Fhll(U_n[(i-2)*Ny+j],U_n[(i-1)*Ny+j],U_n[i*Ny+j],U_n[(i+1)*Ny+j]);
FR = Fhll(U_n[(i-1)*Ny+j],U_n[i*Ny+j],U_n[(i+1)*Ny+j],U_n[(i+2)*Ny+j]);
GL = Ghll(U_n[i*Ny+j-2],U_n[i*Ny+j-1],U_n[i*Ny+j],U_n[i*Ny+j+1]);
GR = Ghll(U_n[i*Ny+j-1],U_n[i*Ny+j],U_n[i*Ny+j+1],U_n[i*Ny+j+2]);
Un[i*Ny+j] = make_U2(dt, U[i*Ny+j], U_n[i*Ny+j], L(FL, FR, GL, GR, dy, dx));
}
}
for(i=2;i<Nx;++i){
for(j=2;j<Ny;++j){
U[i*Ny+j] = Un[i*Ny+j];
}
}
set_boundary(Un, Nx, Ny, 0);
for(i=2;i<Nx-2;++i){
for(j=2;j<Ny-2;++j){
FL = Fhll(Un[(i-2)*Ny+j],Un[(i-1)*Ny+j],Un[i*Ny+j],Un[(i+1)*Ny+j]);
FR = Fhll(Un[(i-1)*Ny+j],Un[i*Ny+j],Un[(i+1)*Ny+j],Un[(i+2)*Ny+j]);
GL = Ghll(Un[i*Ny+j-2],Un[i*Ny+j-1],Un[i*Ny+j],Un[i*Ny+j+1]);
GR = Ghll(Un[i*Ny+j-1],Un[i*Ny+j],Un[i*Ny+j+1],Un[i*Ny+j+2]);
U_n[i*Ny+j] = make_UN(dt, U[i*Ny+j], Un[i*Ny+j], L(FL, FR, GL, GR, dy, dx));
}
}
for(i=2;i<Nx;++i){
for(j=2;j<Ny;++j){
U[i*Ny+j] = U_n[i*Ny+j];
}
}
set_boundary(U, Nx, Ny, 0);
free(U_n); free(Un);
return(dt);
}
void Write_Cons(int Nx, int Ny, Vars * U, double dx, double dy, FILE * fid){
int i, j; for(i=0;i<Nx;++i){
for(j=2;j<Ny-2;++j){
// printf("%d %d\n", i, j);
fprintf(fid, "%e %e %e %e %e %e %e\n", i*dx, j*dy, U[i*Ny+j].mass, U[i*Ny+j].xvelocity/U[i*Ny+j].mass, U[i*Ny+j].yvelocity/U[i*Ny+j].mass, U[i*Ny+j].energy, U[i*Ny+j].press);
}
fprintf(fid,"\n");
}
}
void Print(int Nx, int Ny, Vars * U, double dx, double dy){
int i, j; for(i=0;i<Nx;++i){
for(j=0;j<Ny;++j){
//if(i*dx == .5) printf("%f\n", U[i*Ny+j].mass);
printf( "%e %e %e %e %e %e %e\n", i*dx, j*dy, U[i*Ny+j].mass, U[i*Ny+j].xvelocity/U[i*Ny+j].mass, U[i*Ny+j].yvelocity/U[i*Ny+j].mass, U[i*Ny+j].energy, U[i*Ny+j].press);
}
printf("\n");
}
}
int main(int argc, char ** argv){
FILE * fid, * finit, * fdy;
fid = fopen("data_2D.dat", "w");
finit = fopen("init_data_2D.dat", "w");
int Nx = atof(argv[2]); int Ny = atof(argv[3]); double fac = atof(argv[4]);
Vars * U = malloc(Nx*Ny*sizeof(Vars)); double delt = 0.001;
double T, t, dt, dx, dy; T = atof(argv[1]); t = 0; dx = 1./(double)(Nx); dy = 1./(double)(Ny);
int type = 1;
init_sys(Nx, Ny, U, dx, dy); Write_Cons(Nx, Ny, U, dx, dy, finit); fclose(finit);
int count = 0; char str[80];
int nsteps = (int)(T/0.0001); //number of timestpes
while(t<T){
dt = advance_system(Nx, Ny, U, dx, dy, fac);
t += dt;
if(count % 300 == 0){
sprintf(str, "T_%d.dat", count/300);
fdy = fopen(str, "w");
Write_Cons(Nx, Ny, U, dx, dy, fdy);
fclose(fdy);
}
count += 1;
//break;
}
printf("nsteps = %d\n", count);
Write_Cons(Nx, Ny, U, dx, dy, fid);
fclose(fid);
free(U);
}