Cantera  4.0.0a1
Loading...
Searching...
No Matches
IdealGasPhase.cpp
Go to the documentation of this file.
1/**
2 * @file IdealGasPhase.cpp
3 * ThermoPhase object for the ideal gas equation of
4 * state - workhorse for %Cantera (see @ref thermoprops
5 * and class @link Cantera::IdealGasPhase IdealGasPhase@endlink).
6 */
7
8// This file is part of Cantera. See License.txt in the top-level directory or
9// at https://cantera.org/license.txt for license and copyright information.
10
14
15namespace Cantera
16{
17
18IdealGasPhase::IdealGasPhase(const string& inputFile, const string& id_)
19{
20 initThermoFile(inputFile, id_);
21}
22
23// Molar Thermodynamic Properties of the Solution ------------------
24
26{
27 return GasConstant * (mean_X(entropy_R_ref()) - sum_xlogx() - std::log(pressure() / refPressure()));
28}
29
31{
32 return GasConstant * mean_X(cp_R_ref());
33}
34
36{
37 return cp_mole() - GasConstant;
38}
39
41 return sqrt(
43 );
44}
45
47{
48 return pressure() / RT();
49}
50
51void IdealGasPhase::getActivityCoefficients(span<double> ac) const
52{
53 checkArraySize("IdealGasPhase::getActivityCoefficients", ac.size(), m_kk);
54 for (size_t k = 0; k < m_kk; k++) {
55 ac[k] = 1.0;
56 }
57}
58
59void IdealGasPhase::getStandardChemPotentials(span<double> muStar) const
60{
61 getGibbs_ref(muStar);
62 double tmp = log(pressure() / refPressure()) * RT();
63 for (size_t k = 0; k < m_kk; k++) {
64 muStar[k] += tmp; // add RT*ln(P/P_0)
65 }
66}
67
68// Partial Molar Properties of the Solution --------------
69
70void IdealGasPhase::getChemPotentials(span<double> mu) const
71{
73 for (size_t k = 0; k < m_kk; k++) {
74 double xx = std::max(SmallNumber, moleFraction(k));
75 mu[k] += RT() * log(xx);
76 }
77}
78
79void IdealGasPhase::getPartialMolarEnthalpies(span<double> hbar) const
80{
81 checkArraySize("IdealGasPhase::getPartialMolarEnthalpies", hbar.size(), m_kk);
82 auto _h = enthalpy_RT_ref();
83 scale(_h.begin(), _h.end(), hbar.begin(), RT());
84}
85
86void IdealGasPhase::getPartialMolarEntropies(span<double> sbar) const
87{
88 checkArraySize("IdealGasPhase::getPartialMolarEntropies", sbar.size(), m_kk);
89 auto _s = entropy_R_ref();
90 scale(_s.begin(), _s.end(), sbar.begin(), GasConstant);
91 double logp = log(pressure() / refPressure());
92 for (size_t k = 0; k < m_kk; k++) {
93 double xx = std::max(SmallNumber, moleFraction(k));
94 sbar[k] += GasConstant * (-logp - log(xx));
95 }
96}
97
98void IdealGasPhase::getPartialMolarIntEnergies(span<double> ubar) const
99{
100 checkArraySize("IdealGasPhase::getPartialMolarIntEnergies", ubar.size(), m_kk);
101 auto _h = enthalpy_RT_ref();
102 for (size_t k = 0; k < m_kk; k++) {
103 ubar[k] = RT() * (_h[k] - 1.0);
104 }
105}
106
107void IdealGasPhase::getPartialMolarCp(span<double> cpbar) const
108{
109 checkArraySize("IdealGasPhase::getPartialMolarCp", cpbar.size(), m_kk);
110 auto _cp = cp_R_ref();
111 scale(_cp.begin(), _cp.end(), cpbar.begin(), GasConstant);
112}
113
114void IdealGasPhase::getPartialMolarVolumes(span<double> vbar) const
115{
116 checkArraySize("IdealGasPhase::getPartialMolarVolumes", vbar.size(), m_kk);
117 double vol = 1.0 / molarDensity();
118 for (size_t k = 0; k < m_kk; k++) {
119 vbar[k] = vol;
120 }
121}
122
123// Properties of the Standard State of the Species in the Solution --
124
125void IdealGasPhase::getEnthalpy_RT(span<double> hrt) const
126{
127 checkArraySize("IdealGasPhase::getEnthalpy_RT", hrt.size(), m_kk);
128 auto _h = enthalpy_RT_ref();
129 copy(_h.begin(), _h.end(), hrt.begin());
130}
131
132void IdealGasPhase::getEntropy_R(span<double> sr) const
133{
134 checkArraySize("IdealGasPhase::getEntropy_R", sr.size(), m_kk);
135 auto _s = entropy_R_ref();
136 copy(_s.begin(), _s.end(), sr.begin());
137 double tmp = log(pressure() / refPressure());
138 for (size_t k = 0; k < m_kk; k++) {
139 sr[k] -= tmp;
140 }
141}
142
143void IdealGasPhase::getGibbs_RT(span<double> grt) const
144{
145 checkArraySize("IdealGasPhase::getGibbs_RT", grt.size(), m_kk);
146 auto gibbsrt = gibbs_RT_ref();
147 copy(gibbsrt.begin(), gibbsrt.end(), grt.begin());
148 double tmp = log(pressure() / refPressure());
149 for (size_t k = 0; k < m_kk; k++) {
150 grt[k] += tmp;
151 }
152}
153
154void IdealGasPhase::getIntEnergy_RT(span<double> urt) const
155{
157}
158
159void IdealGasPhase::getCp_R(span<double> cpr) const
160{
161 checkArraySize("IdealGasPhase::getCp_R", cpr.size(), m_kk);
162 auto _cpr = cp_R_ref();
163 copy(_cpr.begin(), _cpr.end(), cpr.begin());
164}
165
166void IdealGasPhase::getStandardVolumes(span<double> vol) const
167{
168 checkArraySize("IdealGasPhase::getStandardVolumes", vol.size(), m_kk);
169 double tmp = 1.0 / molarDensity();
170 for (size_t k = 0; k < m_kk; k++) {
171 vol[k] = tmp;
172 }
173}
174
175// Thermodynamic Values for the Species Reference States ---------
176
177void IdealGasPhase::getEnthalpy_RT_ref(span<double> hrt) const
178{
179 checkArraySize("IdealGasPhase::getEnthalpy_RT_ref", hrt.size(), m_kk);
180 auto _h = enthalpy_RT_ref();
181 copy(_h.begin(), _h.end(), hrt.begin());
182}
183
184void IdealGasPhase::getGibbs_RT_ref(span<double> grt) const
185{
186 checkArraySize("IdealGasPhase::getGibbs_RT_ref", grt.size(), m_kk);
187 auto gibbsrt = gibbs_RT_ref();
188 copy(gibbsrt.begin(), gibbsrt.end(), grt.begin());
189}
190
191void IdealGasPhase::getGibbs_ref(span<double> g) const
192{
193 checkArraySize("IdealGasPhase::getGibbs_ref", g.size(), m_kk);
194 auto gibbsrt = gibbs_RT_ref();
195 scale(gibbsrt.begin(), gibbsrt.end(), g.begin(), RT());
196}
197
198void IdealGasPhase::getEntropy_R_ref(span<double> er) const
199{
200 checkArraySize("IdealGasPhase::getEntropy_R_ref", er.size(), m_kk);
201 auto _s = entropy_R_ref();
202 copy(_s.begin(), _s.end(), er.begin());
203}
204
205void IdealGasPhase::getIntEnergy_RT_ref(span<double> urt) const
206{
207 checkArraySize("IdealGasPhase::getIntEnergy_RT_ref", urt.size(), m_kk);
208 auto _h = enthalpy_RT_ref();
209 for (size_t k = 0; k < m_kk; k++) {
210 urt[k] = _h[k] - 1.0;
211 }
212}
213
214void IdealGasPhase::getCp_R_ref(span<double> cprt) const
215{
216 checkArraySize("IdealGasPhase::getCp_R_ref", cprt.size(), m_kk);
217 auto _cpr = cp_R_ref();
218 copy(_cpr.begin(), _cpr.end(), cprt.begin());
219}
220
221void IdealGasPhase::getStandardVolumes_ref(span<double> vol) const
222{
223 checkArraySize("IdealGasPhase::getStandardVolumes_ref", vol.size(), m_kk);
224 double tmp = RT() / m_p0;
225 for (size_t k = 0; k < m_kk; k++) {
226 vol[k] = tmp;
227 }
228}
229
230bool IdealGasPhase::addSpecies(shared_ptr<Species> spec)
231{
232 bool added = ThermoPhase::addSpecies(spec);
233 if (added) {
234 if (m_kk == 1) {
235 m_p0 = refPressure();
236 }
237 m_h0_RT.push_back(0.0);
238 m_g0_RT.push_back(0.0);
239 m_expg0_RT.push_back(0.0);
240 m_cp0_R.push_back(0.0);
241 m_s0_R.push_back(0.0);
242 m_pp.push_back(0.0);
243 }
244 return added;
245}
246
247void IdealGasPhase::setToEquilState(span<const double> mu_RT)
248{
249 auto grt = gibbs_RT_ref();
250
251 // Within the method, we protect against inf results if the exponent is too
252 // high.
253 //
254 // If it is too low, we set the partial pressure to zero. This capability is
255 // needed by the elemental potential method.
256 double pres = 0.0;
257 for (size_t k = 0; k < m_kk; k++) {
258 double tmp = -grt[k] + mu_RT[k];
259 if (tmp < -600.) {
260 m_pp[k] = 0.0;
261 } else if (tmp > 300.0) {
262 double tmp2 = tmp / 300.;
263 tmp2 *= tmp2;
264 m_pp[k] = m_p0 * exp(300.) * tmp2;
265 } else {
266 m_pp[k] = m_p0 * exp(tmp);
267 }
268 pres += m_pp[k];
269 }
270 // set state
272 setPressure(pres);
273}
274
276{
277 static const int cacheId = m_cache.getId();
278 CachedScalar cached = m_cache.getScalar(cacheId);
279 double tnow = temperature();
280
281 // If the temperature has changed since the last time these
282 // properties were computed, recompute them.
283 if (cached.state1 != tnow) {
285 cached.state1 = tnow;
286
287 // update the species Gibbs functions
288 for (size_t k = 0; k < m_kk; k++) {
289 m_g0_RT[k] = m_h0_RT[k] - m_s0_R[k];
290 }
291 }
292}
293}
ThermoPhase object for the ideal gas equation of state - workhorse for Cantera (see Thermodynamic Pro...
Headers for the factory class that can create known ThermoPhase objects (see Thermodynamic Properties...
void getGibbs_ref(span< double > g) const override
Returns the vector of the Gibbs function of the reference state at the current temperature of the sol...
void getCp_R(span< double > cpr) const override
Get the nondimensional Heat Capacities at constant pressure for the species standard states at the cu...
void getEntropy_R_ref(span< double > er) const override
Returns the vector of nondimensional entropies of the reference state at the current temperature of t...
double m_p0
Reference state pressure.
void getPartialMolarEnthalpies(span< double > hbar) const override
Returns an array of partial molar enthalpies for the species in the mixture.
double soundSpeed() const override
Return the speed of sound. Units: m/s.
double pressure() const override
Pressure.
vector< double > m_g0_RT
Temporary storage for dimensionless reference state Gibbs energies.
void getIntEnergy_RT(span< double > urt) const override
Returns the vector of nondimensional Internal Energies of the standard state species at the current T...
vector< double > m_h0_RT
Temporary storage for dimensionless reference state enthalpies.
void getPartialMolarCp(span< double > cpbar) const override
Return an array of partial molar heat capacities for the species in the mixture.
vector< double > m_pp
Temporary array containing internally calculated partial pressures.
span< const double > enthalpy_RT_ref() const
Returns a reference to the dimensionless reference state enthalpy vector.
void getStandardChemPotentials(span< double > mu) const override
Get the array of chemical potentials at unit activity for the species at their standard states at the...
void getActivityCoefficients(span< double > ac) const override
Get the array of non-dimensional activity coefficients at the current solution temperature,...
void getGibbs_RT(span< double > grt) const override
Get the nondimensional Gibbs functions for the species in their standard states at the current T and ...
void getCp_R_ref(span< double > cprt) const override
Returns the vector of nondimensional constant pressure heat capacities of the reference state at the ...
void setPressure(double p) override
Set the pressure at constant temperature and composition.
void getIntEnergy_RT_ref(span< double > urt) const override
Returns the vector of nondimensional internal Energies of the reference state at the current temperat...
void getEnthalpy_RT_ref(span< double > hrt) const override
Returns the vector of nondimensional enthalpies of the reference state at the current temperature of ...
double cv_mole() const override
Molar heat capacity at constant volume and composition [J/kmol/K].
void getEnthalpy_RT(span< double > hrt) const override
Get the nondimensional Enthalpy functions for the species at their standard states at the current T a...
virtual void updateThermo() const
Update the species reference state thermodynamic functions.
void setToEquilState(span< const double > mu_RT) override
This method is used by the ChemEquil equilibrium solver.
void getEntropy_R(span< double > sr) const override
Get the array of nondimensional Entropy functions for the standard state species at the current T and...
vector< double > m_s0_R
Temporary storage for dimensionless reference state entropies.
double entropy_mole() const override
Molar entropy.
void getGibbs_RT_ref(span< double > grt) const override
Returns the vector of nondimensional Gibbs Free Energies of the reference state at the current temper...
span< const double > cp_R_ref() const
Returns a reference to the dimensionless reference state Heat Capacity vector.
double cp_mole() const override
Molar heat capacity at constant pressure and composition [J/kmol/K].
span< const double > entropy_R_ref() const
Returns a reference to the dimensionless reference state Entropy vector.
void getPartialMolarVolumes(span< double > vbar) const override
Return an array of partial molar volumes for the species in the mixture.
void getStandardVolumes(span< double > vol) const override
Get the molar volumes of the species standard states at the current T and P of the solution.
void getPartialMolarEntropies(span< double > sbar) const override
Returns an array of partial molar entropies of the species in the solution.
double standardConcentration(size_t k=0) const override
Returns the standard concentration , which is used to normalize the generalized concentration.
vector< double > m_cp0_R
Temporary storage for dimensionless reference state heat capacities.
bool addSpecies(shared_ptr< Species > spec) override
Add a Species to this Phase.
span< const double > gibbs_RT_ref() const
Returns a reference to the dimensionless reference state Gibbs free energy vector.
void getChemPotentials(span< double > mu) const override
Get the species chemical potentials. Units: J/kmol.
void getStandardVolumes_ref(span< double > vol) const override
Get the molar volumes of the species reference states at the current T and P_ref of the solution.
IdealGasPhase(const string &inputFile="", const string &id="")
Construct and initialize an IdealGasPhase ThermoPhase object directly from an input file.
void getPartialMolarIntEnergies(span< double > ubar) const override
Return an array of partial molar internal energies for the species in the mixture.
virtual void update(double T, span< double > cp_R, span< double > h_RT, span< double > s_R) const
Compute the reference-state properties for all species.
virtual double molarDensity() const
Molar density (kmol/m^3).
Definition Phase.cpp:587
ValueCache m_cache
Cached for saved calculations within each ThermoPhase.
Definition Phase.h:855
size_t m_kk
Number of species in the phase.
Definition Phase.h:875
double temperature() const
Temperature (K).
Definition Phase.h:585
double meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition Phase.h:676
double sum_xlogx() const
Evaluate .
Definition Phase.cpp:633
double mean_X(span< const double > Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
Definition Phase.cpp:627
double moleFraction(size_t k) const
Return the mole fraction of a single species.
Definition Phase.cpp:447
virtual void setMoleFractions(span< const double > x)
Set the mole fractions to the specified values.
Definition Phase.cpp:283
double RT() const
Return the Gas Constant multiplied by the current temperature.
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.
virtual double refPressure() const
Returns the reference pressure in Pa.
bool addSpecies(shared_ptr< Species > spec) override
Add a Species to this Phase.
CachedScalar getScalar(int id)
Get a reference to a CachedValue object representing a scalar (double) with the given id.
Definition ValueCache.h:161
int getId()
Get a unique id for a cached value.
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition utilities.h:118
const double GasConstant
Universal Gas Constant [J/kmol/K].
Definition ct_defs.h:123
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
const double SmallNumber
smallest number to compare to zero.
Definition ct_defs.h:161
void checkArraySize(const char *procedure, size_t available, size_t required)
Wrapper for throwing ArraySizeError.
A cached property value and the state at which it was evaluated.
Definition ValueCache.h:33
double state1
Value of the first state variable for the state at which value was evaluated, for example temperature...
Definition ValueCache.h:102
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...