libMesh
Loading...
Searching...
No Matches
trilinos_preconditioner.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 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#include "libmesh/enum_preconditioner_type.h"
28
29#include "libmesh/ignore_warnings.h"
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
43#include "libmesh/restore_warnings.h"
44
45namespace libMesh
46{
47
48template <typename T>
50 NumericVector<T> & /* y */ )
51{
52}
53
54
55
56
57template <typename T>
59{
60 libmesh_error_msg_if(!this->_matrix, "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
75template <typename T>
76void
77TrilinosPreconditioner<T>::set_params(Teuchos::ParameterList & list)
78{
79 _param_list = list;
80}
81
82
83template <typename T>
84void
86{
87#ifdef LIBMESH_TRILINOS_HAVE_IFPACK
88 Ifpack_Preconditioner * ifpack = nullptr;
89#endif
90
91#ifdef LIBMESH_TRILINOS_HAVE_ML
92 ML_Epetra::MultiLevelPreconditioner * ml = 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
123template <typename T>
124void
126{
127#ifdef LIBMESH_TRILINOS_HAVE_IFPACK
128 Ifpack_Preconditioner * pc = nullptr;
129#endif
130
131#ifdef LIBMESH_TRILINOS_HAVE_ML
132 ML_Epetra::MultiLevelPreconditioner * ml = 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
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
188template <typename T>
189int
191{
192 return _prec->SetUseTranspose(UseTranspose);
193}
194
195template <typename T>
196int
197TrilinosPreconditioner<T>::Apply(const Epetra_MultiVector & X, Epetra_MultiVector & Y) const
198{
199 return _prec->Apply(X, Y);
200}
201
202template <typename T>
203int
204TrilinosPreconditioner<T>::ApplyInverse(const Epetra_MultiVector & r, Epetra_MultiVector & z) const
205{
206 return _prec->ApplyInverse(r, z);
207}
208
209template <typename T>
210double
212{
213 return _prec->NormInf();
214}
215
216template <typename T>
217const char *
219{
220 return _prec->Label();
221}
222
223template <typename T>
224bool
226{
227 return _prec->UseTranspose();
228}
229
230template <typename T>
231bool
233{
234 return _prec->HasNormInf();
235}
236
237template <typename T>
238const Epetra_Comm &
240{
241 return _prec->Comm();
242}
243
244template <typename T>
245const Epetra_Map &
247{
248 return _prec->OperatorDomainMap();
249}
250
251template <typename T>
252const Epetra_Map &
254{
255 return _prec->OperatorRangeMap();
256}
257
258//------------------------------------------------------------------
259// Explicit instantiations
260template class LIBMESH_EXPORT TrilinosPreconditioner<Number>;
261
262} // namespace libMesh
263
264#endif // LIBMESH_TRILINOS_HAVE_EPETRA
This class provides a nice interface to the Epetra data structures for parallel, sparse matrices.
Epetra_FECrsMatrix * mat()
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
Generic sparse matrix.
This class provides an interface to the suite of preconditioners available from Trilinos.
void compute()
Compute the preconditioner.
virtual int ApplyInverse(const Epetra_MultiVector &r, Epetra_MultiVector &z) const override
virtual bool HasNormInf() const override
virtual double NormInf() const override
virtual const Epetra_Map & OperatorDomainMap() const override
virtual const Epetra_Comm & Comm() const override
void set_params(Teuchos::ParameterList &list)
Stores a copy of the ParameterList list internally.
virtual const char * Label() const override
virtual bool UseTranspose() const override
void set_preconditioner_type(const PreconditionerType &preconditioner_type)
Sets the Trilinos preconditioner to use based on the libMesh PreconditionerType.
virtual void init() override
Initialize data structures if not done so already.
virtual const Epetra_Map & OperatorRangeMap() const override
virtual void apply(const NumericVector< T > &x, NumericVector< T > &y) override
Computes the preconditioned vector y based on input vector x.
virtual int Apply(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const override
virtual int SetUseTranspose(bool UseTranspose) override
The libMesh namespace provides an interface to certain functionality in the library.
PreconditionerType
Defines an enum for preconditioner types.