Reaktoro  v2.11.0
A unified framework for modeling chemically reactive systems
EquilibriumConditions.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
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/Constants.hpp>
22 #include <Reaktoro/Equilibrium/EquilibriumSpecs.hpp>
23 
24 namespace Reaktoro {
25 
26 // Forward declarations
27 class ChemicalState;
28 class ChemicalSystem;
29 
32 {
33 public:
36 
38  explicit EquilibriumConditions(EquilibriumSpecs const& specs);
39 
40  //=================================================================================================
41  //
42  // METHODS TO SPECIFY THERMODYNAMIC CONDITIONS
43  //
44  //=================================================================================================
45 
49  auto temperature(real const& value, String const& unit="K") -> void;
50 
54  auto pressure(real const& value, String const& unit="Pa") -> void;
55 
59  auto volume(real const& value, String const& unit="m3") -> void;
60 
64  auto internalEnergy(real const& value, String const& unit="J") -> void;
65 
69  auto enthalpy(real const& value, String const& unit="J") -> void;
70 
74  auto gibbsEnergy(real const& value, String const& unit="J") -> void;
75 
79  auto helmholtzEnergy(real const& value, String const& unit="J") -> void;
80 
84  auto entropy(real const& value, String const& unit="J/K") -> void;
85 
89  auto charge(real const& value, String const& unit="mol") -> void;
90 
95  auto elementAmount(StringOrIndex const& element, real const& value, String const& unit="mol") -> void;
96 
102  auto elementAmountInPhase(StringOrIndex const& element, StringOrIndex const& phase, real const& value, String const& unit="mol") -> void;
103 
108  auto elementMass(StringOrIndex const& element, real const& value, String const& unit="kg") -> void;
109 
115  auto elementMassInPhase(StringOrIndex const& element, StringOrIndex const& phase, real const& value, String const& unit="kg") -> void;
116 
121  auto phaseAmount(StringOrIndex const& phase, real const& value, String const& unit="mol") -> void;
122 
127  auto phaseMass(StringOrIndex const& phase, real const& value, String const& unit="kg") -> void;
128 
133  auto phaseVolume(StringOrIndex const& phase, real const& value, String const& unit="m3") -> void;
134 
135  //=================================================================================================
136  //
137  // METHODS TO SPECIFY CHEMICAL POTENTIAL CONDITIONS
138  //
139  //=================================================================================================
140 
147  auto chemicalPotential(String const& substance, real const& value, String const& unit="J/mol") -> void;
148 
154  auto lnActivity(String const& species, real const& value) -> void;
155 
161  auto lgActivity(String const& species, real const& value) -> void;
162 
168  auto activity(String const& species, real const& value) -> void;
169 
176  auto fugacity(String const& species, real const& value, String const& unit="bar") -> void;
177 
182  auto pH(real const& value) -> void;
183 
188  auto pMg(real const& value) -> void;
189 
194  auto pE(real const& value) -> void;
195 
201  auto Eh(real const& value, String const& unit="V") -> void;
202 
203  //=================================================================================================
204  //
205  // METHODS FOR SETTING AND GETTING LOWER AND UPPER BOUNDS FOR UNKNOWN VARIABLES
206  //
207  //=================================================================================================
208 
210  auto setLowerBoundTemperature(double value, String const& unit="K") -> void;
211 
213  auto setUpperBoundTemperature(double value, String const& unit="K") -> void;
214 
216  auto setLowerBoundPressure(double value, String const& unit="Pa") -> void;
217 
219  auto setUpperBoundPressure(double value, String const& unit="Pa") -> void;
220 
222  auto setLowerBoundTitrant(String const& substance, double value, String const& unit="mol") -> void;
223 
225  auto setUpperBoundTitrant(String const& substance, double value, String const& unit="mol") -> void;
226 
229 
232 
235 
238 
239  //=================================================================================================
240  //
241  // METHODS FOR SETTING AND GETTING INPUT VARIABLES
242  //
243  //=================================================================================================
244 
249  auto set(String const& input, real const& val) -> void;
250 
255  auto setInputVariable(String const& name, real const& val) -> void;
256 
261  auto setInputVariable(Index index, real const& val) -> void;
262 
264  auto setInputVariables(ArrayXrConstRef const& values) -> void;
265 
267  auto inputNames() const -> Strings const&;
268 
270  auto inputValues() const -> ArrayXrConstRef;
271 
273  auto inputValuesGetOrCompute(ChemicalState const& state0) const -> ArrayXr;
274 
277  auto inputValue(String const& name) const -> real const&;
278 
279  //=================================================================================================
280  //
281  // METHODS TO SPECIFY THE INITIAL COMPOSITIONAL STATE OF THE CHEMICAL SYSTEM BEFORE IT REACTS
282  //
283  //=================================================================================================
284 
288 
295 
303 
309 
317 
321 
322  //=================================================================================================
323  //
324  // MISCELLANEOUS METHODS
325  //
326  //=================================================================================================
327 
329  auto system() const -> ChemicalSystem const&;
330 
331 private:
332  const ChemicalSystem msystem;
333  const MatrixXd C;
334  const Strings wvars;
335  const Strings pvars;
336  const Index itemperature_w;
337  const Index itemperature_p;
338  const Index ipressure_w;
339  const Index ipressure_p;
340  ArrayXr w;
341  ArrayXd c0;
342  ArrayXd plower;
343  ArrayXd pupper;
344 };
345 
346 } // namespace Reaktoro
The chemical state of a chemical system.
Definition: ChemicalState.hpp:41
The class used to represent a chemical system and its attributes and properties.
Definition: ChemicalSystem.hpp:70
The class used to define conditions to be satisfied at chemical equilibrium.
Definition: EquilibriumConditions.hpp:32
auto upperBoundsControlVariablesP() const -> ArrayXdConstRef
Get the specified upper bounds for the p control variables.
auto setLowerBoundTemperature(double value, String const &unit="K") -> void
Set the lower bound for temperature during the equilibrium calculation.
auto setLowerBoundsControlVariablesP(ArrayXdConstRef const &values) -> void
Set the values of the specified lower bounds for the p control variables.
auto setUpperBoundTemperature(double value, String const &unit="K") -> void
Set the upper bound for temperature during the equilibrium calculation.
auto charge(real const &value, String const &unit="mol") -> void
Specify the electric charge at chemical equilibrium.
auto activity(String const &species, real const &value) -> void
Specify the activity of a species at chemical equilibrium.
auto lnActivity(String const &species, real const &value) -> void
Specify the ln activity of a species at chemical equilibrium.
auto lgActivity(String const &species, real const &value) -> void
Specify the lg activity of a species at chemical equilibrium.
auto setUpperBoundTitrant(String const &substance, double value, String const &unit="mol") -> void
Set the upper bound for the amount of a titrant during the equilibrium calculation.
auto inputValue(String const &name) const -> real const &
Get the value of an input variable with given name.
auto setUpperBoundsControlVariablesP(ArrayXdConstRef const &values) -> void
Set the values of the specified upper bounds for the p control variables.
auto setInputVariables(ArrayXrConstRef const &values) -> void
Set the input variables with given vector of input values.
auto pMg(real const &value) -> void
Specify the pMg at chemical equilibrium.
auto initialComponentAmountsGetOrCompute(VectorXdConstRef const &n0) const -> ArrayXd
Get the initial amounts of the conservative components before the chemical system reacts if availabl...
auto enthalpy(real const &value, String const &unit="J") -> void
Specify the enthalpy of the system at chemical equilibrium.
auto setLowerBoundPressure(double value, String const &unit="Pa") -> void
Set the lower bound for pressure during the equilibrium calculation.
auto phaseVolume(StringOrIndex const &phase, real const &value, String const &unit="m3") -> void
Specify the volume of a phase at chemical equilibrium.
auto setInitialComponentAmountsFromSpeciesAmounts(VectorXdConstRef const &n0) -> void
Set the initial amounts of the conservative components before the chemical system reacts.
auto chemicalPotential(String const &substance, real const &value, String const &unit="J/mol") -> void
Specify the chemical potential of a substance at chemical equilibrium.
auto Eh(real const &value, String const &unit="V") -> void
Specify the Eh at chemical equilibrium.
auto inputValuesGetOrCompute(ChemicalState const &state0) const -> ArrayXr
Get the values of the input variables associated with the equilibrium conditions if specified,...
auto pH(real const &value) -> void
Specify the pH at chemical equilibrium.
auto volume(real const &value, String const &unit="m3") -> void
Specify the volume of the system at chemical equilibrium.
auto elementAmountInPhase(StringOrIndex const &element, StringOrIndex const &phase, real const &value, String const &unit="mol") -> void
Specify the amount of an element in a phase at chemical equilibrium.
auto initialComponentAmounts() const -> ArrayXdConstRef
Get the initial amounts of the conservative components before the chemical system reacts.
EquilibriumConditions(ChemicalSystem const &system)
Construct an EquilibriumSolver object with given chemical system.
auto elementAmount(StringOrIndex const &element, real const &value, String const &unit="mol") -> void
Specify the amount of an element at chemical equilibrium.
auto helmholtzEnergy(real const &value, String const &unit="J") -> void
Specify the Helmholtz energy of the system at chemical equilibrium.
auto setInputVariable(String const &name, real const &val) -> void
Set the value of an input variable with given name.
auto lowerBoundsControlVariablesP() const -> ArrayXdConstRef
Get the specified lower bounds for the p control variables.
auto temperature(real const &value, String const &unit="K") -> void
Specify the temperature of the system at chemical equilibrium.
auto system() const -> ChemicalSystem const &
Get the chemical system associated with the equilibrium conditions.
auto setInitialComponentAmountsFromState(ChemicalState const &state0) -> void
Set the initial amounts of the conservative components before the chemical system reacts.
auto elementMass(StringOrIndex const &element, real const &value, String const &unit="kg") -> void
Specify the mass of an element at chemical equilibrium.
auto internalEnergy(real const &value, String const &unit="J") -> void
Specify the internal energy of the system at chemical equilibrium.
auto gibbsEnergy(real const &value, String const &unit="J") -> void
Specify the Gibbs energy of the system at chemical equilibrium.
auto setInitialComponentAmounts(VectorXdConstRef const &c0) -> void
Set the initial amounts of the conservative components before the chemical system reacts.
auto fugacity(String const &species, real const &value, String const &unit="bar") -> void
Specify the fugacity of a gaseous species at chemical equilibrium.
auto inputValues() const -> ArrayXrConstRef
Get the values of the input variables associated with the equilibrium conditions.
auto set(String const &input, real const &val) -> void
Set the value of an input variable with given name.
auto inputNames() const -> Strings const &
Get the names of the input variables associated with the equilibrium conditions.
auto entropy(real const &value, String const &unit="J/K") -> void
Specify the entropy of the system at chemical equilibrium.
auto elementMassInPhase(StringOrIndex const &element, StringOrIndex const &phase, real const &value, String const &unit="kg") -> void
Specify the mass of an element in a phase at chemical equilibrium.
auto pE(real const &value) -> void
Specify the pE at chemical equilibrium.
auto phaseAmount(StringOrIndex const &phase, real const &value, String const &unit="mol") -> void
Specify the amount of a phase at chemical equilibrium.
auto pressure(real const &value, String const &unit="Pa") -> void
Specify the pressure of the system at chemical equilibrium.
auto phaseMass(StringOrIndex const &phase, real const &value, String const &unit="kg") -> void
Specify the mass of a phase at chemical equilibrium.
auto setUpperBoundPressure(double value, String const &unit="Pa") -> void
Set the upper bound for pressure during the equilibrium calculation.
auto setLowerBoundTitrant(String const &substance, double value, String const &unit="mol") -> void
Set the lower bound for the amount of a titrant during the equilibrium calculation.
EquilibriumConditions(EquilibriumSpecs const &specs)
Construct an EquilibriumConditions object with given equilibrium specifications.
The class used to define conditions to be satisfied at chemical equilibrium.
Definition: EquilibriumSpecs.hpp:281
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
auto unit(Index rows, Index i) -> decltype(VectorXd::Unit(rows, i))
Return an expression of a unit vector.
Definition: Matrix.hpp:546
Eigen::MatrixXd MatrixXd
Convenient alias to Eigen type.
Definition: Matrix.hpp:137
std::string String
Convenient alias for std::string.
Definition: Types.hpp:52
std::size_t Index
Define a type that represents an index.
Definition: Index.hpp:26
autodiff::real real
The number type used throughout the library.
Definition: Real.hpp:26
std::vector< std::string > Strings
Convenient alias for std::vector<String>.
Definition: Types.hpp:55
Eigen::Ref< const VectorXd > VectorXdConstRef
Convenient alias to Eigen type.
Definition: Matrix.hpp:76
std::variant< Index, int, std::string > StringOrIndex
The type used to accept either a name or an index.
Definition: Types.hpp:58
Eigen::Ref< const ArrayXd > ArrayXdConstRef
Convenient alias to Eigen type.
Definition: Matrix.hpp:105
auto index(const Container &c, const T &x) -> std::size_t
Return the index of item x in container c or the number of items if not found.
Definition: Algorithms.hpp:37
Eigen::ArrayXd ArrayXd
Convenient alias to Eigen type.
Definition: Matrix.hpp:103
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