Belos  Version of the Day
BelosMatOrthoManager.hpp
Go to the documentation of this file.
1 //@HEADER
2 // ************************************************************************
3 //
4 // Belos: Block Linear Solvers Package
5 // Copyright 2004 Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 //@HEADER
41 
47 #ifndef BELOS_MATORTHOMANAGER_HPP
48 #define BELOS_MATORTHOMANAGER_HPP
49 
65 #include "BelosConfigDefs.hpp"
66 #include "BelosTypes.hpp"
67 #include "BelosOrthoManager.hpp"
68 #include "BelosMultiVecTraits.hpp"
69 #include "BelosOperatorTraits.hpp"
70 
71 namespace Belos {
72 
73  template <class ScalarType, class MV, class OP>
74  class MatOrthoManager : public OrthoManager<ScalarType,MV> {
75  protected:
76  Teuchos::RCP<const OP> _Op;
77  bool _hasOp;
78 
79  public:
81 
82  MatOrthoManager(Teuchos::RCP<const OP> Op = Teuchos::null) : _Op(Op), _hasOp(Op!=Teuchos::null) {};
84 
86  virtual ~MatOrthoManager() {};
88 
90 
91 
93  void setOp( Teuchos::RCP<const OP> Op ) {
94  _Op = Op;
95  _hasOp = (_Op != Teuchos::null);
96  };
97 
99  Teuchos::RCP<const OP> getOp() const { return _Op; }
100 
102 
103 
105 
106 
111  void innerProd( const MV& X, const MV& Y,
112  Teuchos::SerialDenseMatrix<int,ScalarType>& Z ) const {
113  typedef Teuchos::ScalarTraits<ScalarType> SCT;
114  typedef MultiVecTraits<ScalarType,MV> MVT;
116 
117  Teuchos::RCP<const MV> P,Q;
118  Teuchos::RCP<MV> R;
119 
120  if (_hasOp) {
121  // attempt to minimize the amount of work in applying
122  if ( MVT::GetNumberVecs(X) < MVT::GetNumberVecs(Y) ) {
123  R = MVT::Clone(X,MVT::GetNumberVecs(X));
124  OPT::Apply(*_Op,X,*R);
125  P = R;
126  Q = Teuchos::rcp( &Y, false );
127  }
128  else {
129  P = Teuchos::rcp( &X, false );
130  R = MVT::Clone(Y,MVT::GetNumberVecs(Y));
131  OPT::Apply(*_Op,Y,*R);
132  Q = R;
133  }
134  }
135  else {
136  P = Teuchos::rcp( &X, false );
137  Q = Teuchos::rcp( &Y, false );
138  }
139 
140  MVT::MvTransMv(SCT::one(),*P,*Q,Z);
141  }
142 
149  void innerProd( const MV& X, const MV& Y, Teuchos::RCP<const MV> MY,
150  Teuchos::SerialDenseMatrix<int,ScalarType>& Z ) const {
151  typedef Teuchos::ScalarTraits<ScalarType> SCT;
152  typedef MultiVecTraits<ScalarType,MV> MVT;
153 
154  Teuchos::RCP<MV> P,Q;
155 
156  if ( MY == Teuchos::null ) {
157  innerProd(X,Y,Z);
158  }
159  else if ( _hasOp ) {
160  // the user has done the matrix vector for us
161  MVT::MvTransMv(SCT::one(),X,*MY,Z);
162  }
163  else {
164  // there is no matrix vector
165  MVT::MvTransMv(SCT::one(),X,Y,Z);
166  }
167  }
168 
171  void norm( const MV& X, std::vector< typename Teuchos::ScalarTraits<ScalarType>::magnitudeType >& normvec ) const {
172  norm(X,Teuchos::null,normvec);
173  }
174 
192  void
193  norm (const MV& X,
194  Teuchos::RCP<const MV> MX,
195  std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>& normvec) const
196  {
197  typedef Teuchos::ScalarTraits<ScalarType> SCT;
198  typedef Teuchos::ScalarTraits<typename SCT::magnitudeType> MT;
199  typedef MultiVecTraits<ScalarType,MV> MVT;
201 
202  int nvecs = MVT::GetNumberVecs(X);
203 
204  // Make sure that normvec has enough entries to hold the norms
205  // of all the columns of X. std::vector<T>::size_type is
206  // unsigned, so do the appropriate cast to avoid signed/unsigned
207  // comparisons that trigger compiler warnings.
208  if (normvec.size() < static_cast<size_t>(nvecs))
209  normvec.resize (nvecs);
210 
211  if (!_hasOp) {
212  // X == MX, since the operator M is the identity.
213  MX = Teuchos::rcp(&X, false);
214  MVT::MvNorm(X, normvec);
215  }
216  else {
217  // The caller didn't give us a previously computed MX, so
218  // apply the operator. We assign to MX only after applying
219  // the operator, so that if the application fails, MX won't be
220  // modified.
221  if(MX == Teuchos::null) {
222  Teuchos::RCP<MV> tempVec = MVT::Clone(X,nvecs);
223  OPT::Apply(*_Op,X,*tempVec);
224  MX = tempVec;
225  }
226  else {
227  // The caller gave us a previously computed MX. Make sure
228  // that it has at least as many columns as X.
229  const int numColsMX = MVT::GetNumberVecs(*MX);
230  TEUCHOS_TEST_FOR_EXCEPTION(numColsMX < nvecs, std::invalid_argument,
231  "MatOrthoManager::norm(X, MX, normvec): "
232  "MX has fewer columns than X: "
233  "MX has " << numColsMX << " columns, "
234  "and X has " << nvecs << " columns.");
235  }
236 
237  std::vector<ScalarType> dotvec(nvecs);
238  MVT::MvDot(X,*MX,dotvec);
239  for (int i=0; i<nvecs; i++) {
240  normvec[i] = MT::squareroot( SCT::magnitude(dotvec[i]) );
241  }
242  }
243  }
244 
245 
267  virtual void project ( MV &X, Teuchos::RCP<MV> MX,
268  Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C,
269  Teuchos::ArrayView<Teuchos::RCP<const MV> > Q) const = 0;
270 
271 
272 
275  virtual void project ( MV &X,
276  Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C,
277  Teuchos::ArrayView<Teuchos::RCP<const MV> > Q) const {
278  project(X,Teuchos::null,C,Q);
279  }
280 
302  virtual int normalize ( MV &X, Teuchos::RCP<MV> MX,
303  Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B ) const = 0;
304 
305 
308  virtual int normalize ( MV &X, Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B ) const {
309  return normalize(X,Teuchos::null,B);
310  }
311 
312 
313  protected:
314  virtual int
316  Teuchos::RCP<MV> MX,
317  Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C,
318  Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B,
319  Teuchos::ArrayView<Teuchos::RCP<const MV> > Q) const = 0;
320 
321  virtual int
323  Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C,
324  Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B,
325  Teuchos::ArrayView<Teuchos::RCP<const MV> > Q) const
326  {
327  return this->projectAndNormalizeWithMxImpl (X, Teuchos::null, C, B, Q);
328  }
329 
330  public:
331 
366  int
368  Teuchos::RCP<MV> MX,
369  Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C,
370  Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B,
371  Teuchos::ArrayView<Teuchos::RCP<const MV> > Q) const
372  {
373  return this->projectAndNormalizeWithMxImpl (X, MX, C, B, Q);
374  }
375 
377 
379 
382  virtual typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
383  orthonormError(const MV &X) const {
384  return orthonormError(X,Teuchos::null);
385  }
386 
390  virtual typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
391  orthonormError(const MV &X, Teuchos::RCP<const MV> MX) const = 0;
392 
395  virtual typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
396  orthogError(const MV &X1, const MV &X2) const {
397  return orthogError(X1,Teuchos::null,X2);
398  }
399 
404  virtual typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
405  orthogError(const MV &X1, Teuchos::RCP<const MV> MX1, const MV &X2) const = 0;
406 
408 
409  };
410 
411 } // end of Belos namespace
412 
413 
414 #endif
415 
416 // end of file BelosMatOrthoManager.hpp
Belos header file which uses auto-configuration information to include necessary C++ headers.
Declaration of basic traits for the multivector type.
Class which defines basic traits for the operator type.
Templated virtual class for providing orthogonalization/orthonormalization methods.
Collection of types and exceptions used within the Belos solvers.
Belos's templated virtual class for providing routines for orthogonalization and orthonormzalition of...
void innerProd(const MV &X, const MV &Y, Teuchos::RCP< const MV > MY, Teuchos::SerialDenseMatrix< int, ScalarType > &Z) const
Provides the inner product defining the orthogonality concepts, using the provided operator....
virtual int normalize(MV &X, Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B) const
This method calls normalize(X,Teuchos::null,B); see documentation for that function.
virtual ~MatOrthoManager()
Destructor.
virtual Teuchos::ScalarTraits< ScalarType >::magnitudeType orthonormError(const MV &X, Teuchos::RCP< const MV > MX) const =0
This method computes the error in orthonormality of a multivector. The method has the option of explo...
MatOrthoManager(Teuchos::RCP< const OP > Op=Teuchos::null)
Default constructor.
void innerProd(const MV &X, const MV &Y, Teuchos::SerialDenseMatrix< int, ScalarType > &Z) const
Provides the inner product defining the orthogonality concepts, using the provided operator.
int projectAndNormalize(MV &X, Teuchos::RCP< MV > MX, Teuchos::Array< Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > > C, Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
Given a set of bases Q[i] and a multivector X, this method computes an orthonormal basis for .
virtual Teuchos::ScalarTraits< ScalarType >::magnitudeType orthogError(const MV &X1, const MV &X2) const
This method computes the error in orthogonality of two multivectors. This method.
virtual int projectAndNormalizeWithMxImpl(MV &X, Teuchos::RCP< MV > MX, Teuchos::Array< Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > > C, Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const =0
virtual Teuchos::ScalarTraits< ScalarType >::magnitudeType orthogError(const MV &X1, Teuchos::RCP< const MV > MX1, const MV &X2) const =0
This method computes the error in orthogonality of two multivectors. The method has the option of exp...
virtual void project(MV &X, Teuchos::RCP< MV > MX, Teuchos::Array< Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > > C, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const =0
Given a list of (mutually and internally) orthonormal bases Q, this method takes a multivector X and ...
void norm(const MV &X, Teuchos::RCP< const MV > MX, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec) const
Compute norm of each column of X.
void norm(const MV &X, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec) const
Provides the norm induced by innerProd().
Teuchos::RCP< const OP > getOp() const
Get operator.
virtual int normalize(MV &X, Teuchos::RCP< MV > MX, Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B) const =0
This method takes a multivector X and attempts to compute an orthonormal basis for ,...
virtual int projectAndNormalizeImpl(MV &X, Teuchos::Array< Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > > C, Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
void setOp(Teuchos::RCP< const OP > Op)
Set operator.
virtual void project(MV &X, Teuchos::Array< Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > > C, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
This method calls project(X,Teuchos::null,C,Q); see documentation for that function.
Teuchos::RCP< const OP > _Op
virtual Teuchos::ScalarTraits< ScalarType >::magnitudeType orthonormError(const MV &X) const
This method computes the error in orthonormality of a multivector.
Traits class which defines basic operations on multivectors.
Class which defines basic traits for the operator type.
Belos's templated virtual class for providing routines for orthogonalization and orthonormzalition of...

Generated on Sat Jun 5 2021 10:50:20 for Belos by doxygen 1.9.1