Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 // Copyright 2001 California Institute of Technology
8 
9 
10 #ifndef THERMO_FACTORY_H
11 #define THERMO_FACTORY_H
12 
13 #include "ThermoPhase.h"
14 #include "cantera/base/xml.h"
15 #include "cantera/base/ct_thread.h"
17 
18 namespace Cantera
19 {
20 
21 class SpeciesThermoFactory;
22 class VPSSMgr;
23 
24 /*!
25  * @addtogroup thermoprops
26  *
27  * Standard ThermoPhase objects may be instantiated by calling
28  * the main %Cantera factory class for ThermoPhase objects; This class is called ThermoFactory.
29  */
30 //@{
31 
32 //! Specific error to be thrown if the type of Thermo manager is unrecognized.
33 /*!
34  * This particular error class may be caught, if the application may have other
35  * models that the main Cantera application doesn't know about.
36  */
38 {
39 public:
40  //! Constructor
41  /*!
42  * @param proc Function name where the error occurred.
43  * @param thermoModel Sting name of ThermoPhase which didn't match
44  */
45  UnknownThermoPhaseModel(const std::string& proc,
46  const std::string& thermoModel) :
47  CanteraError(proc, "Specified ThermoPhase model "
48  + thermoModel +
49  " does not match any known type.") {}
50 };
51 
52 
53 //! Factory class for thermodynamic property managers.
54 /*!
55  * This class keeps a list of the known ThermoPhase classes, and is
56  * used to create new instances of these classes.
57  */
58 class ThermoFactory : public FactoryBase
59 {
60 
61 public:
62 
63  //! Static function that creates a static instance of the factory.
64  static ThermoFactory* factory() {
65  ScopedLock lock(thermo_mutex);
66  if (!s_factory) {
68  }
69  return s_factory;
70  }
71 
72  //! delete the static instance of this factory
73  virtual void deleteFactory() {
74  ScopedLock lock(thermo_mutex);
75  delete s_factory;
76  s_factory = 0;
77  }
78 
79  //! Create a new thermodynamic property manager.
80  /*!
81  * @param model String to look up the model against
82  *
83  * @return
84  * Returns a pointer to a new ThermoPhase instance matching the
85  * model string. Returns NULL if something went wrong.
86  * Throws an exception UnknownThermoPhaseModel if the string
87  * wasn't matched.
88  */
89  virtual ThermoPhase* newThermoPhase(const std::string& model);
90 
91 private:
92  //! static member of a single instance
94 
95  //! Private constructors prevents usage
97 
98  //! Decl for locking mutex for thermo factory singleton
99  static mutex_t thermo_mutex;
100 };
101 
102 //! Create a new thermo manager instance.
103 /*!
104  * @param model String to look up the model against
105  * @param f ThermoFactory instance to use in matching the string
106  *
107  * @return
108  * Returns a pointer to a new ThermoPhase instance matching the
109  * model string. Returns NULL if something went wrong.
110  * Throws an exception UnknownThermoPhaseModel if the string
111  * wasn't matched.
112  */
113 inline ThermoPhase* newThermoPhase(const std::string& model,
114  ThermoFactory* f=0)
115 {
116  if (f == 0) {
118  }
119  return f->newThermoPhase(model);
120 }
121 
122 //! Translate the eosType id into a string
123 /*!
124  * Returns a string representation of the eosType id for a phase.
125  * @param ieos eosType id of the phase. This is unique for the phase
126  * @param length maximum length of the return string. Defaults to 100
127  *
128  * @return returns a string representation.
129  */
130 std::string eosTypeString(int ieos, int length = 100);
131 
132 //! Create a new ThermoPhase object and initializes it according to the XML
133 //! tree.
134 /*!
135  * This routine first looks up the identity of the model for the solution
136  * thermodynamics in the model attribute of the thermo child of the XML phase
137  * node. Then, it does a string lookup using Cantera's internal ThermoPhase
138  * Factory routines on the model to figure out what ThermoPhase derived class
139  * should be assigned. It creates a new instance of that class, and then calls
140  * importPhase() to populate that class with the correct parameters from the
141  * XML tree.
142  *
143  * @param phase XML_Node reference pointing to the phase XML element.
144  * @return A pointer to the completed and initialized ThermoPhase object.
145  *
146  * @ingroup inputfiles
147  */
148 ThermoPhase* newPhase(XML_Node& phase);
149 
150 //! Create and Initialize a ThermoPhase object from an XML input file.
151 /*!
152  * This routine is a wrapper around the newPhase(XML_Node) routine
153  * which does the work. The wrapper locates the input phase XML_Node
154  * in a file, and then instantiates the object, returning the pointer
155  * to the ThermoPhase object.
156  *
157  * @param infile name of the input file
158  * @param id name of the phase id in the file.
159  * If this is blank, the first phase in the file is used.
160  *
161  * @return
162  * Returns an initialized ThermoPhase object.
163  */
164 ThermoPhase* newPhase(const std::string& infile, std::string id="");
165 
166 //! Import a phase information into an empty ThermoPhase object
167 /*!
168  * Here we read an XML description of the thermodynamic information
169  * for a phase. At the end of this routine, the phase should
170  * be ready to be used within applications. This routine contains
171  * some key routines that are used as pass back routines so that
172  * the phase (and the contents of the XML file) may contain
173  * variable parameterizations for the specification of the
174  * species standard states, the equation of state, and the
175  * specification of other nonidealities. Below, a description
176  * is presented of the main algorithm for bringing up a ThermoPhase
177  * object, with care to present points where customizations
178  * occur.
179  *
180  * Before invoking this routine, either the ThermoPhase Factory routines
181  * are called or direct constructor routines are called that
182  * instantiate an inherited ThermoPhase object. This object is input
183  * to this routine, and therefore contains inherited routines that
184  * drive the customization of the initialization process.
185  *
186  * At the start of the routine, we import descriptions of the elements
187  * that make up the species in a phase.
188  *
189  * We call setParametersFromXML(eos) to read parameters about
190  * the thermo phase before the species are read in.
191  *
192  * We call addElementsFromXML() to add elements into the
193  * description of the phase.
194  *
195  * We create a new species thermo manager. Function
196  * 'newSpeciesThermoMgr' looks at the species in the database
197  * to see what thermodynamic property parameterizations are
198  * used, and selects a class that can handle the
199  * parameterizations found.
200  *
201  * We import information about the species, including their
202  * reference state thermodynamic polynomials. We then freeze
203  * the state of the species in the element.
204  *
205  * Finally, we call initThermoXML(),
206  * a member function of the ThermoPhase object, to "finish"
207  * the description. Now that the species are known,
208  * additional information may be read in about the thermodynamics
209  * of the phase, (e.g., virial coefficients, which are
210  * binary or ternary interaction parameters between species).
211  *
212  * @param phase This object must be the phase node of a
213  * complete XML tree
214  * description of the phase, including all of the
215  * species data. In other words while "phase" must
216  * point to an XML phase object, it must have
217  * sibling nodes "speciesData" that describe
218  * the species in the phase.
219  * @param th Pointer to the ThermoPhase object which will
220  * handle the thermodynamics for this phase.
221  * We initialize part of the ThermoPhase object
222  * here, especially for those objects which are
223  * part of the Cantera Kernel.
224  *
225  * @param spfactory species Thermo factory pointer, if
226  * available. If not available, one will be
227  * created.
228  * @deprecated the 'spfactory' argument is unused and will be removed after
229  * Cantera 2.2.
230  * @ingroup thermoprops
231  *
232  * @deprecated: The return value of this function is always 'true'. After
233  * Cantera 2.2, this function will return 'void'.
234  */
235 bool importPhase(XML_Node& phase, ThermoPhase* th, SpeciesThermoFactory* spfactory = 0);
236 
237 //! Add the elements given in an XML_Node tree to the specified phase
238 void installElements(Phase& th, const XML_Node& phaseNode);
239 
240 //! Install a species into a ThermoPhase object, which defines
241 //! the phase thermodynamics and speciation.
242 /*!
243  * This routine first gathers the information from the Species XML
244  * tree and calls addUniqueSpecies() to add it to the
245  * ThermoPhase object, p.
246  * This information consists of:
247  * ecomp[] = element composition of species.
248  * chgr = electric charge of species
249  * name = string name of species
250  * sz = size of the species
251  * (option double used a lot in thermo)
252  *
253  * Then, the routine processes the "thermo" XML element and
254  * calls underlying utility routines to read the XML elements
255  * containing the thermodynamic information for the reference
256  * state of the species. Failures or lack of information trigger
257  * an "UnknownSpeciesThermoModel" exception being thrown.
258  *
259  * @param k Species Index in the phase
260  * @param s XML_Node containing the species data for this species.
261  * @param p Reference to the ThermoPhase object.
262  * @param spthermo_ptr Reference to the SpeciesThermo object, where
263  * the standard state thermo properties for this
264  * species will be installed.
265  * @param rule Parameter that handles what to do with species
266  * who have elements that aren't declared.
267  * Check that all elements in the species
268  * exist in 'p'. If rule != 0, quietly skip
269  * this species if some elements are undeclared;
270  * otherwise, throw an exception
271  * @param phaseNode_ptr Pointer to the XML_Node for this phase
272  * (defaults to 0)
273  * @param vpss_ptr pointer to the Manager that calculates standard
274  * state thermo properties
275  * @param factory Pointer to the SpeciesThermoFactory .
276  * (defaults to 0)
277  * @deprecated Use newSpecies and addSpecies. For VPStandardStateTP phases, call
278  * createInstallPDSS as well. To be removed after Cantera 2.2.
279  * @return
280  * Returns true if everything is ok, false otherwise.
281  */
282 bool installSpecies(size_t k, const XML_Node& s, thermo_t& p,
283  SpeciesThermo* spthermo_ptr, int rule,
284  XML_Node* phaseNode_ptr = 0,
285  VPSSMgr* vpss_ptr = 0,
286  SpeciesThermoFactory* factory = 0);
287 
288 //! Search an XML tree for species data.
289 /*!
290  * This utility routine will search the XML tree for the species
291  * named by the string, kname. It will return the XML_Node
292  * pointer to the species data for that species.
293  * Failures of any kind return the null pointer.
294  *
295  * @param kname String containing the name of the species.
296  * @param phaseSpeciesData Pointer to the XML speciesData element
297  * containing the species data for that phase.
298  *
299  *
300  */
301 const XML_Node* speciesXML_Node(const std::string& kname,
302  const XML_Node* phaseSpeciesData);
303 
304 //@}
305 
306 }
307 
308 #endif
309 
310 
Specific error to be thrown if the type of Thermo manager is unrecognized.
Definition: ThermoFactory.h:37
virtual void deleteFactory()
delete the static instance of this factory
Definition: ThermoFactory.h:73
ThermoPhase * newPhase(XML_Node &xmlphase)
Create a new ThermoPhase object and initializes it according to the XML tree.
std::string eosTypeString(int ieos, int length)
Translate the eosType id into a string.
bool installSpecies(size_t k, const XML_Node &s, thermo_t &th, SpeciesThermo *spthermo_ptr, int rule, XML_Node *phaseNode_ptr, VPSSMgr *vpss_ptr, SpeciesThermoFactory *factory)
Install a species into a ThermoPhase object, which defines the phase thermodynamics and speciation...
static mutex_t thermo_mutex
Decl for locking mutex for thermo factory singleton.
Definition: ThermoFactory.h:96
static ThermoFactory * factory()
Static function that creates a static instance of the factory.
Definition: ThermoFactory.h:64
Base class for factories.
Definition: FactoryBase.h:18
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:45
Factory class for thermodynamic property managers.
Definition: ThermoFactory.h:58
ThermoPhase thermo_t
typedef for the ThermoPhase class
Definition: ThermoPhase.h:1660
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:97
bool importPhase(XML_Node &phase, ThermoPhase *th, SpeciesThermoFactory *spfactory)
Import a phase information into an empty ThermoPhase object.
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:99
static ThermoFactory * s_factory
static member of a single instance
Definition: ThermoFactory.h:93
ThermoPhase * newThermoPhase(const std::string &model, ThermoFactory *f=0)
Create a new thermo manager instance.
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.
Definition: ThermoFactory.h:96
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.