Reaktoro  v2.11.0
A unified framework for modeling chemically reactive systems
EquilibriumSpecs.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/Matrix.hpp>
22 #include <Reaktoro/Common/Types.hpp>
23 #include <Reaktoro/Core/ChemicalFormula.hpp>
24 #include <Reaktoro/Core/ChemicalSystem.hpp>
25 
26 namespace Reaktoro {
27 
28 // Forward declarations
29 class ChemicalProps;
30 class ChemicalState;
31 
39 {
44  using ChemicalPotentialFn = Fn<real(ChemicalProps const& props, VectorXrConstRef const& p, VectorXrConstRef const& w)>;
45 
48 
51 
54 
57 };
58 
65 {
69  using ChemicalPotentialFn = Fn<real(ChemicalProps const& props, real const& pk)>;
70 
73 
76 };
77 
80 {
85  using Func1 = Fn<real(ChemicalProps const& props, VectorXrConstRef const& p, VectorXrConstRef const& w)>;
86 
90  using Func2 = Fn<real(ChemicalProps const& props, VectorXrConstRef const& w)>;
91 
94  using Func3 = Fn<real(ChemicalProps const& props)>;
95 
97  EquationConstraintFn() = default;
98 
100  EquationConstraintFn(Func1 const& f) : _fn(f) {}
101 
103  EquationConstraintFn(Func2 const& f) : _fn([=](ChemicalProps const& props, VectorXrConstRef const& p, VectorXrConstRef const& w) -> real { return f(props, w); }) {}
104 
106  EquationConstraintFn(Func3 const& f) : _fn([=](ChemicalProps const& props, VectorXrConstRef const& p, VectorXrConstRef const& w) -> real { return f(props); }) {}
107 
112  auto operator()(ChemicalProps const& props, VectorXrConstRef const& p, VectorXrConstRef const& w) const -> real { return _fn(props, p, w); }
113 
115  auto operator=(Func1 const& f) { return *this = EquationConstraintFn(f); }
116 
118  auto operator=(Func2 const& f) { return *this = EquationConstraintFn(f); }
119 
121  auto operator=(Func3 const& f) { return *this = EquationConstraintFn(f); }
122 
124  auto initialized() const { return static_cast<bool>(_fn); }
125 
127  operator bool() const { return initialized(); }
128 
131 };
132 
135 {
138 
141 };
142 
144 struct [[deprecated("ConstraintEquation has been renamed to EquationConstraint. Please make this change in your code.")]] ConstraintEquation : EquationConstraint
145 {};
146 
149 {
154  using Func = Fn<VectorXr(ChemicalProps const& props, VectorXrConstRef const& w, VectorXrConstRef const& p)>;
155 
158 
161 };
162 
167 {
170 
173 
176 };
177 
182 {
185 
188 
191 };
192 
281 {
282 public:
284  explicit EquilibriumSpecs(ChemicalSystem const& system);
285 
286  //=================================================================================================
287  //
288  // STATIC METHODS TO CREATE PREDEFINED CHEMICAL EQUILIBRIUM SPECIFICATIONS
289  //
290  //=================================================================================================
291 
293  static auto TP(ChemicalSystem const& system) -> EquilibriumSpecs;
294 
296  static auto HP(ChemicalSystem const& system) -> EquilibriumSpecs;
297 
299  static auto TV(ChemicalSystem const& system) -> EquilibriumSpecs;
300 
302  static auto UV(ChemicalSystem const& system) -> EquilibriumSpecs;
303 
305  static auto SP(ChemicalSystem const& system) -> EquilibriumSpecs;
306 
308  static auto SV(ChemicalSystem const& system) -> EquilibriumSpecs;
309 
310  //=================================================================================================
311  //
312  // METHODS TO SPECIFY THERMODYNAMIC CONSTRAINTS
313  //
314  //=================================================================================================
315 
322  auto temperature() -> void;
323 
330  auto pressure() -> void;
331 
337  auto volume() -> void;
338 
344  auto internalEnergy() -> void;
345 
351  auto enthalpy() -> void;
352 
358  auto gibbsEnergy() -> void;
359 
365  auto helmholtzEnergy() -> void;
366 
372  auto entropy() -> void;
373 
375  auto charge() -> void;
376 
379  auto elementAmount(StringOrIndex const& element) -> void;
380 
384  auto elementAmountInPhase(StringOrIndex const& element, StringOrIndex const& phase) -> void;
385 
388  auto elementMass(StringOrIndex const& element) -> void;
389 
393  auto elementMassInPhase(StringOrIndex const& element, StringOrIndex const& phase) -> void;
394 
397  auto phaseAmount(StringOrIndex const& phase) -> void;
398 
401  auto phaseMass(StringOrIndex const& phase) -> void;
402 
405  auto phaseVolume(StringOrIndex const& phase) -> void;
406 
407  //=================================================================================================
408  //
409  // METHODS TO SPECIFY UNKNOWN INPUT CONDITIONS
410  //
411  //=================================================================================================
412 
417  auto unknownTemperature() -> void;
418 
423  auto unknownPressure() -> void;
424 
425  //=================================================================================================
426  //
427  // METHODS TO SPECIFY CHEMICAL POTENTIAL CONSTRAINTS
428  //
429  //=================================================================================================
430 
441  auto chemicalPotential(String substance) -> void;
442 
456  auto lnActivity(Species const& species) -> void;
457 
464  auto lnActivity(String species) -> void;
465 
472  auto lgActivity(String species) -> void;
473 
480  auto activity(String species) -> void;
481 
494  auto fugacity(String gas) -> void;
495 
519  auto pH() -> void;
520 
529  auto pMg() -> void;
530 
539  auto pE() -> void;
540 
549  auto Eh() -> void;
550 
551  //=================================================================================================
552  //
553  // METHODS TO SPECIFY HOW THE CHEMICAL SYSTEM IS OPEN
554  //
555  //=================================================================================================
556 
585  auto openTo(ChemicalFormula const& substance) -> void;
586 
588  // auto openTo(ChemicalState const& state) -> void;
589 
591  // auto openTo(Material const& material) -> void;
592 
593  //=================================================================================================
594  //
595  // METHODS TO SPECIFY ADDITIONAL UNKNOWNS
596  //
597  //=================================================================================================
598 
600  auto addUnknownTitrantAmount(ChemicalFormula const& substance) -> void;
601 
602  //=================================================================================================
603  //
604  // METHODS TO GET THE NUMBER OF INTRODUCED CONSTRAINTS, INPUT VARIABLES, AND CONTROL VARIABLES
605  //
606  //=================================================================================================
607 
609  auto numInputs() const -> Index;
610 
612  auto numControlVariables() const -> Index;
613 
615  auto numControlVariablesP() const -> Index;
616 
618  auto numControlVariablesQ() const -> Index;
619 
621  auto numTitrants() const -> Index;
622 
624  auto numTitrantsExplicit() const -> Index;
625 
627  auto numTitrantsImplicit() const -> Index;
628 
630  auto numEquationConstraints() const -> Index;
631 
633  auto numReactivityConstraints() const -> Index;
634 
636  auto numConstraints() const -> Index;
637 
639  auto numConservativeComponents() const -> Index;
640 
641  //=================================================================================================
642  //
643  // METHODS TO GET THE NAMES OF INTRODUCED CONSTRAINTS, INPUT VARIABLES, AND CONTROL VARIABLES
644  //
645  //=================================================================================================
646 
648  auto namesInputs() const -> Strings;
649 
651  auto namesControlVariables() const -> Strings;
652 
654  auto namesControlVariablesP() const -> Strings;
655 
657  auto namesControlVariablesQ() const -> Strings;
658 
660  auto namesTitrants() const -> Strings;
661 
663  auto namesTitrantsExplicit() const -> Strings;
664 
666  auto namesTitrantsImplicit() const -> Strings;
667 
669  auto namesConstraints() const -> Strings;
670 
672  auto namesConservativeComponents() const -> Strings;
673 
674  //=================================================================================================
675  //
676  // METHODS TO ADD CONTROL VARIABLES, CONSTRAINTS, AND INPUT VARIABLES
677  //
678  //=================================================================================================
679 
681  auto addControlVariableQ(ControlVariableQ const& qvar) -> void;
682 
684  auto addControlVariableP(ControlVariableP const& pvar) -> void;
685 
687  auto addConstraint(EquationConstraint const& constraint) -> void;
688 
690  auto addConstraints(EquationConstraints const& constraints) -> void;
691 
693  auto addReactivityConstraint(ReactivityConstraint const& constraint) -> void;
694 
696  auto addReactivityConstraints(ReactivityConstraints const& constraints) -> void;
697 
699  auto addInput(String const& var) -> Index;
700 
701  //=================================================================================================
702  //
703  // MISCELLANEOUS METHODS
704  //
705  //=================================================================================================
706 
708  auto system() const -> ChemicalSystem const&;
709 
711  auto inputs() const -> Strings const&;
712 
714  auto isTemperatureUnknown() const -> bool;
715 
717  auto isPressureUnknown() const -> bool;
718 
720  auto indexTemperatureAmongInputVariables() const -> Index;
721 
723  auto indexTemperatureAmongControlVariablesP() const -> Index;
724 
726  auto indexPressureAmongInputVariables() const -> Index;
727 
729  auto indexPressureAmongControlVariablesP() const -> Index;
730 
732  auto indexInputVariable(String const& name) const -> Index;
733 
735  auto indexControlVariableP(String const& name) const -> Index;
736 
738  auto indexControlVariableQ(String const& name) const -> Index;
739 
741  auto controlVariablesQ() const -> Vec<ControlVariableQ> const&;
742 
744  auto controlVariablesP() const -> Vec<ControlVariableP> const&;
745 
747  auto titrants() const -> Vec<ChemicalFormula>;
748 
750  auto titrantsExplicit() const -> Vec<ChemicalFormula>;
751 
753  auto titrantsImplicit() const -> Vec<ChemicalFormula>;
754 
756  auto equationConstraintsSingle() const -> Vec<EquationConstraint> const&;
757 
759  auto equationConstraintsSystem() const -> Vec<EquationConstraints> const&;
760 
762  auto reactivityConstraintsSingle() const -> Vec<ReactivityConstraint> const&;
763 
765  auto reactivityConstraintsSystem() const -> Vec<ReactivityConstraints> const&;
766 
767  //=================================================================================================
768  //
769  // METHODS THAT ASSEMBLE CONSTRAINTS AND MATRICES FOR CURRENT STATE OF EQUILIBRIUM SPECIFICATIONS
770  //
771  //=================================================================================================
772 
777  auto assembleEquationConstraints() const -> EquationConstraints;
778 
783  auto assembleReactivityConstraints() const -> ReactivityConstraints;
784 
786  auto assembleReactivityConstraintsMatrixKn() const -> MatrixXd;
787 
789  auto assembleReactivityConstraintsMatrixKp() const -> MatrixXd;
790 
793  auto assembleConservationMatrix() const -> MatrixXd;
794 
803  auto assembleConservationMatrixN() const -> MatrixXd;
804 
806  auto assembleConservationMatrixQ() const -> MatrixXd;
807 
809  auto assembleConservationMatrixP() const -> MatrixXd;
810 
811 private:
813  ChemicalSystem m_system;
814 
816  Strings m_inputs;
817 
819  Vec<ControlVariableQ> qvars;
820 
822  Vec<ControlVariableP> pvars;
823 
825  Vec<EquationConstraint> econstraints_single;
826 
828  Vec<EquationConstraints> econstraints_system;
829 
831  Strings econstraints_ids;
832 
834  Vec<ReactivityConstraint> rconstraints_single;
835 
837  Vec<ReactivityConstraints> rconstraints_system;
838 
840  Strings rconstraints_ids;
841 
842  // ----- AUXILIARY DATA MEMBERS ----- //
843 
845  Vec<ChemicalFormula> titrants_explicit;
846 
848  Vec<ChemicalFormula> titrants_implicit;
849 
851  Strings species_with_unknown_chemical_potentials;
852 
853 private:
856  auto throwErrorIfTitrantHasBeenRegistered(ChemicalFormula const& substance) const -> void;
857 };
858 
859 } // namespace Reaktoro
A type used to represent the chemical formula of a chemical species.
Definition: ChemicalFormula.hpp:27
The class that computes chemical properties of a chemical system.
Definition: ChemicalProps.hpp:34
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: EquilibriumSpecs.hpp:281
static auto SV(ChemicalSystem const &system) -> EquilibriumSpecs
Return specifications for a chemical equilbrium problem with given entropy (S) and volume (V).
auto entropy() -> void
Specify that the entropy of the system is given at chemical equilibrium.
auto elementMass(StringOrIndex const &element) -> void
Specify that the mass of an element is given at chemical equilibrium.
auto lnActivity(Species const &species) -> void
Specify that the ln activity of a species is given at chemical equilibrium.
auto helmholtzEnergy() -> void
Specify that the Helmholtz energy of the system is given at chemical equilibrium.
auto lnActivity(String species) -> void
Specify that the ln activity of a species is given at chemical equilibrium.
static auto HP(ChemicalSystem const &system) -> EquilibriumSpecs
Return specifications for a chemical equilbrium problem with given enthalpy (H) and pressure (P).
auto volume() -> void
Specify that the volume of the system is given at chemical equilibrium.
static auto UV(ChemicalSystem const &system) -> EquilibriumSpecs
Return specifications for a chemical equilbrium problem with given internal (U) energy and volume (V)...
auto elementMassInPhase(StringOrIndex const &element, StringOrIndex const &phase) -> void
Specify that the mass of an element in a phase is given at chemical equilibrium.
static auto TP(ChemicalSystem const &system) -> EquilibriumSpecs
Return specifications for a chemical equilbrium problem with given temperature (T) and pressure (P).
auto phaseAmount(StringOrIndex const &phase) -> void
Specify that the amount of a phase is given at chemical equilibrium.
static auto SP(ChemicalSystem const &system) -> EquilibriumSpecs
Return specifications for a chemical equilbrium problem with given entropy (S) and pressure (P).
auto chemicalPotential(String substance) -> void
Specify that the chemical potential of a substance is given at chemical equilibrium.
auto openTo(ChemicalFormula const &substance) -> void
Specify that the chemical system is open to a substance.
auto activity(String species) -> void
Specify that the activity of a species is given at chemical equilibrium.
auto phaseVolume(StringOrIndex const &phase) -> void
Specify that the volume of a phase is given at chemical equilibrium.
auto pH() -> void
Specify that the pH is given at chemical equilibrium.
auto pressure() -> void
Specify that the pressure of the system is given at chemical equilibrium.
auto fugacity(String gas) -> void
Specify that the fugacity of a gaseous species is given at chemical equilibrium.
auto unknownTemperature() -> void
Specify that the temperature of the system is unknown in the chemical equilibrium calculation.
auto unknownPressure() -> void
Specify that the pressure of the system is unknown in the chemical equilibrium calculation.
auto lgActivity(String species) -> void
Specify that the lg activity of a species is given at chemical equilibrium.
auto numInputs() const -> Index
Return the number of introduced input variables.
auto elementAmountInPhase(StringOrIndex const &element, StringOrIndex const &phase) -> void
Specify that the amount of an element in a phase is given at chemical equilibrium.
auto pE() -> void
Specify that pE is given at chemical equilibrium.
auto phaseMass(StringOrIndex const &phase) -> void
Specify that the mass of a phase is given at chemical equilibrium.
EquilibriumSpecs(ChemicalSystem const &system)
Construct an EquilibriumSpecs object.
static auto TV(ChemicalSystem const &system) -> EquilibriumSpecs
Return specifications for a chemical equilbrium problem with given temperature (T) and volume (V).
auto charge() -> void
Specify that the electric charge is given at chemical equilibrium.
auto internalEnergy() -> void
Specify that the internal energy of the system is given at chemical equilibrium.
auto pMg() -> void
Specify that pMg is given at chemical equilibrium.
auto enthalpy() -> void
Specify that the enthalpy of the system is given at chemical equilibrium.
auto Eh() -> void
Specify that Eh is given at chemical equilibrium.
auto elementAmount(StringOrIndex const &element) -> void
Specify that the amount of an element is given at chemical equilibrium.
auto gibbsEnergy() -> void
Specify that the Gibbs energy of the system is given at chemical equilibrium.
auto addUnknownTitrantAmount(ChemicalFormula const &substance) -> void
Specify that the chemical system is open to a given chemical state. // TODO: Implement EquilibriumSpe...
auto temperature() -> void
Specify that the temperature of the system is given at chemical equilibrium.
A type used to represent a chemical species and its attributes.
Definition: Species.hpp:35
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
std::vector< T > Vec
Convenient alias for std::vector<T>.
Definition: Types.hpp:66
Eigen::Ref< const VectorXr > VectorXrConstRef
Convenient alias to Eigen type.
Definition: Matrix.hpp:60
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::VectorXd VectorXd
Convenient alias to Eigen type.
Definition: Matrix.hpp:74
std::variant< Index, int, std::string > StringOrIndex
The type used to accept either a name or an index.
Definition: Types.hpp:58
autodiff::VectorXreal VectorXr
Convenient alias to Eigen type.
Definition: Matrix.hpp:58
std::function< F > Fn
Convenient alias for std::function<R(Args...)>.
Definition: Types.hpp:110
Used to define equation constraints in a chemical equilibrium problem.
Definition: EquilibriumSpecs.hpp:145
Used to define a p control variable in a chemical equilibrium problem.
Definition: EquilibriumSpecs.hpp:65
ChemicalFormula substance
The chemical formula of the substance associated to this p control variable (optional).
Definition: EquilibriumSpecs.hpp:75
String name
The unique name for this p control variable (required).
Definition: EquilibriumSpecs.hpp:72
Fn< real(ChemicalProps const &props, real const &pk)> ChemicalPotentialFn
The signature of functions that evaluate the chemical potential of a species in terms of a p control ...
Definition: EquilibriumSpecs.hpp:69
Used to define a q control variable in a chemical equilibrium problem.
Definition: EquilibriumSpecs.hpp:39
Fn< real(ChemicalProps const &props, VectorXrConstRef const &p, VectorXrConstRef const &w)> ChemicalPotentialFn
The signature of functions that evaluate the prescribed chemical potential of a substance.
Definition: EquilibriumSpecs.hpp:44
ChemicalFormula substance
The chemical formula of the substance associated to this q control variable (required).
Definition: EquilibriumSpecs.hpp:50
String id
The unique identifier for the chemical potential constraint associated to this q control variable (re...
Definition: EquilibriumSpecs.hpp:53
ChemicalPotentialFn fn
The chemical potential function associated to this q control variable (required).
Definition: EquilibriumSpecs.hpp:56
String name
The unique name for this q control variable (required).
Definition: EquilibriumSpecs.hpp:47
Used to define the function that evaluates the residual of an equation constraint in a chemical equil...
Definition: EquilibriumSpecs.hpp:80
Fn< real(ChemicalProps const &props)> Func3
An alternative signature of functions that evaluate the residual of an equation constraint.
Definition: EquilibriumSpecs.hpp:94
EquationConstraintFn()=default
Construct a default EquationConstraintFn object.
auto operator=(Func1 const &f)
Assign an equilibrium constraint function with signature EquationConstraintFn::Func1 to this Equation...
Definition: EquilibriumSpecs.hpp:115
EquationConstraintFn(Func3 const &f)
Construct an EquationConstraintFn object with given equilibrium constraint function with signature Fu...
Definition: EquilibriumSpecs.hpp:106
auto operator=(Func3 const &f)
Assign an equilibrium constraint function with signature EquationConstraintFn::Func3 to this Equation...
Definition: EquilibriumSpecs.hpp:121
EquationConstraintFn(Func1 const &f)
Construct an EquationConstraintFn object with given equilibrium constraint function with signature Fu...
Definition: EquilibriumSpecs.hpp:100
EquationConstraintFn(Func2 const &f)
Construct an EquationConstraintFn object with given equilibrium constraint function with signature Fu...
Definition: EquilibriumSpecs.hpp:103
Func1 _fn
The function that evaluates the residual of the equation constraint.
Definition: EquilibriumSpecs.hpp:130
auto operator=(Func2 const &f)
Assign an equilibrium constraint function with signature EquationConstraintFn::Func2 to this Equation...
Definition: EquilibriumSpecs.hpp:118
auto initialized() const
Return true if this EquationConstraintFn object has been initialized with an equilibrium constraint f...
Definition: EquilibriumSpecs.hpp:124
auto operator()(ChemicalProps const &props, VectorXrConstRef const &p, VectorXrConstRef const &w) const -> real
Evaluate the residual of the equation constraint.
Definition: EquilibriumSpecs.hpp:112
Fn< real(ChemicalProps const &props, VectorXrConstRef const &p, VectorXrConstRef const &w)> Func1
The main signature of functions that evaluate the residual of an equation constraint.
Definition: EquilibriumSpecs.hpp:85
Fn< real(ChemicalProps const &props, VectorXrConstRef const &w)> Func2
An alternative signature of functions that evaluate the residual of an equation constraint.
Definition: EquilibriumSpecs.hpp:90
Used to define equation constraints in a chemical equilibrium problem.
Definition: EquilibriumSpecs.hpp:135
String id
The unique identifier for this equation constraint.
Definition: EquilibriumSpecs.hpp:137
EquationConstraintFn fn
The function defining the equation to be satisfied at chemical equilibrium.
Definition: EquilibriumSpecs.hpp:140
Used to define a system of equation constraints in a chemical equilibrium problem.
Definition: EquilibriumSpecs.hpp:149
Fn< VectorXr(ChemicalProps const &props, VectorXrConstRef const &w, VectorXrConstRef const &p)> Func
The signature of functions that evaluate the residual of the system of equation constraints.
Definition: EquilibriumSpecs.hpp:154
Strings ids
The unique identifier for each equation constraint.
Definition: EquilibriumSpecs.hpp:157
Func fn
The function defining the system of equations to be satisfied at chemical equilibrium.
Definition: EquilibriumSpecs.hpp:160
Used to define reactivity restrictions among species in the chemical equilibrium calculation.
Definition: EquilibriumSpecs.hpp:167
VectorXd Kn
The linear equation coefficients in the constraint corresponding to the species amounts variables n.
Definition: EquilibriumSpecs.hpp:172
String id
The unique identifier for this reactivity constraint.
Definition: EquilibriumSpecs.hpp:169
VectorXd Kp
The linear equation coefficients in the constraint corresponding to the introduced control variables ...
Definition: EquilibriumSpecs.hpp:175
Used to define a system of reactivity restrictions among species in the chemical equilibrium calculat...
Definition: EquilibriumSpecs.hpp:182
MatrixXd Kp
The coefficient matrix of the linear reactivity constraint equations corresponding to the introduced ...
Definition: EquilibriumSpecs.hpp:190
MatrixXd Kn
The coefficient matrix of the linear reactivity constraint equations corresponding to the species amo...
Definition: EquilibriumSpecs.hpp:187
Strings ids
The unique identifiers for each reactivity constraint.
Definition: EquilibriumSpecs.hpp:184