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