Reaktoro
A unified framework for modeling chemically reactive systems
EquilibriumInverseProblem.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright (C) 2014-2018 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 // C++ includes
21 #include <memory>
22 
23 // Reaktoro includes
24 #include <Reaktoro/Math/Matrix.hpp>
25 
26 namespace Reaktoro {
27 
28 // Forward declarations
29 class ChemicalSystem;
30 class ChemicalState;
31 class Partition;
32 struct EquilibriumResult;
33 
36 {
38  Vector val;
39 
42 
45 };
46 
57 {
58 public:
61 
64 
67 
70 
74 
78  auto setTemperature(double val) -> EquilibriumInverseProblem&;
79 
84  auto setTemperature(double val, std::string units) -> EquilibriumInverseProblem&;
85 
89  auto setPressure(double val) -> EquilibriumInverseProblem&;
90 
95  auto setPressure(double val, std::string units) -> EquilibriumInverseProblem&;
96 
100 
105  auto add(std::string name, double amount, std::string units) -> EquilibriumInverseProblem&;
106 
111  auto fixSpeciesAmount(std::string species, double value, std::string units) -> EquilibriumInverseProblem&;
112 
118  auto fixSpeciesAmount(std::string species, double value, std::string units, std::string titrant) -> EquilibriumInverseProblem&;
119 
124  auto fixSpeciesMass(std::string species, double value, std::string units) -> EquilibriumInverseProblem&;
125 
131  auto fixSpeciesMass(std::string species, double value, std::string units, std::string titrant) -> EquilibriumInverseProblem&;
132 
136  auto fixSpeciesActivity(std::string species, double value) -> EquilibriumInverseProblem&;
137 
142  auto fixSpeciesActivity(std::string species, double value, std::string titrant) -> EquilibriumInverseProblem&;
143 
149  auto fixSpeciesActivity(std::string species, double value, std::string titrant1, std::string titrant2) -> EquilibriumInverseProblem&;
150 
155  auto fixSpeciesFugacity(std::string species, double value, std::string units) -> EquilibriumInverseProblem&;
156 
162  auto fixSpeciesFugacity(std::string species, double value, std::string units, std::string titrant) -> EquilibriumInverseProblem&;
163 
169  auto fixPhaseAmount(std::string phase, double value, std::string units, std::string titrant) -> EquilibriumInverseProblem&;
170 
176  auto fixPhaseMass(std::string phase, double value, std::string units, std::string titrant) -> EquilibriumInverseProblem&;
177 
183  auto fixPhaseVolume(std::string phase, double value, std::string units, std::string titrant) -> EquilibriumInverseProblem&;
184 
190  auto fixPhaseSetVolume(const std::vector<std::string>& phases, double value, std::string units, std::string titrant) -> EquilibriumInverseProblem&;
191 
194  auto pH(double value) -> EquilibriumInverseProblem&;
195 
199  auto pH(double value, std::string titrant) -> EquilibriumInverseProblem&;
200 
205  auto pH(double value, std::string titrant1, std::string titrant2) -> EquilibriumInverseProblem&;
206 
209  auto pE(double value) -> EquilibriumInverseProblem&;
210 
214  auto pE(double value, std::string titrant) -> EquilibriumInverseProblem&;
215 
218  auto Eh(double value, std::string units) -> EquilibriumInverseProblem&;
219 
224  auto Eh(double value, std::string units, std::string titrant) -> EquilibriumInverseProblem&;
225 
230  auto alkalinity(double value, std::string units, std::string titrant) -> EquilibriumInverseProblem&;
231 
233  auto system() const -> const ChemicalSystem&;
234 
236  auto partition() const -> const Partition&;
237 
239  auto temperature() const -> double;
240 
242  auto pressure() const -> double;
243 
245  auto numConstraints() const -> Index;
246 
248  auto numTitrants() const -> Index;
249 
254  auto formulaMatrixTitrants() const -> Matrix;
255 
259  auto elementInitialAmounts() const -> Vector;
260 
262  auto titrantInitialAmounts() const -> Vector;
263 
268 
271  auto solve(ChemicalState& state) -> EquilibriumResult;
272 
273 private:
274  struct Impl;
275 
276  std::unique_ptr<Impl> pimpl;
277 };
278 
279 } // namespace Reaktoro
auto operator=(EquilibriumInverseProblem other) -> EquilibriumInverseProblem &
Assign a copy of an EquilibriumInverseProblem instance.
Definition: EquilibriumInverseProblem.cpp:614
auto Eh(double value, std::string units) -> EquilibriumInverseProblem &
Fix the Eh of the aqueous solution (in units of volts).
Definition: EquilibriumInverseProblem.cpp:792
auto solve(ChemicalState &state) -> EquilibriumResult
Solve the inverse equilibrium problem.
Definition: EquilibriumInverseProblem.cpp:863
A type used to define the result of the evaluation of a system of equilibrium constraints.
Definition: EquilibriumInverseProblem.hpp:36
auto setPressure(double val) -> EquilibriumInverseProblem &
Set the pressure for the equilibrium calculation (in units of Pa).
Definition: EquilibriumInverseProblem.cpp:638
auto titrantInitialAmounts() const -> Vector
Return the initial amounts of titrants as initial guess for the inverse equilibrium calculation.
Definition: EquilibriumInverseProblem.cpp:853
auto system() const -> const ChemicalSystem &
Return a reference to the ChemicalSystem instance used to create this EquilibriumProblem instance.
Definition: EquilibriumInverseProblem.cpp:813
auto amount(double value, Index nspecies, Index ispecies) -> ChemicalScalarBase< double, decltype(tr(unit(nspecies, ispecies)))>
Return a ChemicalScalar representation of a mole amount of a species.
Definition: ChemicalScalar.hpp:233
A class to represent a system and its attributes and properties.
Definition: ChemicalSystem.hpp:38
auto fixSpeciesAmount(std::string species, double value, std::string units) -> EquilibriumInverseProblem &
Fix the molar amount of a species at equilibrium.
Definition: EquilibriumInverseProblem.cpp:662
Provides a computational representation of the state of a multiphase chemical system.
Definition: ChemicalState.hpp:61
auto fixSpeciesMass(std::string species, double value, std::string units) -> EquilibriumInverseProblem &
Fix the mass of a species at equilibrium.
Definition: EquilibriumInverseProblem.cpp:676
auto pressure() const -> double
Return the pressure for the equilibrium calculation (in units of Pa)
Definition: EquilibriumInverseProblem.cpp:828
auto elementInitialAmounts() const -> Vector
Return the initial amounts of elements in the equilibrium partition.
Definition: EquilibriumInverseProblem.cpp:848
auto numConstraints() const -> Index
Return the number of constraints used in the inverse equilibrium problem.
Definition: EquilibriumInverseProblem.cpp:833
auto fixPhaseMass(std::string phase, double value, std::string units, std::string titrant) -> EquilibriumInverseProblem &
Fix the total mass of a phase at equilibrium with given titrant.
Definition: EquilibriumInverseProblem.cpp:735
auto fixPhaseSetVolume(const std::vector< std::string > &phases, double value, std::string units, std::string titrant) -> EquilibriumInverseProblem &
Fix the total volume of a set of phases at equilibrium with given titrant.
Definition: EquilibriumInverseProblem.cpp:753
auto setTemperature(double val) -> EquilibriumInverseProblem &
Set the temperature for the equilibrium calculation (in units of K).
Definition: EquilibriumInverseProblem.cpp:626
A class used for defining an inverse equilibrium problem.
Definition: EquilibriumInverseProblem.hpp:57
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
Vector val
The residual values of the equilibrium constraints.
Definition: EquilibriumInverseProblem.hpp:38
auto partition() const -> const Partition &
Return a reference to the Partition instance used to create this EquilibriumProblem instance.
Definition: EquilibriumInverseProblem.cpp:818
auto alkalinity(double value, std::string units, std::string titrant) -> EquilibriumInverseProblem &
Fix the total alkalinity of the aqueous solution.
Definition: EquilibriumInverseProblem.cpp:805
auto formulaMatrixTitrants() const -> Matrix
Return the formula matrix of the titrants.
Definition: EquilibriumInverseProblem.cpp:843
auto fixPhaseAmount(std::string phase, double value, std::string units, std::string titrant) -> EquilibriumInverseProblem &
Fix the total molar amount of a phase at equilibrium with given titrant.
Definition: EquilibriumInverseProblem.cpp:726
auto add(std::string name, double amount, std::string units) -> EquilibriumInverseProblem &
Add a given amount of a compound or species to the initial equilibrium recipe.
Definition: EquilibriumInverseProblem.cpp:656
auto setPartition(const Partition &partition) -> EquilibriumInverseProblem &
Set the partition of the chemical system.
Definition: EquilibriumInverseProblem.cpp:620
auto residualEquilibriumConstraints(VectorConstRef x, const ChemicalState &state) const -> ResidualEquilibriumConstraints
Return the residuals of the equilibrium constraints and their partial derivatives.
Definition: EquilibriumInverseProblem.cpp:858
auto fixPhaseVolume(std::string phase, double value, std::string units, std::string titrant) -> EquilibriumInverseProblem &
Fix the volume of a phase at equilibrium with given titrant.
Definition: EquilibriumInverseProblem.cpp:744
virtual ~EquilibriumInverseProblem()
Destroy this EquilibriumInverseProblem instance.
Definition: EquilibriumInverseProblem.cpp:611
std::size_t Index
Define a type that represents an index.
Definition: Index.hpp:26
EquilibriumInverseProblem(const ChemicalSystem &system)
Construct an EquilibriumInverseProblem instance.
Definition: EquilibriumInverseProblem.cpp:603
auto setElementInitialAmounts(VectorConstRef values) -> EquilibriumInverseProblem &
Set the initial known molar amounts of the elements in the equilibrium partition.
Definition: EquilibriumInverseProblem.cpp:650
auto numTitrants() const -> Index
Return the number of titrants used in the inverse equilibrium problem.
Definition: EquilibriumInverseProblem.cpp:838
Matrix ddn
The partial derivatives of the residuals w.r.t. species amounts n.
Definition: EquilibriumInverseProblem.hpp:44
auto pH(double value) -> EquilibriumInverseProblem &
Fix the pH of the aqueous solution.
Definition: EquilibriumInverseProblem.cpp:762
Matrix ddx
The partial derivatives of the residuals w.r.t. titrant amounts x.
Definition: EquilibriumInverseProblem.hpp:41
Provide a computational representation of the Partition of a chemical system.
Definition: Partition.hpp:56
auto fixSpeciesActivity(std::string species, double value) -> EquilibriumInverseProblem &
Fix the activity of a species at equilibrium.
Definition: EquilibriumInverseProblem.cpp:689
auto fixSpeciesFugacity(std::string species, double value, std::string units) -> EquilibriumInverseProblem &
Fix the fugacity of a gaseous species.
Definition: EquilibriumInverseProblem.cpp:714
Eigen::MatrixXd Matrix
Alias to Eigen type MatrixXd.
Definition: Matrix.hpp:42
auto pE(double value) -> EquilibriumInverseProblem &
Fix the pe of the aqueous solution.
Definition: EquilibriumInverseProblem.cpp:780
Eigen::Ref< const Eigen::VectorXd > VectorConstRef
< Alias to Eigen type Ref<VectorXd>.
Definition: Matrix.hpp:31
A type used to describe the result of an equilibrium calculation.
Definition: EquilibriumResult.hpp:35
auto temperature() const -> double
Return the temperature for the equilibrium calculation (in units of K)
Definition: EquilibriumInverseProblem.cpp:823