-
Notifications
You must be signed in to change notification settings - Fork 7
/
ffd.c
407 lines (346 loc) · 13.4 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
398
399
400
401
402
403
404
405
406
407
///////////////////////////////////////////////////////////////////////////////
///
/// \file ffd.c
///
/// \brief Main routine of Fast Fluid Dynamics
///
/// \author Mingang Jin, Qingyan Chen
/// Purdue University
/// Jin55@purdue.edu, YanChen@purdue.edu
/// Wangda Zuo
/// University of Miami
/// W.Zuo@miami.edu
///
/// \date 8/3/2013
///
///////////////////////////////////////////////////////////////////////////////
#include "ffd.h"
/* global variables */
REAL **var;
int **BINDEX;
REAL *locmin,*locmax;
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 SENSOR_DATA sens;
static INIT_DATA init;
clock_t start, end;
///////////////////////////////////////////////////////////////////////////////
/// Allcoate memory for variables
///
///\param para Pointer to FFD parameters
///
///\return No return needed
///////////////////////////////////////////////////////////////////////////////
int allocate_memory (PARA_DATA *para) {
int nb_var, i;
int size = (geom.imax+2) * (geom.jmax+2) * (geom.kmax+2);
/****************************************************************************
| Allocate memory for variables
****************************************************************************/
nb_var = 46 + para->bc->nb_Xi + para->bc->nb_C;
var = (REAL **) malloc ( nb_var*sizeof(REAL*) );
if(var==NULL) {
ffd_log("allocate_memory(): Could not allocate memory for var.",
FFD_ERROR);
return 1;
}
for(i=0; i<nb_var; i++) {
var[i] = (REAL *) calloc(size, sizeof(REAL));
if(var[i]==NULL) {
sprintf(msg,
"allocate_memory(): Could not allocate memory for var[%d]", i);
ffd_log(msg, FFD_ERROR);
return 1;
}
}
/****************************************************************************
| Allocate memroy for boundary cells
| BINDEX[0]: i of global coordinate in IX(i,j,k)
| BINDEX[1]: j of global coordinate in IX(i,j,k)
| BINDEX[2]: k of global coordinate in IX(i,j,k)
| BINDEX[3]: Fixed temperature or fixed heat flux
| BINDEX[4]: Boundary ID to identify which boundary it belongs to
****************************************************************************/
BINDEX = (int **)malloc(5*sizeof(int*));
if(BINDEX==NULL) {
ffd_log("allocate_memory(): Could not allocate memory for BINDEX.",
FFD_ERROR);
return 1;
}
for(i=0; i<5; i++)
BINDEX[i] = (int *) malloc(size*sizeof(int));
if(BINDEX[i]==NULL) {
sprintf(msg,
"allocate_memory(): Could not allocate memory for BINDEX[%d]", i);
ffd_log(msg, FFD_ERROR);
return 1;
}
return 0;
} // End of allocate_memory()
///////////////////////////////////////////////////////////////////////////////
/// GLUT display callback routines
///
///\return No return needed
///////////////////////////////////////////////////////////////////////////////
static void display_func(void) {
ffd_display_func(¶, var);
} // End of display_func()
///////////////////////////////////////////////////////////////////////////////
/// GLUT keyboard callback routines
///
///\param key Chararcter of the key
///\param x X-position
///\param y Y-Positon
///
///\return No return needed
///////////////////////////////////////////////////////////////////////////////
static void key_func(unsigned char key, int x, int y) {
ffd_key_func(¶, var, BINDEX, key);
} // End of key_func()
///////////////////////////////////////////////////////////////////////////////
/// GLUT idle callback routines
///
///\return No return needed
///////////////////////////////////////////////////////////////////////////////
static void idle_func(void) {
ffd_idle_func(¶, var, BINDEX);
} // End of idle_func()
///////////////////////////////////////////////////////////////////////////////
/// GLUT motion callback routines
///
///\param x X-position
///\param y Y-Positon
///
///\return No return needed
///////////////////////////////////////////////////////////////////////////////
static void motion_func(int x, int y) {
ffd_motion_func(¶, x, y);
} // End of motion_func()
///////////////////////////////////////////////////////////////////////////////
/// GLUT mouse callback routines
///
///\param button Button of the mouse
///\param x X-position
///\param y Y-Positon
///
///\return No return needed
///////////////////////////////////////////////////////////////////////////////
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
///
///\return No return needed
///////////////////////////////////////////////////////////////////////////////
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
///
///\param width Width of the window
///\param height Height of the window
///
///\return No return needed
///////////////////////////////////////////////////////////////////////////////
static void reshape_func(int width, int height) {
ffd_reshape_func(¶, width, height);
} // End of reshape_func()
///////////////////////////////////////////////////////////////////////////////
/// Lanuch the FFD simulation through a thread
///
///\param p Pointer to the cosimulaiton data
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER //Windows
DWORD WINAPI ffd_thread(void *p){
ULONG workerID = (ULONG)(ULONG_PTR)p;
#else //Linux
void ffd_thread(void* p){
#endif
CosimulationData *cosim = (CosimulationData *) p;
int cosimulation = 1;
#ifdef _MSC_VER //Windows
sprintf(msg, "Start Fast Fluid Dynamics Simulation with Thread ID %lu", workerID);
#else //Linux
sprintf(msg, "Start Fast Fluid Dynamics Simulation with Thread");
#endif
printf("%s\n", msg);
ffd_log(msg, FFD_NEW);
sprintf(msg, "fileName=\"%s\"", cosim->para->fileName);
ffd_log(msg, FFD_NORMAL);
para.cosim = (CosimulationData *) malloc(sizeof(CosimulationData));
para.cosim = cosim;
if(ffd(cosimulation)!=0)
cosim->para->ffdError = 1;
ffd_log("Successfully exit FFD.", FFD_NORMAL);
return 0;
} // End of ffd_thread()
///////////////////////////////////////////////////////////////////////////////
/// Main routine of FFD
///
///\para cosimulation Integer to identify the simulation type
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int ffd(int cosimulation) {
// Initialize the parameters
para.geom = &geom;
para.inpu = &inpu;
para.outp = &outp1;
para.prob = &prob;
para.mytime = &mytime;
para.bc = &bc;
para.solv = &solv;
para.sens = &sens;
para.init = &init;
// Stand alone simulation: 0; Cosimulaiton: 1
para.solv->cosimulation = cosimulation;
#ifndef _MSC_VER //Linux
//Initialize glut library
char fakeParam[] = "fake";
char *fakeargv[] = { fakeParam, NULL };
int fakeargc = 1;
glutInit( &fakeargc, fakeargv );
#endif
if(initialize(¶)!=0) {
ffd_log("ffd(): Could not initialize simulation parameters.", FFD_ERROR);
return 1;
}
// Overwrite the mesh and simulation data using SCI generated file
if(para.inpu->parameter_file_format == SCI) {
if(read_sci_max(¶, var)!=0) {
ffd_log("ffd(): Could not read SCi data.", FFD_ERROR);
return 1;
}
}
// Allocate memory for the variables
if(allocate_memory(¶)!=0) {
ffd_log("ffd(): Could not allocate memory for the simulation.", FFD_ERROR);
return 1;
}
// Set the initial values for the simulation data
if(set_initial_data(¶, var, BINDEX)) {
ffd_log("ffd(): Could not set initial data.", FFD_ERROR);
return 1;
}
// Read previous simulation data as initial values
if(para.inpu->read_old_ffd_file==1) read_ffd_data(¶, var);
ffd_log("ffd.c: Start FFD solver.", FFD_NORMAL);
// Solve the problem
if(para.outp->version==DEMO) {
open_glut_window();
glutMainLoop();
}
else
if(FFD_solver(¶, var, BINDEX)!=0) {
ffd_log("ffd(): FFD solver failed.", FFD_ERROR);
return 1;
}
/*---------------------------------------------------------------------------
| Post Process
---------------------------------------------------------------------------*/
// Calculate mean value
if(para.outp->cal_mean == 1)
average_time(¶, var);
// Fixme: Simulaiton stops here
if(write_unsteady(¶, var, "unsteady")!=0) {
ffd_log("FFD_solver(): Could not write the file unsteady.plt.", FFD_ERROR);
return 1;
}
if(write_tecplot_data(¶, var, "result")!=0) {
ffd_log("FFD_solver(): Could not write the file result.plt.", FFD_ERROR);
return 1;
}
if(para.outp->version == DEBUG)
write_tecplot_all_data(¶, var, "result_all");
// Write 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();
// Inform Modelica the stopping command has been received
if(para.solv->cosimulation==1) {
para.cosim->para->flag = 2;
ffd_log("ffd(): Sent stopping signal to Modelica", FFD_NORMAL);
}
return 0;
} // End of ffd( )