HermesCommon  3.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://www.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 "config.h"
26 
27 #include "algebra/matrix.h"
29 
30 #ifdef WITH_PETSC
31 
32 #include <petsc.h>
33 #include <petscmat.h>
34 #include <petscvec.h>
35 #include <petscksp.h>
36 
37 namespace Hermes
38 {
39  namespace Solvers
40  {
41  template <typename Scalar> class PetscLinearMatrixSolver;
42  }
43 }
44 
45 namespace Hermes
46 {
47  namespace Algebra
48  {
50  template <typename Scalar>
51  class PetscMatrix : public SparseMatrix < Scalar >
52  {
53  public:
54  PetscMatrix();
55  virtual ~PetscMatrix();
56 
57  virtual void alloc();
58  virtual void free();
59  virtual void finish();
60  virtual Scalar get(unsigned int m, unsigned int n) const;
61  virtual void zero();
62  virtual void add(unsigned int m, unsigned int n, Scalar v);
64  virtual void export_to_file(const char *filename, const char *var_name, MatrixExportFormat fmt, char* number_format = "%lf");
65  virtual unsigned int get_nnz() const;
66  virtual double get_fill_in() const;
67 
71  virtual void add_sparse_matrix(SparseMatrix<Scalar>* mat);
72 
73  // Multiplies matrix with a Scalar.
74  void multiply_with_Scalar(Scalar value);
82  void create(unsigned int size, unsigned int nnz, int* ap, int* ai, Scalar* ax);
83  // Duplicates a matrix (including allocation).
84  PetscMatrix* duplicate() const;
85  protected:
88  virtual void add_petsc_matrix(PetscMatrix* mat);
89 
91  Mat matrix;
93  unsigned int nnz;
95  bool inited;
96 
97  friend class Solvers::PetscLinearMatrixSolver < Scalar > ;
98  };
99 
102  template <typename Scalar>
103  class PetscVector : public Vector < Scalar >
104  {
105  public:
106  PetscVector();
107  virtual ~PetscVector();
108 
109  virtual void alloc(unsigned int ndofs);
110  virtual void free();
112  virtual void finish();
113  virtual Scalar get(unsigned int idx) const;
114  virtual void extract(Scalar *v) const;
115  virtual void zero();
116  virtual Vector<Scalar>* change_sign();
117  virtual void set(unsigned int idx, Scalar y);
118  virtual void add(unsigned int idx, Scalar y);
119  virtual void add(unsigned int n, unsigned int *idx, Scalar *y);
120  virtual Vector<Scalar>* add_vector(Vector<Scalar>* vec);
121  virtual Vector<Scalar>* add_vector(Scalar* vec);
123  virtual void export_to_file(const char *filename, const char *var_name, MatrixExportFormat fmt, char* number_format = "%lf");
124 
125  protected:
127  Vec vec;
129  bool inited;
130 
131  friend class Solvers::PetscLinearMatrixSolver < Scalar > ;
132  };
133  }
134  namespace Solvers
135  {
138  template <typename Scalar>
139  class HERMES_API PetscLinearMatrixSolver : public DirectSolver < Scalar >
140  {
141  public:
142  PetscLinearMatrixSolver(PetscMatrix<Scalar> *mat, PetscVector<Scalar> *rhs);
143  virtual ~PetscLinearMatrixSolver();
144 
145  virtual void solve();
146  virtual int get_matrix_size();
147 
149  PetscMatrix<Scalar> *m;
151  PetscVector<Scalar> *rhs;
152  };
153  }
154 }
155 #endif
156 #endif
General (abstract) matrix representation in Hermes.
Definition: matrix.h:36
General namespace for the Hermes library.
Linear matrix solver functionality.
General (abstract) vector representation in Hermes.
General (abstract) sparse matrix representation in Hermes.
MatrixExportFormat
Format of file matrix and vector output.
Basic matrix classes and operations.
void change_sign(T **matrix, unsigned int m, unsigned int n)
Changes the sign of a matrix.