HermesCommon  3.0
linear_matrix_solver.h
Go to the documentation of this file.
1 // This file is part of HermesCommon
2 //
3 // Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
4 // Email: hpfem-group@unr.edu, home page: http://www.hpfem.org/.
5 //
6 // Hermes2D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published
8 // by the Free Software Foundation; either version 2 of the License,
9 // or (at your option) any later version.
10 //
11 // Hermes2D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Hermes2D; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #ifndef __HERMES_COMMON_LINEAR_MATRIX_SOLVER_H_
23 #define __HERMES_COMMON_LINEAR_MATRIX_SOLVER_H_
24 
25 #include "precond.h"
26 #include "exceptions.h"
27 #include "algebra/cs_matrix.h"
28 #include "algebra/vector.h"
29 #include "mixins.h"
30 #include "algebra/algebra_mixins.h"
31 
32 using namespace Hermes::Algebra;
33 
35 namespace Hermes
36 {
38  namespace Solvers
39  {
40  using namespace Hermes::Preconditioners;
76  {
85  };
87 
89  template <typename Scalar> class DirectSolver;
90  template <typename Scalar> class LoopSolver;
91  template <typename Scalar> class IterSolver;
92  template <typename Scalar> class AMGSolver;
93 
99  template <typename Scalar>
101  {
102  public:
104  DirectSolver<Scalar>* as_DirectSolver() const;
105  LoopSolver<Scalar>* as_LoopSolver() const;
106  IterSolver<Scalar>* as_IterSolver() const;
107  AMGSolver<Scalar>* as_AMGSolver() const;
108  virtual ~LinearMatrixSolver();
109  virtual void free() = 0;
110 
113  virtual void solve() = 0;
114 
118  virtual void solve(Scalar* initial_guess) = 0;
119 
122  Scalar *get_sln_vector();
123 
126  double get_time();
127 
129  virtual int get_matrix_size() = 0;
130 
132  virtual MatrixStructureReuseScheme get_used_reuse_scheme() const;
133 
136  virtual void set_reuse_scheme(MatrixStructureReuseScheme reuse_scheme);
137 
139  virtual void set_reuse_scheme();
140 
142  virtual void use_node_wise_ordering(unsigned int num_pdes);
143  virtual void use_equations_wise_ordering();
144 
146  virtual double get_residual_norm();
147 
148  SparseMatrix<Scalar>* get_matrix();
149  Vector<Scalar>* get_rhs();
150 
151  protected:
154 
155  SparseMatrix<Scalar>* general_matrix;
156  Vector<Scalar>* general_rhs;
157 
159  Scalar *sln;
160 
162  double time;
163 
165  unsigned int n_eq;
166 
167  bool node_wise_ordering;
168  };
169 
172  template <typename Scalar>
173  class HERMES_API ExternalSolver : public LinearMatrixSolver<Scalar>, public Algebra::Mixins::MatrixRhsOutput < Scalar >
174  {
175  public:
176  typedef ExternalSolver<Scalar>* (*creation)(CSCMatrix<Scalar> *m, SimpleVector<Scalar> *rhs);
177  static creation create_external_solver;
178 
180  virtual void solve() { throw Exceptions::MethodNotOverridenException("ExternalSolver::solve()."); };
181  virtual void solve(Scalar* initial_guess) { throw Exceptions::MethodNotOverridenException("ExternalSolver::solve()."); };
182  virtual int get_matrix_size() { return this->m->get_size(); };
185  CSCMatrix<Scalar> *get_matrix() { return this->m; }
188  SimpleVector<Scalar> *get_rhs() { return this->rhs; }
189 
190  protected:
195  };
196 
198  template <typename Scalar>
199  class HERMES_API SimpleExternalSolver : public ExternalSolver < Scalar >
200  {
201  public:
203  void solve();
204  void solve(Scalar* initial_guess);
205 
206  protected:
210  virtual std::string command() = 0;
211  };
212 
215  template <typename Scalar>
216  class HERMES_API DirectSolver : public LinearMatrixSolver < Scalar >
217  {
218  public:
220  virtual void solve() = 0;
221  virtual void solve(Scalar* initial_guess);
222 
224  virtual double get_residual_norm() { return 0.; };
225  };
226 
231  {
232  AbsoluteTolerance = 0,
233  RelativeTolerance = 1,
234  DivergenceTolerance = 2
235  };
236 
238  template <typename Scalar>
239  class HERMES_API LoopSolver : public LinearMatrixSolver < Scalar >
240  {
241  public:
243 
245  virtual int get_num_iters() = 0;
246 
248  virtual double get_residual_norm() = 0;
249 
252  virtual void set_tolerance(double tol);
253 
257  virtual void set_tolerance(double tolerance, LoopSolverToleranceType toleranceType);
258 
261  virtual void set_max_iters(int iters);
262 
263  protected:
267  double tolerance;
271  };
272 
276  {
277  CG = 0,
278  GMRES = 1,
279  BiCGStab = 2,
280  CR = 3,
281  IDR = 4
282  };
283 
286  template <typename Scalar>
287  class HERMES_API IterSolver : public virtual LoopSolver < Scalar >
288  {
289  public:
291 
293  virtual void set_precond(Precond<Scalar> *pc) = 0;
294 
297  void set_solver_type(IterSolverType iterSolverType);
298 
299  protected:
302 
303  // Paralution solver type.
304  IterSolverType iterSolverType;
305  };
306 
309  template <typename Scalar>
310  class HERMES_API AMGSolver : public virtual LoopSolver < Scalar >
311  {
312  public:
314 
316  virtual void set_smoother(IterSolverType solverType, PreconditionerType preconditionerType);
317 
318  protected:
321  PreconditionerType smootherPreconditionerType;
322  };
323 
328  template<typename Scalar>
329  HERMES_API LinearMatrixSolver<Scalar>*
330  create_linear_solver(Matrix<Scalar>* matrix, Vector<Scalar>* rhs, bool use_direct_solver = false);
331  }
332 } // End of documentation group Solvers.
334 #endif
General (abstract) matrix representation in Hermes.
Definition: matrix.h:36
virtual void solve(Scalar *initial_guess)
virtual unsigned int get_size() const
Definition: matrix.cpp:82
General namespace for the Hermes library.
General (abstract) vector representation in Hermes.
Abstract class to define interface for preconditioners.
Definition: precond.h:57
double tolerance
Convergence tolerance.
Namespace containing classes for vector / matrix operations.
CSCMatrix< Scalar > * m
Matrix to solve.
bool precond_yes
Whether the solver is preconditioned.
SimpleVector< Scalar > * rhs
Right hand side vector.
PreconditionerType
The preconditioner type.
Definition: precond.h:42
CSCMatrix< Scalar > * get_matrix()
General (abstract) sparse matrix representation in Hermes.
IterSolverType smootherSolverType
Smoother.
Class using time measurement Can be used directly (is not abstract), so one can use e...
Definition: mixins.h:183
Abstract class for defining interface for Algebraic Multigrid solvers. Internal, though utilizable fo...
An example class for using external solvers that run a command and store the result in a file...
Basic cs (Compressed sparse) matrix classes and operations.
Vector used with MUMPS solver.
Definition: vector.h:116
Special-purpose abstract class for using external solvers. For examples implementation, see the class SimpleExternalSolver.
virtual int get_matrix_size()
Get size of matrix.
General CSC Matrix class. (can be used in umfpack, in that case use the CSCMatrix subclass...
Definition: cs_matrix.h:131
Abstract middle-class for solvers that work in a loop of a kind (iterative, multigrid, ...)
HERMES_API LinearMatrixSolver< Scalar > * create_linear_solver(Matrix< Scalar > *matrix, Vector< Scalar > *rhs, bool use_direct_solver=false)
Function returning a solver according to the users's choice.
LoopSolverToleranceType toleranceType
File containing definition of exceptions classes.
Mixins classes for algebraic purposes.
Abstract class for defining solver interface.
Namespace containing objects for preconditioners.
Definition: epetra.h:48
Abstract class for defining interface for iterative solvers. Internal, though utilizable for defining...
unsigned int n_eq
Number of equations in a system of PDEs.
Basic vector classes and operations.
General functionality for preconditioners. Contains class Precond.
Mix-in classes for one functionality, for various classes to be derived from.
int max_iters
Maximum number of iterations.
pattern as the one already factorized.
Method is not overriden and should be.
Definition: exceptions.h:201
virtual double get_residual_norm()
Returns 0. - for compatibility.
SimpleVector< Scalar > * get_rhs()
MatrixStructureReuseScheme reuse_scheme
Factorization scheme.
Class the output of which is loggable, i.e. that uses functionality of info(), warn() Contains the cl...
Definition: mixins.h:62