From 262511f8b7999c311f1852007bbe0f8de2006e89 Mon Sep 17 00:00:00 2001 From: SaurabhAgarwala Date: Tue, 31 Dec 2019 13:23:30 +0530 Subject: Implemented inheritance for Unit Operations --- OMChem/Flowsheet.py | 24 ++++---- OMChem/MatStm.py | 5 +- OMChem/UnitOprns.py | 10 ++-- UnitOperations.py | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++ dockWidget.py | 2 +- helper.py | 23 ++++--- mainApp.py | 2 +- 7 files changed, 202 insertions(+), 34 deletions(-) create mode 100644 UnitOperations.py diff --git a/OMChem/Flowsheet.py b/OMChem/Flowsheet.py index a35f686..59134b9 100644 --- a/OMChem/Flowsheet.py +++ b/OMChem/Flowsheet.py @@ -233,7 +233,7 @@ class Flowsheet(): for unitop in self.UnitOpn: if unitop.type != 'MatStm': - self.data.append(unitop.send_to_flowsheet(self.compounds).OM_Flowsheet_Initialize(self.compounds)) + self.data.append(unitop.send_to_flowsheet().OM_Flowsheet_Initialize(self.compounds)) else: self.data.append(unitop.OM_Flowsheet_Init(self.compounds)) @@ -256,7 +256,7 @@ class Flowsheet(): if unitop.type == 'MatStm': self.data.append(unitop.OM_Flowsheet_Eqn(self.compounds,'Eqn')) else: - self.data.append(unitop.send_to_flowsheet(self.compounds).OM_Flowsheet_Equation(self.compounds)) + self.data.append(unitop.send_to_flowsheet().OM_Flowsheet_Equation(self.compounds)) else: pass with open(self.Flomo_path, 'w') as txtfile: @@ -266,7 +266,7 @@ class Flowsheet(): with open(self.eqn_mos_path, 'w') as mosFile: mosFile.write('loadModel(Modelica);\n') - mosFile.write("loadFile(\"Simulator\package.mo\");\n") + mosFile.write("loadFile(\"Simulator/package.mo\");\n") mosFile.write("loadFile(\"Flowsheet.mo\");\n") mosFile.write("simulate(Flowsheet, outputFormat=\"csv\", stopTime=1.0, numberOfIntervals=1);\n") @@ -330,27 +330,27 @@ class Flowsheet(): self.data.append("parameter Simulator.Files.Chemsep_Database." + ucase +' '+ ucase + "; \n") print("##############compounds added") - self.data.append(unitop.send_to_flowsheet(self.compounds).OM_Flowsheet_Initialize(self.compounds)) + self.data.append(unitop.send_to_flowsheet().OM_Flowsheet_Initialize(self.compounds)) if type(outstms) is list: for stm in outstms: - self.data.append(stm.send_to_flowsheet(self.compounds).OM_Flowsheet_Initialize(self.compounds)) + self.data.append(stm.send_to_flowsheet().OM_Flowsheet_Initialize(self.compounds)) else: - self.data.append(outstms.send_to_flowsheet(self.compounds).OM_Flowsheet_Initialize(self.compounds)) + self.data.append(outstms.send_to_flowsheet().OM_Flowsheet_Initialize(self.compounds)) if engstms: - self.data.append(engstms.send_to_flowsheet(self.compounds).OM_Flowsheet_Initialize(self.compounds)) + self.data.append(engstms.send_to_flowsheet().OM_Flowsheet_Initialize(self.compounds)) if type(inpstms) is list: for stm in inpstms: - self.data.append(stm.send_to_flowsheet(self.compounds).OM_Flowsheet_Initialize(self.compounds)) + self.data.append(stm.send_to_flowsheet().OM_Flowsheet_Initialize(self.compounds)) else: - self.data.append(inpstms.send_to_flowsheet(self.compounds).OM_Flowsheet_Initialize(self.compounds)) + self.data.append(inpstms.send_to_flowsheet().OM_Flowsheet_Initialize(self.compounds)) self.data.append('equation\n') print("##################equation") - self.data.append(unitop.send_to_flowsheet(self.compounds).OM_Flowsheet_Equation(self.compounds)) + self.data.append(unitop.send_to_flowsheet().OM_Flowsheet_Equation(self.compounds)) ''' if type(outstms) is list: for stm in outstms: @@ -363,10 +363,10 @@ class Flowsheet(): if type(inpstms) is list: for stm in inpstms: #stm.GetEquationValues() - self.data.append(stm.send_to_flowsheet(self.compounds).OM_Flowsheet_Equation(self.compounds,'SM')) + self.data.append(stm.send_to_flowsheet().OM_Flowsheet_Equation(self.compounds,'SM')) else: #inpstms.GetEquationValues() - self.data.append(inpstms.send_to_flowsheet(self.compounds).OM_Flowsheet_Equation(self.compounds,'SM')) + self.data.append(inpstms.send_to_flowsheet().OM_Flowsheet_Equation(self.compounds,'SM')) # os.chdir(self.sim_dir_path) unitmofile = os.path.join(self.sim_dir_path,unitop.name.lower()+'.mo') diff --git a/OMChem/MatStm.py b/OMChem/MatStm.py index 69a6845..686231f 100644 --- a/OMChem/MatStm.py +++ b/OMChem/MatStm.py @@ -34,6 +34,7 @@ class MatStm(): MatStm.counter+=1 self.startDict = {} self.eqnDict = {} + self.modesList = ["PT","PH","PVF","TVF","PS"] self.Prop = { @@ -92,9 +93,7 @@ class MatStm(): 'totMasFlo[3]':None } - - def modesList(self): - return ["PT","PH","PVF","TVF","PS"] + def paramgetter(self,mode): dict = {} if(mode=="PT"): diff --git a/OMChem/UnitOprns.py b/OMChem/UnitOprns.py index 90cd546..4cafe0d 100644 --- a/OMChem/UnitOprns.py +++ b/OMChem/UnitOprns.py @@ -85,9 +85,9 @@ class UnitOpr(): - for i in self.parameters: + for k,v in self.parameters.items(): self.OM_data_init += ', ' - self.OM_data_init += i[0] + ' = ' + str(i[1]) + self.OM_data_init += k + ' = ' + str(v) self.OM_data_init += ');\n' else: @@ -96,9 +96,9 @@ class UnitOpr(): comp = comp.replace("'", "") self.OM_data_init += ',comp = {' + comp + '}' - for i in self.parameters: + for k,v in self.parameters.items(): self.OM_data_init += ', ' - self.OM_data_init += i[0] + ' = ' + str(i[1]) + self.OM_data_init += k + ' = ' + str(v) self.OM_data_init += ');\n' @@ -129,7 +129,7 @@ class UnitOpr(): else: self.OM_data_eqn += ('connect(' + self.name + '.outlet,' + self.OutputStms[0].name + '.inlet);\n') - if self.mode: + if self.mode: self.OM_data_eqn += (self.name + '.' + self.mode + '=' + self.modeVal + ';\n') return self.OM_data_eqn diff --git a/UnitOperations.py b/UnitOperations.py new file mode 100644 index 0000000..b9bbb0b --- /dev/null +++ b/UnitOperations.py @@ -0,0 +1,170 @@ +from OMChem.Flowsheet import Flowsheet +from OMChem.MatStm import MatStm +from OMChem.UnitOprns import UnitOpr +from OMChem.EngStm import EngStm +from Graphics import Graphics +from PyQt5.QtCore import * +from PyQt5.QtWidgets import * +from PyQt5.QtGui import QTextDocument ,QTextCursor ,QTextCharFormat ,QFont ,QPixmap +from PyQt5.uic import loadUiType +from PyQt5.QtCore import Qt +from PyQt5.QtWidgets import QGraphicsProxyWidget, QGraphicsObject, QGraphicsEllipseItem ,QGraphicsPixmapItem,QApplication, QGraphicsView, QGraphicsScene, QHBoxLayout, QWidget, QLabel +from PyQt5.QtGui import QBrush ,QTransform ,QMouseEvent +from PyQt5.QtGui import * +import PyQt5.QtCore as QtCore +import PyQt5.QtWidgets as QtWidgets +from component_selector import * + + +class UnitOperation(): + counter = 1 + + def __init__(self): + self.OM_data_eqn = '' + self.OM_data_init = '' + self.InputStms = [] + self.OutputStms = [] + self.name = '' + self.mode = None + self.modeVal = None + self.type = '' + self.no_of_inputs = 1 + self.no_of_outputs = 1 + self.x = 2500-30 + self.y = 2500-30 + self.Prop = {} + self.modeslist = [] + self.parameters = {} + # self.input_parameters = {} + self.extra = None + self.multidict = None + self.ThermoPackReq = False + self.thermoPackage = None + + def paramgetter(self,mode=None): + params = {} + if mode == None: + self.mode = self.modeslist[0] + else: + self.mode = mode + params = self.parameters.copy() + params[self.mode] = None + return params + + def paramsetter(self,params): + print(params) + for k,v in params.items(): + print(k,v) + if k != self.mode: + self.k = v + self.parameters[k] = v + else: + self.modeVal = params[self.mode] + + def connect(self,InputStms,OutputStms): + self.InputStms = InputStms + self.OutputStms = OutputStms + + def add_connection(self,flag,UnitOpr): + if flag==1: + # Input stream if flag is 1 + self.InputStms.append(UnitOpr) + print("INPUT CONNECTION") + else : + print("OUTPUT CONNECTION") + self.OutputStms.append(UnitOpr) + + def send_to_flowsheet(self): + Inst = UnitOpr(name=self.name,counter=type(self).counter,Type='Heater',parameters=self.parameters,mode=self.mode,modeVal=self.modeVal,ThermoPackReq=self.ThermoPackReq,thermoPack=self.thermoPackage,extra=self.extra,multidict=self.multidict,inputs=self.no_of_inputs,outputs=self.no_of_outputs) + Inst.connect(self.InputStms,self.OutputStms) + return Inst + +class HeaterClass(UnitOperation): + + def __init__(self, name='Heater', pressDrop=None, eff='None'): + UnitOperation.__init__(self) + self.name = name + str(type(self).counter) + self.type = 'Heater' + self.no_of_inputs = 1 + self.no_of_outputs = 1 + self.Prop = { + 'pressDrop':None, + 'eff':None, + 'outT':None, + 'tempInc':None, + 'heatAdd':None, + } + self.modesList = ["heatAdd","outT","outVapPhasMolFrac","tempInc","enFlo"] + self.extra = None + self.pressDrop = pressDrop + self.eff = eff + self.parameters = {'pressDrop':self.pressDrop, 'eff':self.eff} + # self.input_parameters = {'pressDrop':self.pressDrop, 'eff':self.eff} + type(self).counter += 1 + +class CoolerClass(UnitOperation): + + def __init__(self, name='Cooler', pressDrop=None, eff='None'): + UnitOperation.__init__(self) + self.name = name + str(type(self).counter) + self.type = 'Cooler' + self.no_of_inputs = 1 + self.no_of_outputs = 1 + self.Prop = { + 'pressDrop':None, + 'eff':None, + 'outT':None, + 'tempDrop':None, + 'heatRem':None, + } + self.modesList = ["heatRem","outT","outVapPhasMolFrac","tempDrop","enFlo"] + self.extra = None + self.pressDrop = pressDrop + self.eff = eff + self.parameters = {'pressDrop':self.pressDrop, 'eff':self.eff} + # self.input_parameters = {'pressDrop':self.pressDrop, 'eff':self.eff} + type(self).counter += 1 + +class AdiaCompClass(UnitOperation): + + def __init__(self, name='AdiaComp', eff='None'): + UnitOperation.__init__(self) + self.name = name + str(type(self).counter) + self.type = 'AdiaComp' + self.no_of_inputs = 1 + self.no_of_outputs = 1 + self.modesList = ["pressInc","outP","reqPow"] + self.extra = ['Adiabatic_Compressor'] + self.ThermoPackReq = True + self.thermoPackage ="Raoults_Law" + self.eff = eff + self.parameters = {'eff':self.eff} + # self.input_parameters = {"eff":self.eff,"thermoPackage":self.thermoPackage} + type(self).counter += 1 + +class AdiaExpClass(UnitOperation): + + def __init__(self, name='AdiaExp', eff='None'): + UnitOperation.__init__(self) + self.name = name + str(type(self).counter) + self.type = 'AdiaExp' + self.no_of_inputs = 1 + self.no_of_outputs = 1 + self.modesList = ["pressDrop","outP","genPow"] + self.extra = ['Adiabatic_Expander'] + self.ThermoPackReq = True + self.thermoPackage ="Raoults_Law" + self.eff = eff + self.parameters = {'eff':self.eff} + # self.input_parameters = {"eff":self.eff,"thermoPackage":self.thermoPackage} + type(self).counter += 1 + + +def helperFunc(type): + if(type=="MatStm"): + return MatStm(CompNames=compound_selected) + else: + return eval(type+"Class")() + + + diff --git a/dockWidget.py b/dockWidget.py index a7325e4..fef3264 100644 --- a/dockWidget.py +++ b/dockWidget.py @@ -28,7 +28,7 @@ class dockWidget(QDockWidget,ui_dialog): self.dict = {} def modes(self): - modesList = self.obj.modesList() + modesList = self.obj.modesList if(modesList): for j in modesList: self.comboBox.addItem(str(j)) diff --git a/helper.py b/helper.py index 2af4cde..d9e9890 100644 --- a/helper.py +++ b/helper.py @@ -35,7 +35,7 @@ class HeaterClass(): self.no_of_outputs = 1 self.x = 2500-30 self.y = 2500-30 - self.addedcomp = None + # self.addedcomp = None self.extra = None self.Prop = { 'pressDrop':None, @@ -46,16 +46,15 @@ class HeaterClass(): } HeaterClass.counter+=1 - def modesList(self): - return ["heatAdd","outT","outVapPhasMolFrac","tempInc","enFlo"] + self.modesList = ["heatAdd","outT","outVapPhasMolFrac","tempInc","enFlo"] def paramgetter(self,mode="heatAdd"): self.mode = mode dict = {"pressDrop":None,"eff":None,self.mode:None} return dict - def add_compounds(self,addedcomp): - self.addedcomp = addedcomp + # def add_compounds(self,addedcomp): + # self.addedcomp = addedcomp @@ -80,10 +79,10 @@ class HeaterClass(): - def send_to_flowsheet(self,addedcomp): - self.addedcomp = addedcomp + def send_to_flowsheet(self): + # self.addedcomp = addedcomp parameters = [['pressDrop',self.pressDrop],['eff',self.eff]] - Inst = UnitOpr(self.name,HeaterClass.counter,'Heater',parameters,self.mode,self.modeVal,False,None,self.extra,None,self.no_of_inputs,self.no_of_outputs) + Inst = UnitOpr(name=self.name,counter=HeaterClass.counter,Type='Heater',parameters=parameters,mode=self.mode,modeVal=self.modeVal,ThermoPackReq=False,thermoPack=None,extra=self.extra,multidict=None,inputs=self.no_of_inputs,outputs=self.no_of_outputs) Inst.connect(self.InputStms,self.OutputStms) return Inst @@ -106,7 +105,7 @@ class CoolerClass(): self.no_of_outputs = 1 self.x = 2500-30 self.y = 2500-30 - self.addedcomp = None + # self.addedcomp = None self.extra = None self.Prop = { 'pressDrop':None, @@ -153,7 +152,7 @@ class CoolerClass(): def send_to_flowsheet(self,addedcomp): self.addedcomp = addedcomp - parameters = [['pressDrop',self.pressDrop],['eff',self.eff]] + parameters = {'pressDrop': self.pressDrop, 'eff': self.eff } Inst = UnitOpr(self.name,CoolerClass.counter,'Cooler',parameters,self.mode,self.modeVal,False,None,self.extra,None,self.no_of_inputs,self.no_of_outputs) Inst.connect(self.InputStms,self.OutputStms) return Inst @@ -180,7 +179,7 @@ class AdiaCompClass(): self.no_of_outputs = 1 self.x = 2500-30 self.y = 2500-30 - self.addedcomp = None + # self.addedcomp = None self.extra = ['Adiabatic_Compressor'] AdiaCompClass.counter+=1 @@ -264,7 +263,7 @@ class AdiaExpClass(): # Input stream if flag is 1 self.InputStms.append(UnitOpr) print("INPUT CONNECTION") - else : + else: print("OUTPUT CONNECTION") self.OutputStms.append(UnitOpr) diff --git a/mainApp.py b/mainApp.py index 04a1d64..e688c7d 100644 --- a/mainApp.py +++ b/mainApp.py @@ -22,7 +22,7 @@ from component_selector import componentSelector from component_selector import * from dockWidget import dockWidget from resDockWidget import resdockWidget -from helper import helperFunc +from UnitOperations import helperFunc import datetime from container import Container from Graphics import Graphics -- cgit