Connecting to COCO from MATLAB
Posted: 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
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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