24 #include "Teuchos_GlobalMPISession.hpp"
31 int main(
int argc,
char *argv[]) {
33 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
36 int iprint = argc - 1;
37 ROL::Ptr<std::ostream> outStream;
40 outStream = ROL::makePtrFromRef(std::cout);
42 outStream = ROL::makePtrFromRef(bhs);
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);
70 ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(
dim, 0.0);
72 for (
int i=0; i<
dim; i++) {
78 algo.run(x, obj,
true, *outStream);
82 H = ROL::computeDenseHessian<RealT>(obj, x);
86 std::vector<std::vector<RealT> > eigenvals = ROL::computeEigenvalues<RealT>(H);
88 *outStream <<
"\nEigenvalues:\n";
89 for (
unsigned i=0; i<(eigenvals[0]).size(); i++) {
91 *outStream << std::right
92 << std::setw(28) <<
"Real"
93 << std::setw(28) <<
"Imag"
96 *outStream << std::scientific << std::setprecision(16) << std::right
97 << std::setw(28) << (eigenvals[0])[i]
98 << std::setw(28) << (eigenvals[1])[i]
105 std::vector<std::vector<RealT> > genEigenvals = ROL::computeGenEigenvalues<RealT>(H, M);
107 *outStream <<
"\nGeneralized eigenvalues:\n";
108 for (
unsigned i=0; i<(genEigenvals[0]).size(); i++) {
110 *outStream << std::right
111 << std::setw(28) <<
"Real"
112 << std::setw(28) <<
"Imag"
115 *outStream << std::scientific << std::setprecision(16) << std::right
116 << std::setw(28) << (genEigenvals[0])[i]
117 << std::setw(28) << (genEigenvals[1])[i]
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());
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>()) ) {
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";
133 if ( std::abs( (genEigenvals[1])[i] - (eigenvals[1])[i] ) > errtol*((eigenvals[1])[i]+ROL::ROL_THRESHOLD<RealT>()) ) {
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";
140 Teuchos::SerialDenseMatrix<int, RealT> invH = ROL::computeInverse<RealT>(H);
141 Teuchos::SerialDenseMatrix<int, RealT> HinvH(H);
144 HinvH.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, H, invH, 0.0);
147 if (HinvH.normOne() > errtol) {
149 *outStream << std::scientific << std::setprecision(20) <<
"1-norm of H*inv(H) - I = " << HinvH.normOne() <<
" > " << errtol <<
"\n";
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);
162 for (
int i=0; i<
dim; i++) {
167 newton_algo.run(x, obj,
true, *outStream);
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 ) {
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";
179 catch (std::logic_error& err) {
180 *outStream << err.what() <<
"\n";
185 std::cout <<
"End Result: TEST FAILED\n";
187 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...
basic_nullstream< char, std::char_traits< char >> nullstream
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)
int main(int argc, char *argv[])
Poisson material inversion.