IFPACK  Development
 All Classes Namespaces Files Functions Variables Enumerations Friends Pages
Ifpack_Condest.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_Condest.h"
45 #include "Ifpack_CondestType.h"
46 #include "Ifpack_Preconditioner.h"
47 #include "Epetra_Vector.h"
48 #include "Epetra_LinearProblem.h"
49 #include "Epetra_Map.h"
50 #include "Epetra_RowMatrix.h"
51 #ifdef HAVE_IFPACK_AZTECOO
52 #include "AztecOO.h"
53 #endif
54 
55 double Ifpack_Condest(const Ifpack_Preconditioner& IFP,
56  const Ifpack_CondestType CT,
57  const int MaxIters,
58  const double Tol,
59  Epetra_RowMatrix* Matrix)
60 {
61  double ConditionNumberEstimate = -1.0;
62 
63  if (CT == Ifpack_Cheap) {
64 
65  // Create a vector with all values equal to one
66  Epetra_Vector Ones(IFP.OperatorDomainMap());
67  Ones.PutScalar(1.0);
68  // Create the vector of results
69  Epetra_Vector OnesResult(IFP.OperatorRangeMap());
70  // Compute the effect of the solve on the vector of ones
71  IFPACK_CHK_ERR(IFP.ApplyInverse(Ones, OnesResult));
72  // Make all values non-negative
73  IFPACK_CHK_ERR(OnesResult.Abs(OnesResult));
74  // Get the maximum value across all processors
75  IFPACK_CHK_ERR(OnesResult.MaxValue(&ConditionNumberEstimate));
76 
77  }
78  else if (CT == Ifpack_CG) {
79 
80 #ifdef HAVE_IFPACK_AZTECOO
81  if (Matrix == 0)
82  Matrix = (Epetra_RowMatrix*)&(IFP.Matrix());
83 
85  LHS.PutScalar(0.0);
87  RHS.Random();
88  Epetra_LinearProblem Problem;
89  Problem.SetOperator(Matrix);
90  Problem.SetLHS(&LHS);
91  Problem.SetRHS(&RHS);
92 
93  AztecOO Solver(Problem);
94  Solver.SetAztecOption(AZ_output,AZ_none);
95  Solver.SetAztecOption(AZ_solver,AZ_cg_condnum);
96  Solver.Iterate(MaxIters,Tol);
97 
98  const double* status = Solver.GetAztecStatus();
99  ConditionNumberEstimate = status[AZ_condnum];
100 #endif
101 
102  } else if (CT == Ifpack_GMRES) {
103 
104 #ifdef HAVE_IFPACK_AZTECOO
105  if (Matrix == 0)
106  Matrix = (Epetra_RowMatrix*)&(IFP.Matrix());
107 
108  Epetra_Vector LHS(IFP.OperatorDomainMap());
109  LHS.PutScalar(0.0);
110  Epetra_Vector RHS(IFP.OperatorRangeMap());
111  RHS.Random();
112  Epetra_LinearProblem Problem;
113  Problem.SetOperator(Matrix);
114  Problem.SetLHS(&LHS);
115  Problem.SetRHS(&RHS);
116 
117  AztecOO Solver(Problem);
118  Solver.SetAztecOption(AZ_solver,AZ_gmres_condnum);
119  Solver.SetAztecOption(AZ_output,AZ_none);
120  // the following can be problematic for large problems,
121  // but any restart would destroy useful information about
122  // the condition number.
123  Solver.SetAztecOption(AZ_kspace,MaxIters);
124  Solver.Iterate(MaxIters,Tol);
125 
126  const double* status = Solver.GetAztecStatus();
127  ConditionNumberEstimate = status[AZ_condnum];
128 #endif
129  }
130 
131  return(ConditionNumberEstimate);
132 
133 }
void SetLHS(Epetra_MultiVector *X)
void SetOperator(Epetra_RowMatrix *A)
virtual const Epetra_RowMatrix & Matrix() const =0
Returns a pointer to the matrix to be preconditioned.
virtual const Epetra_Map & OperatorDomainMap() const =0
virtual const Epetra_Map & OperatorRangeMap() const =0
Ifpack_Preconditioner: basic class for preconditioning in Ifpack.
virtual int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const =0
Applies the preconditioner to vector X, returns the result in Y.
void SetRHS(Epetra_MultiVector *B)