Debian Codegraph @

packages/rol/example/PDE-OPT/published/TRSPG_Kouri2021/elasticity/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 TOPOT_COMPLIANCE_ROBJ_H
11 #define TOPOT_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 TopOptComplianceObjective : 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 ROL::ParameterList solverList_;
33 Real tol_, maxtol_;
34 bool useInexact_;
35
36 // Output information
37 const std::string uname_, dname_;
38
39 // Vector Storage
40 ROL::Ptr<Tpetra::MultiVector<>> F_data_, state_sens_data_;
41 ROL::Ptr<Tpetra::MultiVector<>> dstat_data_, dctrl_data_;
42 ROL::Ptr<ROL::Vector<Real>> state_, dctrl_;
43 ROL::Ptr<ROL::VectorController<Real>> stateStore_;
44
45 bool storage_;
46 Real cmpScale_;
47 bool nuke_;
48 int printFreq_;
49
50 unsigned nstat_, nsens_;
51 unsigned nupda_, nfval_, ngrad_, nhess_, nprec_;
52 unsigned napJ1_, nasJ1_, napJ2_, nasJ2_, napH2_, nasH2_, nasLo_;
53
54 // Hessian Approximation Data
55 std::string hessAppr_;
56 Real H2scale_;
57 ROL::Ptr<ROL::Secant<Real>> secant_;
58 ROL::Ptr<Tpetra::MultiVector<>> s_data_;
59 ROL::Ptr<ROL::Vector<Real>> g_new_, g_old_, s_, g_;
60 bool computeGrad_;
61
62 public:
63 TopOptComplianceObjective(
64 const ROL::Ptr<PDE<Real>> &pde,
65 const ROL::Ptr<MeshManager<Real>> &mesh,
66 const ROL::Ptr<const Teuchos::Comm<int>> &comm,
67 ROL::ParameterList &list,
68 std::ostream &stream = std::cout,
69 std::string uname = "state",
70 std::string dname = "density")
71 : pde_(pde), assembleJ1_(true), assembleJ2_(true),
72 assembleH22_(true), assembleF_(true), tol_(1e-2), maxtol_(1e-2),
73 uname_(uname), dname_(dname),
74 nstat_(0), nsens_(0),
75 nupda_(0), nfval_(0), ngrad_(0), nhess_(0), nprec_(0),
76 napJ1_(0), nasJ1_(0), napJ2_(0), nasJ2_(0), napH2_(0), nasH2_(0), nasLo_(0), H2scale_(1) {
77 // Elasticity PDE
78 assembler_ = ROL::makePtr<Assembler<Real>>(pde_->getFields(),
79 pde_->getFields2(),
80 mesh,comm,list,stream);
81 assembler_->setCellNodes(*pde_);
82 solver_ = ROL::makePtr<Solver<Real>>(list.sublist("Solver"));
83 solver_cache_ = ROL::makePtr<Solver<Real>>(list.sublist("Solver"));
84 solverList_ = list.sublist("Solver");
85 useInexact_ = list.sublist("Problem").get("Use Inexact Linear Solves",false);
86
87 // Vector storage
88 F_data_ = assembler_->createResidualVector();
89 state_sens_data_ = assembler_->createStateVector();
90 dstat_data_ = assembler_->createResidualVector();
91 dctrl_data_ = assembler_->createControlVector();
92 state_ = ROL::makePtr<PDE_PrimalSimVector<Real>>(assembler_->createStateVector(),pde_,assembler_,list);
93 dctrl_ = ROL::makePtr<PDE_DualOptVector<Real>>(dctrl_data_,pde_,assembler_,list);
94 stateStore_ = ROL::makePtr<ROL::VectorController<Real>>();
95
96 storage_ = list.sublist("Problem").get("Use state storage", true);
97 cmpScale_ = list.sublist("Problem").get("Compliance Scaling", 1.0);
98 hessAppr_ = list.sublist("Problem").get("Hessian Approximation", "None");
99 nuke_ = list.sublist("Problem").get("Use Basic Update",false);
100 printFreq_ = list.sublist("Problem").get("Output Frequency",0);
101
102 computeGrad_ = true;
103 if (hessAppr_ == "Secant") {
104 s_data_ = assembler_->createControlVector();
105 s_ = ROL::makePtr<PDE_PrimalOptVector<Real>>(s_data_,pde_,assembler_,list);
106 g_ = ROL::makePtr<PDE_DualOptVector<Real>>(assembler_->createControlVector(),pde_,assembler_,list);
107 g_old_ = ROL::makePtr<PDE_DualOptVector<Real>>(assembler_->createControlVector(),pde_,assembler_,list);
108 g_new_ = ROL::makePtr<PDE_DualOptVector<Real>>(assembler_->createControlVector(),pde_,assembler_,list);
109 secant_ = ROL::SecantFactory<Real>(list);
110 }
111
112 initialize();
113 }
114
115 const ROL::Ptr<Assembler<Real>> getAssembler(void) const {
116 return assembler_;
117 }
118
119 void update(const ROL::Vector<Real> &z, ROL::UpdateType type, int iter = -1) {
120 nupda_++;
121 stateStore_->objectiveUpdate(type);
122 if (nuke_) {
123 update_temp(z,iter);
124 }
125 else {
126 switch (type) {
127 case ROL::UpdateType::Initial: update_initial(z,iter); break;
128 case ROL::UpdateType::Accept: update_accept(z,iter); break;
129 case ROL::UpdateType::Revert: update_revert(z,iter); break;
130 case ROL::UpdateType::Trial: update_trial(z,iter); break;
131 case ROL::UpdateType::Temp: update_temp(z,iter); break;
132 }
133 }
134 // Print
135 if (printFreq_ > 0 && iter%printFreq_ == 0) {
136 std::stringstream ufile, zfile;
137 ufile << uname_ << "_" << iter << ".txt";
138 zfile << dname_ << "_" << iter << ".txt";
139 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
140 ROL::Ptr<const Tpetra::MultiVector<>> u_data = getConstField(*state_);
141 assembler_->outputTpetraVector(u_data, ufile.str());
142 assembler_->outputTpetraVector(z_data, zfile.str());
143 }
144 }
145
146 void update( const ROL::Vector<Real> &z, bool flag = true, int iter = -1 ) {
147 nupda_++;
148 stateStore_->objectiveUpdate(true);
149 if (nuke_) {
150 update_temp(z,iter);
151 }
152 else {
153 // Trial: flag = false, iter = -1
154 // Check: flag = true, iter = -1
155 // Reject: flag = false, iter > -1
156 // Accept: flag = true, iter > -1
157 if (flag) {
158 if (iter > -1) {
159 update_accept(z,iter);
160 }
161 else {
162 update_temp(z,iter);
163 }
164 }
165 else {
166 if (iter > -1) {
167 update_revert(z,iter);
168 }
169 else {
170 update_trial(z,iter);
171 }
172 }
173 }
174 }
175
176 Real normalize(const ROL::Vector<Real> &z, Real &tol) {
177 update(z,ROL::UpdateType::Temp);
178 Real val = value(z,tol);
179 cmpScale_ /= val;
180 return cmpScale_;
181 }
182
183 void printToFile(const ROL::Vector<Real> &z, std::ostream &stream = std::cout,
184 const std::string ufile = "state.txt", const std::string dfile = "density.txt") {
185 Real tol(1e-2*std::sqrt(ROL::ROL_EPSILON<Real>()));
186 update(z,ROL::UpdateType::Temp);
187 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
188 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(*state_);
189 solve_state_equation(u_data, z_data, tol);
190 assembler_->outputTpetraVector(u_data, ufile);
191 assembler_->outputTpetraVector(z_data, dfile);
192 assembler_->printMeshData(stream);
193 }
194
195 void solveState(ROL::Vector<Real> &u, const ROL::Vector<Real> &z) {
196 Real tol(1e-2*std::sqrt(ROL::ROL_EPSILON<Real>()));
197 update(z,ROL::UpdateType::Temp);
198 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
199 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(u);
200 solve_state_equation(u_data, z_data, tol);
201 }
202
203 Real value( const ROL::Vector<Real> &z, Real &tol ) {
204 nfval_++;
205 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
206 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(*state_);
207 // Solve state equation
208 solve_state_equation(*state_,z,tol);
209 // Compute compliance
210 Teuchos::Array<Real> cmp(1,0);
211 F_data_->dot(*u_data, cmp.view(0,1));
212 return cmpScale_*cmp[0];
213 }
214
215 void gradient( ROL::Vector<Real> &g, const ROL::Vector<Real> &z, Real &tol ) {
216 ngrad_++;
217 if (hessAppr_ == "Secant") {
218 // Will not work for stochastic problems
219 if (computeGrad_) {
220 ROL::Ptr<Tpetra::MultiVector<>> g_data = getField(g);
221 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
222 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(*state_);
223 // Solve state equation
224 solve_state_equation(*state_,z,tol);
225 // Apply adjoint density jacobian
226 assembleJ2(u_data, z_data);
227 applyJacobian2(g_data, u_data, true);
228 g_data->scale(-cmpScale_);
229
230 g_->set(g);
231 computeGrad_ = false;
232 }
233 else {
234 g.set(*g_);
235 }
236 }
237 else {
238 ROL::Ptr<Tpetra::MultiVector<>> g_data = getField(g);
239 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
240 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(*state_);
241 // Solve state equation
242 solve_state_equation(*state_,z,tol);
243 // Apply adjoint density jacobian
244 assembleJ2(u_data, z_data);
245 applyJacobian2(g_data, u_data, true);
246 g_data->scale(-cmpScale_);
247 }
248 }
249
250 void hessVec( ROL::Vector<Real> &hv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &z, Real &tol ) {
251 nhess_++;
252 if (hessAppr_ == "Zero") {
253 hv.zero();
254 }
255 else if (hessAppr_ == "Positive Riesz") {
256 hv.set(v.dual());
257 }
258 else if (hessAppr_ == "Negative Riesz") {
259 hv.set(v.dual());
260 hv.scale(static_cast<Real>(-1));
261 }
262 else if (hessAppr_ == "Negative") {
263 ROL::Ptr<Tpetra::MultiVector<>> h_data = getField(hv);
264 ROL::Ptr<const Tpetra::MultiVector<>> v_data = getConstField(v);
265 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
266 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(*state_);
267 // Solve state equation
268 solve_state_equation(*state_,z,tol_);
269 // Apply density hessian to direction
270 assembleH22(u_data, z_data);
271 applyHessian22(h_data, v_data);
272 h_data->scale(-cmpScale_);
273 }
274 else if (hessAppr_ == "Positive") {
275 ROL::Ptr<Tpetra::MultiVector<>> h_data = getField(hv);
276 ROL::Ptr<const Tpetra::MultiVector<>> v_data = getConstField(v);
277 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
278 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(*state_);
279 // Solve state equation
280 solve_state_equation(*state_,z,tol_);
281 // Solve state sensitivity equation
282 assembleJ2(u_data, z_data);
283 solve_state_sensitivity(state_sens_data_, v_data, tol_);
284 // Apply adjoint density jacobian to state sensitivity
285 applyJacobian2(h_data, state_sens_data_, true);
286 h_data->scale(static_cast<Real>(2)*cmpScale_);
287 }
288 else if (hessAppr_ == "No Solve") {
289 ROL::Ptr<Tpetra::MultiVector<>> h_data = getField(hv);
290 ROL::Ptr<const Tpetra::MultiVector<>> v_data = getConstField(v);
291 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
292 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(*state_);
293 // Solve state equation
294 solve_state_equation(*state_,z,tol_);
295 // Apply density hessian to direction
296 assembleH22(u_data, z_data);
297 applyHessian22(h_data, v_data);
298 // Approximate positive term
299 assembleJ2(u_data, z_data);
300 applyJacobian2(dstat_data_, v_data, false);
301 applyJacobian2(dctrl_data_, dstat_data_, true);
302 // Approximation of inv(K) by aI where a solves min||aF-U||
303 // or b=1/a solves min||F-bU||
304 Teuchos::Array<Real> UdotF(1,0), UdotU(1,0);
305 u_data->dot(*F_data_,UdotF.view(0,1));
306 F_data_->dot(*F_data_,UdotU.view(0,1));
307 H2scale_ = UdotF[0]/UdotU[0];
308 //u_data->dot(*u_data,UdotU.view(0,1));
309 //H2scale_ = UdotU[0]/UdotF[0];
310 h_data->update(static_cast<Real>(2)*cmpScale_*H2scale_,*dctrl_data_,-cmpScale_);
311 }
312 else if (hessAppr_ == "Secant") {
313 // Will not work for stochastic problems
314 ROL::Ptr<Tpetra::MultiVector<>> h_data = getField(hv);
315 ROL::Ptr<const Tpetra::MultiVector<>> v_data = getConstField(v);
316 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
317 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(*state_);
318 // Solve state equation
319 solve_state_equation(*state_,z,tol_);
320 // Apply density hessian to direction
321 assembleH22(u_data, z_data);
322 applyHessian22(h_data, v_data);
323 // Apply secant approximation of positive term
324 secant_->applyB(*dctrl_, v);
325 h_data->update(static_cast<Real>(2)*cmpScale_,*dctrl_data_,-cmpScale_);
326 }
327 else {
328 ROL::Ptr<Tpetra::MultiVector<>> h_data = getField(hv);
329 ROL::Ptr<const Tpetra::MultiVector<>> v_data = getConstField(v);
330 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
331 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(*state_);
332 // Solve state equation
333 solve_state_equation(*state_,z,tol_);
334 // Solve state sensitivity equation
335 assembleJ2(u_data, z_data);
336 solve_state_sensitivity(state_sens_data_, v_data, tol_);
337 // Apply density hessian to direction
338 assembleH22(u_data, z_data);
339 applyHessian22(h_data, v_data);
340 // Apply adjoint density jacobian to state sensitivity
341 applyJacobian2(dctrl_data_, state_sens_data_, true);
342 h_data->update(static_cast<Real>(2)*cmpScale_,*dctrl_data_,-cmpScale_);
343 }
344 }
345
346 /** \brief Apply a reduced Hessian preconditioner.
347 */
348 virtual void precond( ROL::Vector<Real> &Pv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &z, Real &tol ) {
349 nprec_++;
350 Pv.set(v.dual());
351 }
352
353 void summarize(std::ostream &stream) const {
354 stream << std::endl;
355 stream << std::string(114,'=') << std::endl;
356 stream << " Objective_Compliance::summarize" << std::endl;
357 stream << " Number of calls to update: " << nupda_ << std::endl;
358 stream << " Number of calls to value: " << nfval_ << std::endl;
359 stream << " Number of calls to gradient: " << ngrad_ << std::endl;
360 stream << " Number of calls to hessVec: " << nhess_ << std::endl;
361 stream << " Number of calls to precond: " << nprec_ << std::endl;
362 stream << " Number of state solves: " << nstat_ << std::endl;
363 stream << " Number of state sensitivity solves: " << nsens_ << std::endl;
364 stream << " Number of load assemblies: " << nasLo_ << std::endl;
365 stream << " Number of Jacobian_1 assemblies: " << nasJ1_ << std::endl;
366 stream << " Number of Jacobian_1 sovles: " << napJ1_ << std::endl;
367 stream << " Number of Jacobian_2 assemblies: " << nasJ2_ << std::endl;
368 stream << " Number of Jacobian_2 applies: " << napJ2_ << std::endl;
369 stream << " Number of Hessian_22 assemblies: " << nasH2_ << std::endl;
370 stream << " Number of Hessian_22 applies: " << napH2_ << std::endl;
371 stream << std::string(114,'=') << std::endl;
372 stream << std::endl;
373 }
374
375 void reset(void) {
376 nupda_ = 0; nfval_ = 0; ngrad_ = 0; nhess_ = 0; nprec_ = 0; nstat_ = 0; nsens_ = 0;
377 nasLo_ = 0; nasJ1_ = 0; napJ1_ = 0; nasJ2_ = 0; napJ2_ = 0; nasH2_ = 0; napH2_ = 0;
378 }
379
380 // For parametrized (stochastic) objective functions and constraints
381 public:
382 void setParameter(const std::vector<Real> &param) {
383 ROL::Objective<Real>::setParameter(param);
384 pde_->setParameter(param);
385 assembleF_ = true;
386 }
387
388 private:
389
390 ROL::Ptr<const Tpetra::MultiVector<>> getConstField(const ROL::Vector<Real> &x) const {
391 ROL::Ptr<const Tpetra::MultiVector<>> xp;
392 try {
393 xp = dynamic_cast<const ROL::TpetraMultiVector<Real>&>(x).getVector();
394 }
395 catch (std::exception &e) {
396 try {
397 ROL::Ptr<const ROL::TpetraMultiVector<Real>> xvec
398 = dynamic_cast<const PDE_OptVector<Real>&>(x).getField();
399 if (xvec == ROL::nullPtr) {
400 xp = ROL::nullPtr;
401 }
402 else {
403 xp = xvec->getVector();
404 }
405 }
406 catch (std::exception &ee) {
407 xp = ROL::nullPtr;
408 }
409 }
410 return xp;
411 }
412
413 ROL::Ptr<Tpetra::MultiVector<>> getField(ROL::Vector<Real> &x) const {
414 ROL::Ptr<Tpetra::MultiVector<>> xp;
415 try {
416 xp = dynamic_cast<ROL::TpetraMultiVector<Real>&>(x).getVector();
417 }
418 catch (std::exception &e) {
419 try {
420 ROL::Ptr<ROL::TpetraMultiVector<Real>> xvec
421 = dynamic_cast<PDE_OptVector<Real>&>(x).getField();
422 if ( xvec == ROL::nullPtr ) {
423 xp = ROL::nullPtr;
424 }
425 else {
426 xp = xvec->getVector();
427 }
428 }
429 catch (std::exception &ee) {
430 xp = ROL::nullPtr;
431 }
432 }
433 return xp;
434 }
435
436 void initialize(void) {
437 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(*state_);
438 u_data->putScalar(0.0);
439 dctrl_data_->putScalar(1.0);
440 assembleJ1_ = true;
441 assembleJ2_ = true;
442 assembleJ1(u_data, dctrl_data_,false);
443 assembleJ2(u_data, dctrl_data_);
444 matJ1_tmp_ = matJ1_;
445 matJ2_tmp_ = matJ2_;
446 matJ1_ = matJ1_cache_;
447 matJ2_ = matJ2_cache_;
448 assembleJ1_ = true;
449 assembleJ2_ = true;
450 assembleJ1(u_data, dctrl_data_,false);
451 assembleJ2(u_data, dctrl_data_);
452 matJ1_cache_ = matJ1_;
453 matJ2_cache_ = matJ2_;
454 solver_tmp_ = solver_;
455 solver_ = solver_cache_;
456 assembleJ1_ = true;
457 assembleJ2_ = true;
458 // Upon exit, matJ1, matJ2 and solver are set to their cached versions
459 }
460
461 // Assemble load vector
462 void assembleF(void) {
463 if (assembleF_) {
464 nasLo_++;
465 ROL::Ptr<Tpetra::MultiVector<>> u0 = assembler_->createStateVector();
466 ROL::Ptr<Tpetra::MultiVector<>> z1 = assembler_->createControlVector();
467 u0->putScalar(0.0);
468 z1->putScalar(1.0);
469 assembler_->assemblePDEResidual(F_data_,pde_,u0,z1);
470 F_data_->scale(static_cast<Real>(-1));
471 assembleF_ = false;
472 }
473 }
474
475 void assembleJ2(const ROL::Ptr<const Tpetra::MultiVector<>> &u, const ROL::Ptr<const Tpetra::MultiVector<>> &z) {
476 if (assembleJ2_) {
477 nasJ2_++;
478 assembler_->assemblePDEJacobian2(matJ2_,pde_,u,z);
479 assembleJ2_ = false;
480 }
481 }
482
483 void applyJacobian2(ROL::Ptr<Tpetra::MultiVector<>> &Jv, const ROL::Ptr<const Tpetra::MultiVector<>> &v,
484 const bool transpose) {
485 napJ2_++;
486 if (transpose) {
487 matJ2_->apply(*v,*Jv,Teuchos::TRANS);
488 }
489 else {
490 matJ2_->apply(*v,*Jv);
491 }
492 }
493
494 void assembleH22(const ROL::Ptr<const Tpetra::MultiVector<>> &u, const ROL::Ptr<const Tpetra::MultiVector<>> &z) {
495 if (assembleH22_) {
496 nasH2_++;
497 assembler_->assemblePDEHessian22(matH22_,pde_,u,u,z);
498 assembleH22_ = false;
499 }
500 }
501
502 void applyHessian22(ROL::Ptr<Tpetra::MultiVector<>> &Hv, const ROL::Ptr<const Tpetra::MultiVector<>> &v) {
503 matH22_->apply(*v,*Hv);
504 napH2_++;
505 }
506
507 void assembleJ1(const ROL::Ptr<const Tpetra::MultiVector<>> &u, const ROL::Ptr<const Tpetra::MultiVector<>> &z, bool setSolve = true) {
508 if (assembleJ1_) {
509 nasJ1_++;
510 assembler_->assemblePDEJacobian1(matJ1_,pde_,u,z);
511 if (setSolve) {
512 solver_->setA(matJ1_);
513 }
514 assembleJ1_ = false;
515 }
516 }
517
518 void solve(ROL::Ptr<Tpetra::MultiVector<>> &x, const ROL::Ptr<const Tpetra::MultiVector<>> &b, Real &tol) {
519 napJ1_++;
520 if (useInexact_) {
521 solverList_.sublist("Belos").set("Convergence Tolerance",tol);
522 solver_->setParameters(solverList_);
523 }
524 solver_->solve(x,b,false);
525 }
526
527 void solve_state_equation(ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
528 bool isComputed = false;
529 Real mytol = std::min(maxtol_,tol);
530 if (storage_ && tol_ <= mytol) {
531 isComputed = stateStore_->get(u, ROL::Objective<Real>::getParameter());
532 }
533 if (!isComputed || !storage_ || tol_ > mytol) {
534 ROL::Ptr<const Tpetra::MultiVector<>> z_data = getConstField(z);
535 ROL::Ptr<Tpetra::MultiVector<>> u_data = getField(u);
536 solve_state_equation(u_data,z_data,mytol);
537 tol_ = mytol;
538 if (storage_) {
539 stateStore_->set(u, ROL::Objective<Real>::getParameter());
540 }
541 }
542 }
543
544 void solve_state_equation(ROL::Ptr<Tpetra::MultiVector<>> &u, const ROL::Ptr<const Tpetra::MultiVector<>> &z, Real &tol) {
545 nstat_++;
546 assembleF(); assembleJ1(u,z,true);
547 solve(u,F_data_,tol);
548 }
549
550 void solve_state_sensitivity(ROL::Ptr<Tpetra::MultiVector<>> &s, const ROL::Ptr<const Tpetra::MultiVector<>> &v, Real &tol) {
551 // This assumes that assembleJ1 and assemble J2 were called
552 nsens_++;
553 applyJacobian2(dstat_data_, v, false);
554 solve(s, dstat_data_, tol);
555 }
556
557 void update_initial( const ROL::Vector<Real> &z, int iter ) {
558 assembleJ1_ = true;
559 assembleJ2_ = true;
560 assembleH22_ = true;
561
562 // For secant approximation
563 if (hessAppr_ == "Secant") {
564 computeGrad_ = true;
565 const Real half(0.5), one(1);
566 Real tol(std::sqrt(ROL::ROL_EPSILON<Real>()));
567 gradient(*g_old_,z,tol); // g_old = J'(z)
568 g_old_->scale(half/cmpScale_); // g_old = J'(z)/2c
569 s_->set(z); s_->scale(-one); // s = -z
570 }
571 }
572
573 void update_accept( const ROL::Vector<Real> &z, int iter ) {
574 // Push trial state storage into base state storage.
575 solver_tmp_ = solver_cache_;
576 solver_cache_ = solver_;
577 matJ1_tmp_ = matJ1_cache_;
578 matJ1_cache_ = matJ1_;
579 matJ2_tmp_ = matJ2_cache_;
580 matJ2_cache_ = matJ2_;
581 assembleJ1_ = false;
582 // If assembleJ2 was not called, then assembleJ2_ = true
583 assembleH22_ = true;
584
585 // For secant update
586 // Secant Equation: B s = 1/2c(J'(z) - (J'(z_old) + H s))
587 if (hessAppr_ == "Secant") {
588 computeGrad_ = true;
589 const Real half(0.5), one(1);
590 Real tol(std::sqrt(ROL::ROL_EPSILON<Real>()));
591 s_->plus(z); // s = z - z_old
592 applyHessian22(dctrl_data_,s_data_); // dctrl = -H s
593 g_old_->axpy(-half,*dctrl_); // g_old = (J'(z_old) + cH s)/2c
594 gradient(*g_new_,z,tol); // gnew = J'(z)
595 g_new_->scale(half/cmpScale_); // gnew = J'(z)/2c
596 secant_->updateStorage(z,*g_new_,*g_old_,*s_,s_->norm(),iter);
597 s_->set(z); s_->scale(-one); // s = -z
598 g_old_->set(*g_new_); // g_old = J'(z)/2c
599 }
600 }
601
602 void update_temp( const ROL::Vector<Real> &z, int iter ) {
603 assembleJ1_ = true;
604 assembleJ2_ = true;
605 assembleH22_ = true;
606 if (hessAppr_ == "Secant") {
607 computeGrad_ = true;
608 }
609 }
610
611 void update_trial( const ROL::Vector<Real> &z, int iter ) {
612 solver_ = solver_tmp_;
613 matJ1_ = matJ1_tmp_;
614 matJ2_ = matJ2_tmp_;
615 assembleJ1_ = true;
616 assembleJ2_ = true;
617 assembleH22_ = false;
618 }
619
620 void update_revert( const ROL::Vector<Real> &z, int iter ) {
621 solver_ = solver_cache_;
622 matJ1_ = matJ1_cache_;
623 matJ2_ = matJ2_cache_;
624 assembleJ1_ = false;
625 // If assembleJ2 was not called, then assembleJ2_ = true
626 assembleH22_ = false;
627 // If gradient was not called, then computeGrad_ = true
628 }
629 }; // class TopOptComplianceObjective
630
631 #endif