| Cantera 3.0.0

Loading...
Searching...
No Matches
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
12#include "cantera/thermo/LatticePhase.h"
13#include "cantera/thermo/ThermoFactory.h"
14#include "cantera/thermo/Species.h"
15#include "cantera/base/stringUtils.h"
16#include "cantera/base/utilities.h"
17
18 namespace Cantera
19{
20
21 LatticePhase::LatticePhase(const string& inputFile, const string& id_)
22{
23 initThermoFile(inputFile, id_);
24}
25
26 double LatticePhase::enthalpy_mole() const
27{
28 return RT() * mean_X(enthalpy_RT_ref()) +
29 (pressure() - m_Pref)/molarDensity();
30}
31
32 double LatticePhase::entropy_mole() const
33{
34 return GasConstant * (mean_X(entropy_R_ref()) - sum_xlogx());
35}
36
37 double LatticePhase::cp_mole() const
38{
39 return GasConstant * mean_X(cp_R_ref());
40}
41
42 double LatticePhase::cv_mole() const
43{
44 return cp_mole();
45}
46
47 double LatticePhase::calcDensity()
48{
49 assignDensity(std::max(meanMolecularWeight() * m_site_density, SmallNumber));
50 return meanMolecularWeight() * m_site_density;
51}
52
53 void LatticePhase::setPressure(double p)
54{
55 m_Pcurrent = p;
56 calcDensity();
57}
58
59 void LatticePhase::compositionChanged()
60{
61 Phase::compositionChanged();
62 calcDensity();
63}
64
65 Units LatticePhase::standardConcentrationUnits() const
66{
67 return Units(1.0);
68}
69
70 void LatticePhase::getActivityConcentrations(double* c) const
71{
72 getMoleFractions(c);
73}
74
75 void LatticePhase::getActivityCoefficients(double* ac) const
76{
77 for (size_t k = 0; k < m_kk; k++) {
78 ac[k] = 1.0;
79 }
80}
81
82 double LatticePhase::standardConcentration(size_t k) const
83{
84 return 1.0;
85}
86
87 double LatticePhase::logStandardConc(size_t k) const
88{
89 return 0.0;
90}
91
92 void LatticePhase::getChemPotentials(double* mu) const
93{
94 double delta_p = m_Pcurrent - m_Pref;
95 const vector<double>& g_RT = gibbs_RT_ref();
96 for (size_t k = 0; k < m_kk; k++) {
97 double xx = std::max(SmallNumber, moleFraction(k));
98 mu[k] = RT() * (g_RT[k] + log(xx))
99 + delta_p * m_speciesMolarVolume[k];
100 }
101}
102
103 void LatticePhase::getPartialMolarEnthalpies(double* hbar) const
104{
105 const vector<double>& _h = enthalpy_RT_ref();
106 scale(_h.begin(), _h.end(), hbar, RT());
107}
108
109 void LatticePhase::getPartialMolarEntropies(double* sbar) const
110{
111 const vector<double>& _s = entropy_R_ref();
112 for (size_t k = 0; k < m_kk; k++) {
113 double xx = std::max(SmallNumber, moleFraction(k));
114 sbar[k] = GasConstant * (_s[k] - log(xx));
115 }
116}
117
118 void LatticePhase::getPartialMolarCp(double* cpbar) const
119{
120 getCp_R(cpbar);
121 for (size_t k = 0; k < m_kk; k++) {
122 cpbar[k] *= GasConstant;
123 }
124}
125
126 void LatticePhase::getPartialMolarVolumes(double* vbar) const
127{
128 getStandardVolumes(vbar);
129}
130
131 void LatticePhase::getStandardChemPotentials(double* mu0) const
132{
133 const vector<double>& gibbsrt = gibbs_RT_ref();
134 scale(gibbsrt.begin(), gibbsrt.end(), mu0, RT());
135}
136
137 void LatticePhase::getPureGibbs(double* gpure) const
138{
139 const vector<double>& gibbsrt = gibbs_RT_ref();
140 double delta_p = (m_Pcurrent - m_Pref);
141 for (size_t k = 0; k < m_kk; k++) {
142 gpure[k] = RT() * gibbsrt[k] + delta_p * m_speciesMolarVolume[k];
143 }
144}
145
146 void LatticePhase::getEnthalpy_RT(double* hrt) const
147{
148 const vector<double>& _h = enthalpy_RT_ref();
149 double delta_prt = (m_Pcurrent - m_Pref) / RT();
150 for (size_t k = 0; k < m_kk; k++) {
151 hrt[k] = _h[k] + delta_prt * m_speciesMolarVolume[k];
152 }
153}
154
155 void LatticePhase::getEntropy_R(double* sr) const
156{
157 const vector<double>& _s = entropy_R_ref();
158 std::copy(_s.begin(), _s.end(), sr);
159}
160
161 void LatticePhase::getGibbs_RT(double* grt) const
162{
163 const vector<double>& gibbsrt = gibbs_RT_ref();
164 double delta_prt = (m_Pcurrent - m_Pref) / RT();
165 for (size_t k = 0; k < m_kk; k++) {
166 grt[k] = gibbsrt[k] + delta_prt * m_speciesMolarVolume[k];
167 }
168}
169
170 void LatticePhase::getGibbs_ref(double* g) const
171{
172 getGibbs_RT_ref(g);
173 for (size_t k = 0; k < m_kk; k++) {
174 g[k] *= RT();
175 }
176}
177
178 void LatticePhase::getCp_R(double* cpr) const
179{
180 const vector<double>& _cpr = cp_R_ref();
181 std::copy(_cpr.begin(), _cpr.end(), cpr);
182}
183
184 void LatticePhase::getStandardVolumes(double* vbar) const
185{
186 copy(m_speciesMolarVolume.begin(), m_speciesMolarVolume.end(), vbar);
187}
188
189 const vector<double>& LatticePhase::enthalpy_RT_ref() const
190{
191 _updateThermo();
192 return m_h0_RT;
193}
194
195 const vector<double>& LatticePhase::gibbs_RT_ref() const
196{
197 _updateThermo();
198 return m_g0_RT;
199}
200
201 void LatticePhase::getGibbs_RT_ref(double* grt) const
202{
203 _updateThermo();
204 for (size_t k = 0; k < m_kk; k++) {
205 grt[k] = m_g0_RT[k];
206 }
207}
208
209 const vector<double>& LatticePhase::entropy_R_ref() const
210{
211 _updateThermo();
212 return m_s0_R;
213}
214
215 const vector<double>& LatticePhase::cp_R_ref() const
216{
217 _updateThermo();
218 return m_cp0_R;
219}
220
221 bool LatticePhase::addSpecies(shared_ptr<Species> spec)
222{
223 bool added = ThermoPhase::addSpecies(spec);
224 if (added) {
225 if (m_kk == 1) {
226 m_Pref = refPressure();
227 }
228 m_h0_RT.push_back(0.0);
229 m_g0_RT.push_back(0.0);
230 m_cp0_R.push_back(0.0);
231 m_s0_R.push_back(0.0);
232 double mv = 1.0 / m_site_density;
233 if (spec->input.hasKey("equation-of-state")) {
234 auto& eos = spec->input["equation-of-state"].getMapWhere(
235 "model", "constant-volume");
236 if (eos.hasKey("density")) {
237 mv = molecularWeight(m_kk-1) / eos.convert("density", "kg/m^3");
238 } else if (eos.hasKey("molar-density")) {
239 mv = 1.0 / eos.convert("molar-density", "kmol/m^3");
240 } else if (eos.hasKey("molar-volume")) {
241 mv = eos.convert("molar-volume", "m^3/kmol");
242 }
243 }
244 m_speciesMolarVolume.push_back(mv);
245 }
246 return added;
247}
248
249 void LatticePhase::setSiteDensity(double sitedens)
250{
251 m_site_density = sitedens;
252 for (size_t k = 0; k < m_kk; k++) {
253 if (species(k)->input.hasKey("equation-of-state")) {
254 auto& eos = species(k)->input["equation-of-state"].getMapWhere(
255 "model", "constant-volume");
256 if (eos.hasKey("molar-volume") || eos.hasKey("density")
257 || eos.hasKey("molar-density")) {
258 continue;
259 }
260 }
261 m_speciesMolarVolume[k] = 1.0 / m_site_density;
262 }
263}
264
265 void LatticePhase::_updateThermo() const
266{
267 double tnow = temperature();
268 if (m_tlast != tnow) {
269 m_spthermo.update(tnow, &m_cp0_R[0], &m_h0_RT[0], &m_s0_R[0]);
270 m_tlast = tnow;
271 for (size_t k = 0; k < m_kk; k++) {
272 m_g0_RT[k] = m_h0_RT[k] - m_s0_R[k];
273 }
274 m_tlast = tnow;
275 }
276}
277
278 void LatticePhase::initThermo()
279{
280 if (m_input.hasKey("site-density")) {
281 setSiteDensity(m_input.convert("site-density", "kmol/m^3"));
282 }
283}
284
285 void LatticePhase::getParameters(AnyMap& phaseNode) const
286{
287 ThermoPhase::getParameters(phaseNode);
288 phaseNode["site-density"].setQuantity(m_site_density, "kmol/m^3");
289}
290
291 void LatticePhase::getSpeciesParameters(const string& name, AnyMap& speciesNode) const
292{
293 ThermoPhase::getSpeciesParameters(name, speciesNode);
294 size_t k = speciesIndex(name);
295 // Output volume information in a form consistent with the input
296 const auto S = species(k);
297 if (S->input.hasKey("equation-of-state")) {
298 auto& eosIn = S->input["equation-of-state"].getMapWhere(
299 "model", "constant-volume");
300 auto& eosOut = speciesNode["equation-of-state"].getMapWhere(
301 "model", "constant-volume", true);
302
303 if (eosIn.hasKey("density")) {
304 eosOut["model"] = "constant-volume";
305 eosOut["density"].setQuantity(
306 molecularWeight(k) / m_speciesMolarVolume[k], "kg/m^3");
307 } else if (eosIn.hasKey("molar-density")) {
308 eosOut["model"] = "constant-volume";
309 eosOut["molar-density"].setQuantity(1.0 / m_speciesMolarVolume[k],
310 "kmol/m^3");
311 } else if (eosIn.hasKey("molar-volume")) {
312 eosOut["model"] = "constant-volume";
313 eosOut["molar-volume"].setQuantity(m_speciesMolarVolume[k],
314 "m^3/kmol");
315 }
316 }
317 // Otherwise, species volume is determined by the phase-level site density
318}
319
320}
LatticePhase.h
Header for a simple thermodynamics model of a bulk phase derived from ThermoPhase,...
Species.h
Declaration for class Cantera::Species.
ThermoFactory.h
Headers for the factory class that can create known ThermoPhase objects (see Thermodynamic Properties...
Cantera::AnyMap
A map of string keys to values whose type can vary at runtime.
Definition AnyMap.h:427
Cantera::AnyMap::hasKey
bool hasKey(const string &key) const
Returns true if the map contains an item named key.
Definition AnyMap.cpp:1423
Cantera::AnyMap::convert
double convert(const string &key, const string &units) const
Convert the item stored by the given key to the units specified in units.
Definition AnyMap.cpp:1535
Cantera::LatticePhase::entropy_R_ref
const vector< double > & entropy_R_ref() const
Returns a reference to the dimensionless reference state Entropy vector.
Definition LatticePhase.cpp:209
Cantera::LatticePhase::enthalpy_mole
double enthalpy_mole() const override
Return the Molar Enthalpy. Units: J/kmol.
Definition LatticePhase.cpp:26
Cantera::LatticePhase::logStandardConc
double logStandardConc(size_t k=0) const override
Natural logarithm of the standard concentration of the kth species.
Definition LatticePhase.cpp:87
Cantera::LatticePhase::getPartialMolarEnthalpies
void getPartialMolarEnthalpies(double *hbar) const override
Returns an array of partial molar enthalpies for the species in the mixture.
Definition LatticePhase.cpp:103
Cantera::LatticePhase::getChemPotentials
void getChemPotentials(double *mu) const override
Get the species chemical potentials. Units: J/kmol.
Definition LatticePhase.cpp:92
Cantera::LatticePhase::m_site_density
double m_site_density
Site Density of the lattice solid.
Definition LatticePhase.h:592
Cantera::LatticePhase::pressure
double pressure() const override
Pressure. Units: Pa.
Definition LatticePhase.h:287
Cantera::LatticePhase::m_g0_RT
vector< double > m_g0_RT
Temporary storage for the reference state Gibbs energies.
Definition LatticePhase.h:574
Cantera::LatticePhase::getSpeciesParameters
void getSpeciesParameters(const string &name, AnyMap &speciesNode) const override
Get phase-specific parameters of a Species object such that an identical one could be reconstructed a...
Definition LatticePhase.cpp:291
Cantera::LatticePhase::getEntropy_R
void getEntropy_R(double *sr) const override
Get the array of nondimensional Entropy functions for the species standard states at the current T an...
Definition LatticePhase.cpp:155
Cantera::LatticePhase::m_h0_RT
vector< double > m_h0_RT
Reference state enthalpies / RT.
Definition LatticePhase.h:568
Cantera::LatticePhase::getGibbs_ref
void getGibbs_ref(double *g) const override
Returns the vector of the Gibbs function of the reference state at the current temperature of the sol...
Definition LatticePhase.cpp:170
Cantera::LatticePhase::m_Pref
double m_Pref
Reference state pressure.
Definition LatticePhase.h:556
Cantera::LatticePhase::getStandardChemPotentials
void getStandardChemPotentials(double *mu) const override
Get the array of chemical potentials at unit activity for the species at their standard states at the...
Definition LatticePhase.cpp:131
Cantera::LatticePhase::getCp_R
void getCp_R(double *cpr) const override
Get the nondimensional Heat Capacities at constant pressure for the species standard states at the cu...
Definition LatticePhase.cpp:178
Cantera::LatticePhase::getParameters
void getParameters(AnyMap &phaseNode) const override
Store the parameters of a ThermoPhase object such that an identical one could be reconstructed using ...
Definition LatticePhase.cpp:285
Cantera::LatticePhase::initThermo
void initThermo() override
Initialize the ThermoPhase object after all species have been set up.
Definition LatticePhase.cpp:278
Cantera::LatticePhase::getActivityConcentrations
void getActivityConcentrations(double *c) const override
This method returns an array of generalized concentrations.
Definition LatticePhase.cpp:70
Cantera::LatticePhase::setPressure
void setPressure(double p) override
Set the internally stored pressure (Pa) at constant temperature and composition.
Definition LatticePhase.cpp:53
Cantera::LatticePhase::gibbs_RT_ref
const vector< double > & gibbs_RT_ref() const
Returns a reference to the dimensionless reference state Gibbs free energy vector.
Definition LatticePhase.cpp:195
Cantera::LatticePhase::getPartialMolarVolumes
void getPartialMolarVolumes(double *vbar) const override
Return an array of partial molar volumes for the species in the mixture.
Definition LatticePhase.cpp:126
Cantera::LatticePhase::cv_mole
double cv_mole() const override
Molar heat capacity at constant volume of the solution.
Definition LatticePhase.cpp:42
Cantera::LatticePhase::getPureGibbs
void getPureGibbs(double *gpure) const override
Get the Gibbs functions for the standard state of the species at the current T and P of the solution.
Definition LatticePhase.cpp:137
Cantera::LatticePhase::getEnthalpy_RT
void getEnthalpy_RT(double *hrt) const override
Get the nondimensional Enthalpy functions for the species standard states at their standard states at...
Definition LatticePhase.cpp:146
Cantera::LatticePhase::m_s0_R
vector< double > m_s0_R
Temporary storage for the reference state entropies at the current temperature.
Definition LatticePhase.h:578
Cantera::LatticePhase::getGibbs_RT
void getGibbs_RT(double *grt) const override
Get the nondimensional Gibbs functions for the species standard states at the current T and P of the ...
Definition LatticePhase.cpp:161
Cantera::LatticePhase::entropy_mole
double entropy_mole() const override
Molar entropy of the solution. Units: J/kmol/K.
Definition LatticePhase.cpp:32
Cantera::LatticePhase::setSiteDensity
void setSiteDensity(double sitedens)
Set the density of lattice sites [kmol/m^3].
Definition LatticePhase.cpp:249
Cantera::LatticePhase::m_speciesMolarVolume
vector< double > m_speciesMolarVolume
Vector of molar volumes for each species in the solution.
Definition LatticePhase.h:584
Cantera::LatticePhase::getStandardVolumes
void getStandardVolumes(double *vol) const override
Get the molar volumes of the species standard states at the current T and P of the solution.
Definition LatticePhase.cpp:184
Cantera::LatticePhase::cp_mole
double cp_mole() const override
Molar heat capacity at constant pressure of the solution.
Definition LatticePhase.cpp:37
Cantera::LatticePhase::standardConcentrationUnits
Units standardConcentrationUnits() const override
Returns the units of the "standard concentration" for this phase.
Definition LatticePhase.cpp:65
Cantera::LatticePhase::getPartialMolarCp
void getPartialMolarCp(double *cpbar) const override
Returns an array of partial molar Heat Capacities at constant pressure of the species in the solution...
Definition LatticePhase.cpp:118
Cantera::LatticePhase::compositionChanged
void compositionChanged() override
Apply changes to the state which are needed after the composition changes.
Definition LatticePhase.cpp:59
Cantera::LatticePhase::standardConcentration
double standardConcentration(size_t k=0) const override
Return the standard concentration for the kth species.
Definition LatticePhase.cpp:82
Cantera::LatticePhase::m_cp0_R
vector< double > m_cp0_R
Temporary storage for the reference state heat capacities.
Definition LatticePhase.h:571
Cantera::LatticePhase::calcDensity
double calcDensity()
Calculate the density of the mixture using the partial molar volumes and mole fractions as input.
Definition LatticePhase.cpp:47
Cantera::LatticePhase::addSpecies
bool addSpecies(shared_ptr< Species > spec) override
Add a Species to this Phase.
Definition LatticePhase.cpp:221
Cantera::LatticePhase::_updateThermo
void _updateThermo() const
Update the species reference state thermodynamic functions.
Definition LatticePhase.cpp:265
Cantera::LatticePhase::getGibbs_RT_ref
void getGibbs_RT_ref(double *grt) const override
Returns the vector of nondimensional Gibbs Free Energies of the reference state at the current temper...
Definition LatticePhase.cpp:201
Cantera::LatticePhase::getActivityCoefficients
void getActivityCoefficients(double *ac) const override
Get the array of non-dimensional activity coefficients at the current solution temperature,...
Definition LatticePhase.cpp:75
Cantera::LatticePhase::LatticePhase
LatticePhase(const string &inputFile="", const string &id="")
Full constructor for a lattice phase.
Definition LatticePhase.cpp:21
Cantera::LatticePhase::getPartialMolarEntropies
void getPartialMolarEntropies(double *sbar) const override
Returns an array of partial molar entropies of the species in the solution.
Definition LatticePhase.cpp:109
Cantera::LatticePhase::cp_R_ref
const vector< double > & cp_R_ref() const
Returns a reference to the dimensionless reference state Heat Capacity vector.
Definition LatticePhase.cpp:215
Cantera::LatticePhase::m_Pcurrent
double m_Pcurrent
The current pressure.
Definition LatticePhase.h:565
Cantera::MultiSpeciesThermo::update
virtual void update(double T, double *cp_R, double *h_RT, double *s_R) const
Compute the reference-state properties for all species.
Definition MultiSpeciesThermo.cpp:94
Cantera::Phase::molarDensity
virtual double molarDensity() const
Molar density (kmol/m^3).
Definition Phase.cpp:689
Cantera::Phase::assignDensity
void assignDensity(const double density_)
Set the internally stored constant density (kg/m^3) of the phase.
Definition Phase.cpp:718
Cantera::Phase::m_kk
size_t m_kk
Number of species in the phase.
Definition Phase.h:947
Cantera::Phase::temperature
double temperature() const
Temperature (K).
Definition Phase.h:662
Cantera::Phase::meanMolecularWeight
double meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition Phase.h:760
Cantera::Phase::getMoleFractions
void getMoleFractions(double *const x) const
Get the species mole fraction vector.
Definition Phase.cpp:540
Cantera::Phase::sum_xlogx
double sum_xlogx() const
Evaluate .
Definition Phase.cpp:747
Cantera::Phase::speciesIndex
size_t speciesIndex(const string &name) const
Returns the index of a species named 'name' within the Phase object.
Definition Phase.cpp:138
Cantera::Phase::moleFraction
double moleFraction(size_t k) const
Return the mole fraction of a single species.
Definition Phase.cpp:545
Cantera::Phase::compositionChanged
virtual void compositionChanged()
Apply changes to the state which are needed after the composition changes.
Definition Phase.cpp:1026
Cantera::Phase::mean_X
double mean_X(const double *const Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
Definition Phase.cpp:737
Cantera::Phase::molecularWeight
double molecularWeight(size_t k) const
Molecular weight of species k.
Definition Phase.cpp:482
Cantera::Phase::species
shared_ptr< Species > species(const string &name) const
Return the Species object for the named species.
Definition Phase.cpp:977
Cantera::Phase::name
string name() const
Return the name of the phase.
Definition Phase.cpp:20
Cantera::ThermoPhase::getParameters
virtual void getParameters(AnyMap &phaseNode) const
Store the parameters of a ThermoPhase object such that an identical one could be reconstructed using ...
Definition ThermoPhase.cpp:1176
Cantera::ThermoPhase::RT
double RT() const
Return the Gas Constant multiplied by the current temperature.
Definition ThermoPhase.h:1077
Cantera::ThermoPhase::m_tlast
double m_tlast
last value of the temperature processed by reference state
Definition ThermoPhase.h:2149
Cantera::ThermoPhase::initThermoFile
void initThermoFile(const string &inputFile, const string &id)
Initialize a ThermoPhase object using an input file.
Definition ThermoPhase.cpp:1072
Cantera::ThermoPhase::getSpeciesParameters
virtual void getSpeciesParameters(const 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:1979
Cantera::ThermoPhase::m_spthermo
MultiSpeciesThermo m_spthermo
Pointer to the calculation manager for species reference-state thermodynamic properties.
Definition ThermoPhase.h:2126
Cantera::ThermoPhase::refPressure
virtual double refPressure() const
Returns the reference pressure in Pa.
Definition ThermoPhase.h:436
Cantera::ThermoPhase::addSpecies
bool addSpecies(shared_ptr< Species > spec) override
Add a Species to this Phase.
Definition ThermoPhase.cpp:1131
Cantera::ThermoPhase::m_input
AnyMap m_input
Data supplied via setParameters.
Definition ThermoPhase.h:2130
Cantera::ThermoPhase::input
const AnyMap & input() const
Access input data associated with the phase description.
Definition ThermoPhase.cpp:1229
Cantera::Units
A representation of the units associated with a dimensional quantity.
Definition Units.h:35
Cantera::scale
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition utilities.h:104
Cantera::GasConstant
const double GasConstant
Universal Gas Constant [J/kmol/K].
Definition ct_defs.h:120
Cantera
Namespace for the Cantera kernel.
Definition AnyMap.cpp:564
Cantera::SmallNumber
const double SmallNumber
smallest number to compare to zero.
Definition ct_defs.h:158
stringUtils.h
Contains declarations for string manipulation functions within Cantera.
utilities.h
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...