dune-pdelab  2.7-git
seq_amg_dg_backend.hh
Go to the documentation of this file.
1 #ifndef DUNE_PDELAB_BACKEND_ISTL_SEQ_AMG_DG_BACKEND_HH
2 #define DUNE_PDELAB_BACKEND_ISTL_SEQ_AMG_DG_BACKEND_HH
3 
4 #include <dune/common/power.hh>
5 #include <dune/common/parametertree.hh>
6 
7 #include <dune/istl/matrixmatrix.hh>
8 
9 #include <dune/grid/common/datahandleif.hh>
10 
16 
17 namespace Dune {
18  namespace PDELab {
19 
28  template<class DGMatrix, class DGPrec, class CGPrec, class P>
29  class SeqDGAMGPrec : public Dune::Preconditioner<typename DGPrec::domain_type,typename DGPrec::range_type>
30  {
31  DGMatrix& dgmatrix;
32  DGPrec& dgprec;
33  CGPrec& cgprec;
34  P& p;
35  int n1,n2;
36  bool firstapply;
37 
38  public:
39  typedef typename DGPrec::domain_type X;
40  typedef typename DGPrec::range_type Y;
41  typedef typename CGPrec::domain_type CGX;
42  typedef typename CGPrec::range_type CGY;
43 
44  SolverCategory::Category category() const override
45  {
46  return SolverCategory::sequential;
47  }
48 
56  SeqDGAMGPrec (DGMatrix& dgmatrix_, DGPrec& dgprec_, CGPrec& cgprec_, P& p_, int n1_, int n2_)
57  : dgmatrix(dgmatrix_), dgprec(dgprec_), cgprec(cgprec_), p(p_), n1(n1_), n2(n2_),
58  firstapply(true)
59  {
60  }
61 
67  virtual void pre (X& x, Y& b) override
68  {
69  dgprec.pre(x,b);
70  CGY cgd(p.M());
71  cgd = 0.0;
72  CGX cgv(p.M());
73  cgv = 0.0;
74  cgprec.pre(cgv,cgd);
75  }
76 
82  virtual void apply (X& x, const Y& b) override
83  {
84  // need local copies to store defect and solution
85  Y d(b);
86  X v(x);
87 
88  // pre-smoothing on DG matrix
89  for (int i=0; i<n1; i++)
90  {
91  v = 0.0;
92  dgprec.apply(v,d);
93  dgmatrix.mmv(v,d);
94  x += v;
95  }
96 
97  // restrict defect to CG subspace
98  CGY cgd(p.M());
99  p.mtv(d,cgd);
100  CGX cgv(p.M());
101  cgv = 0.0;
102 
103  // apply AMG
104  cgprec.apply(cgv,cgd);
105 
106  // prolongate correction
107  p.mv(cgv,v);
108  dgmatrix.mmv(v,d);
109  x += v;
110 
111  // pre-smoothing on DG matrix
112  for (int i=0; i<n2; i++)
113  {
114  v = 0.0;
115  dgprec.apply(v,d);
116  dgmatrix.mmv(v,d);
117  x += v;
118  }
119  }
120 
126  virtual void post (X& x) override
127  {
128  dgprec.post(x);
129  CGX cgv(p.M());
130  cgv = 0.0;
131  cgprec.post(cgv);
132  }
133  };
134 
135 
145  template<class DGGO, class CGGFS, class TransferLOP, template<class,class,class,int> class DGPrec, template<class> class Solver>
147  {
148  // DG grid function space
149  typedef typename DGGO::Traits::TrialGridFunctionSpace GFS;
150 
151  // vectors and matrices on DG level
152  typedef typename DGGO::Traits::Jacobian M; // wrapped istl DG matrix
153  typedef typename DGGO::Traits::Domain V; // wrapped istl DG vector
154  typedef Backend::Native<M> Matrix; // istl DG matrix
155  typedef Backend::Native<V> Vector; // istl DG vector
156  typedef typename Vector::field_type field_type;
157 
158  // vectors and matrices on CG level
159  using CGV = Dune::PDELab::Backend::Vector<CGGFS,field_type>; // wrapped istl CG vector
160  typedef Backend::Native<CGV> CGVector; // istl CG vector
161 
162  // prolongation matrix
165  typedef TransferLOP CGTODGLOP; // local operator
167  typedef typename PGO::Jacobian PMatrix; // wrapped ISTL prolongation matrix
168  typedef Backend::Native<PMatrix> P; // ISTL prolongation matrix
169 
170  // CG subspace matrix
171  typedef typename Dune::TransposedMatMultMatResult<P,Matrix>::type PTADG;
172  typedef typename Dune::MatMultMatResult<PTADG,P>::type ACG; // istl coarse space matrix
173  typedef ACG CGMatrix; // another name
174 
175  // AMG in CG-subspace
176  typedef Dune::MatrixAdapter<CGMatrix,CGVector,CGVector> CGOperator;
177  typedef Dune::SeqSSOR<CGMatrix,CGVector,CGVector,1> Smoother;
178  typedef Dune::Amg::AMG<CGOperator,CGVector,Smoother> AMG;
179  typedef Dune::Amg::Parameters Parameters;
180 
181  DGGO& dggo;
182  CGGFS& cggfs;
183  std::shared_ptr<CGOperator> cgop;
184  std::shared_ptr<AMG> amg;
185  Parameters amg_parameters;
186  unsigned maxiter;
187  int verbose;
188  bool reuse;
189  bool firstapply;
190  bool usesuperlu;
191  std::size_t low_order_space_entries_per_row;
192 
193  CGTODGLOP cgtodglop; // local operator to assemble prolongation matrix
194  PGO pgo; // grid operator to assemble prolongation matrix
195  PMatrix pmatrix; // wrapped prolongation matrix
196  ACG acg; // CG-subspace matrix
197 
198  public:
199  ISTLBackend_SEQ_AMG_4_DG(DGGO& dggo_, CGGFS& cggfs_, unsigned maxiter_=5000, int verbose_=1, bool reuse_=false, bool usesuperlu_=true)
200  : dggo(dggo_)
201  , cggfs(cggfs_)
202  , amg_parameters(15,2000)
203  , maxiter(maxiter_)
204  , verbose(verbose_)
205  , reuse(reuse_)
206  , firstapply(true)
207  , usesuperlu(usesuperlu_)
208  , low_order_space_entries_per_row(StaticPower<3,GFS::Traits::GridView::dimension>::power)
209  , cgtodglop()
210  , pgo(cggfs,dggo.trialGridFunctionSpace(),cgtodglop,MBE(low_order_space_entries_per_row))
211  , pmatrix(pgo)
212  , acg()
213  {
214  amg_parameters.setDefaultValuesIsotropic(GFS::Traits::GridViewType::Traits::Grid::dimension);
215  amg_parameters.setDebugLevel(verbose_);
216 #if !HAVE_SUPERLU
217  if (usesuperlu == true)
218  {
219  std::cout << "WARNING: You are using AMG without SuperLU!"
220  << " Please consider installing SuperLU,"
221  << " or set the usesuperlu flag to false"
222  << " to suppress this warning." << std::endl;
223  }
224 #endif
225 
226  // assemble prolongation matrix; this will not change from one apply to the next
227  pmatrix = 0.0;
228  if (verbose>0) std::cout << "allocated prolongation matrix of size " << pmatrix.N() << " x " << pmatrix.M() << std::endl;
229  CGV cgx(cggfs,0.0); // need vector to call jacobian
230  pgo.jacobian(cgx,pmatrix);
231  }
232 
233  ISTLBackend_SEQ_AMG_4_DG(DGGO& dggo_, CGGFS& cggfs_, const ParameterTree& params)//unsigned maxiter_=5000, int verbose_=1, bool usesuperlu_=true)
234  : dggo(dggo_)
235  , cggfs(cggfs_)
236  , amg_parameters(15,2000)
237  , maxiter(params.get<int>("max_iterations",5000))
238  , verbose(params.get<int>("verbose",1))
239  , reuse(params.get<bool>("reuse", false))
240  , firstapply(true)
241  , usesuperlu(params.get<bool>("use_superlu",true))
242  , low_order_space_entries_per_row(params.get<std::size_t>("low_order_space.entries_per_row",StaticPower<3,GFS::Traits::GridView::dimension>::power))
243  , cgtodglop()
244  , pgo(cggfs,dggo.trialGridFunctionSpace(),cgtodglop,MBE(low_order_space_entries_per_row))
245  , pmatrix(pgo)
246  {
247  amg_parameters.setDefaultValuesIsotropic(GFS::Traits::GridViewType::Traits::Grid::dimension);
248  amg_parameters.setDebugLevel(params.get<int>("verbose",1));
249 #if !HAVE_SUPERLU
250  if (usesuperlu == true)
251  {
252  std::cout << "WARNING: You are using AMG without SuperLU!"
253  << " Please consider installing SuperLU,"
254  << " or set the usesuperlu flag to false"
255  << " to suppress this warning." << std::endl;
256  }
257 #endif
258 
259 
260  // assemble prolongation matrix; this will not change from one apply to the next
261  pmatrix = 0.0;
262  if (verbose>0) std::cout << "allocated prolongation matrix of size " << pmatrix.N() << " x " << pmatrix.M() << std::endl;
263  CGV cgx(cggfs,0.0); // need vector to call jacobian
264  pgo.jacobian(cgx,pmatrix);
265  }
266 
271  typename V::ElementType norm (const V& v) const
272  {
273  return Backend::native(v).two_norm();
274  }
275 
280  void setParameters(const Parameters& amg_parameters_)
281  {
282  amg_parameters = amg_parameters_;
283  }
284 
292  const Parameters& parameters() const
293  {
294  return amg_parameters;
295  }
296 
298  void setReuse(bool reuse_)
299  {
300  reuse = reuse_;
301  }
302 
304  bool getReuse() const
305  {
306  return reuse;
307  }
308 
316  void apply (M& A, V& z, V& r, typename Dune::template FieldTraits<typename V::ElementType >::real_type reduction)
317  {
318  using Backend::native;
319  // do triple matrix product ACG = P^T ADG P
320  Dune::Timer watch;
321  watch.reset();
322  // only do triple matrix product if the matrix changes
323  double triple_product_time = 0.0;
324  // no need to set acg here back to zero, this is done in matMultmat
325  if(reuse == false || firstapply == true) {
326  PTADG ptadg;
327  Dune::transposeMatMultMat(ptadg,native(pmatrix),native(A));
328  Dune::matMultMat(acg,ptadg,native(pmatrix));
329  triple_product_time = watch.elapsed();
330  if (verbose>0)
331  std::cout << "=== triple matrix product " << triple_product_time << " s" << std::endl;
332  //Dune::printmatrix(std::cout,acg,"triple product matrix","row",10,2);
333  }
334  else if (verbose>0)
335  std::cout << "=== reuse CG matrix, SKIPPING triple matrix product " << std::endl;
336 
337  // set up AMG solver for the CG subspace
338  typedef typename Dune::Amg::SmootherTraits<Smoother>::Arguments SmootherArgs;
339  SmootherArgs smootherArgs;
340  smootherArgs.iterations = 1;
341  smootherArgs.relaxationFactor = 1.0;
342  typedef Dune::Amg::CoarsenCriterion<Dune::Amg::SymmetricCriterion<CGMatrix,Dune::Amg::FirstDiagonal> > Criterion;
343  Criterion criterion(amg_parameters);
344  watch.reset();
345 
346  // only construct a new AMG for the CG-subspace if the matrix changes
347  double amg_setup_time = 0.0;
348  if(reuse == false || firstapply == true) {
349  cgop.reset(new CGOperator(acg));
350  amg.reset(new AMG(*cgop,criterion,smootherArgs));
351  firstapply = false;
352  amg_setup_time = watch.elapsed();
353  if (verbose>0)
354  std::cout << "=== AMG setup " <<amg_setup_time << " s" << std::endl;
355  }
356  else if (verbose>0)
357  std::cout << "=== reuse CG matrix, SKIPPING AMG setup " << std::endl;
358 
359  // set up hybrid DG/CG preconditioner
360  Dune::MatrixAdapter<Matrix,Vector,Vector> op(native(A));
361  DGPrec<Matrix,Vector,Vector,1> dgprec(native(A),1,1);
362  typedef SeqDGAMGPrec<Matrix,DGPrec<Matrix,Vector,Vector,1>,AMG,P> HybridPrec;
363  HybridPrec hybridprec(native(A),dgprec,*amg,native(pmatrix),2,2);
364 
365  // set up solver
366  Solver<Vector> solver(op,hybridprec,reduction,maxiter,verbose);
367 
368  // solve
369  Dune::InverseOperatorResult stat;
370  watch.reset();
371  solver.apply(native(z),native(r),stat);
372  double amg_solve_time = watch.elapsed();
373  if (verbose>0) std::cout << "=== Hybrid total solve time " << amg_solve_time+amg_setup_time+triple_product_time << " s" << std::endl;
374  res.converged = stat.converged;
375  res.iterations = stat.iterations;
376  res.elapsed = amg_solve_time+amg_setup_time+triple_product_time;
377  res.reduction = stat.reduction;
378  res.conv_rate = stat.conv_rate;
379  }
380 
381  };
382  }
383 }
384 #endif // DUNE_PDELAB_BACKEND_ISTL_SEQ_AMG_DG_BACKEND_HH
Dune::PDELab::GridOperator< CGGFS, GFS, CGTODGLOP, MBE, field_type, field_type, field_type, CC, CC >
Dune::PDELab::LinearSolverResult::reduction
RFType reduction
Definition: solver.hh:35
Dune::PDELab::ISTLBackend_SEQ_AMG_4_DG::parameters
const Parameters & parameters() const
Get the parameters describing the behaviuour of AMG.
Definition: seq_amg_dg_backend.hh:292
bcrsmatrix.hh
Dune::PDELab::ISTLBackend_SEQ_AMG_4_DG::ISTLBackend_SEQ_AMG_4_DG
ISTLBackend_SEQ_AMG_4_DG(DGGO &dggo_, CGGFS &cggfs_, const ParameterTree &params)
Definition: seq_amg_dg_backend.hh:233
Dune::PDELab::Backend::Vector
typename impl::BackendVectorSelector< GridFunctionSpace, FieldType >::Type Vector
alias of the return type of BackendVectorSelector
Definition: backend/interface.hh:106
p
const P & p
Definition: constraints.hh:148
Dune::PDELab::LinearSolverResult::conv_rate
RFType conv_rate
Definition: solver.hh:36
Dune::PDELab::Backend::Native
typename native_type< T >::type Native
Alias of the native container type associated with T or T itself if it is not a backend wrapper.
Definition: backend/interface.hh:176
Dune::PDELab::LinearResultStorage::res
Dune::PDELab::LinearSolverResult< double > res
Definition: solver.hh:63
Dune::PDELab::SeqDGAMGPrec::Y
DGPrec::range_type Y
Definition: seq_amg_dg_backend.hh:40
Dune::PDELab::GridOperator< CGGFS, GFS, CGTODGLOP, MBE, field_type, field_type, field_type, CC, CC >::Jacobian
Dune::PDELab::Backend::Matrix< MBE, Domain, Range, field_type > Jacobian
The type of the jacobian.
Definition: gridoperator.hh:47
Dune::PDELab::ISTLBackend_SEQ_AMG_4_DG::setReuse
void setReuse(bool reuse_)
Set whether the AMG should be reused again during call to apply().
Definition: seq_amg_dg_backend.hh:298
Dune
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
Dune::PDELab::LinearSolverResult::converged
bool converged
Definition: solver.hh:32
Dune::PDELab::Backend::native
std::enable_if< std::is_base_of< impl::WrapperBase, T >::value, Native< T > & >::type native(T &t)
Definition: backend/interface.hh:192
Dune::PDELab::ISTL::BCRSMatrixBackend
Backend using (possibly nested) ISTL BCRSMatrices.
Definition: bcrsmatrixbackend.hh:187
Dune::PDELab::LinearSolverResult::iterations
unsigned int iterations
Definition: solver.hh:33
Dune::PDELab::GridOperator::jacobian
void jacobian(const Domain &x, Jacobian &a) const
Assembler jacobian.
Definition: gridoperator.hh:180
Dune::PDELab::SeqDGAMGPrec::post
virtual void post(X &x) override
Clean up.
Definition: seq_amg_dg_backend.hh:126
Parameters
Definition: recipe-operator-splitting.cc:107
Dune::PDELab::SeqDGAMGPrec::X
DGPrec::domain_type X
Definition: seq_amg_dg_backend.hh:39
Dune::PDELab::SeqDGAMGPrec::category
SolverCategory::Category category() const override
Definition: seq_amg_dg_backend.hh:44
Dune::PDELab::SeqDGAMGPrec::CGX
CGPrec::domain_type CGX
Definition: seq_amg_dg_backend.hh:41
Dune::PDELab::ISTLBackend_SEQ_AMG_4_DG::getReuse
bool getReuse() const
Return whether the AMG is reused during call to apply()
Definition: seq_amg_dg_backend.hh:304
vector.hh
Dune::PDELab::SeqDGAMGPrec::apply
virtual void apply(X &x, const Y &b) override
Apply the precondioner.
Definition: seq_amg_dg_backend.hh:82
Dune::PDELab::SeqDGAMGPrec::CGY
CGPrec::range_type CGY
Definition: seq_amg_dg_backend.hh:42
ovlpistlsolverbackend.hh
Dune::PDELab::SeqDGAMGPrec::SeqDGAMGPrec
SeqDGAMGPrec(DGMatrix &dgmatrix_, DGPrec &dgprec_, CGPrec &cgprec_, P &p_, int n1_, int n2_)
Constructor.
Definition: seq_amg_dg_backend.hh:56
Dune::PDELab::LinearResultStorage
Definition: solver.hh:53
Dune::PDELab::ISTLBackend_SEQ_AMG_4_DG::norm
V::ElementType norm(const V &v) const
compute global norm of a vector
Definition: seq_amg_dg_backend.hh:271
Dune::PDELab::EmptyTransformation
Definition: constraintstransformation.hh:111
bcrsmatrixbackend.hh
Dune::PDELab::ISTLBackend_SEQ_AMG_4_DG
Definition: seq_amg_dg_backend.hh:146
Dune::PDELab::LinearSolverResult::elapsed
double elapsed
Definition: solver.hh:34
gridoperator.hh
Dune::PDELab::SeqDGAMGPrec::pre
virtual void pre(X &x, Y &b) override
Prepare the preconditioner.
Definition: seq_amg_dg_backend.hh:67
Dune::PDELab::SeqDGAMGPrec
Definition: seq_amg_dg_backend.hh:29
Dune::PDELab::ISTLBackend_SEQ_AMG_4_DG::setParameters
void setParameters(const Parameters &amg_parameters_)
set AMG parameters
Definition: seq_amg_dg_backend.hh:280
Dune::PDELab::ISTLBackend_SEQ_AMG_4_DG::apply
void apply(M &A, V &z, V &r, typename Dune::template FieldTraits< typename V::ElementType >::real_type reduction)
solve the given linear system
Definition: seq_amg_dg_backend.hh:316
Dune::PDELab::ISTLBackend_SEQ_AMG_4_DG::ISTLBackend_SEQ_AMG_4_DG
ISTLBackend_SEQ_AMG_4_DG(DGGO &dggo_, CGGFS &cggfs_, unsigned maxiter_=5000, int verbose_=1, bool reuse_=false, bool usesuperlu_=true)
Definition: seq_amg_dg_backend.hh:199