MFEM  v3.3.2
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
blockmatrix.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_BLOCKMATRIX
13 #define MFEM_BLOCKMATRIX
14 
15 #include "../config/config.hpp"
16 #include "../general/array.hpp"
17 #include "../general/globals.hpp"
18 #include "vector.hpp"
19 #include "sparsemat.hpp"
20 
21 namespace mfem
22 {
23 
25 {
26 public:
27  //! Constructor for square block matrices
28  /**
29  * offsets: offsets that mark the start of each row/column block (size nRowBlocks+1).
30  * Note: BlockMatrix will not own/copy the data contained in offsets.
31  */
32  BlockMatrix(const Array<int> & offsets);
33  //! Constructor for rectangular block matrices
34  /**
35  * row_offsets: offsets that mark the start of each row block (size nRowBlocks+1).
36  * col_offsets: offsets that mark the start of each column block (size nColBlocks+1).
37  * Note: BlockMatrix will not own/copy the data contained in offsets.
38  */
39  BlockMatrix(const Array<int> & row_offsets, const Array<int> & col_offsets);
40  //! Set A(i,j) = mat
41  void SetBlock(int i, int j, SparseMatrix * mat);
42  //! Return the number of row blocks
43  int NumRowBlocks() const {return nRowBlocks; }
44  //! Return the number of column blocks
45  int NumColBlocks() const {return nColBlocks; }
46  //! Return a reference to block (i,j). Reference may be invalid if Aij(i,j) == NULL
47  SparseMatrix & GetBlock(int i, int j);
48  //! Return a reference to block (i,j). Reference may be invalid if Aij(i,j)
49  //! == NULL. (const version)
50  const SparseMatrix & GetBlock(int i, int j) const;
51  //! Check if block (i,j) is a zero block.
52  int IsZeroBlock(int i, int j) const {return (Aij(i,j)==NULL) ? 1 : 0; }
53  //! Return the row offsets for block starts
54  Array<int> & RowOffsets() { return row_offsets; }
55  //! Return the columns offsets for block starts
56  Array<int> & ColOffsets() { return col_offsets; }
57  //! Return the row offsets for block starts (const version)
58  const Array<int> & RowOffsets() const { return row_offsets; }
59  //! Return the row offsets for block starts (const version)
60  const Array<int> & ColOffsets() const { return col_offsets; }
61  //! Return the number of non zeros in row i
62  int RowSize(const int i) const;
63  //! Symmetric elimination of the marked degree of freedom.
64  /**
65  * ess_bc_dofs: marker of the degree of freedom to be eliminated
66  * dof i is eliminated if ess_bc_dofs[i] = 1.
67  * sol: vector that stores the values of the degree of freedom that need to
68  * be eliminated
69  * rhs: vector that stores the rhs of the system.
70  */
71  void EliminateRowCol(Array<int> & ess_bc_dofs, Vector & sol, Vector & rhs);
72  //! Returns a monolithic CSR matrix that represents this operator.
74  //! Export the monolithic matrix to file.
75  void PrintMatlab(std::ostream & os = mfem::out) const;
76 
77  //@name Matrix interface
78  //@{
79  /// Returns reference to a_{ij}.
80  virtual double& Elem (int i, int j);
81  /// Returns constant reference to a_{ij}.
82  virtual const double& Elem (int i, int j) const;
83  /// Returns a pointer to (approximation) of the matrix inverse.
84  virtual MatrixInverse * Inverse() const
85  {
86  mfem_error("BlockMatrix::Inverse not implemented \n");
87  return static_cast<MatrixInverse*>(NULL);
88  }
89  //@}
90 
91  //@name AbstractSparseMatrix interface
92  //@{
93  //! Returns the total number of non zeros in the matrix.
94  virtual int NumNonZeroElems() const;
95  /// Gets the columns indexes and values for row *row*.
96  /// The return value is always 0 since cols and srow are copies of the values in the matrix.
97  virtual int GetRow(const int row, Array<int> &cols, Vector &srow) const;
98  //! If the matrix is square, it will place 1 on the diagonal (i,i) if row i
99  //! has "almost" zero l1-norm.
100  /**
101  * If entry (i,i) does not belong to the sparsity pattern of A, then a error will occur.
102  */
103  virtual void EliminateZeroRows();
104 
105  /// Matrix-Vector Multiplication y = A*x
106  virtual void Mult(const Vector & x, Vector & y) const;
107  /// Matrix-Vector Multiplication y = y + val*A*x
108  virtual void AddMult(const Vector & x, Vector & y, const double val = 1.) const;
109  /// MatrixTranspose-Vector Multiplication y = A'*x
110  virtual void MultTranspose(const Vector & x, Vector & y) const;
111  /// MatrixTranspose-Vector Multiplication y = y + val*A'*x
112  virtual void AddMultTranspose(const Vector & x, Vector & y,
113  const double val = 1.) const;
114  //@}
115 
116  //! Destructor
117  virtual ~BlockMatrix();
118  //! if owns_blocks the SparseMatrix objects Aij will be deallocated.
120 
121 private:
122  //! Given a global row iglobal finds to which row iloc in block iblock belongs to.
123  inline void findGlobalRow(int iglobal, int & iblock, int & iloc) const;
124  //! Given a global column jglobal finds to which column jloc in block jblock belongs to.
125  inline void findGlobalCol(int jglobal, int & jblock, int & jloc) const;
126 
127  //! Number of row blocks
128  int nRowBlocks;
129  //! Number of columns blocks
130  int nColBlocks;
131  //! row offsets for each block start (length nRowBlocks+1).
132  Array<int> row_offsets;
133  //! column offsets for each block start (length nColBlocks+1).
134  Array<int> col_offsets;
135  //! 2D array that stores each block of the BlockMatrix. Aij(iblock, jblock)
136  //! == NULL if block (iblock, jblock) is all zeros.
138 };
139 
140 //! Transpose a BlockMatrix: result = A'
141 BlockMatrix * Transpose(const BlockMatrix & A);
142 //! Multiply BlockMatrix matrices: result = A*B
143 BlockMatrix * Mult(const BlockMatrix & A, const BlockMatrix & B);
144 
145 inline void BlockMatrix::findGlobalRow(int iglobal, int & iblock,
146  int & iloc) const
147 {
148  if (iglobal > row_offsets[nRowBlocks])
149  {
150  mfem_error("BlockMatrix::findGlobalRow");
151  }
152 
153  for (iblock = 0; iblock < nRowBlocks; ++iblock)
154  if (row_offsets[iblock+1] > iglobal)
155  {
156  break;
157  }
158 
159  iloc = iglobal - row_offsets[iblock];
160 }
161 
162 inline void BlockMatrix::findGlobalCol(int jglobal, int & jblock,
163  int & jloc) const
164 {
165  if (jglobal > col_offsets[nColBlocks])
166  {
167  mfem_error("BlockMatrix::findGlobalCol");
168  }
169 
170  for (jblock = 0; jblock < nColBlocks; ++jblock)
171  if (col_offsets[jblock+1] > jglobal)
172  {
173  break;
174  }
175 
176  jloc = jglobal - col_offsets[jblock];
177 }
178 
179 }
180 
181 #endif /* MFEM_BLOCKMATRIX */
virtual MatrixInverse * Inverse() const
Returns a pointer to (approximation) of the matrix inverse.
Definition: blockmatrix.hpp:84
SparseMatrix * CreateMonolithic() const
Returns a monolithic CSR matrix that represents this operator.
int owns_blocks
if owns_blocks the SparseMatrix objects Aij will be deallocated.
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Definition: table.cpp:468
Abstract data type for sparse matrices.
Definition: matrix.hpp:69
Abstract data type for matrix inverse.
Definition: matrix.hpp:58
BlockMatrix(const Array< int > &offsets)
Constructor for square block matrices.
Definition: blockmatrix.cpp:22
virtual void AddMultTranspose(const Vector &x, Vector &y, const double val=1.) const
MatrixTranspose-Vector Multiplication y = y + val*A&#39;*x.
virtual int GetRow(const int row, Array< int > &cols, Vector &srow) const
void SetBlock(int i, int j, SparseMatrix *mat)
Set A(i,j) = mat.
Definition: blockmatrix.cpp:60
virtual double & Elem(int i, int j)
Returns reference to a_{ij}.
virtual void EliminateZeroRows()
Data type sparse matrix.
Definition: sparsemat.hpp:38
int IsZeroBlock(int i, int j) const
Check if block (i,j) is a zero block.
Definition: blockmatrix.hpp:52
int NumRowBlocks() const
Return the number of row blocks.
Definition: blockmatrix.hpp:43
Array< int > & ColOffsets()
Return the columns offsets for block starts.
Definition: blockmatrix.hpp:56
void Transpose(const Table &A, Table &At, int _ncols_A)
Transpose a Table.
Definition: table.cpp:408
void PrintMatlab(std::ostream &os=mfem::out) const
Export the monolithic matrix to file.
Array< int > & RowOffsets()
Return the row offsets for block starts.
Definition: blockmatrix.hpp:54
void EliminateRowCol(Array< int > &ess_bc_dofs, Vector &sol, Vector &rhs)
Symmetric elimination of the marked degree of freedom.
virtual int NumNonZeroElems() const
Returns the total number of non zeros in the matrix.
virtual ~BlockMatrix()
Destructor.
Definition: blockmatrix.cpp:50
virtual void MultTranspose(const Vector &x, Vector &y) const
MatrixTranspose-Vector Multiplication y = A&#39;*x.
virtual void Mult(const Vector &x, Vector &y) const
Matrix-Vector Multiplication y = A*x.
void mfem_error(const char *msg)
Definition: error.cpp:107
int NumColBlocks() const
Return the number of column blocks.
Definition: blockmatrix.hpp:45
virtual void AddMult(const Vector &x, Vector &y, const double val=1.) const
Matrix-Vector Multiplication y = y + val*A*x.
const Array< int > & ColOffsets() const
Return the row offsets for block starts (const version)
Definition: blockmatrix.hpp:60
SparseMatrix & GetBlock(int i, int j)
Return a reference to block (i,j). Reference may be invalid if Aij(i,j) == NULL.
Definition: blockmatrix.cpp:81
Vector data type.
Definition: vector.hpp:41
const Array< int > & RowOffsets() const
Return the row offsets for block starts (const version)
Definition: blockmatrix.hpp:58
int RowSize(const int i) const
Return the number of non zeros in row i.
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition: globals.hpp:64