Debian Codegraph @

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