HermesCommon  2.0
mumps_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_MUMPS_SOLVER_H_
23 #define __HERMES_COMMON_MUMPS_SOLVER_H_
24 #include "config.h"
25 #ifdef WITH_MUMPS
26 #include "linear_matrix_solver.h"
27 #include "matrix.h"
28 
29 extern "C"
30 {
31 #include <mumps_c_types.h>
32 #include <dmumps_c.h>
33 #include <zmumps_c.h>
34 }
35 
36 #ifdef WITH_MPI
37 #include <mpi.h>
38 #endif
39 
40 namespace Hermes
41 {
42  namespace Solvers
43  {
44  template <typename Scalar> class MumpsSolver;
45  }
46 }
47 namespace Hermes
48 {
49  namespace Algebra
50  {
52  template <typename Scalar> struct mumps_type;
53 
55  template <>
56  struct mumps_type<std::complex<double> >
57  {
59  typedef ZMUMPS_STRUC_C mumps_struct;
61  typedef ZMUMPS_COMPLEX mumps_Scalar;
62  };
63 
65  template <>
66  struct mumps_type<double>
67  {
69  typedef DMUMPS_STRUC_C mumps_struct;
71  typedef double mumps_Scalar;
72  };
73 
75  template <typename Scalar>
76  class MumpsMatrix : public SparseMatrix<Scalar>
77  {
78  public:
79  MumpsMatrix();
80  virtual ~MumpsMatrix();
81 
82  virtual void alloc();
83  virtual void free();
84  virtual Scalar get(unsigned int m, unsigned int n);
85  virtual void zero();
86  virtual void add(unsigned int m, unsigned int n, Scalar v);
87  virtual void add_to_diagonal(Scalar v);
88  virtual void add(unsigned int m, unsigned int n, Scalar **mat, int *rows, int *cols);
89  virtual bool dump(FILE *file, const char *var_name, EMatrixDumpFormat fmt = DF_MATLAB_SPARSE, char* number_format = "%lf");
90  virtual unsigned int get_matrix_size() const;
91  virtual unsigned int get_nnz() const;
92  virtual double get_fill_in() const;
95  virtual void add_matrix(MumpsMatrix* mat);
99  virtual void add_to_diagonal_blocks(int num_stages, MumpsMatrix* mat);
100  virtual void add_sparse_to_diagonal_blocks(int num_stages, SparseMatrix<Scalar>* mat);
105  virtual void add_as_block(unsigned int i, unsigned int j, MumpsMatrix* mat);
106 
108  void multiply_with_vector(Scalar* vector_in, Scalar* vector_out);
110  void multiply_with_Scalar(Scalar value);
112  void create(unsigned int size, unsigned int nnz, int* ap, int* ai, Scalar* ax);
114  MumpsMatrix* duplicate();
115 
116  protected:
118  unsigned int nnz;
119  int *irn;
120  int *jcn;
121  typename mumps_type<Scalar>::mumps_Scalar *Ax;
122  int *Ai;
123  unsigned int *Ap;
124 
125  friend class Solvers::MumpsSolver<Scalar>;
126  template<typename T> friend SparseMatrix<T>* create_matrix();
127  };
128 
130  template <typename Scalar>
131  class MumpsVector : public Vector<Scalar>
132  {
133  public:
134  MumpsVector();
135  virtual ~MumpsVector();
136 
137  virtual void alloc(unsigned int ndofs);
138  virtual void free();
139  virtual Scalar get(unsigned int idx);
140  virtual void extract(Scalar *v) const;
141  virtual void zero();
142  virtual void change_sign();
143  virtual void set(unsigned int idx, Scalar y);
144  virtual void add(unsigned int idx, Scalar y);
145  virtual void add(unsigned int n, unsigned int *idx, Scalar *y);
146  virtual void add_vector(Vector<Scalar>* vec);
147  virtual void add_vector(Scalar* vec);
148  virtual bool dump(FILE *file, const char *var_name, EMatrixDumpFormat fmt = DF_MATLAB_SPARSE, char* number_format = "%lf");
149 
150  protected:
151  // MUMPS specific data structures for storing the rhs.
153  Scalar *v;
154 
155  friend class Solvers::MumpsSolver<Scalar>;
156  };
157  }
158  namespace Solvers
159  {
163  template <typename Scalar>
164  class HERMES_API MumpsSolver : public DirectSolver<Scalar>
165  {
166  public:
170  MumpsSolver(MumpsMatrix<Scalar> *m, MumpsVector<Scalar> *rhs);
171  virtual ~MumpsSolver();
172 
173  virtual bool solve();
174  virtual int get_matrix_size();
175 
177  MumpsMatrix<Scalar> *m;
179  MumpsVector<Scalar> *rhs;
180 
182  bool setup_factorization();
183 
185  typename mumps_type<Scalar>::mumps_struct param;
186 
188  bool check_status();
189 
193  bool reinit();
195  bool inited;
196  private:
197  void mumps_c(typename mumps_type<Scalar>::mumps_struct * param); //wrapper around dmums_c or zmumps_c
198  };
199  }
200 }
201 #endif
202 #endif