OpenFCST: The open-source Fuel Cell Simulation Toolbox
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
linear_solvers.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 //
3 // FCST: Fuel Cell Simulation Toolbox
4 //
5 // Copyright (C) 2006-2013 by Energy Systems Design Laboratory, University of Alberta
6 //
7 // This software is distributed under the MIT License
8 // For more information, see the README file in /doc/LICENSE
9 //
10 // - Class: linear_solvers.h
11 // - Description: This namespace contains various linear solvers and preconditioners
12 // - Developers: Valentin N. Zingan, University of Alberta
13 // Marc Secanell Gallart, University of Alberta
14 //
15 // ----------------------------------------------------------------------------
16 
17 #ifndef _FCST_LINEAR_SOLVERS_H_
18 #define _FCST_LINEAR_SOLVERS_H_
19 
20 //-- dealII
21 #include <deal.II/lac/vector.h>
22 #include <deal.II/lac/block_vector.h>
23 #include <deal.II/lac/sparse_matrix.h>
24 #include <deal.II/lac/block_sparse_matrix.h>
25 #include <deal.II/lac/block_matrix_array.h>
26 #include <deal.II/lac/vector_memory.h>
27 #include <deal.II/lac/sparse_direct.h>
28 #include <deal.II/lac/solver_gmres.h>
29 #include <deal.II/lac/precondition.h>
30 #include <deal.II/lac/sparse_ilu.h>
31 
32 using namespace dealii;
33 
53 namespace LinearSolvers
54 {
55 
59  static GrowingVectorMemory< Vector<double> > vector_pool;
60  static GrowingVectorMemory< BlockVector<double> > block_vector_pool;
61 
72  {
73  public:
74 
76 
77 
81  SparseDirectUMFPACKSolver() { FcstUtilities::log << "Solving the linear system using UMFPACK" << std::endl; }
82 
87 
89 
91 
92 
102  template<typename MATRIX, typename VECTOR>
103  void solve(const MATRIX& matrix,
104  VECTOR& solution,
105  const VECTOR& right_hand_side) const
106  {
107  Vector<double> tmp;
108  tmp = right_hand_side;
109 
110  SparseDirectUMFPACK solver;
111  solver.initialize(matrix);
112  solver.solve(tmp);
113 
114  solution = tmp;
115  }
116 
118 
119  };
120 
130  {
131  public:
132 
134 
135 
139  GMRESSolver() { FcstUtilities::log << "Solving the linear system using GMRES" << std::endl; }
140 
145 
147 
149 
150 
162  template<typename MATRIX, typename VECTOR, typename PRECONDITIONER>
163  void solve(const MATRIX& matrix,
164  VECTOR& solution,
165  const VECTOR& right_hand_side,
166  const unsigned int& n_iter,
167  const double& tolerance,
168  const PRECONDITIONER& preconditioner) const
169  {
170  SolverControl solver_control(n_iter, tolerance);
171 
172  typename SolverGMRES< VECTOR >::AdditionalData additional_data(100, false, true);
173 
174  SolverGMRES< VECTOR > solver(solver_control, additional_data);
175 
176  solver.solve(matrix,
177  solution,
178  right_hand_side,
179  preconditioner);
180 
181  }
182 
194  template<typename MATRIX, typename VECTOR, typename PRECONDITIONER>
195  void solve(SolverControl& solver_control,
196  const MATRIX& matrix,
197  VECTOR& solution,
198  const VECTOR& right_hand_side,
199  const PRECONDITIONER& preconditioner) const
200  {
201  SolverGMRES< VECTOR > solver(solver_control);
202 
203  solver.solve(matrix,
204  solution,
205  right_hand_side,
206  preconditioner);
207 
208  }
210 
211  };
212 
222  {
223  public:
224 
226 
227 
231  ILUPreconditioner(const BlockSparseMatrix<double>& matrix)
232  :
233  preconditioner_matrix(matrix.n_block_rows()),
234  preconditioner_pointer(matrix.n_block_rows()),
235  preconditioner(matrix.n_block_rows())
236  {
237  FcstUtilities::log << "ILUPreconditioner is used" << std::endl;
238 
239  for(unsigned int i = 0; i < matrix.n_block_rows(); ++i)
240  {
241  preconditioner_matrix[i].initialize(matrix.block(i,i),
242  SparseILU<double>::AdditionalData());
243  preconditioner_pointer[i].set_memory(&vector_pool);
244  preconditioner_pointer[i] = &preconditioner_matrix[i];
245  }
246 
247  for(unsigned int i = 0; i < matrix.n_block_rows(); ++i)
248  for(unsigned int j = 0; j < matrix.n_block_cols(); ++j)
249  {
250  if( i == j )
251  preconditioner.enter(preconditioner_pointer[i], i, i);
252  else
253  preconditioner.enter(matrix.block(i,j), i, j);
254  }
255  }
256 
261 
263 
265 
266 
270  BlockTrianglePrecondition<double> preconditioner;
271 
272 
276  std::vector< SparseILU<double> > preconditioner_matrix;
277 
281  std::vector< PointerMatrixAux< SparseILU<double>, Vector<double> > > preconditioner_pointer;
282 
283 
285 
286  };
287 
288 } // LinearSolvers
289 
290 #endif
This class implements ILU preconditioner.
Definition: linear_solvers.h:221
SparseDirectUMFPACKSolver()
Constructor.
Definition: linear_solvers.h:81
~GMRESSolver()
Destructor.
Definition: linear_solvers.h:144
void solve(const MATRIX &matrix, VECTOR &solution, const VECTOR &right_hand_side) const
This function solves the linear system Ax = b where.
Definition: linear_solvers.h:103
This class implements GMRES solver.
Definition: linear_solvers.h:129
std::vector< PointerMatrixAux< SparseILU< double >, Vector< double > > > preconditioner_pointer
Preconditioner pointer.
Definition: linear_solvers.h:281
static GrowingVectorMemory< BlockVector< double > > block_vector_pool
Definition: linear_solvers.h:60
void solve(SolverControl &solver_control, const MATRIX &matrix, VECTOR &solution, const VECTOR &right_hand_side, const PRECONDITIONER &preconditioner) const
This routine is used when solver_control is an object already initialized.
Definition: linear_solvers.h:195
FCSTLogStream log
Object used to output data to file and, if file attached recorded to a file as well.
BlockTrianglePrecondition< double > preconditioner
Preconditioner.
Definition: linear_solvers.h:270
This class implements an interface to the sparse direct solver UMFPACK, see the link below http://www...
Definition: linear_solvers.h:71
~ILUPreconditioner()
Destructor.
Definition: linear_solvers.h:260
void solve(const MATRIX &matrix, VECTOR &solution, const VECTOR &right_hand_side, const unsigned int &n_iter, const double &tolerance, const PRECONDITIONER &preconditioner) const
This function solves the linear system Ax = b where.
Definition: linear_solvers.h:163
ILUPreconditioner(const BlockSparseMatrix< double > &matrix)
Constructor.
Definition: linear_solvers.h:231
static GrowingVectorMemory< Vector< double > > vector_pool
Memory management objects.
Definition: linear_solvers.h:59
std::vector< SparseILU< double > > preconditioner_matrix
Preconditioner matrix.
Definition: linear_solvers.h:276
GMRESSolver()
Constructor.
Definition: linear_solvers.h:139
~SparseDirectUMFPACKSolver()
Destructor.
Definition: linear_solvers.h:86