Reaktoro  v2.11.0
A unified framework for modeling chemically reactive systems
ThermoPropsPhase.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive phases.
2 //
3 // Copyright © 2014-2024 Allan Leal
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library. If not, see <http://www.gnu.org/licenses/>.
17 
18 #pragma once
19 
20 // Reaktoro includes
21 #include <Reaktoro/Common/ArrayStream.hpp>
22 #include <Reaktoro/Common/AutoDiff.hpp>
23 #include <Reaktoro/Common/TypeOp.hpp>
24 #include <Reaktoro/Core/Phase.hpp>
25 
26 namespace Reaktoro {
27 
29 template<template<typename> typename TypeOp>
31 {
33  TypeOp<real> T;
34 
36  TypeOp<real> P;
37 
39  TypeOp<ArrayXr> G0;
40 
42  TypeOp<ArrayXr> H0;
43 
45  TypeOp<ArrayXr> V0;
46 
48  TypeOp<ArrayXr> VT0;
49 
51  TypeOp<ArrayXr> VP0;
52 
54  TypeOp<ArrayXr> Cp0;
55 
57  template<template<typename> typename OtherTypeOp>
59  {
60  T = other.T;
61  P = other.P;
62  G0 = other.G0;
63  H0 = other.H0;
64  V0 = other.V0;
65  VT0 = other.VT0;
66  VP0 = other.VP0;
67  Cp0 = other.Cp0;
68  return *this;
69  }
70 
72  template<template<typename> typename OtherTypeOp>
74  {
75  return { T, P, G0, H0, V0, VT0, VP0, Cp0 };
76  }
77 
79  template<template<typename> typename OtherTypeOp>
81  {
82  return { T, P, G0, H0, V0, VT0, VP0, Cp0 };
83  }
84 
86  auto operator=(const ArrayStream<real>& array)
87  {
88  array.to(T, P, G0, H0, V0, VT0, VP0, Cp0);
89  return *this;
90  }
91 
93  operator ArrayStream<real>() const
94  {
95  return {T, P, G0, H0, V0, VT0, VP0, Cp0};
96  }
97 };
98 
101 
104 
107 
110 
112 template<template<typename> typename TypeOp>
114 {
115 public:
118  : mphase(phase)
119  {
120  const auto numspecies = phase.species().size();
121 
122  mdata.G0 = ArrayXr::Zero(numspecies);
123  mdata.H0 = ArrayXr::Zero(numspecies);
124  mdata.V0 = ArrayXr::Zero(numspecies);
125  mdata.VT0 = ArrayXr::Zero(numspecies);
126  mdata.VP0 = ArrayXr::Zero(numspecies);
127  mdata.Cp0 = ArrayXr::Zero(numspecies);
128  }
129 
132  : mphase(phase), mdata(data)
133  {}
134 
136  template<template<typename> typename OtherTypeOp>
138  : mphase(other.mphase), mdata(other.mdata)
139  {}
140 
142  template<template<typename> typename OtherTypeOp>
144  : mphase(other.mphase), mdata(other.mdata)
145  {}
146 
150  auto update(const real& T, const real& P)
151  {
152  // Check if this update call can be skipped if T, P conditions remain the same
153  if(T == mdata.T && P == mdata.P)
154  return;
155 
156  mdata.T = T;
157  mdata.P = P;
158 
159  auto& G0 = mdata.G0;
160  auto& H0 = mdata.H0;
161  auto& V0 = mdata.V0;
162  auto& VT0 = mdata.VT0;
163  auto& VP0 = mdata.VP0;
164  auto& Cp0 = mdata.Cp0;
165 
166  const auto& species = mphase.species();
167  const auto size = species.size();
168 
169  assert(G0.size() == size);
170  assert(H0.size() == size);
171  assert(V0.size() == size);
172  assert(Cp0.size() == size);
173 
174  // Compute the standard thermodynamic properties of the species in the phase.
176  for(auto i = 0; i < size; ++i)
177  {
178  const auto& standard_thermo_model = mphase.species(i).standardThermoModel();
179  aux = standard_thermo_model ? standard_thermo_model(T, P) : StandardThermoProps{};
180  G0[i] = aux.G0;
181  H0[i] = aux.H0;
182  V0[i] = aux.V0;
183  VT0[i] = aux.VT0;
184  VP0[i] = aux.VP0;
185  Cp0[i] = aux.Cp0;
186  }
187  }
188 
193  auto update(const real& T, const real& P, Wrt<real&> wrtvar)
194  {
195  autodiff::seed(wrtvar);
196  update(T, P);
197  autodiff::unseed(wrtvar);
198  }
199 
201  auto phase() const -> const Phase&
202  {
203  return mphase;
204  }
205 
207  auto data() const -> const ThermoPropsPhaseBaseData<TypeOp>&
208  {
209  return mdata;
210  }
211 
213  auto temperature() const -> real
214  {
215  return mdata.T;
216  }
217 
219  auto pressure() const -> real
220  {
221  return mdata.P;
222  }
223 
226  {
227  return mdata.V0;
228  }
229 
232  {
233  return mdata.VT0;
234  }
235 
238  {
239  return mdata.VP0;
240  }
241 
244  {
245  return mdata.G0;
246  }
247 
250  {
251  return mdata.H0;
252  }
253 
256  {
257  return (mdata.H0 - mdata.G0)/mdata.T; // from G0 = H0 - T*S0
258  }
259 
262  {
263  return mdata.H0 - mdata.P * mdata.V0; // from H0 = U0 + P*V0
264  }
265 
268  {
269  return mdata.G0 - mdata.P * mdata.V0; // from A0 = U0 - T*S0 = (H0 - P*V0) + (G0 - H0) = G0 - P*V0
270  }
271 
274  {
275  return mdata.Cp0;
276  }
277 
280  {
281  return mdata.Cp0 + mdata.T * mdata.VT0 * mdata.VT0 / mdata.VP0; // from Cv0 = Cp0 + T*VT0*VT0/VP0
282  }
283 
285  auto operator=(const ArrayStream<real>& array)
286  {
287  mdata = array;
288  return *this;
289  }
290 
292  operator ArrayStream<real>() const
293  {
294  return mdata;
295  }
296 
297  // Ensure other ThermoPropsPhaseBase types are friend among themselves.
298  template<template<typename> typename OtherTypeOp>
299  friend class ThermoPropsPhaseBase;
300 
301 private:
303  Phase mphase;
304 
307 };
308 
311 
314 
317 
318 } // namespace Reaktoro
The class used to serialize/deserialize data using array.
Definition: ArrayStream.hpp:28
auto to(Args &... args) const
Transfer this ArrayStream object data to given list of scalars and/or arrays.
Definition: ArrayStream.hpp:54
A type used to define a phase and its attributes.
Definition: Phase.hpp:33
auto species() const -> const SpeciesList &
Return the species of the phase.
The base type for standard thermodynamic properties of a phase and its species.
Definition: ThermoPropsPhase.hpp:114
auto temperature() const -> real
Return the temperature of the phase (in K).
Definition: ThermoPropsPhase.hpp:213
auto speciesStandardVolumesT() const -> ArrayXrConstRef
Return the temperature derivative of the standard partial molar volumes of the species in the phase (...
Definition: ThermoPropsPhase.hpp:231
auto pressure() const -> real
Return the pressure of the phase (in Pa).
Definition: ThermoPropsPhase.hpp:219
auto update(const real &T, const real &P)
Update the standard thermodynamic properties of the phase.
Definition: ThermoPropsPhase.hpp:150
ThermoPropsPhaseBase(const Phase &phase, const ThermoPropsPhaseBaseData< TypeOp > &data)
Construct a ThermoPropsPhaseBase instance.
Definition: ThermoPropsPhase.hpp:131
auto speciesStandardInternalEnergies() const -> ArrayXr
Return the standard partial molar internal energies of formation of the species in the phase (in J/mo...
Definition: ThermoPropsPhase.hpp:261
auto update(const real &T, const real &P, Wrt< real & > wrtvar)
Update the standard thermodynamic properties of the phase.
Definition: ThermoPropsPhase.hpp:193
auto speciesStandardHeatCapacitiesConstP() const -> ArrayXrConstRef
Return the standard partial molar isobaric heat capacities of the species in the phase (in J/(mol·K))...
Definition: ThermoPropsPhase.hpp:273
auto speciesStandardEntropies() const -> ArrayXr
Return the standard partial molar entropies of formation of the species in the phase (in J/(mol·K)).
Definition: ThermoPropsPhase.hpp:255
auto speciesStandardGibbsEnergies() const -> ArrayXrConstRef
Return the standard partial molar Gibbs energies of formation of the species in the phase (in J/mol).
Definition: ThermoPropsPhase.hpp:243
auto speciesStandardEnthalpies() const -> ArrayXrConstRef
Return the standard partial molar enthalpies of formation of the species in the phase (in J/mol).
Definition: ThermoPropsPhase.hpp:249
ThermoPropsPhaseBase(const ThermoPropsPhaseBase< OtherTypeOp > &other)
Construct a ThermoPropsPhaseBase instance.
Definition: ThermoPropsPhase.hpp:143
auto phase() const -> const Phase &
Return the underlying Phase object.
Definition: ThermoPropsPhase.hpp:201
auto speciesStandardVolumes() const -> ArrayXrConstRef
Return the standard partial molar volumes of the species in the phase (in m³/mol).
Definition: ThermoPropsPhase.hpp:225
ThermoPropsPhaseBase(const Phase &phase)
Construct a ThermoPropsPhaseBase instance.
Definition: ThermoPropsPhase.hpp:117
auto data() const -> const ThermoPropsPhaseBaseData< TypeOp > &
Return the primary standard thermodynamic property data of the phase from which others are calculated...
Definition: ThermoPropsPhase.hpp:207
auto speciesStandardVolumesP() const -> ArrayXrConstRef
Return the pressure derivative of the standard partial molar volumes of the species in the phase (in ...
Definition: ThermoPropsPhase.hpp:237
auto operator=(const ArrayStream< real > &array)
Assign the given array data to this ThermoPropsPhaseBase object.
Definition: ThermoPropsPhase.hpp:285
ThermoPropsPhaseBase(ThermoPropsPhaseBase< OtherTypeOp > &other)
Construct a ThermoPropsPhaseBase instance.
Definition: ThermoPropsPhase.hpp:137
auto speciesStandardHelmholtzEnergies() const -> ArrayXr
Return the standard partial molar Helmholtz energies of formation of the species in the phase (in J/m...
Definition: ThermoPropsPhase.hpp:267
auto speciesStandardHeatCapacitiesConstV() const -> ArrayXrConstRef
Return the standard partial molar isochoric heat capacities of the species in the phase (in J/(mol·K)...
Definition: ThermoPropsPhase.hpp:279
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
autodiff::real real
The number type used throughout the library.
Definition: Real.hpp:26
Fn< void(ThermoPropsPhaseDataRef, const real &, const real &, ArrayXrConstRef)> ThermoPropsPhaseFn
The type of functions that computes the primary standard thermodynamic property data of a phase.
Definition: ThermoPropsPhase.hpp:109
std::function< F > Fn
Convenient alias for std::function<R(Args...)>.
Definition: Types.hpp:110
Eigen::Ref< const ArrayXr > ArrayXrConstRef
Convenient alias to Eigen type.
Definition: Matrix.hpp:89
autodiff::ArrayXreal ArrayXr
Convenient alias to Eigen type.
Definition: Matrix.hpp:87
The primary standard thermodynamic properties of a chemical species.
Definition: StandardThermoProps.hpp:27
real VP0
The pressure derivative of the standard molar volume of the species (in m³/(mol·Pa)).
Definition: StandardThermoProps.hpp:44
real VT0
The temperature derivative of the standard molar volume of the species (in m³/(mol·K)).
Definition: StandardThermoProps.hpp:41
real H0
The standard molar enthalpy of formation of the species (in J/mol).
Definition: StandardThermoProps.hpp:32
real G0
The standard molar Gibbs energy of formation of the species (in J/mol).
Definition: StandardThermoProps.hpp:29
real V0
The standard molar volume of the species (in m³/mol).
Definition: StandardThermoProps.hpp:35
real Cp0
The standard molar isobaric heat capacity of the species (in J/(mol·K)).
Definition: StandardThermoProps.hpp:38
The base type for primary standard thermodynamic property data of a phase from which others are compu...
Definition: ThermoPropsPhase.hpp:31
TypeOp< real > P
The pressure of the phase (in Pa).
Definition: ThermoPropsPhase.hpp:36
auto operator=(const ThermoPropsPhaseBaseData< OtherTypeOp > &other)
Assign a ThermoPropsPhaseBaseData object to this.
Definition: ThermoPropsPhase.hpp:58
TypeOp< ArrayXr > G0
The standard molar Gibbs energies of formation of the species in the phase (in J/mol)
Definition: ThermoPropsPhase.hpp:39
TypeOp< ArrayXr > V0
The standard molar volumes of the species in the phase (in m³/mol)
Definition: ThermoPropsPhase.hpp:45
TypeOp< ArrayXr > Cp0
The standard molar isobaric heat capacities of the species in the phase (in J/(mol·K))
Definition: ThermoPropsPhase.hpp:54
TypeOp< ArrayXr > VP0
The pressure derivative of the standard molar volumes of the species in the phase (in m³/(mol·Pa)).
Definition: ThermoPropsPhase.hpp:51
TypeOp< ArrayXr > H0
The standard molar enthalpies of formation of the species in the phase (in J/mol)
Definition: ThermoPropsPhase.hpp:42
TypeOp< ArrayXr > VT0
The temperature derivative of the standard molar volumes of the species in the phase (in m³/(mol·K)).
Definition: ThermoPropsPhase.hpp:48
auto operator=(const ArrayStream< real > &array)
Assign the given array data to this ThermoPropsPhaseBaseData object.
Definition: ThermoPropsPhase.hpp:86
TypeOp< real > T
The temperature of the phase (in K).
Definition: ThermoPropsPhase.hpp:33