Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
equilibrate.cpp
Go to the documentation of this file.
1 /**
2  * @file equilibrate.cpp Driver routines for the chemical equilibrium solvers.
3  */
4 
7 
8 namespace Cantera
9 {
10 
11 doublereal equilibrate(MultiPhase& s, const char* XY,
12  doublereal tol, int maxsteps, int maxiter,
13  int loglevel)
14 {
15  warn_deprecated("equilibrate(MultiPhase&, ...)",
16  "Use MultiPhase::equilibrate instead. To be removed after Cantera 2.2.");
17  s.init();
18  int ixy = _equilflag(XY);
19  if (ixy == TP || ixy == HP || ixy == SP || ixy == TV) {
20  try {
21  double err = s.equilibrate(ixy, tol, maxsteps, maxiter, loglevel);
22  return err;
23  } catch (CanteraError& err) {
24  err.save();
25  throw err;
26  }
27  } else {
28  throw CanteraError("equilibrate","unsupported option");
29  return -1.0;
30  }
31  return 0.0;
32 }
33 
34 int equilibrate(thermo_t& s, const char* XY, int solver,
35  doublereal rtol, int maxsteps, int maxiter, int loglevel)
36 {
37  warn_deprecated("equilibrate(ThermoPhase&, ...)",
38  "Use ThermoPhase::equilibrate instead. To be removed after Cantera 2.2.");
39  bool redo = true;
40  int retn = -1;
41  int nAttempts = 0;
42  int retnSub = 0;
43 
44  while (redo) {
45  if (solver >= 2) {
46  int printLvlSub = loglevel;
47  int estimateEquil = 0;
48  try {
49  MultiPhase m;
50  m.addPhase(&s, 1.0);
51  m.init();
52  nAttempts++;
53  vcs_equilibrate(m, XY, estimateEquil, printLvlSub, solver,
54  rtol, maxsteps, maxiter, loglevel-1);
55  redo = false;
56  retn = nAttempts;
57  } catch (CanteraError& err) {
58  err.save();
59  if (nAttempts < 2) {
60  solver = -1;
61  } else {
62  throw err;
63  }
64  }
65  } else if (solver == 1) {
66  try {
67  MultiPhase m;
68  m.addPhase(&s, 1.0);
69  m.init();
70  nAttempts++;
71  equilibrate(m, XY, rtol, maxsteps, maxiter, loglevel-1);
72  redo = false;
73  retn = nAttempts;
74  } catch (CanteraError& err) {
75  err.save();
76  if (nAttempts < 2) {
77  solver = -1;
78  } else {
79  throw err;
80  }
81  }
82  } else { // solver <= 0
83  /*
84  * Call the element potential solver
85  */
86  try {
87  ChemEquil e;
88  e.options.maxIterations = maxsteps;
89  e.options.relTolerance = rtol;
90  nAttempts++;
91  bool useThermoPhaseElementPotentials = true;
92  retnSub = e.equilibrate(s, XY, useThermoPhaseElementPotentials,
93  loglevel-1);
94  if (retnSub < 0) {
95  if (nAttempts < 2) {
96  solver = 1;
97  } else {
98  throw CanteraError("equilibrate",
99  "Both equilibrium solvers failed");
100  }
101  }
102  retn = nAttempts;
103  s.setElementPotentials(e.elementPotentials());
104  redo = false;
105  }
106 
107  catch (CanteraError& err) {
108  err.save();
109  // If ChemEquil fails, try the MultiPhase solver
110  if (solver < 0) {
111  solver = 1;
112  } else {
113  redo = false;
114  throw err;
115  }
116  }
117  }
118  } // while (redo)
119  /*
120  * We are here only for a success
121  */
122  return retn;
123 }
124 }
Class ChemEquil implements a chemical equilibrium solver for single-phase solutions.
Definition: ChemEquil.h:92
EquilOpt options
Options controlling how the calculation is carried out.
Definition: ChemEquil.h:147
doublereal relTolerance
Relative tolerance.
Definition: ChemEquil.h:35
void addPhase(ThermoPhase *p, doublereal moles)
Add a phase to the mixture.
Definition: MultiPhase.cpp:91
int equilibrate(thermo_t &s, const char *XY, bool useThermoPhaseElementPotentials=false, int loglevel=0)
Definition: ChemEquil.cpp:336
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:78
doublereal equilibrate(MultiPhase &s, const char *XY, doublereal tol, int maxsteps, int maxiter, int loglevel)
Equilibrate a MultiPhase object.
Definition: equilibrate.cpp:11
doublereal equilibrate(int XY, doublereal err=1.0e-9, int maxsteps=1000, int maxiter=200, int loglevel=-99)
Set the mixture to a state of chemical equilibrium.
Definition: MultiPhase.cpp:561
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:97
A class for multiphase mixtures.
Definition: MultiPhase.h:53
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:99
Interface class for the vcsnonlinear solver.
void init()
Process phases and build atomic composition array.
Definition: MultiPhase.cpp:160
void setElementPotentials(const vector_fp &lambda)
Stores the element potentials in the ThermoPhase object.
int maxIterations
Maximum number of iterations.
Definition: ChemEquil.h:37
int _equilflag(const char *xy)
map property strings to integers
Definition: ChemEquil.cpp:24
int vcs_equilibrate(thermo_t &s, const char *XY, int estimateEquil, int printLvl, int solver, doublereal rtol, int maxsteps, int maxiter, int loglevel)
Set a single-phase chemical solution to chemical equilibrium.
Chemical equilibrium.
void save()
Function to put this error onto Cantera's error stack.