-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathphasefraction.m
227 lines (171 loc) · 4.67 KB
/
phasefraction.m
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
%% Calculate phase mole fraction by solving Rachford-Rice equation
%
% phasefrac : Phase fraction
% comp : Phase composition
% K : K values or equilibrium ratios
% comp_overall : Overall composition
% tol : Iteration tolerance
% maxiter : Maximum number of iterations
function [phasefrac, comp] = phasefraction(K, comp_overall, tol, maxiter)
% Calculate the initial estimate of phase mole fraction.
phasefrac = phasefracest(K, comp_overall);
if isempty(phasefrac)
exit;
end
fun = @(x) minfun(K, comp_overall, x);
grad = @(x) gradfun(K, comp_overall, x);
hessian = @(x) hessianfun(K, comp_overall, x);
%maxstepsize = @(x, dx) maxstepsizefun(K, comp_overall, x, dx);
phasefrac = newton(fun, grad, hessian, phasefrac, tol, maxiter);
comp = calccomp(K, comp_overall, phasefrac);
end
%% Calculate variables.
function t = calct(K, phasefrac)
% K : equilibrium constant
% phasefrac: phase mole fraction
nphase = size(phasefrac,1); % the number of phases.
ncomp = size(K,1); % the number of components.
Ktemp = ones(ncomp, nphase) - K;
t = ones(ncomp,1) - Ktemp*phasefrac;
end
function comp = calccomp(K, comp_overall, phasefrac)
ncomp = size(K, 1);
nphase = size(phasefrac, 1) + 1;
t = calct(K, phasefrac);
comp = zeros(ncomp, nphase);
for i = 1:ncomp
comp(i, nphase) = comp_overall(i)/t(i);
for j = 1:nphase-1
comp(i, j) = K(i, j)*comp(i, nphase);
end
end
end
%% Objective function to be minimized
% Based on Okuno et al., (2010)
function f = minfun(K, comp_overall, phasefrac)
ncomp = size(comp_overall, 1);
t = calct(K, phasefrac);
f = 0;
for i = 1:ncomp
f = f - comp_overall(i)*log(abs(t(i)));
end
end
function g = gradfun(K, comp_overall, phasefrac)
nphase = size(phasefrac, 1); % the number of phases - 1.
ncomp = size(K,1); % the number of components.
t = calct(K, phasefrac);
temp = zeros(ncomp, 1);
for i = 1:ncomp
temp(i) = comp_overall(i)/t(i);
end
g = (ones(ncomp, nphase) - K)'*temp;
end
function H = hessianfun(K, z, beta)
nphase = size(beta,1); % the number of phase - 1.
ncomp = size(K,1); % the number of components
t = calct(K,beta);
% Calculate Hessian matrix.
H = zeros(nphase, nphase);
for j = 1:nphase
for k = 1:nphase
for i = 1:ncomp
H(j, k) = H(j, k) + (1 - K(i, j))*(1 - K(i, k))*z(i)/(t(i))^2;
end
end
end
end
%% Phase mole fraction Calculation
function phasefrac = phasefracest(K, comp_overall)
% Calculate a and b.
a = calca(K);
b = calcb(K, comp_overall);
% Calculate feasible region.
fr = feasibleregion(a, b);
% center of min feasible region
nphase = size(fr,1);
ncomp = size(fr,2);
phasefrac = zeros(nphase,1);
for i = 1:nphase
for j = 1:ncomp
phasefrac(i) = phasefrac(i) + fr(i,j);
end
end
phasefrac = phasefrac/ncomp;
end
%% Calculate a feasible region for the minimizing function
% Based on Okuno et al. (2010)
function a = calca(K)
ncomp = size(K,1); % the number of components.
nphase = size(K,2); % (the number of phases) - 1.
a = ones(ncomp, nphase) - K;
end
function b = calcb(K, comp_overall)
nphase = size(K,2); % the number of phases - 1.
ncomp = size(K,1); % the number of components.
b = zeros(ncomp, 1);
for i = 1:ncomp
x1 = 1 - comp_overall(i);
x2 = 1 - K(i,1)*comp_overall(i);
for j = 2:nphase
temp = 1 - K(i,j)*comp_overall(i);
if (x2 > temp)
x2 = temp;
end
end
if x1 < x2
b(i) = x1;
else
b(i) = x2;
end
end
end
function fr = feasibleregion(a, b)
ncomp = size(b, 1);
nphase = size(a, 2);
index = combnk(1:ncomp, nphase);
fr = [];
for i = 1:size(index,1)
at = [];
bt = [];
for j = 1:nphase
at = cat(1, at, a(index(i,j),:));
bt = cat(1, bt, b(index(i,j)));
end
phasefrac = at\bt;
abeta = a*phasefrac;
flag = 1;
for j = 1:ncomp
if abeta(j) > b(j);
comp = 1;
for k = 1:nphase
if index(i,k) == j
comp = comp*0;
end
end
if comp ~= 0
flag = flag*0;
end
end
end
if flag == 1
fr = cat(2, fr, phasefrac);
end
end
end
%% Calculate the maximum step size for Newton iteration
function maxstepsize = maxstepsizefun(K, comp_overall, phasefrac, d)
ncomp = size(K, 1); % the number of components
a = calca(K);
b = calcb(K, comp_overall);
temp1 = b - a*phasefrac;
temp2 = a*d;
maxstepsize = 1;
for i = 1:ncomp
if temp2(i) > 0
boundary = temp1(i)/temp2(i);
if boundary < maxstepsize
maxstepsize = boundary;
end
end
end
end