Debian Codegraph @

packages/rol/example/PDE-OPT/thermal-fluids/example_03.cpp

raw
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
10 /*! \file example_01.cpp
11 \brief Shows how to solve the Navier-Stokes control problem.
12 */
13
14 #include "Teuchos_Comm.hpp"
15 #include "ROL_Stream.hpp"
16 #include "Teuchos_GlobalMPISession.hpp"
17 #include "Teuchos_XMLParameterListHelpers.hpp"
18
19 #include "Tpetra_Core.hpp"
20 #include "Tpetra_Version.hpp"
21
22 #include <iostream>
23 #include <algorithm>
24 //#include <fenv.h>
25
26 #include "ROL_TpetraMultiVector.hpp"
27 #include "ROL_Reduced_Objective_SimOpt.hpp"
28 #include "ROL_OptimizationSolver.hpp"
29
30 #include "../TOOLS/pdeconstraint.hpp"
31 #include "../TOOLS/pdeobjective.hpp"
32 #include "../TOOLS/pdevector.hpp"
33 #include "pde_thermal-fluids_ex03.hpp"
34 #include "obj_thermal-fluids_ex03.hpp"
35 #include "mesh_thermal-fluids.hpp"
36
37 typedef double RealT;
38
39 int main(int argc, char *argv[]) {
40 //feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
41
42 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
43 int iprint = argc - 1;
44 ROL::Ptr<std::ostream> outStream;
45 ROL::nullstream bhs; // outputs nothing
46
47 /*** Initialize communicator. ***/
48 Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs);
49 ROL::Ptr<const Teuchos::Comm<int> > comm
50 = Tpetra::getDefaultComm();
51 const int myRank = comm->getRank();
52 if ((iprint > 0) && (myRank == 0)) {
53 outStream = ROL::makePtrFromRef(std::cout);
54 }
55 else {
56 outStream = ROL::makePtrFromRef(bhs);
57 }
58 int errorFlag = 0;
59
60 // *** Example body.
61 try {
62
63 /*** Read in XML input ***/
64 std::string filename = "input_ex03.xml";
65 Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() );
66 Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
67
68 /*** Initialize main data structure. ***/
69 ROL::Ptr<MeshManager<RealT> > meshMgr
70 = ROL::makePtr<MeshManager_ThermalFluids<RealT>>(*parlist);
71 // Initialize PDE describing Navier-Stokes equations.
72 ROL::Ptr<PDE_ThermalFluids_ex03<RealT> > pde
73 = ROL::makePtr<PDE_ThermalFluids_ex03<RealT>>(*parlist);
74 ROL::Ptr<ROL::Constraint_SimOpt<RealT> > con
75 = ROL::makePtr<PDE_Constraint<RealT>>(pde,meshMgr,comm,*parlist,*outStream);
76 // Cast the constraint and get the assembler.
77 ROL::Ptr<PDE_Constraint<RealT> > pdecon
78 = ROL::dynamicPtrCast<PDE_Constraint<RealT> >(con);
79 ROL::Ptr<Assembler<RealT> > assembler = pdecon->getAssembler();
80 con->setSolveParameters(*parlist);
81 pdecon->outputTpetraData();
82
83 // Create state vector and set to zeroes
84 ROL::Ptr<Tpetra::MultiVector<> > u_ptr, p_ptr, du_ptr, r_ptr, z_ptr, dz_ptr;
85 u_ptr = assembler->createStateVector(); u_ptr->randomize();
86 p_ptr = assembler->createStateVector(); p_ptr->randomize();
87 du_ptr = assembler->createStateVector(); du_ptr->randomize();
88 r_ptr = assembler->createResidualVector(); r_ptr->randomize();
89 z_ptr = assembler->createControlVector(); z_ptr->randomize();
90 dz_ptr = assembler->createControlVector(); dz_ptr->randomize();
91 ROL::Ptr<ROL::Vector<RealT> > up, pp, dup, rp, zp, dzp;
92 up = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,assembler);
93 pp = ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,assembler);
94 dup = ROL::makePtr<PDE_PrimalSimVector<RealT>>(du_ptr,pde,assembler);
95 rp = ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,assembler);
96 zp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pde,assembler);
97 dzp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(dz_ptr,pde,assembler);
98 // Create ROL SimOpt vectors
99 ROL::Vector_SimOpt<RealT> x(up,zp);
100 ROL::Vector_SimOpt<RealT> d(dup,dzp);
101
102 // Initialize objective function.
103 std::vector<ROL::Ptr<QoI<RealT> > > qoi_vec(2,ROL::nullPtr);
104 qoi_vec[0] = ROL::makePtr<QoI_State_ThermalFluids<RealT>>(*parlist,
105 pde->getVelocityFE(),
106 pde->getPressureFE(),
107 pde->getThermalFE(),
108 pde->getFieldHelper());
109 qoi_vec[1] = ROL::makePtr<QoI_L2Penalty_ThermalFluids<RealT>>(pde->getVelocityFE(),
110 pde->getPressureFE(),
111 pde->getThermalFE(),
112 pde->getThermalBdryFE(),
113 pde->getBdryCellLocIds(),
114 pde->getFieldHelper());
115 ROL::Ptr<StdObjective_ThermalFluids<RealT> > std_obj
116 = ROL::makePtr<StdObjective_ThermalFluids<RealT>>(*parlist);
117 ROL::Ptr<ROL::Objective_SimOpt<RealT> > obj
118 = ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,std_obj,assembler);
119 ROL::Ptr<ROL::VectorController<RealT> > stateStore
120 = ROL::makePtr<ROL::VectorController<RealT>>();
121 ROL::Ptr<ROL::Reduced_Objective_SimOpt<RealT> > robj
122 = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(obj, con, stateStore, up, zp, pp, true, false);
123
124 //up->zero();
125 //zp->zero();
126 //z_ptr->putScalar(1.e0);
127 //dz_ptr->putScalar(0);
128
129 // Run derivative checks
130 bool checkDeriv = parlist->sublist("Problem").get("Check derivatives",false);
131 if ( checkDeriv ) {
132 *outStream << "Check Gradient of Full Objective Function" << std::endl;
133 obj->checkGradient(x,d,true,*outStream);
134 *outStream << std::endl << "Check Hessian of Full Objective Function" << std::endl;
135 obj->checkHessVec(x,d,true,*outStream);
136 *outStream << std::endl << "Check Jacobian of Constraint" << std::endl;
137 con->checkApplyJacobian(x,d,*up,true,*outStream);
138 *outStream << std::endl << "Check Jacobian_1 of Constraint" << std::endl;
139 con->checkApplyJacobian_1(*up,*zp,*dup,*rp,true,*outStream);
140 *outStream << std::endl << "Check Jacobian_2 of Constraint" << std::endl;
141 con->checkApplyJacobian_2(*up,*zp,*dzp,*rp,true,*outStream);
142 *outStream << std::endl << "Check Hessian of Constraint" << std::endl;
143 con->checkApplyAdjointHessian(x,*dup,d,x,true,*outStream);
144 *outStream << std::endl << "Check Hessian_11 of Constraint" << std::endl;
145 con->checkApplyAdjointHessian_11(*up,*zp,*pp,*dup,*rp,true,*outStream);
146 *outStream << std::endl << "Check Hessian_12 of Constraint" << std::endl;
147 con->checkApplyAdjointHessian_12(*up,*zp,*pp,*dup,*dzp,true,*outStream);
148 *outStream << std::endl << "Check Hessian_21 of Constraint" << std::endl;
149 con->checkApplyAdjointHessian_21(*up,*zp,*pp,*dzp,*rp,true,*outStream);
150 *outStream << std::endl << "Check Hessian_22 of Constraint" << std::endl;
151 con->checkApplyAdjointHessian_22(*up,*zp,*pp,*dzp,*dzp,true,*outStream);
152
153 *outStream << std::endl << "Check Adjoint Jacobian of Constraint" << std::endl;
154 con->checkAdjointConsistencyJacobian(*dup,d,x,true,*outStream);
155 *outStream << std::endl << "Check Adjoint Jacobian_1 of Constraint" << std::endl;
156 con->checkAdjointConsistencyJacobian_1(*pp,*dup,*up,*zp,true,*outStream);
157 *outStream << std::endl << "Check Adjoint Jacobian_2 of Constraint" << std::endl;
158 con->checkAdjointConsistencyJacobian_2(*pp,*dzp,*up,*zp,true,*outStream);
159
160 *outStream << std::endl << "Check Constraint Solve" << std::endl;
161 con->checkSolve(*up,*zp,*rp,true,*outStream);
162 *outStream << std::endl << "Check Inverse Jacobian_1 of Constraint" << std::endl;
163 con->checkInverseJacobian_1(*rp,*dup,*up,*zp,true,*outStream);
164 *outStream << std::endl << "Check Inverse Adjoint Jacobian_1 of Constraint" << std::endl;
165 con->checkInverseAdjointJacobian_1(*rp,*pp,*up,*zp,true,*outStream);
166
167 *outStream << std::endl << "Check Gradient of Reduced Objective Function" << std::endl;
168 robj->checkGradient(*zp,*dzp,true,*outStream);
169 *outStream << std::endl << "Check Hessian of Reduced Objective Function" << std::endl;
170 robj->checkHessVec(*zp,*dzp,true,*outStream);
171 }
172 up->zero();
173 zp->zero();
174
175 RealT tol(1.e-8);
176 bool initSolve = parlist->sublist("Problem").get("Solve state for full space",true);
177 if (initSolve) {
178 con->solve(*rp,*up,*zp,tol);
179 pdecon->outputTpetraVector(u_ptr,"state_uncontrolled.txt");
180 }
181
182 bool useFullSpace = parlist->sublist("Problem").get("Full space",false);
183 ROL::Ptr<ROL::Algorithm<RealT> > algo;
184 if ( useFullSpace ) {
185 std::string step = parlist->sublist("Step").get("Type", "Composite Step");
186 ROL::EStep els = ROL::StringToEStep(step);
187 switch( els ) {
188 case ROL::STEP_COMPOSITESTEP:
189 case ROL::STEP_FLETCHER: {
190 ROL::OptimizationProblem<RealT> optProb(obj, makePtrFromRef(x), con, rp);
191 ROL::OptimizationSolver<RealT> optSolver(optProb, *parlist);
192 optSolver.solve(*outStream);
193 break;
194 }
195 default: {
196 *outStream << "ERROR: Unsupported step." << std::endl;
197 errorFlag = 1;
198 }
199 }
200 }
201 else {
202 parlist->sublist("Step").set("Step","Trust Region");
203 ROL::OptimizationProblem<RealT> optProb(robj, zp);
204 ROL::OptimizationSolver<RealT> optSolver(optProb, *parlist);
205 optSolver.solve(*outStream);
206 std::vector<RealT> param;
207 stateStore->get(*up,param);
208 }
209
210 // Output.
211 assembler->printMeshData(*outStream);
212 Teuchos::Array<RealT> res(1,0);
213 //con->solve(*rp,*up,*zp,tol);
214 pdecon->outputTpetraVector(u_ptr,"state.txt");
215 pdecon->outputTpetraVector(z_ptr,"control.txt");
216 con->value(*rp,*up,*zp,tol);
217 r_ptr->norm2(res.view(0,1));
218 *outStream << "Residual Norm: " << res[0] << std::endl;
219 errorFlag += (res[0] > 1.e-6 ? 1 : 0);
220 pdecon->outputTpetraData();
221
222 ROL::Ptr<ROL::Objective_SimOpt<RealT> > obj0
223 = ROL::makePtr<IntegralObjective<RealT>>(qoi_vec[0],assembler);
224 RealT val = obj0->value(*up,*zp,tol);
225 *outStream << "Vorticity Value: " << val << std::endl;
226
227 // Get a summary from the time monitor.
228 Teuchos::TimeMonitor::summarize();
229 }
230 catch (std::logic_error& err) {
231 *outStream << err.what() << "\n";
232 errorFlag = -1000;
233 }; // end try
234
235 if (errorFlag != 0)
236 std::cout << "End Result: TEST FAILED\n";
237 else
238 std::cout << "End Result: TEST PASSED\n";
239
240 return 0;
241 }