-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrivenCavityFlow_v11.c
307 lines (282 loc) · 7.58 KB
/
DrivenCavityFlow_v11.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct _PARAMS_{
double AP;
double AE;
double AW;
double AN;
double AS;
double AEE;
double AWW;
double ANN;
double ASS;
}PARAMS, *PPARAMS;
typedef struct _GRID_{
double x;
double y;
}GRID, *PGRID;
typedef struct _U_{
double value;
GRID grid;
PARAMS params;
PARAMS pressure_params;
}U, *PU;
typedef struct _V_{
double value;
GRID grid;
PARAMS params;
PARAMS pressure_params
}V, *PV;
typedef struct _P_{
double value;
GRID grid;
PARAMS pressure_params; // TODO:
}P, *PP;
// allocate the memory
void AllocateMemory(U** u, V** v, P** p, int nx, int ny)
{
*u = (U*)malloc(sizeof(U)*nx*ny);
*v = (V*)malloc(sizeof(V)*nx*ny);
*p = (P*)malloc(sizeof(P)*nx*ny);
// set the memory to zero
memset(*u, 0, sizeof(U)*nx*ny);
memset(*v, 0, sizeof(V)*nx*ny);
memset(*p, 0, sizeof(P)*nx*ny);
}
// free the memory
void FreeMemory(U* u, V* v, P* p)
{
free(u);
free(v);
free(p);
u = NULL;
v = NULL;
p = NULL;
}
// grid generation, only one grid system
// walk through the J direction first.
// =========================== ->ue
// j-> | |
// | |
// | |
// O==========================
// /|\
// |
// i
void Grid(U* u, V* v, P* p, int nx, int ny, double X, double Y)
{
int i, j;
double dx = X / (double)nx;
double dy = Y / (double)ny;
for (i = 0; i < nx; i++){
for (j = 0; j < ny; j++){
u[i*ny + j].grid.x = i*dx;
u[i*ny + j].grid.y = j*dy;
v[i*ny + j].grid.x = i*dx;
v[i*ny + j].grid.y = j*dy;
p[i*ny + j].grid.x = i*dx;
p[i*ny + j].grid.y = j*dy;
printf("(%lf, %lf)\n", p[i*ny + j].grid.x, p[i*ny + j].grid.y);
}
}
}
// initialize the field
void Initialize(U *u, V* v, P* p,
int nx, int ny,
double init_u, double init_v, double init_p)
{
int i, j;
for (j = 0; j < ny; j++){
for (i = 0; i < nx; i++){
u[i*ny + j].value = init_u;
v[i*ny + j].value = init_v;
p[i*ny + j].value = init_p;
}
}
}
void DebugPrintGrid(U* u, V* v, P* p, int nx, int ny)
{
int i, j;
FILE *fp = NULL;
fp = fopen("grid.txt", "w");
if (fp == NULL){
printf("[*] DebugPrintGrid : can not open the file....\n");
return;
}
fputs("U location\n", fp);
for (j = ny - 1; j >= 0; j--){
for (i = 0; i < nx; i++){
fprintf(fp, "(%lf, %lf) ", u[i*ny + j].grid.x, u[i*ny + j].grid.y);
}
fputs("\n", fp);
}
fputs("V location\n", fp);
for (j = ny - 1; j >= 0; j--){
for (i = 0; i < nx; i++){
fprintf(fp, "(%lf, %lf) ", v[i*ny + j].grid.x, v[i*ny + j].grid.y);
}
fputs("\n", fp);
}
fputs("P location\n", fp);
for (j = ny - 1; j >= 0; j--){
for (i = 0; i < nx; i++){
fprintf(fp, "(%lf, %lf) ", p[i*ny + j].grid.x, p[i*ny + j].grid.y);
}
fputs("\n", fp);
}
fclose(fp);
}
// Boundary dynamic_viscosityition ( driven cavity flow )
void Boundary(U* u, V* v, P* p, int nx, int ny, double boundary_u)
{
int i, j;
// upper wall
for (i = 0; i < nx; i++){
u[i*ny + ny - 1].value = boundary_u;
u[i*ny + ny - 2].value = boundary_u;
v[i*ny + ny - 1].value = 0.0;
v[i*ny + ny - 2].value = 0.0;
}
// bottom wall
for (i = 0; i < nx; i++){
u[i*ny + 0].value = 0.0;
u[i*ny + 1].value = 0.0;
v[i*ny + 0].value = 0.0;
v[i*ny + 1].value = 0.0;
}
// left wall
for (j = 0; j < ny; j++){
u[0 * ny + j].value = 0.0;
u[1 * ny + j].value = 0.0;
v[0 * ny + j].value = 0.0;
v[1 * ny + j].value = 0.0;
}
// right wall
for (j = 0; j < ny; j++){
u[(nx - 1)*ny + j].value = 0.0;
u[(nx - 2)*ny + j].value = 0.0;
v[(nx - 1)*ny + j].value = 0.0;
v[(nx - 2)*ny + j].value = 0.0;
}
}
// Debug the boundary
void DebugPrintBoundary(U* u, V* v, P* p, int nx, int ny)
{
int i, j;
FILE *fp = NULL;
fp = fopen("boundary.txt", "w");
if (fp == NULL){
printf("[*] DebugPrintBoundary : can not open the file....\n");
return;
}
fputs("U\n", fp);
for (j = ny - 1; j >= 0; j--){
for (i = 0; i < nx; i++){
fprintf(fp, "%6.2f ", u[i*ny + j].value);
}
fputs("\n", fp);
}
fputs("V\n", fp);
for (j = ny - 1; j >= 0; j--){
for (i = 0; i < nx; i++){
fprintf(fp, "%6.2f ", v[i*ny + j].value);
}
fputs("\n", fp);
}
fclose(fp);
}
// get the coefficient of momentum equation on X direction
void MomentumX(U* u, V* v, P* p,
int nx, int ny,
U* last_u, V* last_v, P* last_p,
double rho, double dynamic_viscosity,
double dx, double dy)
{
int i, j;
double flow_u = 0.0;
double flow_v = 0.0;
int x = nx - 4;
int y = ny - 4;
for (i = 2; i < nx-2; i++){
for (j = 2; j < ny-2; j++){
flow_u = rho*last_u[i*ny + j].value;
flow_v = rho*last_v[i*ny + j].value;
if (u[i*ny + j].value>0){
// velocity parameters
u[i*ny + j].params.AP = (3 * flow_u / (2 * dx)) +
(3 * flow_v / (2 * dy)) +
(2 * dynamic_viscosity / (dx*dx)) +
(2 * dynamic_viscosity / (dy*dy));
u[i*ny + j].params.AW = (4 * flow_u / (2 * dx)) + dynamic_viscosity / (dx*dx);
u[i*ny + j].params.AWW = -2 * flow_u / (2 * dx);
u[i*ny + j].params.AE = dynamic_viscosity / (dx*dx);
u[i*ny + j].params.AS = (4 * flow_v / (2 * dy)) + dynamic_viscosity / (dy*dy);
u[i*ny + j].params.ASS = -flow_v / (2 * dy);
u[i*ny + j].params.AN = dynamic_viscosity / (dy*dy);
u[i*ny + j].params.ANN = 0.0;
u[i*ny + j].params.AEE = 0.0;
// pressure parameters
u[i*ny + j].pressure_params.AP = -3 / (2 * dx);
u[i*ny + j].pressure_params.AW = 4 / (2 * dx);
u[i*ny + j].pressure_params.AWW = -1 / (2 * dx);
u[i*ny + j].pressure_params.AE = 0;
u[i*ny + j].pressure_params.AEE = 0;
u[i*ny + j].pressure_params.AS = 0;
u[i*ny + j].pressure_params.ASS = 0;
u[i*ny + j].pressure_params.AN = 0;
u[i*ny + j].pressure_params.ANN = 0;
}
else if (u[i*ny + j].value < 0){
// TODO:
}
}
}
}
// get the coefficient of momentum equation on Y direction
void MomentumY(U*u, V* v, P* p,
int nx, int ny,
U* last_u, V* last_v, P* last_p,
double rho, double dynamic_viscosity)
{
int i, j;
int x = nx - 4;
int y = ny - 4;
for (i = 2; i < nx - 2; i++){
for (j = 2; j < ny - 2; j++){
if (v[i*ny + j].value>0){
// todo
}
}
}
}
int main(int argc, char *argv[])
{
double X = 10.0;
double Y = 5.0;
int nx = 20; // col
int ny = 20; // row
double init_u = 1.0;
double init_v = 1.0;
double init_p = 0.0;
double boundary_u = 100.0;
U* u = NULL;
V* v = NULL;
P* p = NULL;
AllocateMemory(&u, &v, &p, nx, ny);
printf("Allocate memory has done...\n");
Grid(u, v, p, nx, ny, X, Y);
printf("Grid generation has done...\n");
DebugPrintGrid(u, v, p, nx, ny);
printf("DebugPrintGrid has done...\n");
Initialize(u, v, p, nx, ny, init_u, init_v, init_p);
printf("Initialize has done...\n");
Boundary(u, v, p, nx, ny, boundary_u);
printf("Boundary has done...\n");
DebugPrintBoundary(u, v, p, nx, ny);
FreeMemory(u, v, p);
printf("All the allocated memory has been freed...\n");
printf("Done...\n");
return 0;
}