| 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_PHASEFIELD_HPP |
| 15 |
#define PDEOPT_QOI_PHASEFIELD_HPP |
| 16 |
|
| 17 |
#include "../../TOOLS/qoi.hpp" |
| 18 |
#include "../../TOOLS/fe.hpp" |
| 19 |
|
| 20 |
template <class Real> |
| 21 |
class QoI_ModicaMortolaEnergy_PhaseField : public QoI<Real> { |
| 22 |
private: |
| 23 |
const ROL::Ptr<FE<Real>> fe_; |
| 24 |
const Real scale_; |
| 25 |
|
| 26 |
public: |
| 27 |
QoI_ModicaMortolaEnergy_PhaseField(const ROL::Ptr<FE<Real>> &fe, |
| 28 |
const Real scale = Real(1)) |
| 29 |
: fe_(fe), scale_(scale) {} |
| 30 |
|
| 31 |
Real value(ROL::Ptr<Intrepid::FieldContainer<Real>> & val, |
| 32 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff, |
| 33 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr, |
| 34 |
const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) { |
| 35 |
const Real one(1), cPhi(0.75); |
| 36 |
// Get relevant dimensions |
| 37 |
int c = fe_->gradN()->dimension(0); |
| 38 |
int p = fe_->gradN()->dimension(2); |
| 39 |
int d = fe_->gradN()->dimension(3); |
| 40 |
// Initialize output val |
| 41 |
val = ROL::makePtr<Intrepid::FieldContainer<Real>>(c); |
| 42 |
// Evaluate on FE basis |
| 43 |
ROL::Ptr<Intrepid::FieldContainer<Real>> valZ_eval, gradZ_eval, valPhi_eval; |
| 44 |
valZ_eval = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p); |
| 45 |
gradZ_eval = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p, d); |
| 46 |
valPhi_eval = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p); |
| 47 |
fe_->evaluateValue(valZ_eval,z_coeff); |
| 48 |
fe_->evaluateGradient(gradZ_eval,z_coeff); |
| 49 |
for (int i = 0; i < c; ++i) { |
| 50 |
for (int j = 0; j < p; ++j) { |
| 51 |
Real zij = (*valZ_eval)(i,j); |
| 52 |
(*valPhi_eval)(i,j) = cPhi*(zij*zij - one)/scale_; |
| 53 |
} |
| 54 |
} |
| 55 |
// Integrate |
| 56 |
fe_->computeIntegral(val,gradZ_eval,gradZ_eval,false); |
| 57 |
fe_->computeIntegral(val,valPhi_eval,valPhi_eval,true); |
| 58 |
Intrepid::RealSpaceTools<Real>::scale(*val, static_cast<Real>(0.5)*scale_); |
| 59 |
return static_cast<Real>(0); |
| 60 |
} |
| 61 |
|
| 62 |
void gradient_1(ROL::Ptr<Intrepid::FieldContainer<Real>> & grad, |
| 63 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff, |
| 64 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr, |
| 65 |
const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) { |
| 66 |
throw Exception::Zero(">>> QoI_ModicaMortolaEnergy_PhaseField::gradient_1 is zero."); |
| 67 |
} |
| 68 |
|
| 69 |
void gradient_2(ROL::Ptr<Intrepid::FieldContainer<Real>> & grad, |
| 70 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff, |
| 71 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr, |
| 72 |
const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) { |
| 73 |
const Real one(1), cPhi(9.0/8.0); |
| 74 |
// Retrieve dimensions. |
| 75 |
int c = fe_->gradN()->dimension(0); |
| 76 |
int f = fe_->gradN()->dimension(1); |
| 77 |
int p = fe_->gradN()->dimension(2); |
| 78 |
int d = fe_->gradN()->dimension(3); |
| 79 |
// Initialize Gradients. |
| 80 |
grad = ROL::makePtr<Intrepid::FieldContainer<Real>>(c,f); |
| 81 |
// Evaluate/interpolate finite element fields on cells. |
| 82 |
ROL::Ptr<Intrepid::FieldContainer<Real>> valZ_eval, gradZ_eval, valPhi_eval; |
| 83 |
valZ_eval = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p); |
| 84 |
gradZ_eval = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p, d); |
| 85 |
valPhi_eval = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p); |
| 86 |
fe_->evaluateValue(valZ_eval,z_coeff); |
| 87 |
fe_->evaluateGradient(gradZ_eval,z_coeff); |
| 88 |
for (int i = 0; i < c; ++i) { |
| 89 |
for (int j = 0; j < p; ++j) { |
| 90 |
Real zij = (*valZ_eval)(i,j); |
| 91 |
(*valPhi_eval)(i,j) = cPhi*zij*(zij*zij-one)/scale_; |
| 92 |
} |
| 93 |
} |
| 94 |
// Evaluate gradient of energy. |
| 95 |
Intrepid::FunctionSpaceTools::integrate<Real>(*grad, |
| 96 |
*gradZ_eval, // grad(Z) |
| 97 |
*fe_->gradNdetJ(), // grad(N) |
| 98 |
Intrepid::COMP_CPP, |
| 99 |
false); |
| 100 |
Intrepid::RealSpaceTools<Real>::scale(*grad,scale_); |
| 101 |
Intrepid::FunctionSpaceTools::integrate<Real>(*grad, |
| 102 |
*valPhi_eval, // Phi'(Z) |
| 103 |
*fe_->NdetJ(), // N |
| 104 |
Intrepid::COMP_CPP, |
| 105 |
true); |
| 106 |
} |
| 107 |
|
| 108 |
void HessVec_11(ROL::Ptr<Intrepid::FieldContainer<Real>> & hess, |
| 109 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & v_coeff, |
| 110 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff, |
| 111 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr, |
| 112 |
const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) { |
| 113 |
throw Exception::Zero(">>> QoI_ModicaMortolaEnergy_PhaseField::HessVec_11 is zero."); |
| 114 |
} |
| 115 |
|
| 116 |
void HessVec_12(ROL::Ptr<Intrepid::FieldContainer<Real>> & hess, |
| 117 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & v_coeff, |
| 118 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff, |
| 119 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr, |
| 120 |
const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) { |
| 121 |
throw Exception::Zero(">>> QoI_ModicaMortolaEnergy_PhaseField::HessVec_12 is zero."); |
| 122 |
} |
| 123 |
|
| 124 |
void HessVec_21(ROL::Ptr<Intrepid::FieldContainer<Real>> & hess, |
| 125 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & v_coeff, |
| 126 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff, |
| 127 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr, |
| 128 |
const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) { |
| 129 |
throw Exception::Zero(">>> QoI_ModicaMortolaEnergy_PhaseField::HessVec_21 is zero."); |
| 130 |
} |
| 131 |
|
| 132 |
void HessVec_22(ROL::Ptr<Intrepid::FieldContainer<Real>> & hess, |
| 133 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & v_coeff, |
| 134 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff, |
| 135 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr, |
| 136 |
const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) { |
| 137 |
const Real one(1), three(3), cPhi(9.0/8.0); |
| 138 |
// Retrieve dimensions. |
| 139 |
int c = fe_->gradN()->dimension(0); |
| 140 |
int f = fe_->gradN()->dimension(1); |
| 141 |
int p = fe_->gradN()->dimension(2); |
| 142 |
int d = fe_->gradN()->dimension(3); |
| 143 |
// Initialize Hessians. |
| 144 |
hess = ROL::makePtr<Intrepid::FieldContainer<Real>>(c,f); |
| 145 |
// Evaluate/interpolate finite element fields on cells. |
| 146 |
ROL::Ptr<Intrepid::FieldContainer<Real>> valZ_eval, valV_eval, gradV_eval, valPhi_eval; |
| 147 |
valZ_eval = ROL::makePtr<Intrepid::FieldContainer<Real>>(c,p); |
| 148 |
valV_eval = ROL::makePtr<Intrepid::FieldContainer<Real>>(c,p); |
| 149 |
gradV_eval = ROL::makePtr<Intrepid::FieldContainer<Real>>(c,p,d); |
| 150 |
valPhi_eval = ROL::makePtr<Intrepid::FieldContainer<Real>>(c,p); |
| 151 |
fe_->evaluateValue(valZ_eval,z_coeff); |
| 152 |
fe_->evaluateValue(valV_eval,v_coeff); |
| 153 |
fe_->evaluateGradient(gradV_eval,v_coeff); |
| 154 |
for (int i = 0; i < c; ++i) { |
| 155 |
for (int j = 0; j < p; ++j) { |
| 156 |
Real zij = (*valZ_eval)(i,j); |
| 157 |
(*valPhi_eval)(i,j) = (*valV_eval)(i,j)*cPhi*(three*zij*zij-one)/scale_; |
| 158 |
} |
| 159 |
} |
| 160 |
// Evaluate hessian-times-vector of energy. |
| 161 |
Intrepid::FunctionSpaceTools::integrate<Real>(*hess, |
| 162 |
*gradV_eval, // grad(V) |
| 163 |
*fe_->gradNdetJ(), // grad(N) |
| 164 |
Intrepid::COMP_CPP, |
| 165 |
false); |
| 166 |
Intrepid::RealSpaceTools<Real>::scale(*hess,scale_); |
| 167 |
Intrepid::FunctionSpaceTools::integrate<Real>(*hess, |
| 168 |
*valPhi_eval, // Phi''(Z)V |
| 169 |
*fe_->NdetJ(), // N |
| 170 |
Intrepid::COMP_CPP, |
| 171 |
true); |
| 172 |
} |
| 173 |
|
| 174 |
}; // QoI_ModicaMortolaEnergy_PhaseField |
| 175 |
|
| 176 |
template <class Real> |
| 177 |
class QoI_Volume_PhaseField : public QoI<Real> { |
| 178 |
private: |
| 179 |
const ROL::Ptr<FE<Real>> fe_; |
| 180 |
|
| 181 |
public: |
| 182 |
QoI_Volume_PhaseField(const ROL::Ptr<FE<Real>> &fe) : fe_(fe) {} |
| 183 |
|
| 184 |
Real value(ROL::Ptr<Intrepid::FieldContainer<Real>> & val, |
| 185 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff, |
| 186 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr, |
| 187 |
const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) { |
| 188 |
// Get relevant dimensions |
| 189 |
int c = fe_->gradN()->dimension(0); |
| 190 |
int p = fe_->gradN()->dimension(2); |
| 191 |
// Initialize output val |
| 192 |
val = ROL::makePtr<Intrepid::FieldContainer<Real>>(c); |
| 193 |
// Evaluate on FE basis |
| 194 |
ROL::Ptr<Intrepid::FieldContainer<Real>> valZ_eval |
| 195 |
= ROL::makePtr<Intrepid::FieldContainer<Real>>(c,p); |
| 196 |
fe_->evaluateValue(valZ_eval,z_coeff); |
| 197 |
// Approximate characteristic function |
| 198 |
for (int i = 0; i < c; ++i) { |
| 199 |
for (int j = 0; j < p; ++j) { |
| 200 |
(*valZ_eval)(i,j) += static_cast<Real>(1); |
| 201 |
} |
| 202 |
} |
| 203 |
// Compute volume |
| 204 |
fe_->computeIntegral(val,valZ_eval,valZ_eval); |
| 205 |
Intrepid::RealSpaceTools<Real>::scale(*val,static_cast<Real>(0.25)); |
| 206 |
return static_cast<Real>(0); |
| 207 |
} |
| 208 |
|
| 209 |
void gradient_1(ROL::Ptr<Intrepid::FieldContainer<Real>> & grad, |
| 210 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff, |
| 211 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr, |
| 212 |
const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) { |
| 213 |
throw Exception::Zero(">>> QoI_Volume_PhaseField::gradient_1 is zero."); |
| 214 |
} |
| 215 |
|
| 216 |
void gradient_2(ROL::Ptr<Intrepid::FieldContainer<Real>> & grad, |
| 217 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff, |
| 218 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr, |
| 219 |
const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) { |
| 220 |
// Get relevant dimensions |
| 221 |
int c = fe_->gradN()->dimension(0); |
| 222 |
int f = fe_->gradN()->dimension(1); |
| 223 |
int p = fe_->gradN()->dimension(2); |
| 224 |
int d = fe_->gradN()->dimension(3); |
| 225 |
// Initialize output grad |
| 226 |
grad = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f); |
| 227 |
// Evaluate on FE basis |
| 228 |
ROL::Ptr<Intrepid::FieldContainer<Real>> valZ_eval |
| 229 |
= ROL::makePtr<Intrepid::FieldContainer<Real>>(c,p); |
| 230 |
fe_->evaluateValue(valZ_eval,z_coeff); |
| 231 |
// Approximate derivative of characteristic function |
| 232 |
for (int i = 0; i < c; ++i) { |
| 233 |
for (int j = 0; j < p; ++j) { |
| 234 |
(*valZ_eval)(i,j) += static_cast<Real>(1); |
| 235 |
} |
| 236 |
} |
| 237 |
// Compute gradient of volume |
| 238 |
Intrepid::FunctionSpaceTools::integrate<Real>(*grad, |
| 239 |
*valZ_eval, |
| 240 |
*(fe_->NdetJ()), |
| 241 |
Intrepid::COMP_CPP, false); |
| 242 |
Intrepid::RealSpaceTools<Real>::scale(*grad,static_cast<Real>(0.5)); |
| 243 |
} |
| 244 |
|
| 245 |
void HessVec_11(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_Volume_PhaseField::HessVec_11 is zero."); |
| 251 |
} |
| 252 |
|
| 253 |
void HessVec_12(ROL::Ptr<Intrepid::FieldContainer<Real>> & hess, |
| 254 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & v_coeff, |
| 255 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff, |
| 256 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr, |
| 257 |
const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) { |
| 258 |
throw Exception::Zero(">>> QoI_Volume_PhaseField::HessVec_12 is zero."); |
| 259 |
} |
| 260 |
|
| 261 |
void HessVec_21(ROL::Ptr<Intrepid::FieldContainer<Real>> & hess, |
| 262 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & v_coeff, |
| 263 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff, |
| 264 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr, |
| 265 |
const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) { |
| 266 |
throw Exception::Zero(">>> QoI_Volume_PhaseField::HessVec_21 is zero."); |
| 267 |
} |
| 268 |
|
| 269 |
void HessVec_22(ROL::Ptr<Intrepid::FieldContainer<Real>> & hess, |
| 270 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & v_coeff, |
| 271 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff, |
| 272 |
const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr, |
| 273 |
const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) { |
| 274 |
// Get relevant dimensions |
| 275 |
int c = fe_->gradN()->dimension(0); |
| 276 |
int f = fe_->gradN()->dimension(1); |
| 277 |
int p = fe_->gradN()->dimension(2); |
| 278 |
int d = fe_->gradN()->dimension(3); |
| 279 |
// Initialize output grad |
| 280 |
hess = ROL::makePtr<Intrepid::FieldContainer<Real>>(c,f); |
| 281 |
// Evaluate on FE basis |
| 282 |
ROL::Ptr<Intrepid::FieldContainer<Real>> valV_eval |
| 283 |
= ROL::makePtr<Intrepid::FieldContainer<Real>>(c,p); |
| 284 |
fe_->evaluateValue(valV_eval,v_coeff); |
| 285 |
// Compute gradient of volume |
| 286 |
Intrepid::FunctionSpaceTools::integrate<Real>(*hess, |
| 287 |
*valV_eval, |
| 288 |
*(fe_->NdetJ()), |
| 289 |
Intrepid::COMP_CPP, false); |
| 290 |
Intrepid::RealSpaceTools<Real>::scale(*hess,static_cast<Real>(0.5)); |
| 291 |
} |
| 292 |
|
| 293 |
}; // QoI_Volume_PhaseField |
| 294 |
|
| 295 |
#endif |