Epetra Package Browser (Single Doxygen Collection)
Development
Main Page
Related Pages
Namespaces
Classes
Files
Examples
File List
File Members
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
example
UG_Ex1
power_method.cpp
Go to the documentation of this file.
1
//@HEADER
2
// ************************************************************************
3
//
4
// Epetra: Linear Algebra Services Package
5
// Copyright 2011 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
42
43
#include <stdio.h>
44
#include <stdlib.h>
45
#include "
Epetra_Comm.h
"
46
#include "
Epetra_Map.h
"
47
#include "
Epetra_Vector.h
"
48
#include "
Epetra_CrsMatrix.h
"
49
// Simple Power method algorithm
50
double
power_method
(
const
Epetra_CrsMatrix
&
A
) {
51
// variable needed for iteration
52
double
lambda = 0.0;
53
int
niters = A.
RowMap
().
NumGlobalElements
()*10;
54
double
tolerance = 1.0e-10;
55
// Create vectors
56
Epetra_Vector
q(A.
RowMap
());
57
Epetra_Vector
z(A.
RowMap
());
58
Epetra_Vector
resid(A.
RowMap
());
59
// Fill z with random Numbers
60
z.
Random
();
61
// variable needed for iteration
62
double
normz;
63
double
residual = 0;
64
int
iter = 0;
65
while
(iter==0 || (iter < niters && residual > tolerance)) {
66
z.Norm2(&normz);
// Compute 2-norm of z
67
q.Scale(1.0/normz, z);
68
A.
Multiply
(
false
, q, z);
// Compute z = A*q
69
q.Dot(z, &lambda);
// Approximate maximum eigenvalue
70
if
(iter%10==0 || iter+1==niters) {
71
// Compute A*q - lambda*q every 10 iterations
72
resid.Update(1.0, z, -lambda, q, 0.0);
73
resid.Norm2(&residual);
74
if
(q.Map().Comm().MyPID()==0)
75
std::cout <<
"Iter = "
<< iter <<
" Lambda = "
<< lambda
76
<<
" Two-norm of A*q - lambda*q = "
77
<< residual << std::endl;
78
}
79
iter++;
80
}
81
return
(lambda);
82
}
Epetra_BlockMap::NumGlobalElements
int NumGlobalElements() const
Number of elements across all processors.
Definition:
Epetra_BlockMap.h:546
Epetra_MultiVector::Random
int Random()
Set multi-vector values to random numbers.
Definition:
Epetra_MultiVector.cpp:490
Epetra_CrsMatrix::Multiply
int Multiply(bool TransA, const Epetra_Vector &x, Epetra_Vector &y) const
Returns the result of a Epetra_CrsMatrix multiplied by a Epetra_Vector x in y.
Definition:
Epetra_CrsMatrix.cpp:3028
power_method
int power_method(Epetra_CrsMatrix &A, double &lambda, int niters, double tolerance, bool verbose)
Definition:
example/petra_power_method/cxx_main.cpp:244
A
Epetra_Vector
Epetra_Vector: A class for constructing and using dense vectors on a parallel computer.
Definition:
Epetra_Vector.h:142
Epetra_Comm.h
Epetra_CrsMatrix::RowMap
const Epetra_Map & RowMap() const
Returns the Epetra_Map object associated with the rows of this matrix.
Definition:
Epetra_CrsMatrix.h:1166
Epetra_CrsMatrix
Epetra_CrsMatrix: A class for constructing and using real-valued double-precision sparse compressed r...
Definition:
Epetra_CrsMatrix.h:173
Epetra_Map.h
Epetra_Vector.h
Epetra_CrsMatrix.h
Generated by
1.8.5