IFPACK  Development
Ifpack_DiagonalFilter.cpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack: Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2002) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
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 "Ifpack_ConfigDefs.h"
44 #include "Ifpack_DiagonalFilter.h"
45 #include "Epetra_ConfigDefs.h"
46 #include "Epetra_RowMatrix.h"
47 #include "Epetra_Comm.h"
48 #include "Epetra_Map.h"
49 #include "Epetra_MultiVector.h"
50 #include "Epetra_Vector.h"
51 
52 //==============================================================================
53 Ifpack_DiagonalFilter::Ifpack_DiagonalFilter(const Teuchos::RefCountPtr<Epetra_RowMatrix>& Matrix,
54  double AbsoluteThreshold,
55  double RelativeThreshold) :
56  A_(Matrix),
57  AbsoluteThreshold_(AbsoluteThreshold),
58  RelativeThreshold_(RelativeThreshold)
59 {
60  Epetra_Time Time(Comm());
61 
62  pos_.resize(NumMyRows());
63  val_.resize(NumMyRows());
64 
65  vector<int> Indices(MaxNumEntries());
66  vector<double> Values(MaxNumEntries());
67  int NumEntries;
68 
69  for (int MyRow = 0 ; MyRow < NumMyRows() ; ++MyRow) {
70 
71  pos_[MyRow] = -1;
72  val_[MyRow] = 0.0;
73  int ierr = A_->ExtractMyRowCopy(MyRow, MaxNumEntries(), NumEntries,
74  &Values[0], &Indices[0]);
75  assert (ierr == 0);
76 
77  for (int i = 0 ; i < NumEntries ; ++i) {
78  if (Indices[i] == MyRow) {
79  pos_[MyRow] = i;
80  val_[MyRow] = Values[i] * (RelativeThreshold_ - 1) +
81  AbsoluteThreshold_ * EPETRA_SGN(Values[i]);
82  }
83  break;
84  }
85  }
86  cout << "TIME = " << Time.ElapsedTime() << endl;
87 }
88 
89 //==============================================================================
90 int Ifpack_DiagonalFilter::
91 ExtractMyRowCopy(int MyRow, int Length, int& NumEntries,
92  double* Values, int* Indices) const
93 {
94 
95  IFPACK_CHK_ERR(A_->ExtractMyRowCopy(MyRow, Length, NumEntries,
96  Values,Indices));
97 
98  if (pos_[MyRow] != -1)
99  Values[pos_[MyRow]] += val_[MyRow];
100 
101  return(0);
102 }
103 
104 //==============================================================================
105 int Ifpack_DiagonalFilter::
106 Multiply(bool TransA, const Epetra_MultiVector& X,
107  Epetra_MultiVector& Y) const
108 {
109 
110  if (X.NumVectors() != Y.NumVectors())
111  IFPACK_CHK_ERR(-2);
112 
113  IFPACK_CHK_ERR(A_->Multiply(TransA, X, Y));
114 
115  for (int v = 0 ; v < X.NumVectors() ; ++v)
116  for (int i = 0 ; i < NumMyRows() ; ++i)
117  Y[v][i] += val_[i] * X[v][i];
118 
119 
120  return(0);
121 }
virtual int MaxNumEntries() const
Returns the maximum number of entries.
Ifpack_DiagonalFilter(const Teuchos::RefCountPtr< Epetra_RowMatrix > &Matrix, double AbsoluteThreshold, double RelativeThreshold)
Constructor.