Debian Codegraph @

packages/rol/example/PDE-OPT/0ld/poisson/constraint.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 constraint.hpp
11 \brief Defines the SimOpt constraint for the 'poisson' example.
12 */
13
14 #ifndef ROL_PDEOPT_POISSON_CONSTRAINT_H
15 #define ROL_PDEOPT_POISSON_CONSTRAINT_H
16
17 #include "ROL_Constraint_SimOpt.hpp"
18 #include "ROL_TpetraMultiVector.hpp"
19 #include "Amesos2.hpp"
20 #include "data.hpp"
21
22 template<class Real>
23 class EqualityConstraint_PDEOPT_Poisson : public ROL::Constraint_SimOpt<Real> {
24 private:
25
26 ROL::Ptr<PoissonData<Real> > data_;
27
28 public:
29
30 EqualityConstraint_PDEOPT_Poisson(const ROL::Ptr<PoissonData<Real> > &data,
31 const Teuchos::RCP<Teuchos::ParameterList> &parlist) {
32 data_ = data;
33 }
34
35 using ROL::Constraint_SimOpt<Real>::value;
36 void value(ROL::Vector<Real> &c, const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
37 ROL::Ptr<Tpetra::MultiVector<> > cp =
38 (dynamic_cast<ROL::TpetraMultiVector<Real>&>(c)).getVector();
39 ROL::Ptr<const Tpetra::MultiVector<> > up =
40 (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(u)).getVector();
41 ROL::Ptr<const Tpetra::MultiVector<> > zp =
42 (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(z)).getVector();
43
44 Real one(1);
45
46 // A*u
47 data_->getMatA()->apply(*up, *cp);
48
49 // B*z + A*u
50 data_->getMatB()->apply(*zp, *cp, Teuchos::NO_TRANS, one, one);
51
52 // A*u + B*z - f
53 cp->update(-one, *(data_->getVecF()), one);
54 }
55
56
57 void applyJacobian_1(ROL::Vector<Real> &jv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &u,
58 const ROL::Vector<Real> &z, Real &tol) {
59 ROL::Ptr<Tpetra::MultiVector<> > jvp =
60 (dynamic_cast<ROL::TpetraMultiVector<Real>&>(jv)).getVector();
61 ROL::Ptr<const Tpetra::MultiVector<> > vp =
62 (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(v)).getVector();
63
64 // A*v
65 data_->getMatA()->apply(*vp, *jvp);
66 }
67
68
69 void applyJacobian_2(ROL::Vector<Real> &jv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &u,
70 const ROL::Vector<Real> &z, Real &tol) {
71 ROL::Ptr<Tpetra::MultiVector<> > jvp =
72 (dynamic_cast<ROL::TpetraMultiVector<Real>&>(jv)).getVector();
73 ROL::Ptr<const Tpetra::MultiVector<> > vp =
74 (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(v)).getVector();
75
76 // B*v
77 data_->getMatB()->apply(*vp, *jvp);
78 }
79
80
81 void applyAdjointJacobian_1(ROL::Vector<Real> &ajv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &u,
82 const ROL::Vector<Real> &z, Real &tol) {
83 ROL::Ptr<Tpetra::MultiVector<> > ajvp =
84 (dynamic_cast<ROL::TpetraMultiVector<Real>&>(ajv)).getVector();
85 ROL::Ptr<const Tpetra::MultiVector<> > vp =
86 (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(v)).getVector();
87
88 // A'*v
89 bool transpose = true;
90 data_->getMatA(transpose)->apply(*vp, *ajvp);
91 }
92
93
94 void applyAdjointJacobian_2(ROL::Vector<Real> &ajv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &u,
95 const ROL::Vector<Real> &z, Real &tol) {
96 ROL::Ptr<Tpetra::MultiVector<> > ajvp =
97 (dynamic_cast<ROL::TpetraMultiVector<Real>&>(ajv)).getVector();
98 ROL::Ptr<const Tpetra::MultiVector<> > vp =
99 (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(v)).getVector();
100
101 // B'*v
102 bool transpose = true;
103 data_->getMatB(transpose)->apply(*vp, *ajvp);
104 }
105
106
107 void applyAdjointHessian_11(ROL::Vector<Real> &ahwv, const ROL::Vector<Real> &w, const ROL::Vector<Real> &v,
108 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
109 ahwv.zero();
110 }
111
112
113 void applyAdjointHessian_12(ROL::Vector<Real> &ahwv, const ROL::Vector<Real> &w, const ROL::Vector<Real> &v,
114 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
115 ahwv.zero();
116 }
117
118
119 void applyAdjointHessian_21(ROL::Vector<Real> &ahwv, const ROL::Vector<Real> &w, const ROL::Vector<Real> &v,
120 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
121 ahwv.zero();
122 }
123
124
125 void applyAdjointHessian_22(ROL::Vector<Real> &ahwv, const ROL::Vector<Real> &w, const ROL::Vector<Real> &v,
126 const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol) {
127 ahwv.zero();
128 }
129
130
131 void applyInverseJacobian_1(ROL::Vector<Real> &ijv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &u,
132 const ROL::Vector<Real> &z, Real &tol) {
133 ROL::Ptr<Tpetra::MultiVector<> > ijvp =
134 (dynamic_cast<ROL::TpetraMultiVector<Real>&>(ijv)).getVector();
135 ROL::Ptr<const Tpetra::MultiVector<> > vp =
136 (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(v)).getVector();
137
138 data_->getSolver()->setX(ijvp);
139 data_->getSolver()->setB(vp);
140 data_->getSolver()->solve();
141
142 /*
143 // Construct solver using Amesos2 factory.
144 ROL::Ptr<Amesos2::Solver< Tpetra::CrsMatrix<>, Tpetra::MultiVector<> > > solver;
145 try{
146 solver = Amesos2::create< Tpetra::CrsMatrix<>,Tpetra::MultiVector<> >("KLU2", data_->getMatA(), ijvp, vp);
147 } catch (std::invalid_argument e) {
148 std::cout << e.what() << std::endl;
149 }
150 solver->numericFactorization();
151 solver->solve();
152 */
153
154 }
155
156
157 void applyInverseAdjointJacobian_1(ROL::Vector<Real> &iajv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &u,
158 const ROL::Vector<Real> &z, Real &tol) {
159 ROL::Ptr<Tpetra::MultiVector<> > iajvp =
160 (dynamic_cast<ROL::TpetraMultiVector<Real>&>(iajv)).getVector();
161 ROL::Ptr<const Tpetra::MultiVector<> > vp =
162 (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(v)).getVector();
163
164 bool transpose = true;
165 data_->getSolver(transpose)->setX(iajvp);
166 data_->getSolver(transpose)->setB(vp);
167 data_->getSolver(transpose)->solve();
168
169 /*
170 bool transpose = true;
171 // Construct solver using Amesos2 factory.
172 ROL::Ptr<Amesos2::Solver< Tpetra::CrsMatrix<>, Tpetra::MultiVector<> > > solver;
173 try{
174 solver = Amesos2::create< Tpetra::CrsMatrix<>,Tpetra::MultiVector<> >("KLU2", data_->getMatA(transpose), iajvp, vp);
175 } catch (std::invalid_argument e) {
176 std::cout << e.what() << std::endl;
177 }
178 solver->numericFactorization();
179 solver->solve();
180 */
181
182 }
183
184 };
185
186 #endif