// @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 Poisson control problem. */ #include "Teuchos_Comm.hpp" #include "ROL_Stream.hpp" #include "Teuchos_GlobalMPISession.hpp" #include "Teuchos_XMLParameterListHelpers.hpp" #include "Tpetra_Core.hpp" #include "Tpetra_Version.hpp" #include #include #include "ROL_Algorithm.hpp" #include "ROL_Reduced_Objective_SimOpt.hpp" #include "../../TOOLS/meshmanager.hpp" #include "../../TOOLS/linearpdeconstraint.hpp" #include "../../TOOLS/pdeobjective.hpp" #include "../../TOOLS/pdevector.hpp" #include "pde_fractional_poisson.hpp" #include "obj_fractional_poisson.hpp" #include "fractional_constraint.hpp" #include "fractional_objective.hpp" typedef double RealT; int main(int argc, char *argv[]) { // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. int iprint = argc - 1; ROL::Ptr outStream; ROL::nullstream bhs; // outputs nothing /*** Initialize communicator. ***/ Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs); ROL::Ptr > comm = Tpetra::getDefaultComm(); const int myRank = comm->getRank(); if ((iprint > 0) && (myRank == 0)) { outStream = ROL::makePtrFromRef(std::cout); } else { outStream = ROL::makePtrFromRef(bhs); } int errorFlag = 0; // *** Example body. try { /*** Read in XML input ***/ std::string filename = "input.xml"; Teuchos::RCP parlist = Teuchos::rcp( new Teuchos::ParameterList() ); Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); // Set parameters for cylidner mesh RealT s = parlist->sublist("Problem").get("Fractional Power",0.5); RealT gamma = (s == 0.5) ? static_cast(1) : static_cast(3)/(static_cast(2)*s) + static_cast(1e-3); int NX = parlist->sublist("Geometry").get("NX",20); int NY = parlist->sublist("Geometry").get("NY",20); RealT NT = static_cast(NX*NY); RealT width = static_cast(1) + std::log10(NT)/static_cast(3); int NI = static_cast(width * std::sqrt(NT)); parlist->sublist("Geometry").sublist("Cylinder").set("Grading Parameter", gamma); parlist->sublist("Geometry").sublist("Cylinder").set("NI", NI); parlist->sublist("Geometry").sublist("Cylinder").set("Height", width); *outStream << std::endl; *outStream << "Fractional Power: " << s << std::endl; *outStream << "Mesh Grading Parameter: " << gamma << std::endl; *outStream << "Cylinder Height: " << width << std::endl; *outStream << "Number of Intervals: " << NI << std::endl; *outStream << std::endl; // Initialize 2D Poisson's equation ROL::Ptr > meshMgr_local = ROL::makePtr>(*parlist); ROL::Ptr > pde_local = ROL::makePtr>(*parlist); // Initialize 1D singular Poisson's equation ROL::Ptr > meshMgr_cylinder = ROL::makePtr>(*parlist); ROL::Ptr > pde_cylinder = ROL::makePtr>(*parlist); // Build fractional constraint ROL::Ptr > con = ROL::makePtr>(pde_local, meshMgr_local, comm, pde_cylinder, meshMgr_cylinder, comm, *parlist, *outStream); ROL::Ptr > fracCon = ROL::dynamicPtrCast >(con); ROL::Ptr > assembler = fracCon->getLocalAssembler(); // Build objective fuction std::vector > > qoi_vec(2,ROL::nullPtr); qoi_vec[0] = ROL::makePtr>(pde_local->getFE()); qoi_vec[1] = ROL::makePtr>(pde_local->getFE()); RealT stateCost = parlist->sublist("Problem").get("State Cost",1e0); RealT controlCost = parlist->sublist("Problem").get("Control Cost",1e0); std::vector wts = {stateCost, controlCost}; ROL::Ptr > pdeobj = ROL::makePtr>(qoi_vec,wts,assembler); ROL::Ptr > obj = ROL::makePtr>(pdeobj); // Build vectors ROL::Ptr > stateVec = assembler->createStateVector(); ROL::Ptr > u_ptr = ROL::makePtr>(stateVec->getMap(),NI+1); ROL::Ptr > p_ptr = ROL::makePtr>(stateVec->getMap(),NI+1); ROL::Ptr > du_ptr = ROL::makePtr>(stateVec->getMap(),NI+1); u_ptr->randomize(); //u_ptr->putScalar(static_cast(1)); p_ptr->randomize(); //p_ptr->putScalar(static_cast(1)); du_ptr->randomize(); //du_ptr->putScalar(static_cast(0)); ROL::Ptr > up = ROL::makePtr>(u_ptr); ROL::Ptr > pp = ROL::makePtr>(p_ptr); ROL::Ptr > dup = ROL::makePtr>(du_ptr); // Create residual vectors ROL::Ptr > conVec = assembler->createResidualVector(); ROL::Ptr > r_ptr = ROL::makePtr>(conVec->getMap(),NI+1); r_ptr->randomize(); //r_ptr->putScalar(static_cast(1)); ROL::Ptr > rp = ROL::makePtr>(r_ptr); // Create control vector and set to ones ROL::Ptr > z_ptr = assembler->createControlVector(); ROL::Ptr > dz_ptr = assembler->createControlVector(); z_ptr->randomize(); //z_ptr->putScalar(static_cast(1)); dz_ptr->randomize(); //dz_ptr->putScalar(static_cast(0)); ROL::Ptr > zp = ROL::makePtr>(z_ptr); ROL::Ptr > dzp = ROL::makePtr>(dz_ptr); // Create ROL SimOpt vectors ROL::Vector_SimOpt x(up,zp); ROL::Vector_SimOpt d(dup,dzp); // Build reduced objective function bool storage = parlist->sublist("Problem").get("Use State and Adjoint Storage",true); ROL::Ptr > objReduced = ROL::makePtr>(obj, con, up, zp, pp, storage, false); // Check derivatives. bool checkDeriv = parlist->sublist("Problem").get("Check Derivatives",false); if ( checkDeriv ) { *outStream << "\n\nCheck Gradient_1 of Full Objective Function\n"; obj->checkGradient_1(*up,*zp,*dup,true,*outStream); *outStream << "\n\nCheck Gradient_2 of Full Objective Function\n"; obj->checkGradient_2(*up,*zp,*dzp,true,*outStream); *outStream << "\n\nCheck Gradient of Full Objective Function\n"; obj->checkGradient(x,d,true,*outStream); *outStream << "\n\nCheck Hessian_11 of Full Objective Function\n"; obj->checkHessVec_11(*up,*zp,*dup,true,*outStream); *outStream << "\n\nCheck Hessian_12 of Full Objective Function\n"; obj->checkHessVec_12(*up,*zp,*dzp,true,*outStream); *outStream << "\n\nCheck Hessian_21 of Full Objective Function\n"; obj->checkHessVec_21(*up,*zp,*dup,true,*outStream); *outStream << "\n\nCheck Hessian_22 of Full Objective Function\n"; obj->checkHessVec_22(*up,*zp,*dzp,true,*outStream); *outStream << "\n\nCheck Hessian of Full Objective Function\n"; obj->checkHessVec(x,d,true,*outStream); *outStream << "\n\nCheck Full Jacobian of PDE Constraint\n"; con->checkApplyJacobian(x,d,*rp,true,*outStream); *outStream << "\n\nCheck Jacobian_1 of PDE Constraint\n"; con->checkApplyJacobian_1(*up,*zp,*dup,*rp,true,*outStream); *outStream << "\n\nCheck Jacobian_2 of PDE Constraint\n"; con->checkApplyJacobian_2(*up,*zp,*dzp,*rp,true,*outStream); *outStream << "\n\nCheck Full Hessian of PDE Constraint\n"; con->checkApplyAdjointHessian(x,*pp,d,x,true,*outStream); *outStream << "\n\nCheck Hessian_11 of PDE Constraint\n"; con->checkApplyAdjointHessian_11(*up,*zp,*pp,*dup,*dup,true,*outStream); *outStream << "\n\nCheck Hessian_21 of PDE Constraint\n"; con->checkApplyAdjointHessian_21(*up,*zp,*pp,*dzp,*dup,true,*outStream); *outStream << "\n\nCheck Hessian_12 of PDE Constraint\n"; con->checkApplyAdjointHessian_12(*up,*zp,*pp,*dup,*dzp,true,*outStream); *outStream << "\n\nCheck Hessian_22 of PDE Constraint\n"; con->checkApplyAdjointHessian_22(*up,*zp,*pp,*dzp,*dzp,true,*outStream); *outStream << "\n"; con->checkAdjointConsistencyJacobian(*dup,d,x,true,*outStream); *outStream << "\n"; con->checkInverseJacobian_1(*up,*up,*up,*zp,true,*outStream); *outStream << "\n"; *outStream << "\n\nCheck Gradient of Reduced Objective Function\n"; objReduced->checkGradient(*zp,*dzp,true,*outStream); *outStream << "\n\nCheck Hessian of Reduced Objective Function\n"; objReduced->checkHessVec(*zp,*dzp,true,*outStream); } // Run optimization ROL::Ptr > algo = ROL::makePtr>("Trust Region",*parlist,false); zp->zero(); algo->run(*zp,*objReduced,true,*outStream); // Get a summary from the time monitor. Teuchos::TimeMonitor::summarize(); } 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; }