Cantera  2.1.2
MultiPhaseEquil.h
Go to the documentation of this file.
1 //! @file MultiPhaseEquil.h
2 #ifndef CT_MULTIPHASE_EQUIL
3 #define CT_MULTIPHASE_EQUIL
4 
5 #include "cantera/base/ct_defs.h"
6 #include "MultiPhase.h"
7 
8 namespace Cantera
9 {
10 
11 /*!
12  * Multiphase chemical equilibrium solver. Class MultiPhaseEquil is designed
13  * to be used to set a mixture containing one or more phases to a state of
14  * chemical equilibrium. It implements the VCS algorithm, described in Smith
15  * and Missen, "Chemical Reaction Equilibrium."
16  *
17  * This class only handles chemical equilibrium at a specified temperature and
18  * pressure. To compute equilibrium holding other properties fixed, it is
19  * necessary to iterate on T and P in an "outer" loop, until the specified
20  * properties have the desired values. This is done, for example, in method
21  * equilibrate of class MultiPhase.
22  *
23  * This class is primarily meant to be used internally by the equilibrate
24  * method of class MultiPhase, although there is no reason it cannot be used
25  * directly in application programs if desired.
26  *
27  * @ingroup equil
28  */
30 {
31 public:
32  //! Construct a multiphase equilibrium manager for a multiphase mixture.
33  //! @param mix Pointer to a multiphase mixture object.
34  //! @param start If true, the initial composition will be determined by a
35  //! linear Gibbs minimization, otherwise the initial mixture
36  //! composition will be used.
37  MultiPhaseEquil(MultiPhase* mix, bool start=true, int loglevel = 0);
38 
39  virtual ~MultiPhaseEquil() {}
40 
41  size_t constituent(size_t m) {
42  if (m < m_nel) {
43  return m_order[m];
44  } else {
45  return npos;
46  }
47  }
48 
49  void getStoichVector(size_t rxn, vector_fp& nu) {
50  size_t k;
51  nu.resize(m_nsp, 0.0);
52  if (rxn > nFree()) {
53  return;
54  }
55  for (k = 0; k < m_nsp; k++) {
56  nu[m_order[k]] = m_N(k, rxn);
57  }
58  }
59 
60  int iterations() {
61  return m_iter;
62  }
63 
64  doublereal equilibrate(int XY, doublereal err = 1.0e-9,
65  int maxsteps = 1000, int loglevel=-99);
66  doublereal error();
67 
68 #if defined(WITH_HTML_LOGS)
69  //! Return a string specifying the jth reaction.
70  std::string reactionString(size_t j);
71  void printInfo(int loglevel);
72 #else
73  inline std::string reactionString(size_t j) {
74  return std::string("");
75  }
76  inline void printInfo(int loglevel) {}
77 #endif
78 
79  void setInitialMixMoles(int loglevel = 0) {
80  setInitialMoles(loglevel);
81  finish();
82  }
83 
84  size_t componentIndex(size_t n) {
85  return m_species[m_order[n]];
86  }
87 
88  void reportCSV(const std::string& reportFile);
89 
90  double phaseMoles(size_t iph) const;
91 
92 protected:
93  //! This method finds a set of component species and a complete set of
94  //! formation reactions for the non-components in terms of the components.
95  //! In most cases, many different component sets are possible, and
96  //! therefore neither the components returned by this method nor the
97  //! formation reactions are unique. The algorithm used here is described
98  //! in Smith and Missen, Chemical Reaction Equilibrium Analysis.
99  //!
100  //! The component species are taken to be the first M species in array
101  //! 'species' that have linearly-independent compositions.
102  //!
103  //! @param order On entry, vector \a order should contain species index
104  //! numbers in the order of decreasing desirability as a component.
105  //! For example, if it is desired to choose the components from among
106  //! the major species, this array might list species index numbers in
107  //! decreasing order of mole fraction. If array 'species' does not
108  //! have length = nSpecies(), then the species will be considered as
109  //! candidates to be components in declaration order, beginning with
110  //! the first phase added.
111  void getComponents(const std::vector<size_t>& order);
112 
113  //! Estimate the initial mole numbers. This is done by running each
114  //! reaction as far forward or backward as possible, subject to the
115  //! constraint that all mole numbers remain non-negative. Reactions for
116  //! which \f$ \Delta \mu^0 \f$ are positive are run in reverse, and ones
117  //! for which it is negative are run in the forward direction. The end
118  //! result is equivalent to solving the linear programming problem of
119  //! minimizing the linear Gibbs function subject to the element and non-
120  //! negativity constraints.
121  int setInitialMoles(int loglevel = 0);
122 
123  void computeN();
124 
125  //! Take one step in composition, given the gradient of G at the starting
126  //! point, and a vector of reaction steps dxi.
127  doublereal stepComposition(int loglevel = 0);
128 
129  //! Re-arrange a vector of species properties in sorted form
130  //! (components first) into unsorted, sequential form.
131  void unsort(vector_fp& x);
132 
133  void step(doublereal omega, vector_fp& deltaN, int loglevel = 0);
134 
135  //! Compute the change in extent of reaction for each reaction.
136  doublereal computeReactionSteps(vector_fp& dxi);
137 
138  void updateMixMoles();
139 
140  //! Clean up the composition. The solution algorithm can leave some
141  //! species in stoichiometric condensed phases with very small negative
142  //! mole numbers. This method simply sets these to zero.
143  void finish();
144 
145  // moles of the species with sorted index ns
146  double moles(size_t ns) const {
147  return m_moles[m_order[ns]];
148  }
149  double& moles(size_t ns) {
150  return m_moles[m_order[ns]];
151  }
152  int solutionSpecies(size_t n) const {
153  return m_dsoln[m_order[n]];
154  }
155  bool isStoichPhase(size_t n) const {
156  return (m_dsoln[m_order[n]] == 0);
157  }
158  doublereal mu(size_t n) const {
159  return m_mu[m_species[m_order[n]]];
160  }
161  std::string speciesName(size_t n) const {
162  return
163  m_mix->speciesName(m_species[m_order[n]]);
164  }
165 
166  //! Number of degrees of freedom
167  size_t nFree() const {
168  return (m_nsp > m_nel) ? m_nsp - m_nel : 0;
169  }
170 
171  size_t m_nel_mix, m_nsp_mix, m_np;
172  size_t m_nel, m_nsp;
173  size_t m_eloc;
174  int m_iter;
175  MultiPhase* m_mix;
176  doublereal m_press, m_temp;
177  std::vector<size_t> m_order;
178  DenseMatrix m_N, m_A;
179  vector_fp m_work, m_work2, m_work3;
180  vector_fp m_moles, m_lastmoles, m_dxi;
181  vector_fp m_deltaG_RT, m_mu;
182  std::vector<bool> m_majorsp;
183  std::vector<size_t> m_sortindex;
184  vector_int m_lastsort;
185  vector_int m_dsoln;
186  vector_int m_incl_element, m_incl_species;
187 
188  // Vector of indices for species that are included in the calculation.
189  // This is used to exclude pure-phase species with invalid thermo data
190  std::vector<size_t> m_species;
191  std::vector<size_t> m_element;
192  std::vector<bool> m_solnrxn;
193  bool m_force;
194 };
195 
196 }
197 
198 #endif
doublereal computeReactionSteps(vector_fp &dxi)
Compute the change in extent of reaction for each reaction.
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:173
std::string speciesName(const size_t kGlob) const
Name of species with global index kGlob.
Definition: MultiPhase.cpp:969
size_t nFree() const
Number of degrees of freedom.
void getComponents(const std::vector< size_t > &order)
This method finds a set of component species and a complete set of formation reactions for the non-co...
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
MultiPhaseEquil(MultiPhase *mix, bool start=true, int loglevel=0)
Construct a multiphase equilibrium manager for a multiphase mixture.
Headers for the MultiPhase object that is used to set up multiphase equilibrium problems (see Equilib...
int setInitialMoles(int loglevel=0)
Estimate the initial mole numbers.
std::vector< int > vector_int
Vector of ints.
Definition: ct_defs.h:167
A class for multiphase mixtures.
Definition: MultiPhase.h:54
doublereal stepComposition(int loglevel=0)
Take one step in composition, given the gradient of G at the starting point, and a vector of reaction...
void unsort(vector_fp &x)
Re-arrange a vector of species properties in sorted form (components first) into unsorted, sequential form.
std::string reactionString(size_t j)
Return a string specifying the jth reaction.
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 finish()
Clean up the composition.
A class for full (non-sparse) matrices with Fortran-compatible data storage, which adds matrix operat...
Definition: DenseMatrix.h:70