// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #include "Teuchos_ParameterList.hpp" #include "Teuchos_XMLParameterListHelpers.hpp" #include "ROL_Stream.hpp" #include "Teuchos_LAPACK.hpp" #include "Teuchos_GlobalMPISession.hpp" #include "Teuchos_Comm.hpp" #include "Teuchos_DefaultComm.hpp" #include "Teuchos_CommHelpers.hpp" #include // ROL_Types contains predefined constants and objects #include "ROL_Types.hpp" // ROL algorithmic information #include "ROL_Algorithm.hpp" #include "ROL_ConstraintStatusTest.hpp" #include "ROL_CompositeStep.hpp" #include "ROL_TrustRegionStep.hpp" // ROL vectors #include "ROL_StdVector.hpp" // ROL objective functions and constraints #include "ROL_Objective_SimOpt.hpp" #include "ROL_Constraint_SimOpt.hpp" #include "ROL_Reduced_Objective_SimOpt.hpp" #include "ROL_OptimizationProblem.hpp" // ROL sample generators #include "ROL_MonteCarloGenerator.hpp" #include "ROL_StdTeuchosBatchManager.hpp" #include "ROL_SimulatedConstraint.hpp" #include "ROL_SimulatedObjective.hpp" #include "ROL_SimulatedObjectiveCVaR.hpp" template class Constraint_BurgersControl : public ROL::Constraint_SimOpt { private: int nx_; Real dx_; Real compute_norm(const std::vector &r) { return std::sqrt(dot(r,r)); } Real dot(const std::vector &x, const std::vector &y) { Real ip = 0.0; Real c = (((int)x.size()==nx_) ? 4.0 : 2.0); for (unsigned i=0; i::update; void update(std::vector &u, const std::vector &s, const Real alpha=1.0) { for (unsigned i=0; i &u, const Real alpha=0.0) { for (unsigned i=0; i &r, const std::vector &u, const std::vector &z) { r.clear(); r.resize(nx_,0.0); const std::vector param = ROL::Constraint_SimOpt::getParameter(); Real nu = std::pow(10.0,param[0]-2.0); Real f = param[1]/100.0; Real u0 = 1.0+param[2]/1000.0; Real u1 = param[3]/1000.0; for (int i=0; i0) { r[i] -= u[i-1]*(u[i-1]+u[i])/6.0; } // Contribution from control r[i] -= dx_/6.0*(z[i]+4.0*z[i+1]+z[i+2]); // Contribution from right-hand side r[i] -= dx_*f; } // Contribution from Dirichlet boundary terms r[ 0] -= u0*u[ 0]/6.0 + u0*u0/6.0 + nu*u0/dx_; r[nx_-1] += u1*u[nx_-1]/6.0 + u1*u1/6.0 - nu*u1/dx_; } void compute_pde_jacobian(std::vector &dl, std::vector &d, std::vector &du, const std::vector &u) { const std::vector param = ROL::Constraint_SimOpt::getParameter(); Real nu = std::pow(10.0,param[0]-2.0); Real u0 = 1.0+param[2]/1000.0; Real u1 = param[3]/1000.0; // Get Diagonal and Off-Diagonal Entries of linear PDE Jacobian d.clear(); d.resize(nx_,nu*2.0/dx_); dl.clear(); dl.resize(nx_-1,-nu/dx_); du.clear(); du.resize(nx_-1,-nu/dx_); // Contribution from nonlinearity for (int i=0; i0) { d[i] += -u[i-1]/6.0; du[i-1] += (u[i-1]+2.0*u[i])/6.0; } } // Contribution from Dirichlet boundary conditions d[ 0] -= u0/6.0; d[nx_-1] += u1/6.0; } void linear_solve(std::vector &u, std::vector &dl, std::vector &d, std::vector &du, const std::vector &r, const bool transpose = false) { u.assign(r.begin(),r.end()); // Perform LDL factorization Teuchos::LAPACK lp; std::vector du2(nx_-2,0.0); std::vector ipiv(nx_,0); int info; int ldb = nx_; int nhrs = 1; lp.GTTRF(nx_,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&info); char trans = 'N'; if ( transpose ) { trans = 'T'; } lp.GTTRS(trans,nx_,nhrs,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&u[0],ldb,&info); } ROL::Ptr> getVector( ROL::Vector& x ) { return dynamic_cast&>(x).getVector(); } ROL::Ptr> getVector( const ROL::Vector& x ) { return dynamic_cast&>(x).getVector(); } public: Constraint_BurgersControl(int nx = 128) : nx_(nx), dx_(1.0/((Real)nx+1.0)) {} void value(ROL::Vector &c, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { auto cp = getVector(c); auto up = getVector(u); auto zp = getVector(z); compute_residual(*cp,*up,*zp); } void solve(ROL::Vector &c, ROL::Vector &u, const ROL::Vector &z, Real &tol) { auto up = getVector(u); up->assign(up->size(),1.0); ROL::Constraint_SimOpt::solve(c,u,z,tol); } void applyJacobian_1(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { auto jvp = getVector(jv); auto vp = getVector(v); auto up = getVector(u); auto zp = getVector(z); const std::vector param = ROL::Constraint_SimOpt::getParameter(); Real nu = std::pow(10.0,param[0]-2.0); Real u0 = 1.0+param[2]/1000.0; Real u1 = param[3]/1000.0; // Fill jvp for (int i = 0; i < nx_; i++) { (*jvp)[i] = nu/dx_*2.0*(*vp)[i]; if ( i > 0 ) { (*jvp)[i] += -nu/dx_*(*vp)[i-1] -(*up)[i-1]/6.0*(*vp)[i] -((*up)[i]+2.0*(*up)[i-1])/6.0*(*vp)[i-1]; } if ( i < nx_-1 ) { (*jvp)[i] += -nu/dx_*(*vp)[i+1] +(*up)[i+1]/6.0*(*vp)[i] +((*up)[i]+2.0*(*up)[i+1])/6.0*(*vp)[i+1]; } } (*jvp)[ 0] -= u0/6.0*(*vp)[0]; (*jvp)[nx_-1] += u1/6.0*(*vp)[nx_-1]; } void applyJacobian_2(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { auto jvp = getVector(jv); auto vp = getVector(v); auto up = getVector(u); auto zp = getVector(z); for (int i=0; i &ijv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { auto ijvp = getVector(ijv); auto vp = getVector(v); auto up = getVector(u); auto zp = getVector(z); // Get PDE Jacobian std::vector d(nx_,0.0); std::vector dl(nx_-1,0.0); std::vector du(nx_-1,0.0); compute_pde_jacobian(dl,d,du,*up); // Solve solve state sensitivity system at current time step linear_solve(*ijvp,dl,d,du,*vp); } void applyAdjointJacobian_1(ROL::Vector &ajv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { auto ajvp = getVector(ajv); auto vp = getVector(v); auto up = getVector(u); auto zp = getVector(z); const std::vector param = ROL::Constraint_SimOpt::getParameter(); Real nu = std::pow(10.0,param[0]-2.0); Real u0 = 1.0+param[2]/1000.0; Real u1 = param[3]/1000.0; // Fill jvp for (int i = 0; i < nx_; i++) { (*ajvp)[i] = nu/dx_*2.0*(*vp)[i]; if ( i > 0 ) { (*ajvp)[i] += -nu/dx_*(*vp)[i-1] -(*up)[i-1]/6.0*(*vp)[i] +((*up)[i-1]+2.0*(*up)[i])/6.0*(*vp)[i-1]; } if ( i < nx_-1 ) { (*ajvp)[i] += -nu/dx_*(*vp)[i+1] +(*up)[i+1]/6.0*(*vp)[i] -((*up)[i+1]+2.0*(*up)[i])/6.0*(*vp)[i+1]; } } (*ajvp)[ 0] -= u0/6.0*(*vp)[0]; (*ajvp)[nx_-1] += u1/6.0*(*vp)[nx_-1]; } void applyAdjointJacobian_2(ROL::Vector &ajv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { auto ajvp = getVector(ajv); auto vp = getVector(v); auto up = getVector(u); auto zp = getVector(z); for (int i=0; i &iajv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { auto iajvp = getVector(iajv); auto vp = getVector(v); auto up = getVector(u); auto zp = getVector(z); // Get PDE Jacobian std::vector d(nx_,0.0); std::vector du(nx_-1,0.0); std::vector dl(nx_-1,0.0); compute_pde_jacobian(dl,d,du,*up); // Solve solve adjoint system at current time step linear_solve(*iajvp,dl,d,du,*vp,true); } void applyAdjointHessian_11(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { auto ahwvp = getVector(ahwv); auto wp = getVector(w); auto vp = getVector(v); auto up = getVector(u); auto zp = getVector(z); for (int i=0; i0) { (*ahwvp)[i] += ((*wp)[i-1]*((*vp)[i-1]+2.0*(*vp)[i]) - (*wp)[i]*(*vp)[i-1])/6.0; } } } void applyAdjointHessian_12(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { ahwv.zero(); } void applyAdjointHessian_21(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { ahwv.zero(); } void applyAdjointHessian_22(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { ahwv.zero(); } }; template class Objective_BurgersControl : public ROL::Objective_SimOpt { private: Real alpha_; // Penalty Parameter int nx_; // Number of interior nodes Real dx_; // Mesh spacing (i.e. 1/(nx+1)) /***************************************************************/ /********** BEGIN PRIVATE MEMBER FUNCTION DECLARATION **********/ /***************************************************************/ Real evaluate_target(Real x) { Real val = 0.0; int example = 2; switch (example) { case 1: val = ((x<0.5) ? 1.0 : 0.0); break; case 2: val = 1.0; break; case 3: val = std::abs(std::sin(8.0*M_PI*x)); break; case 4: val = std::exp(-0.5*(x-0.5)*(x-0.5)); break; } return val; } Real dot(const std::vector &x, const std::vector &y) { Real ip = 0.0; Real c = (((int)x.size()==nx_) ? 4.0 : 2.0); for (unsigned i=0; i &Mu, const std::vector &u ) { Mu.resize(u.size(),0.0); Real c = (((int)u.size()==nx_) ? 4.0 : 2.0); for (unsigned i=0; i> getVector( ROL::Vector& x ) { return dynamic_cast&>(x).getVector(); } ROL::Ptr> getVector( const ROL::Vector& x ) { return dynamic_cast&>(x).getVector(); } /*************************************************************/ /********** END PRIVATE MEMBER FUNCTION DECLARATION **********/ /*************************************************************/ public: Objective_BurgersControl(Real alpha = 1.e-4, int nx = 128) : alpha_(alpha), nx_(nx) { dx_ = 1.0/((Real)nx+1.0); } using ROL::Objective_SimOpt::value; Real value( const ROL::Vector &u, const ROL::Vector &z, Real &tol ) { auto up = getVector(u); auto zp = getVector(z); // COMPUTE RESIDUAL Real res1 = 0.0, res2 = 0.0, res3 = 0.0; Real valu = 0.0, valz = dot(*zp,*zp); for (int i=0; i &g, const ROL::Vector &u, const ROL::Vector &z, Real &tol ) { auto gup = getVector(g); auto up = getVector(u); auto zp = getVector(z); // COMPUTE GRADIENT WRT U std::vector diff(nx_,0.0); for (int i=0; i &g, const ROL::Vector &u, const ROL::Vector &z, Real &tol ) { auto gzp = getVector(g); auto up = getVector(u); auto zp = getVector(z); // COMPUTE GRADIENT WRT Z for (int i=0; i &hv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol ) { auto hvup = getVector(hv); auto vup = getVector(v); auto up = getVector(u); auto zp = getVector(z); // COMPUTE GRADIENT WRT U apply_mass(*hvup,*vup); } void hessVec_12( ROL::Vector &hv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol ) { hv.zero(); } void hessVec_21( ROL::Vector &hv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol ) { hv.zero(); } void hessVec_22( ROL::Vector &hv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol ) { auto hvzp = getVector(hv); auto vzp = getVector(v); // COMPUTE GRADIENT WRT Z for (int i=0; i