-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsolver.c
More file actions
368 lines (331 loc) · 11.9 KB
/
solver.c
File metadata and controls
368 lines (331 loc) · 11.9 KB
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
///////////////////////////////////////////////////////////////////////////////
///
/// \file solver.c
///
/// \brief Solver of FFD
///
/// \author Mingang Jin, Qingyan Chen
/// Purdue University
/// Wangda Zuo
/// University of Miami
///
/// \date 8/3/2013
///
///////////////////////////////////////////////////////////////////////////////
#include "solver.h"
///////////////////////////////////////////////////////////////////////////////
/// FFD solver
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///\param BINDEX Pointer to boundary index
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int FFD_solver(PARA_DATA *para, REAL **var, int **BINDEX) {
int imax = para->geom->imax, jmax = para->geom->jmax;
int kmax = para->geom->kmax;
int step_total = para->mytime->step_total;
REAL t_steady = para->mytime->t_steady;
int cal_mean = para->outp->cal_mean;
double t_cosim;
int flag, next;
if(para->solv->cosimulation == 1)
t_cosim = para->mytime->t + para->cosim->modelica->dt;
/***************************************************************************
| Solver Loop
***************************************************************************/
next = 1;
while(next==1) {
//-------------------------------------------------------------------------
// Integration
//-------------------------------------------------------------------------
flag = vel_step(para, var, BINDEX);
if(flag != 0) {
ffd_log("FFD_solver(): Could not solve velocity.", FFD_ERROR);
return flag;
}
flag = temp_step(para, var, BINDEX);
if(flag != 0) {
ffd_log("FFD_solver(): Could not solve temperature.", FFD_ERROR);
return flag;
}
flag = den_step(para, var, BINDEX);
if(flag != 0) {
ffd_log("FFD_solver(): Could not solve trace substance.", FFD_ERROR);
return flag;
}
timing(para);
//-------------------------------------------------------------------------
// Process for Cosimulation
//-------------------------------------------------------------------------
if(para->solv->cosimulation == 1) {
/*.......................................................................
| Conditon 1: If synchronization point is reached,
| Action: Do data exchange
.......................................................................*/
if(fabs(para->mytime->t - t_cosim)<SMALL) {
// Average the FFD simulation data
flag = average_time(para, var);
if(flag != 0) {
ffd_log("FFD_solver(): Could not average the data over time.",
FFD_ERROR);
return flag;
}
// the data for cosimulation
flag = read_cosim_data(para, var, BINDEX);
if(flag != 0) {
ffd_log("FFD_solver(): Could not read cosimulation data.", FFD_ERROR);
return flag;
}
flag = write_cosim_data(para, var);
if(flag != 0) {
ffd_log("FFD_solver(): Could not write cosimulation data.", FFD_ERROR);
return flag;
}
sprintf(msg, "ffd_solver(): Synchronized data at t=%f[s]\n", para->mytime->t);
ffd_log(msg, FFD_NORMAL);
// Set the next synchronization time
t_cosim += para->cosim->modelica->dt;
// Reset all the averaged data to 0
flag = reset_time_averaged_data(para, var);
if(flag != 0) {
ffd_log("FFD_solver(): Could not reset averaged data.",
FFD_ERROR);
return flag;
}
/*.......................................................................
| Check if Modelica asks to stop the simulation
.......................................................................*/
if(para->cosim->para->flag==0) {
// Stop the solver
next = 0;
sprintf(msg,
"ffd_solver(): Received stop command from Modelica at "
"FFD time: %f[s], Modelica Time: %f[s].",
para->mytime->t, para->cosim->modelica->t);
if(para->mytime->t==para->cosim->modelica->t)
ffd_log(msg, FFD_NORMAL);
else
ffd_log(msg, FFD_WARNING);
}
continue;
} // End of Conditon 1
/*.......................................................................
| Conditon 2: synchronization point is not reached ,
| but already miss the synchronization point
| Action: Stop simulation
.......................................................................*/
else if(para->mytime->t-t_cosim>SMALL) {
sprintf(msg,
"ffd_solver(): Mis-matched synchronization step with "
"t_ffd=%f[s], t_cosim=%f[s], dt_syn=%f[s], dt_ffd=%f[s].",
para->mytime->t, t_cosim,
para->cosim->modelica->dt, para->mytime->dt);
ffd_log(msg, FFD_ERROR);
sprintf(msg, "para->mytime->t - t_cosim=%lf", para->mytime->t - t_cosim);
ffd_log(msg, FFD_ERROR);
return 1;
} // end of Conditon 2
/*.......................................................................
| Conditon 3: synchronization point is not reached
| and not miss the synchronization point
| Action: Do FFD internal simulation and add data for future average
.......................................................................*/
else {
// Integrate the data on the boundary surface
flag = surface_integrate(para, var, BINDEX);
if(flag != 0) {
ffd_log("FFD_solver(): "
"Could not average the data on boundary.",
FFD_ERROR);
return flag;
}
flag = add_time_averaged_data(para, var);
if(flag != 0) {
ffd_log("FFD_solver(): "
"Could not add the averaged data.",
FFD_ERROR);
return flag;
}
} // End of Condition 3
} // End of cosimulation
//-------------------------------------------------------------------------
// Process for single simulation
//-------------------------------------------------------------------------
else {
// Start to record data for calculating mean velocity if needed
if(para->mytime->t>t_steady && cal_mean==0) {
cal_mean = 1;
flag = reset_time_averaged_data(para, var);
if(flag != 0) {
ffd_log("FFD_solver(): Could not reset averaged data.",
FFD_ERROR);
return flag;
}
else
ffd_log("FFD_solver(): Start to calculate mean properties.",
FFD_NORMAL);
}
if(cal_mean==1) {
flag = add_time_averaged_data(para, var);
if(flag != 0) {
ffd_log("FFD_solver(): Could not add the averaged data.",
FFD_ERROR);
return 1;
}
}
next = para->mytime->step_current < step_total ? 1 : 0;
}
} // End of While loop
return flag;
} // End of FFD_solver( )
///////////////////////////////////////////////////////////////////////////////
/// Calculate the temperature
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///\param BINDEX Pointer to boundary index
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int temp_step(PARA_DATA *para, REAL **var, int **BINDEX) {
REAL *T = var[TEMP], *T0 = var[TMP1];
int flag = 0;
flag = advect(para, var, TEMP, 0, T0, T, BINDEX);
if(flag!=0) {
ffd_log("temp_step(): Could not advect temperature.", FFD_ERROR);
return flag;
}
flag = diffusion(para, var, TEMP, 0, T, T0, BINDEX);
if(flag!=0) {
ffd_log("temp_step(): Could not diffuse temperature.", FFD_ERROR);
return flag;
}
return flag;
} // End of temp_step( )
///////////////////////////////////////////////////////////////////////////////
/// Calculate the contaminant concentration
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///\param BINDEX Pointer to boundary index
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int den_step(PARA_DATA *para, REAL **var, int **BINDEX) {
REAL *den, *den0 = var[TMP1];
int i, flag = 0;
for(i=0; i<para->bc->nb_Xi; i++) {
den = var[TRACE+i];
flag = advect(para, var, TRACE, i, den0, den, BINDEX);
if(flag!=0) {
sprintf(msg, "den_step(): Could not advect for trace substance %d", i);
ffd_log(msg, FFD_ERROR);
return flag;
}
flag = diffusion(para, var, TRACE, i, den, den0, BINDEX);
if(flag!=0) {
sprintf(msg, "den_step(): Could not diffuse trace substance %d", i);
ffd_log(msg, FFD_ERROR);
return flag;
}
}
return flag;
} // End of den_step( )
///////////////////////////////////////////////////////////////////////////////
/// Calculate the velocity
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///\param BINDEX Pointer to boundary index
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int vel_step(PARA_DATA *para, REAL **var,int **BINDEX) {
REAL *u = var[VX], *v = var[VY], *w = var[VZ];
REAL *u0 = var[TMP1], *v0 = var[TMP2], *w0 = var[TMP3];
int flag = 0;
flag = advect(para, var, VX, 0, u0, u, BINDEX);
if(flag!=0) {
ffd_log("vel_step(): Could not advect for velocity X.", FFD_ERROR);
return flag;
}
flag = advect(para, var, VY, 0, v0, v, BINDEX);
if(flag!=0) {
ffd_log("vel_step(): Could not advect for velocity Y.", FFD_ERROR);
return flag;
}
flag = advect(para, var, VZ, 0, w0, w, BINDEX);
if(flag!=0) {
ffd_log("vel_step(): Could not advect for velocity Z.", FFD_ERROR);
return flag;
}
flag = diffusion(para, var, VX, 0, u, u0, BINDEX);
if(flag!=0) {
ffd_log("vel_step(): Could not diffuse velocity X.", FFD_ERROR);
return flag;
}
flag = diffusion(para, var, VY, 0, v, v0, BINDEX);
if(flag!=0) {
ffd_log("vel_step(): Could not diffuse velocity Y.", FFD_ERROR);
return flag;
}
flag = diffusion(para, var, VZ, 0, w, w0, BINDEX);
if(flag!=0) {
ffd_log("vel_step(): Could not diffuse velocity Z.", FFD_ERROR);
return flag;
}
flag = project(para, var,BINDEX);
if(flag!=0) {
ffd_log("vel_step(): Could not project velocity.", FFD_ERROR);
return flag;
}
if(para->bc->nb_outlet!=0) flag = mass_conservation(para, var,BINDEX);
if(flag!=0) {
ffd_log("vel_step(): Could not conduct mass conservation correction.",
FFD_ERROR);
return flag;
}
return flag;
} // End of vel_step( )
///////////////////////////////////////////////////////////////////////////////
/// Solver for equations
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///\param var_type Variable type
///\param Pointer to variable
///
///\return 0 if not error occurred
///////////////////////////////////////////////////////////////////////////////
int equ_solver(PARA_DATA *para, REAL **var, int var_type, REAL *psi) {
REAL *flagp = var[FLAGP], *flagu = var[FLAGU],
*flagv = var[FLAGV], *flagw = var[FLAGW];
int flag = 0;
switch(var_type) {
case VX:
Gauss_Seidel(para, var, flagu, psi);
break;
case VY:
Gauss_Seidel(para, var, flagv, psi);
break;
case VZ:
Gauss_Seidel(para, var, flagw, psi);
break;
case TEMP:
case IP:
case TRACE:
Gauss_Seidel(para, var, flagp, psi);
break;
default:
sprintf(msg, "equ_solver(): Solver for variable type %d is not defined.",
var_type);
ffd_log(msg, FFD_ERROR);
flag = 1;
break;
}
return flag;
}// end of equ_solver