Skip to content

Commit

Permalink
[Thermo] Add factory methods returning shared_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Mar 7, 2023
1 parent 25b3ce3 commit f3e840d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 24 deletions.
6 changes: 3 additions & 3 deletions include/cantera/base/Interface.h
Expand Up @@ -52,7 +52,7 @@ class Interface : public Solution

//! Create and initialize a new Interface from an input file
/*!
* This constructor wraps newPhase() and newKinetics()
* This constructor wraps newThermoPhase() and newKinetics()
*
* @param infile name of the input file
* @param name name of the surface phase in the file.
Expand All @@ -68,7 +68,7 @@ shared_ptr<Interface> newInterface(const std::string& infile,

//! Create and initialize a new Interface from an input file
/*!
* This constructor wraps newPhase() and newKinetics()
* This constructor wraps newThermoPhase() and newKinetics()
*
* @param infile name of the input file
* @param name name of the phase in the file. If this is the empty string, the first
Expand All @@ -82,7 +82,7 @@ shared_ptr<Interface> newInterface(const std::string& infile,

//! Create and initialize a new Interface from AnyMap objects
/*!
* This constructor wraps newPhase() and newKinetics()
* This constructor wraps newThermoPhase() and newKinetics()
*
* @param phaseNode the node containing the phase definition (that is, thermo model,
* list of species, and initial state)
Expand Down
10 changes: 5 additions & 5 deletions include/cantera/base/Solution.h
Expand Up @@ -148,7 +148,7 @@ class Solution : public std::enable_shared_from_this<Solution>

//! Create and initialize a new Solution from an input file
/*!
* This constructor wraps newPhase(), newKinetics() and newTransport() routines
* This constructor wraps newThermoPhase(), newKinetics() and newTransport() routines
* for initialization.
*
* @param infile name of the input file
Expand All @@ -166,8 +166,8 @@ shared_ptr<Solution> newSolution(const std::string& infile, const std::string& n

//! Create and initialize a new Solution manager from an input file
/*!
* This constructor wraps newPhase(), newKinetics() and newTransport() routines for
* initialization.
* This constructor wraps newThermoPhase(), newKinetics() and newTransport() routines
* for initialization.
*
* @param infile name of the input file
* @param name name of the phase in the file.
Expand All @@ -184,8 +184,8 @@ shared_ptr<Solution> newSolution(const std::string& infile,

//! Create and initialize a new Solution manager from AnyMap objects
/*!
* This constructor wraps newPhase(), newKinetics() and newTransport() routines for
* initialization.
* This constructor wraps newThermoPhase(), newKinetics() and newTransport() routines
* for initialization.
*
* @param phaseNode the node containing the phase definition (that is, thermo model,
* list of species, and initial state)
Expand Down
16 changes: 14 additions & 2 deletions include/cantera/thermo/ThermoFactory.h
Expand Up @@ -44,6 +44,7 @@ class ThermoFactory : public Factory<ThermoPhase>
* @param model The name of the thermo model
* @returns a pointer to a new ThermoPhase object of the type specified. Throws a
* CanteraError if the named model isn't registered with ThermoFactory.
* @deprecated To be removed after Cantera 3.0; superseded by newThermo()
*/
virtual ThermoPhase* newThermoPhase(const std::string& model);

Expand All @@ -58,13 +59,15 @@ class ThermoFactory : public Factory<ThermoPhase>
static std::mutex thermo_mutex;
};

//! @copydoc ThermoFactory::newThermoPhase
//! @copydoc ThermoFactory::newThermo(const string&)
//! @deprecated To be removed after Cantera 3.0; superseded by newThermo()
ThermoPhase* newThermoPhase(const string& model);

//! Create a new ThermoPhase instance.
/*!
* @param model String to look up the model against
* @returns a shared pointer to a new ThermoPhase instance matching the model string.
* @returns a shared pointer to a new ThermoPhase object of the type specified. Throws a
* CanteraError if the named model is not registered with ThermoFactory.
*/
shared_ptr<ThermoPhase> newThermo(const string& model);

Expand All @@ -76,6 +79,11 @@ shared_ptr<ThermoPhase> newThermo(const string& model);
* which will be used as the default location from which to read species
* definitions.
*/
shared_ptr<ThermoPhase> newThermoPhase(const AnyMap& phaseNode,
const AnyMap& rootNode=AnyMap());

//! @copydoc ThermoFactory::newThermoPhase(const AnyMap&, const AnyMap&)
//! @deprecated To be removed after Cantera 3.0; superseded by newThermoPhase()
unique_ptr<ThermoPhase> newPhase(const AnyMap& phaseNode,
const AnyMap& rootNode=AnyMap());

Expand All @@ -90,6 +98,10 @@ unique_ptr<ThermoPhase> newPhase(const AnyMap& phaseNode,
* If this is blank, the first phase in the file is used.
* @returns an initialized ThermoPhase object.
*/
shared_ptr<ThermoPhase> newThermoPhase(const string& infile, const string& id);

//! @copydoc ThermoFactory::newThermoPhase(const string&, const string&)
//! @deprecated To be removed after Cantera 3.0; superseded by newThermoPhase()
ThermoPhase* newPhase(const std::string& infile, std::string id="");

//! Initialize a ThermoPhase object
Expand Down
4 changes: 2 additions & 2 deletions include/cantera/thermo/ThermoPhase.h
Expand Up @@ -1663,7 +1663,7 @@ class ThermoPhase : public Phase
const AnyMap& rootNode=AnyMap());

//! Returns the parameters of a ThermoPhase object such that an identical
//! one could be reconstructed using the newPhase(AnyMap&) function.
//! one could be reconstructed using the newThermoPhase(AnyMap&) function.
//! @param withInput If true, include additional input data fields associated
//! with the phase description, such as user-defined fields from a YAML input
//! file, as returned by the input() method.
Expand Down Expand Up @@ -1799,7 +1799,7 @@ class ThermoPhase : public Phase

protected:
//! Store the parameters of a ThermoPhase object such that an identical
//! one could be reconstructed using the newPhase(AnyMap&) function. This
//! one could be reconstructed using the newThermoPhase(AnyMap&) function. This
//! does not include user-defined fields available in input().
virtual void getParameters(AnyMap& phaseNode) const;

Expand Down
4 changes: 2 additions & 2 deletions src/base/Solution.cpp
Expand Up @@ -182,7 +182,7 @@ shared_ptr<Solution> newSolution(const std::string &infile,
}

if (extension == "cti" || extension == "xml") {
throw CanteraError("newPhase",
throw CanteraError("newSolution",
"The CTI and XML formats are no longer supported.");
}

Expand Down Expand Up @@ -216,7 +216,7 @@ shared_ptr<Solution> newSolution(const AnyMap& phaseNode,
const std::map<std::string, shared_ptr<Solution>>& related)
{
// thermo phase
auto thermo = shared_ptr<ThermoPhase>(newPhase(phaseNode, rootNode));
auto thermo = newThermoPhase(phaseNode, rootNode);

// instantiate Solution object of the correct derived type
shared_ptr<Solution> sol;
Expand Down
4 changes: 2 additions & 2 deletions src/thermo/IonsFromNeutralVPSSTP.cpp
Expand Up @@ -478,10 +478,10 @@ void IonsFromNeutralVPSSTP::initThermo()
AnyMap infile = AnyMap::fromYamlFile(fileName,
m_input.getString("__file__", ""));
AnyMap& phaseNode = infile["phases"].getMapWhere("name", neutralName);
setNeutralMoleculePhase(newPhase(phaseNode, infile));
setNeutralMoleculePhase(newThermoPhase(phaseNode, infile));
} else {
AnyMap& phaseNode = m_rootNode["phases"].getMapWhere("name", neutralName);
setNeutralMoleculePhase(newPhase(phaseNode, m_rootNode));
setNeutralMoleculePhase(newThermoPhase(phaseNode, m_rootNode));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/thermo/LatticeSolidPhase.cpp
Expand Up @@ -306,7 +306,7 @@ void LatticeSolidPhase::initThermo()
compositionMap composition = m_input["composition"].asMap<double>();
for (auto& item : composition) {
AnyMap& node = m_rootNode["phases"].getMapWhere("name", item.first);
addLattice(newPhase(node, m_rootNode));
addLattice(newThermoPhase(node, m_rootNode));
}
setLatticeStoichiometry(composition);
}
Expand Down
42 changes: 35 additions & 7 deletions src/thermo/ThermoFactory.cpp
Expand Up @@ -122,8 +122,17 @@ void ThermoFactory::deleteFactory()
s_factory = 0;
}

ThermoPhase* ThermoFactory::newThermoPhase(const std::string& model)
{
warn_deprecated("newThermoPhase",
"To be removed after Cantera 3.0; superseded by newThermo.");
return create(model);
}

ThermoPhase* newThermoPhase(const string& model)
{
warn_deprecated("newThermoPhase",
"To be removed after Cantera 3.0; superseded by newThermo.");
return ThermoFactory::factory()->create(model);
}

Expand All @@ -133,13 +142,23 @@ shared_ptr<ThermoPhase> newThermo(const string& model)
return tptr;
}

ThermoPhase* ThermoFactory::newThermoPhase(const std::string& model)
shared_ptr<ThermoPhase> newThermoPhase(const AnyMap& phaseNode, const AnyMap& rootNode)
{
return create(model);
if (!phaseNode.hasKey("kinetics") && phaseNode.hasKey("reactions")) {
throw InputFileError("newPhase", phaseNode["reactions"],
"Phase entry includes a 'reactions' field but does not "
"specify a kinetics model.");
}
shared_ptr<ThermoPhase> t = newThermo(phaseNode["thermo"].asString());
setupPhase(*t, phaseNode, rootNode);
return t;
}

unique_ptr<ThermoPhase> newPhase(const AnyMap& phaseNode, const AnyMap& rootNode)
{
warn_deprecated("newPhase",
"To be removed after Cantera 3.0; superseded by\n"
"newThermoPhase(const AnyMap&, const AnyMap&).");
if (!phaseNode.hasKey("kinetics") && phaseNode.hasKey("reactions")) {
throw InputFileError("newPhase", phaseNode["reactions"],
"Phase entry includes a 'reactions' field but does not "
Expand All @@ -150,24 +169,33 @@ unique_ptr<ThermoPhase> newPhase(const AnyMap& phaseNode, const AnyMap& rootNode
return t;
}

ThermoPhase* newPhase(const std::string& infile, std::string id)
shared_ptr<ThermoPhase> newThermoPhase(const string& infile, const string& id)
{
size_t dot = infile.find_last_of(".");
string extension;
if (dot != npos) {
extension = toLowerCopy(infile.substr(dot+1));
}
string id_ = id;
if (id == "-") {
id = "";
id_ = "";
}
if (extension == "cti" || extension == "xml") {
throw CanteraError("newPhase",
throw CanteraError("newThermo",
"The CTI and XML formats are no longer supported.");
}

AnyMap root = AnyMap::fromYamlFile(infile);
AnyMap& phase = root["phases"].getMapWhere("name", id);
return newPhase(phase, root).release();
AnyMap& phase = root["phases"].getMapWhere("name", id_);
return newThermoPhase(phase, root);
}

ThermoPhase* newPhase(const std::string& infile, std::string id)
{
warn_deprecated("newPhase",
"To be removed after Cantera 3.0; superseded by\n"
"newThermoPhase(const std::string&, const std::string&).");
return newThermoPhase(infile, id).get();
}

void addDefaultElements(ThermoPhase& thermo, const vector<string>& element_names) {
Expand Down

0 comments on commit f3e840d

Please sign in to comment.