// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /*! \file example_06.cpp \brief Shows how to solve a steady Burgers' optimal control problem using full-space methods. */ #include "ROL_Types.hpp" #include "ROL_Vector.hpp" #include "ROL_BoundConstraint.hpp" #include "ROL_Constraint_SimOpt.hpp" #include "ROL_TeuchosBatchManager.hpp" #include "Teuchos_LAPACK.hpp" template class L2VectorPrimal; template class L2VectorDual; template class H1VectorPrimal; template class H1VectorDual; template class BurgersFEM { private: int nx_; Real dx_; Real nu_; Real nl_; Real u0_; Real u1_; Real f_; Real cH1_; Real cL2_; private: void update(std::vector &u, const std::vector &s, const Real alpha=1.0) const { for (unsigned i=0; i &out, const Real a, const std::vector &x, const std::vector &y) const { for (unsigned i=0; i < x.size(); i++) { out[i] = a*x[i] + y[i]; } } void scale(std::vector &u, const Real alpha=0.0) const { for (unsigned i=0; i &u, std::vector &dl, std::vector &d, std::vector &du, const std::vector &r, const bool transpose = false) const { if ( r.size() == 1 ) { u.resize(1,r[0]/d[0]); } else if ( r.size() == 2 ) { u.resize(2,0.0); Real det = d[0]*d[1] - dl[0]*du[0]; u[0] = (d[1]*r[0] - du[0]*r[1])/det; u[1] = (d[0]*r[1] - dl[0]*r[0])/det; } else { u.assign(r.begin(),r.end()); // Perform LDL factorization Teuchos::LAPACK lp; std::vector du2(r.size()-2,0.0); std::vector ipiv(r.size(),0); int info; int dim = r.size(); int ldb = r.size(); int nhrs = 1; lp.GTTRF(dim,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&info); char trans = 'N'; if ( transpose ) { trans = 'T'; } lp.GTTRS(trans,dim,nhrs,&dl[0],&d[0],&du[0],&du2[0],&ipiv[0],&u[0],ldb,&info); } } public: BurgersFEM(int nx = 128, Real nl = 1.0, Real cH1 = 1.0, Real cL2 = 1.0) : nx_(nx), dx_(1.0/((Real)nx+1.0)), nl_(nl), cH1_(cH1), cL2_(cL2) {} void set_problem_data(const Real nu, const Real f, const Real u0, const Real u1) { nu_ = std::pow(10.0,nu-2.0); f_ = 0.01*f; u0_ = 1.0+0.001*u0; u1_ = 0.001*u1; } int num_dof(void) const { return nx_; } Real mesh_spacing(void) const { return dx_; } /***************************************************************************/ /*********************** L2 INNER PRODUCT FUNCTIONS ************************/ /***************************************************************************/ // Compute L2 inner product Real compute_L2_dot(const std::vector &x, const std::vector &y) const { Real ip = 0.0; Real c = (((int)x.size()==nx_) ? 4.0 : 2.0); for (unsigned i=0; i &r) const { return std::sqrt(compute_L2_dot(r,r)); } // Apply L2 Reisz operator void apply_mass(std::vector &Mu, const std::vector &u ) const { Mu.resize(u.size(),0.0); Real c = (((int)u.size()==nx_) ? 4.0 : 2.0); for (unsigned i=0; i &Mu, const std::vector &u) const { unsigned nx = u.size(); // Build mass matrix std::vector dl(nx-1,dx_/6.0); std::vector d(nx,2.0*dx_/3.0); std::vector du(nx-1,dx_/6.0); if ( (int)nx != nx_ ) { d[ 0] = dx_/3.0; d[nx-1] = dx_/3.0; } linear_solve(Mu,dl,d,du,u); } void test_inverse_mass(std::ostream &outStream = std::cout) { // State Mass Matrix std::vector u(nx_,0.0), Mu(nx_,0.0), iMMu(nx_,0.0), diff(nx_,0.0); for (int i = 0; i < nx_; i++) { u[i] = 2.0*(Real)rand()/(Real)RAND_MAX - 1.0; } apply_mass(Mu,u); apply_inverse_mass(iMMu,Mu); axpy(diff,-1.0,iMMu,u); Real error = compute_L2_norm(diff); Real normu = compute_L2_norm(u); outStream << "\nTest Inverse State Mass Matrix\n"; outStream << " ||u - inv(M)Mu|| = " << error << "\n"; outStream << " ||u|| = " << normu << "\n"; outStream << " Relative Error = " << error/normu << "\n"; outStream << "\n"; // Control Mass Matrix u.resize(nx_+2,0.0); Mu.resize(nx_+2,0.0); iMMu.resize(nx_+2,0.0); diff.resize(nx_+2,0.0); for (int i = 0; i < nx_+2; i++) { u[i] = 2.0*(Real)rand()/(Real)RAND_MAX - 1.0; } apply_mass(Mu,u); apply_inverse_mass(iMMu,Mu); axpy(diff,-1.0,iMMu,u); error = compute_L2_norm(diff); normu = compute_L2_norm(u); outStream << "\nTest Inverse Control Mass Matrix\n"; outStream << " ||z - inv(M)Mz|| = " << error << "\n"; outStream << " ||z|| = " << normu << "\n"; outStream << " Relative Error = " << error/normu << "\n"; outStream << "\n"; } /***************************************************************************/ /*********************** H1 INNER PRODUCT FUNCTIONS ************************/ /***************************************************************************/ // Compute H1 inner product Real compute_H1_dot(const std::vector &x, const std::vector &y) const { Real ip = 0.0; for (int i=0; i &r) const { return std::sqrt(compute_H1_dot(r,r)); } // Apply H2 Reisz operator void apply_H1(std::vector &Mu, const std::vector &u ) const { Mu.resize(nx_,0.0); for (int i=0; i &Mu, const std::vector &u) const { // Build mass matrix std::vector dl(nx_-1,cL2_*dx_/6.0 - cH1_/dx_); std::vector d(nx_,2.0*(cL2_*dx_/3.0 + cH1_/dx_)); std::vector du(nx_-1,cL2_*dx_/6.0 - cH1_/dx_); linear_solve(Mu,dl,d,du,u); } void test_inverse_H1(std::ostream &outStream = std::cout) { std::vector u(nx_,0.0), Mu(nx_,0.0), iMMu(nx_,0.0), diff(nx_,0.0); for (int i = 0; i < nx_; i++) { u[i] = 2.0*(Real)rand()/(Real)RAND_MAX - 1.0; } apply_H1(Mu,u); apply_inverse_H1(iMMu,Mu); axpy(diff,-1.0,iMMu,u); Real error = compute_H1_norm(diff); Real normu = compute_H1_norm(u); outStream << "\nTest Inverse State H1 Matrix\n"; outStream << " ||u - inv(M)Mu|| = " << error << "\n"; outStream << " ||u|| = " << normu << "\n"; outStream << " Relative Error = " << error/normu << "\n"; outStream << "\n"; } /***************************************************************************/ /*********************** PDE RESIDUAL AND SOLVE ****************************/ /***************************************************************************/ // Compute current PDE residual void compute_residual(std::vector &r, const std::vector &u, const std::vector &z) const { r.clear(); r.resize(nx_,0.0); for (int i=0; i0) { r[i] -= nl_*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] -= nl_*(u0_*u[ 0]/6.0 + u0_*u0_/6.0) + nu_*u0_/dx_; r[nx_-1] += nl_*(u1_*u[nx_-1]/6.0 + u1_*u1_/6.0) - nu_*u1_/dx_; } /***************************************************************************/ /*********************** PDE JACOBIAN FUNCTIONS ****************************/ /***************************************************************************/ // Build PDE Jacobian trigiagonal matrix void compute_pde_jacobian(std::vector &dl, // Lower diagonal std::vector &d, // Diagonal std::vector &du, // Upper diagonal const std::vector &u) const { // State variable // 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] -= nl_*u[i-1]/6.0; du[i-1] += nl_*(u[i-1]+2.0*u[i])/6.0; } } // Contribution from Dirichlet boundary conditions d[ 0] -= nl_*u0_/6.0; d[nx_-1] += nl_*u1_/6.0; } // Apply PDE Jacobian to a vector void apply_pde_jacobian(std::vector &jv, const std::vector &v, const std::vector &u, const std::vector &z) const { // Fill jv for (int i = 0; i < nx_; i++) { jv[i] = nu_/dx_*2.0*v[i]; if ( i > 0 ) { jv[i] += -nu_/dx_*v[i-1]-nl_*(u[i-1]/6.0*v[i]+(u[i]+2.0*u[i-1])/6.0*v[i-1]); } if ( i < nx_-1 ) { jv[i] += -nu_/dx_*v[i+1]+nl_*(u[i+1]/6.0*v[i]+(u[i]+2.0*u[i+1])/6.0*v[i+1]); } } jv[ 0] -= nl_*u0_/6.0*v[0]; jv[nx_-1] += nl_*u1_/6.0*v[nx_-1]; } // Apply inverse PDE Jacobian to a vector void apply_inverse_pde_jacobian(std::vector &ijv, const std::vector &v, const std::vector &u, const std::vector &z) const { // 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,u); // Solve solve state sensitivity system at current time step linear_solve(ijv,dl,d,du,v); } // Apply adjoint PDE Jacobian to a vector void apply_adjoint_pde_jacobian(std::vector &ajv, const std::vector &v, const std::vector &u, const std::vector &z) const { // Fill jvp for (int i = 0; i < nx_; i++) { ajv[i] = nu_/dx_*2.0*v[i]; if ( i > 0 ) { ajv[i] += -nu_/dx_*v[i-1]-nl_*(u[i-1]/6.0*v[i] -(u[i-1]+2.0*u[i])/6.0*v[i-1]); } if ( i < nx_-1 ) { ajv[i] += -nu_/dx_*v[i+1]+nl_*(u[i+1]/6.0*v[i] -(u[i+1]+2.0*u[i])/6.0*v[i+1]); } } ajv[ 0] -= nl_*u0_/6.0*v[0]; ajv[nx_-1] += nl_*u1_/6.0*v[nx_-1]; } // Apply inverse adjoint PDE Jacobian to a vector void apply_inverse_adjoint_pde_jacobian(std::vector &iajv, const std::vector &v, const std::vector &u, const std::vector &z) const { // 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,u); // Solve solve adjoint system at current time step linear_solve(iajv,dl,d,du,v,true); } /***************************************************************************/ /*********************** CONTROL JACOBIAN FUNCTIONS ************************/ /***************************************************************************/ // Apply control Jacobian to a vector void apply_control_jacobian(std::vector &jv, const std::vector &v, const std::vector &u, const std::vector &z) const { for (int i=0; i &jv, const std::vector &v, const std::vector &u, const std::vector &z) const { for (int i=0; i &ahwv, const std::vector &w, const std::vector &v, const std::vector &u, const std::vector &z) const { for (int i=0; i0) { ahwv[i] += (w[i-1]*(v[i-1]+2.0*v[i]) - w[i]*v[i-1])/6.0; } } //ahwv.assign(u.size(),0.0); } void apply_adjoint_pde_control_hessian(std::vector &ahwv, const std::vector &w, const std::vector &v, const std::vector &u, const std::vector &z) { ahwv.assign(u.size(),0.0); } void apply_adjoint_control_pde_hessian(std::vector &ahwv, const std::vector &w, const std::vector &v, const std::vector &u, const std::vector &z) { ahwv.assign(z.size(),0.0); } void apply_adjoint_control_hessian(std::vector &ahwv, const std::vector &w, const std::vector &v, const std::vector &u, const std::vector &z) { ahwv.assign(z.size(),0.0); } }; template class L2VectorPrimal : public ROL::Vector { private: ROL::Ptr > vec_; ROL::Ptr > fem_; mutable ROL::Ptr > dual_vec_; public: L2VectorPrimal(const ROL::Ptr > & vec, const ROL::Ptr > &fem) : vec_(vec), fem_(fem), dual_vec_(ROL::nullPtr) {} void set( const ROL::Vector &x ) { const L2VectorPrimal &ex = dynamic_cast(x); const std::vector& xval = *ex.getVector(); std::copy(xval.begin(),xval.end(),vec_->begin()); } void plus( const ROL::Vector &x ) { const L2VectorPrimal &ex = dynamic_cast(x); const std::vector& xval = *ex.getVector(); unsigned dimension = vec_->size(); for (unsigned i=0; isize(); for (unsigned i=0; i &x ) const { const L2VectorPrimal & ex = dynamic_cast(x); const std::vector& xval = *ex.getVector(); return fem_->compute_L2_dot(xval,*vec_); } Real norm() const { Real val = 0; val = std::sqrt( dot(*this) ); return val; } ROL::Ptr > clone() const { return ROL::makePtr( ROL::makePtr>(vec_->size(),0.0),fem_); } ROL::Ptr > getVector() const { return vec_; } ROL::Ptr > getVector() { return vec_; } ROL::Ptr > basis( const int i ) const { ROL::Ptr e = ROL::makePtr( ROL::makePtr>(vec_->size(),0.0),fem_); (*e->getVector())[i] = 1.0; return e; } int dimension() const { return vec_->size(); } const ROL::Vector& dual() const { dual_vec_ = ROL::makePtr>( ROL::makePtr>(*vec_),fem_); fem_->apply_mass(*(ROL::constPtrCast >(dual_vec_->getVector())),*vec_); return *dual_vec_; } }; template class L2VectorDual : public ROL::Vector { private: ROL::Ptr > vec_; ROL::Ptr > fem_; mutable ROL::Ptr > dual_vec_; public: L2VectorDual(const ROL::Ptr > & vec, const ROL::Ptr > &fem) : vec_(vec), fem_(fem), dual_vec_(ROL::nullPtr) {} void set( const ROL::Vector &x ) { const L2VectorDual &ex = dynamic_cast(x); const std::vector& xval = *ex.getVector(); std::copy(xval.begin(),xval.end(),vec_->begin()); } void plus( const ROL::Vector &x ) { const L2VectorDual &ex = dynamic_cast(x); const std::vector& xval = *ex.getVector(); unsigned dimension = vec_->size(); for (unsigned i=0; isize(); for (unsigned i=0; i &x ) const { const L2VectorDual & ex = dynamic_cast(x); const std::vector& xval = *ex.getVector(); unsigned dimension = vec_->size(); std::vector Mx(dimension,0.0); fem_->apply_inverse_mass(Mx,xval); Real val = 0.0; for (unsigned i = 0; i < dimension; i++) { val += Mx[i]*(*vec_)[i]; } return val; } Real norm() const { Real val = 0; val = std::sqrt( dot(*this) ); return val; } ROL::Ptr > clone() const { return ROL::makePtr( ROL::makePtr>(vec_->size(),0.0),fem_); } ROL::Ptr > getVector() const { return vec_; } ROL::Ptr > getVector() { return vec_; } ROL::Ptr > basis( const int i ) const { ROL::Ptr e = ROL::makePtr( ROL::makePtr>(vec_->size(),0.0),fem_); (*e->getVector())[i] = 1.0; return e; } int dimension() const { return vec_->size(); } const ROL::Vector& dual() const { dual_vec_ = ROL::makePtr>( ROL::makePtr>(*vec_),fem_); fem_->apply_inverse_mass(*(ROL::constPtrCast >(dual_vec_->getVector())),*vec_); return *dual_vec_; } }; template class H1VectorPrimal : public ROL::Vector { private: ROL::Ptr > vec_; ROL::Ptr > fem_; mutable ROL::Ptr > dual_vec_; public: H1VectorPrimal(const ROL::Ptr > & vec, const ROL::Ptr > &fem) : vec_(vec), fem_(fem), dual_vec_(ROL::nullPtr) {} void set( const ROL::Vector &x ) { const H1VectorPrimal &ex = dynamic_cast(x); const std::vector& xval = *ex.getVector(); std::copy(xval.begin(),xval.end(),vec_->begin()); } void plus( const ROL::Vector &x ) { const H1VectorPrimal &ex = dynamic_cast(x); const std::vector& xval = *ex.getVector(); unsigned dimension = vec_->size(); for (unsigned i=0; isize(); for (unsigned i=0; i &x ) const { const H1VectorPrimal & ex = dynamic_cast(x); const std::vector& xval = *ex.getVector(); return fem_->compute_H1_dot(xval,*vec_); } Real norm() const { Real val = 0; val = std::sqrt( dot(*this) ); return val; } ROL::Ptr > clone() const { return ROL::makePtr( ROL::makePtr>(vec_->size(),0.0),fem_); } ROL::Ptr > getVector() const { return vec_; } ROL::Ptr > getVector() { return vec_; } ROL::Ptr > basis( const int i ) const { ROL::Ptr e = ROL::makePtr( ROL::makePtr>(vec_->size(),0.0),fem_); (*e->getVector())[i] = 1.0; return e; } int dimension() const { return vec_->size(); } const ROL::Vector& dual() const { dual_vec_ = ROL::makePtr>( ROL::makePtr>(*vec_),fem_); fem_->apply_H1(*(ROL::constPtrCast >(dual_vec_->getVector())),*vec_); return *dual_vec_; } }; template class H1VectorDual : public ROL::Vector { private: ROL::Ptr > vec_; ROL::Ptr > fem_; mutable ROL::Ptr > dual_vec_; public: H1VectorDual(const ROL::Ptr > & vec, const ROL::Ptr > &fem) : vec_(vec), fem_(fem), dual_vec_(ROL::nullPtr) {} void set( const ROL::Vector &x ) { const H1VectorDual &ex = dynamic_cast(x); const std::vector& xval = *ex.getVector(); std::copy(xval.begin(),xval.end(),vec_->begin()); } void plus( const ROL::Vector &x ) { const H1VectorDual &ex = dynamic_cast(x); const std::vector& xval = *ex.getVector(); unsigned dimension = vec_->size(); for (unsigned i=0; isize(); for (unsigned i=0; i &x ) const { const H1VectorDual & ex = dynamic_cast(x); const std::vector& xval = *ex.getVector(); unsigned dimension = vec_->size(); std::vector Mx(dimension,0.0); fem_->apply_inverse_H1(Mx,xval); Real val = 0.0; for (unsigned i = 0; i < dimension; i++) { val += Mx[i]*(*vec_)[i]; } return val; } Real norm() const { Real val = 0; val = std::sqrt( dot(*this) ); return val; } ROL::Ptr > clone() const { return ROL::makePtr( ROL::makePtr>(vec_->size(),0.0),fem_); } ROL::Ptr > getVector() const { return vec_; } ROL::Ptr > getVector() { return vec_; } ROL::Ptr > basis( const int i ) const { ROL::Ptr e = ROL::makePtr( ROL::makePtr>(vec_->size(),0.0),fem_); (*e->getVector())[i] = 1.0; return e; } int dimension() const { return vec_->size(); } const ROL::Vector& dual() const { dual_vec_ = ROL::makePtr>( ROL::makePtr>(*vec_),fem_); fem_->apply_inverse_H1(*(ROL::constPtrCast >(dual_vec_->getVector())),*vec_); return *dual_vec_; } }; template class Constraint_BurgersControl : public ROL::Constraint_SimOpt { private: typedef H1VectorPrimal PrimalStateVector; typedef H1VectorDual DualStateVector; typedef L2VectorPrimal PrimalControlVector; typedef L2VectorDual DualControlVector; typedef H1VectorDual PrimalConstraintVector; typedef H1VectorPrimal DualConstraintVector; ROL::Ptr > fem_; bool useHessian_; public: Constraint_BurgersControl(const ROL::Ptr > &fem, const bool useHessian = true) : fem_(fem), useHessian_(useHessian) {} void value(ROL::Vector &c, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { ROL::Ptr > cp = dynamic_cast(c).getVector(); ROL::Ptr > up = dynamic_cast(u).getVector(); ROL::Ptr > zp = dynamic_cast(z).getVector(); const std::vector param = ROL::Constraint_SimOpt::getParameter(); fem_->set_problem_data(param[0],param[1],param[2],param[3]); fem_->compute_residual(*cp,*up,*zp); } void applyJacobian_1(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { ROL::Ptr > jvp = dynamic_cast(jv).getVector(); ROL::Ptr > vp = dynamic_cast(v).getVector(); ROL::Ptr > up = dynamic_cast(u).getVector(); ROL::Ptr > zp = dynamic_cast(z).getVector(); const std::vector param = ROL::Constraint_SimOpt::getParameter(); fem_->set_problem_data(param[0],param[1],param[2],param[3]); fem_->apply_pde_jacobian(*jvp,*vp,*up,*zp); } void applyJacobian_2(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { ROL::Ptr > jvp = dynamic_cast(jv).getVector(); ROL::Ptr > vp = dynamic_cast(v).getVector(); ROL::Ptr > up = dynamic_cast(u).getVector(); ROL::Ptr > zp = dynamic_cast(z).getVector(); const std::vector param = ROL::Constraint_SimOpt::getParameter(); fem_->set_problem_data(param[0],param[1],param[2],param[3]); fem_->apply_control_jacobian(*jvp,*vp,*up,*zp); } void applyInverseJacobian_1(ROL::Vector &ijv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { ROL::Ptr > ijvp = dynamic_cast(ijv).getVector(); ROL::Ptr > vp = dynamic_cast(v).getVector(); ROL::Ptr > up = dynamic_cast(u).getVector(); ROL::Ptr > zp = dynamic_cast(z).getVector(); const std::vector param = ROL::Constraint_SimOpt::getParameter(); fem_->set_problem_data(param[0],param[1],param[2],param[3]); fem_->apply_inverse_pde_jacobian(*ijvp,*vp,*up,*zp); } void applyAdjointJacobian_1(ROL::Vector &ajv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { ROL::Ptr > jvp = dynamic_cast(ajv).getVector(); ROL::Ptr > vp = dynamic_cast(v).getVector(); ROL::Ptr > up = dynamic_cast(u).getVector(); ROL::Ptr > zp = dynamic_cast(z).getVector(); const std::vector param = ROL::Constraint_SimOpt::getParameter(); fem_->set_problem_data(param[0],param[1],param[2],param[3]); fem_->apply_adjoint_pde_jacobian(*jvp,*vp,*up,*zp); } void applyAdjointJacobian_2(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { ROL::Ptr > jvp = dynamic_cast(jv).getVector(); ROL::Ptr > vp = dynamic_cast(v).getVector(); ROL::Ptr > up = dynamic_cast(u).getVector(); ROL::Ptr > zp = dynamic_cast(z).getVector(); const std::vector param = ROL::Constraint_SimOpt::getParameter(); fem_->set_problem_data(param[0],param[1],param[2],param[3]); fem_->apply_adjoint_control_jacobian(*jvp,*vp,*up,*zp); } void applyInverseAdjointJacobian_1(ROL::Vector &iajv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { ROL::Ptr > iajvp = dynamic_cast(iajv).getVector(); ROL::Ptr > vp = dynamic_cast(v).getVector(); ROL::Ptr > up = dynamic_cast(u).getVector(); ROL::Ptr > zp = dynamic_cast(z).getVector(); const std::vector param = ROL::Constraint_SimOpt::getParameter(); fem_->set_problem_data(param[0],param[1],param[2],param[3]); fem_->apply_inverse_adjoint_pde_jacobian(*iajvp,*vp,*up,*zp); } void applyAdjointHessian_11(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { if ( useHessian_ ) { ROL::Ptr > ahwvp = dynamic_cast(ahwv).getVector(); ROL::Ptr > wp = dynamic_cast(w).getVector(); ROL::Ptr > vp = dynamic_cast(v).getVector(); ROL::Ptr > up = dynamic_cast(u).getVector(); ROL::Ptr > zp = dynamic_cast(z).getVector(); const std::vector param = ROL::Constraint_SimOpt::getParameter(); fem_->set_problem_data(param[0],param[1],param[2],param[3]); fem_->apply_adjoint_pde_hessian(*ahwvp,*wp,*vp,*up,*zp); } else { ahwv.zero(); } } void applyAdjointHessian_12(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { if ( useHessian_ ) { ROL::Ptr > ahwvp = dynamic_cast(ahwv).getVector(); ROL::Ptr > wp = dynamic_cast(w).getVector(); ROL::Ptr > vp = dynamic_cast(v).getVector(); ROL::Ptr > up = dynamic_cast(u).getVector(); ROL::Ptr > zp = dynamic_cast(z).getVector(); const std::vector param = ROL::Constraint_SimOpt::getParameter(); fem_->set_problem_data(param[0],param[1],param[2],param[3]); fem_->apply_adjoint_control_pde_hessian(*ahwvp,*wp,*vp,*up,*zp); } else { 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) { if ( useHessian_ ) { ROL::Ptr > ahwvp = dynamic_cast(ahwv).getVector(); ROL::Ptr > wp = dynamic_cast(w).getVector(); ROL::Ptr > vp = dynamic_cast(v).getVector(); ROL::Ptr > up = dynamic_cast(u).getVector(); ROL::Ptr > zp = dynamic_cast(z).getVector(); const std::vector param = ROL::Constraint_SimOpt::getParameter(); fem_->set_problem_data(param[0],param[1],param[2],param[3]); fem_->apply_adjoint_pde_control_hessian(*ahwvp,*wp,*vp,*up,*zp); } else { 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) { if ( useHessian_ ) { ROL::Ptr > ahwvp = dynamic_cast(ahwv).getVector(); ROL::Ptr > wp = dynamic_cast(w).getVector(); ROL::Ptr > vp = dynamic_cast(v).getVector(); ROL::Ptr > up = dynamic_cast(u).getVector(); ROL::Ptr > zp = dynamic_cast(z).getVector(); const std::vector param = ROL::Constraint_SimOpt::getParameter(); fem_->set_problem_data(param[0],param[1],param[2],param[3]); fem_->apply_adjoint_control_hessian(*ahwvp,*wp,*vp,*up,*zp); } else { ahwv.zero(); } } }; template class L2VectorBatchManager : public ROL::TeuchosBatchManager { private: void cast_vector(ROL::Ptr > &xvec, ROL::Vector &x) const { try { xvec = ROL::constPtrCast >( (dynamic_cast&>(x)).getVector()); } catch (std::exception &e) { xvec = ROL::constPtrCast >( (dynamic_cast&>(x)).getVector()); } } public: L2VectorBatchManager(const ROL::Ptr > &comm) : ROL::TeuchosBatchManager(comm) {} void sumAll(ROL::Vector &input, ROL::Vector &output) { ROL::Ptr > input_ptr; cast_vector(input_ptr,input); int dim_i = input_ptr->size(); ROL::Ptr > output_ptr; cast_vector(output_ptr,output); int dim_o = output_ptr->size(); if ( dim_i != dim_o ) { std::cout << "L2VectorBatchManager: DIMENSION MISMATCH ON RANK " << ROL::TeuchosBatchManager::batchID() << "\n"; } else { ROL::TeuchosBatchManager::sumAll(&(*input_ptr)[0],&(*output_ptr)[0],dim_i); } } };