Cantera  4.0.0a1
Loading...
Searching...
No Matches
BinarySolutionTabulatedThermo.cpp
Go to the documentation of this file.
1/**
2 * @file BinarySolutionTabulatedThermo.cpp Implementation file for an binary
3 * solution model with tabulated standard state thermodynamic data (see
4 * @ref thermoprops and class
5 * @link Cantera::BinarySolutionTabulatedThermo BinarySolutionTabulatedThermo@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
12#include "cantera/thermo/PDSS.h"
18
19namespace Cantera
20{
21
23 const string& id_)
24{
25 initThermoFile(inputFile, id_);
26}
27
29{
32}
33
35{
36 static const int cacheId = m_cache.getId();
37 CachedScalar cached = m_cache.getScalar(cacheId);
38 bool x_changed = !cached.validate(stateMFNumber());
39
40 if (x_changed) {
41 double x_tab = moleFraction(m_kk_tab);
42 double x_other = moleFraction(1 - m_kk_tab);
43 m_h0_tab = interpolate(x_tab, m_enthalpy_tab);
44 m_s0_tab = interpolate(x_tab, m_entropy_tab);
45 m_s0_tab += GasConstant*std::log(std::max(SmallNumber, x_tab)
46 / std::max(SmallNumber, x_other))
49 }
50
51 double tnow = temperature();
52 if (x_changed || m_tlast != tnow) {
53 // Update the thermodynamic functions of the reference state.
55 double rrt = 1.0 / RT();
56 m_h0_RT[m_kk_tab] += m_h0_tab * rrt;
58 for (size_t k = 0; k < m_kk; k++) {
59 m_g0_RT[k] = m_h0_RT[k] - m_s0_R[k];
60 }
61 m_tlast = tnow;
62 }
63}
64
65bool BinarySolutionTabulatedThermo::addSpecies(shared_ptr<Species> spec)
66{
67 if (m_kk == 2) {
68 throw CanteraError("BinarySolutionTabulatedThermo::addSpecies",
69 "No. of species should be equal to 2");
70 }
71 bool added = IdealSolidSolnPhase::addSpecies(spec);
72 return added;
73}
74
76{
77 if (m_input.hasKey("tabulated-thermo")) {
78 m_kk_tab = speciesIndex(m_input["tabulated-species"].asString(), true);
79 if (nSpecies() != 2) {
80 throw InputFileError("BinarySolutionTabulatedThermo::initThermo",
81 m_input["species"],
82 "No. of species should be equal to 2 in phase '{}'!",name());
83 }
84 if (m_kk_tab == npos) {
85 throw InputFileError("BinarySolutionTabulatedThermo::initThermo",
86 m_input["tabulated-species"],
87 "Species '{}' is not in phase '{}'",
88 m_input["tabulated-species"].asString(), name());
89 }
90 const AnyMap& table = m_input["tabulated-thermo"].as<AnyMap>();
91 vector<double> x = table["mole-fractions"].asVector<double>();
92 size_t N = x.size();
93 vector<double> h = table.convertVector("enthalpy", "J/kmol", N);
94 vector<double> s = table.convertVector("entropy", "J/kmol/K", N);
95 vector<double> vmol(N);
96 // Check for molar-volume key in tabulatedThermo table,
97 // otherwise calculate molar volume from pure species molar volumes
98 if (table.hasKey("molar-volume")) {
99 vmol = table.convertVector("molar-volume", "m^3/kmol", N);
100 } else {
101 for(size_t i = 0; i < N; i++) {
102 vmol[i] = x[i] * m_speciesMolarVolume[m_kk_tab] + (1-x[i])
104 }
105 }
106
107 // Sort the x, h, s, vmol data in the order of increasing x
108 vector<pair<double,double>> x_h(N), x_s(N), x_vmol(N);
109 for(size_t i = 0; i < N; i++) {
110 x_h[i] = {x[i], h[i]};
111 x_s[i] = {x[i], s[i]};
112 x_vmol[i] = {x[i], vmol[i]};
113 }
114 std::sort(x_h.begin(), x_h.end());
115 std::sort(x_s.begin(), x_s.end());
116 std::sort(x_vmol.begin(), x_vmol.end());
117
118 // Store the sorted values in different arrays
119 m_molefrac_tab.resize(N);
120 m_enthalpy_tab.resize(N);
121 m_entropy_tab.resize(N);
122 m_molar_volume_tab.resize(N);
123 m_derived_molar_volume_tab.resize(N);
124
125 for (size_t i = 0; i < N; i++) {
126 m_molefrac_tab[i] = x_h[i].first;
127 m_enthalpy_tab[i] = x_h[i].second;
128 m_entropy_tab[i] = x_s[i].second;
129 m_molar_volume_tab[i] = x_vmol[i].second;
130 }
131
132 diff(m_molar_volume_tab, m_derived_molar_volume_tab);
133 }
135}
136
138{
139 return !m_molefrac_tab.empty();
140}
141
143{
145 phaseNode["tabulated-species"] = speciesName(m_kk_tab);
146 AnyMap tabThermo;
147 tabThermo["mole-fractions"] = m_molefrac_tab;
148 tabThermo["enthalpy"].setQuantity(m_enthalpy_tab, "J/kmol");
149 tabThermo["entropy"].setQuantity(m_entropy_tab, "J/kmol/K");
150 tabThermo["molar-volume"].setQuantity(m_molar_volume_tab, "m^3/kmol");
151 phaseNode["tabulated-thermo"] = std::move(tabThermo);
152}
153
155 span<const double> inputData) const
156{
157 double c;
158 // Check if x is out of bound
159 if (x > m_molefrac_tab.back()) {
160 c = inputData.back();
161 return c;
162 }
163 if (x < m_molefrac_tab.front()) {
164 c = inputData.front();
165 return c;
166 }
167 size_t i = std::distance(m_molefrac_tab.begin(),
168 std::lower_bound(m_molefrac_tab.begin(), m_molefrac_tab.end(), x));
169 c = inputData[i-1] + (inputData[i] - inputData[i-1])
170 * (x - m_molefrac_tab[i-1]) / (m_molefrac_tab[i] - m_molefrac_tab[i-1]);
171 return c;
172}
173
174void BinarySolutionTabulatedThermo::diff(span<const double> inputData,
175 span<double> derivedData) const
176{
177 if (inputData.size() > 1) {
178 derivedData[0] = (inputData[1] - inputData[0]) /
180 derivedData.back() = (inputData.back() - inputData[inputData.size()-2]) /
181 (m_molefrac_tab.back() - m_molefrac_tab[m_molefrac_tab.size()-2]);
182
183 if (inputData.size() > 2) {
184 for (size_t i = 1; i < inputData.size()-1; i++) {
185 derivedData[i] = (inputData[i+1] - inputData[i-1]) /
186 (m_molefrac_tab[i+1] - m_molefrac_tab[i-1]);
187 }
188 }
189 } else {
190 derivedData.front() = 0;
191 }
192}
193
195{
196 checkArraySize("BinarySolutionTabulatedThermo::getPartialMolarVolumes",
197 vbar.size(), m_kk);
198 std::copy(m_speciesMolarVolume.begin(), m_speciesMolarVolume.end(), vbar.begin());
199}
200
202{
203 double Xtab = moleFraction(m_kk_tab);
204 double Vm = interpolate(Xtab, m_molar_volume_tab);
205 double dVdX_tab = interpolate(Xtab, m_derived_molar_volume_tab);
206 m_speciesMolarVolume[m_kk_tab] = Vm + (1 - Xtab) * dVdX_tab;
207 m_speciesMolarVolume[1-m_kk_tab] = Vm - Xtab * dVdX_tab;
208
209 double dens = meanMolecularWeight() / Vm;
210
211 // Set the density in the parent State object directly, by calling the
212 // Phase::assignDensity() function.
214}
215}
Header file for an binary solution model with tabulated standard state thermodynamic data (see Thermo...
Header for a general species thermodynamic property manager for a phase (see MultiSpeciesThermo).
Declarations for the virtual base class PDSS (pressure dependent standard state) which handles calcul...
Header for factory functions to build instances of classes that manage the standard-state thermodynam...
Declaration for class Cantera::Species.
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:431
size_t size() const
Returns the number of elements in this map.
Definition AnyMap.cpp:1728
bool hasKey(const string &key) const
Returns true if the map contains an item named key.
Definition AnyMap.cpp:1477
vector< double > convertVector(const string &key, const string &units, size_t nMin=npos, size_t nMax=npos) const
Convert a vector of dimensional values.
Definition AnyMap.cpp:1615
vector< double > m_molefrac_tab
Vector for storing tabulated thermo.
double m_s0_tab
Tabulated contribution to s0[m_kk_tab] at the current composition.
void getParameters(AnyMap &phaseNode) const override
Store the parameters of a ThermoPhase object such that an identical one could be reconstructed using ...
void initThermo() override
Initialize the ThermoPhase object after all species have been set up.
double interpolate(const double x, span< const double > inputData) const
Species thermodynamics linear interpolation function.
size_t m_kk_tab
Current tabulated species index.
void calcDensity() override
Overloads the calcDensity() method of IdealSolidSoln to also consider non-ideal behavior.
BinarySolutionTabulatedThermo(const string &infile="", const string &id="")
Construct and initialize an BinarySolutionTabulatedThermo ThermoPhase object directly from an input f...
void getPartialMolarVolumes(span< double > vbar) const override
returns an array of partial molar volumes of the species in the solution.
double m_h0_tab
Tabulated contribution to h0[m_kk_tab] at the current composition.
void compositionChanged() override
If the compositions have changed, update the tabulated thermo lookup.
bool ready() const override
Returns a bool indicating whether the object is ready for use.
bool addSpecies(shared_ptr< Species > spec) override
Add a Species to this Phase.
void _updateThermo() const override
This function gets called for every call to functions in this class.
void diff(span< const double > inputData, span< double > derivedData) const
Numerical derivative of the molar volume table.
Base class for exceptions thrown by Cantera classes.
vector< double > m_g0_RT
Vector containing the species reference Gibbs functions at T = m_tlast.
vector< double > m_h0_RT
Vector containing the species reference enthalpies at T = m_tlast.
void getParameters(AnyMap &phaseNode) const override
Store the parameters of a ThermoPhase object such that an identical one could be reconstructed using ...
void initThermo() override
Initialize the ThermoPhase object after all species have been set up.
double standardConcentration(size_t k) const override
The standard concentration used to normalize the generalized concentration.
vector< double > m_s0_R
Vector containing the species reference entropies at T = m_tlast.
vector< double > m_speciesMolarVolume
Vector of molar volumes for each species in the solution.
void compositionChanged() override
Apply changes to the state which are needed after the composition changes.
vector< double > m_cp0_R
Vector containing the species reference constant pressure heat capacities at T = m_tlast.
bool addSpecies(shared_ptr< Species > spec) override
Add a Species to this Phase.
Error thrown for problems processing information contained in an AnyMap or AnyValue.
Definition AnyMap.h:749
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.
void assignDensity(const double density_)
Set the internally stored constant density (kg/m^3) of the phase.
Definition Phase.cpp:618
ValueCache m_cache
Cached for saved calculations within each ThermoPhase.
Definition Phase.h:862
size_t nSpecies() const
Returns the number of species in the phase.
Definition Phase.h:247
size_t m_kk
Number of species in the phase.
Definition Phase.h:882
size_t speciesIndex(const string &name, bool raise=true) const
Returns the index of a species named 'name' within the Phase object.
Definition Phase.cpp:127
double temperature() const
Temperature (K).
Definition Phase.h:586
double meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition Phase.h:677
string speciesName(size_t k) const
Name of the species with index k.
Definition Phase.cpp:143
double moleFraction(size_t k) const
Return the mole fraction of a single species.
Definition Phase.cpp:457
int stateMFNumber() const
Return the State Mole Fraction Number.
Definition Phase.h:801
string name() const
Return the name of the phase.
Definition Phase.cpp:20
double RT() const
Return the Gas Constant multiplied by the current temperature.
double m_tlast
last value of the temperature processed by reference state
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.
AnyMap m_input
Data supplied via setParameters.
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.
const double Faraday
Faraday constant [C/kmol].
Definition ct_defs.h:134
const double GasConstant
Universal Gas Constant [J/kmol/K].
Definition ct_defs.h:123
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
const size_t npos
index returned by functions to indicate "no position"
Definition ct_defs.h:183
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.
Contains declarations for string manipulation functions within Cantera.
A cached property value and the state at which it was evaluated.
Definition ValueCache.h:33
bool validate(double state1New)
Check whether the currently cached value is valid based on a single state variable.
Definition ValueCache.h:39