Using COCO Tea in a C++ code

Moderator: jasper

Re: Using COCO Tea in a C++ code

Postby jasper » 18 April 2016, 15:11

Are you sure it is -2147167261 (0x8004D3E3)? This is not CAPE-OPEN error, nor any other error I can find. Certainly not an access violation (which wil report as 0xC0000005, but as an exception, not a HRESULT).

Why not immediately call CoCreateInstance on ppMananger and skip pGetRes and the following QueryInterface?

The error surely is because you are passing a NULL pointer for a VARIANT. So I suspect the HRESULT is E_POINTER, 0x80004003.

Try this

Code: Select all
VARIANT propPackags;
VariantInit(&propPackages);
... call the method
.... deal with the result
VariantClear(&propPackags);
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, 16:12

You are right, that was the issue.

I use now CComVariant and CComVarianArray which is simpler.

I finally got something that works.The code is below, I think that it can be useful for someone who, as me, does not know COM.
Immediately calling CoCreateInstance also works.

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: Error %i", hr);
   CComPtr<IDispatch> pGetRes;
   hr = pGetRes.CoCreateInstance(teaId);
   if (FAILED(hr)) messages::get().error("Cannot instantiate CAPE-OPEN property package manager: Error %i", hr);
   CComPtr<ICapeThermoPropertyPackageManager> ppManager;
   hr = pGetRes->QueryInterface(__uuidof(ICapeThermoPropertyPackageManager), (LPVOID*)&ppManager);
   if (FAILED(hr)) messages::get().error("Cannot get Interface for property package manager: Error %i", hr);

   CComVariant propPackages;
   hr = ppManager->GetPropertyPackageList(&propPackages);
   CComSafeArray <BSTR> propPackagesArray;
   propPackagesArray.Attach(propPackages.parray);
   printf("Number of property packages %i\n", propPackagesArray.GetCount());
   for (int i = 0; i < propPackagesArray.GetCount(); i++){
      CComBSTR mElement(propPackagesArray.GetAt(i));
      printf("Property package: %i %s\n", i, _com_util::ConvertBSTRToString(mElement.m_str));
   }
   propPackagesArray.Detach();
   
   pGetRes.Release();
   ppManager.Release();
   CoUninitialize();


Many thanks for your help!
seb007
 
Posts: 15
Joined: 25 March 2016, 15:12

Re: Using COCO Tea in a C++ code

Postby jasper » 18 April 2016, 18:58

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

Re: Using COCO Tea in a C++ code

Postby seb007 » 20 April 2016, 10:11

Hi,

I am progressing, but I still have an issue. I try to access directly to the material object, using DECLARE_NO_REGISTRY() in the ATL/COM class; then calling MyCapeOpenCOM::CreateObject() from the dll.

However, MyCapeOpenCOM type has an incomplete type in may main C++ code.

- Do I have to remove the #import of the COM dll, as I use it as a regular dll?
- Do I have to #include the MyCapeOpenCom.h file instead? When I try I get a compilation error (CComObjectRootEx is not a template).
- Do I have to export the interface as a regular dll?

Also, I have another question for the future. You said that I need to implement ICapeThermoCompounds. Isn't that included in property package?

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

Re: Using COCO Tea in a C++ code

Postby jasper » 20 April 2016, 10:18

Just #include the header in which you define the class... ComObjectRootEx is declared in the ATL headers. You may need to include those as well.

Yes, the compounds interface is implemented by the PP, which provides the compound definitions. However, the material object may contain a sub-set of the compounds in the PP. So the PP will call GetCompoundList on the MO's implementation to see which compounds are present on the MO.

If you always keep PP order, I suppose you can pass this call back on the PP, and let the PP answer its own question.

Note that the PP will likely ask you this once in between every call to SetMaterial and store the result. At least, this is how the MaterialContext is intended (not all implementations may follow this strategy). So if you have only one material object to work with, only call SetMaterial once. However, if you change the compound list on the material object, you must call SetMaterial again.
User avatar
jasper
 
Posts: 1128
Joined: 24 October 2012, 15:33
Location: Spain

Re: Using COCO Tea in a C++ code

Postby seb007 » 21 April 2016, 13:11

Thanks! By implementing ICapeThermoCompounds (just calling the Property Packages's methods), I can now do property calculations and even call calcEquilibrium. I still cannot create a material object other than through the COCreateInstance, but I will let that for later...

My idea for using a property package was to compute pressure and temperature FROM density and enthalpy rather than the opposite. This does not seem to be possible (at least when looking at the properties that TEA can compute), and I need to perform iterations on my side (which I tried to avoid). Is there any other way to get pressure and temperature from density and enthalpy?
seb007
 
Posts: 15
Joined: 25 March 2016, 15:12

Re: Using COCO Tea in a C++ code

Postby jasper » 22 April 2016, 15:52

No - in CAPE-OPEN, T and P are chosen as the degrees of freedom for mixture properties.

Well - that is, theoretically you could do a flash at T and rho and only allow for the phase of interest. Clearly in the case of a cubic equation of state (or any equation of state explicit in volume) you can avoid the iterations on both ends if you would do the calculation directly.

TEA does not currently support this flash though, and I doubt any other implementation has this special case of a single phase flash implemented in a non-iterative manner. And implementing this special case is easy enough, but it would make sense if I were to supply P-V flash support for the VL system as well, which is a bit more work. Also the single phase PV flash would of course not work for an incompressible liquid model. And would still be iterative for any model that is not explicit in volume.
User avatar
jasper
 
Posts: 1128
Joined: 24 October 2012, 15:33
Location: Spain

Re: Using COCO Tea in a C++ code

Postby seb007 » 25 April 2016, 09:27

OK. Iterating with TEA through COM calls is quite slow (compared to my own PR implementation), so I will use pre-computed tables and interpolate, which will be valid if the composition does not change.

Thanks a lot for your help in setting up the COM interaction with CAPE-OPEN components!
seb007
 
Posts: 15
Joined: 25 March 2016, 15:12

Re: Using COCO Tea in a C++ code

Postby jasper » 25 April 2016, 10:10

For a well-implemented material object, the CAPE-OPEN overhead is small if the number of compounds is more than a few. There may be an impact in systems with a very limited number of compounds.

As mentioned at the start of this thread, TEA itself is not the fastest. It was not designed with fast in mind. It was designed with interoperability in mind.
User avatar
jasper
 
Posts: 1128
Joined: 24 October 2012, 15:33
Location: Spain

Previous

Return to TEA Thermodynamic server (AmsterCHEM)

Who is online

Users browsing this forum: No registered users and 0 guests

cron