OGS 6.2.0-97-g4a610c866
MatrixTranslator.h
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include <memory>
13 
14 #include "TimeDiscretization.h"
15 #include "Types.h"
16 
17 #include "MathLib/LinAlg/LinAlg.h"
18 
19 namespace NumLib
20 {
23 
30 template <ODESystemTag ODETag>
32 
38 template <>
40 {
41 public:
43  virtual void computeA(GlobalMatrix const& M, GlobalMatrix const& K,
44  GlobalMatrix& A) const = 0;
45 
47  virtual void computeRhs(const GlobalMatrix& M, const GlobalMatrix& K,
48  const GlobalVector& b, GlobalVector& rhs) const = 0;
49 
54  virtual void computeResidual(GlobalMatrix const& M, GlobalMatrix const& K,
55  GlobalVector const& b,
56  GlobalVector const& x_new_timestep,
57  GlobalVector const& xdot,
58  GlobalVector& res) const = 0;
59 
61  virtual void computeJacobian(GlobalMatrix const& Jac_in,
62  GlobalMatrix& Jac_out) const = 0;
63 
71  virtual void pushMatrices(GlobalMatrix const& /*M*/,
72  GlobalMatrix const& /*K*/,
73  GlobalVector const& /*b*/)
74  {
75  }
76 
77  virtual ~MatrixTranslator() = default;
78 };
79 
85 template <ODESystemTag ODETag>
87 
96 template <>
98  : public MatrixTranslator<ODESystemTag::FirstOrderImplicitQuasilinear>
99 {
100 public:
105  explicit MatrixTranslatorGeneral(TimeDiscretization const& timeDisc)
106  : _time_disc(timeDisc)
107  {
108  }
109 
111  void computeA(GlobalMatrix const& M, GlobalMatrix const& K,
112  GlobalMatrix& A) const override;
113 
115  void computeRhs(const GlobalMatrix& M, const GlobalMatrix& /*K*/,
116  const GlobalVector& b, GlobalVector& rhs) const override;
117 
119  void computeResidual(GlobalMatrix const& M, GlobalMatrix const& K,
120  GlobalVector const& b,
121  GlobalVector const& x_new_timestep,
122  GlobalVector const& xdot,
123  GlobalVector& res) const override;
124 
127  void computeJacobian(GlobalMatrix const& Jac_in,
128  GlobalMatrix& Jac_out) const override;
129 
130 private:
131  TimeDiscretization const&
133 
135  mutable std::size_t _tmp_id = 0u;
136 };
137 
142 template <ODESystemTag ODETag>
144 
153 template <>
155  : public MatrixTranslator<ODESystemTag::FirstOrderImplicitQuasilinear>
156 {
157 public:
162  explicit MatrixTranslatorForwardEuler(ForwardEuler const& timeDisc)
163  : _fwd_euler(timeDisc)
164  {
165  }
166 
168  void computeA(GlobalMatrix const& M, GlobalMatrix const& /*K*/,
169  GlobalMatrix& A) const override;
170 
172  void computeRhs(const GlobalMatrix& M, const GlobalMatrix& K,
173  const GlobalVector& b, GlobalVector& rhs) const override;
174 
176  void computeResidual(GlobalMatrix const& M, GlobalMatrix const& K,
177  GlobalVector const& b,
178  GlobalVector const& x_new_timestep,
179  GlobalVector const& xdot,
180  GlobalVector& res) const override;
181 
184  void computeJacobian(GlobalMatrix const& Jac_in,
185  GlobalMatrix& Jac_out) const override;
186 
187 private:
189 
191  mutable std::size_t _tmp_id = 0u;
192 };
193 
198 template <ODESystemTag ODETag>
200 
209 template <>
211  : public MatrixTranslator<ODESystemTag::FirstOrderImplicitQuasilinear>
212 {
213 public:
219  : _crank_nicolson(timeDisc),
220  _M_bar(NumLib::GlobalMatrixProvider::provider.getMatrix()),
221  _b_bar(NumLib::GlobalVectorProvider::provider.getVector())
222  {
223  }
224 
226  {
228  _M_bar);
230  _b_bar);
231  }
232 
235  void computeA(GlobalMatrix const& M, GlobalMatrix const& K,
236  GlobalMatrix& A) const override;
237 
240  void computeRhs(const GlobalMatrix& M, const GlobalMatrix& /*K*/,
241  const GlobalVector& b, GlobalVector& rhs) const override;
242 
245  void computeResidual(GlobalMatrix const& M, GlobalMatrix const& K,
246  GlobalVector const& b,
247  GlobalVector const& x_new_timestep,
248  GlobalVector const& xdot,
249  GlobalVector& res) const override;
250 
257  void computeJacobian(GlobalMatrix const& Jac_in,
258  GlobalMatrix& Jac_out) const override;
259 
271  void pushMatrices(GlobalMatrix const& M, GlobalMatrix const& K,
272  GlobalVector const& b) override;
273 
274 private:
276 
277  GlobalMatrix&
279  GlobalVector& _b_bar;
281 
284  mutable std::size_t _tmp_id = 0u;
285 };
286 
289 template <ODESystemTag ODETag>
290 std::unique_ptr<MatrixTranslator<ODETag>> createMatrixTranslator(
291  TimeDiscretization const& timeDisc)
292 {
293  if (auto* fwd_euler = dynamic_cast<ForwardEuler const*>(&timeDisc))
294  {
295  return std::unique_ptr<MatrixTranslator<ODETag>>(
296  new MatrixTranslatorForwardEuler<ODETag>(*fwd_euler));
297  }
298  if (auto* crank = dynamic_cast<CrankNicolson const*>(&timeDisc))
299  {
300  return std::unique_ptr<MatrixTranslator<ODETag>>(
302  }
303 
304  return std::unique_ptr<MatrixTranslator<ODETag>>(
305  new MatrixTranslatorGeneral<ODETag>(timeDisc));
306 }
307 
309 } // 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:25
Forward Euler scheme.
virtual void releaseVector(GlobalVector const &x)=0