Reference documentation for deal.II version 8.4.2
solver_richardson.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2015 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II 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 deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__solver_richardson_h
17 #define dealii__solver_richardson_h
18 
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/logstream.h>
22 #include <deal.II/lac/solver.h>
23 #include <deal.II/lac/solver_control.h>
24 #include <deal.II/base/subscriptor.h>
25 
26 DEAL_II_NAMESPACE_OPEN
27 
30 
60 template <class VectorType = Vector<double> >
61 class SolverRichardson : public Solver<VectorType>
62 {
63 public:
68  {
72  explicit
73  AdditionalData (const double omega = 1,
74  const bool use_preconditioned_residual = false);
75 
79  double omega;
80 
85 
86  };
87 
93  const AdditionalData &data=AdditionalData());
94 
100  const AdditionalData &data=AdditionalData());
101 
105  virtual ~SolverRichardson ();
106 
110  template<typename MatrixType, typename PreconditionerType>
111  void
112  solve (const MatrixType &A,
113  VectorType &x,
114  const VectorType &b,
115  const PreconditionerType &precondition);
116 
120  template<typename MatrixType, typename PreconditionerType>
121  void
122  Tsolve (const MatrixType &A,
123  VectorType &x,
124  const VectorType &b,
125  const PreconditionerType &precondition);
126 
130  void set_omega (const double om=1.);
131 
137  virtual void print_vectors (const unsigned int step,
138  const VectorType &x,
139  const VectorType &r,
140  const VectorType &d) const;
141 
142 protected:
146  virtual typename VectorType::value_type criterion();
147 
152  VectorType *Vr;
158  VectorType *Vd;
159 
164 
171  typename VectorType::value_type res;
172 };
173 
175 /*----------------- Implementation of the Richardson Method ------------------*/
176 
177 #ifndef DOXYGEN
178 
179 template <class VectorType>
180 inline
182 AdditionalData (const double omega,
183  const bool use_preconditioned_residual)
184  :
185  omega(omega),
186  use_preconditioned_residual(use_preconditioned_residual)
187 {}
188 
189 
190 template <class VectorType>
193  const AdditionalData &data)
194  :
195  Solver<VectorType> (cn,mem),
196  additional_data(data)
197 {}
198 
199 
200 
201 template <class VectorType>
203  const AdditionalData &data)
204  :
205  Solver<VectorType> (cn),
206  additional_data(data)
207 {}
208 
209 
210 
211 template <class VectorType>
213 {}
214 
215 
216 template <class VectorType>
217 template <typename MatrixType, typename PreconditionerType>
218 void
219 SolverRichardson<VectorType>::solve (const MatrixType &A,
220  VectorType &x,
221  const VectorType &b,
222  const PreconditionerType &precondition)
223 {
225 
226  double last_criterion = -std::numeric_limits<double>::max();
227 
228  unsigned int iter = 0;
229 
230  // Memory allocation
231  Vr = this->memory.alloc();
232  VectorType &r = *Vr;
233  r.reinit(x);
234  Vd = this->memory.alloc();
235  VectorType &d = *Vd;
236  d.reinit(x);
237 
238  deallog.push("Richardson");
239 
240  try
241  {
242  // Main loop
243  while (conv==SolverControl::iterate)
244  {
245  // Do not use residual,
246  // but do it in 2 steps
247  A.vmult(r,x);
248  r.sadd(-1.,1.,b);
249  precondition.vmult(d,r);
250 
251  // The required norm of the
252  // (preconditioned)
253  // residual is computed in
254  // criterion() and stored
255  // in res.
256  last_criterion = criterion();
257  conv = this->iteration_status (iter, last_criterion, x);
258  if (conv != SolverControl::iterate)
259  break;
260 
261  x.add(additional_data.omega,d);
262  print_vectors(iter,x,r,d);
263 
264  ++iter;
265  }
266  }
267  catch (...)
268  {
269  this->memory.free(Vr);
270  this->memory.free(Vd);
271  deallog.pop();
272  throw;
273  }
274  // Deallocate Memory
275  this->memory.free(Vr);
276  this->memory.free(Vd);
277  deallog.pop();
278 
279  // in case of failure: throw exception
280  if (conv != SolverControl::success)
282  last_criterion));
283  // otherwise exit as normal
284 }
285 
286 
287 template <class VectorType>
288 template <typename MatrixType, typename PreconditionerType>
289 void
290 SolverRichardson<VectorType>::Tsolve (const MatrixType &A,
291  VectorType &x,
292  const VectorType &b,
293  const PreconditionerType &precondition)
294 {
296  double last_criterion = -std::numeric_limits<double>::max();
297 
298  unsigned int iter = 0;
299 
300  // Memory allocation
301  Vr = this->memory.alloc();
302  VectorType &r = *Vr;
303  r.reinit(x);
304  Vd =this-> memory.alloc();
305  VectorType &d = *Vd;
306  d.reinit(x);
307 
308  deallog.push("RichardsonT");
309 
310  try
311  {
312  // Main loop
313  while (conv==SolverControl::iterate)
314  {
315  // Do not use Tresidual,
316  // but do it in 2 steps
317  A.Tvmult(r,x);
318  r.sadd(-1.,1.,b);
319  precondition.Tvmult(d,r);
320 
321  last_criterion = criterion();
322  conv = this->iteration_status (iter, last_criterion, x);
323  if (conv != SolverControl::iterate)
324  break;
325 
326  x.add(additional_data.omega,d);
327  print_vectors(iter,x,r,d);
328 
329  ++iter;
330  }
331  }
332  catch (...)
333  {
334  this->memory.free(Vr);
335  this->memory.free(Vd);
336  deallog.pop();
337  throw;
338  }
339 
340  // Deallocate Memory
341  this->memory.free(Vr);
342  this->memory.free(Vd);
343  deallog.pop();
344  // in case of failure: throw exception
345  if (conv != SolverControl::success)
346  AssertThrow(false, SolverControl::NoConvergence (iter, last_criterion));
347 
348  // otherwise exit as normal
349 }
350 
351 
352 template <class VectorType>
353 void
355  const VectorType &,
356  const VectorType &,
357  const VectorType &) const
358 {}
359 
360 
361 
362 template <class VectorType>
363 inline typename VectorType::value_type
365 {
367  res = Vr->l2_norm();
368  else
369  res = Vd->l2_norm();
370  return res;
371 }
372 
373 
374 template <class VectorType>
375 inline void
377 {
379 }
380 
381 #endif // DOXYGEN
382 
383 DEAL_II_NAMESPACE_CLOSE
384 
385 #endif
void pop()
Definition: logstream.cc:300
Continue iteration.
void set_omega(const double om=1.)
virtual void print_vectors(const unsigned int step, const VectorType &x, const VectorType &r, const VectorType &d) const
AdditionalData additional_data
#define AssertThrow(cond, exc)
Definition: exceptions.h:358
SolverRichardson(SolverControl &cn, VectorMemory< VectorType > &mem, const AdditionalData &data=AdditionalData())
Stop iteration, goal reached.
VectorType::value_type res
VectorMemory< VectorType > & memory
Definition: solver.h:402
boost::signals2::signal< SolverControl::State(const unsigned int iteration, const double check_value, const VectorType &current_iterate), StateCombiner > iteration_status
Definition: solver.h:448
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &precondition)
Definition: solver.h:325
void push(const std::string &text)
Definition: logstream.cc:288
void Tsolve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &precondition)
virtual ~SolverRichardson()
AdditionalData(const double omega=1, const bool use_preconditioned_residual=false)
virtual VectorType::value_type criterion()