HermesCommon  2.0
umfpack_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://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_UMFPACK_SOLVER_H_
23 #define __HERMES_COMMON_UMFPACK_SOLVER_H_
24 #include "config.h"
25 #ifdef WITH_UMFPACK
26 #include "linear_matrix_solver.h"
27 #include "matrix.h"
28 
29 using namespace Hermes::Algebra;
30 
31 namespace Hermes
32 {
33  namespace Solvers
34  {
35  template <typename Scalar> class HERMES_API UMFPackLinearMatrixSolver;
36  template <typename Scalar> class HERMES_API UMFPackIterator;
37  }
38 
39  namespace Algebra
40  {
41  using namespace Hermes::Solvers;
45  template <typename Scalar>
46  class HERMES_API CSCMatrix : public SparseMatrix<Scalar>
47  {
48  public:
55  void create(unsigned int size, unsigned int nnz, int* ap, int* ai, Scalar* ax);
56 
58  CSCMatrix();
62  CSCMatrix(unsigned int size);
63  virtual ~CSCMatrix();
64  virtual void alloc();
65  virtual void free();
66  virtual Scalar get(unsigned int m, unsigned int n);
67  virtual void zero();
68  virtual void add(unsigned int m, unsigned int n, Scalar v);
69  virtual void add_to_diagonal(Scalar v);
72  virtual void add_matrix(CSCMatrix<Scalar>* mat);
76  virtual void add_to_diagonal_blocks(int num_stages, CSCMatrix<Scalar>* mat);
77  virtual void add_sparse_to_diagonal_blocks(int num_stages, SparseMatrix<Scalar>* mat);
82  virtual void add_as_block(unsigned int i, unsigned int j, CSCMatrix<Scalar>* mat);
83  virtual void add(unsigned int m, unsigned int n, Scalar **mat, int *rows, int *cols);
84  virtual bool dump(FILE *file, const char *var_name, EMatrixDumpFormat fmt = DF_MATLAB_SPARSE, char* number_format = "%lf");
85  virtual unsigned int get_matrix_size() const;
86  virtual unsigned int get_nnz() const;
87  virtual double get_fill_in() const;
88 
89  // Applies the matrix to vector_in and saves result to vector_out.
90  void multiply_with_vector(Scalar* vector_in, Scalar* vector_out);
91  // Multiplies matrix with a Scalar.
92  void multiply_with_Scalar(Scalar value);
93 
94  // Duplicates a matrix (including allocation).
95  CSCMatrix* duplicate();
96  // Exposes pointers to the CSC arrays.
98  int *get_Ap();
99  // Exposes pointers to the CSC arrays.
101  int *get_Ai();
102  // Exposes pointers to the CSC arrays.
104  Scalar *get_Ax();
105 
106  protected:
107  // UMFPack specific data structures for storing the system matrix (CSC format).
109  Scalar *Ax;
111  int *Ai;
113  int *Ap;
115  unsigned int nnz;
116  template <typename T> friend class Hermes::Solvers::UMFPackLinearMatrixSolver;
117  template <typename T> friend class Hermes::Solvers::UMFPackIterator;
118  template<typename T> friend SparseMatrix<T>* create_matrix();
119  };
120 
122  template <typename Scalar>
123  class HERMES_API UMFPackMatrix : public CSCMatrix<Scalar>
124  {
125  template <typename T> friend class Hermes::Solvers::UMFPackLinearMatrixSolver;
126  template <typename T> friend class Hermes::Solvers::UMFPackIterator;
127  template<typename T> friend SparseMatrix<T>* create_matrix();
128  };
129 
131  template <typename Scalar>
132  class HERMES_API UMFPackVector : public Vector<Scalar>
133  {
134  public:
135  UMFPackVector();
138  UMFPackVector(unsigned int size);
139  virtual ~UMFPackVector();
140  virtual void alloc(unsigned int ndofs);
141  virtual void free();
142  virtual Scalar get(unsigned int idx);
143  virtual void extract(Scalar *v) const;
144  virtual void zero();
145  virtual void change_sign();
146  virtual void set(unsigned int idx, Scalar y);
147  virtual void add(unsigned int idx, Scalar y);
148  virtual void add(unsigned int n, unsigned int *idx, Scalar *y);
149  virtual void add_vector(Vector<Scalar>* vec);
150  virtual void add_vector(Scalar* vec);
151  virtual bool dump(FILE *file, const char *var_name, EMatrixDumpFormat fmt = DF_MATLAB_SPARSE, char* number_format = "%lf");
152 
155  Scalar *get_c_array();
156 
157  protected:
159  Scalar *v;
160  template <typename T> friend class Hermes::Solvers::UMFPackLinearMatrixSolver;
161  template <typename T> friend class Hermes::Solvers::UMFPackIterator;
162  template<typename T> friend Vector<T>* Hermes::Algebra::create_vector();
163  };
164  }
165  namespace Solvers
166  {
170  template <typename Scalar>
171  class HERMES_API UMFPackLinearMatrixSolver : public DirectSolver<Scalar>
172  {
173  public:
177  UMFPackLinearMatrixSolver(UMFPackMatrix<Scalar> *m, UMFPackVector<Scalar> *rhs);
178  virtual ~UMFPackLinearMatrixSolver();
179  virtual bool solve();
180  virtual int get_matrix_size();
181 
183  UMFPackMatrix<Scalar> *m;
185  UMFPackVector<Scalar> *rhs;
186 
189  void *symbolic;
190  void *numeric;
191 
193  void free_factorization_data();
195  bool setup_factorization();
196  template <typename T> friend class Hermes::Algebra::CSCMatrix;
197  template <typename T> friend class Hermes::Algebra::UMFPackMatrix;
198  template <typename T> friend class Hermes::Algebra::UMFPackVector;
199  template<typename T> friend LinearMatrixSolver<T>* create_linear_solver(Matrix<T>* matrix, Vector<T>* rhs);
200  void check_status(const char *fn_name, int status);
201  };
202 
204  template <typename Scalar>
205  class UMFPackIterator
206  {
207  protected:
208  UMFPackIterator(CSCMatrix<Scalar>* mat);
209  bool init();
210  void get_current_position(int& i, int& j, Scalar& val);
211  bool move_to_position(int i, int j);
212  bool move_ptr();
213  void add_to_current_position(Scalar val);
214  protected:
215  int size;
216  int nnz;
217  int* Ai;
218  int* Ap;
219  Scalar* Ax;
220  int Ai_pos;
221  int Ap_pos;
222  template <typename T> friend class Hermes::Algebra::CSCMatrix;
223  template <typename T> friend class Hermes::Algebra::UMFPackMatrix;
224  template <typename T> friend class Hermes::Algebra::UMFPackVector;
225  };
226  }
227 }
228 #endif
229 #endif