Connecting to COCO from MATLAB
Moderator: jasper
Connecting to COCO from MATLAB
Here is an email exchange I had with Jasper regarding the subject topic. It will be useful to someone who is treading the same path as mine.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Hello Jasper,
I wish to:
1. automate some COCO operations through MATLAB (eg - automatically calculate a flowsheet by cycling through a matrix of values for certain variables) and
2. Access certain parameters (eg: flow, composition, etc) from within MATLAB.
I have been through your site and forums in great detail. I have tried to emulate instructions here https://www.cocosimulator.org/index_hel ... eation.htm but I seem to hit a wall after inputting this command: a=actxserver('COCO_COFE.Document') in MATLAB. I cannot seem to access any flowsheet in the directory. See image.
Please help me understand what to do next.
Thanks very much,
Samvith
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Dear Samvith,
COCO's type lib is not baked into COCO, Matlab does not like that:
https://es.mathworks.com/matlabcentral/ ... com-object
Even if this were so, the CAPE-OPEN interfaces are exposed only as early binding, something that Matlab does not like either. You can still access from Matlab the functions described here:
https://www.cocosimulator.org/index_hel ... rfaces.htm
and see sub topics. But you would have to do so via invoke:
a=actxserver('COCO_COFE.Document')
a.invoke("ShowMainConfiguration")
All of this is not of much use, as without access to the CAPE-OPEN interfaces there is no way to get to thermo property calculations, to stream content, to unit operation parameters etc, as far as I can tell. This is because Matlab does not support early binding on COM objects, and it is hard, if not impossible, to implement CAPE-OPEN interfaces via late binding (IDispatch) due to some restrictions in CAPE-OPEN itself (technical background, see here: https://www.cocosimulator.org/downloads ... t_2014.pdf, slide 10 an on)
Here's possible work-arounds:
1) wait until Matlab supports early binding (probably not an option)
2) wait until COFE 4, which might introduce more elegant early binding interfaces to circumvent this (probably not an option)
3) use an external scripting tool that does support early binding (which excludes vbscript) and invoke that from Matlab, or
4) do it all from Matlab, but via a different route. This is the approach I would look into:
Flowsheet fsd files are zip files. In there you will find an XML file called flowsheet.xml. You can parse (and change and rewrite) the xml from matlab: https://es.mathworks.com/help/matlab/ref/xmlread.html. Do do so you must unzip (and re-zip) the file, https://es.mathworks.com/help/matlab/ref/zip.html. Now all you are short on is to run the file: https://www.cocosimulator.org/index_hel ... estand.htm.
Hope that helps.
Best wishes,
Jasper
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Hello Jasper,
I wish to:
1. automate some COCO operations through MATLAB (eg - automatically calculate a flowsheet by cycling through a matrix of values for certain variables) and
2. Access certain parameters (eg: flow, composition, etc) from within MATLAB.
I have been through your site and forums in great detail. I have tried to emulate instructions here https://www.cocosimulator.org/index_hel ... eation.htm but I seem to hit a wall after inputting this command: a=actxserver('COCO_COFE.Document') in MATLAB. I cannot seem to access any flowsheet in the directory. See image.
Please help me understand what to do next.
Thanks very much,
Samvith
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Dear Samvith,
COCO's type lib is not baked into COCO, Matlab does not like that:
https://es.mathworks.com/matlabcentral/ ... com-object
Even if this were so, the CAPE-OPEN interfaces are exposed only as early binding, something that Matlab does not like either. You can still access from Matlab the functions described here:
https://www.cocosimulator.org/index_hel ... rfaces.htm
and see sub topics. But you would have to do so via invoke:
a=actxserver('COCO_COFE.Document')
a.invoke("ShowMainConfiguration")
All of this is not of much use, as without access to the CAPE-OPEN interfaces there is no way to get to thermo property calculations, to stream content, to unit operation parameters etc, as far as I can tell. This is because Matlab does not support early binding on COM objects, and it is hard, if not impossible, to implement CAPE-OPEN interfaces via late binding (IDispatch) due to some restrictions in CAPE-OPEN itself (technical background, see here: https://www.cocosimulator.org/downloads ... t_2014.pdf, slide 10 an on)
Here's possible work-arounds:
1) wait until Matlab supports early binding (probably not an option)
2) wait until COFE 4, which might introduce more elegant early binding interfaces to circumvent this (probably not an option)
3) use an external scripting tool that does support early binding (which excludes vbscript) and invoke that from Matlab, or
4) do it all from Matlab, but via a different route. This is the approach I would look into:
Flowsheet fsd files are zip files. In there you will find an XML file called flowsheet.xml. You can parse (and change and rewrite) the xml from matlab: https://es.mathworks.com/help/matlab/ref/xmlread.html. Do do so you must unzip (and re-zip) the file, https://es.mathworks.com/help/matlab/ref/zip.html. Now all you are short on is to run the file: https://www.cocosimulator.org/index_hel ... estand.htm.
Hope that helps.
Best wishes,
Jasper
- Attachments
-
- Screenshot
- error.PNG (9.3 KiB) Viewed 26033 times
Re: Connecting to COCO from MATLAB
Did it work out?
Re: Connecting to COCO from MATLAB
Yes. Thanks again Jasper
Re: Connecting to COCO from MATLAB
Glad to hear it.
Re: Connecting to COCO from MATLAB
Thanks @jasper and @samvithvr for sharing this. So, as I understand, it is not even possible to pull (get) information from COCO directly into MATLAB using ActiveX(?).
Also, I followed the xml approach proposed by @jasper and built a sample case where Octave (or MATLAB) is used to optimize a simple process (the optimization doesn't make much sense physically as the compressor work is being maximized
)
I thought it'd be good to share it in the forum. The only major problem here (specially for optimization work) is the computational speed, which is hugely affected by frequent zipping/unzipping of the xml. I'm not sure if there is a better way to code this. Or maybe instead of working with xmls, putting Excel between Octave and COCO would result in faster data exchanges; or a different way to zip/upzip(?) Any comments would be appreciated!
Also, I followed the xml approach proposed by @jasper and built a sample case where Octave (or MATLAB) is used to optimize a simple process (the optimization doesn't make much sense physically as the compressor work is being maximized

I thought it'd be good to share it in the forum. The only major problem here (specially for optimization work) is the computational speed, which is hugely affected by frequent zipping/unzipping of the xml. I'm not sure if there is a better way to code this. Or maybe instead of working with xmls, putting Excel between Octave and COCO would result in faster data exchanges; or a different way to zip/upzip(?) Any comments would be appreciated!
- Attachments
-
- COCO_Octave_or_MATLAB.zip
- Link between Octave (or MATLAB) and COCO through xml
- (20.08 KiB) Downloaded 1281 times
Re: Connecting to COCO from MATLAB
FWIW, if you type 'open unzip' in Matlab you see the matlab version of unzip. There is nothing there about sys_cmd_unzip, I think this is archaic.
I suppose pkg comes from here? https://es.mathworks.com/matlabcentral/ ... kg-install
I do not think it is the zip/unzip actions that take long - running COFEStand on such a small file has a bit of overhead. Try this?
Create COFE.py
From Matlab
I suppose pkg comes from here? https://es.mathworks.com/matlabcentral/ ... kg-install
I do not think it is the zip/unzip actions that take long - running COFEStand on such a small file has a bit of overhead. Try this?
Create COFE.py
Code: Select all
import zipfile
import os
import xml.etree.ElementTree
filename="" #path to fsd
cofestand="" #path to cofestand64.exe
xmlFile=None #keep parsed XML file in memory, no need to re-parse twice
compressorProps=None
pyFolder=os.path.dirname(os.path.abspath(__file__)) #this folder
def initialize(f,s):
global filename,cofestand
filename=f
cofestand=s
def solveForPressureRatio(rat):
global filename,cofestand,xmlFile,compressorProps,pyFolder,runCmd
#parse
if xmlFile is None:
#unzip
zip_ref = zipfile.ZipFile(filename, 'r')
zip_ref.extractall(pyFolder)
zip_ref.close()
#parse
xmlFile=xml.etree.ElementTree.parse('Flowsheet.xml')
compressorProps=xmlFile.find('unitOperation').find('properties').findall('value') #first unit op is compressor
#set compression ratio on compressor
compressorProps[8].text=str(rat) #pressure ratio
#save xml
xmlFile.write('Flowsheet.xml')
#compress xml
zip_ref=zipfile.ZipFile(filename, 'w')
zip_ref.write('Flowsheet.xml')
zip_ref.close()
#solve
res=os.system(cofestand+' '+filename) #note os.system does not deal well with spaces in path names, or quotes, ...
if (res!=0):
raise RuntimeError('COFE run failed')
#unzip
zip_ref = zipfile.ZipFile(filename, 'r')
zip_ref.extractall(pyFolder)
zip_ref.close()
#parse XML
xmlFile=xml.etree.ElementTree.parse('Flowsheet.xml')
compressorProps=xmlFile.find('unitOperation').find('properties').findall('value')
#return energy demand
return float(compressorProps[5].text)
Code: Select all
>> py.COFE.initialize('compressors.fsd','c:\proga~1\COCO\bin\cofestand64.exe'); %python's os.system does not deal well with spaces
>> py.COFE.solveForPressureRatio(4)
Re: Connecting to COCO from MATLAB
Thanks a lot @jasper for your prompt reply and writing that piece of Python code for me. You were right about the speed bottleneck, which is Cofestand, rather than the zip/unzip process (the cpu time Cofestand takes for this particular example is an order of magnitude higher than that for zipping or unzipping). In clock time, solving the simulation through Cofestand takes about 8 s, whereas solving it from the COCO environment takes only less than a second. It'd be great if at least solveflowsheet could be called right from MATLAB through ActiveX (which I suppose would have much less overhead than Cofestand?), removing the need to use Cofestand.
Also, regarding sys_cmd_unzip in my code, I was not using it and should have removed it for more clarity. I had tried it to see if 7z could be a faster alternative to the builtin Octave zip/unzip. But, it turns out this is not the bottleneck here.
Also, regarding sys_cmd_unzip in my code, I was not using it and should have removed it for more clarity. I had tried it to see if 7z could be a faster alternative to the builtin Octave zip/unzip. But, it turns out this is not the bottleneck here.
Re: Connecting to COCO from MATLAB
Given the availability to access python directly from Matlab, give this route a try?
https://www.oreilly.com/library/view/py ... 03s03.html
Python should be able to access COFE via early binding.
https://www.oreilly.com/library/view/py ... 03s03.html
Python should be able to access COFE via early binding.
Re: Connecting to COCO from MATLAB
8 seconds?? That's a lot - I do not see that kind of latency. Could it be that these 8 seconds are eaten by a virus scanner while loading COFEStand64.exe?
Re: Connecting to COCO from MATLAB
.. and you see 8 seconds on each turn-around, or just on the first call?