ROL
ROL_ConjugateResiduals.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_CONJUGATERESIDUALS_H
45 #define ROL_CONJUGATERESIDUALS_H
46 
51 #include "ROL_Krylov.hpp"
52 #include "ROL_Types.hpp"
53 
54 namespace ROL {
55 
56 template<class Real>
57 class ConjugateResiduals : public Krylov<Real> {
58 
61  ROL::Ptr<Vector<Real> > r_;
62  ROL::Ptr<Vector<Real> > v_;
63  ROL::Ptr<Vector<Real> > p_;
64  ROL::Ptr<Vector<Real> > Ap_;
65  ROL::Ptr<Vector<Real> > MAp_;
66 
67 public:
68  ConjugateResiduals( Real absTol = 1.e-4, Real relTol = 1.e-2, int maxit = 100, bool useInexact = false )
69  : Krylov<Real>(absTol,relTol,maxit), isInitialized_(false), useInexact_(useInexact) {}
70 
71  // Run Krylov Method
73  int &iter, int &flag ) {
74  if ( !isInitialized_ ) {
75  r_ = x.clone();
76  v_ = b.clone();
77  p_ = x.clone();
78  Ap_ = b.clone();
79  MAp_ = x.clone();
80  isInitialized_ = true;
81  }
82 
83  // Initialize
84  Real rnorm = b.norm();
86  Real itol = std::sqrt(ROL_EPSILON<Real>());
87  x.zero();
88 
89  // Apply preconditioner to residual
90  M.applyInverse(*r_,b,itol);
91 
92  // Initialize direction p
93  p_->set(*r_);
94 
95  // Get Hessian tolerance
96  if ( useInexact_ ) {
97  itol = rtol/((Real)Krylov<Real>::getMaximumIteration() * rnorm);
98  }
99 
100  // Apply Hessian to residual
101  A.apply(*v_, *r_, itol);
102 
103  // Apply Hessian to direction p
104  //A.apply(*Ap_, *p_, itol);
105  Ap_->set(*v_);
106 
107  // Initialize scalar quantities
108  iter = 0;
109  flag = 0;
110  Real kappa(0), beta(0), alpha(0), tmp(0);
111  Real gHg = r_->dot(v_->dual());
112 
113  for (iter = 0; iter < (int)Krylov<Real>::getMaximumIteration(); iter++) {
114  itol = std::sqrt(ROL_EPSILON<Real>());
115  M.applyInverse(*MAp_, *Ap_, itol);
116  kappa = MAp_->dot(Ap_->dual());
117  //if ( gHg <= 0.0 || kappa <= 0.0 ) {
118  //flag = 2;
119  //break;
120  //}
121  alpha = gHg/kappa;
122 
123  x.axpy(alpha,*p_);
124 
125  r_->axpy(-alpha,*MAp_);
126  rnorm = r_->norm();
127  if ( rnorm < rtol ) {
128  break;
129  }
130 
131  if ( useInexact_ ) {
132  itol = rtol/((Real)Krylov<Real>::getMaximumIteration() * rnorm);
133  }
134  A.apply(*v_, *r_, itol);
135  tmp = gHg;
136  gHg = r_->dot(v_->dual());
137  beta = gHg/tmp;
138 
139  p_->scale(beta);
140  p_->plus(*r_);
141 
142  Ap_->scale(beta);
143  Ap_->plus(*v_);
144  }
145  if ( iter == (int)Krylov<Real>::getMaximumIteration() ) {
146  flag = 1;
147  }
148  else {
149  iter++;
150  }
151  return rnorm;
152  }
153 };
154 
155 
156 }
157 
158 #endif
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
Provides definition of the Conjugate Residual solver.
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:153
Contains definitions of custom data types in ROL.
virtual void apply(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const =0
Apply linear operator.
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:167
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
ROL::Ptr< Vector< Real > > r_
ROL::Ptr< Vector< Real > > Ap_
virtual void applyInverse(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const
Apply inverse of linear operator.
Real run(Vector< Real > &x, LinearOperator< Real > &A, const Vector< Real > &b, LinearOperator< Real > &M, int &iter, int &flag)
Provides definitions for Krylov solvers.
Definition: ROL_Krylov.hpp:58
Provides the interface to apply a linear operator.
ROL::Ptr< Vector< Real > > v_
ConjugateResiduals(Real absTol=1.e-4, Real relTol=1.e-2, int maxit=100, bool useInexact=false)
ROL::Ptr< Vector< Real > > p_
ROL::Ptr< Vector< Real > > MAp_
virtual Real norm() const =0
Returns where .