Cantera  2.3.0
LatticePhase.cpp
Go to the documentation of this file.
1 /**
2  * @file LatticePhase.cpp
3  * Definitions for a simple thermodynamics model of a bulk phase
4  * derived from ThermoPhase,
5  * assuming a lattice of solid atoms
6  * (see \ref thermoprops and class \link Cantera::LatticePhase LatticePhase\endlink).
7  */
8 
9 // This file is part of Cantera. See License.txt in the top-level directory or
10 // at http://www.cantera.org/license.txt for license and copyright information.
11 
15 #include "cantera/base/ctml.h"
16 #include "cantera/base/utilities.h"
17 
18 namespace Cantera
19 {
20 
22  m_Pref(OneAtm),
23  m_Pcurrent(OneAtm),
24  m_speciesMolarVolume(0),
25  m_site_density(0.0)
26 {
27 }
28 
30  m_Pref(OneAtm),
31  m_Pcurrent(OneAtm),
32  m_speciesMolarVolume(0),
33  m_site_density(0.0)
34 {
35  *this = right;
36 }
37 
38 LatticePhase& LatticePhase::operator=(const LatticePhase& right)
39 {
40  if (&right != this) {
42  m_Pref = right.m_Pref;
43  m_Pcurrent = right.m_Pcurrent;
44  m_h0_RT = right.m_h0_RT;
45  m_cp0_R = right.m_cp0_R;
46  m_g0_RT = right.m_g0_RT;
47  m_s0_R = right.m_s0_R;
48  m_vacancy = right.m_vacancy;
49  m_speciesMolarVolume = right.m_speciesMolarVolume;
50  m_site_density = right.m_site_density;
51  }
52  return *this;
53 }
54 
55 LatticePhase::LatticePhase(const std::string& inputFile, const std::string& id_)
56 {
57  initThermoFile(inputFile, id_);
58 }
59 
60 LatticePhase::LatticePhase(XML_Node& phaseRef, const std::string& id_)
61 {
62  importPhase(phaseRef, this);
63 }
64 
66 {
67  return new LatticePhase(*this);
68 }
69 
70 doublereal LatticePhase::enthalpy_mole() const
71 {
72  return RT() * mean_X(enthalpy_RT_ref()) +
74 }
75 
76 doublereal LatticePhase::entropy_mole() const
77 {
78  return GasConstant * (mean_X(entropy_R_ref()) - sum_xlogx());
79 }
80 
81 doublereal LatticePhase::cp_mole() const
82 {
83  return GasConstant * mean_X(cp_R_ref());
84 }
85 
86 doublereal LatticePhase::cv_mole() const
87 {
88  return cp_mole();
89 }
90 
92 {
95 }
96 
97 void LatticePhase::setPressure(doublereal p)
98 {
99  m_Pcurrent = p;
100  calcDensity();
101 }
102 
104 {
106  calcDensity();
107 }
108 
110 {
111  getMoleFractions(c);
112 }
113 
114 void LatticePhase::getActivityCoefficients(doublereal* ac) const
115 {
116  for (size_t k = 0; k < m_kk; k++) {
117  ac[k] = 1.0;
118  }
119 }
120 
121 doublereal LatticePhase::standardConcentration(size_t k) const
122 {
123  return 1.0;
124 }
125 
126 doublereal LatticePhase::logStandardConc(size_t k) const
127 {
128  return 0.0;
129 }
130 
131 void LatticePhase::getChemPotentials(doublereal* mu) const
132 {
133  doublereal delta_p = m_Pcurrent - m_Pref;
134  const vector_fp& g_RT = gibbs_RT_ref();
135  for (size_t k = 0; k < m_kk; k++) {
136  double xx = std::max(SmallNumber, moleFraction(k));
137  mu[k] = RT() * (g_RT[k] + log(xx))
138  + delta_p * m_speciesMolarVolume[k];
139  }
140 }
141 
142 void LatticePhase::getPartialMolarEnthalpies(doublereal* hbar) const
143 {
144  const vector_fp& _h = enthalpy_RT_ref();
145  scale(_h.begin(), _h.end(), hbar, RT());
146 }
147 
148 void LatticePhase::getPartialMolarEntropies(doublereal* sbar) const
149 {
150  const vector_fp& _s = entropy_R_ref();
151  for (size_t k = 0; k < m_kk; k++) {
152  double xx = std::max(SmallNumber, moleFraction(k));
153  sbar[k] = GasConstant * (_s[k] - log(xx));
154  }
155 }
156 
157 void LatticePhase::getPartialMolarCp(doublereal* cpbar) const
158 {
159  getCp_R(cpbar);
160  for (size_t k = 0; k < m_kk; k++) {
161  cpbar[k] *= GasConstant;
162  }
163 }
164 
165 void LatticePhase::getPartialMolarVolumes(doublereal* vbar) const
166 {
167  getStandardVolumes(vbar);
168 }
169 
170 void LatticePhase::getStandardChemPotentials(doublereal* mu0) const
171 {
172  const vector_fp& gibbsrt = gibbs_RT_ref();
173  scale(gibbsrt.begin(), gibbsrt.end(), mu0, RT());
174 }
175 
176 void LatticePhase::getPureGibbs(doublereal* gpure) const
177 {
178  const vector_fp& gibbsrt = gibbs_RT_ref();
179  doublereal delta_p = (m_Pcurrent - m_Pref);
180  for (size_t k = 0; k < m_kk; k++) {
181  gpure[k] = RT() * gibbsrt[k] + delta_p * m_speciesMolarVolume[k];
182  }
183 }
184 
185 void LatticePhase::getEnthalpy_RT(doublereal* hrt) const
186 {
187  const vector_fp& _h = enthalpy_RT_ref();
188  doublereal delta_prt = (m_Pcurrent - m_Pref) / RT();
189  for (size_t k = 0; k < m_kk; k++) {
190  hrt[k] = _h[k] + delta_prt * m_speciesMolarVolume[k];
191  }
192 }
193 
194 void LatticePhase::getEntropy_R(doublereal* sr) const
195 {
196  const vector_fp& _s = entropy_R_ref();
197  std::copy(_s.begin(), _s.end(), sr);
198 }
199 
200 void LatticePhase::getGibbs_RT(doublereal* grt) const
201 {
202  const vector_fp& gibbsrt = gibbs_RT_ref();
203  doublereal delta_prt = (m_Pcurrent - m_Pref) / RT();
204  for (size_t k = 0; k < m_kk; k++) {
205  grt[k] = gibbsrt[k] + delta_prt * m_speciesMolarVolume[k];
206  }
207 }
208 
209 void LatticePhase::getGibbs_ref(doublereal* g) const
210 {
211  getGibbs_RT_ref(g);
212  for (size_t k = 0; k < m_kk; k++) {
213  g[k] *= RT();
214  }
215 }
216 
217 void LatticePhase::getCp_R(doublereal* cpr) const
218 {
219  const vector_fp& _cpr = cp_R_ref();
220  std::copy(_cpr.begin(), _cpr.end(), cpr);
221 }
222 
223 void LatticePhase::getStandardVolumes(doublereal* vbar) const
224 {
225  copy(m_speciesMolarVolume.begin(), m_speciesMolarVolume.end(), vbar);
226 }
227 
228 const vector_fp& LatticePhase::enthalpy_RT_ref() const
229 {
230  _updateThermo();
231  return m_h0_RT;
232 }
233 
235 {
236  _updateThermo();
237  return m_g0_RT;
238 }
239 
240 void LatticePhase::getGibbs_RT_ref(doublereal* grt) const
241 {
242  _updateThermo();
243  for (size_t k = 0; k < m_kk; k++) {
244  grt[k] = m_g0_RT[k];
245  }
246 }
247 
249 {
250  _updateThermo();
251  return m_s0_R;
252 }
253 
255 {
256  _updateThermo();
257  return m_cp0_R;
258 }
259 
260 bool LatticePhase::addSpecies(shared_ptr<Species> spec)
261 {
262  bool added = ThermoPhase::addSpecies(spec);
263  if (added) {
264  if (m_kk == 1) {
265  m_Pref = refPressure();
266  }
267  m_h0_RT.push_back(0.0);
268  m_g0_RT.push_back(0.0);
269  m_cp0_R.push_back(0.0);
270  m_s0_R.push_back(0.0);
271  m_speciesMolarVolume.push_back(0.0);
272  }
273  return added;
274 }
275 
276 void LatticePhase::initThermoXML(XML_Node& phaseNode, const std::string& id_)
277 {
278  if (!id_.empty() && id_ != phaseNode.id()) {
279  throw CanteraError("LatticePhase::initThermoXML",
280  "ids don't match");
281  }
282 
283  // Check on the thermo field. Must have:
284  // <thermo model="Lattice" />
285  if (phaseNode.hasChild("thermo")) {
286  XML_Node& thNode = phaseNode.child("thermo");
287  if (!ba::iequals(thNode["model"], "lattice")) {
288  throw CanteraError("LatticePhase::initThermoXML",
289  "Unknown thermo model: " + thNode["model"]);
290  }
291  } else {
292  throw CanteraError("LatticePhase::initThermoXML",
293  "Unspecified thermo model");
294  }
295 
296  // Now go get the molar volumes. use the default if not found
297  XML_Node& speciesList = phaseNode.child("speciesArray");
298  XML_Node* speciesDB = get_XML_NameID("speciesData", speciesList["datasrc"], &phaseNode.root());
299 
300  for (size_t k = 0; k < m_kk; k++) {
302  XML_Node* s = speciesDB->findByAttr("name", speciesName(k));
303  if (!s) {
304  throw CanteraError(" LatticePhase::initThermoXML", "database problems");
305  }
306  XML_Node* ss = s->findByName("standardState");
307  if (ss && ss->findByName("molarVolume")) {
308  m_speciesMolarVolume[k] = getFloat(*ss, "molarVolume", "toSI");
309  }
310  }
311 
312  // Call the base initThermo, which handles setting the initial state.
313  ThermoPhase::initThermoXML(phaseNode, id_);
314 }
315 
317 {
318  doublereal tnow = temperature();
319  if (m_tlast != tnow) {
320  m_spthermo->update(tnow, &m_cp0_R[0], &m_h0_RT[0], &m_s0_R[0]);
321  m_tlast = tnow;
322  for (size_t k = 0; k < m_kk; k++) {
323  m_g0_RT[k] = m_h0_RT[k] - m_s0_R[k];
324  }
325  m_tlast = tnow;
326  }
327 }
328 
329 void LatticePhase::setParameters(int n, doublereal* const c)
330 {
331  m_site_density = c[0];
333 }
334 
335 void LatticePhase::getParameters(int& n, doublereal* const c) const
336 {
337  c[0] = molarDensity();
338  n = 1;
339 }
340 
342 {
343  eosdata._require("model", "Lattice");
344  m_site_density = getFloat(eosdata, "site_density", "toSI");
345  m_vacancy = getChildValue(eosdata, "vacancy_species");
346 }
347 
348 }
const vector_fp & entropy_R_ref() const
Returns a reference to the dimensionless reference state Entropy vector.
virtual doublereal pressure() const
In this equation of state implementation, the density is a function only of the mole fractions...
Definition: LatticePhase.h:349
virtual bool addSpecies(shared_ptr< Species > spec)
doublereal m_site_density
Site Density of the lattice solid.
Definition: LatticePhase.h:718
CTML ("Cantera Markup Language") is the variant of XML that Cantera uses to store data...
virtual void getEntropy_R(doublereal *sr) const
Get the array of nondimensional Entropy functions for the species standard states at the current T an...
doublereal m_Pref
Reference state pressure.
Definition: LatticePhase.h:676
std::string getChildValue(const XML_Node &parent, const std::string &nameString)
This function reads a child node with the name, nameString, and returns its XML value as the return s...
Definition: ctml.cpp:145
const XML_Node * findByName(const std::string &nm, int depth=100000) const
This routine carries out a recursive search for an XML node based on the name of the node...
Definition: xml.cpp:695
const doublereal OneAtm
One atmosphere [Pa].
Definition: ct_defs.h:69
virtual doublereal cp_mole() const
Molar heat capacity at constant pressure of the solution.
doublereal temperature() const
Temperature (K).
Definition: Phase.h:601
Various templated functions that carry out common vector operations (see Templated Utility Functions)...
virtual doublereal standardConcentration(size_t k=0) const
Return the standard concentration for the kth species.
virtual void update(doublereal T, doublereal *cp_R, doublereal *h_RT, doublereal *s_R) const
Compute the reference-state properties for all species.
doublereal calcDensity()
Calculate the density of the mixture using the partial molar volumes and mole fractions as input...
vector_fp m_g0_RT
Temporary storage for the reference state Gibbs energies.
Definition: LatticePhase.h:694
doublereal moleFraction(size_t k) const
Return the mole fraction of a single species.
Definition: Phase.cpp:547
ThermoPhase & operator=(const ThermoPhase &right)
Definition: ThermoPhase.cpp:59
vector_fp m_s0_R
Temporary storage for the reference state entropies at the current temperature.
Definition: LatticePhase.h:698
virtual void getPartialMolarCp(doublereal *cpbar) const
Returns an array of partial molar Heat Capacities at constant pressure of the species in the solution...
virtual doublereal logStandardConc(size_t k=0) const
Natural logarithm of the standard concentration of the kth species.
doublereal sum_xlogx() const
Evaluate .
Definition: Phase.cpp:700
vector_fp m_h0_RT
Reference state enthalpies / RT.
Definition: LatticePhase.h:688
Headers for the factory class that can create known ThermoPhase objects (see Thermodynamic Properties...
virtual void getParameters(int &n, doublereal *const c) const
Get the equation of state parameters in a vector.
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:97
virtual void getPureGibbs(doublereal *gpure) const
Get the Gibbs functions for the standard state of the species at the current T and P of the solution...
virtual void setParameters(int n, doublereal *const c)
Set the equation of state parameters from the argument list.
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 setPressure(doublereal p)
Set the internally stored pressure (Pa) at constant temperature and composition.
A simple thermodynamic model for a bulk phase, assuming a lattice of solid atoms. ...
Definition: LatticePhase.h:232
virtual doublereal cv_mole() const
Molar heat capacity at constant volume of the solution.
virtual void setParametersFromXML(const XML_Node &eosdata)
Set equation of state parameter values from XML entries.
std::string m_vacancy
String name for the species which represents a vacancy in the lattice.
Definition: LatticePhase.h:704
doublereal m_tlast
last value of the temperature processed by reference state
Definition: ThermoPhase.h:1737
const vector_fp & cp_R_ref() const
Returns a reference to the dimensionless reference state Heat Capacity vector.
doublereal mean_X(const doublereal *const Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
Definition: Phase.cpp:690
doublereal RT() const
Return the Gas Constant multiplied by the current temperature.
Definition: ThermoPhase.h:809
virtual void getGibbs_ref(doublereal *g) const
Returns the vector of the Gibbs function of the reference state at the current temperature of the sol...
virtual void getCp_R(doublereal *cpr) const
Get the nondimensional Heat Capacities at constant pressure for the species standard states at the cu...
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:93
virtual void getPartialMolarVolumes(doublereal *vbar) const
Return an array of partial molar volumes for the species in the mixture.
void _require(const std::string &a, const std::string &v) const
Require that the current XML node have an attribute named by the first argument, a, and that this attribute have the the string value listed in the second argument, v.
Definition: xml.cpp:576
virtual doublereal enthalpy_mole() const
Return the Molar Enthalpy. Units: J/kmol.
doublereal molarDensity() const
Molar density (kmol/m^3).
Definition: Phase.cpp:666
std::string speciesName(size_t k) const
Name of the species with index k.
Definition: Phase.cpp:267
virtual void compositionChanged()
Apply changes to the state which are needed after the composition changes.
Definition: Phase.cpp:899
virtual void getPartialMolarEntropies(doublereal *sbar) const
Returns an array of partial molar entropies of the species in the solution.
virtual void getActivityCoefficients(doublereal *ac) const
Get the array of non-dimensional activity coefficients at the current solution temperature, pressure, and solution concentration.
doublereal m_Pcurrent
The current pressure.
Definition: LatticePhase.h:685
void _updateThermo() const
Update the species reference state thermodynamic functions.
vector_fp m_cp0_R
Temporary storage for the reference state heat capacities.
Definition: LatticePhase.h:691
virtual ThermoPhase * duplMyselfAsThermoPhase() const
Duplication routine for objects which inherit from ThermoPhase.
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:65
virtual void getGibbs_RT(doublereal *grt) const
Get the nondimensional Gibbs functions for the species standard states at the current T and P of the ...
virtual void getActivityConcentrations(doublereal *c) const
The activity of a species in solution is related to the chemical potential by The quantity is the ...
virtual void getEnthalpy_RT(doublereal *hrt) const
Get the nondimensional Enthalpy functions for the species standard states at their standard states at...
void getMoleFractions(doublereal *const x) const
Get the species mole fraction vector.
Definition: Phase.cpp:542
virtual void setMolarDensity(const doublereal molarDensity)
Set the internally stored molar density (kmol/m^3) of the phase.
Definition: Phase.cpp:671
void importPhase(XML_Node &phase, ThermoPhase *th)
Import a phase information into an empty ThermoPhase object.
virtual void getGibbs_RT_ref(doublereal *grt) const
Returns the vector of nondimensional Gibbs Free Energies of the reference state at the current temper...
bool hasChild(const std::string &ch) const
Tests whether the current node has a child node with a particular name.
Definition: xml.cpp:536
MultiSpeciesThermo * m_spthermo
Pointer to the calculation manager for species reference-state thermodynamic properties.
Definition: ThermoPhase.h:1693
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
XML_Node & root() const
Return the root of the current XML_Node tree.
Definition: xml.cpp:1025
virtual void getChemPotentials(doublereal *mu) const
Get the species chemical potentials. Units: J/kmol.
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::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
virtual doublereal refPressure() const
Returns the reference pressure in Pa.
Definition: ThermoPhase.h:149
vector_fp m_speciesMolarVolume
Vector of molar volumes for each species in the solution.
Definition: LatticePhase.h:710
std::string id() const
Return the id attribute, if present.
Definition: xml.cpp:428
const doublereal GasConstant
Universal Gas Constant. [J/kmol/K].
Definition: ct_defs.h:64
Contains declarations for string manipulation functions within Cantera.
doublereal getFloat(const XML_Node &parent, const std::string &name, const std::string &type)
Get a floating-point value from a child element.
Definition: ctml.cpp:178
virtual bool addSpecies(shared_ptr< Species > spec)
virtual doublereal entropy_mole() const
Molar entropy of the solution. Units: J/kmol/K.
size_t m_kk
Number of species in the phase.
Definition: Phase.h:784
XML_Node * findByAttr(const std::string &attr, const std::string &val, int depth=100000) const
This routine carries out a recursive search for an XML node based on an attribute of each XML node...
Definition: xml.cpp:661
const vector_fp & gibbs_RT_ref() const
Returns a reference to the dimensionless reference state Gibbs free energy vector.
virtual void initThermoFile(const std::string &inputFile, const std::string &id)
Namespace for the Cantera kernel.
Definition: application.cpp:29
virtual void initThermoXML(XML_Node &phaseNode, const std::string &id)
Import and initialize a ThermoPhase object using an XML tree.
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:252
Header for a simple thermodynamics model of a bulk phase derived from ThermoPhase, assuming a lattice of solid atoms (see Thermodynamic Properties and class LatticePhase).
virtual void getStandardChemPotentials(doublereal *mu) const
Get the array of chemical potentials at unit activity for the species at their standard states at the...
LatticePhase()
Base Empty constructor.
virtual void compositionChanged()
Apply changes to the state which are needed after the composition changes.
virtual void getPartialMolarEnthalpies(doublereal *hbar) const
Returns an array of partial molar enthalpies for the species in the mixture.