ROL
ROL_GMRES.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_GMRES_H
45 #define ROL_GMRES_H
46 
51 #include "ROL_Krylov.hpp"
52 #include "ROL_LinearOperator.hpp"
53 #include "ROL_Vector.hpp"
54 #include "ROL_Types.hpp"
55 
56 #include "Teuchos_SerialDenseMatrix.hpp"
57 #include "Teuchos_SerialDenseVector.hpp"
58 #include "Teuchos_LAPACK.hpp"
59 
60 namespace ROL {
61 
62 template<class Real>
63 class GMRES : public Krylov<Real> {
64 
65  typedef Teuchos::SerialDenseMatrix<int, Real> SDMatrix;
66  typedef Teuchos::SerialDenseVector<int, Real> SDVector;
67 
68 private:
69 
70  Teuchos::RCP<Vector<Real> > r_;
71  Teuchos::RCP<Vector<Real> > z_;
72  Teuchos::RCP<Vector<Real> > w_;
73 
74  Teuchos::RCP<SDMatrix> H_; // quasi-Hessenberg matrix
75  Teuchos::RCP<SDVector> cs_; // Givens Rotations cosine components
76  Teuchos::RCP<SDVector> sn_; // Givens Rotations sine components
77  Teuchos::RCP<SDVector> s_;
78  Teuchos::RCP<SDVector> y_;
79  Teuchos::RCP<SDVector> cnorm_;
80 
81  Teuchos::RCP<std::vector<Real> > res_;
82 
85  bool useInitialGuess_; // If false, inital x will be ignored and zero vec used
86  int maxit_;
87  Real absTol_;
88  Real relTol_;
89 
90  Teuchos::LAPACK<int,Real> lapack_;
91 
92 public:
93 
94  GMRES( Teuchos::ParameterList &parlist ) : isInitialized_(false) {
95 
96  using Teuchos::RCP;
97  using Teuchos::rcp;
98  using std::vector;
99 
100  Real zero(0), oem2(1.e-2), oem4(1.e-4);
101 
102  Teuchos::ParameterList &gList = parlist.sublist("General");
103  Teuchos::ParameterList &kList = gList.sublist("Krylov");
104 
105  useInexact_ = gList.get("Inexact Hessian-Times-A-Vector",false);
106  maxit_ = kList.get("Iteration Limit",50);
107  absTol_ = kList.get("Absolute Tolerance", oem4);
108  relTol_ = kList.get("Relative Tolerance", oem2);
109  useInitialGuess_ = kList.get("Use Initial Guess",false);
110 
111  H_ = rcp( new SDMatrix( maxit_+1, maxit_ ) );
112  cs_ = rcp( new SDVector( maxit_ ) );
113  sn_ = rcp( new SDVector( maxit_ ) );
114  s_ = rcp( new SDVector( maxit_+1 ) );
115  y_ = rcp( new SDVector( maxit_+1 ) );
116  cnorm_ = rcp( new SDVector( maxit_ ) );
117  res_ = rcp( new std::vector<Real>(maxit_+1,zero) );
118 
119  }
120 
122  LinearOperator<Real> &M, int &iter, int &flag ) {
123 
124  using Teuchos::RCP;
125 
126  flag = 0;
127 
128  Real zero(0), one(1);
129 
130  if ( !isInitialized_ ) {
131  r_ = b.clone();
132  w_ = b.clone();
133  z_ = x.clone();
134 
135  isInitialized_ = true;
136  }
137 
138  Real itol = std::sqrt(ROL_EPSILON<Real>());
139 
140  // Compute initial residual
141  if(useInitialGuess_) {
142 
143  A.apply(*r_,x,itol);
144  r_->scale(-one);
145  r_->plus(b); // r = b-Ax
146 
147  }
148  else {
149  x.zero();
150  r_->set(b);
151  }
152 
153  Real temp = 0;
154 
155  std::vector<RCP<Vector<Real > > > V;
156  std::vector<RCP<Vector<Real > > > Z;
157 
158  (*res_)[0] = r_->norm();
159 
160  Real rtol = std::min(absTol_,relTol_*(*res_)[0]);
161 
162  V.push_back(b.clone());
163  (V[0])->set(*r_);
164  (V[0])->scale(one/(*res_)[0]);
165 
166  (*s_)(0) = (*res_)[0];
167 
168  for( iter=0; iter<maxit_; ++iter ) {
169 
170 // std::cout << (*res_)[iter] << std::endl;
171 
172  if( useInexact_ ) {
173  itol = rtol/(maxit_*(*res_)[iter]);
174  }
175 
176  Z.push_back(x.clone());
177 
178  // Apply right preconditioner
179  M.applyInverse(*(Z[iter]),*(V[iter]),itol);
180 
181  // Apply operator
182  A.apply(*w_,*(Z[iter]),itol);
183 
184  // Evaluate coefficients and orthogonalize using Gram-Schmidt
185  for( int k=0; k<=iter; ++k ) {
186  (*H_)(k,iter) = w_->dot(*(V[k]));
187  w_->axpy( -(*H_)(k,iter), *(V[k]) );
188  }
189 
190  (*H_)(iter+1,iter) = w_->norm();
191 
192  V.push_back( b.clone() );
193  (V[iter+1])->set(*w_);
194  (V[iter+1])->scale(one/((*H_)(iter+1,iter)));
195 
196  // Apply Givens rotations
197  for( int k=0; k<=iter-1; ++k ) {
198  temp = (*cs_)(k)*(*H_)(k,iter) + (*sn_)(k)*(*H_)(k+1,iter);
199  (*H_)(k+1,iter) = -(*sn_)(k)*(*H_)(k,iter) + (*cs_)(k)*(*H_)(k+1,iter);
200  (*H_)(k,iter) = temp;
201  }
202 
203  // Form i-th rotation matrix
204  if( (*H_)(iter+1,iter) == zero ) {
205  (*cs_)(iter) = one;
206  (*sn_)(iter) = zero;
207  }
208  else if ( std::abs((*H_)(iter+1,iter)) > std::abs((*H_)(iter,iter)) ) {
209  temp = (*H_)(iter,iter) / (*H_)(iter+1,iter);
210  (*sn_)(iter) = one / std::sqrt( one + temp*temp );
211  (*cs_)(iter) = temp*(*sn_)(iter);
212  }
213  else {
214  temp = (*H_)(iter+1,iter) / (*H_)(iter,iter);
215  (*cs_)(iter) = one / std::sqrt( one + temp*temp );
216  (*sn_)(iter) = temp*(*cs_)(iter);
217  }
218 
219  // Approximate residual norm
220  temp = (*cs_)(iter)*(*s_)(iter);
221  (*s_)(iter+1) = -(*sn_)(iter)*(*s_)(iter);
222  (*s_)(iter) = temp;
223  (*H_)(iter,iter) = (*cs_)(iter)*(*H_)(iter,iter) + (*sn_)(iter)*(*H_)(iter+1,iter);
224  (*H_)(iter+1,iter) = zero;
225  (*res_)[iter+1] = std::abs((*s_)(iter+1));
226 
227  // Update solution approximation.
228  const char uplo = 'U';
229  const char trans = 'N';
230  const char diag = 'N';
231  const char normin = 'N';
232  Real scaling = zero;
233  int info = 0;
234  *y_ = *s_;
235  lapack_.LATRS(uplo, trans, diag, normin, iter+1, H_->values(), maxit_+1, y_->values(), &scaling, cnorm_->values(), &info);
236 
237  z_->zero();
238 
239  for( int k=0; k<=iter;++k ) {
240  z_->axpy((*y_)(k),*(Z[k]));
241  }
242 
243  if( (*res_)[iter+1] <= rtol ) {
244  // Update solution vector
245  x.plus(*z_);
246  break;
247  }
248 
249  if(iter == maxit_) {
250  flag = 1;
251  }
252  } // loop over iter
253 
254  }
255 
256 
257 }; // class GMRES
258 
259 } // namespace ROL
260 
261 #endif // ROL_GMRES_H
262 
bool isInitialized_
Definition: ROL_GMRES.hpp:83
virtual void plus(const Vector &x)=0
Compute , where .
Teuchos::RCP< SDVector > cs_
Definition: ROL_GMRES.hpp:75
bool useInexact_
Definition: ROL_GMRES.hpp:84
Contains definitions of custom data types in ROL.
GMRES(Teuchos::ParameterList &parlist)
Definition: ROL_GMRES.hpp:94
Teuchos::RCP< Vector< Real > > r_
Definition: ROL_GMRES.hpp:70
virtual void apply(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const =0
Apply linear operator.
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:157
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Preconditioned GMRES solver.
Definition: ROL_GMRES.hpp:63
Teuchos::RCP< SDVector > y_
Definition: ROL_GMRES.hpp:78
Teuchos::RCP< SDVector > cnorm_
Definition: ROL_GMRES.hpp:79
Teuchos::RCP< SDVector > sn_
Definition: ROL_GMRES.hpp:76
bool useInitialGuess_
Definition: ROL_GMRES.hpp:85
Teuchos::RCP< SDMatrix > H_
Definition: ROL_GMRES.hpp:74
virtual void applyInverse(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const
Apply inverse of linear operator.
void run(Vector< Real > &x, LinearOperator< Real > &A, const Vector< Real > &b, LinearOperator< Real > &M, int &iter, int &flag)
Definition: ROL_GMRES.hpp:121
Provides definitions for Krylov solvers.
Definition: ROL_Krylov.hpp:57
Teuchos::SerialDenseMatrix< int, Real > SDMatrix
Definition: ROL_GMRES.hpp:65
Provides the interface to apply a linear operator.
Teuchos::RCP< Vector< Real > > w_
Definition: ROL_GMRES.hpp:72
Real relTol_
Definition: ROL_GMRES.hpp:88
Teuchos::SerialDenseVector< int, Real > SDVector
Definition: ROL_GMRES.hpp:66
Teuchos::LAPACK< int, Real > lapack_
Definition: ROL_GMRES.hpp:90
Teuchos::RCP< std::vector< Real > > res_
Definition: ROL_GMRES.hpp:81
Teuchos::RCP< SDVector > s_
Definition: ROL_GMRES.hpp:77
Real absTol_
Definition: ROL_GMRES.hpp:87
Teuchos::RCP< Vector< Real > > z_
Definition: ROL_GMRES.hpp:71