summaryrefslogtreecommitdiff
path: root/Streams.py
blob: 05598315dc046ce59eaec6206d9b49e24fc0994f (plain)
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import json
import sys
from PyQt5.QtCore import *
from ComponentSelector import compound_selected

class MaterialStream():
    counter = 1
    def __init__(self, compound_names = []):

        self.name = 'MaterialStream' + str(MaterialStream.counter)
        self.type = 'MaterialStream'

        self.compound_names = compound_names
        self.count = MaterialStream.counter
        self.thermo_package ="RaoultsLaw"
        self.mode1 = "P"
        self.mode2 = "T"

        self.mode1_val = ""
        self.mode2_val = ""
        self.OM_data_init = ''
        self.OM_data_eqn = ''
        self.no_of_inputs = 1
        self.no_of_outputs = 1
        self.x = 2500-30
        self.y = 2500-30
        self.pos = QPointF(self.x, self.y)
        MaterialStream.counter+=1
        self.start_dict = {}
        self.eqn_dict = {}
        self.modes_list = ["PT", "PH", "PVF", "TVF", "PS"]
        self.saved = False
        self.mode = self.modes_list[0]
        
        self.variables = {
            'P'     : {'name':'Pressure',         'value':101325,    'unit':'Pa'},         
            'T'     : {'name':'Temperature',      'value':300,       'unit':'K'},

            'xvap'   : {'name':'Vapour Phase Mole Fraction',          'value':None,       'unit':''},
            'H_p[1]'  : {'name':'Mixture Phase Molar Entalpy',        'value':None,       'unit':'J/mol'},
            'S_p[1]'  : {'name':'Mixture Phase Molar Entropy',        'value':None,       'unit':'J/mol.K'},
            'F_p[1]'  : {'name':'Mixture Molar Flow Rate',            'value':100,       'unit':'mol/s'},

            'H_p[2]'  : {'name':'Liquid Phase Molar Entalpy',        'value':None,       'unit':'J/mol'},
            'S_p[2]'  : {'name':'Liquid Phase Molar Entropy',        'value':None,       'unit':'J/mol.K'},
            'F_p[2]'  : {'name':'Liquid Molar Flow Rate',            'value':None,       'unit':'mol/s'},

            'H_p[3]'  : {'name':'Vapour Phase Molar Entalpy',        'value':None,       'unit':'J/mol'},
            'S_p[3]'  : {'name':'Vapour Phase Molar Entropy',        'value':None,       'unit':'J/mol.K'},
            'F_p[3]'  : {'name':'Vapour Molar Flow Rate',            'value':None,       'unit':'mol/s'},

            'x_pc'  : {'name':'Mole Fraction',    'value':[],      'unit':''},
            'xm_pc' : {'name':'Mass Fraction',    'value':None,     'unit':''},
            
            'F_pc'  : {'name':'Mole Flow',        'value':100,      'unit':'mol/s'},
            'Fm_pc' : {'name':'Mass Flow',        'value':None,     'unit':'g/s'},
        }
        self.init_variables()

    def update_compounds(self):
        self.compound_names = compound_selected

    def init_variables(self):
        Nc = len(self.compound_names)
        for i, val in enumerate(self.compound_names):
            self.variables['x_pc[1,'+ str(i+1)+']'] = {'name':val + ' Mixture Mole Fraction', 'value':round(1/Nc,4), 'unit':''}
            self.variables['xm_pc[1,'+ str(i+1)+']'] = {'name':val + ' Mixture Mass Fraction', 'value':None, 'unit':''}
            self.variables['F_pc[1,'+ str(i+1)+']'] = {'name':val + ' Mixture Mole Flow', 'value':None, 'unit':'mol/s'}
            self.variables['Fm_pc[1,'+ str(i+1)+']'] = {'name':val + ' Mixture Mass Flow', 'value':None, 'unit':'g/s'}

            self.variables['x_pc[2,'+ str(i+1)+']'] = {'name':[val + ' Liquid Mole Fraction'], 'value':None, 'unit':''}
            self.variables['xm_pc[2,'+ str(i+1)+']'] = {'name':[val + ' Liquid Mass Fraction'], 'value':None, 'unit':''}
            self.variables['F_pc[2,'+ str(i+1)+']'] = {'name':[val + ' Liquid Mole Flow'], 'value':None, 'unit':'mol/s'}
            self.variables['Fm_pc[2,'+ str(i+1)+']'] = {'name':[val + ' Liquid Mass Flow'], 'value':None, 'unit':'g/s'}

            self.variables['x_pc[3,'+ str(i+1)+']'] = {'name':[val + ' Vapour Mole Fraction'], 'value':None, 'unit':''}
            self.variables['xm_pc[3,'+ str(i+1)+']'] = {'name':[val + ' Vapour Mass Fraction'], 'value':None, 'unit':''}
            self.variables['F_pc[3,'+ str(i+1)+']'] = {'name':[val + ' Vapour Mole Flow'], 'value':None, 'unit':'mol/s'}
            self.variables['Fm_pc[3,'+ str(i+1)+']'] = {'name':[val + ' Vapour Mass Flow'], 'value':None, 'unit':'g/s'}

        for i in self.compound_names:
            self.variables[i] = {'value':''}

    def param_getter_tooltip(self,mode):
        dict = {}

        temp = []
        for i, val in enumerate(self.compound_names):
            try:
                temp.append(self.variables['x_pc[1,' + str(i+1) + ']']['value'])
            except:
                pass
        self.variables['x_pc']['value'] = temp

        if(mode=="PT"):
            self.mode1 = 'P'
            self.mode2 = 'T'
            mode1_n = self.variables['P']['name']
            mode2_n = self.variables['T']['name']
            dict = {mode1_n:str(self.variables['P']['value'])+' '+self.variables['P']['unit'], 
					mode2_n:str(self.variables['T']['value'])+' '+self.variables['T']['unit']}
        elif(mode=="PH"):
            self.mode1 = 'P'
            self.mode2 = 'H_p[1]'
            mode1_n = self.variables['P']['name']
            mode2_n = self.variables['H_p[1]']['name']
				
            dict = {mode1_n:str(self.variables['P']['value'])+' '+self.variables['P']['unit'], 
		    		mode2_n:str(self.variables['H_p[1]']['value'])+' '+self.variables['H_p[1]']['unit']}
        elif(mode=="PVF"):
            self.mode1 = 'P'
            self.mode2 = 'xvap'
            mode1_n = self.variables['P']['name']
            mode2_n = self.variables['xvap']['name']
		
            dict = {mode1_n:str(self.variables['P']['value'])+' '+self.variables['P']['unit'], 
					mode2_n:str(self.variables['xvap']['value'])+' '+self.variables['xvap']['unit']}
        elif(mode=="TVF"):
            self.mode1 = 'T'
            self.mode2 = 'xvap'
            mode1_n = self.variables['T']['name']
            mode2_n = self.variables['xvap']['name']
            dict = {mode1_n:str(self.variables['T']['value'])+' '+self.variables['T']['unit'], 
					mode2_n:str(self.variables['xvap']['value'])+' '+self.variables['xvap']['unit']}
			
        elif(mode=="PS"):
            self.mode1 = 'P'
            self.mode2 = 'S_p[1]'
            mode1_n = self.variables['P']['name']
            mode2_n = self.variables['S_p[1]']['name']

            dict = {mode1_n:str(self.variables['P']['value'])+' '+self.variables['P']['unit'], 
					mode2_n:str(self.variables['S_p[1]']['value'])+' '+self.variables['S_p[1]']['unit']}
				
        dict['Mole Flow'] = str(self.variables['F_p[1]']['value'])+' '+self.variables['F_p[1]']['unit']
        dict[self.variables['x_pc']['name']] = str(self.variables['x_pc']['value'])+' '+self.variables['x_pc']['unit']
        dict['Thermo Package'] = self.thermo_package
        return dict

    def param_getter(self,mode):
        dict = {}

        temp = []
        for i, val in enumerate(self.compound_names):
            try:
                temp.append(self.variables['x_pc[1,' + str(i+1) + ']']['value'])
            except:
                pass
        self.variables['x_pc']['value'] = temp

        if(mode=="PT"):
            self.mode1 = 'P'
            self.mode2 = 'T'
            
            dict = {self.mode1:self.variables['P']['value'], self.mode2:self.variables['T']['value'],
                    "MolFlow":self.variables['F_p[1]']['value'],"x_pc":self.variables['x_pc']['value'],
                    "Thermo Package": self.thermo_package}
            #print('dictionary is :' + str(dict))

        elif(mode=="PH"):
            self.mode1 = 'P'
            self.mode2 = 'H_p[1]'
            dict = {self.mode1:self.variables['P']['value'], self.mode2:self.variables['H_p[1]']['value'],
                    "MolFlow":self.variables['F_p[1]']['value'], "x_pc":self.variables['x_pc']['value'],
                    "Thermo Package": self.thermo_package}
        elif(mode=="PVF"):
            self.mode1 = 'P'
            self.mode2 = 'xvap'
            dict = {self.mode1:self.variables['P']['value'], self.mode2:self.variables['xvap']['value'],
                    "MolFlow":self.variables['F_p[1]']['value'], "x_pc":self.variables['x_pc']['value'],
                    "Thermo Package": self.thermo_package}
        elif(mode=="TVF"):
            self.mode1 = 'T'
            self.mode2 = 'xvap'
            dict = {self.mode1:self.variables['T']['value'], self.mode2:self.variables['xvap']['value'],
                    "MolFlow":self.variables['F_p[1]']['value'], "x_pc":self.variables['x_pc']['value'],
                    "Thermo Package": self.thermo_package}
        elif(mode=="PS"):
            self.mode1 = 'P'
            self.mode2 = 'S_p[1]'
            dict = {self.mode1:self.variables['P']['value'], self.mode2: self.variables['S_p[1]']['value'],
                    "MolFlow":self.variables['F_p[1]']['value'], "x_pc":self.variables['x_pc']['value'],
                    "Thermo Package": self.thermo_package}
        
        return dict

    def param_setter(self,dict):
        self.variables['x_pc']['value'] = dict['x_pc'].split(",")
        #print('xpc is :' + str(self.variables['x_pc']['value']))
        self.thermo_package = dict['Thermo Package']
        self.variables['F_p[1]']['value'] = dict['MolFlow']
        self.variables[self.mode1]['value'] = dict[self.mode1]
        self.variables[self.mode2]['value'] = dict[self.mode2]
        
        for i in range(len(self.compound_names)):
            if self.variables['x_pc']['value'][i]:
                self.variables['x_pc[1,'+str(i+1)+']']['value'] = self.variables['x_pc']['value'][i]
            else:
                self.variables['x_pc[1,'+str(i+1)+']']['value'] = None
            self.variables['xm_pc[1,'+str(i+1)+']']['value'] = self.variables['xm_pc']['value']

            self.variables['F_pc[1,'+str(i+1)+']']['value'] = None
            self.variables['Fm_pc[1,'+str(i+1)+']']['value'] = None
        for i in range(0,len(self.compound_names)):
            self.variables['x_pc[2,'+str(i+1)+']']['value'] = None
            self.variables['xm_pc[2,'+str(i+1)+']']['value'] = None
            self.variables['F_pc[2,'+str(i+1)+']']['value'] = None
            self.variables['Fm_pc[2,'+str(i+1)+']']['value'] = None

            self.variables['x_pc[3,'+str(i+1)+']']['value'] = None
            self.variables['xm_pc[3,'+str(i+1)+']']['value'] = None
            self.variables['F_pc[3,'+str(i+1)+']']['value'] = None
            self.variables['Fm_pc[3,'+str(i+1)+']']['value'] = None 
    
    def set_pos(self,pos):
        self.pos = pos

    def get_min_eqn_values(self):
        x_pclist = []
        for i in range(0,len(self.compound_names)):
            x_pclist.append(self.variables['x_pc[1,'+str(i+1)+']']['value'])
        x_pc = json.dumps(x_pclist)
        x_pc = x_pc.replace('[','{')
        x_pc = x_pc.replace(']','}')
        x_pc = x_pc.replace('"','')
        
        if self.variables[self.mode1]['value']:
            self.eqn_dict[self.mode1] = self.variables[self.mode1]['value']
        if self.variables[self.mode2]['value']:
            self.eqn_dict[self.mode2] = self.variables[self.mode2]['value']
        if self.variables['x_pc']['value']:
            self.eqn_dict['x_pc[1,:]'] = x_pc
        if self.variables['F_pc']['value']:
            self.eqn_dict['F_p[1]'] = self.variables['F_p[1]']['value']

    def get_start_values(self):
        try:
            if self.variables[self.mode1]['value']:
                self.start_dict[self.mode1] = self.variables[self.mode1]['value']
            
            if self.variables[self.mode2]['value']:
                self.start_dict[self.mode2] = self.variables[self.mode2]['value']
            

            if self.variables['x_pc[2,1]']['value'] != None:
                x_pcarr = []
                for i in range(1,4):
                    cmf = []
                    for j in range(1,len(self.compound_names)+1):
                        cmf.append(str(self.variables['x_pc['+str(i)+','+str(j)+']']['value']))
                    x_pcarr.append(cmf)
                x_pcstr = json.dumps(x_pcarr)
                x_pcstr = x_pcstr.replace('[','{')
                x_pcstr = x_pcstr.replace(']','}')
                x_pcstr = x_pcstr.replace('"','')
                self.start_dict['x_pc'] = x_pcstr

            if self.variables['xm_pc[2,1]']['value'] != None:
                xm_pcarr = []
                for i in range(1,4):
                    cmf = []
                    for j in range(1,len(self.compound_names)+1):
                        cmf.append(str(self.variables['xm_pc['+str(i)+','+str(j)+']']['value']))
                    xm_pcarr.append(cmf)
                xm_pcstr = json.dumps(x_pcarr)
                xm_pcstr = xm_pcstr.replace('[','{')
                xm_pcstr = xm_pcstr.replace(']','}')
                xm_pcstr = xm_pcstr.replace('"','')
                self.start_dict['xm_pc'] = xm_pcstr

            if self.variables['Fm_pc[2,1]']['value'] != None:
                Fm_pcarr = []
                for i in range(1,4):
                    cmf = []
                    for j in range(1,len(self.compound_names)+1):
                        cmf.append(str(self.variables['Fm_pc['+str(i)+','+str(j)+']']['value']))
                    Fm_pcarr.append(cmf)
                Fm_pcstr = json.dumps(x_pcarr)
                Fm_pcstr = Fm_pcstr.replace('[','{')
                Fm_pcstr = Fm_pcstr.replace(']','}')
                Fm_pcstr = Fm_pcstr.replace('"','')
                self.start_dict['Fm_pc'] = Fm_pcstr

            if self.variables['F_pc[2,1]']['value'] != None:
                F_pcarr = []
                for i in range(1,4):
                    cmf = []
                    for j in range(1,len(self.compound_names)+1):
                        cmf.append(str(self.variables['F_pc['+str(i)+','+str(j)+']']['value']))
                    F_pcarr.append(cmf)
                F_pcstr = json.dumps(F_pcarr)
                F_pcstr = F_pcstr.replace('[','{')
                F_pcstr = F_pcstr.replace(']','}')
                F_pcstr = F_pcstr.replace('"','')
                self.start_dict['F_pc'] = F_pcstr

            if self.variables['MW_p[2]']['value'] != None:
                MW_pArr = []
                for i in range(1,4):
                    MW_pArr.append(self.variables['MW_p['+str(i)+']']['value'])
                MW_pStr = json.dumps(MW_pArr)
                MW_pStr = MW_pStr.replace('[','{')
                MW_pStr = MW_pStr.replace(']','}')
                MW_pStr = MW_pStr.replace('"','')
                self.start_dict['MW_p'] = MW_pStr

            if self.variables['F_p[2]']['value'] != None:
                F_pArr = []
                for i in range(1,4):
                    F_pArr.append(self.variables['F_p['+str(i)+']']['value'])
                F_pStr = json.dumps(F_pArr)
                F_pStr = F_pStr.replace('[','{')
                F_pStr = F_pStr.replace(']','}')
                F_pStr = F_pStr.replace('"','')
                self.start_dict['F_p'] = F_pStr

            if self.variables['Cp_p[2]']['value'] != None:
                Cp_pArr = []
                for i in range(1,4):
                    Cp_pArr.append(self.variables['Cp_p['+str(i)+']']['value'])
                Cp_pStr = json.dumps(Cp_pArr)
                Cp_pStr = Cp_pStr.replace('[','{')
                Cp_pStr = Cp_pStr.replace(']','}')
                Cp_pStr = Cp_pStr.replace('"','')
                self.start_dict['Cp_p'] = Cp_pStr

            if self.variables['H_p[2]']['value'] != None:
                H_pArr = []
                for i in range(1,4):
                    H_pArr.append(self.variables['H_p['+str(i)+']']['value'])
                H_pStr = json.dumps(H_pArr)
                H_pStr = H_pStr.replace('[','{')
                H_pStr = H_pStr.replace(']','}')
                H_pStr = H_pStr.replace('"','')
                self.start_dict['H_p'] = H_pStr


            if self.variables['S_p[2]']['value'] != None:
                S_pArr = []
                for i in range(1,4):
                    S_pArr.append(self.variables['S_p['+str(i)+']']['value'])
                S_pStr = json.dumps(S_pArr)
                S_pStr = S_pStr.replace('[','{')
                S_pStr = S_pStr.replace(']','}')
                S_pStr = S_pStr.replace('"','')
                self.start_dict['S_p'] = S_pStr

            if self.variables['Fm_p[2]']['value'] != None:
                Fm_pArr = []
                for i in range(1,4):
                    Fm_pArr.append(self.variables['Fm_p['+str(i)+']']['value'])
                Fm_pStr = json.dumps(Fm_pArr)
                Fm_pStr = Fm_pStr.replace('[','{')
                Fm_pStr = Fm_pStr.replace(']','}')
                Fm_pStr = Fm_pStr.replace('"','')
                self.start_dict['Fm_p'] = Fm_pStr

        except Exception as e:
            exc_type, exc_obj, exc_tb = sys.exc_info()
            print(exc_type,exc_tb.tb_lineno)
            print(e)
            print('error')

    def OM_Flowsheet_Initialize(self,addedcomp):
        self.OM_data_init = ''
        self.OM_data_init = self.OM_data_init + ("model ms"+str(self.count)+"\n")
        self.OM_data_init = self.OM_data_init + ("extends Simulator.Streams.MaterialStream;\n" )
        self.OM_data_init = self.OM_data_init + ("extends Simulator.Files.ThermodynamicPackages."+self.thermo_package+";\n")
        self.OM_data_init = self.OM_data_init + ("end ms"+str(self.count)+";\n")
        comp_count = len(addedcomp)
       
        self.OM_data_init = self.OM_data_init + "ms"+str(self.count) +" " + self.name +"(Nc = " + str(comp_count)
        self.OM_data_init = self.OM_data_init + ",C = {"
        C = str(addedcomp).strip('[').strip(']')
        C = C.replace("'","")
        self.OM_data_init = self.OM_data_init + C + "},"
        
        self.OM_data_init = self.OM_data_init[:-1]
        self.OM_data_init = self.OM_data_init + ');\n'        
        return self.OM_data_init

    def OM_Flowsheet_Equation(self,addedcomp,method):
        self.OM_data_eqn = ''
        self.comp_count = len(addedcomp)
        if method == 'Eqn':
            self.eqn_dict = {}
            self.get_min_eqn_values()
        if method == 'SM':
            self.eqn_dict = {}
            self.get_min_eqn_values()
           
        for key,value in self.eqn_dict.items():
            self.OM_data_eqn = self.OM_data_eqn + self.name + '.'+ key + ' = ' + str(value) + ';\n'
        return self.OM_data_eqn