summaryrefslogtreecommitdiff
path: root/OMChem/Flowsheet.py
blob: 4fb0459365652d41afeb0043ca247eef02e4bb33 (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
import os
import csv
from subprocess import Popen, PIPE
import pandas as pd

class Flowsheet():
    def __init__(self):
        self.sim_name = 'Simulator'
        self.sim_method = ''
        self.unit_operations = []
        self.data = []
        self.compounds = []
        self.interface = ''
        self.omc_path = None
        self.root_dir = os.getcwd()		                                    # Chemical-Simulator-GUI
        self.sim_dir_path = os.path.join(self.root_dir, self.sim_name)      # Chemical-Simulator-GUI/Simulator
        self.Flomo_path = os.path.join(self.sim_dir_path,'Flowsheet.mo') 
        self.eqn_mos_path = os.path.join(self.sim_dir_path,'simulateEQN.mos')
        self.sm_mos_path = os.path.join(self.sim_dir_path,'simulateSM.mos')
        self.result_data = []
        self.stdout=None
        self.stderr=None
    
    def get_omc_path(self):
        try:
            self.omhome = os.environ.get('OPENMODELICAHOME')
            if self.omhome is None:
                self.omhome = os.path.split(os.path.split(os.path.realpath(spawn.find_executable("omc")))[0])[0]
            elif os.path.exists('/opt/local/bin/omc'):
                self.omhome = '/opt/local'
            elif os.path.exists('/usr/bin/omc'):
                self.omhome = '/usr'
            return os.path.join(self.omhome, 'bin', 'omc')
        except BaseException:
            print("The OpenModelica compiler is missing in the System path please install it" )
            raise

    def add_unit_operations(self,unitop):
        self.unit_operations.append(unitop)

    def remove_unit_operations(self,unitop):
        self.unit_operations.remove(unitop)

    def add_compound_list(self,C):
        self.compounds = C
    
    def send_for_simulation_Eqn(self):
        self.result_data = []
        self.omc_path = self.get_omc_path()
        print(self.omc_path)

        if self.sim_method == 'Eqn':
            simpath = self.eqn_mos_path
            os.chdir(self.sim_dir_path)
            
            self.process = Popen([self.omc_path, '-s',simpath], stdout=PIPE, stderr=PIPE)
            self.stdout, self.stderr = self.process.communicate()
           
            os.chdir(self.root_dir)
            if ('timeSimulation = 0.0,\n' in self.stdout.decode("utf-8")):
                self.result_data = []
            else:
                csvpath = os.path.join(self.sim_dir_path,'Flowsheet_res.csv')
                with open (csvpath,'r') as resultFile:
                    self.result_data = []
                    csvreader = csv.reader(resultFile,delimiter=',')
                    for row in csvreader:
                        self.result_data.append(row)

    def send_for_simulation_SM(self,unitop):

        self.result_data = []
        self.omc_path = self.get_omc_path()
        os.chdir(self.sim_dir_path)
        self.process = Popen([self.omc_path, '-s',unitop.name,'.mos'], stdout=PIPE, stderr=PIPE)
        stdout, stderr = self.process.communicate()
        # print("############### StdOut ################")
        # print(stdout)
        self.result_data = []
        print('Simulating '+unitop.name+'...')
        csvpath = os.path.join(self.sim_dir_path,unitop.name+'_res.csv')
        with open(csvpath,'r') as resultFile:
            csvreader = csv.reader(resultFile,delimiter=',')
            for row in csvreader:
                self.result_data.append(row)
        self.ext_data()

    def ext_data(self):
        for unit in self.unit_operations:
            if unit[0].type == 'MaterialStream':
                for key, value in unit[0].Prop.items():
                    property_name = unit[0].name + '.' + key
                    if property_name in self.result_data[0]:
                        ind = self.result_data[0].index(property_name)
                        resultval = str(self.result_data[-1][ind])
                        unit[0].Prop[key] = resultval
             
    def simulate_EQN(self):
        self.data = []
        self.sim_method = 'Eqn'
        self.data.append("model Flowsheet\n")
        
        for c in self.compounds:
            ucase = c.title()
            lcase = c.lower()
            self.data.append("parameter database." + ucase +' '+ ucase + "; \n")

        self.data.append("parameter Integer Nc = " + str(len(self.compounds)) + ";\n")
        self.data.append("parameter Simulator.Files.ChemsepDatabase.GeneralProperties C[Nc] = {" +
                         str(self.compounds).strip('[').strip(']').replace("'", "") + "};\n")

        for unitop in self.unit_operations:
            if unitop.type != 'MaterialStream':
                self.data.append(unitop.OM_Flowsheet_Initialize())
            else:
                self.data.append(unitop.OM_Flowsheet_Initialize(self.compounds))
                    
        self.data.append("equation\n")
        
        self.outlist = []
        self.stm = ['MaterialStream','EngStm']
        for unitop in self.unit_operations:
            if unitop.type not in self.stm:
                for j in unitop.output_stms.values():
                    self.outlist.append(j)  
                
        for unitop in self.unit_operations:
            if unitop not in self.outlist:
                if unitop.type == 'MaterialStream':
                    self.data.append(unitop.OM_Flowsheet_Equation(self.compounds,'Eqn'))
                else:
                    self.data.append(unitop.OM_Flowsheet_Equation())
            else:
                pass
        with open(self.Flomo_path, 'w') as txtfile:
            for d in self.data:
                txtfile.write(str(d))
            txtfile.write('end Flowsheet;\n')

        with open(self.eqn_mos_path, 'w') as mosFile:
            mosFile.write('loadModel(Modelica);\n')
            mosFile.write('loadFile(\"Simulator/package.mo\");\n')
            mosFile.write('loadFile(\"database.mo\");\n')
            mosFile.write('loadFile(\"Flowsheet.mo\");\n')
            mosFile.write('simulate(Flowsheet, outputFormat=\"csv\", stopTime=1.0, numberOfIntervals=1);\n')

        print('Initiating Simulation in Equation Oriented Mode')

        self.send_for_simulation_Eqn()

    def simulate_SM(self,ip,op):
        self.sim_method = 'SM'
        self.data = []
        self.result_data = []
        self.unit = []
        self.csvlist = []
       
        for i in ip:
            common = ip[i]
           
            for k,v in op.items():
                if(set(v) & set(common)):
                    if((i in self.unit) and (k in self.unit)):
                        pass
                    elif(i in self.unit):
                        self.unit.insert(self.unit.index(i),k)
                    elif(k in self.unit):
                        self.unit.append(i)
                    else:
                        self.unit.append(k)
                        self.unit.append(i)
                
        for unitop in self.unit:
            os.chdir(self.root_dir)
            self.data = []
            if unitop.type not in ['MaterialStream','EngStm']:
                inpstms = unitop.input_stms
                outstms = unitop.output_stms
                
                try:
                    engstms = unitop.EngStms
                except:
                    engstms = None

                self.data.append("model "+unitop.name.lower()+'\n')
                
                for c in self.compounds:
                    ucase = c.title()
                    lcase = c.lower()
                    self.data.append("parameter Simulator.Files.Chemsep_Database." + ucase +' '+ ucase + "; \n")
                
                self.data.append(unitop.OM_Flowsheet_Initialize())
                
                if type(outstms) is list:
                    for stm in outstms:
                        self.data.append(stm.OM_Flowsheet_Initialize())
                else:
                    self.data.append(outstms.OM_Flowsheet_Initialize())
                
                if engstms:
                    self.data.append(engstms.OM_Flowsheet_Initialize())

                if type(inpstms) is list:
                    for stm in inpstms:
                        self.data.append(stm.OM_Flowsheet_Initialize())
                else:
                    self.data.append(inpstms.OM_Flowsheet_Initialize())
                
                self.data.append('equation\n')
                self.data.append(unitop.OM_Flowsheet_Equation())
                
                if type(inpstms) is list:
                    for stm in inpstms:
                        self.data.append(stm.OM_Flowsheet_Equation())
                else:
                    self.data.append(inpstms.OM_Flowsheet_Equation())

                unitmofile = os.path.join(self.sim_dir_path,unitop.name.lower()+'.mo')

                with open(unitmofile,'w') as unitFile:
                    for d in self.data:
                        unitFile.write(d)
                    unitFile.write('end '+unitop.name.lower()+';\n')
                
                unitmosfile = os.path.join(self.sim_dir_path,unitop.name.lower()+'.mos')
                with open(unitmosfile, 'w') as mosFile:
                    mosFile.write('loadModel(Modelica);\n')
                    mosFile.write("loadFile(\"Simulator\package.mo\");\n")
                
                    mosFile.write("loadFile(\""+unitop.name.lower()+".mo\");\n")
                    mosFile.write("simulate("+unitop.name.lower()+", outputFormat=\"csv\", stopTime=1.0, numberOfIntervals=1);\n")

                print("Initiating simulation in Sequential Modular Mode")
                self.omc_path = self.get_omc_path()
                os.chdir(self.sim_dir_path)
                sim = os.path.join(self.sim_dir_path,unitop.name.lower()+'.mos')
                self.process = Popen([self.omc_path, '-s',sim], stdout=PIPE, stderr=PIPE)
                self.stdout, self.stderr = self.process.communicate()
                os.chdir(self.root_dir)
                
                # print("############### StdOut ################")
                # print(self.stdout)
                # print("############### StdErr ################")
                # print(self.stderr)
                # print('Simulating '+unitop.name.lower()+'...')
                csvpath = os.path.join(self.sim_dir_path,unitop.name.lower()+'_res.csv')
                
                self.csvlist.append(csvpath)
                
                with open(csvpath,'r') as resultFile:
                    csvreader = csv.reader(resultFile,delimiter=',')
                    for row in csvreader:
                        self.result_data.append(row)
                
                os.chdir(self.root_dir)
                if type(inpstms) is list:
                    for stm in inpstms:
                        for key,value in stm.Prop.items():
                            property_name = stm.name + '.' + key
                            if property_name in self.result_data[0]:
                                ind = self.result_data[0].index(property_name)
                                resultval = str(self.result_data[-1][ind])
                                stm.Prop[key] = resultval
                else:
                    for key, value in inpstms.Prop.items():
                            property_name = inpstms.name + '.' + key
                            if property_name in self.result_data[0]:
                                ind = self.result_data[0].index(property_name)
                                resultval = str(self.result_data[-1][ind])
                                inpstms.Prop[key] = resultval

                if type(outstms) is list:
                    for stm in outstms:
                        for key, value in stm.Prop.items():
                            property_name = stm.name + '.' + key
                            if property_name in self.result_data[0]:
                                ind = self.result_data[0].index(property_name)
                                resultval = str(self.result_data[-1][ind])
                                stm.Prop[key] = resultval
                else:
                    for key, value in outstms.Prop.items():
                            property_name = outstms.name + '.' + key
                            if property_name in self.result_data[0]:
                                ind = self.result_data[0].index(property_name)
                                resultval = str(self.result_data[-1][ind])
                                outstms.Prop[key] = resultval
        
        self.dataframes = [pd.read_csv(i) for i in self.csvlist]
        os.chdir(self.sim_dir_path)
        dffinal = pd.concat(self.dataframes,axis=1)
        dffinal.to_csv('FlowsheetSEQ.csv',index=False)
        self.result_data.clear()
        with open(os.path.join(self.sim_dir_path+'/FlowsheetSEQ.csv'),'r') as resultFile:
                    csvreader = csv.reader(resultFile,delimiter=',')
                    for row in csvreader:
                        self.result_data.append(row)