ROL
poisson-inversion/example_01.cpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Rapid Optimization Library (ROL) Package
4//
5// Copyright 2014 NTESS and the ROL contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
15#define USE_HESSVEC 1
16
17#include "ROL_Types.hpp"
19#include "ROL_Algorithm.hpp"
22#include "ROL_StatusTest.hpp"
23#include "ROL_Stream.hpp"
24#include "ROL_GlobalMPISession.hpp"
25
26#include <iostream>
27#include <algorithm>
28
29typedef double RealT;
30
31int main(int argc, char *argv[]) {
32
33 ROL::GlobalMPISession mpiSession(&argc, &argv);
34
35 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
36 int iprint = argc - 1;
37 ROL::Ptr<std::ostream> outStream;
38 ROL::nullstream bhs; // outputs nothing
39 if (iprint > 0)
40 outStream = ROL::makePtrFromRef(std::cout);
41 else
42 outStream = ROL::makePtrFromRef(bhs);
43
44 int errorFlag = 0;
45
46 // *** Example body.
47
48 try {
49
50 int dim = 128; // Set problem dimension.
52
53 // Define algorithm.
54 ROL::ParameterList parlist;
55 std::string stepname = "Trust Region";
56 parlist.sublist("Step").sublist(stepname).set("Subproblem Solver", "Truncated CG");
57 parlist.sublist("General").sublist("Krylov").set("Iteration Limit",50);
58 parlist.sublist("General").sublist("Krylov").set("Relative Tolerance",1e-2);
59 parlist.sublist("General").sublist("Krylov").set("Absolute Tolerance",1e-4);
60 parlist.sublist("Status Test").set("Gradient Tolerance",1.e-12);
61 parlist.sublist("Status Test").set("Step Tolerance",1.e-14);
62 parlist.sublist("Status Test").set("Iteration Limit",100);
63 ROL::Ptr<ROL::Step<RealT>>
64 step = ROL::makePtr<ROL::TrustRegionStep<RealT>>(parlist);
65 ROL::Ptr<ROL::StatusTest<RealT>>
66 status = ROL::makePtr<ROL::StatusTest<RealT>>(parlist);
67 ROL::Algorithm<RealT> algo(step,status,false);
68
69 // Iteration vector.
70 ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
71 // Set initial guess.
72 for (int i=0; i<dim; i++) {
73 (*x_ptr)[i] = 0.1;
74 }
75 ROL::StdVector<RealT> x(x_ptr);
76
77 // Run algorithm.
78 algo.run(x, obj, true, *outStream);
79
80 // Compute dense Hessian matrix.
81 Teuchos::SerialDenseMatrix<int, RealT> H(x.dimension(), x.dimension());
82 H = ROL::computeDenseHessian<RealT>(obj, x);
83 //H.print(*outStream);
84
85 // Compute and print eigenvalues.
86 std::vector<std::vector<RealT> > eigenvals = ROL::computeEigenvalues<RealT>(H);
87
88 *outStream << "\nEigenvalues:\n";
89 for (unsigned i=0; i<(eigenvals[0]).size(); i++) {
90 if (i==0) {
91 *outStream << std::right
92 << std::setw(28) << "Real"
93 << std::setw(28) << "Imag"
94 << "\n";
95 }
96 *outStream << std::scientific << std::setprecision(16) << std::right
97 << std::setw(28) << (eigenvals[0])[i]
98 << std::setw(28) << (eigenvals[1])[i]
99 << "\n";
100 }
101
102 // Compute and print generalized eigenvalues.
103 Teuchos::SerialDenseMatrix<int, RealT> M = computeDotMatrix(x);
104 //M.print(*outStream);
105 std::vector<std::vector<RealT> > genEigenvals = ROL::computeGenEigenvalues<RealT>(H, M);
106
107 *outStream << "\nGeneralized eigenvalues:\n";
108 for (unsigned i=0; i<(genEigenvals[0]).size(); i++) {
109 if (i==0) {
110 *outStream << std::right
111 << std::setw(28) << "Real"
112 << std::setw(28) << "Imag"
113 << "\n";
114 }
115 *outStream << std::scientific << std::setprecision(16) << std::right
116 << std::setw(28) << (genEigenvals[0])[i]
117 << std::setw(28) << (genEigenvals[1])[i]
118 << "\n";
119 }
120
121 // Sort and compare eigenvalues and generalized eigenvalues - should be close.
122 std::sort((eigenvals[0]).begin(), (eigenvals[0]).end());
123 std::sort((eigenvals[1]).begin(), (eigenvals[1]).end());
124 std::sort((genEigenvals[0]).begin(), (genEigenvals[0]).end());
125 std::sort((genEigenvals[1]).begin(), (genEigenvals[1]).end());
126
127 RealT errtol = std::sqrt(ROL::ROL_EPSILON<RealT>());
128 for (unsigned i=0; i<(eigenvals[0]).size(); i++) {
129 if ( std::abs( (genEigenvals[0])[i] - (eigenvals[0])[i] ) > errtol*((eigenvals[0])[i]+ROL::ROL_THRESHOLD<RealT>()) ) {
130 errorFlag++;
131 *outStream << std::scientific << std::setprecision(20) << "Real genEigenvals - eigenvals (" << i << ") = " << std::abs( (genEigenvals[0])[i] - (eigenvals[0])[i] ) << " > " << errtol*((eigenvals[0])[i]+1e4*ROL::ROL_THRESHOLD<RealT>()) << "\n";
132 }
133 if ( std::abs( (genEigenvals[1])[i] - (eigenvals[1])[i] ) > errtol*((eigenvals[1])[i]+ROL::ROL_THRESHOLD<RealT>()) ) {
134 errorFlag++;
135 *outStream << std::scientific << std::setprecision(20) << "Imag genEigenvals - eigenvals (" << i << ") = " << std::abs( (genEigenvals[1])[i] - (eigenvals[1])[i] ) << " > " << errtol*((eigenvals[1])[i]+ROL::ROL_THRESHOLD<RealT>()) << "\n";
136 }
137 }
138
139 // Compute inverse of Hessian.
140 Teuchos::SerialDenseMatrix<int, RealT> invH = ROL::computeInverse<RealT>(H);
141 Teuchos::SerialDenseMatrix<int, RealT> HinvH(H);
142
143 // Multiply with Hessian and verify that it gives the identity (l2 dot matrix M from above).
144 HinvH.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, H, invH, 0.0);
145 //*outStream << std::scientific << std::setprecision(6); HinvH.print(*outStream);
146 HinvH -= M;
147 if (HinvH.normOne() > errtol) {
148 errorFlag++;
149 *outStream << std::scientific << std::setprecision(20) << "1-norm of H*inv(H) - I = " << HinvH.normOne() << " > " << errtol << "\n";
150 }
151
152 // Use Newton algorithm with line search.
153 stepname = "Line Search";
154 parlist.sublist("Step").sublist(stepname).sublist("Descent Method").set("Type", "Newton's Method");
155 ROL::Ptr<ROL::Step<RealT>>
156 newton_step = ROL::makePtr<ROL::LineSearchStep<RealT>>(parlist);
157 ROL::Ptr<ROL::StatusTest<RealT>>
158 newton_status = ROL::makePtr<ROL::StatusTest<RealT>>(parlist);
159 ROL::Algorithm<RealT> newton_algo(newton_step,newton_status,false);
160
161 // Reset initial guess.
162 for (int i=0; i<dim; i++) {
163 (*x_ptr)[i] = 0.1;
164 }
165
166 // Run Newton algorithm.
167 newton_algo.run(x, obj, true, *outStream);
168
169 ROL::Ptr<const ROL::AlgorithmState<RealT> > new_state = newton_algo.getState();
170 ROL::Ptr<const ROL::AlgorithmState<RealT> > old_state = algo.getState();
171 *outStream << "old_optimal_value = " << old_state->value << std::endl;
172 *outStream << "new_optimal_value = " << new_state->value << std::endl;
173 if ( std::abs(new_state->value - old_state->value) / std::abs(old_state->value) > errtol ) {
174 errorFlag++;
175 *outStream << std::scientific << std::setprecision(20) << "\nabs(new_optimal_value - old_optimal_value) / abs(old_optimal_value) = " << std::abs(new_state->value - old_state->value) / std::abs(old_state->value) << " > " << errtol << "\n";
176 }
177
178 }
179 catch (std::logic_error& err) {
180 *outStream << err.what() << "\n";
181 errorFlag = -1000;
182 }; // end try
183
184 if (errorFlag != 0)
185 std::cout << "End Result: TEST FAILED\n";
186 else
187 std::cout << "End Result: TEST PASSED\n";
188
189 return 0;
190
191}
192
Contains definitions for Poisson material inversion.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Contains definitions of custom data types in ROL.
Provides an interface to run optimization algorithms.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
int dimension() const
Return dimension of the vector space.
int main(int argc, char *argv[])
constexpr auto dim