Reaktoro 
A unified framework for modeling chemically reactive systems
ChemicalSystem.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright (C) 2014-2015 Allan Leal
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 #pragma once
19 
20 // Reaktoro includes
21 #include <Reaktoro/Math/Matrix.hpp>
22 #include <Reaktoro/Core/Element.hpp>
23 #include <Reaktoro/Core/Species.hpp>
24 #include <Reaktoro/Core/Phase.hpp>
25 
26 namespace Reaktoro {
27 
28 // Forward declarations
29 class ChemicalProperties;
30 class ThermoProperties;
31 
33 using ThermoModelResult = std::vector<PhaseThermoModelResult>;
34 
36 using ChemicalModelResult = std::vector<PhaseChemicalModelResult>;
37 
39 using ThermoModel = std::function<ThermoModelResult(double, double)>;
40 
42 using ChemicalModel = std::function<ChemicalModelResult(double, double, const Vector&)>;
43 
48 {
49 public:
52 
54  explicit ChemicalSystem(const std::vector<Phase>& phases);
55 
57  virtual ~ChemicalSystem();
58 
60  auto numElements() const -> unsigned;
61 
63  auto numSpecies() const -> unsigned;
64 
67  auto numSpeciesInPhase(Index iphase) const -> unsigned;
68 
70  auto numPhases() const -> unsigned;
71 
73  auto elements() const -> const std::vector<Element>&;
74 
76  auto species() const -> const std::vector<Species>&;
77 
79  auto phases() const -> const std::vector<Phase>&;
80 
84  auto formulaMatrix() const -> const Matrix&;
85 
88  auto element(Index index) const -> const Element&;
89 
92  auto element(std::string name) const -> const Element&;
93 
96  auto species(Index index) const -> const Species&;
97 
100  auto species(std::string name) const -> const Species&;
101 
104  auto phase(Index index) const -> const Phase&;
105 
108  auto phase(std::string name) const -> const Phase&;
109 
112  auto indexElement(std::string name) const -> Index;
113 
117  auto indexElementWithError(std::string name) const -> Index;
118 
121  auto indexSpecies(std::string name) const -> Index;
122 
126  auto indexSpeciesWithError(std::string name) const -> Index;
127 
131  auto indexSpeciesAny(const std::vector<std::string>& names) const -> Index;
132 
136  auto indexSpeciesAnyWithError(const std::vector<std::string>& names) const -> Index;
137 
140  auto indexPhase(std::string name) const -> Index;
141 
145  auto indexPhaseWithError(std::string name) const -> Index;
146 
149  auto indexPhaseWithSpecies(Index index) const -> Index;
150 
153  auto indexFirstSpeciesInPhase(Index iphase) const -> unsigned;
154 
157  auto indicesElements(const std::vector<std::string>& names) const -> Indices;
158 
161  auto indicesElementsInSpecies(Index index) const -> Indices;
162 
165  auto indicesElementsInSpecies(const Indices& indices) const -> Indices;
166 
169  auto indicesSpecies(const std::vector<std::string>& names) const -> Indices;
170 
173  auto indicesSpeciesInPhases(const Indices& indices) const -> Indices;
174 
177  auto indicesPhases(const std::vector<std::string>& names) const -> Indices;
178 
181  auto indicesPhasesWithSpecies(const Indices& indices) const -> Indices;
182 
184  auto indicesFluidPhases() const -> Indices;
185 
187  auto indicesFluidSpecies() const -> Indices;
188 
190  auto indicesSolidPhases() const -> Indices;
191 
193  auto indicesSolidSpecies() const -> Indices;
194 
197  auto elementAmounts(const Vector& n) const -> Vector;
198 
202  auto elementAmountsInPhase(Index iphase, const Vector& n) const -> Vector;
203 
207  auto elementAmountsInSpecies(const Indices& ispecies, const Vector& n) const -> Vector;
208 
212  auto elementAmount(Index ielement, const Vector& n) const -> double;
213 
218  auto elementAmountInPhase(Index ielement, Index iphase, const Vector& n) const -> double;
219 
224  auto elementAmountInSpecies(Index ielement, const Indices& ispecies, const Vector& n) const -> double;
225 
229  auto properties(double T, double P) const -> ThermoProperties;
230 
235  auto properties(double T, double P, const Vector& n) const -> ChemicalProperties;
236 
237 private:
238  struct Impl;
239 
240  std::shared_ptr<Impl> pimpl;
241 };
242 
244 auto operator<<(std::ostream& out, const ChemicalSystem& system) -> std::ostream&;
245 
246 } // namespace Reaktoro
A type used to define a phase and its attributes.
Definition: Phase.hpp:45
Eigen::MatrixXd Matrix
Define an alias to the matrix type of the Eigen library.
Definition: Matrix.hpp:387
auto elements() const -> const std::vector< Element > &
Return the list of elements in the system.
Definition: ChemicalSystem.cpp:162
auto indexElementWithError(std::string name) const -> Index
Return the index of an element in the system.
Definition: ChemicalSystem.cpp:210
auto indexFirstSpeciesInPhase(Index iphase) const -> unsigned
Return the index of the first species in a phase.
Definition: ChemicalSystem.cpp:277
auto numSpeciesInPhase(Index iphase) const -> unsigned
Return the number of species in a phase of the system.
Definition: ChemicalSystem.cpp:142
A type used to describe a species and its attributes.
Definition: Species.hpp:41
A class for querying thermodynamic and chemical properties of a chemical system.
Definition: ChemicalProperties.hpp:36
std::vector< PhaseChemicalModelResult > ChemicalModelResult
The result of the chemical model function that calculates the chemical properties of all phases in a ...
Definition: ChemicalSystem.hpp:36
auto phases() const -> const std::vector< Phase > &
Return the list of phases in the system.
Definition: ChemicalSystem.cpp:195
std::vector< Index > Indices
Define a type that represents a collection of indices.
Definition: Index.hpp:29
auto index(const T &value, const std::vector< T > &values) -> Index
Find the index of a value in a container of values.
Definition: SetUtils.hxx:21
A class to represent a system and its attributes and properties.
Definition: ChemicalSystem.hpp:47
auto names(const NamedValues &values) -> std::vector< std::string >
Return the names of the entries in a container.
Definition: Utils.hxx:22
auto numSpecies() const -> unsigned
Return the number of species in the system.
Definition: ChemicalSystem.cpp:137
auto elementAmountInSpecies(Index ielement, const Indices &ispecies, const Vector &n) const -> double
Calculate the molar amounts of the elements in a given set of species (in units of mol) ...
Definition: ChemicalSystem.cpp:425
auto indexPhase(std::string name) const -> Index
Return the index of a phase in the system.
Definition: ChemicalSystem.cpp:250
auto indexSpeciesAny(const std::vector< std::string > &names) const -> Index
Return the index of the first species in the system with any of the given names.
Definition: ChemicalSystem.cpp:235
auto elementAmounts(const Vector &n) const -> Vector
Calculate the molar amounts of the elements (in units of mol)
Definition: ChemicalSystem.cpp:384
auto indicesFluidPhases() const -> Indices
Return the indices of the fluid phases.
Definition: ChemicalSystem.cpp:354
auto elementAmountsInPhase(Index iphase, const Vector &n) const -> Vector
Calculate the molar amounts of the elements in a given phase (in units of mol)
Definition: ChemicalSystem.cpp:390
auto element(Index index) const -> const Element &
Return an element of the system.
Definition: ChemicalSystem.cpp:152
auto indexSpeciesAnyWithError(const std::vector< std::string > &names) const -> Index
Return the index of the first species in the system with any of the given names.
Definition: ChemicalSystem.cpp:240
auto indicesFluidSpecies() const -> Indices
Return the indices of the species in the fluid phases.
Definition: ChemicalSystem.cpp:364
auto phase(Index index) const -> const Phase &
Return a phase of the system.
Definition: ChemicalSystem.cpp:182
auto indicesElements(const std::vector< std::string > &names) const -> Indices
Return the indices of a set of elements in the system.
Definition: ChemicalSystem.cpp:285
auto indicesSolidSpecies() const -> Indices
Return the indices of the species in the solid phases.
Definition: ChemicalSystem.cpp:379
Eigen::VectorXd Vector
Define an alias to the vector type of the Eigen library.
Definition: Matrix.hpp:384
auto indexSpeciesWithError(std::string name) const -> Index
Return the index of a species in the system.
Definition: ChemicalSystem.cpp:226
auto elementAmountsInSpecies(const Indices &ispecies, const Vector &n) const -> Vector
Calculate the molar amounts of the elements in a given set of species (in units of mol) ...
Definition: ChemicalSystem.cpp:400
virtual ~ChemicalSystem()
Destroy this ChemicalSystem instance.
Definition: ChemicalSystem.cpp:129
auto indexPhaseWithError(std::string name) const -> Index
Return the index of a phase in the system.
Definition: ChemicalSystem.cpp:255
auto indicesSpeciesInPhases(const Indices &indices) const -> Indices
Return the indices of the species in a given set of phases.
Definition: ChemicalSystem.cpp:322
auto indicesSpecies(const std::vector< std::string > &names) const -> Indices
Return the indices of a set of species in the system.
Definition: ChemicalSystem.cpp:313
auto formulaMatrix() const -> const Matrix &
Return the formula matrix of the system The formula matrix is defined as the matrix whose entry (j...
Definition: ChemicalSystem.cpp:200
auto numElements() const -> unsigned
Return the number of elements in the system.
Definition: ChemicalSystem.cpp:132
std::size_t Index
Define a type that represents an index.
Definition: Index.hpp:26
auto indexSpecies(std::string name) const -> Index
Return the index of a species in the system.
Definition: ChemicalSystem.cpp:221
auto indicesPhasesWithSpecies(const Indices &indices) const -> Indices
Return the index of the phase that contains a given species.
Definition: ChemicalSystem.cpp:345
auto elementAmountInPhase(Index ielement, Index iphase, const Vector &n) const -> double
Calculate the molar amounts of the elements in a given phase (in units of mol)
Definition: ChemicalSystem.cpp:415
std::function< ThermoModelResult(double, double)> ThermoModel
The signature of the chemical model function that calculates the thermodynamic properties of a system...
Definition: ChemicalSystem.hpp:39
ChemicalSystem()
Construct a default ChemicalSystem instance.
Definition: ChemicalSystem.cpp:121
auto indicesElementsInSpecies(Index index) const -> Indices
Return the indices of the elements that compose a species.
Definition: ChemicalSystem.cpp:294
auto indicesPhases(const std::vector< std::string > &names) const -> Indices
Return the indices of a set of phases in the system.
Definition: ChemicalSystem.cpp:336
auto species() const -> const std::vector< Species > &
Return the list of species in the system.
Definition: ChemicalSystem.cpp:177
auto indices(const std::vector< std::string > &names, const NamedValues &values) -> Indices
Return the indices of some entries in a container.
Definition: SetUtils.hxx:59
auto indexElement(std::string name) const -> Index
Return the index of an element in the system.
Definition: ChemicalSystem.cpp:205
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
std::function< ChemicalModelResult(double, double, const Vector &)> ChemicalModel
The signature of the chemical model function that calculates the chemical properties of a system...
Definition: ChemicalSystem.hpp:42
auto elementAmount(Index ielement, const Vector &n) const -> double
Calculate the molar amount of an elements (in units of mol)
Definition: ChemicalSystem.cpp:409
A type used to define a chemical element and its attributes.
Definition: Element.hpp:27
auto indexPhaseWithSpecies(Index index) const -> Index
Return the index of the phase that contains a given species.
Definition: ChemicalSystem.cpp:266
A class used for calculating standard thermodynamic properties of species in a chemical system...
Definition: ThermoProperties.hpp:32
auto indicesSolidPhases() const -> Indices
Return the indices of the solid phases.
Definition: ChemicalSystem.cpp:369
auto numPhases() const -> unsigned
Return the number of phases in the system.
Definition: ChemicalSystem.cpp:147
auto properties(double T, double P) const -> ThermoProperties
Calculate the standard thermodynamic properties of the species.
Definition: ChemicalSystem.cpp:434
std::vector< PhaseThermoModelResult > ThermoModelResult
The result of the thermodynamic model function that calculates the standard thermodynamic properties ...
Definition: ChemicalSystem.hpp:33