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::getPartialMolarIntEnergies_TV(span<double> utilde) const
108{
110}
111
112void IdealGasPhase::getPartialMolarCp(span<double> cpbar) const
113{
114 checkArraySize("IdealGasPhase::getPartialMolarCp", cpbar.size(), m_kk);
115 auto _cp = cp_R_ref();
116 scale(_cp.begin(), _cp.end(), cpbar.begin(), GasConstant);
117}
118
119void IdealGasPhase::getPartialMolarCv_TV(span<double> cvtilde) const
120{
121 checkArraySize("IdealGasPhase::getPartialMolarCv_TV", cvtilde.size(), m_kk);
122 auto _cp = cp_R_ref();
123 for (size_t k = 0; k < m_kk; k++) {
124 cvtilde[k] = GasConstant * (_cp[k] - 1.0);
125 }
126}
127
128void IdealGasPhase::getPartialMolarVolumes(span<double> vbar) const
129{
130 checkArraySize("IdealGasPhase::getPartialMolarVolumes", vbar.size(), m_kk);
131 double vol = 1.0 / molarDensity();
132 for (size_t k = 0; k < m_kk; k++) {
133 vbar[k] = vol;
134 }
135}
136
137// Properties of the Standard State of the Species in the Solution --
138
139void IdealGasPhase::getEnthalpy_RT(span<double> hrt) const
140{
141 checkArraySize("IdealGasPhase::getEnthalpy_RT", hrt.size(), m_kk);
142 auto _h = enthalpy_RT_ref();
143 copy(_h.begin(), _h.end(), hrt.begin());
144}
145
146void IdealGasPhase::getEntropy_R(span<double> sr) const
147{
148 checkArraySize("IdealGasPhase::getEntropy_R", sr.size(), m_kk);
149 auto _s = entropy_R_ref();
150 copy(_s.begin(), _s.end(), sr.begin());
151 double tmp = log(pressure() / refPressure());
152 for (size_t k = 0; k < m_kk; k++) {
153 sr[k] -= tmp;
154 }
155}
156
157void IdealGasPhase::getGibbs_RT(span<double> grt) const
158{
159 checkArraySize("IdealGasPhase::getGibbs_RT", grt.size(), m_kk);
160 auto gibbsrt = gibbs_RT_ref();
161 copy(gibbsrt.begin(), gibbsrt.end(), grt.begin());
162 double tmp = log(pressure() / refPressure());
163 for (size_t k = 0; k < m_kk; k++) {
164 grt[k] += tmp;
165 }
166}
167
168void IdealGasPhase::getIntEnergy_RT(span<double> urt) const
169{
171}
172
173void IdealGasPhase::getCp_R(span<double> cpr) const
174{
175 checkArraySize("IdealGasPhase::getCp_R", cpr.size(), m_kk);
176 auto _cpr = cp_R_ref();
177 copy(_cpr.begin(), _cpr.end(), cpr.begin());
178}
179
180void IdealGasPhase::getStandardVolumes(span<double> vol) const
181{
182 checkArraySize("IdealGasPhase::getStandardVolumes", vol.size(), m_kk);
183 double tmp = 1.0 / molarDensity();
184 for (size_t k = 0; k < m_kk; k++) {
185 vol[k] = tmp;
186 }
187}
188
189// Thermodynamic Values for the Species Reference States ---------
190
191void IdealGasPhase::getEnthalpy_RT_ref(span<double> hrt) const
192{
193 checkArraySize("IdealGasPhase::getEnthalpy_RT_ref", hrt.size(), m_kk);
194 auto _h = enthalpy_RT_ref();
195 copy(_h.begin(), _h.end(), hrt.begin());
196}
197
198void IdealGasPhase::getGibbs_RT_ref(span<double> grt) const
199{
200 checkArraySize("IdealGasPhase::getGibbs_RT_ref", grt.size(), m_kk);
201 auto gibbsrt = gibbs_RT_ref();
202 copy(gibbsrt.begin(), gibbsrt.end(), grt.begin());
203}
204
205void IdealGasPhase::getGibbs_ref(span<double> g) const
206{
207 checkArraySize("IdealGasPhase::getGibbs_ref", g.size(), m_kk);
208 auto gibbsrt = gibbs_RT_ref();
209 scale(gibbsrt.begin(), gibbsrt.end(), g.begin(), RT());
210}
211
212void IdealGasPhase::getEntropy_R_ref(span<double> er) const
213{
214 checkArraySize("IdealGasPhase::getEntropy_R_ref", er.size(), m_kk);
215 auto _s = entropy_R_ref();
216 copy(_s.begin(), _s.end(), er.begin());
217}
218
219void IdealGasPhase::getIntEnergy_RT_ref(span<double> urt) const
220{
221 checkArraySize("IdealGasPhase::getIntEnergy_RT_ref", urt.size(), m_kk);
222 auto _h = enthalpy_RT_ref();
223 for (size_t k = 0; k < m_kk; k++) {
224 urt[k] = _h[k] - 1.0;
225 }
226}
227
228void IdealGasPhase::getCp_R_ref(span<double> cprt) const
229{
230 checkArraySize("IdealGasPhase::getCp_R_ref", cprt.size(), m_kk);
231 auto _cpr = cp_R_ref();
232 copy(_cpr.begin(), _cpr.end(), cprt.begin());
233}
234
235void IdealGasPhase::getStandardVolumes_ref(span<double> vol) const
236{
237 checkArraySize("IdealGasPhase::getStandardVolumes_ref", vol.size(), m_kk);
238 double tmp = RT() / m_p0;
239 for (size_t k = 0; k < m_kk; k++) {
240 vol[k] = tmp;
241 }
242}
243
244bool IdealGasPhase::addSpecies(shared_ptr<Species> spec)
245{
246 bool added = ThermoPhase::addSpecies(spec);
247 if (added) {
248 if (m_kk == 1) {
249 m_p0 = refPressure();
250 }
251 m_h0_RT.push_back(0.0);
252 m_g0_RT.push_back(0.0);
253 m_expg0_RT.push_back(0.0);
254 m_cp0_R.push_back(0.0);
255 m_s0_R.push_back(0.0);
256 m_pp.push_back(0.0);
257 }
258 return added;
259}
260
261void IdealGasPhase::setToEquilState(span<const double> mu_RT)
262{
263 auto grt = gibbs_RT_ref();
264
265 // Within the method, we protect against inf results if the exponent is too
266 // high.
267 //
268 // If it is too low, we set the partial pressure to zero. This capability is
269 // needed by the elemental potential method.
270 double pres = 0.0;
271 for (size_t k = 0; k < m_kk; k++) {
272 double tmp = -grt[k] + mu_RT[k];
273 if (tmp < -600.) {
274 m_pp[k] = 0.0;
275 } else if (tmp > 300.0) {
276 double tmp2 = tmp / 300.;
277 tmp2 *= tmp2;
278 m_pp[k] = m_p0 * exp(300.) * tmp2;
279 } else {
280 m_pp[k] = m_p0 * exp(tmp);
281 }
282 pres += m_pp[k];
283 }
284 // set state
286 setPressure(pres);
287}
288
290{
291 static const int cacheId = m_cache.getId();
292 CachedScalar cached = m_cache.getScalar(cacheId);
293 double tnow = temperature();
294
295 // If the temperature has changed since the last time these
296 // properties were computed, recompute them.
297 if (cached.state1 != tnow) {
299 cached.state1 = tnow;
300
301 // update the species Gibbs functions
302 for (size_t k = 0; k < m_kk; k++) {
303 m_g0_RT[k] = m_h0_RT[k] - m_s0_R[k];
304 }
305 }
306}
307}
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 getPartialMolarIntEnergies_TV(span< double > utilde) const override
Return an array of partial molar internal energies at constant temperature and volume [J/kmol].
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 getPartialMolarCv_TV(span< double > cvtilde) const override
Return an array of species molar heat capacities associated with the constant-volume partial molar in...
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...