-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata_handling.py
256 lines (215 loc) · 8.42 KB
/
data_handling.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Data read/write.
ex.
>>> dataset1.load_VLE(['acetone','water'])
>>> dataset2.load_VLE(['benzene','cyclohexane'])
"""
#import ConfigParser
import configparser
import os
import numpy
import logging
#%% Load data classes
class ImportData:
"""Load data of coupounds specified with Data.load_pure_data()"""
def __init__(self):
self.c = [] # creates a new empty list for components
# Read configuration file
# TODO: Add argparse optionals to read from.
config = configparser.ConfigParser()
configfile = 'config.cfg'
if os.path.exists(configfile):
config.read('config.cfg')
self.datadir = os.path.expanduser(config.get('paths', 'datadir'))
else:
message = ("Cannot find config file {0}. "
"Try copying sample_config.cfg to {0}.").format(
configfile)
raise EnvironmentError(message)
def run_options(self, args):
"""
Load data vars into data class
Parameters
----------
args : object
argparse arguments given when running main.py
"""
self.comps = args.compounds
self.phases = args.phases
# Otionals
self.eos = args.eos
self.model = args.model
self.r = args.r
self.s = args.s
self.T = args.temperature
self.P = args.pressure
self.Z_0 = args.z
self.k_params = args.k_params
self.lle_only = args.lle_only
self.vle_only = args.vle_only
# Plots
self.plot_isotherms = args.plot_isotherms
self.plot_isobars = args.plot_isobars
self.plot_gibbs = args.plot_gibbs
self.plot_pure = args.plot_pure
self.plot_epsilon = args.plot_epsilon
# Saves
self.optimise = args.optimise
self.save_results = args.save
self.save_pure = args.save_pure
self.force_pure_update = args.force_pure_update
def load_pure_data(self):
from csvDict import load_csv_as_dict
"""
Returns the pure component VLE data for specified components.
"""
for i, comp in enumerate(self.comps):
ldstr = os.path.join(self.datadir
,'Pure_Component','{}.csv'.format(comp))
try:
Data = load_csv_as_dict(ldstr)
Data['name'] = [comp,]
Data['model'] = self.model
self.c.append(Data)
except IOError: # Raise error if not found
raise IOError('Data for specified component ' \
+'"{}" not found'.format(comp))
def load(self):
from csvDict import load_csv_as_dict
# Find file name path for specified components
filename = '_'.join(self.comps)
ldstr = os.path.join(self.datadir, 'nComp_E', filename + '.csv')
try: # Load data from file path
Data = load_csv_as_dict(ldstr)
self.VLE = Data
except IOError: # Raise error if not found
raise IOError('Phase data for '
'system "{}" not found'.format(filename))
def parameter_build(Data):
"""
Move data container to parameter output dictionary and find the
critical Van der Waals contants if not defined.
Parameters
----------
Data : Dictionary containing data loaded from the stored .csv file.
"""
if len(Data): #TODO: Merge single and multi comp param defs here
pass
p = {'T' : Data['T (K)'],
'P' : Data['P (Pa)'],
'T_c' : Data['T_c (K)'][0],
'P_c' : Data['P_c (Pa)'][0],
'V_c' : Data['V_c (m3 mol-1)'][0],
'Z_c' : Data['Z_c'][0],
'R' : Data['R (m3 Pa K-1 mol-1)'][0],
'w' : Data['w'][0],
#'a_c' : Data['a_c (Pa m6 mol-2)'][0],
#'b_c' : Data['b_c (m3 mol-1)'][0],
'vT' : Data['virialT'],
'vB' : Data['virialB'],
'Model': Data['model'],
'name': Data['name']
}
try:
p['a_c'] = Data['a_c (Pa m6 mol-2)'][0]
p['b_c'] = Data['b_c (m3 mol-1)'][0]
except IndexError:
p['a_c'] = ''
p['b_c'] = ''
if Data['model'] == 'Adachi-Lu': # Find model params if not defined
p['m'] = Data['m (Adachi-Lu)'][0]
elif Data['model'] == 'Soave':
p['m'] = Data['m (Soave)'][0]
if p['a_c'] == '' or p['b_c'] == '':
p['b_c'] = p['R']*p['T_c']/(8*p['P_c'])
p['a_c'] = 27*(p['R']**2)*(p['T_c']**2)/(64.0*p['P_c'])
else:
pass
for key, value in p.items(): # Filter out '' values
if not (value.__class__ == float or value.__class__ == numpy.float64):
if not key is 'Model':
p[key] = list(filter(lambda a: a != '', value))
return p
class MixParameters:
"""
Store mixture and pure parameters in the same class.
Parameters
----------
Data : Dictionary containing data loaded from the stored .csv file.
"""
def __init__(self):
self.c = [] # creates a new empty list for components
self.c.append('nan') # Define an empty set in index 0
def mixture_parameters(self, data_VLE, data):
"""Mixture model parameters"""
import copy
M = {'T' : data_VLE['T (K)'], # Temperature Pressure data
'P' : data_VLE['P (Pa)'],
'n' : len(data.comps),
'phases' : len(data.phases),
'Model' : data.eos,
'Valid phases' : copy.copy(data.phases),
# Save a set of data for LLE type equilibrium points
'Data phases' : copy.copy(data.phases)
}
# Define phases
for i in range(len(data.phases)):
M[data.phases[i]] = ['nan']
# Define equilibria for each component in phase
for j in range(1, M['n'] + 1):
for i in range(len(data.phases)):
M[data.phases[i]].append(data_VLE[data.phases[i] +
'{}'.format(j)])
# NOTE: This routine will change component strings the for the
# equilibrium of each phase into a list simple list for each
# phase ex. data_VLE['x1'], data_VLE['x2'] becomes: M['x'][1],
# M['x'][2]
# Eliminate equilibrium phases from list of valid phases
for ph in M['Valid phases']:
if ('I' in ph) or ('V' in ph):
M['Valid phases'].remove(ph)
# Define model paramters
# Empty lists for model interaction paramters
M['k'] = [['nan'] for _ in range(M['n'] + 1)]
if data.eos == 'DWPM':
# Find the interaction paramters between and put them into
# component lists (ex. data_VLE['k12'] --> M['k'][1][2])
ind = 0
for j in range(1, M['n'] + 1):
for i in range(1, M['n'] + 1):
# Define empty list
M['k'][j].append('nan')
if i != j: # Define interaction paramter
if data.k_params is None: # Use .csv data
M['k'][j][i] = data_VLE['k{J}{I}'.format(J=j,
I=i)][0]
else: # Use argparse data in ordered seq.
M['k'][j][i] = data.k_params[ind]
ind += 1
if data.r is None:
M['r'] = data_VLE['r']
else:
M['r'] = data.r
if data.s is None:
M['s'] = data_VLE['s']
else:
M['s'] = data.s
else:
logging.warning('Specified model not implemented')
for key, value in M.items(): # Filter out '' values
if not value.__class__ == float:
# if key != 'x' and key != 'y' and key != 'k' and key != 'phases':
try:
M[key] = list(filter(lambda a: a != '', value))
except TypeError:
pass
self.m = M
def parameters(self, data):
# (Recieving data[i] dict, not full data class)
p = parameter_build(data) # should be data_handling.parameter_build?
#p['name'] = Data['name']
self.c.append(p)
if __name__ == '__main__':
pass