Debian Codegraph @

packages/rol/example/PDE-OPT/topo-opt/multimat/src/compliance_robj.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 MULTIMAT_COMPLIANCE_ROBJ_H
11 #define MULTIMAT_COMPLIANCE_ROBJ_H
12
13 #include "ROL_Objective.hpp"
14 #include "ROL_VectorController.hpp"
15 #include "ROL_SecantFactory.hpp"
16 #include "../../../TOOLS/pde.hpp"
17 #include "../../../TOOLS/assembler.hpp"
18 #include "../../../TOOLS/solver.hpp"
19 #include "../../../TOOLS/pdevector.hpp"
20
21 template <class Real>
22 class MultiMat_Compliance_Objective : public ROL::Objective<Real> {
23 private:
24 // Elasticity PDE
25 const ROL::Ptr<PDE<Real>> pde_;
26 ROL::Ptr<Assembler<Real>> assembler_;
27 ROL::Ptr<Solver<Real>> solver_, solver_cache_, solver_tmp_;
28 ROL::Ptr<Tpetra::CrsMatrix<>> matJ1_, matJ1_cache_, matJ1_tmp_;
29 ROL::Ptr<Tpetra::CrsMatrix<>> matJ2_, matJ2_cache_, matJ2_tmp_;
30 ROL::Ptr<Tpetra::CrsMatrix<>> matH22_;
31 bool assembleJ1_, assembleJ2_, assembleH22_, assembleF_;
32
33 // Output information
34 const std::string uname_, dname_;
35
36 // Vector Storage
37 ROL::Ptr<Tpetra::MultiVector<>> F_data_, state_sens_data_;
38 ROL::Ptr<Tpetra::MultiVector<>> dstat_data_, dctrl_data_;
39 ROL::Ptr<ROL::Vector<Real>> state_, dctrl_;
40 ROL::Ptr<ROL::VectorController<Real>> stateStore_;
41
42 bool storage_;
43 Real cmpScale_;
44 bool nuke_;
45 int printFreq_;
46
47 unsigned nstat_, nsens_;
48 unsigned nupda_, nfval_, ngrad_, nhess_, nprec_;
49 unsigned napJ1_, nasJ1_, napJ2_, nasJ2_, napH2_, nasH2_, nasLo_;
50
51 public:
52 MultiMat_Compliance_Objective(
53 const ROL::Ptr<PDE<Real>> &pde,
54 const ROL::Ptr<MeshManager<Real>> &mesh,
55 const ROL::Ptr<const Teuchos::Comm<int>> &comm,
56 ROL::ParameterList &list,
57 std::ostream &stream = std::cout,
58 std::string uname = "state",
59 std::string dname = "density")
60 : pde_(pde), assembleJ1_(true), assembleJ2_(true),
61 assembleH22_(true), assembleF_(true),
62 uname_(uname), dname_(dname),
63 nstat_(0), nsens_(0),
64 nupda_(0), nfval_(0), ngrad_(0), nhess_(0), nprec_(0),
65 napJ1_(0), nasJ1_(0), napJ2_(0), nasJ2_(0), napH2_(0), nasH2_(0), nasLo_(0) {
66 // Elasticity PDE
67 assembler_ = ROL::makePtr<Assembler<Real>>(pde_->getFields(),
68 pde_->getFields2(),
69 mesh,comm,list,stream);
70 assembler_->setCellNodes(*pde_);
71 solver_ = ROL::makePtr<Solver<Real>>(list.sublist("Solver"));
72 solver_cache_ = ROL::makePtr<Solver<Real>>(list.sublist("Solver"));
73
74 // Vector storage
75 F_data_ = assembler_->createResidualVector();
76 state_sens_data_ = assembler_->createStateVector();
77 dstat_data_ = assembler_->createResidualVector();
78 dctrl_data_ = assembler_->createControlVector();
79 state_ = ROL::makePtr<PDE_PrimalSimVector<Real>>(assembler_->createStateVector(),pde_,assembler_,list);
80 dctrl_ = ROL::makePtr<PDE_DualOptVector<Real>>(dctrl_data_,pde_,assembler_,list);
81 stateStore_ = ROL::makePtr<ROL::VectorController<Real>>();
82
83 storage_ = list.sublist("Problem").get("Use state storage", true);
84 cmpScale_ = list.sublist("Problem").get("Compliance Scaling", 1.0);
85 nuke_ = list.sublist("Problem").get("Use Basic Update",false);
86 printFreq_ = list.sublist("Problem").get("Output Frequency",0);
87
88 initialize();
89 }
90
91 const ROL::Ptr<Assembler<Real>> getAssembler(void) const {
92 return assembler_;
93 }
94
95 void update(const ROL::Vector<Real> &z, ROL::UpdateType type, int iter = -1) {
96 nupda_++;
97 stateStore_->objectiveUpdate(type);
98 if (nuke_) {
99 update_temp(z,iter);
100 }
101 else {
102 switch (type) {
103 case ROL::UpdateType::Initial: update_initial(z,iter); break;
104 case ROL::UpdateType::Accept: update_accept(z,iter); break;
105 case ROL::UpdateType::Revert: update_revert(z,iter); break;
106 case ROL::UpdateType::Trial: update_trial(z,iter); break;
107 case ROL::UpdateType::Temp: update_temp(z,iter); break;
108 }
109 }
110 // Print
111 if (printFreq_ > 0 && iter%printFreq_ == 0) {
112 std::stringstream ufile, zfile;
113 ufile << uname_ << "_" << iter << ".txt";
114 zfile << dname_ << "_" << iter << ".txt";
115 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
116 ROL::Ptr<const Tpetra::MultiVector<>> u_data = getConstField(*state_);
117 assembler_->outputTpetraVector(u_data, ufile.str());
118 assembler_->outputTpetraVector(z_data, zfile.str());
119 }
120 }
121
122 void update( const ROL::Vector<Real> &z, bool flag = true, int iter = -1 ) {
123 nupda_++;
124 stateStore_->objectiveUpdate(true);
125 if (nuke_) {
126 update_temp(z,iter);
127 }
128 else {
129 // Trial: flag = false, iter = -1
130 // Check: flag = true, iter = -1
131 // Reject: flag = false, iter > -1
132 // Accept: flag = true, iter > -1
133 if (flag) {
134 if (iter > -1) {
135 update_accept(z,iter);
136 }
137 else {
138 update_temp(z,iter);
139 }
140 }
141 else {
142 if (iter > -1) {
143 update_revert(z,iter);
144 }
145 else {
146 update_trial(z,iter);
147 }
148 }
149 }
150 }
151
152 Real normalize(const ROL::Vector<Real> &z, Real &tol) {
153 update(z,ROL::UpdateType::Temp);
154 Real val = value(z,tol);
155 cmpScale_ /= val;
156 return cmpScale_;
157 }
158
159 void printToFile(const ROL::Vector<Real> &z, std::ostream &stream = std::cout,
160 const std::string ufile = "state.txt", const std::string dfile = "density.txt") {
161 update(z,ROL::UpdateType::Temp);
162 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
163 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(*state_);
164 solve_state_equation(u_data, z_data);
165 assembler_->outputTpetraVector(u_data, ufile);
166 assembler_->outputTpetraVector(z_data, dfile);
167 assembler_->printMeshData(stream);
168 }
169
170 Real value( const ROL::Vector<Real> &z, Real &tol ) {
171 nfval_++;
172 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
173 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(*state_);
174 // Solve state equation
175 solve_state_equation(*state_,z);
176 // Compute compliance
177 Teuchos::Array<Real> cmp(1,0);
178 F_data_->dot(*u_data, cmp.view(0,1));
179 return cmpScale_*cmp[0];
180 }
181
182 void gradient( ROL::Vector<Real> &g, const ROL::Vector<Real> &z, Real &tol ) {
183 ngrad_++;
184 ROL::Ptr<Tpetra::MultiVector<>> g_data = getField(g);
185 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
186 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(*state_);
187 // Solve state equation
188 solve_state_equation(*state_,z);
189 // Apply adjoint density jacobian
190 assembleJ2(u_data, z_data);
191 applyJacobian2(g_data, u_data, true);
192 g_data->scale(-cmpScale_);
193 }
194
195 void hessVec( ROL::Vector<Real> &hv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &z, Real &tol ) {
196 nhess_++;
197 ROL::Ptr<Tpetra::MultiVector<>> h_data = getField(hv);
198 ROL::Ptr<const Tpetra::MultiVector<>> v_data = getConstField(v);
199 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
200 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(*state_);
201 // Solve state equation
202 solve_state_equation(*state_,z);
203 // Solve state sensitivity equation
204 assembleJ2(u_data, z_data);
205 solve_state_sensitivity(state_sens_data_, v_data);
206 // Apply density hessian to direction
207 assembleH22(u_data, z_data);
208 applyHessian22(h_data, v_data);
209 // Apply adjoint density jacobian to state sensitivity
210 applyJacobian2(dctrl_data_, state_sens_data_, true);
211 h_data->update(static_cast<Real>(2)*cmpScale_,*dctrl_data_,-cmpScale_);
212 }
213
214 /** \brief Apply a reduced Hessian preconditioner.
215 */
216 virtual void precond( ROL::Vector<Real> &Pv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &z, Real &tol ) {
217 nprec_++;
218 Pv.set(v.dual());
219 }
220
221 void summarize(std::ostream &stream) const {
222 stream << std::endl;
223 stream << std::string(114,'=') << std::endl;
224 stream << " Objective_Compliance::summarize" << std::endl;
225 stream << " Number of calls to update: " << nupda_ << std::endl;
226 stream << " Number of calls to value: " << nfval_ << std::endl;
227 stream << " Number of calls to gradient: " << ngrad_ << std::endl;
228 stream << " Number of calls to hessVec: " << nhess_ << std::endl;
229 stream << " Number of calls to precond: " << nprec_ << std::endl;
230 stream << " Number of state solves: " << nstat_ << std::endl;
231 stream << " Number of state sensitivity solves: " << nsens_ << std::endl;
232 stream << " Number of load assemblies: " << nasLo_ << std::endl;
233 stream << " Number of Jacobian_1 assemblies: " << nasJ1_ << std::endl;
234 stream << " Number of Jacobian_1 sovles: " << napJ1_ << std::endl;
235 stream << " Number of Jacobian_2 assemblies: " << nasJ2_ << std::endl;
236 stream << " Number of Jacobian_2 applies: " << napJ2_ << std::endl;
237 stream << " Number of Hessian_22 assemblies: " << nasH2_ << std::endl;
238 stream << " Number of Hessian_22 applies: " << napH2_ << std::endl;
239 stream << std::string(114,'=') << std::endl;
240 stream << std::endl;
241 }
242
243 // For parametrized (stochastic) objective functions and constraints
244 public:
245 void setParameter(const std::vector<Real> &param) {
246 ROL::Objective<Real>::setParameter(param);
247 pde_->setParameter(param);
248 assembleF_ = true;
249 }
250
251 private:
252
253 ROL::Ptr<const Tpetra::MultiVector<>> getConstField(const ROL::Vector<Real> &x) const {
254 ROL::Ptr<const Tpetra::MultiVector<>> xp;
255 try {
256 xp = dynamic_cast<const ROL::TpetraMultiVector<Real>&>(x).getVector();
257 }
258 catch (std::exception &e) {
259 try {
260 ROL::Ptr<const ROL::TpetraMultiVector<Real>> xvec
261 = dynamic_cast<const PDE_OptVector<Real>&>(x).getField();
262 if (xvec == ROL::nullPtr) {
263 xp = ROL::nullPtr;
264 }
265 else {
266 xp = xvec->getVector();
267 }
268 }
269 catch (std::exception &ee) {
270 xp = ROL::nullPtr;
271 }
272 }
273 return xp;
274 }
275
276 ROL::Ptr<Tpetra::MultiVector<>> getField(ROL::Vector<Real> &x) const {
277 ROL::Ptr<Tpetra::MultiVector<>> xp;
278 try {
279 xp = dynamic_cast<ROL::TpetraMultiVector<Real>&>(x).getVector();
280 }
281 catch (std::exception &e) {
282 try {
283 ROL::Ptr<ROL::TpetraMultiVector<Real>> xvec
284 = dynamic_cast<PDE_OptVector<Real>&>(x).getField();
285 if ( xvec == ROL::nullPtr ) {
286 xp = ROL::nullPtr;
287 }
288 else {
289 xp = xvec->getVector();
290 }
291 }
292 catch (std::exception &ee) {
293 xp = ROL::nullPtr;
294 }
295 }
296 return xp;
297 }
298
299 void initialize(void) {
300 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(*state_);
301 u_data->putScalar(0.0);
302 dctrl_data_->putScalar(1.0);
303 assembleJ1_ = true;
304 assembleJ2_ = true;
305 assembleJ1(u_data, dctrl_data_,false);
306 assembleJ2(u_data, dctrl_data_);
307 matJ1_tmp_ = matJ1_;
308 matJ2_tmp_ = matJ2_;
309 matJ1_ = matJ1_cache_;
310 matJ2_ = matJ2_cache_;
311 assembleJ1_ = true;
312 assembleJ2_ = true;
313 assembleJ1(u_data, dctrl_data_,false);
314 assembleJ2(u_data, dctrl_data_);
315 matJ1_cache_ = matJ1_;
316 matJ2_cache_ = matJ2_;
317 solver_tmp_ = solver_;
318 solver_ = solver_cache_;
319 assembleJ1_ = true;
320 assembleJ2_ = true;
321 // Upon exit, matJ1, matJ2 and solver are set to their cached versions
322 }
323
324 // Assemble load vector
325 void assembleF(void) {
326 if (assembleF_) {
327 nasLo_++;
328 ROL::Ptr<Tpetra::MultiVector<>> u0 = assembler_->createStateVector();
329 ROL::Ptr<Tpetra::MultiVector<>> z1 = assembler_->createControlVector();
330 u0->putScalar(0.0);
331 z1->putScalar(1.0);
332 assembler_->assemblePDEResidual(F_data_,pde_,u0,z1);
333 F_data_->scale(static_cast<Real>(-1));
334 assembleF_ = false;
335 }
336 }
337
338 void assembleJ2(const ROL::Ptr<const Tpetra::MultiVector<>> &u, const ROL::Ptr<const Tpetra::MultiVector<>> &z) {
339 if (assembleJ2_) {
340 nasJ2_++;
341 assembler_->assemblePDEJacobian2(matJ2_,pde_,u,z);
342 assembleJ2_ = false;
343 }
344 }
345
346 void applyJacobian2(ROL::Ptr<Tpetra::MultiVector<>> &Jv, const ROL::Ptr<const Tpetra::MultiVector<>> &v,
347 const bool transpose) {
348 napJ2_++;
349 if (transpose) {
350 matJ2_->apply(*v,*Jv,Teuchos::TRANS);
351 }
352 else {
353 matJ2_->apply(*v,*Jv);
354 }
355 }
356
357 void assembleH22(const ROL::Ptr<const Tpetra::MultiVector<>> &u, const ROL::Ptr<const Tpetra::MultiVector<>> &z) {
358 if (assembleH22_) {
359 nasH2_++;
360 assembler_->assemblePDEHessian22(matH22_,pde_,u,u,z);
361 assembleH22_ = false;
362 }
363 }
364
365 void applyHessian22(ROL::Ptr<Tpetra::MultiVector<>> &Hv, const ROL::Ptr<const Tpetra::MultiVector<>> &v) {
366 matH22_->apply(*v,*Hv);
367 napH2_++;
368 }
369
370 void assembleJ1(const ROL::Ptr<const Tpetra::MultiVector<>> &u, const ROL::Ptr<const Tpetra::MultiVector<>> &z, bool setSolve = true) {
371 if (assembleJ1_) {
372 nasJ1_++;
373 assembler_->assemblePDEJacobian1(matJ1_,pde_,u,z);
374 if (setSolve) {
375 solver_->setA(matJ1_);
376 }
377 assembleJ1_ = false;
378 }
379 }
380
381 void solve(ROL::Ptr<Tpetra::MultiVector<>> &x, const ROL::Ptr<const Tpetra::MultiVector<>> &b) {
382 napJ1_++;
383 solver_->solve(x,b,false);
384 }
385
386 void solve_state_equation(ROL::Vector<Real> &u, const ROL::Vector<Real> &z) {
387 bool isComputed = false;
388 if (storage_) {
389 isComputed = stateStore_->get(u, ROL::Objective<Real>::getParameter());
390 }
391 if (!isComputed || !storage_) {
392 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
393 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(u);
394 solve_state_equation(u_data,z_data);
395 if (storage_) {
396 stateStore_->set(u, ROL::Objective<Real>::getParameter());
397 }
398 }
399 }
400
401 void solve_state_equation(ROL::Ptr<Tpetra::MultiVector<>> &u, const ROL::Ptr<const Tpetra::MultiVector<>> &z) {
402 nstat_++;
403 assembleF(); assembleJ1(u,z,true);
404 solve(u,F_data_);
405 }
406
407 void solve_state_sensitivity(ROL::Ptr<Tpetra::MultiVector<>> &s, const ROL::Ptr<const Tpetra::MultiVector<>> &v) {
408 // This assumes that assembleJ1 and assemble J2 were called
409 nsens_++;
410 applyJacobian2(dstat_data_, v, false);
411 solve(s, dstat_data_);
412 }
413
414 void update_initial( const ROL::Vector<Real> &z, int iter ) {
415 assembleJ1_ = true;
416 assembleJ2_ = true;
417 assembleH22_ = true;
418 }
419
420 void update_accept( const ROL::Vector<Real> &z, int iter ) {
421 // Push trial state storage into base state storage.
422 solver_tmp_ = solver_cache_;
423 solver_cache_ = solver_;
424 matJ1_tmp_ = matJ1_cache_;
425 matJ1_cache_ = matJ1_;
426 matJ2_tmp_ = matJ2_cache_;
427 matJ2_cache_ = matJ2_;
428 assembleJ1_ = false;
429 // If assembleJ2 was not called, then assembleJ2_ = true
430 assembleH22_ = true;
431 }
432
433 void update_temp( const ROL::Vector<Real> &z, int iter ) {
434 assembleJ1_ = true;
435 assembleJ2_ = true;
436 assembleH22_ = true;
437 }
438
439 void update_trial( const ROL::Vector<Real> &z, int iter ) {
440 solver_ = solver_tmp_;
441 matJ1_ = matJ1_tmp_;
442 matJ2_ = matJ2_tmp_;
443 assembleJ1_ = true;
444 assembleJ2_ = true;
445 assembleH22_ = false;
446 }
447
448 void update_revert( const ROL::Vector<Real> &z, int iter ) {
449 solver_ = solver_cache_;
450 matJ1_ = matJ1_cache_;
451 matJ2_ = matJ2_cache_;
452 assembleJ1_ = false;
453 // If assembleJ2 was not called, then assembleJ2_ = true
454 assembleH22_ = false;
455 // If gradient was not called, then computeGrad_ = true
456 }
457 }; // class Compliance_Objective
458
459 #endif