ROL
burgers-control/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
17#include "ROL_Bounds.hpp"
18
19#include "ROL_GlobalMPISession.hpp"
20
21#include <iostream>
22#include <fstream>
23#include <algorithm>
24
25#include "ROL_Stream.hpp"
26
27#include "example_01.hpp"
28
29typedef double RealT;
30
31int main(int argc, char *argv[]) {
32
33 typedef std::vector<RealT> vector;
34 typedef ROL::Vector<RealT> V;
35 typedef ROL::StdVector<RealT> SV;
36
37 typedef typename vector::size_type luint;
38
39 ROL::GlobalMPISession mpiSession(&argc, &argv);
40
41 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
42 int iprint = argc - 1;
43 ROL::Ptr<std::ostream> outStream;
44 ROL::nullstream bhs; // outputs nothing
45 if (iprint > 0)
46 outStream = ROL::makePtrFromRef(std::cout);
47 else
48 outStream = ROL::makePtrFromRef(bhs);
49
50 int errorFlag = 0;
51
52 // *** Example body.
53
54 try {
55 // Initialize objective function.
56 luint nx = 1028; // Set spatial discretization.
57 RealT alpha = 1.e-3; // Set penalty parameter.
59 // Initialize iteration vectors.
60 ROL::Ptr<vector> x_ptr = ROL::makePtr<vector>(nx+2, 1.0);
61 ROL::Ptr<vector> y_ptr = ROL::makePtr<vector>(nx+2, 0.0);
62 for (luint i=0; i<nx+2; i++) {
63 (*x_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
64 (*y_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
65 }
66
67 SV x(x_ptr);
68 SV y(y_ptr);
69
70 // Check derivatives.
71 obj.checkGradient(x,x,y,true,*outStream);
72 obj.checkHessVec(x,x,y,true,*outStream);
73
74 // Initialize Constraints
75 ROL::Ptr<vector> l_ptr = ROL::makePtr<vector>(nx+2,0.0);
76 ROL::Ptr<vector> u_ptr = ROL::makePtr<vector>(nx+2,1.0);
77 ROL::Ptr<V> lo = ROL::makePtr<SV>(l_ptr);
78 ROL::Ptr<V> up = ROL::makePtr<SV>(u_ptr);
79
80 ROL::Bounds<RealT> bcon(lo,up);
81
82 // Primal dual active set.
83 std::string filename = "input.xml";
84 auto parlist = ROL::getParametersFromXmlFile( filename );
85
86 // Krylov parameters.
87 parlist->sublist("General").sublist("Krylov").set("Absolute Tolerance",1.e-8);
88 parlist->sublist("General").sublist("Krylov").set("Relative Tolerance",1.e-4);
89 parlist->sublist("General").sublist("Krylov").set("Iteration Limit",50);
90 // PDAS parameters.
91 parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Step Tolerance",1.e-10);
92 parlist->sublist("Step").sublist("Primal Dual Active Set").set("Relative Gradient Tolerance",1.e-8);
93 parlist->sublist("Step").sublist("Primal Dual Active Set").set("Iteration Limit", 10);
94 parlist->sublist("Step").sublist("Primal Dual Active Set").set("Dual Scaling",(alpha>0.0)?alpha:1.e-4);
95 // Status test parameters.
96 parlist->sublist("Status Test").set("Gradient Tolerance",1.e-12);
97 parlist->sublist("Status Test").set("Step Tolerance",1.e-16);
98 parlist->sublist("Status Test").set("Iteration Limit",100);
99 // Set initial guess.
100 x.zero();
101 {
102 // Define algorithm.
104 // Run algorithm.
105 algo.run(x, obj, bcon, *outStream);
106 }
107 // Output control to file.
108 std::ofstream file_pdas;
109 file_pdas.open("control_PDAS.txt");
110 for ( unsigned i = 0; i < (unsigned)nx+2; i++ ) {
111 file_pdas << (*x_ptr)[i] << "\n";
112 }
113 file_pdas.close();
114
115 // Projected Newton.
116 parlist->sublist("General").sublist("Krylov").set("Absolute Tolerance",1.e-4);
117 parlist->sublist("General").sublist("Krylov").set("Relative Tolerance",1.e-2);
118 parlist->sublist("General").sublist("Krylov").set("Iteration Limit",50);
119 // Set initial guess.
120 y.zero();
121 {
122 // Define algorithm.
124 // Run Algorithm
125 algo.run(y,obj,bcon,*outStream);
126 }
127 // Output control to file.
128 std::ofstream file_tr;
129 file_tr.open("control_TR.txt");
130 for ( unsigned i = 0; i < (unsigned)nx+2; i++ ) {
131 file_tr << (*y_ptr)[i] << "\n";
132 }
133 file_tr.close();
134 // Output state to file.
135 std::vector<RealT> u(nx,0.0);
136 std::vector<RealT> param(4,0.0);
137 obj.solve_state(u,*x_ptr,param);
138 std::ofstream file;
139 file.open("state.txt");
140 for (unsigned i=0; i<(unsigned)nx; i++) {
141 file << i/((RealT)(nx+1)) << " " << u[i] << "\n";
142 }
143 file.close();
144
145 // Compute error between PDAS and Lin-More solutions.
146 ROL::Ptr<ROL::Vector<RealT> > diff = x.clone();
147 diff->set(x);
148 diff->axpy(-1.0,y);
149 RealT error = diff->norm();
150 *outStream << "\nError between PDAS solution and TR solution is " << error << "\n";
151 errorFlag = ((error > 1e2*std::sqrt(ROL::ROL_EPSILON<RealT>())) ? 1 : 0);
152 }
153 catch (std::logic_error& err) {
154 *outStream << err.what() << "\n";
155 errorFlag = -1000;
156 }; // end try
157
158 if (errorFlag != 0)
159 std::cout << "End Result: TEST FAILED\n";
160 else
161 std::cout << "End Result: TEST PASSED\n";
162
163 return 0;
164
165}
166
Vector< Real > V
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
int main(int argc, char *argv[])
void solve_state(std::vector< Real > &u, const std::vector< Real > &z, const std::vector< Real > &param)
Provides the elementwise interface to apply upper and lower bound constraints.
virtual std::vector< std::vector< Real > > checkGradient(const Vector< Real > &x, const Vector< Real > &d, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference gradient check.
virtual std::vector< std::vector< Real > > checkHessVec(const Vector< Real > &x, const Vector< Real > &v, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference Hessian-applied-to-vector check.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Provides an interface to run the trust-region algorithm of Lin and More.
void run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &bnd, std::ostream &outStream=std::cout) override
Run algorithm on bound constrained problems (Type-B). This general interface supports the use of dual...
Provides an interface to run the projected secant algorithm.
void run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &bnd, std::ostream &outStream=std::cout) override
Run algorithm on bound constrained problems (Type-B). This general interface supports the use of dual...
Defines the linear algebra or vector space interface.
Example of how to supply ROL with parameters from a JSON file. Requires that json-cpp be installed.