ROL
ROL_ConjugateGradients.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_CONJUGATEGRADIENTS_H
45 #define ROL_CONJUGATEGRADIENTS_H
46 
51 #include "ROL_Krylov.hpp"
52 #include "ROL_LinearOperator.hpp"
53 #include "ROL_Vector.hpp"
54 #include "ROL_Types.hpp"
55 
56 namespace ROL {
57 
58 template<class Real>
59 class ConjugateGradients : public Krylov<Real> {
60 
63  ROL::Ptr<Vector<Real> > r_;
64  ROL::Ptr<Vector<Real> > v_;
65  ROL::Ptr<Vector<Real> > p_;
66  ROL::Ptr<Vector<Real> > Ap_;
67 
68 public:
69  ConjugateGradients(Real absTol = 1.e-4, Real relTol = 1.e-2, unsigned maxit = 100, bool useInexact = false)
70  : Krylov<Real>(absTol,relTol,maxit), isInitialized_(false), useInexact_(useInexact) {}
71 
73  int &iter, int &flag ) {
74  if ( !isInitialized_ ) {
75  r_ = b.clone();
76  v_ = x.clone();
77  p_ = x.clone();
78  Ap_ = b.clone();
79  isInitialized_ = true;
80  }
81 
82  Real rnorm = b.norm();
84  Real itol = std::sqrt(ROL_EPSILON<Real>());
85 
86  x.zero();
87  r_->set(b);
88 
89  M.applyInverse(*v_, *r_, itol);
90  p_->set(*v_);
91 
92  iter = 0;
93  flag = 0;
94 
95  Real kappa(0), beta(0), alpha(0), tmp(0), zero(0);
96  Real gv = v_->dot(r_->dual());
97 
98  for (iter = 0; iter < (int)Krylov<Real>::getMaximumIteration(); iter++) {
99  if ( useInexact_ ) {
100  itol = rtol/((Real)Krylov<Real>::getMaximumIteration() * rnorm);
101  }
102  A.apply(*Ap_, *p_, itol);
103 
104  kappa = p_->dot(Ap_->dual());
105  if ( kappa <= zero ) {
106  flag = 2;
107  break;
108  }
109  alpha = gv/kappa;
110 
111  x.axpy(alpha,*p_);
112 
113  r_->axpy(-alpha,*Ap_);
114  rnorm = r_->norm();
115  if ( rnorm < rtol ) {
116  break;
117  }
118 
119  itol = std::sqrt(ROL_EPSILON<Real>());
120  M.applyInverse(*v_, *r_, itol);
121  tmp = gv;
122  gv = v_->dot(r_->dual());
123  beta = gv/tmp;
124 
125  p_->scale(beta);
126  p_->plus(*v_);
127  }
128  if ( iter == (int)Krylov<Real>::getMaximumIteration() ) {
129  flag = 1;
130  }
131  else {
132  iter++;
133  }
134  return rnorm;
135  }
136 };
137 
138 
139 }
140 
141 #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: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
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
ROL::Ptr< Vector< Real > > v_
virtual void applyInverse(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const
Apply inverse of linear operator.
Provides definitions of the Conjugate Gradient solver.
Provides definitions for Krylov solvers.
Definition: ROL_Krylov.hpp:58
Provides the interface to apply a linear operator.
ConjugateGradients(Real absTol=1.e-4, Real relTol=1.e-2, unsigned maxit=100, bool useInexact=false)
ROL::Ptr< Vector< Real > > p_
ROL::Ptr< Vector< Real > > r_
virtual Real norm() const =0
Returns where .
ROL::Ptr< Vector< Real > > Ap_
Real run(Vector< Real > &x, LinearOperator< Real > &A, const Vector< Real > &b, LinearOperator< Real > &M, int &iter, int &flag)