HermesCommon  2.0
petsc_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_PETSC_SOLVER_H_
23 #define __HERMES_COMMON_PETSC_SOLVER_H_
24 
25 #include "matrix.h"
26 #include "linear_matrix_solver.h"
27 
28 #ifdef WITH_PETSC
29 #include <petsc.h>
30 #include <petscmat.h>
31 #include <petscvec.h>
32 #include <petscksp.h>
33 
34 namespace Hermes
35 {
36  namespace Solvers
37  {
38  template <typename Scalar> class PetscLinearMatrixSolver;
39  }
40 }
41 
42 namespace Hermes
43 {
44  namespace Algebra
45  {
47  template <typename Scalar>
48  class PetscMatrix : public SparseMatrix<Scalar>
49  {
50  public:
51  PetscMatrix();
52  virtual ~PetscMatrix();
53 
54  virtual void alloc();
55  virtual void free();
56  virtual void finish();
57  virtual Scalar get(unsigned int m, unsigned int n);
58  virtual void zero();
59  virtual void add(unsigned int m, unsigned int n, Scalar v);
60  virtual void add_to_diagonal(Scalar v);
61  virtual void add(unsigned int m, unsigned int n, Scalar **mat, int *rows, int *cols);
62  virtual bool dump(FILE *file, const char *var_name, EMatrixDumpFormat fmt = DF_MATLAB_SPARSE, char* number_format = "%lf");
63  virtual unsigned int get_matrix_size() const;
64  virtual unsigned int get_nnz() const;
65  virtual double get_fill_in() const;
68  virtual void add_matrix(PetscMatrix* mat);
72  virtual void add_to_diagonal_blocks(int num_stages, PetscMatrix* mat);
73  virtual void add_sparse_to_diagonal_blocks(int num_stages, SparseMatrix<Scalar>* mat)
74  {
75  add_to_diagonal_blocks(num_stages, dynamic_cast<PetscMatrix<Scalar>*>(mat));
76  }
81  virtual void add_as_block(unsigned int i, unsigned int j, PetscMatrix* mat);
82 
83  // Applies the matrix to vector_in and saves result to vector_out.
84  void multiply_with_vector(Scalar* vector_in, Scalar* vector_out);
85 
86  // Multiplies matrix with a Scalar.
87  void multiply_with_Scalar(Scalar value);
95  void create(unsigned int size, unsigned int nnz, int* ap, int* ai, Scalar* ax);
96  // Duplicates a matrix (including allocation).
97  PetscMatrix* duplicate();
98  protected:
100  Mat matrix;
102  unsigned int nnz;
104  bool inited;
105 
106  friend class Solvers::PetscLinearMatrixSolver<Scalar>;
107  };
108 
111  template <typename Scalar>
112  class PetscVector : public Vector<Scalar>
113  {
114  public:
115  PetscVector();
116  virtual ~PetscVector();
117 
118  virtual void alloc(unsigned int ndofs);
119  virtual void free();
121  virtual void finish();
122  virtual Scalar get(unsigned int idx);
123  virtual void extract(Scalar *v) const;
124  virtual void zero();
125  virtual void change_sign();
126  virtual void set(unsigned int idx, Scalar y);
127  virtual void add(unsigned int idx, Scalar y);
128  virtual void add(unsigned int n, unsigned int *idx, Scalar *y);
129  virtual void add_vector(Vector<Scalar>* vec);
130  virtual void add_vector(Scalar* vec);
131  virtual bool dump(FILE *file, const char *var_name, EMatrixDumpFormat fmt = DF_MATLAB_SPARSE, char* number_format = "%lf");
132 
133  protected:
135  Vec vec;
137  bool inited;
138 
139  friend class Solvers::PetscLinearMatrixSolver<Scalar>;
140  };
141  }
142  namespace Solvers
143  {
147  template <typename Scalar>
148  class HERMES_API PetscLinearMatrixSolver : public DirectSolver<Scalar>
149  {
150  public:
151  PetscLinearMatrixSolver(PetscMatrix<Scalar> *mat, PetscVector<Scalar> *rhs);
152  virtual ~PetscLinearMatrixSolver();
153 
154  virtual bool solve();
155  virtual int get_matrix_size();
156 
158  PetscMatrix<Scalar> *m;
160  PetscVector<Scalar> *rhs;
161  };
162  }
163 }
164 #endif
165 #endif