Reaktoro  v2.11.0
A unified framework for modeling chemically reactive systems
ChemicalSystem.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/Meta.hpp>
23 #include <Reaktoro/Common/TraitsUtils.hpp>
24 #include <Reaktoro/Common/Types.hpp>
25 #include <Reaktoro/Core/Database.hpp>
26 #include <Reaktoro/Core/Element.hpp>
27 #include <Reaktoro/Core/ElementList.hpp>
28 #include <Reaktoro/Core/Phase.hpp>
29 #include <Reaktoro/Core/PhaseList.hpp>
30 #include <Reaktoro/Core/Phases.hpp>
31 #include <Reaktoro/Core/Reaction.hpp>
32 #include <Reaktoro/Core/ReactionList.hpp>
33 #include <Reaktoro/Core/Reactions.hpp>
34 #include <Reaktoro/Core/Species.hpp>
35 #include <Reaktoro/Core/SpeciesList.hpp>
36 #include <Reaktoro/Core/Surface.hpp>
37 #include <Reaktoro/Core/SurfaceList.hpp>
38 #include <Reaktoro/Core/Surfaces.hpp>
39 
40 namespace Reaktoro {
41 
42 // Forward declarations
43 class ChemicalSystem;
44 
45 template<typename T, typename... Ts>
46 constexpr auto _arePhaseReactionOrSurfaceConvertible()
47 {
48  constexpr auto isPhaseConvertible = isBaseOf<GeneralPhase, T> || isBaseOf<GeneralPhasesGenerator, T>;
49  constexpr auto isReactionConvertible = isConvertible<T, Reaction> || isConvertible<T, GeneralReaction> || isConvertible<T, ReactionGenerator>;
50  constexpr auto isSurfaceConvertible = isConvertible<T, Surface> || isConvertible<T, GeneralSurface> || isConvertible<T, SurfaceGenerator>;
51  constexpr auto aux = isPhaseConvertible || isReactionConvertible || isSurfaceConvertible;
52 
53  if constexpr (sizeof...(Ts) > 0)
54  return aux && _arePhaseReactionOrSurfaceConvertible<Ts...>();
55  else return aux;
56 }
57 
59 template<typename T, typename... Ts>
60 constexpr auto arePhaseReactionOrSurfaceConvertible = _arePhaseReactionOrSurfaceConvertible<T, Ts...>();
61 
63 template<typename... Args>
64 auto createChemicalSystem(Database const& db, Args const&... args) -> ChemicalSystem;
65 
70 {
71 public:
74 
76  explicit ChemicalSystem(Database const& database, PhaseList const& phases);
77 
79  explicit ChemicalSystem(Database const& database, PhaseList const& phases, SurfaceList const& surfaces);
80 
82  explicit ChemicalSystem(Database const& database, PhaseList const& phases, ReactionList const& reactions);
83 
85  explicit ChemicalSystem(Database const& database, PhaseList const& phases, ReactionList const& reactions, SurfaceList const& surfaces);
86 
88  explicit ChemicalSystem(Phases const& phases);
89 
91  explicit ChemicalSystem(Phases const& phases, Surfaces const& surfaces);
92 
94  explicit ChemicalSystem(Phases const& phases, Reactions const& reactions);
95 
97  explicit ChemicalSystem(Phases const& phases, Reactions const& reactions, Surfaces const& surfaces);
98 
100  template<typename... Args, Requires<arePhaseReactionOrSurfaceConvertible<Args...>> = true>
101  explicit ChemicalSystem(Database const& db, Args const&... args)
102  : ChemicalSystem(createChemicalSystem(db, args...)) {}
103 
106  auto id() const -> Index;
107 
109  auto database() const -> Database const&;
110 
112  auto element(Index index) const -> Element const&;
113 
115  auto elements() const -> ElementList const&;
116 
118  auto species(Index index) const -> Species const&;
119 
121  auto species() const -> SpeciesList const&;
122 
124  auto phase(Index index) const -> Phase const&;
125 
127  auto phases() const -> PhaseList const&;
128 
130  auto reaction(Index index) const -> Reaction const&;
131 
133  auto reactions() const -> ReactionList const&;
134 
136  auto surface(Index index) const -> Surface const&;
137 
139  auto surfaces() const -> SurfaceList const&;
140 
145  auto formulaMatrix() const -> MatrixXdConstRef;
146 
148  auto formulaMatrixElements() const -> MatrixXdConstRef;
149 
151  auto formulaMatrixCharge() const -> MatrixXdConstRef;
152 
156  auto stoichiometricMatrix() const -> MatrixXdConstRef;
157 
158 private:
159  struct Impl;
160 
161  SharedPtr<Impl> pimpl;
162 };
163 
165 auto operator<<(std::ostream& out, ChemicalSystem const& system) -> std::ostream&;
166 
167 template<typename... Args>
168 auto createChemicalSystem(Database const& db, Args const&... args) -> ChemicalSystem
169 {
170  Phases phases(db);
171  Reactions reactions;
172  Surfaces surfaces;
173 
174  ForEach([&](auto arg) constexpr {
175  using T = Decay<decltype(arg)>;
176  constexpr auto isPhaseConvertible = isBaseOf<GeneralPhase, T> || isBaseOf<GeneralPhasesGenerator, T>;
177  constexpr auto isReactionConvertible = isConvertible<T, Reaction> || isConvertible<T, GeneralReaction> || isConvertible<T, ReactionGenerator>;
178  constexpr auto isSurfaceConvertible = isConvertible<T, Surface> || isConvertible<T, GeneralSurface> || isConvertible<T, SurfaceGenerator>;
179 
180  static_assert(isPhaseConvertible || isReactionConvertible || isSurfaceConvertible, "One of the arguments in your list of arguments for the construction of a ChemicalSystem object has a non-convertible type to either phase, reaction, or surface.");
181 
182  if constexpr(isPhaseConvertible) phases.add(arg);
183  else if constexpr(isReactionConvertible) reactions.add(arg);
184  else surfaces.add(arg);
185  }, args...);
186 
187  return ChemicalSystem(phases, reactions, surfaces);
188 }
189 
190 } // namespace Reaktoro
The class used to represent a chemical system and its attributes and properties.
Definition: ChemicalSystem.hpp:70
ChemicalSystem(Database const &database, PhaseList const &phases)
Construct a ChemicalSystem object with given database and phases.
ChemicalSystem(Phases const &phases, Reactions const &reactions, Surfaces const &surfaces)
Construct a ChemicalSystem object with given phases, reactions, and surfaces.
ChemicalSystem(Phases const &phases, Surfaces const &surfaces)
Construct a ChemicalSystem object with given phases and surfaces.
ChemicalSystem(Database const &database, PhaseList const &phases, ReactionList const &reactions, SurfaceList const &surfaces)
Construct a ChemicalSystem object with given database, phases, reactions, and surfaces.
auto id() const -> Index
Return the unique identification number of this ChemicalSystem object.
ChemicalSystem(Phases const &phases)
Construct a ChemicalSystem object with given phases.
ChemicalSystem(Database const &database, PhaseList const &phases, ReactionList const &reactions)
Construct a ChemicalSystem object with given database, phases, and reactions.
ChemicalSystem()
Construct a default uninitialized ChemicalSystem object.
ChemicalSystem(Database const &db, Args const &... args)
Construct a ChemicalSystem object with given database and a list of phase and reaction convertible ob...
Definition: ChemicalSystem.hpp:101
ChemicalSystem(Database const &database, PhaseList const &phases, SurfaceList const &surfaces)
Construct a ChemicalSystem object with given database, phases, and surfaces.
ChemicalSystem(Phases const &phases, Reactions const &reactions)
Construct a ChemicalSystem object with given phases and reactions.
The class used to store and retrieve data of chemical species.
Definition: Database.hpp:32
A type used as a collection of elements.
Definition: ElementList.hpp:28
A type used to define a element and its attributes.
Definition: Element.hpp:28
A type used as a collection of phases.
Definition: PhaseList.hpp:29
A type used to define a phase and its attributes.
Definition: Phase.hpp:33
The class used to define the phases that will constitute the chemical system of interest.
Definition: Phases.hpp:282
auto add(GeneralPhase const &phase) -> void
Add a GeneralPhase object into the Phases container.
A type used as a collection of reactions.
Definition: ReactionList.hpp:29
A class to represent a reaction and its attributes.
Definition: Reaction.hpp:42
Used to represent a collection of reactions controlled kinetically.
Definition: Reactions.hpp:128
auto add(T const &item) -> void
Add a reaction generator into the Reactions container.
Definition: Reactions.hpp:144
A type used as a collection of species.
Definition: SpeciesList.hpp:29
A type used to represent a chemical species and its attributes.
Definition: Species.hpp:35
A type used as a collection of surfaces.
Definition: SurfaceList.hpp:29
Used to represent a surface across which chemical reactions take place.
Definition: Surface.hpp:31
Used to represent a collection of surfaces across which chemical reactions take place.
Definition: Surfaces.hpp:77
auto add(T const &item) -> void
Add a surface generator into the Surfaces container.
Definition: Surfaces.hpp:93
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
auto createChemicalSystem(Database const &db, Args const &... args) -> ChemicalSystem
Create a ChemicalSystem object with given database and a list of phase and reaction convertible objec...
Definition: ChemicalSystem.hpp:168
std::size_t Index
Define a type that represents an index.
Definition: Index.hpp:26
constexpr auto arePhaseReactionOrSurfaceConvertible
Used to determine if T and all types in Ts are either GeneralPhase or GeneralPhaseGenerator.
Definition: ChemicalSystem.hpp:60
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
constexpr auto ForEach(Function const &f) -> void
Generate evaluation statements f(arg0); f(arg1); ...; f(argn); at compile time.
Definition: Meta.hpp:62
Eigen::Ref< const MatrixXd > MatrixXdConstRef
Convenient alias to Eigen type.
Definition: Matrix.hpp:139
std::shared_ptr< T > SharedPtr
Convenient alias for std::shared_ptr<T>.
Definition: Types.hpp:106