| 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_NONLINEAR_ELLIPTIC_HPP |
| 15 |
#define PDEOPT_QOI_NONLINEAR_ELLIPTIC_HPP |
| 16 |
|
| 17 |
#include "../TOOLS/qoi.hpp" |
| 18 |
#include "pde_nonlinear_elliptic.hpp" |
| 19 |
|
| 20 |
template <class Real> |
| 21 |
class QoI_StateTracking_Nonlinear_Elliptic : public QoI<Real> { |
| 22 |
private: |
| 23 |
ROL::Ptr<FE<Real> > fe_; |
| 24 |
|
| 25 |
ROL::Ptr<Intrepid::FieldContainer<Real> > target_; |
| 26 |
|
| 27 |
protected: |
| 28 |
void computeTracking(void) { |
| 29 |
int c = fe_->cubPts()->dimension(0); |
| 30 |
int p = fe_->cubPts()->dimension(1); |
| 31 |
int d = fe_->cubPts()->dimension(2); |
| 32 |
std::vector<Real> pt(d); |
| 33 |
target_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(c,p); |
| 34 |
for (int i = 0; i < c; ++i) { |
| 35 |
for (int j = 0; j < p; ++j) { |
| 36 |
for (int k = 0; k < d; ++k) { |
| 37 |
pt[k] = (*fe_->cubPts())(i,j,k); |
| 38 |
} |
| 39 |
(*target_)(i,j) = targetFunc(pt); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
public: |
| 45 |
QoI_StateTracking_Nonlinear_Elliptic(const ROL::Ptr<FE<Real> > &fe) : fe_(fe) { |
| 46 |
computeTracking(); |
| 47 |
} |
| 48 |
|
| 49 |
virtual Real targetFunc(const std::vector<Real> & x) const { |
| 50 |
const int size = x.size(); |
| 51 |
const Real zero(0), one(1); |
| 52 |
Real val(0); |
| 53 |
for (int i = 0; i < size; ++i) { |
| 54 |
val = std::max(val, std::min(x[i] - zero, one - x[i])); |
| 55 |
} |
| 56 |
return val; |
| 57 |
//const int size = x.size(); |
| 58 |
//bool flag = true; |
| 59 |
//for (int i = 0; i < size; ++i) { |
| 60 |
// flag *= (x[i] < static_cast<Real>(0.6))*(x[i] > static_cast<Real>(0.4)); |
| 61 |
//} |
| 62 |
//return (flag ? static_cast<Real>(1) : static_cast<Real>(0)); |
| 63 |
} |
| 64 |
|
| 65 |
Real value(ROL::Ptr<Intrepid::FieldContainer<Real> > & val, |
| 66 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 67 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 68 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 69 |
// Get relevant dimensions |
| 70 |
int c = u_coeff->dimension(0); |
| 71 |
int p = fe_->cubPts()->dimension(1); |
| 72 |
// Initialize output val |
| 73 |
val = ROL::makePtr<Intrepid::FieldContainer<Real>>(c); |
| 74 |
// Evaluate state on FE basis |
| 75 |
ROL::Ptr<Intrepid::FieldContainer<Real> > valU_eval = |
| 76 |
ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p); |
| 77 |
fe_->evaluateValue(valU_eval, u_coeff); |
| 78 |
// Compute difference between state and target |
| 79 |
Intrepid::RealSpaceTools<Real>::subtract(*valU_eval,*target_); |
| 80 |
// Compute squared L2-norm of diff |
| 81 |
fe_->computeIntegral(val,valU_eval,valU_eval); |
| 82 |
// Scale by one half |
| 83 |
Intrepid::RealSpaceTools<Real>::scale(*val,static_cast<Real>(0.5)); |
| 84 |
return static_cast<Real>(0); |
| 85 |
} |
| 86 |
|
| 87 |
void gradient_1(ROL::Ptr<Intrepid::FieldContainer<Real> > & grad, |
| 88 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 89 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 90 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 91 |
// Get relevant dimensions |
| 92 |
int c = u_coeff->dimension(0); |
| 93 |
int p = fe_->cubPts()->dimension(1); |
| 94 |
int f = fe_->N()->dimension(1); |
| 95 |
// Initialize output grad |
| 96 |
grad = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f); |
| 97 |
// Evaluate state on FE basis |
| 98 |
ROL::Ptr<Intrepid::FieldContainer<Real> > valU_eval = |
| 99 |
ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p); |
| 100 |
fe_->evaluateValue(valU_eval, u_coeff); |
| 101 |
// Compute difference between state and target |
| 102 |
Intrepid::RealSpaceTools<Real>::subtract(*valU_eval,*target_); |
| 103 |
// Compute gradient of squared L2-norm of diff |
| 104 |
Intrepid::FunctionSpaceTools::integrate<Real>(*grad, |
| 105 |
*valU_eval, |
| 106 |
*(fe_->NdetJ()), |
| 107 |
Intrepid::COMP_CPP, false); |
| 108 |
} |
| 109 |
|
| 110 |
void gradient_2(ROL::Ptr<Intrepid::FieldContainer<Real> > & grad, |
| 111 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 112 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 113 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 114 |
throw Exception::Zero(">>> QoI_L2Tracking_Nonlinear_Elliptic::gradient_2 is zero."); |
| 115 |
} |
| 116 |
|
| 117 |
void HessVec_11(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess, |
| 118 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & v_coeff, |
| 119 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 120 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 121 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 122 |
int c = v_coeff->dimension(0); |
| 123 |
int p = fe_->cubPts()->dimension(1); |
| 124 |
int f = fe_->N()->dimension(1); |
| 125 |
ROL::Ptr<Intrepid::FieldContainer<Real> > valV_eval = |
| 126 |
ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p); |
| 127 |
hess = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f); |
| 128 |
fe_->evaluateValue(valV_eval, v_coeff); |
| 129 |
Intrepid::FunctionSpaceTools::integrate<Real>(*hess, |
| 130 |
*valV_eval, |
| 131 |
*(fe_->NdetJ()), |
| 132 |
Intrepid::COMP_CPP, false); |
| 133 |
} |
| 134 |
|
| 135 |
void HessVec_12(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess, |
| 136 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & v_coeff, |
| 137 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 138 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 139 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 140 |
throw Exception::Zero(">>> QoI_L2Tracking_Nonlinear_Elliptic::HessVec_12 is zero."); |
| 141 |
} |
| 142 |
|
| 143 |
void HessVec_21(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess, |
| 144 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & v_coeff, |
| 145 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 146 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 147 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 148 |
throw Exception::Zero(">>> QoI_L2Tracking_Nonlinear_Elliptic::HessVec_21 is zero."); |
| 149 |
} |
| 150 |
|
| 151 |
void HessVec_22(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess, |
| 152 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & v_coeff, |
| 153 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 154 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 155 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 156 |
throw Exception::Zero(">>> QoI_L2Tracking_Nonlinear_Elliptic::HessVec_22 is zero."); |
| 157 |
} |
| 158 |
|
| 159 |
}; // QoI_L2Tracking |
| 160 |
|
| 161 |
template <class Real> |
| 162 |
class QoI_ControlPenalty_Nonlinear_Elliptic : public QoI<Real> { |
| 163 |
private: |
| 164 |
ROL::Ptr<FE<Real> > fe_; |
| 165 |
ROL::Ptr<Intrepid::FieldContainer<Real> > ones_; |
| 166 |
|
| 167 |
public: |
| 168 |
QoI_ControlPenalty_Nonlinear_Elliptic(const ROL::Ptr<FE<Real> > &fe) : fe_(fe) { |
| 169 |
// Get relevant dimensions |
| 170 |
int c = fe_->gradN()->dimension(0); |
| 171 |
int p = fe_->gradN()->dimension(2); |
| 172 |
// Build field container of ones |
| 173 |
ones_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(c,p); |
| 174 |
for (int i = 0; i < c; ++i) { |
| 175 |
for (int j = 0; j < p; ++j) { |
| 176 |
(*ones_)(i,j) = static_cast<Real>(1); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
Real value(ROL::Ptr<Intrepid::FieldContainer<Real> > & val, |
| 182 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 183 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 184 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 185 |
// Get relevant dimensions |
| 186 |
int c = fe_->gradN()->dimension(0); |
| 187 |
int p = fe_->gradN()->dimension(2); |
| 188 |
// Initialize output val |
| 189 |
val = ROL::makePtr<Intrepid::FieldContainer<Real>>(c); |
| 190 |
// Build local state tracking term |
| 191 |
ROL::Ptr<Intrepid::FieldContainer<Real> > valZ_eval = |
| 192 |
ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p); |
| 193 |
fe_->evaluateValue(valZ_eval, z_coeff); |
| 194 |
fe_->computeIntegral(val,ones_,valZ_eval); |
| 195 |
return static_cast<Real>(0); |
| 196 |
} |
| 197 |
|
| 198 |
void gradient_1(ROL::Ptr<Intrepid::FieldContainer<Real> > & grad, |
| 199 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 200 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 201 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 202 |
throw Exception::Zero(">>> QoI_L2Penalty_Nonlinear_Elliptic::gradient_1 is zero."); |
| 203 |
} |
| 204 |
|
| 205 |
void gradient_2(ROL::Ptr<Intrepid::FieldContainer<Real> > & grad, |
| 206 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 207 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 208 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 209 |
// Get relevant dimensions |
| 210 |
int c = fe_->gradN()->dimension(0); |
| 211 |
int f = fe_->gradN()->dimension(1); |
| 212 |
// Initialize output grad |
| 213 |
grad = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f); |
| 214 |
// Build local gradient of state tracking term |
| 215 |
Intrepid::FunctionSpaceTools::integrate<Real>(*grad, |
| 216 |
*ones_, |
| 217 |
*(fe_->NdetJ()), |
| 218 |
Intrepid::COMP_CPP, false); |
| 219 |
} |
| 220 |
|
| 221 |
void HessVec_11(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess, |
| 222 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & v_coeff, |
| 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 |
throw Exception::Zero(">>> QoI_L2Penalty_Nonlinear_Elliptic::HessVec_11 is zero."); |
| 227 |
} |
| 228 |
|
| 229 |
void HessVec_12(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess, |
| 230 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & v_coeff, |
| 231 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 232 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 233 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 234 |
throw Exception::Zero(">>> QoI_L2Penalty_Nonlinear_Elliptic::HessVec_12 is zero."); |
| 235 |
} |
| 236 |
|
| 237 |
void HessVec_21(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess, |
| 238 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & v_coeff, |
| 239 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 240 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 241 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 242 |
throw Exception::Zero(">>> QoI_L2Penalty_Nonlinear_Elliptic::HessVec_21 is zero."); |
| 243 |
} |
| 244 |
|
| 245 |
void HessVec_22(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess, |
| 246 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & v_coeff, |
| 247 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff, |
| 248 |
const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr, |
| 249 |
const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) { |
| 250 |
throw Exception::Zero(">>> QoI_L2Penalty_Nonlinear_Elliptic::HessVec_22 is zero."); |
| 251 |
} |
| 252 |
|
| 253 |
}; // QoI_L2Penalty |
| 254 |
|
| 255 |
#endif |