| 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 constraint.hpp |
| 11 |
\brief Defines the SimOpt constraint for the 'poisson' example. |
| 12 |
*/ |
| 13 |
|
| 14 |
#ifndef ROL_PDEOPT_BILINEARPDECONSTRAINT_H |
| 15 |
#define ROL_PDEOPT_BILINEARPDECONSTRAINT_H |
| 16 |
|
| 17 |
#include "ROL_Constraint_SimOpt.hpp" |
| 18 |
#include "../../../TOOLS/solver.hpp" |
| 19 |
#include "../../../TOOLS/pdevector.hpp" |
| 20 |
#include "femdata.hpp" |
| 21 |
|
| 22 |
//// Global Timers. |
| 23 |
#ifdef ROL_TIMERS |
| 24 |
namespace ROL { |
| 25 |
namespace PDEOPT { |
| 26 |
ROL::Ptr<Teuchos::Time> BilinearPDEConstraintSolverConstruct_Jacobian1 = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Solver Construction Time for Jacobian1"); |
| 27 |
ROL::Ptr<Teuchos::Time> BilinearPDEConstraintSolverConstruct_AdjointJacobian1 = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Solver Construction Time for Adjoint Jacobian1"); |
| 28 |
ROL::Ptr<Teuchos::Time> BilinearPDEConstraintSolverSolve_Jacobian1 = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Solver Solution Time for Jacobian1"); |
| 29 |
ROL::Ptr<Teuchos::Time> BilinearPDEConstraintSolverSolve_AdjointJacobian1 = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Solver Solution Time for Adjoint Jacobian1"); |
| 30 |
ROL::Ptr<Teuchos::Time> BilinearPDEConstraintApplyJacobian1 = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Apply Jacobian1"); |
| 31 |
ROL::Ptr<Teuchos::Time> BilinearPDEConstraintApplyJacobian2 = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Apply Jacobian2"); |
| 32 |
ROL::Ptr<Teuchos::Time> BilinearPDEConstraintApplyAdjointJacobian1 = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Apply Adjoint Jacobian1"); |
| 33 |
ROL::Ptr<Teuchos::Time> BilinearPDEConstraintApplyAdjointJacobian2 = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Apply Adjoint Jacobian2"); |
| 34 |
ROL::Ptr<Teuchos::Time> BilinearPDEConstraintApplyHessian = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Apply Hessian"); |
| 35 |
ROL::Ptr<Teuchos::Time> BilinearPDEConstraintApplyAdjontHessian = Teuchos::TimeMonitor::getNewCounter("ROL::PDEOPT: Bilinear PDE Constraint Apply Adjoint Hessian"); |
| 36 |
} |
| 37 |
} |
| 38 |
#endif |
| 39 |
|
| 40 |
|
| 41 |
template<class Real> |
| 42 |
class Bilinear_PDE_Constraint : public ROL::Constraint_SimOpt<Real> { |
| 43 |
private: |
| 44 |
const ROL::Ptr<FEM_Data<Real>> fem_; |
| 45 |
ROL::Ptr<Solver<Real>> solver_; |
| 46 |
bool setSolver_, assembleJ1_, buildState_, buildAdjoint_; |
| 47 |
|
| 48 |
ROL::Ptr<Tpetra::MultiVector<>> cvec_; |
| 49 |
ROL::Ptr<Tpetra::RowMatrix<>> matJ1_; |
| 50 |
ROL::Ptr<Tpetra::MultiVector<>> S0u_; |
| 51 |
std::vector<std::vector<std::vector<ROL::Ptr<Tpetra::MultiVector<>>>>> Su_; |
| 52 |
std::vector<std::vector<std::vector<ROL::Ptr<Tpetra::MultiVector<>>>>> Sl_; |
| 53 |
|
| 54 |
unsigned M_, N_, T_; |
| 55 |
|
| 56 |
void setSolver(void) { |
| 57 |
ROL::Ptr<Tpetra::CrsMatrix<>> tmp = ROL::staticPtrCast<Tpetra::CrsMatrix<>>(matJ1_); |
| 58 |
solver_->setA(tmp); |
| 59 |
setSolver_ = false; |
| 60 |
} |
| 61 |
|
| 62 |
void solveForward(ROL::Ptr<Tpetra::MultiVector<>> &x, |
| 63 |
const ROL::Ptr<const Tpetra::MultiVector<>> &b) { |
| 64 |
if (setSolver_) { |
| 65 |
#ifdef ROL_TIMERS |
| 66 |
Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintSolverConstruct_Jacobian1); |
| 67 |
#endif |
| 68 |
setSolver(); |
| 69 |
} |
| 70 |
|
| 71 |
#ifdef ROL_TIMERS |
| 72 |
Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintSolverSolve_Jacobian1); |
| 73 |
#endif |
| 74 |
solver_->solve(x,b,false); |
| 75 |
} |
| 76 |
|
| 77 |
void solveAdjoint(ROL::Ptr<Tpetra::MultiVector<>> &x, |
| 78 |
const ROL::Ptr<const Tpetra::MultiVector<>> &b) { |
| 79 |
if (setSolver_) { |
| 80 |
#ifdef ROL_TIMERS |
| 81 |
Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintSolverConstruct_AdjointJacobian1); |
| 82 |
#endif |
| 83 |
setSolver(); |
| 84 |
} |
| 85 |
|
| 86 |
#ifdef ROL_TIMERS |
| 87 |
Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintSolverSolve_AdjointJacobian1); |
| 88 |
#endif |
| 89 |
solver_->solve(x,b,true); |
| 90 |
} |
| 91 |
|
| 92 |
void buildSu(const ROL::Ptr<const Tpetra::MultiVector<>> &u) { |
| 93 |
if (buildState_) { |
| 94 |
fem_->applyS0(*S0u_,*u); |
| 95 |
for (unsigned i = 0; i < M_; ++i) { |
| 96 |
for (unsigned j = 0; j < N_; ++j) { |
| 97 |
for (unsigned k = 0; k < T_; ++k) { |
| 98 |
fem_->applyS(i,j,k,*Su_[i][j][k],*u); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
buildState_ = false; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
void applySuz(const ROL::Ptr<Tpetra::MultiVector<>> &Juz, |
| 107 |
const ROL::Ptr<const Tpetra::MultiVector<>> &u, |
| 108 |
const ROL::Ptr<const std::vector<Real>> &z) { |
| 109 |
const Real zero(0), one(1); |
| 110 |
buildSu(u); |
| 111 |
Juz->update(one,*S0u_,zero); |
| 112 |
for (unsigned i = 0; i < M_; ++i) { |
| 113 |
for (unsigned j = 0; j < N_; ++j) { |
| 114 |
for (unsigned k = 0; k < T_; ++k) { |
| 115 |
Juz->update((*z)[i+M_*(j+N_*k)],*Su_[i][j][k],one); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
void buildJacobian1(const ROL::Ptr<const std::vector<Real>> &z) { |
| 122 |
if (assembleJ1_) { |
| 123 |
const Real zero(0), one(1); |
| 124 |
ROL::Ptr<Teuchos::ParameterList> list = ROL::makePtr<Teuchos::ParameterList>(); |
| 125 |
matJ1_ = matJ1_->add(one,*fem_->getS0(),zero, |
| 126 |
fem_->getS0()->getDomainMap(),fem_->getS0()->getRangeMap(),list); |
| 127 |
for (unsigned i = 0; i < M_; ++i) { |
| 128 |
for (unsigned j = 0; j < N_; ++j) { |
| 129 |
for (unsigned k = 0; k < T_; ++k) { |
| 130 |
matJ1_ = matJ1_->add((*z)[i+M_*(j+N_*k)],*fem_->getS(i,j,k),one, |
| 131 |
fem_->getS(i,j,k)->getDomainMap(),fem_->getS(i,j,k)->getRangeMap(),list); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
assembleJ1_ = false; |
| 136 |
setSolver_ = true; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
void applyJacobian1(const ROL::Ptr<Tpetra::MultiVector<>> &Jv, |
| 141 |
const ROL::Ptr<const Tpetra::MultiVector<>> &v) { |
| 142 |
#ifdef ROL_TIMERS |
| 143 |
Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BilinearPDEConstraintApplyJacobian1); |
| 144 |
#endif |
| 145 |
matJ1_->apply(*v,*Jv); |
| 146 |
} |
| 147 |
|
| 148 |
void applyAdjointJacobian1(const ROL::Ptr<Tpetra::MultiVector<>> &Jv, |
| 149 |
const ROL::Ptr<const Tpetra::MultiVector<>> &v) { |
| 150 |
#ifdef ROL_TIMERS |
| 151 |
Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintApplyAdjointJacobian1); |
| 152 |
#endif |
| 153 |
matJ1_->apply(*v,*Jv,Teuchos::TRANS); |
| 154 |
} |
| 155 |
|
| 156 |
void applyJacobian2(const ROL::Ptr<Tpetra::MultiVector<>> &Jv, |
| 157 |
const ROL::Ptr<const Tpetra::MultiVector<>> &u, |
| 158 |
const ROL::Ptr<const std::vector<Real>> &v) { |
| 159 |
#ifdef ROL_TIMERS |
| 160 |
Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintApplyJacobian2); |
| 161 |
#endif |
| 162 |
buildSu(u); |
| 163 |
Jv->scale(static_cast<Real>(0)); |
| 164 |
const Real one(1); |
| 165 |
for (unsigned i = 0; i < M_; ++i) { |
| 166 |
for (unsigned j = 0; j < N_; ++j) { |
| 167 |
for (unsigned k = 0; k < T_; ++k) { |
| 168 |
Jv->update((*v)[i+M_*(j+N_*k)],*Su_[i][j][k],one); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void applyAdjointJacobian2(const ROL::Ptr<std::vector<Real>> &Jv, |
| 175 |
const ROL::Ptr<const Tpetra::MultiVector<>> &v, |
| 176 |
const ROL::Ptr<const Tpetra::MultiVector<>> &u) { |
| 177 |
#ifdef ROL_TIMERS |
| 178 |
Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintApplyAdjointJacobian2); |
| 179 |
#endif |
| 180 |
buildSu(u); |
| 181 |
Teuchos::Array<Real> val(1,0); |
| 182 |
for (unsigned i = 0; i < M_; ++i) { |
| 183 |
for (unsigned j = 0; j < N_; ++j) { |
| 184 |
for (unsigned k = 0; k < T_; ++k) { |
| 185 |
Su_[i][j][k]->dot(*v, val.view(0,1)); |
| 186 |
(*Jv)[i+M_*(j+N_*k)] = val[0]; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
void buildSl(const ROL::Ptr<const Tpetra::MultiVector<>> &l) { |
| 193 |
if (buildAdjoint_) { |
| 194 |
for (unsigned i = 0; i < M_; ++i) { |
| 195 |
for (unsigned j = 0; j < N_; ++j) { |
| 196 |
for (unsigned k = 0; k < T_; ++k) { |
| 197 |
fem_->applyS(i,j,k,*Sl_[i][j][k],*l,true); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
buildAdjoint_ = false; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
void applyHessian(const ROL::Ptr<Tpetra::MultiVector<>> &H, |
| 206 |
const ROL::Ptr<const std::vector<Real>> &v, |
| 207 |
const ROL::Ptr<const Tpetra::MultiVector<>> &l) { |
| 208 |
#ifdef ROL_TIMERS |
| 209 |
Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintApplyHessian); |
| 210 |
#endif |
| 211 |
buildSl(l); |
| 212 |
H->scale(static_cast<Real>(0)); |
| 213 |
const Real one(1); |
| 214 |
for (unsigned i = 0; i < M_; ++i) { |
| 215 |
for (unsigned j = 0; j < N_; ++j) { |
| 216 |
for (unsigned k = 0; k < T_; ++k) { |
| 217 |
H->update((*v)[i+M_*(j+N_*k)],*Sl_[i][j][k],one); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
void applyAdjointHessian(const ROL::Ptr<std::vector<Real>> &H, |
| 224 |
const ROL::Ptr<const Tpetra::MultiVector<>> &v, |
| 225 |
const ROL::Ptr<const Tpetra::MultiVector<>> &l) { |
| 226 |
#ifdef ROL_TIMERS |
| 227 |
Teuchos::TimeMonitor LocalTimer(*ROL::PDEOPT::BinlinearPDEConstraintApplyAdjointHessian); |
| 228 |
#endif |
| 229 |
buildSl(l); |
| 230 |
Teuchos::Array<Real> val(1,0); |
| 231 |
for (unsigned i = 0; i < M_; ++i) { |
| 232 |
for (unsigned j = 0; j < N_; ++j) { |
| 233 |
for (unsigned k = 0; k < T_; ++k) { |
| 234 |
Sl_[i][j][k]->dot(*v, val.view(0,1)); |
| 235 |
(*H)[i+M_*(j+N_*k)] = val[0]; |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
public: |
| 242 |
Bilinear_PDE_Constraint(const ROL::Ptr<FEM_Data<Real>> &fem, |
| 243 |
Teuchos::ParameterList &list, |
| 244 |
std::ostream &outStream = std::cout) |
| 245 |
: ROL::Constraint_SimOpt<Real>(), |
| 246 |
fem_(fem), |
| 247 |
setSolver_(true), |
| 248 |
assembleJ1_(true), |
| 249 |
buildState_(true), |
| 250 |
buildAdjoint_(true) { |
| 251 |
// Construct solver. |
| 252 |
solver_ = ROL::makePtr<Solver<Real>>(list.sublist("Solver")); |
| 253 |
// Initialize zero vectors. |
| 254 |
cvec_ = fem_->getAssembler()->createResidualVector(); |
| 255 |
|
| 256 |
std::vector<Real> ym = ROL::getArrayFromStringParameter<Real>(list.sublist("Problem"), "Young's Modulus"); |
| 257 |
M_ = list.sublist("Problem").get("Number of Horizontal Cells",10); |
| 258 |
N_ = list.sublist("Problem").get("Number of Vertical Cells",20); |
| 259 |
T_ = ym.size(); |
| 260 |
|
| 261 |
matJ1_ = ROL::makePtr<Tpetra::CrsMatrix<>>(fem_->getS0()->getCrsGraph()); |
| 262 |
S0u_ = fem_->getAssembler()->createResidualVector(); |
| 263 |
Su_.resize(M_); |
| 264 |
Sl_.resize(M_); |
| 265 |
for (unsigned i = 0; i < M_; ++i) { |
| 266 |
Su_[i].resize(N_); |
| 267 |
Sl_[i].resize(N_); |
| 268 |
for (unsigned j = 0; j < N_; ++j) { |
| 269 |
Su_[i][j].resize(T_); |
| 270 |
Sl_[i][j].resize(T_); |
| 271 |
for (unsigned k = 0; k < T_; ++k) { |
| 272 |
Su_[i][j][k] = fem_->getAssembler()->createResidualVector(); |
| 273 |
Sl_[i][j][k] = fem_->getAssembler()->createResidualVector(); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
const ROL::Ptr<Assembler<Real>> getAssembler(void) const { |
| 280 |
return fem_->getAssembler(); |
| 281 |
} |
| 282 |
|
| 283 |
const ROL::Ptr<PDE<Real>> getPDE(void) const { |
| 284 |
return fem_->getPDE(); |
| 285 |
} |
| 286 |
|
| 287 |
using ROL::Constraint_SimOpt<Real>::update_1; |
| 288 |
void update_1(const ROL::Vector<Real> &u, bool flag = true, int iter = -1) { |
| 289 |
buildState_ = true; |
| 290 |
buildAdjoint_ = true; |
| 291 |
} |
| 292 |
|
| 293 |
using ROL::Constraint_SimOpt<Real>::update_2; |
| 294 |
void update_2(const ROL::Vector<Real> &z, bool flag = true, int iter = -1) { |
| 295 |
assembleJ1_ = true; |
| 296 |
} |
| 297 |
|
| 298 |
using ROL::Constraint_SimOpt<Real>::update; |
| 299 |
void update(const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, bool flag = true, int iter = -1) { |
| 300 |
//update_1(u,flag,iter); |
| 301 |
//update_2(z,flag,iter); |
| 302 |
} |
| 303 |
|
| 304 |
using ROL::Constraint_SimOpt<Real>::value; |
| 305 |
void value(ROL::Vector<Real> &c, const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) { |
| 306 |
ROL::Ptr<Tpetra::MultiVector<>> cf = getField(c); |
| 307 |
ROL::Ptr<const Tpetra::MultiVector<>> uf = getConstField(u); |
| 308 |
ROL::Ptr<const Tpetra::MultiVector<>> zf = getConstField(z); |
| 309 |
ROL::Ptr<const std::vector<Real>> zp = getConstParameter(z); |
| 310 |
|
| 311 |
const Real one(1); |
| 312 |
cvec_->putScalar(static_cast<Real>(0)); |
| 313 |
c.zero(); |
| 314 |
|
| 315 |
cf->scale(one,*fem_->getForce()); |
| 316 |
applySuz(cvec_,uf,zp); |
| 317 |
cf->update(one,*cvec_,one); |
| 318 |
} |
| 319 |
|
| 320 |
void solve(ROL::Vector<Real> &c, ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) { |
| 321 |
ROL::Ptr<Tpetra::MultiVector<>> cf = getField(c); |
| 322 |
ROL::Ptr<Tpetra::MultiVector<>> uf = getField(u); |
| 323 |
ROL::Ptr<const Tpetra::MultiVector<>> zf = getConstField(z); |
| 324 |
ROL::Ptr<const std::vector<Real>> zp = getConstParameter(z); |
| 325 |
|
| 326 |
c.zero(); |
| 327 |
cvec_->putScalar(static_cast<Real>(0)); |
| 328 |
u.zero(); |
| 329 |
|
| 330 |
const Real one(1); |
| 331 |
cf->scale(one,*fem_->getForce()); |
| 332 |
buildJacobian1(zp); |
| 333 |
solveForward(uf,cf); |
| 334 |
uf->scale(-one); |
| 335 |
|
| 336 |
applySuz(cvec_,uf,zp); |
| 337 |
cf->update(one,*cvec_,one); |
| 338 |
} |
| 339 |
|
| 340 |
void applyJacobian_1(ROL::Vector<Real> &jv, |
| 341 |
const ROL::Vector<Real> &v, |
| 342 |
const ROL::Vector<Real> &u, |
| 343 |
const ROL::Vector<Real> &z, Real &tol) { |
| 344 |
jv.zero(); |
| 345 |
ROL::Ptr<Tpetra::MultiVector<>> jvf = getField(jv); |
| 346 |
ROL::Ptr<const Tpetra::MultiVector<>> vf = getConstField(v); |
| 347 |
ROL::Ptr<const Tpetra::MultiVector<>> zf = getConstField(z); |
| 348 |
ROL::Ptr<const std::vector<Real>> zp = getConstParameter(z); |
| 349 |
|
| 350 |
buildJacobian1(zp); |
| 351 |
applyJacobian1(jvf,vf); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
void applyJacobian_2(ROL::Vector<Real> &jv, |
| 356 |
const ROL::Vector<Real> &v, |
| 357 |
const ROL::Vector<Real> &u, |
| 358 |
const ROL::Vector<Real> &z, Real &tol) { |
| 359 |
jv.zero(); |
| 360 |
ROL::Ptr<Tpetra::MultiVector<>> jvf = getField(jv); |
| 361 |
ROL::Ptr<const Tpetra::MultiVector<>> uf = getConstField(u); |
| 362 |
ROL::Ptr<const std::vector<Real>> vp = getConstParameter(v); |
| 363 |
|
| 364 |
applyJacobian2(jvf,uf,vp); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
void applyAdjointJacobian_1(ROL::Vector<Real> &ajv, |
| 369 |
const ROL::Vector<Real> &v, |
| 370 |
const ROL::Vector<Real> &u, |
| 371 |
const ROL::Vector<Real> &z, Real &tol) { |
| 372 |
ajv.zero(); |
| 373 |
ROL::Ptr<Tpetra::MultiVector<>> ajvf = getField(ajv); |
| 374 |
ROL::Ptr<const Tpetra::MultiVector<>> vf = getConstField(v); |
| 375 |
ROL::Ptr<const Tpetra::MultiVector<>> zf = getConstField(z); |
| 376 |
ROL::Ptr<const std::vector<Real>> zp = getConstParameter(z); |
| 377 |
|
| 378 |
buildJacobian1(zp); |
| 379 |
applyAdjointJacobian1(ajvf,vf); |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
void applyAdjointJacobian_2(ROL::Vector<Real> &ajv, |
| 384 |
const ROL::Vector<Real> &v, |
| 385 |
const ROL::Vector<Real> &u, |
| 386 |
const ROL::Vector<Real> &z, Real &tol) { |
| 387 |
ajv.zero(); |
| 388 |
ROL::Ptr<std::vector<Real>> ajvp = getParameter(ajv); |
| 389 |
ROL::Ptr<const Tpetra::MultiVector<>> uf = getConstField(u); |
| 390 |
ROL::Ptr<const Tpetra::MultiVector<>> vf = getConstField(v); |
| 391 |
|
| 392 |
applyAdjointJacobian2(ajvp,vf,uf); |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
void applyInverseJacobian_1(ROL::Vector<Real> &ijv, |
| 397 |
const ROL::Vector<Real> &v, |
| 398 |
const ROL::Vector<Real> &u, |
| 399 |
const ROL::Vector<Real> &z, Real &tol) { |
| 400 |
ijv.zero(); |
| 401 |
ROL::Ptr<Tpetra::MultiVector<>> ijvf = getField(ijv); |
| 402 |
ROL::Ptr<const Tpetra::MultiVector<>> vf = getConstField(v); |
| 403 |
ROL::Ptr<const std::vector<Real>> zp = getConstParameter(z); |
| 404 |
|
| 405 |
buildJacobian1(zp); |
| 406 |
solveForward(ijvf,vf); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
void applyInverseAdjointJacobian_1(ROL::Vector<Real> &iajv, |
| 411 |
const ROL::Vector<Real> &v, |
| 412 |
const ROL::Vector<Real> &u, |
| 413 |
const ROL::Vector<Real> &z, Real &tol) { |
| 414 |
iajv.zero(); |
| 415 |
ROL::Ptr<Tpetra::MultiVector<>> iajvf = getField(iajv); |
| 416 |
ROL::Ptr<const Tpetra::MultiVector<>> vf = getConstField(v); |
| 417 |
ROL::Ptr<const std::vector<Real>> zp = getConstParameter(z); |
| 418 |
|
| 419 |
buildJacobian1(zp); |
| 420 |
solveAdjoint(iajvf,vf); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
void applyAdjointHessian_11(ROL::Vector<Real> &ahwv, |
| 425 |
const ROL::Vector<Real> &w, |
| 426 |
const ROL::Vector<Real> &v, |
| 427 |
const ROL::Vector<Real> &u, |
| 428 |
const ROL::Vector<Real> &z, Real &tol) { |
| 429 |
ahwv.zero(); |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
void applyAdjointHessian_12(ROL::Vector<Real> &ahwv, |
| 434 |
const ROL::Vector<Real> &w, |
| 435 |
const ROL::Vector<Real> &v, |
| 436 |
const ROL::Vector<Real> &u, |
| 437 |
const ROL::Vector<Real> &z, Real &tol) { |
| 438 |
ahwv.zero(); |
| 439 |
ROL::Ptr<std::vector<Real>> ahwvp = getParameter(ahwv); |
| 440 |
ROL::Ptr<const Tpetra::MultiVector<>> wf = getConstField(w); |
| 441 |
ROL::Ptr<const Tpetra::MultiVector<>> vf = getConstField(v); |
| 442 |
|
| 443 |
applyAdjointHessian(ahwvp,vf,wf); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
void applyAdjointHessian_21(ROL::Vector<Real> &ahwv, |
| 448 |
const ROL::Vector<Real> &w, |
| 449 |
const ROL::Vector<Real> &v, |
| 450 |
const ROL::Vector<Real> &u, |
| 451 |
const ROL::Vector<Real> &z, Real &tol) { |
| 452 |
ahwv.zero(); |
| 453 |
ROL::Ptr<Tpetra::MultiVector<>> ahwvf = getField(ahwv); |
| 454 |
ROL::Ptr<const Tpetra::MultiVector<>> wf = getConstField(w); |
| 455 |
ROL::Ptr<const std::vector<Real>> vp = getConstParameter(v); |
| 456 |
|
| 457 |
applyHessian(ahwvf,vp,wf); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
void applyAdjointHessian_22(ROL::Vector<Real> &ahwv, |
| 462 |
const ROL::Vector<Real> &w, |
| 463 |
const ROL::Vector<Real> &v, |
| 464 |
const ROL::Vector<Real> &u, |
| 465 |
const ROL::Vector<Real> &z, Real &tol) { |
| 466 |
ahwv.zero(); |
| 467 |
} |
| 468 |
|
| 469 |
/***************************************************************************/ |
| 470 |
/* Output routines. */ |
| 471 |
/***************************************************************************/ |
| 472 |
void printMeshData(std::ostream &outStream) const { |
| 473 |
fem_->printMeshData(outStream); |
| 474 |
} |
| 475 |
|
| 476 |
void outputTpetraData() const { |
| 477 |
Tpetra::MatrixMarket::Writer< Tpetra::CrsMatrix<>> matWriter; |
| 478 |
if (matJ1_ != ROL::nullPtr) { |
| 479 |
matWriter.writeSparseFile("jacobian1.txt", ROL::staticPtrCast<Tpetra::CrsMatrix<>>(matJ1_)); |
| 480 |
} |
| 481 |
else { |
| 482 |
std::ofstream emptyfile; |
| 483 |
emptyfile.open("jacobian1.txt"); |
| 484 |
emptyfile.close(); |
| 485 |
} |
| 486 |
if (fem_->getForce() != ROL::nullPtr) { |
| 487 |
matWriter.writeDenseFile("residual.txt", fem_->getForce()); |
| 488 |
} |
| 489 |
else { |
| 490 |
std::ofstream emptyfile; |
| 491 |
emptyfile.open("residual.txt"); |
| 492 |
emptyfile.close(); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
void outputTpetraVector(const ROL::Ptr<const Tpetra::MultiVector<>> &vec, |
| 497 |
const std::string &filename) const { |
| 498 |
fem_->outputTpetraVector(vec,filename); |
| 499 |
} |
| 500 |
/***************************************************************************/ |
| 501 |
/* End of output routines. */ |
| 502 |
/***************************************************************************/ |
| 503 |
|
| 504 |
private: // Vector accessor functions |
| 505 |
|
| 506 |
ROL::Ptr<const Tpetra::MultiVector<>> getConstField(const ROL::Vector<Real> &x) const { |
| 507 |
ROL::Ptr<const Tpetra::MultiVector<>> xp; |
| 508 |
try { |
| 509 |
xp = dynamic_cast<const ROL::TpetraMultiVector<Real>&>(x).getVector(); |
| 510 |
} |
| 511 |
catch (std::exception &e) { |
| 512 |
try { |
| 513 |
ROL::Ptr<const ROL::TpetraMultiVector<Real>> xvec |
| 514 |
= dynamic_cast<const PDE_OptVector<Real>&>(x).getField(); |
| 515 |
if (xvec == ROL::nullPtr) { |
| 516 |
xp = ROL::nullPtr; |
| 517 |
} |
| 518 |
else { |
| 519 |
xp = xvec->getVector(); |
| 520 |
} |
| 521 |
} |
| 522 |
catch (std::exception &ee) { |
| 523 |
xp = ROL::nullPtr; |
| 524 |
} |
| 525 |
} |
| 526 |
return xp; |
| 527 |
} |
| 528 |
|
| 529 |
ROL::Ptr<Tpetra::MultiVector<>> getField(ROL::Vector<Real> &x) const { |
| 530 |
ROL::Ptr<Tpetra::MultiVector<>> xp; |
| 531 |
try { |
| 532 |
xp = dynamic_cast<ROL::TpetraMultiVector<Real>&>(x).getVector(); |
| 533 |
} |
| 534 |
catch (std::exception &e) { |
| 535 |
try { |
| 536 |
ROL::Ptr<ROL::TpetraMultiVector<Real>> xvec |
| 537 |
= dynamic_cast<PDE_OptVector<Real>&>(x).getField(); |
| 538 |
if ( xvec == ROL::nullPtr ) { |
| 539 |
xp = ROL::nullPtr; |
| 540 |
} |
| 541 |
else { |
| 542 |
xp = xvec->getVector(); |
| 543 |
} |
| 544 |
} |
| 545 |
catch (std::exception &ee) { |
| 546 |
xp = ROL::nullPtr; |
| 547 |
} |
| 548 |
} |
| 549 |
return xp; |
| 550 |
} |
| 551 |
|
| 552 |
ROL::Ptr<const std::vector<Real>> getConstParameter(const ROL::Vector<Real> &x) const { |
| 553 |
ROL::Ptr<const std::vector<Real>> xp; |
| 554 |
try { |
| 555 |
xp = dynamic_cast<const ROL::StdVector<Real>&>(x).getVector(); |
| 556 |
} |
| 557 |
catch (std::exception &e) { |
| 558 |
try { |
| 559 |
ROL::Ptr<const ROL::StdVector<Real>> xvec |
| 560 |
= dynamic_cast<const PDE_OptVector<Real>&>(x).getParameter(); |
| 561 |
if ( xvec == ROL::nullPtr ) { |
| 562 |
xp = ROL::nullPtr; |
| 563 |
} |
| 564 |
else { |
| 565 |
xp = xvec->getVector(); |
| 566 |
} |
| 567 |
} |
| 568 |
catch (std::exception &ee) { |
| 569 |
xp = ROL::nullPtr; |
| 570 |
} |
| 571 |
} |
| 572 |
return xp; |
| 573 |
} |
| 574 |
|
| 575 |
ROL::Ptr<std::vector<Real>> getParameter(ROL::Vector<Real> &x) const { |
| 576 |
ROL::Ptr<std::vector<Real>> xp; |
| 577 |
try { |
| 578 |
xp = dynamic_cast<ROL::StdVector<Real>&>(x).getVector(); |
| 579 |
} |
| 580 |
catch (std::exception &e) { |
| 581 |
try { |
| 582 |
ROL::Ptr<ROL::StdVector<Real>> xvec |
| 583 |
= dynamic_cast<PDE_OptVector<Real>&>(x).getParameter(); |
| 584 |
if ( xvec == ROL::nullPtr ) { |
| 585 |
xp = ROL::nullPtr; |
| 586 |
} |
| 587 |
else { |
| 588 |
xp = xvec->getVector(); |
| 589 |
} |
| 590 |
} |
| 591 |
catch (std::exception &ee) { |
| 592 |
xp = ROL::nullPtr; |
| 593 |
} |
| 594 |
} |
| 595 |
return xp; |
| 596 |
} |
| 597 |
}; |
| 598 |
|
| 599 |
#endif |