Reaktoro 
A unified framework for modeling chemically reactive systems
ChemicalSolver.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 // C++ includes
21 #include <memory>
22 #include <string>
23 
24 // Reaktoro includes
25 #include <Reaktoro/Common/Index.hpp>
26 #include <Reaktoro/Math/Matrix.hpp>
27 
28 namespace Reaktoro {
29 
30 // Forward declarations
31 class ChemicalField;
32 class ChemicalSystem;
33 class ChemicalState;
34 class Partition;
35 class ReactionSystem;
36 
39 {
40 public:
41  // Forward declaration of the Array type.
42  template<typename T>
43  class Array;
44 
45  // Forward declaration of the Grid type.
46  template<typename T>
47  class Grid;
48 
51 
53  ChemicalSolver(const ChemicalSystem& system, Index npoints);
54 
56  ChemicalSolver(const ReactionSystem& reactions, Index npoints);
57 
59  auto numPoints() const -> Index;
60 
62  auto numEquilibriumElements() const -> Index;
63 
65  auto numKineticSpecies() const -> Index;
66 
68  auto numComponents() const -> Index;
69 
71  auto setPartition(const Partition& partition) -> void;
72 
75  auto setStates(const ChemicalState& state) -> void;
76 
79  auto setStates(const Array<ChemicalState>& states) -> void;
80 
84  auto setStateAt(Index ipoint, const ChemicalState& state) -> void;
85 
89  auto setStateAt(const Array<Index>& ipoints, const ChemicalState& state) -> void;
90 
94  auto setStateAt(const Array<Index>& ipoints, const Array<ChemicalState>& states) -> void;
95 
97  auto equilibrate(Array<double> T, Array<double> P, Array<double> be) -> void;
98 
100  auto equilibrate(Array<double> T, Array<double> P, Grid<double> be) -> void;
101 
103  auto react(double t, double dt) -> void;
104 
106  auto state(Index i) const -> const ChemicalState&;
107 
109  auto states() const -> const std::vector<ChemicalState>&;
110 
112  auto componentAmounts() -> const std::vector<Vector>&;
113 
115  auto equilibriumSpeciesAmounts() -> const std::vector<ChemicalField>&;
116 
118  auto porosity() -> const ChemicalField&;
119 
121  auto fluidSaturations() -> const std::vector<ChemicalField>&;
122 
124  auto fluidDensities() -> const std::vector<ChemicalField>&;
125 
127  auto fluidVolumes() -> const std::vector<ChemicalField>&;
128 
130  auto fluidTotalVolume() -> const ChemicalField&;
131 
133  auto solidTotalVolume() -> const ChemicalField&;
134 
136  auto componentRates() -> const std::vector<ChemicalField>&;
137 
138 private:
139  struct Impl;
140 
141  std::shared_ptr<Impl> pimpl;
142 
143 public:
145  template<typename T>
146  class Array
147  {
148  public:
151  {}
152 
154  Array(const T* data, Index size)
155  : data(data), size(size) {}
156 
159  template<typename VectorType>
160  Array(const VectorType& vec)
161  : data(vec.data()), size(vec.size()) {}
162 
163  friend class ChemicalSolver;
164 
165  private:
167  const T* data = nullptr;
168 
170  Index size = 0;
171  };
172 
174  template<typename T>
175  class Grid
176  {
177  public:
180  {}
181 
183  Grid(const T** data, Index rows, Index cols)
184  : data(data), rows(rows), cols(cols) {}
185 
188  template<typename VectorType>
189  Grid(const std::vector<VectorType>& vec)
190  {
191  pointers.reserve(vec.size());
192  for(const VectorType& v : vec)
193  pointers.push_back(v.data());
194  rows = vec.size();
195  cols = vec.front().size();
196  }
197 
198  friend class ChemicalSolver;
199 
200  private:
202  const T** data = nullptr;
203 
205  std::vector<const T*> pointers;
206 
208  Index rows = 0;
209 
211  Index cols = 0;
212  };
213 };
214 
215 } // namespace Reaktoro
216 
Grid(const T **data, Index rows, Index cols)
Construct a custom Grid instance.
Definition: ChemicalSolver.hpp:183
auto setStates(const ChemicalState &state) -> void
Set the chemical state of all field points uniformly.
Definition: ChemicalSolver.cpp:520
auto numKineticSpecies() const -> Index
Return the number of kinetic species.
Definition: ChemicalSolver.cpp:505
auto react(double t, double dt) -> void
React the chemical state at every field point.
Definition: ChemicalSolver.cpp:574
auto componentRates() -> const std::vector< ChemicalField > &
Return the kinetic rates of the chemical components at every field point (in units of mol/s)...
Definition: ChemicalSolver.cpp:637
auto fluidTotalVolume() -> const ChemicalField &
Return the total volume of the fluid phases at every field point (in units of m3).
Definition: ChemicalSolver.cpp:625
auto fluidSaturations() -> const std::vector< ChemicalField > &
Return the saturations of the fluid phases at every field point.
Definition: ChemicalSolver.cpp:607
A class to represent a system and its attributes and properties.
Definition: ChemicalSystem.hpp:47
auto numComponents() const -> Index
Return the number of chemical components.
Definition: ChemicalSolver.cpp:510
STL namespace.
A type that contains the values of a scalar field and its derivatives.
Definition: ChemicalField.hpp:31
auto numEquilibriumElements() const -> Index
Return the number of equilibrium elements.
Definition: ChemicalSolver.cpp:500
A type that represents a two-dimensional array of values.
Definition: ChemicalSolver.hpp:47
Grid()
Construct a default Grid instance.
Definition: ChemicalSolver.hpp:179
Array()
Construct a default Array instance.
Definition: ChemicalSolver.hpp:150
auto fluidVolumes() -> const std::vector< ChemicalField > &
Return the volumes of the fluid phases at every field point (in units of m3).
Definition: ChemicalSolver.cpp:619
ChemicalSolver()
Construct a default ChemicalSolver instance.
Definition: ChemicalSolver.cpp:483
A type that describes a solver for many chemical calculations.
Definition: ChemicalSolver.hpp:38
Eigen::VectorXd Vector
Define an alias to the vector type of the Eigen library.
Definition: Matrix.hpp:384
A class that represents a system of chemical reactions.
Definition: ReactionSystem.hpp:41
A type that represents a one-dimensional array of values.
Definition: ChemicalSolver.hpp:43
Provide a computational representation of the Partition of a chemical system.
Definition: Partition.hpp:55
auto equilibrate(Array< double > T, Array< double > P, Array< double > be) -> void
Equilibrate the chemical state at every field point.
Definition: ChemicalSolver.cpp:564
std::size_t Index
Define a type that represents an index.
Definition: Index.hpp:26
auto fluidDensities() -> const std::vector< ChemicalField > &
Return the densities of the fluid phases at every field point (in units of kg/m3).
Definition: ChemicalSolver.cpp:613
auto porosity() -> const ChemicalField &
Return the porosity at every field point.
Definition: ChemicalSolver.cpp:601
Provides a computational representation of the state of a multiphase chemical system.
Definition: ChemicalState.hpp:60
Array(const VectorType &vec)
Construct an Array instance from a vector-like instance.
Definition: ChemicalSolver.hpp:160
auto numPoints() const -> Index
Return the number of field points.
Definition: ChemicalSolver.cpp:495
auto solidTotalVolume() -> const ChemicalField &
Return the total volume of the solid phases at every field point (in units of m3).
Definition: ChemicalSolver.cpp:631
Grid(const std::vector< VectorType > &vec)
Construct an Grid instance from a vector-like instance.
Definition: ChemicalSolver.hpp:189
auto states() const -> const std::vector< ChemicalState > &
Return the chemical states at all field points.
Definition: ChemicalSolver.cpp:584
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
auto setPartition(const Partition &partition) -> void
Set the partitioning of the chemical system.
Definition: ChemicalSolver.cpp:515
auto componentAmounts() -> const std::vector< Vector > &
Return the molar amounts of the chemical components at every field point (in units of mol)...
Definition: ChemicalSolver.cpp:589
auto equilibriumSpeciesAmounts() -> const std::vector< ChemicalField > &
Return the molar amounts of each equilibrium species and their derivatives at every field point...
Definition: ChemicalSolver.cpp:595
auto state(Index i) const -> const ChemicalState &
Return the chemical state at given index.
Definition: ChemicalSolver.cpp:579
Array(const T *data, Index size)
Construct a custom Array instance.
Definition: ChemicalSolver.hpp:154
auto setStateAt(Index ipoint, const ChemicalState &state) -> void
Set the chemical state at a specified field point.
Definition: ChemicalSolver.cpp:535