Debian Codegraph @

packages/rol/example/PDE-OPT/0ld/elasticitySIMP_topologyOptimization/objective.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 objective.hpp
11 \brief Defines the SimOpt objective function for the 'poisson' example.
12 */
13
14 #ifndef ROL_PDEOPT_ELASTICITYSIMP_OBJECTIVE_H
15 #define ROL_PDEOPT_ELASTICITYSIMP_OBJECTIVE_H
16
17 #include "ROL_Objective_SimOpt.hpp"
18 #include "ROL_ScaledTpetraMultiVector.hpp"
19 #include "filter.hpp"
20 #include "data.hpp"
21
22 template<class Real>
23 class Objective_PDEOPT_ElasticitySIMP : public ROL::Objective_SimOpt<Real> {
24 private:
25
26 const ROL::Ptr<ElasticitySIMPOperators<Real> > data_;
27 const ROL::Ptr<DensityFilter<Real> > filter_;
28 Real scale_;
29 bool useFU_;
30 bool print_;
31
32 public:
33
34 Objective_PDEOPT_ElasticitySIMP(const ROL::Ptr<ElasticitySIMPOperators<Real> > &data,
35 const ROL::Ptr<DensityFilter<Real> > &filter,
36 const Teuchos::RCP<Teuchos::ParameterList> &parlist)
37 : data_(data), filter_(filter) {
38 // Compute compliance scaling
39 Teuchos::Array<Real> dotF(1, 0);
40 (data_->getVecF())->dot(*(data_->getVecF()),dotF);
41 Real minDensity = parlist->sublist("ElasticitySIMP").get<Real>("Minimum Density");
42 scale_ = minDensity/dotF[0];
43 useFU_ = parlist->sublist("ElasticitySIMP").get("Use Force Dot Displacement Objective",true);
44 print_ = parlist->sublist("ElasticitySIMP").get("Print Density",false);
45 }
46
47 void update(const ROL::Vector<Real> &u,
48 const ROL::Vector<Real> &z,
49 bool flag = true,
50 int iter = -1) {
51 if ( !useFU_ ) {
52 ROL::Ptr<const Tpetra::MultiVector<> > zp
53 = (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(z)).getVector();
54 ROL::Ptr<Tpetra::MultiVector<> > Fzp
55 = ROL::makePtr<Tpetra::MultiVector<>>(zp->getMap(), 1);
56 filter_->apply(Fzp, zp);
57 data_->updateMaterialDensity(Fzp);
58 }
59 if ( (flag && iter >= 0) && print_ ) {
60 ROL::Ptr<const Tpetra::MultiVector<> > zp
61 = (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(z)).getVector();
62 std::stringstream filename;
63 filename << "density_" << iter << ".txt";
64 data_->outputTpetraVector(zp,filename.str());
65 }
66 }
67
68 Real value(const ROL::Vector<Real> &u,
69 const ROL::Vector<Real> &z,
70 Real &tol) {
71 ROL::Ptr<const Tpetra::MultiVector<> > up
72 = (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(u)).getVector();
73 ROL::Ptr<const Tpetra::MultiVector<> > zp
74 = (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(z)).getVector();
75
76 Teuchos::Array<Real> dotvalU(1, 0);
77 if ( useFU_ ) {
78 up->dot(*(data_->getVecF()), dotvalU.view(0,1));
79 }
80 else {
81 ROL::Ptr<Tpetra::MultiVector<> > matvecp
82 = ROL::makePtr<Tpetra::MultiVector<>>(*up, Teuchos::Copy);
83 data_->ApplyJacobian1ToVec(matvecp, up);
84 up->dot(*matvecp, dotvalU.view(0,1));
85 }
86
87 return scale_*dotvalU[0];
88 }
89
90 void gradient_1(ROL::Vector<Real> &g,
91 const ROL::Vector<Real> &u,
92 const ROL::Vector<Real> &z,
93 Real &tol) {
94 ROL::Ptr<Tpetra::MultiVector<> > gp
95 = (dynamic_cast<ROL::TpetraMultiVector<Real>&>(g)).getVector();
96 if ( useFU_ ) {
97 Tpetra::deep_copy(*gp, *(data_->getVecF()));
98 }
99 else {
100 ROL::Ptr<const Tpetra::MultiVector<> > up
101 = (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(u)).getVector();
102 data_->ApplyJacobian1ToVec(gp, up);
103 Real two(2);
104 gp->scale(two);
105 }
106 gp->scale(scale_);
107 }
108
109 void gradient_2(ROL::Vector<Real> &g,
110 const ROL::Vector<Real> &u,
111 const ROL::Vector<Real> &z,
112 Real &tol) {
113 ROL::Ptr<Tpetra::MultiVector<> > gp
114 = (dynamic_cast<ROL::TpetraMultiVector<Real>&>(g)).getVector();
115 ROL::Ptr<const Tpetra::MultiVector<> > zp
116 = (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(z)).getVector();
117 if ( useFU_ ) {
118 g.zero();
119 }
120 else {
121 ROL::Ptr<const Tpetra::MultiVector<> > up
122 = (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(u)).getVector();
123 ROL::Ptr<Tpetra::MultiVector<> > tmp
124 = ROL::makePtr<Tpetra::MultiVector<>>(gp->getMap(), 1);
125 data_->ApplyAdjointJacobian2ToVec (tmp, up, up);
126 filter_->apply(gp, tmp);
127 }
128 }
129
130 void hessVec_11(ROL::Vector<Real> &hv,
131 const ROL::Vector<Real> &v,
132 const ROL::Vector<Real> &u,
133 const ROL::Vector<Real> &z,
134 Real &tol) {
135 if ( useFU_ ) {
136 hv.zero();
137 }
138 else {
139 ROL::Ptr<Tpetra::MultiVector<> > hvp
140 = (dynamic_cast<ROL::TpetraMultiVector<Real>&>(hv)).getVector();
141 ROL::Ptr<const Tpetra::MultiVector<> > vp
142 = (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(v)).getVector();
143 data_->ApplyJacobian1ToVec(hvp, vp);
144 Real two(2);
145 hvp->scale(two*scale_);
146 }
147 }
148
149 void hessVec_12(ROL::Vector<Real> &hv,
150 const ROL::Vector<Real> &v,
151 const ROL::Vector<Real> &u,
152 const ROL::Vector<Real> &z,
153 Real &tol) {
154 if ( useFU_ ) {
155 hv.zero();
156 }
157 else {
158 ROL::Ptr<Tpetra::MultiVector<> > hvp
159 = (dynamic_cast<ROL::TpetraMultiVector<Real>&>(hv)).getVector();
160 ROL::Ptr<const Tpetra::MultiVector<> > vp
161 = (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(v)).getVector();
162 ROL::Ptr<const Tpetra::MultiVector<> > up
163 = (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(u)).getVector();
164 ROL::Ptr<Tpetra::MultiVector<> > Fvp
165 = ROL::makePtr<Tpetra::MultiVector<>>(vp->getMap(), 1);
166 filter_->apply(Fvp, vp);
167 data_->ApplyJacobian2ToVec (hvp, up, Fvp);
168 Real two(2);
169 hvp->scale(two*scale_);
170 }
171 }
172
173 void hessVec_21(ROL::Vector<Real> &hv,
174 const ROL::Vector<Real> &v,
175 const ROL::Vector<Real> &u,
176 const ROL::Vector<Real> &z,
177 Real &tol) {
178 if ( useFU_ ) {
179 hv.zero();
180 }
181 else {
182 ROL::Ptr<Tpetra::MultiVector<> > hvp
183 = (dynamic_cast<ROL::TpetraMultiVector<Real>&>(hv)).getVector();
184 ROL::Ptr<const Tpetra::MultiVector<> > vp
185 = (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(v)).getVector();
186 ROL::Ptr<const Tpetra::MultiVector<> > up
187 = (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(u)).getVector();
188 ROL::Ptr<Tpetra::MultiVector<> > tmp
189 = ROL::makePtr<Tpetra::MultiVector<>>(hvp->getMap(), 1);
190 data_->ApplyAdjointJacobian2ToVec (tmp, up, vp);
191 filter_->apply(hvp, tmp);
192 Real two(2);
193 hvp->scale(two*scale_);
194 }
195 }
196
197 void hessVec_22(ROL::Vector<Real> &hv,
198 const ROL::Vector<Real> &v,
199 const ROL::Vector<Real> &u,
200 const ROL::Vector<Real> &z,
201 Real &tol) {
202 ROL::Ptr<Tpetra::MultiVector<> > hvp
203 = (dynamic_cast<ROL::TpetraMultiVector<Real>&>(hv)).getVector();
204 ROL::Ptr<const Tpetra::MultiVector<> > vp
205 = (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(v)).getVector();
206 if ( useFU_ ) {
207 hv.zero();
208 }
209 else {
210 ROL::Ptr<const Tpetra::MultiVector<> > up
211 = (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(u)).getVector();
212 ROL::Ptr<Tpetra::MultiVector<> > Fvp
213 = ROL::makePtr<Tpetra::MultiVector<>>(vp->getMap(), 1);
214 filter_->apply(Fvp, vp);
215 ROL::Ptr<Tpetra::MultiVector<> > tmp
216 = ROL::makePtr<Tpetra::MultiVector<>>(hvp->getMap(), 1);
217 data_->ApplyAdjointHessian22ToVec (tmp, up, Fvp, up);
218 filter_->apply(hvp, tmp);
219 }
220 }
221
222 };
223
224 #endif