MFEM  v3.3.2
Finite element discretization library
solvers.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_SOLVERS
13 #define MFEM_SOLVERS
14 
15 #include "../config/config.hpp"
16 #include "operator.hpp"
17 
18 #ifdef MFEM_USE_MPI
19 #include <mpi.h>
20 #endif
21 
22 #ifdef MFEM_USE_SUITESPARSE
23 #include "sparsemat.hpp"
24 #include <umfpack.h>
25 #include <klu.h>
26 #endif
27 
28 namespace mfem
29 {
30 
31 /// Abstract base class for iterative solver
32 class IterativeSolver : public Solver
33 {
34 #ifdef MFEM_USE_MPI
35 private:
36  int dot_prod_type; // 0 - local, 1 - global over 'comm'
37  MPI_Comm comm;
38 #endif
39 
40 protected:
41  const Operator *oper;
43 
45  double rel_tol, abs_tol;
46 
47  // stats
48  mutable int final_iter, converged;
49  mutable double final_norm;
50 
51  double Dot(const Vector &x, const Vector &y) const;
52  double Norm(const Vector &x) const { return sqrt(Dot(x, x)); }
53 
54 public:
56 
57 #ifdef MFEM_USE_MPI
58  IterativeSolver(MPI_Comm _comm);
59 #endif
60 
61  void SetRelTol(double rtol) { rel_tol = rtol; }
62  void SetAbsTol(double atol) { abs_tol = atol; }
63  void SetMaxIter(int max_it) { max_iter = max_it; }
64  void SetPrintLevel(int print_lvl);
65 
66  int GetNumIterations() const { return final_iter; }
67  int GetConverged() const { return converged; }
68  double GetFinalNorm() const { return final_norm; }
69 
70  /// This should be called before SetOperator
71  virtual void SetPreconditioner(Solver &pr);
72 
73  /// Also calls SetOperator for the preconditioner if there is one
74  virtual void SetOperator(const Operator &op);
75 };
76 
77 
78 /// Stationary linear iteration: x <- x + B (b - A x)
79 class SLISolver : public IterativeSolver
80 {
81 protected:
82  mutable Vector r, z;
83 
84  void UpdateVectors();
85 
86 public:
87  SLISolver() { }
88 
89 #ifdef MFEM_USE_MPI
90  SLISolver(MPI_Comm _comm) : IterativeSolver(_comm) { }
91 #endif
92 
93  virtual void SetOperator(const Operator &op)
95 
96  virtual void Mult(const Vector &b, Vector &x) const;
97 };
98 
99 /// Stationary linear iteration. (tolerances are squared)
100 void SLI(const Operator &A, const Vector &b, Vector &x,
101  int print_iter = 0, int max_num_iter = 1000,
102  double RTOLERANCE = 1e-12, double ATOLERANCE = 1e-24);
103 
104 /// Preconditioned stationary linear iteration. (tolerances are squared)
105 void SLI(const Operator &A, Solver &B, const Vector &b, Vector &x,
106  int print_iter = 0, int max_num_iter = 1000,
107  double RTOLERANCE = 1e-12, double ATOLERANCE = 1e-24);
108 
109 
110 /// Conjugate gradient method
111 class CGSolver : public IterativeSolver
112 {
113 protected:
114  mutable Vector r, d, z;
115 
116  void UpdateVectors();
117 
118 public:
119  CGSolver() { }
120 
121 #ifdef MFEM_USE_MPI
122  CGSolver(MPI_Comm _comm) : IterativeSolver(_comm) { }
123 #endif
124 
125  virtual void SetOperator(const Operator &op)
127 
128  virtual void Mult(const Vector &b, Vector &x) const;
129 };
130 
131 /// Conjugate gradient method. (tolerances are squared)
132 void CG(const Operator &A, const Vector &b, Vector &x,
133  int print_iter = 0, int max_num_iter = 1000,
134  double RTOLERANCE = 1e-12, double ATOLERANCE = 1e-24);
135 
136 /// Preconditioned conjugate gradient method. (tolerances are squared)
137 void PCG(const Operator &A, Solver &B, const Vector &b, Vector &x,
138  int print_iter = 0, int max_num_iter = 1000,
139  double RTOLERANCE = 1e-12, double ATOLERANCE = 1e-24);
140 
141 
142 /// GMRES method
144 {
145 protected:
146  int m;
147 
148 public:
149  GMRESSolver() { m = 50; }
150 
151 #ifdef MFEM_USE_MPI
152  GMRESSolver(MPI_Comm _comm) : IterativeSolver(_comm) { m = 50; }
153 #endif
154 
155  void SetKDim(int dim) { m = dim; }
156 
157  virtual void Mult(const Vector &b, Vector &x) const;
158 };
159 
160 /// FGMRES method
162 {
163 protected:
164  int m;
165 
166 public:
167  FGMRESSolver() { m = 50; }
168 
169 #ifdef MFEM_USE_MPI
170  FGMRESSolver(MPI_Comm _comm) : IterativeSolver(_comm) { m = 50; }
171 #endif
172 
173  void SetKDim(int dim) { m = dim; }
174 
175  virtual void Mult(const Vector &b, Vector &x) const;
176 };
177 
178 /// GMRES method. (tolerances are squared)
179 int GMRES(const Operator &A, Vector &x, const Vector &b, Solver &M,
180  int &max_iter, int m, double &tol, double atol, int printit);
181 
182 /// GMRES method. (tolerances are squared)
183 void GMRES(const Operator &A, Solver &B, const Vector &b, Vector &x,
184  int print_iter = 0, int max_num_iter = 1000, int m = 50,
185  double rtol = 1e-12, double atol = 1e-24);
186 
187 
188 /// BiCGSTAB method
190 {
191 protected:
192  mutable Vector p, phat, s, shat, t, v, r, rtilde;
193 
194  void UpdateVectors();
195 
196 public:
198 
199 #ifdef MFEM_USE_MPI
200  BiCGSTABSolver(MPI_Comm _comm) : IterativeSolver(_comm) { }
201 #endif
202 
203  virtual void SetOperator(const Operator &op)
205 
206  virtual void Mult(const Vector &b, Vector &x) const;
207 };
208 
209 /// BiCGSTAB method. (tolerances are squared)
210 int BiCGSTAB(const Operator &A, Vector &x, const Vector &b, Solver &M,
211  int &max_iter, double &tol, double atol, int printit);
212 
213 /// BiCGSTAB method. (tolerances are squared)
214 void BiCGSTAB(const Operator &A, Solver &B, const Vector &b, Vector &x,
215  int print_iter = 0, int max_num_iter = 1000,
216  double rtol = 1e-12, double atol = 1e-24);
217 
218 
219 /// MINRES method
221 {
222 protected:
223  mutable Vector v0, v1, w0, w1, q;
224  mutable Vector u1; // used in the preconditioned version
225 
226 public:
228 
229 #ifdef MFEM_USE_MPI
230  MINRESSolver(MPI_Comm _comm) : IterativeSolver(_comm) { }
231 #endif
232 
233  virtual void SetPreconditioner(Solver &pr)
234  {
236  if (oper) { u1.SetSize(width); }
237  }
238 
239  virtual void SetOperator(const Operator &op);
240 
241  virtual void Mult(const Vector &b, Vector &x) const;
242 };
243 
244 /// MINRES method without preconditioner. (tolerances are squared)
245 void MINRES(const Operator &A, const Vector &b, Vector &x, int print_it = 0,
246  int max_it = 1000, double rtol = 1e-12, double atol = 1e-24);
247 
248 /// MINRES method with preconditioner. (tolerances are squared)
249 void MINRES(const Operator &A, Solver &B, const Vector &b, Vector &x,
250  int print_it = 0, int max_it = 1000,
251  double rtol = 1e-12, double atol = 1e-24);
252 
253 
254 /// Newton's method for solving F(x)=b for a given operator F.
255 /** The method GetGradient() must be implemented for the operator F.
256  The preconditioner is used (in non-iterative mode) to evaluate
257  the action of the inverse gradient of the operator. */
259 {
260 protected:
261  mutable Vector r, c;
262 
263 public:
265 
266 #ifdef MFEM_USE_MPI
267  NewtonSolver(MPI_Comm _comm) : IterativeSolver(_comm) { }
268 #endif
269  virtual void SetOperator(const Operator &op);
270 
271  /// Set the linear solver for inverting the Jacobian.
272  /** This method is equivalent to calling SetPreconditioner(). */
273  virtual void SetSolver(Solver &solver) { prec = &solver; }
274 
275  /// Solve the nonlinear system with right-hand side @a b.
276  /** If `b.Size() != Height()`, then @a b is assumed to be zero. */
277  virtual void Mult(const Vector &b, Vector &x) const;
278 
279  /** @brief This method can be overloaded in derived classes to implement line
280  search algorithms. */
281  /** The base class implementation (NewtonSolver) simply returns 1. A return
282  value of 0 indicates a failure, interrupting the Newton iteration. */
283  virtual double ComputeScalingFactor(const Vector &x, const Vector &b) const
284  { return 1.0; }
285 };
286 
287 /** Adaptive restarted GMRES.
288  m_max and m_min(=1) are the maximal and minimal restart parameters.
289  m_step(=1) is the step to use for going from m_max and m_min.
290  cf(=0.4) is a desired convergence factor. */
291 int aGMRES(const Operator &A, Vector &x, const Vector &b,
292  const Operator &M, int &max_iter,
293  int m_max, int m_min, int m_step, double cf,
294  double &tol, double &atol, int printit);
295 
296 
297 /** SLBQP: (S)ingle (L)inearly Constrained with (B)ounds (Q)uadratic (P)rogram
298 
299  minimize 1/2 ||x - x_t||^2, subject to:
300  lo_i <= x_i <= hi_i
301  sum_i w_i x_i = a
302 */
304 {
305 protected:
307  double a;
308 
309  /// Solve QP at fixed lambda
310  inline double solve(double l, const Vector &xt, Vector &x, int &nclip) const
311  {
312  add(xt, l, w, x);
313  x.median(lo,hi);
314  nclip++;
315  return Dot(w,x)-a;
316  }
317 
318  inline void print_iteration(int it, double r, double l) const;
319 
320 public:
322 
323 #ifdef MFEM_USE_MPI
324  SLBQPOptimizer(MPI_Comm _comm) : IterativeSolver(_comm) {}
325 #endif
326 
327  void SetBounds(const Vector &_lo, const Vector &_hi);
328  void SetLinearConstraint(const Vector &_w, double _a);
329 
330  // For this problem type, we let the target values play the role of the
331  // initial vector xt, from which the operator generates the optimal vector x.
332  virtual void Mult(const Vector &xt, Vector &x) const;
333 
334  /// These are not currently meaningful for this solver and will error out.
335  virtual void SetPreconditioner(Solver &pr);
336  virtual void SetOperator(const Operator &op);
337 };
338 
339 
340 #ifdef MFEM_USE_SUITESPARSE
341 
342 /// Direct sparse solver using UMFPACK
343 class UMFPackSolver : public Solver
344 {
345 protected:
348  void *Numeric;
349  SuiteSparse_long *AI, *AJ;
350 
351  void Init();
352 
353 public:
354  double Control[UMFPACK_CONTROL];
355  mutable double Info[UMFPACK_INFO];
356 
357  /** @brief For larger matrices, if the solver fails, set the parameter @a
358  _use_long_ints = true. */
359  UMFPackSolver(bool _use_long_ints = false)
360  : use_long_ints(_use_long_ints) { Init(); }
361  /** @brief Factorize the given SparseMatrix using the defaults. For larger
362  matrices, if the solver fails, set the parameter @a _use_long_ints =
363  true. */
364  UMFPackSolver(SparseMatrix &A, bool _use_long_ints = false)
365  : use_long_ints(_use_long_ints) { Init(); SetOperator(A); }
366 
367  /** @brief Factorize the given Operator @a op which must be a SparseMatrix.
368 
369  The factorization uses the parameters set in the #Control data member.
370  @note This method calls SparseMatrix::SortColumnIndices() with @a op,
371  modifying the matrix if the column indices are not already sorted. */
372  virtual void SetOperator(const Operator &op);
373 
374  /// Set the print level field in the #Control data member.
375  void SetPrintLevel(int print_lvl) { Control[UMFPACK_PRL] = print_lvl; }
376 
377  virtual void Mult(const Vector &b, Vector &x) const;
378  virtual void MultTranspose(const Vector &b, Vector &x) const;
379 
380  virtual ~UMFPackSolver();
381 };
382 
383 /// Direct sparse solver using KLU
384 class KLUSolver : public Solver
385 {
386 protected:
388  klu_symbolic *Symbolic;
389  klu_numeric *Numeric;
390 
391  void Init();
392 
393 public:
395  : mat(0),Symbolic(0),Numeric(0)
396  { Init(); }
398  : mat(0),Symbolic(0),Numeric(0)
399  { Init(); SetOperator(A); }
400 
401  // Works on sparse matrices only; calls SparseMatrix::SortColumnIndices().
402  virtual void SetOperator(const Operator &op);
403 
404  virtual void Mult(const Vector &b, Vector &x) const;
405  virtual void MultTranspose(const Vector &b, Vector &x) const;
406 
407  virtual ~KLUSolver();
408 
409  mutable klu_common Common;
410 };
411 
412 #endif // MFEM_USE_SUITESPARSE
413 
414 }
415 
416 #endif // MFEM_SOLVERS
Conjugate gradient method.
Definition: solvers.hpp:111
virtual void MultTranspose(const Vector &b, Vector &x) const
Action of the transpose operator: y=A^t(x). The default behavior in class Operator is to generate an ...
Definition: solvers.cpp:1820
double Info[UMFPACK_INFO]
Definition: solvers.hpp:355
SparseMatrix * mat
Definition: solvers.hpp:347
int GetNumIterations() const
Definition: solvers.hpp:66
const Operator * oper
Definition: solvers.hpp:41
SuiteSparse_long * AI
Definition: solvers.hpp:349
virtual void Mult(const Vector &b, Vector &x) const
Operator application: y=A(x).
Definition: solvers.cpp:295
void SetSize(int s)
Resize the vector to size s.
Definition: vector.hpp:320
NewtonSolver(MPI_Comm _comm)
Definition: solvers.hpp:267
FGMRES method.
Definition: solvers.hpp:161
virtual void Mult(const Vector &b, Vector &x) const
Operator application: y=A(x).
Definition: solvers.cpp:702
Direct sparse solver using KLU.
Definition: solvers.hpp:384
virtual void SetSolver(Solver &solver)
Set the linear solver for inverting the Jacobian.
Definition: solvers.hpp:273
virtual void MultTranspose(const Vector &b, Vector &x) const
Action of the transpose operator: y=A^t(x). The default behavior in class Operator is to generate an ...
Definition: solvers.cpp:1921
UMFPackSolver(SparseMatrix &A, bool _use_long_ints=false)
Factorize the given SparseMatrix using the defaults. For larger matrices, if the solver fails...
Definition: solvers.hpp:364
MINRES method.
Definition: solvers.hpp:220
void add(const Vector &v1, const Vector &v2, Vector &v)
Definition: vector.cpp:264
double Norm(const Vector &x) const
Definition: solvers.hpp:52
double GetFinalNorm() const
Definition: solvers.hpp:68
int BiCGSTAB(const Operator &A, Vector &x, const Vector &b, Solver &M, int &max_iter, double &tol, double atol, int printit)
BiCGSTAB method. (tolerances are squared)
Definition: solvers.cpp:1009
virtual void Mult(const Vector &b, Vector &x) const
Operator application: y=A(x).
Definition: solvers.cpp:117
void SetKDim(int dim)
Definition: solvers.hpp:173
FGMRESSolver(MPI_Comm _comm)
Definition: solvers.hpp:170
Direct sparse solver using UMFPACK.
Definition: solvers.hpp:343
UMFPackSolver(bool _use_long_ints=false)
For larger matrices, if the solver fails, set the parameter _use_long_ints = true.
Definition: solvers.hpp:359
virtual void Mult(const Vector &b, Vector &x) const
Operator application: y=A(x).
Definition: solvers.cpp:522
virtual void Mult(const Vector &b, Vector &x) const
Operator application: y=A(x).
Definition: solvers.cpp:1046
void SetLinearConstraint(const Vector &_w, double _a)
Definition: solvers.cpp:1476
int dim
Definition: ex3.cpp:47
virtual void SetPreconditioner(Solver &pr)
This should be called before SetOperator.
Definition: solvers.hpp:233
void SetPrintLevel(int print_lvl)
Definition: solvers.cpp:72
virtual void SetOperator(const Operator &op)
Also calls SetOperator for the preconditioner if there is one.
Definition: solvers.cpp:99
double solve(double l, const Vector &xt, Vector &x, int &nclip) const
Solve QP at fixed lambda.
Definition: solvers.hpp:310
Data type sparse matrix.
Definition: sparsemat.hpp:38
void MINRES(const Operator &A, const Vector &b, Vector &x, int print_it, int max_it, double rtol, double atol)
MINRES method without preconditioner. (tolerances are squared)
Definition: solvers.cpp:1204
virtual ~UMFPackSolver()
Definition: solvers.cpp:1854
void SetMaxIter(int max_it)
Definition: solvers.hpp:63
void median(const Vector &lo, const Vector &hi)
v = median(v,lo,hi) entrywise. Implementation assumes lo <= hi.
Definition: vector.cpp:445
SuiteSparse_long * AJ
Definition: solvers.hpp:349
void SetKDim(int dim)
Definition: solvers.hpp:155
Newton's method for solving F(x)=b for a given operator F.
Definition: solvers.hpp:258
void CG(const Operator &A, const Vector &b, Vector &x, int print_iter, int max_num_iter, double RTOLERANCE, double ATOLERANCE)
Conjugate gradient method. (tolerances are squared)
Definition: solvers.cpp:444
void print_iteration(int it, double r, double l) const
Definition: solvers.cpp:1494
void PCG(const Operator &A, Solver &B, const Vector &b, Vector &x, int print_iter, int max_num_iter, double RTOLERANCE, double ATOLERANCE)
Preconditioned conjugate gradient method. (tolerances are squared)
Definition: solvers.cpp:457
virtual void SetOperator(const Operator &op)
Set/update the solver for the given operator.
Definition: solvers.cpp:1876
virtual void SetOperator(const Operator &op)
Also calls SetOperator for the preconditioner if there is one.
Definition: solvers.cpp:1032
double Dot(const Vector &x, const Vector &y) const
Definition: solvers.cpp:51
int GetConverged() const
Definition: solvers.hpp:67
BiCGSTABSolver(MPI_Comm _comm)
Definition: solvers.hpp:200
klu_symbolic * Symbolic
Definition: solvers.hpp:388
SLBQPOptimizer(MPI_Comm _comm)
Definition: solvers.hpp:324
virtual void Mult(const Vector &b, Vector &x) const
Solve the nonlinear system with right-hand side b.
Definition: solvers.cpp:1241
CGSolver(MPI_Comm _comm)
Definition: solvers.hpp:122
void SetPrintLevel(int print_lvl)
Set the print level field in the Control data member.
Definition: solvers.hpp:375
Stationary linear iteration: x <- x + B (b - A x)
Definition: solvers.hpp:79
void SetAbsTol(double atol)
Definition: solvers.hpp:62
void SetRelTol(double rtol)
Definition: solvers.hpp:61
double Control[UMFPACK_CONTROL]
Definition: solvers.hpp:354
Abstract base class for iterative solver.
Definition: solvers.hpp:32
virtual void Mult(const Vector &b, Vector &x) const
Operator application: y=A(x).
Definition: solvers.cpp:880
virtual void Mult(const Vector &b, Vector &x) const
Operator application: y=A(x).
Definition: solvers.cpp:1907
virtual void SetOperator(const Operator &op)
Also calls SetOperator for the preconditioner if there is one.
Definition: solvers.cpp:1488
SLISolver(MPI_Comm _comm)
Definition: solvers.hpp:90
GMRES method.
Definition: solvers.hpp:143
GMRESSolver(MPI_Comm _comm)
Definition: solvers.hpp:152
void SetBounds(const Vector &_lo, const Vector &_hi)
Definition: solvers.cpp:1470
void UpdateVectors()
Definition: solvers.cpp:111
klu_common Common
Definition: solvers.hpp:409
virtual double ComputeScalingFactor(const Vector &x, const Vector &b) const
This method can be overloaded in derived classes to implement line search algorithms.
Definition: solvers.hpp:283
virtual void SetOperator(const Operator &op)
Also calls SetOperator for the preconditioner if there is one.
Definition: solvers.hpp:203
virtual void Mult(const Vector &xt, Vector &x) const
Operator application: y=A(x).
Definition: solvers.cpp:1501
virtual void SetOperator(const Operator &op)
Also calls SetOperator for the preconditioner if there is one.
Definition: solvers.hpp:125
Vector data type.
Definition: vector.hpp:41
MINRESSolver(MPI_Comm _comm)
Definition: solvers.hpp:230
virtual void SetPreconditioner(Solver &pr)
This should be called before SetOperator.
Definition: solvers.cpp:93
KLUSolver(SparseMatrix &A)
Definition: solvers.hpp:397
int aGMRES(const Operator &A, Vector &x, const Vector &b, const Operator &M, int &max_iter, int m_max, int m_min, int m_step, double cf, double &tol, double &atol, int printit)
Definition: solvers.cpp:1318
virtual ~KLUSolver()
Definition: solvers.cpp:1935
void UpdateVectors()
Definition: solvers.cpp:288
BiCGSTAB method.
Definition: solvers.hpp:189
SparseMatrix * mat
Definition: solvers.hpp:387
Base class for solvers.
Definition: operator.hpp:259
virtual void SetPreconditioner(Solver &pr)
These are not currently meaningful for this solver and will error out.
Definition: solvers.cpp:1482
Abstract operator.
Definition: operator.hpp:21
klu_numeric * Numeric
Definition: solvers.hpp:389
int width
Dimension of the input / number of columns in the matrix.
Definition: operator.hpp:25
virtual void Mult(const Vector &b, Vector &x) const
Operator application: y=A(x).
Definition: solvers.cpp:1788
virtual void SetOperator(const Operator &op)
Also calls SetOperator for the preconditioner if there is one.
Definition: solvers.hpp:93
void SLI(const Operator &A, const Vector &b, Vector &x, int print_iter, int max_num_iter, double RTOLERANCE, double ATOLERANCE)
Stationary linear iteration. (tolerances are squared)
Definition: solvers.cpp:260
int GMRES(const Operator &A, Vector &x, const Vector &b, Solver &M, int &max_iter, int m, double &tol, double atol, int printit)
GMRES method. (tolerances are squared)
Definition: solvers.cpp:844
virtual void SetOperator(const Operator &op)
Also calls SetOperator for the preconditioner if there is one.
Definition: solvers.cpp:1230
virtual void SetOperator(const Operator &op)
Factorize the given Operator op which must be a SparseMatrix.
Definition: solvers.cpp:1688