-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththermolib.cpp
174 lines (152 loc) · 3.53 KB
/
thermolib.cpp
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
#include "thermolib.hpp"
#include "defines.hpp"
#include "secant.cpp"
using namespace std;
// affectation :
thermolib & thermolib::operator = ( const thermolib & t ) {
if (dim!=t.dim)
reset(t.dim);
for ( i = 0 ; i < dim ; i++ ) {
Pc[i] = t.Pc[i];
Tc[i] = t.Tc[i];
omega[i] = t.omega[i];
molefrac[i] = t.molefrac[i];
}
return *this;
}
thermolib::~thermolib() {
delete solver;
delete [] Pc;
delete [] Tc;
delete [] omega;
delete [] molefrac;
}
void thermolib::construct ( void ) {
molefrac = new double[dim];
Pc = new double[dim];
Tc = new double[dim];
omega = new double[dim];
// C. Tribes add this for more robustness (variables may be initialized uncorrectly dependent on the execution)
for ( i = 0 ; i < dim ; i++ )
{
Pc[i] = 0.0;
Tc[i] = 0.0;
omega[i] = 0.0;
molefrac[i] = 0.0;
}
solver = new secant<thermolib>();
}
void thermolib::reset(int b)
{
delete [] molefrac;
delete [] Pc;
delete [] Tc;
delete [] omega;
delete solver;
dim = b;
construct();
}
double thermolib::a_mix()
{
if (dim>1)
{
tmp=0;
for (i=0;i<dim;i++)
for (j=0;j<dim;j++)
tmp += molefrac[i]*molefrac[j]*sqrt(a(i)*a(j));
return tmp;
}
else return a(0);
}
double thermolib::b_mix()
{
if (dim>1)
{
tmp=0;
for (i=0;i<dim;i++)
tmp += molefrac[i]*b(i);
return tmp;
}
else return b(0);
}
void thermolib::send(double* pc, double* tc, double* w, double* y)
{
for (i=0;i<dim;i++)
{
Pc[i] = pc[i]*101.325;
Tc[i] = tc[i];
omega[i] = w[i];
molefrac[i] = y[i];
}
}
double thermolib::P()
{
task=0;
pressure = 8.3144*temperature/molevolume;
solver->set(this, pressure, 1.001*pressure);
success=solver->run();
return pressure/101.325;
}
double thermolib::T()
{
task=1;
temperature = pressure*molevolume/8.144;
solver->set(this, temperature, 1.001*temperature);
success=solver->run();
return temperature;
}
double thermolib::v()
{
if (mole>EPS)
{
task=2;
molevolume = 8.3144*temperature/pressure;
solver->set(this, molevolume, 1.001*molevolume);
success=solver->run();
return 0.001*mole*molevolume;
}
else return 0.0;
}
double thermolib::Zv()
{
task=4;
solver->set(this, 1.0, 0.99);
success=solver->run();
return Z;
}
double thermolib::phiV(int i)
{
return exp((Z-1)*B(i)/B() - log(Z-B()) - A()/B()*(2*sqrt(A(i)/A())-B(i)/B())*log(1+B()/Z));
}
double thermolib::phiL(int i)
{
Pr = pressure/Pc[i];
Tr = temperature/Tc[i];
tmp = 2.05135 - 2.10899/Tr - 0.19396*pow(Tr,2) + 0.02282*pow(Tr,3) + (0.08852 - 0.00872*pow(Tr,2))*Pr + (-0.00353 - 0.00203*Tr)*pow(Pr,2) - log10(Pr);
tmp += omega[i]*(-4.23893 + 8.65808*Tr - 1.2206/Tr - 3.15224*pow(Tr,3) - 0.025*(Pr-0.6));
return pow(10, tmp);
}
double thermolib::f(double x)
{
if (task==0)
{
pressure=x;
x= 8.3144*temperature/(molevolume-b_mix()) - a_mix()/(pow(molevolume,2)+b_mix()*molevolume) - x;
}
if (task==1)
{
temperature=x;
x= 8.3144*x/(molevolume-b_mix()) - a_mix()/(pow(molevolume,2)+b_mix()*molevolume) - pressure;
}
if (task==2)
{
molevolume=x;
x= 8.3144*temperature/(x-b_mix()) - a_mix()/(pow(x,2)+b_mix()*x) - pressure;
}
if(task==4)
{
Z=x;
x= (pow(x,3)-pow(x,2)+(A()-B()-pow(B(),2))*x-A()*B());
}
return x;
}