Cantera  2.1.2
newEquilibrate.cpp
1 
2 #include "cantera/equil/equil.h"
6 #include "cantera/base/global.h"
8 
9 namespace Cantera
10 {
11 
12 int new_equilibrate(thermo_t& s, const char* XY, int solver,
13  doublereal rtol, int maxsteps, int maxiter, int loglevel)
14 {
15  // Decide which solvers to consider
16  int ixy = _equilflag(XY);
17  vector_int solvers;
18  if (solver == -1) {
19  solvers.push_back(2);
20  if (ixy == TP || ixy == HP || ixy == SP || ixy == TV) {
21  solvers.push_back(1);
22  }
23  solvers.push_back(0);
24  } else if (solver == 1) {
25  if (ixy == TP || ixy == HP || ixy == SP || ixy == TV) {
26  solvers.push_back(1);
27  } else {
28  throw CanteraError("equilibrate",
29  std::string("MultiPhase equilibrium solver does not support property pair ") + XY);
30  }
31  } else if (solver == 0 || solver == 2) {
32  solvers.push_back(0);
33  } else {
34  throw CanteraError("equilibrate",
35  "unknown solver:" + int2str(solver));
36  }
37 
38  int nAttempts = 0;
39  while (!solvers.empty()) {
40  nAttempts++;
41  solver = solvers.back();
42  solvers.pop_back();
43 
44  if (solver == 0) {
45  ChemEquil e;
46  e.options.maxIterations = maxsteps;
47  e.options.relTolerance = rtol;
48  nAttempts++;
49  int retnSub = e.equilibrate(s, XY, true, loglevel-1);
50  if (retnSub < 0) {
51  } else {
52  return nAttempts;
53  }
54  s.setElementPotentials(e.elementPotentials());
55  } else if (solver == 1) {
56  try {
57  MultiPhase m;
58  m.addPhase(&s, 1.0);
59  m.init();
60  nAttempts++;
61  std::cout << "Calling equilibrate(Multiphase)" << std::endl;
62  equilibrate(m, XY, rtol, maxsteps, maxiter, loglevel-1);
63  return nAttempts;
64  } catch (CanteraError& err) {
65  err.save();
66  if (nAttempts < 2) {
67  solver = -1;
68  } else {
69  throw err;
70  }
71  }
72 
73  }
74 
75  }
76 
77  throw CanteraError("equilibrate", "All available solvers failed");
78 
79 }
80 
81 }
std::string int2str(const int n, const std::string &fmt)
Convert an int to a string using a format converter.
Definition: stringUtils.cpp:40
This file contains the definition of some high level general equilibration routines.
doublereal equilibrate(MultiPhase &s, const char *XY, doublereal tol, int maxsteps, int maxiter, int loglevel)
Equilibrate a MultiPhase object.
Definition: equilibrate.cpp:14
This file contains definitions for utility functions and text for modules, inputfiles, logs, textlogs, HTML_logs (see Input File Handling, Diagnostic Output, Writing messages to the screen and Writing HTML Logfiles).
ThermoPhase thermo_t
typedef for the ThermoPhase class
Definition: ThermoPhase.h:1684
std::vector< int > vector_int
Vector of ints.
Definition: ct_defs.h:167
Interface class for the vcsnonlinear solver.
Contains declarations for string manipulation functions within Cantera.
int _equilflag(const char *xy)
map property strings to integers
Definition: ChemEquil.cpp:29
Chemical equilibrium.