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 // - Id: $Id: linear_solvers.h 1354 2013-08-17 00:01:22Z secanell $
15 //
16 // ----------------------------------------------------------------------------
17 
18 #ifndef _FCST_LINEAR_SOLVERS_H_
19 #define _FCST_LINEAR_SOLVERS_H_
20 
21 #include <lac/vector.h>
22 #include <lac/block_vector.h>
23 
24 #include <lac/sparse_matrix.h>
25 #include <lac/block_sparse_matrix.h>
26 #include <lac/filtered_matrix.h>
27 
28 #include <lac/block_matrix_array.h>
29 #include <lac/vector_memory.h>
30 
31 #include <lac/sparse_direct.h>
32 #include <lac/solver_gmres.h>
33 
34 #include <lac/precondition.h>
35 #include <lac/sparse_ilu.h>
36 
37 using namespace dealii;
38 
58 namespace LinearSolvers
59 {
60 
64 static GrowingVectorMemory< Vector<double> > vector_pool;
65 static GrowingVectorMemory< BlockVector<double> > block_vector_pool;
66 
77 {
78 public:
79 
81 
82 
86  SparseDirectUMFPACKSolver() { deallog << "Solving the linear system using UMFPACK" << std::endl; }
87 
92 
94 
96 
97 
107  template<typename MATRIX, typename VECTOR>
108  void solve(const MATRIX& matrix,
109  VECTOR& solution,
110  const VECTOR& right_hand_side) const
111  {
112  Vector<double> tmp;
113  tmp = right_hand_side;
114 
115  SparseDirectUMFPACK solver;
116  solver.initialize(matrix);
117  solver.solve(tmp);
118 
119  solution = tmp;
120  }
121 
123 
124 };
125 
135 {
136 public:
137 
139 
140 
144  GMRESSolver() { deallog << "Solving the linear system using GMRES" << std::endl; }
145 
150 
152 
154 
155 
171  template<typename MATRIX, typename VECTOR, typename PRECONDITIONER>
172  void solve(const MATRIX& matrix,
173  VECTOR& solution,
174  const VECTOR& right_hand_side,
175  const unsigned int& n_iter,
176  const double& tolerance,
177  const PRECONDITIONER& preconditioner) const
178  {
179  SolverControl solver_control(n_iter,
180  tolerance);
181 
182  typename SolverGMRES< VECTOR >::AdditionalData additional_data(100,
183  false,
184  true);
185 
186  SolverGMRES< VECTOR > solver(solver_control,
187  additional_data);
188 
189  solver.solve(matrix,
190  solution,
191  right_hand_side,
192  preconditioner);
193  }
194 
196 
197 };
198 
208 {
209 public:
210 
212 
213 
217  ILUPreconditioner(const BlockSparseMatrix<double>& matrix)
218  :
219  preconditioner_matrix(matrix.n_block_rows()),
220  preconditioner_pointer(matrix.n_block_rows())
221  {
222  deallog << "ILUPreconditioner is used" << std::endl;
223 
224  preconditioner.initialize(matrix.n_block_rows(),
225  vector_pool);
226 
227  for(unsigned int i = 0; i < matrix.n_block_rows(); ++i)
228  {
229  preconditioner_matrix[i].initialize(matrix.block(i,i),
230  SparseILU<double>::AdditionalData());
231  preconditioner_pointer[i].set_memory(&vector_pool);
232  preconditioner_pointer[i] = &preconditioner_matrix[i];
233  }
234 
235  for(unsigned int i = 0; i < matrix.n_block_rows(); ++i)
236  for(unsigned int j = 0; j < matrix.n_block_cols(); ++j)
237  {
238  if( i == j )
239  preconditioner.enter(preconditioner_pointer[i], i, i);
240  else
241  preconditioner.enter(matrix.block(i,j), i, j);
242  }
243  }
244 
249 
251 
253 
254 
258  BlockTrianglePrecondition<double> preconditioner;
259 
263  std::vector< SparseILU<double> > preconditioner_matrix;
264 
268  std::vector< PointerMatrixAux< SparseILU<double>, Vector<double> > > preconditioner_pointer;
269 
271 
272 };
273 
274 } // LinearSolvers
275 
276 #endif