Debian Codegraph @

packages/rol/adapters/tpetra/src/vector/ROL_ScaledTpetraMultiVector.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_SCALEDTPETRAMULTIVECTOR_HPP
11 #define ROL_SCALEDTPETRAMULTIVECTOR_HPP
12
13 /** \class ROL::ScaledTpetraMultiVector
14 \brief Implements the ROL::Vector interface for a Tpetra_MultiVector
15 with scaled dot function.
16 \author Created by Drew Kouri
17 */
18
19 #include "ROL_TpetraMultiVector.hpp"
20
21 namespace ROL {
22
23 template <class Real,
24 class LO=Tpetra::Map<>::local_ordinal_type,
25 class GO=Tpetra::Map<>::global_ordinal_type,
26 class Node=Tpetra::Map<>::node_type >
27 class PrimalScaledTpetraMultiVector;
28
29 template <class Real,
30 class LO=Tpetra::Map<>::local_ordinal_type,
31 class GO=Tpetra::Map<>::global_ordinal_type,
32 class Node=Tpetra::Map<>::node_type >
33 class DualScaledTpetraMultiVector;
34
35 template <class Real, class LO, class GO, class Node>
36 class PrimalScaledTpetraMultiVector : public TpetraMultiVector<Real,LO,GO,Node> {
37 private:
38 const ROL::Ptr<const Tpetra::Vector<Real,LO,GO,Node> > scale_vec_;
39 mutable ROL::Ptr<DualScaledTpetraMultiVector<Real> > dual_vec_;
40 mutable bool isDualInitialized_;
41
42 void applyScaling(Tpetra::MultiVector<Real,LO,GO,Node> &out,
43 const Tpetra::MultiVector<Real,LO,GO,Node> &in) const {
44 Real zero(0), one(1);
45 out.elementWiseMultiply(one,*scale_vec_,in,zero);
46 }
47
48 public:
49 virtual ~PrimalScaledTpetraMultiVector() {}
50
51 PrimalScaledTpetraMultiVector(const ROL::Ptr<Tpetra::MultiVector<Real,LO,GO,Node> > &tpetra_vec,
52 const ROL::Ptr<const Tpetra::Vector<Real,LO,GO,Node> > &scale_vec)
53 : TpetraMultiVector<Real,LO,GO,Node>(tpetra_vec),
54 scale_vec_(scale_vec), isDualInitialized_(false) {}
55
56 Real dot( const Vector<Real> &x ) const {
57 ROL_TEST_FOR_EXCEPTION( (TpetraMultiVector<Real,LO,GO,Node>::dimension() != x.dimension()),
58 std::invalid_argument,
59 "Error: Vectors must have the same dimension." );
60 const Tpetra::MultiVector<Real,LO,GO,Node> &ex
61 = *(dynamic_cast<const TpetraMultiVector<Real,LO,GO,Node>&>(x).getVector());
62 const Tpetra::MultiVector<Real,LO,GO,Node> &ey
63 = *(TpetraMultiVector<Real,LO,GO,Node>::getVector());
64 size_t n = ey.getNumVectors();
65 // Scale x with scale_vec_
66 Tpetra::MultiVector<Real,LO,GO,Node> wex(TpetraMultiVector<Real>::getMap(), n);
67 applyScaling(wex,ex);
68 // Perform Euclidean dot between *this and scaled x for each vector
69 Teuchos::Array<Real> val(n,0);
70 ey.dot( wex, val.view(0,n) );
71 // Combine dots for each vector to get a scalar
72 Real xy(0);
73 for (size_t i = 0; i < n; ++i) {
74 xy += val[i];
75 }
76 return xy;
77 }
78
79 ROL::Ptr<Vector<Real> > clone() const {
80 const Tpetra::MultiVector<Real,LO,GO,Node> &ey
81 = *(TpetraMultiVector<Real,LO,GO,Node>::getVector());
82 size_t n = ey.getNumVectors();
83 return ROL::makePtr<PrimalScaledTpetraMultiVector<Real,LO,GO,Node>>(
84 ROL::makePtr<Tpetra::MultiVector<Real,LO,GO,Node>>(TpetraMultiVector<Real>::getMap(),n),
85 scale_vec_);
86 }
87
88 const Vector<Real> & dual() const {
89 if ( !isDualInitialized_ ) {
90 // Create new memory for dual vector
91 size_t n = TpetraMultiVector<Real,LO,GO,Node>::getVector()->getNumVectors();
92 dual_vec_ = ROL::makePtr<DualScaledTpetraMultiVector<Real,LO,GO,Node>>(
93 ROL::makePtr<Tpetra::MultiVector<Real,LO,GO,Node>>(TpetraMultiVector<Real>::getMap(),n),
94 scale_vec_);
95 isDualInitialized_ = true;
96 }
97 // Scale *this with scale_vec_ and place in dual vector
98 applyScaling(*(dual_vec_->getVector()),*(TpetraMultiVector<Real,LO,GO,Node>::getVector()));
99 return *dual_vec_;
100 }
101
102 Real apply(const Vector<Real> &x) const {
103 const DualScaledTpetraMultiVector<Real,LO,GO,Node> & ex = dynamic_cast<const DualScaledTpetraMultiVector<Real,LO,GO,Node>&>(x);
104 return TpetraMultiVector<Real,LO,GO,Node>::dot(ex);
105 }
106
107 }; // class PrimalScaledTpetraMultiVector
108
109 template <class Real, class LO, class GO, class Node>
110 class DualScaledTpetraMultiVector : public TpetraMultiVector<Real,LO,GO,Node> {
111 private:
112 const ROL::Ptr<const Tpetra::Vector<Real,LO,GO,Node> > scale_vec_;
113 mutable ROL::Ptr<PrimalScaledTpetraMultiVector<Real> > primal_vec_;
114 mutable bool isDualInitialized_;
115
116 void applyScaling(Tpetra::MultiVector<Real,LO,GO,Node> &out,
117 const Tpetra::MultiVector<Real,LO,GO,Node> &in) const {
118 Real zero(0), one(1);
119 Tpetra::Vector<Real,LO,GO,Node> W(scale_vec_->getMap());
120 W.reciprocal(*scale_vec_);
121 out.elementWiseMultiply(one,W,in,zero);
122 }
123
124 public:
125 virtual ~DualScaledTpetraMultiVector() {}
126
127 DualScaledTpetraMultiVector(const ROL::Ptr<Tpetra::MultiVector<Real,LO,GO,Node> > &tpetra_vec,
128 const ROL::Ptr<const Tpetra::Vector<Real,LO,GO,Node> > &scale_vec)
129 : TpetraMultiVector<Real,LO,GO,Node>(tpetra_vec),
130 scale_vec_(scale_vec), isDualInitialized_(false) {}
131
132 Real dot( const Vector<Real> &x ) const {
133 ROL_TEST_FOR_EXCEPTION( (TpetraMultiVector<Real,LO,GO,Node>::dimension() != x.dimension()),
134 std::invalid_argument,
135 "Error: Vectors must have the same dimension." );
136 const Tpetra::MultiVector<Real,LO,GO,Node> &ex
137 = *(dynamic_cast<const TpetraMultiVector<Real,LO,GO,Node>&>(x).getVector());
138 const Tpetra::MultiVector<Real,LO,GO,Node> &ey
139 = *(TpetraMultiVector<Real>::getVector());
140 size_t n = ey.getNumVectors();
141 // Scale x with 1/scale_vec_
142 Tpetra::MultiVector<Real,LO,GO,Node> wex(TpetraMultiVector<Real>::getMap(), n);
143 applyScaling(wex,ex);
144 // Perform Euclidean dot between *this and scaled x for each vector
145 Teuchos::Array<Real> val(n,0);
146 ey.dot( wex, val.view(0,n) );
147 // Combine dots for each vector to get a scalar
148 Real xy(0);
149 for (size_t i = 0; i < n; ++i) {
150 xy += val[i];
151 }
152 return xy;
153 }
154
155 ROL::Ptr<Vector<Real> > clone() const {
156 const Tpetra::MultiVector<Real,LO,GO,Node> &ey
157 = *(TpetraMultiVector<Real,LO,GO,Node>::getVector());
158 size_t n = ey.getNumVectors();
159 return ROL::makePtr<DualScaledTpetraMultiVector<Real,LO,GO,Node>>(
160 ROL::makePtr<Tpetra::MultiVector<Real,LO,GO,Node>>(TpetraMultiVector<Real>::getMap(),n),
161 scale_vec_);
162 }
163
164 const Vector<Real> & dual() const {
165 if ( !isDualInitialized_ ) {
166 // Create new memory for dual vector
167 size_t n = TpetraMultiVector<Real,LO,GO,Node>::getVector()->getNumVectors();
168 primal_vec_ = ROL::makePtr<PrimalScaledTpetraMultiVector<Real,LO,GO,Node>>(
169 ROL::makePtr<Tpetra::MultiVector<Real,LO,GO,Node>>(TpetraMultiVector<Real>::getMap(),n),
170 scale_vec_);
171 isDualInitialized_ = true;
172 }
173 // Scale *this with scale_vec_ and place in dual vector
174 applyScaling(*(primal_vec_->getVector()),*(TpetraMultiVector<Real,LO,GO,Node>::getVector()));
175 return *primal_vec_;
176 }
177
178 Real apply(const Vector<Real> &x) const {
179 const PrimalScaledTpetraMultiVector<Real,LO,GO,Node> & ex = dynamic_cast<const PrimalScaledTpetraMultiVector<Real,LO,GO,Node>&>(x);
180 return TpetraMultiVector<Real,LO,GO,Node>::dot(ex);
181 }
182
183 }; // class DualScaledTpetraMultiVector
184
185 } // namespace ROL
186
187 #endif