// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /*! \file con_selection.hpp \brief Defines the selectiont constraint. */ #ifndef ROL_PDEOPT_SELECTION_H #define ROL_PDEOPT_SELECTION_H #include "ROL_Constraint.hpp" #include "../../../TOOLS/pde.hpp" #include "../../../TOOLS/assembler.hpp" #include "../../../TOOLS/pdevector.hpp" template class MultiMat_Selection_Constraint : public ROL::Constraint { private: const ROL::Ptr> pde_; ROL::Ptr> assembler_; ROL::Ptr> vecR_; ROL::Ptr> matJ_; unsigned nupda_; unsigned nfval_; unsigned njaco_; unsigned najac_; unsigned nhess_; unsigned nasse_; public: MultiMat_Selection_Constraint(const ROL::Ptr> &pde, const ROL::Ptr> &meshMgr, const ROL::Ptr> &comm, ROL::ParameterList &parlist, std::ostream &outStream = std::cout) : pde_(pde), nupda_(0), nfval_(0), njaco_(0), najac_(0), nhess_(0), nasse_(0) { assembler_ = ROL::makePtr>(pde_->getFields(),pde_->getFields2(),meshMgr,comm,parlist,outStream); assembler_->setCellNodes(*pde_); assemble(); } const ROL::Ptr> getAssembler(void) const { return assembler_; } const ROL::Ptr> getPDE(void) const { return pde_; } void update( const ROL::Vector &z, bool flag = true, int iter = -1 ) { nupda_++; } void value(ROL::Vector &c, const ROL::Vector &z, Real &tol) { nfval_++; ROL::Ptr> cf = getField(c); ROL::Ptr> zf = getConstField(z); buildResidual(cf,zf); } void applyJacobian(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &z, Real &tol) { njaco_++; ROL::Ptr> jvf = getField(jv); ROL::Ptr> vf = getConstField(v); applyJacobian(jvf,vf); } void applyAdjointJacobian(ROL::Vector &ajv, const ROL::Vector &v, const ROL::Vector &z, Real &tol) { najac_++; ROL::Ptr > ajvf = getField(ajv); ROL::Ptr > vf = getConstField(v); applyJacobian(ajvf,vf,true); } void applyAdjointHessian(ROL::Vector &ahwv, const ROL::Vector &w, const ROL::Vector &v, const ROL::Vector &z, Real &tol) { nhess_++; ahwv.zero(); } void outputTpetraData() const { Tpetra::MatrixMarket::Writer< Tpetra::CrsMatrix<> > matWriter; if (matJ_ != ROL::nullPtr) { matWriter.writeSparseFile("selection_jacobian.txt", matJ_); } else { std::ofstream emptyfile; emptyfile.open("selection_jacobian.txt"); emptyfile.close(); } if (vecR_ != ROL::nullPtr) { matWriter.writeDenseFile("selection_rhs.txt", vecR_); } else { std::ofstream emptyfile; emptyfile.open("selection_rhs.txt"); emptyfile.close(); } } void outputTpetraVector(const ROL::Ptr > &vec, const std::string &filename) const { assembler_->outputTpetraVector(vec, filename); } void inputTpetraVector(ROL::Ptr> &vec, const std::string &filename) const { assembler_->inputTpetraVector(vec, filename); } void summarize(std::ostream &stream) const { stream << std::endl; stream << std::string(114,'=') << std::endl; stream << " Selection_Constraint::summarize" << std::endl; stream << " Number of calls to update: " << nupda_ << std::endl; stream << " Number of calls to value: " << nfval_ << std::endl; stream << " Number of calls to applyJacobian: " << njaco_ << std::endl; stream << " Number of calls to applyAdjointJacobian: " << najac_ << std::endl; stream << " Number of calls to applyAdjointHessian: " << nhess_ << std::endl; stream << " Number of assemblies: " << nasse_ << std::endl; stream << std::string(114,'=') << std::endl; stream << std::endl; } 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; } void assemble(void) { nasse_++; ROL::Ptr> u = assembler_->createStateVector(); ROL::Ptr> z = assembler_->createControlVector(); u->putScalar(static_cast(0)); z->putScalar(static_cast(0)); assembler_->assemblePDEResidual(vecR_, pde_, u, z); assembler_->assemblePDEJacobian2(matJ_, pde_, u, z); outputTpetraData(); } // Application routines void applyJacobian(const ROL::Ptr> &Jv, const ROL::Ptr> &v, bool transpose = false) const { if (transpose) { matJ_->apply(*v,*Jv,Teuchos::TRANS); } else { matJ_->apply(*v,*Jv); } } void buildResidual(const ROL::Ptr> &r, const ROL::Ptr> &z) const { applyJacobian(r,z); r->update(static_cast(1),*vecR_,static_cast(1)); } }; #endif