Cantera 2.6.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 https://cantera.org/license.txt for license and copyright information.
11
16#include "cantera/base/ctml.h"
18
19namespace Cantera
20{
21
22LatticePhase::LatticePhase(const std::string& inputFile, const std::string& id_)
23 : m_Pref(OneAtm)
24 , m_Pcurrent(OneAtm)
25 , m_site_density(0.0)
26{
27 initThermoFile(inputFile, id_);
28}
29
30LatticePhase::LatticePhase(XML_Node& phaseRef, const std::string& id_)
31{
32 importPhase(phaseRef, this);
33}
34
36{
37 return RT() * mean_X(enthalpy_RT_ref()) +
39}
40
42{
43 return GasConstant * (mean_X(entropy_R_ref()) - sum_xlogx());
44}
45
46doublereal LatticePhase::cp_mole() const
47{
48 return GasConstant * mean_X(cp_R_ref());
49}
50
51doublereal LatticePhase::cv_mole() const
52{
53 return cp_mole();
54}
55
57{
60}
61
62void LatticePhase::setPressure(doublereal p)
63{
64 m_Pcurrent = p;
66}
67
69{
72}
73
75{
76 return Units(1.0);
77}
78
80{
82}
83
84void LatticePhase::getActivityCoefficients(doublereal* ac) const
85{
86 for (size_t k = 0; k < m_kk; k++) {
87 ac[k] = 1.0;
88 }
89}
90
91doublereal LatticePhase::standardConcentration(size_t k) const
92{
93 return 1.0;
94}
95
96doublereal LatticePhase::logStandardConc(size_t k) const
97{
98 return 0.0;
99}
100
101void LatticePhase::getChemPotentials(doublereal* mu) const
102{
103 doublereal delta_p = m_Pcurrent - m_Pref;
104 const vector_fp& g_RT = gibbs_RT_ref();
105 for (size_t k = 0; k < m_kk; k++) {
106 double xx = std::max(SmallNumber, moleFraction(k));
107 mu[k] = RT() * (g_RT[k] + log(xx))
108 + delta_p * m_speciesMolarVolume[k];
109 }
110}
111
112void LatticePhase::getPartialMolarEnthalpies(doublereal* hbar) const
113{
114 const vector_fp& _h = enthalpy_RT_ref();
115 scale(_h.begin(), _h.end(), hbar, RT());
116}
117
118void LatticePhase::getPartialMolarEntropies(doublereal* sbar) const
119{
120 const vector_fp& _s = entropy_R_ref();
121 for (size_t k = 0; k < m_kk; k++) {
122 double xx = std::max(SmallNumber, moleFraction(k));
123 sbar[k] = GasConstant * (_s[k] - log(xx));
124 }
125}
126
127void LatticePhase::getPartialMolarCp(doublereal* cpbar) const
128{
129 getCp_R(cpbar);
130 for (size_t k = 0; k < m_kk; k++) {
131 cpbar[k] *= GasConstant;
132 }
133}
134
135void LatticePhase::getPartialMolarVolumes(doublereal* vbar) const
136{
137 getStandardVolumes(vbar);
138}
139
141{
142 const vector_fp& gibbsrt = gibbs_RT_ref();
143 scale(gibbsrt.begin(), gibbsrt.end(), mu0, RT());
144}
145
146void LatticePhase::getPureGibbs(doublereal* gpure) const
147{
148 const vector_fp& gibbsrt = gibbs_RT_ref();
149 doublereal delta_p = (m_Pcurrent - m_Pref);
150 for (size_t k = 0; k < m_kk; k++) {
151 gpure[k] = RT() * gibbsrt[k] + delta_p * m_speciesMolarVolume[k];
152 }
153}
154
155void LatticePhase::getEnthalpy_RT(doublereal* hrt) const
156{
157 const vector_fp& _h = enthalpy_RT_ref();
158 doublereal delta_prt = (m_Pcurrent - m_Pref) / RT();
159 for (size_t k = 0; k < m_kk; k++) {
160 hrt[k] = _h[k] + delta_prt * m_speciesMolarVolume[k];
161 }
162}
163
164void LatticePhase::getEntropy_R(doublereal* sr) const
165{
166 const vector_fp& _s = entropy_R_ref();
167 std::copy(_s.begin(), _s.end(), sr);
168}
169
170void LatticePhase::getGibbs_RT(doublereal* grt) const
171{
172 const vector_fp& gibbsrt = gibbs_RT_ref();
173 doublereal delta_prt = (m_Pcurrent - m_Pref) / RT();
174 for (size_t k = 0; k < m_kk; k++) {
175 grt[k] = gibbsrt[k] + delta_prt * m_speciesMolarVolume[k];
176 }
177}
178
179void LatticePhase::getGibbs_ref(doublereal* g) const
180{
182 for (size_t k = 0; k < m_kk; k++) {
183 g[k] *= RT();
184 }
185}
186
187void LatticePhase::getCp_R(doublereal* cpr) const
188{
189 const vector_fp& _cpr = cp_R_ref();
190 std::copy(_cpr.begin(), _cpr.end(), cpr);
191}
192
193void LatticePhase::getStandardVolumes(doublereal* vbar) const
194{
195 copy(m_speciesMolarVolume.begin(), m_speciesMolarVolume.end(), vbar);
196}
197
198const vector_fp& LatticePhase::enthalpy_RT_ref() const
199{
201 return m_h0_RT;
202}
203
205{
207 return m_g0_RT;
208}
209
210void LatticePhase::getGibbs_RT_ref(doublereal* grt) const
211{
213 for (size_t k = 0; k < m_kk; k++) {
214 grt[k] = m_g0_RT[k];
215 }
216}
217
219{
221 return m_s0_R;
222}
223
225{
227 return m_cp0_R;
228}
229
230bool LatticePhase::addSpecies(shared_ptr<Species> spec)
231{
232 bool added = ThermoPhase::addSpecies(spec);
233 if (added) {
234 if (m_kk == 1) {
236 }
237 m_h0_RT.push_back(0.0);
238 m_g0_RT.push_back(0.0);
239 m_cp0_R.push_back(0.0);
240 m_s0_R.push_back(0.0);
241 double mv = 1.0 / m_site_density;
242 if (spec->input.hasKey("equation-of-state")) {
243 auto& eos = spec->input["equation-of-state"].getMapWhere(
244 "model", "constant-volume");
245 if (eos.hasKey("density")) {
246 mv = molecularWeight(m_kk-1) / eos.convert("density", "kg/m^3");
247 } else if (eos.hasKey("molar-density")) {
248 mv = 1.0 / eos.convert("molar-density", "kmol/m^3");
249 } else if (eos.hasKey("molar-volume")) {
250 mv = eos.convert("molar-volume", "m^3/kmol");
251 }
252 } else if (spec->input.hasKey("molar_volume")) {
253 // @Deprecated - remove this case for Cantera 3.0 with removal of the XML format
254 mv = spec->input["molar_volume"].asDouble();
255 }
256 m_speciesMolarVolume.push_back(mv);
257 }
258 return added;
259}
260
261void LatticePhase::setSiteDensity(double sitedens)
262{
263 m_site_density = sitedens;
264 for (size_t k = 0; k < m_kk; k++) {
265 if (species(k)->input.hasKey("molar_volume")) {
266 // @Deprecated - remove this case for Cantera 3.0 with removal of the XML format
267 continue;
268 } else if (species(k)->input.hasKey("equation-of-state")) {
269 auto& eos = species(k)->input["equation-of-state"].getMapWhere(
270 "model", "constant-volume");
271 if (eos.hasKey("molar-volume") || eos.hasKey("density")
272 || eos.hasKey("molar-density")) {
273 continue;
274 }
275 }
277 }
278}
279
281{
282 doublereal tnow = temperature();
283 if (m_tlast != tnow) {
284 m_spthermo.update(tnow, &m_cp0_R[0], &m_h0_RT[0], &m_s0_R[0]);
285 m_tlast = tnow;
286 for (size_t k = 0; k < m_kk; k++) {
287 m_g0_RT[k] = m_h0_RT[k] - m_s0_R[k];
288 }
289 m_tlast = tnow;
290 }
291}
292
294{
295 if (m_input.hasKey("site-density")) {
296 setSiteDensity(m_input.convert("site-density", "kmol/m^3"));
297 }
298}
299
301{
303 phaseNode["site-density"].setQuantity(m_site_density, "kmol/m^3");
304}
305
306void LatticePhase::getSpeciesParameters(const std::string& name,
307 AnyMap& speciesNode) const
308{
310 size_t k = speciesIndex(name);
311 // Output volume information in a form consistent with the input
312 const auto S = species(k);
313 if (S->input.hasKey("equation-of-state")) {
314 auto& eosIn = S->input["equation-of-state"].getMapWhere(
315 "model", "constant-volume");
316 auto& eosOut = speciesNode["equation-of-state"].getMapWhere(
317 "model", "constant-volume", true);
318
319 if (eosIn.hasKey("density")) {
320 eosOut["model"] = "constant-volume";
321 eosOut["density"].setQuantity(
322 molecularWeight(k) / m_speciesMolarVolume[k], "kg/m^3");
323 } else if (eosIn.hasKey("molar-density")) {
324 eosOut["model"] = "constant-volume";
325 eosOut["molar-density"].setQuantity(1.0 / m_speciesMolarVolume[k],
326 "kmol/m^3");
327 } else if (eosIn.hasKey("molar-volume")) {
328 eosOut["model"] = "constant-volume";
329 eosOut["molar-volume"].setQuantity(m_speciesMolarVolume[k],
330 "m^3/kmol");
331 }
332 } else if (S->input.hasKey("molar_volume")) {
333 // Species came from XML
334 auto& eosOut = speciesNode["equation-of-state"].getMapWhere(
335 "model", "constant-volume", true);
336 eosOut["model"] = "constant-volume";
337 eosOut["molar-volume"].setQuantity(m_speciesMolarVolume[k], "m^3/kmol");
338 }
339 // Otherwise, species volume is determined by the phase-level site density
340}
341
342
344{
345 eosdata._require("model", "Lattice");
346 setSiteDensity(getFloat(eosdata, "site_density", "toSI"));
347}
348
349}
Header for a simple thermodynamics model of a bulk phase derived from ThermoPhase,...
Declaration for class Cantera::Species.
Headers for the factory class that can create known ThermoPhase objects (see Thermodynamic Properties...
A map of string keys to values whose type can vary at runtime.
Definition: AnyMap.h:399
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:1508
bool hasKey(const std::string &key) const
Returns true if the map contains an item named key.
Definition: AnyMap.cpp:1406
vector_fp m_g0_RT
Temporary storage for the reference state Gibbs energies.
Definition: LatticePhase.h:650
vector_fp m_cp0_R
Temporary storage for the reference state heat capacities.
Definition: LatticePhase.h:647
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 getParameters(AnyMap &phaseNode) const
Store the parameters of a ThermoPhase object such that an identical one could be reconstructed using ...
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:644
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:654
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...
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:660
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:641
virtual doublereal pressure() const
Pressure. Units: Pa.
Definition: LatticePhase.h:325
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 getSpeciesParameters(const std::string &name, AnyMap &speciesNode) const
Get phase-specific parameters of a Species object such that an identical one could be reconstructed a...
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.
LatticePhase(const std::string &inputFile="", const std::string &id="")
Full constructor for a lattice phase.
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:632
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:668
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:671
void assignDensity(const double density_)
Set the internally stored constant density (kg/m^3) of the phase.
Definition: Phase.cpp:698
doublereal mean_X(const doublereal *const Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
Definition: Phase.cpp:717
std::string name() const
Return the name of the phase.
Definition: Phase.cpp:70
size_t m_kk
Number of species in the phase.
Definition: Phase.h:943
doublereal molecularWeight(size_t k) const
Molecular weight of species k.
Definition: Phase.cpp:492
doublereal meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition: Phase.h:751
void getMoleFractions(double *const x) const
Get the species mole fraction vector.
Definition: Phase.cpp:543
double moleFraction(size_t k) const
Return the mole fraction of a single species.
Definition: Phase.cpp:548
virtual void compositionChanged()
Apply changes to the state which are needed after the composition changes.
Definition: Phase.cpp:999
doublereal temperature() const
Temperature (K).
Definition: Phase.h:654
size_t speciesIndex(const std::string &name) const
Returns the index of a species named 'name' within the Phase object.
Definition: Phase.cpp:187
shared_ptr< Species > species(const std::string &name) const
Return the Species object for the named species.
Definition: Phase.cpp:950
doublereal sum_xlogx() const
Evaluate .
Definition: Phase.cpp:727
virtual bool addSpecies(shared_ptr< Species > spec)
doublereal RT() const
Return the Gas Constant multiplied by the current temperature.
Definition: ThermoPhase.h:782
doublereal m_tlast
last value of the temperature processed by reference state
Definition: ThermoPhase.h:1928
void initThermoFile(const std::string &inputFile, const std::string &id)
virtual doublereal refPressure() const
Returns the reference pressure in Pa.
Definition: ThermoPhase.h:150
MultiSpeciesThermo m_spthermo
Pointer to the calculation manager for species reference-state thermodynamic properties.
Definition: ThermoPhase.h:1894
AnyMap m_input
Data supplied via setParameters.
Definition: ThermoPhase.h:1898
const AnyMap & input() const
Access input data associated with the phase description.
virtual void getSpeciesParameters(const std::string &name, AnyMap &speciesNode) const
Get phase-specific parameters of a Species object such that an identical one could be reconstructed a...
Definition: ThermoPhase.h:1726
virtual void getParameters(int &n, doublereal *const c) const
Get the equation of state parameters in a vector.
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:103
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:577
CTML ("Cantera Markup Language") is the variant of XML that Cantera uses to store data.
void importPhase(XML_Node &phase, ThermoPhase *th)
Import a phase information into an empty ThermoPhase object.
Namespace for the Cantera kernel.
Definition: AnyMap.h:29
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:166
const double OneAtm
One atmosphere [Pa].
Definition: ct_defs.h:81
const double SmallNumber
smallest number to compare to zero.
Definition: ct_defs.h:153
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition: utilities.h:100
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:184
const double GasConstant
Universal Gas Constant [J/kmol/K].
Definition: ct_defs.h:113
Contains declarations for string manipulation functions within Cantera.
Various templated functions that carry out common vector operations (see Templated Utility Functions)...