Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MultiPhase.h
Go to the documentation of this file.
1 /**
2  * @file MultiPhase.h
3  * Headers for the \link Cantera::MultiPhase MultiPhase\endlink
4  * object that is used to set up multiphase equilibrium problems (see \ref equilfunctions).
5  */
6 // Copyright 2004 California Institute of Technology
7 
8 #ifndef CT_MULTIPHASE_H
9 #define CT_MULTIPHASE_H
10 
13 
14 namespace Cantera
15 {
16 
17 //! A class for multiphase mixtures. The mixture can contain any
18 //! number of phases of any type.
19 /*!
20  * This object is the basic tool used by Cantera for use in Multiphase
21  * equilibrium calculations.
22  *
23  * It is a container for a set of phases. Each phase has a given number of
24  * kmoles. Therefore, MultiPhase may be considered an "extrinsic"
25  * thermodynamic object, in contrast to the ThermoPhase object, which is an
26  * "intrinsic" thermodynamic object.
27  *
28  * MultiPhase may be considered to be "upstream" of the ThermoPhase objects in
29  * the sense that setting a property within MultiPhase, such as temperature,
30  * pressure, or species mole number, affects the underlying ThermoPhase
31  * object, but not the other way around.
32  *
33  * All phases have the same temperature and pressure, and a specified number
34  * of moles for each phase. The phases do not need to have the same elements.
35  * For example, a mixture might consist of a gaseous phase with elements (H,
36  * C, O, N), a solid carbon phase containing only element C, etc. A master
37  * element set will be constructed for the mixture that is the intersection of
38  * the elements of each phase.
39  *
40  * Below, reference is made to global species and global elements. These refer
41  * to the collective species and elements encompassing all of the phases
42  * tracked by the object.
43  *
44  * The global element list kept by this object is an intersection of the
45  * element lists of all the phases that comprise the MultiPhase.
46  *
47  * The global species list kept by this object is a concatenated list of all
48  * of the species in all the phases that comprise the MultiPhase. The ordering
49  * of species is contiguous with respect to the phase id.
50  *
51  * @ingroup equilfunctions
52  */
54 {
55 public:
56  //! Constructor.
57  /*!
58  * The constructor takes no arguments, since phases are added using
59  * method addPhase().
60  */
61  MultiPhase();
62 
63  MultiPhase(const MultiPhase& right);
64 
65  //! Destructor. Does nothing. Class MultiPhase does not take "ownership"
66  //! (i.e. responsibility for destroying) the phase objects.
67  virtual ~MultiPhase() {}
68 
69  MultiPhase& operator=(const MultiPhase& right);
70 
71  //! Add a vector of phases to the mixture
72  /*!
73  * See the single addPhases command. This just does a bunch of phases
74  * at one time
75  * @param phases Vector of pointers to phases
76  * @param phaseMoles Vector of mole numbers in each phase (kmol)
77  */
78  void addPhases(std::vector<ThermoPhase*>& phases, const vector_fp& phaseMoles);
79 
80  //! Add all phases present in 'mix' to this mixture.
81  /*!
82  * @param mix Add all of the phases in another MultiPhase
83  * object to the current object.
84  */
85  void addPhases(MultiPhase& mix);
86 
87  //! Add a phase to the mixture.
88  /*!
89  * This function must be called before the init() function is called,
90  * which serves to freeze the MultiPhase.
91  *
92  * @param p pointer to the phase object
93  * @param moles total number of moles of all species in this phase
94  */
95  void addPhase(ThermoPhase* p, doublereal moles);
96 
97  //! Number of elements.
98  size_t nElements() const {
99  return m_nel;
100  }
101 
102  //! Check that the specified element index is in range.
103  //! Throws an exception if m is greater than nElements()-1
104  void checkElementIndex(size_t m) const;
105 
106  //! Check that an array size is at least nElements().
107  //! Throws an exception if mm is less than nElements(). Used before calls
108  //! which take an array pointer.
109  void checkElementArraySize(size_t mm) const;
110 
111  //! Returns the name of the global element *m*.
112  /*!
113  * @param m index of the global element
114  */
115  std::string elementName(size_t m) const;
116 
117  //! Returns the index of the element with name \a name.
118  /*!
119  * @param name String name of the global element
120  */
121  size_t elementIndex(const std::string& name) const;
122 
123  //! Number of species, summed over all phases.
124  size_t nSpecies() const {
125  return m_nsp;
126  }
127 
128  //! Check that the specified species index is in range.
129  //! Throws an exception if k is greater than nSpecies()-1
130  void checkSpeciesIndex(size_t k) const;
131 
132  //! Check that an array size is at least nSpecies().
133  //! Throws an exception if kk is less than nSpecies(). Used before calls
134  //! which take an array pointer.
135  void checkSpeciesArraySize(size_t kk) const;
136 
137  //! Name of species with global index \a kGlob
138  /*!
139  * @param kGlob global species index
140  */
141  std::string speciesName(const size_t kGlob) const;
142 
143  //! Returns the Number of atoms of global element \a mGlob in
144  //! global species \a kGlob.
145  /*!
146  * @param kGlob global species index
147  * @param mGlob global element index
148  * @return returns the number of atoms.
149  */
150  doublereal nAtoms(const size_t kGlob, const size_t mGlob) const;
151 
152  /// Returns the global Species mole fractions.
153  /*!
154  * Write the array of species mole
155  * fractions into array \c x. The mole fractions are
156  * normalized to sum to one in each phase.
157  *
158  * @param x vector of mole fractions.
159  * Length = number of global species.
160  */
161  void getMoleFractions(doublereal* const x) const;
162 
163  //! Process phases and build atomic composition array.
164  /*!
165  * This method must be called after all phases are added, before doing
166  * anything else with the mixture. After init() has been called, no more
167  * phases may be added.
168  */
169  void init();
170 
171  //! Returns the name of the n'th phase
172  /*!
173  * @param iph phase Index
174  */
175  std::string phaseName(const size_t iph) const;
176 
177  //! Returns the index, given the phase name
178  /*!
179  * @param pName Name of the phase
180  *
181  * @return returns the index. A value of -1 means
182  * the phase isn't in the object.
183  */
184  int phaseIndex(const std::string& pName) const;
185 
186  //! Return the number of moles in phase n.
187  /*!
188  * @param n Index of the phase.
189  */
190  doublereal phaseMoles(const size_t n) const;
191 
192  //! Set the number of moles of phase with index n.
193  /*!
194  * @param n Index of the phase
195  * @param moles Number of moles in the phase (kmol)
196  */
197  void setPhaseMoles(const size_t n, const doublereal moles);
198 
199  /// Return a reference to phase n.
200  /*!
201  * The state of phase n is also updated to match the state stored locally
202  * in the mixture object.
203  *
204  * @param n Phase Index
205  * @return Reference to the ThermoPhase object for the phase
206  */
207  thermo_t& phase(size_t n);
208 
209  //! Check that the specified phase index is in range
210  //! Throws an exception if m is greater than nPhases()
211  void checkPhaseIndex(size_t m) const;
212 
213  //! Check that an array size is at least nPhases()
214  //! Throws an exception if mm is less than nPhases(). Used before calls
215  //! which take an array pointer.
216  void checkPhaseArraySize(size_t mm) const;
217 
218  //! Returns the moles of global species \c k. units = kmol
219  /*!
220  * @param kGlob Global species index k
221  */
222  doublereal speciesMoles(size_t kGlob) const;
223 
224  //! Return the global index of the species belonging to phase number \c p
225  //! with local index \c k within the phase.
226  /*!
227  * @param k local index of the species within the phase
228  * @param p index of the phase
229  */
230  size_t speciesIndex(size_t k, size_t p) const {
231  return m_spstart[p] + k;
232  }
233 
234  //! Return the global index of the species belonging to phase name \c phaseName
235  //! with species name \c speciesName
236  /*!
237  * @param speciesName Species Name
238  * @param phaseName Phase Name
239  *
240  * @return returns the global index
241  *
242  * If the species or phase name is not recognized, this routine throws
243  * a CanteraError.
244  */
245  size_t speciesIndex(const std::string& speciesName, const std::string& phaseName);
246 
247  /// Minimum temperature for which all solution phases have valid thermo
248  /// data. Stoichiometric phases are not considered, since they may have
249  /// thermo data only valid for conditions for which they are stable.
250  doublereal minTemp() const {
251  return m_Tmin;
252  }
253 
254  /// Maximum temperature for which all solution phases have valid thermo
255  /// data. Stoichiometric phases are not considered, since they may have
256  /// thermo data only valid for conditions for which they are stable.
257  doublereal maxTemp() const {
258  return m_Tmax;
259  }
260 
261  //! Total charge summed over all phases (Coulombs).
262  doublereal charge() const;
263 
264  /// Charge (Coulombs) of phase with index \a p.
265  /*!
266  * The net charge is computed as \f[ Q_p = N_p \sum_k F z_k X_k \f]
267  * where the sum runs only over species in phase \a p.
268  * @param p index of the phase for which the charge is desired.
269  */
270  doublereal phaseCharge(size_t p) const;
271 
272  //! Total moles of global element \a m, summed over all phases.
273  /*!
274  * @param m Index of the global element
275  */
276  doublereal elementMoles(size_t m) const;
277 
278  //! Returns a vector of Chemical potentials.
279  /*!
280  * Write into array \a mu the chemical potentials of all species
281  * [J/kmol]. The chemical potentials are related to the activities by
282  *
283  * \f$
284  * \mu_k = \mu_k^0(T, P) + RT \ln a_k.
285  * \f$.
286  *
287  * @param mu Chemical potential vector. Length = num global species. Units
288  * = J/kmol.
289  */
290  void getChemPotentials(doublereal* mu) const;
291 
292  /// Returns a vector of Valid chemical potentials.
293  /*!
294  * Write into array \a mu the chemical potentials of all species with
295  * thermo data valid for the current temperature [J/kmol]. For other
296  * species, set the chemical potential to the value \a not_mu. If \a
297  * standard is set to true, then the values returned are standard chemical
298  * potentials.
299  *
300  * This method is designed for use in computing chemical equilibrium by
301  * Gibbs minimization. For solution phases (more than one species), this
302  * does the same thing as getChemPotentials. But for stoichiometric
303  * phases, this writes into array \a mu the user-specified value \a not_mu
304  * instead of the chemical potential if the temperature is outside the
305  * range for which the thermo data for the one species in the phase are
306  * valid. The need for this arises since many condensed phases have thermo
307  * data fit only for the temperature range for which they are stable. For
308  * example, in the NASA database, the fits for H2O(s) are only done up to
309  * 0 C, the fits for H2O(L) are only done from 0 C to 100 C, etc. Using
310  * the polynomial fits outside the range for which the fits were done can
311  * result in spurious chemical potentials, and can lead to condensed
312  * phases appearing when in fact they should be absent.
313  *
314  * By setting \a not_mu to a large positive value, it is possible to force
315  * routines which seek to minimize the Gibbs free energy of the mixture to
316  * zero out any phases outside the temperature range for which their
317  * thermo data are valid.
318  *
319  * @param not_mu Value of the chemical potential to set species in phases,
320  * for which the thermo data is not valid
321  *
322  * @param mu Vector of chemical potentials. length = Global species,
323  * units = J kmol-1
324  *
325  * @param standard If this method is called with \a standard set to true,
326  * then the composition-independent standard chemical
327  * potentials are returned instead of the composition-
328  * dependent chemical potentials.
329  */
330  void getValidChemPotentials(doublereal not_mu, doublereal* mu,
331  bool standard = false) const;
332 
333  //! Temperature [K].
334  doublereal temperature() const {
335  return m_temp;
336  }
337 
338  //! Set the mixture to a state of chemical equilibrium.
339  /*!
340  * @param XY Integer flag specifying properties to hold fixed.
341  * @param err Error tolerance for \f$\Delta \mu/RT \f$ for all
342  * reactions. Also used as the relative error tolerance for
343  * the outer loop.
344  * @param maxsteps Maximum number of steps to take in solving the fixed
345  * TP problem.
346  * @param maxiter Maximum number of "outer" iterations for problems
347  * holding fixed something other than (T,P).
348  * @param loglevel Level of diagnostic output
349  */
350  doublereal equilibrate(int XY, doublereal err = 1.0e-9,
351  int maxsteps = 1000, int maxiter = 200, int loglevel = -99);
352 
353  //! Equilibrate a MultiPhase object
354  /*!
355  * Set this mixture to chemical equilibrium by calling one of Cantera's
356  * equilibrium solvers. The XY parameter indicates what two thermodynamic
357  * quantities are to be held constant during the equilibration process.
358  *
359  * @param XY String representation of what two properties are being
360  * held constant
361  * @param solver Name of the solver to be used to equilibrate the phase.
362  * If solver = 'vcs', the vcs_MultiPhaseEquil solver will be used. If
363  * solver = 'gibbs', the MultiPhaseEquil solver will be used. If solver
364  * = 'auto', the 'vcs' solver will be tried first, followed by the
365  * 'gibbs' solver if the first one fails.
366  * @param rtol Relative tolerance
367  * @param max_steps Maximum number of steps to take to find the solution
368  * @param max_iter The maximum number of outer temperature or pressure
369  * iterations to take when T and/or P is not held fixed.
370  * @param estimate_equil integer indicating whether the solver should
371  * estimate its own initial condition. If 0, the initial mole fraction
372  * vector in the ThermoPhase object is used as the initial condition.
373  * If 1, the initial mole fraction vector is used if the element
374  * abundances are satisfied. If -1, the initial mole fraction vector is
375  * thrown out, and an estimate is formulated.
376  * @param log_level loglevel Controls amount of diagnostic output.
377  * log_level=0 suppresses diagnostics, and increasingly-verbose
378  * messages are written as loglevel increases.
379  *
380  * @ingroup equilfunctions
381  */
382  void equilibrate(const std::string& XY, const std::string& solver="auto",
383  double rtol=1e-9, int max_steps=50000, int max_iter=100,
384  int estimate_equil=0, int log_level=0);
385 
386  /// Set the temperature [K].
387  /*!
388  * @param T value of the temperature (Kelvin)
389  */
390  void setTemperature(const doublereal T);
391 
392  //! Set the state of the underlying ThermoPhase objects in one call
393  /*!
394  * @param T Temperature of the system (kelvin)
395  * @param Pres pressure of the system (pascal)
396  */
397  void setState_TP(const doublereal T, const doublereal Pres);
398 
399  //! Set the state of the underlying ThermoPhase objects in one call
400  /*!
401  * @param T Temperature of the system (kelvin)
402  * @param Pres pressure of the system (pascal)
403  * @param Moles Vector of mole numbers of all the species in all the phases
404  * (kmol)
405  */
406  void setState_TPMoles(const doublereal T, const doublereal Pres, const doublereal* Moles);
407 
408  /// Pressure [Pa].
409  doublereal pressure() const {
410  return m_press;
411  }
412 
413  /// The total mixture volume [m^3].
414  /*!
415  * Returns the cumulative sum of the volumes of all the
416  * phases in the mixture.
417  */
418  doublereal volume() const;
419 
420  //! Set the pressure [Pa].
421  /*!
422  * @param P Set the pressure in the MultiPhase object (Pa)
423  */
424  void setPressure(doublereal P) {
425  m_press = P;
426  updatePhases();
427  }
428 
429  //! The enthalpy of the mixture [J].
430  doublereal enthalpy() const;
431 
432  //! The internal energy of the mixture [J].
433  doublereal IntEnergy() const;
434 
435  //! The entropy of the mixture [J/K].
436  doublereal entropy() const;
437 
438  //! The Gibbs function of the mixture [J].
439  doublereal gibbs() const;
440 
441  //! Heat capacity at constant pressure [J/K]. Note that this does not
442  //! account for changes in composition of the mixture with temperature.
443  doublereal cp() const;
444 
445  //! Number of phases.
446  size_t nPhases() const {
447  return m_np;
448  }
449 
450  //! Return true is species \a kGlob is a species in a
451  //! multicomponent solution phase.
452  /*!
453  * @param kGlob index of the global species
454  */
455  bool solutionSpecies(size_t kGlob) const;
456 
457  //! Returns the phase index of the Kth "global" species
458  /*!
459  * @param kGlob Global species index.
460  *
461  * @return Returns the index of the owning phase.
462  */
463  size_t speciesPhaseIndex(const size_t kGlob) const;
464 
465  //! Returns the mole fraction of global species k
466  /*!
467  * @param kGlob Index of the global species.
468  */
469  doublereal moleFraction(const size_t kGlob) const;
470 
471  //! Set the Mole fractions of the nth phase
472  /*!
473  * This function sets the mole fractions of the nth phase. Note, the mole
474  * number of the phase stays constant
475  *
476  * @param n index of the phase
477  * @param x Vector of input mole fractions.
478  */
479  void setPhaseMoleFractions(const size_t n, const doublereal* const x);
480 
481  //! Set the number of moles of species in the mixture
482  /*!
483  * @param xMap CompositionMap of the species with nonzero mole numbers.
484  * Mole numbers that are less than or equal to zero will be
485  * set to zero. units = kmol.
486  */
487  void setMolesByName(const compositionMap& xMap);
488 
489  //! Set the moles via a string containing their names.
490  /*!
491  * The string x is in the form of a composition map. Species which are not
492  * listed are set to zero.
493  *
494  * @param x string x in the form of a composition map
495  * where values are the moles of the species.
496  */
497  void setMolesByName(const std::string& x);
498 
499  //! Get the mole numbers of all species in the multiphase object
500  /*!
501  * @param[out] molNum Vector of doubles of length nSpecies containing the
502  * global mole numbers (kmol).
503  */
504  void getMoles(doublereal* molNum) const;
505 
506  //! Sets all of the global species mole numbers
507  /*!
508  * The state of each phase object is also updated to have the specified
509  * composition and the mixture temperature and pressure.
510  *
511  * @param n Vector of doubles of length nSpecies containing the global
512  * mole numbers (kmol).
513  */
514  void setMoles(const doublereal* n);
515 
516  //! Adds moles of a certain species to the mixture
517  /*!
518  * @param indexS Index of the species in the MultiPhase object
519  * @param addedMoles Value of the moles that are added to the species.
520  */
521  void addSpeciesMoles(const int indexS, const doublereal addedMoles);
522 
523  //! Retrieves a vector of element abundances
524  /*!
525  * @param elemAbundances Vector of element abundances
526  * Length = number of elements in the MultiPhase object.
527  * Index is the global element index. Units is in kmol.
528  */
529  void getElemAbundances(doublereal* elemAbundances) const;
530 
531  //! Return true if the phase \a p has valid thermo data for
532  //! the current temperature.
533  /*!
534  * @param p Index of the phase.
535  */
536  bool tempOK(size_t p) const;
537 
538  // These methods are meant for internal use.
539 
540  //! Update the locally-stored composition within this object
541  //! to match the current compositions of the phase objects.
542  /*!
543  * Query the underlying ThermoPhase objects for their mole fractions and
544  * fill in the mole fraction vector of this current object. Adjust
545  * element compositions within this object to match.
546  *
547  * This is an upload operation in the sense that we are taking downstream
548  * information (ThermoPhase object info) and applying it to an upstream
549  * object (MultiPhase object).
550  */
552 
553  //! Set the states of the phase objects to the locally-stored
554  //! state within this MultiPhase object.
555  /*!
556  * This method sets each phase to the mixture temperature and pressure,
557  * and sets the phase mole fractions based on the mixture mole numbers.
558  *
559  * This is an download operation in the sense that we are taking
560  * upstream object information (MultiPhase object) and
561  * applying it to downstream objects (ThermoPhase object information)
562  *
563  * Therefore, the term, "update", is appropriate for a downstream
564  * operation.
565  */
566  void updatePhases() const;
567 
568 private:
569  //! Calculate the element abundance vector
570  void calcElemAbundances() const;
571 
572  //! Set the mixture to a state of chemical equilibrium using the
573  //! MultiPhaseEquil solver.
574  /*!
575  * @param XY Integer flag specifying properties to hold fixed.
576  * @param err Error tolerance for \f$\Delta \mu/RT \f$ for all reactions.
577  * Also used as the relative error tolerance for the outer
578  * loop.
579  * @param maxsteps Maximum number of steps to take in solving the fixed
580  * TP problem.
581  * @param maxiter Maximum number of "outer" iterations for problems holding
582  * fixed something other than (T,P).
583  * @param loglevel Level of diagnostic output
584  */
585  double equilibrate_MultiPhaseEquil(int XY, doublereal err, int maxsteps,
586  int maxiter, int loglevel);
587 
588  //! Vector of the number of moles in each phase.
589  /*!
590  * Length = m_np, number of phases.
591  */
593 
594  //! Vector of the ThermoPhase pointers.
595  std::vector<ThermoPhase*> m_phase;
596 
597  //! Global Stoichiometric Coefficient array
598  /*!
599  * This is a two dimensional array m_atoms(m, k). The first index is the
600  * global element index. The second index, k, is the global species
601  * index. The value is the number of atoms of type m in species k.
602  */
604 
605  /**
606  * Locally stored vector of mole fractions of all species
607  * comprising the MultiPhase object.
608  */
610 
611  //! Mapping between the global species number and the phase ID
612  /*!
613  * m_spphase[kGlobal] = iPhase
614  * Length = number of global species
615  */
616  std::vector<size_t> m_spphase;
617 
618  //! Vector of ints containing of first species index in the global list of
619  //! species for each phase
620  /*!
621  * kfirst = m_spstart[ip], kfirst is the index of the first species in
622  * the ip'th phase.
623  */
624  std::vector<size_t> m_spstart;
625 
626  //! String names of the global elements
627  /*!
628  * This has a length equal to the number of global elements.
629  */
630  std::vector<std::string> m_enames;
631 
632  //! Atomic number of each element
633  /*!
634  * This is the atomic number of each global element.
635  */
637 
638  //! Vector of species names in the problem
639  /*!
640  * Vector is over all species defined in the object,
641  * the global species index.
642  */
643  std::vector<std::string> m_snames;
644 
645  //! Returns the global element index, given the element string name
646  /*!
647  * -> used in the construction. However, wonder if it needs to be global.
648  */
649  std::map<std::string, size_t> m_enamemap;
650 
651  //! Number of phases in the MultiPhase object
652  size_t m_np;
653 
654  //! Current value of the temperature (kelvin)
655  doublereal m_temp;
656 
657  //! Current value of the pressure (Pa)
658  doublereal m_press;
659 
660  //! Number of distinct elements in all of the phases
661  size_t m_nel;
662 
663  //! Number of distinct species in all of the phases
664  size_t m_nsp;
665 
666  //! True if the init() routine has been called, and the MultiPhase frozen
667  bool m_init;
668 
669  //! Global ID of the element corresponding to the electronic charge.
670  /*!
671  * If there is none, then this is equal to -1
672  */
673  size_t m_eloc;
674 
675  //! Vector of bools indicating whether temperatures are ok for phases.
676  /*!
677  * If the current temperature is outside the range of valid temperatures
678  * for the phase thermodynamics, the phase flag is set to false.
679  */
680  mutable std::vector<bool> m_temp_OK;
681 
682  //! Minimum temperature for which thermo parameterizations are valid
683  /*!
684  * Stoichiometric phases are ignored in this determination.
685  * units Kelvin
686  */
687  doublereal m_Tmin;
688 
689  //! Minimum temperature for which thermo parameterizations are valid
690  /*!
691  * Stoichiometric phases are ignored in this determination. units Kelvin
692  */
693  doublereal m_Tmax;
694 
695  //! Vector of element abundances
696  /*!
697  * m_elemAbundances[mGlobal] = kmol of element mGlobal summed over all
698  * species in all phases.
699  */
701 };
702 
703 //! Function to output a MultiPhase description to a stream
704 /*!
705  * Writes out a description of the contents of each phase of the
706  * MultiPhase using the report function.
707  *
708  * @param s ostream
709  * @param x Reference to a MultiPhase
710  * @return returns a reference to the ostream
711  */
712 inline std::ostream& operator<<(std::ostream& s, Cantera::MultiPhase& x)
713 {
714  size_t ip;
715  for (ip = 0; ip < x.nPhases(); ip++) {
716  if (x.phase(ip).name() != "") {
717  s << "*************** " << x.phase(ip).name() << " *****************" << std::endl;
718  } else {
719  s << "*************** Phase " << ip << " *****************" << std::endl;
720  }
721  s << "Moles: " << x.phaseMoles(ip) << std::endl;
722 
723  s << x.phase(ip).report() << std::endl;
724  }
725  return s;
726 }
727 
728 //! Choose the optimum basis of species for the equilibrium calculations.
729 /*!
730  * This is done by choosing the species with the largest mole fraction not
731  * currently a linear combination of the previous components. Then, calculate
732  * the stoichiometric coefficient matrix for that basis.
733  *
734  * Calculates the identity of the component species in the mechanism.
735  * Rearranges the solution data to put the component data at the
736  * front of the species list.
737  *
738  * Then, calculates SC(J,I) the formation reactions for all noncomponent
739  * species in the mechanism.
740  *
741  * @param[in] mphase Pointer to the multiphase object. Contains the species
742  * mole fractions, which are used to pick the current optimal species
743  * component basis.
744  * @param[in] orderVectorElements Order vector for the elements. The element
745  * rows in the formula matrix are rearranged according to this vector.
746  * @param[in] orderVectorSpecies Order vector for the species. The species are
747  * rearranged according to this formula. The first nCompoments of this
748  * vector contain the calculated species components on exit.
749  * @param[in] doFormRxn If true, the routine calculates the formation
750  * reaction matrix based on the calculated component species. If
751  * false, this step is skipped.
752  * @param[out] usedZeroedSpecies = If true, then a species with a zero
753  * concentration was used as a component. The problem may be
754  * converged.
755  * @param[out] formRxnMatrix
756  * @return The number of components.
757  *
758  * @ingroup equilfunctions
759  */
760 size_t BasisOptimize(int* usedZeroedSpecies, bool doFormRxn,
761  MultiPhase* mphase, std::vector<size_t>& orderVectorSpecies,
762  std::vector<size_t>& orderVectorElements,
763  vector_fp& formRxnMatrix);
764 
765 //! Handles the potential rearrangement of the constraint equations
766 //! represented by the Formula Matrix.
767 /*!
768  * Rearrangement is only necessary when the number of components is less
769  * than the number of elements. For this case, some constraints can never
770  * be satisfied exactly, because the range space represented by the Formula
771  * Matrix of the components can't span the extra space. These constraints,
772  * which are out of the range space of the component Formula matrix
773  * entries, are migrated to the back of the Formula matrix.
774  *
775  * A prototypical example is an extra element column in FormulaMatrix[], which
776  * is identically zero. For example, let's say that argon is has an element
777  * column in FormulaMatrix[], but no species in the mechanism actually
778  * contains argon. Then, nc < ne. Unless the entry for desired element
779  * abundance vector for Ar is zero, then this element abundance constraint can
780  * never be satisfied. The constraint vector is not in the range space of the
781  * formula matrix.
782  *
783  * Also, without perturbation of FormulaMatrix[], BasisOptimize[] would
784  * produce a zero pivot because the matrix would be singular (unless the argon
785  * element column was already the last column of FormulaMatrix[].
786  *
787  * This routine borrows heavily from BasisOptimize algorithm. It finds nc
788  * constraints which span the range space of the Component Formula matrix, and
789  * assigns them as the first nc components in the formula matrix. This
790  * guarantees that BasisOptimize has a nonsingular matrix to invert.
791  *
792  * @param[in] nComponents Number of components calculated previously.
793  * @param[in] elementAbundances Current value of the element abundances
794  * @param[in] mphase Input pointer to a MultiPhase object
795  * @param[in] orderVectorSpecies input vector containing the ordering of the
796  * global species in mphase. This is used to extract the component
797  * basis of the mphase object.
798  * @param[out] orderVectorElements Output vector containing the order of the
799  * elements that is necessary for calculation of the formula matrix.
800  *
801  * @ingroup equilfunctions
802  * @deprecated - The return value for this function is deprecated. After
803  * Cantera 2.2, this function will return void.
804  */
805 size_t ElemRearrange(size_t nComponents, const vector_fp& elementAbundances,
806  MultiPhase* mphase,
807  std::vector<size_t>& orderVectorSpecies,
808  std::vector<size_t>& orderVectorElements);
809 
810 //! External int that is used to turn on debug printing for the
811 //! BasisOptimze program.
812 /*!
813  * Set this to 1 if you want debug printing from BasisOptimize.
814  */
815 extern int BasisOptimize_print_lvl;
816 }
817 
818 #endif
std::map< std::string, doublereal > compositionMap
Map connecting a string name with a double.
Definition: ct_defs.h:149
std::vector< size_t > m_spphase
Mapping between the global species number and the phase ID.
Definition: MultiPhase.h:616
doublereal m_temp
Current value of the temperature (kelvin)
Definition: MultiPhase.h:655
size_t BasisOptimize(int *usedZeroedSpecies, bool doFormRxn, MultiPhase *mphase, std::vector< size_t > &orderVectorSpecies, std::vector< size_t > &orderVectorElements, vector_fp &formRxnMatrix)
Choose the optimum basis of species for the equilibrium calculations.
int BasisOptimize_print_lvl
External int that is used to turn on debug printing for the BasisOptimze program. ...
bool tempOK(size_t p) const
Return true if the phase p has valid thermo data for the current temperature.
Definition: MultiPhase.cpp:958
MultiPhase()
Constructor.
Definition: MultiPhase.cpp:18
bool m_init
True if the init() routine has been called, and the MultiPhase frozen.
Definition: MultiPhase.h:667
void setPhaseMoles(const size_t n, const doublereal moles)
Set the number of moles of phase with index n.
Definition: MultiPhase.cpp:943
doublereal speciesMoles(size_t kGlob) const
Returns the moles of global species k. units = kmol.
Definition: MultiPhase.cpp:247
void checkElementIndex(size_t m) const
Check that the specified element index is in range.
Definition: MultiPhase.cpp:861
std::map< std::string, size_t > m_enamemap
Returns the global element index, given the element string name.
Definition: MultiPhase.h:649
std::string speciesName(const size_t kGlob) const
Name of species with global index kGlob.
Definition: MultiPhase.cpp:904
void checkSpeciesArraySize(size_t kk) const
Check that an array size is at least nSpecies().
Definition: MultiPhase.cpp:897
vector_int m_atomicNumber
Atomic number of each element.
Definition: MultiPhase.h:636
doublereal phaseMoles(const size_t n) const
Return the number of moles in phase n.
Definition: MultiPhase.cpp:938
doublereal temperature() const
Temperature [K].
Definition: MultiPhase.h:334
void addPhase(ThermoPhase *p, doublereal moles)
Add a phase to the mixture.
Definition: MultiPhase.cpp:91
doublereal phaseCharge(size_t p) const
Charge (Coulombs) of phase with index p.
Definition: MultiPhase.cpp:295
doublereal equilibrate(int XY, doublereal err=1.0e-9, int maxsteps=1000, int maxiter=200, int loglevel=-99)
Set the mixture to a state of chemical equilibrium.
Definition: MultiPhase.cpp:561
int phaseIndex(const std::string &pName) const
Returns the index, given the phase name.
Definition: MultiPhase.cpp:925
std::string name() const
Return the name of the phase.
Definition: Phase.cpp:157
void getChemPotentials(doublereal *mu) const
Returns a vector of Chemical potentials.
Definition: MultiPhase.cpp:306
void checkPhaseArraySize(size_t mm) const
Check that an array size is at least nPhases() Throws an exception if mm is less than nPhases()...
Definition: MultiPhase.cpp:240
vector_fp m_moles
Vector of the number of moles in each phase.
Definition: MultiPhase.h:592
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:97
void getMoleFractions(doublereal *const x) const
Returns the global Species mole fractions.
Definition: MultiPhase.cpp:914
size_t speciesPhaseIndex(const size_t kGlob) const
Returns the phase index of the Kth "global" species.
Definition: MultiPhase.cpp:948
std::vector< int > vector_int
Vector of ints.
Definition: ct_defs.h:159
std::vector< std::string > m_enames
String names of the global elements.
Definition: MultiPhase.h:630
doublereal gibbs() const
The Gibbs function of the mixture [J].
Definition: MultiPhase.cpp:346
void addSpeciesMoles(const int indexS, const doublereal addedMoles)
Adds moles of a certain species to the mixture.
Definition: MultiPhase.cpp:490
vector_fp m_moleFractions
Locally stored vector of mole fractions of all species comprising the MultiPhase object.
Definition: MultiPhase.h:609
void uploadMoleFractionsFromPhases()
Update the locally-stored composition within this object to match the current compositions of the pha...
Definition: MultiPhase.cpp:963
A class for multiphase mixtures.
Definition: MultiPhase.h:53
void checkPhaseIndex(size_t m) const
Check that the specified phase index is in range Throws an exception if m is greater than nPhases() ...
Definition: MultiPhase.cpp:233
doublereal charge() const
Total charge summed over all phases (Coulombs).
Definition: MultiPhase.cpp:269
std::vector< bool > m_temp_OK
Vector of bools indicating whether temperatures are ok for phases.
Definition: MultiPhase.h:680
void setState_TPMoles(const doublereal T, const doublereal Pres, const doublereal *Moles)
Set the state of the underlying ThermoPhase objects in one call.
Definition: MultiPhase.cpp:509
void checkSpeciesIndex(size_t k) const
Check that the specified species index is in range.
Definition: MultiPhase.cpp:890
std::string elementName(size_t m) const
Returns the name of the global element m.
Definition: MultiPhase.cpp:875
size_t m_eloc
Global ID of the element corresponding to the electronic charge.
Definition: MultiPhase.h:673
void getValidChemPotentials(doublereal not_mu, doublereal *mu, bool standard=false) const
Returns a vector of Valid chemical potentials.
Definition: MultiPhase.cpp:316
doublereal entropy() const
The entropy of the mixture [J/K].
Definition: MultiPhase.cpp:385
void addPhases(std::vector< ThermoPhase * > &phases, const vector_fp &phaseMoles)
Add a vector of phases to the mixture.
Definition: MultiPhase.cpp:80
DenseMatrix m_atoms
Global Stoichiometric Coefficient array.
Definition: MultiPhase.h:603
std::vector< size_t > m_spstart
Vector of ints containing of first species index in the global list of species for each phase...
Definition: MultiPhase.h:624
void setPhaseMoleFractions(const size_t n, const doublereal *const x)
Set the Mole fractions of the nth phase.
Definition: MultiPhase.cpp:411
size_t m_nsp
Number of distinct species in all of the phases.
Definition: MultiPhase.h:664
void calcElemAbundances() const
Calculate the element abundance vector.
Definition: MultiPhase.cpp:526
size_t m_nel
Number of distinct elements in all of the phases.
Definition: MultiPhase.h:661
void setTemperature(const doublereal T)
Set the temperature [K].
Definition: MultiPhase.cpp:852
void checkElementArraySize(size_t mm) const
Check that an array size is at least nElements().
Definition: MultiPhase.cpp:868
double equilibrate_MultiPhaseEquil(int XY, doublereal err, int maxsteps, int maxiter, int loglevel)
Set the mixture to a state of chemical equilibrium using the MultiPhaseEquil solver.
Definition: MultiPhase.cpp:570
size_t elementIndex(const std::string &name) const
Returns the index of the element with name name.
Definition: MultiPhase.cpp:880
virtual ~MultiPhase()
Destructor.
Definition: MultiPhase.h:67
doublereal IntEnergy() const
The internal energy of the mixture [J].
Definition: MultiPhase.cpp:372
size_t m_np
Number of phases in the MultiPhase object.
Definition: MultiPhase.h:652
vector_fp m_elemAbundances
Vector of element abundances.
Definition: MultiPhase.h:700
std::vector< ThermoPhase * > m_phase
Vector of the ThermoPhase pointers.
Definition: MultiPhase.h:595
void getMoles(doublereal *molNum) const
Get the mole numbers of all species in the multiphase object.
Definition: MultiPhase.cpp:441
void setMolesByName(const compositionMap &xMap)
Set the number of moles of species in the mixture.
Definition: MultiPhase.cpp:424
doublereal nAtoms(const size_t kGlob, const size_t mGlob) const
Returns the Number of atoms of global element mGlob in global species kGlob.
Definition: MultiPhase.cpp:909
void setPressure(doublereal P)
Set the pressure [Pa].
Definition: MultiPhase.h:424
doublereal moleFraction(const size_t kGlob) const
Returns the mole fraction of global species k.
Definition: MultiPhase.cpp:953
void updatePhases() const
Set the states of the phase objects to the locally-stored state within this MultiPhase object...
Definition: MultiPhase.cpp:974
void setState_TP(const doublereal T, const doublereal Pres)
Set the state of the underlying ThermoPhase objects in one call.
Definition: MultiPhase.cpp:499
doublereal enthalpy() const
The enthalpy of the mixture [J].
Definition: MultiPhase.cpp:359
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:157
doublereal maxTemp() const
Maximum temperature for which all solution phases have valid thermo data.
Definition: MultiPhase.h:257
doublereal cp() const
Heat capacity at constant pressure [J/K].
Definition: MultiPhase.cpp:398
Headers for the DenseMatrix object, which deals with dense rectangular matrices and description of th...
std::string phaseName(const size_t iph) const
Returns the name of the n'th phase.
Definition: MultiPhase.cpp:919
virtual std::string report(bool show_thermo=true, doublereal threshold=-1e-14) const
returns a summary of the state of the phase as a string
doublereal elementMoles(size_t m) const
Total moles of global element m, summed over all phases.
Definition: MultiPhase.cpp:253
void init()
Process phases and build atomic composition array.
Definition: MultiPhase.cpp:160
std::vector< std::string > m_snames
Vector of species names in the problem.
Definition: MultiPhase.h:643
bool solutionSpecies(size_t kGlob) const
Return true is species kGlob is a species in a multicomponent solution phase.
Definition: MultiPhase.cpp:337
thermo_t & phase(size_t n)
Return a reference to phase n.
Definition: MultiPhase.cpp:222
doublereal minTemp() const
Minimum temperature for which all solution phases have valid thermo data.
Definition: MultiPhase.h:250
doublereal m_press
Current value of the pressure (Pa)
Definition: MultiPhase.h:658
doublereal pressure() const
Pressure [Pa].
Definition: MultiPhase.h:409
void getElemAbundances(doublereal *elemAbundances) const
Retrieves a vector of element abundances.
Definition: MultiPhase.cpp:517
size_t nSpecies() const
Number of species, summed over all phases.
Definition: MultiPhase.h:124
size_t speciesIndex(size_t k, size_t p) const
Return the global index of the species belonging to phase number p with local index k within the phas...
Definition: MultiPhase.h:230
doublereal m_Tmax
Minimum temperature for which thermo parameterizations are valid.
Definition: MultiPhase.h:693
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).
size_t ElemRearrange(size_t nComponents, const vector_fp &elementAbundances, MultiPhase *mphase, std::vector< size_t > &orderVectorSpecies, std::vector< size_t > &orderVectorElements)
Handles the potential rearrangement of the constraint equations represented by the Formula Matrix...
doublereal m_Tmin
Minimum temperature for which thermo parameterizations are valid.
Definition: MultiPhase.h:687
void setMoles(const doublereal *n)
Sets all of the global species mole numbers.
Definition: MultiPhase.cpp:459
doublereal volume() const
The total mixture volume [m^3].
Definition: MultiPhase.cpp:550
A class for full (non-sparse) matrices with Fortran-compatible data storage, which adds matrix operat...
Definition: DenseMatrix.h:71
size_t nElements() const
Number of elements.
Definition: MultiPhase.h:98
size_t nPhases() const
Number of phases.
Definition: MultiPhase.h:446