IFPACK  Development
 All Classes Files Functions Variables Enumerations Friends Pages
Ifpack_DropFilter.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_DropFilter.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_DropFilter::Ifpack_DropFilter(const Teuchos::RefCountPtr<Epetra_RowMatrix>& Matrix,
54  double DropTol) :
55  A_(Matrix),
56  DropTol_(DropTol),
57  MaxNumEntries_(0),
58  MaxNumEntriesA_(0),
59  NumNonzeros_(0)
60 {
61  // use this filter only on serial matrices
62  if (A_->Comm().NumProc() != 1) {
63  cerr << "Ifpack_DropFilter can be used with Comm().NumProc() == 1" << endl;
64  cerr << "only. This class is a tool for Ifpack_AdditiveSchwarz," << endl;
65  cerr << "and it is not meant to be used otherwise." << endl;
66  exit(EXIT_FAILURE);
67  }
68 
69  if ((A_->NumMyRows() != A_->NumGlobalRows()) ||
70  (A_->NumMyRows() != A_->NumMyCols()))
71  IFPACK_CHK_ERRV(-2);
72 
73  NumRows_ = A_->NumMyRows();
74  MaxNumEntriesA_ = A_->MaxNumEntries();
75 
76  NumEntries_.resize(NumRows_);
77  Indices_.resize(MaxNumEntriesA_);
78  Values_.resize(MaxNumEntriesA_);
79 
80  vector<int> Ind(MaxNumEntriesA_);
81  vector<double> Val(MaxNumEntriesA_);
82 
83  for (int i = 0 ; i < NumRows_ ; ++i) {
84  NumEntries_[i] = MaxNumEntriesA_;
85  int Nnz;
86  IFPACK_CHK_ERRV(ExtractMyRowCopy(i,MaxNumEntriesA_,Nnz,
87  &Val[0], &Ind[0]));
88 
89  NumEntries_[i] = Nnz;
90  NumNonzeros_ += Nnz;
91  if (Nnz > MaxNumEntries_)
92  MaxNumEntries_ = Nnz;
93  }
94 
95 }
96 
97 //==============================================================================
98 int Ifpack_DropFilter::
99 ExtractMyRowCopy(int MyRow, int Length, int & NumEntries,
100  double *Values, int * Indices) const
101 {
102  if (Length < NumEntries_[MyRow])
103  IFPACK_CHK_ERR(-1);
104 
105  int Nnz;
106 
107  IFPACK_CHK_ERR(A_->ExtractMyRowCopy(MyRow,MaxNumEntriesA_,Nnz,
108  &Values_[0],&Indices_[0]));
109 
110  // loop over all nonzero elements of row MyRow,
111  // and drop elements below specified threshold.
112  // Also, add value to zero diagonal
113  int count = 0;
114  for (int i = 0 ; i < Nnz ; ++i) {
115 
116  // if element is above specified tol, add to the
117  // user's defined arrays. Check that we are not
118  // exceeding allocated space. Do not drop any diagonal entry.
119  if ((Indices_[i] == MyRow) || (IFPACK_ABS(Values_[i]) >= DropTol_)) {
120  if (count == Length)
121  IFPACK_CHK_ERR(-1);
122  Values[count] = Values_[i];
123  Indices[count] = Indices_[i];
124  count++;
125  }
126  }
127 
128  NumEntries = count;
129  return(0);
130 }
131 
132 //==============================================================================
133 int Ifpack_DropFilter::
134 ExtractDiagonalCopy(Epetra_Vector & Diagonal) const
135 {
136  IFPACK_CHK_ERR(A_->ExtractDiagonalCopy(Diagonal));
137  return(0);
138 }
139 
140 //==============================================================================
141 int Ifpack_DropFilter::
142 Multiply(bool TransA, const Epetra_MultiVector& X,
143  Epetra_MultiVector& Y) const
144 {
145  // NOTE: I suppose that the matrix has been localized,
146  // hence all maps are trivial.
147  int NumVectors = X.NumVectors();
148  if (NumVectors != Y.NumVectors())
149  IFPACK_CHK_ERR(-1);
150 
151  Y.PutScalar(0.0);
152 
153  vector<int> Indices(MaxNumEntries_);
154  vector<double> Values(MaxNumEntries_);
155 
156  for (int i = 0 ; i < NumRows_ ; ++i) {
157 
158  int Nnz;
159  ExtractMyRowCopy(i,MaxNumEntries_,Nnz,
160  &Values[0], &Indices[0]);
161  if (!TransA) {
162  // no transpose first
163  for (int j = 0 ; j < NumVectors ; ++j) {
164  for (int k = 0 ; k < Nnz ; ++k) {
165  Y[j][i] += Values[k] * X[j][Indices[k]];
166  }
167  }
168  }
169  else {
170  // transpose here
171  for (int j = 0 ; j < NumVectors ; ++j) {
172  for (int k = 0 ; k < Nnz ; ++k) {
173  Y[j][Indices[k]] += Values[k] * X[j][i];
174  }
175  }
176  }
177  }
178 
179  return(0);
180 }
181 
182 //==============================================================================
183 int Ifpack_DropFilter::
184 Solve(bool Upper, bool Trans, bool UnitDiagonal,
185  const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
186 {
187  IFPACK_CHK_ERR(-99);
188 }
189 
190 //==============================================================================
191 int Ifpack_DropFilter::
192 Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
193 {
194  IFPACK_RETURN(Multiply(UseTranspose(),X,Y));
195 }
196 
197 //==============================================================================
198 int Ifpack_DropFilter::
199 ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
200 {
201  IFPACK_CHK_ERR(-99);
202 }
203 
204 //==============================================================================
205 int Ifpack_DropFilter::InvRowSums(Epetra_Vector& x) const
206 {
207  IFPACK_CHK_ERR(-1);
208 }
209 
210 //==============================================================================
211 int Ifpack_DropFilter::InvColSums(Epetra_Vector& x) const
212 {
213  IFPACK_CHK_ERR(-1);
214 }
Ifpack_DropFilter(const Teuchos::RefCountPtr< Epetra_RowMatrix > &Matrix, double DropTol)
Constructor.