58 #include "Teuchos_GlobalMPISession.hpp"
65 int main(
int argc,
char *argv[]) {
67 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
70 int iprint = argc - 1;
71 ROL::Ptr<std::ostream> outStream;
74 outStream = ROL::makePtrFromRef(std::cout);
76 outStream = ROL::makePtrFromRef(bhs);
88 ROL::ParameterList parlist;
89 std::string stepname =
"Trust Region";
90 parlist.sublist(
"Step").sublist(stepname).set(
"Subproblem Solver",
"Truncated CG");
91 parlist.sublist(
"General").sublist(
"Krylov").set(
"Iteration Limit",50);
92 parlist.sublist(
"General").sublist(
"Krylov").set(
"Relative Tolerance",1e-2);
93 parlist.sublist(
"General").sublist(
"Krylov").set(
"Absolute Tolerance",1e-4);
94 parlist.sublist(
"Status Test").set(
"Gradient Tolerance",1.e-12);
95 parlist.sublist(
"Status Test").set(
"Step Tolerance",1.e-14);
96 parlist.sublist(
"Status Test").set(
"Iteration Limit",100);
97 ROL::Ptr<ROL::Step<RealT>>
98 step = ROL::makePtr<ROL::TrustRegionStep<RealT>>(parlist);
99 ROL::Ptr<ROL::StatusTest<RealT>>
100 status = ROL::makePtr<ROL::StatusTest<RealT>>(parlist);
104 ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(
dim, 0.0);
106 for (
int i=0; i<
dim; i++) {
112 algo.run(x, obj,
true, *outStream);
116 H = ROL::computeDenseHessian<RealT>(obj, x);
120 std::vector<std::vector<RealT> > eigenvals = ROL::computeEigenvalues<RealT>(H);
122 *outStream <<
"\nEigenvalues:\n";
123 for (
unsigned i=0; i<(eigenvals[0]).size(); i++) {
125 *outStream << std::right
126 << std::setw(28) <<
"Real"
127 << std::setw(28) <<
"Imag"
130 *outStream << std::scientific << std::setprecision(16) << std::right
131 << std::setw(28) << (eigenvals[0])[i]
132 << std::setw(28) << (eigenvals[1])[i]
139 std::vector<std::vector<RealT> > genEigenvals = ROL::computeGenEigenvalues<RealT>(H, M);
141 *outStream <<
"\nGeneralized eigenvalues:\n";
142 for (
unsigned i=0; i<(genEigenvals[0]).size(); i++) {
144 *outStream << std::right
145 << std::setw(28) <<
"Real"
146 << std::setw(28) <<
"Imag"
149 *outStream << std::scientific << std::setprecision(16) << std::right
150 << std::setw(28) << (genEigenvals[0])[i]
151 << std::setw(28) << (genEigenvals[1])[i]
156 std::sort((eigenvals[0]).begin(), (eigenvals[0]).end());
157 std::sort((eigenvals[1]).begin(), (eigenvals[1]).end());
158 std::sort((genEigenvals[0]).begin(), (genEigenvals[0]).end());
159 std::sort((genEigenvals[1]).begin(), (genEigenvals[1]).end());
161 RealT errtol = std::sqrt(ROL::ROL_EPSILON<RealT>());
162 for (
unsigned i=0; i<(eigenvals[0]).size(); i++) {
163 if ( std::abs( (genEigenvals[0])[i] - (eigenvals[0])[i] ) > errtol*((eigenvals[0])[i]+ROL::ROL_THRESHOLD<RealT>()) ) {
165 *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";
167 if ( std::abs( (genEigenvals[1])[i] - (eigenvals[1])[i] ) > errtol*((eigenvals[1])[i]+ROL::ROL_THRESHOLD<RealT>()) ) {
169 *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";
174 Teuchos::SerialDenseMatrix<int, RealT> invH = ROL::computeInverse<RealT>(H);
175 Teuchos::SerialDenseMatrix<int, RealT> HinvH(H);
178 HinvH.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, H, invH, 0.0);
181 if (HinvH.normOne() > errtol) {
183 *outStream << std::scientific << std::setprecision(20) <<
"1-norm of H*inv(H) - I = " << HinvH.normOne() <<
" > " << errtol <<
"\n";
187 stepname =
"Line Search";
188 parlist.sublist(
"Step").sublist(stepname).sublist(
"Descent Method").set(
"Type",
"Newton-Krylov");
189 ROL::Ptr<ROL::Step<RealT>>
190 newton_step = ROL::makePtr<ROL::LineSearchStep<RealT>>(parlist);
191 ROL::Ptr<ROL::StatusTest<RealT>>
192 newton_status = ROL::makePtr<ROL::StatusTest<RealT>>(parlist);
196 for (
int i=0; i<
dim; i++) {
201 newton_algo.run(x, obj,
true, *outStream);
203 ROL::Ptr<const ROL::AlgorithmState<RealT> > new_state = newton_algo.getState();
204 ROL::Ptr<const ROL::AlgorithmState<RealT> > old_state = algo.getState();
205 *outStream <<
"old_optimal_value = " << old_state->value << std::endl;
206 *outStream <<
"new_optimal_value = " << new_state->value << std::endl;
207 if ( std::abs(new_state->value - old_state->value) / std::abs(old_state->value) > errtol ) {
209 *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";
213 catch (std::logic_error& err) {
214 *outStream << err.what() <<
"\n";
219 std::cout <<
"End Result: TEST FAILED\n";
221 std::cout <<
"End Result: TEST PASSED\n";
Contains definitions of custom data types in ROL.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Contains definitions for Poisson material inversion.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Provides an interface to run optimization algorithms.
int dimension() const
Return dimension of the vector space.
Teuchos::SerialDenseMatrix< int, Real > computeDotMatrix(const Vector< Real > &x)
basic_nullstream< char, char_traits< char >> nullstream
int main(int argc, char *argv[])
Poisson material inversion.