trilinos_preconditioner.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 #include "libmesh/libmesh_common.h"
19 
20 #ifdef LIBMESH_TRILINOS_HAVE_EPETRA
21 
22 // Local Includes
26 #include "libmesh/libmesh_common.h"
28 
30 #ifdef LIBMESH_TRILINOS_HAVE_IFPACK
31 #include "Ifpack.h"
32 #include "Ifpack_DiagPreconditioner.h"
33 #include "Ifpack_AdditiveSchwarz.h"
34 #include "Ifpack_ILU.h"
35 #include "Ifpack_ILUT.h"
36 #include "Ifpack_IC.h"
37 #include "Ifpack_ICT.h"
38 #endif
39 
40 #ifdef LIBMESH_TRILINOS_HAVE_ML
41 #include "ml_MultiLevelPreconditioner.h"
42 #endif
44 
45 namespace libMesh
46 {
47 
48 template <typename T>
50  NumericVector<T> & /* y */ )
51 {
52 }
53 
54 
55 
56 
57 template <typename T>
59 {
60  if (!this->_matrix)
61  libmesh_error_msg("ERROR: No matrix set for PetscPreconditioner, but init() called");
62 
63  // Clear the preconditioner in case it has been created in the past
64  if (!this->_is_initialized)
65  {
66  EpetraMatrix<T> * matrix = cast_ptr<EpetraMatrix<T> *, SparseMatrix<T>>(this->_matrix);
67  _mat = matrix->mat();
68  }
69 
70  set_preconditioner_type(this->_preconditioner_type);
71 
72  this->_is_initialized = true;
73 }
74 
75 
76 template <typename T>
77 void
78 TrilinosPreconditioner<T>::set_params(Teuchos::ParameterList & list)
79 {
80  _param_list = list;
81 }
82 
83 
84 template <typename T>
85 void
87 {
88 #ifdef LIBMESH_TRILINOS_HAVE_IFPACK
89  Ifpack_Preconditioner * ifpack = nullptr;
90 #endif
91 
92 #ifdef LIBMESH_TRILINOS_HAVE_ML
93  ML_Epetra::MultiLevelPreconditioner * ml = nullptr;
94 #endif
95 
96  switch (this->_preconditioner_type)
97  {
98 #ifdef LIBMESH_TRILINOS_HAVE_IFPACK
99  // IFPACK preconditioners
100  case ILU_PRECOND:
101  case SOR_PRECOND:
102  ifpack = dynamic_cast<Ifpack_Preconditioner *>(_prec);
103  ifpack->Compute();
104  break;
105 #endif
106 
107 #ifdef LIBMESH_TRILINOS_HAVE_ML
108  // ML preconditioners
109  case AMG_PRECOND:
110  ml = dynamic_cast<ML_Epetra::MultiLevelPreconditioner *>(_prec);
111  ml->ComputePreconditioner();
112  break;
113 #endif
114 
115  default:
116  // If we made it here, there were no TrilinosPreconditioners
117  // active, so that's probably an error.
118  libmesh_error_msg("ERROR: No valid TrilinosPreconditioners available!");
119  break;
120  }
121 }
122 
123 
124 template <typename T>
125 void
127 {
128 #ifdef LIBMESH_TRILINOS_HAVE_IFPACK
129  Ifpack_Preconditioner * pc = nullptr;
130 #endif
131 
132 #ifdef LIBMESH_TRILINOS_HAVE_ML
133  ML_Epetra::MultiLevelPreconditioner * ml = nullptr;
134 #endif
135 
136  switch (preconditioner_type)
137  {
138  case IDENTITY_PRECOND:
139  // pc = new Ifpack_DiagPreconditioner();
140  break;
141 
142  case CHOLESKY_PRECOND:
143  break;
144 
145  case ICC_PRECOND:
146  break;
147 
148 #ifdef LIBMESH_TRILINOS_HAVE_IFPACK
149  case ILU_PRECOND:
150  pc = new Ifpack_ILU(_mat);
151  pc->SetParameters(_param_list);
152  pc->Initialize();
153  _prec = pc;
154  break;
155 #endif
156 
157  case LU_PRECOND:
158  break;
159 
160  case ASM_PRECOND:
161  break;
162 
163  case JACOBI_PRECOND:
164  break;
165 
167  break;
168 
169  case SOR_PRECOND:
170  break;
171 
172  case EISENSTAT_PRECOND:
173  break;
174 
175 #ifdef LIBMESH_TRILINOS_HAVE_ML
176  case AMG_PRECOND:
177  ml = new ML_Epetra::MultiLevelPreconditioner(*_mat, _param_list, false);;
178  _prec = ml;
179  break;
180 #endif
181 
182  default:
183  libmesh_error_msg("ERROR: Unsupported Trilinos Preconditioner: " << preconditioner_type << "\nContinuing with Trilinos defaults");
184  }
185 
186 }
187 
188 
189 template <typename T>
190 int
192 {
193  return _prec->SetUseTranspose(UseTranspose);
194 }
195 
196 template <typename T>
197 int
198 TrilinosPreconditioner<T>::Apply(const Epetra_MultiVector & X, Epetra_MultiVector & Y) const
199 {
200  return _prec->Apply(X, Y);
201 }
202 
203 template <typename T>
204 int
205 TrilinosPreconditioner<T>::ApplyInverse(const Epetra_MultiVector & r, Epetra_MultiVector & z) const
206 {
207  return _prec->ApplyInverse(r, z);
208 }
209 
210 template <typename T>
211 double
213 {
214  return _prec->NormInf();
215 }
216 
217 template <typename T>
218 const char *
220 {
221  return _prec->Label();
222 }
223 
224 template <typename T>
225 bool
227 {
228  return _prec->UseTranspose();
229 }
230 
231 template <typename T>
232 bool
234 {
235  return _prec->HasNormInf();
236 }
237 
238 template <typename T>
239 const Epetra_Comm &
241 {
242  return _prec->Comm();
243 }
244 
245 template <typename T>
246 const Epetra_Map &
248 {
249  return _prec->OperatorDomainMap();
250 }
251 
252 template <typename T>
253 const Epetra_Map &
255 {
256  return _prec->OperatorRangeMap();
257 }
258 
259 //------------------------------------------------------------------
260 // Explicit instantiations
261 template class TrilinosPreconditioner<Number>;
262 
263 } // namespace libMesh
264 
265 #endif // LIBMESH_TRILINOS_HAVE_EPETRA
void set_params(Teuchos::ParameterList &list)
virtual bool HasNormInf() const override
virtual void apply(const NumericVector< T > &x, NumericVector< T > &y) override
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
Definition: diff_context.h:40
void set_preconditioner_type(const PreconditionerType &preconditioner_type)
virtual const Epetra_Map & OperatorDomainMap() const override
virtual bool UseTranspose() const override
virtual double NormInf() const override
virtual int SetUseTranspose(bool UseTranspose) override
virtual const char * Label() const override
virtual const Epetra_Map & OperatorRangeMap() const override
virtual int Apply(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const override
virtual const Epetra_Comm & Comm() const override
virtual int ApplyInverse(const Epetra_MultiVector &r, Epetra_MultiVector &z) const override
Epetra_FECrsMatrix * mat()