// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /*! \file constraint.hpp \brief Defines the SimOpt constraint for the 'poisson' example. */ #ifndef ROL_PDEOPT_BILINEARPDECONSTRAINT_H #define ROL_PDEOPT_BILINEARPDECONSTRAINT_H #include "ROL_Constraint_SimOpt.hpp" #include "../../../TOOLS/solver.hpp" #include "../../../TOOLS/pdevector.hpp" #include "femdata.hpp" //// Global Timers. #ifdef ROL_TIMERS namespace ROL { namespace PDEOPT { ROL::Ptr BilinearPDEConstraintSolverConstruct_Jacobian1 = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Solver Construction Time for Jacobian1"); ROL::Ptr BilinearPDEConstraintSolverConstruct_AdjointJacobian1 = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Solver Construction Time for Adjoint Jacobian1"); ROL::Ptr BilinearPDEConstraintSolverSolve_Jacobian1 = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Solver Solution Time for Jacobian1"); ROL::Ptr BilinearPDEConstraintSolverSolve_AdjointJacobian1 = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Solver Solution Time for Adjoint Jacobian1"); ROL::Ptr BilinearPDEConstraintApplyJacobian1 = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Apply Jacobian1"); ROL::Ptr BilinearPDEConstraintApplyJacobian2 = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Apply Jacobian2"); ROL::Ptr BilinearPDEConstraintApplyAdjointJacobian1 = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Apply Adjoint Jacobian1"); ROL::Ptr BilinearPDEConstraintApplyAdjointJacobian2 = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Apply Adjoint Jacobian2"); ROL::Ptr BilinearPDEConstraintApplyHessian = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Apply Hessian"); ROL::Ptr BilinearPDEConstraintApplyAdjontHessian = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Apply Adjoint Hessian"); } } #endif template class Bilinear_PDE_Constraint : public ROL::Constraint_SimOpt { private: const ROL::Ptr> fem_; ROL::Ptr> solver_; bool setSolver_, assembleJ1_, buildState_, buildAdjoint_; ROL::Ptr> cvec_; ROL::Ptr> matJ1_; ROL::Ptr> S0u_; std::vector>>>> Su_; std::vector>>>> Sl_; unsigned M_, N_, T_; void setSolver(void) { ROL::Ptr> tmp = ROL::staticPtrCast>(matJ1_); solver_->setA(tmp); setSolver_ = false; } void solveForward(ROL::Ptr> &x, const ROL::Ptr> &b) { if (setSolver_) { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintSolverConstruct_Jacobian1); #endif setSolver(); } #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintSolverSolve_Jacobian1); #endif solver_->solve(x,b,false); } void solveAdjoint(ROL::Ptr> &x, const ROL::Ptr> &b) { if (setSolver_) { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintSolverConstruct_AdjointJacobian1); #endif setSolver(); } #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintSolverSolve_AdjointJacobian1); #endif solver_->solve(x,b,true); } void buildSu(const ROL::Ptr> &u) { if (buildState_) { fem_->applyS0(*S0u_,*u); for (unsigned i = 0; i < M_; ++i) { for (unsigned j = 0; j < N_; ++j) { for (unsigned k = 0; k < T_; ++k) { fem_->applyS(i,j,k,*Su_[i][j][k],*u); } } } buildState_ = false; } } void applySuz(const ROL::Ptr> &Juz, const ROL::Ptr> &u, const ROL::Ptr> &z) { const Real zero(0), one(1); buildSu(u); Juz->update(one,*S0u_,zero); for (unsigned i = 0; i < M_; ++i) { for (unsigned j = 0; j < N_; ++j) { for (unsigned k = 0; k < T_; ++k) { Juz->update((*z)[i+M_*(j+N_*k)],*Su_[i][j][k],one); } } } } void buildJacobian1(const ROL::Ptr> &z) { if (assembleJ1_) { const Real zero(0), one(1); ROL::Ptr list = ROL::makePtr(); matJ1_ = matJ1_->add(one,*fem_->getS0(),zero, fem_->getS0()->getDomainMap(),fem_->getS0()->getRangeMap(),list); for (unsigned i = 0; i < M_; ++i) { for (unsigned j = 0; j < N_; ++j) { for (unsigned k = 0; k < T_; ++k) { matJ1_ = matJ1_->add((*z)[i+M_*(j+N_*k)],*fem_->getS(i,j,k),one, fem_->getS(i,j,k)->getDomainMap(),fem_->getS(i,j,k)->getRangeMap(),list); } } } assembleJ1_ = false; setSolver_ = true; } } void applyJacobian1(const ROL::Ptr> &Jv, const ROL::Ptr> &v) { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BilinearPDEConstraintApplyJacobian1); #endif matJ1_->apply(*v,*Jv); } void applyAdjointJacobian1(const ROL::Ptr> &Jv, const ROL::Ptr> &v) { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintApplyAdjointJacobian1); #endif matJ1_->apply(*v,*Jv,Teuchos::TRANS); } void applyJacobian2(const ROL::Ptr> &Jv, const ROL::Ptr> &u, const ROL::Ptr> &v) { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintApplyJacobian2); #endif buildSu(u); Jv->scale(static_cast(0)); const Real one(1); for (unsigned i = 0; i < M_; ++i) { for (unsigned j = 0; j < N_; ++j) { for (unsigned k = 0; k < T_; ++k) { Jv->update((*v)[i+M_*(j+N_*k)],*Su_[i][j][k],one); } } } } void applyAdjointJacobian2(const ROL::Ptr> &Jv, const ROL::Ptr> &v, const ROL::Ptr> &u) { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintApplyAdjointJacobian2); #endif buildSu(u); Teuchos::Array val(1,0); for (unsigned i = 0; i < M_; ++i) { for (unsigned j = 0; j < N_; ++j) { for (unsigned k = 0; k < T_; ++k) { Su_[i][j][k]->dot(*v, val.view(0,1)); (*Jv)[i+M_*(j+N_*k)] = val[0]; } } } } void buildSl(const ROL::Ptr> &l) { if (buildAdjoint_) { for (unsigned i = 0; i < M_; ++i) { for (unsigned j = 0; j < N_; ++j) { for (unsigned k = 0; k < T_; ++k) { fem_->applyS(i,j,k,*Sl_[i][j][k],*l,true); } } } buildAdjoint_ = false; } } void applyHessian(const ROL::Ptr> &H, const ROL::Ptr> &v, const ROL::Ptr> &l) { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintApplyHessian); #endif buildSl(l); H->scale(static_cast(0)); const Real one(1); for (unsigned i = 0; i < M_; ++i) { for (unsigned j = 0; j < N_; ++j) { for (unsigned k = 0; k < T_; ++k) { H->update((*v)[i+M_*(j+N_*k)],*Sl_[i][j][k],one); } } } } void applyAdjointHessian(const ROL::Ptr> &H, const ROL::Ptr> &v, const ROL::Ptr> &l) { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintApplyAdjointHessian); #endif buildSl(l); Teuchos::Array val(1,0); for (unsigned i = 0; i < M_; ++i) { for (unsigned j = 0; j < N_; ++j) { for (unsigned k = 0; k < T_; ++k) { Sl_[i][j][k]->dot(*v, val.view(0,1)); (*H)[i+M_*(j+N_*k)] = val[0]; } } } } public: Bilinear_PDE_Constraint(const ROL::Ptr> &fem, Teuchos::ParameterList &list, std::ostream &outStream = std::cout) : ROL::Constraint_SimOpt(), fem_(fem), setSolver_(true), assembleJ1_(true), buildState_(true), buildAdjoint_(true) { // Construct solver. solver_ = ROL::makePtr>(list.sublist("Solver")); // Initialize zero vectors. cvec_ = fem_->getAssembler()->createResidualVector(); std::vector ym = ROL::getArrayFromStringParameter(list.sublist("Problem"), "Young's Modulus"); M_ = list.sublist("Problem").get("Number of Horizontal Cells",10); N_ = list.sublist("Problem").get("Number of Vertical Cells",20); T_ = ym.size(); matJ1_ = ROL::makePtr>(fem_->getS0()->getCrsGraph()); S0u_ = fem_->getAssembler()->createResidualVector(); Su_.resize(M_); Sl_.resize(M_); for (unsigned i = 0; i < M_; ++i) { Su_[i].resize(N_); Sl_[i].resize(N_); for (unsigned j = 0; j < N_; ++j) { Su_[i][j].resize(T_); Sl_[i][j].resize(T_); for (unsigned k = 0; k < T_; ++k) { Su_[i][j][k] = fem_->getAssembler()->createResidualVector(); Sl_[i][j][k] = fem_->getAssembler()->createResidualVector(); } } } } const ROL::Ptr> getAssembler(void) const { return fem_->getAssembler(); } const ROL::Ptr> getPDE(void) const { return fem_->getPDE(); } using ROL::Constraint_SimOpt::update_1; void update_1(const ROL::Vector &u, bool flag = true, int iter = -1) { buildState_ = true; buildAdjoint_ = true; } using ROL::Constraint_SimOpt::update_2; void update_2(const ROL::Vector &z, bool flag = true, int iter = -1) { assembleJ1_ = true; } using ROL::Constraint_SimOpt::update; void update(const ROL::Vector &u, const ROL::Vector &z, bool flag = true, int iter = -1) { //update_1(u,flag,iter); //update_2(z,flag,iter); } using ROL::Constraint_SimOpt::value; void value(ROL::Vector &c, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { ROL::Ptr> cf = getField(c); ROL::Ptr> uf = getConstField(u); ROL::Ptr> zf = getConstField(z); ROL::Ptr> zp = getConstParameter(z); const Real one(1); cvec_->putScalar(static_cast(0)); c.zero(); cf->scale(one,*fem_->getForce()); applySuz(cvec_,uf,zp); cf->update(one,*cvec_,one); } void solve(ROL::Vector &c, ROL::Vector &u, const ROL::Vector &z, Real &tol) { ROL::Ptr> cf = getField(c); ROL::Ptr> uf = getField(u); ROL::Ptr> zf = getConstField(z); ROL::Ptr> zp = getConstParameter(z); c.zero(); cvec_->putScalar(static_cast(0)); u.zero(); const Real one(1); cf->scale(one,*fem_->getForce()); buildJacobian1(zp); solveForward(uf,cf); uf->scale(-one); applySuz(cvec_,uf,zp); cf->update(one,*cvec_,one); } void applyJacobian_1(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { jv.zero(); ROL::Ptr> jvf = getField(jv); ROL::Ptr> vf = getConstField(v); ROL::Ptr> zf = getConstField(z); ROL::Ptr> zp = getConstParameter(z); buildJacobian1(zp); applyJacobian1(jvf,vf); } void applyJacobian_2(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { jv.zero(); ROL::Ptr> jvf = getField(jv); ROL::Ptr> uf = getConstField(u); ROL::Ptr> vp = getConstParameter(v); applyJacobian2(jvf,uf,vp); } void applyAdjointJacobian_1(ROL::Vector &ajv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { ajv.zero(); ROL::Ptr> ajvf = getField(ajv); ROL::Ptr> vf = getConstField(v); ROL::Ptr> zf = getConstField(z); ROL::Ptr> zp = getConstParameter(z); buildJacobian1(zp); applyAdjointJacobian1(ajvf,vf); } void applyAdjointJacobian_2(ROL::Vector &ajv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { ajv.zero(); ROL::Ptr> ajvp = getParameter(ajv); ROL::Ptr> uf = getConstField(u); ROL::Ptr> vf = getConstField(v); applyAdjointJacobian2(ajvp,vf,uf); } void applyInverseJacobian_1(ROL::Vector &ijv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { ijv.zero(); ROL::Ptr> ijvf = getField(ijv); ROL::Ptr> vf = getConstField(v); ROL::Ptr> zp = getConstParameter(z); buildJacobian1(zp); solveForward(ijvf,vf); } void applyInverseAdjointJacobian_1(ROL::Vector &iajv, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { iajv.zero(); ROL::Ptr> iajvf = getField(iajv); ROL::Ptr> vf = getConstField(v); ROL::Ptr> zp = getConstParameter(z); buildJacobian1(zp); solveAdjoint(iajvf,vf); } void applyAdjointHessian_11(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_12(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &u, const ROL::Vector &z, Real &tol) { ahwv.zero(); ROL::Ptr> ahwvp = getParameter(ahwv); ROL::Ptr> wf = getConstField(w); ROL::Ptr> vf = getConstField(v); applyAdjointHessian(ahwvp,vf,wf); } 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(); ROL::Ptr> ahwvf = getField(ahwv); ROL::Ptr> wf = getConstField(w); ROL::Ptr> vp = getConstParameter(v); applyHessian(ahwvf,vp,wf); } 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(); } /***************************************************************************/ /* Output routines. */ /***************************************************************************/ void printMeshData(std::ostream &outStream) const { fem_->printMeshData(outStream); } void outputTpetraData() const { Tpetra::MatrixMarket::Writer< Tpetra::CrsMatrix<>> matWriter; if (matJ1_ != ROL::nullPtr) { matWriter.writeSparseFile("jacobian1.txt", ROL::staticPtrCast>(matJ1_)); } else { std::ofstream emptyfile; emptyfile.open("jacobian1.txt"); emptyfile.close(); } if (fem_->getForce() != ROL::nullPtr) { matWriter.writeDenseFile("residual.txt", fem_->getForce()); } else { std::ofstream emptyfile; emptyfile.open("residual.txt"); emptyfile.close(); } } void outputTpetraVector(const ROL::Ptr> &vec, const std::string &filename) const { fem_->outputTpetraVector(vec,filename); } /***************************************************************************/ /* End of output routines. */ /***************************************************************************/ private: // Vector accessor functions ROL::Ptr> getConstField(const ROL::Vector &x) const { ROL::Ptr> xp; try { xp = dynamic_cast&>(x).getVector(); } catch (std::exception &e) { try { ROL::Ptr> xvec = dynamic_cast&>(x).getField(); if (xvec == ROL::nullPtr) { xp = ROL::nullPtr; } else { xp = xvec->getVector(); } } catch (std::exception &ee) { xp = ROL::nullPtr; } } return xp; } ROL::Ptr> getField(ROL::Vector &x) const { ROL::Ptr> xp; try { xp = dynamic_cast&>(x).getVector(); } catch (std::exception &e) { try { ROL::Ptr> xvec = dynamic_cast&>(x).getField(); if ( xvec == ROL::nullPtr ) { xp = ROL::nullPtr; } else { xp = xvec->getVector(); } } catch (std::exception &ee) { xp = ROL::nullPtr; } } return xp; } ROL::Ptr> getConstParameter(const ROL::Vector &x) const { ROL::Ptr> xp; try { xp = dynamic_cast&>(x).getVector(); } catch (std::exception &e) { try { ROL::Ptr> xvec = dynamic_cast&>(x).getParameter(); if ( xvec == ROL::nullPtr ) { xp = ROL::nullPtr; } else { xp = xvec->getVector(); } } catch (std::exception &ee) { xp = ROL::nullPtr; } } return xp; } ROL::Ptr> getParameter(ROL::Vector &x) const { ROL::Ptr> xp; try { xp = dynamic_cast&>(x).getVector(); } catch (std::exception &e) { try { ROL::Ptr> xvec = dynamic_cast&>(x).getParameter(); if ( xvec == ROL::nullPtr ) { xp = ROL::nullPtr; } else { xp = xvec->getVector(); } } catch (std::exception &ee) { xp = ROL::nullPtr; } } return xp; } }; #endif