Cantera  2.5.1
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 https://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 
29 LatticePhase::LatticePhase(const std::string& inputFile, const std::string& id_)
30 {
31  initThermoFile(inputFile, id_);
32 }
33 
34 LatticePhase::LatticePhase(XML_Node& phaseRef, const std::string& id_)
35 {
36  importPhase(phaseRef, this);
37 }
38 
39 doublereal LatticePhase::enthalpy_mole() const
40 {
41  return RT() * mean_X(enthalpy_RT_ref()) +
43 }
44 
45 doublereal LatticePhase::entropy_mole() const
46 {
47  return GasConstant * (mean_X(entropy_R_ref()) - sum_xlogx());
48 }
49 
50 doublereal LatticePhase::cp_mole() const
51 {
52  return GasConstant * mean_X(cp_R_ref());
53 }
54 
55 doublereal LatticePhase::cv_mole() const
56 {
57  return cp_mole();
58 }
59 
61 {
64 }
65 
66 void LatticePhase::setPressure(doublereal p)
67 {
68  m_Pcurrent = p;
69  calcDensity();
70 }
71 
73 {
75  calcDensity();
76 }
77 
79 {
80  return Units(1.0);
81 }
82 
83 void LatticePhase::getActivityConcentrations(doublereal* c) const
84 {
86 }
87 
88 void LatticePhase::getActivityCoefficients(doublereal* ac) const
89 {
90  for (size_t k = 0; k < m_kk; k++) {
91  ac[k] = 1.0;
92  }
93 }
94 
95 doublereal LatticePhase::standardConcentration(size_t k) const
96 {
97  return 1.0;
98 }
99 
100 doublereal LatticePhase::logStandardConc(size_t k) const
101 {
102  return 0.0;
103 }
104 
105 void LatticePhase::getChemPotentials(doublereal* mu) const
106 {
107  doublereal delta_p = m_Pcurrent - m_Pref;
108  const vector_fp& g_RT = gibbs_RT_ref();
109  for (size_t k = 0; k < m_kk; k++) {
110  double xx = std::max(SmallNumber, moleFraction(k));
111  mu[k] = RT() * (g_RT[k] + log(xx))
112  + delta_p * m_speciesMolarVolume[k];
113  }
114 }
115 
116 void LatticePhase::getPartialMolarEnthalpies(doublereal* hbar) const
117 {
118  const vector_fp& _h = enthalpy_RT_ref();
119  scale(_h.begin(), _h.end(), hbar, RT());
120 }
121 
122 void LatticePhase::getPartialMolarEntropies(doublereal* sbar) const
123 {
124  const vector_fp& _s = entropy_R_ref();
125  for (size_t k = 0; k < m_kk; k++) {
126  double xx = std::max(SmallNumber, moleFraction(k));
127  sbar[k] = GasConstant * (_s[k] - log(xx));
128  }
129 }
130 
131 void LatticePhase::getPartialMolarCp(doublereal* cpbar) const
132 {
133  getCp_R(cpbar);
134  for (size_t k = 0; k < m_kk; k++) {
135  cpbar[k] *= GasConstant;
136  }
137 }
138 
139 void LatticePhase::getPartialMolarVolumes(doublereal* vbar) const
140 {
141  getStandardVolumes(vbar);
142 }
143 
144 void LatticePhase::getStandardChemPotentials(doublereal* mu0) const
145 {
146  const vector_fp& gibbsrt = gibbs_RT_ref();
147  scale(gibbsrt.begin(), gibbsrt.end(), mu0, RT());
148 }
149 
150 void LatticePhase::getPureGibbs(doublereal* gpure) const
151 {
152  const vector_fp& gibbsrt = gibbs_RT_ref();
153  doublereal delta_p = (m_Pcurrent - m_Pref);
154  for (size_t k = 0; k < m_kk; k++) {
155  gpure[k] = RT() * gibbsrt[k] + delta_p * m_speciesMolarVolume[k];
156  }
157 }
158 
159 void LatticePhase::getEnthalpy_RT(doublereal* hrt) const
160 {
161  const vector_fp& _h = enthalpy_RT_ref();
162  doublereal delta_prt = (m_Pcurrent - m_Pref) / RT();
163  for (size_t k = 0; k < m_kk; k++) {
164  hrt[k] = _h[k] + delta_prt * m_speciesMolarVolume[k];
165  }
166 }
167 
168 void LatticePhase::getEntropy_R(doublereal* sr) const
169 {
170  const vector_fp& _s = entropy_R_ref();
171  std::copy(_s.begin(), _s.end(), sr);
172 }
173 
174 void LatticePhase::getGibbs_RT(doublereal* grt) const
175 {
176  const vector_fp& gibbsrt = gibbs_RT_ref();
177  doublereal delta_prt = (m_Pcurrent - m_Pref) / RT();
178  for (size_t k = 0; k < m_kk; k++) {
179  grt[k] = gibbsrt[k] + delta_prt * m_speciesMolarVolume[k];
180  }
181 }
182 
183 void LatticePhase::getGibbs_ref(doublereal* g) const
184 {
185  getGibbs_RT_ref(g);
186  for (size_t k = 0; k < m_kk; k++) {
187  g[k] *= RT();
188  }
189 }
190 
191 void LatticePhase::getCp_R(doublereal* cpr) const
192 {
193  const vector_fp& _cpr = cp_R_ref();
194  std::copy(_cpr.begin(), _cpr.end(), cpr);
195 }
196 
197 void LatticePhase::getStandardVolumes(doublereal* vbar) const
198 {
199  copy(m_speciesMolarVolume.begin(), m_speciesMolarVolume.end(), vbar);
200 }
201 
202 const vector_fp& LatticePhase::enthalpy_RT_ref() const
203 {
204  _updateThermo();
205  return m_h0_RT;
206 }
207 
209 {
210  _updateThermo();
211  return m_g0_RT;
212 }
213 
214 void LatticePhase::getGibbs_RT_ref(doublereal* grt) const
215 {
216  _updateThermo();
217  for (size_t k = 0; k < m_kk; k++) {
218  grt[k] = m_g0_RT[k];
219  }
220 }
221 
223 {
224  _updateThermo();
225  return m_s0_R;
226 }
227 
229 {
230  _updateThermo();
231  return m_cp0_R;
232 }
233 
234 bool LatticePhase::addSpecies(shared_ptr<Species> spec)
235 {
236  bool added = ThermoPhase::addSpecies(spec);
237  if (added) {
238  if (m_kk == 1) {
239  m_Pref = refPressure();
240  }
241  m_h0_RT.push_back(0.0);
242  m_g0_RT.push_back(0.0);
243  m_cp0_R.push_back(0.0);
244  m_s0_R.push_back(0.0);
245  double mv = 1.0 / m_site_density;
246  if (spec->input.hasKey("equation-of-state")) {
247  auto& eos = spec->input["equation-of-state"].getMapWhere(
248  "model", "constant-volume");
249  if (eos.hasKey("density")) {
250  mv = molecularWeight(m_kk-1) / eos.convert("density", "kg/m^3");
251  } else if (eos.hasKey("molar-density")) {
252  mv = 1.0 / eos.convert("molar-density", "kmol/m^3");
253  } else if (eos.hasKey("molar-volume")) {
254  mv = eos.convert("molar-volume", "m^3/kmol");
255  }
256  } else if (spec->extra.hasKey("molar_volume")) {
257  // from XML
258  mv = spec->extra["molar_volume"].asDouble();
259  }
260  m_speciesMolarVolume.push_back(mv);
261  }
262  return added;
263 }
264 
265 void LatticePhase::setSiteDensity(double sitedens)
266 {
267  m_site_density = sitedens;
268  for (size_t k = 0; k < m_kk; k++) {
269  if (species(k)->extra.hasKey("molar_volume")) {
270  continue;
271  } else if (species(k)->input.hasKey("equation-of-state")) {
272  auto& eos = species(k)->input["equation-of-state"].getMapWhere(
273  "model", "constant-volume");
274  if (eos.hasKey("molar-volume") || eos.hasKey("density")
275  || eos.hasKey("molar-density")) {
276  continue;
277  }
278  }
280  }
281 }
282 
284 {
285  doublereal tnow = temperature();
286  if (m_tlast != tnow) {
287  m_spthermo.update(tnow, &m_cp0_R[0], &m_h0_RT[0], &m_s0_R[0]);
288  m_tlast = tnow;
289  for (size_t k = 0; k < m_kk; k++) {
290  m_g0_RT[k] = m_h0_RT[k] - m_s0_R[k];
291  }
292  m_tlast = tnow;
293  }
294 }
295 
297 {
298  if (m_input.hasKey("site-density")) {
299  setSiteDensity(m_input.convert("site-density", "kmol/m^3"));
300  }
301 }
302 
304 {
305  eosdata._require("model", "Lattice");
306  setSiteDensity(getFloat(eosdata, "site_density", "toSI"));
307 }
308 
309 }
Header for a simple thermodynamics model of a bulk phase derived from ThermoPhase,...
Headers for the factory class that can create known ThermoPhase objects (see Thermodynamic Properties...
double convert(const std::string &key, const std::string &units) const
Convert the item stored by the given key to the units specified in units.
Definition: AnyMap.cpp:1055
bool hasKey(const std::string &key) const
Returns true if the map contains an item named key.
Definition: AnyMap.cpp:984
vector_fp m_g0_RT
Temporary storage for the reference state Gibbs energies.
Definition: LatticePhase.h:675
vector_fp m_cp0_R
Temporary storage for the reference state heat capacities.
Definition: LatticePhase.h:672
virtual bool addSpecies(shared_ptr< Species > spec)
virtual void getGibbs_RT_ref(doublereal *grt) const
Returns the vector of nondimensional Gibbs Free Energies of the reference state at the current temper...
virtual void getActivityCoefficients(doublereal *ac) const
Get the array of non-dimensional activity coefficients at the current solution temperature,...
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 doublereal cp_mole() const
Molar heat capacity at constant pressure of the solution.
vector_fp m_h0_RT
Reference state enthalpies / RT.
Definition: LatticePhase.h:669
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 doublereal enthalpy_mole() const
Return the Molar Enthalpy. Units: J/kmol.
virtual doublereal logStandardConc(size_t k=0) const
Natural logarithm of the standard concentration of the kth species.
virtual void getPartialMolarVolumes(doublereal *vbar) const
Return an array of partial molar volumes for the species in the mixture.
doublereal calcDensity()
Calculate the density of the mixture using the partial molar volumes and mole fractions as input.
virtual void getStandardVolumes(doublereal *vol) const
Get the molar volumes of the species standard states at the current T and P of the solution.
vector_fp m_s0_R
Temporary storage for the reference state entropies at the current temperature.
Definition: LatticePhase.h:679
virtual doublereal cv_mole() const
Molar heat capacity at constant volume of the solution.
virtual void getActivityConcentrations(doublereal *c) const
This method returns an array of generalized concentrations.
const vector_fp & cp_R_ref() const
Returns a reference to the dimensionless reference state Heat Capacity vector.
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 getPartialMolarCp(doublereal *cpbar) const
Returns an array of partial molar Heat Capacities at constant pressure of the species in the solution...
LatticePhase()
Base Empty constructor.
virtual void getEntropy_R(doublereal *sr) const
Get the array of nondimensional Entropy functions for the species standard states at the current T an...
virtual void setPressure(doublereal p)
Set the internally stored pressure (Pa) at constant temperature and composition.
vector_fp m_speciesMolarVolume
Vector of molar volumes for each species in the solution.
Definition: LatticePhase.h:685
virtual doublereal entropy_mole() const
Molar entropy of the solution. Units: J/kmol/K.
const vector_fp & entropy_R_ref() const
Returns a reference to the dimensionless reference state Entropy vector.
virtual void initThermo()
Initialize the ThermoPhase object after all species have been set up.
const vector_fp & gibbs_RT_ref() const
Returns a reference to the dimensionless reference state Gibbs free energy vector.
void setSiteDensity(double sitedens)
Set the density of lattice sites [kmol/m^3].
doublereal m_Pcurrent
The current pressure.
Definition: LatticePhase.h:666
virtual doublereal pressure() const
In this equation of state implementation, the density is a function only of the mole fractions.
Definition: LatticePhase.h:347
virtual Units standardConcentrationUnits() const
The activity of a species in solution is related to the chemical potential by.
virtual void setParametersFromXML(const XML_Node &eosdata)
Set equation of state parameter values from XML entries.
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 compositionChanged()
Apply changes to the state which are needed after the composition changes.
void _updateThermo() const
Update the species reference state thermodynamic functions.
virtual void getChemPotentials(doublereal *mu) const
Get the species chemical potentials. Units: J/kmol.
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...
doublereal m_Pref
Reference state pressure.
Definition: LatticePhase.h:657
virtual void getEnthalpy_RT(doublereal *hrt) const
Get the nondimensional Enthalpy functions for the species standard states at their standard states at...
doublereal m_site_density
Site Density of the lattice solid.
Definition: LatticePhase.h:693
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 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.
double molarDensity() const
Molar density (kmol/m^3).
Definition: Phase.cpp:700
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
size_t m_kk
Number of species in the phase.
Definition: Phase.h:942
doublereal molecularWeight(size_t k) const
Molecular weight of species k.
Definition: Phase.cpp:521
doublereal meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition: Phase.h:748
void getMoleFractions(double *const x) const
Get the species mole fraction vector.
Definition: Phase.cpp:572
double moleFraction(size_t k) const
Return the mole fraction of a single species.
Definition: Phase.cpp:577
virtual void compositionChanged()
Apply changes to the state which are needed after the composition changes.
Definition: Phase.cpp:1028
doublereal temperature() const
Temperature (K).
Definition: Phase.h:667
shared_ptr< Species > species(const std::string &name) const
Return the Species object for the named species.
Definition: Phase.cpp:980
doublereal sum_xlogx() const
Evaluate .
Definition: Phase.cpp:756
virtual bool addSpecies(shared_ptr< Species > spec)
doublereal RT() const
Return the Gas Constant multiplied by the current temperature.
Definition: ThermoPhase.h:776
doublereal m_tlast
last value of the temperature processed by reference state
Definition: ThermoPhase.h:1904
virtual void initThermoFile(const std::string &inputFile, const std::string &id)
virtual doublereal refPressure() const
Returns the reference pressure in Pa.
Definition: ThermoPhase.h:145
MultiSpeciesThermo m_spthermo
Pointer to the calculation manager for species reference-state thermodynamic properties.
Definition: ThermoPhase.h:1870
AnyMap m_input
Data supplied via setParameters.
Definition: ThermoPhase.h:1874
const AnyMap & input() const
Access input data associated with the phase description.
A representation of the units associated with a dimensional quantity.
Definition: Units.h:30
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:104
void _require(const std::string &a, const std::string &v) const
Require that the current XML node has an attribute named by the first argument, a,...
Definition: xml.cpp:576
CTML ("Cantera Markup Language") is the variant of XML that Cantera uses to store data.
const double OneAtm
One atmosphere [Pa].
Definition: ct_defs.h:78
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 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:164
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)...