Cantera  2.1.2
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(const 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(const 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(const 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(const 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(const 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  //! Species not listed by name in \c xMap are set to zero.
299  //! @param xMap map from species names to mole fraction values.
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  //! Species not listed by name in \c yMap are set to zero.
309  //! @param yMap map from species names to mass fraction values.
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  //! Copy the vector of molecular weights into vector weights.
384  //! @param weights Output vector of molecular weights (kg/kmol)
385  void getMolecularWeights(vector_fp& weights) const;
386 
387  //! Copy the vector of molecular weights into array weights.
388  //! @param weights Output array of molecular weights (kg/kmol)
389  void getMolecularWeights(doublereal* weights) const;
390 
391  //! Return a const reference to the internal vector of molecular weights.
392  //! units = kg / kmol
393  const vector_fp& molecularWeights() const;
394 
395  //! This routine returns the size of species k
396  //! @param k index of the species
397  //! @return The size of the species. Units are meters.
398  doublereal size(size_t k) const {
399  return m_speciesSize[k];
400  }
401 
402  /// @name Composition
403  //@{
404 
405  //! Get the mole fractions by name.
406  //! @param[out] x composition map containing the species mole fractions.
407  void getMoleFractionsByName(compositionMap& x) const;
408 
409  //! Return the mole fraction of a single species
410  //! @param k species index
411  //! @return Mole fraction of the species
412  doublereal moleFraction(size_t k) const;
413 
414  //! Return the mole fraction of a single species
415  //! @param name String name of the species
416  //! @return Mole fraction of the species
417  doublereal moleFraction(const std::string& name) const;
418 
419  //! Return the mass fraction of a single species
420  //! @param k species index
421  //! @return Mass fraction of the species
422  doublereal massFraction(size_t k) const;
423 
424  //! Return the mass fraction of a single species
425  //! @param name String name of the species
426  //! @return Mass Fraction of the species
427  doublereal massFraction(const std::string& name) const;
428 
429  //! Get the species mole fraction vector.
430  //! @param x On return, x contains the mole fractions. Must have a
431  //! length greater than or equal to the number of species.
432  void getMoleFractions(doublereal* const x) const;
433 
434  //! Set the mole fractions to the specified values
435  //! There is no restriction on the sum of the mole fraction vector.
436  //! Internally, the Phase object will normalize this vector before storing
437  //! its contents.
438  //! @param x Array of unnormalized mole fraction values (input). Must
439  //! have a length greater than or equal to the number of species, m_kk.
440  virtual void setMoleFractions(const doublereal* const x);
441 
442  //! Set the mole fractions to the specified values without normalizing.
443  //! This is useful when the normalization condition is being handled by
444  //! some other means, for example by a constraint equation as part of a
445  //! larger set of equations.
446  //! @param x Input vector of mole fractions. Length is m_kk.
447  virtual void setMoleFractions_NoNorm(const doublereal* const x);
448 
449  //! Get the species mass fractions.
450  //! @param[out] y Array of mass fractions, length nSpecies()
451  void getMassFractions(doublereal* const y) const;
452 
453  //! Return a const pointer to the mass fraction array
454  const doublereal* massFractions() const {
455  return &m_y[0];
456  }
457 
458  //! Set the mass fractions to the specified values and normalize them.
459  //! @param[in] y Array of unnormalized mass fraction values. Length
460  //! must be greater than or equal to the number of
461  //! species. The Phase object will normalize this vector
462  //! before storing its contents.
463  virtual void setMassFractions(const doublereal* const y);
464 
465  //! Set the mass fractions to the specified values without normalizing.
466  //! This is useful when the normalization condition is being handled by
467  //! some other means, for example by a constraint equation as part of a
468  //! larger set of equations.
469  //! @param y Input vector of mass fractions. Length is m_kk.
470  virtual void setMassFractions_NoNorm(const doublereal* const y);
471 
472  //! Get the species concentrations (kmol/m^3).
473  //! @param[out] c Array of species concentrations Length must be
474  //! greater than or equal to the number of species.
475  void getConcentrations(doublereal* const c) const;
476 
477  //! Concentration of species k.
478  //! If k is outside the valid range, an exception will be thrown.
479  //! @param k Index of species
480  doublereal concentration(const size_t k) const;
481 
482  //! Set the concentrations to the specified values within the phase.
483  //! We set the concentrations here and therefore we set the overall density
484  //! of the phase. We hold the temperature constant during this operation.
485  //! Therefore, we have possibly changed the pressure of the phase by
486  //! calling this routine.
487  //! @param[in] conc Array of concentrations in dimensional units. For
488  //! bulk phases c[k] is the concentration of the kth
489  //! species in kmol/m3. For surface phases, c[k] is the
490  //! concentration in kmol/m2. The length of the vector
491  //! is the number of species in the phase.
492  virtual void setConcentrations(const doublereal* const conc);
493 
494  //! Returns a const pointer to the start of the moleFraction/MW array.
495  //! This array is the array of mole fractions, each divided by the mean
496  //! molecular weight.
497  const doublereal* moleFractdivMMW() const;
498 
499  //@}
500 
501  //! Dimensionless electrical charge of a single molecule of species k
502  //! The charge is normalized by the the magnitude of the electron charge
503  //! @param k species index
504  doublereal charge(size_t k) const {
505  return m_speciesCharge[k];
506  }
507 
508  //! Charge density [C/m^3].
509  doublereal chargeDensity() const;
510 
511  //! Returns the number of spatial dimensions (1, 2, or 3)
512  size_t nDim() const {
513  return m_ndim;
514  }
515 
516  //! Set the number of spatial dimensions (1, 2, or 3). The number of
517  //! spatial dimensions is used for vector involving directions.
518  //! @param ndim Input number of dimensions.
519  void setNDim(size_t ndim) {
520  m_ndim = ndim;
521  }
522 
523  //! @name Thermodynamic Properties
524  //!@{
525 
526  //! Temperature (K).
527  //! @return The temperature of the phase
528  doublereal temperature() const {
529  return m_temp;
530  }
531 
532  //! Density (kg/m^3).
533  //! @return The density of the phase
534  virtual doublereal density() const {
535  return m_dens;
536  }
537 
538  //! Molar density (kmol/m^3).
539  //! @return The molar density of the phase
540  doublereal molarDensity() const;
541 
542  //! Molar volume (m^3/kmol).
543  //! @return The molar volume of the phase
544  doublereal molarVolume() const;
545 
546  //! Set the internally stored density (kg/m^3) of the phase
547  //! Note the density of a phase is an independent variable.
548  //! @param[in] density_ density (kg/m^3).
549  virtual void setDensity(const doublereal density_) {
550  if (density_ <= 0.0) {
551  throw CanteraError("Phase::setDensity()", "density must be positive");
552  }
553  m_dens = density_;
554  }
555 
556  //! Set the internally stored molar density (kmol/m^3) of the phase.
557  //! @param[in] molarDensity Input molar density (kmol/m^3).
558  virtual void setMolarDensity(const doublereal molarDensity);
559 
560  //! Set the internally stored temperature of the phase (K).
561  //! @param temp Temperature in Kelvin
562  virtual void setTemperature(const doublereal temp) {
563  if (temp <= 0) {
564  throw CanteraError("Phase::setTemperature",
565  "temperature must be positive");
566  }
567  m_temp = temp;
568  }
569  //@}
570 
571  //! @name Mean Properties
572  //!@{
573 
574  //! Evaluate the mole-fraction-weighted mean of an array Q.
575  //! \f[ \sum_k X_k Q_k. \f]
576  //! Q should contain pure-species molar property values.
577  //! @param[in] Q Array of length m_kk that is to be averaged.
578  //! @return mole-fraction-weighted mean of Q
579  doublereal mean_X(const doublereal* const Q) const;
580 
581  //! Evaluate the mass-fraction-weighted mean of an array Q.
582  //! \f[ \sum_k Y_k Q_k \f]
583  //! @param[in] Q Array of species property values in mass units.
584  //! @return The mass-fraction-weighted mean of Q.
585  doublereal mean_Y(const doublereal* const Q) const;
586 
587  //! The mean molecular weight. Units: (kg/kmol)
588  doublereal meanMolecularWeight() const {
589  return m_mmw;
590  }
591 
592  //! Evaluate \f$ \sum_k X_k \log X_k \f$.
593  //! @return The indicated sum. Dimensionless.
594  doublereal sum_xlogx() const;
595 
596  //! Evaluate \f$ \sum_k X_k \log Q_k \f$.
597  //! @param Q Vector of length m_kk to take the log average of
598  //! @return The indicated sum.
599  doublereal sum_xlogQ(doublereal* const Q) const;
600  //@}
601 
602  //! @name Adding Elements and Species
603  //! These methods are used to add new elements or species. These are not
604  //! usually called by user programs.
605  //!
606  //! Since species are checked to insure that they are only composed of
607  //! declared elements, it is necessary to first add all elements before
608  //! adding any species.
609  //!@{
610 
611  //! Add an element.
612  //! @param symbol Atomic symbol std::string.
613  //! @param weight Atomic mass in amu.
614  void addElement(const std::string& symbol, doublereal weight=-12345.0);
615 
616  //! Add an element from an XML specification.
617  //! @param e Reference to the XML_Node where the element is described.
618  void addElement(const XML_Node& e);
619 
620  //! Add an element, checking for uniqueness
621  //! The uniqueness is checked by comparing the string symbol. If not
622  //! unique, nothing is done.
623  //! @param symbol String symbol of the element
624  //! @param weight Atomic weight of the element (kg kmol-1).
625  //! @param atomicNumber Atomic number of the element (unitless)
626  //! @param entropy298 Entropy of the element at 298 K and 1 bar in its
627  //! most stable form. The default is the value ENTROPY298_UNKNOWN, which is
628  //! interpreted as an unknown, and if used will cause %Cantera to throw an
629  //! error.
630  //! @param elem_type Specifies the type of the element constraint
631  //! equation. This defaults to CT_ELEM_TYPE_ABSPOS, i.e., an element.
632  void addUniqueElement(const std::string& symbol, doublereal weight=-12345.0,
633  int atomicNumber = 0,
634  doublereal entropy298 = ENTROPY298_UNKNOWN,
635  int elem_type = CT_ELEM_TYPE_ABSPOS);
636 
637  //! Add an element, checking for uniqueness
638  //! The uniqueness is checked by comparing the string symbol. If not unique,
639  //! nothing is done.
640  //! @param e Reference to the XML_Node where the element is described.
641  void addUniqueElement(const XML_Node& e);
642 
643  //! Add all elements referenced in an XML_Node tree
644  //! @param phase Reference to the root XML_Node of a phase
645  void addElementsFromXML(const XML_Node& phase);
646 
647  //! Prohibit addition of more elements, and prepare to add species.
648  void freezeElements();
649 
650  //! True if freezeElements has been called.
651  bool elementsFrozen();
652 
653  //! Add an element after elements have been frozen, checking for uniqueness
654  //! The uniqueness is checked by comparing the string symbol. If not
655  //! unique, nothing is done.
656  //! @param symbol String symbol of the element
657  //! @param weight Atomic weight of the element (kg kmol-1).
658  //! @param atomicNumber Atomic number of the element (unitless)
659  //! @param entropy298 Entropy of the element at 298 K and 1 bar in its
660  //! most stable form. The default is the value ENTROPY298_UNKNOWN, which
661  //! if used will cause Cantera to throw an error.
662  //! @param elem_type Specifies the type of the element constraint
663  //! equation. This defaults to CT_ELEM_TYPE_ABSPOS, i.e., an element.
664  size_t addUniqueElementAfterFreeze(const std::string& symbol,
665  doublereal weight, int atomicNumber,
666  doublereal entropy298 = ENTROPY298_UNKNOWN,
667  int elem_type = CT_ELEM_TYPE_ABSPOS);
668 
669  void addSpecies(const std::string& name, const doublereal* comp,
670  doublereal charge = 0.0, doublereal size = 1.0);
671 
672  //! Add a species to the phase, checking for uniqueness of the name
673  //! This routine checks for uniqueness of the string name. It only adds the
674  //! species if it is unique.
675  //! @param name String name of the species
676  //! @param comp Array containing the elemental composition of the
677  //! species.
678  //! @param charge Charge of the species. Defaults to zero.
679  //! @param size Size of the species (meters). Defaults to 1 meter.
680  void addUniqueSpecies(const std::string& name, const doublereal* comp,
681  doublereal charge = 0.0,
682  doublereal size = 1.0);
683  //!@} end group adding species and elements
684 
685  //! Call when finished adding species.
686  //! Prepare to use them for calculation of mixture properties.
687  virtual void freezeSpecies();
688 
689  //! True if freezeSpecies has been called.
690  bool speciesFrozen() {
691  return m_speciesFrozen;
692  }
693 
694  virtual bool ready() const;
695 
696  //! Return the State Mole Fraction Number
697  int stateMFNumber() const {
698  return m_stateNum;
699  }
700 
701 protected:
702  //! @internal Initialize.
703  //! Make a local copy of the vector of molecular weights, and resize the
704  //! composition arrays to the appropriate size.
705  //! @param mw Vector of molecular weights of the species.
706  void init(const vector_fp& mw);
707 
708  //! Set the molecular weight of a single species to a given value
709  //! @param k id of the species
710  //! @param mw Molecular Weight (kg kmol-1)
711  void setMolecularWeight(const int k, const double mw) {
712  m_molwts[k] = mw;
713  m_rmolwts[k] = 1.0/mw;
714  }
715 
716  size_t m_kk; //!< Number of species in the phase.
717 
718  //! Dimensionality of the phase. Volumetric phases have dimensionality 3
719  //! and surface phases have dimensionality 2.
720  size_t m_ndim;
721 
722  //! Atomic composition of the species. The number of atoms of element i
723  //! in species k is equal to m_speciesComp[k * m_mm + i]
724  //! The length of this vector is equal to m_kk * m_mm
726 
727  //!Vector of species sizes. length m_kk. Used in some equations of state
728  //! which employ the constant partial molar volume approximation.
730 
731  vector_fp m_speciesCharge; //!< Vector of species charges. length m_kk.
732 
733 private:
734  XML_Node* m_xml; //!< XML node containing the XML info for this phase
735 
736  //! ID of the phase. This is the value of the ID attribute of the XML
737  //! phase node. The field will stay that way even if the name is changed.
738  std::string m_id;
739 
740  //! Name of the phase.
741  //! Initially, this is the value of the ID attribute of the XML phase node.
742  //! It may be changed to another value during the course of a calculation.
743  std::string m_name;
744 
745  doublereal m_temp; //!< Temperature (K). This is an independent variable
746 
747  //! Density (kg m-3). This is an independent variable except in the
748  //! incompressible degenerate case. Thus, the pressure is determined from
749  //! this variable rather than other way round.
750  doublereal m_dens;
751 
752  doublereal m_mmw; //!< mean molecular weight of the mixture (kg kmol-1)
753 
754  //! m_ym[k] = mole fraction of species k divided by the mean molecular
755  //! weight of mixture.
756  mutable vector_fp m_ym;
757 
758  mutable vector_fp m_y; //!< species mass fractions
759 
760  vector_fp m_molwts; //!< species molecular weights (kg kmol-1)
761 
762  vector_fp m_rmolwts; //!< inverse of species molecular weights (kmol kg-1)
763 
764  //! State Change variable. Whenever the mole fraction vector changes,
765  //! this int is incremented.
767 
768  //! Boolean indicating whether the number of species has been frozen.
769  //! During the construction of the phase, this is false. After construction
770  //! of the the phase, this is true.
772 
773  //! If this is true, then no elements may be added to the object.
775 
776  //! Vector of the species names
777  std::vector<std::string> m_speciesNames;
778 
779  size_t m_mm; //!< Number of elements.
780  vector_fp m_atomicWeights; //!< element atomic weights (kg kmol-1)
781  vector_int m_atomicNumbers; //!< element atomic numbers
782  std::vector<std::string> m_elementNames; //!< element names
783  vector_int m_elem_type; //!< Vector of element types
784 
785  //! Entropy at 298.15 K and 1 bar of stable state pure elements (J kmol-1)
787 
788 };
789 
790 }
791 
792 #endif
Phase()
Default constructor.
Definition: Phase.cpp:18
std::map< std::string, doublereal > compositionMap
Map connecting a string name with a double.
Definition: ct_defs.h:162
doublereal nAtoms(size_t k, size_t m) const
Number of atoms of element m in species k.
Definition: Phase.cpp:215
virtual void setMoleFractions_NoNorm(const doublereal *const x)
Set the mole fractions to the specified values without normalizing.
Definition: Phase.cpp:344
doublereal molecularWeight(size_t k) const
Molecular weight of species k.
Definition: Phase.cpp:484
vector_fp m_y
species mass fractions
Definition: Phase.h:758
void setState_RY(doublereal rho, doublereal *y)
Set the density (kg/m^3) and mass fractions.
Definition: Phase.cpp:478
std::vector< std::string > m_elementNames
element names
Definition: Phase.h:782
virtual doublereal density() const
Density (kg/m^3).
Definition: Phase.h:534
void restoreState(const vector_fp &state)
Restore a state saved on a previous call to saveState.
Definition: Phase.cpp:289
bool m_elementsFrozen
If this is true, then no elements may be added to the object.
Definition: Phase.h:774
CTML ("Cantera Markup Language") is the variant of XML that Cantera uses to store data...
void setState_TRY(doublereal t, doublereal dens, const doublereal *y)
Set the internally stored temperature (K), density, and mass fractions.
Definition: Phase.cpp:440
size_t nElements() const
Number of elements.
Definition: Phase.cpp:139
vector_fp m_atomicWeights
element atomic weights (kg kmol-1)
Definition: Phase.h:780
void getMassFractions(doublereal *const y) const
Get the species mass fractions.
Definition: Phase.cpp:561
vector_fp m_speciesCharge
Vector of species charges. length m_kk.
Definition: Phase.h:731
vector_fp m_entropy298
Entropy at 298.15 K and 1 bar of stable state pure elements (J kmol-1)
Definition: Phase.h:786
doublereal atomicWeight(size_t m) const
Atomic weight of element m.
Definition: Phase.cpp:179
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:100
doublereal size(size_t k) const
This routine returns the size of species k.
Definition: Phase.h:398
Contains the LookupWtElements function and the definitions of element constraint types.
vector_fp m_speciesSize
Vector of species sizes.
Definition: Phase.h:729
doublereal m_mmw
mean molecular weight of the mixture (kg kmol-1)
Definition: Phase.h:752
Base class for phases of matter.
Definition: Phase.h:98
void addElement(const std::string &symbol, doublereal weight=-12345.0)
Add an element.
Definition: Phase.cpp:643
#define CT_ELEM_TYPE_ABSPOS
Normal element constraint consisting of positive coefficients for the formula matrix.
Definition: Elements.h:35
int stateMFNumber() const
Return the State Mole Fraction Number.
Definition: Phase.h:697
doublereal molarDensity() const
Molar density (kmol/m^3).
Definition: Phase.cpp:597
void getMoleFractionsByName(compositionMap &x) const
Get the mole fractions by name.
Definition: Phase.cpp:510
void getMoleFractions(doublereal *const x) const
Get the species mole fraction vector.
Definition: Phase.cpp:519
void checkElementIndex(size_t m) const
Check that the specified element index is in range Throws an exception if m is greater than nElements...
Definition: Phase.cpp:144
void getConcentrations(doublereal *const c) const
Get the species concentrations (kmol/m^3).
Definition: Phase.cpp:572
std::string name() const
Return the name of the phase.
Definition: Phase.cpp:129
std::string m_name
Name of the phase.
Definition: Phase.h:743
void checkElementArraySize(size_t mm) const
Check that an array size is at least nElements() Throws an exception if mm is less than nElements()...
Definition: Phase.cpp:151
doublereal concentration(const size_t k) const
Concentration of species k.
Definition: Phase.cpp:566
doublereal sum_xlogQ(doublereal *const Q) const
Evaluate .
Definition: Phase.cpp:638
doublereal entropyElement298(size_t m) const
Entropy of the element in its standard state at 298 K and 1 bar.
Definition: Phase.cpp:184
void checkSpeciesIndex(size_t k) const
Check that the specified species index is in range Throws an exception if k is greater than nSpecies(...
Definition: Phase.cpp:257
void setName(const std::string &nm)
Sets the string name for the phase.
Definition: Phase.cpp:134
doublereal mean_X(const doublereal *const Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
Definition: Phase.cpp:623
std::vector< int > vector_int
Vector of ints.
Definition: ct_defs.h:167
const vector_fp & atomicWeights() const
Return a read-only reference to the vector of atomic weights.
Definition: Phase.cpp:193
void setMassFractionsByName(compositionMap &yMap)
Set the species mass fractions by name.
Definition: Phase.cpp:399
int m_stateNum
State Change variable.
Definition: Phase.h:766
size_t m_ndim
Dimensionality of the phase.
Definition: Phase.h:720
size_t speciesIndex(const std::string &name) const
Returns the index of a species named 'name' within the Phase object.
Definition: Phase.cpp:229
std::vector< std::string > m_speciesNames
Vector of the species names.
Definition: Phase.h:777
void setState_TRX(doublereal t, doublereal dens, const doublereal *x)
Set the internally stored temperature (K), density, and mole fractions.
Definition: Phase.cpp:419
void getAtoms(size_t k, double *atomArray) const
Get a vector containing the atomic composition of species k.
Definition: Phase.cpp:222
bool elementsFrozen()
True if freezeElements has been called.
Definition: Phase.cpp:801
void addUniqueElement(const std::string &symbol, doublereal weight=-12345.0, int atomicNumber=0, doublereal entropy298=ENTROPY298_UNKNOWN, int elem_type=CT_ELEM_TYPE_ABSPOS)
Add an element, checking for uniqueness The uniqueness is checked by comparing the string symbol...
Definition: Phase.cpp:673
doublereal molarVolume() const
Molar volume (m^3/kmol).
Definition: Phase.cpp:607
ElementsFrozen(const std::string &func)
Constructor for class.
Definition: Phase.h:40
doublereal sum_xlogx() const
Evaluate .
Definition: Phase.cpp:633
bool speciesFrozen()
True if freezeSpecies has been called.
Definition: Phase.h:690
int atomicNumber(size_t m) const
Atomic number of element m.
Definition: Phase.cpp:198
doublereal chargeDensity() const
Charge density [C/m^3].
Definition: Phase.cpp:612
const doublereal * massFractions() const
Return a const pointer to the mass fraction array.
Definition: Phase.h:454
std::string id() const
Return the string id for the phase.
Definition: Phase.cpp:119
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:68
void setState_TNX(doublereal t, doublereal n, const doublereal *x)
Set the internally stored temperature (K), molar density (kmol/m^3), and mole fractions.
Definition: Phase.cpp:426
void setMoleFractionsByName(compositionMap &xMap)
Set the species mole fractions by name.
Definition: Phase.cpp:354
void addElementsFromXML(const XML_Node &phase)
Add all elements referenced in an XML_Node tree.
Definition: Phase.cpp:745
virtual void setConcentrations(const doublereal *const conc)
Set the concentrations to the specified values within the phase.
Definition: Phase.cpp:577
virtual void setMoleFractions(const doublereal *const x)
Set the mole fractions to the specified values There is no restriction on the sum of the mole fractio...
Definition: Phase.cpp:306
virtual ~Phase()
Destructor.
Definition: Phase.cpp:106
const std::vector< std::string > & elementNames() const
Return a read-only reference to the vector of element names.
Definition: Phase.cpp:174
doublereal massFraction(size_t k) const
Return the mass fraction of a single species.
Definition: Phase.cpp:545
virtual void setMolarDensity(const doublereal molarDensity)
Set the internally stored molar density (kmol/m^3) of the phase.
Definition: Phase.cpp:602
virtual void freezeSpecies()
Call when finished adding species.
Definition: Phase.cpp:913
#define ENTROPY298_UNKNOWN
Number indicating we don't know the entropy of the element in its most stable state at 298...
Definition: Elements.h:84
virtual void setMassFractions_NoNorm(const doublereal *const y)
Set the mass fractions to the specified values without normalizing.
Definition: Phase.cpp:388
size_t nSpecies() const
Returns the number of species in the phase.
Definition: Phase.h:252
void setNDim(size_t ndim)
Set the number of spatial dimensions (1, 2, or 3).
Definition: Phase.h:519
void setState_TX(doublereal t, doublereal *x)
Set the internally stored temperature (K) and mole fractions.
Definition: Phase.cpp:460
vector_int m_elem_type
Vector of element types.
Definition: Phase.h:783
const std::vector< std::string > & speciesNames() const
Return a const reference to the vector of species names.
Definition: Phase.cpp:252
doublereal moleFraction(size_t k) const
Return the mole fraction of a single species.
Definition: Phase.cpp:524
void setMolecularWeight(const int k, const double mw)
Set the molecular weight of a single species to a given value.
Definition: Phase.h:711
const vector_fp & molecularWeights() const
Return a const reference to the internal vector of molecular weights.
Definition: Phase.cpp:505
bool m_speciesFrozen
Boolean indicating whether the number of species has been frozen.
Definition: Phase.h:771
doublereal temperature() const
Temperature (K).
Definition: Phase.h:528
size_t nDim() const
Returns the number of spatial dimensions (1, 2, or 3)
Definition: Phase.h:512
void freezeElements()
Prohibit addition of more elements, and prepare to add species.
Definition: Phase.cpp:796
vector_fp m_molwts
species molecular weights (kg kmol-1)
Definition: Phase.h:760
void setState_TY(doublereal t, doublereal *y)
Set the internally stored temperature (K) and mass fractions.
Definition: Phase.cpp:466
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Definition: ct_defs.h:165
void checkSpeciesArraySize(size_t kk) const
Check that an array size is at least nSpecies() Throws an exception if kk is less than nSpecies()...
Definition: Phase.cpp:264
size_t elementIndex(const std::string &name) const
Return the index of element named 'name'.
Definition: Phase.cpp:164
Templates for operations on vector-like objects.
Phase & operator=(const Phase &right)
Assignment operator.
Definition: Phase.cpp:54
size_t m_mm
Number of elements.
Definition: Phase.h:779
virtual void setTemperature(const doublereal temp)
Set the internally stored temperature of the phase (K).
Definition: Phase.h:562
std::string m_id
ID of the phase.
Definition: Phase.h:738
doublereal m_dens
Density (kg m-3).
Definition: Phase.h:750
vector_fp m_ym
m_ym[k] = mole fraction of species k divided by the mean molecular weight of mixture.
Definition: Phase.h:756
doublereal meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition: Phase.h:588
XML_Node * m_xml
XML node containing the XML info for this phase.
Definition: Phase.h:734
vector_int m_atomicNumbers
element atomic numbers
Definition: Phase.h:781
std::string speciesSPName(int k) const
Returns the expanded species name of a species, including the phase name This is guaranteed to be uni...
Definition: Phase.cpp:271
void getMolecularWeights(vector_fp &weights) const
Copy the vector of molecular weights into vector weights.
Definition: Phase.cpp:490
std::string elementName(size_t m) const
Name of the element with index m.
Definition: Phase.cpp:158
doublereal m_temp
Temperature (K). This is an independent variable.
Definition: Phase.h:745
void saveState(vector_fp &state) const
Save the current internal state of the phase Write to vector 'state' the current internal state...
Definition: Phase.cpp:277
XML_Node & xml()
Returns a reference to the XML_Node stored for the phase.
Definition: Phase.cpp:114
virtual void setMassFractions(const doublereal *const y)
Set the mass fractions to the specified values and normalize them.
Definition: Phase.cpp:374
int elementType(size_t m) const
Return the element constraint type Possible types include:
Definition: Phase.cpp:203
void init(const vector_fp &mw)
Definition: Phase.cpp:919
vector_fp m_rmolwts
inverse of species molecular weights (kmol kg-1)
Definition: Phase.h:762
vector_fp m_speciesComp
Atomic composition of the species.
Definition: Phase.h:725
void setState_RX(doublereal rho, doublereal *x)
Set the density (kg/m^3) and mole fractions.
Definition: Phase.cpp:472
size_t m_kk
Number of species in the phase.
Definition: Phase.h:716
size_t addUniqueElementAfterFreeze(const std::string &symbol, doublereal weight, int atomicNumber, doublereal entropy298=ENTROPY298_UNKNOWN, int elem_type=CT_ELEM_TYPE_ABSPOS)
Add an element after elements have been frozen, checking for uniqueness The uniqueness is checked by ...
Definition: Phase.cpp:806
int changeElementType(int m, int elem_type)
Change the element type of the mth constraint Reassigns an element type.
Definition: Phase.cpp:208
void setID(const std::string &id)
Set the string id for the phase.
Definition: Phase.cpp:124
void setState_TR(doublereal t, doublereal rho)
Set the internally stored temperature (K) and density (kg/m^3)
Definition: Phase.cpp:454
std::string speciesName(size_t k) const
Name of the species with index k.
Definition: Phase.cpp:246
void addUniqueSpecies(const std::string &name, const doublereal *comp, doublereal charge=0.0, doublereal size=1.0)
Add a species to the phase, checking for uniqueness of the name This routine checks for uniqueness of...
Definition: Phase.cpp:884
const doublereal * moleFractdivMMW() const
Returns a const pointer to the start of the moleFraction/MW array.
Definition: Phase.cpp:540
virtual void setDensity(const doublereal density_)
Set the internally stored density (kg/m^3) of the phase Note the density of a phase is an independent...
Definition: Phase.h:549
doublereal mean_Y(const doublereal *const Q) const
Evaluate the mass-fraction-weighted mean of an array Q.
Definition: Phase.cpp:628
doublereal charge(size_t k) const
Dimensionless electrical charge of a single molecule of species k The charge is normalized by the the...
Definition: Phase.h:504