Debian Codegraph @

packages/rol/example/PDE-OPT/topo-opt/src/volume_obj.hpp

raw
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_OBJ_H
11 #define TOPOPT_VOLUME_OBJ_H
12
13 #include "ROL_Objective.hpp"
14 #include "../../TOOLS/qoi.hpp"
15 #include "../../TOOLS/assembler.hpp"
16 #include "../../TOOLS/pdevector.hpp"
17
18 template <class Real>
19 class TopOptVolumeObjective : public ROL::Objective<Real> {
20 private:
21 const ROL::Ptr<QoI<Real>> qoi_;
22 const ROL::Ptr<Assembler<Real>> assembler_;
23 bool assemble_;
24 ROL::Ptr<Tpetra::MultiVector<>> controlZero_;
25 ROL::Ptr<ROL::Vector<Real>> V_, Vdual_;
26
27 unsigned nupda_;
28 unsigned nfval_;
29 unsigned ngrad_;
30 unsigned nhess_;
31 unsigned nasse_;
32
33 public:
34 TopOptVolumeObjective(
35 const ROL::Ptr<QoI<Real>> &qoi,
36 const ROL::Ptr<Assembler<Real>> &assembler,
37 const ROL::Ptr<const ROL::Vector<Real>> &z)
38 : qoi_(qoi), assembler_(assembler), assemble_(true),
39 nupda_(0), nfval_(0), ngrad_(0), nhess_(0), nasse_(0) {
40 controlZero_ = assembler_->createControlVector(); controlZero_->putScalar(0.0);
41 V_ = z->dual().clone();
42 Vdual_ = z->clone();
43 assemble();
44 }
45
46 void update( const ROL::Vector<Real> &z, bool flag = true, int iter = -1 ) {
47 nupda_++;
48 }
49
50 Real value( const ROL::Vector<Real> &z, Real &tol ) {
51 nfval_++;
52 return Vdual_->dot(z);
53 }
54
55 void gradient( ROL::Vector<Real> &g, const ROL::Vector<Real> &z, Real &tol ) {
56 ngrad_++;
57 g.set(*V_);
58 }
59
60 void hessVec( ROL::Vector<Real> &hv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &z, Real &tol ) {
61 nhess_++;
62 hv.zero();
63 }
64
65 void summarize(std::ostream &stream) const {
66 stream << std::endl;
67 stream << std::string(114,'=') << std::endl;
68 stream << " Volume_Objective::summarize" << std::endl;
69 stream << " Number of calls to update: " << nupda_ << std::endl;
70 stream << " Number of calls to value: " << nfval_ << std::endl;
71 stream << " Number of calls to gradient: " << ngrad_ << std::endl;
72 stream << " Number of calls to hessVec: " << nhess_ << std::endl;
73 stream << " Number of assemblies: " << nasse_ << std::endl;
74 stream << std::string(114,'=') << std::endl;
75 stream << std::endl;
76 }
77
78 // For parametrized (stochastic) objective functions and constraints
79 public:
80 void setParameter(const std::vector<Real> &param) {
81 ROL::Objective<Real>::setParameter(param);
82 qoi_->setParameter(param);
83 }
84
85 private:
86
87 ROL::Ptr<Tpetra::MultiVector<>> getField(ROL::Vector<Real> &x) const {
88 ROL::Ptr<Tpetra::MultiVector<>> xp;
89 try {
90 xp = dynamic_cast<ROL::TpetraMultiVector<Real>&>(x).getVector();
91 }
92 catch (std::exception &e) {
93 try {
94 ROL::Ptr<ROL::TpetraMultiVector<Real>> xvec
95 = dynamic_cast<PDE_OptVector<Real>&>(x).getField();
96 if ( xvec == ROL::nullPtr ) {
97 xp = ROL::nullPtr;
98 }
99 else {
100 xp = xvec->getVector();
101 }
102 }
103 catch (std::exception &ee) {
104 xp = ROL::nullPtr;
105 }
106 }
107 return xp;
108 }
109
110 void assemble(void) {
111 nasse_++;
112 if (assemble_) {
113 ROL::Ptr<Tpetra::MultiVector<>> V = getField(*V_);
114 assembler_->assembleQoIGradient2(V,qoi_,ROL::nullPtr,controlZero_);
115 Vdual_->set(V_->dual());
116 assemble_ = false;
117 }
118 }
119 }; // class TopOptVolumeObjective
120
121 #endif