Reaktoro
A unified framework for modeling chemically reactive systems
TransportSolver.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 // C++ includes
21 #include <memory>
22 #include <vector>
23 
24 // Reaktoro includes
25 #include <Reaktoro/Common/Index.hpp>
26 #include <Reaktoro/Common/StringList.hpp>
27 #include <Reaktoro/Core/ChemicalOutput.hpp>
28 #include <Reaktoro/Core/ChemicalProperties.hpp>
29 #include <Reaktoro/Core/ChemicalState.hpp>
30 #include <Reaktoro/Core/ChemicalSystem.hpp>
31 #include <Reaktoro/Equilibrium/EquilibriumSolver.hpp>
32 #include <Reaktoro/Math/Matrix.hpp>
33 
34 namespace Reaktoro {
35 
36 // Forward declarations
37 //class ChemicalProperties;
38 //class ChemicalState;
39 //class ChemicalSystem;
40 
41 //class BoundaryState
42 //{
43 //public:
44 // BoundaryState(const ChemicalState& state);
45 //
46 //private:
47 //};
48 //
49 //class BoundaryFlux
50 //{
51 //public:
52 // BoundaryFlux(const ChemicalState& state);
53 //
54 //private:
55 //};
56 
57 
59 {
60 public:
61  using Iterator = std::vector<ChemicalState>::iterator;
62 
63  using ConstIterator = std::vector<ChemicalState>::const_iterator;
64 
65  ChemicalField(Index size, const ChemicalSystem& system);
66 
67  ChemicalField(Index size, const ChemicalState& state);
68 
69  auto size() const -> Index { return m_size; }
70 
71  auto begin() const -> ConstIterator { return m_states.cbegin(); }
72 
73  auto begin() -> Iterator { return m_states.begin(); }
74 
75  auto end() const -> ConstIterator { return m_states.cend(); }
76 
77  auto end() -> Iterator { return m_states.end(); }
78 
79  auto operator[](Index index) const -> const ChemicalState& { return m_states[index]; }
80 
81  auto operator[](Index index) -> ChemicalState& { return m_states[index]; }
82 
83  auto set(const ChemicalState& state) -> void;
84 
85  auto temperature(VectorRef values) -> void;
86 
87  auto pressure(VectorRef values) -> void;
88 
89  auto elementAmounts(VectorRef values) -> void;
90 
91  auto output(std::string filename, StringList quantities) -> void;
92 
93 private:
95  Index m_size;
96 
97 // Vector temperatures;
98 //
99 // Vector pressures;
100 //
101 // /// The matrix of amounts for every element (
102 // Matrix element_amounts;
103 
105  ChemicalSystem m_system;
106 
108  std::vector<ChemicalState> m_states;
109 
111  std::vector<ChemicalProperties> m_properties;
112 };
113 
119 {
120 public:
122 
123  TridiagonalMatrix(Index size) : m_size(size), m_data(size * 3) {}
124 
125  auto size() const -> Index { return m_size; }
126 
127  auto data() -> VectorRef { return m_data; }
128 
129  auto data() const -> VectorConstRef { return m_data; }
130 
131  auto row(Index index) -> VectorRef { return m_data.segment(3 * index, 3); }
132 
133  auto row(Index index) const -> VectorConstRef { return m_data.segment(3 * index, 3); }
134 
135  auto a() -> VectorStridedRef { return Vector::Map(m_data.data() + 3, size() - 1, Eigen::InnerStride<3>()); }
136 
137  auto a() const -> VectorConstRef { return Vector::Map(m_data.data() + 3, size() - 1, Eigen::InnerStride<3>()); }
138 
139  auto b() -> VectorStridedRef { return Vector::Map(m_data.data() + 1, size(), Eigen::InnerStride<3>()); }
140 
141  auto b() const -> VectorConstRef { return Vector::Map(m_data.data() + 1, size(), Eigen::InnerStride<3>()); }
142 
143  auto c() -> VectorStridedRef { return Vector::Map(m_data.data() + 2, size() - 1, Eigen::InnerStride<3>()); }
144 
145  auto c() const -> VectorConstRef { return Vector::Map(m_data.data() + 2, size() - 1, Eigen::InnerStride<3>()); }
146 
147  auto resize(Index size) -> void;
148 
150  auto factorize() -> void;
151 
153  auto solve(VectorRef x, VectorConstRef b) const -> void;
154 
157  auto solve(VectorRef x) const -> void;
158 
159  operator Matrix() const;
160 
161 private:
163  Index m_size;
164 
166  Vector m_data;
167 };
168 
170 class Mesh
171 {
172 public:
173  Mesh();
174 
175  Mesh(Index num_cells, double xl = 0.0, double xr = 1.0);
176 
177  auto setDiscretization(Index num_cells, double xl = 0.0, double xr = 1.0) -> void;
178 
179  auto numCells() const -> Index { return m_num_cells; }
180 
181  auto xl() const -> double { return m_xl; }
182 
183  auto xr() const -> double { return m_xr; }
184 
185  auto dx() const -> double { return m_dx; }
186 
187  auto xcells() const -> VectorConstRef { return m_xcells; }
188 
189 private:
191  Index m_num_cells = 10;
192 
194  double m_xl = 0.0;
195 
197  double m_xr = 1.0;
198 
200  double m_dx = 0.1;
201 
203  Vector m_xcells;
204 };
205 
212 {
213 public:
215  TransportSolver();
216 
218  auto setMesh(const Mesh& mesh) -> void { mesh_ = mesh; }
219 
222  auto setVelocity(double val) -> void { velocity = val; }
223 
226  auto setDiffusionCoeff(double val) -> void { diffusion = val; }
227 
230  auto setBoundaryValue(double val) -> void { ul = val; };
231 
233  auto setTimeStep(double val) -> void { dt = val; }
234 
236  auto mesh() const -> const Mesh& { return mesh_; }
237 
240  auto initialize() -> void;
241 
248  auto step(VectorRef u, VectorConstRef q) -> void;
249 
252  auto step(VectorRef u) -> void;
253 
254 private:
256  Mesh mesh_;
257 
259  double dt = 0.0;
260 
262  double velocity = 0.0;
263 
265  double diffusion = 0.0;
266 
268  double ul;
269 
272 
274  Vector phi;
275 
277  Vector u0;
278 };
279 
282 {
283 public:
286 
287  auto setMesh(const Mesh& mesh) -> void;
288 
289  auto setVelocity(double val) -> void;
290 
291  auto setDiffusionCoeff(double val) -> void;
292 
293  auto setBoundaryState(const ChemicalState& state) -> void;
294 
295  auto setTimeStep(double val) -> void;
296 
297  auto system() const -> const ChemicalSystem& { return system_; }
298 
299  auto output() -> ChemicalOutput;
300 
301  auto initialize(const ChemicalField& field) -> void;
302 
303  auto step(ChemicalField& field) -> void;
304 
305 private:
307  ChemicalSystem system_;
308 
310  TransportSolver transportsolver;
311 
313  EquilibriumSolver equilibriumsolver;
314 
316  std::vector<ChemicalOutput> outputs;
317 
319  Vector bbc;
320 
322  Matrix bf;
323 
325  Matrix bs;
326 
328  Matrix b;
329 
331  Index steps = 0;
332 };
333 
334 } // namespace Reaktoro
A class for representing a list of strings with special constructors.
Definition: StringList.hpp:28
auto mesh() const -> const Mesh &
Return the mesh.
Definition: TransportSolver.hpp:236
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
Definition: TransportSolver.hpp:59
A class to represent a system and its attributes and properties.
Definition: ChemicalSystem.hpp:38
A class that defines a Tridiagonal Matrix used on TransportSolver.
Definition: TransportSolver.hpp:119
Provides a computational representation of the state of a multiphase chemical system.
Definition: ChemicalState.hpp:61
auto setVelocity(double val) -> void
Set the velocity for the transport problem.
Definition: TransportSolver.hpp:222
auto solve(VectorRef x, VectorConstRef b) const -> void
Solve a linear system Ax = b with LU decomposition.
Definition: TransportSolver.cpp:120
A class for solving advection-diffusion problem.
Definition: TransportSolver.hpp:212
Use this class for solving reactive transport problems.
Definition: TransportSolver.hpp:282
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
A solver class for solving chemical equilibrium calculations.
Definition: EquilibriumSolver.hpp:38
ReactiveTransportSolver(const ChemicalSystem &system)
Construct a default ReactiveTransportSolver instance.
Definition: TransportSolver.cpp:335
A type used to output sequence of chemical states to a file or terminal.
Definition: ChemicalOutput.hpp:36
auto setDiffusionCoeff(double val) -> void
Set the diffusion coefficient for the transport problem.
Definition: TransportSolver.hpp:226
auto setTimeStep(double val) -> void
Set the time step for the numerical solution of the transport problem.
Definition: TransportSolver.hpp:233
auto setMesh(const Mesh &mesh) -> void
Set the mesh for the numerical solution of the transport problem.
Definition: TransportSolver.hpp:218
std::size_t Index
Define a type that represents an index.
Definition: Index.hpp:26
A class that defines the mesh for TransportSolver.
Definition: TransportSolver.hpp:171
TransportSolver()
Construct a default TransportSolver instance.
Definition: TransportSolver.cpp:194
Eigen::MatrixXd Matrix
Alias to Eigen type MatrixXd.
Definition: Matrix.hpp:42
auto initialize() -> void
Initialize the transport solver before method step is executed.
Definition: TransportSolver.cpp:249
Eigen::Ref< Eigen::VectorXd > VectorRef
< Alias to Eigen type VectorXd.
Definition: Matrix.hpp:29
Eigen::Ref< Vector, 0, Eigen::InnerStride<> > VectorStridedRef
< Alias to Eigen type Ref<VectorXd>.
Definition: Matrix.hpp:30
auto step(VectorRef u, VectorConstRef q) -> void
Step the transport solver.
Definition: TransportSolver.cpp:277
auto setBoundaryValue(double val) -> void
Set the value of the variable on the boundary.
Definition: TransportSolver.hpp:230
Eigen::Ref< const Eigen::VectorXd > VectorConstRef
< Alias to Eigen type Ref<VectorXd>.
Definition: Matrix.hpp:31
auto factorize() -> void
Factorize the tridiagonal matrix to help with linear system A x = b.
Definition: TransportSolver.cpp:100