Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LatticePhase.cpp
Go to the documentation of this file.
1 /**
2  *
3  * @file LatticePhase.cpp
4  * Definitions for a simple thermodynamics model of a bulk phase
5  * derived from ThermoPhase,
6  * assuming a lattice of solid atoms
7  * (see \ref thermoprops and class \link Cantera::LatticePhase LatticePhase\endlink).
8  *
9  */
13 #include "cantera/base/ctml.h"
15 
16 namespace Cantera
17 {
18 
20  m_Pref(OneAtm),
21  m_Pcurrent(OneAtm),
22  m_speciesMolarVolume(0),
23  m_site_density(0.0)
24 {
25 }
26 
28  m_Pref(OneAtm),
29  m_Pcurrent(OneAtm),
30  m_speciesMolarVolume(0),
31  m_site_density(0.0)
32 {
33  *this = right;
34 }
35 
37 {
38  if (&right != this) {
40  m_Pref = right.m_Pref;
41  m_Pcurrent = right.m_Pcurrent;
42  m_h0_RT = right.m_h0_RT;
43  m_cp0_R = right.m_cp0_R;
44  m_g0_RT = right.m_g0_RT;
45  m_s0_R = right.m_s0_R;
46  m_vacancy = right.m_vacancy;
49  }
50  return *this;
51 }
52 
53 LatticePhase::LatticePhase(const std::string& inputFile, const std::string& id_)
54 {
55  initThermoFile(inputFile, id_);
56 }
57 
58 LatticePhase::LatticePhase(XML_Node& phaseRef, const std::string& id_)
59 {
60  importPhase(*findXMLPhase(&phaseRef, id_), this);
61 }
62 
64 {
65  return new LatticePhase(*this);
66 }
67 
68 doublereal LatticePhase::enthalpy_mole() const
69 {
72 }
73 
74 doublereal LatticePhase::entropy_mole() const
75 {
76  return GasConstant * (mean_X(entropy_R_ref()) - sum_xlogx());
77 }
78 
79 doublereal LatticePhase::cp_mole() const
80 {
81  return GasConstant * mean_X(cp_R_ref());
82 }
83 
84 doublereal LatticePhase::cv_mole() const
85 {
86  return cp_mole();
87 }
88 
90 {
93 }
94 
95 void LatticePhase::setPressure(doublereal p)
96 {
97  m_Pcurrent = p;
98  calcDensity();
99 }
100 
101 void LatticePhase::setMoleFractions(const doublereal* const x)
102 {
104  calcDensity();
105 }
106 
107 void LatticePhase::setMoleFractions_NoNorm(const doublereal* const x)
108 {
110  calcDensity();
111 }
112 
113 void LatticePhase::setMassFractions(const doublereal* const y)
114 {
116  calcDensity();
117 }
118 
119 void LatticePhase::setMassFractions_NoNorm(const doublereal* const y)
120 {
122  calcDensity();
123 }
124 
125 void LatticePhase::setConcentrations(const doublereal* const c)
126 {
128  calcDensity();
129 }
130 
132 {
133  getMoleFractions(c);
134 }
135 
136 void LatticePhase::getActivityCoefficients(doublereal* ac) const
137 {
138  for (size_t k = 0; k < m_kk; k++) {
139  ac[k] = 1.0;
140  }
141 }
142 
143 doublereal LatticePhase::standardConcentration(size_t k) const
144 {
145  return 1.0;
146 }
147 
148 doublereal LatticePhase::logStandardConc(size_t k) const
149 {
150  return 0.0;
151 }
152 
153 void LatticePhase::getChemPotentials(doublereal* mu) const
154 {
155  doublereal delta_p = m_Pcurrent - m_Pref;
156  doublereal RT = temperature() * GasConstant;
157  const vector_fp& g_RT = gibbs_RT_ref();
158  for (size_t k = 0; k < m_kk; k++) {
159  double xx = std::max(SmallNumber, moleFraction(k));
160  mu[k] = RT * (g_RT[k] + log(xx))
161  + delta_p * m_speciesMolarVolume[k];
162  }
163 
164 }
165 
166 void LatticePhase::getPartialMolarEnthalpies(doublereal* hbar) const
167 {
168  const vector_fp& _h = enthalpy_RT_ref();
169  scale(_h.begin(), _h.end(), hbar, GasConstant * temperature());
170 }
171 
172 void LatticePhase::getPartialMolarEntropies(doublereal* sbar) const
173 {
174  const vector_fp& _s = entropy_R_ref();
175  for (size_t k = 0; k < m_kk; k++) {
176  double xx = std::max(SmallNumber, moleFraction(k));
177  sbar[k] = GasConstant * (_s[k] - log(xx));
178  }
179 }
180 
181 void LatticePhase::getPartialMolarCp(doublereal* cpbar) const
182 {
183  getCp_R(cpbar);
184  for (size_t k = 0; k < m_kk; k++) {
185  cpbar[k] *= GasConstant;
186  }
187 }
188 
189 void LatticePhase::getPartialMolarVolumes(doublereal* vbar) const
190 {
191  getStandardVolumes(vbar);
192 }
193 
194 void LatticePhase::getStandardChemPotentials(doublereal* mu0) const
195 {
196  const vector_fp& gibbsrt = gibbs_RT_ref();
197  scale(gibbsrt.begin(), gibbsrt.end(), mu0, _RT());
198 }
199 
200 void LatticePhase::getPureGibbs(doublereal* gpure) const
201 {
202  const vector_fp& gibbsrt = gibbs_RT_ref();
203  doublereal delta_p = (m_Pcurrent - m_Pref);
204  double RT = GasConstant * temperature();
205  for (size_t k = 0; k < m_kk; k++) {
206  gpure[k] = RT * gibbsrt[k] + delta_p * m_speciesMolarVolume[k];
207  }
208 }
209 
210 void LatticePhase::getEnthalpy_RT(doublereal* hrt) const
211 {
212  const vector_fp& _h = enthalpy_RT_ref();
213  doublereal delta_prt = ((m_Pcurrent - m_Pref) / (GasConstant * temperature()));
214  for (size_t k = 0; k < m_kk; k++) {
215  hrt[k] = _h[k] + delta_prt * m_speciesMolarVolume[k];
216  }
217 }
218 
219 void LatticePhase::getEntropy_R(doublereal* sr) const
220 {
221  const vector_fp& _s = entropy_R_ref();
222  std::copy(_s.begin(), _s.end(), sr);
223 }
224 
225 void LatticePhase::getGibbs_RT(doublereal* grt) const
226 {
227  const vector_fp& gibbsrt = gibbs_RT_ref();
228  doublereal delta_prt = (m_Pcurrent - m_Pref) / _RT();
229  for (size_t k = 0; k < m_kk; k++) {
230  grt[k] = gibbsrt[k] + delta_prt * m_speciesMolarVolume[k];
231  }
232 }
233 
234 void LatticePhase::getGibbs_ref(doublereal* g) const
235 {
236  getGibbs_RT_ref(g);
237  for (size_t k = 0; k < m_kk; k++) {
238  g[k] *= GasConstant * temperature();
239  }
240 }
241 
242 void LatticePhase::getCp_R(doublereal* cpr) const
243 {
244  const vector_fp& _cpr = cp_R_ref();
245  std::copy(_cpr.begin(), _cpr.end(), cpr);
246 }
247 
248 void LatticePhase::getStandardVolumes(doublereal* vbar) const
249 {
250  copy(m_speciesMolarVolume.begin(), m_speciesMolarVolume.end(), vbar);
251 }
252 
254 {
255  _updateThermo();
256  return m_h0_RT;
257 }
258 
260 {
261  _updateThermo();
262  return m_g0_RT;
263 }
264 
265 void LatticePhase::getGibbs_RT_ref(doublereal* grt) const
266 {
267  _updateThermo();
268  for (size_t k = 0; k < m_kk; k++) {
269  grt[k] = m_g0_RT[k];
270  }
271 }
272 
274 {
275  _updateThermo();
276  return m_s0_R;
277 }
278 
280 {
281  _updateThermo();
282  return m_cp0_R;
283 }
284 
286 {
287  m_Pref = refPressure();
288  m_h0_RT.resize(m_kk);
289  m_g0_RT.resize(m_kk);
290  m_cp0_R.resize(m_kk);
291  m_s0_R.resize(m_kk);
292  m_speciesMolarVolume.resize(m_kk, 0.0);
293 
295 }
296 
297 void LatticePhase::initThermoXML(XML_Node& phaseNode, const std::string& id_)
298 {
299  if (!id_.empty() && id_ != phaseNode.id()) {
300  throw CanteraError("LatticePhase::initThermoXML",
301  "ids don't match");
302  }
303 
304  std::string subname = "LatticePhase::initThermoXML";
305  /*
306  * Check on the thermo field. Must have:
307  * <thermo model="Lattice" />
308  */
309  if (phaseNode.hasChild("thermo")) {
310  XML_Node& thNode = phaseNode.child("thermo");
311  std::string mString = thNode.attrib("model");
312  if (lowercase(mString) != "lattice") {
313  throw CanteraError(subname.c_str(),
314  "Unknown thermo model: " + mString);
315  }
316  } else {
317  throw CanteraError(subname.c_str(),
318  "Unspecified thermo model");
319  }
320  /*
321  * Now go get the molar volumes. use the default if not found
322  */
323  XML_Node& speciesList = phaseNode.child("speciesArray");
324  XML_Node* speciesDB = get_XML_NameID("speciesData", speciesList["datasrc"], &phaseNode.root());
325 
326  for (size_t k = 0; k < m_kk; k++) {
328  XML_Node* s = speciesDB->findByAttr("name", speciesName(k));
329  if (!s) {
330  throw CanteraError(" LatticePhase::initThermoXML", "database problems");
331  }
332  XML_Node* ss = s->findByName("standardState");
333  if (ss) {
334  if (ss->findByName("molarVolume")) {
335  m_speciesMolarVolume[k] = getFloat(*ss, "molarVolume", "toSI");
336  }
337  }
338  }
339 
340  /*
341  * Call the base initThermo, which handles setting the initial
342  * state.
343  */
344  ThermoPhase::initThermoXML(phaseNode, id_);
345 }
346 
348 {
349  doublereal tnow = temperature();
350  if (m_tlast != tnow) {
351  m_spthermo->update(tnow, &m_cp0_R[0], &m_h0_RT[0], &m_s0_R[0]);
352  m_tlast = tnow;
353  for (size_t k = 0; k < m_kk; k++) {
354  m_g0_RT[k] = m_h0_RT[k] - m_s0_R[k];
355  }
356  m_tlast = tnow;
357  }
358 }
359 
360 void LatticePhase::setParameters(int n, doublereal* const c)
361 {
362  m_site_density = c[0];
364 }
365 
366 void LatticePhase::getParameters(int& n, doublereal* const c) const
367 {
368  c[0] = molarDensity();
369  n = 1;
370 }
371 
373 {
374  eosdata._require("model", "Lattice");
375  m_site_density = getFloat(eosdata, "site_density", "toSI");
376  m_vacancy = getChildValue(eosdata, "vacancy_species");
377 }
378 
379 }
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:704
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:603
virtual void getStandardVolumes(doublereal *vol) const
Get the molar volumes of the species standard states at the current T and P of the solution...
doublereal m_site_density
Site Density of the lattice solid.
Definition: LatticePhase.h:886
CTML ("Cantera Markup Language") is the variant of XML that Cantera uses to store data...
doublereal m_Pref
Reference state pressure.
Definition: LatticePhase.h:844
XML_Node * findXMLPhase(XML_Node *root, const std::string &idtarget)
Search an XML_Node tree for a named phase XML_Node.
Definition: xml.cpp:1108
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:142
const doublereal OneAtm
One atmosphere [Pa].
Definition: ct_defs.h:69
std::string attrib(const std::string &attr) const
Function returns the value of an attribute.
Definition: xml.cpp:527
virtual void getActivityConcentrations(doublereal *c) const
The activity of a species in solution is related to the chemical potential by The quantity is the ...
doublereal _RT() const
Return the Gas Constant multiplied by the current temperature.
Definition: ThermoPhase.h:936
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:862
virtual void initThermo()
Initialize the ThermoPhase object after all species have been set up.
ThermoPhase & operator=(const ThermoPhase &right)
Assignment operator.
Definition: ThermoPhase.cpp:60
vector_fp m_s0_R
Temporary storage for the reference state entropies at the current temperature.
Definition: LatticePhase.h:865
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 ...
vector_fp m_h0_RT
Reference state enthalpies / RT.
Definition: LatticePhase.h:856
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.
virtual void getGibbs_RT_ref(doublereal *grt) const
Returns the vector of nondimensional Gibbs Free Energies of the reference state at the current temper...
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:100
virtual void setParameters(int n, doublereal *const c)
Set the equation of state parameters from the argument list.
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:241
doublereal molarDensity() const
Molar density (kmol/m^3).
Definition: Phase.cpp:663
std::string lowercase(const std::string &s)
Cast a copy of a string to lower case.
Definition: stringUtils.cpp:73
virtual doublereal entropy_mole() const
Molar entropy of the solution. Units: J/kmol/K.
void getMoleFractions(doublereal *const x) const
Get the species mole fraction vector.
Definition: Phase.cpp:556
virtual void setParametersFromXML(const XML_Node &eosdata)
Set equation of state parameter values from XML entries.
virtual void getPartialMolarEnthalpies(doublereal *hbar) const
Returns an array of partial molar enthalpies for the species in the mixture.
std::string m_vacancy
String name for the species which represents a vacancy in the lattice.
Definition: LatticePhase.h:872
doublereal m_tlast
last value of the temperature processed by reference state
Definition: ThermoPhase.h:1656
ThermoPhase * duplMyselfAsThermoPhase() const
Duplication function.
virtual void setMoleFractions_NoNorm(const doublereal *const x)
Set the mole fractions, but don't normalize them to one.
XML_Node & child(const size_t n) const
Return a changeable reference to the n'th child of the current node.
Definition: xml.cpp:573
LatticePhase & operator=(const LatticePhase &right)
Assignment operator.
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:97
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 doublereal standardConcentration(size_t k=0) const
Return the standard concentration for the kth species.
doublereal mean_X(const doublereal *const Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
Definition: Phase.cpp:687
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:742
bool importPhase(XML_Node &phase, ThermoPhase *th, SpeciesThermoFactory *spfactory)
Import a phase information into an empty ThermoPhase object.
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...
doublereal sum_xlogx() const
Evaluate .
Definition: Phase.cpp:703
doublereal m_Pcurrent
The current pressure.
Definition: LatticePhase.h:853
virtual void setMoleFractions(const doublereal *const x)
Set the mole fractions.
vector_fp m_cp0_R
Temporary storage for the reference state heat capacities.
Definition: LatticePhase.h:859
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:99
virtual doublereal cv_mole() const
Molar heat capacity at constant volume of the solution.
virtual doublereal pressure() const
In this equation of state implementation, the density is a function only of the mole fractions...
Definition: LatticePhase.h:371
virtual void setConcentrations(const doublereal *const conc)
Set the concentrations to the specified values within the phase.
Definition: Phase.cpp:614
virtual void setMoleFractions(const doublereal *const x)
Set the mole fractions to the specified values There is no restriction on the sum of the mole fractio...
Definition: Phase.cpp:331
virtual void getPartialMolarVolumes(doublereal *vbar) const
Return an array of partial molar volumes for the species in the mixture.
bool hasChild(const std::string &ch) const
Tests whether the current node has a child node with a particular name.
Definition: xml.cpp:563
void _updateThermo() const
Update the species reference state thermodynamic functions.
const vector_fp & gibbs_RT_ref() const
Returns a reference to the dimensionless reference state Gibbs free energy vector.
virtual void setMolarDensity(const doublereal molarDensity)
Set the internally stored molar density (kmol/m^3) of the phase.
Definition: Phase.cpp:668
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 refPressure() const
Returns the reference pressure in Pa.
Definition: ThermoPhase.h:150
virtual void setMassFractions_NoNorm(const doublereal *const y)
Set the mass fractions to the specified values without normalizing.
Definition: Phase.cpp:404
virtual void initThermo()
Initialize the ThermoPhase object after all species have been set up.
doublereal moleFraction(size_t k) const
Return the mole fraction of a single species.
Definition: Phase.cpp:561
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 temperature() const
Temperature (K).
Definition: Phase.h:602
std::string id() const
Return the id attribute, if present.
Definition: xml.cpp:448
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
virtual void getStandardChemPotentials(doublereal *mu) const
Get the array of chemical potentials at unit activity for the species standard states at the current ...
Templates for operations on vector-like objects.
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition: utilities.h:158
const vector_fp & enthalpy_RT_ref() const
Returns the vector of nondimensional Enthalpies of the reference state at the current temperature of ...
virtual doublereal enthalpy_mole() const
Return the Molar Enthalpy. Units: J/kmol.
vector_fp m_speciesMolarVolume
Vector of molar volumes for each species in the solution.
Definition: LatticePhase.h:878
doublereal meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition: Phase.h:669
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:194
virtual void getEnthalpy_RT(doublereal *hrt) const
Get the nondimensional Enthalpy functions for the species standard states at their standard states at...
virtual void getPartialMolarCp(doublereal *cpbar) const
Returns an array of partial molar Heat Capacities at constant pressure of the species in the solution...
const vector_fp & entropy_R_ref() const
Returns a reference to the dimensionless reference state Entropy vector.
virtual void setMassFractions(const doublereal *const y)
Set the mass fractions to the specified values and normalize them.
Definition: Phase.cpp:390
virtual void setConcentrations(const doublereal *const c)
Set the concentration,.
size_t m_kk
Number of species in the phase.
Definition: Phase.h:843
const vector_fp & cp_R_ref() const
Returns a reference to the dimensionless reference state Heat Capacity vector.
virtual void update(doublereal T, doublereal *cp_R, doublereal *h_RT, doublereal *s_R) const =0
Compute the reference-state properties for all species.
virtual void initThermoFile(const std::string &inputFile, const std::string &id)
XML_Node & root() const
Return the root of the current XML_Node tree.
Definition: xml.cpp:1095
virtual void getActivityCoefficients(doublereal *ac) const
Get the array of non-dimensional activity coefficients at the current solution temperature, pressure, and solution concentration.
virtual doublereal logStandardConc(size_t k=0) const
Returns the natural logarithm of the standard concentration of the kth species.
virtual void initThermoXML(XML_Node &phaseNode, const std::string &id)
Import and initialize a ThermoPhase object using an XML tree.
virtual doublereal cp_mole() const
Molar heat capacity at constant pressure of the solution.
virtual void setMassFractions_NoNorm(const doublereal *const y)
Set the mass fractions, but don't normalize them to one.
SpeciesThermo * m_spthermo
Pointer to the calculation manager for species reference-state thermodynamic properties.
Definition: ThermoPhase.h:1607
std::string speciesName(size_t k) const
Name of the species with index k.
Definition: Phase.cpp:272
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).
LatticePhase()
Base Empty constructor.
virtual void getPartialMolarEntropies(doublereal *sbar) const
Returns an array of partial molar entropies of the species in the solution.
virtual void setMassFractions(const doublereal *const y)
Set the mass fractions, and normalize them to one.
virtual void getChemPotentials(doublereal *mu) const
Get the species chemical potentials. Units: J/kmol.