1 |
/* |
2 |
Initial model of a simple mixture, to get the procedure right. This |
3 |
is in preparation for a general algorithm to find mixing conditions |
4 |
in the ideal-mixture case. |
5 |
|
6 |
Started May 28, 2015 |
7 |
*/ |
8 |
|
9 |
#include "../helmholtz.h" |
10 |
#include "../fluids.h" |
11 |
#include "../fprops.h" |
12 |
#include "../refstate.h" |
13 |
#include "../sat.h" |
14 |
|
15 |
#include <stdio.h> |
16 |
#include <assert.h> |
17 |
#include <math.h> |
18 |
|
19 |
/* TODO: maybe add nice colors from `color.h', later. |
20 |
Too much of a distraction to figure it out now. */ |
21 |
|
22 |
extern const EosData eos_rpp_nitrogen; |
23 |
extern const EosData eos_rpp_ammonia; |
24 |
extern const EosData eos_rpp_carbon_dioxide; |
25 |
extern const EosData eos_rpp_methane; |
26 |
extern const EosData eos_rpp_water; |
27 |
|
28 |
/** |
29 |
Calculate overall mass density of a mixture of components, given mass |
30 |
density of components. |
31 |
|
32 |
@param nPure number of pure components |
33 |
@param x mass fractions of components |
34 |
@param rhos mass density of each component |
35 |
@return mass density of mixture |
36 |
*/ |
37 |
double mixture_rho(unsigned nPure, double *x, double *rhos){ |
38 |
double rho_mix; |
39 |
|
40 |
int i; |
41 |
for(i=0,rho_mix=0.0;i<nPure;i++){ |
42 |
rho_mix += x[i]*rhos[i]; /* mixture mass density is simply sum of each mass density weighted by the mass fraction */ |
43 |
} |
44 |
return rho_mix; |
45 |
} |
46 |
|
47 |
/** |
48 |
Get data for the fluids I want available for the mixture: |
49 |
nitrogen |
50 |
ammonia |
51 |
carbon dioxide |
52 |
methane |
53 |
water |
54 |
|
55 |
Then, establish mixture conditions, and find individual densities |
56 |
*/ |
57 |
int main(void){ |
58 |
enum FluidNames {N2,NH3,CO2,/*CH4,H2O,*/NFLUIDS}; |
59 |
ReferenceState ref = {FPROPS_REF_REF0}; |
60 |
|
61 |
const EosData *IdealEos[NFLUIDS]={ |
62 |
&eos_rpp_nitrogen |
63 |
, &eos_rpp_ammonia |
64 |
, &eos_rpp_carbon_dioxide |
65 |
// , &eos_rpp_methane |
66 |
// , &eos_rpp_water |
67 |
}; |
68 |
|
69 |
char *FluidNames[NFLUIDS]={ |
70 |
"nitrogen", "ammonia", "carbondioxide"/* , "methane", "water" */ |
71 |
}; |
72 |
|
73 |
PureFluid *Ideals[NFLUIDS]; |
74 |
PureFluid *Helms[NFLUIDS]; |
75 |
|
76 |
FpropsError err = FPROPS_NO_ERROR; |
77 |
int i; |
78 |
for(i=N2;i<NFLUIDS;i++){ |
79 |
Ideals[i] = ideal_prepare(IdealEos[i],&ref); |
80 |
Helms[i] = fprops_fluid(FluidNames[i],"helmholtz",NULL); |
81 |
} |
82 |
|
83 |
/* Mixing conditions, in temperature and pressure */ |
84 |
double T=300; /* K */ |
85 |
double P=1e5; /* Pa */ |
86 |
double x[NFLUIDS] = {0.5, 0.3, 0.2}; /* mass fraction */ |
87 |
/* Density is found as ideal-gas density for now */ |
88 |
double rho[NFLUIDS]; |
89 |
for(i=N2;i<NFLUIDS;i++){ |
90 |
rho[i] = P / Ideals[i]->data->R / T; |
91 |
printf("\n\t%s%s is : %.4f kg/m3", "The mass density of ", FluidNames[i], rho[i]); |
92 |
} |
93 |
|
94 |
/* mixture properties */ |
95 |
double rho_mx = mixture_rho(NFLUIDS, x, rho); |
96 |
double p_mx=0.0, /* pressure and enthalpy, calculated from mixture mass densities */ |
97 |
h_mx=0.0; |
98 |
|
99 |
/* Calculate pressures and enthalpies with the Ideal model (ideal-gas mixture) */ |
100 |
double p_i, h_i; /* these will hold individual pressures and enthalpies */ |
101 |
printf("\n The ideal-gas case:"); |
102 |
for(i=N2;i<NFLUIDS;i++){ |
103 |
p_i = ideal_p(T, rho[i], Ideals[i]->data, &err); |
104 |
h_i = ideal_h(T, rho[i], Ideals[i]->data, &err); |
105 |
printf("\n\t%s %s\n\t\t%s %g Pa;\n\t\t%s %g J/kg.\n", |
106 |
"For the substance", FluidNames[i], |
107 |
"the pressure is :", p_i, |
108 |
"the enthalpy is :", h_i); |
109 |
p_mx += x[i] * p_i; |
110 |
h_mx += x[i] * h_i; |
111 |
} |
112 |
printf("\n\t%s\t\t:\t %f kg/m3\n\t%s\t:\t %g Pa\n\t%s\t\t:\t %g J/kg\n", |
113 |
"The density of the mixture is", rho_mx, |
114 |
"The average pressure of the mixture is", p_mx, |
115 |
"The enthalpy of the mixture is", h_mx); |
116 |
|
117 |
/* Calculate pressures and enthalpies from Helmholtz model */ |
118 |
printf("\n The non-ideal-gas case:"); |
119 |
p_mx=0.0; /* reset mixture pressure and enthalpy to zero */ |
120 |
h_mx=0.0; |
121 |
for(i=N2;i<NFLUIDS;i++){ |
122 |
p_i = fprops_p((FluidState){T,rho[i],Helms[i]}, &err); |
123 |
h_i = fprops_h((FluidState){T,rho[i],Helms[i]}, &err); |
124 |
printf("\n\t%s %s\n\t\t%s %g Pa;\n\t\t%s %g J/kg.\n", |
125 |
"For the substance", FluidNames[i], |
126 |
"the pressure is :", p_i, |
127 |
"the enthalpy is :", h_i); |
128 |
p_mx += x[i] * p_i; |
129 |
h_mx += x[i] * h_i; |
130 |
} |
131 |
printf("\n\t%s\t\t:\t %f kg/m3\n\t%s\t:\t %g Pa\n\t%s\t\t:\t %g J/kg\n", |
132 |
"The density of the mixture is", rho_mx, |
133 |
"The average pressure of the mixture is", p_mx, |
134 |
"The enthalpy of the mixture is", h_mx); |
135 |
|
136 |
/** |
137 |
Now I drop the assumption that densities can be calculated from the |
138 |
ideal-gas model, and use a root-finding method to find the densities |
139 |
that each component must have to be at the pressure P. |
140 |
|
141 |
That is, since ideal-solution mixing is isobaric (constant pressure), the |
142 |
density of each component is found by assuming it is at pressure P before |
143 |
mixing, and solving for the density that will satisfy that condition. |
144 |
*/ |
145 |
return 0; |
146 |
} |