// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef VOLUME_CON_MULTIMAT_H #define VOLUME_CON_MULTIMAT_H #include "ROL_Constraint.hpp" #include "ROL_SingletonVector.hpp" #include "../../../TOOLS/qoi.hpp" #include "../../../TOOLS/assembler.hpp" #include "../../../TOOLS/pdevector.hpp" template class MultiMat_Volume_Constraint : public ROL::Constraint { private: const ROL::Ptr> qoi_; const ROL::Ptr> assembler_; bool assemble_; ROL::Ptr> V_, Vdual_; Real V0_; unsigned nupda_; unsigned nfval_; unsigned njaco_; unsigned najac_; unsigned nhess_; unsigned nasse_; public: MultiMat_Volume_Constraint( const ROL::Ptr> &qoi, const ROL::Ptr> &assembler, const ROL::Ptr> &z) : qoi_(qoi), assembler_(assembler), assemble_(true), nupda_(0), nfval_(0), njaco_(0), najac_(0), nhess_(0), nasse_(0) { V_ = z->dual().clone(); Vdual_ = z->clone(); assemble(); } 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_++; dynamic_cast&>(c).setValue(V0_ + Vdual_->dot(z)); } void applyJacobian(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &z, Real &tol) { njaco_++; dynamic_cast&>(jv).setValue(Vdual_->dot(v)); } void applyAdjointJacobian( ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &z, Real &tol ) { najac_++; Real vval = dynamic_cast&>(v).getValue(); jv.set(*V_); jv.scale(vval); } 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 (V_ != ROL::nullPtr) { ROL::Ptr> V = getField(*V_); matWriter.writeDenseFile("volume_jacobian.txt", V); } else { std::ofstream emptyfile; emptyfile.open("volume_jacobian.txt"); emptyfile.close(); } } void summarize(std::ostream &stream) const { stream << std::endl; stream << std::string(114,'=') << std::endl; stream << " Volume_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: 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_++; if (assemble_) { ROL::Ptr> V = getField(*V_); ROL::Ptr> z0 = assembler_->createControlVector(); z0->putScalar(0.0); V0_ = assembler_->assembleQoIValue(qoi_,ROL::nullPtr,z0); assembler_->assembleQoIGradient2(V,qoi_,ROL::nullPtr,z0); Vdual_->set(V_->dual()); assemble_ = false; outputTpetraData(); } } }; // class Volume_Constraint #endif