Cantera  2.0
Phase.h
Go to the documentation of this file.
1 /**
2  * @file Phase.h
3  * Header file for class Phase.
4  */
5 // Copyright 2001 California Institute of Technology
6 
7 #ifndef CT_PHASE_H
8 #define CT_PHASE_H
9 
11 #include "cantera/base/ctml.h"
13 
14 namespace Cantera
15 {
16 class SpeciesThermo;
17 
18 /**
19  * @defgroup phases Models of Phases of Matter
20  *
21  * These classes are used to represent the composition and state of a single
22  * phase of matter. Together these classes form the basis for describing the
23  * species and element compositions of a phase as well as the stoichiometry
24  * of each species, and for describing the current state of the phase. They do
25  * not in themselves contain Thermodynamic equation of state information.
26  * However, they do comprise all of the necessary background functionality to
27  * support thermodynamic calculations (see \ref thermoprops).
28  */
29 
30 //! Exception class to indicate a fixed set of elements.
31 /*!
32  * This class is used to warn the user when the number of elements
33  * are changed after at least one species is defined.
34  */
35 class ElementsFrozen : public CanteraError
36 {
37 public:
38  //! Constructor for class
39  //! @param func Function where the error occurred.
40  ElementsFrozen(std::string func)
41  : CanteraError(func, "Elements cannot be added after species.") {}
42 };
43 
44 //! Base class for phases of matter
45 /*!
46  *
47  * Class Phase manages the species and elements in a phase, as well as the
48  * independent variables of temperature, mass density, and species mass/mole
49  * fraction that define the thermodynamic state.
50  *
51  * Class Phase provides information about the elements and species in a
52  * phase - names, index numbers (location in arrays), atomic or molecular
53  * weights, etc. The set of elements must include all those that compose the
54  * species, but may include additional elements.
55  *
56  * It also stores an array of species molecular weights, which are used to
57  * convert between mole and mass representations of the composition. For
58  * efficiency in mass/mole conversion, the vector of mass fractions divided
59  * by molecular weight \f$ Y_k/M_k \f$ is also stored.
60  *
61  * Class Phase is not usually used directly. Its primary use is as a base class
62  * for class ThermoPhase. It is not generally necessary to overloaded any of
63  * class Phase's methods, with the exception of incompressible phases. In that
64  * case, the density must be replaced by the pressure as the independent
65  * variable and functions such as setMassFraction within class Phase must
66  * actually now calculate the density (at constant T and P) instead of leaving
67  * it alone as befits an independent variable. This also applies for nearly-
68  * incompressible phases or phases which utilize standard states based on a
69  * T and P, in which case they need to overload these functions too.
70  *
71  * Class Phase contains a number of utility functions that will set the state
72  * of the phase in its entirety, by first setting the composition, then the
73  * temperature and then the density. An example of this is the function
74  * Phase::setState_TRY(double t, double dens, const double* y).
75  *
76  * Class Phase contains method for saving and restoring the full internal
77  * states of each phase. These are saveState() and restoreState(). These
78  * functions operate on a state vector, which is in general of length
79  * (2 + nSpecies()). The first two entries of the state vector are temperature
80  * and density.
81  *
82  * A species name may be referred to via three methods:
83  *
84  * - "speciesName"
85  * - "PhaseId:speciesName"
86  * - "phaseName:speciesName"
87  * .
88  *
89  * The first two methods of naming may not yield a unique species within
90  * complicated assemblies of %Cantera Phases.
91  *
92  * @todo Make the concept of saving state vectors more general, so that it can
93  * handle other cases where there are additional internal state variables, such
94  * as the voltage, a potential energy, or a strain field.
95  *
96  * @ingroup phases
97  */
98 class Phase
99 {
100 public:
101  Phase(); //!< Default constructor.
102 
103  virtual ~Phase(); //!< Destructor.
104 
105  //! Copy Constructor
106  //! @param right Reference to the class to be used in the copy
107  Phase(const Phase& right);
108 
109  //! Assignment operator
110  //! @param right Reference to the class to be used in the copy
111  Phase& operator=(const Phase& right);
112 
113  //! Returns a reference to the XML_Node stored for the phase.
114  //! The XML_Node for the phase contains all of the input data used to set
115  //! up the model for the phase, during its initialization.
116  XML_Node& xml();
117 
118  /*! @name Name and ID
119  * Class Phase contains two strings that identify a phase. The ID is the
120  * value of the ID attribute of the XML phase node that is used to
121  * initialize a phase when it is read. The name field is also initialized
122  * to the value of the ID attribute of the XML phase node.
123  *
124  * However, the name field may be changed to another value during the
125  * course of a calculation. For example, if a phase is located in two
126  * places, but has the same constitutive input, the ids of the two phases
127  * will be the same, but the names of the two phases may be different.
128  *
129  * It is an error to have two phases in a single problem with the same name
130  * or the same id (or the name from one phase being the same as the id of
131  * another phase). Thus, it is expected that there is a 1-1 correspondence
132  * between names and unique phases within a Cantera problem.
133  */
134  //!@{
135 
136  //! Return the string id for the phase.
137  std::string id() const;
138 
139  //! Set the string id for the phase.
140  //! @param id String id of the phase
141  void setID(std::string id);
142 
143  //! Return the name of the phase.
144  std::string name() const;
145 
146  //! Sets the string name for the phase.
147  //! @param nm String name of the phase
148  void setName(std::string nm);
149  //!@} end group Name and ID
150 
151  //! @name Element and Species Information
152  //!@{
153 
154  //! Name of the element with index m.
155  //! @param m Element index.
156  std::string elementName(size_t m) const;
157 
158  //! Return the index of element named 'name'. The index is an integer
159  //! assigned to each element in the order it was added. Returns \ref npos
160  //! if the specified element is not found.
161  //! @param name Name of the element
162  size_t elementIndex(std::string name) const;
163 
164  //! Return a read-only reference to the vector of element names.
165  const std::vector<std::string>& elementNames() const;
166 
167  //! Atomic weight of element m.
168  //! @param m Element index
169  doublereal atomicWeight(size_t m) const;
170 
171  //! Entropy of the element in its standard state at 298 K and 1 bar
172  //! @param m Element index
173  doublereal entropyElement298(size_t m) const;
174 
175  //! Atomic number of element m.
176  //! @param m Element index
177  int atomicNumber(size_t m) const;
178 
179  //! Return the element constraint type
180  //! Possible types include:
181  //!
182  //! CT_ELEM_TYPE_TURNEDOFF -1
183  //! CT_ELEM_TYPE_ABSPOS 0
184  //! CT_ELEM_TYPE_ELECTRONCHARGE 1
185  //! CT_ELEM_TYPE_CHARGENEUTRALITY 2
186  //! CT_ELEM_TYPE_LATTICERATIO 3
187  //! CT_ELEM_TYPE_KINETICFROZEN 4
188  //! CT_ELEM_TYPE_SURFACECONSTRAINT 5
189  //! CT_ELEM_TYPE_OTHERCONSTRAINT 6
190  //!
191  //! The default is CT_ELEM_TYPE_ABSPOS.
192  //! @param m Element index
193  //! @return Returns the element type
194  int elementType(size_t m) const;
195 
196  //! Change the element type of the mth constraint
197  //! Reassigns an element type.
198  //! @param m Element index
199  //! @param elem_type New elem type to be assigned
200  //! @return Returns the old element type
201  int changeElementType(int m, int elem_type);
202 
203  //! Return a read-only reference to the vector of atomic weights.
204  const vector_fp& atomicWeights() const;
205 
206  //! Number of elements.
207  size_t nElements() const;
208 
209  //! Check that the specified element index is in range
210  //! Throws an exception if m is greater than nElements()-1
211  void checkElementIndex(size_t m) const;
212 
213  //! Check that an array size is at least nElements()
214  //! Throws an exception if mm is less than nElements(). Used before calls
215  //! which take an array pointer.
216  void checkElementArraySize(size_t mm) const;
217 
218  //! Number of atoms of element \c m in species \c k.
219  //! @param k species index
220  //! @param m element index
221  doublereal nAtoms(size_t k, size_t m) const;
222 
223  //! Get a vector containing the atomic composition of species k
224  //! @param k species index
225  //! @param atomArray vector containing the atomic number in the species.
226  //! Length: m_mm
227  void getAtoms(size_t k, double* atomArray) const;
228 
229  //! Returns the index of a species named 'name' within the Phase object.
230  //! The first species in the phase will have an index 0, and the last one
231  //! will have an index of nSpecies() - 1.
232  //! @param name String name of the species. It may also be in the form
233  //! phaseName:speciesName
234  //! @return The index of the species. If the name is not found,
235  //! the value \ref npos is returned.
236  size_t speciesIndex(std::string name) const;
237 
238  //! Name of the species with index k
239  //! @param k index of the species
240  std::string speciesName(size_t k) const;
241 
242  //! Returns the expanded species name of a species, including the phase name
243  //! This is guaranteed to be unique within a Cantera problem.
244  //! @param k Species index within the phase
245  //! @return The "phaseName:speciesName" string
246  std::string speciesSPName(int k) const;
247 
248  //! Return a const reference to the vector of species names
249  const std::vector<std::string>& speciesNames() const;
250 
251  /// Returns the number of species in the phase
252  size_t nSpecies() const {
253  return m_kk;
254  }
255 
256  //! Check that the specified species index is in range
257  //! Throws an exception if k is greater than nSpecies()-1
258  void checkSpeciesIndex(size_t k) const;
259 
260  //! Check that an array size is at least nSpecies()
261  //! Throws an exception if kk is less than nSpecies(). Used before calls
262  //! which take an array pointer.
263  void checkSpeciesArraySize(size_t kk) const;
264 
265 
266  //!@} end group Element and Species Information
267 
268  //! Save the current internal state of the phase
269  //! Write to vector 'state' the current internal state.
270  //! @param state output vector. Will be resized to nSpecies() + 2.
271  void saveState(vector_fp& state) const;
272 
273  //! Write to array 'state' the current internal state.
274  //! @param lenstate length of the state array. Must be >= nSpecies()+2
275  //! @param state output vector. Must be of length nSpecies() + 2 or
276  //! greater.
277  void saveState(size_t lenstate, doublereal* state) const;
278 
279  //! Restore a state saved on a previous call to saveState.
280  //! @param state State vector containing the previously saved state.
281  void restoreState(const vector_fp& state);
282 
283  //! Restore the state of the phase from a previously saved state vector.
284  //! @param lenstate Length of the state vector
285  //! @param state Vector of state conditions.
286  void restoreState(size_t lenstate, const doublereal* state);
287 
288  /*! @name Set thermodynamic state
289  * Set the internal thermodynamic state by setting the internally stored
290  * temperature, density and species composition. Note that the composition
291  * is always set first.
292  *
293  * Temperature and density are held constant if not explicitly set.
294  */
295  //!@{
296 
297  //! Set the species mole fractions by name.
298  //! @param xMap map from species names to mole fraction values.
299  //! Species not listed by name in \c xMap are set to zero.
301 
302  //! Set the mole fractions of a group of species by name. Species which
303  //! are not listed by name in the composition map are set to zero.
304  //! @param x string x in the form of a composition map
305  void setMoleFractionsByName(const std::string& x);
306 
307  //! Set the species mass fractions by name.
308  //! @param yMap map from species names to mass fraction values.
309  //! Species not listed by name in \c yMap are set to zero.
311 
312  //! Set the species mass fractions by name.
313  //! Species not listed by name in \c x are set to zero.
314  //! @param x String containing a composition map
315  void setMassFractionsByName(const std::string& x);
316 
317  //! Set the internally stored temperature (K), density, and mole fractions.
318  //! @param t Temperature in kelvin
319  //! @param dens Density (kg/m^3)
320  //! @param x vector of species mole fractions, length m_kk
321  void setState_TRX(doublereal t, doublereal dens, const doublereal* x);
322 
323  //! Set the internally stored temperature (K), density, and mole fractions.
324  //! @param t Temperature in kelvin
325  //! @param dens Density (kg/m^3)
326  //! @param x Composition Map containing the mole fractions.
327  //! Species not included in the map are assumed to have
328  //! a zero mole fraction.
329  void setState_TRX(doublereal t, doublereal dens, compositionMap& x);
330 
331  //! Set the internally stored temperature (K), density, and mass fractions.
332  //! @param t Temperature in kelvin
333  //! @param dens Density (kg/m^3)
334  //! @param y vector of species mass fractions, length m_kk
335  void setState_TRY(doublereal t, doublereal dens, const doublereal* y);
336 
337  //! Set the internally stored temperature (K), density, and mass fractions.
338  //! @param t Temperature in kelvin
339  //! @param dens Density (kg/m^3)
340  //! @param y Composition Map containing the mass fractions.
341  //! Species not included in the map are assumed to have
342  //! a zero mass fraction.
343  void setState_TRY(doublereal t, doublereal dens, compositionMap& y);
344 
345  //! Set the internally stored temperature (K), molar density (kmol/m^3), and mole fractions.
346  //! @param t Temperature in kelvin
347  //! @param n molar density (kmol/m^3)
348  //! @param x vector of species mole fractions, length m_kk
349  void setState_TNX(doublereal t, doublereal n, const doublereal* x);
350 
351  //! Set the internally stored temperature (K) and density (kg/m^3)
352  //! @param t Temperature in kelvin
353  //! @param rho Density (kg/m^3)
354  void setState_TR(doublereal t, doublereal rho);
355 
356  //! Set the internally stored temperature (K) and mole fractions.
357  //! @param t Temperature in kelvin
358  //! @param x vector of species mole fractions, length m_kk
359  void setState_TX(doublereal t, doublereal* x);
360 
361  //! Set the internally stored temperature (K) and mass fractions.
362  //! @param t Temperature in kelvin
363  //! @param y vector of species mass fractions, length m_kk
364  void setState_TY(doublereal t, doublereal* y);
365 
366  //! Set the density (kg/m^3) and mole fractions.
367  //! @param rho Density (kg/m^3)
368  //! @param x vector of species mole fractions, length m_kk
369  void setState_RX(doublereal rho, doublereal* x);
370 
371  //! Set the density (kg/m^3) and mass fractions.
372  //! @param rho Density (kg/m^3)
373  //! @param y vector of species mass fractions, length m_kk
374  void setState_RY(doublereal rho, doublereal* y);
375 
376  //!@} end group set thermo state
377 
378  //! Molecular weight of species \c k.
379  //! @param k index of species \c k
380  //! @return Returns the molecular weight of species \c k.
381  doublereal molecularWeight(size_t k) const;
382 
383  //! Return the Molar mass of species \c k
384  //! Alternate name for molecular weight.
385  //! @param k index for species
386  //! @return Return the molar mass of species k kg/kmol.
387  //! @deprecated use molecularWeight instead
388  doublereal molarMass(size_t k) const {
389  return molecularWeight(k);
390  }
391 
392  //! Copy the vector of molecular weights into vector weights.
393  //! @param weights Output vector of molecular weights (kg/kmol)
394  void getMolecularWeights(vector_fp& weights) const;
395 
396  //! Copy the vector of molecular weights into array weights.
397  //! @param iwt Unused.
398  //! @param weights Output array of molecular weights (kg/kmol)
399  //! @deprecated
400  DEPRECATED(void getMolecularWeights(int iwt, doublereal* weights) const);
401 
402  //! Copy the vector of molecular weights into array weights.
403  //! @param weights Output array of molecular weights (kg/kmol)
404  void getMolecularWeights(doublereal* weights) const;
405 
406  //! Return a const reference to the internal vector of molecular weights.
407  //! units = kg / kmol
408  const vector_fp& molecularWeights() const;
409 
410  //! This routine returns the size of species k
411  //! @param k index of the species
412  //! @return The size of the species. Units are meters.
413  doublereal size(size_t k) const {
414  return m_speciesSize[k];
415  }
416 
417  /// @name Composition
418  //@{
419 
420  //! Get the mole fractions by name.
421  //! @param[out] x composition map containing the species mole fractions.
422  void getMoleFractionsByName(compositionMap& x) const;
423 
424  //! Return the mole fraction of a single species
425  //! @param k species index
426  //! @return Mole fraction of the species
427  doublereal moleFraction(size_t k) const;
428 
429  //! Return the mole fraction of a single species
430  //! @param name String name of the species
431  //! @return Mole fraction of the species
432  doublereal moleFraction(std::string name) const;
433 
434  //! Return the mass fraction of a single species
435  //! @param k species index
436  //! @return Mass fraction of the species
437  doublereal massFraction(size_t k) const;
438 
439  //! Return the mass fraction of a single species
440  //! @param name String name of the species
441  //! @return Mass Fraction of the species
442  doublereal massFraction(std::string name) const;
443 
444  //! Get the species mole fraction vector.
445  //! @param x On return, x contains the mole fractions. Must have a
446  //! length greater than or equal to the number of species.
447  void getMoleFractions(doublereal* const x) const;
448 
449  //! Set the mole fractions to the specified values
450  //! There is no restriction on the sum of the mole fraction vector.
451  //! Internally, the Phase object will normalize this vector before storing
452  //! its contents.
453  //! @param x Array of unnormalized mole fraction values (input). Must
454  //! have a length greater than or equal to the number of species, m_kk.
455  virtual void setMoleFractions(const doublereal* const x);
456 
457  //! Set the mole fractions to the specified values without normalizing.
458  //! This is useful when the normalization condition is being handled by
459  //! some other means, for example by a constraint equation as part of a
460  //! larger set of equations.
461  //! @param x Input vector of mole fractions. Length is m_kk.
462  virtual void setMoleFractions_NoNorm(const doublereal* const x);
463 
464  //! Get the species mass fractions.
465  //! @param[out] y Array of mass fractions, length nSpecies()
466  void getMassFractions(doublereal* const y) const;
467 
468  //! Return a const pointer to the mass fraction array
469  const doublereal* massFractions() const {
470  return &m_y[0];
471  }
472 
473  //! Set the mass fractions to the specified values and normalize them.
474  //! @param[in] y Array of unnormalized mass fraction values. Length
475  //! must be greater than or equal to the number of species. The Ptate
476  //! object will normalize this vector before storing its contents.
477  virtual void setMassFractions(const doublereal* const y);
478 
479  //! Set the mass fractions to the specified values without normalizing.
480  //! This is useful when the normalization condition is being handled by
481  //! some other means, for example by a constraint equation as part of a
482  //! larger set of equations.
483  //! @param y Input vector of mass fractions. Length is m_kk.
484  virtual void setMassFractions_NoNorm(const doublereal* const y);
485 
486  //! Get the species concentrations (kmol/m^3).
487  //! @param[out] c Array of species concentrations Length must be
488  //! greater than or equal to the number of species.
489  void getConcentrations(doublereal* const c) const;
490 
491  //! Concentration of species k.
492  //! If k is outside the valid range, an exception will be thrown.
493  //! @param k Index of species
494  doublereal concentration(const size_t k) const;
495 
496  //! Set the concentrations to the specified values within the phase.
497  //! We set the concentrations here and therefore we set the overall density
498  //! of the phase. We hold the temperature constant during this operation.
499  //! Therefore, we have possibly changed the pressure of the phase by
500  //! calling this routine.
501  //! @param[in] conc Array of concentrations in dimensional units. For
502  //! bulk phases c[k] is the concentration of the kth species in kmol/m3.
503  //! For surface phases, c[k] is the concentration in kmol/m2. The length of
504  //! the vector is the numberof species in the phase.
505  virtual void setConcentrations(const doublereal* const conc);
506 
507  //! Returns a const pointer to the start of the moleFraction/MW array.
508  //! This array is the array of mole fractions, each divided by the mean
509  //! molecular weight.
510  const doublereal* moleFractdivMMW() const;
511 
512  //@}
513 
514  //! Dimensionless electrical charge of a single molecule of species k
515  //! The charge is normalized by the the magnitude of the electron charge
516  //! @param k species index
517  doublereal charge(size_t k) const;
518 
519  //! Charge density [C/m^3].
520  doublereal chargeDensity() const;
521 
522  //! Returns the number of spatial dimensions (1, 2, or 3)
523  size_t nDim() const {
524  return m_ndim;
525  }
526 
527  //! Set the number of spatial dimensions (1, 2, or 3). The number of
528  //! spatial dimensions is used for vector involving directions.
529  //! @param ndim Input number of dimensions.
530  void setNDim(size_t ndim) {
531  m_ndim = ndim;
532  }
533 
534  //! @name Thermodynamic Properties
535  //!@{
536 
537  //! Temperature (K).
538  //! @return The temperature of the phase
539  doublereal temperature() const {
540  return m_temp;
541  }
542 
543  //! Density (kg/m^3).
544  //! @return The density of the phase
545  virtual doublereal density() const {
546  return m_dens;
547  }
548 
549  //! Molar density (kmol/m^3).
550  //! @return The molar density of the phase
551  doublereal molarDensity() const;
552 
553  //! Molar volume (m^3/kmol).
554  //! @return The molar volume of the phase
555  doublereal molarVolume() const;
556 
557  //! Set the internally stored density (kg/m^3) of the phase
558  //! Note the density of a phase is an independent variable.
559  //! @param[in] density density (kg/m^3).
560  virtual void setDensity(const doublereal density) {
561  m_dens = density;
562  }
563 
564  //! Set the internally stored molar density (kmol/m^3) of the phase.
565  //! @param[in] molarDensity Input molar density (kmol/m^3).
566  virtual void setMolarDensity(const doublereal molarDensity);
567 
568  //! Set the internally stored temperature of the phase (K).
569  //! @param temp Temperature in Kelvin
570  virtual void setTemperature(const doublereal temp) {
571  m_temp = temp;
572  }
573  //@}
574 
575  //! @name Mean Properties
576  //!@{
577 
578  //! Evaluate the mole-fraction-weighted mean of an array Q.
579  //! \f[ \sum_k X_k Q_k. \f]
580  //! Q should contain pure-species molar property values.
581  //! @param[in] Q Array of length m_kk that is to be averaged.
582  //! @return mole-fraction-weighted mean of Q
583  doublereal mean_X(const doublereal* const Q) const;
584 
585  //! Evaluate the mass-fraction-weighted mean of an array Q.
586  //! \f[ \sum_k Y_k Q_k \f]
587  //! @param[in] Q Array of species property values in mass units.
588  //! @return The mass-fraction-weighted mean of Q.
589  doublereal mean_Y(const doublereal* const Q) const;
590 
591  //! The mean molecular weight. Units: (kg/kmol)
592  doublereal meanMolecularWeight() const {
593  return m_mmw;
594  }
595 
596  //! Evaluate \f$ \sum_k X_k \log X_k \f$.
597  //! @return The indicated sum. Dimensionless.
598  doublereal sum_xlogx() const;
599 
600  //! Evaluate \f$ \sum_k X_k \log Q_k \f$.
601  //! @param Q Vector of length m_kk to take the log average of
602  //! @return The indicated sum.
603  doublereal sum_xlogQ(doublereal* const Q) const;
604  //@}
605 
606  //! @name Adding Elements and Species
607  //! These methods are used to add new elements or species. These are not
608  //! usually called by user programs.
609  //!
610  //! Since species are checked to insure that they are only composed of
611  //! declared elements, it is necessary to first add all elements before
612  //! adding any species.
613  //!@{
614 
615  //! Add an element.
616  //! @param symbol Atomic symbol std::string.
617  //! @param weight Atomic mass in amu.
618  void addElement(const std::string& symbol, doublereal weight=-12345.0);
619 
620  //! Add an element from an XML specification.
621  //! @param e Reference to the XML_Node where the element is described.
622  void addElement(const XML_Node& e);
623 
624  //! Add an element, checking for uniqueness
625  //! The uniqueness is checked by comparing the string symbol. If not
626  //! unique, nothing is done.
627  //! @param symbol String symbol of the element
628  //! @param weight Atomic weight of the element (kg kmol-1).
629  //! @param atomicNumber Atomic number of the element (unitless)
630  //! @param entropy298 Entropy of the element at 298 K and 1 bar in its
631  //! most stable form. The default is the value ENTROPY298_UNKNOWN, which is
632  //! interpreted as an unknown, and if used will cause %Cantera to throw an
633  //! error.
634  //! @param elem_type Specifies the type of the element constraint
635  //! equation. This defaults to CT_ELEM_TYPE_ABSPOS, i.e., an element.
636  void addUniqueElement(const std::string& symbol, doublereal weight=-12345.0,
637  int atomicNumber = 0,
638  doublereal entropy298 = ENTROPY298_UNKNOWN,
639  int elem_type = CT_ELEM_TYPE_ABSPOS);
640 
641  //! Add an element, checking for uniqueness
642  //! The uniqueness is checked by comparing the string symbol. If not unique,
643  //! nothing is done.
644  //! @param e Reference to the XML_Node where the element is described.
645  void addUniqueElement(const XML_Node& e);
646 
647  //! Add all elements referenced in an XML_Node tree
648  //! @param phase Reference to the root XML_Node of a phase
649  void addElementsFromXML(const XML_Node& phase);
650 
651  //! Prohibit addition of more elements, and prepare to add species.
652  void freezeElements();
653 
654  //! True if freezeElements has been called.
655  bool elementsFrozen();
656 
657  //! Add an element after elements have been frozen, checking for uniqueness
658  //! The uniqueness is checked by comparing the string symbol. If not
659  //! unique, nothing is done.
660  //! @param symbol String symbol of the element
661  //! @param weight Atomic weight of the element (kg kmol-1).
662  //! @param atomicNumber Atomic number of the element (unitless)
663  //! @param entropy298 Entropy of the element at 298 K and 1 bar in its
664  //! most stable form. The default is the value ENTROPY298_UNKNOWN, which
665  //! if used will cause Cantera to throw an error.
666  //! @param elem_type Specifies the type of the element constraint
667  //! equation. This defaults to CT_ELEM_TYPE_ABSPOS, i.e., an element.
668  size_t addUniqueElementAfterFreeze(const std::string& symbol,
669  doublereal weight, int atomicNumber,
670  doublereal entropy298 = ENTROPY298_UNKNOWN,
671  int elem_type = CT_ELEM_TYPE_ABSPOS);
672 
673  void addSpecies(const std::string& name, const doublereal* comp,
674  doublereal charge = 0.0, doublereal size = 1.0);
675 
676  //! Add a species to the phase, checking for uniqueness of the name
677  //! This routine checks for uniqueness of the string name. It only adds the
678  //! species if it is unique.
679  //! @param name String name of the species
680  //! @param comp Array containing the elemental composition of the
681  //! species.
682  //! @param charge Charge of the species. Defaults to zero.
683  //! @param size Size of the species (meters). Defaults to 1 meter.
684  void addUniqueSpecies(const std::string& name, const doublereal* comp,
685  doublereal charge = 0.0,
686  doublereal size = 1.0);
687  //!@} end group adding species and elements
688 
689  //! Call when finished adding species.
690  //! Prepare to use them for calculation of mixture properties.
691  virtual void freezeSpecies();
692 
693  //! True if freezeSpecies has been called.
694  bool speciesFrozen() {
695  return m_speciesFrozen;
696  }
697 
698  virtual bool ready() const;
699 
700  //! Return the State Mole Fraction Number
701  DEPRECATED(int stateMFNumber() const) {
702  return m_stateNum;
703  }
704 
705  //! Every time the mole fractions have changed, this routine will increment
706  //! the stateMFNumber
707  //! @param forceChange If this is true then the stateMFNumber always
708  //! changes. This defaults to false.
709  //! @deprecated
710  DEPRECATED(void stateMFChangeCalc(bool forceChange = false));
711 
712 protected:
713  //! @internal Initialize.
714  //! Make a local copy of the vector of molecular weights, and resize the
715  //! composition arrays to the appropriate size.
716  //! @param mw Vector of molecular weights of the species.
717  void init(const vector_fp& mw);
718 
719  //! Set the molecular weight of a single species to a given value
720  //! @param k id of the species
721  //! @param mw Molecular Weight (kg kmol-1)
722  void setMolecularWeight(const int k, const double mw) {
723  m_molwts[k] = mw;
724  m_rmolwts[k] = 1.0/mw;
725  }
726 
727  size_t m_kk; //!< Number of species in the phase.
728 
729  //! Dimensionality of the phase. Volumetric phases have dimensionality 3
730  //! and surface phases have dimensionality 2.
731  size_t m_ndim;
732 
733  //! Atomic composition of the species. The number of atoms of element i
734  //! in species k is equal to m_speciesComp[k * m_mm + i]
735  //! The length of this vector is equal to m_kk * m_mm
737 
738  //!Vector of species sizes. length m_kk. Used in some equations of state
739  //! which employ the constant partial molar volume approximation.
741 
742  vector_fp m_speciesCharge; //!< Vector of species charges. length m_kk.
743 private:
744 
745  XML_Node* m_xml; //!< XML node containing the XML info for this phase
746 
747  //! ID of the phase. This is the value of the ID attribute of the XML
748  //! phase node. The field will stay that way even if the name is changed.
749  std::string m_id;
750 
751  //! Name of the phase.
752  //! Initially, this is the value of the ID attribute of the XML phase node.
753  //! It may be changed to another value during the course of a calculation.
754  std::string m_name;
755 
756  doublereal m_temp; //!< Temperature (K). This is an independent variable
757 
758  //! Density (kg m-3). This is an independent variable except in the
759  //! incompressible degenerate case. Thus, the pressure is determined from
760  //! this variable rather than other way round.
761  doublereal m_dens;
762 
763  doublereal m_mmw; //!< mean molecular weight of the mixture (kg kmol-1)
764 
765  //! m_ym[k] = mole fraction of species k divided by the mean molecular
766  //! weight of mixture.
767  mutable vector_fp m_ym;
768 
769  mutable vector_fp m_y; //!< species mass fractions
770 
771  vector_fp m_molwts; //!< species molecular weights (kg kmol-1)
772 
773  vector_fp m_rmolwts; //!< inverse of species molecular weights (kmol kg-1)
774 
775  //! State Change variable. Whenever the mole fraction vector changes,
776  //! this int is incremented.
777  //! @deprecated
779 
780  //! Boolean indicating whether the number of species has been frozen.
781  //! During the construction of the phase, this is false. After construction
782  //! of the the phase, this is true.
784 
785  //! If this is true, then no elements may be added to the object.
787 
788  //! Vector of the species names
789  std::vector<std::string> m_speciesNames;
790 
791  size_t m_mm; //!< Number of elements.
792  vector_fp m_atomicWeights; //!< element atomic weights (kg kmol-1)
793  vector_int m_atomicNumbers; //!< element atomic numbers
794  std::vector<std::string> m_elementNames; //!< element names
795  vector_int m_elem_type; //!< Vector of element types
796 
797  //! Entropy at 298.15 K and 1 bar of stable state pure elements (J kmol-1)
799 
800 };
801 
802 }
803 
804 #endif