Cantera  2.4.0
ThermoFactory.h
Go to the documentation of this file.
1 /**
2  * @file ThermoFactory.h
3  * Headers for the factory class that can create known ThermoPhase objects
4  * (see \ref thermoprops and class \link Cantera::ThermoFactory ThermoFactory\endlink).
5  */
6 
7 // This file is part of Cantera. See License.txt in the top-level directory or
8 // at http://www.cantera.org/license.txt for license and copyright information.
9 
10 #ifndef THERMO_FACTORY_H
11 #define THERMO_FACTORY_H
12 
13 #include "ThermoPhase.h"
14 #include "cantera/base/xml.h"
16 
17 namespace Cantera
18 {
19 
20 /*!
21  * @addtogroup thermoprops
22  *
23  * Standard ThermoPhase objects may be instantiated by calling the main %Cantera
24  * factory class for ThermoPhase objects; This class is called ThermoFactory.
25  */
26 //@{
27 
28 //! Specific error to be thrown if the type of Thermo manager is unrecognized.
29 /*!
30  * This particular error class may be caught, if the application may have other
31  * models that the main Cantera application doesn't know about.
32  */
34 {
35 public:
36  //! Constructor
37  /*!
38  * @param proc Function name where the error occurred.
39  * @param thermoModel Sting name of ThermoPhase which didn't match
40  */
41  UnknownThermoPhaseModel(const std::string& proc,
42  const std::string& thermoModel) :
43  CanteraError(proc, "Specified ThermoPhase model "
44  + thermoModel +
45  " does not match any known type.") {}
46 };
47 
48 
49 //! Factory class for thermodynamic property managers.
50 /*!
51  * This class keeps a list of the known ThermoPhase classes, and is
52  * used to create new instances of these classes.
53  */
54 class ThermoFactory : public Factory<ThermoPhase>
55 {
56 public:
57  //! Static function that creates a static instance of the factory.
58  static ThermoFactory* factory() {
59  std::unique_lock<std::mutex> lock(thermo_mutex);
60  if (!s_factory) {
62  }
63  return s_factory;
64  }
65 
66  //! delete the static instance of this factory
67  virtual void deleteFactory() {
68  std::unique_lock<std::mutex> lock(thermo_mutex);
69  delete s_factory;
70  s_factory = 0;
71  }
72 
73  //! Create a new thermodynamic property manager.
74  /*!
75  * @param model String to look up the model against
76  * @returns a pointer to a new ThermoPhase instance matching the model
77  * string. Returns NULL if something went wrong. Throws an exception
78  * UnknownThermoPhaseModel if the string wasn't matched.
79  */
80  virtual ThermoPhase* newThermoPhase(const std::string& model);
81 
82 private:
83  //! static member of a single instance
85 
86  //! Private constructors prevents usage
87  ThermoFactory();
88 
89  //! Decl for locking mutex for thermo factory singleton
90  static std::mutex thermo_mutex;
91 };
92 
93 //! Create a new thermo manager instance.
94 /*!
95  * @param model String to look up the model against
96  * @param f ThermoFactory instance to use in matching the string
97  * @returns a pointer to a new ThermoPhase instance matching the model string.
98  * Returns NULL if something went wrong. Throws an exception
99  * UnknownThermoPhaseModel if the string wasn't matched.
100  */
101 inline ThermoPhase* newThermoPhase(const std::string& model)
102 {
103  return ThermoFactory::factory()->create(model);
104 }
105 
106 //! Create a new ThermoPhase object and initializes it according to the XML tree
107 /*!
108  * This routine first looks up the identity of the model for the solution
109  * thermodynamics in the model attribute of the thermo child of the XML phase
110  * node. Then, it does a string lookup using Cantera's internal ThermoPhase
111  * Factory routines on the model to figure out what ThermoPhase derived class
112  * should be assigned. It creates a new instance of that class, and then calls
113  * importPhase() to populate that class with the correct parameters from the
114  * XML tree.
115  *
116  * @param phase XML_Node reference pointing to the phase XML element.
117  * @return A pointer to the completed and initialized ThermoPhase object.
118  *
119  * @ingroup inputfiles
120  */
121 ThermoPhase* newPhase(XML_Node& phase);
122 
123 //! Create and Initialize a ThermoPhase object from an XML input file.
124 /*!
125  * This routine is a wrapper around the newPhase(XML_Node) routine which does
126  * the work. The wrapper locates the input phase XML_Node in a file, and then
127  * instantiates the object, returning the pointer to the ThermoPhase object.
128  *
129  * @param infile name of the input file
130  * @param id name of the phase id in the file.
131  * If this is blank, the first phase in the file is used.
132  * @returns an initialized ThermoPhase object.
133  */
134 ThermoPhase* newPhase(const std::string& infile, std::string id="");
135 
136 //! Import a phase information into an empty ThermoPhase object
137 /*!
138  * Here we read an XML description of the thermodynamic information for a phase.
139  * At the end of this routine, the phase should be ready to be used within
140  * applications. This routine contains some key routines that are used as pass
141  * back routines so that the phase (and the contents of the XML file) may
142  * contain variable parameterizations for the specification of the species
143  * standard states, the equation of state, and the specification of other
144  * nonidealities. Below, a description is presented of the main algorithm for
145  * bringing up a ThermoPhase object, with care to present points where
146  * customizations occur.
147  *
148  * Before invoking this routine, either the ThermoPhase Factory routines are
149  * called or direct constructor routines are called that instantiate an
150  * inherited ThermoPhase object. This object is input to this routine, and
151  * therefore contains inherited routines that drive the customization of the
152  * initialization process.
153  *
154  * At the start of the routine, we import descriptions of the elements that make
155  * up the species in a phase.
156  *
157  * We call setParametersFromXML(eos) to read parameters about the thermo phase
158  * before the species are read in.
159  *
160  * We call addElementsFromXML() to add elements into the description of the
161  * phase.
162  *
163  * We create a new species thermo manager. Function 'newSpeciesThermoMgr' looks
164  * at the species in the database to see what thermodynamic property
165  * parameterizations are used, and selects a class that can handle the
166  * parameterizations found.
167  *
168  * We import information about the species, including their reference state
169  * thermodynamic polynomials. We then freeze the state of the species in the
170  * element.
171  *
172  * Finally, we call initThermoXML(), a member function of the ThermoPhase
173  * object, to "finish" the description. Now that the species are known,
174  * additional information may be read in about the thermodynamics of the phase,
175  * (e.g., virial coefficients, which are binary or ternary interaction
176  * parameters between species).
177  *
178  * @param phase This object must be the phase node of a complete XML tree
179  * description of the phase, including all of the species data. In
180  * other words while "phase" must point to an XML phase object, it
181  * must have sibling nodes "speciesData" that describe the species
182  * in the phase.
183  * @param th Pointer to the ThermoPhase object which will handle the
184  * thermodynamics for this phase. We initialize part of the
185  * ThermoPhase object here, especially for those objects which are
186  * part of the Cantera Kernel.
187  * @ingroup thermoprops
188  */
189 void importPhase(XML_Node& phase, ThermoPhase* th);
190 
191 //! Add the elements given in an XML_Node tree to the specified phase
192 void installElements(Phase& th, const XML_Node& phaseNode);
193 
194 //! Search an XML tree for species data.
195 /*!
196  * This utility routine will search the XML tree for the species named by the
197  * string, kname. It will return the XML_Node pointer to the species data for
198  * that species. Failures of any kind return the null pointer.
199  *
200  * @param kname String containing the name of the species.
201  * @param phaseSpeciesData Pointer to the XML speciesData element
202  * containing the species data for that phase.
203  */
204 const XML_Node* speciesXML_Node(const std::string& kname,
205  const XML_Node* phaseSpeciesData);
206 
207 //@}
208 
209 }
210 
211 #endif
Specific error to be thrown if the type of Thermo manager is unrecognized.
Definition: ThermoFactory.h:33
virtual void deleteFactory()
delete the static instance of this factory
Definition: ThermoFactory.h:67
ThermoPhase * newPhase(XML_Node &xmlphase)
Create a new ThermoPhase object and initializes it according to the XML tree.
static ThermoFactory * factory()
Static function that creates a static instance of the factory.
Definition: ThermoFactory.h:58
const XML_Node * speciesXML_Node(const std::string &kname, const XML_Node *phaseSpeciesData)
Search an XML tree for species data.
UnknownThermoPhaseModel(const std::string &proc, const std::string &thermoModel)
Constructor.
Definition: ThermoFactory.h:41
Factory class for thermodynamic property managers.
Definition: ThermoFactory.h:54
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:93
static std::mutex thermo_mutex
Decl for locking mutex for thermo factory singleton.
Definition: ThermoFactory.h:90
T * create(std::string name, Args... args)
Create an object using the object construction function corresponding to "name" and the provided cons...
Definition: FactoryBase.h:77
virtual ThermoPhase * newThermoPhase(const std::string &model)
Create a new thermodynamic property manager.
Classes providing support for XML data files.
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:65
static ThermoFactory * s_factory
static member of a single instance
Definition: ThermoFactory.h:84
void importPhase(XML_Node &phase, ThermoPhase *th)
Import a phase information into an empty ThermoPhase object.
Factory class that supports registering functions to create objects.
Definition: FactoryBase.h:71
void installElements(Phase &th, const XML_Node &phaseNode)
Add the elements given in an XML_Node tree to the specified phase.
ThermoFactory()
Private constructors prevents usage.
ThermoPhase * newThermoPhase(const std::string &model)
Create a new thermo manager instance.
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:8
Header file for class ThermoPhase, the base class for phases with thermodynamic properties, and the text for the Module thermoprops (see Thermodynamic Properties and class ThermoPhase).
File contains the FactoryBase class declarations.