-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathncomp_tests.py
executable file
·356 lines (308 loc) · 13.8 KB
/
ncomp_tests.py
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
#!/usr/bin/env python
# TODO: Add list sorting to ensure right component is tests in phase detection.
import unittest
import numpy
from ncomp import *
#%% TEST FUNCTION Binary NRTL
def g_x_test_func(s, p, k=None, ref='x'):
"""
This is the test function of a binary NRTL Model of the water-butyl-acetate
system from Misos et. al. (2007) using the parameters referenced in the
paper.
x_1^0 = 0.5 is an unstable point.
"""
from math import log, e
t_12 = 3.00498# tau paramters
t_21 = 4.69071
a_12 = 0.391965 # Alpha paramter
a_21 = 0.391965#**(-1.0) # Checked. Should be a_12 in Mitsos, see SvA p 448
for i in range(1, p.m['n']+1):
if s.c[i]['x'] <= 1e-20: # Prevent math errors from zero log call.
s.m['g_mix'] = {}
s.m['g_mix']['t'] = 0.0
s.m['g_mix']['x'] = s.m['g_mix']['t']
s.m['g_mix']['ph min'] = 'x'
return s # should be = 0 as s2['y']*log(s2['y']) = 1*log(1) = 0
s.m['g_mix'] = {}
s.m['g_mix']['t'] = ( s.c[1]['x'] * log(s.c[1]['x'])
+ s.c[2]['x'] * log(s.c[2]['x'])
+ s.c[1]['x'] * (s.c[2]['x'])
* ((t_12 * e**(-a_12 * t_12))
/ (s.c[2]['x'] + s.c[1]['x'] * e**(- a_12 * t_12))
+ (t_21 * e**(-a_21 * t_21))
/ (s.c[1]['x'] + s.c[2]['x'] * e**(- a_21 * t_21))))
s.m['g_mix']['x'] = s.m['g_mix']['t']
s.m['g_mix']['ph min'] = 'x'
return s
def g_x_test_func2(s, p, k=None, ref='x'):
"""
This is the test function of a trenary NRTL Model of the toluene_water_
aniline system from Misos et. al. (2007) using the parameters referenced
in the paper.
x_1^0 = [0.3, 0.2] is an unstable point.
"""
from math import log, e
t_12 = 4.93035 # tau paramters
t_21 = 7.77063
t_13 = 1.59806
t_31 = 0.03509
t_23 = 4.18462
t_32 = 1.27932
a_12 = 0.2485 # Alpha paramters
a_21 = a_12
a_13 = 0.3000
a_31 = a_13
a_23 = 0.3412
a_32 = a_23
for i in range(1, p.m['n']+1):
if s.c[i]['x'] <= 1e-20: # Prevent math errors from zero log call.
s.m['g_mix'] = {}
s.m['g_mix']['t'] = 0.0
s.m['g_mix']['x'] = s.m['g_mix']['t']
s.m['g_mix']['ph min'] = 'x'
return s # should be = 0 as s2['y']*log(s2['y']) = 1*log(1) = 0
s.m['g_mix'] = {}
s.m['g_mix']['t'] = ( s.c[1]['x'] * log(s.c[1]['x'])
+ s.c[2]['x'] * log(s.c[2]['x'])
+ s.c[3]['x'] * log(s.c[3]['x'])
+ s.c[1]['x'] *
(t_21 * e**(-a_21 * t_21) * s.c[2]['x']
+ t_31 * e**(-a_31 * t_31) * s.c[3]['x'])
/ (s.c[1]['x']
+ e**(-a_21 * t_21) * s.c[2]['x']
+ e**(-a_31 * t_31) * s.c[3]['x']
)
+ s.c[2]['x'] *
(t_12 * e**(-a_12 * t_12) * s.c[1]['x']
+ t_32 * e**(-a_32 * t_32) * s.c[3]['x'])
/ (e**(-a_12 * t_12) * s.c[1]['x']
+ s.c[2]['x']
+ e**(-a_32 * t_32) * s.c[3]['x']
)
+ s.c[3]['x'] *
(t_13 * e**(-a_13 * t_13) * s.c[1]['x']
+ t_23 * e**(-a_23 * t_23) * s.c[2]['x'])
/ (e**(-a_13 * t_13) * s.c[1]['x']
+ e**(-a_23 * t_23) * s.c[2]['x']
+ s.c[3]['x']
)
)
s.m['g_mix']['x'] = s.m['g_mix']['t']
s.m['g_mix']['ph min'] = 'x'
return s
class TestNcompFuncsBin(unittest.TestCase):
"""
Test ncomp functions
"""
data = data_handling.ImportData()
data.comps = ['carbon_dioxide', 'ethane']
data.phases = ['x', 'y']
data.eos = 'DWPM'
data.model = 'Adachi-Lu'
data.r = None
data.s = None
data.k_params = None
if len(data.comps) > 1: # multi component simulation.
# Load all pure dictionaries data.c[i]
data.load_pure_data()
# Load VLE and mixture parameter data
data.load()
s, p = n_comp_init(data)
def test_b1(self):
"""
State and data class defs and funcs
"""
self.p.m['r'], self.p.m['s'] = 1.0, 1.0
self.p.m['k'][1][2] = 0.124
self.p.m['k'][2][1] = self.p.m['k'][1][2]
self.s.update_state(self.s, self.p, P=24e5, T=263.1,
X=[[0.25], [0.25]])
self.assertTrue(type(self.s.c) is list)
self.assertTrue(type(self.s.c[1]) is dict)
self.assertTrue(type(self.p.c) is list)
self.assertTrue(type(self.p.c[1]) is dict)
self.assertTrue(type(self.p.m['k'][1][2]) is float )
part = a_mix_partial_k(self.s, self.p, k=1, phase='x')
#self.assertTrue(type(part) is float )
#TODO: Type is float64, find test.
def test_b2(self):
"""
Equil. CO2-Ethane Equilibrium
"""
self.p.m['r'], self.p.m['s'] = 1.0, 1.0
self.p.m['k'][1][2] = 0.124
self.p.m['k'][2][1] = self.p.m['k'][1][2]
Z_0 = numpy.array([0.25])
X_eq, g_eq, phase_eq = phase_equilibrium_calculation(self.s, self.p,
g_mix,
Z_0,
k=None,
P=24e5, T=263.1,
tol=1e-9,
gtol=1e-2,
phase_tol=1e-4,
Print_Results=False,
#Plot_Results=True
Plot_Results=False
)
X_eq = sorted(X_eq)
numpy.testing.assert_allclose([X_eq[0], X_eq[1]],
#[0.28226453, 0.25],
[[0.193647], [0.308676]],
#More accurate, see plot
rtol=1e-03,
atol=1e-03)
def test_b3(self):
"""
Phase sep. CO2-Ethane Equilibrium
"""
self.p.m['r'], self.p.m['s'] = 1.0, 1.0
self.p.m['k'][1][2] = 0.124
self.p.m['k'][2][1] = self.p.m['k'][1][2]
ph_eq, mph_eq, mph_ph = phase_seperation_detection(g_mix, self.s,
self.p,
P=24e5,
#P=28e5,
T=263.1,
n=100,
VLE_only=True,
Plot_Results=False)
numpy.testing.assert_allclose(mph_eq[0],
[numpy.array([ 0.19469983]),
numpy.array([ 0.30628315])],
rtol=9e-03)
def test_b4(self):
"""
Equil. Mitsos et al. (2007) test 1 bin
"""
Z_0 = numpy.array([0.5])
self.p.m['Valid phases'] = ['x']
# noinspection PyTupleAssignmentBalance
X_eq, g_eq, phase_eq = phase_equilibrium_calculation(self.s, self.p,
g_x_test_func,
Z_0,
k=None,
tol=1e-9,
gtol=1e-6,
phase_tol=1e-5,
Print_Results=False,
Plot_Results=False)
#numpy.testing.assert_allclose([s.m['X_I'][0], s.m['X_II'][0]],
def test_b5(self):
"""
Phase sep. Mitsos et al. (2007) test 1 bin with gtol = 1e-4 to find two
plane with 2 equilibrium solutions only.
"""
if 0:
self.p.m['Valid phases'] = ['x']
ph_eq, mph_eq, mph_ph = phase_seperation_detection(g_x_test_func,
self.s, self.p,
P=101e3, T=300.0,
n=100,
#gtol=1e-4,
gtol=1e-3,
LLE_only=True,
Plot_Results=False)
print(ph_eq['x'])
numpy.testing.assert_allclose(ph_eq['x'],
[[numpy.array([ 0.58775493]),
numpy.array([ 0.0045433])],
[numpy.array([ 0.5824251]),
numpy.array([ 0.93304455])
]],
rtol=9e-02)
def test_b6(self):
"""
Phase sep. Mitsos et al. (2007) test 1 bin with gtol = 1e-2 to find
three equilibrium solutions on the plane.
"""
if 0:
self.p.m['Valid phases'] = ['x']
ph_eq, mph_eq, mph_ph = phase_seperation_detection(g_x_test_func,
self.s, self.p,
P=101e3, T=300.0,
n=100,
gtol=1e-2,
LLE_only=True,
Plot_Results=False)
numpy.testing.assert_allclose(ph_eq['x'],
numpy.array([[[ 0.591987],
[ 0.004557],
[ 0.934769]]])
, rtol=9e-02)
class TestNcompFuncsTern(unittest.TestCase):
"""
Test ncomp functions
"""
data = data_handling.ImportData()
data.comps = ['acetone', 'benzene', 'water']
data.phases = ['x']
data.eos = 'DWPM'
data.model = 'Adachi-Lu'
print(f'data.model = {data.model}')
print(f'data.eos = {data.eos }')
print('='*10)
data.r = None
data.s = None
data.k_params = None
if len(data.comps) > 1: # multi component simulation.
# Load all pure dictionaries data.c[i]
data.load_pure_data()
# Load VLE and mixture parameter data
data.load()
s, p = n_comp_init(data)
p.m['r'], p.m['s'] = 1.0, 1.0
def test_t1(self):
"""
State and data class definition
"""
self.assertTrue(type(self.s.c) is list)
self.assertTrue(type(self.s.c[1]) is dict)
self.assertTrue(type(self.p.c) is list)
self.assertTrue(type(self.p.c[1]) is dict)
self.assertTrue(type(self.p.m['k'][1][2]) is float )
def test_t2(self):
"""
Equil. Mitsos et al. (2007) test 2 tern
"""
Z_0 = numpy.array([0.30, 0.20])
#Z_0 = numpy.array([0.35, 0.2])
#Z_0 = numpy.array([0.6, 0.1])
X_eq, g_eq, phase_eq = phase_equilibrium_calculation(self.s, self.p,
g_x_test_func2,
Z_0,
k=None,
P=101e3, T=300.0,
#tol=1e-15,
tol=1e-5,
phase_tol=1e-8,
n = 100 + 100 * 3,
Print_Results=False,
Plot_Results=True)
# Order phases correctly:
if X_eq[0][0] < 0.1:
Ans_X_I = [9.0e-05, 9.99990000e-01]
Ans_X_II = [0.346864, 0.075812]
else:
Ans_X_I = [0.346864, 0.075812]
Ans_X_II = [9.0e-05, 9.99990000e-01]
# Note: There is some decimal rounding in the [9.0e-05, 9.9999e-01] ans
numpy.testing.assert_allclose(X_eq[0],
Ans_X_I,
rtol=1e-02)
numpy.testing.assert_allclose(X_eq[1],
Ans_X_II,
rtol=1e-02)
def ncomp_suite():
"""
Gather all the pure tests from this module in a test suite.
"""
TestNcomp = unittest.TestSuite()
ncomp_suite1 = unittest.makeSuite(TestNcompFuncsBin)
ncomp_suite2 = unittest.makeSuite(TestNcompFuncsTern)
TestNcomp.addTest(ncomp_suite1)
TestNcomp.addTest(ncomp_suite2)
return TestNcomp
if __name__ == '__main__':
TestNcomp = ncomp_suite()
unittest.TextTestRunner(verbosity=2).run(TestNcomp)