Debian Codegraph @

packages/rol/example/PDE-OPT/dynamic/navier-stokes/l1penaltydynamic.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 #ifndef L1DYNOBJECTIVE_HPP
11 #define L1DYNOBJECTIVE_HPP
12
13 #include "ROL_Objective.hpp"
14 #include "../../TOOLS/ltiobjective.hpp"
15 #include "../../TOOLS/meshreader.hpp"
16 #include "../../TOOLS/pdevector.hpp"
17
18
19 template <class Real>
20 class L1_Dyn_Objective : public ROL::Objective<Real> {
21 using size_type = typename std::vector<Real>::size_type;
22 private:
23 Real theta_, beta_;//, T_;
24 const size_type Nt_;
25 const std::vector<ROL::TimeStamp<Real>> ts_;
26 const ROL::Ptr<ROL::PartitionedVector<Real>> zl_;
27 const ROL::Ptr<ROL::PartitionedVector<Real>> zu_;
28
29 ROL::PartitionedVector<Real> &partition ( ROL::Vector<Real>& x ) const {
30 return static_cast<ROL::PartitionedVector<Real>&>(x);
31 }
32
33 const ROL::PartitionedVector<Real> &partition ( const ROL::Vector<Real>& x ) const {
34 return static_cast<const ROL::PartitionedVector<Real>&>(x);
35 }
36
37 ROL::Ptr<const std::vector<Real> > getConstParameter(const ROL::Vector<Real> &x) const {
38 ROL::Ptr<const std::vector<Real> > xp;
39 try {
40 xp = dynamic_cast<const ROL::StdVector<Real>&>(x).getVector();
41 }
42 catch (std::exception &e) {
43 try {
44 ROL::Ptr<const ROL::StdVector<Real> > xvec
45 = dynamic_cast<const PDE_OptVector<Real>&>(x).getParameter();
46 if ( xvec == ROL::nullPtr ) {
47 xp = ROL::nullPtr;
48 }
49 else {
50 xp = xvec->getVector();
51 }
52 }
53 catch (std::exception &ee) {
54 xp = ROL::nullPtr;
55 }
56 }
57 return xp;
58 }
59
60 ROL::Ptr<std::vector<Real> > getParameter(ROL::Vector<Real> &x) const {
61 ROL::Ptr<std::vector<Real> > xp;
62 try {
63 xp = dynamic_cast<ROL::StdVector<Real>&>(x).getVector();
64 }
65 catch (std::exception &e) {
66 try {
67 ROL::Ptr<ROL::StdVector<Real> > xvec
68 = dynamic_cast<PDE_OptVector<Real>&>(x).getParameter();
69 if ( xvec == ROL::nullPtr ) {
70 xp = ROL::nullPtr;
71 }
72 else {
73 xp = xvec->getVector();
74 }
75 }
76 catch (std::exception &ee) {
77 xp = ROL::nullPtr;
78 }
79 }
80 return xp;
81 }
82
83
84 public:
85 L1_Dyn_Objective(ROL::ParameterList &parlist,
86 const std::vector<ROL::TimeStamp<Real>> &timeStamp,
87 const ROL::Ptr<ROL::PartitionedVector<Real>> &zl,
88 const ROL::Ptr<ROL::PartitionedVector<Real>> &zu
89 ) : Nt_(timeStamp.size()), ts_(timeStamp), zl_(zl), zu_(zu)
90 {
91 theta_ = parlist.sublist("Reduced Dynamic Objective").sublist("Time Discretization").get("Theta", 1.0);
92 beta_ = parlist.sublist("Problem").get("L1 Control Cost", 1.e-2);
93
94 }
95
96 // value
97 Real value(const ROL::Vector<Real> &z,
98 Real &tol
99 ){
100
101 const ROL::PartitionedVector<Real> &zp = partition(z);
102 const Real one(1);
103 Real dt(0), val(0);
104 bool isinf(false);
105
106 for (size_type k = 0; k < Nt_; ++k){//dynamic obj
107 ROL::Ptr<const std::vector<Real>> zlk = getConstParameter(*zl_->get(k));
108 ROL::Ptr<const std::vector<Real>> zuk = getConstParameter(*zu_->get(k));
109 ROL::Ptr<const std::vector<Real>> zpk = getConstParameter(*zp.get(k)); // ROL vector of partition/kth timestep
110
111 for (size_type i = 0; i < zpk->size(); ++i){
112 if ((*zpk)[i] < (*zlk)[i] || (*zpk)[i]>(*zuk)[i]){
113 val = ROL::ROL_INF<Real>();
114 isinf=true;
115 break;
116 }
117 }
118 }
119
120 if (!isinf) {
121 for (size_type k = 1; k < Nt_; ++k){//dynamic obj
122
123 ROL::Ptr<const std::vector<Real>> zpn = getConstParameter(*zp.get(k)); // ROL vector of partition/kth timestep
124 ROL::Ptr<const std::vector<Real>> zpo = getConstParameter(*zp.get(k-1)); // ROL vector of partition/kth timestep
125 dt = ts_[k].t[1] - ts_[k].t[0];
126
127 for (size_type i = 0; i < zpn->size(); ++i){
128 val += dt*((one - theta_)*std::abs((*zpo)[i]) + theta_*std::abs((*zpn)[i]));
129 } // end i for
130 }// end k for
131 }
132 return beta_*val;
133 } //end value
134
135 // prox
136 void prox(ROL::Vector<Real> &Pz,
137 const ROL::Vector<Real> &z,
138 Real t,
139 Real &tol )
140 {
141
142 //partitioned vectors
143 const ROL::PartitionedVector<Real> &zp = partition(z);
144 ROL::PartitionedVector<Real> &Pzp = partition(Pz);
145
146 //constants
147 const Real one(1), zero(0);
148 Real hk(0), hkplus(0);
149 Real l1param(0);
150
151 for (size_type k = 0; k<Nt_; ++k) {//dynamic part; 0->Nt inclusive
152 ROL::Ptr<const std::vector<Real>> zlk = getConstParameter(*zl_->get(k));
153 ROL::Ptr<const std::vector<Real>> zuk = getConstParameter(*zu_->get(k));
154 ROL::Ptr<const std::vector<Real>> zk = getConstParameter(zp[k]); //kth timestep
155 ROL::Ptr<std::vector<Real>> Pzk = getParameter(Pzp[k]);
156
157 // update prox parameter
158 if (k == 0){
159 hkplus = ts_[k+1].t[1] - ts_[k+1].t[0];
160 l1param = t*hkplus*beta_*(one - theta_);
161 } else if (k == (Nt_ - 1)) {
162 hk = ts_[k].t[1] - ts_[k].t[0];
163 l1param = t*hk*beta_*theta_;
164 } else {
165 hk = ts_[k].t[1] - ts_[k].t[0];
166 hkplus = ts_[k+1].t[1] - ts_[k+1].t[0];
167 l1param = t*beta_*(hkplus*(one - theta_) + theta_*hk);
168 }
169
170 for (size_type i = 0; i < zk->size(); ++i){
171 if ((*zk)[i] > l1param) { (*Pzk)[i] = (*zk)[i] - l1param; }
172 else if ((*zk)[i] < -l1param) {(*Pzk)[i] = (*zk)[i] + l1param;}
173 else { (*Pzk)[i] = zero;}
174
175 (*Pzk)[i] = std::min((*zuk)[i], std::max((*zlk)[i], (*Pzk)[i]));
176 } // end i loop
177
178 }//end k for
179
180 }//end prox
181 };
182
183 #endif