DUNE-ACFEM (unstable)

solverselector.hh
1 #ifndef __DUNE_ACFEM_SOLVER_SELECTOR_HH__
2 #define __DUNE_ACFEM_SOLVER_SELECTOR_HH__
3 
4 // include linear operators
5 #include <dune/fem/operator/linear/istloperator.hh>
6 #include <dune/fem/solver/istlinverseoperators.hh>
7 #include <dune/fem/operator/linear/petscoperator.hh>
8 #include <dune/fem/solver/petscinverseoperators.hh>
9 #include <dune/fem/operator/linear/spoperator.hh>
10 #include <dune/fem/solver/krylovinverseoperators.hh>
11 
12 #include "discretefunctionselector.hh"
13 #include "../models/modeltraits.hh"
14 
15 namespace Dune {
16 
17  namespace ACFem {
18 
23  // Traits for SolverSelector
24 
25  template<class DiscreteFunction>
26  struct DiscreteFunctionTraits;
27 
28  template<class DiscreteSpace>
29  struct DiscreteFunctionTraits<Fem::AdaptiveDiscreteFunction<DiscreteSpace> >
30  {
31  static constexpr SolverFamily family = SolverFamily::FEM;
32  };
33 
34  template<class DiscreteSpace>
35  struct DiscreteFunctionTraits<Fem::ISTLBlockVectorDiscreteFunction<DiscreteSpace> >
36  {
37  static constexpr SolverFamily family = SolverFamily::ISTL;
38  };
39 
40  template<class DiscreteSpace>
41  struct DiscreteFunctionTraits<Fem::PetscDiscreteFunction<DiscreteSpace> >
42  {
43  static constexpr SolverFamily family = SolverFamily::PETSC;
44  };
45 
46 
50  enum class SolverType {
51  runtime = -1,
52  CG = 0,
53  MINRES = 3,
54  BICGSTAB = 1,
55  GMRES = 2
56  };
57 
89  template<class DiscreteFunction, class Model>
91  {
92  private:
93  static_assert(IsPDEModel<Model>::value,
94  "Model should better be a model ...");
95 
96  using DiscreteFunctionType = DiscreteFunction;
97 
98  static constexpr SolverFamily family = DiscreteFunctionTraits<DiscreteFunctionType>::family;
99 
100  public:
101  // Choose PCG if symmetric and semi-definite, otherwise Minres
102  // if symmetric, otherwise GMRES
103  static constexpr SolverType solverType = ExpressionTraits<Model>::isSymmetric
105  ? SolverType::CG
106  : SolverType::MINRES )
107  : SolverType::GMRES ;
108  private:
109  template<SolverFamily fam, bool dummy = false>
110  struct OperatorSelector;
111 
112  template<bool dummy>
113  struct OperatorSelector<SolverFamily::ISTL, dummy>
114  {
115  using LinearOperatorType = Fem::ISTLLinearOperator<DiscreteFunctionType, DiscreteFunctionType>;
116 
117  using LinearInverseOperatorType = Fem::ISTLInverseOperator<DiscreteFunctionType>;
118  };
119 
120 #if HAVE_PETSC
121  template<bool dummy>
122  struct OperatorSelector<SolverFamily::PETSC, dummy>
123  {
124  typedef
125  Fem::PetscLinearOperator<DiscreteFunctionType, DiscreteFunctionType>
126  LinearOperatorType;
127 
128  // The PETSC inverse operator is configured at run-time.
129  typedef
130  Fem::PetscInverseOperator<DiscreteFunctionType, LinearOperatorType>
131  LinearInverseOperatorType;
132  };
133 #endif
134 
135  template<bool dummy>
136  struct OperatorSelector<SolverFamily::FEM, dummy>
137  {
138  typedef
139  Fem::SparseRowLinearOperator<DiscreteFunctionType, DiscreteFunctionType>
140  LinearOperatorType;
141 
142  static_assert(solverType != SolverType::MINRES, "MinRes solver not available in bare dune fem.");
143 
144  using LinearInverseOperatorType = Fem::KrylovInverseOperator<DiscreteFunctionType>;
145  };
146  public:
147  typedef typename OperatorSelector<family>::LinearOperatorType LinearOperatorType;
148  typedef typename OperatorSelector<family>::LinearInverseOperatorType LinearInverseOperatorType;
149  };
150 
151  template<class SolverSelector>
152  void chooseDefaultSolverMethod(const Dune::Fem::SolverParameter& solverParams)
153  {
154  std::string prefix = solverParams.keyPrefix();
155  if(!Fem::Parameter::exists(prefix + "method"))
156  {
157  if constexpr (SolverSelector::solverType == SolverType::CG)
158  Fem::Parameter::append(prefix + "method", "cg");
159  else if constexpr (SolverSelector::solverType == SolverType::GMRES)
160  Fem::Parameter::append(prefix + "method", "gmres");
161  else if constexpr (SolverSelector::solverType == SolverType::MINRES)
162  Fem::Parameter::append(prefix + "method", "minres");
163  }
164  }
166 
167  } // ACFEM::
168 
169 } // Dune::
170 
171 
172 #endif // __DUNE_ACFEM_SOLVER_SELECTOR_HH__
SolverType
Define some symbolic constants for SolverSelector and "parse" some preprocessort defines.
Definition: solverselector.hh:50
ModelIntrospection::template IsModel< Model > IsPDEModel
std::true_type if Model is derived from ModelBase.
Definition: modeltraits.hh:894
Default expression traits definition is a recursion in order to ease disambiguation.
Definition: expressiontraits.hh:54
Select one appropriate (linear) solver depending on whether the model is symmetric and/or semidefinit...
Definition: solverselector.hh:91
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 2, 22:35, 2024)