$extrastylesheet
petsc_matrix.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 
00019 
00020 #ifndef LIBMESH_PETSC_MATRIX_H
00021 #define LIBMESH_PETSC_MATRIX_H
00022 
00023 #include "libmesh/libmesh_common.h"
00024 
00025 #ifdef LIBMESH_HAVE_PETSC
00026 
00027 // Local includes
00028 #include "libmesh/sparse_matrix.h"
00029 #include "libmesh/petsc_macro.h"
00030 #include "libmesh/parallel.h"
00031 
00032 // C++ includes
00033 #include <algorithm>
00034 
00035 // Macro to identify and debug functions which should be called in
00036 // parallel on parallel matrices but which may be called in serial on
00037 // serial matrices.  This macro will only be valid inside non-static
00038 // PetscMatrix methods
00039 #undef semiparallel_only
00040 #ifndef NDEBUG
00041 #include <cstring>
00042 
00043 #define semiparallel_only() do { if (this->initialized()) { const char *mytype; \
00044       MatGetType(_mat,&mytype);                                         \
00045       if (!strcmp(mytype, MATSEQAIJ))                                   \
00046         parallel_object_only(); } } while (0)
00047 #else
00048 #define semiparallel_only()
00049 #endif
00050 
00051 
00055 EXTERN_C_FOR_PETSC_BEGIN
00056 # include <petscmat.h>
00057 EXTERN_C_FOR_PETSC_END
00058 
00059 
00060 
00061 namespace libMesh
00062 {
00063 
00064 // Forward Declarations
00065 template <typename T> class DenseMatrix;
00066 
00067 
00076 template <typename T>
00077 class PetscMatrix : public SparseMatrix<T>
00078 {
00079 public:
00095   explicit
00096   PetscMatrix (const Parallel::Communicator &comm_in
00097                LIBMESH_CAN_DEFAULT_TO_COMMWORLD);
00098 
00106   explicit
00107   PetscMatrix (Mat m,
00108                const Parallel::Communicator &comm_in
00109                LIBMESH_CAN_DEFAULT_TO_COMMWORLD);
00110 
00116   ~PetscMatrix ();
00117 
00128   void init (const numeric_index_type m,
00129              const numeric_index_type n,
00130              const numeric_index_type m_l,
00131              const numeric_index_type n_l,
00132              const numeric_index_type nnz=30,
00133              const numeric_index_type noz=10,
00134              const numeric_index_type blocksize=1);
00135 
00146   void init (const numeric_index_type m,
00147              const numeric_index_type n,
00148              const numeric_index_type m_l,
00149              const numeric_index_type n_l,
00150              const std::vector<numeric_index_type>& n_nz,
00151              const std::vector<numeric_index_type>& n_oz,
00152              const numeric_index_type blocksize=1);
00153 
00157   void init ();
00158 
00165   void clear ();
00166 
00171   void zero ();
00172 
00176   void zero_rows (std::vector<numeric_index_type> & rows, T diag_value = 0.0);
00177 
00183   void close () const;
00184 
00189   numeric_index_type m () const;
00190 
00195   numeric_index_type n () const;
00196 
00201   numeric_index_type row_start () const;
00202 
00207   numeric_index_type row_stop () const;
00208 
00215   void set (const numeric_index_type i,
00216             const numeric_index_type j,
00217             const T value);
00218 
00227   void add (const numeric_index_type i,
00228             const numeric_index_type j,
00229             const T value);
00230 
00238   void add_matrix (const DenseMatrix<T> &dm,
00239                    const std::vector<numeric_index_type> &rows,
00240                    const std::vector<numeric_index_type> &cols);
00241 
00246   void add_matrix (const DenseMatrix<T> &dm,
00247                    const std::vector<numeric_index_type> &dof_indices);
00248 
00256   virtual void add_block_matrix (const DenseMatrix<T> &dm,
00257                                  const std::vector<numeric_index_type> &brows,
00258                                  const std::vector<numeric_index_type> &bcols);
00259 
00264   virtual void add_block_matrix (const DenseMatrix<T> &dm,
00265                                  const std::vector<numeric_index_type> &dof_indices)
00266   { this->add_block_matrix (dm, dof_indices, dof_indices); }
00267 
00279   void add (const T a, SparseMatrix<T> &X);
00280 
00288   T operator () (const numeric_index_type i,
00289                  const numeric_index_type j) const;
00290 
00302   Real l1_norm () const;
00303 
00316   Real linfty_norm () const;
00317 
00322   bool closed() const;
00323 
00331   void print_personal(std::ostream& os=libMesh::out) const;
00332 
00339   void print_matlab(const std::string& name = "") const;
00340 
00344   virtual void get_diagonal (NumericVector<T>& dest) const;
00345 
00350   virtual void get_transpose (SparseMatrix<T>& dest) const;
00351 
00355   void swap (PetscMatrix<T> &);
00356 
00362   Mat mat () { libmesh_assert (_mat); return _mat; }
00363 
00364 protected:
00365 
00375   virtual void _get_submatrix(SparseMatrix<T>& submatrix,
00376                               const std::vector<numeric_index_type>& rows,
00377                               const std::vector<numeric_index_type>& cols,
00378                               const bool reuse_submatrix) const;
00379 
00380 private:
00381 
00385   Mat _mat;
00386 
00391   bool _destroy_mat_on_exit;
00392 };
00393 
00394 } // namespace libMesh
00395 
00396 #endif // #ifdef LIBMESH_HAVE_PETSC
00397 #endif // LIBMESH_PETSC_MATRIX_H