Belos  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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:
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 
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,
114  typedef MultiVecTraits<ScalarType,MV> MVT;
116 
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,
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,
195  std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>& normvec) const
196  {
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,
270 
271 
272 
275  virtual void project ( MV &X,
278  project(X,Teuchos::null,C,Q);
279  }
280 
302  virtual int normalize ( MV &X, Teuchos::RCP<MV> MX,
304 
305 
309  return normalize(X,Teuchos::null,B);
310  }
311 
312 
313  protected:
314  virtual int
316  Teuchos::RCP<MV> MX,
320 
321  virtual int
326  {
327  return this->projectAndNormalizeWithMxImpl (X, Teuchos::null, C, B, Q);
328  }
329 
330  public:
331 
366  int
368  Teuchos::RCP<MV> MX,
372  {
373  return this->projectAndNormalizeWithMxImpl (X, MX, C, B, Q);
374  }
375 
377 
379 
383  orthonormError(const MV &X) const {
384  return orthonormError(X,Teuchos::null);
385  }
386 
391  orthonormError(const MV &X, Teuchos::RCP<const MV> MX) const = 0;
392 
396  orthogError(const MV &X1, const MV &X2) const {
397  return orthogError(X1,Teuchos::null,X2);
398  }
399 
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
Collection of types and exceptions used within the Belos solvers.
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.
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 ...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Declaration of basic traits for the multivector type.
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. ...
Class which defines basic traits for the operator type.
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 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 ...
Traits class which defines basic operations on multivectors.
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...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< const OP > _Op
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
virtual ~MatOrthoManager()
Destructor.
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 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, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec) const
Provides the norm induced by innerProd().
Templated virtual class for providing orthogonalization/orthonormalization methods.
MatOrthoManager(Teuchos::RCP< const OP > Op=Teuchos::null)
Default constructor.
Teuchos::RCP< const OP > getOp() const
Get operator.
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...
Class which defines basic traits for the operator type.
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.
Belos header file which uses auto-configuration information to include necessary C++ headers...
virtual Teuchos::ScalarTraits< ScalarType >::magnitudeType orthonormError(const MV &X) const
This method computes the error in orthonormality of a multivector.
void setOp(Teuchos::RCP< const OP > Op)
Set operator.

Generated on Fri Aug 14 2020 10:48:33 for Belos by doxygen 1.8.5