Making a copy of Cantera::ThermoPhase (C++)

66 views
Skip to first unread message

Lukas

unread,
Sep 10, 2021, 8:46:03 AM9/10/21
to Cantera Users' Group
Hi everybody,

I am quite new to Cantera and would like to make a copy of a TermoPhase-object (especially PengRobinson and RedlichKwongMFTP) in C++, but unfortunately there is no copy/duplicate-method and the copy-constructor was explicitly deleted.

I already found out, that this was done when switching from Cantera 2.3 to 2.4 and I'm sure that there was a good reason for that. Anyway, is there still a possibility to make a copy of a ThermoPhase-object (or other complex Cantera-classes), e.g. with the factory or so?

Currently, I have to create a new object with same properties to create a similar object. I am doing so, by creating a new object out of the same YAML-file and setting temperature, pressure and composition (with setState_TPX). To create an object that really equals the original one, I don't know which properties I need to copy to the new object?

Thanks in advance :)

Lukas


Lukas

unread,
Sep 17, 2021, 9:22:56 AM9/17/21
to Cantera Users' Group
Update:
In the meatime I have tried some other methods to copy a ThermoPhase object. My idea was to take the ThermoPhase::input() [type: AnyMap] or ThermoPhase::xml() [type: XML_Node] and use them to create a new object with newPhase(input_or_xml). Unfortunately, this also failed with errors "Key 'species' not found." (with input()) and "The XML Node <phase> does not contain a required child node named <thermo>" (with xml()).

It seems like there are missing fields in the input/xml-object or they have another structure than expected by the newPhase-method?

Has anyone an idea how to do this? Or am I the only one who is trying to copy/duplicate ThermoPhase-objects?

If it's really not possible, it would be nice to at least know what properties I need to copy to create a duplicate (that is 100% the same as the old object).

Ray Speth

unread,
Sep 17, 2021, 10:48:39 AM9/17/21
to Cantera Users' Group

Hi Lukas,

The best way to create multiple identical ThermoPhase objects is to initialize them from the same input file, and then set them to the same state. To copy the state information as it is stored by the ThermoPhase object, you can use the saveState(vector_fp& state) and restoreState(vector_fp& state) methods (with the size of the vector needed provided by stateSize()). For most phase models (including Peng-Robinson and Redlich-Kwong), these variables are the temperature, density, and mass fractions.

The old method of duplicating ThermoPhase objects using the copy constructor was removed because it was complex to maintain and rarely useful (and because it was rarely used, it was prone to errors that were not easily found). Copying a whole ThermoPhase object (or worse, a Kinetics or Transport object) is an expensive operation compared to just keeping multiple state vectors for different mixture states. This is, for instance, how the 1D flame model uses a single set of ThermoPhase, Kinetics, and Transport objects to represent hundreds of points within the flame. I’m curious about your reason for wanting to duplicate ThermoPhase objects, to help understand whether there is indeed a use case for this that I haven’t thought of.

Regards,
Ray

Lukas

unread,
Oct 4, 2021, 3:08:36 AM10/4/21
to Cantera Users' Group
Hi Ray,

sorry it took me son long to reply, but I posted the last update shortly before my vacations :D

Thanks for you response. I think, this is what I was searching for. I've already noticed the saveState and restoreState functions before, but I wasn't sure if it really stores all information required to get the same object.

The reason why I wanted to duplicate a ThermoPhase object, was that I wanted to pass a const ThermoPhase reference to a function which does some calculations on it. So I wanted to make a copy to not affect the passed constant object. With your suggestion, I think I will just remove the const keyword (or use const_cast) and simply save and restore the original state that I want to preserve.

Regards,
Lukas
Reply all
Reply to author
Forward
0 new messages