Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vcs_equilibrate.cpp
Go to the documentation of this file.
1 /**
2  * @file vcs_equilibrate.cpp
3  * Driver routines for equilibrium solvers
4  */
5 /*
6  * Copyright (2006) Sandia Corporation. Under the terms of
7  * Contract DE-AC04-94AL85000 with Sandia Corporation, the
8  * U.S. Government retains certain rights in this software.
9  */
11 #include "cantera/equil/equil.h"
12 
15 
16 using namespace std;
17 
18 namespace Cantera
19 {
20 int vcs_equilibrate(thermo_t& s, const char* XY,
21  int estimateEquil, int printLvl,
22  int solver,
23  doublereal rtol, int maxsteps, int maxiter,
24  int loglevel)
25 {
26  warn_deprecated("vcs_equilibrate", "Use ThermoPhase::equilibrate instead. "
27  "To be removed after Cantera 2.2.");
28  MultiPhase* m = 0;
29  int retn = 1;
30 
31  if (solver == 2) {
32  m = new MultiPhase;
33  try {
34  /*
35  * Set the kmoles of the phase to 1.0, arbitrarily.
36  * It actually doesn't matter.
37  */
38  m->addPhase(&s, 1.0);
39  m->init();
40 
41  retn = vcs_equilibrate(*m, XY, estimateEquil, printLvl, solver,
42  rtol, maxsteps, maxiter, loglevel);
43  delete m;
44  } catch (CanteraError& err) {
45  err.save();
46  delete m;
47  throw err;
48  }
49  } else if (solver == 1) {
50  m = new MultiPhase;
51  try {
52  m->addPhase(&s, 1.0);
53  m->init();
54  (void) equilibrate(*m, XY, rtol, maxsteps, maxiter, loglevel-1);
55  delete m;
56  retn = 1;
57  } catch (CanteraError& err) {
58  err.save();
59  delete m;
60  throw err;
61  }
62  } else if (solver == 0) {
63  ChemEquil* e = new ChemEquil;
64  try {
65  e->options.maxIterations = maxsteps;
66  e->options.relTolerance = rtol;
67  bool useThermoPhaseElementPotentials = false;
68  if (estimateEquil == 0) {
69  useThermoPhaseElementPotentials = true;
70  }
71  int retnSub = e->equilibrate(s, XY,
72  useThermoPhaseElementPotentials, loglevel-1);
73  if (retnSub < 0) {
74  delete e;
75  throw CanteraError("equilibrate",
76  "ChemEquil equilibrium solver failed");
77  }
78  retn = 1;
79  s.setElementPotentials(e->elementPotentials());
80  delete e;
81  } catch (CanteraError& err) {
82  err.save();
83  delete e;
84  throw err;
85  }
86  } else {
87  throw CanteraError("vcs_equilibrate",
88  "unknown solver");
89  }
90 
91  /*
92  * We are here only for a success
93  */
94  return retn;
95 }
96 
97 int vcs_equilibrate(MultiPhase& s, const char* XY,
98  int estimateEquil, int printLvl, int solver,
99  doublereal tol, int maxsteps, int maxiter,
100  int loglevel)
101 {
102  int ixy = _equilflag(XY);
103  int retn = vcs_equilibrate_1(s, ixy, estimateEquil, printLvl, solver,
104  tol, maxsteps, maxiter, loglevel);
105  return retn;
106 }
107 
109  int estimateEquil, int printLvl, int solver,
110  doublereal tol, int maxsteps, int maxiter, int loglevel)
111 {
112  warn_deprecated("vcs_equilibrate_1", "Use MultiPhase::equilibrate instead. "
113  "To be removed after Cantera 2.2.");
114  static int counter = 0;
115  int retn = 1;
116 
117  int printLvlSub = std::max(0, printLvl-1);
118 
119  s.init();
120 
121  if (solver == 2) {
122  try {
123  vcs_MultiPhaseEquil* eqsolve = new vcs_MultiPhaseEquil(&s, printLvlSub);
124  int err = eqsolve->equilibrate(ixy, estimateEquil, printLvlSub, tol, maxsteps, loglevel);
125  if (err != 0) {
126  retn = -1;
127  }
128  // hard code a csv output file.
129  if (printLvl > 0) {
130  string reportFile = "vcs_equilibrate_res.csv";
131  if (counter > 0) {
132  reportFile = "vcs_equilibrate_res_" + int2str(counter) + ".csv";
133  }
134  eqsolve->reportCSV(reportFile);
135  counter++;
136  }
137  delete eqsolve;
138  } catch (CanteraError& e) {
139  e.save();
140  retn = -1;
141  throw e;
142  }
143  } else if (solver == 1) {
144  if (ixy == TP || ixy == HP || ixy == SP || ixy == TV) {
145  try {
146  s.equilibrate(ixy, tol, maxsteps, maxiter, loglevel);
147  return 0;
148  } catch (CanteraError& e) {
149  e.save();
150  throw e;
151  }
152  } else {
153  throw CanteraError("equilibrate","unsupported option");
154  }
155  } else {
156  throw CanteraError("vcs_equilibrate_1", "unknown solver");
157  }
158  return retn;
159 }
160 
162  double& funcStab, int printLvl, int loglevel)
163 {
164  int iStab = 0;
165  static int counter = 0;
166  int printLvlSub = std::max(0, printLvl-1);
167 
168  s.init();
169  try {
170  vcs_MultiPhaseEquil* eqsolve = new vcs_MultiPhaseEquil(&s, printLvlSub);
171  iStab = eqsolve->determine_PhaseStability(iphase, funcStab, printLvlSub, loglevel);
172  // hard code a csv output file.
173  if (printLvl > 0) {
174  string reportFile = "vcs_phaseStability.csv";
175  if (counter > 0) {
176  reportFile = "vcs_phaseStability_" + int2str(counter) + ".csv";
177  }
178  eqsolve->reportCSV(reportFile);
179  counter++;
180  }
181  delete eqsolve;
182  } catch (CanteraError& e) {
183  throw e;
184  }
185  return iStab;
186 }
187 
188 }
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
std::string int2str(const int n, const std::string &fmt)
Convert an int to a string using a format converter.
Definition: stringUtils.cpp:39
doublereal relTolerance
Relative tolerance.
Definition: ChemEquil.h:35
This file contains the definition of some high level general equilibration routines.
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
void reportCSV(const std::string &reportFile)
Report the equilibrium answer in a comma separated table format.
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:97
Cantera's Interface to the Multiphase chemical equilibrium solver.
A class for multiphase mixtures.
Definition: MultiPhase.h:53
int vcs_determine_PhaseStability(MultiPhase &s, int iphase, double &funcStab, int printLvl, int loglevel)
Determine the phase stability of a single phase given the current conditions in a MultiPhase object...
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:99
int equilibrate(int XY, int estimateEquil=0, int printLvl=0, doublereal err=1.0e-6, int maxsteps=VCS_MAXSTEPS, int loglevel=-99)
Equilibrate the solution using the current element abundances stored in the MultiPhase object...
int vcs_equilibrate(MultiPhase &s, const char *XY, int estimateEquil, int printLvl, int solver, doublereal tol, int maxsteps, int maxiter, int loglevel)
Set a multi-phase chemical solution to chemical equilibrium.
int determine_PhaseStability(int iph, double &funcStab, int printLvl=0, int logLevel=-99)
Determine the phase stability of a phase at the current conditions.
Interface class for the vcsnonlinear solver.
void init()
Process phases and build atomic composition array.
Definition: MultiPhase.cpp:160
Contains declarations for string manipulation functions within Cantera.
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
Chemical equilibrium.
int vcs_equilibrate_1(MultiPhase &s, int ixy, int estimateEquil, int printLvl, int solver, doublereal tol, int maxsteps, int maxiter, int loglevel)
Set a multi-phase chemical solution to chemical equilibrium.
void save()
Function to put this error onto Cantera's error stack.