Debian Codegraph @

packages/rol/example/PDE-OPT/dynamic/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 "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_Stream.hpp"
24 #include "ROL_ParameterList.hpp"
25 #include "ROL_OptimizationSolver.hpp"
26 #include "ROL_ReducedDynamicObjective.hpp"
27 #include "ROL_DynamicConstraintCheck.hpp"
28 #include "ROL_DynamicObjectiveCheck.hpp"
29
30 #include "../../TOOLS/dynconstraint.hpp"
31 #include "../../TOOLS/pdeobjective.hpp"
32 #include "../../TOOLS/ltiobjective.hpp"
33 #include "../../TOOLS/meshreader.hpp"
34 #include "../../TOOLS/pdevector.hpp"
35 #include "dynpde_thermal-fluids.hpp"
36 #include "obj_thermal-fluids.hpp"
37 #include "initial_condition.hpp"
38
39 template<class Real>
40 void computeInitialCondition(const ROL::Ptr<ROL::Vector<Real>> &u0,
41 const ROL::Ptr<ROL::Vector<Real>> &ck,
42 const ROL::Ptr<ROL::Vector<Real>> &uo,
43 const ROL::Ptr<ROL::Vector<Real>> &un,
44 const ROL::Ptr<ROL::Vector<Real>> &zk,
45 const ROL::Ptr<DynConstraint<Real>> &con,
46 const Real dt,
47 std::ostream &outStream);
48
49 int main(int argc, char *argv[]) {
50 //feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
51 using RealT = double;
52
53 /*** Initialize communicator. ***/
54 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
55 ROL::Ptr<const Teuchos::Comm<int>> comm
56 = Tpetra::getDefaultComm();
57
58 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
59 const int numProcs = (comm->getSize() > 1) ? comm->getSize() : 0;
60 const int myRank = comm->getRank();
61 ROL::Ptr<std::ostream> outStream = ROL::makeStreamPtr( std::cout, (argc > 1) && (myRank==0) );
62
63 int errorFlag = 0;
64
65 // *** Example body.
66 try {
67
68 /*** Read in XML input ***/
69 ROL::Ptr<ROL::ParameterList> parlist = ROL::getParametersFromXmlFile("input.xml");
70 int nt = parlist->sublist("Time Discretization").get("Number of Time Steps", 100);
71 RealT T = parlist->sublist("Time Discretization").get("End Time", 1.0);
72 RealT dt = T/static_cast<RealT>(nt);
73 int verbosity = parlist->sublist("General").get("Print Verbosity", 0);
74 verbosity = (myRank==0 ? verbosity : 0);
75 bool solveOutput = parlist->sublist("Dynamic Constraint").sublist("Solve").get("Output Iteration History", false);
76 solveOutput = (myRank==0 ? solveOutput : false);
77 parlist->sublist("General").set("Print Verbosity", verbosity);
78 parlist->sublist("Dynamic Constraint").sublist("Solve").set("Output Iteration History", solveOutput);
79 RealT Re = parlist->sublist("Problem").get("Reynolds Number",200.0);
80 RealT Gr = parlist->sublist("Problem").get("Grashof Number", 40000.0);
81 RealT Pr = parlist->sublist("Problem").get("Prandtl Number", 0.72);
82 RealT Tc = parlist->sublist("Problem").get("Cylinder Temperature", 1.0);
83
84 /*************************************************************************/
85 /***************** BUILD GOVERNING PDE ***********************************/
86 /*************************************************************************/
87 /*** Initialize mesh data structure. ***/
88 ROL::Ptr<MeshManager<RealT>> meshMgr
89 = ROL::makePtr<MeshReader<RealT>>(*parlist, numProcs);
90 // Initialize PDE describing Navier-Stokes equations.
91 ROL::Ptr<DynamicPDE_ThermalFluids<RealT>> pde
92 = ROL::makePtr<DynamicPDE_ThermalFluids<RealT>>(*parlist);
93
94 /*************************************************************************/
95 /***************** BUILD CONSTRAINT **************************************/
96 /*************************************************************************/
97 ROL::Ptr<DynConstraint<RealT>> dyn_con
98 = ROL::makePtr<DynConstraint<RealT>>(pde,meshMgr,comm,*parlist,*outStream);
99 const ROL::Ptr<Assembler<RealT>> assembler = dyn_con->getAssembler();
100 dyn_con->setSolveParameters(*parlist);
101 dyn_con->getAssembler()->printMeshData(*outStream);
102
103 /*************************************************************************/
104 /***************** BUILD VECTORS *****************************************/
105 /*************************************************************************/
106 ROL::Ptr<Tpetra::MultiVector<>> u0_ptr = assembler->createStateVector();
107 ROL::Ptr<Tpetra::MultiVector<>> uo_ptr = assembler->createStateVector();
108 ROL::Ptr<Tpetra::MultiVector<>> un_ptr = assembler->createStateVector();
109 ROL::Ptr<Tpetra::MultiVector<>> ck_ptr = assembler->createResidualVector();
110 ROL::Ptr<ROL::Vector<RealT>> u0, uo, un, ck, zk;
111 u0 = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u0_ptr,pde,*assembler,*parlist);
112 uo = ROL::makePtr<PDE_PrimalSimVector<RealT>>(uo_ptr,pde,*assembler,*parlist);
113 un = ROL::makePtr<PDE_PrimalSimVector<RealT>>(un_ptr,pde,*assembler,*parlist);
114 ck = ROL::makePtr<PDE_DualSimVector<RealT>>(ck_ptr,pde,*assembler,*parlist);
115 zk = ROL::makePtr<PDE_OptVector<RealT>>(ROL::makePtr<ROL::StdVector<RealT>>(1));
116 ROL::Ptr<ROL::PartitionedVector<RealT>> z
117 = ROL::PartitionedVector<RealT>::create(*zk, nt);
118
119 /*************************************************************************/
120 /***************** BUILD COST FUNCTIONAL *********************************/
121 /*************************************************************************/
122 std::vector<ROL::Ptr<QoI<RealT>>> qoi_vec(4,ROL::nullPtr), qoi_T(1,ROL::nullPtr);
123 RealT w1 = parlist->sublist("Problem").get("State Cost",1.0);
124 RealT w2 = parlist->sublist("Problem").get("Control Cost",0.0);
125 std::vector<RealT> wts = {w1, w1, w1, w2}, wts_T = {w1};
126 qoi_vec[0] = ROL::makePtr<QoI_State_ThermalFluids<RealT>>("Dissipation",
127 *parlist,
128 pde->getVelocityFE(),
129 pde->getPressureFE(),
130 pde->getThermalFE(),
131 pde->getFieldHelper());
132 qoi_vec[1] = ROL::makePtr<QoI_State_ThermalFluids<RealT>>("Bouyancy",
133 *parlist,
134 pde->getVelocityFE(),
135 pde->getPressureFE(),
136 pde->getThermalFE(),
137 pde->getFieldHelper());
138 qoi_vec[2] = ROL::makePtr<QoI_DownStreamPower_ThermalFluids<RealT>>(pde->getVelocityFE(),
139 pde->getPressureFE(),
140 pde->getThermalFE(),
141 pde->getVelocityBdryFE(1),
142 pde->getBdryCellLocIds(1),
143 pde->getFieldHelper());
144 qoi_vec[3] = ROL::makePtr<QoI_RotationControl_ThermalFluids<RealT>>();
145 qoi_T[0] = ROL::makePtr<QoI_State_ThermalFluids<RealT>>("Tracking",
146 *parlist,
147 pde->getVelocityFE(),
148 pde->getPressureFE(),
149 pde->getThermalFE(),
150 pde->getFieldHelper());
151 ROL::Ptr<ROL::Objective_SimOpt<RealT>> obj_k
152 = ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,wts,assembler);
153 ROL::Ptr<ROL::Objective_SimOpt<RealT>> obj_T
154 = ROL::makePtr<PDE_Objective<RealT>>(qoi_T,wts_T,assembler);
155 ROL::Ptr<LTI_Objective<RealT>> dyn_obj
156 = ROL::makePtr<LTI_Objective<RealT>>(*parlist,obj_k,obj_T);
157
158 /*************************************************************************/
159 /***************** BUILD REDUCED COST FUNCTIONAL *************************/
160 /*************************************************************************/
161 std::vector<ROL::TimeStamp<RealT>> timeStamp(nt);
162 for( int k=0; k<nt; ++k ) {
163 timeStamp.at(k).t.resize(2);
164 timeStamp.at(k).t.at(0) = k*dt;
165 timeStamp.at(k).t.at(1) = (k+1)*dt;
166 }
167 // Compute initial condition
168 std::clock_t timer_init = std::clock();
169 std::stringstream file;
170 file << "initial_condition"
171 << "_Re" << static_cast<int>(Re)
172 << "_Gr" << static_cast<int>(Gr)
173 << "_Pr" << static_cast<int>(100.0*Pr)
174 << "_Tc" << static_cast<int>(Tc) << ".txt";
175 std::ifstream infile(file.str());
176 if (infile.good()) {
177 dyn_con->inputTpetraVector(u0_ptr, file.str());
178 }
179 else {
180 PotentialFlow<RealT> pf(pde->getVelocityFE(),
181 pde->getPressureFE(),
182 pde->getThermalFE(),
183 pde->getCellNodes(),
184 assembler->getDofManager()->getCellDofs(),
185 assembler->getCellIds(),
186 pde->getFieldHelper(),
187 *parlist);
188 pf.build(u0_ptr);
189 computeInitialCondition<RealT>(u0,ck,uo,un,zk,dyn_con,dt,*outStream);
190 dyn_con->outputTpetraVector(u0_ptr, file.str());
191 }
192 *outStream << "Initial condition time: "
193 << static_cast<RealT>(std::clock()-timer_init)/static_cast<RealT>(CLOCKS_PER_SEC)
194 << " seconds." << std::endl << std::endl;
195 // Construct reduce dynamic objective function
196 ROL::ParameterList &rpl = parlist->sublist("Reduced Dynamic Objective");
197 ROL::Ptr<ROL::ReducedDynamicObjective<RealT>> obj
198 = ROL::makePtr<ROL::ReducedDynamicObjective<RealT>>(dyn_obj, dyn_con, u0, zk, ck, timeStamp, rpl);
199
200 /*************************************************************************/
201 /***************** RUN VECTOR AND DERIVATIVE CHECKS **********************/
202 /*************************************************************************/
203 bool checkDeriv = parlist->sublist("Problem").get("Check Derivatives",false);
204 if ( checkDeriv ) {
205 ROL::Ptr<ROL::PartitionedVector<RealT>> dz = ROL::PartitionedVector<RealT>::create(*zk, nt);
206 ROL::Ptr<ROL::PartitionedVector<RealT>> hz = ROL::PartitionedVector<RealT>::create(*zk, nt);
207 zk->randomize(); z->randomize(); dz->randomize(); hz->randomize();
208 uo->randomize(); un->randomize();
209 ROL::ValidateFunction<RealT> validate(1,13,20,11,true,*outStream);
210 ROL::DynamicObjectiveCheck<RealT>::check(*dyn_obj,validate,*uo,*un,*zk);
211 ROL::DynamicConstraintCheck<RealT>::check(*dyn_con,validate,*uo,*un,*zk);
212 obj->checkGradient(*z,*dz,true,*outStream);
213 obj->checkHessVec(*z,*dz,true,*outStream);
214 obj->checkHessSym(*z,*dz,*hz,true,*outStream);
215 }
216
217 /*************************************************************************/
218 /***************** OUTPUT UNCONTROLLED STATE *****************************/
219 /*************************************************************************/
220 bool printU0 = parlist->sublist("Problem").get("Print Uncontrolled State", false);
221 if (printU0) {
222 std::clock_t timer_print0 = std::clock();
223 // Output state and control to file
224 zk->zero(); uo->set(*u0); un->zero();
225 for (int k = 1; k < nt; ++k) {
226 // Print previous state to file
227 std::stringstream u0file;
228 u0file << "uncontrolled_state." << k-1 << ".txt";
229 dyn_con->outputTpetraVector(uo_ptr, u0file.str());
230 // Advance time stepper
231 dyn_con->solve(*ck, *uo, *un, *zk, timeStamp[k]);
232 uo->set(*un);
233 }
234 // Print previous state to file
235 std::stringstream u0file;
236 u0file << "uncontrolled_state." << nt-1 << ".txt";
237 dyn_con->outputTpetraVector(uo_ptr, u0file.str());
238 *outStream << "Output uncontrolled state time: "
239 << static_cast<RealT>(std::clock()-timer_print0)/static_cast<RealT>(CLOCKS_PER_SEC)
240 << " seconds." << std::endl << std::endl;
241 }
242
243 /*************************************************************************/
244 /***************** SOLVE OPTIMIZATION PROBLEM ****************************/
245 /*************************************************************************/
246 ROL::OptimizationProblem<RealT> problem(obj,z);
247 ROL::OptimizationSolver<RealT> solver(problem,*parlist);
248 z->zero();
249 std::clock_t timer = std::clock();
250 solver.solve(*outStream);
251 *outStream << "Optimization time: "
252 << static_cast<RealT>(std::clock()-timer)/static_cast<RealT>(CLOCKS_PER_SEC)
253 << " seconds." << std::endl << std::endl;
254
255 /*************************************************************************/
256 /***************** OUTPUT RESULTS ****************************************/
257 /*************************************************************************/
258 std::clock_t timer_print = std::clock();
259 // Output state and control to file
260 uo->set(*u0); un->zero();
261 for (int k = 1; k < nt; ++k) {
262 // Print previous state to file
263 std::stringstream ufile;
264 ufile << "state." << k-1 << ".txt";
265 dyn_con->outputTpetraVector(uo_ptr, ufile.str());
266 // Print current control
267 if (myRank == 0) {
268 std::stringstream zname;
269 zname << "control." << k-1 << ".txt";
270 std::ofstream zfile;
271 zfile.open(zname.str());
272 zfile << std::scientific << std::setprecision(15);
273 ROL::Ptr<std::vector<RealT>> zn
274 = ROL::dynamicPtrCast<PDE_OptVector<RealT>>(z->get(k-1))->getParameter()->getVector();
275 zfile << std::right << std::setw(25) << (*zn)[0];
276 zfile.close();
277 }
278 // Advance time stepper
279 dyn_con->solve(*ck, *uo, *un, *z->get(k), timeStamp[k]);
280 uo->set(*un);
281 }
282 // Print previous state to file
283 std::stringstream ufile;
284 ufile << "state." << nt-1 << ".txt";
285 dyn_con->outputTpetraVector(uo_ptr, ufile.str());
286 // Print current control
287 if (myRank == 0) {
288 std::stringstream zname;
289 zname << "control." << nt-1 << ".txt";
290 std::ofstream zfile;
291 zfile.open(zname.str());
292 zfile << std::scientific << std::setprecision(15);
293 ROL::Ptr<std::vector<RealT>> zn
294 = ROL::dynamicPtrCast<PDE_OptVector<RealT>>(z->get(nt-1))->getParameter()->getVector();
295 zfile << std::right << std::setw(25) << (*zn)[0];
296 zfile.close();
297 }
298
299 *outStream << "Output time: "
300 << static_cast<RealT>(std::clock()-timer_print)/static_cast<RealT>(CLOCKS_PER_SEC)
301 << " seconds." << std::endl << std::endl;
302 }
303 catch (std::logic_error& err) {
304 *outStream << err.what() << "\n";
305 errorFlag = -1000;
306 }; // end try
307
308 if (errorFlag != 0)
309 std::cout << "End Result: TEST FAILED\n";
310 else
311 std::cout << "End Result: TEST PASSED\n";
312
313 return 0;
314 }
315
316 template<class Real>
317 void computeInitialCondition(const ROL::Ptr<ROL::Vector<Real>> &u0,
318 const ROL::Ptr<ROL::Vector<Real>> &ck,
319 const ROL::Ptr<ROL::Vector<Real>> &uo,
320 const ROL::Ptr<ROL::Vector<Real>> &un,
321 const ROL::Ptr<ROL::Vector<Real>> &zk,
322 const ROL::Ptr<DynConstraint<Real>> &con,
323 const Real dt,
324 std::ostream &outStream) {
325 Real T = 80.0;
326 int nt = static_cast<int>(T/dt);
327 std::vector<ROL::TimeStamp<Real>> ts(nt);
328 for( int k=0; k<nt; ++k ) {
329 ts.at(k).t.resize(2);
330 ts.at(k).t.at(0) = k*dt;
331 ts.at(k).t.at(1) = (k+1)*dt;
332 }
333 // Solve Navier-Stokes equation to determine initial condition
334 zk->zero(); uo->set(*u0); un->zero();
335 Real unorm = uo->norm();
336 outStream << std::scientific << std::setprecision(6);
337 outStream << std::right << std::setw(8) << "ts"
338 << std::right << std::setw(16) << "||u(ts)||"
339 << std::right << std::setw(16) << "avg time (sec)"
340 << std::endl;
341 outStream << std::right << std::setw(8) << 0
342 << std::right << std::setw(16) << unorm
343 << std::right << std::setw(16) << "---"
344 << std::endl;
345 std::vector<Real> time(10);
346 std::clock_t timer_step;
347 Real time_avg(0);
348 for (int k = 1; k < nt; ++k) {
349 // Advance time stepper
350 timer_step = std::clock();
351 con->solve(*ck, *uo, *un, *zk, ts[k]);
352 time[k%10] = static_cast<Real>(std::clock()-timer_step)/static_cast<Real>(CLOCKS_PER_SEC);
353 uo->set(*un);
354 if ( k%10==0 ) {
355 unorm = uo->norm();
356 time_avg = 0.0;
357 for (int i = 0; i < 10; ++i) {
358 time_avg += time[i];
359 }
360 time_avg *= 0.1;
361 outStream << std::right << std::setw(8) << k
362 << std::right << std::setw(16) << unorm
363 << std::right << std::setw(16) << time_avg
364 << std::endl;
365 }
366 }
367 u0->set(*uo);
368 }