Cantera  2.5.1
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 https://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  * @returns a pointer to a new ThermoPhase instance matching the model string.
97  * Returns NULL if something went wrong. Throws an exception
98  * UnknownThermoPhaseModel if the string wasn't matched.
99  */
100 inline ThermoPhase* newThermoPhase(const std::string& model)
101 {
102  return ThermoFactory::factory()->create(model);
103 }
104 
105 //! Create a new ThermoPhase object and initializes it according to the XML tree
106 /*!
107  * This routine first looks up the identity of the model for the solution
108  * thermodynamics in the model attribute of the thermo child of the XML phase
109  * node. Then, it does a string lookup using Cantera's internal ThermoPhase
110  * Factory routines on the model to figure out what ThermoPhase derived class
111  * should be assigned. It creates a new instance of that class, and then calls
112  * importPhase() to populate that class with the correct parameters from the
113  * XML tree.
114  *
115  * @param phase XML_Node reference pointing to the phase XML element.
116  * @return A pointer to the completed and initialized ThermoPhase object.
117  *
118  * @ingroup inputfiles
119  *
120  * @deprecated The XML input format is deprecated and will be removed in
121  * Cantera 3.0.
122  */
123 ThermoPhase* newPhase(XML_Node& phase);
124 
125 //! Create a new ThermoPhase object and initialize it
126 /*!
127  * @param phaseNode The node containing the phase definition (i.e. thermo
128  * model, list of species, and initial state)
129  * @param rootNode The root node of the tree containing the phase definition,
130  * which will be used as the default location from which to read species
131  * definitions.
132  */
133 unique_ptr<ThermoPhase> newPhase(AnyMap& phaseNode,
134  const AnyMap& rootNode=AnyMap());
135 
136 //! Create and Initialize a ThermoPhase object from an input file.
137 /*!
138  * For YAML input files, this function uses AnyMap::fromYamlFile() to read the
139  * input file, newThermoPhase() to create an empty ThermoPhase of the
140  * appropriate type, and setupPhase() to initialize the phase.
141  *
142  * For CTI and XML input files, this function uses get_XML_File() to read the
143  * input file and newPhase(XML_Node) to create and initialize the phase.
144  *
145  * @param infile name of the input file
146  * @param id name (id) of the phase in the file.
147  * If this is blank, the first phase in the file is used.
148  * @returns an initialized ThermoPhase object.
149  */
150 ThermoPhase* newPhase(const std::string& infile, std::string id="");
151 
152 //! Import a phase information into an empty ThermoPhase object
153 /*!
154  * Here we read an XML description of the thermodynamic information for a phase.
155  * At the end of this routine, the phase should be ready to be used within
156  * applications. This routine contains some key routines that are used as pass
157  * back routines so that the phase (and the contents of the XML file) may
158  * contain variable parameterizations for the specification of the species
159  * standard states, the equation of state, and the specification of other
160  * nonidealities. Below, a description is presented of the main algorithm for
161  * bringing up a ThermoPhase object, with care to present points where
162  * customizations occur.
163  *
164  * Before invoking this routine, either the ThermoPhase Factory routines are
165  * called or direct constructor routines are called that instantiate an
166  * inherited ThermoPhase object. This object is input to this routine, and
167  * therefore contains inherited routines that drive the customization of the
168  * initialization process.
169  *
170  * At the start of the routine, we import descriptions of the elements that make
171  * up the species in a phase.
172  *
173  * We call setParametersFromXML(eos) to read parameters about the thermo phase
174  * before the species are read in.
175  *
176  * We call addElementsFromXML() to add elements into the description of the
177  * phase.
178  *
179  * We create a new species thermo manager. Function 'newSpeciesThermoMgr' looks
180  * at the species in the database to see what thermodynamic property
181  * parameterizations are used, and selects a class that can handle the
182  * parameterizations found.
183  *
184  * We import information about the species, including their reference state
185  * thermodynamic polynomials. We then freeze the state of the species in the
186  * element.
187  *
188  * Finally, we call initThermoXML(), a member function of the ThermoPhase
189  * object, to "finish" the description. Now that the species are known,
190  * additional information may be read in about the thermodynamics of the phase,
191  * (e.g., virial coefficients, which are binary or ternary interaction
192  * parameters between species).
193  *
194  * @param phase This object must be the phase node of a complete XML tree
195  * description of the phase, including all of the species data. In
196  * other words while "phase" must point to an XML phase object, it
197  * must have sibling nodes "speciesData" that describe the species
198  * in the phase.
199  * @param th Pointer to the ThermoPhase object which will handle the
200  * thermodynamics for this phase. We initialize part of the
201  * ThermoPhase object here, especially for those objects which are
202  * part of the Cantera Kernel.
203  * @ingroup thermoprops
204  *
205  * @deprecated The XML input format is deprecated and will be removed in
206  * Cantera 3.0.
207  */
208 void importPhase(XML_Node& phase, ThermoPhase* th);
209 
210 //! Initialize a ThermoPhase object
211 /*!
212  * @param phase The ThermoPhase object to be initialized
213  * @param phaseNode The node containing the phase definition (i.e. thermo
214  * model, list of species, and initial state)
215  * @param rootNode The root node of the tree containing the phase definition,
216  * which will be used as the default location from which to read species
217  * definitions.
218  */
219 void setupPhase(ThermoPhase& phase, AnyMap& phaseNode,
220  const AnyMap& rootNode=AnyMap());
221 
222 //! Add the elements given in an XML_Node tree to the specified phase
223 //!
224 //! @deprecated The XML input format is deprecated and will be removed in
225 //! Cantera 3.0.
226 void installElements(Phase& th, const XML_Node& phaseNode);
227 
228 //! Search an XML tree for species data.
229 /*!
230  * This utility routine will search the XML tree for the species named by the
231  * string, kname. It will return the XML_Node pointer to the species data for
232  * that species. Failures of any kind return the null pointer.
233  *
234  * @param kname String containing the name of the species.
235  * @param phaseSpeciesData Pointer to the XML speciesData element
236  * containing the species data for that phase.
237  *
238  * @deprecated The XML input format is deprecated and will be removed in
239  * Cantera 3.0.
240  */
241 const XML_Node* speciesXML_Node(const std::string& kname,
242  const XML_Node* phaseSpeciesData);
243 
244 //@}
245 
246 }
247 
248 #endif
File contains the FactoryBase class declarations.
Header file for class ThermoPhase, the base class for phases with thermodynamic properties,...
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
Factory class that supports registering functions to create objects.
Definition: FactoryBase.h:71
T * create(const std::string &name, Args... args)
Create an object using the object construction function corresponding to "name" and the provided cons...
Definition: FactoryBase.h:77
Factory class for thermodynamic property managers.
Definition: ThermoFactory.h:55
ThermoFactory()
Private constructors prevents usage.
static ThermoFactory * factory()
Static function that creates a static instance of the factory.
Definition: ThermoFactory.h:58
static std::mutex thermo_mutex
Decl for locking mutex for thermo factory singleton.
Definition: ThermoFactory.h:90
virtual ThermoPhase * newThermoPhase(const std::string &model)
Create a new thermodynamic property manager.
virtual void deleteFactory()
delete the static instance of this factory
Definition: ThermoFactory.h:67
static ThermoFactory * s_factory
static member of a single instance
Definition: ThermoFactory.h:84
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:102
Specific error to be thrown if the type of Thermo manager is unrecognized.
Definition: ThermoFactory.h:34
UnknownThermoPhaseModel(const std::string &proc, const std::string &thermoModel)
Constructor.
Definition: ThermoFactory.h:41
ThermoPhase * newPhase(XML_Node &xmlphase)
Create a new ThermoPhase object and initializes it according to the XML tree.
void importPhase(XML_Node &phase, ThermoPhase *th)
Import a phase information into an empty ThermoPhase object.
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:264
void setupPhase(ThermoPhase &thermo, AnyMap &phaseNode, const AnyMap &rootNode)
Initialize a ThermoPhase object.
const XML_Node * speciesXML_Node(const std::string &kname, const XML_Node *phaseSpeciesData)
Search an XML tree for species data.
void installElements(Phase &th, const XML_Node &phaseNode)
Add the elements given in an XML_Node tree to the specified phase.
ThermoPhase * newThermoPhase(const std::string &model)
Create a new thermo manager instance.
Classes providing support for XML data files.