// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /*! \file example_01_l1.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_OptimizationSolver.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" #include "l1penaltydynamic.hpp" #include "ROL_TypeP_TrustRegionAlgorithm.hpp" #include "ROL_TypeP_ProxGradientAlgorithm.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_02.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", true); int verbosity = parlist->sublist("General").get("Output Level", 1); verbosity = (myRank==0 ? verbosity : 0); parlist->sublist("General").set("Output Level", verbosity); bool solveOutput = parlist->sublist("Dynamic Constraint").sublist("Solve").get("Output Iteration History", true); 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", true); 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); // nt 'clones' of zk's // write l1 obj that takes in rol vector, loops nt entries, grabs // data from nt entries, sum abs of zk clones /*************************************************************************/ /***************** 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("L2 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 // pde integrated obj = ROL::makePtr>(qoi_vec,wts,assembler); ROL::Ptr> obj_T // final time obj = ROL::makePtr>(qoi_T,wts_T,assembler); ROL::Ptr> dyn_obj // combination = ROL::makePtr>(*parlist,obj_k,obj_T);// integrates over interval, added over timepoints somewhere /*************************************************************************/ /***************** 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);// summing over // create l1 dynamic objective for nobj, pass to TRnonsmooth ROL::Ptr> zlo = ROL::PartitionedVector::create(*zk, nt); ROL::Ptr> zhi = ROL::PartitionedVector::create(*zk, nt); zlo->setScalar(parlist->sublist("Problem").get("Lower Control Bound", -1.e0)); zhi->setScalar(parlist->sublist("Problem").get("Upper Control Bound", 1.e0)); ROL::Ptr> nobj = ROL::makePtr>(*parlist,timeStamp, zlo, zhi); //Algo pointer ROL::Ptr> algo; //ROL::Ptr> algo; /*************************************************************************/ /***************** 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) { bool readFromFile = parlist->sublist("Initial Guess").get("Read From File",false); if (!readFromFile) { // 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); } } else { for (int k = 1; k < nt; ++k) { std::stringstream zname; zname << "initial_control." << k-1 << ".txt"; std::fstream zfile; zfile.open(zname.str(),std::ios::in); if (!zfile.is_open()) std::cout << "CANNOT OPEN " << zname.str() << std::endl; zfile >> (*(ROL::dynamicPtrCast>(z->get(k-1))->getParameter()->getVector()))[0]; zfile.close(); } } } //parlist->sublist("Step").sublist("Trust Region").sublist("TRN").sublist("Solver").set("Subproblem Solver", "NCG"); algo = ROL::makePtr>(*parlist); //algo = ROL::makePtr>(*parlist); //ROL::OptimizationProblem problem(obj,z);// need to change this //ROL::OptimizationSolver solver(problem,*parlist);// need to change this std::clock_t timer = std::clock(); //solver.solve(*outStream); algo->run(*z, *obj, *nobj, *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); }