Ifpack2 Templated Preconditioning Package  Version 1.0
Ifpack2_Condest.hpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack2: Tempated Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2009) 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 #ifndef IFPACK2_CONDEST_HPP
44 #define IFPACK2_CONDEST_HPP
45 
46 #include "Ifpack2_ConfigDefs.hpp"
47 #include "Ifpack2_CondestType.hpp"
48 #include "Ifpack2_Preconditioner.hpp"
49 #include <Teuchos_Ptr.hpp>
50 
51 namespace Ifpack2 {
52 
84 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
85 typename Teuchos::ScalarTraits<Scalar>::magnitudeType
87  const Ifpack2::CondestType CT,
88  const int MaxIters = 1550,
89  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType& Tol = Teuchos::as<Scalar> (1e-9),
90  const Teuchos::Ptr<const Tpetra::RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& matrix_in = Teuchos::null)
91 {
92  using Teuchos::Ptr;
93  typedef Teuchos::ScalarTraits<Scalar> STS;
94  typedef typename STS::magnitudeType MT;
95  typedef Teuchos::ScalarTraits<MT> STM;
96  typedef Tpetra::RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> row_matrix_type;
97  typedef Tpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> vec_type;
98 
99  MT condNumEst = -STS::magnitude( STS::one() );
100 
101  // Users may either provide a matrix for which to estimate the
102  // condition number, or use the Preconditioner's built-in matrix.
103  Ptr<const row_matrix_type> matrix = matrix_in;
104  if (matrix_in == Teuchos::null) {
105  matrix = TIFP.getMatrix ().ptr ();
106  TEUCHOS_TEST_FOR_EXCEPTION(
107  matrix == Teuchos::null,
108  std::logic_error,
109  "Ifpack2::Condest: Both the input matrix (matrix_in) and the Ifpack2 "
110  "preconditioner's matrix are null, so we have no matrix with which to "
111  "compute a condition number estimate. This probably indicates a bug "
112  "in Ifpack2, since no Ifpack2::Preconditioner subclass should accept a"
113  "null matrix.");
114  }
115 
116  if (CT == Ifpack2::Cheap) {
117  vec_type ones (TIFP.getDomainMap ()); // Vector of ones
118  ones.putScalar (STS::one ());
119  vec_type onesResult (TIFP.getRangeMap ()); // A*ones
120  onesResult.putScalar (STS::zero ());
121  TIFP.apply (ones, onesResult);
122  condNumEst = onesResult.normInf (); // max (abs (A*ones))
123  TEUCHOS_TEST_FOR_EXCEPTION(
124  STM::isnaninf (condNumEst),
125  std::runtime_error,
126  "Ifpack2::Condest: $\\|A*[1, ..., 1]^T\\|_{\\infty}$ = " << condNumEst << " is NaN or Inf.");
127  } else if (CT == Ifpack2::CG) {
128  TEUCHOS_TEST_FOR_EXCEPTION(
129  true, std::logic_error,
130  "Ifpack2::Condest: Condition number estimation using CG is not currently supported.");
131  } else if (CT == Ifpack2::GMRES) {
132  TEUCHOS_TEST_FOR_EXCEPTION(
133  true, std::logic_error,
134  "Ifpack2::Condest: Condition number estimation using GMRES is not currently supported.");
135  }
136  return condNumEst;
137 }
138 
139 }//namespace Ifpack2
140 
141 #endif // IFPACK2_CONDEST_HPP
142 
CondestType
Ifpack2::CondestType: enum to define the type of condition number estimate.
Definition: Ifpack2_CondestType.hpp:50
virtual void apply(const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), Scalar beta=Teuchos::ScalarTraits< Scalar >::zero()) const =0
Applies the effect of the preconditioner.
virtual const Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > & getRangeMap() const =0
Returns the Map associated with the range of this operator, which must be compatible with Y...
virtual const Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > & getDomainMap() const =0
Returns the Map associated with the domain of this operator, which must be compatible with X...
Base class for all Ifpack2 preconditioners.
Definition: Ifpack2_Preconditioner.hpp:130
virtual Teuchos::RCP< const Tpetra::RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getMatrix() const =0
Returns a pointer to the input matrix.
Uses AztecOO&#39;s CG.
Definition: Ifpack2_CondestType.hpp:52
Uses AztecOO&#39;s GMRES.
Definition: Ifpack2_CondestType.hpp:53
Ifpack2::Container: a pure virtual class for creating and solving local linear problems.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:67
cheap estimate
Definition: Ifpack2_CondestType.hpp:51