Epetra Package Browser (Single Doxygen Collection)
Development
Toggle main menu visibility
Loading...
Searching...
No Matches
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_Comm.h
Epetra_CrsMatrix.h
Epetra_Map.h
Epetra_Vector.h
Epetra_BlockMap::NumGlobalElements
int NumGlobalElements() const
Number of elements across all processors.
Definition
Epetra_BlockMap.h:546
Epetra_BlockMap::Comm
const Epetra_Comm & Comm() const
Access function for Epetra_Comm communicator.
Definition
Epetra_BlockMap.h:770
Epetra_Comm::MyPID
virtual int MyPID() const =0
Return my process ID.
Epetra_CrsMatrix
Epetra_CrsMatrix: A class for constructing and using real-valued double-precision sparse compressed r...
Definition
Epetra_CrsMatrix.h:173
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::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
Epetra_DistObject::Map
const Epetra_BlockMap & Map() const
Returns the address of the Epetra_BlockMap for this multi-vector.
Definition
Epetra_DistObject.h:192
Epetra_MultiVector::Scale
int Scale(double ScalarValue)
Scale the current values of a multi-vector, this = ScalarValue*this.
Definition
Epetra_MultiVector.cpp:1229
Epetra_MultiVector::Dot
int Dot(const Epetra_MultiVector &A, double *Result) const
Computes dot product of each corresponding pair of vectors.
Definition
Epetra_MultiVector.cpp:1123
Epetra_MultiVector::Update
int Update(double ScalarA, const Epetra_MultiVector &A, double ScalarThis)
Update multi-vector values with scaled values of A, this = ScalarThis*this + ScalarA*A.
Definition
Epetra_MultiVector.cpp:1276
Epetra_MultiVector::Random
int Random()
Set multi-vector values to random numbers.
Definition
Epetra_MultiVector.cpp:490
Epetra_MultiVector::Norm2
int Norm2(double *Result) const
Compute 2-norm of each vector in multi-vector.
Definition
Epetra_MultiVector.cpp:1547
Epetra_Vector
Epetra_Vector: A class for constructing and using dense vectors on a parallel computer.
Definition
Epetra_Vector.h:142
power_method
double power_method(const Epetra_CrsMatrix &A)
Definition
power_method.cpp:50
Generated by
1.17.0