Using COCO Tea in a C++ code

Moderator: jasper

Using COCO Tea in a C++ code

Postby seb007 » 07 April 2016, 13:06

Hello,

I am writing a C++ CFD code, in which would I need to make calls to a thermodynamic property package to update pressure, temperature (and later phase composition for multiphase flows) from density and enthalpy. I will need to do this on a lot of points, so the calls need to be fast enough.

I though of using COCO Tea property package to do that, and I have a few questions before starting.

- Do you think that using Tea for that task would be appropriate (and fast)?
- Is there a simple example of a C++ code calling the Tea property package?
- In the Cape-Open interface, there does not seem to be an option to pass vectors of data and compute a single property for all the entries of these vectors. Is that something that can be done (for efficiency)?

Thanks for any help
seb007
 
Posts: 15
Joined: 25 March 2016, 15:12

Re: Using COCO Tea in a C++ code

Postby jasper » 07 April 2016, 18:13

TEA is not the fastest.

To use any CAPE-OPEN thermodynamics package you will have to create a material object from C++. This involves writing a COM object that exposes and implements the following CAPE-OPEN interfaces: ICapeMaterial, ICapeThermoCompounds, ICapeThermoPhases (not really required in your case if you are not doing phase equibria), ICapeThermoPropertyRoutine (not required if you are only using the MO to pass to a property package) and ICapeThermoEquilibriumRoutine (also not required in your case).

Vector calls are not supported in the current versions of CAPE-OPEN. It is on the list of things to look into for a future CAPE-OPEN version.
User avatar
jasper
 
Posts: 1128
Joined: 24 October 2012, 15:33
Location: Spain

Re: Using COCO Tea in a C++ code

Postby seb007 » 18 April 2016, 09:30

Thank you for the answer.

EDIT: I was able to build a COM dll implementing ICapeThermoMaterial, using the ATL method and "Implement Interface" wizard; and calling it from my C++ code using this


Code: Select all
#import "myCapeOpenCOM.dll"  raw_interfaces_only, raw_native_types, no_namespace, named_guids, auto_search
#import "CAPE-OPENv1-1-0.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids, auto_search



Code: Select all
CoInitialize(NULL);
ICapeThermoMaterial *material = NULL;
HRESULT hr = ::CoCreateInstance(__uuidof(myCapeOpenCOM),
      NULL,
      CLSCTX_INPROC_SERVER,
      __uuidof(ICapeThermoMaterial),
      (void**)&material);
   hr = NULL;             

...
mat->ClearAllProps();
...
           
mat->Release()
CoUninitialize();
Last edited by seb007 on 18 April 2016, 10:07, edited 5 times in total.
seb007
 
Posts: 15
Joined: 25 March 2016, 15:12

Re: Using COCO Tea in a C++ code

Postby jasper » 18 April 2016, 09:52

You are not showing in your snippet the declaration of the variable material, unless it is the same variable as mat.

The one but last argument to CoCreateInstance is an IID, you pass the CLSID. The IID (interface ID) is the identifier of the interface you want to have returned via CoCreateInstance. You could pass IID_ICapeThermoMaterial for example, and then your material variable should be an ICapeThermoMaterial *. You can also get other interfaces by using QueryInterface. Please note that if you are using raw pointers, you should be aware of the fact that each CoCreateInstance and each QueryInterface must be paired by a Release() to manage the life time of the COM object. One way to get around that would be to use smart pointers to manage the life span of your objects.

Also note that you do not need to access your material object via the CAPE-OPEN interfaces. As you supply the material object, you have control over how it looks. Often it is implemented in the same process, instead of in a separate COM server, and not registered in the Windows Registry (if you are using ATL, use DECLARE_NO_REGISTRY()). Then create the object with MyCapeOpenCOM::CreateObject() (presuming this is the name of your class) instead of CoCreateInstance, and access the methods or data members in the object directly. Only the property package that you talk to needs to access your material object over the CAPE-OPEN interfaces.

Be also aware of the ownership of data arguments. Arguments marked by [in] may not be changed by the called method. Arguments marked [out] are allocated by the called method, and must be released or deallocated by the caller. Arguments marked [in,out] must in addition have a valid value on entry. For both [out] and [in,out] the safe thing to do is to initialize objects by NULL (unless input is actually required for the method). COM programming is a bit tricky once in a while when it comes to memory management, and memory leaks are fairly hard to debug.
User avatar
jasper
 
Posts: 1128
Joined: 24 October 2012, 15:33
Location: Spain

Re: Using COCO Tea in a C++ code

Postby seb007 » 18 April 2016, 09:56

Thanks for this fast answer. Sorry, I edited the prior message while you where answering... I look at your comments for the next steps.
seb007
 
Posts: 15
Joined: 25 March 2016, 15:12

Re: Using COCO Tea in a C++ code

Postby seb007 » 18 April 2016, 10:59

Another question:

I try now to load TEA package. Should I directly use #import on the COCOTEA.DLL? It creates a .tlh which does not seem to have cape-open 1.1 methods and produces a compilation:
Code: Select all
Error   1   error : invalid redeclaration of type name "UINT_PTR"
seb007
 
Posts: 15
Joined: 25 March 2016, 15:12

Re: Using COCO Tea in a C++ code

Postby jasper » 18 April 2016, 11:06

No - directly importing a specific package would defeat the purpose of CAPE-OPEN.

The idea is that if you can use TEA, you can use any 3rd party CAPE-OPEN compliant thermodynamics software.

The type libraries to import are those made available by CO-LaN. If you have installed COCO, you will typically find them in <program files>\Common files\CAPE-OPEN (also the CO-LaN installer places them there).

Then find all COM objects that implement either category ID "CAPE-OPEN 1.1 Property Package Manager" (such as TEA), or "CAPE-OPEN 1.1 Property Package" for stand-alone property packages (such as the Water/Steamer package that comes with COCO). Use CoCreateInstance() on those.
User avatar
jasper
 
Posts: 1128
Joined: 24 October 2012, 15:33
Location: Spain

Re: Using COCO Tea in a C++ code

Postby jasper » 18 April 2016, 11:07

Note that you need only import the 1.1 type lib.
User avatar
jasper
 
Posts: 1128
Joined: 24 October 2012, 15:33
Location: Spain

Re: Using COCO Tea in a C++ code

Postby jasper » 18 April 2016, 11:08

Also note that IID_ICapeThermoMaterial (in my previous reply) will be defined at this point.
User avatar
jasper
 
Posts: 1128
Joined: 24 October 2012, 15:33
Location: Spain

Re: Using COCO Tea in a C++ code

Postby seb007 » 18 April 2016, 14:40

I can instantiate a property package manager now. However, I still get an error when calling a method on it.

I call GetPropertyPackageList and it returns an error -2147167261 (last line of the snippet), which looks like a segfault. As you said, the output parameter propPackages is allocated by the package, right?

Code: Select all
   GUID teaId;
   HRESULT hr = CLSIDFromString(OLESTR("COCO_TEA.PropertyPackManager.1"), (LPCLSID)&teaId);
   if (FAILED(hr)) messages::get().error("Cannot find property package id");
   CComPtr<IDispatch> pGetRes;
   hr = pGetRes.CoCreateInstance(teaId);
   if (FAILED(hr)) messages::get().error("Cannot instantiate CAPE-OPEN property package manager");
   CComPtr<ICapeThermoPropertyPackageManager> ppManager;
   hr = pGetRes->QueryInterface(__uuidof(ICapeThermoPropertyPackageManager), (LPVOID*)&ppManager);
   if (FAILED(hr)) messages::get().error("Cannot find Interface for property package manager");
   VARIANT  *propPackages = NULL;
   hr = ppManager->GetPropertyPackageList(propPackages);


   if (hr == ECapeNoImplHR)
      messages::get().error("Cannot get property packages list: not implemented");
   if (hr == ECapeUnknownHR)
      messages::get().error("Cannot get property packages list: unknown error");
   if (FAILED(hr)) messages::get().error("Cannot get property packages list: Error %i",hr);
seb007
 
Posts: 15
Joined: 25 March 2016, 15:12

Next

Return to TEA Thermodynamic server (AmsterCHEM)

Who is online

Users browsing this forum: No registered users and 0 guests

cron