Debian Codegraph @

packages/rol/example/PDE-OPT/topo-opt/multimat/src/volume_con.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 VOLUME_CON_MULTIMAT_H
11 #define VOLUME_CON_MULTIMAT_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 MultiMat_Volume_Constraint : public ROL::Constraint<Real> {
21 private:
22 const ROL::Ptr<QoI<Real>> qoi_;
23 const ROL::Ptr<Assembler<Real>> assembler_;
24 bool assemble_;
25 ROL::Ptr<ROL::Vector<Real>> V_, Vdual_;
26 Real V0_;
27
28 unsigned nupda_;
29 unsigned nfval_;
30 unsigned njaco_;
31 unsigned najac_;
32 unsigned nhess_;
33 unsigned nasse_;
34
35 public:
36 MultiMat_Volume_Constraint(
37 const ROL::Ptr<QoI<Real>> &qoi,
38 const ROL::Ptr<Assembler<Real>> &assembler,
39 const ROL::Ptr<const ROL::Vector<Real>> &z)
40 : qoi_(qoi), assembler_(assembler), assemble_(true),
41 nupda_(0), nfval_(0), njaco_(0), najac_(0), nhess_(0), nasse_(0) {
42 V_ = z->dual().clone();
43 Vdual_ = z->clone();
44 assemble();
45 }
46
47 void update( const ROL::Vector<Real> &z, bool flag = true, int iter = -1 ) {
48 nupda_++;
49 }
50
51 void value( ROL::Vector<Real> &c, const ROL::Vector<Real> &z, Real &tol ) {
52 nfval_++;
53 dynamic_cast<ROL::SingletonVector<Real>&>(c).setValue(V0_ + Vdual_->dot(z));
54 }
55
56 void applyJacobian(ROL::Vector<Real> &jv, const ROL::Vector<Real> &v,
57 const ROL::Vector<Real> &z, Real &tol) {
58 njaco_++;
59 dynamic_cast<ROL::SingletonVector<Real>&>(jv).setValue(Vdual_->dot(v));
60 }
61
62 void applyAdjointJacobian( ROL::Vector<Real> &jv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &z, Real &tol ) {
63 najac_++;
64 Real vval = dynamic_cast<const ROL::SingletonVector<Real>&>(v).getValue();
65 jv.set(*V_);
66 jv.scale(vval);
67 }
68
69 void applyAdjointHessian( ROL::Vector<Real> &ahwv, const ROL::Vector<Real> &w, const ROL::Vector<Real> &v, const ROL::Vector<Real> &z, Real &tol ) {
70 nhess_++;
71 ahwv.zero();
72 }
73
74 void outputTpetraData() const {
75 Tpetra::MatrixMarket::Writer< Tpetra::CrsMatrix<> > matWriter;
76 if (V_ != ROL::nullPtr) {
77 ROL::Ptr<Tpetra::MultiVector<>> V = getField(*V_);
78 matWriter.writeDenseFile("volume_jacobian.txt", V);
79 }
80 else {
81 std::ofstream emptyfile;
82 emptyfile.open("volume_jacobian.txt");
83 emptyfile.close();
84 }
85 }
86
87 void summarize(std::ostream &stream) const {
88 stream << std::endl;
89 stream << std::string(114,'=') << std::endl;
90 stream << " Volume_Constraint::summarize" << std::endl;
91 stream << " Number of calls to update: " << nupda_ << std::endl;
92 stream << " Number of calls to value: " << nfval_ << std::endl;
93 stream << " Number of calls to applyJacobian: " << njaco_ << std::endl;
94 stream << " Number of calls to applyAdjointJacobian: " << najac_ << std::endl;
95 stream << " Number of calls to applyAdjointHessian: " << nhess_ << std::endl;
96 stream << " Number of assemblies: " << nasse_ << std::endl;
97 stream << std::string(114,'=') << std::endl;
98 stream << std::endl;
99 }
100
101 private:
102
103 ROL::Ptr<Tpetra::MultiVector<>> getField(ROL::Vector<Real> &x) const {
104 ROL::Ptr<Tpetra::MultiVector<>> xp;
105 try {
106 xp = dynamic_cast<ROL::TpetraMultiVector<Real>&>(x).getVector();
107 }
108 catch (std::exception &e) {
109 try {
110 ROL::Ptr<ROL::TpetraMultiVector<Real>> xvec
111 = dynamic_cast<PDE_OptVector<Real>&>(x).getField();
112 if ( xvec == ROL::nullPtr ) {
113 xp = ROL::nullPtr;
114 }
115 else {
116 xp = xvec->getVector();
117 }
118 }
119 catch (std::exception &ee) {
120 xp = ROL::nullPtr;
121 }
122 }
123 return xp;
124 }
125
126 void assemble(void) {
127 nasse_++;
128 if (assemble_) {
129 ROL::Ptr<Tpetra::MultiVector<>> V = getField(*V_);
130 ROL::Ptr<Tpetra::MultiVector<>> z0 = assembler_->createControlVector();
131 z0->putScalar(0.0);
132 V0_ = assembler_->assembleQoIValue(qoi_,ROL::nullPtr,z0);
133 assembler_->assembleQoIGradient2(V,qoi_,ROL::nullPtr,z0);
134 Vdual_->set(V_->dual());
135 assemble_ = false;
136 outputTpetraData();
137 }
138 }
139 }; // class Volume_Constraint
140
141 #endif