HermesCommon 1.0
petsc_solver.h
Go to the documentation of this file.
00001 // This file is part of HermesCommon
00002 //
00003 // Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
00004 // Email: hpfem-group@unr.edu, home page: http://hpfem.org/.
00005 //
00006 // Hermes2D is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published
00008 // by the Free Software Foundation; either version 2 of the License,
00009 // or (at your option) any later version.
00010 //
00011 // Hermes2D is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with Hermes2D; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00022 #ifndef __HERMES_COMMON_PETSC_SOLVER_H_
00023 #define __HERMES_COMMON_PETSC_SOLVER_H_
00024 
00025 #include "matrix.h"
00026 #include "linear_matrix_solver.h"
00027 
00028 #ifdef WITH_PETSC
00029 #include <petsc.h>
00030 #include <petscmat.h>
00031 #include <petscvec.h>
00032 #include <petscksp.h>
00033 
00034 namespace Hermes
00035 {
00036   namespace Solvers
00037   {
00038     template <typename Scalar> class PetscLinearMatrixSolver;
00039   }
00040 }
00041 
00042 namespace Hermes
00043 {
00044   namespace Algebra
00045   {
00047     template <typename Scalar>
00048     class PetscMatrix : public SparseMatrix<Scalar>
00049     {
00050     public:
00051       PetscMatrix();
00052       virtual ~PetscMatrix();
00053 
00054       virtual void alloc();
00055       virtual void free();
00056       virtual void finish();
00057       virtual Scalar get(unsigned int m, unsigned int n);
00058       virtual void zero();
00059       virtual void add(unsigned int m, unsigned int n, Scalar v);
00060       virtual void add_to_diagonal(Scalar v);
00061       virtual void add(unsigned int m, unsigned int n, Scalar **mat, int *rows, int *cols);
00062       virtual bool dump(FILE *file, const char *var_name, EMatrixDumpFormat fmt = DF_MATLAB_SPARSE, char* number_format = "%lf");
00063       virtual unsigned int get_matrix_size() const;
00064       virtual unsigned int get_nnz() const;
00065       virtual double get_fill_in() const;
00068       virtual void add_matrix(PetscMatrix* mat);
00072       virtual void add_to_diagonal_blocks(int num_stages, PetscMatrix* mat);
00073       virtual void add_sparse_to_diagonal_blocks(int num_stages, SparseMatrix<Scalar>* mat)
00074       {
00075         add_to_diagonal_blocks(num_stages, dynamic_cast<PetscMatrix<Scalar>*>(mat));
00076       }
00081       virtual void add_as_block(unsigned int i, unsigned int j, PetscMatrix* mat);
00082 
00083       // Applies the matrix to vector_in and saves result to vector_out.
00084       void multiply_with_vector(Scalar* vector_in, Scalar* vector_out);
00085 
00086       // Multiplies matrix with a Scalar.
00087       void multiply_with_Scalar(Scalar value);
00095       void create(unsigned int size, unsigned int nnz, int* ap, int* ai, Scalar* ax);
00096       // Duplicates a matrix (including allocation).
00097       PetscMatrix* duplicate();
00098     protected:
00100       Mat matrix;
00102       unsigned int nnz;
00104       bool inited;
00105 
00106       friend class Solvers::PetscLinearMatrixSolver<Scalar>;
00107     };
00108 
00111     template <typename Scalar>
00112     class PetscVector : public Vector<Scalar>
00113     {
00114     public:
00115       PetscVector();
00116       virtual ~PetscVector();
00117 
00118       virtual void alloc(unsigned int ndofs);
00119       virtual void free();
00121       virtual void finish();
00122       virtual Scalar get(unsigned int idx);
00123       virtual void extract(Scalar *v) const;
00124       virtual void zero();
00125       virtual void change_sign();
00126       virtual void set(unsigned int idx, Scalar y);
00127       virtual void add(unsigned int idx, Scalar y);
00128       virtual void add(unsigned int n, unsigned int *idx, Scalar *y);
00129       virtual void add_vector(Vector<Scalar>* vec);
00130       virtual void add_vector(Scalar* vec);
00131       virtual bool dump(FILE *file, const char *var_name, EMatrixDumpFormat fmt = DF_MATLAB_SPARSE, char* number_format = "%lf");
00132 
00133     protected:
00135       Vec vec;
00137       bool inited;
00138 
00139       friend class Solvers::PetscLinearMatrixSolver<Scalar>;
00140     };
00141   }
00142   namespace Solvers
00143   {
00147     template <typename Scalar>
00148     class HERMES_API PetscLinearMatrixSolver : public DirectSolver<Scalar>
00149     {
00150     public:
00151       PetscLinearMatrixSolver(PetscMatrix<Scalar> *mat, PetscVector<Scalar> *rhs);
00152       virtual ~PetscLinearMatrixSolver();
00153 
00154       virtual bool solve();
00155       virtual int get_matrix_size();
00156 
00158       PetscMatrix<Scalar> *m;
00160       PetscVector<Scalar> *rhs;
00161     };
00162   }
00163 }
00164 #endif
00165 #endif