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
54namespace ROL {
55
56template<class Real>
57class 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
67public:
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 Real gHg = r_->apply(*v_);
113
114 for (iter = 0; iter < (int)Krylov<Real>::getMaximumIteration(); iter++) {
115 itol = std::sqrt(ROL_EPSILON<Real>());
116 M.applyInverse(*MAp_, *Ap_, itol);
117 //kappa = MAp_->dot(Ap_->dual());
118 kappa = MAp_->apply(*Ap_);
119 //if ( gHg <= 0.0 || kappa <= 0.0 ) {
120 //flag = 2;
121 //break;
122 //}
123 alpha = gHg/kappa;
124
125 x.axpy(alpha,*p_);
126
127 r_->axpy(-alpha,*MAp_);
128 rnorm = r_->norm();
129 if ( rnorm < rtol ) {
130 break;
131 }
132
133 if ( useInexact_ ) {
134 itol = rtol/((Real)Krylov<Real>::getMaximumIteration() * rnorm);
135 }
136 A.apply(*v_, *r_, itol);
137 tmp = gHg;
138 //gHg = r_->dot(v_->dual());
139 gHg = r_->apply(*v_);
140 beta = gHg/tmp;
141
142 p_->scale(beta);
143 p_->plus(*r_);
144
145 Ap_->scale(beta);
146 Ap_->plus(*v_);
147 }
148 if ( iter == (int)Krylov<Real>::getMaximumIteration() ) {
149 flag = 1;
150 }
151 else {
152 iter++;
153 }
154 return rnorm;
155 }
156};
157
158
159}
160
161#endif
Contains definitions of custom data types in ROL.
Provides definition of the Conjugate Residual solver.
Real run(Vector< Real > &x, LinearOperator< Real > &A, const Vector< Real > &b, LinearOperator< Real > &M, int &iter, int &flag)
ROL::Ptr< Vector< Real > > MAp_
ROL::Ptr< Vector< Real > > p_
ROL::Ptr< Vector< Real > > r_
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 > > Ap_
Provides definitions for Krylov solvers.
unsigned getMaximumIteration(void) const
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.
virtual Real norm() const =0
Returns where .
virtual void zero()
Set to zero vector.
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 .