| 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 obj.hpp |
| 11 |
\brief Provides the interface for local (cell-based) objective function computations. |
| 12 |
*/ |
| 13 |
|
| 14 |
#ifndef PDEOPT_QOI_L2TRACKING_ALLEN_CAHN_HPP |
| 15 |
#define PDEOPT_QOI_L2TRACKING_ALLEN_CAHN_HPP |
| 16 |
|
| 17 |
#include "../TOOLS/qoi.hpp" |
| 18 |
#include "pde_allen_cahn.hpp" |
| 19 |
|
| 20 |
template <class Real> |
| 21 |
class QoI_State_Cost_Allen_Cahn : public QoI<Real> { |
| 22 |
private: |
| 23 |
ROL::Ptr<FE<Real> > fe_; |
| 24 |
|
| 25 |
ROL::Ptr<Intrepid::FieldContainer<Real> > target_; |
| 26 |
|
| 27 |
Real targetFunc(const std::vector<Real> & x) const { |
| 28 |
int size = x.size(); |
| 29 |
Real val(0); |
| 30 |
for (int i = 0; i < size; ++i) { |
| 31 |
val += x[i]*x[i]; |
| 32 |
} |
| 33 |
return val; |
| 34 |
} |
| 35 |
|
| 36 |
public: |
| 37 |
QoI_State_Cost_Allen_Cahn(const ROL::Ptr<FE<Real> > &fe) : fe_(fe) { |
| 38 |
int c = fe_->cubPts()->dimension(0); |
| 39 |
int p = fe_->cubPts()->dimension(1); |
| 40 |
int d = fe_->cubPts()->dimension(2); |
| 41 |
std::vector<Real> pt(d); |
| 42 |
target_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(c,p); |
| 43 |
for (int i = 0; i < c; ++i) { |
| 44 |
for (int j = 0; j < p; ++j) { |
| 45 |
for (int k = 0; k < d; ++k) { |
| 46 |
pt[k] = (*fe_->cubPts())(i,j,k); |
| 47 |
} |
| 48 |
(*target_)(i,j) = targetFunc(pt); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
Real value(ROL::Ptr<Intrepid::FieldContainer<Real> > & val, |
| 54 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 55 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 56 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 57 |
// Get relevant dimensions |
| 58 |
int c = u_coeff->dimension(0); |
| 59 |
int p = fe_->cubPts()->dimension(1); |
| 60 |
// Initialize output val |
| 61 |
val = ROL::makePtr<Intrepid::FieldContainer<Real>>(c); |
| 62 |
// Evaluate state on FE basis |
| 63 |
ROL::Ptr<Intrepid::FieldContainer<Real> > valU_eval = |
| 64 |
ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p); |
| 65 |
fe_->evaluateValue(valU_eval, u_coeff); |
| 66 |
// Compute difference between state and target |
| 67 |
Intrepid::RealSpaceTools<Real>::subtract(*valU_eval,*target_); |
| 68 |
// Compute squared L2-norm of diff |
| 69 |
fe_->computeIntegral(val,valU_eval,valU_eval); |
| 70 |
// Scale by one half |
| 71 |
Intrepid::RealSpaceTools<Real>::scale(*val,static_cast<Real>(0.5)); |
| 72 |
return static_cast<Real>(0); |
| 73 |
} |
| 74 |
|
| 75 |
void gradient_1(ROL::Ptr<Intrepid::FieldContainer<Real> > & grad, |
| 76 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 77 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 78 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 79 |
// Get relevant dimensions |
| 80 |
int c = u_coeff->dimension(0); |
| 81 |
int p = fe_->cubPts()->dimension(1); |
| 82 |
int f = fe_->N()->dimension(1); |
| 83 |
// Initialize output grad |
| 84 |
grad = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f); |
| 85 |
// Evaluate state on FE basis |
| 86 |
ROL::Ptr<Intrepid::FieldContainer<Real> > valU_eval = |
| 87 |
ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p); |
| 88 |
fe_->evaluateValue(valU_eval, u_coeff); |
| 89 |
// Compute difference between state and target |
| 90 |
Intrepid::RealSpaceTools<Real>::subtract(*valU_eval,*target_); |
| 91 |
// Compute gradient of squared L2-norm of diff |
| 92 |
Intrepid::FunctionSpaceTools::integrate<Real>(*grad, |
| 93 |
*valU_eval, |
| 94 |
*(fe_->NdetJ()), |
| 95 |
Intrepid::COMP_CPP, false); |
| 96 |
} |
| 97 |
|
| 98 |
void gradient_2(ROL::Ptr<Intrepid::FieldContainer<Real> > & grad, |
| 99 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 100 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 101 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 102 |
throw Exception::Zero(">>> QoI_State_Cost_Allen_Cahn::gradient_2 is zero."); |
| 103 |
} |
| 104 |
|
| 105 |
void HessVec_11(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess, |
| 106 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & v_coeff, |
| 107 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 108 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 109 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 110 |
int c = v_coeff->dimension(0); |
| 111 |
int p = fe_->cubPts()->dimension(1); |
| 112 |
int f = fe_->N()->dimension(1); |
| 113 |
ROL::Ptr<Intrepid::FieldContainer<Real> > valV_eval = |
| 114 |
ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p); |
| 115 |
hess = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f); |
| 116 |
fe_->evaluateValue(valV_eval, v_coeff); |
| 117 |
Intrepid::FunctionSpaceTools::integrate<Real>(*hess, |
| 118 |
*valV_eval, |
| 119 |
*(fe_->NdetJ()), |
| 120 |
Intrepid::COMP_CPP, false); |
| 121 |
} |
| 122 |
|
| 123 |
void HessVec_12(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess, |
| 124 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & v_coeff, |
| 125 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 126 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 127 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 128 |
throw Exception::Zero(">>> QoI_State_Cost_Allen_Cahn::HessVec_12 is zero."); |
| 129 |
} |
| 130 |
|
| 131 |
void HessVec_21(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess, |
| 132 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & v_coeff, |
| 133 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 134 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 135 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 136 |
throw Exception::Zero(">>> QoI_State_Cost_Allen_Cahn::HessVec_21 is zero."); |
| 137 |
} |
| 138 |
|
| 139 |
void HessVec_22(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess, |
| 140 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & v_coeff, |
| 141 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 142 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 143 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 144 |
throw Exception::Zero(">>> QoI_State_Cost_Allen_Cahn::HessVec_22 is zero."); |
| 145 |
} |
| 146 |
|
| 147 |
}; // QoI_L2Tracking |
| 148 |
|
| 149 |
template <class Real> |
| 150 |
class QoI_Control_Cost_Allen_Cahn : public QoI<Real> { |
| 151 |
private: |
| 152 |
const ROL::Ptr<FE<Real> > fe_vol_; |
| 153 |
const std::vector<std::vector<ROL::Ptr<FE<Real> > > > fe_bdry_; |
| 154 |
const std::vector<std::vector<std::vector<int> > > bdryCellLocIds_; |
| 155 |
|
| 156 |
ROL::Ptr<Intrepid::FieldContainer<Real> > getBoundaryCoeff( |
| 157 |
const Intrepid::FieldContainer<Real> & cell_coeff, |
| 158 |
const int sideset, const int locSideId) const { |
| 159 |
std::vector<int> bdryCellLocId = bdryCellLocIds_[sideset][locSideId]; |
| 160 |
const int numCellsSide = bdryCellLocId.size(); |
| 161 |
const int f = fe_vol_->N()->dimension(1); |
| 162 |
|
| 163 |
ROL::Ptr<Intrepid::FieldContainer<Real > > bdry_coeff = |
| 164 |
ROL::makePtr<Intrepid::FieldContainer<Real >>(numCellsSide, f); |
| 165 |
for (int i = 0; i < numCellsSide; ++i) { |
| 166 |
for (int j = 0; j < f; ++j) { |
| 167 |
(*bdry_coeff)(i, j) = cell_coeff(bdryCellLocId[i], j); |
| 168 |
} |
| 169 |
} |
| 170 |
return bdry_coeff; |
| 171 |
} |
| 172 |
|
| 173 |
public: |
| 174 |
QoI_Control_Cost_Allen_Cahn(const ROL::Ptr<FE<Real> > &fe_vol, |
| 175 |
const std::vector<std::vector<ROL::Ptr<FE<Real> > > > &fe_bdry, |
| 176 |
const std::vector<std::vector<std::vector<int> > > &bdryCellLocIds) |
| 177 |
: fe_vol_(fe_vol), fe_bdry_(fe_bdry), bdryCellLocIds_(bdryCellLocIds) {} |
| 178 |
|
| 179 |
Real value(ROL::Ptr<Intrepid::FieldContainer<Real> > & val, |
| 180 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 181 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 182 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 183 |
const int c = fe_vol_->gradN()->dimension(0); |
| 184 |
// Initialize output val |
| 185 |
val = ROL::makePtr<Intrepid::FieldContainer<Real>>(c); |
| 186 |
// Compute cost integral |
| 187 |
const int numSideSets = bdryCellLocIds_.size(); |
| 188 |
for (int i = 0; i < numSideSets; ++i) { |
| 189 |
const int numLocSides = bdryCellLocIds_[i].size(); |
| 190 |
for (int j = 0; j < numLocSides; ++j) { |
| 191 |
const int numCellsSide = bdryCellLocIds_[i][j].size(); |
| 192 |
if ( numCellsSide ) { |
| 193 |
const int numCubPerSide = fe_bdry_[i][j]->cubPts()->dimension(1); |
| 194 |
// Evaluate control on FE basis |
| 195 |
ROL::Ptr<Intrepid::FieldContainer<Real> > z_coeff_bdry |
| 196 |
= getBoundaryCoeff(*z_coeff, i, j); |
| 197 |
ROL::Ptr<Intrepid::FieldContainer<Real> > valZ_eval |
| 198 |
= ROL::makePtr<Intrepid::FieldContainer<Real>>(numCellsSide, numCubPerSide); |
| 199 |
fe_bdry_[i][j]->evaluateValue(valZ_eval, z_coeff_bdry); |
| 200 |
// Integrate cell cost |
| 201 |
ROL::Ptr<Intrepid::FieldContainer<Real> > intVal |
| 202 |
= ROL::makePtr<Intrepid::FieldContainer<Real>>(numCellsSide); |
| 203 |
fe_bdry_[i][j]->computeIntegral(intVal,valZ_eval,valZ_eval); |
| 204 |
// Add to integral value |
| 205 |
for (int k = 0; k < numCellsSide; ++k) { |
| 206 |
int cidx = bdryCellLocIds_[i][j][k]; |
| 207 |
(*val)(cidx) += static_cast<Real>(0.5)*(*intVal)(k); |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
return static_cast<Real>(0); |
| 213 |
} |
| 214 |
|
| 215 |
void gradient_1(ROL::Ptr<Intrepid::FieldContainer<Real> > & grad, |
| 216 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 217 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 218 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 219 |
throw Exception::Zero(">>> QoI_Control_Cost_Allen_Cahn::gradient_1 is zero."); |
| 220 |
} |
| 221 |
|
| 222 |
void gradient_2(ROL::Ptr<Intrepid::FieldContainer<Real> > & grad, |
| 223 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 224 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 225 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 226 |
const int c = fe_vol_->gradN()->dimension(0); |
| 227 |
const int f = fe_vol_->gradN()->dimension(1); |
| 228 |
// Initialize output val |
| 229 |
grad = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f); |
| 230 |
// Compute cost integral |
| 231 |
const int numSideSets = bdryCellLocIds_.size(); |
| 232 |
for (int i = 0; i < numSideSets; ++i) { |
| 233 |
const int numLocSides = bdryCellLocIds_[i].size(); |
| 234 |
for (int j = 0; j < numLocSides; ++j) { |
| 235 |
const int numCellsSide = bdryCellLocIds_[i][j].size(); |
| 236 |
if ( numCellsSide ) { |
| 237 |
const int numCubPerSide = fe_bdry_[i][j]->cubPts()->dimension(1); |
| 238 |
// Evaluate control on FE basis |
| 239 |
ROL::Ptr<Intrepid::FieldContainer<Real> > z_coeff_bdry |
| 240 |
= getBoundaryCoeff(*z_coeff, i, j); |
| 241 |
ROL::Ptr<Intrepid::FieldContainer<Real> > valZ_eval |
| 242 |
= ROL::makePtr<Intrepid::FieldContainer<Real>>(numCellsSide, numCubPerSide); |
| 243 |
fe_bdry_[i][j]->evaluateValue(valZ_eval, z_coeff_bdry); |
| 244 |
// Compute gradient of squared L2-norm of diff |
| 245 |
ROL::Ptr<Intrepid::FieldContainer<Real> > intGrad |
| 246 |
= ROL::makePtr<Intrepid::FieldContainer<Real>>(numCellsSide, f); |
| 247 |
Intrepid::FunctionSpaceTools::integrate<Real>(*intGrad, |
| 248 |
*valZ_eval, |
| 249 |
*(fe_bdry_[i][j]->NdetJ()), |
| 250 |
Intrepid::COMP_CPP, false); |
| 251 |
// Add to integral value |
| 252 |
for (int k = 0; k < numCellsSide; ++k) { |
| 253 |
int cidx = bdryCellLocIds_[i][j][k]; |
| 254 |
for (int l = 0; l < f; ++l) { |
| 255 |
(*grad)(cidx,l) += (*intGrad)(k,l); |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
void HessVec_11(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess, |
| 264 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & v_coeff, |
| 265 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 266 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 267 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 268 |
throw Exception::Zero(">>> QoI_Control_Cost_Allen_Cahn::HessVec_11 is zero."); |
| 269 |
} |
| 270 |
|
| 271 |
void HessVec_12(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess, |
| 272 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & v_coeff, |
| 273 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 274 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 275 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 276 |
throw Exception::Zero(">>> QoI_Control_Cost_Allen_Cahn::HessVec_12 is zero."); |
| 277 |
} |
| 278 |
|
| 279 |
void HessVec_21(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess, |
| 280 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & v_coeff, |
| 281 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 282 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 283 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 284 |
throw Exception::Zero(">>> QoI_Control_Cost_Allen_Cahn::HessVec_21 is zero."); |
| 285 |
} |
| 286 |
|
| 287 |
void HessVec_22(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess, |
| 288 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & v_coeff, |
| 289 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 290 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 291 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 292 |
const int c = fe_vol_->gradN()->dimension(0); |
| 293 |
const int f = fe_vol_->gradN()->dimension(1); |
| 294 |
// Initialize output val |
| 295 |
hess = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f); |
| 296 |
// Compute cost integral |
| 297 |
const int numSideSets = bdryCellLocIds_.size(); |
| 298 |
for (int i = 0; i < numSideSets; ++i) { |
| 299 |
const int numLocSides = bdryCellLocIds_[i].size(); |
| 300 |
for (int j = 0; j < numLocSides; ++j) { |
| 301 |
const int numCellsSide = bdryCellLocIds_[i][j].size(); |
| 302 |
if ( numCellsSide ) { |
| 303 |
const int numCubPerSide = fe_bdry_[i][j]->cubPts()->dimension(1); |
| 304 |
// Evaluate direction on FE basis |
| 305 |
ROL::Ptr<Intrepid::FieldContainer<Real> > v_coeff_bdry |
| 306 |
= getBoundaryCoeff(*v_coeff, i, j); |
| 307 |
ROL::Ptr<Intrepid::FieldContainer<Real> > valV_eval |
| 308 |
= ROL::makePtr<Intrepid::FieldContainer<Real>>(numCellsSide, numCubPerSide); |
| 309 |
fe_bdry_[i][j]->evaluateValue(valV_eval, v_coeff_bdry); |
| 310 |
// Compute hessian times a vector of cost |
| 311 |
ROL::Ptr<Intrepid::FieldContainer<Real> > intHess |
| 312 |
= ROL::makePtr<Intrepid::FieldContainer<Real>>(numCellsSide, f); |
| 313 |
Intrepid::FunctionSpaceTools::integrate<Real>(*intHess, |
| 314 |
*valV_eval, |
| 315 |
*(fe_bdry_[i][j]->NdetJ()), |
| 316 |
Intrepid::COMP_CPP, false); |
| 317 |
// Add to integral value |
| 318 |
for (int k = 0; k < numCellsSide; ++k) { |
| 319 |
int cidx = bdryCellLocIds_[i][j][k]; |
| 320 |
for (int l = 0; l < f; ++l) { |
| 321 |
(*hess)(cidx,l) += (*intHess)(k,l); |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
}; // QoI_L2Penalty |
| 330 |
|
| 331 |
#endif |