Debian Codegraph @

packages/rol/example/PDE-OPT/dynamic/navier-stokes/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 "Teuchos_GlobalMPISession.hpp"
16 #include "Tpetra_Core.hpp"
17 #include "Tpetra_Version.hpp"
18
19 #include <iostream>
20 #include <algorithm>
21 //#include <fenv.h>
22
23 #include "ROL_Bounds.hpp"
24 #include "ROL_Stream.hpp"
25 #include "ROL_ParameterList.hpp"
26 #include "ROL_Solver.hpp"
27 #include "ROL_ReducedDynamicObjective.hpp"
28 #include "ROL_DynamicConstraintCheck.hpp"
29 #include "ROL_DynamicObjectiveCheck.hpp"
30
31 #include "../../TOOLS/dynconstraint.hpp"
32 #include "../../TOOLS/pdeconstraint.hpp"
33 #include "../../TOOLS/pdeobjective.hpp"
34 #include "../../TOOLS/ltiobjective.hpp"
35 #include "../../TOOLS/meshreader.hpp"
36 #include "../../TOOLS/pdevector.hpp"
37 #include "dynpde_navier-stokes.hpp"
38 #include "obj_navier-stokes.hpp"
39 #include "initial_condition.hpp"
40
41 template<class Real>
42 void computeInitialCondition(const ROL::Ptr<ROL::Vector<Real>> &u0,
43 const ROL::Ptr<ROL::Vector<Real>> &ck,
44 const ROL::Ptr<ROL::Vector<Real>> &uo,
45 const ROL::Ptr<ROL::Vector<Real>> &un,
46 const ROL::Ptr<ROL::Vector<Real>> &zk,
47 const ROL::Ptr<DynConstraint<Real>> &con,
48 const Real dt,
49 std::ostream &outStream);
50
51 int main(int argc, char *argv[]) {
52 //feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
53 using RealT = double;
54
55 /*** Initialize communicator. ***/
56 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
57 ROL::Ptr<const Teuchos::Comm<int>> comm
58 = Tpetra::getDefaultComm();
59
60 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
61 const int numProcs = (comm->getSize() > 1) ? comm->getSize() : 0;
62 const int myRank = comm->getRank();
63 ROL::Ptr<std::ostream> outStream = ROL::makeStreamPtr( std::cout, (argc > 1) && (myRank==0) );
64
65 int errorFlag = 0;
66
67 // *** Example body.
68 try {
69
70 /*** Read in XML input ***/
71 ROL::Ptr<ROL::ParameterList> parlist = ROL::getParametersFromXmlFile("input.xml");
72 int nt = parlist->sublist("Time Discretization").get("Number of Time Steps", 100);
73 RealT T = parlist->sublist("Time Discretization").get("End Time", 1.0);
74 RealT dt = T/static_cast<RealT>(nt);
75 bool useParametricControl = parlist->sublist("Problem").get("Use Parametric Control", false);
76 int verbosity = parlist->sublist("General").get("Print Verbosity", 0);
77 verbosity = (myRank==0 ? verbosity : 0);
78 parlist->sublist("General").set("Print Verbosity", verbosity);
79 bool solveOutput = parlist->sublist("Dynamic Constraint").sublist("Solve").get("Output Iteration History", false);
80 solveOutput = (myRank==0 ? solveOutput : false);
81 parlist->sublist("Dynamic Constraint").sublist("Solve").set("Output Iteration History", solveOutput);
82 solveOutput = parlist->sublist("SimOpt").sublist("Solve").get("Output Iteration History", false);
83 solveOutput = (myRank==0 ? solveOutput : false);
84 parlist->sublist("SimOpt").sublist("Solve").set("Output Iteration History", solveOutput);
85 RealT Re = parlist->sublist("Problem").get("Reynolds Number",200.0);
86
87 parlist->sublist("Reduced Dynamic Objective").set("State Domain Seed",12321*(myRank+1));
88 parlist->sublist("Reduced Dynamic Objective").set("State Range Seed", 32123*(myRank+1));
89 parlist->sublist("Reduced Dynamic Objective").set("Adjoint Domain Seed",23432*(myRank+1));
90 parlist->sublist("Reduced Dynamic Objective").set("Adjoint Range Seed", 43234*(myRank+1));
91 parlist->sublist("Reduced Dynamic Objective").set("State Sensitivity Domain Seed",34543*(myRank+1));
92 parlist->sublist("Reduced Dynamic Objective").set("State Sensitivity Range Seed", 54345*(myRank+1));
93
94 /*************************************************************************/
95 /***************** BUILD GOVERNING PDE ***********************************/
96 /*************************************************************************/
97 /*** Initialize mesh data structure. ***/
98 ROL::Ptr<MeshManager<RealT>> meshMgr
99 = ROL::makePtr<MeshReader<RealT>>(*parlist, numProcs);
100 // Initialize PDE describing Navier-Stokes equations.
101 ROL::Ptr<DynamicPDE_NavierStokes<RealT>> pde
102 = ROL::makePtr<DynamicPDE_NavierStokes<RealT>>(*parlist);
103
104 /*************************************************************************/
105 /***************** BUILD CONSTRAINT **************************************/
106 /*************************************************************************/
107 ROL::Ptr<DynConstraint<RealT>> dyn_con
108 = ROL::makePtr<DynConstraint<RealT>>(pde,meshMgr,comm,*parlist,*outStream);
109 const ROL::Ptr<Assembler<RealT>> assembler = dyn_con->getAssembler();
110 dyn_con->setSolveParameters(*parlist);
111 dyn_con->getAssembler()->printMeshData(*outStream);
112
113 /*************************************************************************/
114 /***************** BUILD VECTORS *****************************************/
115 /*************************************************************************/
116 ROL::Ptr<Tpetra::MultiVector<>> u0_ptr = assembler->createStateVector();
117 ROL::Ptr<Tpetra::MultiVector<>> uo_ptr = assembler->createStateVector();
118 ROL::Ptr<Tpetra::MultiVector<>> un_ptr = assembler->createStateVector();
119 ROL::Ptr<Tpetra::MultiVector<>> ck_ptr = assembler->createResidualVector();
120 ROL::Ptr<ROL::Vector<RealT>> u0, uo, un, ck, zk;
121 u0 = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u0_ptr,pde,*assembler,*parlist);
122 uo = ROL::makePtr<PDE_PrimalSimVector<RealT>>(uo_ptr,pde,*assembler,*parlist);
123 un = ROL::makePtr<PDE_PrimalSimVector<RealT>>(un_ptr,pde,*assembler,*parlist);
124 ck = ROL::makePtr<PDE_DualSimVector<RealT>>(ck_ptr,pde,*assembler,*parlist);
125 if (!useParametricControl) {
126 ROL::Ptr<Tpetra::MultiVector<>> zk_ptr = assembler->createControlVector();
127 zk = ROL::makePtr<PDE_PrimalOptVector<RealT>>(zk_ptr,pde,*assembler,*parlist);
128 }
129 else {
130 zk = ROL::makePtr<PDE_OptVector<RealT>>(ROL::makePtr<ROL::StdVector<RealT>>(1));
131 }
132 ROL::Ptr<ROL::PartitionedVector<RealT>> z
133 = ROL::PartitionedVector<RealT>::create(*zk, nt);
134
135 /*************************************************************************/
136 /***************** BUILD COST FUNCTIONAL *********************************/
137 /*************************************************************************/
138 std::vector<ROL::Ptr<QoI<RealT>>> qoi_vec(3,ROL::nullPtr), qoi_T(1,ROL::nullPtr);
139 RealT w1 = parlist->sublist("Problem").get("State Cost",1.0);
140 RealT w2 = parlist->sublist("Problem").get("State Boundary Cost",1.0);
141 RealT w3 = parlist->sublist("Problem").get("Control Cost",0.0);
142 RealT wT = parlist->sublist("Problem").get("Final Time State Cost",1.0);
143 std::vector<RealT> wts = {w1, w2, w3}, wts_T = {wT};
144 std::string intObj = parlist->sublist("Problem").get("Integrated Objective Type", "Dissipation");
145 std::string ftObj = parlist->sublist("Problem").get("Final Time Objective Type", "Tracking");
146 qoi_vec[0] = ROL::makePtr<QoI_State_NavierStokes<RealT>>(intObj,
147 *parlist,
148 pde->getVelocityFE(),
149 pde->getPressureFE(),
150 pde->getFieldHelper());
151 qoi_vec[1] = ROL::makePtr<QoI_DownStreamPower_NavierStokes<RealT>>(pde->getVelocityFE(),
152 pde->getPressureFE(),
153 pde->getVelocityBdryFE(1),
154 pde->getBdryCellLocIds(1),
155 pde->getFieldHelper());
156 qoi_T[0] = ROL::makePtr<QoI_State_NavierStokes<RealT>>(ftObj,
157 *parlist,
158 pde->getVelocityFE(),
159 pde->getPressureFE(),
160 pde->getFieldHelper());
161 if (useParametricControl) {
162 qoi_vec[2] = ROL::makePtr<QoI_RotationControl_NavierStokes<RealT>>();
163 }
164 else {
165 qoi_vec[2] = ROL::makePtr<QoI_L2Penalty_NavierStokes<RealT>>(pde->getVelocityFE(),
166 pde->getPressureFE(),
167 pde->getVelocityBdryFE(4),
168 pde->getBdryCellLocIds(4),
169 pde->getFieldHelper());
170 }
171 ROL::Ptr<ROL::Objective_SimOpt<RealT>> obj_k
172 = ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,wts,assembler);
173 ROL::Ptr<ROL::Objective_SimOpt<RealT>> obj_T
174 = ROL::makePtr<PDE_Objective<RealT>>(qoi_T,wts_T,assembler);
175 ROL::Ptr<LTI_Objective<RealT>> dyn_obj
176 = ROL::makePtr<LTI_Objective<RealT>>(*parlist,obj_k,obj_T);
177
178 /*************************************************************************/
179 /***************** BUILD REDUCED COST FUNCTIONAL *************************/
180 /*************************************************************************/
181 std::vector<ROL::TimeStamp<RealT>> timeStamp(nt);
182 for( int k=0; k<nt; ++k ) {
183 timeStamp.at(k).t.resize(2);
184 timeStamp.at(k).t.at(0) = k*dt;
185 timeStamp.at(k).t.at(1) = (k+1)*dt;
186 }
187 std::clock_t timer_init = std::clock();
188 std::stringstream file;
189 file << "initial_condition_Re" << static_cast<int>(Re) << ".txt";
190 std::ifstream infile(file.str());
191 if (infile.good()) {
192 dyn_con->inputTpetraVector(u0_ptr, file.str());
193 }
194 else {
195 PotentialFlow<RealT> pf(pde->getVelocityFE(),
196 pde->getPressureFE(),
197 pde->getCellNodes(),
198 assembler->getDofManager()->getCellDofs(),
199 assembler->getCellIds(),
200 pde->getFieldHelper(),
201 *parlist);
202 pf.build(u0_ptr);
203 computeInitialCondition<RealT>(u0,ck,uo,un,zk,dyn_con,dt,*outStream);
204 dyn_con->outputTpetraVector(u0_ptr, file.str());
205 }
206 *outStream << "Initial condition time: "
207 << static_cast<RealT>(std::clock()-timer_init)/static_cast<RealT>(CLOCKS_PER_SEC)
208 << " seconds." << std::endl << std::endl;
209 // Construct reduce dynamic objective function
210 ROL::ParameterList &rpl = parlist->sublist("Reduced Dynamic Objective");
211 ROL::Ptr<ROL::ReducedDynamicObjective<RealT>> obj
212 = ROL::makePtr<ROL::ReducedDynamicObjective<RealT>>(dyn_obj, dyn_con, u0, zk, ck, timeStamp, rpl, outStream);
213
214 /*************************************************************************/
215 /***************** RUN VECTOR AND DERIVATIVE CHECKS **********************/
216 /*************************************************************************/
217 bool checkDeriv = parlist->sublist("Problem").get("Check Derivatives",false);
218 if ( checkDeriv ) {
219 ROL::Ptr<ROL::PartitionedVector<RealT>> dz = ROL::PartitionedVector<RealT>::create(*zk, nt);
220 ROL::Ptr<ROL::PartitionedVector<RealT>> hz = ROL::PartitionedVector<RealT>::create(*zk, nt);
221 zk->randomize(); z->randomize(); dz->randomize(); hz->randomize();
222 uo->randomize(); un->randomize();
223 ROL::ValidateFunction<RealT> validate(1,13,20,11,true,*outStream);
224 ROL::DynamicObjectiveCheck<RealT>::check(*dyn_obj,validate,*uo,*un,*zk);
225 ROL::DynamicConstraintCheck<RealT>::check(*dyn_con,validate,*uo,*un,*zk);
226 obj->checkGradient(*z,*dz,true,*outStream);
227 obj->checkHessVec(*z,*dz,true,*outStream);
228 obj->checkHessSym(*z,*dz,*hz,true,*outStream);
229 }
230
231 /*************************************************************************/
232 /***************** RUN VECTOR AND DERIVATIVE CHECKS **********************/
233 /*************************************************************************/
234 bool printU0 = parlist->sublist("Problem").get("Print Uncontrolled State", false);
235 if (printU0) {
236 std::clock_t timer_print0 = std::clock();
237 zk->zero(); uo->set(*u0); un->zero();
238 for (int k = 1; k < nt; ++k) {
239 // Print previous state to file
240 std::stringstream u0file;
241 u0file << "uncontrolled_state." << k-1 << ".txt";
242 dyn_con->outputTpetraVector(uo_ptr, u0file.str());
243 // Advance time stepper
244 dyn_con->solve(*ck, *uo, *un, *zk, timeStamp[k]);
245 uo->set(*un);
246 }
247 // Print previous state to file
248 std::stringstream u0file;
249 u0file << "uncontrolled_state." << nt-1 << ".txt";
250 dyn_con->outputTpetraVector(uo_ptr, u0file.str());
251 *outStream << "Output uncontrolled state time: "
252 << static_cast<RealT>(std::clock()-timer_print0)/static_cast<RealT>(CLOCKS_PER_SEC)
253 << " seconds." << std::endl << std::endl;
254 }
255
256 /*************************************************************************/
257 /***************** SOLVE OPTIMIZATION PROBLEM ****************************/
258 /*************************************************************************/
259 z->zero();
260 if (useParametricControl) {
261 // Linearly interpolate between optimal values for angular velocity
262 // amplitude and Strouhal number obtained for Re=200, 1000 in
263 // JW He, R Glowinski, R Metcalfe, A Nordlander, J Periaux
264 // Active Control and Drag Optimization for Flow Past a
265 // Circular Cylinder
266 // Journal of Computation Physics, 163, pg. 83-117, 2000.
267 RealT Re = parlist->sublist("Problem").get("Reynolds Number",200.0);
268 RealT amp0 = 6.0 - (Re - 200.0)/1600.0;
269 RealT Se0 = 0.74 - (Re - 200.0) * (0.115/800.0);
270 RealT amp = parlist->sublist("Problem").sublist("Initial Guess").get("Amplitude", amp0);
271 RealT Se = parlist->sublist("Problem").sublist("Initial Guess").get("Strouhal Number", Se0);
272 RealT ph = parlist->sublist("Problem").sublist("Initial Guess").get("Phase Shift", 0.0);
273 for( int k=0; k<nt; ++k ) {
274 ROL::Ptr<std::vector<RealT>> zn
275 = ROL::dynamicPtrCast<PDE_OptVector<RealT>>(z->get(k))->getParameter()->getVector();
276 (*zn)[0] = -amp * std::sin(2.0 * M_PI * Se * timeStamp[k].t[0] + ph);
277 }
278 }
279 ROL::Ptr<ROL::Problem<RealT>> problem = ROL::makePtr<ROL::Problem<RealT>>(obj,z);
280 problem->finalize(false,true,*outStream);
281 ROL::Solver<RealT> solver(problem,*parlist);
282 std::clock_t timer = std::clock();
283 solver.solve(*outStream);
284 *outStream << "Optimization time: "
285 << static_cast<RealT>(std::clock()-timer)/static_cast<RealT>(CLOCKS_PER_SEC)
286 << " seconds." << std::endl << std::endl;
287
288 /*************************************************************************/
289 /***************** OUTPUT RESULTS ****************************************/
290 /*************************************************************************/
291 std::clock_t timer_print = std::clock();
292 // Output state and control to file
293 uo->set(*u0); un->zero();
294 for (int k = 1; k < nt; ++k) {
295 // Print previous state to file
296 std::stringstream ufile;
297 ufile << "state." << k-1 << ".txt";
298 dyn_con->outputTpetraVector(uo_ptr, ufile.str());
299 // Print current control
300 if (!useParametricControl) {
301 std::stringstream zfile;
302 zfile << "control." << k-1 << ".txt";
303 dyn_con->outputTpetraVector(ROL::dynamicPtrCast<PDE_PrimalOptVector<RealT>>(z->get(k-1))->getVector(),zfile.str());
304 }
305 else {
306 if (myRank == 0) {
307 std::stringstream zname;
308 zname << "control." << k-1 << ".txt";
309 std::ofstream zfile;
310 zfile.open(zname.str());
311 zfile << std::scientific << std::setprecision(15);
312 ROL::Ptr<std::vector<RealT>> zn
313 = ROL::dynamicPtrCast<PDE_OptVector<RealT>>(z->get(k-1))->getParameter()->getVector();
314 zfile << std::right << std::setw(25) << (*zn)[0];
315 zfile.close();
316 }
317 }
318 // Advance time stepper
319 dyn_con->solve(*ck, *uo, *un, *z->get(k), timeStamp[k]);
320 uo->set(*un);
321 }
322 // Print previous state to file
323 std::stringstream ufile;
324 ufile << "state." << nt-1 << ".txt";
325 dyn_con->outputTpetraVector(uo_ptr, ufile.str());
326 // Print current control
327 if (!useParametricControl) {
328 std::stringstream zfile;
329 zfile << "control." << nt-1 << ".txt";
330 dyn_con->outputTpetraVector(ROL::dynamicPtrCast<PDE_PrimalOptVector<RealT>>(z->get(nt-1))->getVector(),zfile.str());
331 }
332 else {
333 if (myRank == 0) {
334 std::stringstream zname;
335 zname << "control." << nt-1 << ".txt";
336 std::ofstream zfile;
337 zfile.open(zname.str());
338 zfile << std::scientific << std::setprecision(15);
339 ROL::Ptr<std::vector<RealT>> zn
340 = ROL::dynamicPtrCast<PDE_OptVector<RealT>>(z->get(nt-1))->getParameter()->getVector();
341 zfile << std::right << std::setw(25) << (*zn)[0];
342 zfile.close();
343 }
344 }
345 *outStream << "Output time: "
346 << static_cast<RealT>(std::clock()-timer_print)/static_cast<RealT>(CLOCKS_PER_SEC)
347 << " seconds." << std::endl << std::endl;
348 }
349 catch (std::logic_error& err) {
350 *outStream << err.what() << "\n";
351 errorFlag = -1000;
352 }; // end try
353
354 if (errorFlag != 0)
355 std::cout << "End Result: TEST FAILED\n";
356 else
357 std::cout << "End Result: TEST PASSED\n";
358
359 return 0;
360 }
361
362 template<class Real>
363 void computeInitialCondition(const ROL::Ptr<ROL::Vector<Real>> &u0,
364 const ROL::Ptr<ROL::Vector<Real>> &ck,
365 const ROL::Ptr<ROL::Vector<Real>> &uo,
366 const ROL::Ptr<ROL::Vector<Real>> &un,
367 const ROL::Ptr<ROL::Vector<Real>> &zk,
368 const ROL::Ptr<DynConstraint<Real>> &con,
369 const Real dt,
370 std::ostream &outStream) {
371 Real T = 80.0;
372 int nt = static_cast<int>(T/dt);
373 std::vector<ROL::TimeStamp<Real>> ts(nt);
374 for( int k=0; k<nt; ++k ) {
375 ts.at(k).t.resize(2);
376 ts.at(k).t.at(0) = k*dt;
377 ts.at(k).t.at(1) = (k+1)*dt;
378 }
379 // Solve Navier-Stokes equation to determine initial condition
380 zk->zero(); uo->set(*u0); un->zero();
381 Real unorm = uo->norm();
382 outStream << std::scientific << std::setprecision(6);
383 outStream << std::right << std::setw(8) << "ts"
384 << std::right << std::setw(16) << "||u(ts)||"
385 << std::right << std::setw(16) << "avg time (sec)"
386 << std::endl;
387 outStream << std::right << std::setw(8) << 0
388 << std::right << std::setw(16) << unorm
389 << std::right << std::setw(16) << "---"
390 << std::endl;
391 std::vector<Real> time(10);
392 std::clock_t timer_step;
393 Real time_avg(0);
394 for (int k = 1; k < nt; ++k) {
395 // Advance time stepper
396 timer_step = std::clock();
397 con->solve(*ck, *uo, *un, *zk, ts[k]);
398 time[k%10] = static_cast<Real>(std::clock()-timer_step)/static_cast<Real>(CLOCKS_PER_SEC);
399 uo->set(*un);
400 if ( k%10==0 ) {
401 unorm = uo->norm();
402 time_avg = 0.0;
403 for (int i = 0; i < 10; ++i) {
404 time_avg += time[i];
405 }
406 time_avg *= 0.1;
407 outStream << std::right << std::setw(8) << k
408 << std::right << std::setw(16) << unorm
409 << std::right << std::setw(16) << time_avg
410 << std::endl;
411 }
412 }
413 u0->set(*uo);
414 }
415
416