// @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_LINDYNCONSTRAINT_H #define ROL_PDEOPT_LINDYNCONSTRAINT_H #include "ROL_DynamicConstraint.hpp" #include "assembler.hpp" #include "solver.hpp" #include "pdevector.hpp" // Do not instantiate the template in this translation unit. extern template class Assembler; //// Global Timers. #ifdef ROL_TIMERS namespace ROL { namespace PDEOPT { ROL::Ptr LinDynConstraintSolverConstruct_Jacobian_un = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: LinDyn Constraint Solver Construction Time for Jacobian un"); ROL::Ptr LinDynConstraintSolverSolve_Jacobian_un = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: LinDyn Constraint Solver Solution Time for Jacobian un"); ROL::Ptr LinDynConstraintSolverSolve_AdjointJacobian_un = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: LinDyn Constraint Solver Solution Time for Adjoint Jacobian un"); ROL::Ptr LinDynConstraintApplyJacobian_uo = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: LinDyn Constraint Apply Jacobian uo"); ROL::Ptr LinDynConstraintApplyJacobian_un = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: LinDyn Constraint Apply Jacobian un"); ROL::Ptr LinDynConstraintApplyJacobian_zf = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: LinDyn Constraint Apply Jacobian zf"); ROL::Ptr LinDynConstraintApplyJacobian_zp = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: LinDyn Constraint Apply Jacobian zp"); ROL::Ptr LinDynConstraintApplyAdjointJacobian_uo = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: LinDyn Constraint Apply Adjoint Jacobian uo"); ROL::Ptr LinDynConstraintApplyAdjointJacobian_un = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: LinDyn Constraint Apply Adjoint Jacobian un"); ROL::Ptr LinDynConstraintApplyAdjointJacobian_zf = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: LinDyn Constraint Apply Adjoint Jacobian zf"); ROL::Ptr LinDynConstraintApplyAdjointJacobian_zp = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: LinDyn Constraint Apply Adjoint Jacobian zp"); } } #endif template class LinDynConstraint : public ROL::DynamicConstraint { private: const ROL::Ptr> pde_; ROL::Ptr> solver_; ROL::Ptr> assembler_; ROL::Ptr> uvec_; mutable ROL::Ptr> zvec_; mutable ROL::Ptr> zpar_; mutable ROL::Ptr> matJuo_; mutable ROL::Ptr> matJun_; mutable ROL::Ptr> vecR_; mutable ROL::Ptr> matJzf_; mutable ROL::Ptr> vecJzp_; mutable ROL::Ptr> cvec_; const bool isLTI_; mutable bool isAssembled_; mutable bool initialize_; void initialize(const ROL::Vector &z) const { if (initialize_) { // Initialize control vectors ROL::Ptr> zf = getConstField(z); ROL::Ptr> zp = getConstParameter(z); if (zf != ROL::nullPtr) { zvec_ = assembler_->createControlVector(); zvec_->putScalar(static_cast(0)); } if (zp != ROL::nullPtr) { zpar_ = ROL::makePtr>(zp->size(),static_cast(0)); } initialize_ = false; } } void assemble(const ROL::Vector &z, const ROL::TimeStamp &ts) const { initialize(z); if (!isAssembled_) { // Assemble uold Jacobian. assembler_->assembleDynPDEJacobian_uo(matJuo_,pde_,ts,uvec_,uvec_,zvec_,zpar_); // Assemble unew Jacobian and initialize linear solver. assembler_->assembleDynPDEJacobian_un(matJun_,pde_,ts,uvec_,uvec_,zvec_,zpar_); // Assemble old affine term. assembler_->assembleDynPDEResidual(vecR_,pde_,ts,uvec_,uvec_,zvec_,zpar_); // Assemble old control Jacobian. if (zvec_ != ROL::nullPtr) { assembler_->assembleDynPDEJacobian_zf(matJzf_,pde_,ts,uvec_,uvec_,zvec_,zpar_); } if (zpar_ != ROL::nullPtr) { assembler_->assembleDynPDEJacobian_zp(vecJzp_,pde_,ts,uvec_,uvec_,zvec_,zpar_); } // Set solver with unew Jacobian setSolver(); isAssembled_ = true; } } void setSolver(void) const { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::LinDynConstraintSolverConstruct_Jacobian_un); #endif solver_->setA(matJun_); } void solveForward(ROL::Ptr> &x, const ROL::Ptr> &b) const { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::LinDynConstraintSolverSolve_Jacobian_un); #endif solver_->solve(x,b,false); } void solveAdjoint(ROL::Ptr> &x, const ROL::Ptr> &b) const { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::LinDynConstraintSolverSolve_AdjointJacobian_un); #endif solver_->solve(x,b,true); } void applyJacobian_uo(const ROL::Ptr> &Jv, const ROL::Ptr> &v, const bool trans = false) const { if (!trans) { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::LinDynConstraintApplyJacobian_uo); #endif matJuo_->apply(*v,*Jv,Teuchos::NO_TRANS); } else { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::LinDynConstraintApplyAdjointJacobian_uo); #endif matJuo_->apply(*v,*Jv,Teuchos::TRANS); } } void applyJacobian_un(const ROL::Ptr> &Jv, const ROL::Ptr> &v, const bool trans = false) const { if (!trans) { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::LinDynConstraintApplyJacobian_un); #endif matJun_->apply(*v,*Jv,Teuchos::NO_TRANS); } else { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::LinDynConstraintApplyAdjointJacobian_un); #endif matJun_->apply(*v,*Jv,Teuchos::TRANS); } } void applyJacobian_zf(const ROL::Ptr> &Jv, const ROL::Ptr> &v, const bool trans = false) const { if (!trans) { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::LinDynConstraintApplyJacobian_zf); #endif matJzf_->apply(*v,*Jv,Teuchos::NO_TRANS); } else { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::LinDynConstraintApplyAdjointJacobian_zf); #endif matJzf_->apply(*v,*Jv,Teuchos::TRANS); } } void applyJacobian_zp(const ROL::Ptr> &Jv, const ROL::Ptr> &v, const bool zeroOut = true) const { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::LinDynConstraintApplyJacobian_zp); #endif if ( zeroOut ) { Jv->putScalar(static_cast(0)); } const size_t size = static_cast(v->size()); for (size_t i = 0; i < size; ++i) { Teuchos::ArrayView col(&i,1); Jv->update((*v)[i],*(vecJzp_->subView(col)),static_cast(1)); } } void applyAdjointJacobian_zp(const ROL::Ptr > &Jv, const ROL::Ptr > &v) const { #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::LinDynConstraintApplyAdjointJacobian_zp); #endif Teuchos::Array val(1,0); const size_t size = static_cast(Jv->size()); for (size_t i = 0; i < size; ++i) { Teuchos::ArrayView col(&i,1); vecJzp_->subView(col)->dot(*v, val.view(0,1)); (*Jv)[i] = val[0]; } } public: LinDynConstraint(const ROL::Ptr> &pde, const ROL::Ptr> &meshMgr, const ROL::Ptr> &comm, Teuchos::ParameterList &parlist, bool isLTI = false, std::ostream &outStream = std::cout) : pde_ ( pde ), isLTI_ ( isLTI ), isAssembled_ ( false ), initialize_ ( true ) { // Construct assembler. assembler_ = ROL::makePtr>(pde_->getFields(),meshMgr,comm,parlist,outStream); assembler_->setCellNodes(*pde_); // Construct solver. solver_ = ROL::makePtr>(parlist.sublist("Solver")); // Initialize state and constraint vectors. cvec_ = assembler_->createResidualVector(); uvec_ = assembler_->createStateVector(); uvec_->putScalar(static_cast(0)); } const ROL::Ptr> getAssembler(void) const { return assembler_; } const ROL::Ptr> getPDE(void) const { return pde_; } void update(const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) { isAssembled_ = (!isAssembled_ ? isAssembled_ : isLTI_); assemble(z,ts); } void value(ROL::Vector &c, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::Ptr> cf = getField(c); ROL::Ptr> uof = getConstField(uo); ROL::Ptr> unf = getConstField(un); ROL::Ptr> zf = getConstField(z); ROL::Ptr> zp = getConstParameter(z); const Real one(1); assemble(z,ts); c.zero(); cvec_->putScalar(static_cast(0)); // Apply old state contribution applyJacobian_uo(cf, uof, false); // Add affine/control terms cf->update(one, *vecR_, one); if (zf != ROL::nullPtr) { applyJacobian_zf(cvec_, zf, false); cf->update(one, *cvec_, one); } if (zp != ROL::nullPtr) { applyJacobian_zp(cvec_, zp, true); cf->update(one, *cvec_, one); } // Add new state contribution applyJacobian_un(cvec_, unf, false); cf->update(one, *cvec_, one); } void solve(ROL::Vector &c, const ROL::Vector &uo, ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) { ROL::Ptr> cf = getField(c); ROL::Ptr> uof = getConstField(uo); ROL::Ptr> unf = getField(un); ROL::Ptr> zf = getConstField(z); ROL::Ptr> zp = getConstParameter(z); const Real one(1); assemble(z,ts); c.zero(); cvec_->putScalar(static_cast(0)); // Apply old state contribution applyJacobian_uo(cf, uof, false); // Add affine/control terms cf->update(one, *vecR_, one); if (zf != ROL::nullPtr) { applyJacobian_zf(cvec_, zf, false); cf->update(one, *cvec_, one); } if (zp != ROL::nullPtr) { applyJacobian_zp(cvec_, zp, true); cf->update(one, *cvec_, one); } // Apply inverse of new state jacobian solveForward(unf, cf); unf->scale(static_cast(-1)); // Compute residual applyJacobian_un(cvec_, unf, false); cf->update(one, *cvec_, one); } void applyJacobian_uo(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::Ptr> jvf = getField(jv); ROL::Ptr> vf = getConstField(v); assemble(z,ts); applyJacobian_uo(jvf,vf,false); } void applyJacobian_un(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::Ptr> jvf = getField(jv); ROL::Ptr> vf = getConstField(v); assemble(z,ts); applyJacobian_un(jvf,vf,false); } void applyJacobian_z(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { jv.zero(); ROL::Ptr> jvf = getField(jv); const Real one(1); assemble(z,ts); ROL::Ptr> vf = getConstField(v); if (vf != ROL::nullPtr) { applyJacobian_zf(cvec_,vf,false); jvf->update(one,*cvec_,one); } ROL::Ptr> vp = getConstParameter(v); bool zeroOut = (vf == ROL::nullPtr); if (vp != ROL::nullPtr) { applyJacobian_zp(cvec_,vp,zeroOut); jvf->update(one,*cvec_,one); } } void applyAdjointJacobian_uo(ROL::Vector &ajv, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::Ptr> ajvf = getField(ajv); ROL::Ptr> vf = getConstField(v); assemble(z,ts); applyJacobian_uo(ajvf,vf,true); } void applyAdjointJacobian_un(ROL::Vector &ajv, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::Ptr> ajvf = getField(ajv); ROL::Ptr> vf = getConstField(v); assemble(z,ts); applyJacobian_un(ajvf,vf,true); } void applyAdjointJacobian_z(ROL::Vector &ajv, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::Ptr> ajvf = getField(ajv); ROL::Ptr> ajvp = getParameter(ajv); ROL::Ptr> vf = getConstField(v); ROL::Ptr> zf = getConstField(z); ROL::Ptr> zp = getConstParameter(z); assemble(z,ts); if (zf != ROL::nullPtr) { applyJacobian_zf(ajvf,vf,true); } if (zp != ROL::nullPtr) { applyAdjointJacobian_zp(ajvp,vf); } } void applyInverseJacobian_un(ROL::Vector &ijv, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::Ptr> ijvf = getField(ijv); ROL::Ptr> vf = getConstField(v); assemble(z,ts); solveForward(ijvf,vf); } void applyInverseAdjointJacobian_un(ROL::Vector &iajv, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::Ptr> iajvf = getField(iajv); ROL::Ptr> vf = getConstField(v); assemble(z,ts); solveAdjoint(iajvf,vf); } void applyAdjointHessian_uo_uo(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ahwv.zero(); } void applyAdjointHessian_uo_un(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ahwv.zero(); } void applyAdjointHessian_uo_z(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ahwv.zero(); } void applyAdjointHessian_un_uo(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ahwv.zero(); } void applyAdjointHessian_un_un(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ahwv.zero(); } void applyAdjointHessian_un_z(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ahwv.zero(); } void applyAdjointHessian_z_uo(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ahwv.zero(); } void applyAdjointHessian_z_un(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ahwv.zero(); } void applyAdjointHessian_z_z(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &uo, const ROL::Vector &un, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ahwv.zero(); } /***************************************************************************/ /* Output routines. */ /***************************************************************************/ void printMeshData(std::ostream &outStream) const { assembler_->printMeshData(outStream); } void outputTpetraData() const { Tpetra::MatrixMarket::Writer< Tpetra::CrsMatrix<>> matWriter; if (matJuo_ != ROL::nullPtr) { matWriter.writeSparseFile("jacobian_uo.txt", matJuo_); } else { std::ofstream emptyfile; emptyfile.open("jacobian_uo.txt"); emptyfile.close(); } if (matJun_ != ROL::nullPtr) { matWriter.writeSparseFile("jacobian_un.txt", matJun_); } else { std::ofstream emptyfile; emptyfile.open("jacobian_un.txt"); emptyfile.close(); } if (matJzf_ != ROL::nullPtr) { matWriter.writeSparseFile("jacobian_zf.txt", matJzf_); } else { std::ofstream emptyfile; emptyfile.open("jacobian_zf.txt"); emptyfile.close(); } if (vecR_ != ROL::nullPtr) { matWriter.writeDenseFile("residual.txt", vecR_); } else { std::ofstream emptyfile; emptyfile.open("residual.txt"); emptyfile.close(); } } void outputTpetraVector(const ROL::Ptr> &vec, const std::string &filename) const { Tpetra::MatrixMarket::Writer< Tpetra::CrsMatrix<>> vecWriter; vecWriter.writeDenseFile(filename, vec); std::string mapfile = "map_" + filename; vecWriter.writeMapFile(mapfile, *(vec->getMap())); } /***************************************************************************/ /* 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