Cantera  2.5.1
MultiSpeciesThermo.cpp
Go to the documentation of this file.
1 /**
2  * @file MultiSpeciesThermo.cpp
3  * Declarations for a thermodynamic property manager for multiple species
4  * in a phase (see \ref spthermo and
5  * \link Cantera::MultiSpeciesThermo MultiSpeciesThermo\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 #include "cantera/base/utilities.h"
16 
17 namespace Cantera
18 {
20  m_tlow_max(0.0),
21  m_thigh_min(1.0E30),
22  m_p0(OneAtm)
23 {
24 }
25 
27  shared_ptr<SpeciesThermoInterpType> stit_ptr)
28 {
29  if (!stit_ptr) {
30  throw CanteraError("MultiSpeciesThermo::install_STIT",
31  "null pointer");
32  }
33  AssertThrowMsg(m_speciesLoc.find(index) == m_speciesLoc.end(),
34  "MultiSpeciesThermo::install_STIT",
35  "Index position isn't null, duplication of assignment: {}", index);
36  int type = stit_ptr->reportType();
37  m_speciesLoc[index] = {type, m_sp[type].size()};
38  m_sp[type].emplace_back(index, stit_ptr);
39  if (m_sp[type].size() == 1) {
40  m_tpoly[type].resize(stit_ptr->temperaturePolySize());
41  }
42 
43  // Calculate max and min T
44  m_tlow_max = std::max(stit_ptr->minTemp(), m_tlow_max);
45  m_thigh_min = std::min(stit_ptr->maxTemp(), m_thigh_min);
46  markInstalled(index);
47 }
48 
50  shared_ptr<SpeciesThermoInterpType> spthermo)
51 {
52  if (!spthermo) {
53  throw CanteraError("MultiSpeciesThermo::modifySpecies",
54  "null pointer");
55  }
56  if (m_speciesLoc.find(index) == m_speciesLoc.end()) {
57  throw CanteraError("MultiSpeciesThermo::modifySpecies",
58  "Species with this index not previously added: {}",
59  index);
60  }
61  int type = spthermo->reportType();
62  if (m_speciesLoc[index].first != type) {
63  throw CanteraError("MultiSpeciesThermo::modifySpecies",
64  "Type of parameterization changed: {} != {}", type,
65  m_speciesLoc[index].first);
66  }
67  if (spthermo->minTemp() > m_tlow_max) {
68  throw CanteraError("MultiSpeciesThermo::modifySpecies",
69  "Cannot increase minimum temperature for phase from {} to {}",
70  m_tlow_max, spthermo->minTemp());
71  }
72  if (spthermo->maxTemp() < m_thigh_min) {
73  throw CanteraError("MultiSpeciesThermo::modifySpecies",
74  "Cannot increase minimum temperature for phase from {} to {}",
75  m_thigh_min, spthermo->maxTemp());
76  }
77 
78  m_sp[type][m_speciesLoc[index].second] = {index, spthermo};
79 }
80 
81 void MultiSpeciesThermo::update_single(size_t k, double t, double* cp_R,
82  double* h_RT, double* s_R) const
83 {
84  const SpeciesThermoInterpType* sp_ptr = provideSTIT(k);
85  if (sp_ptr) {
86  sp_ptr->updatePropertiesTemp(t, cp_R, h_RT, s_R);
87  }
88 }
89 
90 void MultiSpeciesThermo::update(doublereal t, doublereal* cp_R,
91  doublereal* h_RT, doublereal* s_R) const
92 {
93  auto iter = m_sp.begin();
94  auto jter = m_tpoly.begin();
95  for (; iter != m_sp.end(); iter++, jter++) {
96  const std::vector<index_STIT>& species = iter->second;
97  double* tpoly = &jter->second[0];
98  species[0].second->updateTemperaturePoly(t, tpoly);
99  for (size_t k = 0; k < species.size(); k++) {
100  size_t i = species[k].first;
101  species[k].second->updateProperties(tpoly, cp_R+i, h_RT+i, s_R+i);
102  }
103  }
104 }
105 
106 int MultiSpeciesThermo::reportType(size_t index) const
107 {
108  const SpeciesThermoInterpType* sp = provideSTIT(index);
109  if (sp) {
110  return sp->reportType();
111  }
112  return -1;
113 }
114 
115 void MultiSpeciesThermo::reportParams(size_t index, int& type,
116  doublereal* const c, doublereal& minTemp_, doublereal& maxTemp_,
117  doublereal& refPressure_) const
118 {
119  const SpeciesThermoInterpType* sp = provideSTIT(index);
120  size_t n;
121  if (sp) {
122  sp->reportParameters(n, type, minTemp_, maxTemp_,
123  refPressure_, c);
124  } else {
125  type = -1;
126  }
127 }
128 
129 doublereal MultiSpeciesThermo::minTemp(size_t k) const
130 {
131  if (k != npos) {
132  const SpeciesThermoInterpType* sp = provideSTIT(k);
133  if (sp) {
134  return sp->minTemp();
135  }
136  }
137  return m_tlow_max;
138 }
139 
140 doublereal MultiSpeciesThermo::maxTemp(size_t k) const
141 {
142  if (k != npos) {
143  const SpeciesThermoInterpType* sp = provideSTIT(k);
144  if (sp) {
145  return sp->maxTemp();
146  }
147  }
148  return m_thigh_min;
149 }
150 
151 doublereal MultiSpeciesThermo::refPressure(size_t k) const
152 {
153  if (k != npos) {
154  const SpeciesThermoInterpType* sp = provideSTIT(k);
155  if (sp) {
156  return sp->refPressure();
157  }
158  }
159  return m_p0;
160 }
161 
163 {
164  try {
165  const std::pair<int, size_t>& loc = m_speciesLoc.at(k);
166  return m_sp.at(loc.first)[loc.second].second.get();
167  } catch (std::out_of_range&) {
168  return 0;
169  }
170 }
171 
173 {
174  try {
175  const std::pair<int, size_t>& loc = m_speciesLoc.at(k);
176  return m_sp.at(loc.first)[loc.second].second.get();
177  } catch (std::out_of_range&) {
178  return 0;
179  }
180 }
181 
182 doublereal MultiSpeciesThermo::reportOneHf298(const size_t k) const
183 {
184  const SpeciesThermoInterpType* sp_ptr = provideSTIT(k);
185  doublereal h = -1.0;
186  if (sp_ptr) {
187  h = sp_ptr->reportHf298(0);
188  }
189  return h;
190 }
191 
192 void MultiSpeciesThermo::modifyOneHf298(const size_t k, const doublereal Hf298New)
193 {
195  if (sp_ptr) {
196  sp_ptr->modifyOneHf298(k, Hf298New);
197  }
198 }
199 
200 void MultiSpeciesThermo::resetHf298(const size_t k)
201 {
203  if (sp_ptr) {
204  sp_ptr->resetHf298();
205  }
206 }
207 
208 bool MultiSpeciesThermo::ready(size_t nSpecies) {
209  if (m_installed.size() < nSpecies) {
210  return false;
211  }
212  for (size_t k = 0; k < nSpecies; k++) {
213  if (!m_installed[k]) {
214  return false;
215  }
216  }
217  return true;
218 }
219 
221  if (k >= m_installed.size()) {
222  m_installed.resize(k+1, false);
223  }
224  m_installed[k] = true;
225 }
226 
227 }
Header for a general species thermodynamic property manager for a phase (see MultiSpeciesThermo).
Header for factory functions to build instances of classes that manage the standard-state thermodynam...
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
bool ready(size_t nSpecies)
Check if data for all species (0 through nSpecies-1) has been installed.
virtual doublereal minTemp(size_t k=npos) const
Minimum temperature.
STIT_map m_sp
This is the main data structure, which contains the SpeciesThermoInterpType objects,...
SpeciesThermoInterpType * provideSTIT(size_t k)
Provide the SpeciesThermoInterpType object.
virtual void install_STIT(size_t index, shared_ptr< SpeciesThermoInterpType > stit)
Install a new species thermodynamic property parameterization for one species.
virtual doublereal refPressure(size_t k=npos) const
The reference-state pressure for species k.
void markInstalled(size_t k)
Mark species k as having its thermodynamic data installed.
std::vector< bool > m_installed
indicates if data for species has been installed
virtual void modifyOneHf298(const size_t k, const doublereal Hf298New)
Modify the value of the 298 K Heat of Formation of the standard state of one species in the phase (J ...
virtual void modifySpecies(size_t index, shared_ptr< SpeciesThermoInterpType > spec)
Modify the species thermodynamic property parameterization for a species.
virtual int reportType(size_t index) const
This utility function reports the type of parameterization used for the species with index number ind...
virtual doublereal reportOneHf298(const size_t k) const
Report the 298 K Heat of Formation of the standard state of one species (J kmol-1)
doublereal m_thigh_min
Minimum value of the highest temperature.
virtual void update(doublereal T, doublereal *cp_R, doublereal *h_RT, doublereal *s_R) const
Compute the reference-state properties for all species.
virtual void reportParams(size_t index, int &type, doublereal *const c, doublereal &minTemp, doublereal &maxTemp, doublereal &refPressure) const
This utility function reports back the type of parameterization and all of the parameters for the spe...
tpoly_map m_tpoly
Temperature polynomials for each thermo parameterization.
std::map< size_t, std::pair< int, size_t > > m_speciesLoc
Map from species index to location within m_sp, such that m_sp[m_speciesLoc[k].first][m_speciesLoc[k]...
doublereal m_p0
reference pressure (Pa)
virtual void update_single(size_t k, double T, double *cp_R, double *h_RT, double *s_R) const
Like update_one, but without applying offsets to the output pointers.
virtual doublereal maxTemp(size_t k=npos) const
Maximum temperature.
doublereal m_tlow_max
Maximum value of the lowest temperature.
virtual void resetHf298(const size_t k)
Restore the original heat of formation of one or more species.
Abstract Base class for the thermodynamic manager for an individual species' reference state.
virtual void reportParameters(size_t &index, int &type, doublereal &minTemp, doublereal &maxTemp, doublereal &refPressure, doublereal *const coeffs) const
This utility function returns the type of parameterization and all of the parameters for the species.
virtual doublereal maxTemp() const
Returns the maximum temperature that the thermo parameterization is valid.
virtual doublereal refPressure() const
Returns the reference pressure (Pa)
virtual void modifyOneHf298(const size_t k, const doublereal Hf298New)
Modify the value of the 298 K Heat of Formation of one species in the phase (J kmol-1)
virtual doublereal reportHf298(doublereal *const h298=0) const
Report the 298 K Heat of Formation of the standard state of one species (J kmol-1)
virtual void resetHf298()
Restore the original heat of formation for this species.
virtual void updatePropertiesTemp(const doublereal temp, doublereal *cp_R, doublereal *h_RT, doublereal *s_R) const
Compute the reference-state property of one species.
virtual doublereal minTemp() const
Returns the minimum temperature that the thermo parameterization is valid.
virtual int reportType() const
Returns an integer representing the type of parameterization.
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
#define AssertThrowMsg(expr, procedure,...)
Assertion must be true or an error is thrown.
Definition: ctexceptions.h:265
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:188
const double OneAtm
One atmosphere [Pa].
Definition: ct_defs.h:78
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:264
Contains declarations for string manipulation functions within Cantera.
Various templated functions that carry out common vector operations (see Templated Utility Functions)...