Cantera  2.4.0
vcs_prob.cpp
Go to the documentation of this file.
1 /**
2  * @file vcs_prob.cpp
3  * Implementation for the Interface class for the vcs thermo
4  * equilibrium solver package,
5  */
6 
7 // This file is part of Cantera. See License.txt in the top-level directory or
8 // at http://www.cantera.org/license.txt for license and copyright information.
9 
13 #include "cantera/equil/vcs_defs.h"
17 
18 #include <cstdio>
19 
20 using namespace std;
21 
22 namespace Cantera
23 {
24 
25 void VCS_SOLVE::prob_report(int print_lvl)
26 {
27  m_printLvl = print_lvl;
28 
29  // Printout the species information: PhaseID's and mole nums
30  if (m_printLvl > 0) {
31  writeline('=', 80, true, true);
32  writeline('=', 20, false);
33  plogf(" VCS_PROB: PROBLEM STATEMENT ");
34  writeline('=', 31);
35  writeline('=', 80);
36  plogf("\n");
37  plogf("\tSolve a constant T, P problem:\n");
38  plogf("\t\tT = %g K\n", m_temperature);
39  double pres_atm = m_pressurePA / 1.01325E5;
40 
41  plogf("\t\tPres = %g atm\n", pres_atm);
42  plogf("\n");
43  plogf(" Phase IDs of species\n");
44  plogf(" species phaseID phaseName ");
45  plogf(" Initial_Estimated_Moles Species_Type\n");
46  for (size_t i = 0; i < m_nsp; i++) {
47  vcs_VolPhase* Vphase = m_VolPhaseList[m_phaseID[i]].get();
48  plogf("%16s %5d %16s", m_mix->speciesName(i), m_phaseID[i],
49  Vphase->PhaseName);
50  if (m_doEstimateEquil >= 0) {
51  plogf(" %-10.5g", m_molNumSpecies_old[i]);
52  } else {
53  plogf(" N/A");
54  }
55  if (m_speciesUnknownType[i] == VCS_SPECIES_TYPE_MOLNUM) {
56  plogf(" Mol_Num");
57  } else if (m_speciesUnknownType[i] == VCS_SPECIES_TYPE_INTERFACIALVOLTAGE) {
58  plogf(" Voltage");
59  } else {
60  plogf(" ");
61  }
62  plogf("\n");
63  }
64 
65  // Printout of the Phase structure information
66  writeline('-', 80, true, true);
67  plogf(" Information about phases\n");
68  plogf(" PhaseName PhaseNum SingSpec GasPhase "
69  " EqnState NumSpec");
70  plogf(" TMolesInert TKmoles\n");
71 
72  for (size_t iphase = 0; iphase < m_numPhases; iphase++) {
73  vcs_VolPhase* Vphase = m_VolPhaseList[iphase].get();
74  plogf("%16s %5d %5d %8d ", Vphase->PhaseName,
75  Vphase->VP_ID_, Vphase->m_singleSpecies, Vphase->m_gasPhase);
76  plogf("%16s %8d %16e ", Vphase->eos_name(),
77  Vphase->nSpecies(), Vphase->totalMolesInert());
78  if (m_doEstimateEquil >= 0) {
79  plogf("%16e\n", Vphase->totalMoles());
80  } else {
81  plogf(" N/A\n");
82  }
83  }
84 
85  plogf("\nElemental Abundances: ");
86  plogf(" Target_kmol ElemType ElActive\n");
87  for (size_t i = 0; i < m_nelem; ++i) {
88  writeline(' ', 26, false);
89  plogf("%-2.2s", m_elementName[i]);
90  plogf("%20.12E ", m_elemAbundancesGoal[i]);
91  plogf("%3d %3d\n", m_elType[i], m_elementActive[i]);
92  }
93 
94  plogf("\nChemical Potentials: (J/kmol)\n");
95  plogf(" Species (phase) "
96  " SS0ChemPot StarChemPot\n");
97  for (size_t iphase = 0; iphase < m_numPhases; iphase++) {
98  vcs_VolPhase* Vphase = m_VolPhaseList[iphase].get();
99  Vphase->setState_TP(m_temperature, m_pressurePA);
100  for (size_t kindex = 0; kindex < Vphase->nSpecies(); kindex++) {
101  size_t kglob = Vphase->spGlobalIndexVCS(kindex);
102  plogf("%16s ", m_mix->speciesName(kglob));
103  if (kindex == 0) {
104  plogf("%16s", Vphase->PhaseName);
105  } else {
106  plogf(" ");
107  }
108 
109  plogf("%16g %16g\n", Vphase->G0_calc_one(kindex),
110  Vphase->GStar_calc_one(kindex));
111  }
112  }
113  writeline('=', 80, true, true);
114  writeline('=', 20, false);
115  plogf(" VCS_PROB: END OF PROBLEM STATEMENT ");
116  writeline('=', 24);
117  writeline('=', 80);
118  plogf("\n");
119  }
120 }
121 
122 void VCS_SOLVE::addPhaseElements(vcs_VolPhase* volPhase)
123 {
124  size_t neVP = volPhase->nElemConstraints();
125  // Loop through the elements in the vol phase object
126  for (size_t eVP = 0; eVP < neVP; eVP++) {
127  size_t foundPos = npos;
128  std::string enVP = volPhase->elementName(eVP);
129 
130  // Search for matches with the existing elements. If found, then fill in
131  // the entry in the global mapping array.
132  for (size_t e = 0; e < m_nelem; e++) {
133  std::string en = m_elementName[e];
134  if (!strcmp(enVP.c_str(), en.c_str())) {
135  volPhase->setElemGlobalIndex(eVP, e);
136  foundPos = e;
137  }
138  }
139  if (foundPos == npos) {
140  int elType = volPhase->elementType(eVP);
141  int elactive = volPhase->elementActive(eVP);
142  size_t e = addElement(enVP.c_str(), elType, elactive);
143  volPhase->setElemGlobalIndex(eVP, e);
144  }
145  }
146 }
147 
148 size_t VCS_SOLVE::addElement(const char* elNameNew, int elType, int elactive)
149 {
150  if (!elNameNew) {
151  throw CanteraError("VCS_SOLVE::addElement",
152  "error: element must have a name");
153  }
154  m_nelem++;
155  m_numComponents++;
156 
157  m_formulaMatrix.resize(m_nsp, m_nelem, 0.0);
158  m_stoichCoeffRxnMatrix.resize(m_nelem, m_nsp, 0.0);
159  m_elType.push_back(elType);
160  m_elementActive.push_back(elactive);
161  m_elemAbundances.push_back(0.0);
162  m_elemAbundancesGoal.push_back(0.0);
163  m_elementMapIndex.push_back(0);
164  m_elementName.push_back(elNameNew);
165  return m_nelem - 1;
166 }
167 
168 size_t VCS_SOLVE::addOnePhaseSpecies(vcs_VolPhase* volPhase, size_t k, size_t kT)
169 {
170  if (kT > m_nsp) {
171  // Need to expand the number of species here
172  throw CanteraError("VCS_PROB::addOnePhaseSpecies", "Shouldn't be here");
173  }
174  const Array2D& fm = volPhase->getFormulaMatrix();
175  for (size_t eVP = 0; eVP < volPhase->nElemConstraints(); eVP++) {
176  size_t e = volPhase->elemGlobalIndex(eVP);
177  AssertThrowMsg(e != npos, "VCS_PROB::addOnePhaseSpecies",
178  "element not found");
179  m_formulaMatrix(kT,e) = fm(k,eVP);
180  }
181 
182  // Tell the phase object about the current position of the species within
183  // the global species vector
184  volPhase->setSpGlobalIndexVCS(k, kT);
185  return kT;
186 }
187 
188 }
bool m_singleSpecies
If true, this phase consists of a single species.
Definition: vcs_VolPhase.h:541
double totalMolesInert() const
Returns the value of the total kmol of inert in the phase.
size_t nElemConstraints() const
Returns the number of element constraints.
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:165
const Array2D & getFormulaMatrix() const
Get a constant form of the Species Formula Matrix.
size_t spGlobalIndexVCS(const size_t spIndex) const
Return the Global VCS index of the kth species in the phase.
STL namespace.
std::string elementName(const size_t e) const
Name of the element constraint with index e.
std::string PhaseName
String name for the phase.
Definition: vcs_VolPhase.h:627
size_t elemGlobalIndex(const size_t e) const
Returns the global index of the local element index for the phase.
Headers for the MultiPhase object that is used to set up multiphase equilibrium problems (see Equilfu...
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition: Array.h:31
Header for intermediate ThermoPhase object for phases which employ molality based activity coefficien...
size_t VP_ID_
Original ID of the phase in the problem.
Definition: vcs_VolPhase.h:538
Header file for the internal object that holds the vcs equilibrium problem (see Class VCS_SOLVE and E...
Header for the object representing each phase within vcs.
double GStar_calc_one(size_t kspec) const
Gibbs free energy calculation for standard state of one species.
#define VCS_SPECIES_TYPE_MOLNUM
Unknown refers to mole number of a single species.
Definition: vcs_defs.h:281
Defines and definitions within the vcs package.
Internal declarations for the VCSnonideal package.
std::string eos_name() const
Return the name corresponding to the equation of state.
size_t nSpecies() const
Return the number of species in the phase.
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:65
int elementType(const size_t e) const
Type of the element constraint with index e.
bool m_gasPhase
If true, this phase is a gas-phase like phase.
Definition: vcs_VolPhase.h:548
double totalMoles() const
Return the total moles in the phase.
Phase information and Phase calculations for vcs.
Definition: vcs_VolPhase.h:81
#define AssertThrowMsg(expr, procedure,...)
Assertion must be true or an error is thrown.
Definition: ctexceptions.h:264
void setSpGlobalIndexVCS(const size_t spIndex, const size_t spGlobalIndex)
set the Global VCS index of the kth species in the phase
#define plogf
define this Cantera function to replace printf
Definition: vcs_internal.h:18
#define VCS_SPECIES_TYPE_INTERFACIALVOLTAGE
Unknown refers to the voltage level of a phase.
Definition: vcs_defs.h:289
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:8
void setState_TP(const double temperature_Kelvin, const double pressure_PA)
Sets the temperature and pressure in this object and underlying ThermoPhase objects.
double G0_calc_one(size_t kspec) const
Gibbs free energy calculation at a temperature for the reference state of a species, return a value for one species.
void setElemGlobalIndex(const size_t eLocal, const size_t eGlobal)
sets a local phase element to a global index value