Debian Codegraph @

packages/rol/example/PDE-OPT/dynamic/semilinear/obj_semilinear.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 obj.hpp
11 \brief Provides the interface for local (cell-based) objective function computations.
12 */
13
14 #ifndef PDEOPT_PARABOLIC_SEMILINEAR_HPP
15 #define PDEOPT_PARABOLIC_SEMILINEAR_HPP
16
17 #include "../../TOOLS/qoi.hpp"
18 #include "pde_semilinear.hpp"
19
20 template <class Real>
21 class QoI_State_Cost_Semilinear : 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 return static_cast<Real>(1);
29 // int size = x.size();
30 // Real val(0);
31 // for (int i = 0; i < size; ++i) {
32 // val += x[i]*x[i];
33 // }
34 // return val;
35 }
36
37 public:
38 QoI_State_Cost_Semilinear(const ROL::Ptr<FE<Real>> &fe) : fe_(fe) {
39 int c = fe_->cubPts()->dimension(0);
40 int p = fe_->cubPts()->dimension(1);
41 int d = fe_->cubPts()->dimension(2);
42 std::vector<Real> pt(d);
43 target_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(c,p);
44 for (int i = 0; i < c; ++i) {
45 for (int j = 0; j < p; ++j) {
46 for (int k = 0; k < d; ++k) {
47 pt[k] = (*fe_->cubPts())(i,j,k);
48 }
49 (*target_)(i,j) = targetFunc(pt);
50 }
51 }
52 }
53
54 Real value(ROL::Ptr<Intrepid::FieldContainer<Real>> & val,
55 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff,
56 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr,
57 const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) {
58 // Get relevant dimensions
59 int c = u_coeff->dimension(0);
60 int p = fe_->cubPts()->dimension(1);
61 // Initialize output val
62 val = ROL::makePtr<Intrepid::FieldContainer<Real>>(c);
63 // Evaluate state on FE basis
64 ROL::Ptr<Intrepid::FieldContainer<Real>> valU_eval =
65 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p);
66 fe_->evaluateValue(valU_eval, u_coeff);
67 // Compute difference between state and target
68 Intrepid::RealSpaceTools<Real>::subtract(*valU_eval,*target_);
69 // Compute squared L2-norm of diff
70 fe_->computeIntegral(val,valU_eval,valU_eval);
71 // Scale by one half
72 Intrepid::RealSpaceTools<Real>::scale(*val,static_cast<Real>(0.5));
73 return static_cast<Real>(0);
74 }
75
76 void gradient_1(ROL::Ptr<Intrepid::FieldContainer<Real>> & grad,
77 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff,
78 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr,
79 const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) {
80 // Get relevant dimensions
81 int c = u_coeff->dimension(0);
82 int p = fe_->cubPts()->dimension(1);
83 int f = fe_->N()->dimension(1);
84 // Initialize output grad
85 grad = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f);
86 // Evaluate state on FE basis
87 ROL::Ptr<Intrepid::FieldContainer<Real>> valU_eval =
88 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p);
89 fe_->evaluateValue(valU_eval, u_coeff);
90 // Compute difference between state and target
91 Intrepid::RealSpaceTools<Real>::subtract(*valU_eval,*target_);
92 // Compute gradient of squared L2-norm of diff
93 Intrepid::FunctionSpaceTools::integrate<Real>(*grad,
94 *valU_eval,
95 *(fe_->NdetJ()),
96 Intrepid::COMP_CPP, false);
97 }
98
99 void gradient_2(ROL::Ptr<Intrepid::FieldContainer<Real>> & grad,
100 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff,
101 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr,
102 const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) {
103 throw Exception::Zero(">>> QoI_State_Cost_Poisson_Boltzmann::gradient_2 is zero.");
104 }
105
106 void HessVec_11(ROL::Ptr<Intrepid::FieldContainer<Real>> & hess,
107 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & v_coeff,
108 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff,
109 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr,
110 const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) {
111 int c = v_coeff->dimension(0);
112 int p = fe_->cubPts()->dimension(1);
113 int f = fe_->N()->dimension(1);
114 ROL::Ptr<Intrepid::FieldContainer<Real>> valV_eval =
115 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p);
116 hess = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f);
117 fe_->evaluateValue(valV_eval, v_coeff);
118 Intrepid::FunctionSpaceTools::integrate<Real>(*hess,
119 *valV_eval,
120 *(fe_->NdetJ()),
121 Intrepid::COMP_CPP, false);
122 }
123
124 void HessVec_12(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_State_Cost_Poisson_Boltzmann::HessVec_12 is zero.");
130 }
131
132 void HessVec_21(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 throw Exception::Zero(">>> QoI_State_Cost_Poisson_Boltzmann::HessVec_21 is zero.");
138 }
139
140 void HessVec_22(ROL::Ptr<Intrepid::FieldContainer<Real>> & hess,
141 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & v_coeff,
142 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff,
143 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr,
144 const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) {
145 throw Exception::Zero(">>> QoI_State_Cost_Poisson_Boltzmann::HessVec_22 is zero.");
146 }
147
148 }; // QoI_State_Cost
149
150 template <class Real>
151 class QoI_Control_Cost_Semilinear : public QoI<Real> {
152 private:
153 ROL::Ptr<FE<Real>> fe_;
154
155 public:
156 QoI_Control_Cost_Semilinear(const ROL::Ptr<FE<Real>> &fe) : fe_(fe) {}
157
158 Real value(ROL::Ptr<Intrepid::FieldContainer<Real>> & val,
159 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff,
160 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr,
161 const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) {
162 // Get relevant dimensions
163 const int c = fe_->gradN()->dimension(0);
164 const int p = fe_->gradN()->dimension(2);
165 // Initialize output val
166 val = ROL::makePtr<Intrepid::FieldContainer<Real>>(c);
167 // Evaluate control on FE basis
168 ROL::Ptr<Intrepid::FieldContainer<Real>> valZ_eval =
169 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p);
170 fe_->evaluateValue(valZ_eval, z_coeff);
171 // Compute squared L2-norm
172 fe_->computeIntegral(val,valZ_eval,valZ_eval);
173 // Scale by one half
174 Intrepid::RealSpaceTools<Real>::scale(*val,static_cast<Real>(0.5));
175 return static_cast<Real>(0);
176 }
177
178 void gradient_1(ROL::Ptr<Intrepid::FieldContainer<Real>> & grad,
179 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff,
180 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr,
181 const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) {
182 throw Exception::Zero(">>> QoI_Control_Cost_Semilinear::gradient_1 is zero.");
183 }
184
185 void gradient_2(ROL::Ptr<Intrepid::FieldContainer<Real>> & grad,
186 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff,
187 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr,
188 const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) {
189 // Get relevant dimensions
190 const int c = fe_->gradN()->dimension(0);
191 const int f = fe_->gradN()->dimension(1);
192 const int p = fe_->gradN()->dimension(2);
193 // Initialize output grad
194 grad = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f);
195 // Evaluate state on FE basis
196 ROL::Ptr<Intrepid::FieldContainer<Real>> valZ_eval =
197 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p);
198 fe_->evaluateValue(valZ_eval, z_coeff);
199 // Compute gradient of squared L2-norm of gradient
200 Intrepid::FunctionSpaceTools::integrate<Real>(*grad,
201 *valZ_eval,
202 *(fe_->NdetJ()),
203 Intrepid::COMP_CPP, false);
204 }
205
206 void HessVec_11(ROL::Ptr<Intrepid::FieldContainer<Real>> & hess,
207 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & v_coeff,
208 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff,
209 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr,
210 const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) {
211 throw Exception::Zero(">>> QoI_Control_Cost_Semilinear::HessVec_11 is zero.");
212 }
213
214 void HessVec_12(ROL::Ptr<Intrepid::FieldContainer<Real>> & hess,
215 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & v_coeff,
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_Semilinear::HessVec_12 is zero.");
220 }
221
222 void HessVec_21(ROL::Ptr<Intrepid::FieldContainer<Real>> & hess,
223 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & v_coeff,
224 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff,
225 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr,
226 const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) {
227 throw Exception::Zero(">>> QoI_Control_Cost_Semilinear::HessVec_21 is zero.");
228 }
229
230 void HessVec_22(ROL::Ptr<Intrepid::FieldContainer<Real>> & hess,
231 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & v_coeff,
232 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & u_coeff,
233 const ROL::Ptr<const Intrepid::FieldContainer<Real>> & z_coeff = ROL::nullPtr,
234 const ROL::Ptr<const std::vector<Real>> & z_param = ROL::nullPtr) {
235 const int c = fe_->gradN()->dimension(0);
236 const int f = fe_->gradN()->dimension(1);
237 const int p = fe_->gradN()->dimension(2);
238 ROL::Ptr<Intrepid::FieldContainer<Real>> valV_eval =
239 ROL::makePtr<Intrepid::FieldContainer<Real>>(c, p);
240 hess = ROL::makePtr<Intrepid::FieldContainer<Real>>(c, f);
241 fe_->evaluateValue(valV_eval, v_coeff);
242 Intrepid::FunctionSpaceTools::integrate<Real>(*hess,
243 *valV_eval,
244 *(fe_->NdetJ()),
245 Intrepid::COMP_CPP, false);
246 }
247
248 }; // QoI_Control_Cost
249
250 #endif