Debian Codegraph @

packages/rol/adapters/teuchos/src/function/ROL_Teuchos_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 #ifndef ROL_TEUCHOS_EQUALITYCONSTRAINT_H
11 #define ROL_TEUCHOS_EQUALITYCONSTRAINT_H
12
13 #include "ROL_Constraint.hpp"
14 #include "ROL_TeuchosVector.hpp"
15
16 /** @ingroup func_group
17 \class ROL::TeuchosConstraint
18 \brief Defines the equality constraint operator interface for
19 Teuchos::SerialDenseVectors
20
21 */
22
23 namespace ROL {
24
25 template <class Ordinal, class Real>
26 class TeuchosConstraint : public Constraint<Real> {
27
28 template<class T> using RCP = Teuchos::RCP<T>;
29 using SerialDenseVector = Teuchos::SerialDenseVector<Ordinal,Real>;
30
31 RCP<const SerialDenseVector> getVector( const Vector<Real>& x ) {
32 return dynamic_cast<const TeuchosVector<Ordinal,Real>&>(x).getVector();
33 }
34
35 RCP<SerialDenseVector> getVector( Vector<Real>& x ) {
36 return dynamic_cast<TeuchosVector<Ordinal,Real>&>(x).getVector();
37 }
38
39 public:
40
41 virtual ~TeuchosConstraint() {}
42
43 virtual void update( const SerialDenseVector &x, bool flag = true, int iter = -1 ) {}
44
45 using Constraint<Real>::update;
46 void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
47 auto xp = getVector(x);
48 update(*xp,flag,true);
49 }
50
51
52 virtual void value( SerialDenseVector &c, const SerialDenseVector &x, Real &tol ) = 0;
53
54 using Constraint<Real>::value;
55 void value(Vector<Real> &c, const Vector<Real> &x, Real &tol) {
56 auto cp = getVector(c);
57 auto xp = getVector(x);
58 value(*cp,*xp,tol);
59 }
60
61
62 virtual void applyJacobian( SerialDenseVector &jv, const SerialDenseVector &v,
63 const SerialDenseVector &x, Real &tol ) {
64 ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
65 ">>> ERROR (ROL::TeuchosConstraint): applyJacobian not implemented!");
66 }
67
68
69 using Constraint<Real>::applyJacobian;
70 void applyJacobian(Vector<Real> &jv, const Vector<Real> &v,
71 const Vector<Real> &x, Real &tol) {
72
73 auto jvp = getVector(jv);
74 auto vp = getVector(v);
75 auto xp = getVector(x);
76 try {
77 applyJacobian(*jvp,*vp,*xp,tol);
78 }
79 catch (std::exception &e ){
80 Constraint<Real>::applyJacobian(jv,v,x,tol);
81 }
82 }
83
84 virtual void applyAdjointJacobian( SerialDenseVector &ajv, const SerialDenseVector &v,
85 const SerialDenseVector &x, Real &tol ) {
86 ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
87 ">>> ERROR (ROL::TeuchosConstraint): applyAdjointJacobian not implemented!");
88 }
89
90
91 using Constraint<Real>::applyAdjointJacobian;
92 void applyAdjointJacobian(Vector<Real> &ajv, const Vector<Real> &v,
93 const Vector<Real> &x, Real &tol) {
94
95 auto ajvp = getVector(ajv);
96 auto vp = getVector(v);
97 auto xp = getVector(x);
98
99 try {
100 applyAdjointJacobian(*ajvp,*vp,*xp,tol);
101 }
102 catch (std::exception &e ){
103 Constraint<Real>::applyAdjointJacobian(ajv,v,x,tol);
104 }
105 }
106
107 virtual void applyAdjointHessian( SerialDenseVector &ahuv, const SerialDenseVector &u,
108 const SerialDenseVector &v, const SerialDenseVector &x,
109 Real &tol ) {
110 ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
111 ">>> ERROR (ROL::TeuchosConstraint) : applyAdjointHessian not implemented!");
112 }
113
114
115 using Constraint<Real>::applyAdjointHessian;
116 void applyAdjointHessian(Vector<Real> &ahuv, const Vector<Real> &u, const Vector<Real> &v,
117 const Vector<Real> &x, Real &tol) {
118
119 auto ahuvp = getVector(ahuv);
120 auto up = getVector(u);
121 auto vp = getVector(v);
122 auto xp = getVector(x);
123
124 try {
125 applyAdjointHessian( *ahuvp, *up, *vp, *xp, tol );
126 }
127 catch (std::exception &e) {
128 Constraint<Real>::applyAdjointHessian(ahuv,u,v,x,tol);
129 }
130
131 }
132
133 virtual std::vector<Real> solveAugmentedSystem( SerialDenseVector &v1, SerialDenseVector &v2,
134 const SerialDenseVector &b1, const SerialDenseVector &b2,
135 const SerialDenseVector &x, Real &tol ) {
136 ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
137 ">>> ERROR (ROL::TeuchosConstraint) : solveAugmentedSystem not implemented!");
138 }
139
140
141 using Constraint<Real>::solveAugmentedSystem;
142 std::vector<Real> solveAugmentedSystem(Vector<Real> &v1, Vector<Real> &v2,
143 const Vector<Real> &b1, const Vector<Real> &b2,
144 const Vector<Real> &x, Real &tol) {
145 auto v1p = getVector(v1);
146 auto v2p = getVector(v2);
147 auto b1p = getVector(b1);
148 auto b2p = getVector(b2);
149 auto xp = getVector(x);
150
151 try {
152 return solveAugmentedSystem( *v1p, *v2p, *b1p, *b2p, *xp, tol );
153 }
154 catch (std::exception &e) {
155 return Constraint<Real>::solveAugmentedSystem(v1,v2,b1,b2,x,tol);
156 }
157 }
158
159 virtual void applyPreconditioner( SerialDenseVector &pv, const SerialDenseVector &v,
160 const SerialDenseVector &x, const SerialDenseVector &g, Real &tol ) {
161 ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
162 ">>> ERROR (ROL::TeuchosConstraint) : applyPreconditioner not implemented!");
163 }
164
165
166 using Constraint<Real>::applyPreconditioner;
167 void applyPreconditioner(Vector<Real> &Pv, const Vector<Real> &v, const Vector<Real> &x,
168 const Vector<Real> &g, Real &tol) {
169 auto Pvp = getVector(Pv);
170 auto vp = getVector(v);
171 auto xp = getVector(x);
172 auto gp = getVector(g);
173
174 try {
175 applyPreconditioner( *Pvp, *vp, *xp, *gp, tol );
176 }
177 catch (std::exception &e) {
178 Constraint<Real>::applyPreconditioner(Pv,v,x,g,tol);
179 }
180 }
181
182 }; // class TeuchosConstraint
183
184 } // namespace ROL
185
186
187 #endif // ROL_TEUCHOS_EQUALITYCONSTRAINT_H