ROL
step/krylov/test_03.cpp
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
48#include "ROL_StdVector.hpp"
49#include "ROL_GMRES.hpp"
50#include "ROL_KrylovFactory.hpp"
51#include "ROL_RandomVector.hpp"
52
53#include "ROL_Stream.hpp"
54#include "Teuchos_GlobalMPISession.hpp"
55
56#include<iomanip>
57
58// Identity operator for preconditioner
59template<class Real>
60class Identity : public ROL::LinearOperator<Real> {
62public:
63 void apply( V& Hv, const V& v, Real &tol ) const {
64 Hv.set(v);
65 }
66}; // class Identity
67
68
69// Apply a tridiagonal Toeplitz matrix to a ROL::StdVector to test Krylov solvers
70template<class Real>
72
73 typedef std::vector<Real> vector;
76
77 typedef typename vector::size_type uint;
78
79private:
80
81 Real a_; // subdiagonal
82 Real b_; // diagonal
83 Real c_; // superdiagonal
84
85 ROL::LAPACK<int,Real> lapack_;
86
87public:
88
89 TridiagonalToeplitzOperator( Real &a, Real &b, Real &c ) : a_(a), b_(b), c_(c) {}
90
91 // Tridiagonal multiplication
92 void apply( V &Hv, const V &v, Real &tol ) const {
93
94 SV &Hvs = dynamic_cast<SV&>(Hv);
95 ROL::Ptr<vector> Hvp = Hvs.getVector();
96
97 const SV &vs = dynamic_cast<const SV&>(v);
98 ROL::Ptr<const vector> vp = vs.getVector();
99
100 uint n = vp->size();
101
102 (*Hvp)[0] = b_*(*vp)[0] + c_*(*vp)[1];
103
104 for(uint k=1; k<n-1; ++k) {
105 (*Hvp)[k] = a_*(*vp)[k-1] + b_*(*vp)[k] + c_*(*vp)[k+1];
106 }
107
108 (*Hvp)[n-1] = a_*(*vp)[n-2] + b_*(*vp)[n-1];
109
110 }
111
112 // Tridiagonal solve - compare against GMRES
113 void applyInverse( V &Hv, const V &v, Real &tol ) const {
114
115
116
117 SV &Hvs = dynamic_cast<SV&>(Hv);
118 ROL::Ptr<vector> Hvp = Hvs.getVector();
119
120 const SV &vs = dynamic_cast<const SV&>(v);
121 ROL::Ptr<const vector> vp = vs.getVector();
122
123 uint n = vp->size();
124
125 const char TRANS = 'N';
126 const int NRHS = 1;
127
128 vector dl(n-1,a_);
129 vector d(n,b_);
130 vector du(n-1,c_);
131 vector du2(n-2,0.0);
132
133 std::vector<int> ipiv(n);
134 int info;
135
136 Hv.set(v); // LAPACK will modify this in place
137
138 // Do Tridiagonal LU factorization
139 lapack_.GTTRF(n,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&info);
140
141 // Solve the system with the LU factors
142 lapack_.GTTRS(TRANS,n,NRHS,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&(*Hvp)[0],n,&info);
143
144 }
145
146}; // class TridiagonalToeplitzOperator
147
148
149
150typedef double RealT;
151
152int main(int argc, char *argv[]) {
153
154 typedef std::vector<RealT> vector;
155 typedef ROL::StdVector<RealT> SV;
156
157 typedef typename vector::size_type uint;
158
159 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
160
161 int iprint = argc - 1;
162 ROL::Ptr<std::ostream> outStream;
163 ROL::nullstream bhs; // outputs nothing
164 if (iprint > 0)
165 outStream = ROL::makePtrFromRef(std::cout);
166 else
167 outStream = ROL::makePtrFromRef(bhs);
168
169 int errorFlag = 0;
170
171 try {
172
173 ROL::ParameterList parlist;
174 ROL::ParameterList &gList = parlist.sublist("General");
175 ROL::ParameterList &kList = gList.sublist("Krylov");
176
177 kList.set("Type","MINRES");
178 kList.set("Iteration Limit",20);
179 kList.set("Absolute Tolerance",1.e-8);
180 kList.set("Relative Tolerance",1.e-6);
181 kList.set("Use Initial Guess",false);
182
183 uint dim = 10;
184
185 auto xp = ROL::makePtr<vector>(dim,0.0);
186 auto yp = ROL::makePtr<vector>(dim,0.0);
187 auto zp = ROL::makePtr<vector>(dim,0.0);
188 auto bp = ROL::makePtr<vector>(dim,0.0);
189
190 SV x(xp); // Exact solution
191 SV y(yp); // Solution using direct solve
192 SV z(zp); // Solution using GMRES
193
194 SV b(bp); // Right-hand-side
195
196 RealT left = -1.0;
197 RealT right = 1.0;
198
199 ROL::RandomizeVector(x,left,right);
200
201 RealT sub = 1.0;
202 RealT diag = -2.0;
203 RealT super = 1.0;
204
205 TridiagonalToeplitzOperator<RealT> T(sub,diag,super);
207
208 RealT tol = 0.0;
209
210 T.apply(b,x,tol);
211
212 T.applyInverse(y,b,tol);
213
214 auto krylov = ROL::KrylovFactory<RealT>( parlist );
215
216 int iter;
217 int flag;
218 z.zero();
219 krylov->run(z,T,b,I,iter,flag);
220
221 *outStream << std::setw(10) << "Exact"
222 << std::setw(10) << "LAPACK"
223 << std::setw(10) << "MINRES" << std::endl;
224 *outStream << "---------------------------------" << std::endl;
225
226 for(uint k=0;k<dim;++k) {
227 *outStream << std::setw(10) << (*xp)[k] << " "
228 << std::setw(10) << (*yp)[k] << " "
229 << std::setw(10) << (*zp)[k] << " " << std::endl;
230 }
231
232 *outStream << "MINRES performed " << iter << " iterations." << std::endl;
233
234 z.axpy(-1.0,x);
235
236 if( z.norm() > std::sqrt(ROL::ROL_EPSILON<RealT>()) ) {
237 ++errorFlag;
238 }
239
240 }
241 catch (std::logic_error& err) {
242 *outStream << err.what() << "\n";
243 errorFlag = -1000;
244 }; // end try
245
246 if (errorFlag != 0)
247 std::cout << "End Result: TEST FAILED\n";
248 else
249 std::cout << "End Result: TEST PASSED\n";
250
251 return 0;
252}
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
ROL::Vector< Real > V
void apply(V &Hv, const V &v, Real &tol) const
Apply linear operator.
Provides the interface to apply a linear operator.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Ptr< const std::vector< Element > > getVector() const
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:84
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:209
void apply(V &Hv, const V &v, Real &tol) const
Apply linear operator.
TridiagonalToeplitzOperator(Real &a, Real &b, Real &c)
void applyInverse(V &Hv, const V &v, Real &tol) const
Apply inverse of linear operator.
ROL::LAPACK< int, Real > lapack_
void RandomizeVector(Vector< Real > &x, const Real &lower=0.0, const Real &upper=1.0)
Fill a ROL::Vector with uniformly-distributed random numbers in the interval [lower,...
int main(int argc, char *argv[])
double RealT
constexpr auto dim