Tpetra Matrix/Vector Services  Version of the Day
Tpetra_Power_Method.hpp
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Tpetra: Templated Linear Algebra Services Package
6 // Copyright (2008) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ************************************************************************
41 // @HEADER
42 */
43 
44 #ifndef TPETRA_POWER_METHOD_HPP
45 #define TPETRA_POWER_METHOD_HPP
46 
47 #include <Tpetra_Operator.hpp>
48 #include <Tpetra_Vector.hpp>
49 #include <Teuchos_ScalarTraits.hpp>
50 
51 namespace TpetraExamples {
52 
55  template <class Scalar, class Ordinal>
56  Scalar powerMethod(const Teuchos::RCP<const Tpetra::Operator<Scalar,Ordinal> > &A, int niters, typename Teuchos::ScalarTraits<Scalar>::magnitudeType tolerance, bool verbose)
57  {
58  typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType Magnitude;
59  typedef Tpetra::Vector<Scalar,Ordinal> Vector;
60 
61  if ( A->getRangeMap() != A->getDomainMap() ) {
62  throw std::runtime_error("TpetraExamples::powerMethod(): operator must have domain and range maps that are equivalent.");
63  }
64  // create three vectors, fill z with random numbers
65  Teuchos::RCP<Vector> z, q, r;
66  q = Tpetra::createVector<Scalar>(A->getRangeMap());
67  r = Tpetra::createVector<Scalar>(A->getRangeMap());
68  z = Tpetra::createVector<Scalar>(A->getRangeMap());
69  z->randomize();
70  //
71  Scalar lambda = 0.0;
72  Magnitude normz, residual = 0.0;
73  // power iteration
74  for (int iter = 0; iter < niters; ++iter) {
75  normz = z->norm2(); // Compute 2-norm of z
76  q->scale(1.0/normz, *z); // Set q = z / normz
77  A->apply(*q, *z); // Compute z = A*q
78  lambda = q->dot(*z); // Approximate maximum eigenvalue: lamba = dot(q,z)
79  if ( iter % 100 == 0 || iter + 1 == niters ) {
80  r->update(1.0, *z, -lambda, *q, 0.0); // Compute A*q - lambda*q
81  residual = Teuchos::ScalarTraits<Scalar>::magnitude(r->norm2() / lambda);
82  if (verbose) {
83  std::cout << "Iter = " << iter
84  << " Lambda = " << lambda
85  << " Residual of A*q - lambda*q = " << residual
86  << std::endl;
87  }
88  }
89  if (residual < tolerance) {
90  break;
91  }
92  }
93  return lambda;
94  }
95 
96 } // end of namespace TpetraExamples
97 
106 #endif
Namespace for Tpetra example classes and methods.
Abstract interface for linear operators accepting Tpetra MultiVector objects.
static magnitudeType magnitude(T a)
A distributed dense vector.
Scalar powerMethod(const Teuchos::RCP< const Tpetra::Operator< Scalar, Ordinal > > &A, int niters, typename Teuchos::ScalarTraits< Scalar >::magnitudeType tolerance, bool verbose)
Simple power iteration eigensolver for a Tpetra::Operator.