ROL
ROL_BiCGSTAB.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_BICGSTAB_H
45 #define ROL_BICGSTAB_H
46 
51 #include "ROL_Krylov.hpp"
52 #include "ROL_Types.hpp"
53 
54 namespace ROL {
55 
56 template<class Real>
57 class BiCGSTAB : public Krylov<Real> {
58 
60  const bool useInexact_;
61  Ptr<Vector<Real>> r_, r1_, p_, v_, s_, t_, h_, y_, z_;
62 
63 public:
64  BiCGSTAB(Real absTol = 1.e-4, Real relTol = 1.e-2, unsigned maxit = 100, bool useInexact = false)
65  : Krylov<Real>(absTol,relTol,maxit), isInitialized_(false), useInexact_(useInexact) {}
66 
67  BiCGSTAB(ParameterList &parlist, bool useInexact = false)
68  : Krylov<Real>(parlist), isInitialized_(false), useInexact_(useInexact) {}
69 
71  int &iter, int &flag ) {
72  if ( !isInitialized_ ) {
73  r_ = b.clone(); r1_ = b.clone(); p_ = b.clone();
74  v_ = b.clone(); s_ = b.clone(); t_ = b.clone();
75  h_ = x.clone(); y_ = x.clone(); z_ = x.clone();
76  isInitialized_ = true;
77  }
78 
79  Real rho(1), rho1(1), alpha(1), beta(0), omega(1);
80  Real rnorm = b.norm();
81  Real itol = std::sqrt(ROL_EPSILON<Real>());
82  const Real rtol = std::min(Krylov<Real>::getAbsoluteTolerance(),Krylov<Real>::getRelativeTolerance()*rnorm);
83  if (rnorm <= rtol) return rnorm;
84 
85  x.zero();
86  v_->zero();
87  p_->zero();
88  r_->set(b);
89  r1_->set(*r_);
90 
91  iter = 0;
92  flag = 0;
93 
94  for (iter = 0; iter < (int)Krylov<Real>::getMaximumIteration(); iter++) {
95  rho1 = r_->dot(*r1_);
96  beta = (rho1/rho)*(alpha/omega);
97  p_->axpy(-omega,*v_);
98  p_->scale(beta);
99  p_->plus(*r_);
100 
101  if ( useInexact_ )
102  itol = rtol/((Real)Krylov<Real>::getMaximumIteration() * rnorm);
103  M.applyInverse(*y_, *p_, itol);
104  A.apply(*v_, *y_, itol);
105 
106  alpha = rho1 / v_->dot(*r1_);
107  h_->set(x);
108  h_->axpy(alpha,*y_);
109  s_->set(*r_);
110  s_->axpy(-alpha,*v_);
111 
112  rnorm = s_->norm();
113  if (rnorm <= rtol) {
114  x.set(*h_);
115  break;
116  }
117 
118  if ( useInexact_ )
119  itol = rtol/((Real)Krylov<Real>::getMaximumIteration() * rnorm);
120  M.applyInverse(*z_, *s_, itol);
121  A.apply(*t_, *z_, itol);
122 
123  omega = t_->dot(*s_) / t_->dot(*t_);
124  x.set(*h_);
125  x.axpy(omega,*z_);
126  r_->set(*s_);
127  r_->axpy(-omega,*t_);
128 
129  rnorm = r_->norm();
130  if (rnorm <= rtol) break;
131 
132  rho = rho1;
133  }
134  if (iter == (int)Krylov<Real>::getMaximumIteration()) {
135  flag = 1;
136  }
137  else {
138  iter++;
139  }
140  return rnorm;
141  }
142 };
143 
144 
145 }
146 
147 #endif
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:119
Ptr< Vector< Real > > t_
Ptr< Vector< Real > > h_
Contains definitions of custom data types in ROL.
BiCGSTAB(ParameterList &parlist, bool useInexact=false)
virtual void apply(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const =0
Apply linear operator.
Ptr< Vector< Real > > r1_
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:133
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
BiCGSTAB(Real absTol=1.e-4, Real relTol=1.e-2, unsigned maxit=100, bool useInexact=false)
const bool useInexact_
Ptr< Vector< Real > > r_
Ptr< Vector< Real > > p_
Ptr< Vector< Real > > s_
virtual void applyInverse(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const
Apply inverse of linear operator.
Provides definitions for Krylov solvers.
Definition: ROL_Krylov.hpp:24
Provides the interface to apply a linear operator.
Ptr< Vector< Real > > z_
Ptr< Vector< Real > > y_
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:175
virtual Real norm() const =0
Returns where .
Real run(Vector< Real > &x, LinearOperator< Real > &A, const Vector< Real > &b, LinearOperator< Real > &M, int &iter, int &flag)
Ptr< Vector< Real > > v_