Cantera  2.1.2
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  * Copyright (2005) Sandia Corporation. Under the terms of
11  * Contract DE-AC04-94AL85000 with Sandia Corporation, the
12  * U.S. Government retains certain rights in this software.
13  */
14 
16 #include "cantera/thermo/VPSSMgr.h"
17 #include "cantera/thermo/PDSS.h"
21 
22 using namespace std;
23 
24 namespace Cantera
25 {
26 
27 IdealSolnGasVPSS::IdealSolnGasVPSS() :
29  m_idealGas(0),
30  m_formGC(0)
31 {
32 }
33 
34 IdealSolnGasVPSS::IdealSolnGasVPSS(const std::string& infile, std::string id_) :
36  m_idealGas(0),
37  m_formGC(0)
38 {
39  XML_Node* root = get_XML_File(infile);
40  if (id_ == "-") {
41  id_ = "";
42  }
43  XML_Node* xphase = get_XML_NameID("phase", std::string("#")+id_, root);
44  if (!xphase) {
45  throw CanteraError("newPhase",
46  "Couldn't find phase named \"" + id_ + "\" in file, " + infile);
47  }
48  importPhase(*xphase, this);
49 }
50 
53  m_idealGas(0),
54  m_formGC(0)
55 {
56  *this = b;
57 }
58 
61 {
62  if (&b != this) {
63  /*
64  * Mostly, this is a passthrough to the underlying
65  * assignment operator for the ThermoPhae parent object.
66  */
68  /*
69  * However, we have to handle data that we own.
70  */
72  m_formGC = b.m_formGC;
73  }
74  return *this;
75 }
76 
78 {
79  return new IdealSolnGasVPSS(*this);
80 }
81 
83 {
84  if (m_idealGas) {
85  return cIdealSolnGasVPSS;
86  }
87  return cIdealSolnGasVPSS_iscv;
88 }
89 
90 /*
91  * ------------Molar Thermodynamic Properties -------------------------
92  */
93 
95 {
97  const vector_fp& enth_RT = m_VPSS_ptr->enthalpy_RT();
98  return (GasConstant * temperature() *
99  mean_X(DATA_PTR(enth_RT)));
100 }
101 
103 {
104  doublereal p0 = pressure();
105  doublereal md = molarDensity();
106  return enthalpy_mole() - p0 / md;
107 }
108 
110 {
112  const vector_fp& entrop_R = m_VPSS_ptr->entropy_R();
113  return GasConstant * (mean_X(DATA_PTR(entrop_R)) - sum_xlogx());
114 
115 }
116 
118 {
119  return enthalpy_mole() - temperature() * entropy_mole();
120 }
121 
122 doublereal IdealSolnGasVPSS::cp_mole() const
123 {
125  const vector_fp& cp_R = m_VPSS_ptr->cp_R();
126  return GasConstant * (mean_X(DATA_PTR(cp_R)));
127 }
128 
129 doublereal IdealSolnGasVPSS::cv_mole() const
130 {
131  return cp_mole() - GasConstant;
132 }
133 
135 {
136  m_Pcurrent = p;
138  calcDensity();
139 }
140 
142 {
143  /*
144  * Calculate the molarVolume of the solution (m**3 kmol-1)
145  */
146  if (m_idealGas) {
147  double dens = (m_Pcurrent * meanMolecularWeight()
148  /(GasConstant * temperature()));
149  Phase::setDensity(dens);
150  } else {
151  const doublereal* const dtmp = moleFractdivMMW();
152  const vector_fp& vss = m_VPSS_ptr->getStandardVolumes();
153  double invDens = dot(vss.begin(), vss.end(), dtmp);
154  /*
155  * Set the density in the parent State object directly,
156  * by calling the Phase::setDensity() function.
157  */
158  double dens = 1.0/invDens;
159  Phase::setDensity(dens);
160  }
161 }
162 
164 {
165  if (m_idealGas) {
166  return 1.0 / m_Pcurrent;
167  } else {
168  throw CanteraError("IdealSolnGasVPSS::isothermalCompressibility() ",
169  "not implemented");
170  }
171  return 0.0;
172 }
173 
175 {
176  if (m_idealGas) {
178  } else {
179  const vector_fp& vss = m_VPSS_ptr->getStandardVolumes();
180  switch (m_formGC) {
181  case 0:
182  for (size_t k = 0; k < m_kk; k++) {
183  c[k] = moleFraction(k);
184  }
185  break;
186  case 1:
187  for (size_t k = 0; k < m_kk; k++) {
188  c[k] = moleFraction(k) / vss[k];
189  }
190  break;
191  case 2:
192  for (size_t k = 0; k < m_kk; k++) {
193  c[k] = moleFraction(k) / vss[0];
194  }
195  break;
196  }
197  }
198 }
199 
200 doublereal IdealSolnGasVPSS::standardConcentration(size_t k) const
201 {
202  if (m_idealGas) {
203  double p = pressure();
204  return p/(GasConstant * temperature());
205  } else {
206  const vector_fp& vss = m_VPSS_ptr->getStandardVolumes();
207  switch (m_formGC) {
208  case 0:
209  return 1.0;
210  case 1:
211  return 1.0 / vss[k];
212  case 2:
213  return 1.0/ vss[0];
214  }
215  return 0.0;
216 
217  }
218 }
219 
220 doublereal IdealSolnGasVPSS::logStandardConc(size_t k) const
221 {
222  double c = standardConcentration(k);
223  return std::log(c);
224 }
225 
226 void IdealSolnGasVPSS::getUnitsStandardConc(double* uA, int, int sizeUA) const
227 {
228  int eos = eosType();
229  if (eos == cIdealSolnGasPhase0) {
230  for (int i = 0; i < sizeUA; i++) {
231  uA[i] = 0.0;
232  }
233  } else {
234  for (int i = 0; i < sizeUA; i++) {
235  if (i == 0) {
236  uA[0] = 1.0;
237  }
238  if (i == 1) {
239  uA[1] = -int(nDim());
240  }
241  if (i == 2) {
242  uA[2] = 0.0;
243  }
244  if (i == 3) {
245  uA[3] = 0.0;
246  }
247  if (i == 4) {
248  uA[4] = 0.0;
249  }
250  if (i == 5) {
251  uA[5] = 0.0;
252  }
253  }
254  }
255 }
256 
258 {
259  for (size_t k = 0; k < m_kk; k++) {
260  ac[k] = 1.0;
261  }
262 }
263 
264 /*
265  * ---- Partial Molar Properties of the Solution -----------------
266  */
267 
268 void IdealSolnGasVPSS::getChemPotentials_RT(doublereal* muRT) const
269 {
270  getChemPotentials(muRT);
271  doublereal invRT = 1.0 / _RT();
272  for (size_t k = 0; k < m_kk; k++) {
273  muRT[k] *= invRT;
274  }
275 }
276 
277 void IdealSolnGasVPSS::getChemPotentials(doublereal* mu) const
278 {
280  doublereal xx;
281  doublereal rt = temperature() * GasConstant;
282  for (size_t k = 0; k < m_kk; k++) {
283  xx = std::max(SmallNumber, moleFraction(k));
284  mu[k] += rt*(log(xx));
285  }
286 }
287 
289 {
290  getEnthalpy_RT(hbar);
291  doublereal rt = GasConstant * temperature();
292  scale(hbar, hbar+m_kk, hbar, rt);
293 }
294 
295 void IdealSolnGasVPSS::getPartialMolarEntropies(doublereal* sbar) const
296 {
297  getEntropy_R(sbar);
298  doublereal r = GasConstant;
299  scale(sbar, sbar+m_kk, sbar, r);
300  for (size_t k = 0; k < m_kk; k++) {
301  doublereal xx = std::max(SmallNumber, moleFraction(k));
302  sbar[k] += r * (- log(xx));
303  }
304 }
305 
307 {
308  getIntEnergy_RT(ubar);
309  doublereal rt = GasConstant * temperature();
310  scale(ubar, ubar+m_kk, ubar, rt);
311 }
312 
313 void IdealSolnGasVPSS::getPartialMolarCp(doublereal* cpbar) const
314 {
315  getCp_R(cpbar);
316  doublereal r = GasConstant;
317  scale(cpbar, cpbar+m_kk, cpbar, r);
318 }
319 
320 void IdealSolnGasVPSS::getPartialMolarVolumes(doublereal* vbar) const
321 {
322  getStandardVolumes(vbar);
323 }
324 
326 {
327  initLengths();
329 }
330 
331 void IdealSolnGasVPSS::setToEquilState(const doublereal* mu_RT)
332 {
333  double tmp, tmp2;
335  const vector_fp& grt = m_VPSS_ptr->Gibbs_RT_ref();
336 
337  /*
338  * Within the method, we protect against inf results if the
339  * exponent is too high.
340  *
341  * If it is too low, we set
342  * the partial pressure to zero. This capability is needed
343  * by the elemental potential method.
344  */
345  doublereal pres = 0.0;
346  double m_p0 = m_VPSS_ptr->refPressure();
347  for (size_t k = 0; k < m_kk; k++) {
348  tmp = -grt[k] + mu_RT[k];
349  if (tmp < -600.) {
350  m_pp[k] = 0.0;
351  } else if (tmp > 500.0) {
352  tmp2 = tmp / 500.;
353  tmp2 *= tmp2;
354  m_pp[k] = m_p0 * exp(500.) * tmp2;
355  } else {
356  m_pp[k] = m_p0 * exp(tmp);
357  }
358  pres += m_pp[k];
359  }
360  // set state
361  setState_PX(pres, &m_pp[0]);
362 }
363 
365 {
366  m_kk = nSpecies();
367  m_pp.resize(m_kk, 0.0);
368 }
369 
370 void IdealSolnGasVPSS::initThermoXML(XML_Node& phaseNode, const std::string& id_)
371 {
373 
374  if (phaseNode.hasChild("thermo")) {
375  XML_Node& thermoNode = phaseNode.child("thermo");
376  std::string model = thermoNode["model"];
377  if (model == "IdealGasVPSS") {
378  m_idealGas = 1;
379  } else if (model == "IdealSolnVPSS") {
380  m_idealGas = 0;
381  } else {
382  throw CanteraError("IdealSolnGasVPSS::initThermoXML",
383  "Unknown thermo model : " + model);
384  }
385  }
386 
387  /*
388  * Form of the standard concentrations. Must have one of:
389  *
390  * <standardConc model="unity" />
391  * <standardConc model="molar_volume" />
392  * <standardConc model="solvent_volume" />
393  */
394  if (phaseNode.hasChild("standardConc")) {
395  if (m_idealGas) {
396  throw CanteraError("IdealSolnGasVPSS::initThermoXML",
397  "standardConc node for ideal gas");
398  }
399  XML_Node& scNode = phaseNode.child("standardConc");
400  string formStringa = scNode.attrib("model");
401  string formString = lowercase(formStringa);
402  if (formString == "unity") {
403  m_formGC = 0;
404  } else if (formString == "molar_volume") {
405  m_formGC = 1;
406  } else if (formString == "solvent_volume") {
407  m_formGC = 2;
408  } else {
409  throw CanteraError("initThermoXML",
410  "Unknown standardConc model: " + formStringa);
411  }
412  } else {
413  if (!m_idealGas) {
414  throw CanteraError("initThermoXML",
415  "Unspecified standardConc model");
416  }
417  }
418 
419  VPStandardStateTP::initThermoXML(phaseNode, id_);
420 }
421 
423 {
425  std::string model = thermoNode["model"];
426  if (model == "IdealGasVPSS") {
427  m_idealGas = 1;
428  } else if (model == "IdealSolnVPSS") {
429  m_idealGas = 0;
430  } else {
431  throw CanteraError("IdealSolnGasVPSS::initThermoXML",
432  "Unknown thermo model : " + model);
433  }
434 }
435 
436 }
virtual void getPartialMolarEnthalpies(doublereal *hbar) const
Get the species partial molar enthalpies. Units: J/kmol.
const vector_fp & Gibbs_RT_ref() const
Return a reference to the vector of Gibbs free energies of the species.
Definition: VPSSMgr.h:409
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:144
virtual void getPartialMolarEntropies(doublereal *sbar) const
Get the species partial molar entropies. Units: J/kmol/K.
std::string attrib(const std::string &attr) const
Function returns the value of an attribute.
Definition: xml.cpp:534
Declaration file for a virtual base class that manages the calculation of standard state properties f...
doublereal _RT() const
Return the Gas Constant multiplied by the current temperature.
Definition: ThermoPhase.h:977
virtual void getStandardVolumes(doublereal *vol) const
Get the molar volumes of each species in their standard states at the current T and P of the solution...
Definition file for a derived class of ThermoPhase that assumes either an ideal gas or ideal solution...
virtual doublereal isothermalCompressibility() const
Returns the isothermal compressibility. Units: 1/Pa.
virtual void getCp_R(doublereal *cpr) const
Get the nondimensional Heat Capacities at constant pressure for the standard state of the species at ...
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:100
virtual void updateStandardStateThermo() const
Updates the standard state thermodynamic functions at the current T and P of the solution.
doublereal cv_mole() const
Molar heat capacity at constant volume. Units: J/kmol/K.
virtual void setParametersFromXML(const XML_Node &eosdata)
Set equation of state parameter values from XML entries.
virtual ThermoPhase * duplMyselfAsThermoPhase() const
Duplication routine.
const vector_fp & cp_R() const
Return a reference to a vector of the constant pressure heat capacities of the species.
Definition: VPSSMgr.h:348
doublereal molarDensity() const
Molar density (kmol/m^3).
Definition: Phase.cpp:597
std::string lowercase(const std::string &s)
Cast a copy of a string to lower case.
Definition: stringUtils.cpp:58
VPSSMgr * m_VPSS_ptr
Pointer to the VPSS manager that calculates all of the standard state info efficiently.
void getConcentrations(doublereal *const c) const
Get the species concentrations (kmol/m^3).
Definition: Phase.cpp:572
vector_fp m_pp
Temporary storage - length = m_kk.
doublereal cp_mole() const
Molar heat capacity at constant pressure. Units: J/kmol/K.
XML_Node & child(const size_t n) const
Return a changeable reference to the n'th child of the current node.
Definition: xml.cpp:584
const vector_fp & enthalpy_RT() const
Return a reference to a vector of the molar enthalpies of the species in their standard states...
Definition: VPSSMgr.h:300
VPStandardStateTP & operator=(const VPStandardStateTP &b)
Assignment operator.
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:101
doublereal mean_X(const doublereal *const Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
Definition: Phase.cpp:623
virtual void getPartialMolarCp(doublereal *cpbar) const
Get the partial molar heat capacities Units: J/kmol/K.
bool importPhase(XML_Node &phase, ThermoPhase *th, SpeciesThermoFactory *spfactory)
Import a phase information into an empty thermophase object.
Declarations for the virtual base class PDSS (pressure dependent standard state) which handles calcul...
virtual void initThermo()
Initialize the ThermoPhase object after all species have been set up.
virtual void calcDensity()
Calculate the density of the mixture using the partial molar volumes and mole fractions as input...
doublereal pressure() const
Returns the current pressure of the phase.
doublereal sum_xlogx() const
Evaluate .
Definition: Phase.cpp:633
virtual doublereal standardConcentration(size_t k=0) const
Returns the standard concentration , which is used to normalize the generalized concentration.
doublereal enthalpy_mole() const
Molar enthalpy. Units: J/kmol.
int m_formGC
form of the generalized concentrations
virtual doublereal refPressure(size_t k=npos) const
The reference-state pressure for the standard state.
Definition: VPSSMgr.cpp:477
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:68
This is a filter class for ThermoPhase that implements some prepatory steps for efficiently handling ...
virtual void getActivityConcentrations(doublereal *c) const
This method returns an array of generalized concentrations.
IdealSolnGasVPSS & operator=(const IdealSolnGasVPSS &)
Assignment operator.
bool hasChild(const std::string &ch) const
Tests whether the current node has a child node with a particular name.
Definition: xml.cpp:574
virtual void getPartialMolarVolumes(doublereal *vbar) const
Get the species partial molar volumes. Units: m^3/kmol.
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:131
const vector_fp & entropy_R() const
Return a reference to a vector of the entropies of the species.
Definition: VPSSMgr.h:314
void setPressure(doublereal p)
Set the pressure in the fluid.
size_t nSpecies() const
Returns the number of species in the phase.
Definition: Phase.h:252
virtual void getIntEnergy_RT(doublereal *urt) const
Returns the vector of nondimensional internal Energies of the standard state at the current temperatu...
virtual void setParametersFromXML(const XML_Node &thermoNode)
Set equation of state parameter values from XML entries.
doublereal moleFraction(size_t k) const
Return the mole fraction of a single species.
Definition: Phase.cpp:524
virtual void getEntropy_R(doublereal *sr) const
Get the array of nondimensional Enthalpy functions for the standard state species at the current T an...
doublereal temperature() const
Temperature (K).
Definition: Phase.h:528
size_t nDim() const
Returns the number of spatial dimensions (1, 2, or 3)
Definition: Phase.h:512
const doublereal SmallNumber
smallest number to compare to zero.
Definition: ct_defs.h:139
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:165
virtual int eosType() const
Equation of state type flag.
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition: utilities.h:154
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 getEnthalpy_RT(doublereal *hrt) const
Get the nondimensional Enthalpy functions for the species at their standard states at the current T a...
virtual void getChemPotentials(doublereal *mu) const
Get the species chemical potentials. Units: J/kmol.
virtual doublereal logStandardConc(size_t k=0) const
Returns the natural logarithm of the standard concentration of the kth species.
doublereal meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition: Phase.h:588
const doublereal GasConstant
Universal Gas Constant. [J/kmol/K].
Definition: ct_defs.h:66
virtual void initThermoXML(XML_Node &phaseNode, const std::string &id)
Initialize a ThermoPhase object, potentially reading activity coefficient information from an XML dat...
virtual void setState_PX(doublereal p, doublereal *x)
Set the pressure (Pa) and mole fractions.
virtual void getUnitsStandardConc(double *uA, int k=0, int sizeUA=6) const
Returns the units of the standard and generalized concentrations.
Contains declarations for string manipulation functions within Cantera.
#define DATA_PTR(vec)
Creates a pointer to the start of the raw data for a vector.
Definition: ct_defs.h:36
doublereal intEnergy_mole() const
Molar internal energy. Units: J/kmol.
doublereal entropy_mole() const
Molar entropy. Units: J/kmol/K.
virtual void initThermoXML(XML_Node &phaseNode, const std::string &id)
Initialize a ThermoPhase object, potentially reading activity coefficient information from an XML dat...
virtual void getStandardVolumes(doublereal *vol) const
Get the molar volumes of each species in their standard states at the current T and P of the solution...
Definition: VPSSMgr.cpp:217
size_t m_kk
Number of species in the phase.
Definition: Phase.h:716
doublereal m_Pcurrent
Current value of the pressure - state variable.
doublereal gibbs_mole() const
Molar Gibbs function. Units: J/kmol.
virtual void getStandardChemPotentials(doublereal *mu) const
Get the array of chemical potentials at unit activity.
virtual void getPartialMolarIntEnergies(doublereal *ubar) const
Get the species partial molar enthalpies. Units: J/kmol.
const doublereal * moleFractdivMMW() const
Returns a const pointer to the start of the moleFraction/MW array.
Definition: Phase.cpp:540
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:271
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 Note the density of a phase is an independent...
Definition: Phase.h:549
An ideal solution or an ideal gas approximation of a phase.
void getChemPotentials_RT(doublereal *mu) const
Get the array of non-dimensional species chemical potentials These are partial molar Gibbs free energ...