OGS 6.2.1-499-g3b941532c.dirty.20191012113459
MatrixTranslator.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <memory>
14 
15 #include "TimeDiscretization.h"
16 #include "Types.h"
17 
18 #include "MathLib/LinAlg/LinAlg.h"
19 
20 namespace NumLib
21 {
24 
31 template <ODESystemTag ODETag>
33 
39 template <>
41 {
42 public:
44  virtual void computeA(GlobalMatrix const& M, GlobalMatrix const& K,
45  GlobalMatrix& A) const = 0;
46 
48  virtual void computeRhs(const GlobalMatrix& M, const GlobalMatrix& K,
49  const GlobalVector& b, GlobalVector& rhs) const = 0;
50 
55  virtual void computeResidual(GlobalMatrix const& M, GlobalMatrix const& K,
56  GlobalVector const& b,
57  GlobalVector const& x_new_timestep,
58  GlobalVector const& xdot,
59  GlobalVector& res) const = 0;
60 
62  virtual void computeJacobian(GlobalMatrix const& Jac_in,
63  GlobalMatrix& Jac_out) const = 0;
64 
72  virtual void pushMatrices(GlobalMatrix const& /*M*/,
73  GlobalMatrix const& /*K*/,
74  GlobalVector const& /*b*/)
75  {
76  }
77 
78  virtual ~MatrixTranslator() = default;
79 };
80 
86 template <ODESystemTag ODETag>
88 
97 template <>
99  : public MatrixTranslator<ODESystemTag::FirstOrderImplicitQuasilinear>
100 {
101 public:
106  explicit MatrixTranslatorGeneral(TimeDiscretization const& timeDisc)
107  : _time_disc(timeDisc)
108  {
109  }
110 
112  void computeA(GlobalMatrix const& M, GlobalMatrix const& K,
113  GlobalMatrix& A) const override;
114 
116  void computeRhs(const GlobalMatrix& M, const GlobalMatrix& /*K*/,
117  const GlobalVector& b, GlobalVector& rhs) const override;
118 
120  void computeResidual(GlobalMatrix const& M, GlobalMatrix const& K,
121  GlobalVector const& b,
122  GlobalVector const& x_new_timestep,
123  GlobalVector const& xdot,
124  GlobalVector& res) const override;
125 
128  void computeJacobian(GlobalMatrix const& Jac_in,
129  GlobalMatrix& Jac_out) const override;
130 
131 private:
132  TimeDiscretization const&
134 
136  mutable std::size_t _tmp_id = 0u;
137 };
138 
143 template <ODESystemTag ODETag>
145 
154 template <>
156  : public MatrixTranslator<ODESystemTag::FirstOrderImplicitQuasilinear>
157 {
158 public:
163  explicit MatrixTranslatorForwardEuler(ForwardEuler const& timeDisc)
164  : _fwd_euler(timeDisc)
165  {
166  }
167 
169  void computeA(GlobalMatrix const& M, GlobalMatrix const& /*K*/,
170  GlobalMatrix& A) const override;
171 
173  void computeRhs(const GlobalMatrix& M, const GlobalMatrix& K,
174  const GlobalVector& b, GlobalVector& rhs) const override;
175 
177  void computeResidual(GlobalMatrix const& M, GlobalMatrix const& K,
178  GlobalVector const& b,
179  GlobalVector const& x_new_timestep,
180  GlobalVector const& xdot,
181  GlobalVector& res) const override;
182 
185  void computeJacobian(GlobalMatrix const& Jac_in,
186  GlobalMatrix& Jac_out) const override;
187 
188 private:
190 
192  mutable std::size_t _tmp_id = 0u;
193 };
194 
199 template <ODESystemTag ODETag>
201 
210 template <>
212  : public MatrixTranslator<ODESystemTag::FirstOrderImplicitQuasilinear>
213 {
214 public:
220  : _crank_nicolson(timeDisc),
221  _M_bar(NumLib::GlobalMatrixProvider::provider.getMatrix()),
222  _b_bar(NumLib::GlobalVectorProvider::provider.getVector())
223  {
224  }
225 
227  {
229  _M_bar);
231  _b_bar);
232  }
233 
236  void computeA(GlobalMatrix const& M, GlobalMatrix const& K,
237  GlobalMatrix& A) const override;
238 
241  void computeRhs(const GlobalMatrix& M, const GlobalMatrix& /*K*/,
242  const GlobalVector& b, GlobalVector& rhs) const override;
243 
246  void computeResidual(GlobalMatrix const& M, GlobalMatrix const& K,
247  GlobalVector const& b,
248  GlobalVector const& x_new_timestep,
249  GlobalVector const& xdot,
250  GlobalVector& res) const override;
251 
258  void computeJacobian(GlobalMatrix const& Jac_in,
259  GlobalMatrix& Jac_out) const override;
260 
272  void pushMatrices(GlobalMatrix const& M, GlobalMatrix const& K,
273  GlobalVector const& b) override;
274 
275 private:
277 
278  GlobalMatrix&
280  GlobalVector& _b_bar;
282 
285  mutable std::size_t _tmp_id = 0u;
286 };
287 
290 template <ODESystemTag ODETag>
291 std::unique_ptr<MatrixTranslator<ODETag>> createMatrixTranslator(
292  TimeDiscretization const& timeDisc)
293 {
294  if (auto* fwd_euler = dynamic_cast<ForwardEuler const*>(&timeDisc))
295  {
296  return std::unique_ptr<MatrixTranslator<ODETag>>(
297  new MatrixTranslatorForwardEuler<ODETag>(*fwd_euler));
298  }
299  if (auto* crank = dynamic_cast<CrankNicolson const*>(&timeDisc))
300  {
301  return std::unique_ptr<MatrixTranslator<ODETag>>(
303  }
304 
305  return std::unique_ptr<MatrixTranslator<ODETag>>(
306  new MatrixTranslatorGeneral<ODETag>(timeDisc));
307 }
308 
310 } // namespace NumLib
Generalized Crank-Nicolson scheme.
TimeDiscretization const & _time_disc
the time discretization used.
virtual void releaseMatrix(GlobalMatrix const &A)=0
std::unique_ptr< MatrixTranslator< ODETag > > createMatrixTranslator(TimeDiscretization const &timeDisc)
virtual void pushMatrices(GlobalMatrix const &, GlobalMatrix const &, GlobalVector const &)
static NUMLIB_EXPORT MatrixProvider & provider
static NUMLIB_EXPORT VectorProvider & provider
ODESystemTag
Tag used to specify the type of ODE.
Definition: Types.h:26
Forward Euler scheme.
virtual void releaseVector(GlobalVector const &x)=0