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
44 /*! \file example_04.cpp
45 \brief Shows how to solve a steady Burgers' optimal control problem using
46 full-space methods.
47 */
48
49 #include "ROL_Algorithm.hpp"
50 #include "ROL_MoreauYosidaPenaltyStep.hpp"
51 #include "ROL_BoundConstraint_SimOpt.hpp"
52 #include "ROL_Vector_SimOpt.hpp"
53 #include "ROL_ParameterList.hpp"
54
55 #include "ROL_Stream.hpp"
56 #include "Teuchos_GlobalMPISession.hpp"
57
58 #include <iostream>
59 #include <algorithm>
60
61 #include "example_04.hpp"
62
63 typedef double RealT;
64 typedef H1VectorPrimal<RealT> PrimalStateVector;
65 typedef H1VectorDual<RealT> DualStateVector;
66 typedef L2VectorPrimal<RealT> PrimalControlVector;
67 typedef L2VectorDual<RealT> DualControlVector;
68 typedef H1VectorDual<RealT> PrimalConstraintVector;
69 typedef H1VectorPrimal<RealT> DualConstraintVector;
70
main(int argc,char * argv[])71 int main(int argc, char *argv[]) {
72
73 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
74 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
75 int iprint = argc - 1;
76 ROL::Ptr<std::ostream> outStream;
77 ROL::nullstream bhs; // outputs nothing
78 if (iprint > 0)
79 outStream = ROL::makePtrFromRef(std::cout);
80 else
81 outStream = ROL::makePtrFromRef(bhs);
82
83 int errorFlag = 0;
84
85 // *** Example body.
86 try {
87 /*************************************************************************/
88 /************* INITIALIZE BURGERS FEM CLASS ******************************/
89 /*************************************************************************/
90 int nx = 128; // Set spatial discretization.
91 RealT alpha = 1.e-3; // Set penalty parameter.
92 RealT nu = 1e-2; // Viscosity parameter.
93 RealT nl = 1.0; // Nonlinearity parameter (1 = Burgers, 0 = linear).
94 RealT u0 = 1.0; // Dirichlet boundary condition at x=0.
95 RealT u1 = 0.0; // Dirichlet boundary condition at x=1.
96 RealT f = 0.0; // Constant volumetric force.
97 RealT cH1 = 1.0; // Scale for derivative term in H1 norm.
98 RealT cL2 = 0.0; // Scale for mass term in H1 norm.
99 ROL::Ptr<BurgersFEM<RealT> > fem
100 = ROL::makePtr<BurgersFEM<RealT>>(nx,nu,nl,u0,u1,f,cH1,cL2);
101 fem->test_inverse_mass(*outStream);
102 fem->test_inverse_H1(*outStream);
103 /*************************************************************************/
104 /************* INITIALIZE SIMOPT OBJECTIVE FUNCTION **********************/
105 /*************************************************************************/
106 ROL::Ptr<std::vector<RealT> > ud_ptr
107 = ROL::makePtr<std::vector<RealT>>(nx, 1.);
108 ROL::Ptr<ROL::Vector<RealT> > ud
109 = ROL::makePtr<L2VectorPrimal<RealT>>(ud_ptr,fem);
110 Objective_BurgersControl<RealT> obj(fem,ud,alpha);
111 /*************************************************************************/
112 /************* INITIALIZE SIMOPT EQUALITY CONSTRAINT *********************/
113 /*************************************************************************/
114 bool useEChessian = true;
115 Constraint_BurgersControl<RealT> con(fem, useEChessian);
116 /*************************************************************************/
117 /************* INITIALIZE BOUND CONSTRAINTS ******************************/
118 /*************************************************************************/
119 // INITIALIZE STATE CONSTRAINTS
120 std::vector<RealT> Ulo(nx, 0.), Uhi(nx, 1.);
121 //std::vector<RealT> Ulo(nx, -1.e8), Uhi(nx, 1.e8);
122 ROL::Ptr<ROL::BoundConstraint<RealT> > Ubnd
123 = ROL::makePtr<H1BoundConstraint<RealT>>(Ulo,Uhi,fem);
124 //Ubnd->deactivate();
125 // INITIALIZE CONTROL CONSTRAINTS
126 //std::vector<RealT> Zlo(nx+2, -1.e8), Zhi(nx+2, 1.e8);
127 std::vector<RealT> Zlo(nx+2,0.), Zhi(nx+2,2.);
128 ROL::Ptr<ROL::BoundConstraint<RealT> > Zbnd
129 = ROL::makePtr<L2BoundConstraint<RealT>>(Zlo,Zhi,fem);
130 //Zbnd->deactivate();
131 // INITIALIZE SIMOPT BOUND CONSTRAINTS
132 ROL::BoundConstraint_SimOpt<RealT> bnd(Ubnd,Zbnd);
133 bnd.deactivate();
134 /*************************************************************************/
135 /************* INITIALIZE VECTOR STORAGE *********************************/
136 /*************************************************************************/
137 // INITIALIZE CONTROL VECTORS
138 ROL::Ptr<std::vector<RealT> > z_ptr
139 = ROL::makePtr<std::vector<RealT>>(nx+2, 0.);
140 ROL::Ptr<std::vector<RealT> > zrand_ptr
141 = ROL::makePtr<std::vector<RealT>>(nx+2, 1.);
142 ROL::Ptr<std::vector<RealT> > gz_ptr
143 = ROL::makePtr<std::vector<RealT>>(nx+2, 1.);
144 ROL::Ptr<std::vector<RealT> > yz_ptr
145 = ROL::makePtr<std::vector<RealT>>(nx+2, 1.);
146 for (int i=0; i<nx+2; i++) {
147 (*zrand_ptr)[i] = 10.*(RealT)rand()/(RealT)RAND_MAX-5.;
148 (*yz_ptr)[i] = 10.*(RealT)rand()/(RealT)RAND_MAX-5.;
149 }
150 ROL::Ptr<ROL::Vector<RealT> > zp
151 = ROL::makePtr<PrimalControlVector>(z_ptr,fem);
152 ROL::Ptr<ROL::Vector<RealT> > zrandp
153 = ROL::makePtr<PrimalControlVector>(zrand_ptr,fem);
154 ROL::Ptr<ROL::Vector<RealT> > gzp
155 = ROL::makePtr<DualControlVector>(gz_ptr,fem);
156 ROL::Ptr<ROL::Vector<RealT> > yzp
157 = ROL::makePtr<PrimalControlVector>(yz_ptr,fem);
158 // INITIALIZE STATE VECTORS
159 ROL::Ptr<std::vector<RealT> > u_ptr
160 = ROL::makePtr<std::vector<RealT>>(nx, 1.);
161 ROL::Ptr<std::vector<RealT> > gu_ptr
162 = ROL::makePtr<std::vector<RealT>>(nx, 1.);
163 ROL::Ptr<std::vector<RealT> > yu_ptr
164 = ROL::makePtr<std::vector<RealT>>(nx, 1.);
165 for (int i=0; i<nx; i++) {
166 (*yu_ptr)[i] = 10.*(RealT)rand()/(RealT)RAND_MAX-5.;
167 }
168 ROL::Ptr<ROL::Vector<RealT> > up
169 = ROL::makePtr<PrimalStateVector>(u_ptr,fem);
170 ROL::Ptr<ROL::Vector<RealT> > gup
171 = ROL::makePtr<DualStateVector>(gu_ptr,fem);
172 ROL::Ptr<ROL::Vector<RealT> > yup
173 = ROL::makePtr<PrimalStateVector>(yu_ptr,fem);
174 // INITIALIZE CONSTRAINT VECTORS
175 ROL::Ptr<std::vector<RealT> > c_ptr
176 = ROL::makePtr<std::vector<RealT>>(nx, 1.);
177 ROL::Ptr<std::vector<RealT> > l_ptr
178 = ROL::makePtr<std::vector<RealT>>(nx, 1.);
179 for (int i=0; i<nx; i++) {
180 (*l_ptr)[i] = (RealT)rand()/(RealT)RAND_MAX;
181 }
182 PrimalConstraintVector c(c_ptr,fem);
183 DualConstraintVector l(l_ptr,fem);
184 // INITIALIZE SIMOPT VECTORS
185 ROL::Vector_SimOpt<RealT> x(up,zp);
186 ROL::Vector_SimOpt<RealT> g(gup,gzp);
187 ROL::Vector_SimOpt<RealT> y(yup,yzp);
188 // READ IN XML INPUT
189 std::string filename = "input.xml";
190 auto parlist = ROL::getParametersFromXmlFile( filename );
191
192 /*************************************************************************/
193 /************* CHECK DERIVATIVES AND CONSISTENCY *************************/
194 /*************************************************************************/
195 zp->set(*zrandp);
196 // CHECK OBJECTIVE DERIVATIVES
197 obj.checkGradient(x,g,y,true,*outStream);
198 obj.checkHessVec(x,g,y,true,*outStream);
199 // CHECK EQUALITY CONSTRAINT DERIVATIVES
200 con.checkApplyJacobian(x,y,c,true,*outStream);
201 con.checkApplyAdjointHessian(x,*yup,y,g,true,*outStream);
202 // CHECK EQUALITY CONSTRAINT CONSISTENCY
203 con.checkSolve(*up,*zp,c,true,*outStream);
204 con.checkAdjointConsistencyJacobian_1(l,*yup,*up,*zp,true,*outStream);
205 con.checkAdjointConsistencyJacobian_2(l,*yzp,*up,*zp,true,*outStream);
206 con.checkInverseJacobian_1(c,*yup,*up,*zp,true,*outStream);
207 con.checkInverseAdjointJacobian_1(c,*yup,*up,*zp,true,*outStream);
208 *outStream << "\n";
209 // CHECK PENALTY OBJECTIVE DERIVATIVES
210 ROL::Ptr<ROL::Objective<RealT> > obj_ptr = ROL::makePtrFromRef(obj);
211 ROL::Ptr<ROL::Constraint<RealT> > con_ptr = ROL::makePtrFromRef(con);
212 ROL::Ptr<ROL::BoundConstraint<RealT> > bnd_ptr = ROL::makePtrFromRef(bnd);
213 ROL::MoreauYosidaPenalty<RealT> myPen(obj_ptr,bnd_ptr,x,*parlist);
214 myPen.checkGradient(x, y, true, *outStream);
215 myPen.checkHessVec(x, g, y, true, *outStream);
216 ROL::AugmentedLagrangian<RealT> myAugLag(obj_ptr,con_ptr,l,1.,x,c,*parlist);
217 myAugLag.checkGradient(x, y, true, *outStream);
218 myAugLag.checkHessVec(x, g, y, true, *outStream);
219 /*************************************************************************/
220 /************* RUN OPTIMIZATION ******************************************/
221 /*************************************************************************/
222 // SOLVE USING MOREAU-YOSIDA PENALTY
223 ROL::Ptr<ROL::Step<RealT>>
224 stepMY = ROL::makePtr<ROL::MoreauYosidaPenaltyStep<RealT>>(*parlist);
225 ROL::Ptr<ROL::StatusTest<RealT>>
226 statusMY = ROL::makePtr<ROL::ConstraintStatusTest<RealT>>(*parlist);
227 ROL::Algorithm<RealT> algoMY(stepMY,statusMY,false);
228 zp->set(*zrandp);
229 RealT zerotol = std::sqrt(ROL::ROL_EPSILON<RealT>());
230 con.solve(c,*up,*zp,zerotol);
231 obj.gradient_1(*gup,*up,*zp,zerotol);
232 gup->scale(-1.0);
233 con.applyInverseAdjointJacobian_1(l,*gup,*up,*zp,zerotol);
234 gup->zero(); c.zero();
235 algoMY.run(x, g, l, c, myPen, con, bnd, true, *outStream);
236 ROL::Ptr<ROL::Vector<RealT> > xMY = x.clone();
237 xMY->set(x);
238 // SOLVE USING AUGMENTED LAGRANGIAN
239 ROL::Ptr<ROL::Step<RealT>>
240 stepAL = ROL::makePtr<ROL::AugmentedLagrangianStep<RealT>>(*parlist);
241 ROL::Ptr<ROL::StatusTest<RealT>>
242 statusAL = ROL::makePtr<ROL::ConstraintStatusTest<RealT>>(*parlist);
243 ROL::Algorithm<RealT> algoAL(stepAL,statusAL,false);
244 zp->set(*zrandp);
245 con.solve(c,*up,*zp,zerotol);
246 obj.gradient_1(*gup,*up,*zp,zerotol);
247 gup->scale(-1.0);
248 con.applyInverseAdjointJacobian_1(l,*gup,*up,*zp,zerotol);
249 gup->zero(); c.zero();
250 algoAL.run(x, g, l, c, myAugLag, con, bnd, true, *outStream);
251 // COMPARE SOLUTIONS
252 ROL::Ptr<ROL::Vector<RealT> > err = x.clone();
253 err->set(x); err->axpy(-1.,*xMY);
254 errorFlag += ((err->norm() > 1.e-7*x.norm()) ? 1 : 0);
255 }
256 catch (std::logic_error& err) {
257 *outStream << err.what() << "\n";
258 errorFlag = -1000;
259 }; // end try
260
261 if (errorFlag != 0)
262 std::cout << "End Result: TEST FAILED\n";
263 else
264 std::cout << "End Result: TEST PASSED\n";
265
266 return 0;
267 }
268