Connecting to COCO from MATLAB

Discusses use of COCO, the process simulation and modelling software suite from AmsterCHEM, downloadable from http://www.cocosimulator.org

Moderator: jasper

Connecting to COCO from MATLAB

Postby samvithvr » 21 February 2018, 19:28

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
Attachments
error.PNG
Screenshot
error.PNG (9.3 KiB) Viewed 21739 times
samvithvr
 
Posts: 2
Joined: 01 February 2018, 22:03

Re: Connecting to COCO from MATLAB

Postby jasper » 22 February 2018, 09:12

Did it work out?
User avatar
jasper
 
Posts: 1128
Joined: 24 October 2012, 15:33
Location: Spain

Re: Connecting to COCO from MATLAB

Postby samvithvr » 26 February 2018, 07:53

Yes. Thanks again Jasper
samvithvr
 
Posts: 2
Joined: 01 February 2018, 22:03

Re: Connecting to COCO from MATLAB

Postby jasper » 26 February 2018, 08:15

Glad to hear it.
User avatar
jasper
 
Posts: 1128
Joined: 24 October 2012, 15:33
Location: Spain

Re: Connecting to COCO from MATLAB

Postby cocofan » 10 April 2020, 09:52

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!
Attachments
COCO_Octave_or_MATLAB.zip
Link between Octave (or MATLAB) and COCO through xml
(20.08 KiB) Downloaded 969 times
cocofan
 
Posts: 11
Joined: 24 March 2020, 02:36

Re: Connecting to COCO from MATLAB

Postby jasper » 10 April 2020, 11:32

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/fileexchange/73256-matlab-pkg-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)


From Matlab
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)
User avatar
jasper
 
Posts: 1128
Joined: 24 October 2012, 15:33
Location: Spain

Re: Connecting to COCO from MATLAB

Postby cocofan » 12 April 2020, 05:40

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.
cocofan
 
Posts: 11
Joined: 24 March 2020, 02:36

Re: Connecting to COCO from MATLAB

Postby jasper » 12 April 2020, 11:42

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.
User avatar
jasper
 
Posts: 1128
Joined: 24 October 2012, 15:33
Location: Spain

Re: Connecting to COCO from MATLAB

Postby jasper » 12 April 2020, 11:43

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?
User avatar
jasper
 
Posts: 1128
Joined: 24 October 2012, 15:33
Location: Spain

Re: Connecting to COCO from MATLAB

Postby jasper » 12 April 2020, 11:44

.. and you see 8 seconds on each turn-around, or just on the first call?
User avatar
jasper
 
Posts: 1128
Joined: 24 October 2012, 15:33
Location: Spain

Next

Return to COCO (AmsterCHEM)

Who is online

Users browsing this forum: Google [Bot] and 5 guests

cron