Reference documentation for deal.II version 9.2.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
solver_bicgstab.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2019 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.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_solver_bicgstab_h
17 #define dealii_solver_bicgstab_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #include <deal.II/base/logstream.h>
25 
26 #include <deal.II/lac/solver.h>
28 
29 #include <cmath>
30 
32 
35 
36 namespace internal
37 {
43  {
44  protected:
48  double alpha;
52  double beta;
56  double omega;
60  double rho;
64  double rhobar;
65 
69  unsigned int step;
70 
74  double res;
75 
81  };
82 } // namespace internal
83 
123 template <typename VectorType = Vector<double>>
124 class SolverBicgstab : public SolverBase<VectorType>,
126 {
127 public:
139  {
146  explicit AdditionalData(const bool exact_residual = true,
147  const double breakdown = 1.e-10)
150  {}
158  double breakdown;
159  };
160 
166  const AdditionalData & data = AdditionalData());
167 
173  const AdditionalData &data = AdditionalData());
174 
178  virtual ~SolverBicgstab() override = default;
179 
183  template <typename MatrixType, typename PreconditionerType>
184  void
185  solve(const MatrixType & A,
186  VectorType & x,
187  const VectorType & b,
188  const PreconditionerType &preconditioner);
189 
190 protected:
195 
200 
205 
210 
215 
220 
225 
230 
234  const VectorType *Vb;
235 
239  template <typename MatrixType>
240  double
241  criterion(const MatrixType &A, const VectorType &x, const VectorType &b);
242 
248  virtual void
249  print_vectors(const unsigned int step,
250  const VectorType & x,
251  const VectorType & r,
252  const VectorType & d) const;
253 
257  AdditionalData additional_data;
258 
259 private:
263  template <typename MatrixType>
265  start(const MatrixType &A);
266 
272  {
273  bool breakdown;
275  unsigned int last_step;
277 
278  IterationResult(const bool breakdown,
280  const unsigned int last_step,
281  const double last_residual);
282  };
283 
288  template <typename MatrixType, typename PreconditionerType>
290  iterate(const MatrixType &A, const PreconditionerType &preconditioner);
291 };
292 
294 /*-------------------------Inline functions -------------------------------*/
295 
296 #ifndef DOXYGEN
297 
298 
299 template <typename VectorType>
301  const bool breakdown,
302  const SolverControl::State state,
303  const unsigned int last_step,
304  const double last_residual)
305  : breakdown(breakdown)
306  , state(state)
307  , last_step(last_step)
308  , last_residual(last_residual)
309 {}
310 
311 
312 
313 template <typename VectorType>
316  const AdditionalData & data)
317  : SolverBase<VectorType>(cn, mem)
318  , Vx(nullptr)
319  , Vb(nullptr)
320  , additional_data(data)
321 {}
322 
323 
324 
325 template <typename VectorType>
327  const AdditionalData &data)
328  : SolverBase<VectorType>(cn)
329  , Vx(nullptr)
330  , Vb(nullptr)
331  , additional_data(data)
332 {}
333 
334 
335 
336 template <typename VectorType>
337 template <typename MatrixType>
338 double
339 SolverBicgstab<VectorType>::criterion(const MatrixType &A,
340  const VectorType &x,
341  const VectorType &b)
342 {
343  A.vmult(*Vt, x);
344  Vt->add(-1., b);
345  res = Vt->l2_norm();
346 
347  return res;
348 }
349 
350 
351 
352 template <typename VectorType>
353 template <typename MatrixType>
355 SolverBicgstab<VectorType>::start(const MatrixType &A)
356 {
357  A.vmult(*Vr, *Vx);
358  Vr->sadd(-1., 1., *Vb);
359  res = Vr->l2_norm();
360 
361  return this->iteration_status(step, res, *Vx);
362 }
363 
364 
365 
366 template <typename VectorType>
367 void
369  const VectorType &,
370  const VectorType &,
371  const VectorType &) const
372 {}
373 
374 
375 
376 template <typename VectorType>
377 template <typename MatrixType, typename PreconditionerType>
379 SolverBicgstab<VectorType>::iterate(const MatrixType & A,
380  const PreconditionerType &preconditioner)
381 {
382  // TODO:[GK] Implement "use the length of the computed orthogonal residual" in
383  // the BiCGStab method.
385  alpha = omega = rho = 1.;
386 
387  VectorType &r = *Vr;
388  VectorType &rbar = *Vrbar;
389  VectorType &p = *Vp;
390  VectorType &y = *Vy;
391  VectorType &z = *Vz;
392  VectorType &t = *Vt;
393  VectorType &v = *Vv;
394 
395  rbar = r;
396  bool startup = true;
397 
398  do
399  {
400  ++step;
401 
402  rhobar = r * rbar;
403  beta = rhobar * alpha / (rho * omega);
404  rho = rhobar;
405  if (startup == true)
406  {
407  p = r;
408  startup = false;
409  }
410  else
411  {
412  p.sadd(beta, 1., r);
413  p.add(-beta * omega, v);
414  }
415 
416  preconditioner.vmult(y, p);
417  A.vmult(v, y);
418  rhobar = rbar * v;
419 
420  alpha = rho / rhobar;
421 
422  // TODO:[?] Find better breakdown criterion
423 
424  if (std::fabs(alpha) > 1.e10)
425  return IterationResult(true, state, step, res);
426 
427  res = std::sqrt(r.add_and_dot(-alpha, v, r));
428 
429  // check for early success, see the lac/bicgstab_early testcase as to
430  // why this is necessary
431  //
432  // note: the vector *Vx we pass to the iteration_status signal here is
433  // only the current approximation, not the one we will return with, which
434  // will be x=*Vx + alpha*y
436  {
437  Vx->add(alpha, y);
438  print_vectors(step, *Vx, r, y);
439  return IterationResult(false, SolverControl::success, step, res);
440  }
441 
442  preconditioner.vmult(z, r);
443  A.vmult(t, z);
444  rhobar = t * r;
445  omega = rhobar / (t * t);
446  Vx->add(alpha, y, omega, z);
447 
449  {
450  r.add(-omega, t);
451  res = criterion(A, *Vx, *Vb);
452  }
453  else
454  res = std::sqrt(r.add_and_dot(-omega, t, r));
455 
456  state = this->iteration_status(step, res, *Vx);
457  print_vectors(step, *Vx, r, y);
458  }
459  while (state == SolverControl::iterate);
460  return IterationResult(false, state, step, res);
461 }
462 
463 
464 template <typename VectorType>
465 template <typename MatrixType, typename PreconditionerType>
466 void
467 SolverBicgstab<VectorType>::solve(const MatrixType & A,
468  VectorType & x,
469  const VectorType & b,
470  const PreconditionerType &preconditioner)
471 {
472  LogStream::Prefix prefix("Bicgstab");
473  Vr = typename VectorMemory<VectorType>::Pointer(this->memory);
475  Vp = typename VectorMemory<VectorType>::Pointer(this->memory);
476  Vy = typename VectorMemory<VectorType>::Pointer(this->memory);
477  Vz = typename VectorMemory<VectorType>::Pointer(this->memory);
478  Vt = typename VectorMemory<VectorType>::Pointer(this->memory);
479  Vv = typename VectorMemory<VectorType>::Pointer(this->memory);
480 
481  Vr->reinit(x, true);
482  Vrbar->reinit(x, true);
483  Vp->reinit(x, true);
484  Vy->reinit(x, true);
485  Vz->reinit(x, true);
486  Vt->reinit(x, true);
487  Vv->reinit(x, true);
488 
489  Vx = &x;
490  Vb = &b;
491 
492  step = 0;
493 
494  IterationResult state(false, SolverControl::failure, 0, 0);
495 
496  // iterate while the inner iteration returns a breakdown
497  do
498  {
499  if (step != 0)
500  deallog << "Restart step " << step << std::endl;
502  {
503  state.state = SolverControl::success;
504  break;
505  }
506  state = iterate(A, preconditioner);
507  ++step;
508  }
509  while (state.breakdown == true);
510 
511  // in case of failure: throw exception
512  AssertThrow(state.state == SolverControl::success,
513  SolverControl::NoConvergence(state.last_step,
514  state.last_residual));
515  // otherwise exit as normal
516 }
517 
518 #endif // DOXYGEN
519 
521 
522 #endif
SolverBicgstab::AdditionalData::AdditionalData
AdditionalData(const bool exact_residual=true, const double breakdown=1.e-10)
Definition: solver_bicgstab.h:146
SolverBicgstab::Vp
VectorMemory< VectorType >::Pointer Vp
Definition: solver_bicgstab.h:209
solver.h
SolverBicgstab::IterationResult
Definition: solver_bicgstab.h:271
SolverBicgstab::Vb
const VectorType * Vb
Definition: solver_bicgstab.h:234
LogStream::Prefix
Definition: logstream.h:103
SolverControl::State
State
Definition: solver_control.h:74
internal::SolverBicgstabData::step
unsigned int step
Definition: solver_bicgstab.h:69
SolverBicgstab::AdditionalData::exact_residual
bool exact_residual
Definition: solver_bicgstab.h:154
SolverControl::NoConvergence
Definition: solver_control.h:96
SolverBicgstab::AdditionalData::breakdown
double breakdown
Definition: solver_bicgstab.h:158
SolverBicgstab::Vv
VectorMemory< VectorType >::Pointer Vv
Definition: solver_bicgstab.h:229
SolverBicgstab::Vr
VectorMemory< VectorType >::Pointer Vr
Definition: solver_bicgstab.h:199
SolverBicgstab::additional_data
AdditionalData additional_data
Definition: solver_bicgstab.h:257
VectorType
SolverBicgstab::Vy
VectorMemory< VectorType >::Pointer Vy
Definition: solver_bicgstab.h:214
SolverBicgstab::print_vectors
virtual void print_vectors(const unsigned int step, const VectorType &x, const VectorType &r, const VectorType &d) const
Physics::Elasticity::Kinematics::e
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
Physics::Elasticity::Kinematics::d
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
internal::SolverBicgstabData::alpha
double alpha
Definition: solver_bicgstab.h:48
deallog
LogStream deallog
Definition: logstream.cc:37
internal::SolverBicgstabData::beta
double beta
Definition: solver_bicgstab.h:52
SolverBase
Definition: solver.h:333
Differentiation::SD::fabs
Expression fabs(const Expression &x)
Definition: symengine_math.cc:273
SolverBicgstab::IterationResult::last_residual
double last_residual
Definition: solver_bicgstab.h:276
SolverControl::iterate
@ iterate
Continue iteration.
Definition: solver_control.h:77
SolverBicgstab::iterate
IterationResult iterate(const MatrixType &A, const PreconditionerType &preconditioner)
subscriptor.h
VectorMemory::Pointer
Definition: vector_memory.h:192
SolverBicgstab::AdditionalData
Definition: solver_bicgstab.h:138
SolverBicgstab::~SolverBicgstab
virtual ~SolverBicgstab() override=default
DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
SolverBase< Vector< double > >::memory
VectorMemory< Vector< double > > & memory
Definition: solver.h:412
Physics::Elasticity::Kinematics::b
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
SolverBicgstab::IterationResult::breakdown
bool breakdown
Definition: solver_bicgstab.h:273
SolverBicgstab::IterationResult::last_step
unsigned int last_step
Definition: solver_bicgstab.h:275
SolverBase< Vector< double > >::iteration_status
boost::signals2::signal< SolverControl::State(const unsigned int iteration, const double check_value, const Vector< double > &current_iterate), StateCombiner > iteration_status
Definition: solver.h:463
SolverBicgstab::solve
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
SolverBicgstab::IterationResult::IterationResult
IterationResult(const bool breakdown, const SolverControl::State state, const unsigned int last_step, const double last_residual)
SolverBicgstab::start
SolverControl::State start(const MatrixType &A)
SolverBicgstab
Definition: solver_bicgstab.h:124
LAPACKSupport::A
static const char A
Definition: lapack_support.h:155
SolverBicgstab::Vz
VectorMemory< VectorType >::Pointer Vz
Definition: solver_bicgstab.h:219
std::sqrt
inline ::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &x)
Definition: vectorization.h:5412
SolverBicgstab::Vt
VectorMemory< VectorType >::Pointer Vt
Definition: solver_bicgstab.h:224
SolverControl::failure
@ failure
Stop iteration, goal not reached.
Definition: solver_control.h:81
SolverBicgstab::IterationResult::state
SolverControl::State state
Definition: solver_bicgstab.h:274
SolverBicgstab::SolverBicgstab
SolverBicgstab(SolverControl &cn, VectorMemory< VectorType > &mem, const AdditionalData &data=AdditionalData())
internal::SolverBicgstabData::SolverBicgstabData
SolverBicgstabData()
Definition: solver_bicgstab.cc:22
signaling_nan.h
SolverControl::success
@ success
Stop iteration, goal reached.
Definition: solver_control.h:79
internal::SolverBicgstabData::rho
double rho
Definition: solver_bicgstab.h:60
config.h
internal::SolverBicgstabData::res
double res
Definition: solver_bicgstab.h:74
SolverBicgstab::Vx
VectorType * Vx
Definition: solver_bicgstab.h:194
internal
Definition: aligned_vector.h:369
SolverControl
Definition: solver_control.h:67
DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
logstream.h
internal::SolverBicgstabData
Definition: solver_bicgstab.h:42
SolverBicgstab::Vrbar
VectorMemory< VectorType >::Pointer Vrbar
Definition: solver_bicgstab.h:204
SolverBicgstab::criterion
double criterion(const MatrixType &A, const VectorType &x, const VectorType &b)
internal::SolverBicgstabData::rhobar
double rhobar
Definition: solver_bicgstab.h:64
AssertThrow
#define AssertThrow(cond, exc)
Definition: exceptions.h:1531
internal::SolverBicgstabData::omega
double omega
Definition: solver_bicgstab.h:56
solver_control.h
VectorMemory
Definition: vector_memory.h:107