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