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-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 // 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 #include <Reaktoro/Thermodynamics/Models/ThermoModel.hpp>
26 #include <Reaktoro/Thermodynamics/Models/ChemicalModel.hpp>
27 
28 namespace Reaktoro {
29 
30 // Forward declarations
31 class ChemicalProperties;
32 class ThermoProperties;
33 
38 {
39 public:
42 
44  explicit ChemicalSystem(const std::vector<Phase>& phases);
45 
47  ChemicalSystem(const std::vector<Phase>& phases, const ThermoModel& thermo_model, const ChemicalModel& chemical_model);
48 
50  virtual ~ChemicalSystem();
51 
53  auto numElements() const -> unsigned;
54 
56  auto numSpecies() const -> unsigned;
57 
60  auto numSpeciesInPhase(Index iphase) const -> unsigned;
61 
63  auto numPhases() const -> unsigned;
64 
66  auto elements() const -> const std::vector<Element>&;
67 
69  auto species() const -> const std::vector<Species>&;
70 
72  auto phases() const -> const std::vector<Phase>&;
73 
75  auto thermoModel() const -> const ThermoModel&;
76 
78  auto chemicalModel() const -> const ChemicalModel&;
79 
83  auto formulaMatrix() const -> MatrixConstRef;
84 
87  auto element(Index index) const -> const Element&;
88 
91  auto element(std::string name) const -> const Element&;
92 
95  auto species(Index index) const -> const Species&;
96 
99  auto species(std::string name) const -> const Species&;
100 
103  auto phase(Index index) const -> const Phase&;
104 
107  auto phase(std::string name) const -> const Phase&;
108 
111  auto indexElement(std::string name) const -> Index;
112 
116  auto indexElementWithError(std::string name) const -> Index;
117 
120  auto indexSpecies(std::string name) const -> Index;
121 
125  auto indexSpeciesWithError(std::string name) const -> Index;
126 
130  auto indexSpeciesAny(const std::vector<std::string>& names) const -> Index;
131 
135  auto indexSpeciesAnyWithError(const std::vector<std::string>& names) const -> Index;
136 
139  auto indexPhase(std::string name) const -> Index;
140 
144  auto indexPhaseWithError(std::string name) const -> Index;
145 
148  auto indexPhaseWithSpecies(Index index) const -> Index;
149 
152  auto indexFirstSpeciesInPhase(Index iphase) const -> unsigned;
153 
156  auto indicesElements(const std::vector<std::string>& names) const -> Indices;
157 
161 
164  auto indicesElementsInSpecies(const Indices& indices) const -> Indices;
165 
168  auto indicesSpecies(const std::vector<std::string>& names) const -> Indices;
169 
172  auto indicesSpeciesInPhases(const Indices& indices) const -> Indices;
173 
176  auto indicesPhases(const std::vector<std::string>& names) const -> Indices;
177 
180  auto indicesPhasesWithSpecies(const Indices& indices) const -> Indices;
181 
183  auto indicesFluidPhases() const -> Indices;
184 
186  auto indicesFluidSpecies() const -> Indices;
187 
189  auto indicesSolidPhases() const -> Indices;
190 
192  auto indicesSolidSpecies() const -> Indices;
193 
196  auto elementAmounts(VectorConstRef n) const -> Vector;
197 
201  auto elementAmountsInPhase(Index iphase, VectorConstRef n) const -> Vector;
202 
206  auto elementAmountsInSpecies(const Indices& ispecies, VectorConstRef n) const -> Vector;
207 
211  auto elementAmount(Index ielement, VectorConstRef n) const -> double;
212 
217  auto elementAmountInPhase(Index ielement, Index iphase, VectorConstRef n) const -> double;
218 
223  auto elementAmountInSpecies(Index ielement, const Indices& ispecies, VectorConstRef n) const -> double;
224 
228  auto properties(double T, double P) const -> ThermoProperties;
229 
234  auto properties(double T, double P, VectorConstRef n) const -> ChemicalProperties;
235 
236 private:
237  struct Impl;
238 
239  std::shared_ptr<Impl> pimpl;
240 };
241 
243 auto operator<<(std::ostream& out, const ChemicalSystem& system) -> std::ostream&;
244 
245 } // namespace Reaktoro
std::function< void(ThermoModelResult &, Temperature, Pressure)> ThermoModel
The signature of the thermodynamic model function that calculates the standard thermodynamic properti...
Definition: ThermoModel.hpp:114
auto formulaMatrix() const -> MatrixConstRef
Return the formula matrix of the system The formula matrix is defined as the matrix whose entry (j,...
Definition: ChemicalSystem.cpp:260
auto indexElementWithError(std::string name) const -> Index
Return the index of an element in the system.
Definition: ChemicalSystem.cpp:270
std::function< void(ChemicalModelResult &, Temperature, Pressure, VectorConstRef)> ChemicalModel
The signature of the chemical model function that calculates the chemical properties of the species i...
Definition: ChemicalModel.hpp:132
auto properties(double T, double P) const -> ThermoProperties
Calculate the standard thermodynamic properties of the species.
Definition: ChemicalSystem.cpp:493
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
ChemicalSystem()
Construct a default ChemicalSystem instance.
Definition: ChemicalSystem.cpp:167
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
auto names(const NamedValues &values) -> std::vector< std::string >
Return the names of the entries in a container.
Definition: Utils.hxx:22
virtual ~ChemicalSystem()
Destroy this ChemicalSystem instance.
Definition: ChemicalSystem.cpp:179
auto numPhases() const -> unsigned
Return the number of phases in the system.
Definition: ChemicalSystem.cpp:197
auto indexSpecies(std::string name) const -> Index
Return the index of a species in the system.
Definition: ChemicalSystem.cpp:281
auto thermoModel() const -> const ThermoModel &
Return the thermodynamic model of the system.
Definition: ChemicalSystem.cpp:250
A class to represent a system and its attributes and properties.
Definition: ChemicalSystem.hpp:38
auto numElements() const -> unsigned
Return the number of elements in the system.
Definition: ChemicalSystem.cpp:182
auto elementAmount(Index ielement, VectorConstRef n) const -> double
Calculate the molar amount of elements (in units of mol)
Definition: ChemicalSystem.cpp:468
auto phases() const -> const std::vector< Phase > &
Return the list of phases in the system.
Definition: ChemicalSystem.cpp:245
auto elementAmountInPhase(Index ielement, Index iphase, VectorConstRef n) const -> double
Calculate the molar amounts of elements in a given phase (in units of mol)
Definition: ChemicalSystem.cpp:474
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:300
auto indicesPhasesWithSpecies(const Indices &indices) const -> Indices
Return the index of the phase that contains a given species.
Definition: ChemicalSystem.cpp:405
auto indicesSolidSpecies() const -> Indices
Return the indices of the species in the solid phases.
Definition: ChemicalSystem.cpp:438
auto numSpeciesInPhase(Index iphase) const -> unsigned
Return the number of species in a phase of the system.
Definition: ChemicalSystem.cpp:192
auto indexElement(std::string name) const -> Index
Return the index of an element in the system.
Definition: ChemicalSystem.cpp:265
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
auto chemicalModel() const -> const ChemicalModel &
Return the chemical model of the system.
Definition: ChemicalSystem.cpp:255
auto elements() const -> const std::vector< Element > &
Return the list of elements in the system.
Definition: ChemicalSystem.cpp:212
auto elementAmountsInSpecies(const Indices &ispecies, VectorConstRef n) const -> Vector
Calculate the molar amounts of the elements in a given set of species (in units of mol)
Definition: ChemicalSystem.cpp:459
auto phase(Index index) const -> const Phase &
Return a phase of the system.
Definition: ChemicalSystem.cpp:232
auto indexSpeciesWithError(std::string name) const -> Index
Return the index of a species in the system.
Definition: ChemicalSystem.cpp:286
auto numSpecies() const -> unsigned
Return the number of species in the system.
Definition: ChemicalSystem.cpp:187
A class for querying thermodynamic and chemical properties of a chemical system.
Definition: ChemicalProperties.hpp:33
auto indexFirstSpeciesInPhase(Index iphase) const -> unsigned
Return the index of the first species in a phase.
Definition: ChemicalSystem.cpp:337
auto indicesFluidSpecies() const -> Indices
Return the indices of the species in the fluid phases.
Definition: ChemicalSystem.cpp:423
A type used to define a phase and its attributes.
Definition: Phase.hpp:42
auto indexPhaseWithError(std::string name) const -> Index
Return the index of a phase in the system.
Definition: ChemicalSystem.cpp:315
auto indicesPhases(const std::vector< std::string > &names) const -> Indices
Return the indices of a set of phases in the system.
Definition: ChemicalSystem.cpp:396
auto elementAmountsInPhase(Index iphase, VectorConstRef n) const -> Vector
Calculate the molar amounts of the elements in a given phase (in units of mol)
Definition: ChemicalSystem.cpp:449
auto indicesSpeciesInPhases(const Indices &indices) const -> Indices
Return the indices of the species in a given set of phases.
Definition: ChemicalSystem.cpp:382
A class used for calculating standard thermodynamic properties of species in a chemical system.
Definition: ThermoProperties.hpp:30
auto indexPhaseWithSpecies(Index index) const -> Index
Return the index of the phase that contains a given species.
Definition: ChemicalSystem.cpp:326
A type used to define a chemical element and its attributes.
Definition: Element.hpp:28
std::size_t Index
Define a type that represents an index.
Definition: Index.hpp:26
auto indicesSolidPhases() const -> Indices
Return the indices of the solid phases.
Definition: ChemicalSystem.cpp:428
std::vector< Index > Indices
Define a type that represents a collection of indices.
Definition: Index.hpp:29
auto indicesElementsInSpecies(Index index) const -> Indices
Return the indices of the elements that compose a species.
Definition: ChemicalSystem.cpp:354
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:295
auto indicesFluidPhases() const -> Indices
Return the indices of the fluid phases.
Definition: ChemicalSystem.cpp:413
auto indexPhase(std::string name) const -> Index
Return the index of a phase in the system.
Definition: ChemicalSystem.cpp:310
Eigen::Ref< const Eigen::MatrixXd > MatrixConstRef
Alias to Eigen type Ref<const MatrixXd>.
Definition: Matrix.hpp:44
auto element(Index index) const -> const Element &
Return an element of the system.
Definition: ChemicalSystem.cpp:202
A type used to describe a species and its attributes.
Definition: Species.hpp:42
Eigen::Ref< const Eigen::VectorXd > VectorConstRef
< Alias to Eigen type Ref<VectorXd>.
Definition: Matrix.hpp:31
auto species() const -> const std::vector< Species > &
Return the list of species in the system.
Definition: ChemicalSystem.cpp:227
auto indicesSpecies(const std::vector< std::string > &names) const -> Indices
Return the indices of a set of species in the system.
Definition: ChemicalSystem.cpp:373
auto indicesElements(const std::vector< std::string > &names) const -> Indices
Return the indices of a set of elements in the system.
Definition: ChemicalSystem.cpp:345
auto elementAmounts(VectorConstRef n) const -> Vector
Calculate the molar amounts of the elements (in units of mol)
Definition: ChemicalSystem.cpp:443
auto elementAmountInSpecies(Index ielement, const Indices &ispecies, VectorConstRef n) const -> double
Calculate the molar amounts of elements in a given set of species (in units of mol)
Definition: ChemicalSystem.cpp:484