Debian Codegraph @

packages/rol/example/PDE-OPT/0ld/poisson/objective.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 objective.hpp
11 \brief Defines the SimOpt objective function for the 'poisson' example.
12 */
13
14 #ifndef ROL_PDEOPT_POISSON_OBJECTIVE_H
15 #define ROL_PDEOPT_POISSON_OBJECTIVE_H
16
17 #include "ROL_Objective_SimOpt.hpp"
18 #include "ROL_TpetraMultiVector.hpp"
19 #include "data.hpp"
20
21 template<class Real>
22 class Objective_PDEOPT_Poisson : public ROL::Objective_SimOpt<Real> {
23 private:
24
25 ROL::Ptr<PoissonData<Real> > data_;
26 Real alpha_;
27
28 public:
29
30 Objective_PDEOPT_Poisson(const ROL::Ptr<PoissonData<Real> > &data,
31 const Teuchos::RCP<Teuchos::ParameterList> &parlist) {
32 data_ = data;
33 alpha_ = parlist->sublist("Problem").get("Control penalty parameter", 1e-2);
34 }
35
36 Real value(const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
37 ROL::Ptr<const Tpetra::MultiVector<> > up =
38 (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(u)).getVector();
39 ROL::Ptr<const Tpetra::MultiVector<> > zp =
40 (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(z)).getVector();
41
42 Teuchos::Array<Real> dotvalU(1, 0);
43 Teuchos::Array<Real> dotvalZ(1, 0);
44
45 // Set difference vector diffp to up.
46 ROL::Ptr<Tpetra::MultiVector<> > diffp =
47 ROL::makePtr<Tpetra::MultiVector<>>(*up, Teuchos::Copy);
48 // Temporary matvec vector.
49 ROL::Ptr<Tpetra::MultiVector<> > matvecp =
50 ROL::makePtr<Tpetra::MultiVector<>>(*up, Teuchos::Copy);
51
52 // (u-ud)
53 diffp->update(-1.0, *(data_->getVecUd()), 1.0);
54 // M*(u-ud)
55 data_->getMatM()->apply(*diffp, *matvecp);
56 // (u-ud)'*M*(u-ud)
57 diffp->dot(*matvecp, dotvalU);
58
59 // R*z
60 data_->getMatR()->apply(*zp, *matvecp);
61 // z'*R*z
62 zp->dot(*matvecp, dotvalZ);
63
64 // 1/2 * (u-ud)'*M*(u-ud) + alpha/2 * z'*R*z
65 return(0.5*dotvalU[0] + 0.5*alpha_*dotvalZ[0]);
66 }
67
68 void gradient_1(ROL::Vector<Real> &g, const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
69 ROL::Ptr<Tpetra::MultiVector<> > gp =
70 (dynamic_cast<ROL::TpetraMultiVector<Real>&>(g)).getVector();
71 ROL::Ptr<const Tpetra::MultiVector<> > up =
72 (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(u)).getVector();
73
74 // Set difference vector diffp to up.
75 ROL::Ptr<Tpetra::MultiVector<> > diffp =
76 ROL::makePtr<Tpetra::MultiVector<>>(*up, Teuchos::Copy);
77 // (u-ud)
78 diffp->update(-1.0, *(data_->getVecUd()), 1.0);
79 // M*(u-ud)
80 data_->getMatM()->apply(*diffp, *gp);
81 }
82
83 void gradient_2(ROL::Vector<Real> &g, const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
84 ROL::Ptr<Tpetra::MultiVector<> > gp =
85 (dynamic_cast<ROL::TpetraMultiVector<Real>&>(g)).getVector();
86 ROL::Ptr<const Tpetra::MultiVector<> > zp =
87 (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(z)).getVector();
88
89 // alpha * R*z
90 data_->getMatR()->apply(*zp, *gp);
91 gp->scale(alpha_);
92 }
93
94 void hessVec_11(ROL::Vector<Real> &hv, const ROL::Vector<Real> &v,
95 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
96 ROL::Ptr<Tpetra::MultiVector<> > hvp =
97 (dynamic_cast<ROL::TpetraMultiVector<Real>&>(hv)).getVector();
98 ROL::Ptr<const Tpetra::MultiVector<> > vp =
99 (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(v)).getVector();
100
101 // M*v
102 data_->getMatM()->apply(*vp, *hvp);
103 }
104
105 void hessVec_12(ROL::Vector<Real> &hv, const ROL::Vector<Real> &v,
106 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
107 ROL::Ptr<Tpetra::MultiVector<> > hvp =
108 (dynamic_cast<ROL::TpetraMultiVector<Real>&>(hv)).getVector();
109
110 // zero
111 hvp->scale(0);
112 }
113
114 void hessVec_21(ROL::Vector<Real> &hv, const ROL::Vector<Real> &v,
115 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
116 ROL::Ptr<Tpetra::MultiVector<> > hvp =
117 (dynamic_cast<ROL::TpetraMultiVector<Real>&>(hv)).getVector();
118
119 // zero
120 hvp->scale(0);
121 }
122
123 void hessVec_22(ROL::Vector<Real> &hv, const ROL::Vector<Real> &v,
124 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
125 ROL::Ptr<Tpetra::MultiVector<> > hvp =
126 (dynamic_cast<ROL::TpetraMultiVector<Real>&>(hv)).getVector();
127 ROL::Ptr<const Tpetra::MultiVector<> > vp =
128 (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(v)).getVector();
129
130 // alpha * R*v
131 data_->getMatR()->apply(*vp, *hvp);
132 hvp->scale(alpha_);
133 }
134
135 };
136
137 #endif