libmesh解析
本工作只是尝试解析原libmesh的代码,供学习使用
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 
trilinos_preconditioner.C
浏览该文件的文档.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2023 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 
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  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 
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 = 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 
123 template <typename T>
124 void
125 TrilinosPreconditioner<T>::set_preconditioner_type (const PreconditionerType & preconditioner_type)
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 
165  case BLOCK_JACOBI_PRECOND:
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 LIBMESH_EXPORT TrilinosPreconditioner<Number>;
261 
262 } // namespace libMesh
263 
264 #endif // LIBMESH_TRILINOS_HAVE_EPETRA
void set_params(Teuchos::ParameterList &list)
存储 ParameterList list 的副本。
virtual void init() override
初始化预条件器。
virtual bool HasNormInf() const override
virtual void apply(const NumericVector< T > &x, NumericVector< T > &y) override
应用预条件器。
提供了不同线性代数库的向量存储方案的统一接口。
Definition: dof_map.h:67
void set_preconditioner_type(const PreconditionerType &preconditioner_type)
根据 libMesh PreconditionerType 设置 Trilinos 预条件器。
这个类提供了一个接口,用于使用Trilinos提供的预条件器套件。 所有重写的虚拟函数在preconditioner.h中有文档说明。
这是一个通用的稀疏矩阵类。该类包含了必须在派生类中覆盖的纯虚拟成员。 使用一个公共的基类允许从不同的求解器包中以不同的格式统一访问稀疏矩阵。
Definition: dof_map.h:66
bool _is_initialized
Flag that tells if init() has been called.
Definition: libmesh.C:242
void compute()
计算预条件器。在 Trilinos 中,我们需要显式调用此函数。
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()
获取原始Epetra_FECrsMatrix指针。
此类提供了对Epetra数据结构的并行、稀疏矩阵的友好接口。所有重写的虚拟函数在sparse_matrix.h中都有文档。