-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
287 lines (229 loc) · 10.8 KB
/
main.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
#!/usr/bin/env python
"""
python2 main.py -c acetone benzene water -p x y
python2 -i main.py -c acetone benzene water -p x y -T 273.15 -P 101e3 -r 1.0 -s 1.0
python2 main.py -c acetone benzene water -p x y -T 273.15 -P 101e3 -r 1.0 -s 1.0
-c carbon_dioxide ethane -p x y -P 24e5 -T 263.1 -r 1.0 -s 1.0 -plti -kij 0.124 0.124
"""
import data_handling
import ncomp
import pure
import argparse
if __name__ == '__main__':
# Return basic data
data = data_handling.ImportData()
parser = argparse.ArgumentParser('Test argument parsing')
# Positional arguments
parser.add_argument('-c', '--compounds', nargs='+', required=True,
help='compounds to simulate for example'
'acetone benzene water')
parser.add_argument('-p', '--phases', nargs='+', required=True,
help='List of valid phases in equilibrium'
'ex. for VLE use x y')
# Optional arguments
parser.add_argument('-e', '--eos', default='DWPM',
choices=['DWPM'],
help='Equation of State / Mixture rule')
parser.add_argument('-r', type=float,
help='Force value of r')
parser.add_argument('-s', type=float,
help='Force value of s')
parser.add_argument('-m', '--model', nargs=1,
default="Adachi-Lu",
choices=['Adachi-Lu', 'Soave'],
help='Actvity coefficient model')
#TODO Add k_ij array input and processing
parser.add_argument('-kij', '--k_params', nargs='+', type=float,
help='Force value of interaction parameters')
parser.add_argument('-T', '--temperature', type=float,
help='Temperature for point simulation')
parser.add_argument('-P', '--pressure', type=float,
help='Pressure for point simulation')
parser.add_argument('-z', type=float, nargs="+",
help='Composition for point simulation')
#
# parser.add_argument('-g_f', '--g_x_func' type=float, nargs="+",
# help='')
parser.add_argument('-vle', '--vle_only',
action="store_true",
help='If specified then phase seperation of same '
'volume root instability will be ignored.')
parser.add_argument('-lle', '--lle_only',
action="store_true",
help='Calculate only phase seperation of same volume '
'root')
# Plots
parser.add_argument('-pltg', '--plot_gibbs',
action="store_true",
help='plot gibbs energy (binary and ternary systems '
'only)')
parser.add_argument('-pltit', '--plot_isotherms', nargs='+', type=float,
help='plot isotherm phase envelope (binary and '
'ternary systems only)')
parser.add_argument('-pltib', '--plot_isobars', nargs='+', type=float,
help='plot isobar phase envelope (binary and '
'ternary systems only)')
parser.add_argument('-pltp', '--plot_pure',
action="store_true",
help='Plot the pure vapour pressure model')
parser.add_argument('-epsp', '--plot_epsilon',
action="store_true",
help='Plot the errors of the objective function')
# Optimise
parser.add_argument('-opt', '--optimise',
action="store_true",
help='Optimise the DWPM parameters')
# Save
parser.add_argument('-save', nargs=1, type=bool,
default=False,
help='Save the results of the multi-component'
'optimisation')
parser.add_argument('-save_pure', nargs=1, type=bool,
default=False,
help='Save the results of the pure component'
'optimisation')
parser.add_argument('-force_pure_update', nargs=1, type=bool,
default=False,
help=' force a new optimisation for the m'
'parameter for the selected Model, to be '
'used if new vapour data is added')
args = parser.parse_args()
data.run_options(args)
if len(data.comps) == 1: # pure component simulation.
# Load pure data
data.load_pure_data() # Using data.comps
# Find all specified outputs
s, p = pure.pure_sim(data, i=0)
# plot output
if data.plot_pure:
from plot import PsatPlots
PP = PsatPlots(data.comps[0], data.model[0])
if len(PP.DBr) == 0:
P_sat_store, T_sat_store = PP.psat_range(s, p)
PP.plot_Psat(data.comps[0], p)
if len(data.comps) > 1: # multi component simulation.
from ncomp import phase_equilibrium_calculation as pec
from ncomp import phase_seperation_detection as psd
from ncomp import equilibrium_range as er
# TODO: Select EOS model from input:
from ncomp import g_mix as g_x_func
# Load all pure dictionaries data.c[i]
data.load_pure_data()
# Load VLE and mixture parameter data
data.load()
s, p = ncomp.n_comp_init(data)
# Parameter optimisation
if data.optimise:
from param import TopShiftParam
from ncomp import g_mix
import numpy
# Local+global routine
s.update_state(s, p, P=24e5, T=263.1, X=[0.0],
Force_Update=True)
#print('r = {}'.format(p.m['r']))
#print('s = {}'.format(p.m['s']))
TSP = TopShiftParam(p,
rs=True,
kij=True,
#rskij=True
)
tsp_args = (s, p, g_mix)
tsp_args = (s, p, g_mix, False, True, 6)
Z_0 = [p.m['k'][1][2], p.m['k'][2][1]]
Z_0 = [p.m['r'], p.m['s'], p.m['k'][1][2], p.m['k'][2][1]]
### Dev
if False:
TSP.tsp_objective_function(Z_0, s, p, g_x_func,
dp_pec=False)
###
#if False:
TSP.optimise(s, p, g_x_func, Z_0,
method_d='tgo',
#method_d='L-BFGS-B',
#method_eq='L-BFGS-B',
method_eq='SLSQP',
bounds=[
(-20.0, 20.0),
(-20.0, 20.0),
(-0.9, 0.99),
(-0.9, 0.99),
#(-4.0, 5.0)
])
if False:
#TODO: Move this to a unittest
s.update_state(s, p, P=24e5, T=263.1, X=[0.0], Force_Update=True)
TSP = TopShiftParam(p)
X_I = numpy.array([0.1939063]) # 'x'
X_II = numpy.array([0.308988493]) # 'y'
# X_I = numpy.array([0.1939063, 0.5]) # 'x'
# X_II = numpy.array([0.308988493, 0.1]) # 'y'
params = [1.0, 1.0] # r and s
TSP.vdw_dwpm_params(params, p)
X_D = TSP.d_points(5, X_I, X_II)
X_o = TSP.o_points(5, X_I, X_II)
print('X_o = {}'.format(X_o))
plane, Lambda_sol_est, G_sol = TSP.d_plane(g_mix, s, p, X_I, X_II)
f_dual_gap = TSP.dual_gap(g_mix, plane, X_D, s, p)
epsilon_d = TSP.dual_gap_error_sum(f_dual_gap)
print('epsilon_d = {}'.format(epsilon_d))
epsilon_e = TSP.norm_eta_sum(X_D, Lambda_sol_est, X_I, X_II, G_sol)
epsilon_x = TSP.data_error([X_I, X_II], ['x', 'y'],
X_D, g_mix, s, p)
print('epsilon_x = {}'.format(epsilon_x))
Z_0 = TSP.d_Z_0(X_I, X_II)
print('Z_0 = {}'.format(Z_0))
if data.plot_epsilon:
from param import TopShiftParam
from ncomp import g_mix
import numpy
s.update_state(s, p, P=24e5, T=263.1, X=[0.0],
Force_Update=True)
# Plot
tsp_args = (s, p, g_mix, False)
TSP = TopShiftParam(p, rs=True, kij=False)
#TSP = TopShiftParam(p, rs=False, kij=True)
tsp_args = (s, p, g_mix, False, True, 5)
bounds = [(-5.0, 4.0), (-5.0, 4.0)]
#bounds = [(-2.0, 2.0), (-2.0, 2.0)]
#bounds = [(-0.15, 0.99), (-0.15, 0.99)]
x_r = 40
plot_kwargs = TSP.obj_func_range(TSP.tsp_objective_function,
bounds, x_r, tsp_args,
comps=data.comps)
TSP.plot_ep(plot_kwargs)
# Simulate specifications
if data.P is not None and data.T is not None and data.Z_0 is None:
ph_eq, mph_eq, mph_ph = \
psd(g_x_func, s, p, data.P, data.T, n=100,
#n_dual=1,
LLE_only=data.lle_only,
VLE_only=data.vle_only,
Plot_Results=True) # Tested/working
print('ph_eq = {}'.format(ph_eq))
print('mph_eq = {}'.format(mph_eq))
print('mph_ph = {}'.format(mph_ph))
if data.P is not None and data.T is not None and data.Z_0 is not None:
pec(s, p, g_x_func, data.Z_0, k=None, P=data.P, T=data.T,
tol=1e-9, Print_Results=True, Plot_Results=data.plot_gibbs)
# Not tested
if data.plot_gibbs: #TODO: No need for this if we plot in pec?
pass # Need to add tie lines
#options = plot.plot_options
#plot.plot_g_mix(s, p, options, figno=None)
#plot.plot_g_mix(s, p, g_x_func)#, figno=None)
if data.plot_isotherms is not None:
from ncomp import g_mix as g_x_func
from plot import IsoDetection
iso = IsoDetection(components=data.comps)
import time
start = time.time()
# Use res = 30 for database standard
iso.plot_iso(s, p, g_x_func, res=40, n=3000, T=data.plot_isotherms,
VLE_only=True, n_dual=300, Plot_Results=False)
print("="*90)
print('Done in {}'.format(time.time() - start))
print("="*90)
from matplotlib import pyplot as plot
#plot.show()
from matplotlib import pyplot as plot
plot.show()