// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /*! \file example_01.cpp \brief Shows how to solve the Navier-Stokes control problem. */ #include "Teuchos_Comm.hpp" #include "Teuchos_GlobalMPISession.hpp" #include "Tpetra_Core.hpp" #include "Tpetra_Version.hpp" #include #include //#include #include "ROL_Bounds.hpp" #include "ROL_Stream.hpp" #include "ROL_ParameterList.hpp" #include "ROL_Solver.hpp" #include "ROL_ReducedDynamicObjective.hpp" #include "ROL_DynamicConstraintCheck.hpp" #include "ROL_DynamicObjectiveCheck.hpp" #include "../../TOOLS/dynconstraint.hpp" #include "../../TOOLS/pdeconstraint.hpp" #include "../../TOOLS/pdeobjective.hpp" #include "../../TOOLS/ltiobjective.hpp" #include "../../TOOLS/meshreader.hpp" #include "../../TOOLS/pdevector.hpp" #include "dynpde_navier-stokes.hpp" #include "obj_navier-stokes.hpp" #include "initial_condition.hpp" template void computeInitialCondition(const ROL::Ptr> &u0, const ROL::Ptr> &ck, const ROL::Ptr> &uo, const ROL::Ptr> &un, const ROL::Ptr> &zk, const ROL::Ptr> &con, const Real dt, std::ostream &outStream); int main(int argc, char *argv[]) { //feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); using RealT = double; /*** Initialize communicator. ***/ Teuchos::GlobalMPISession mpiSession(&argc, &argv); ROL::Ptr> comm = Tpetra::getDefaultComm(); // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. const int numProcs = (comm->getSize() > 1) ? comm->getSize() : 0; const int myRank = comm->getRank(); ROL::Ptr outStream = ROL::makeStreamPtr( std::cout, (argc > 1) && (myRank==0) ); int errorFlag = 0; // *** Example body. try { /*** Read in XML input ***/ ROL::Ptr parlist = ROL::getParametersFromXmlFile("input.xml"); int nt = parlist->sublist("Time Discretization").get("Number of Time Steps", 100); RealT T = parlist->sublist("Time Discretization").get("End Time", 1.0); RealT dt = T/static_cast(nt); bool useParametricControl = parlist->sublist("Problem").get("Use Parametric Control", false); int verbosity = parlist->sublist("General").get("Print Verbosity", 0); verbosity = (myRank==0 ? verbosity : 0); parlist->sublist("General").set("Print Verbosity", verbosity); bool solveOutput = parlist->sublist("Dynamic Constraint").sublist("Solve").get("Output Iteration History", false); solveOutput = (myRank==0 ? solveOutput : false); parlist->sublist("Dynamic Constraint").sublist("Solve").set("Output Iteration History", solveOutput); solveOutput = parlist->sublist("SimOpt").sublist("Solve").get("Output Iteration History", false); solveOutput = (myRank==0 ? solveOutput : false); parlist->sublist("SimOpt").sublist("Solve").set("Output Iteration History", solveOutput); RealT Re = parlist->sublist("Problem").get("Reynolds Number",200.0); parlist->sublist("Reduced Dynamic Objective").set("State Domain Seed",12321*(myRank+1)); parlist->sublist("Reduced Dynamic Objective").set("State Range Seed", 32123*(myRank+1)); parlist->sublist("Reduced Dynamic Objective").set("Adjoint Domain Seed",23432*(myRank+1)); parlist->sublist("Reduced Dynamic Objective").set("Adjoint Range Seed", 43234*(myRank+1)); parlist->sublist("Reduced Dynamic Objective").set("State Sensitivity Domain Seed",34543*(myRank+1)); parlist->sublist("Reduced Dynamic Objective").set("State Sensitivity Range Seed", 54345*(myRank+1)); /*************************************************************************/ /***************** BUILD GOVERNING PDE ***********************************/ /*************************************************************************/ /*** Initialize mesh data structure. ***/ ROL::Ptr> meshMgr = ROL::makePtr>(*parlist, numProcs); // Initialize PDE describing Navier-Stokes equations. ROL::Ptr> pde = ROL::makePtr>(*parlist); /*************************************************************************/ /***************** BUILD CONSTRAINT **************************************/ /*************************************************************************/ ROL::Ptr> dyn_con = ROL::makePtr>(pde,meshMgr,comm,*parlist,*outStream); const ROL::Ptr> assembler = dyn_con->getAssembler(); dyn_con->setSolveParameters(*parlist); dyn_con->getAssembler()->printMeshData(*outStream); /*************************************************************************/ /***************** BUILD VECTORS *****************************************/ /*************************************************************************/ ROL::Ptr> u0_ptr = assembler->createStateVector(); ROL::Ptr> uo_ptr = assembler->createStateVector(); ROL::Ptr> un_ptr = assembler->createStateVector(); ROL::Ptr> ck_ptr = assembler->createResidualVector(); ROL::Ptr> u0, uo, un, ck, zk; u0 = ROL::makePtr>(u0_ptr,pde,*assembler,*parlist); uo = ROL::makePtr>(uo_ptr,pde,*assembler,*parlist); un = ROL::makePtr>(un_ptr,pde,*assembler,*parlist); ck = ROL::makePtr>(ck_ptr,pde,*assembler,*parlist); if (!useParametricControl) { ROL::Ptr> zk_ptr = assembler->createControlVector(); zk = ROL::makePtr>(zk_ptr,pde,*assembler,*parlist); } else { zk = ROL::makePtr>(ROL::makePtr>(1)); } ROL::Ptr> z = ROL::PartitionedVector::create(*zk, nt); /*************************************************************************/ /***************** BUILD COST FUNCTIONAL *********************************/ /*************************************************************************/ std::vector>> qoi_vec(3,ROL::nullPtr), qoi_T(1,ROL::nullPtr); RealT w1 = parlist->sublist("Problem").get("State Cost",1.0); RealT w2 = parlist->sublist("Problem").get("State Boundary Cost",1.0); RealT w3 = parlist->sublist("Problem").get("Control Cost",0.0); RealT wT = parlist->sublist("Problem").get("Final Time State Cost",1.0); std::vector wts = {w1, w2, w3}, wts_T = {wT}; std::string intObj = parlist->sublist("Problem").get("Integrated Objective Type", "Dissipation"); std::string ftObj = parlist->sublist("Problem").get("Final Time Objective Type", "Tracking"); qoi_vec[0] = ROL::makePtr>(intObj, *parlist, pde->getVelocityFE(), pde->getPressureFE(), pde->getFieldHelper()); qoi_vec[1] = ROL::makePtr>(pde->getVelocityFE(), pde->getPressureFE(), pde->getVelocityBdryFE(1), pde->getBdryCellLocIds(1), pde->getFieldHelper()); qoi_T[0] = ROL::makePtr>(ftObj, *parlist, pde->getVelocityFE(), pde->getPressureFE(), pde->getFieldHelper()); if (useParametricControl) { qoi_vec[2] = ROL::makePtr>(); } else { qoi_vec[2] = ROL::makePtr>(pde->getVelocityFE(), pde->getPressureFE(), pde->getVelocityBdryFE(4), pde->getBdryCellLocIds(4), pde->getFieldHelper()); } ROL::Ptr> obj_k = ROL::makePtr>(qoi_vec,wts,assembler); ROL::Ptr> obj_T = ROL::makePtr>(qoi_T,wts_T,assembler); ROL::Ptr> dyn_obj = ROL::makePtr>(*parlist,obj_k,obj_T); /*************************************************************************/ /***************** BUILD REDUCED COST FUNCTIONAL *************************/ /*************************************************************************/ std::vector> timeStamp(nt); for( int k=0; k(Re) << ".txt"; std::ifstream infile(file.str()); if (infile.good()) { dyn_con->inputTpetraVector(u0_ptr, file.str()); } else { PotentialFlow pf(pde->getVelocityFE(), pde->getPressureFE(), pde->getCellNodes(), assembler->getDofManager()->getCellDofs(), assembler->getCellIds(), pde->getFieldHelper(), *parlist); pf.build(u0_ptr); computeInitialCondition(u0,ck,uo,un,zk,dyn_con,dt,*outStream); dyn_con->outputTpetraVector(u0_ptr, file.str()); } *outStream << "Initial condition time: " << static_cast(std::clock()-timer_init)/static_cast(CLOCKS_PER_SEC) << " seconds." << std::endl << std::endl; // Construct reduce dynamic objective function ROL::ParameterList &rpl = parlist->sublist("Reduced Dynamic Objective"); ROL::Ptr> obj = ROL::makePtr>(dyn_obj, dyn_con, u0, zk, ck, timeStamp, rpl, outStream); /*************************************************************************/ /***************** RUN VECTOR AND DERIVATIVE CHECKS **********************/ /*************************************************************************/ bool checkDeriv = parlist->sublist("Problem").get("Check Derivatives",false); if ( checkDeriv ) { ROL::Ptr> dz = ROL::PartitionedVector::create(*zk, nt); ROL::Ptr> hz = ROL::PartitionedVector::create(*zk, nt); zk->randomize(); z->randomize(); dz->randomize(); hz->randomize(); uo->randomize(); un->randomize(); ROL::ValidateFunction validate(1,13,20,11,true,*outStream); ROL::DynamicObjectiveCheck::check(*dyn_obj,validate,*uo,*un,*zk); ROL::DynamicConstraintCheck::check(*dyn_con,validate,*uo,*un,*zk); obj->checkGradient(*z,*dz,true,*outStream); obj->checkHessVec(*z,*dz,true,*outStream); obj->checkHessSym(*z,*dz,*hz,true,*outStream); } /*************************************************************************/ /***************** RUN VECTOR AND DERIVATIVE CHECKS **********************/ /*************************************************************************/ bool printU0 = parlist->sublist("Problem").get("Print Uncontrolled State", false); if (printU0) { std::clock_t timer_print0 = std::clock(); zk->zero(); uo->set(*u0); un->zero(); for (int k = 1; k < nt; ++k) { // Print previous state to file std::stringstream u0file; u0file << "uncontrolled_state." << k-1 << ".txt"; dyn_con->outputTpetraVector(uo_ptr, u0file.str()); // Advance time stepper dyn_con->solve(*ck, *uo, *un, *zk, timeStamp[k]); uo->set(*un); } // Print previous state to file std::stringstream u0file; u0file << "uncontrolled_state." << nt-1 << ".txt"; dyn_con->outputTpetraVector(uo_ptr, u0file.str()); *outStream << "Output uncontrolled state time: " << static_cast(std::clock()-timer_print0)/static_cast(CLOCKS_PER_SEC) << " seconds." << std::endl << std::endl; } /*************************************************************************/ /***************** SOLVE OPTIMIZATION PROBLEM ****************************/ /*************************************************************************/ z->zero(); if (useParametricControl) { // Linearly interpolate between optimal values for angular velocity // amplitude and Strouhal number obtained for Re=200, 1000 in // JW He, R Glowinski, R Metcalfe, A Nordlander, J Periaux // Active Control and Drag Optimization for Flow Past a // Circular Cylinder // Journal of Computation Physics, 163, pg. 83-117, 2000. RealT Re = parlist->sublist("Problem").get("Reynolds Number",200.0); RealT amp0 = 6.0 - (Re - 200.0)/1600.0; RealT Se0 = 0.74 - (Re - 200.0) * (0.115/800.0); RealT amp = parlist->sublist("Problem").sublist("Initial Guess").get("Amplitude", amp0); RealT Se = parlist->sublist("Problem").sublist("Initial Guess").get("Strouhal Number", Se0); RealT ph = parlist->sublist("Problem").sublist("Initial Guess").get("Phase Shift", 0.0); for( int k=0; k> zn = ROL::dynamicPtrCast>(z->get(k))->getParameter()->getVector(); (*zn)[0] = -amp * std::sin(2.0 * M_PI * Se * timeStamp[k].t[0] + ph); } } ROL::Ptr> problem = ROL::makePtr>(obj,z); problem->finalize(false,true,*outStream); ROL::Solver solver(problem,*parlist); std::clock_t timer = std::clock(); solver.solve(*outStream); *outStream << "Optimization time: " << static_cast(std::clock()-timer)/static_cast(CLOCKS_PER_SEC) << " seconds." << std::endl << std::endl; /*************************************************************************/ /***************** OUTPUT RESULTS ****************************************/ /*************************************************************************/ std::clock_t timer_print = std::clock(); // Output state and control to file uo->set(*u0); un->zero(); for (int k = 1; k < nt; ++k) { // Print previous state to file std::stringstream ufile; ufile << "state." << k-1 << ".txt"; dyn_con->outputTpetraVector(uo_ptr, ufile.str()); // Print current control if (!useParametricControl) { std::stringstream zfile; zfile << "control." << k-1 << ".txt"; dyn_con->outputTpetraVector(ROL::dynamicPtrCast>(z->get(k-1))->getVector(),zfile.str()); } else { if (myRank == 0) { std::stringstream zname; zname << "control." << k-1 << ".txt"; std::ofstream zfile; zfile.open(zname.str()); zfile << std::scientific << std::setprecision(15); ROL::Ptr> zn = ROL::dynamicPtrCast>(z->get(k-1))->getParameter()->getVector(); zfile << std::right << std::setw(25) << (*zn)[0]; zfile.close(); } } // Advance time stepper dyn_con->solve(*ck, *uo, *un, *z->get(k), timeStamp[k]); uo->set(*un); } // Print previous state to file std::stringstream ufile; ufile << "state." << nt-1 << ".txt"; dyn_con->outputTpetraVector(uo_ptr, ufile.str()); // Print current control if (!useParametricControl) { std::stringstream zfile; zfile << "control." << nt-1 << ".txt"; dyn_con->outputTpetraVector(ROL::dynamicPtrCast>(z->get(nt-1))->getVector(),zfile.str()); } else { if (myRank == 0) { std::stringstream zname; zname << "control." << nt-1 << ".txt"; std::ofstream zfile; zfile.open(zname.str()); zfile << std::scientific << std::setprecision(15); ROL::Ptr> zn = ROL::dynamicPtrCast>(z->get(nt-1))->getParameter()->getVector(); zfile << std::right << std::setw(25) << (*zn)[0]; zfile.close(); } } *outStream << "Output time: " << static_cast(std::clock()-timer_print)/static_cast(CLOCKS_PER_SEC) << " seconds." << std::endl << std::endl; } catch (std::logic_error& err) { *outStream << err.what() << "\n"; errorFlag = -1000; }; // end try if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; return 0; } template void computeInitialCondition(const ROL::Ptr> &u0, const ROL::Ptr> &ck, const ROL::Ptr> &uo, const ROL::Ptr> &un, const ROL::Ptr> &zk, const ROL::Ptr> &con, const Real dt, std::ostream &outStream) { Real T = 80.0; int nt = static_cast(T/dt); std::vector> ts(nt); for( int k=0; kzero(); uo->set(*u0); un->zero(); Real unorm = uo->norm(); outStream << std::scientific << std::setprecision(6); outStream << std::right << std::setw(8) << "ts" << std::right << std::setw(16) << "||u(ts)||" << std::right << std::setw(16) << "avg time (sec)" << std::endl; outStream << std::right << std::setw(8) << 0 << std::right << std::setw(16) << unorm << std::right << std::setw(16) << "---" << std::endl; std::vector time(10); std::clock_t timer_step; Real time_avg(0); for (int k = 1; k < nt; ++k) { // Advance time stepper timer_step = std::clock(); con->solve(*ck, *uo, *un, *zk, ts[k]); time[k%10] = static_cast(std::clock()-timer_step)/static_cast(CLOCKS_PER_SEC); uo->set(*un); if ( k%10==0 ) { unorm = uo->norm(); time_avg = 0.0; for (int i = 0; i < 10; ++i) { time_avg += time[i]; } time_avg *= 0.1; outStream << std::right << std::setw(8) << k << std::right << std::setw(16) << unorm << std::right << std::setw(16) << time_avg << std::endl; } } u0->set(*uo); }