Debian Codegraph @

packages/rol/example/PDE-OPT/nonlinear-elliptic/pde_nonlinear_elliptic.hpp

raw
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 pde.hpp
11 \brief Implements the local PDE interface for the Poisson-Boltzmann control problem.
12 */
13
14 #ifndef PDE_NONLINEAR_ELLIPTIC_HPP
15 #define PDE_NONLINEAR_ELLIPTIC_HPP
16
17 #include "../TOOLS/pde.hpp"
18 #include "../TOOLS/fe.hpp"
19
20 #include "Intrepid_HGRAD_QUAD_C1_FEM.hpp"
21 #include "Intrepid_HGRAD_QUAD_C2_FEM.hpp"
22 #include "Intrepid_DefaultCubatureFactory.hpp"
23 #include "Intrepid_FunctionSpaceTools.hpp"
24 #include "Intrepid_CellTools.hpp"
25
26 #include "ROL_Ptr.hpp"
27
28 template <class Real>
29 class PDE_Nonlinear_Elliptic : public PDE<Real> {
30 private:
31 // Finite element basis information
32 ROL::Ptr<Intrepid::Basis<Real, Intrepid::FieldContainer<Real> > > basisPtr_;
33 std::vector<ROL::Ptr<Intrepid::Basis<Real, Intrepid::FieldContainer<Real> > > > basisPtrs_;
34 // Cell cubature information
35 ROL::Ptr<Intrepid::Cubature<Real> > cellCub_;
36 // Cell node information
37 ROL::Ptr<Intrepid::FieldContainer<Real> > volCellNodes_;
38 std::vector<std::vector<ROL::Ptr<Intrepid::FieldContainer<Real> > > > bdryCellNodes_;
39 std::vector<std::vector<std::vector<int> > > bdryCellLocIds_;
40 // Finite element definition
41 ROL::Ptr<FE<Real> > fe_;
42
43 void computeBetaGradU(const ROL::Ptr<Intrepid::FieldContainer<Real> > &BgradU,
44 const ROL::Ptr<const Intrepid::FieldContainer<Real> > &gradU) const {
45 // GET DIMENSIONS
46 int c = fe_->gradN()->dimension(0);
47 int p = fe_->gradN()->dimension(2);
48 int d = fe_->gradN()->dimension(3);
49 std::vector<Real> U(d), BU(d);
50 for (int i = 0; i < c; ++i) {
51 for (int j = 0; j < p; ++j) {
52 for (int k = 0; k < d; ++k) {
53 U[k] = (*gradU)(i,j,k);
54 }
55 beta_value(BU,U);
56 for (int k = 0; k < d; ++k) {
57 (*BgradU)(i,j,k) = BU[k];
58 }
59 }
60 }
61 }
62
63 void computeBetaJacobianGradU(const ROL::Ptr<Intrepid::FieldContainer<Real> > &BgradU,
64 const ROL::Ptr<const Intrepid::FieldContainer<Real> > &gradU) const {
65 // GET DIMENSIONS
66 int c = fe_->gradN()->dimension(0);
67 int p = fe_->gradN()->dimension(2);
68 int d = fe_->gradN()->dimension(3);
69 std::vector<Real> U(d);
70 std::vector<std::vector<Real> > BU(d, U);
71 for (int i = 0; i < c; ++i) {
72 for (int j = 0; j < p; ++j) {
73 for (int k = 0; k < d; ++k) {
74 U[k] = (*gradU)(i,j,k);
75 }
76 beta_jacobian(BU,U);
77 for (int k = 0; k < d; ++k) {
78 for (int m = 0; m < d; ++m) {
79 (*BgradU)(i,j,k,m) = BU[k][m];
80 }
81 }
82 }
83 }
84 }
85
86 void computeBetaHessianGradUGradL(const ROL::Ptr<Intrepid::FieldContainer<Real> > &BgradUgradL,
87 const ROL::Ptr<const Intrepid::FieldContainer<Real> > &gradU,
88 const ROL::Ptr<const Intrepid::FieldContainer<Real> > &gradL) const {
89 // GET DIMENSIONS
90 int c = fe_->gradN()->dimension(0);
91 int p = fe_->gradN()->dimension(2);
92 int d = fe_->gradN()->dimension(3);
93 std::vector<Real> U(d), L(d);
94 std::vector<std::vector<Real> > BU(d, U);
95 for (int i = 0; i < c; ++i) {
96 for (int j = 0; j < p; ++j) {
97 for (int k = 0; k < d; ++k) {
98 U[k] = (*gradU)(i,j,k);
99 L[k] = (*gradL)(i,j,k);
100 }
101 beta_hessian(BU,U,L);
102 for (int k = 0; k < d; ++k) {
103 for (int m = 0; m < d; ++m) {
104 (*BgradUgradL)(i,j,k,m) = BU[k][m];
105 }
106 }
107 }
108 }
109 }
110
111 void computeDiffusivity(ROL::Ptr<Intrepid::FieldContainer<Real> > &kappa) const {
112 // GET DIMENSIONS
113 int c = fe_->gradN()->dimension(0);
114 int p = fe_->gradN()->dimension(2);
115 int d = fe_->gradN()->dimension(3);
116 std::vector<Real> pt(d);
117 for (int i = 0; i < c; ++i) {
118 for (int j = 0; j < p; ++j) {
119 for ( int k = 0; k < d; ++k) {
120 pt[k] = (*fe_->cubPts())(i,j,k);
121 }
122 // Compute diffusivity kappa
123 (*kappa)(i,j) = kappa_value(pt);
124 }
125 }
126 }
127
128 void computeRHS(ROL::Ptr<Intrepid::FieldContainer<Real> > &rhs) const {
129 // GET DIMENSIONS
130 int c = fe_->gradN()->dimension(0);
131 int p = fe_->gradN()->dimension(2);
132 int d = fe_->gradN()->dimension(3);
133 std::vector<Real> pt(d);
134 for (int i = 0; i < c; ++i) {
135 for (int j = 0; j < p; ++j) {
136 for ( int k = 0; k < d; ++k) {
137 pt[k] = (*fe_->cubPts())(i,j,k);
138 }
139 // Compute diffusivity kappa
140 (*rhs)(i,j) = -rhs_value(pt);
141 }
142 }
143 }
144
145 public:
146 PDE_Nonlinear_Elliptic(Teuchos::ParameterList &parlist) {
147 // Finite element fields.
148 int basisOrder = parlist.sublist("Problem").get("Basis Order",1);
149 if (basisOrder == 1) {
150 basisPtr_ = ROL::makePtr<Intrepid::Basis_HGRAD_QUAD_C1_FEM<Real, Intrepid::FieldContainer<Real> >>();
151 }
152 else if (basisOrder == 2) {
153 basisPtr_ = ROL::makePtr<Intrepid::Basis_HGRAD_QUAD_C2_FEM<Real, Intrepid::FieldContainer<Real> >>();
154 }
155 basisPtrs_.clear(); basisPtrs_.push_back(basisPtr_);
156 // Quadrature rules.
157 shards::CellTopology cellType = basisPtr_->getBaseCellTopology(); // get the cell type from any basis
158 Intrepid::DefaultCubatureFactory<Real> cubFactory; // create cubature factory
159 int cubDegree = parlist.sublist("Problem").get("Cubature Degree",2); // set cubature degree, e.g., 2
160 cellCub_ = cubFactory.create(cellType, cubDegree); // create default cubature
161 }
162
163 virtual void beta_value(std::vector<Real> &BU,
164 const std::vector<Real> &U) const {
165 const int size = U.size();
166 Real magU(0);
167 for (int i = 0; i < size; ++i) {
168 magU += U[i] * U[i];
169 }
170 for (int i = 0; i < size; ++i) {
171 BU[i] = magU * U[i];
172 }
173 }
174
175 virtual void beta_jacobian(std::vector<std::vector<Real> > &BU,
176 const std::vector<Real> &U) const {
177 const int size = U.size();
178 Real magU(0);
179 for (int i = 0; i < size; ++i) {
180 magU += U[i] * U[i];
181 }
182 for (int i = 0; i < size; ++i) {
183 for (int j = 0; j < size; ++j) {
184 BU[i][j] = static_cast<Real>(2) * U[j] * U[i];
185 }
186 BU[i][i] += magU;
187 }
188 }
189
190 virtual void beta_hessian(std::vector<std::vector<Real> > &BU,
191 const std::vector<Real> &U,
192 const std::vector<Real> &L) const {
193 const int size = U.size();
194 Real UL(0);
195 for (int i = 0; i < size; ++i) {
196 UL += static_cast<Real>(2) * U[i] * L[i];
197 }
198 for (int i = 0; i < size; ++i) {
199 for (int j = 0; j < size; ++j) {
200 BU[i][j] = static_cast<Real>(2) * (L[j] * U[i] + L[i] * U[j]);
201 }
202 BU[i][i] += UL;
203 }
204 }
205
206 Real kappa_value(const std::vector<Real> &x) const {
207 const std::vector<Real> param = PDE<Real>::getParameter();
208 const int ns = param.size(), d = x.size();
209 if (ns > d+1) {
210 // random diffusion coefficient from i. babuska, f. nobile, r. tempone 2010.
211 // simplified model for random stratified media.
212 const Real one(1), two(2), three(3), eight(8), sixteen(16), half(0.5);
213 const Real lc = one/sixteen, sqrtpi = std::sqrt(M_PI);
214 const Real xi = std::sqrt(sqrtpi*lc), sqrt3 = std::sqrt(three);
215 Real f(0), phi(0);
216 Real val = one + sqrt3*param[0]*std::sqrt(sqrtpi*lc*half);
217 Real arg = one + sqrt3*std::sqrt(sqrtpi*lc*half);
218 for (int i = 1; i < ns-(d+1); ++i) {
219 f = floor(half*static_cast<Real>(i+1));
220 phi = ((i+1)%2 ? std::sin(f*M_PI*x[0]) : std::cos(f*M_PI*x[0]));
221 val += xi*std::exp(-std::pow(f*M_PI*lc,two)/eight)*phi*sqrt3*param[i];
222 arg += xi*std::exp(-std::pow(f*M_PI*lc,two)/eight)*std::abs(phi)*sqrt3;
223 }
224 return half + two*std::exp(val)/std::exp(arg);
225 }
226 return static_cast<Real>(1);
227 }
228
229 Real rhs_value(const std::vector<Real> &x) const {
230 const std::vector<Real> param = PDE<Real>::getParameter();
231 const int ns = param.size(), d = x.size();
232 if (ns) {
233 const int nk = ns - (d+1);
234 const Real ten(10), two(2), pi(M_PI);
235 Real val = std::pow(ten,two*param[nk]-two);
236 for (int i = 0; i < d; ++i) {
237 val *= std::sin(two*param[nk+i+1]*pi*x[i]);
238 }
239 return val;
240 }
241 return static_cast<Real>(0);
242 }
243
244 void residual(ROL::Ptr<Intrepid::FieldContainer<Real> > & res,
245 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff,
246 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr,
247 const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) {
248 // GET DIMENSIONS
249 int c = fe_->gradN()->dimension(0);
250 int f = fe_->gradN()->dimension(1);
251 int p = fe_->gradN()->dimension(2);
252 int d = fe_->gradN()->dimension(3);
253 // INITIALIZE RESIDUAL
254 res = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f);
255 // COMPUTE STIFFNESS TERM
256 ROL::Ptr<Intrepid::FieldContainer<Real> > gradU_eval =
257 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p, d);
258 fe_->evaluateGradient(gradU_eval, u_coeff);
259 ROL::Ptr<Intrepid::FieldContainer<Real> > kappa =
260 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p);
261 computeDiffusivity(kappa);
262 ROL::Ptr<Intrepid::FieldContainer<Real> > KgradU =
263 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p, d);
264 Intrepid::FunctionSpaceTools::scalarMultiplyDataData<Real>(*KgradU,
265 *kappa,
266 *gradU_eval);
267 Intrepid::FunctionSpaceTools::integrate<Real>(*res,
268 *KgradU,
269 *(fe_->gradNdetJ()),
270 Intrepid::COMP_CPP, false);
271 // ADD MASS TERM
272 ROL::Ptr<Intrepid::FieldContainer<Real> > valU_eval =
273 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p);
274 fe_->evaluateValue(valU_eval, u_coeff);
275 Intrepid::FunctionSpaceTools::integrate<Real>(*res,
276 *valU_eval,
277 *(fe_->NdetJ()),
278 Intrepid::COMP_CPP, true);
279 // ADD NONLINEAR TERM
280 ROL::Ptr<Intrepid::FieldContainer<Real> > BgradU
281 = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p, d);
282 computeBetaGradU(BgradU,gradU_eval);
283 Intrepid::FunctionSpaceTools::integrate<Real>(*res,
284 *BgradU,
285 *(fe_->gradNdetJ()),
286 Intrepid::COMP_CPP, true);
287 // ADD RHS
288 ROL::Ptr<Intrepid::FieldContainer<Real> > rhs =
289 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p);
290 computeRHS(rhs);
291 Intrepid::FunctionSpaceTools::integrate<Real>(*res,
292 *rhs,
293 *(fe_->NdetJ()),
294 Intrepid::COMP_CPP, true);
295 // ADD CONTROL TERM
296 if ( z_coeff != ROL::nullPtr ) {
297 ROL::Ptr<Intrepid::FieldContainer<Real> > valZ_eval =
298 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p);
299 fe_->evaluateValue(valZ_eval, z_coeff);
300 Intrepid::RealSpaceTools<Real>::scale(*valZ_eval,static_cast<Real>(-1));
301 Intrepid::FunctionSpaceTools::integrate<Real>(*res,
302 *valZ_eval,
303 *(fe_->NdetJ()),
304 Intrepid::COMP_CPP, true);
305 }
306 }
307
308 void Jacobian_1(ROL::Ptr<Intrepid::FieldContainer<Real> > & jac,
309 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff,
310 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr,
311 const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) {
312 // GET DIMENSIONS
313 int c = fe_->gradN()->dimension(0);
314 int f = fe_->gradN()->dimension(1);
315 int p = fe_->gradN()->dimension(2);
316 int d = fe_->gradN()->dimension(3);
317 // INITIALIZE JACOBIAN
318 jac = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f, f);
319 // COMPUTE STIFFNESS TERM
320 ROL::Ptr<Intrepid::FieldContainer<Real> > kappa =
321 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p);
322 computeDiffusivity(kappa);
323 ROL::Ptr<Intrepid::FieldContainer<Real> > KgradN =
324 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f, p, d);
325 Intrepid::FunctionSpaceTools::scalarMultiplyDataField<Real>(*KgradN,
326 *kappa,
327 *(fe_->gradN()));
328 Intrepid::FunctionSpaceTools::integrate<Real>(*jac,
329 *KgradN,
330 *(fe_->gradNdetJ()),
331 Intrepid::COMP_CPP, false);
332 // ADD MASS TERM
333 Intrepid::FunctionSpaceTools::integrate<Real>(*jac,
334 *(fe_->N()),
335 *(fe_->NdetJ()),
336 Intrepid::COMP_CPP, true);
337 // ADD NONLINEAR TERM
338 ROL::Ptr<Intrepid::FieldContainer<Real> > gradU_eval
339 = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p, d);
340 fe_->evaluateGradient(gradU_eval, u_coeff);
341 ROL::Ptr<Intrepid::FieldContainer<Real> > BgradU
342 = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p, d, d);
343 computeBetaJacobianGradU(BgradU,gradU_eval);
344 ROL::Ptr<Intrepid::FieldContainer<Real> > BgradUgradN
345 = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f, p, d);
346 Intrepid::FunctionSpaceTools::tensorMultiplyDataField<Real>(*BgradUgradN,
347 *BgradU,
348 *(fe_->gradN()));
349 Intrepid::FunctionSpaceTools::integrate<Real>(*jac,
350 *BgradUgradN,
351 *(fe_->gradNdetJ()),
352 Intrepid::COMP_CPP, true);
353 }
354
355 void Jacobian_2(ROL::Ptr<Intrepid::FieldContainer<Real> > & jac,
356 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff,
357 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr,
358 const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) {
359 if ( z_coeff != ROL::nullPtr ) {
360 // GET DIMENSIONS
361 int c = fe_->N()->dimension(0);
362 int f = fe_->N()->dimension(1);
363 // INITIALIZE JACOBIAN
364 jac = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f, f);
365 // ADD CONTROL TERM
366 Intrepid::FunctionSpaceTools::integrate<Real>(*jac,
367 *(fe_->N()),
368 *(fe_->NdetJ()),
369 Intrepid::COMP_CPP, false);
370 Intrepid::RealSpaceTools<Real>::scale(*jac,static_cast<Real>(-1));
371 }
372 }
373
374 void Hessian_11(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess,
375 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & l_coeff,
376 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff,
377 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr,
378 const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) {
379 // GET DIMENSIONS
380 int c = fe_->gradN()->dimension(0);
381 int f = fe_->gradN()->dimension(1);
382 int p = fe_->gradN()->dimension(2);
383 int d = fe_->gradN()->dimension(3);
384 // INITIALIZE JACOBIAN
385 hess = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f, f);
386 // COMPUTE NONLINEAR TERM
387 ROL::Ptr<Intrepid::FieldContainer<Real> > gradU_eval
388 = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p, d);
389 fe_->evaluateGradient(gradU_eval, u_coeff);
390 ROL::Ptr<Intrepid::FieldContainer<Real> > gradL_eval
391 = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p, d);
392 fe_->evaluateGradient(gradL_eval, l_coeff);
393 ROL::Ptr<Intrepid::FieldContainer<Real> > BgradUgradL
394 = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p, d, d);
395 computeBetaHessianGradUGradL(BgradUgradL,gradU_eval,gradL_eval);
396 ROL::Ptr<Intrepid::FieldContainer<Real> > BgradUgradLgradN
397 = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f, p, d);
398 Intrepid::FunctionSpaceTools::tensorMultiplyDataField<Real>(*BgradUgradLgradN,
399 *BgradUgradL,
400 *(fe_->gradN()));
401 Intrepid::FunctionSpaceTools::integrate<Real>(*hess,
402 *BgradUgradLgradN,
403 *(fe_->gradNdetJ()),
404 Intrepid::COMP_CPP, false);
405 }
406
407 void Hessian_12(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess,
408 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & l_coeff,
409 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff,
410 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr,
411 const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) {
412 throw Exception::Zero(">>> (PDE_Nonlinear_Elliptic:Hessian_12: Hessian is zero.");
413 }
414
415 void Hessian_21(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess,
416 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & l_coeff,
417 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff,
418 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr,
419 const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) {
420 throw Exception::Zero(">>> (PDE_Nonlinear_Elliptic:Hessian_21: Hessian is zero.");
421 }
422
423 void Hessian_22(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess,
424 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & l_coeff,
425 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff,
426 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr,
427 const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) {
428 throw Exception::Zero(">>> (PDE_Nonlinear_Elliptic:Hessian_22: Hessian is zero.");
429 }
430
431 void RieszMap_1(ROL::Ptr<Intrepid::FieldContainer<Real> > & riesz) {
432 // GET DIMENSIONS
433 int c = fe_->N()->dimension(0);
434 int f = fe_->N()->dimension(1);
435 // INITIALIZE RIESZ
436 riesz = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f, f);
437 *riesz = *fe_->stiffMat();
438 Intrepid::RealSpaceTools<Real>::add(*riesz,*(fe_->massMat()));
439 }
440
441 void RieszMap_2(ROL::Ptr<Intrepid::FieldContainer<Real> > & riesz) {
442 // GET DIMENSIONS
443 int c = fe_->N()->dimension(0);
444 int f = fe_->N()->dimension(1);
445 // INITIALIZE RIESZ
446 riesz = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f, f);
447 *riesz = *fe_->massMat();
448 }
449
450 std::vector<ROL::Ptr<Intrepid::Basis<Real, Intrepid::FieldContainer<Real> > > > getFields() {
451 return basisPtrs_;
452 }
453
454 void setCellNodes(const ROL::Ptr<Intrepid::FieldContainer<Real> > &volCellNodes,
455 const std::vector<std::vector<ROL::Ptr<Intrepid::FieldContainer<Real> > > > &bdryCellNodes,
456 const std::vector<std::vector<std::vector<int> > > &bdryCellLocIds) {
457 volCellNodes_ = volCellNodes;
458 bdryCellNodes_ = bdryCellNodes;
459 bdryCellLocIds_ = bdryCellLocIds;
460 // Finite element definition.
461 fe_ = ROL::makePtr<FE<Real>>(volCellNodes_,basisPtr_,cellCub_);
462 }
463
464 const ROL::Ptr<FE<Real> > getFE(void) const {
465 return fe_;
466 }
467
468 }; // PDE_Nonlinear_Elliptic
469
470 #endif