Cantera  2.4.0
IdealSolnGasVPSS.cpp
Go to the documentation of this file.
1 /**
2  * @file IdealSolnGasVPSS.cpp
3  * Definition file for a derived class of ThermoPhase that assumes either
4  * an ideal gas or ideal solution approximation and handles
5  * variable pressure standard state methods for calculating
6  * thermodynamic properties (see \ref thermoprops and
7  * class \link Cantera::IdealSolnGasVPSS IdealSolnGasVPSS\endlink).
8  */
9 
10 // This file is part of Cantera. See License.txt in the top-level directory or
11 // at http://www.cantera.org/license.txt for license and copyright information.
12 
14 #include "cantera/thermo/PDSS.h"
17 #include "cantera/base/utilities.h"
18 
19 using namespace std;
20 
21 namespace Cantera
22 {
23 
24 IdealSolnGasVPSS::IdealSolnGasVPSS() :
25  m_idealGas(-1),
26  m_formGC(0)
27 {
28 }
29 
30 IdealSolnGasVPSS::IdealSolnGasVPSS(const std::string& infile, std::string id_) :
31  m_idealGas(0),
32  m_formGC(0)
33 {
34  XML_Node* root = get_XML_File(infile);
35  if (id_ == "-") {
36  id_ = "";
37  }
38  XML_Node* xphase = get_XML_NameID("phase", std::string("#")+id_, root);
39  if (!xphase) {
40  throw CanteraError("newPhase",
41  "Couldn't find phase named \"" + id_ + "\" in file, " + infile);
42  }
43  importPhase(*xphase, this);
44 }
45 
47 {
48  if (m_idealGas) {
49  throw CanteraError("IdealSolnGasVPSS::setStandardConcentrationModel",
50  "Standard concentration model not applicable for ideal gas");
51  }
52 
53  if (caseInsensitiveEquals(model, "unity")) {
54  m_formGC = 0;
55  } else if (caseInsensitiveEquals(model, "molar_volume")) {
56  m_formGC = 1;
57  } else if (caseInsensitiveEquals(model, "solvent_volume")) {
58  m_formGC = 2;
59  } else {
60  throw CanteraError("IdealSolnGasVPSS::setStandardConcentrationModel",
61  "Unknown standard concentration model '{}'", model);
62  }
63 }
64 
65 // ------------Molar Thermodynamic Properties -------------------------
66 
68 {
70  return RT() * mean_X(m_hss_RT);
71 }
72 
74 {
76  return GasConstant * (mean_X(m_sss_R) - sum_xlogx());
77 }
78 
79 doublereal IdealSolnGasVPSS::cp_mole() const
80 {
82  return GasConstant * mean_X(m_cpss_R);
83 }
84 
85 doublereal IdealSolnGasVPSS::cv_mole() const
86 {
87  return cp_mole() - GasConstant;
88 }
89 
91 {
92  m_Pcurrent = p;
94  calcDensity();
95 }
96 
98 {
99  // Calculate the molarVolume of the solution (m**3 kmol-1)
100  if (m_idealGas) {
101  double dens = m_Pcurrent * meanMolecularWeight() / RT();
102  Phase::setDensity(dens);
103  } else {
104  const doublereal* const dtmp = moleFractdivMMW();
105  const vector_fp& vss = getStandardVolumes();
106  double dens = 1.0 / dot(vss.begin(), vss.end(), dtmp);
107 
108  // Set the density in the parent State object directly
109  Phase::setDensity(dens);
110  }
111 }
112 
114 {
115  if (m_idealGas) {
116  return 1.0 / m_Pcurrent;
117  } else {
118  throw CanteraError("IdealSolnGasVPSS::isothermalCompressibility() ",
119  "not implemented");
120  }
121  return 0.0;
122 }
123 
125 {
126  if (m_idealGas) {
128  } else {
129  const vector_fp& vss = getStandardVolumes();
130  switch (m_formGC) {
131  case 0:
132  for (size_t k = 0; k < m_kk; k++) {
133  c[k] = moleFraction(k);
134  }
135  break;
136  case 1:
137  for (size_t k = 0; k < m_kk; k++) {
138  c[k] = moleFraction(k) / vss[k];
139  }
140  break;
141  case 2:
142  for (size_t k = 0; k < m_kk; k++) {
143  c[k] = moleFraction(k) / vss[0];
144  }
145  break;
146  }
147  }
148 }
149 
150 doublereal IdealSolnGasVPSS::standardConcentration(size_t k) const
151 {
152  if (m_idealGas) {
153  return pressure() / RT();
154  } else {
155  const vector_fp& vss = getStandardVolumes();
156  switch (m_formGC) {
157  case 0:
158  return 1.0;
159  case 1:
160  return 1.0 / vss[k];
161  case 2:
162  return 1.0/ vss[0];
163  }
164  return 0.0;
165  }
166 }
167 
169 {
170  for (size_t k = 0; k < m_kk; k++) {
171  ac[k] = 1.0;
172  }
173 }
174 
175 // ---- Partial Molar Properties of the Solution -----------------
176 
177 void IdealSolnGasVPSS::getChemPotentials(doublereal* mu) const
178 {
180  for (size_t k = 0; k < m_kk; k++) {
181  double xx = std::max(SmallNumber, moleFraction(k));
182  mu[k] += RT() * log(xx);
183  }
184 }
185 
187 {
188  getEnthalpy_RT(hbar);
189  scale(hbar, hbar+m_kk, hbar, RT());
190 }
191 
192 void IdealSolnGasVPSS::getPartialMolarEntropies(doublereal* sbar) const
193 {
194  getEntropy_R(sbar);
195  scale(sbar, sbar+m_kk, sbar, GasConstant);
196  for (size_t k = 0; k < m_kk; k++) {
197  doublereal xx = std::max(SmallNumber, moleFraction(k));
198  sbar[k] += GasConstant * (- log(xx));
199  }
200 }
201 
203 {
204  getIntEnergy_RT(ubar);
205  scale(ubar, ubar+m_kk, ubar, RT());
206 }
207 
208 void IdealSolnGasVPSS::getPartialMolarCp(doublereal* cpbar) const
209 {
210  getCp_R(cpbar);
211  scale(cpbar, cpbar+m_kk, cpbar, GasConstant);
212 }
213 
214 void IdealSolnGasVPSS::getPartialMolarVolumes(doublereal* vbar) const
215 {
216  getStandardVolumes(vbar);
217 }
218 
219 void IdealSolnGasVPSS::setToEquilState(const doublereal* mu_RT)
220 {
222 
223  // Within the method, we protect against inf results if the exponent is too
224  // high.
225  //
226  // If it is too low, we set the partial pressure to zero. This capability is
227  // needed by the elemental potential method.
228  doublereal pres = 0.0;
229  double m_p0 = refPressure();
230  for (size_t k = 0; k < m_kk; k++) {
231  double tmp = -m_g0_RT[k] + mu_RT[k];
232  if (tmp < -600.) {
233  m_pp[k] = 0.0;
234  } else if (tmp > 500.0) {
235  double tmp2 = tmp / 500.;
236  tmp2 *= tmp2;
237  m_pp[k] = m_p0 * exp(500.) * tmp2;
238  } else {
239  m_pp[k] = m_p0 * exp(tmp);
240  }
241  pres += m_pp[k];
242  }
243  // set state
244  setState_PX(pres, &m_pp[0]);
245 }
246 
247 bool IdealSolnGasVPSS::addSpecies(shared_ptr<Species> spec)
248 {
249  bool added = VPStandardStateTP::addSpecies(spec);
250  if (added) {
251  m_pp.push_back(0.0);
252  }
253  return added;
254 }
255 
257 {
259  if (m_idealGas == -1) {
260  throw CanteraError("IdealSolnGasVPSS::initThermo",
261  "solution / gas mode not set");
262  }
263 }
264 
265 void IdealSolnGasVPSS::initThermoXML(XML_Node& phaseNode, const std::string& id_)
266 {
267  if (phaseNode.hasChild("thermo")) {
268  XML_Node& thermoNode = phaseNode.child("thermo");
269  std::string model = thermoNode["model"];
270  if (model == "IdealGasVPSS") {
271  setGasMode();
272  } else if (model == "IdealSolnVPSS") {
273  setSolnMode();
274  } else {
275  throw CanteraError("IdealSolnGasVPSS::initThermoXML",
276  "Unknown thermo model : " + model);
277  }
278  }
279 
280  // Form of the standard concentrations. Must have one of:
281  //
282  // <standardConc model="unity" />
283  // <standardConc model="molar_volume" />
284  // <standardConc model="solvent_volume" />
285  if (phaseNode.hasChild("standardConc")) {
286  XML_Node& scNode = phaseNode.child("standardConc");
287  setStandardConcentrationModel(scNode.attrib("model"));
288  } else if (!m_idealGas) {
289  throw CanteraError("IdealSolnGasVPSS::initThermoXML",
290  "Unspecified standardConc model");
291  }
292 
293  VPStandardStateTP::initThermoXML(phaseNode, id_);
294 }
295 
297 {
299  std::string model = thermoNode["model"];
300  if (model == "IdealGasVPSS") {
301  setGasMode();
302  } else if (model == "IdealSolnVPSS") {
303  setSolnMode();
304  } else {
305  throw CanteraError("IdealSolnGasVPSS::initThermoXML",
306  "Unknown thermo model : " + model);
307  }
308 }
309 
310 }
virtual void updateStandardStateThermo() const
Updates the standard state thermodynamic functions at the current T and P of the solution.
virtual doublereal cv_mole() const
Molar heat capacity at constant volume. Units: J/kmol/K.
virtual doublereal entropy_mole() const
Molar entropy. Units: J/kmol/K.
XML_Node * get_XML_File(const std::string &file, int debug)
Return a pointer to the XML tree for a Cantera input file.
Definition: global.cpp:105
vector_fp m_sss_R
Vector containing the species Standard State entropies at T = m_tlast and P = m_plast.
Various templated functions that carry out common vector operations (see Templated Utility Functions)...
doublereal moleFraction(size_t k) const
Return the mole fraction of a single species.
Definition: Phase.cpp:471
Definition file for a derived class of ThermoPhase that assumes either an ideal gas or ideal solution...
doublereal sum_xlogx() const
Evaluate .
Definition: Phase.cpp:624
Headers for the factory class that can create known ThermoPhase objects (see Thermodynamic Properties...
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:97
virtual void getCp_R(doublereal *cpr) const
Get the nondimensional Heat Capacities at constant pressure for the species standard states at the cu...
virtual doublereal enthalpy_mole() const
Molar enthalpy. Units: J/kmol.
STL namespace.
virtual void getPartialMolarIntEnergies(doublereal *ubar) const
Return an array of partial molar internal energies for the species in the mixture.
virtual bool addSpecies(shared_ptr< Species > spec)
vector_fp m_pp
Temporary storage - length = m_kk.
doublereal mean_X(const doublereal *const Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
Definition: Phase.cpp:614
doublereal RT() const
Return the Gas Constant multiplied by the current temperature.
Definition: ThermoPhase.h:748
virtual bool addSpecies(shared_ptr< Species > spec)
void setStandardConcentrationModel(const std::string &model)
Set the standard concentration model.
Declarations for the virtual base class PDSS (pressure dependent standard state) which handles calcul...
virtual void calcDensity()
Calculate the density of the mixture using the partial molar volumes and mole fractions as input...
virtual doublereal standardConcentration(size_t k=0) const
Returns the standard concentration , which is used to normalize the generalized concentration.
const doublereal * moleFractdivMMW() const
Returns a const pointer to the start of the moleFraction/MW array.
Definition: Phase.cpp:487
int m_formGC
form of the generalized concentrations
bool caseInsensitiveEquals(const std::string &input, const std::string &test)
Case insensitive equality predicate.
virtual void getStandardChemPotentials(doublereal *mu) const
Get the array of chemical potentials at unit activity for the species at their standard states at the...
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:65
vector_fp m_cpss_R
Vector containing the species Standard State constant pressure heat capacities at T = m_tlast and P =...
void setSolnMode()
Set this phase to represent an ideal liquid or solid solution.
void importPhase(XML_Node &phase, ThermoPhase *th)
Import a phase information into an empty ThermoPhase object.
int m_idealGas
boolean indicating what ideal solution this is
doublereal dot(InputIter x_begin, InputIter x_end, InputIter2 y_begin)
Function that calculates a templated inner product.
Definition: utilities.h:107
virtual doublereal pressure() const
Returns the current pressure of the phase.
void setPressure(doublereal p)
Set the internally stored pressure (Pa) at constant temperature and composition.
bool hasChild(const std::string &ch) const
Tests whether the current node has a child node with a particular name.
Definition: xml.cpp:536
XML_Node & child(const size_t n) const
Return a changeable reference to the n&#39;th child of the current node.
Definition: xml.cpp:546
virtual void setParametersFromXML(const XML_Node &thermoNode)
Set equation of state parameter values from XML entries.
virtual void getIntEnergy_RT(doublereal *urt) const
Returns the vector of nondimensional Internal Energies of the standard state species at the current T...
virtual void getPartialMolarEnthalpies(doublereal *hbar) const
Returns an array of partial molar enthalpies for the species in the mixture.
virtual void getPartialMolarCp(doublereal *cpbar) const
Return an array of partial molar heat capacities for the species in the mixture.
virtual void initThermoXML(XML_Node &phaseNode, const std::string &id)
Import and initialize a ThermoPhase object using an XML tree.
const doublereal SmallNumber
smallest number to compare to zero.
Definition: ct_defs.h:126
std::string attrib(const std::string &attr) const
Function returns the value of an attribute.
Definition: xml.cpp:500
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Definition: ct_defs.h:157
doublereal meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition: Phase.h:661
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition: utilities.h:130
vector_fp m_hss_RT
Vector containing the species Standard State enthalpies at T = m_tlast and P = m_plast.
virtual doublereal refPressure() const
Returns the reference pressure in Pa.
Definition: ThermoPhase.h:116
virtual void setParametersFromXML(const XML_Node &eosdata)
Set equation of state parameter values from XML entries.
Definition: ThermoPhase.h:1468
vector_fp m_g0_RT
Vector containing the species reference Gibbs functions at T = m_tlast and P = p_ref.
const doublereal GasConstant
Universal Gas Constant. [J/kmol/K].
Definition: ct_defs.h:64
virtual void setState_PX(doublereal p, doublereal *x)
Set the pressure (Pa) and mole fractions.
Contains declarations for string manipulation functions within Cantera.
virtual void getActivityConcentrations(doublereal *c) const
This method returns an array of generalized concentrations.
virtual void getActivityCoefficients(doublereal *ac) const
Get the array of non-dimensional activity coefficients at the current solution temperature, pressure, and solution concentration.
virtual void initThermoXML(XML_Node &phaseNode, const std::string &id)
Import and initialize a ThermoPhase object using an XML tree.
size_t m_kk
Number of species in the phase.
Definition: Phase.h:788
virtual void getEntropy_R(doublereal *sr) const
Get the array of nondimensional Entropy functions for the standard state species at the current T and...
void setGasMode()
Set this phase to represent an ideal gas.
virtual void getPartialMolarEntropies(doublereal *sbar) const
Returns an array of partial molar entropies of the species in the solution.
doublereal m_Pcurrent
Current value of the pressure - state variable.
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:8
void getConcentrations(doublereal *const c) const
Get the species concentrations (kmol/m^3).
Definition: Phase.cpp:519
virtual void getEnthalpy_RT(doublereal *hrt) const
Get the nondimensional Enthalpy functions for the species at their standard states at the current T a...
XML_Node * get_XML_NameID(const std::string &nameTarget, const std::string &file_ID, XML_Node *root)
This routine will locate an XML node in either the input XML tree or in another input file specified ...
Definition: global.cpp:227
virtual doublereal isothermalCompressibility() const
Returns the isothermal compressibility. Units: 1/Pa.
virtual doublereal cp_mole() const
Molar heat capacity at constant pressure. Units: J/kmol/K.
virtual void getStandardVolumes(doublereal *vol) const
Get the molar volumes of the species standard states at the current T and P of the solution...
virtual void setToEquilState(const doublereal *lambda_RT)
This method is used by the ChemEquil equilibrium solver.
virtual void setDensity(const doublereal density_)
Set the internally stored density (kg/m^3) of the phase.
Definition: Phase.h:622
virtual void getChemPotentials(doublereal *mu) const
Get the species chemical potentials. Units: J/kmol.
virtual void getPartialMolarVolumes(doublereal *vbar) const
Return an array of partial molar volumes for the species in the mixture.