// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef TOPOPT_VOLUME_CON_H #define TOPOPT_VOLUME_CON_H #include "ROL_Constraint.hpp" #include "ROL_SingletonVector.hpp" #include "../../TOOLS/qoi.hpp" #include "../../TOOLS/assembler.hpp" #include "../../TOOLS/pdevector.hpp" template class TopOptVolumeConstraint : public ROL::Constraint { private: // Elasticity PDE const ROL::Ptr> qoi_; const ROL::Ptr> assembler_; bool assemble_; ROL::Ptr> controlZero_; ROL::Ptr> V_, Vdual_; Real V0_; unsigned nupda_; unsigned nfval_; unsigned njaco_; unsigned najac_; unsigned nhess_; unsigned nasse_; public: TopOptVolumeConstraint( 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) { controlZero_ = assembler_->createControlVector(); controlZero_->putScalar(0.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 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; } // For parametrized (stochastic) objective functions and constraints public: void setParameter(const std::vector ¶m) { ROL::Constraint::setParameter(param); qoi_->setParameter(param); } 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_); V0_ = assembler_->assembleQoIValue(qoi_,ROL::nullPtr,controlZero_); assembler_->assembleQoIGradient2(V,qoi_,ROL::nullPtr,controlZero_); Vdual_->set(V_->dual()); assemble_ = false; } } }; // class Volume_Constraint #endif