forked from zuowangda/Fast-Fluid-Dynamics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffd.c
397 lines (344 loc) · 13.9 KB
/
ffd.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
///////////////////////////////////////////////////////////////////////////////
//
// Filename: ffd.c
//
// Task: Main routine of Fast Fluid Dynamics
//
// Modification history:
// 7/10/2013 by Wangda Zuo: re-constructed the code for release
//
///////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#ifdef _MSC_VER
#include <glut.h>
#elseif
#include <GL/freeglut.h>
#endif
#include "data_structure.h" // This file must be the first included
#include "boundary.h"
#include "data_writer.h"
#include "initialization.h"
#include "visualization.h"
#include "ffd.h"
#include "ffd_data_reader.h"
#include "grid.h"
#include "timing.h"
#include "sci_reader.h"
#include "solver.h"
/* global variables */
static REAL dt, diff, visc;
static REAL force, source;
static int screen;
REAL **var;
int **BINDEX;
int *xindex, *yindex, *zindex, *fltemp, *bcid;
REAL *x, *y, *z, *gx, *gy, *gz;
REAL *u, *v, *w, *u_s, *v_s, *w_s, *u_mean, *v_mean, *w_mean;
REAL *dens, *dens_s, *temp, *temp_s, *temp_mean, *p, *my_div, *pp;
REAL *tmp1, *tmp2, *tmp3;
REAL *ap, *an, *as, *aw, *ae, *b, *ab, *af, *ap0;
REAL *flagp, *flagu, *flagv, *flagw;
REAL *locmin,*locmax;
REAL *vxbc,*vybc,*vzbc,*tempbc, *qfluxbc, *qflux;
static GEOM_DATA geom;
static PROB_DATA prob;
static TIME_DATA mytime;
static INPU_DATA inpu;
static OUTP_DATA outp1;
static BC_DATA bc;
static SOLV_DATA solv;
static PARA_DATA para;
clock_t start, end;
/******************************************************************************
| allocate data
******************************************************************************/
int allocate_data (void)
{
int size = (geom.imax+2) * (geom.jmax+2) * (geom.kmax+2);
printf( "size=%d\n", size);
x = (REAL *) malloc ( size*sizeof(REAL) );
y = (REAL *) malloc ( size*sizeof(REAL) );
z = (REAL *) malloc ( size*sizeof(REAL) );
u = (REAL *) malloc ( size*sizeof(REAL) );
v = (REAL *) malloc ( size*sizeof(REAL) );
w = (REAL *) malloc ( size*sizeof(REAL) );
u_s = (REAL *) malloc ( size*sizeof(REAL) );
v_s = (REAL *) malloc ( size*sizeof(REAL) );
w_s = (REAL *) malloc ( size*sizeof(REAL) );
u_mean = (REAL *) malloc ( size*sizeof(REAL) );
v_mean = (REAL *) malloc ( size*sizeof(REAL) );
w_mean = (REAL *) malloc ( size*sizeof(REAL) );
temp = (REAL *) malloc ( size*sizeof(REAL) );
temp_s = (REAL *) malloc ( size*sizeof(REAL) );
temp_mean = (REAL *) malloc ( size*sizeof(REAL) );
dens = (REAL *) malloc ( size*sizeof(REAL) );
dens_s = (REAL *) malloc ( size*sizeof(REAL) );
p = (REAL *) malloc ( size*sizeof(REAL) );
tmp1 = (REAL *) malloc ( size*sizeof(REAL) );
tmp2 = (REAL *) malloc ( size*sizeof(REAL) );
tmp3 = (REAL *) malloc ( size*sizeof(REAL) );
ap = (REAL *) malloc ( size*sizeof(REAL) );
an = (REAL *) malloc ( size*sizeof(REAL) );
as = (REAL *) malloc ( size*sizeof(REAL) );
aw = (REAL *) malloc ( size*sizeof(REAL) );
ae = (REAL *) malloc ( size*sizeof(REAL) );
ab = (REAL *) malloc ( size*sizeof(REAL) );
af = (REAL *) malloc ( size*sizeof(REAL) );
b = (REAL *) malloc ( size*sizeof(REAL) );
gx = (REAL *) malloc ( size*sizeof(REAL) );
gy = (REAL *) malloc ( size*sizeof(REAL) );
gz = (REAL *) malloc ( size*sizeof(REAL) );
ap0 = (REAL *) malloc ( size*sizeof(REAL) );
pp = (REAL *) malloc ( size*sizeof(REAL) );
flagp = (REAL *) malloc ( size*sizeof(REAL) );
flagu = (REAL *) malloc ( size*sizeof(REAL) );
flagv = (REAL *) malloc ( size*sizeof(REAL) );
flagw = (REAL *) malloc ( size*sizeof(REAL) );
locmin = (REAL *) malloc ( size*sizeof(REAL) );
locmax = (REAL *) malloc ( size*sizeof(REAL) );
vxbc = (REAL *) malloc ( size*sizeof(REAL) );
vybc = (REAL *) malloc ( size*sizeof(REAL) );
vzbc = (REAL *) malloc ( size*sizeof(REAL) );
tempbc = (REAL *) malloc ( size*sizeof(REAL) );
qfluxbc = (REAL *) malloc ( size*sizeof(REAL) );
qflux = (REAL *) malloc ( size*sizeof(REAL) );
var = (REAL **) malloc ( 46*sizeof(REAL*) );
var[X] = x;
var[Y] = y;
var[Z] = z;
var[VX] = u;
var[VY] = v;
var[VZ] = w;
var[VXS] = u_s;
var[VYS] = v_s;
var[VZS] = w_s;
var[VXM] = u_mean;
var[VYM] = v_mean;
var[VZM] = w_mean;
var[DEN] = dens;
var[DENS] = dens_s;
var[IP] = p;
var[TEMP] = temp;
var[TEMPS] = temp_s;
var[TEMPM] = temp_mean;
var[AP] = ap;
var[AN] = an;
var[AS] = as;
var[AW] = aw;
var[AE] = ae;
var[AB] = ab;
var[AF] = af;
var[B] = b;
var[TMP1] = tmp1;
var[TMP2] = tmp2;
var[TMP3] = tmp3;
var[GX] = gx;
var[GY] = gy;
var[GZ] = gz;
var[AP0] = ap0;
var[PP] = pp;
var[FLAGP] =flagp;
var[FLAGU] =flagu;
var[FLAGV] =flagv;
var[FLAGW] =flagw;
var[LOCMIN] =locmin;
var[LOCMAX] =locmax;
var[VXBC] =vxbc;
var[VYBC] =vybc;
var[VZBC] =vzbc;
var[TEMPBC] =tempbc;
var[QFLUXBC]= qfluxbc;
var[QFLUX] = qflux;
xindex = (int *) malloc ( size*sizeof(int) );
yindex = (int *) malloc ( size*sizeof(int) );
zindex = (int *) malloc ( size*sizeof(int) );
fltemp = (int *) malloc ( size*sizeof(int) );
bcid = (int *) malloc ( size*sizeof(int) );
BINDEX = (int **) malloc ( 5*sizeof(int*) );
BINDEX[0] = xindex; // i of global coordinate in IX(i,j,k)
BINDEX[1] = yindex; // j of global coordinate in IX(i,j,k)
BINDEX[2] = zindex; // k of global coordinate in IX(i,j,k)
BINDEX[3] = fltemp; // fixed temperature or fixed heat flux
BINDEX[4] = bcid; // id of boundary
if( !x || !y || !z || !u || !v || !w || !u_s || !v_s || !w_s ||
!u_mean || !v_mean || !w_mean ||
!dens || !dens_s || !temp || !temp_s || !temp_mean ||
!tmp1 || !tmp2 || !tmp3 ||
!ap || !ae || !aw || !as || !an || !ab || !af || !b || !gx || !gy || !gz || !ap0 || !pp || !flagp ||
!flagu || !flagv || !flagw || !locmin || !locmax ||
!vxbc || !vybc ||! vzbc || !tempbc || !qfluxbc || !qflux ||
!xindex || !yindex || !zindex || !bcid)
{
fprintf( stderr, "cannot allocate data\n" );
return 1;
}
return 0;
} /** allocate_data() **/
/******************************************************************************
| GLUT display callback routines
******************************************************************************/
static void display_func(void)
{
ffd_display_func(¶, var);
} // End of display_func()
/******************************************************************************
| GLUT keyboard callback routines
******************************************************************************/
static void key_func(unsigned char key, int x, int y)
{
ffd_key_func(¶, var, BINDEX, key, x, y);
} // End of key_func()
/******************************************************************************
| GLUT idle callback routines
******************************************************************************/
static void idle_func(void)
{
ffd_idle_func(¶, var, BINDEX);
} // End of idle_func()
/******************************************************************************
| GLUT motion callback routines
******************************************************************************/
static void motion_func(int x, int y)
{
ffd_motion_func(¶, x, y);
} // End of motion_func()
/******************************************************************************
| GLUT mouse callback routines
******************************************************************************/
static void mouse_func(int button, int state, int x, int y)
{
ffd_mouse_func(¶, button, state, x, y);
} // End of mouse_func()
/******************************************************************************
| open_glut_window --- open a glut compatible window and set callbacks
******************************************************************************/
static void open_glut_window()
{
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
/*---------------------------------------------------------------------------
| void glutInitWindowPosition(int x, int y);
| x: Window X location in pixels.
| y: Window Y location in pixels.
---------------------------------------------------------------------------*/
glutInitWindowPosition(0, 0);
/*---------------------------------------------------------------------------
| Initialize the size of window
| void glutInitWindowSize(int width, int height);
| width: Width in pixels; height: Height in pixels
---------------------------------------------------------------------------*/
glutInitWindowSize(para.outp->winx, para.outp->winy);
para.outp->win_id = glutCreateWindow("FFD, Author: W. Zuo, Q. Chen");
/*---------------------------------------------------------------------------
|void glClearColor(GLclampf red, GLclampf green, GLclampf blue,
| GLclampf alpha)
|set the color when you clear the screen, alpha is not useful here
|white :1.0, 1.0, 1.0, 0.0
|black :0.0, 0.0, 0.0, 0.0
|most blue:0.0, 0.0, 1.0, 0.0
|most red :1.0, 0.0, 0.0, 0.0
---------------------------------------------------------------------------*/
glClearColor(0.0, 0.0, 0.0, 1.0);
/*--------------------------------------------------------------------------
| clear buffers within the viewport
---------------------------------------------------------------------------*/
glClear(GL_COLOR_BUFFER_BIT);
/*---------------------------------------------------------------------------
| Performs a buffer swap on the layer in use for the current window
---------------------------------------------------------------------------*/
glutSwapBuffers();
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
pre_2d_display(¶);
/*---------------------------------------------------------------------------
| void glutKeyboardFunc(void (*func)(unsigned char key, int x, int y));
| sets the keyboard callback for the current window.
| When a user types into the window, each key press generating an ASCII
| character will generate a keyboard callback.
---------------------------------------------------------------------------*/
glutKeyboardFunc(key_func);
/*---------------------------------------------------------------------------
| void glutMouseFunc(void (*func)(int button, int state, int x, int y));
| sets the mouse callback for the current window.
---------------------------------------------------------------------------*/
glutMouseFunc(mouse_func);
/*---------------------------------------------------------------------------
| void glutMotionFunc(void (*func)(int x, int y));
| The motion callback for a window is called when the mouse moves within
| the window while one or more mouse buttons are pressed
---------------------------------------------------------------------------*/
glutMotionFunc(motion_func);
/*---------------------------------------------------------------------------
| void glutReshapeFunc(void (*func)(int width, int height));
| The reshape callback is triggered when a window is reshaped
---------------------------------------------------------------------------*/
glutReshapeFunc(reshape_func);
/*---------------------------------------------------------------------------
| void glutIdleFunc(void (*func)(void));
| sets the global idle callback to be func so a GLUT program can perform
| background processing tasks or continuous animation when window system
| events are not being received
---------------------------------------------------------------------------*/
glutIdleFunc(idle_func);
/*---------------------------------------------------------------------------
| void glutDisplayFunc(void (*func)(void));
| sets the display callback for the current window
---------------------------------------------------------------------------*/
glutDisplayFunc (display_func);
} // End of open_glut_window()
/******************************************************************************
| GLUT reshape callback routines
******************************************************************************/
static void reshape_func(int width, int height)
{
ffd_reshape_func(¶, width, height);
} // End of reshape_func()
/******************************************************************************
main --- main routine
******************************************************************************/
int main()
{
// Initialize the parameters
para.geom = &geom;
para.inpu = &inpu;
para.outp = &outp1;
para.prob = &prob;
para.mytime = &mytime;
para.bc = &bc;
para.solv = &solv;
ffd_log("Start Fast Fluid Dynamics Simulation.", FFD_NEW);
if(initialize(¶)) exit(1);
// Overwrite the mesh and simulation data using SCI generated file
if(para.inpu->parameter_file_format == SCI)
{
if(read_sci_max(¶, var)) exit(1);
}
if(para.outp->version == DEBUG)
printf("imax= %d\t jmax= %d\t kmax= %d\n ", para.geom->imax,para.geom->jmax,para.geom->kmax);
// Allocate memory for the variables
if(allocate_data( )) exit(1);
// Set the initial values for the simulation data
if(set_initial_data(¶, var, BINDEX)) exit(1);
// Read previous simulation data as initial values
if(para.inpu->read_old_ffd_file==1) read_ffd_data(¶, var);
// Solve the problem
if(para.outp->version==DEMO) /* show visulization */
{
open_glut_window();
glutMainLoop();
}
else
FFD_solver(¶, var, BINDEX);
if(para.outp->version == DEBUG)
write_tecplot_all_data(¶, var, "result_all");
// Wrtie the data in SCI format
write_SCI(¶, var, "output");
// Free the memory
free_data(var);
free_index(BINDEX);
// End the simulation
if(para.outp->version==DEBUG || para.outp->version==DEMO) {}//getchar();
ffd_log("ffd.c: Exit successfully.", FFD_NORMAL);
exit (0);
} // End of main( )