// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /*! \file example_03.cpp \brief Shows how to solve a steady Burgers' optimal control problem using full-space methods. */ #include "ROL_MonteCarloGenerator.hpp" #include "ROL_Stream.hpp" #include "Teuchos_GlobalMPISession.hpp" #include "Teuchos_Comm.hpp" #include "Teuchos_DefaultComm.hpp" #include "Teuchos_CommHelpers.hpp" #include #include #include #include "test_05.hpp" typedef double RealT; typedef H1VectorPrimal PrimalStateVector; typedef H1VectorDual DualStateVector; typedef L2VectorPrimal PrimalControlVector; typedef L2VectorDual DualControlVector; typedef H1VectorDual PrimalConstraintVector; typedef H1VectorPrimal DualConstraintVector; int main(int argc, char *argv[]) { Teuchos::GlobalMPISession mpiSession(&argc, &argv); ROL::Ptr > comm = ROL::toPtr(Teuchos::DefaultComm::getComm()); // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. int iprint = argc - 1; bool print = (iprint>0); ROL::Ptr outStream; ROL::nullstream bhs; // outputs nothing if (print) outStream = ROL::makePtrFromRef(std::cout); else outStream = ROL::makePtrFromRef(bhs); int errorFlag = 0; // *** Example body. try { /*************************************************************************/ /************* INITIALIZE BURGERS FEM CLASS ******************************/ /*************************************************************************/ int nx = 512; // Set spatial discretization. RealT nl = 1.0; // Nonlinearity parameter (1 = Burgers, 0 = linear). RealT cH1 = 1.0; // Scale for derivative term in H1 norm. RealT cL2 = 0.0; // Scale for mass term in H1 norm. ROL::Ptr > fem = ROL::makePtr>(nx,nl,cH1,cL2); fem->test_inverse_mass(*outStream); fem->test_inverse_H1(*outStream); /*************************************************************************/ /************* INITIALIZE SIMOPT EQUALITY CONSTRAINT *********************/ /*************************************************************************/ bool hess = true; ROL::Ptr > pcon = ROL::makePtr>(fem,hess); /*************************************************************************/ /************* INITIALIZE VECTOR STORAGE *********************************/ /*************************************************************************/ // INITIALIZE CONTROL VECTORS ROL::Ptr > z_ptr = ROL::makePtr>(nx+2, 0.0); ROL::Ptr > zp = ROL::makePtr(z_ptr,fem); // INITIALIZE STATE VECTORS ROL::Ptr > u_ptr = ROL::makePtr>(nx, 1.0); ROL::Ptr > up = ROL::makePtr(u_ptr,fem); // INITIALIZE CONSTRAINT VECTORS ROL::Ptr > c_ptr = ROL::makePtr>(nx, 1.0); ROL::Ptr > cp = ROL::makePtr(c_ptr,fem); /*************************************************************************/ /************* INITIALIZE SAMPLE GENERATOR *******************************/ /*************************************************************************/ int dim = 4, nSamp = 10000; std::vector tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0; std::vector > bounds(dim,tmp); ROL::Ptr > bman = ROL::makePtr>(comm); ROL::Ptr > sampler = ROL::makePtr>( nSamp,bounds,bman,false,false,100); /*************************************************************************/ /************* CHECK DERIVATIVES AND CONSISTENCY *************************/ /*************************************************************************/ RealT tol = std::sqrt(ROL::ROL_EPSILON()); for (int i = sampler->start(); i < sampler->numMySamples(); i++) { // *outStream << "(" << sampler->getMyPoint(i)[0] << ", " // << sampler->getMyPoint(i)[1] << ", " // << sampler->getMyPoint(i)[2] << ", " // << sampler->getMyPoint(i)[3] << ")\n"; pcon->setParameter(sampler->getMyPoint(i)); pcon->solve(*cp,*up,*zp,tol); RealT rnorm = cp->norm(); pcon->value(*cp,*up,*zp,tol); RealT cnorm = cp->norm(); errorFlag += ((cnorm > tol) ? 1 : 0) + ((rnorm > tol) ? 1 : 0); *outStream << "Sample " << i << " Rank " << sampler->batchID() << "\n"; *outStream << std::scientific << std::setprecision(8); *outStream << "Test SimOpt solve at feasible (u,z):\n"; *outStream << " Solver Residual = " << rnorm << "\n"; *outStream << " ||c(u,z)|| = " << cnorm; *outStream << "\n\n"; } } catch (std::logic_error& err) { *outStream << err.what() << "\n"; errorFlag = -1000; }; // end try comm->barrier(); if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; return 0; }