Cantera  2.1.2
SingleSpeciesTP.cpp
Go to the documentation of this file.
1 /**
2  * @file SingleSpeciesTP.cpp
3  * Definitions for the %SingleSpeciesTP class, which is a filter class for %ThermoPhase,
4  * that eases the construction of single species phases
5  * ( see \ref thermoprops and class \link Cantera::SingleSpeciesTP SingleSpeciesTP\endlink).
6  */
7 
8 /*
9  * Copyright (2005) Sandia Corporation. Under the terms of
10  * Contract DE-AC04-94AL85000 with Sandia Corporation, the
11  * U.S. Government retains certain rights in this software.
12  */
15 
16 using namespace std;
17 
18 namespace Cantera
19 {
20 SingleSpeciesTP::SingleSpeciesTP() :
21  ThermoPhase(),
22  m_press(OneAtm),
23  m_p0(OneAtm),
24  m_tlast(-1.0)
25 {
26 }
27 
29  ThermoPhase(),
30  m_press(OneAtm),
31  m_p0(OneAtm),
32  m_tlast(-1.0)
33 {
34  *this = operator=(right);
35 }
36 
38 {
39  if (&right != this) {
41  m_press = right.m_press;
42  m_p0 = right.m_p0;
43  m_tlast = right.m_tlast;
44  m_h0_RT = right.m_h0_RT;
45  m_cp0_R = right.m_cp0_R;
46  m_s0_R = right.m_s0_R;
47  }
48  return *this;
49 }
50 
52 {
53  return new SingleSpeciesTP(*this);
54 }
55 
57 {
58  err("eosType");
59  return -1;
60 }
61 
62 /*
63  * ------------ Molar Thermodynamic Properties --------------------
64  */
65 
67 {
68  double hbar;
70  return hbar;
71 }
72 
74 {
75  double ubar;
77  return ubar;
78 }
79 
81 {
82  double sbar;
84  return sbar;
85 }
86 
87 doublereal SingleSpeciesTP::gibbs_mole() const
88 {
89  double gbar;
90  /*
91  * Get the chemical potential of the first species.
92  * This is the same as the partial molar Gibbs
93  * free energy.
94  */
95  getChemPotentials(&gbar);
96  return gbar;
97 }
98 
99 doublereal SingleSpeciesTP::cp_mole() const
100 {
101  double cpbar;
102  /*
103  * Really should have a partial molar heat capacity
104  * function in ThermoPhase. However, the standard
105  * state heat capacity will do fine here for now.
106  */
107  //getPartialMolarCp(&cpbar);
108  getCp_R(&cpbar);
109  cpbar *= GasConstant;
110  return cpbar;
111 }
112 
113 doublereal SingleSpeciesTP::cv_mole() const
114 {
115  /*
116  * For single species, we go directory to the general Cp - Cv relation
117  *
118  * Cp = Cv + alpha**2 * V * T / beta
119  *
120  * where
121  * alpha = volume thermal expansion coefficient
122  * beta = isothermal compressibility
123  */
124  doublereal cvbar = cp_mole();
125  doublereal alpha = thermalExpansionCoeff();
126  doublereal beta = isothermalCompressibility();
127  doublereal molecW = molecularWeight(0);
128  doublereal V = molecW/density();
129  doublereal T = temperature();
130  if (beta != 0.0) {
131  cvbar -= alpha * alpha * V * T / beta;
132  }
133  return cvbar;
134 }
135 
136 /*
137  * ----------- Partial Molar Properties of the Solution -----------------
138  */
139 
140 void SingleSpeciesTP::getChemPotentials(doublereal* mu) const
141 {
143 }
144 
145 void SingleSpeciesTP::getChemPotentials_RT(doublereal* murt) const
146 {
148  double rt = GasConstant * temperature();
149  murt[0] /= rt;
150 }
151 
153 {
154  getChemPotentials(mu);
155 }
156 
158 getPartialMolarEnthalpies(doublereal* hbar) const
159 {
160  double _rt = GasConstant * temperature();
161  getEnthalpy_RT(hbar);
162  hbar[0] *= _rt;
163 }
164 
166 getPartialMolarIntEnergies(doublereal* ubar) const
167 {
168  double _rt = GasConstant * temperature();
169  getIntEnergy_RT(ubar);
170  ubar[0] *= _rt;
171 }
172 
174 getPartialMolarEntropies(doublereal* sbar) const
175 {
176  getEntropy_R(sbar);
177  sbar[0] *= GasConstant;
178 }
179 
180 void SingleSpeciesTP::getPartialMolarCp(doublereal* cpbar) const
181 {
182  getCp_R(cpbar);
183  cpbar[0] *= GasConstant;
184 }
185 
186 void SingleSpeciesTP::getPartialMolarVolumes(doublereal* vbar) const
187 {
188  double mw = molecularWeight(0);
189  double dens = density();
190  vbar[0] = mw / dens;
191 }
192 
193 /*
194  * Properties of the Standard State of the Species in the Solution
195  */
196 
197 void SingleSpeciesTP::getPureGibbs(doublereal* gpure) const
198 {
199  getGibbs_RT(gpure);
200  gpure[0] *= GasConstant * temperature();
201 }
202 
203 void SingleSpeciesTP::getStandardVolumes(doublereal* vbar) const
204 {
205  double mw = molecularWeight(0);
206  double dens = density();
207  vbar[0] = mw / dens;
208 }
209 
210 /*
211  * ---- Thermodynamic Values for the Species Reference States -------
212  */
213 
214 void SingleSpeciesTP::getEnthalpy_RT_ref(doublereal* hrt) const
215 {
216  _updateThermo();
217  hrt[0] = m_h0_RT[0];
218 }
219 
220 void SingleSpeciesTP::getGibbs_RT_ref(doublereal* grt) const
221 {
222  _updateThermo();
223  grt[0] = m_h0_RT[0] - m_s0_R[0];
224 }
225 
226 void SingleSpeciesTP::getGibbs_ref(doublereal* g) const
227 {
228  getGibbs_RT_ref(g);
229  g[0] *= GasConstant * temperature();
230 }
231 
232 void SingleSpeciesTP::getEntropy_R_ref(doublereal* er) const
233 {
234  _updateThermo();
235  er[0] = m_s0_R[0];
236 }
237 
238 void SingleSpeciesTP::getCp_R_ref(doublereal* cpr) const
239 {
240  _updateThermo();
241  cpr[0] = m_cp0_R[0];
242 }
243 
244 /*
245  * ------------------ Setting the State ------------------------
246  */
247 
248 void SingleSpeciesTP::setState_TPX(doublereal t, doublereal p,
249  const doublereal* x)
250 {
251  setTemperature(t);
252  setPressure(p);
253 }
254 
255 void SingleSpeciesTP::setState_TPX(doublereal t, doublereal p,
256  compositionMap& x)
257 {
258  setTemperature(t);
259  setPressure(p);
260 }
261 
262 void SingleSpeciesTP::setState_TPX(doublereal t, doublereal p,
263  const std::string& x)
264 {
265  setTemperature(t);
266  setPressure(p);
267 }
268 
269 void SingleSpeciesTP::setState_TPY(doublereal t, doublereal p,
270  const doublereal* y)
271 {
272  setTemperature(t);
273  setPressure(p);
274 }
275 
276 void SingleSpeciesTP::setState_TPY(doublereal t, doublereal p,
277  compositionMap& y)
278 {
279  setTemperature(t);
280  setPressure(p);
281 }
282 
283 void SingleSpeciesTP::setState_TPY(doublereal t, doublereal p,
284  const std::string& y)
285 {
286  setTemperature(t);
287  setPressure(p);
288 }
289 
290 void SingleSpeciesTP::setState_PX(doublereal p, doublereal* x)
291 {
292  if (x[0] != 1.0) {
293  err("setStatePX -> x[0] not 1.0");
294  }
295  setPressure(p);
296 }
297 
298 void SingleSpeciesTP::setState_PY(doublereal p, doublereal* y)
299 {
300  if (y[0] != 1.0) {
301  err("setStatePY -> x[0] not 1.0");
302  }
303  setPressure(p);
304 }
305 
306 void SingleSpeciesTP::setState_HP(doublereal h, doublereal p,
307  doublereal tol)
308 {
309  doublereal dt;
310  setPressure(p);
311  for (int n = 0; n < 50; n++) {
312  dt = (h - enthalpy_mass())/cp_mass();
313  if (dt > 100.0) {
314  dt = 100.0;
315  } else if (dt < -100.0) {
316  dt = -100.0;
317  }
318  setState_TP(temperature() + dt, p);
319  if (fabs(dt) < tol) {
320  return;
321  }
322  }
323  throw CanteraError("setState_HP","no convergence. dt = " + fp2str(dt));
324 }
325 
326 void SingleSpeciesTP::setState_UV(doublereal u, doublereal v,
327  doublereal tol)
328 {
329  doublereal dt;
330  if (v == 0.0) {
331  setDensity(1.0E100);
332  } else {
333  setDensity(1.0/v);
334  }
335  for (int n = 0; n < 50; n++) {
336  dt = (u - intEnergy_mass())/cv_mass();
337  if (dt > 100.0) {
338  dt = 100.0;
339  } else if (dt < -100.0) {
340  dt = -100.0;
341  }
342  setTemperature(temperature() + dt);
343  if (fabs(dt) < tol) {
344  return;
345  }
346  }
347  throw CanteraError("setState_UV",
348  "no convergence. dt = " + fp2str(dt)+"\n"
349  +"u = "+fp2str(u)+" v = "+fp2str(v)+"\n");
350 }
351 
352 void SingleSpeciesTP::setState_SP(doublereal s, doublereal p,
353  doublereal tol)
354 {
355  doublereal dt;
356  setPressure(p);
357  for (int n = 0; n < 50; n++) {
358  dt = (s - entropy_mass())*temperature()/cp_mass();
359  if (dt > 100.0) {
360  dt = 100.0;
361  } else if (dt < -100.0) {
362  dt = -100.0;
363  }
364  setState_TP(temperature() + dt, p);
365  if (fabs(dt) < tol) {
366  return;
367  }
368  }
369  throw CanteraError("setState_SP","no convergence. dt = " + fp2str(dt));
370 }
371 
372 void SingleSpeciesTP::setState_SV(doublereal s, doublereal v,
373  doublereal tol)
374 {
375  doublereal dt;
376  if (v == 0.0) {
377  setDensity(1.0E100);
378  } else {
379  setDensity(1.0/v);
380  }
381  for (int n = 0; n < 50; n++) {
382  dt = (s - entropy_mass())*temperature()/cv_mass();
383  if (dt > 100.0) {
384  dt = 100.0;
385  } else if (dt < -100.0) {
386  dt = -100.0;
387  }
388  setTemperature(temperature() + dt);
389  if (fabs(dt) < tol) {
390  return;
391  }
392  }
393  throw CanteraError("setState_SV","no convergence. dt = " + fp2str(dt));
394 }
395 
396 doublereal SingleSpeciesTP::err(const std::string& msg) const
397 {
398  throw CanteraError("SingleSpeciesTP","Base class method "
399  +msg+" called. Equation of state type: "
400  +int2str(eosType()));
401  return 0;
402 }
403 
405 {
406  /*
407  * Make sure there is one and only one species in this phase.
408  */
409  if (nSpecies() != 1) {
410  throw CanteraError("initThermo",
411  "stoichiometric substances may only contain one species.");
412  }
413 
414  /*
415  * Resize temporary arrays.
416  */
417  int leng = 1;
418  m_h0_RT.resize(leng);
419  m_cp0_R.resize(leng);
420  m_s0_R.resize(leng);
421 
422  /*
423  * Make sure the species mole fraction is equal to 1.0;
424  */
425  double x = 1.0;
426  setMoleFractions(&x);
427  /*
428  * Call the base class initThermo object.
429  */
431 }
432 
434 {
435  doublereal tnow = temperature();
436  if (m_tlast != tnow) {
438  DATA_PTR(m_s0_R));
439  m_tlast = tnow;
440  }
441 }
442 
443 }
std::map< std::string, doublereal > compositionMap
Map connecting a string name with a double.
Definition: ct_defs.h:162
doublereal molecularWeight(size_t k) const
Molecular weight of species k.
Definition: Phase.cpp:484
virtual doublereal density() const
Density (kg/m^3).
Definition: Phase.h:534
std::string int2str(const int n, const std::string &fmt)
Convert an int to a string using a format converter.
Definition: stringUtils.cpp:40
doublereal cv_mole() const
Molar heat capacity at constant volume. Units: J/kmol/K.
const doublereal OneAtm
One atmosphere [Pa].
Definition: ct_defs.h:71
ThermoPhase * duplMyselfAsThermoPhase() const
Duplication function.
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: ThermoPhase.h:721
virtual void setState_HP(doublereal h, doublereal p, doublereal tol=1.e-8)
Set the internally stored specific enthalpy (J/kg) and pressure (Pa) of the phase.
virtual void initThermo()
Initialize the ThermoPhase object after all species have been set up.
ThermoPhase & operator=(const ThermoPhase &right)
Assignment operator.
Definition: ThermoPhase.cpp:57
virtual void getEnthalpy_RT_ref(doublereal *hrt) const
doublereal err(const std::string &msg) const
Error return for unhandled cases.
virtual void setState_UV(doublereal u, doublereal v, doublereal tol=1.e-8)
Set the specific internal energy (J/kg) and specific volume (m^3/kg).
virtual void getEntropy_R_ref(doublereal *er) const
virtual void getStandardChemPotentials(doublereal *mu) const
Get the array of chemical potentials at unit activity for the species at their standard states at the...
Definition: ThermoPhase.h:711
void setState_TPY(doublereal t, doublereal p, const doublereal *y)
Set the internally stored temperature (K), pressure (Pa), and mass fractions of the phase...
void getPartialMolarCp(doublereal *cpbar) const
Get the species partial molar Heat Capacities. Units: J/ kmol /K.
vector_fp m_s0_R
Dimensionless entropy at the (mtlast, m_p0)
virtual doublereal thermalExpansionCoeff() const
Return the volumetric thermal expansion coefficient. Units: 1/K.
Definition: ThermoPhase.h:358
doublereal intEnergy_mass() const
Specific internal energy.
Definition: ThermoPhase.h:940
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:101
void setState_PY(doublereal p, doublereal *y)
Set the internally stored pressure (Pa) and mass fractions.
void getChemPotentials_RT(doublereal *murt) const
Get the array of non-dimensional species chemical potentials These are partial molar Gibbs free energ...
void getPartialMolarEnthalpies(doublereal *hbar) const
Get the species partial molar enthalpies. Units: J/kmol.
virtual void getGibbs_RT_ref(doublereal *grt) const
virtual void getCp_R_ref(doublereal *cprt) const
void getPartialMolarEntropies(doublereal *sbar) const
Get the species partial molar entropy. Units: J/kmol K.
virtual void getIntEnergy_RT(doublereal *urt) const
Returns the vector of nondimensional Internal Energies of the standard state species at the current T...
Definition: ThermoPhase.h:762
doublereal entropy_mass() const
Specific entropy.
Definition: ThermoPhase.h:947
virtual int eosType() const
Returns the equation of state type flag.
doublereal enthalpy_mole() const
Molar enthalpy. Units: J/kmol.
void getChemPotentials(doublereal *mu) const
Get the array of chemical potentials.
std::string fp2str(const double x, const std::string &fmt)
Convert a double into a c++ string.
Definition: stringUtils.cpp:29
virtual void getCp_R(doublereal *cpr) const
Get the nondimensional Heat Capacities at constant pressure for the species standard states at the cu...
Definition: ThermoPhase.h:773
SingleSpeciesTP & operator=(const SingleSpeciesTP &right)
Assignment operator.
doublereal cp_mass() const
Specific heat at constant pressure.
Definition: ThermoPhase.h:961
void getElectrochemPotentials(doublereal *mu) const
Get the species electrochemical potentials. Units: J/kmol.
doublereal entropy_mole() const
Molar entropy. Units: J/kmol/K.
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:68
vector_fp m_h0_RT
Dimensionless enthalpy at the (mtlast, m_p0)
doublereal cv_mass() const
Specific heat at constant volume.
Definition: ThermoPhase.h:968
Header for the SingleSpeciesTP class, which is a filter class for ThermoPhase, that eases the constru...
virtual void setMoleFractions(const doublereal *const x)
Set the mole fractions to the specified values There is no restriction on the sum of the mole fractio...
Definition: Phase.cpp:306
vector_fp m_cp0_R
Dimensionless heat capacity at the (mtlast, m_p0)
virtual void getGibbs_ref(doublereal *g) const
void getStandardVolumes(doublereal *vbar) const
Get the molar volumes of each species in their standard states at the current T and P of the solution...
virtual void setState_TP(doublereal t, doublereal p)
Set the temperature (K) and pressure (Pa)
size_t nSpecies() const
Returns the number of species in the phase.
Definition: Phase.h:252
virtual void setState_SP(doublereal s, doublereal p, doublereal tol=1.e-8)
Set the specific entropy (J/kg/K) and pressure (Pa).
virtual doublereal isothermalCompressibility() const
Returns the isothermal compressibility. Units: 1/Pa.
Definition: ThermoPhase.h:346
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: ThermoPhase.h:731
doublereal gibbs_mole() const
Molar Gibbs function. Units: J/kmol.
doublereal temperature() const
Temperature (K).
Definition: Phase.h:528
doublereal intEnergy_mole() const
Molar internal energy. Units: J/kmol.
virtual void setPressure(doublereal p)
Set the internally stored pressure (Pa) at constant temperature and composition.
Definition: ThermoPhase.h:331
virtual void setState_SV(doublereal s, doublereal v, doublereal tol=1.e-8)
Set the specific entropy (J/kg/K) and specific volume (m^3/kg).
virtual void setTemperature(const doublereal temp)
Set the internally stored temperature of the phase (K).
Definition: Phase.h:562
doublereal enthalpy_mass() const
Specific enthalpy.
Definition: ThermoPhase.h:933
void setState_TPX(doublereal t, doublereal p, const doublereal *x)
Set the temperature (K), pressure (Pa), and mole fractions.
const doublereal GasConstant
Universal Gas Constant. [J/kmol/K].
Definition: ct_defs.h:66
Contains declarations for string manipulation functions within Cantera.
#define DATA_PTR(vec)
Creates a pointer to the start of the raw data for a vector.
Definition: ct_defs.h:36
virtual void getPartialMolarIntEnergies(doublereal *ubar) const
Get the species partial molar internal energies. Units: J/kmol.
doublereal m_press
The current pressure of the solution (Pa)
doublereal cp_mole() const
Molar heat capacity at constant pressure. Units: J/kmol/K.
doublereal m_tlast
Last temperature used to evaluate the thermodynamic polynomial.
void getPartialMolarVolumes(doublereal *vbar) const
Get the species partial molar volumes. Units: m^3/kmol.
void getPureGibbs(doublereal *gpure) const
Get the dimensional Gibbs functions for the standard state of the species at the current T and P...
SingleSpeciesTP()
Base empty constructor.
virtual void update(doublereal T, doublereal *cp_R, doublereal *h_RT, doublereal *s_R) const =0
Compute the reference-state properties for all species.
SpeciesThermo * m_spthermo
Pointer to the calculation manager for species reference-state thermodynamic properties.
Definition: ThermoPhase.h:1625
void setState_PX(doublereal p, doublereal *x)
Set the pressure (Pa) and mole fractions.
The SingleSpeciesTP class is a filter class for ThermoPhase.
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: ThermoPhase.h:741
virtual void setDensity(const doublereal density_)
Set the internally stored density (kg/m^3) of the phase Note the density of a phase is an independent...
Definition: Phase.h:549