Cantera  3.1.0a2
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 */
99 void addPhase(ThermoPhase* p, double moles);
100
101 //! Number of elements.
102 size_t nElements() const {
103 return m_nel;
104 }
105
106 //! Check that the specified element index is in range.
107 //! Throws an exception if m is greater than nElements()-1
108 void checkElementIndex(size_t m) const;
109
110 //! Check that an array size is at least nElements().
111 //! Throws an exception if mm is less than nElements(). Used before calls
112 //! which take an array pointer.
113 void checkElementArraySize(size_t mm) const;
114
115 //! Returns the name of the global element *m*.
116 /*!
117 * @param m index of the global element
118 */
119 string elementName(size_t m) const;
120
121 //! Returns the index of the element with name @e name.
122 /*!
123 * @param name String name of the global element
124 */
125 size_t elementIndex(const string& name) const;
126
127 //! Number of species, summed over all phases.
128 size_t nSpecies() const {
129 return m_nsp;
130 }
131
132 //! Check that the specified species index is in range.
133 //! Throws an exception if k is greater than nSpecies()-1
134 void checkSpeciesIndex(size_t k) const;
135
136 //! Check that an array size is at least nSpecies().
137 //! Throws an exception if kk is less than nSpecies(). Used before calls
138 //! which take an array pointer.
139 void checkSpeciesArraySize(size_t kk) const;
140
141 //! Name of species with global index @e kGlob
142 /*!
143 * @param kGlob global species index
144 */
145 string speciesName(const size_t kGlob) const;
146
147 //! Returns the Number of atoms of global element @e mGlob in
148 //! global species @e kGlob.
149 /*!
150 * @param kGlob global species index
151 * @param mGlob global element index
152 * @returns the number of atoms.
153 */
154 double nAtoms(const size_t kGlob, const size_t mGlob) const;
155
156 //! Returns the global Species mole fractions.
157 /*!
158 * Write the array of species mole
159 * fractions into array @c x. The mole fractions are
160 * normalized to sum to one in each phase.
161 *
162 * @param x vector of mole fractions. Length = number of global species.
163 */
164 void getMoleFractions(double* const x) const;
165
166 //! Process phases and build atomic composition array.
167 /*!
168 * This method must be called after all phases are added, before doing
169 * anything else with the mixture. After init() has been called, no more
170 * phases may be added.
171 */
172 void init();
173
174 //! Returns the name of the n'th phase
175 /*!
176 * @param iph phase Index
177 */
178 string phaseName(const size_t iph) const;
179
180 //! Returns the index, given the phase name
181 /*!
182 * @param pName Name of the phase
183 * @returns the index. A value of -1 means the phase isn't in the object.
184 */
185 int phaseIndex(const string& pName) const;
186
187 //! Return the number of moles in phase n.
188 /*!
189 * @param n Index of the phase.
190 */
191 double 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 double 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 ThermoPhase& 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 double 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 * @returns the global index
242 *
243 * If the species or phase name is not recognized, this routine throws a
244 * CanteraError.
245 */
246 size_t speciesIndex(const string& speciesName, const 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 double 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 double maxTemp() const {
259 return m_Tmax;
260 }
261
262 //! Total charge summed over all phases (Coulombs).
263 double charge() const;
264
265 //! Charge (Coulombs) of phase with index @e 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 @e p.
269 * @param p index of the phase for which the charge is desired.
270 */
271 double phaseCharge(size_t p) const;
272
273 //! Total moles of global element @e m, summed over all phases.
274 /*!
275 * @param m Index of the global element
276 */
277 double elementMoles(size_t m) const;
278
279 //! Returns a vector of Chemical potentials.
280 /*!
281 * Write into array @e 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(double* mu) const;
292
293 //! Returns a vector of Valid chemical potentials.
294 /*!
295 * Write into array @e 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 @e not_mu. If @e
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 @e mu the user-specified value @e 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 @e 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 * @param mu Vector of chemical potentials. length = Global species,
323 * units = J kmol-1
324 * @param standard If this method is called with @e standard set to true,
325 * then the composition-independent standard chemical
326 * potentials are returned instead of the composition-
327 * dependent chemical potentials.
328 */
329 void getValidChemPotentials(double not_mu, double* mu,
330 bool standard = false) const;
331
332 //! Temperature [K].
333 double temperature() const {
334 return m_temp;
335 }
336
337 //! Equilibrate a MultiPhase object
338 /*!
339 * Set this mixture to chemical equilibrium by calling one of Cantera's
340 * equilibrium solvers. The XY parameter indicates what two thermodynamic
341 * quantities are to be held constant during the equilibration process.
342 *
343 * @param XY String representation of what two properties are being
344 * held constant
345 * @param solver Name of the solver to be used to equilibrate the phase.
346 * If solver = 'vcs', the vcs_MultiPhaseEquil solver will be used. If
347 * solver = 'gibbs', the MultiPhaseEquil solver will be used. If solver
348 * = 'auto', the 'vcs' solver will be tried first, followed by the
349 * 'gibbs' solver if the first one fails.
350 * @param rtol Relative tolerance
351 * @param max_steps Maximum number of steps to take to find the solution
352 * @param max_iter The maximum number of outer temperature or pressure
353 * iterations to take when T and/or P is not held fixed.
354 * @param estimate_equil integer indicating whether the solver should
355 * estimate its own initial condition. If 0, the initial mole fraction
356 * vector in the ThermoPhase object is used as the initial condition.
357 * If 1, the initial mole fraction vector is used if the element
358 * abundances are satisfied. If -1, the initial mole fraction vector is
359 * thrown out, and an estimate is formulated.
360 * @param log_level loglevel Controls amount of diagnostic output.
361 * log_level=0 suppresses diagnostics, and increasingly-verbose
362 * messages are written as loglevel increases.
363 *
364 * @ingroup equilGroup
365 */
366 void equilibrate(const string& XY, const string& solver="auto",
367 double rtol=1e-9, int max_steps=50000, int max_iter=100,
368 int estimate_equil=0, int log_level=0);
369
370 //! Set the temperature [K].
371 /*!
372 * @param T value of the temperature (Kelvin)
373 */
374 void setTemperature(const double T);
375
376 //! Set the state of the underlying ThermoPhase objects in one call
377 /*!
378 * @param T Temperature of the system (kelvin)
379 * @param Pres pressure of the system (pascal)
380 */
381 void setState_TP(const double T, const double Pres);
382
383 //! Set the state of the underlying ThermoPhase objects in one call
384 /*!
385 * @param T Temperature of the system (kelvin)
386 * @param Pres pressure of the system (pascal)
387 * @param Moles Vector of mole numbers of all the species in all the phases
388 * (kmol)
389 */
390 void setState_TPMoles(const double T, const double Pres, const double* Moles);
391
392 //! Pressure [Pa].
393 double pressure() const {
394 return m_press;
395 }
396
397 //! The total mixture volume [m^3].
398 /*!
399 * Returns the cumulative sum of the volumes of all the phases in the
400 * mixture.
401 */
402 double volume() const;
403
404 //! Set the pressure [Pa].
405 /*!
406 * @param P Set the pressure in the MultiPhase object (Pa)
407 */
408 void setPressure(double P) {
409 m_press = P;
410 updatePhases();
411 }
412
413 //! The enthalpy of the mixture [J].
414 double enthalpy() const;
415
416 //! The internal energy of the mixture [J].
417 double IntEnergy() const;
418
419 //! The entropy of the mixture [J/K].
420 double entropy() const;
421
422 //! The Gibbs function of the mixture [J].
423 double gibbs() const;
424
425 //! Heat capacity at constant pressure [J/K]. Note that this does not
426 //! account for changes in composition of the mixture with temperature.
427 double cp() const;
428
429 //! Number of phases.
430 size_t nPhases() const {
431 return m_phase.size();
432 }
433
434 //! Return true is species @e kGlob is a species in a multicomponent
435 //! solution phase.
436 /*!
437 * @param kGlob index of the global species
438 */
439 bool solutionSpecies(size_t kGlob) const;
440
441 //! Returns the phase index of the Kth "global" species
442 /*!
443 * @param kGlob Global species index.
444 * @returns the index of the owning phase.
445 */
446 size_t speciesPhaseIndex(const size_t kGlob) const;
447
448 //! Returns the mole fraction of global species k
449 /*!
450 * @param kGlob Index of the global species.
451 */
452 double moleFraction(const size_t kGlob) const;
453
454 //! Set the Mole fractions of the nth phase
455 /*!
456 * This function sets the mole fractions of the nth phase. Note, the mole
457 * number of the phase stays constant
458 *
459 * @param n index of the phase
460 * @param x Vector of input mole fractions.
461 */
462 void setPhaseMoleFractions(const size_t n, const double* const x);
463
464 //! Set the number of moles of species in the mixture
465 /*!
466 * @param xMap Composition of the species with nonzero mole numbers.
467 * Mole numbers that are less than or equal to zero will be
468 * set to zero. units = kmol.
469 */
470 void setMolesByName(const Composition& xMap);
471
472 //! Set the moles via a string containing their names.
473 /*!
474 * The string x is in the form of a composition map. Species which are not
475 * listed are set to zero.
476 *
477 * @param x string x in the form of a composition map
478 * where values are the moles of the species.
479 */
480 void setMolesByName(const string& x);
481
482 //! Get the mole numbers of all species in the multiphase object
483 /*!
484 * @param[out] molNum Vector of doubles of length nSpecies() containing the
485 * global mole numbers (kmol).
486 */
487 void getMoles(double* molNum) const;
488
489 //! Sets all of the global species mole numbers
490 /*!
491 * The state of each phase object is also updated to have the specified
492 * composition and the mixture temperature and pressure.
493 *
494 * @param n Vector of doubles of length nSpecies() containing the global
495 * mole numbers (kmol).
496 */
497 void setMoles(const double* n);
498
499 //! Adds moles of a certain species to the mixture
500 /*!
501 * @param indexS Index of the species in the MultiPhase object
502 * @param addedMoles Value of the moles that are added to the species.
503 */
504 void addSpeciesMoles(const int indexS, const double addedMoles);
505
506 //! Retrieves a vector of element abundances
507 /*!
508 * @param elemAbundances Vector of element abundances
509 * Length = number of elements in the MultiPhase object.
510 * Index is the global element index. Units is in kmol.
511 */
512 void getElemAbundances(double* elemAbundances) const;
513
514 //! Return true if the phase @e p has valid thermo data for the current
515 //! temperature.
516 /*!
517 * @param p Index of the phase.
518 */
519 bool tempOK(size_t p) const;
520
521 // These methods are meant for internal use.
522
523 //! Update the locally-stored composition within this object to match the
524 //! current compositions of the phase objects.
525 /*!
526 * Query the underlying ThermoPhase objects for their mole fractions and
527 * fill in the mole fraction vector of this current object. Adjust element
528 * compositions within this object to match.
529 *
530 * This is an upload operation in the sense that we are taking downstream
531 * information (ThermoPhase object info) and applying it to an upstream
532 * object (MultiPhase object).
533 */
535
536 //! Set the states of the phase objects to the locally-stored
537 //! state within this MultiPhase object.
538 /*!
539 * This method sets each phase to the mixture temperature and pressure,
540 * and sets the phase mole fractions based on the mixture mole numbers.
541 *
542 * This is an download operation in the sense that we are taking upstream
543 * object information (MultiPhase object) and applying it to downstream
544 * objects (ThermoPhase object information)
545 *
546 * Therefore, the term, "update", is appropriate for a downstream operation.
547 */
548 void updatePhases() const;
549
550private:
551 //! Calculate the element abundance vector
552 void calcElemAbundances() const;
553
554 //! Set the mixture to a state of chemical equilibrium using the
555 //! MultiPhaseEquil solver.
556 /*!
557 * @param XY Integer flag specifying properties to hold fixed.
558 * @param err Error tolerance for @f$ \Delta \mu/RT @f$ for all reactions.
559 * Also used as the relative error tolerance for the outer loop.
560 * @param maxsteps Maximum number of steps to take in solving the fixed TP
561 * problem.
562 * @param maxiter Maximum number of "outer" iterations for problems holding
563 * fixed something other than (T,P).
564 * @param loglevel Level of diagnostic output
565 */
566 double equilibrate_MultiPhaseEquil(int XY, double err, int maxsteps,
567 int maxiter, int loglevel);
568
569 //! Vector of the number of moles in each phase.
570 /*!
571 * Length = m_np, number of phases.
572 */
573 vector<double> m_moles;
574
575 //! Vector of the ThermoPhase pointers.
576 vector<ThermoPhase*> m_phase;
577
578 //! Global Stoichiometric Coefficient array
579 /*!
580 * This is a two dimensional array m_atoms(m, k). The first index is the
581 * global element index. The second index, k, is the global species index.
582 * The value is the number of atoms of type m in species k.
583 */
585
586 //! Locally stored vector of mole fractions of all species comprising the
587 //! MultiPhase object.
588 vector<double> m_moleFractions;
589
590 //! Mapping between the global species number and the phase ID
591 /*!
592 * m_spphase[kGlobal] = iPhase
593 * Length = number of global species
594 */
595 vector<size_t> m_spphase;
596
597 //! Vector of ints containing of first species index in the global list of
598 //! species for each phase
599 /*!
600 * kfirst = m_spstart[ip], kfirst is the index of the first species in
601 * the ip'th phase.
602 */
603 vector<size_t> m_spstart;
604
605 //! String names of the global elements. This has a length equal to the
606 //! number of global elements.
607 vector<string> m_enames;
608
609 //! Atomic number of each global element.
610 vector<int> m_atomicNumber;
611
612 //! Vector of species names in the problem. Vector is over all species
613 //! defined in the object, the global species index.
614 vector<string> m_snames;
615
616 //! Returns the global element index, given the element string name
617 /*!
618 * -> used in the construction. However, wonder if it needs to be global.
619 */
620 map<string, size_t> m_enamemap;
621
622 //! Current value of the temperature (kelvin)
623 double m_temp = 298.15;
624
625 //! Current value of the pressure (Pa)
626 double m_press = OneBar;
627
628 //! Number of distinct elements in all of the phases
629 size_t m_nel = 0;
630
631 //! Number of distinct species in all of the phases
632 size_t m_nsp = 0;
633
634 //! True if the init() routine has been called, and the MultiPhase frozen
635 bool m_init = false;
636
637 //! Global ID of the element corresponding to the electronic charge. If
638 //! there is none, then this is equal to -1
639 size_t m_eloc = npos;
640
641 //! Vector of bools indicating whether temperatures are ok for phases.
642 /*!
643 * If the current temperature is outside the range of valid temperatures
644 * for the phase thermodynamics, the phase flag is set to false.
645 */
646 mutable vector<bool> m_temp_OK;
647
648 //! Minimum temperature for which thermo parameterizations are valid.
649 //! Stoichiometric phases are ignored in this determination. units Kelvin
650 double m_Tmin = 1.0;
651
652 //! Minimum temperature for which thermo parameterizations are valid.
653 //! Stoichiometric phases are ignored in this determination. units Kelvin
654 double m_Tmax = 100000.0;
655
656 //! Vector of element abundances
657 /*!
658 * m_elemAbundances[mGlobal] = kmol of element mGlobal summed over all
659 * species in all phases.
660 */
661 mutable vector<double> m_elemAbundances;
662};
663
664//! Function to output a MultiPhase description to a stream
665/*!
666 * Writes out a description of the contents of each phase of the
667 * MultiPhase using the report function.
668 *
669 * @param s ostream
670 * @param x Reference to a MultiPhase
671 * @returns a reference to the ostream
672 */
673std::ostream& operator<<(std::ostream& s, MultiPhase& x);
674
675//! Choose the optimum basis of species for the equilibrium calculations.
676/*!
677 * This is done by choosing the species with the largest mole fraction not
678 * currently a linear combination of the previous components. Then, calculate
679 * the stoichiometric coefficient matrix for that basis.
680 *
681 * Calculates the identity of the component species in the mechanism. Rearranges
682 * the solution data to put the component data at the front of the species list.
683 *
684 * Then, calculates SC(J,I) the formation reactions for all noncomponent
685 * species in the mechanism.
686 *
687 * @param[in] mphase Pointer to the multiphase object. Contains the species
688 * mole fractions, which are used to pick the current optimal species
689 * component basis.
690 * @param[in] orderVectorElements Order vector for the elements. The element
691 * rows in the formula matrix are rearranged according to this vector.
692 * @param[in] orderVectorSpecies Order vector for the species. The species are
693 * rearranged according to this formula. The first nComponents of this
694 * vector contain the calculated species components on exit.
695 * @param[in] doFormRxn If true, the routine calculates the formation
696 * reaction matrix based on the calculated component species. If
697 * false, this step is skipped.
698 * @param[out] usedZeroedSpecies = If true, then a species with a zero
699 * concentration was used as a component. The problem may be converged.
700 * @param[out] formRxnMatrix
701 * @return The number of components.
702 *
703 * @ingroup equilGroup
704 */
705size_t BasisOptimize(int* usedZeroedSpecies, bool doFormRxn,
706 MultiPhase* mphase, vector<size_t>& orderVectorSpecies,
707 vector<size_t>& orderVectorElements,
708 vector<double>& formRxnMatrix);
709
710//! Handles the potential rearrangement of the constraint equations
711//! represented by the Formula Matrix.
712/*!
713 * Rearrangement is only necessary when the number of components is less
714 * than the number of elements. For this case, some constraints can never
715 * be satisfied exactly, because the range space represented by the Formula
716 * Matrix of the components can't span the extra space. These constraints,
717 * which are out of the range space of the component Formula matrix
718 * entries, are migrated to the back of the Formula matrix.
719 *
720 * A prototypical example is an extra element column in FormulaMatrix[], which
721 * is identically zero. For example, let's say that argon is has an element
722 * column in FormulaMatrix[], but no species in the mechanism actually
723 * contains argon. Then, nc < ne. Unless the entry for desired element
724 * abundance vector for Ar is zero, then this element abundance constraint can
725 * never be satisfied. The constraint vector is not in the range space of the
726 * formula matrix.
727 *
728 * Also, without perturbation of FormulaMatrix[], BasisOptimize[] would
729 * produce a zero pivot because the matrix would be singular (unless the argon
730 * element column was already the last column of FormulaMatrix[].
731 *
732 * This routine borrows heavily from BasisOptimize algorithm. It finds nc
733 * constraints which span the range space of the Component Formula matrix, and
734 * assigns them as the first nc components in the formula matrix. This
735 * guarantees that BasisOptimize has a nonsingular matrix to invert.
736 *
737 * @param[in] nComponents Number of components calculated previously.
738 * @param[in] elementAbundances Current value of the element abundances
739 * @param[in] mphase Input pointer to a MultiPhase object
740 * @param[in] orderVectorSpecies input vector containing the ordering of the
741 * global species in mphase. This is used to extract the component
742 * basis of the mphase object.
743 * @param[out] orderVectorElements Output vector containing the order of the
744 * elements that is necessary for calculation of the formula matrix.
745 *
746 * @ingroup equilGroup
747 */
748void ElemRearrange(size_t nComponents, const vector<double>& elementAbundances,
749 MultiPhase* mphase,
750 vector<size_t>& orderVectorSpecies,
751 vector<size_t>& orderVectorElements);
752
753//! External int that is used to turn on debug printing for the
754//! BasisOptimize program.
755/*!
756 * Set this to 1 if you want debug printing from BasisOptimize.
757 */
758extern int BasisOptimize_print_lvl;
759}
760
761#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:55
A class for multiphase mixtures.
Definition MultiPhase.h:62
void init()
Process phases and build atomic composition array.
void checkPhaseIndex(size_t m) const
Check that the specified phase index is in range Throws an exception if m is greater than nPhases()
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
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:576
double nAtoms(const size_t kGlob, const size_t mGlob) const
Returns the Number of atoms of global element mGlob in global species kGlob.
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:584
double gibbs() const
The Gibbs function of the mixture [J].
void checkSpeciesIndex(size_t k) const
Check that the specified species index is in range.
size_t m_nel
Number of distinct elements in all of the phases.
Definition MultiPhase.h:629
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:623
void calcElemAbundances() const
Calculate the element abundance vector.
size_t nSpecies() const
Number of species, summed over all phases.
Definition MultiPhase.h:128
double enthalpy() const
The enthalpy of the mixture [J].
double pressure() const
Pressure [Pa].
Definition MultiPhase.h:393
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:603
vector< size_t > m_spphase
Mapping between the global species number and the phase ID.
Definition MultiPhase.h:595
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:251
vector< double > m_moleFractions
Locally stored vector of mole fractions of all species comprising the MultiPhase object.
Definition MultiPhase.h:588
vector< double > m_elemAbundances
Vector of element abundances.
Definition MultiPhase.h:661
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.
void checkPhaseArraySize(size_t mm) const
Check that an array size is at least nPhases() Throws an exception if mm is less than nPhases().
vector< bool > m_temp_OK
Vector of bools indicating whether temperatures are ok for phases.
Definition MultiPhase.h:646
double phaseCharge(size_t p) const
Charge (Coulombs) of phase with index p.
int phaseIndex(const string &pName) const
Returns the index, given the phase name.
size_t nPhases() const
Number of phases.
Definition MultiPhase.h:430
size_t m_eloc
Global ID of the element corresponding to the electronic charge.
Definition MultiPhase.h:639
double entropy() const
The entropy of the mixture [J/K].
double temperature() const
Temperature [K].
Definition MultiPhase.h:333
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.
double m_press
Current value of the pressure (Pa)
Definition MultiPhase.h:626
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.
void addPhase(ThermoPhase *p, double moles)
Add a phase to the mixture.
map< string, size_t > m_enamemap
Returns the global element index, given the element string name.
Definition MultiPhase.h:620
void addSpeciesMoles(const int indexS, const double addedMoles)
Adds moles of a certain species to the mixture.
size_t elementIndex(const string &name) const
Returns the index of the element with name name.
void checkElementArraySize(size_t mm) const
Check that an array size is at least nElements().
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:408
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:607
string speciesName(const size_t kGlob) const
Name of species with global index kGlob.
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:632
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:610
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:635
vector< string > m_snames
Vector of species names in the problem.
Definition MultiPhase.h:614
double m_Tmin
Minimum temperature for which thermo parameterizations are valid.
Definition MultiPhase.h:650
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.
void checkSpeciesArraySize(size_t kk) const
Check that an array size is at least nSpecies().
size_t nElements() const
Number of elements.
Definition MultiPhase.h:102
double charge() const
Total charge summed over all phases (Coulombs).
string phaseName(const size_t iph) const
Returns the name of the n'th phase.
double cp() const
Heat capacity at constant pressure [J/K].
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.
void checkElementIndex(size_t m) const
Check that the specified element index is in range.
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:258
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:573
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.
double m_Tmax
Minimum temperature for which thermo parameterizations are valid.
Definition MultiPhase.h:654
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:564
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