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