Cantera  2.5.1
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 
16 #include "cantera/base/ctml.h"
17 #include "cantera/base/utilities.h"
18 
19 using namespace std;
20 
21 namespace Cantera
22 {
23 SurfPhase::SurfPhase(doublereal n0):
24  m_press(OneAtm)
25 {
26  setSiteDensity(n0);
27  setNDim(2);
28 }
29 
30 SurfPhase::SurfPhase(const std::string& infile, const std::string& id_) :
31  m_press(OneAtm)
32 {
33  initThermoFile(infile, id_);
34 }
35 
37  m_press(OneAtm)
38 {
39  importPhase(xmlphase, this);
40 }
41 
42 doublereal SurfPhase::enthalpy_mole() const
43 {
44  if (m_n0 <= 0.0) {
45  return 0.0;
46  }
47  _updateThermo();
48  return mean_X(m_h0);
49 }
50 
51 doublereal SurfPhase::intEnergy_mole() const
52 {
53  return enthalpy_mole();
54 }
55 
56 doublereal SurfPhase::entropy_mole() const
57 {
58  _updateThermo();
59  doublereal s = 0.0;
60  for (size_t k = 0; k < m_kk; k++) {
61  s += moleFraction(k) * (m_s0[k] -
62  GasConstant * log(std::max(concentration(k) * size(k)/m_n0, SmallNumber)));
63  }
64  return s;
65 }
66 
67 doublereal SurfPhase::cp_mole() const
68 {
69  _updateThermo();
70  return mean_X(m_cp0);
71 }
72 
73 doublereal SurfPhase::cv_mole() const
74 {
75  return cp_mole();
76 }
77 
78 void SurfPhase::getPartialMolarEnthalpies(doublereal* hbar) const
79 {
80  getEnthalpy_RT(hbar);
81  for (size_t k = 0; k < m_kk; k++) {
82  hbar[k] *= RT();
83  }
84 }
85 
86 void SurfPhase::getPartialMolarEntropies(doublereal* sbar) const
87 {
88  getEntropy_R(sbar);
89  for (size_t k = 0; k < m_kk; k++) {
90  sbar[k] *= GasConstant;
91  }
92 }
93 
94 void SurfPhase::getPartialMolarCp(doublereal* cpbar) const
95 {
96  getCp_R(cpbar);
97  for (size_t k = 0; k < m_kk; k++) {
98  cpbar[k] *= GasConstant;
99  }
100 }
101 
102 // HKM 9/1/11 The partial molar volumes returned here are really partial molar areas.
103 // Partial molar volumes for this phase should actually be equal to zero.
104 void SurfPhase::getPartialMolarVolumes(doublereal* vbar) const
105 {
106  getStandardVolumes(vbar);
107 }
108 
109 void SurfPhase::getStandardChemPotentials(doublereal* mu0) const
110 {
111  _updateThermo();
112  copy(m_mu0.begin(), m_mu0.end(), mu0);
113 }
114 
115 void SurfPhase::getChemPotentials(doublereal* mu) const
116 {
117  _updateThermo();
118  copy(m_mu0.begin(), m_mu0.end(), mu);
120  for (size_t k = 0; k < m_kk; k++) {
121  mu[k] += RT() * (log(m_work[k]) - logStandardConc(k));
122  }
123 }
124 
125 void SurfPhase::getActivityConcentrations(doublereal* c) const
126 {
128 }
129 
130 doublereal SurfPhase::standardConcentration(size_t k) const
131 {
132  return m_n0/size(k);
133 }
134 
135 doublereal SurfPhase::logStandardConc(size_t k) const
136 {
137  return m_logn0 - m_logsize[k];
138 }
139 
140 void SurfPhase::setParameters(int n, doublereal* const c)
141 {
142  if (n != 1) {
143  throw CanteraError("SurfPhase::setParameters",
144  "Bad value for number of parameter");
145  }
146  setSiteDensity(c[0]);
147 }
148 
149 void SurfPhase::getPureGibbs(doublereal* g) const
150 {
151  _updateThermo();
152  copy(m_mu0.begin(), m_mu0.end(), g);
153 }
154 
155 void SurfPhase::getGibbs_RT(doublereal* grt) const
156 {
157  _updateThermo();
158  scale(m_mu0.begin(), m_mu0.end(), grt, 1.0/RT());
159 }
160 
161 void SurfPhase::getEnthalpy_RT(doublereal* hrt) const
162 {
163  _updateThermo();
164  scale(m_h0.begin(), m_h0.end(), hrt, 1.0/RT());
165 }
166 
167 void SurfPhase::getEntropy_R(doublereal* sr) const
168 {
169  _updateThermo();
170  scale(m_s0.begin(), m_s0.end(), sr, 1.0/GasConstant);
171 }
172 
173 void SurfPhase::getCp_R(doublereal* cpr) const
174 {
175  _updateThermo();
176  scale(m_cp0.begin(), m_cp0.end(), cpr, 1.0/GasConstant);
177 }
178 
179 void SurfPhase::getStandardVolumes(doublereal* vol) const
180 {
181  _updateThermo();
182  for (size_t k = 0; k < m_kk; k++) {
183  vol[k] = 1.0/standardConcentration(k);
184  }
185 }
186 
187 void SurfPhase::getGibbs_RT_ref(doublereal* grt) const
188 {
189  getGibbs_RT(grt);
190 }
191 
192 void SurfPhase::getEnthalpy_RT_ref(doublereal* hrt) const
193 {
194  getEnthalpy_RT(hrt);
195 }
196 
197 void SurfPhase::getEntropy_R_ref(doublereal* sr) const
198 {
199  getEntropy_R(sr);
200 }
201 
202 void SurfPhase::getCp_R_ref(doublereal* cprt) const
203 {
204  getCp_R(cprt);
205 }
206 
207 bool SurfPhase::addSpecies(shared_ptr<Species> spec)
208 {
209  bool added = ThermoPhase::addSpecies(spec);
210  if (added) {
211  m_h0.push_back(0.0);
212  m_s0.push_back(0.0);
213  m_cp0.push_back(0.0);
214  m_mu0.push_back(0.0);
215  m_work.push_back(0.0);
216  m_speciesSize.push_back(spec->size);
217  m_logsize.push_back(log(spec->size));
218  if (m_kk == 1) {
219  vector_fp cov{1.0};
220  setCoverages(cov.data());
221  }
222  }
223  return added;
224 }
225 
226 void SurfPhase::setSiteDensity(doublereal n0)
227 {
228  if (n0 <= 0.0) {
229  throw CanteraError("SurfPhase::setSiteDensity",
230  "Site density must be positive. Got {}", n0);
231  }
232  m_n0 = n0;
233  m_logn0 = log(m_n0);
234 }
235 
236 void SurfPhase::setCoverages(const doublereal* theta)
237 {
238  double sum = 0.0;
239  for (size_t k = 0; k < m_kk; k++) {
240  sum += theta[k];
241  }
242  if (sum <= 0.0) {
243  throw CanteraError("SurfPhase::setCoverages",
244  "Sum of Coverage fractions is zero or negative");
245  }
246  for (size_t k = 0; k < m_kk; k++) {
247  m_work[k] = m_n0*theta[k]/(sum*size(k));
248  }
249  // Call the Phase:: class function setConcentrations.
250  setConcentrations(m_work.data());
251 }
252 
253 void SurfPhase::setCoveragesNoNorm(const doublereal* theta)
254 {
255  for (size_t k = 0; k < m_kk; k++) {
256  m_work[k] = m_n0*theta[k]/size(k);
257  }
259 }
260 
261 void SurfPhase::getCoverages(doublereal* theta) const
262 {
263  getConcentrations(theta);
264  for (size_t k = 0; k < m_kk; k++) {
265  theta[k] *= size(k)/m_n0;
266  }
267 }
268 
269 void SurfPhase::setCoveragesByName(const std::string& cov)
270 {
272 }
273 
275 {
276  vector_fp cv(m_kk, 0.0);
277  bool ifound = false;
278  for (size_t k = 0; k < m_kk; k++) {
279  double c = getValue(cov, speciesName(k), 0.0);
280  if (c > 0.0) {
281  ifound = true;
282  cv[k] = c;
283  }
284  }
285  if (!ifound) {
286  throw CanteraError("SurfPhase::setCoveragesByName",
287  "Input coverages are all zero or negative");
288  }
289  setCoverages(cv.data());
290 }
291 
292 void SurfPhase::setState(const AnyMap& state) {
293  if (state.hasKey("coverages")) {
294  if (state["coverages"].is<string>()) {
295  setCoveragesByName(state["coverages"].asString());
296  } else {
297  setCoveragesByName(state["coverages"].asMap<double>());
298  }
299  }
300  ThermoPhase::setState(state);
301 }
302 
303 void SurfPhase::_updateThermo(bool force) const
304 {
305  doublereal tnow = temperature();
306  if (m_tlast != tnow || force) {
307  m_spthermo.update(tnow, m_cp0.data(), m_h0.data(), m_s0.data());
308  m_tlast = tnow;
309  for (size_t k = 0; k < m_kk; k++) {
310  m_h0[k] *= GasConstant * tnow;
311  m_s0[k] *= GasConstant;
312  m_cp0[k] *= GasConstant;
313  m_mu0[k] = m_h0[k] - tnow*m_s0[k];
314  }
315  m_tlast = tnow;
316  }
317 }
318 
320 {
321  eosdata._require("model","Surface");
322  doublereal n = getFloat(eosdata, "site_density", "toSI");
323  setSiteDensity(n);
324 }
325 
327 {
328  if (m_input.hasKey("site-density")) {
329  // Units are kmol/m^2 for surface phases or kmol/m for edge phases
330  setSiteDensity(m_input.convert("site-density",
331  Units(1.0, 0, -static_cast<double>(m_ndim), 0, 0, 0, 1)));
332  }
333 }
334 
336 {
337  double t;
338  if (getOptionalFloat(state, "temperature", t, "temperature")) {
339  setTemperature(t);
340  }
341 
342  if (state.hasChild("coverages")) {
343  string comp = getChildValue(state,"coverages");
344  setCoveragesByName(comp);
345  }
346 }
347 
348 EdgePhase::EdgePhase(doublereal n0) : SurfPhase(n0)
349 {
350  setNDim(1);
351 }
352 
354 {
355  eosdata._require("model","Edge");
356  doublereal n = getFloat(eosdata, "site_density", "toSI");
357  setSiteDensity(n);
358 }
359 
360 }
Declarations for the EdgePhase ThermoPhase object, which models the interface between two surfaces (s...
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:360
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
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
virtual void setParametersFromXML(const XML_Node &thermoData)
Set the Equation-of-State parameters by reading an XML Node Input.
Definition: SurfPhase.cpp:353
EdgePhase(doublereal n0=1.0)
Constructor.
Definition: SurfPhase.cpp:348
virtual void update(doublereal T, doublereal *cp_R, doublereal *h_RT, doublereal *s_R) const
Compute the reference-state properties for all species.
void getConcentrations(double *const c) const
Get the species concentrations (kmol/m^3).
Definition: Phase.cpp:625
doublereal mean_X(const doublereal *const Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
Definition: Phase.cpp:746
virtual void setConcentrationsNoNorm(const double *const conc)
Set the concentrations without ignoring negative concentrations.
Definition: Phase.cpp:652
void setNDim(size_t ndim)
Set the number of spatial dimensions (1, 2, or 3).
Definition: Phase.h:658
size_t m_kk
Number of species in the phase.
Definition: Phase.h:942
size_t m_ndim
Dimensionality of the phase.
Definition: Phase.h:946
virtual void setConcentrations(const double *const conc)
Set the concentrations to the specified values within the phase.
Definition: Phase.cpp:630
double concentration(const size_t k) const
Concentration of species k.
Definition: Phase.cpp:619
std::string speciesName(size_t k) const
Name of the species with index k.
Definition: Phase.cpp:229
double moleFraction(size_t k) const
Return the mole fraction of a single species.
Definition: Phase.cpp:577
doublereal temperature() const
Temperature (K).
Definition: Phase.h:667
const std::vector< std::string > & speciesNames() const
Return a const reference to the vector of species names.
Definition: Phase.cpp:235
virtual void setTemperature(const doublereal temp)
Set the internally stored temperature of the phase (K).
Definition: Phase.h:724
A simple thermodynamic model for a surface phase, assuming an ideal solution model.
Definition: SurfPhase.h:143
virtual void setStateFromXML(const XML_Node &state)
Set the initial state of the Surface Phase from an XML_Node.
Definition: SurfPhase.cpp:335
void setCoveragesNoNorm(const doublereal *theta)
Set the surface site fractions to a specified state.
Definition: SurfPhase.cpp:253
void setCoveragesByName(const std::string &cov)
Set the coverages from a string of colon-separated name:value pairs.
Definition: SurfPhase.cpp:269
virtual bool addSpecies(shared_ptr< Species > spec)
Definition: SurfPhase.cpp:207
virtual void getGibbs_RT_ref(doublereal *grt) const
Returns the vector of nondimensional Gibbs Free Energies of the reference state at the current temper...
Definition: SurfPhase.cpp:187
vector_fp m_s0
Temporary storage for the reference state entropies.
Definition: SurfPhase.h:430
virtual void getGibbs_RT(doublereal *grt) const
Get the nondimensional Gibbs functions for the species in their standard states at the current T and ...
Definition: SurfPhase.cpp:155
virtual void setState(const AnyMap &state)
Set the state using an AnyMap containing any combination of properties supported by the thermodynamic...
Definition: SurfPhase.cpp:292
virtual doublereal cp_mole() const
Molar heat capacity at constant pressure. Units: J/kmol/K.
Definition: SurfPhase.cpp:67
virtual void getPartialMolarEnthalpies(doublereal *hbar) const
Returns an array of partial molar enthalpies for the species in the mixture.
Definition: SurfPhase.cpp:78
vector_fp m_speciesSize
Vector of species sizes (number of sites occupied). length m_kk.
Definition: SurfPhase.h:418
virtual void getPartialMolarEntropies(doublereal *sbar) const
Returns an array of partial molar entropies of the species in the solution.
Definition: SurfPhase.cpp:86
virtual doublereal enthalpy_mole() const
Return the Molar Enthalpy. Units: J/kmol.
Definition: SurfPhase.cpp:42
virtual doublereal logStandardConc(size_t k=0) const
Natural logarithm of the standard concentration of the kth species.
Definition: SurfPhase.cpp:135
virtual void getPartialMolarVolumes(doublereal *vbar) const
Return an array of partial molar volumes for the species in the mixture.
Definition: SurfPhase.cpp:104
void getCoverages(doublereal *theta) const
Return a vector of surface coverages.
Definition: SurfPhase.cpp:261
virtual void getStandardVolumes(doublereal *vol) const
Get the molar volumes of the species standard states at the current T and P of the solution.
Definition: SurfPhase.cpp:179
void setSiteDensity(doublereal n0)
Set the site density of the surface phase (kmol m-2)
Definition: SurfPhase.cpp:226
virtual doublereal cv_mole() const
Molar heat capacity at constant volume. Units: J/kmol/K.
Definition: SurfPhase.cpp:73
virtual void getActivityConcentrations(doublereal *c) const
Return a vector of activity concentrations for each species.
Definition: SurfPhase.cpp:125
virtual void setParametersFromXML(const XML_Node &thermoData)
Set the Equation-of-State parameters by reading an XML Node Input.
Definition: SurfPhase.cpp:319
doublereal m_n0
Surface site density (kmol m-2)
Definition: SurfPhase.h:415
doublereal m_logn0
log of the surface site density
Definition: SurfPhase.h:421
vector_fp m_h0
Temporary storage for the reference state enthalpies.
Definition: SurfPhase.h:427
virtual void getCp_R(doublereal *cpr) const
Get the nondimensional Heat Capacities at constant pressure for the species standard states at the cu...
Definition: SurfPhase.cpp:173
virtual void getPartialMolarCp(doublereal *cpbar) const
Return an array of partial molar heat capacities for the species in the mixture.
Definition: SurfPhase.cpp:94
virtual void getStandardChemPotentials(doublereal *mu0) const
Get the array of chemical potentials at unit activity for the species at their standard states at the...
Definition: SurfPhase.cpp:109
virtual double size(size_t k) const
Returns the number of sites occupied by one molecule of species k.
Definition: SurfPhase.h:327
virtual void getEntropy_R(doublereal *sr) const
Get the array of nondimensional Entropy functions for the standard state species at the current T and...
Definition: SurfPhase.cpp:167
virtual doublereal entropy_mole() const
Return the Molar Entropy. Units: J/kmol-K.
Definition: SurfPhase.cpp:56
vector_fp m_work
Temporary work array.
Definition: SurfPhase.h:439
virtual void initThermo()
Initialize the ThermoPhase object after all species have been set up.
Definition: SurfPhase.cpp:326
vector_fp m_mu0
Temporary storage for the reference state Gibbs energies.
Definition: SurfPhase.h:436
virtual void getEntropy_R_ref(doublereal *er) const
Returns the vector of nondimensional entropies of the reference state at the current temperature of t...
Definition: SurfPhase.cpp:197
void _updateThermo(bool force=false) const
Update the species reference state thermodynamic functions.
Definition: SurfPhase.cpp:303
virtual void getCp_R_ref(doublereal *cprt) const
Returns the vector of nondimensional constant pressure heat capacities of the reference state at the ...
Definition: SurfPhase.cpp:202
vector_fp m_cp0
Temporary storage for the reference state heat capacities.
Definition: SurfPhase.h:433
virtual void getPureGibbs(doublereal *g) const
Get the Gibbs functions for the standard state of the species at the current T and P of the solution.
Definition: SurfPhase.cpp:149
virtual void setParameters(int n, doublereal *const c)
Set the equation of state parameters from the argument list.
Definition: SurfPhase.cpp:140
virtual void getChemPotentials(doublereal *mu) const
Get the species chemical potentials. Units: J/kmol.
Definition: SurfPhase.cpp:115
virtual void getEnthalpy_RT(doublereal *hrt) const
Get the nondimensional Enthalpy functions for the species at their standard states at the current T a...
Definition: SurfPhase.cpp:161
void setCoverages(const doublereal *theta)
Set the surface site fractions to a specified state.
Definition: SurfPhase.cpp:236
SurfPhase(doublereal n0=1.0)
Constructor.
Definition: SurfPhase.cpp:23
virtual doublereal intEnergy_mole() const
Return the Molar Internal Energy. Units: J/kmol.
Definition: SurfPhase.cpp:51
vector_fp m_logsize
vector storing the log of the size of each species.
Definition: SurfPhase.h:446
virtual void getEnthalpy_RT_ref(doublereal *hrt) const
Returns the vector of nondimensional enthalpies of the reference state at the current temperature of ...
Definition: SurfPhase.cpp:192
virtual doublereal standardConcentration(size_t k=0) const
Return the standard concentration for the kth species.
Definition: SurfPhase.cpp:130
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 setState(const AnyMap &state)
Set the state using an AnyMap containing any combination of properties supported by the thermodynamic...
virtual void initThermoFile(const std::string &inputFile, const std::string &id)
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
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
bool hasChild(const std::string &ch) const
Tests whether the current node has a child node with a particular name.
Definition: xml.cpp:528
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
std::map< std::string, double > compositionMap
Map connecting a string name with a double.
Definition: ct_defs.h:172
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
const U & getValue(const std::map< T, U > &m, const T &key, const U &default_val)
Const accessor for a value in a std::map.
Definition: utilities.h:528
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
bool getOptionalFloat(const XML_Node &parent, const std::string &name, doublereal &fltRtn, const std::string &type)
Get an optional floating-point value from a child element.
Definition: ctml.cpp:212
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:131
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition: utilities.h:135
compositionMap parseCompString(const std::string &ss, const std::vector< std::string > &names)
Parse a composition string into a map consisting of individual key:composition pairs.
Definition: stringUtils.cpp:60
Contains declarations for string manipulation functions within Cantera.
Various templated functions that carry out common vector operations (see Templated Utility Functions)...