-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMixture_IdealGas(2.0).c
480 lines (386 loc) · 16.1 KB
/
Mixture_IdealGas(2.0).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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
*sccs id: @(#)real_ideal.c 1.10 Copyright 1900/11/09 ANSYS, Inc.
*/
/*
* Copyright 1988-1998 ANSYS, Inc.
* All Rights Reserved
*
* This is unpublished proprietary source code of ANSYS, Inc.
* It is protected by U.S. copyright law as an unpublished work
* and is furnished pursuant to a written license agreement. It
* is considered by ANSYS, Inc. to be confidential and may not be
* used, copied, or disclosed to others except in accordance with
* the terms and conditions of the license agreement.
*/
/*
* Copyright for the forked version "Arash Jalil-Khabbazi"
*/
/*
* Warning!!! Including udf.h is for getting definitions for
* ANSYS FLUENT constructs such as Domain. You must
* NOT reference any ANSYS FLUENT globals directly from
* within this module nor link this against any ANSYS
* FLUENT libs, doing so will cause dependencies on a
* specific ANSYS FLUENT binary such as fl551.exe and
* thus won't be version independent.
*/
/* ALL VARIABLES ARE IN SI UNITS.
*
* pressure [Pa]
* temperature [K]
* molar_weight [kg/kmol]
* specific_volume [m3/kg]
* molar_volume [m3/kmol]
* universal_gas_constant (molar_gas_constant) [J/kmol-K]
* mass_specific_gas_constant (J/kg-K)
* enthalpy [J/kg]
* entropy [J/kg-K]
* specific_heat [J/kg-K]
*
*/
#include "udf.h"
#include "stdio.h"
#include "ctype.h"
#include "stdarg.h"
#if RP_DOUBLE
#define SMLL 1.e-20
#else
#define SMLL 1.e-10
#endif
#define NSPECIE_NAME 80
#define RGASU UNIVERSAL_GAS_CONSTANT /* 8314.34 SI units: J/kmol/K */
#define PI 3.141592654
/* Here input the number of species in the mixture */
/* THIS IS A USER INPUT */
#define n_specs 2
/* OPTIONAL REFERENCE (OFFSET) VALUES FOR ENTHALPY AND ENTROPY */
#define h_ref 0.0
#define s_ref 0.0
static int (*usersMessage)(const char *,...);
static void (*usersError)(const char *,...);
/* REFERENCE STATE */
#define P_ref 101325.0
#define T_ref 298.15
//static double P_ref, T_ref;
static char gas[n_specs][NSPECIE_NAME];
/* static property parameters */
static double cp[5][n_specs]; /* specific heat polynomial coefficients */
static double mw[n_specs]; /* molecular weights */
static double tcrit[n_specs]; /* critical temperature */
static double pcrit[n_specs]; /* critical pressure */
static double vcrit[n_specs]; /* critical specific volume */
/* Static variables associated with ideal gas model */
static double rgas[n_specs], cp_inT_ref[n_specs], h_ideal_ref[n_specs];
void Mw();
void Cp_Parameters();
void Tcrit();
void Pcrit();
void Vcrit();
double cp_ideal_gas(double temp, int i);
double viscosity(double temp, int i);
double thermal_conductivity(double temp, int i);
DEFINE_ON_DEMAND(I_do_nothing)
{
/*
This is a dummy function
must be included to allow for the use of the
ANSYS FLUENT UDF compilation utility
*/
}
/*******************************************************************/
/* Mixture Functions */
/* These are the only functions called from ANSYS FLUENT Code */
/*******************************************************************/
void MIXTURE_Setup(Domain *domain, cxboolean vapor_phase, char *specielist,
int (*messagefunc)(const char *format,...),
void (*errorfunc)(const char *format,...))
{
/* This function will be called from ANSYS FLUENT after the
UDF library has been loaded.
User must enter the number of species in the mixture
and the name of the individual species.
*/
int i, j;
usersMessage = messagefunc;
usersError = errorfunc;
/*
P_ref = ABS_P(RP_Get_Real("reference-pressure"), op_pres);
T_ref = 298.15;
Message0("\n MIXTURE_Setup: ideal gas equation of State \n");
Message0("\n MIXTURE_Setup: reference-temperature is %f \n", T_ref);
if (P_ref == 0.0){
Message0("\n MIXTURE_Setup: reference-pressure was not set by user \n");
Message0("\n MIXTURE_Setup: setting reference-pressure to 101325 Pa \n");
P_ref = 101325.0;
}
*/
/*====================================================*/
/*========= User Input Section ======================*/
/*====================================================*/
/*
Define Species name.
DO NOT use space for naming species
*/
(void)strcpy(gas[0], "H2");
(void)strcpy(gas[1], "CH4");
/*====================================================*/
/*========= End Of User Input Section ===============*/
/*====================================================*/
Message0("\n MIXTURE_Setup: RealGas mixture initialization \n");
Message0("\n MIXTURE_Setup: Number of Species = %d \n", n_specs);
for (i=0; i<n_specs; ++i)
{
Message0("\n MIXTURE_Setup: Species[%d] = %s \n", i, gas[i]);
}
/*
concatenate species name into one string and send back to fluent
*/
strcat(specielist, gas[0]);
for (i=1; i<n_specs; ++i)
{
strcat(specielist, " ");
strcat(specielist, gas[i]);
}
/* initialize */
Mw();
Cp_Parameters();
Tcrit();
Pcrit();
Vcrit();
for (i=0; i<n_specs; ++i)
{
rgas[i] = RGASU/mw[i];
h_ideal_ref[i] = T_ref*(cp[0][i] + T_ref*(0.5*cp[1][i] + T_ref*(1./3.*cp[2][i] + T_ref*(0.25*cp[3][i] + T_ref*0.2*cp[4][i]))));
cp_inT_ref[i] = cp[0][i]*log(T_ref)+T_ref*(cp[1][i]+T_ref*(0.5*cp[2][i]+T_ref*(1./3.*cp[3][i]+0.25*cp[4][i]*T_ref)));
}
}
/*------------------------------------------------------------*/
/* FUNCTION: MIXTURE_mw */
/* Returns mixture molar mass */
/*------------------------------------------------------------*/
double MIXTURE_mw(double yi[])
{
double sum = 0.0;
int i;
for (i=0; i<n_specs; ++i)
sum += yi[i]/mw[i];
return 1.0/sum; /* (kg/kmol) */
}
/*------------------------------------------------------------*/
/* FUNCTION: MIXTURE_rgas */
/* Returns mass-specific mixture gas constant */
/*------------------------------------------------------------*/
double MIXTURE_rgas(double yi[])
{
double rgas_m = 0.0;
int i;
for (i=0; i<n_specs; ++i)
rgas_m += yi[i]*rgas[i];
return rgas_m; /* (J/kg/K) */
}
/*------------------------------------------------------------*/
/* FUNCTION: MIXTURE_density */
/* Returns density given T and P */
/*------------------------------------------------------------*/
double MIXTURE_density(cell_t cell, Thread *thread, cxboolean vapor_phase, double temp, double P, double yi[])
{
return P/(MIXTURE_rgas(yi)*temp); /* (kg/m^3) */
}
/*------------------------------------------------------------*/
/* FUNCTION: MIXTURE_specific_heat */
/* Returns specific heat given T and rho */
/*------------------------------------------------------------*/
double MIXTURE_specific_heat(cell_t cell, Thread *thread, double temp, double density, double P, double yi[])
{
double cp = 0.0;
int i;
for (i=0; i<n_specs; ++i)
cp += yi[i]*(cp[0][i] + temp*(cp[1][i] + temp*(cp[2][i] + temp*(cp[3][i] + temp*cp[4][i]))));
return cp; /* (J/kg/K) */
}
/*------------------------------------------------------------*/
/* FUNCTION: MIXTURE_enthalpy */
/* Returns specific enthalpy given T and rho */
/*------------------------------------------------------------*/
double MIXTURE_enthalpy(cell_t cell, Thread *thread, double temp, double density, double P, double yi[])
{
double h = 0.0;
int i;
for (i=0; i<n_specs; ++i)
h += yi[i]*(temp*(cp[0][i] + temp*(0.5*cp[1][i] + temp*(1./3.*cp[2][i] + temp*(0.25*cp[3][i] + temp*0.2*cp[4][i]))))
- h_ideal_ref[i]);
return h + h_ref; /* (J/kg) */
/*------------------------------------------------------------*/
/* FUNCTION: MIXTURE_cp_integral */
/* Returns entropy's Cp_integral part given T and rho */
/*------------------------------------------------------------*/
double MIXTURE_cp_integral(double temp, double yi[])
{
double cp_int = 0.0;
int i;
for (i=0; i<n_specs; ++i)
cp_int += yi[i]*(cp[0][i]*log(temp)+temp*(cp[1][i]+temp*(0.5*cp[2][i]+temp*(1./3.*cp[3][i]+0.25*cp[4][i]*temp)))
- cp_inT_ref[i]);
return cp_int; /* (J/kg-K) */
}
/*------------------------------------------------------------*/
/* FUNCTION: MIXTURE_entropy */
/* Returns entropy given T and rho */
/*------------------------------------------------------------*/
double MIXTURE_entropy(cell_t cell, Thread *thread, double temp, double density, double P, double yi[])
{
return (s_ref + MIXTURE_cp_integral(temp, yi) + MIXTURE_rgas(yi)*log(P_ref/P)); /* (J/kg/K) */
}
/*------------------------------------------------------------*/
/* FUNCTION: MIXTURE_speed_of_sound */
/* Returns s.o.s given T and rho */
/*------------------------------------------------------------*/
double MIXTURE_speed_of_sound(cell_t cell, Thread *thread, double temp, double density, double P, double yi[])
{
double cp = MIXTURE_specific_heat(cell, thread, temp, density, P, yi);
double rgas = MIXTURE_rgas(yi);
return sqrt(rgas*temp*cp/(cp-rgas)); /* m/s */
}
/*------------------------------------------------------------------------*/
/* FUNCTION: MIXTURE_rho_t */
/* derivative of denisty w.r.t. temperature at constant pressure (drdT|p) */
/*------------------------------------------------------------------------*/
double MIXTURE_rho_t(cell_t cell, Thread *thread, double temp, double density, double P, double yi[])
{
return -density/temp; /* (kg/m^3/K) */
}
/*------------------------------------------------------------------------*/
/* FUNCTION: MIXTURE_rho_p */
/* derivative of denisty w.r.t. pressure at constant temperature (drdp|T) */
/*------------------------------------------------------------------------*/
double MIXTURE_rho_p(cell_t cell, Thread *thread, double temp, double density, double P, double yi[])
{
double rgas = MIXTURE_rgas(yi);
return 1./(rgas*temp); /* (kg/m^3/Pa) */
}
/*-------------------------------------------------------------------------*/
/* FUNCTION: MIXTURE_enthalpy_t */
/* derivative of enthalpy w.r.t. temperature at constant pressure (dhdT|p) */
/*-------------------------------------------------------------------------*/
double MIXTURE_enthalpy_t(cell_t cell, Thread *thread, double Temp, double density, double P, double yi[])
{
return MIXTURE_specific_heat(cell, thread, Temp, density, P, yi); /* J/(kg.K) */
}
/*-------------------------------------------------------------------------*/
/* FUNCTION: MIXTURE_enthalpy_p */
/* derivative of enthalpy w.r.t. pressure at constant temperature (dhdp|T) */
/*-------------------------------------------------------------------------*/
double MIXTURE_enthalpy_p(cell_t cell, Thread *thread, double Temp, double density, double P, double yi[])
{
/* general form dh/dp|T = (1/rho)*[ 1 + (T/rho)*drho/dT|p] */
/* but for ideal gas dh/dp = 0 */
return 0.0; /* J/(kg.Pa) */
}
/*------------------------------------------------------------*/
/* FUNCTION: MIXTURE_viscosity */
/*------------------------------------------------------------*/
double MIXTURE_viscosity(cell_t cell, Thread *thread, double temp, double density, double P, double yi[])
{
double mu = 0.;
int i;
for (i=0; i<n_specs; ++i)
mu += yi[i]*viscosity(temp, i);
return mu; /* kg/(m.s) */
}
/*------------------------------------------------------------*/
/* FUNCTION: MIXTURE_thermal_conductivity */
/*------------------------------------------------------------*/
double MIXTURE_thermal_conductivity(cell_t cell, Thread *thread, double temp, double density, double P, double yi[])
{
double kt = 0.;
int i;
for (i=0; i<n_specs; ++i)
kt += yi[i]*thermal_conductivity(temp, i);
return kt; /* W/(m.K) */
}
/*******************************************************************/
/* Species Property Definitions */
/*******************************************************************/
void Mw() /* molecular weight */
{ /* kg/kmol */
mw[0] = 2.01594; /*H2*/
mw[1] = 16.04303; /*CH4*/
}
void Pcrit() /* critical pressure */
{ /* Pa */
pcrit[0] = 1.293e6; /*H2*/
pcrit[1] = 4.599e6; /*CH4*/
}
void Tcrit() /* critical temperature */
{ /* K */
tcrit[0] = 32.98; /*H2*/
tcrit[1] = 190.56; /*CH4*/
}
void Vcrit() /* critical specific volume */
{ /* m3/kg */
vcrit[0] = 0.031846; /*H2*/
vcrit[1] = 0.006146; /*CH4*/
}
void Cp_Parameters() /* coefficients of specific heat polynomials */
{ /* J/kg/K */
cp[0][0] = 13602.45 ; /*H2*/
cp[1][0] = 3.402317;
cp[2][0] = -0.003358423;
cp[3][0] = -3.907953e-07;
cp[4][0] = 1.705345e-09;
cp[0][1] = 403.5847 ; /*CH4*/
cp[1][1] = 9.057335;
cp[2][1] = -0.01442509;
cp[3][1] = 1.580519e-05;
cp[4][1] = -6.343051e-09 ;
}
/*------------------------------------------------------------*/
/* FUNCTION: cp_ideal_gas */
/* Returns ideal gas specific heat given T */
/*------------------------------------------------------------*/
double cp_ideal_gas(double temp, int i)
{
double cpi = (cp[0][i]+temp*(cp[1][i]+temp*(cp[2][i]+temp*(cp[3][i] +temp*cp[4][i]))));
return cpi;
}
/*******************************************************************/
/* FUNCTION: viscosity */
/*******************************************************************/
double viscosity(double temp, int i)
{
double mu, tr, tc, pcatm;
tr = temp/tcrit[i];
tc = tcrit[i];
pcatm = pcrit[i]/101325.;
mu = 6.3e-7 * sqrt(mw[i])*pow(pcatm,0.6666)/pow(tc,0.16666) *(pow(tr,1.5)/(tr+0.8));
return mu; /* (kg/m/s) */
}
/*******************************************************************/
/* FUNCTION: thermal_conductivity */
/*******************************************************************/
double thermal_conductivity(double temp, int i)
{
double cp, mu;
cp = cp_ideal_gas(temp, i);
mu = viscosity(temp, i);
return (cp + 1.25*rgas[i])*mu; /* W/m/K */
}
/*******************************************************************/
/* Mixture Functions Structure */
/*******************************************************************/
UDF_EXPORT RGAS_Functions RealGasFunctionList =
{
MIXTURE_Setup, /* initialize */
MIXTURE_density, /* density */
MIXTURE_enthalpy, /* sensible enthalpy */
MIXTURE_entropy, /* entropy */
MIXTURE_specific_heat, /* specific_heat */
MIXTURE_mw, /* molecular_weight */
MIXTURE_speed_of_sound, /* speed_of_sound */
MIXTURE_viscosity, /* viscosity */
MIXTURE_thermal_conductivity, /* thermal_conductivity */
MIXTURE_rho_t, /* drho/dT |const p */
MIXTURE_rho_p, /* drho/dp |const T */
MIXTURE_enthalpy_t, /* dh/dT |const p */
MIXTURE_enthalpy_p, /* dh/dp |const T */
};