Cantera  2.5.1
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 https://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("IdealSolnGasVPSS::IdealSolnGasVPSS",
41  "Couldn't find phase named '{} in file '{}'",
42  id_, infile);
43  }
44  importPhase(*xphase, this);
45 }
46 
48 {
49  if (m_idealGas) {
50  throw CanteraError("IdealSolnGasVPSS::setStandardConcentrationModel",
51  "Standard concentration model not applicable for ideal gas");
52  }
53 
54  if (caseInsensitiveEquals(model, "unity")) {
55  m_formGC = 0;
56  } else if (caseInsensitiveEquals(model, "species-molar-volume")
57  || caseInsensitiveEquals(model, "molar_volume")) {
58  m_formGC = 1;
59  } else if (caseInsensitiveEquals(model, "solvent-molar-volume")
60  || caseInsensitiveEquals(model, "solvent_volume")) {
61  m_formGC = 2;
62  } else {
63  throw CanteraError("IdealSolnGasVPSS::setStandardConcentrationModel",
64  "Unknown standard concentration model '{}'", model);
65  }
66 }
67 
68 // ------------Molar Thermodynamic Properties -------------------------
69 
71 {
73  return RT() * mean_X(m_hss_RT);
74 }
75 
77 {
79  return GasConstant * (mean_X(m_sss_R) - sum_xlogx());
80 }
81 
82 doublereal IdealSolnGasVPSS::cp_mole() const
83 {
85  return GasConstant * mean_X(m_cpss_R);
86 }
87 
88 doublereal IdealSolnGasVPSS::cv_mole() const
89 {
90  return cp_mole() - GasConstant;
91 }
92 
94 {
95  m_Pcurrent = p;
97  calcDensity();
98 }
99 
101 {
102  // Calculate the molarVolume of the solution (m**3 kmol-1)
103  if (m_idealGas) {
104  double dens = m_Pcurrent * meanMolecularWeight() / RT();
105  Phase::assignDensity(dens);
106  } else {
107  const doublereal* const dtmp = moleFractdivMMW();
108  const vector_fp& vss = getStandardVolumes();
109  double dens = 1.0 / dot(vss.begin(), vss.end(), dtmp);
110 
111  // Set the density in the parent State object directly
112  Phase::assignDensity(dens);
113  }
114 }
115 
117 {
118  if (m_idealGas) {
119  return 1.0 / m_Pcurrent;
120  } else {
121  throw NotImplementedError("IdealSolnGasVPSS::isothermalCompressibility");
122  }
123 }
124 
126 {
127  if (m_idealGas || m_formGC != 0) {
128  return Units(1.0, 0, -3, 0, 0, 0, 1);
129  } else {
130  return Units(1.0);
131  }
132 }
133 
135 {
136  if (m_idealGas) {
138  } else {
139  const vector_fp& vss = getStandardVolumes();
140  switch (m_formGC) {
141  case 0:
142  for (size_t k = 0; k < m_kk; k++) {
143  c[k] = moleFraction(k);
144  }
145  break;
146  case 1:
147  for (size_t k = 0; k < m_kk; k++) {
148  c[k] = moleFraction(k) / vss[k];
149  }
150  break;
151  case 2:
152  for (size_t k = 0; k < m_kk; k++) {
153  c[k] = moleFraction(k) / vss[0];
154  }
155  break;
156  }
157  }
158 }
159 
160 doublereal IdealSolnGasVPSS::standardConcentration(size_t k) const
161 {
162  if (m_idealGas) {
163  return pressure() / RT();
164  } else {
165  const vector_fp& vss = getStandardVolumes();
166  switch (m_formGC) {
167  case 0:
168  return 1.0;
169  case 1:
170  return 1.0 / vss[k];
171  case 2:
172  return 1.0/ vss[0];
173  }
174  return 0.0;
175  }
176 }
177 
179 {
180  for (size_t k = 0; k < m_kk; k++) {
181  ac[k] = 1.0;
182  }
183 }
184 
185 // ---- Partial Molar Properties of the Solution -----------------
186 
187 void IdealSolnGasVPSS::getChemPotentials(doublereal* mu) const
188 {
190  for (size_t k = 0; k < m_kk; k++) {
191  double xx = std::max(SmallNumber, moleFraction(k));
192  mu[k] += RT() * log(xx);
193  }
194 }
195 
197 {
198  getEnthalpy_RT(hbar);
199  scale(hbar, hbar+m_kk, hbar, RT());
200 }
201 
202 void IdealSolnGasVPSS::getPartialMolarEntropies(doublereal* sbar) const
203 {
204  getEntropy_R(sbar);
205  scale(sbar, sbar+m_kk, sbar, GasConstant);
206  for (size_t k = 0; k < m_kk; k++) {
207  doublereal xx = std::max(SmallNumber, moleFraction(k));
208  sbar[k] += GasConstant * (- log(xx));
209  }
210 }
211 
213 {
214  getIntEnergy_RT(ubar);
215  scale(ubar, ubar+m_kk, ubar, RT());
216 }
217 
218 void IdealSolnGasVPSS::getPartialMolarCp(doublereal* cpbar) const
219 {
220  getCp_R(cpbar);
221  scale(cpbar, cpbar+m_kk, cpbar, GasConstant);
222 }
223 
224 void IdealSolnGasVPSS::getPartialMolarVolumes(doublereal* vbar) const
225 {
226  getStandardVolumes(vbar);
227 }
228 
229 void IdealSolnGasVPSS::setToEquilState(const doublereal* mu_RT)
230 {
232 
233  // Within the method, we protect against inf results if the exponent is too
234  // high.
235  //
236  // If it is too low, we set the partial pressure to zero. This capability is
237  // needed by the elemental potential method.
238  doublereal pres = 0.0;
239  double m_p0 = refPressure();
240  for (size_t k = 0; k < m_kk; k++) {
241  double tmp = -m_g0_RT[k] + mu_RT[k];
242  if (tmp < -600.) {
243  m_pp[k] = 0.0;
244  } else if (tmp > 500.0) {
245  double tmp2 = tmp / 500.;
246  tmp2 *= tmp2;
247  m_pp[k] = m_p0 * exp(500.) * tmp2;
248  } else {
249  m_pp[k] = m_p0 * exp(tmp);
250  }
251  pres += m_pp[k];
252  }
253  // set state
254  setState_PX(pres, &m_pp[0]);
255 }
256 
257 bool IdealSolnGasVPSS::addSpecies(shared_ptr<Species> spec)
258 {
259  bool added = VPStandardStateTP::addSpecies(spec);
260  if (added) {
261  m_pp.push_back(0.0);
262  }
263  return added;
264 }
265 
267 {
269  if (m_input.hasKey("thermo")) {
270  const string& model = m_input["thermo"].asString();
271  if (model == "ideal-solution-VPSS") {
272  setSolnMode();
273  } else if (model == "ideal-gas-VPSS") {
274  setGasMode();
275  }
276  }
277  if (m_input.hasKey("standard-concentration-basis")) {
278  setStandardConcentrationModel(m_input["standard-concentration-basis"].asString());
279  }
280 
281  if (m_idealGas == -1) {
282  throw CanteraError("IdealSolnGasVPSS::initThermo",
283  "solution / gas mode not set");
284  }
285 }
286 
287 void IdealSolnGasVPSS::initThermoXML(XML_Node& phaseNode, const std::string& id_)
288 {
289  if (phaseNode.hasChild("thermo")) {
290  XML_Node& thermoNode = phaseNode.child("thermo");
291  std::string model = thermoNode["model"];
292  if (model == "IdealGasVPSS") {
293  setGasMode();
294  } else if (model == "IdealSolnVPSS") {
295  setSolnMode();
296  } else {
297  throw CanteraError("IdealSolnGasVPSS::initThermoXML",
298  "Unknown thermo model : " + model);
299  }
300  }
301 
302  // Form of the standard concentrations. Must have one of:
303  //
304  // <standardConc model="unity" />
305  // <standardConc model="molar_volume" />
306  // <standardConc model="solvent_volume" />
307  if (phaseNode.hasChild("standardConc")) {
308  XML_Node& scNode = phaseNode.child("standardConc");
309  setStandardConcentrationModel(scNode.attrib("model"));
310  } else if (!m_idealGas) {
311  throw CanteraError("IdealSolnGasVPSS::initThermoXML",
312  "Unspecified standardConc model");
313  }
314 
315  VPStandardStateTP::initThermoXML(phaseNode, id_);
316 }
317 
319 {
321  std::string model = thermoNode["model"];
322  if (model == "IdealGasVPSS") {
323  setGasMode();
324  } else if (model == "IdealSolnVPSS") {
325  setSolnMode();
326  } else {
327  throw CanteraError("IdealSolnGasVPSS::setParametersFromXML",
328  "Unknown thermo model : " + model);
329  }
330 }
331 
332 }
Definition file for a derived class of ThermoPhase that assumes either an ideal gas or ideal solution...
Declarations for the virtual base class PDSS (pressure dependent standard state) which handles calcul...
Headers for the factory class that can create known ThermoPhase objects (see Thermodynamic Properties...
bool hasKey(const std::string &key) const
Returns true if the map contains an item named key.
Definition: AnyMap.cpp:984
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
virtual void setToEquilState(const doublereal *lambda_RT)
This method is used by the ChemEquil equilibrium solver.
virtual void getActivityCoefficients(doublereal *ac) const
Get the array of non-dimensional activity coefficients at the current solution temperature,...
virtual void getPartialMolarIntEnergies(doublereal *ubar) const
Return an array of partial molar internal energies for the species in the mixture.
virtual doublereal cp_mole() const
Molar heat capacity at constant pressure. Units: J/kmol/K.
virtual void getPartialMolarEnthalpies(doublereal *hbar) const
Returns an array of partial molar enthalpies for the species in the mixture.
virtual void getPartialMolarEntropies(doublereal *sbar) const
Returns an array of partial molar entropies of the species in the solution.
virtual void initThermoXML(XML_Node &phaseNode, const std::string &id)
Import and initialize a ThermoPhase object using an XML tree.
void setGasMode()
Set this phase to represent an ideal gas.
virtual doublereal enthalpy_mole() const
Molar enthalpy. Units: J/kmol.
virtual void getPartialMolarVolumes(doublereal *vbar) const
Return an array of partial molar volumes for the species in the mixture.
vector_fp m_pp
Temporary storage - length = m_kk.
virtual doublereal cv_mole() const
Molar heat capacity at constant volume. Units: J/kmol/K.
virtual void getActivityConcentrations(doublereal *c) const
This method returns an array of generalized concentrations.
virtual void getPartialMolarCp(doublereal *cpbar) const
Return an array of partial molar heat capacities for the species in the mixture.
void setPressure(doublereal p)
Set the internally stored pressure (Pa) at constant temperature and composition.
virtual doublereal entropy_mole() const
Molar entropy. Units: J/kmol/K.
void setSolnMode()
Set this phase to represent an ideal liquid or solid solution.
int m_formGC
form of the generalized concentrations
virtual Units standardConcentrationUnits() const
Returns the units of the "standard concentration" for this phase.
void setStandardConcentrationModel(const std::string &model)
Set the standard concentration model.
virtual void getChemPotentials(doublereal *mu) const
Get the species chemical potentials. Units: J/kmol.
virtual void setParametersFromXML(const XML_Node &thermoNode)
Set equation of state parameter values from XML entries.
virtual void calcDensity()
Calculate the density of the mixture using the partial molar volumes and mole fractions as input.
virtual doublereal isothermalCompressibility() const
Returns the isothermal compressibility. Units: 1/Pa.
int m_idealGas
boolean indicating what ideal solution this is
virtual doublereal standardConcentration(size_t k=0) const
Returns the standard concentration , which is used to normalize the generalized concentration.
An error indicating that an unimplemented function has been called.
Definition: ctexceptions.h:187
void getConcentrations(double *const c) const
Get the species concentrations (kmol/m^3).
Definition: Phase.cpp:625
void assignDensity(const double density_)
Set the internally stored constant density (kg/m^3) of the phase.
Definition: Phase.cpp:727
doublereal mean_X(const doublereal *const Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
Definition: Phase.cpp:746
const double * moleFractdivMMW() const
Returns a const pointer to the start of the moleFraction/MW array.
Definition: Phase.cpp:593
size_t m_kk
Number of species in the phase.
Definition: Phase.h:942
doublereal meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition: Phase.h:748
double moleFraction(size_t k) const
Return the mole fraction of a single species.
Definition: Phase.cpp:577
doublereal sum_xlogx() const
Evaluate .
Definition: Phase.cpp:756
doublereal RT() const
Return the Gas Constant multiplied by the current temperature.
Definition: ThermoPhase.h:776
virtual void initThermoXML(XML_Node &phaseNode, const std::string &id)
Import and initialize a ThermoPhase object using an XML tree.
virtual void setState_PX(doublereal p, doublereal *x)
Set the pressure (Pa) and mole fractions.
virtual doublereal refPressure() const
Returns the reference pressure in Pa.
Definition: ThermoPhase.h:145
AnyMap m_input
Data supplied via setParameters.
Definition: ThermoPhase.h:1874
virtual void setParametersFromXML(const XML_Node &eosdata)
Set equation of state parameter values from XML entries.
Definition: ThermoPhase.h:1728
A representation of the units associated with a dimensional quantity.
Definition: Units.h:30
vector_fp m_g0_RT
Vector containing the species reference Gibbs functions at T = m_tlast and P = p_ref.
virtual bool addSpecies(shared_ptr< Species > spec)
Add a Species to this Phase.
Definition: Phase.cpp:833
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 getCp_R(doublereal *cpr) const
Get the nondimensional Heat Capacities at constant pressure for the species standard states at the cu...
virtual void getEntropy_R(doublereal *sr) const
Get the array of nondimensional Entropy functions for the standard state species at the current T and...
vector_fp m_sss_R
Vector containing the species Standard State entropies at T = m_tlast and P = m_plast.
vector_fp m_cpss_R
Vector containing the species Standard State constant pressure heat capacities at T = m_tlast and P =...
virtual void getIntEnergy_RT(doublereal *urt) const
Returns the vector of nondimensional Internal Energies of the standard state species at the current T...
doublereal m_Pcurrent
Current value of the pressure - state variable.
virtual doublereal pressure() const
Returns the current pressure of the phase.
virtual void getStandardChemPotentials(doublereal *mu) const
Get the array of chemical potentials at unit activity for the species at their standard states at the...
virtual void updateStandardStateThermo() const
Updates the standard state thermodynamic functions at the current T and P of the solution.
vector_fp m_hss_RT
Vector containing the species Standard State enthalpies at T = m_tlast and P = m_plast.
virtual void getEnthalpy_RT(doublereal *hrt) const
Get the nondimensional Enthalpy functions for the species at their standard states at the current T a...
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:104
std::string attrib(const std::string &attr) const
Function returns the value of an attribute.
Definition: xml.cpp:492
bool hasChild(const std::string &ch) const
Tests whether the current node has a child node with a particular name.
Definition: xml.cpp:528
XML_Node & child(const size_t n) const
Return a changeable reference to the n'th child of the current node.
Definition: xml.cpp:546
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:110
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:232
const double SmallNumber
smallest number to compare to zero.
Definition: ct_defs.h:149
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:180
const double GasConstant
Universal Gas Constant [J/kmol/K].
Definition: ct_defs.h:109
void importPhase(XML_Node &phase, ThermoPhase *th)
Import a phase information into an empty ThermoPhase object.
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:264
doublereal dot(InputIter x_begin, InputIter x_end, InputIter2 y_begin)
Function that calculates a templated inner product.
Definition: utilities.h:112
bool caseInsensitiveEquals(const std::string &input, const std::string &test)
Case insensitive equality predicate.
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition: utilities.h:135
Contains declarations for string manipulation functions within Cantera.
Various templated functions that carry out common vector operations (see Templated Utility Functions)...