Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_Details_LinearSolverFactory_def.hpp
Go to the documentation of this file.
1 /*
2 //@HEADER
3 // ***********************************************************************
4 //
5 // Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
6 // Copyright (2009) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 //@HEADER
42 */
43 
47 
48 #ifndef IFPACK2_DETAILS_LINEARSOLVERFACTORY_DEF_HPP
49 #define IFPACK2_DETAILS_LINEARSOLVERFACTORY_DEF_HPP
50 
53 #include "Ifpack2_Details_LinearSolver.hpp"
54 #include "Ifpack2_Factory.hpp"
55 #include "Tpetra_RowMatrix.hpp"
56 #include <type_traits> // std::is_same
57 
58 namespace Ifpack2 {
59 namespace Details {
60 
61 template<class SC, class LO, class GO, class NT>
64 getLinearSolver (const std::string& solverName)
65 {
66  using Teuchos::null;
67  using Teuchos::RCP;
68  using Teuchos::rcp_dynamic_cast;
71  typedef Tpetra::RowMatrix<SC, LO, GO, NT> ROW;
72  const char prefix[] = "Ifpack2::Details::LinearSolverFactory::getLinearSolver: ";
73 
74  RCP<prec_type> solver;
75  try {
76  // The solver to create must be a subclass of
77  // Ifpack2::Details::CanChangeMatrix (see documentation of
78  // Ifpack2::Details::LinearSolver). As a result, it should be
79  // possible to create the solver with a null matrix.
80  solver = Ifpack2::Factory::template create<ROW> (solverName, null);
81  }
82  catch (std::exception& e) {
84  (true, std::invalid_argument, prefix << "Failed to create Ifpack2 "
85  "preconditioner named \"" << solverName << "\", for the following "
86  "template parameters: "
87  << "SC = " << TypeNameTraits<SC>::name ()
88  << ", LO = " << TypeNameTraits<LO>::name ()
89  << ", GO = " << TypeNameTraits<GO>::name ()
90  << ", NT = " << TypeNameTraits<NT>::name ()
91  << ". Ifpack2::Factory::create threw an exception: " << e.what ());
92  }
94  (solver.is_null (), std::invalid_argument, prefix << "Failed to create "
95  "Ifpack2 preconditioner named \"" << solverName << "\", for the "
96  "following template parameters: "
97  << "SC = " << TypeNameTraits<SC>::name ()
98  << ", LO = " << TypeNameTraits<LO>::name ()
99  << ", GO = " << TypeNameTraits<GO>::name ()
100  << ", NT = " << TypeNameTraits<NT>::name ()
101  << ". Ifpack2::Factory::create returned null.");
102 
104  return Teuchos::rcp (new impl_type (solver, solverName));
105 }
106 
107 template<class SC, class LO, class GO, class NT>
108 void
111 {
112  typedef Tpetra::MultiVector<SC, LO, GO, NT> MV;
113  typedef Tpetra::Operator<SC, LO, GO, NT> OP;
114  typedef typename MV::mag_type mag_type;
117 
118 #ifdef HAVE_TEUCHOSCORE_CXX11
119  typedef std::shared_ptr<factory_base_type> base_ptr_type;
120  typedef std::shared_ptr<factory_impl_type> impl_ptr_type;
121 #else
122  typedef Teuchos::RCP<factory_base_type> base_ptr_type;
123  typedef Teuchos::RCP<factory_impl_type> impl_ptr_type;
124 #endif // HAVE_TEUCHOSCORE_CXX11
125 
126  impl_ptr_type factory (new factory_impl_type ());
127  base_ptr_type factoryBase = factory; // implicit cast to base class
128 
130  (factoryBase.get () == NULL, std::logic_error, "Factory is null! This "
131  "should never happen! Please report this bug to the Ifpack2 developers.");
132 
133 // #ifdef HAVE_IFPACK2_DEBUG
134 // {
135 // using std::cerr;
136 // using std::endl;
137 // using Teuchos::TypeNameTraits;
138 // cerr << "Registering Ifpack2 LinearSolverFactory for"
139 // << " SC = " << TypeNameTraits<SC>::name ()
140 // << ", LO = " << TypeNameTraits<LO>::name ()
141 // << ", GO = " << TypeNameTraits<GO>::name ()
142 // << ", NT = " << TypeNameTraits<NT>::name ()
143 // << ", and mag_type = " << TypeNameTraits<mag_type>::name ()
144 // << endl;
145 // }
146 // #endif // HAVE_IFPACK2_DEBUG
147  Trilinos::Details::registerLinearSolverFactory<MV, OP, mag_type> ("Ifpack2", factoryBase);
148 }
149 
150 } // namespace Details
151 } // namespace Ifpack2
152 
153 // Do explicit instantiation of Ifpack2::Details::LinearSolverFactory,
154 // for Tpetra objects, with the given Tpetra template parameters.
155 #define IFPACK2_DETAILS_LINEARSOLVERFACTORY_INSTANT( SC, LO, GO, NT ) \
156  template class Ifpack2::Details::LinearSolverFactory<SC, LO, GO, NT>;
157 
158 #endif // IFPACK2_DETAILS_LINEARSOLVERFACTORY_DEF_HPP
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
static void registerLinearSolverFactory()
Register this LinearSolverFactory with the central registry.
Definition: Ifpack2_Details_LinearSolverFactory_def.hpp:110
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Interface for a &quot;factory&quot; that creates Ifpack2 solvers.
Definition: Ifpack2_Details_LinearSolverFactory_decl.hpp:65
Ifpack2&#39;s implementation of Trilinos::Details::LinearSolver interface.
Definition: Ifpack2_Details_LinearSolver_decl.hpp:105
virtual Teuchos::RCP< solver_type > getLinearSolver(const std::string &solverName)
Get an instance of a Ifpack2 solver.
Definition: Ifpack2_Details_LinearSolverFactory_def.hpp:64
Interface for all Ifpack2 preconditioners.
Definition: Ifpack2_Preconditioner.hpp:107
Declaration of interface for preconditioners that can change their matrix after construction.