Reaktoro  v2.11.0
A unified framework for modeling chemically reactive systems
Reactions.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/TraitsUtils.hpp>
22 #include <Reaktoro/Common/Types.hpp>
23 #include <Reaktoro/Core/Reaction.hpp>
24 #include <Reaktoro/Core/ReactionRateModel.hpp>
25 
26 namespace Reaktoro {
27 
28 class Database;
29 class PhaseList;
30 class SpeciesList;
31 class SurfaceList;
32 
37 {
40 
43 
45  PhaseList const& phases;
46 
49 };
50 
54 
58 {
59 public:
62 
65  explicit GeneralReaction(String const& equation);
66 
68  auto setName(String const& name) -> GeneralReaction&;
69 
83 
86 
91  auto setRateModel(ReactionRateModelGenerator const& model_generator) -> GeneralReaction&;
92 
94  auto set(ReactionRateModelGenerator const& model_generator) -> GeneralReaction&;
95 
97  auto name() const -> String const&;
98 
100  auto equation() const -> String const&;
101 
103  auto rateModel() const -> ReactionRateModel const&;
104 
107 
109  auto operator()(ReactionGeneratorArgs args) const -> Reaction;
110 
111 private:
113  String reaction_name;
114 
116  String reaction_equation;
117 
119  ReactionRateModel rate_model;
120 
122  ReactionRateModelGenerator rate_model_generator;
123 };
124 
128 {
129 public:
132 
135  template<typename... ReactionConvertible>
136  explicit Reactions(ReactionConvertible const&... reactions)
137  {
138  static_assert(sizeof...(reactions) > 0);
139  addAux(reactions...);
140  }
141 
143  template<typename T>
144  auto add(T const& item) -> void
145  {
146  static_assert(
147  isConvertible<T, Reaction> ||
148  isConvertible<T, GeneralReaction> ||
149  isConvertible<T, ReactionGenerator>);
150 
151  if constexpr(isConvertible<T, Reaction>)
152  {
153  reaction_generators.push_back([=](ReactionGeneratorArgs args) -> Vec<Reaction> { return { item }; }); // item is a Reaction object
154  }
155  else if constexpr(isConvertible<T, GeneralReaction>)
156  {
157  reaction_generators.push_back([=](ReactionGeneratorArgs args) -> Vec<Reaction> { return { item(args) }; }); // item is a GeneralReaction object; use operator()(ReactionGeneratorArgs) to convert to Reaction
158  }
159  else
160  {
161  reaction_generators.push_back(item); // item is already a ReactionGenerator
162  errorif(!reaction_generators.back(), "Expecting an object in Reactions::add call that produces an initialized ReactionGenerator function object.");
163  }
164  }
165 
169 
170 private:
172  Vec<ReactionGenerator> reaction_generators;
173 
175  template<typename Arg, typename... Args>
176  auto addAux(const Arg& arg, const Args&... args) -> void
177  {
178  add(arg);
179  if constexpr (sizeof...(Args) > 0)
180  addAux(args...);
181  }
182 };
183 
184 } // namespace Reaktoro
The class used to store and retrieve data of chemical species.
Definition: Database.hpp:32
Used to define a general reaction.
Definition: Reactions.hpp:58
auto setName(String const &name) -> GeneralReaction &
Set the unique name of the reaction.
auto setRateModel(ReactionRateModelGenerator const &model_generator) -> GeneralReaction &
Set the reaction rate model generator of the reaction.
auto set(ReactionRateModelGenerator const &model_generator) -> GeneralReaction &
Set the reaction rate model generator of the reaction (equivalent to GeneralReaction::setRateModel).
GeneralReaction()
Construct a default GeneralReaction object.
auto rateModelGenerator() const -> ReactionRateModelGenerator const &
Return the reaction rate model generator of the reaction.
auto setRateModel(ReactionRateModel const &model) -> GeneralReaction &
Set the reaction rate model of the reaction.
GeneralReaction(String const &equation)
Construct a GeneralReaction object with given reaction equation as a formatted string.
auto setEquation(String const &equation) -> GeneralReaction &
Set the equation of the reaction as a formatted string.
auto name() const -> String const &
Return the name of the reaction.
auto rateModel() const -> ReactionRateModel const &
Return the reaction rate model of the reaction.
auto equation() const -> String const &
Return the reaction equation of the reaction.
A type used as a collection of phases.
Definition: PhaseList.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
Reactions(ReactionConvertible const &... reactions)
Construct a Reactions object with given Reaction, GeneralReaction, or ReactionGenerator objects.
Definition: Reactions.hpp:136
auto add(T const &item) -> void
Add a reaction generator into the Reactions container.
Definition: Reactions.hpp:144
auto convert(ReactionGeneratorArgs args) const -> Vec< Reaction >
Convert this Reactions object into a vector of Reaction objects.
Reactions()
Construct a Reactions object.
A type used as a collection of species.
Definition: SpeciesList.hpp:29
A type used as a collection of surfaces.
Definition: SurfaceList.hpp:29
#define errorif(condition,...)
Define a macro to raise a runtime exception if condition is true.
Definition: Exception.hpp:140
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
Fn< Vec< Reaction >(ReactionGeneratorArgs args)> ReactionGenerator
The function type for the generation of reactions with given species in the chemical system.
Definition: Reactions.hpp:53
Fn< ReactionRateModel(ReactionRateModelGeneratorArgs args)> ReactionRateModelGenerator
The function signature for functions that generates a ReactionRateModel for a reaction.
Definition: ReactionRateModel.hpp:67
std::vector< T > Vec
Convenient alias for std::vector<T>.
Definition: Types.hpp:66
std::string String
Convenient alias for std::string.
Definition: Types.hpp:52
std::function< F > Fn
Convenient alias for std::function<R(Args...)>.
Definition: Types.hpp:110
The data provided to a ReactionGenerator to construct a Reaction object.
Definition: Reactions.hpp:37
PhaseList const & phases
The phases in the chemical system where the reaction belongs to.
Definition: Reactions.hpp:45
SurfaceList const & surfaces
The surfaces in the chemical system where the reaction belongs to.
Definition: Reactions.hpp:48
SpeciesList const & species
The species in the chemical system where the reaction belongs to.
Definition: Reactions.hpp:42
Database const & database
The thermodynamic database used to construct the chemical system where the reaction belongs to.
Definition: Reactions.hpp:39