libMesh
trilinos_preconditioner.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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
23 #include "libmesh/trilinos_preconditioner.h"
24 #include "libmesh/trilinos_epetra_matrix.h"
25 #include "libmesh/trilinos_epetra_vector.h"
26 #include "libmesh/libmesh_common.h"
27 
28 #include "libmesh/ignore_warnings.h"
29 #ifdef LIBMESH_TRILINOS_HAVE_IFPACK
30 #include "Ifpack.h"
31 #include "Ifpack_DiagPreconditioner.h"
32 #include "Ifpack_AdditiveSchwarz.h"
33 #include "Ifpack_ILU.h"
34 #include "Ifpack_ILUT.h"
35 #include "Ifpack_IC.h"
36 #include "Ifpack_ICT.h"
37 #endif
38 
39 #ifdef LIBMESH_TRILINOS_HAVE_ML
40 #include "ml_MultiLevelPreconditioner.h"
41 #endif
42 #include "libmesh/restore_warnings.h"
43 
44 namespace libMesh
45 {
46 
47 template <typename T>
49  NumericVector<T> & /* y */ )
50 {
51 }
52 
53 
54 
55 
56 template <typename T>
58 {
59  if (!this->_matrix)
60  libmesh_error_msg("ERROR: No matrix set for PetscPreconditioner, but init() called");
61 
62  // Clear the preconditioner in case it has been created in the past
63  if (!this->_is_initialized)
64  {
65  EpetraMatrix<T> * matrix = cast_ptr<EpetraMatrix<T> *, SparseMatrix<T>>(this->_matrix);
66  _mat = matrix->mat();
67  }
68 
69  set_preconditioner_type(this->_preconditioner_type);
70 
71  this->_is_initialized = true;
72 }
73 
74 
75 template <typename T>
76 void
77 TrilinosPreconditioner<T>::set_params(Teuchos::ParameterList & list)
78 {
79  _param_list = list;
80 }
81 
82 
83 template <typename T>
84 void
86 {
87 #ifdef LIBMESH_TRILINOS_HAVE_IFPACK
88  Ifpack_Preconditioner * ifpack = libmesh_nullptr;
89 #endif
90 
91 #ifdef LIBMESH_TRILINOS_HAVE_ML
92  ML_Epetra::MultiLevelPreconditioner * ml = libmesh_nullptr;
93 #endif
94 
95  switch (this->_preconditioner_type)
96  {
97 #ifdef LIBMESH_TRILINOS_HAVE_IFPACK
98  // IFPACK preconditioners
99  case ILU_PRECOND:
100  case SOR_PRECOND:
101  ifpack = dynamic_cast<Ifpack_Preconditioner *>(_prec);
102  ifpack->Compute();
103  break;
104 #endif
105 
106 #ifdef LIBMESH_TRILINOS_HAVE_ML
107  // ML preconditioners
108  case AMG_PRECOND:
109  ml = dynamic_cast<ML_Epetra::MultiLevelPreconditioner *>(_prec);
110  ml->ComputePreconditioner();
111  break;
112 #endif
113 
114  default:
115  // If we made it here, there were no TrilinosPreconditioners
116  // active, so that's probably an error.
117  libmesh_error_msg("ERROR: No valid TrilinosPreconditioners available!");
118  break;
119  }
120 }
121 
122 
123 template <typename T>
124 void
126 {
127 #ifdef LIBMESH_TRILINOS_HAVE_IFPACK
128  Ifpack_Preconditioner * pc = libmesh_nullptr;
129 #endif
130 
131 #ifdef LIBMESH_TRILINOS_HAVE_ML
132  ML_Epetra::MultiLevelPreconditioner * ml = libmesh_nullptr;
133 #endif
134 
135  switch (preconditioner_type)
136  {
137  case IDENTITY_PRECOND:
138  // pc = new Ifpack_DiagPreconditioner();
139  break;
140 
141  case CHOLESKY_PRECOND:
142  break;
143 
144  case ICC_PRECOND:
145  break;
146 
147 #ifdef LIBMESH_TRILINOS_HAVE_IFPACK
148  case ILU_PRECOND:
149  pc = new Ifpack_ILU(_mat);
150  pc->SetParameters(_param_list);
151  pc->Initialize();
152  _prec = pc;
153  break;
154 #endif
155 
156  case LU_PRECOND:
157  break;
158 
159  case ASM_PRECOND:
160  break;
161 
162  case JACOBI_PRECOND:
163  break;
164 
166  break;
167 
168  case SOR_PRECOND:
169  break;
170 
171  case EISENSTAT_PRECOND:
172  break;
173 
174 #ifdef LIBMESH_TRILINOS_HAVE_ML
175  case AMG_PRECOND:
176  ml = new ML_Epetra::MultiLevelPreconditioner(*_mat, _param_list, false);;
177  _prec = ml;
178  break;
179 #endif
180 
181  default:
182  libmesh_error_msg("ERROR: Unsupported Trilinos Preconditioner: " << preconditioner_type << "\nContinuing with Trilinos defaults");
183  }
184 
185 }
186 
187 
188 template <typename T>
189 int
191 {
192  return _prec->SetUseTranspose(UseTranspose);
193 }
194 
195 template <typename T>
196 int
197 TrilinosPreconditioner<T>::Apply(const Epetra_MultiVector & X, Epetra_MultiVector & Y) const
198 {
199  return _prec->Apply(X, Y);
200 }
201 
202 template <typename T>
203 int
204 TrilinosPreconditioner<T>::ApplyInverse(const Epetra_MultiVector & r, Epetra_MultiVector & z) const
205 {
206  return _prec->ApplyInverse(r, z);
207 }
208 
209 template <typename T>
210 double
212 {
213  return _prec->NormInf();
214 }
215 
216 template <typename T>
217 const char *
219 {
220  return _prec->Label();
221 }
222 
223 template <typename T>
224 bool
226 {
227  return _prec->UseTranspose();
228 }
229 
230 template <typename T>
231 bool
233 {
234  return _prec->HasNormInf();
235 }
236 
237 template <typename T>
238 const Epetra_Comm &
240 {
241  return _prec->Comm();
242 }
243 
244 template <typename T>
245 const Epetra_Map &
247 {
248  return _prec->OperatorDomainMap();
249 }
250 
251 template <typename T>
252 const Epetra_Map &
254 {
255  return _prec->OperatorRangeMap();
256 }
257 
258 //------------------------------------------------------------------
259 // Explicit instantiations
260 template class TrilinosPreconditioner<Number>;
261 
262 } // namespace libMesh
263 
264 #endif // LIBMESH_TRILINOS_HAVE_EPETRA
virtual const char * Label() const libmesh_override
void set_params(Teuchos::ParameterList &list)
Stores a copy of the ParameterList list internally.
virtual int SetUseTranspose(bool UseTranspose) libmesh_override
const class libmesh_nullptr_t libmesh_nullptr
Numeric vector.
Definition: dof_map.h:66
virtual const Epetra_Map & OperatorRangeMap() const libmesh_override
virtual int Apply(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const libmesh_override
The libMesh namespace provides an interface to certain functionality in the library.
PetscErrorCode Vec Mat Mat pc
virtual const Epetra_Comm & Comm() const libmesh_override
void set_preconditioner_type(const PreconditionerType &preconditioner_type)
Sets the Trilinos preconditioner to use based on the libMesh PreconditionerType.
This class provides an interface to the suite of preconditioners available from Trilinos.
virtual double NormInf() const libmesh_override
Generic sparse matrix.
Definition: dof_map.h:65
PreconditionerType
Defines an enum for preconditioner types.
bool _is_initialized
Flag that tells if init() has been called.
Definition: libmesh.C:255
void compute()
Compute the preconditioner.
virtual bool HasNormInf() const libmesh_override
virtual bool UseTranspose() const libmesh_override
virtual void init() libmesh_override
Initialize data structures if not done so already.
virtual void apply(const NumericVector< T > &x, NumericVector< T > &y) libmesh_override
Computes the preconditioned vector y based on input vector x.
virtual int ApplyInverse(const Epetra_MultiVector &r, Epetra_MultiVector &z) const libmesh_override
virtual const Epetra_Map & OperatorDomainMap() const libmesh_override