deal2lkit: A ToolKit library for Deal.II
parsed_solver.h
Go to the documentation of this file.
1 //-----------------------------------------------------------
2 //
3 // Copyright (C) 2015 by the deal2lkit authors
4 //
5 // This file is part of the deal2lkit library.
6 //
7 // The deal2lkit library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal2lkit distribution.
13 //
14 //-----------------------------------------------------------
15 
16 #ifndef _d2k_parsed_solver_h
17 #define _d2k_parsed_solver_h
18 
19 #include <deal2lkit/config.h>
20 #include <deal.II/base/config.h>
21 
22 
23 #include <deal.II/lac/solver.h>
24 #include <deal.II/lac/solver_cg.h>
32 
34 #include <deal2lkit/utilities.h>
35 
36 using namespace dealii;
37 
38 
39 D2K_NAMESPACE_OPEN
40 
41 template <typename VECTOR>
42 std::function<void(VECTOR &, bool)> default_reinit()
43 {
44  return [](VECTOR &, bool)
45  {
46  Assert(false, ExcInternalError("It seems you really need to set a ReinitFunction for your "
47  "operator. Try avoiding PackagedOperator if you don't want "
48  "to implement this function."));
49  };
50 }
51 
70 template<typename VECTOR>
71 class ParsedSolver : public LinearOperator<VECTOR, VECTOR>, public ParameterAcceptor
72 {
73 public:
82  ParsedSolver(const std::string &name="",
83  const std::string &default_solver="cg",
84  const unsigned int iter=1000,
85  const double reduction=1e-8,
86  const LinearOperator<VECTOR> &op=
87  identity_operator<VECTOR>(default_reinit<VECTOR>()),
88  const LinearOperator<VECTOR> &prec=
89  identity_operator<VECTOR>(default_reinit<VECTOR>()));
90 
94  virtual void declare_parameters(ParameterHandler &prm);
95 
99  virtual void parse_parameters(ParameterHandler &prm);
100 
104  virtual void parse_parameters_call_back();
105 
106 
113 
120 
125 private:
129  template<typename MySolver >
130  void initialize_solver(MySolver *);
131 
135  std::string solver_name;
136 
141  unsigned int max_iterations;
142 
147  double reduction;
148 
152  shared_ptr<Solver<VECTOR> > solver;
153 };
154 
155 // ============================================================
156 // Explicit template functions
157 // ============================================================
158 
159 template<typename VECTOR>
160 ParsedSolver<VECTOR>::ParsedSolver(const std::string &name,
161  const std::string &default_solver,
162  const unsigned int default_iter,
163  const double default_reduction,
164  const LinearOperator<VECTOR> &op,
165  const LinearOperator<VECTOR> &prec) :
166  ParameterAcceptor(name),
167  op(op),
168  prec(prec),
169  solver_name(default_solver),
170  max_iterations(default_iter),
171  reduction(default_reduction)
172 {}
173 
174 
175 template<typename VECTOR>
177 {
178  add_parameter(prm, &solver_name, "Solver name", solver_name,
179  Patterns::Selection("cg|bicgstab|gmres|fgmres|"
180  "minres|qmrs|richardson"),
181  "Name of the solver to use.");
182 
184 
185  prm.set("Max steps", std::to_string(max_iterations));
186  prm.set("Reduction", reduction);
187 }
188 
189 
190 template<typename VECTOR>
192 {
195 }
196 
197 
198 template<typename VECTOR>
199 template<typename MySolver >
201 {
202  solver = SP(s);
204 }
205 
206 template<typename VECTOR>
208 {
209  if (solver_name == "cg")
210  {
212  }
213  else if (solver_name == "bicgstab")
214  {
216  }
217  else if (solver_name == "gmres")
218  {
220  }
221  else if (solver_name == "fgmres")
222  {
224  }
225  else if (solver_name == "minres")
226  {
228  }
229  else if (solver_name == "qmrs")
230  {
232  }
233  else if (solver_name == "richardson")
234  {
236  }
237  else
238  {
239  Assert(false, ExcInternalError("Solver should not be unknonw."));
240  }
241 }
242 
243 D2K_NAMESPACE_CLOSE
244 
245 
246 #endif
247 
A parameter acceptor base class.
static ParameterHandler prm
Static parameter.
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
std::function< void(VECTOR &, bool)> default_reinit()
Definition: parsed_solver.h:42
LinearOperator< VECTOR > op
The Operator this solver use.
virtual void parse_parameters(ParameterHandler &prm)
Parse solver type and solver options.
A solver selector which uses parameter files to choose between different options. ...
Definition: parsed_solver.h:71
void set(const std::string &entry_name, const std::string &new_value)
void parse_parameters(ParameterHandler &param)
shared_ptr< T > SP(T *t)
Construct a shared pointer to a non const class T.
Definition: utilities.h:483
LinearOperator< VECTOR > prec
The preconditioner used by this solver.
ReductionControl control
ReductionControl.
static void declare_parameters(ParameterHandler &param)
#define Assert(cond, exc)
void initialize_solver(MySolver *)
Store a shared pointer, and intilize the inverse operator.
double reduction
Default reduction required to succesfully complete a solution step.
virtual void declare_parameters(ParameterHandler &prm)
Declare solver type and solver options.
ParsedSolver(const std::string &name="", const std::string &default_solver="cg", const unsigned int iter=1000, const double reduction=1e-8, const LinearOperator< VECTOR > &op=identity_operator< VECTOR >(default_reinit< VECTOR >()), const LinearOperator< VECTOR > &prec=identity_operator< VECTOR >(default_reinit< VECTOR >()))
Constructor.
unsigned int max_iterations
Default number of maximum iterations required to succesfully complete a solution step.
virtual void parse_parameters(ParameterHandler &prm)
Parse the parameter file.
LinearOperator< VECTOR, VECTOR, Payload > inverse_operator(const LinearOperator< VECTOR, VECTOR, Payload > &op, Solver &solver, const Preconditioner &preconditioner)
virtual void parse_parameters_call_back()
Initialize internal variables.
std::string solver_name
Solver name.
shared_ptr< Solver< VECTOR > > solver
The actual solver.
void add_parameter(ParameterHandler &prm, T *parameter, const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation=std::string())
Add a parameter the given parameter list.
static::ExceptionBase & ExcInternalError()