Debian Codegraph @

packages/rol/example/PDE-OPT/topo-opt/multimat/src/con_selection.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 /*! \file con_selection.hpp
11 \brief Defines the selectiont constraint.
12 */
13
14 #ifndef ROL_PDEOPT_SELECTION_H
15 #define ROL_PDEOPT_SELECTION_H
16
17 #include "ROL_Constraint.hpp"
18 #include "../../../TOOLS/pde.hpp"
19 #include "../../../TOOLS/assembler.hpp"
20 #include "../../../TOOLS/pdevector.hpp"
21
22 template<class Real>
23 class MultiMat_Selection_Constraint : public ROL::Constraint<Real> {
24 private:
25 const ROL::Ptr<PDE<Real>> pde_;
26 ROL::Ptr<Assembler<Real>> assembler_;
27 ROL::Ptr<Tpetra::MultiVector<>> vecR_;
28 ROL::Ptr<Tpetra::CrsMatrix<>> matJ_;
29
30 unsigned nupda_;
31 unsigned nfval_;
32 unsigned njaco_;
33 unsigned najac_;
34 unsigned nhess_;
35 unsigned nasse_;
36
37 public:
38 MultiMat_Selection_Constraint(const ROL::Ptr<PDE<Real>> &pde,
39 const ROL::Ptr<MeshManager<Real>> &meshMgr,
40 const ROL::Ptr<const Teuchos::Comm<int>> &comm,
41 ROL::ParameterList &parlist,
42 std::ostream &outStream = std::cout)
43 : pde_(pde),
44 nupda_(0), nfval_(0), njaco_(0), najac_(0), nhess_(0), nasse_(0) {
45 assembler_ = ROL::makePtr<Assembler<Real>>(pde_->getFields(),pde_->getFields2(),meshMgr,comm,parlist,outStream);
46 assembler_->setCellNodes(*pde_);
47 assemble();
48 }
49
50 const ROL::Ptr<Assembler<Real>> getAssembler(void) const {
51 return assembler_;
52 }
53
54 const ROL::Ptr<PDE<Real>> getPDE(void) const {
55 return pde_;
56 }
57
58 void update( const ROL::Vector<Real> &z, bool flag = true, int iter = -1 ) {
59 nupda_++;
60 }
61
62 void value(ROL::Vector<Real> &c, const ROL::Vector<Real> &z, Real &tol) {
63 nfval_++;
64 ROL::Ptr<Tpetra::MultiVector<>> cf = getField(c);
65 ROL::Ptr<const Tpetra::MultiVector<>> zf = getConstField(z);
66 buildResidual(cf,zf);
67 }
68
69 void applyJacobian(ROL::Vector<Real> &jv,
70 const ROL::Vector<Real> &v,
71 const ROL::Vector<Real> &z, Real &tol) {
72 njaco_++;
73 ROL::Ptr<Tpetra::MultiVector<>> jvf = getField(jv);
74 ROL::Ptr<const Tpetra::MultiVector<>> vf = getConstField(v);
75 applyJacobian(jvf,vf);
76 }
77
78 void applyAdjointJacobian(ROL::Vector<Real> &ajv,
79 const ROL::Vector<Real> &v,
80 const ROL::Vector<Real> &z, Real &tol) {
81 najac_++;
82 ROL::Ptr<Tpetra::MultiVector<> > ajvf = getField(ajv);
83 ROL::Ptr<const Tpetra::MultiVector<> > vf = getConstField(v);
84 applyJacobian(ajvf,vf,true);
85 }
86
87 void applyAdjointHessian(ROL::Vector<Real> &ahwv,
88 const ROL::Vector<Real> &w,
89 const ROL::Vector<Real> &v,
90 const ROL::Vector<Real> &z, Real &tol) {
91 nhess_++;
92 ahwv.zero();
93 }
94
95 void outputTpetraData() const {
96 Tpetra::MatrixMarket::Writer< Tpetra::CrsMatrix<> > matWriter;
97 if (matJ_ != ROL::nullPtr) {
98 matWriter.writeSparseFile("selection_jacobian.txt", matJ_);
99 }
100 else {
101 std::ofstream emptyfile;
102 emptyfile.open("selection_jacobian.txt");
103 emptyfile.close();
104 }
105 if (vecR_ != ROL::nullPtr) {
106 matWriter.writeDenseFile("selection_rhs.txt", vecR_);
107 }
108 else {
109 std::ofstream emptyfile;
110 emptyfile.open("selection_rhs.txt");
111 emptyfile.close();
112 }
113 }
114
115 void outputTpetraVector(const ROL::Ptr<const Tpetra::MultiVector<> > &vec,
116 const std::string &filename) const {
117 assembler_->outputTpetraVector(vec, filename);
118 }
119
120 void inputTpetraVector(ROL::Ptr<Tpetra::MultiVector<>> &vec,
121 const std::string &filename) const {
122 assembler_->inputTpetraVector(vec, filename);
123 }
124
125 void summarize(std::ostream &stream) const {
126 stream << std::endl;
127 stream << std::string(114,'=') << std::endl;
128 stream << " Selection_Constraint::summarize" << std::endl;
129 stream << " Number of calls to update: " << nupda_ << std::endl;
130 stream << " Number of calls to value: " << nfval_ << std::endl;
131 stream << " Number of calls to applyJacobian: " << njaco_ << std::endl;
132 stream << " Number of calls to applyAdjointJacobian: " << najac_ << std::endl;
133 stream << " Number of calls to applyAdjointHessian: " << nhess_ << std::endl;
134 stream << " Number of assemblies: " << nasse_ << std::endl;
135 stream << std::string(114,'=') << std::endl;
136 stream << std::endl;
137 }
138
139 private: // Vector accessor functions
140
141 ROL::Ptr<const Tpetra::MultiVector<>> getConstField(const ROL::Vector<Real> &x) const {
142 ROL::Ptr<const Tpetra::MultiVector<>> xp;
143 try {
144 xp = dynamic_cast<const ROL::TpetraMultiVector<Real>&>(x).getVector();
145 }
146 catch (std::exception &e) {
147 try {
148 ROL::Ptr<const ROL::TpetraMultiVector<Real> > xvec
149 = dynamic_cast<const PDE_OptVector<Real>&>(x).getField();
150 if (xvec == ROL::nullPtr) {
151 xp = ROL::nullPtr;
152 }
153 else {
154 xp = xvec->getVector();
155 }
156 }
157 catch (std::exception &ee) {
158 xp = ROL::nullPtr;
159 }
160 }
161 return xp;
162 }
163
164 ROL::Ptr<Tpetra::MultiVector<>> getField(ROL::Vector<Real> &x) const {
165 ROL::Ptr<Tpetra::MultiVector<>> xp;
166 try {
167 xp = dynamic_cast<ROL::TpetraMultiVector<Real>&>(x).getVector();
168 }
169 catch (std::exception &e) {
170 try {
171 ROL::Ptr<ROL::TpetraMultiVector<Real> > xvec
172 = dynamic_cast<PDE_OptVector<Real>&>(x).getField();
173 if ( xvec == ROL::nullPtr ) {
174 xp = ROL::nullPtr;
175 }
176 else {
177 xp = xvec->getVector();
178 }
179 }
180 catch (std::exception &ee) {
181 xp = ROL::nullPtr;
182 }
183 }
184 return xp;
185 }
186
187 void assemble(void) {
188 nasse_++;
189 ROL::Ptr<Tpetra::MultiVector<>> u = assembler_->createStateVector();
190 ROL::Ptr<Tpetra::MultiVector<>> z = assembler_->createControlVector();
191 u->putScalar(static_cast<Real>(0)); z->putScalar(static_cast<Real>(0));
192 assembler_->assemblePDEResidual(vecR_, pde_, u, z);
193 assembler_->assemblePDEJacobian2(matJ_, pde_, u, z);
194 outputTpetraData();
195 }
196
197 // Application routines
198 void applyJacobian(const ROL::Ptr<Tpetra::MultiVector<>> &Jv,
199 const ROL::Ptr<const Tpetra::MultiVector<>> &v,
200 bool transpose = false) const {
201 if (transpose) {
202 matJ_->apply(*v,*Jv,Teuchos::TRANS);
203 }
204 else {
205 matJ_->apply(*v,*Jv);
206 }
207 }
208
209 void buildResidual(const ROL::Ptr<Tpetra::MultiVector<>> &r,
210 const ROL::Ptr<const Tpetra::MultiVector<>> &z) const {
211 applyJacobian(r,z);
212 r->update(static_cast<Real>(1),*vecR_,static_cast<Real>(1));
213 }
214 };
215
216 #endif