Debian Codegraph @

packages/rol/example/PDE-OPT/poisson-boltzmann/pde_poisson_boltzmann.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_POISSON_BOLTZMANN_HPP
15 #define PDE_POISSON_BOLTZMANN_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_Poisson_Boltzmann : 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_vol_;
42
43 public:
44 PDE_Poisson_Boltzmann(Teuchos::ParameterList &parlist) {
45 // Finite element fields.
46 int basisOrder = parlist.sublist("Problem").get("Order of FE discretization",1);
47 if (basisOrder == 1) {
48 basisPtr_ = ROL::makePtr<Intrepid::Basis_HGRAD_QUAD_C1_FEM<Real, Intrepid::FieldContainer<Real> >>();
49 }
50 else if (basisOrder == 2) {
51 basisPtr_ = ROL::makePtr<Intrepid::Basis_HGRAD_QUAD_C2_FEM<Real, Intrepid::FieldContainer<Real> >>();
52 }
53 basisPtrs_.clear(); basisPtrs_.push_back(basisPtr_);
54 // Quadrature rules.
55 shards::CellTopology cellType = basisPtr_->getBaseCellTopology(); // get the cell type from any basis
56 Intrepid::DefaultCubatureFactory<Real> cubFactory; // create cubature factory
57 int cubDegree = parlist.sublist("PDE Poisson Boltzmann").get("Cubature Degree",2); // set cubature degree, e.g., 2
58 cellCub_ = cubFactory.create(cellType, cubDegree); // create default cubature
59 }
60
61 void residual(ROL::Ptr<Intrepid::FieldContainer<Real> > & res,
62 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff,
63 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr,
64 const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) {
65 // GET DIMENSIONS
66 int c = fe_vol_->N()->dimension(0);
67 int f = fe_vol_->N()->dimension(1);
68 int p = fe_vol_->N()->dimension(2);
69 int d = cellCub_->getDimension();
70 // INITIALIZE RESIDUAL
71 res = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f);
72 // COMPUTE STIFFNESS TERM
73 ROL::Ptr<Intrepid::FieldContainer<Real> > gradU_eval =
74 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p, d);
75 fe_vol_->evaluateGradient(gradU_eval, u_coeff);
76 Intrepid::FunctionSpaceTools::integrate<Real>(*res,
77 *gradU_eval,
78 *(fe_vol_->gradNdetJ()),
79 Intrepid::COMP_CPP, false);
80 // ADD NONLINEAR TERM
81 ROL::Ptr<Intrepid::FieldContainer<Real> > valU_eval =
82 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p);
83 fe_vol_->evaluateValue(valU_eval, u_coeff);
84 for (int i = 0; i < c; ++i) {
85 for (int j = 0; j < p; ++j) {
86 (*valU_eval)(i,j) = std::exp(-(*valU_eval)(i,j));
87 }
88 }
89 Intrepid::FunctionSpaceTools::integrate<Real>(*res,
90 *valU_eval,
91 *(fe_vol_->NdetJ()),
92 Intrepid::COMP_CPP, true);
93 // ADD CONTROL TERM
94 if ( z_coeff != ROL::nullPtr ) {
95 ROL::Ptr<Intrepid::FieldContainer<Real> > valZ_eval =
96 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p);
97 fe_vol_->evaluateValue(valZ_eval, z_coeff);
98 Intrepid::RealSpaceTools<Real>::scale(*valZ_eval,static_cast<Real>(-1));
99 Intrepid::FunctionSpaceTools::integrate<Real>(*res,
100 *valZ_eval,
101 *(fe_vol_->NdetJ()),
102 Intrepid::COMP_CPP, true);
103 }
104 }
105
106 void Jacobian_1(ROL::Ptr<Intrepid::FieldContainer<Real> > & jac,
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 // GET DIMENSIONS
111 int c = fe_vol_->N()->dimension(0);
112 int f = fe_vol_->N()->dimension(1);
113 int p = fe_vol_->N()->dimension(2);
114 // INITIALIZE JACOBIAN
115 jac = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f, f);
116 // COMPUTE STIFFNESS TERM
117 Intrepid::FunctionSpaceTools::integrate<Real>(*jac,
118 *(fe_vol_->gradN()),
119 *(fe_vol_->gradNdetJ()),
120 Intrepid::COMP_CPP, false);
121 // ADD NONLINEAR TERM
122 ROL::Ptr<Intrepid::FieldContainer<Real> > valU_eval =
123 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p);
124 fe_vol_->evaluateValue(valU_eval, u_coeff);
125 for (int i = 0; i < c; ++i) {
126 for (int j = 0; j < p; ++j) {
127 (*valU_eval)(i,j) = -std::exp(-(*valU_eval)(i,j));
128 }
129 }
130 Intrepid::FieldContainer<Real> NexpU(c,f,p);
131 Intrepid::FunctionSpaceTools::scalarMultiplyDataField<Real>(NexpU,
132 *valU_eval,
133 *(fe_vol_->N()));
134 Intrepid::FunctionSpaceTools::integrate<Real>(*jac,
135 NexpU,
136 *(fe_vol_->NdetJ()),
137 Intrepid::COMP_CPP, true);
138 }
139
140 void Jacobian_2(ROL::Ptr<Intrepid::FieldContainer<Real> > & jac,
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 if ( z_coeff != ROL::nullPtr ) {
145 // GET DIMENSIONS
146 int c = fe_vol_->N()->dimension(0);
147 int f = fe_vol_->N()->dimension(1);
148 // INITIALIZE JACOBIAN
149 jac = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f, f);
150 // ADD CONTROL TERM
151 Intrepid::FunctionSpaceTools::integrate<Real>(*jac,
152 *(fe_vol_->N()),
153 *(fe_vol_->NdetJ()),
154 Intrepid::COMP_CPP, false);
155 Intrepid::RealSpaceTools<Real>::scale(*jac,static_cast<Real>(-1));
156 }
157 }
158
159 void Hessian_11(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess,
160 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & l_coeff,
161 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff,
162 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr,
163 const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) {
164 // GET DIMENSIONS
165 int c = fe_vol_->N()->dimension(0);
166 int f = fe_vol_->N()->dimension(1);
167 int p = fe_vol_->N()->dimension(2);
168 // INITIALIZE HESSIAN
169 hess = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f, f);
170 // COMPUTE NONLINEAR TERM
171 ROL::Ptr<Intrepid::FieldContainer<Real> > valU_eval =
172 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p);
173 fe_vol_->evaluateValue(valU_eval, u_coeff);
174 ROL::Ptr<Intrepid::FieldContainer<Real> > valL_eval =
175 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p);
176 fe_vol_->evaluateValue(valL_eval, l_coeff);
177 for (int i = 0; i < c; ++i) {
178 for (int j = 0; j < p; ++j) {
179 (*valU_eval)(i,j) = (*valL_eval)(i,j)*std::exp(-(*valU_eval)(i,j));
180 }
181 }
182 Intrepid::FieldContainer<Real> NLexpU(c,f,p);
183 Intrepid::FunctionSpaceTools::scalarMultiplyDataField<Real>(NLexpU,
184 *valU_eval,
185 *(fe_vol_->N()));
186 Intrepid::FunctionSpaceTools::integrate<Real>(*hess,
187 NLexpU,
188 *(fe_vol_->NdetJ()),
189 Intrepid::COMP_CPP, false);
190 }
191
192 void Hessian_12(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess,
193 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & l_coeff,
194 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff,
195 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr,
196 const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) {
197 throw Exception::Zero(">>> (PDE_Poisson_Boltzmann:Hessian_12: Hessian is zero.");
198 }
199
200 void Hessian_21(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess,
201 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & l_coeff,
202 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & u_coeff,
203 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & z_coeff = ROL::nullPtr,
204 const ROL::Ptr<const std::vector<Real> > & z_param = ROL::nullPtr) {
205 throw Exception::Zero(">>> (PDE_Poisson_Boltzmann:Hessian_21: Hessian is zero.");
206 }
207
208 void Hessian_22(ROL::Ptr<Intrepid::FieldContainer<Real> > & hess,
209 const ROL::Ptr<const Intrepid::FieldContainer<Real> > & l_coeff,
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(">>> (PDE_Poisson_Boltzmann:Hessian_22: Hessian is zero.");
214 }
215
216 void RieszMap_1(ROL::Ptr<Intrepid::FieldContainer<Real> > & riesz) {
217 // GET DIMENSIONS
218 int c = fe_vol_->N()->dimension(0);
219 int f = fe_vol_->N()->dimension(1);
220 // INITIALIZE RIESZ
221 riesz = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f, f);
222 *riesz = *fe_vol_->stiffMat();
223 Intrepid::RealSpaceTools<Real>::add(*riesz,*(fe_vol_->massMat()));
224 }
225
226 void RieszMap_2(ROL::Ptr<Intrepid::FieldContainer<Real> > & riesz) {
227 // GET DIMENSIONS
228 int c = fe_vol_->N()->dimension(0);
229 int f = fe_vol_->N()->dimension(1);
230 // INITIALIZE RIESZ
231 riesz = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f, f);
232 *riesz = *fe_vol_->massMat();
233 }
234
235 std::vector<ROL::Ptr<Intrepid::Basis<Real, Intrepid::FieldContainer<Real> > > > getFields() {
236 return basisPtrs_;
237 }
238
239 void setCellNodes(const ROL::Ptr<Intrepid::FieldContainer<Real> > &volCellNodes,
240 const std::vector<std::vector<ROL::Ptr<Intrepid::FieldContainer<Real> > > > &bdryCellNodes,
241 const std::vector<std::vector<std::vector<int> > > &bdryCellLocIds) {
242 volCellNodes_ = volCellNodes;
243 bdryCellNodes_ = bdryCellNodes;
244 bdryCellLocIds_ = bdryCellLocIds;
245 // Finite element definition.
246 fe_vol_ = ROL::makePtr<FE<Real>>(volCellNodes_,basisPtr_,cellCub_);
247 }
248
249 const ROL::Ptr<FE<Real> > getFE(void) const {
250 return fe_vol_;
251 }
252
253 }; // PDE_Poisson_Boltzmann
254
255 #endif