Cantera  3.3.0a1
Loading...
Searching...
No Matches
SurfPhase.cpp
Go to the documentation of this file.
1/**
2 * @file SurfPhase.cpp
3 * Definitions for a simple thermodynamic model of a surface phase
4 * derived from ThermoPhase, assuming an ideal solution model
5 * (see @ref thermoprops and class
6 * @link Cantera::SurfPhase SurfPhase@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
18
19namespace Cantera
20{
21
22SurfPhase::SurfPhase(const string& infile, const string& id_)
23{
24 setNDim(2);
25 initThermoFile(infile, id_);
26}
27
29{
30 if (m_n0 <= 0.0) {
31 return 0.0;
32 }
34 return mean_X(m_h0);
35}
36
38{
39 return enthalpy_mole();
40}
41
43{
45 double s = 0.0;
46 for (size_t k = 0; k < m_kk; k++) {
47 s += moleFraction(k) * (m_s0[k] -
48 GasConstant * log(std::max(concentration(k) * size(k)/m_n0, SmallNumber)));
49 }
50 return s;
51}
52
53double SurfPhase::cp_mole() const
54{
56 return mean_X(m_cp0);
57}
58
59double SurfPhase::cv_mole() const
60{
61 return cp_mole();
62}
63
65{
66 getEnthalpy_RT(hbar);
67 for (size_t k = 0; k < m_kk; k++) {
68 hbar[k] *= RT();
69 }
70}
71
73{
74 getEntropy_R(sbar);
76 for (size_t k = 0; k < m_kk; k++) {
77 sbar[k] -= log(std::max(m_work[k], SmallNumber)) - logStandardConc(k);
78 sbar[k] *= GasConstant;
79 }
80}
81
82void SurfPhase::getPartialMolarCp(double* cpbar) const
83{
84 getCp_R(cpbar);
85 for (size_t k = 0; k < m_kk; k++) {
86 cpbar[k] *= GasConstant;
87 }
88}
89
90// HKM 9/1/11 The partial molar volumes returned here are really partial molar areas.
91// Partial molar volumes for this phase should actually be equal to zero.
92void SurfPhase::getPartialMolarVolumes(double* vbar) const
93{
95}
96
98{
100 copy(m_mu0.begin(), m_mu0.end(), mu0);
101}
102
103void SurfPhase::getChemPotentials(double* mu) const
104{
106 copy(m_mu0.begin(), m_mu0.end(), mu);
108 for (size_t k = 0; k < m_kk; k++) {
109 mu[k] += RT() * (log(std::max(m_work[k], SmallNumber)) - logStandardConc(k));
110 }
111}
112
114{
116}
117
119{
120 return m_n0/size(k);
121}
122
123double SurfPhase::logStandardConc(size_t k) const
124{
125 return m_logn0 - m_logsize[k];
126}
127
128void SurfPhase::getGibbs_RT(double* grt) const
129{
131 scale(m_mu0.begin(), m_mu0.end(), grt, 1.0/RT());
132}
133
134void SurfPhase::getEnthalpy_RT(double* hrt) const
135{
137 scale(m_h0.begin(), m_h0.end(), hrt, 1.0/RT());
138}
139
140void SurfPhase::getEntropy_R(double* sr) const
141{
143 scale(m_s0.begin(), m_s0.end(), sr, 1.0/GasConstant);
144}
145
146void SurfPhase::getCp_R(double* cpr) const
147{
149 scale(m_cp0.begin(), m_cp0.end(), cpr, 1.0/GasConstant);
150}
151
152void SurfPhase::getStandardVolumes(double* vol) const
153{
154 for (size_t k = 0; k < m_kk; k++) {
155 vol[k] = 0.0;
156 }
157}
158
159void SurfPhase::getGibbs_RT_ref(double* grt) const
160{
161 getGibbs_RT(grt);
162}
163
164void SurfPhase::getEnthalpy_RT_ref(double* hrt) const
165{
166 getEnthalpy_RT(hrt);
167}
168
169void SurfPhase::getEntropy_R_ref(double* sr) const
170{
171 getEntropy_R(sr);
172}
173
174void SurfPhase::getCp_R_ref(double* cprt) const
175{
176 getCp_R(cprt);
177}
178
179bool SurfPhase::addSpecies(shared_ptr<Species> spec)
180{
181 bool added = ThermoPhase::addSpecies(spec);
182 if (added) {
183 m_h0.push_back(0.0);
184 m_s0.push_back(0.0);
185 m_cp0.push_back(0.0);
186 m_mu0.push_back(0.0);
187 m_work.push_back(0.0);
188 m_speciesSize.push_back(spec->size);
189 m_logsize.push_back(log(spec->size));
190 if (m_kk == 1) {
191 vector<double> cov{1.0};
192 setCoverages(cov.data());
193 }
194 }
195 return added;
196}
197
199{
200 if (n0 <= 0.0) {
201 throw CanteraError("SurfPhase::setSiteDensity",
202 "Site density must be positive. Got {}", n0);
203 }
204 m_n0 = n0;
205 m_logn0 = log(m_n0);
206 compositionChanged(); // trigger update of density
207}
208
209void SurfPhase::setCoverages(const double* theta)
210{
211 double sum = 0.0;
212 for (size_t k = 0; k < m_kk; k++) {
213 sum += theta[k] / size(k);
214 }
215 if (sum <= 0.0) {
216 throw CanteraError("SurfPhase::setCoverages",
217 "Sum of Coverage fractions is zero or negative");
218 }
219 for (size_t k = 0; k < m_kk; k++) {
220 m_work[k] = theta[k] / (sum * size(k));
221 }
222 setMoleFractions(m_work.data());
223}
224
225void SurfPhase::setCoveragesNoNorm(const double* theta)
226{
227 double sum = 0.0;
228 double sum2 = 0.0;
229 for (size_t k = 0; k < m_kk; k++) {
230 sum += theta[k] / size(k);
231 sum2 += theta[k];
232 }
233 if (sum <= 0.0) {
234 throw CanteraError("SurfPhase::setCoverages",
235 "Sum of Coverage fractions is zero or negative");
236 }
237 for (size_t k = 0; k < m_kk; k++) {
238 m_work[k] = theta[k] * sum2 / (sum * size(k));
239 }
241}
242
243void SurfPhase::getCoverages(double* theta) const
244{
245 double sum_X = 0.0;
246 double sum_X_s = 0.0;
247 getMoleFractions(theta);
248 for (size_t k = 0; k < m_kk; k++) {
249 sum_X += theta[k];
250 sum_X_s += theta[k] * size(k);
251 }
252 for (size_t k = 0; k < m_kk; k++) {
253 theta[k] *= size(k) * sum_X / sum_X_s;
254 }
255}
256
257void SurfPhase::setCoveragesByName(const string& cov)
258{
260}
261
263{
264 vector<double> cv(m_kk, 0.0);
265 bool ifound = false;
266 for (size_t k = 0; k < m_kk; k++) {
267 double c = getValue(cov, speciesName(k), 0.0);
268 if (c > 0.0) {
269 ifound = true;
270 cv[k] = c;
271 }
272 }
273 if (!ifound) {
274 throw CanteraError("SurfPhase::setCoveragesByName",
275 "Input coverages are all zero or negative");
276 }
277 setCoverages(cv.data());
278}
279
280void SurfPhase::setState(const AnyMap& state) {
281 if (state.hasKey("coverages")) {
282 if (state["coverages"].is<string>()) {
283 setCoveragesByName(state["coverages"].asString());
284 } else {
285 setCoveragesByName(state["coverages"].asMap<double>());
286 }
287 }
289}
290
292{
294 getMoleFractions(m_work.data());
295 double q = 0;
296 double sumX = 0;
297 for (size_t k = 0; k < m_kk; k++) {
298 q += m_work[k] * size(k);
299 // This term accounts for unnormalized coverages used in calculation of
300 // derivatives using finite differences.
301 sumX += m_work[k];
302 }
303 assignDensity(m_n0 * meanMolecularWeight() * sumX / q);
304}
305
306void SurfPhase::_updateThermo(bool force) const
307{
308 double tnow = temperature();
309 if (m_tlast != tnow || force) {
310 m_spthermo.update(tnow, m_cp0.data(), m_h0.data(), m_s0.data());
311 m_tlast = tnow;
312 for (size_t k = 0; k < m_kk; k++) {
313 m_h0[k] *= GasConstant * tnow;
314 m_s0[k] *= GasConstant;
315 m_cp0[k] *= GasConstant;
316 m_mu0[k] = m_h0[k] - tnow*m_s0[k];
317 }
318 m_tlast = tnow;
319 }
320}
321
323{
324 if (m_input.hasKey("site-density")) {
325 // Units are kmol/m^2 for surface phases or kmol/m for edge phases
326 setSiteDensity(m_input.convert("site-density",
327 Units(1.0, 0, -static_cast<double>(m_ndim), 0, 0, 0, 1)));
328 }
329}
330
331void SurfPhase::getParameters(AnyMap& phaseNode) const
332{
334 phaseNode["site-density"].setQuantity(
335 m_n0, Units(1.0, 0, -static_cast<double>(m_ndim), 0, 0, 0, 1));
336}
337
338EdgePhase::EdgePhase(const string& infile, const string& id_)
339{
340 setNDim(1);
341 initThermoFile(infile, id_);
342}
343
344}
Declarations for the EdgePhase ThermoPhase object, which models the interface between two surfaces (s...
Declaration for class Cantera::Species.
Header for a simple thermodynamics model of a surface phase derived from ThermoPhase,...
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:431
bool hasKey(const string &key) const
Returns true if the map contains an item named key.
Definition AnyMap.cpp:1477
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:1595
Base class for exceptions thrown by Cantera classes.
EdgePhase(const string &infile="", const string &id="")
Construct and initialize an EdgePhase directly from an input file.
virtual void update(double T, double *cp_R, double *h_RT, double *s_R) const
Compute the reference-state properties for all species.
virtual void getConcentrations(double *const c) const
Get the species concentrations (kmol/m^3).
Definition Phase.cpp:507
void assignDensity(const double density_)
Set the internally stored constant density (kg/m^3) of the phase.
Definition Phase.cpp:620
virtual void setMoleFractions(const double *const x)
Set the mole fractions to the specified values.
Definition Phase.cpp:307
void setNDim(size_t ndim)
Set the number of spatial dimensions (1, 2, or 3).
Definition Phase.h:589
size_t m_kk
Number of species in the phase.
Definition Phase.h:890
size_t m_ndim
Dimensionality of the phase.
Definition Phase.h:894
double temperature() const
Temperature (K).
Definition Phase.h:598
double meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition Phase.h:691
virtual double concentration(const size_t k) const
Concentration of species k.
Definition Phase.cpp:501
string speciesName(size_t k) const
Name of the species with index k.
Definition Phase.cpp:143
void getMoleFractions(double *const x) const
Get the species mole fraction vector.
Definition Phase.cpp:459
double moleFraction(size_t k) const
Return the mole fraction of a single species.
Definition Phase.cpp:464
virtual void compositionChanged()
Apply changes to the state which are needed after the composition changes.
Definition Phase.cpp:941
double mean_X(const double *const Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
Definition Phase.cpp:639
const vector< string > & speciesNames() const
Return a const reference to the vector of species names.
Definition Phase.cpp:151
virtual void setMoleFractions_NoNorm(const double *const x)
Set the mole fractions to the specified values without normalizing.
Definition Phase.cpp:339
SurfPhase(const string &infile="", const string &id="")
Construct and initialize a SurfPhase ThermoPhase object directly from an input file.
Definition SurfPhase.cpp:22
void getStandardChemPotentials(double *mu0) const override
Get the array of chemical potentials at unit activity for the species at their standard states at the...
Definition SurfPhase.cpp:97
double enthalpy_mole() const override
Return the Molar Enthalpy. Units: J/kmol.
Definition SurfPhase.cpp:28
void setSiteDensity(double n0)
Set the site density of the surface phase (kmol m-2)
double logStandardConc(size_t k=0) const override
Natural logarithm of the standard concentration of the kth species.
void setState(const AnyMap &state) override
Set the state using an AnyMap containing any combination of properties supported by the thermodynamic...
void getPartialMolarEnthalpies(double *hbar) const override
Returns an array of partial molar enthalpies for the species in the mixture.
Definition SurfPhase.cpp:64
void getChemPotentials(double *mu) const override
Get the species chemical potentials. Units: J/kmol.
void getEntropy_R(double *sr) const override
Get the array of nondimensional Entropy functions for the standard state species at the current T and...
vector< double > m_logsize
vector storing the log of the size of each species.
Definition SurfPhase.h:360
vector< double > m_work
Temporary work array.
Definition SurfPhase.h:353
void getCp_R(double *cpr) const override
Get the nondimensional Heat Capacities at constant pressure for the species standard states at the cu...
void getParameters(AnyMap &phaseNode) const override
Store the parameters of a ThermoPhase object such that an identical one could be reconstructed using ...
void initThermo() override
Initialize the ThermoPhase object after all species have been set up.
void getActivityConcentrations(double *c) const override
Return a vector of activity concentrations for each species.
double m_n0
Surface site density (kmol m-2)
Definition SurfPhase.h:329
double size(size_t k) const
Returns the number of sites occupied by one molecule of species k.
Definition SurfPhase.h:237
vector< double > m_h0
Temporary storage for the reference state enthalpies.
Definition SurfPhase.h:341
void getPartialMolarVolumes(double *vbar) const override
Return an array of partial molar volumes for the species in the mixture.
Definition SurfPhase.cpp:92
double cv_mole() const override
Molar heat capacity at constant volume and composition [J/kmol/K].
Definition SurfPhase.cpp:59
vector< double > m_s0
Temporary storage for the reference state entropies.
Definition SurfPhase.h:344
void setCoverages(const double *theta)
Set the surface site fractions to a specified state.
vector< double > m_cp0
Temporary storage for the reference state heat capacities.
Definition SurfPhase.h:347
vector< double > m_speciesSize
Vector of species sizes (number of sites occupied). length m_kk.
Definition SurfPhase.h:332
void getEnthalpy_RT(double *hrt) const override
Get the nondimensional Enthalpy functions for the species at their standard states at the current T a...
void getEntropy_R_ref(double *er) const override
Returns the vector of nondimensional entropies of the reference state at the current temperature of t...
void getGibbs_RT(double *grt) const override
Get the nondimensional Gibbs functions for the species in their standard states at the current T and ...
double intEnergy_mole() const override
Return the Molar Internal Energy. Units: J/kmol.
Definition SurfPhase.cpp:37
double entropy_mole() const override
Return the Molar Entropy. Units: J/kmol-K.
Definition SurfPhase.cpp:42
void _updateThermo(bool force=false) const
Update the species reference state thermodynamic functions.
void getCp_R_ref(double *cprt) const override
Returns the vector of nondimensional constant pressure heat capacities of the reference state at the ...
void getStandardVolumes(double *vol) const override
Get the molar volumes of the species standard states at the current T and P of the solution.
void setCoveragesNoNorm(const double *theta)
Set the surface site fractions to a specified state.
void getCoverages(double *theta) const
Return a vector of surface coverages.
double cp_mole() const override
Molar heat capacity at constant pressure and composition [J/kmol/K].
Definition SurfPhase.cpp:53
void getPartialMolarCp(double *cpbar) const override
Return an array of partial molar heat capacities for the species in the mixture.
Definition SurfPhase.cpp:82
void compositionChanged() override
Apply changes to the state which are needed after the composition changes.
double standardConcentration(size_t k=0) const override
Return the standard concentration for the kth species.
vector< double > m_mu0
Temporary storage for the reference state Gibbs energies.
Definition SurfPhase.h:350
bool addSpecies(shared_ptr< Species > spec) override
Add a Species to this Phase.
void setCoveragesByName(const string &cov)
Set the coverages from a string of colon-separated name:value pairs.
void getGibbs_RT_ref(double *grt) const override
Returns the vector of nondimensional Gibbs Free Energies of the reference state at the current temper...
double m_logn0
log of the surface site density
Definition SurfPhase.h:335
void getPartialMolarEntropies(double *sbar) const override
Returns an array of partial molar entropies of the species in the solution.
Definition SurfPhase.cpp:72
void getEnthalpy_RT_ref(double *hrt) const override
Returns the vector of nondimensional enthalpies of the reference state at the current temperature of ...
virtual void getParameters(AnyMap &phaseNode) const
Store the parameters of a ThermoPhase object such that an identical one could be reconstructed using ...
virtual void setState(const AnyMap &state)
Set the state using an AnyMap containing any combination of properties supported by the thermodynamic...
double RT() const
Return the Gas Constant multiplied by the current temperature.
double m_tlast
last value of the temperature processed by reference state
void initThermoFile(const string &inputFile, const string &id)
Initialize a ThermoPhase object using an input file.
MultiSpeciesThermo m_spthermo
Pointer to the calculation manager for species reference-state thermodynamic properties.
bool addSpecies(shared_ptr< Species > spec) override
Add a Species to this Phase.
AnyMap m_input
Data supplied via setParameters.
A representation of the units associated with a dimensional quantity.
Definition Units.h:35
Composition parseCompString(const string &ss, const vector< string > &names)
Parse a composition string into a map consisting of individual key:composition pairs.
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition utilities.h:104
const double GasConstant
Universal Gas Constant [J/kmol/K].
Definition ct_defs.h:120
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
const double SmallNumber
smallest number to compare to zero.
Definition ct_defs.h:158
const U & getValue(const map< T, U > &m, const T &key, const U &default_val)
Const accessor for a value in a map.
Definition utilities.h:190
map< string, double > Composition
Map from string names to doubles.
Definition ct_defs.h:177
Contains declarations for string manipulation functions within Cantera.
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...