Code: Select all
Exepath=["C:\\Progra~1\\COCO\\cofestand64"];
t=cputime;
tic
[STATUS]=system([Exepath " compressors.fsd"],RETURN_OUTPUT);
toc
t_run=cputime-t
Moderator: jasper
Code: Select all
Exepath=["C:\\Progra~1\\COCO\\cofestand64"];
t=cputime;
tic
[STATUS]=system([Exepath " compressors.fsd"],RETURN_OUTPUT);
toc
t_run=cputime-t
Code: Select all
Loading C:\Users\Octave\compressors.fsd
added stream type default
Restoring flowsheet from file
message: ChemSep/COPP v8.20 (bcf4e2bfbbc6 2020-4-6 01:56) Title= User=FA Date=2020-04-13 Time=19:10:48
added stream type default
message: ChemSep/COPP v8.20 (bcf4e2bfbbc6 2020-4-6 01:56) Title= User=FA Date=2020-04-13 Time=19:10:48
added stream type default
added stream type default
added stream type default
added stream type default
message: ChemSep/COPP v8.20 (bcf4e2bfbbc6 2020-4-6 01:56) Title= User=FA Date=2020-04-13 Time=19:10:48
message: ChemSep/COPP v8.20 (bcf4e2bfbbc6 2020-4-6 01:56) Title= User=FA Date=2020-04-13 Time=19:10:48
message: ChemSep/COPP v8.20 (bcf4e2bfbbc6 2020-4-6 01:56) Title= User=FA Date=2020-04-13 Time=19:10:48
message: ChemSep/COPP v8.20 (bcf4e2bfbbc6 2020-4-6 01:56) Title= User=FA Date=2020-04-13 Time=19:10:48
Starting solve
Solve: solving unit Compressor_1
Solve: solving unit HeaterCooler_2
Solve: solving unit Compressor_3
Solve finished
Saved compressors.fsd
Saved C:\Users\FA\Dropbox\AUT Teaching\Modeling-Simulation\Simulation97\Software\COCO\COCO_Octave\compressors.fsd
SOLVE: OK
Code: Select all
pyenv
Code: Select all
pip install comtypes
Code: Select all
import comtypes.client #requires pip install comtypes
import comtypes.gen
#PLUM Python Link Used by Matlab
#Jasper, 2020
#tell comtypes to load type libs
cofeTlb=('{0D1006C7-6086-4838-89FC-FBDCC0E98780}',1,0) #COFE type lib
cofeTypes=comtypes.client.GetModule(cofeTlb)
coTlb=('{4A5E2E81-C093-11D4-9F1B-0010A4D198C2}',1,1) #CAPE-OPEN v1.1 type lib
coTypes=comtypes.client.GetModule(coTlb)
#print(cofeTypes.__file__)
#variables
doc=None
sourceName=""
targetName=""
sourceParName=""
targetParName=""
#set up the problem
def setup(fileName,sourceUnitName,sourceParameterName,targetUnitName,targetParameterName):
global doc,sourceName,targetName,sourceParName,targetParName
#get and load the doc
doc=comtypes.client.CreateObject('COCO_COFE.Document',interface=cofeTypes.ICOFEDocument)
doc.Import(fileName) #'Import' requires COFE 3.3.0.16 or newer
#save the names
sourceName=sourceUnitName
targetName=targetUnitName
sourceParName=sourceParameterName
targetParName=targetParameterName
def eval(parVal):
global doc,sourceName,targetName,sourceParName,targetParName
#when COFE is running in multi-theaded mode, unit operation interfaces cannot be saved across a call to Solve.
doc.GetUnit(sourceName).QueryInterface(coTypes.ICapeUtilities).Parameters.QueryInterface(coTypes.ICapeCollection).Item(sourceParName).QueryInterface(coTypes.ICapeParameter).value=float(parVal)
doc.Solve()
return doc.GetUnit(targetName).QueryInterface(coTypes.ICapeUtilities).Parameters.QueryInterface(coTypes.ICapeCollection).Item(targetParName).QueryInterface(coTypes.ICapeParameter).value
Code: Select all
>> py.PLUM.setup('compressors.fsd','Compressor_1','Pressure ratio','Compressor_1','Energy demand')
>> py.PLUM.eval(4)
Code: Select all
import comtypes.client
import comtypes.gen
from comtypes import COMError
from comtypes.automation import VARIANT
import array
import os
cofeTlb=('{0D1006C7-6086-4838-89FC-FBDCC0E98780}',1,0)
cofeTypes=comtypes.client.GetModule(cofeTlb)
coTlb=('{4A5E2E81-C093-11D4-9F1B-0010A4D198C2}',1,1)
coTypes=comtypes.client.GetModule(coTlb)
# load the file
filename = 'Flowsheet1.fsd'
filename=os.path.join(os.getcwd(),filename)
doc=comtypes.client.CreateObject('COCO_COFE.Document',interface=cofeTypes.ICOFEDocument)
doc.Import(filename)
# print out all of the unit parameters
count = doc.GetUnit('Column_1').QueryInterface(coTypes.ICapeUtilities). \
Parameters.QueryInterface(coTypes.ICapeCollection).Count()
for i in range(1, count+1):
print(doc.GetUnit('Column_1').QueryInterface(coTypes.ICapeUtilities).Parameters. \
QueryInterface(coTypes.ICapeCollection).Item(i).QueryInterface(coTypes.ICapeParameter). \
QueryInterface(coTypes.ICapeIdentification).ComponentName)
# set inlet stream flowrates (also example of error handling)
try:
doc.GetStream('1').QueryInterface(coTypes.ICapeThermoMaterial).SetOverallProp("flow", "mole", array.array('d', [300, 400, 500]))
except COMError:
print("Error:", self.doc.GetStream('1').QueryInterface(coTypes.ECapeRoot).name)
# set some unit parameters
doc.GetUnit('Column_1').QueryInterface(coTypes.ICapeUtilities).Parameters. \
QueryInterface(coTypes.ICapeCollection).Item("Number of stages").QueryInterface(coTypes.ICapeParameter).value = float(20)
doc.GetUnit('Column_1').QueryInterface(coTypes.ICapeUtilities).Parameters.QueryInterface(coTypes.ICapeCollection).Item(
"Reflux ratio").QueryInterface(coTypes.ICapeParameter).value = float(5)
doc.GetUnit('Column_1').QueryInterface(coTypes.ICapeUtilities).Parameters.QueryInterface(coTypes.ICapeCollection).\
Item("Reboil ratio").QueryInterface(coTypes.ICapeParameter).value = float(5)
doc.Solve()
# get an output parameter
print(f"""distillate flowrates: {doc.GetStream('2').QueryInterface(coTypes.ICapeThermoMaterial).GetOverallProp("flow", "mole")}""")