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_Types.hpp"
53
54namespace ROL {
55
56template<class Real>
57class ConjugateGradients : 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
66public:
67 ConjugateGradients(Real absTol = 1.e-4, Real relTol = 1.e-2, unsigned maxit = 100, bool useInexact = false)
68 : Krylov<Real>(absTol,relTol,maxit), isInitialized_(false), useInexact_(useInexact) {}
69
71 int &iter, int &flag ) {
72 if ( !isInitialized_ ) {
73 r_ = b.clone();
74 v_ = x.clone();
75 p_ = x.clone();
76 Ap_ = b.clone();
77 isInitialized_ = true;
78 }
79
80 Real rnorm = b.norm();
82 Real itol = std::sqrt(ROL_EPSILON<Real>());
83
84 x.zero();
85 r_->set(b);
86
87 M.applyInverse(*v_, *r_, itol);
88 p_->set(*v_);
89
90 iter = 0;
91 flag = 0;
92
93 Real kappa(0), beta(0), alpha(0), tmp(0), zero(0);
94 //Real gv = v_->dot(r_->dual());
95 Real gv = v_->apply(*r_);
96
97 for (iter = 0; iter < (int)Krylov<Real>::getMaximumIteration(); iter++) {
98 if ( useInexact_ ) {
99 itol = rtol/((Real)Krylov<Real>::getMaximumIteration() * rnorm);
100 }
101 A.apply(*Ap_, *p_, itol);
102
103 //kappa = p_->dot(Ap_->dual());
104 kappa = p_->apply(*Ap_);
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 gv = v_->apply(*r_);
124 beta = gv/tmp;
125
126 p_->scale(beta);
127 p_->plus(*v_);
128 }
129 if ( iter == (int)Krylov<Real>::getMaximumIteration() ) {
130 flag = 1;
131 }
132 else {
133 iter++;
134 }
135 return rnorm;
136 }
137};
138
139
140}
141
142#endif
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Contains definitions of custom data types in ROL.
Provides definitions of the Conjugate Gradient solver.
ROL::Ptr< Vector< Real > > r_
ROL::Ptr< Vector< Real > > v_
ROL::Ptr< Vector< Real > > Ap_
ROL::Ptr< Vector< Real > > p_
ConjugateGradients(Real absTol=1.e-4, Real relTol=1.e-2, unsigned maxit=100, bool useInexact=false)
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.
virtual void apply(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const =0
Apply linear operator.
virtual void applyInverse(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const
Apply inverse of linear operator.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:84
virtual Real norm() const =0
Returns where .
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:167
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