| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Rapid Optimization Library (ROL) Package |
| 4 |
// |
| 5 |
// Copyright 2014 NTESS and the ROL contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef TOPOPT_VOLUME_CON_H |
| 11 |
#define TOPOPT_VOLUME_CON_H |
| 12 |
|
| 13 |
#include "ROL_Constraint.hpp" |
| 14 |
#include "ROL_SingletonVector.hpp" |
| 15 |
#include "../../TOOLS/qoi.hpp" |
| 16 |
#include "../../TOOLS/assembler.hpp" |
| 17 |
#include "../../TOOLS/pdevector.hpp" |
| 18 |
|
| 19 |
template <class Real> |
| 20 |
class TopOptVolumeConstraint : public ROL::Constraint<Real> { |
| 21 |
private: |
| 22 |
// Elasticity PDE |
| 23 |
const ROL::Ptr<QoI<Real>> qoi_; |
| 24 |
const ROL::Ptr<Assembler<Real>> assembler_; |
| 25 |
bool assemble_; |
| 26 |
ROL::Ptr<Tpetra::MultiVector<>> controlZero_; |
| 27 |
ROL::Ptr<ROL::Vector<Real>> V_, Vdual_; |
| 28 |
Real V0_; |
| 29 |
|
| 30 |
unsigned nupda_; |
| 31 |
unsigned nfval_; |
| 32 |
unsigned njaco_; |
| 33 |
unsigned najac_; |
| 34 |
unsigned nhess_; |
| 35 |
unsigned nasse_; |
| 36 |
|
| 37 |
public: |
| 38 |
TopOptVolumeConstraint( |
| 39 |
const ROL::Ptr<QoI<Real>> &qoi, |
| 40 |
const ROL::Ptr<Assembler<Real>> &assembler, |
| 41 |
const ROL::Ptr<const ROL::Vector<Real>> &z) |
| 42 |
: qoi_(qoi), assembler_(assembler), assemble_(true), |
| 43 |
nupda_(0), nfval_(0), njaco_(0), najac_(0), nhess_(0), nasse_(0) { |
| 44 |
controlZero_ = assembler_->createControlVector(); controlZero_->putScalar(0.0); |
| 45 |
V_ = z->dual().clone(); |
| 46 |
Vdual_ = z->clone(); |
| 47 |
assemble(); |
| 48 |
} |
| 49 |
|
| 50 |
void update( const ROL::Vector<Real> &z, bool flag = true, int iter = -1 ) { |
| 51 |
nupda_++; |
| 52 |
} |
| 53 |
|
| 54 |
void value( ROL::Vector<Real> &c, const ROL::Vector<Real> &z, Real &tol ) { |
| 55 |
nfval_++; |
| 56 |
dynamic_cast<ROL::SingletonVector<Real>&>(c).setValue(V0_ + Vdual_->dot(z)); |
| 57 |
} |
| 58 |
|
| 59 |
void applyJacobian(ROL::Vector<Real> &jv, const ROL::Vector<Real> &v, |
| 60 |
const ROL::Vector<Real> &z, Real &tol) { |
| 61 |
njaco_++; |
| 62 |
dynamic_cast<ROL::SingletonVector<Real>&>(jv).setValue(Vdual_->dot(v)); |
| 63 |
} |
| 64 |
|
| 65 |
void applyAdjointJacobian( ROL::Vector<Real> &jv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &z, Real &tol ) { |
| 66 |
najac_++; |
| 67 |
Real vval = dynamic_cast<const ROL::SingletonVector<Real>&>(v).getValue(); |
| 68 |
jv.set(*V_); |
| 69 |
jv.scale(vval); |
| 70 |
} |
| 71 |
|
| 72 |
void applyAdjointHessian( ROL::Vector<Real> &ahwv, const ROL::Vector<Real> &w, const ROL::Vector<Real> &v, const ROL::Vector<Real> &z, Real &tol ) { |
| 73 |
nhess_++; |
| 74 |
ahwv.zero(); |
| 75 |
} |
| 76 |
|
| 77 |
void summarize(std::ostream &stream) const { |
| 78 |
stream << std::endl; |
| 79 |
stream << std::string(114,'=') << std::endl; |
| 80 |
stream << " Volume_Constraint::summarize" << std::endl; |
| 81 |
stream << " Number of calls to update: " << nupda_ << std::endl; |
| 82 |
stream << " Number of calls to value: " << nfval_ << std::endl; |
| 83 |
stream << " Number of calls to applyJacobian: " << njaco_ << std::endl; |
| 84 |
stream << " Number of calls to applyAdjointJacobian: " << najac_ << std::endl; |
| 85 |
stream << " Number of calls to applyAdjointHessian: " << nhess_ << std::endl; |
| 86 |
stream << " Number of assemblies: " << nasse_ << std::endl; |
| 87 |
stream << std::string(114,'=') << std::endl; |
| 88 |
stream << std::endl; |
| 89 |
} |
| 90 |
|
| 91 |
// For parametrized (stochastic) objective functions and constraints |
| 92 |
public: |
| 93 |
void setParameter(const std::vector<Real> ¶m) { |
| 94 |
ROL::Constraint<Real>::setParameter(param); |
| 95 |
qoi_->setParameter(param); |
| 96 |
} |
| 97 |
|
| 98 |
private: |
| 99 |
|
| 100 |
ROL::Ptr<Tpetra::MultiVector<>> getField(ROL::Vector<Real> &x) const { |
| 101 |
ROL::Ptr<Tpetra::MultiVector<>> xp; |
| 102 |
try { |
| 103 |
xp = dynamic_cast<ROL::TpetraMultiVector<Real>&>(x).getVector(); |
| 104 |
} |
| 105 |
catch (std::exception &e) { |
| 106 |
try { |
| 107 |
ROL::Ptr<ROL::TpetraMultiVector<Real>> xvec |
| 108 |
= dynamic_cast<PDE_OptVector<Real>&>(x).getField(); |
| 109 |
if ( xvec == ROL::nullPtr ) { |
| 110 |
xp = ROL::nullPtr; |
| 111 |
} |
| 112 |
else { |
| 113 |
xp = xvec->getVector(); |
| 114 |
} |
| 115 |
} |
| 116 |
catch (std::exception &ee) { |
| 117 |
xp = ROL::nullPtr; |
| 118 |
} |
| 119 |
} |
| 120 |
return xp; |
| 121 |
} |
| 122 |
|
| 123 |
void assemble(void) { |
| 124 |
nasse_++; |
| 125 |
if (assemble_) { |
| 126 |
ROL::Ptr<Tpetra::MultiVector<>> V = getField(*V_); |
| 127 |
V0_ = assembler_->assembleQoIValue(qoi_,ROL::nullPtr,controlZero_); |
| 128 |
assembler_->assembleQoIGradient2(V,qoi_,ROL::nullPtr,controlZero_); |
| 129 |
Vdual_->set(V_->dual()); |
| 130 |
assemble_ = false; |
| 131 |
} |
| 132 |
} |
| 133 |
}; // class Volume_Constraint |
| 134 |
|
| 135 |
#endif |