Cantera  2.0
vcs_MultiPhaseEquil.h
Go to the documentation of this file.
1 /**
2  * @file vcs_MultiPhaseEquil.h
3  * Interface class for the vcsnonlinear solver
4  */
5 #ifndef VCS_MULTIPHASEEQUIL_H
6 #define VCS_MULTIPHASEEQUIL_H
7 
8 #include "cantera/base/ct_defs.h"
9 #include "MultiPhase.h"
10 #include "vcs_defs.h"
11 
12 namespace Cantera
13 {
14 
15 
16 //! Set a single-phase chemical solution to chemical equilibrium.
17 /*!
18  * The function uses the element abundance vector that is
19  * currently consistent with the composition within the phase
20  * itself. Two other thermodynamic quantities, determined by the
21  * XY string, are held constant during the equilibration.
22  * This is a convenience function that uses one or the other of
23  * the two chemical equilibrium solvers.
24  *
25  * @param s The object to set to an equilibrium state
26  *
27  * @param XY An integer specifying the two properties to be held
28  * constant.
29  *
30  * @param estimateEquil integer indicating whether the solver
31  * should estimate its own initial condition.
32  * If 0, the initial mole fraction vector
33  * in the %ThermoPhase object is used as the
34  * initial condition.
35  * If 1, the initial mole fraction vector
36  * is used if the element abundances are
37  * satisfied.
38  * if -1, the initial mole fraction vector
39  * is thrown out, and an estimate is
40  * formulated.
41  *
42  * @param printLvl Determines the amount of printing that
43  * gets sent to stdout from the vcs package
44  * (Note, you may have to compile with debug
45  * flags to get some printing).
46  *
47  * @param solver The equilibrium solver to use. If solver = 0,
48  * the ChemEquil solver will be used, and if
49  * solver = 1, the vcs_MultiPhaseEquil solver will
50  * be used (slower than ChemEquil,
51  * but more stable). If solver < 0 (default, then
52  * ChemEquil will be tried first, and if it fails
53  * vcs_MultiPhaseEquil will be tried.
54  *
55  * @param rtol Relative tolerance of the solve. Defaults to
56  * 1.0E-9.
57  *
58  * @param maxsteps The maximum number of steps to take to find
59  * the solution.
60  *
61  * @param maxiter For the MultiPhaseEquil solver only, this is
62  * the maximum number of outer temperature or
63  * pressure iterations to take when T and/or P is
64  * not held fixed.
65  *
66  * @param loglevel Controls amount of diagnostic output. loglevel
67  * = 0 suppresses diagnostics, and increasingly-verbose
68  * messages are written as loglevel increases. The
69  * messages are written to a file in HTML format for viewing
70  * in a web browser. @see HTML_logs
71  *
72  * @ingroup equilfunctions
73  */
74 int vcs_equilibrate(thermo_t& s, const char* XY,
75  int estimateEquil = 0, int printLvl = 0,
76  int solver = -1, doublereal rtol = 1.0e-9,
77  int maxsteps = VCS_MAXSTEPS,
78  int maxiter = 100, int loglevel = -99);
79 
80 
81 //! Set a multi-phase chemical solution to chemical equilibrium.
82 /*!
83  * This function uses the vcs_MultiPhaseEquil interface to the
84  * vcs solver.
85  * The function uses the element abundance vector that is
86  * currently consistent with the composition within the phases
87  * themselves. Two other thermodynamic quantities, determined by the
88  * XY string, are held constant during the equilibration.
89  *
90  * @param s The object to set to an equilibrium state
91  *
92  * @param XY A character string representing the unknowns
93  * to be held constant
94  *
95  * @param estimateEquil integer indicating whether the solver
96  * should estimate its own initial condition.
97  * If 0, the initial mole fraction vector
98  * in the %ThermoPhase object is used as the
99  * initial condition.
100  * If 1, the initial mole fraction vector
101  * is used if the element abundances are
102  * satisfied.
103  * if -1, the initial mole fraction vector
104  * is thrown out, and an estimate is
105  * formulated.
106  *
107  * @param printLvl Determines the amount of printing that
108  * gets sent to stdout from the vcs package
109  * (Note, you may have to compile with debug
110  * flags to get some printing).
111  *
112  * @param solver Determines which solver is used.
113  * - 1 MultiPhaseEquil solver
114  * - 2 VCSnonideal Solver (default)
115  *
116  * @param rtol Relative tolerance of the solve. Defaults to
117  * 1.0E-9.
118  *
119  * @param maxsteps The maximum number of steps to take to find
120  * the solution.
121  *
122  * @param maxiter For the MultiPhaseEquil solver only, this is
123  * the maximum number of outer temperature or
124  * pressure iterations to take when T and/or P is
125  * not held fixed.
126  *
127  * @param loglevel Controls amount of diagnostic output. loglevel
128  * = 0 suppresses diagnostics, and increasingly-verbose
129  * messages are written as loglevel increases. The
130  * messages are written to a file in HTML format for viewing
131  * in a web browser. @see HTML_logs
132  *
133  * @ingroup equilfunctions
134  */
135 int vcs_equilibrate(MultiPhase& s, const char* XY,
136  int estimateEquil = 0, int printLvl = 0,
137  int solver = 2,
138  doublereal rtol = 1.0e-9, int maxsteps = VCS_MAXSTEPS,
139  int maxiter = 100, int loglevel = -99);
140 
141 //! Set a multi-phase chemical solution to chemical equilibrium.
142 /*!
143  * This function uses the vcs_MultiPhaseEquil interface to the
144  * vcs solver.
145  * The function uses the element abundance vector that is
146  * currently consistent with the composition within the phases
147  * themselves. Two other thermodynamic quantities, determined by the
148  * XY string, are held constant during the equilibration.
149  *
150  * @param s The MultiPhase object to be set to an equilibrium state
151  *
152  * @param ixy An integer specifying the two properties to be held
153  * constant.
154  *
155  * @param estimateEquil integer indicating whether the solver
156  * should estimate its own initial condition.
157  * If 0, the initial mole fraction vector
158  * in the %ThermoPhase object is used as the
159  * initial condition.
160  * If 1, the initial mole fraction vector
161  * is used if the element abundances are
162  * satisfied.
163  * if -1, the initial mole fraction vector
164  * is thrown out, and an estimate is
165  * formulated.
166  *
167  * @param printLvl Determines the amount of printing that
168  * gets sent to stdout from the vcs package
169  * (Note, you may have to compile with debug
170  * flags to get some printing).
171  *
172  * @param solver Determines which solver is used.
173  * - 1 MultiPhaseEquil solver
174  * - 2 VCSnonideal Solver (default)
175  *
176  * @param rtol Relative tolerance of the solve. Defaults to
177  * 1.0E-9.
178  *
179  * @param maxsteps The maximum number of steps to take to find
180  * the solution.
181  *
182  * @param maxiter For the MultiPhaseEquil solver only, this is
183  * the maximum number of outer temperature or
184  * pressure iterations to take when T and/or P is
185  * not held fixed.
186  *
187  * @param loglevel Controls amount of diagnostic output. loglevel
188  * = 0 suppresses diagnostics, and increasingly-verbose
189  * messages are written as loglevel increases. The
190  * messages are written to a file in HTML format for viewing
191  * in a web browser. @see HTML_logs
192  *
193  * @ingroup equilfunctions
194  */
195 int vcs_equilibrate_1(MultiPhase& s, int ixy,
196  int estimateEquil = 0, int printLvl = 0,
197  int solver = 2,
198  doublereal rtol = 1.0e-9, int maxsteps = VCS_MAXSTEPS,
199  int maxiter = 100, int loglevel = -99);
200 
201 //! Determine the phase stability of a single phase given the current conditions
202 //! in a MultiPhase object
203 /*!
204  *
205  * @param s The MultiPhase object to be set to an equilibrium state
206  * @param iphase Phase index within the multiphase object to be
207  * tested for stability.
208  * @param funcStab Function value that tests equilibrium. > 0 indicates stable
209  * < 0 indicates unstable
210  *
211  * @param printLvl Determines the amount of printing that
212  * gets sent to stdout from the vcs package
213  * (Note, you may have to compile with debug
214  * flags to get some printing).
215  *
216  * @param loglevel Controls amount of diagnostic output. loglevel
217  * = 0 suppresses diagnostics, and increasingly-verbose
218  * messages are written as loglevel increases. The
219  * messages are written to a file in HTML format for viewing
220  * in a web browser. @see HTML_logs
221  */
222 int vcs_determine_PhaseStability(MultiPhase& s, int iphase,
223  double& funcStab, int printLvl, int loglevel);
224 
225 }
226 
227 namespace VCSnonideal
228 {
229 
230 
231 class VCS_PROB;
232 class VCS_SOLVE;
233 
234 //! Translate a MultiPhase object into a VCS_PROB problem definition object
235 /*!
236  * @param mphase MultiPhase object that is the source for all of the information
237  * @param vprob VCS_PROB problem definition that gets all of the information
238  *
239  * Note, both objects share the underlying Thermophase objects. So, neither
240  * can be const objects.
241  */
242 int vcs_Cantera_to_vprob(Cantera::MultiPhase* mphase,
243  VCSnonideal::VCS_PROB* vprob);
244 
245 //! Translate a MultiPhase information into a VCS_PROB problem definition object
246 /*!
247  * This version updates the problem statement information only. All species and
248  * phase definitions remain the same.
249  *
250  * @param mphase MultiPhase object that is the source for all of the information
251  * @param vprob VCS_PROB problem definition that gets all of the information
252  *
253  */
254 int vcs_Cantera_update_vprob(Cantera::MultiPhase* mphase,
255  VCSnonideal::VCS_PROB* vprob);
256 
257 //! Cantera's Interface to the Multiphase chemical equilibrium solver.
258 /*!
259  * Class MultiPhaseEquil is designed to be used to set a mixture
260  * containing one or more phases to a state of chemical equilibrium.
261  *
262  * Note, as currently constructed, the underlying ThermoPhase
263  * objects are shared between the MultiPhase object and this
264  * object. Therefore, mix is not a const argument, and the
265  * return parameters are contained in underlying ThermoPhase
266  * objects.
267  *
268  * @ingroup equilfunctions
269  */
271 {
272 public:
273  //! Typedef for an index variable
274  typedef size_t index_t;
275 
276  //! Default empty constructor
278 
279 
280  //! Constructor for the multiphase equilibrium solver
281  /*!
282  * This constructor will initialize the object with a MultiPhase
283  * object, setting up the internal equilibration problem.
284  * Note, as currently constructed, the underlying ThermoPhase
285  * objects are shared between the MultiPhase object and this
286  * object. Therefore, mix is not a const argument, and the
287  * return parameters are contained in underlying ThermoPhase
288  * objects.
289  *
290  * @param mix Object containing the MultiPhase object
291  * @param printLvl Determines the amount of printing to stdout
292  * that occurs for each call:
293  * - 0 No printing
294  * - 1 Only printing to the .csv file
295  * - 2 print the soln only
296  * - 3 Print the setup and then the soln only
297  * - 4 Print a table for each iteration
298  * - 5 Print more than a table for each iteration
299  *
300  */
301  vcs_MultiPhaseEquil(Cantera::MultiPhase* mix, int printLvl);
302 
303  //! Destructor for the class
304  virtual ~vcs_MultiPhaseEquil();
305 
306  //! Return the index of the ith component
307  /*!
308  * Returns the index of the ith component in the equilibrium
309  * calculation. The index refers to the ordering of the species
310  * in the MultiPhase object.
311  *
312  * @param m Index of the component. Must be between 0 and the
313  * number of components, which can be obtained from the
314  * numComponents() command.
315  */
316  size_t component(size_t m) const ;
317 
318  //! Get the stoichiometric reaction coefficients for a single
319  //! reaction index
320  /*!
321  * This returns a stoichiometric reaction vector for a single
322  * formation reaction for a noncomponent species. There are
323  * (nSpecies() - nComponents) formation reactions. Each
324  * formation reaction will have a value of 1.0 for the species
325  * that is being formed, and the other non-zero coefficients will
326  * all involve the components of the mixture.
327  *
328  * @param rxn Reaction number.
329  * @param nu Vector of coefficients for the formation reaction.
330  * Length is equal to the number of species in
331  * the MultiPhase object.
332  */
334 
335  //! return the number of iterations
336  int iterations() const {
337  return m_iter;
338  }
339 
340  //! Equilibrate the solution using the current element abundances
341  //! stored in the MultiPhase object
342  /*!
343  * Use the vcs algorithm to equilibrate the current multiphase
344  * mixture.
345  *
346  * @param XY Integer representing what two thermo quantities
347  * are held constant during the equilibration
348  *
349  * @param estimateEquil integer indicating whether the solver
350  * should estimate its own initial condition.
351  * If 0, the initial mole fraction vector
352  * in the %ThermoPhase object is used as the
353  * initial condition.
354  * If 1, the initial mole fraction vector
355  * is used if the element abundances are
356  * satisfied.
357  * if -1, the initial mole fraction vector
358  * is thrown out, and an estimate is
359  * formulated.
360  *
361  * @param printLvl Determines the amount of printing that
362  * gets sent to stdout from the vcs package
363  * (Note, you may have to compile with debug
364  * flags to get some printing).
365  * @param err Internal error level
366  * @param maxsteps max steps allowed.
367  * @param loglevel for
368  */
369  int equilibrate(int XY, int estimateEquil = 0,
370  int printLvl= 0, doublereal err = 1.0e-6,
371  int maxsteps = VCS_MAXSTEPS, int loglevel=-99);
372 
373  //! Equilibrate the solution using the current element abundances
374  //! stored in the MultiPhase object using constant T and P
375  /*!
376  * Use the vcs algorithm to equilibrate the current multiphase
377  * mixture.
378  *
379  * @param estimateEquil integer indicating whether the solver
380  * should estimate its own initial condition.
381  * If 0, the initial mole fraction vector
382  * in the %ThermoPhase object is used as the
383  * initial condition.
384  * If 1, the initial mole fraction vector
385  * is used if the element abundances are
386  * satisfied.
387  * if -1, the initial mole fraction vector
388  * is thrown out, and an estimate is
389  * formulated.
390  *
391  * @param printLvl Determines the amount of printing that
392  * gets sent to stdout from the vcs package
393  * (Note, you may have to compile with debug
394  * flags to get some printing).
395  * @param err Internal error level
396  * @param maxsteps max steps allowed.
397  * @param loglevel for
398  */
399  int equilibrate_TP(int estimateEquil = 0,
400  int printLvl= 0, doublereal err = 1.0e-6,
401  int maxsteps = VCS_MAXSTEPS, int loglevel=-99);
402 
403  //! Equilibrate the solution using the current element abundances
404  //! stored in the MultiPhase object using either constant H and P
405  //! or constant U and P.
406  /*!
407  * Use the vcs algorithm to equilibrate the current multiphase
408  * mixture. The pressure of the calculation is taken from
409  * the current pressure stored with the MultiPhase object.
410  *
411  * @param Htarget Value of the total mixture enthalpy or total
412  * internal energy that will be
413  * kept constant. Note, this is and must be an extensive
414  * quantity. units = Joules
415  *
416  * @param XY Integer flag indicating what is held constant.
417  * Must be either HP or UP.
418  *
419  * @param Tlow Lower limit of the temperature. It's an
420  * error condition if the temperature falls
421  * below Tlow.
422  *
423  * @param Thigh Upper limit of the temperature. It's an
424  * error condition if the temperature goes
425  * higher than Thigh.
426  *
427  * @param estimateEquil integer indicating whether the solver
428  * should estimate its own initial condition.
429  * If 0, the initial mole fraction vector
430  * in the %ThermoPhase object is used as the
431  * initial condition.
432  * If 1, the initial mole fraction vector
433  * is used if the element abundances are
434  * satisfied.
435  * if -1, the initial mole fraction vector
436  * is thrown out, and an estimate is
437  * formulated.
438  *
439  * @param printLvl Determines the amount of printing that
440  * gets sent to stdout from the vcs package
441  * (Note, you may have to compile with debug
442  * flags to get some printing). See main
443  * constructor call for meaning of the levels.
444  *
445  * @param err Internal error level
446  *
447  * @param maxsteps max steps allowed.
448  *
449  * @param loglevel Determines the amount of printing to the HTML
450  * output file.
451  */
452  int equilibrate_HP(doublereal Htarget, int XY, double Tlow, double Thigh,
453  int estimateEquil = 0,
454  int printLvl = 0, doublereal err = 1.0E-6,
455  int maxsteps = VCS_MAXSTEPS, int loglevel=-99);
456 
457  //! Equilibrate the solution using the current element abundances
458  //! stored in the MultiPhase object using constant S and P.
459  /*!
460  * Use the vcs algorithm to equilibrate the current multiphase
461  * mixture. The pressure of the calculation is taken from
462  * the current pressure stored with the MultiPhase object.
463  *
464  * @param Starget Value of the total mixture entropy
465  * that will be
466  * kept constant. Note, this is and must be an extensive
467  * quantity. units = Joules/K
468  *
469  *
470  * @param Tlow Lower limit of the temperature. It's an
471  * error condition if the temperature falls
472  * below Tlow.
473  *
474  * @param Thigh Upper limit of the temperature. It's an
475  * error condition if the temperature goes
476  * higher than Thigh.
477  *
478  * @param estimateEquil integer indicating whether the solver
479  * should estimate its own initial condition.
480  * If 0, the initial mole fraction vector
481  * in the %ThermoPhase object is used as the
482  * initial condition.
483  * If 1, the initial mole fraction vector
484  * is used if the element abundances are
485  * satisfied.
486  * if -1, the initial mole fraction vector
487  * is thrown out, and an estimate is
488  * formulated.
489  *
490  * @param printLvl Determines the amount of printing that
491  * gets sent to stdout from the vcs package
492  * (Note, you may have to compile with debug
493  * flags to get some printing). See main
494  * constructor call for meaning of the levels.
495  *
496  * @param err Internal error level
497  *
498  * @param maxsteps max steps allowed.
499  *
500  * @param loglevel Determines the amount of printing to the HTML
501  * output file.
502  */
503  int equilibrate_SP(doublereal Starget, double Tlow, double Thigh,
504  int estimateEquil = 0,
505  int printLvl = 0, doublereal err = 1.0E-6,
506  int maxsteps = VCS_MAXSTEPS, int loglevel=-99);
507 
508 
509  //! Equilibrate the solution using the current element abundances
510  //! stored in the MultiPhase object using constant V and constant
511  //! T, H, U, or S.
512  /*!
513  * Use the vcs algorithm to equilibrate the current multiphase
514  * mixture. The pressure of the calculation is taken from
515  * the current pressure stored with the MultiPhase object.
516  *
517  *
518  * @param XY Integer flag indicating what is held constant.
519  * Must be either TV, HV, UV, or SV.
520  *
521  * @param xtarget Value of the total thermodynamic parameter to
522  * be held constant in addition to V.
523  * Note, except for T, this must be an extensive
524  * quantity. units = Joules/K or Joules
525  *
526  * @param estimateEquil integer indicating whether the solver
527  * should estimate its own initial condition.
528  * If 0, the initial mole fraction vector
529  * in the %ThermoPhase object is used as the
530  * initial condition.
531  * If 1, the initial mole fraction vector
532  * is used if the element abundances are
533  * satisfied.
534  * if -1, the initial mole fraction vector
535  * is thrown out, and an estimate is
536  * formulated.
537  *
538  * @param printLvl Determines the amount of printing that
539  * gets sent to stdout from the vcs package
540  * (Note, you may have to compile with debug
541  * flags to get some printing). See main
542  * constructor call for meaning of the levels.
543  *
544  * @param err Internal error level
545  *
546  * @param maxsteps max steps allowed.
547  *
548  * @param logLevel Determines the amount of printing to the HTML
549  * output file.
550  */
551  int equilibrate_TV(int XY, doublereal xtarget,
552  int estimateEquil = 0,
553  int printLvl = 0, doublereal err = 1.0E-6,
554  int maxsteps = VCS_MAXSTEPS, int logLevel = -99);
555 
556  //! Determine the phase stability of a phase at the current conditions
557  /*!
558  * Equilibration of the solution is not done before the determination is made.
559  *
560  * @param iph Phase number to determine the equilibrium. If the phase
561  * has a non-zero mole number....
562  * @param funcStab Value of the phase pop function
563  * @param printLvl Determines the amount of printing that
564  * gets sent to stdout from the vcs package
565  * (Note, you may have to compile with debug
566  * flags to get some printing).
567  * @param logLevel Determines the amount of printing to the HTML output file.
568  */
569  int determine_PhaseStability(int iph, double& funcStab, int printLvl= 0, int logLevel = -99);
570 
571  //! Report the equilibrium answer in a comma separated table format
572  /*!
573  * This routine is used for in the test suite.
574  *
575  * @param reportFile Base name of the file to get the report.
576  * File name is incremented by 1 for each report.
577  */
578  void reportCSV(const std::string& reportFile);
579 
580  //! reports the number of components in the equilibration problem
581  /*!
582  * @return returns the number of components. If an equilibrium
583  * problem hasn't been solved yet, it returns -1.
584  */
585  size_t numComponents() const;
586 
587  //! Reports the number of element constraints in the equilibration problem
588  /*!
589  * @return returns the number of element constraints. If an equilibrium
590  * problem hasn't been solved yet, it returns -1.
591  */
592  size_t numElemConstraints() const;
593 
594 
595 
596  // Friend functions
597 
598  friend int vcs_Cantera_to_vprob(Cantera::MultiPhase* mphase,
599  VCSnonideal::VCS_PROB* vprob);
601  VCSnonideal::VCS_PROB* vprob);
602 
603 protected:
604 
605  //! Vector that takes into account of the current sorting of the species
606  /*!
607  * The index of m_order is the original k value of the species in the
608  * multiphase. The value of m_order, k_sorted, is the current value of the
609  * species index.
610  *
611  * m_order[korig] = k_sorted
612  */
614 
615  //! Object which contains the problem statement
616  /*!
617  * The problem statement may contain some subtleties. For example,
618  * the element constraints may be different than just an element
619  * conservation contraint equations.
620  * There may be kinetically frozen degrees of freedom.
621  * There may be multiple electrolyte phases with zero charge constraints.
622  * All of these make the problem statement different than the
623  * simple element conservation statement.
624  */
626 
627  //! Pointer to the MultiPhase mixture that will be equilibrated.
628  /*!
629  * Equilibrium solutions will be returned via this variable.
630  */
632 
633  //! Print level from the VCSnonlinear package
634  /*!
635  * (Note, you may have to compile with debug
636  * flags to get some printing).
637  *
638  * - 0 No IO from the routine whatsoever
639  * - 1 file IO from reportCSV() carried out.
640  * One line print statements from equilibrate_XY() functions
641  * - 2 Problem statement information from vcs_Cantera_update_vprob()
642  * - Final state of the system from vcs_solve_TP()
643  * - 3 Several more setup tables
644  * - Problem initialization routine
645  * - 4 One table for each iteration within vcs_solve_Tp()
646  * - 5 Multiple tables for each iteration within vcs_solve_TP()
647  * - full discussion of decisions made for each variable.
648  */
650 
651  //! Stoichiometric matrix
653 
654  //! Iteration Count
655  int m_iter;
656 
657  //! Vector of indices for species that are included in the
658  //! calculation.
659  /*!
660  * This is used to exclude pure-phase species
661  * with invalid thermo data
662  */
664 
665  //! Pointer to the object that does all of the equilibration work.
666  /*!
667  * VCS_SOLVE will have different ordering for species and element constraints
668  * than this object or the VCS_PROB object. This object owns the pointer.
669  */
671 
672 };
673 
674 //! Global hook for turning on and off time printing.
675 /*!
676  * Default is to allow printing. But, you can assign this to zero
677  * globally to turn off all time printing.
678  * This is helpful for test suite purposes where you are interested
679  * in differences in text files.
680  */
681 extern int vcs_timing_print_lvl;
682 
683 }
684 #endif
685