libMesh
dense_submatrix.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_DENSE_SUBMATRIX_H
21 #define LIBMESH_DENSE_SUBMATRIX_H
22 
23 // Local Includes
24 #include "libmesh/libmesh_common.h"
25 #include "libmesh/dense_matrix.h"
26 #include "libmesh/dense_subvector.h"
27 #include "libmesh/int_range.h"
28 
29 // C++ includes
30 
31 namespace libMesh
32 {
33 
44 template<typename T>
46 {
47 public:
48 
55  DenseSubMatrix(DenseMatrix<T> & new_parent,
56  const unsigned int ioff=0,
57  const unsigned int joff=0,
58  const unsigned int m=0,
59  const unsigned int n=0);
60 
65  DenseSubMatrix (DenseSubMatrix &&) = default;
66  DenseSubMatrix (const DenseSubMatrix &) = default;
67  DenseSubMatrix & operator= (const DenseSubMatrix &) = default;
69  virtual ~DenseSubMatrix() = default;
70 
75 
76  virtual void zero() override;
77 
81  T operator() (const unsigned int i,
82  const unsigned int j) const;
83 
87  T & operator() (const unsigned int i,
88  const unsigned int j);
89 
90  virtual T el(const unsigned int i,
91  const unsigned int j) const override
92  { return (*this)(i,j); }
93 
94  virtual T & el(const unsigned int i,
95  const unsigned int j) override
96  { return (*this)(i,j); }
97 
98  virtual void left_multiply (const DenseMatrixBase<T> & M2) override;
99 
100  virtual void right_multiply (const DenseMatrixBase<T> & M3) override;
101 
105  void reposition(const unsigned int ioff,
106  const unsigned int joff,
107  const unsigned int new_m,
108  const unsigned int new_n);
109 
113  unsigned int i_off() const { return _i_off; }
114 
118  unsigned int j_off() const { return _j_off; }
119 
126  void condense(const unsigned int i,
127  const unsigned int j,
128  const T val,
129  DenseSubVector<T> & rhs)
130  {
131  this->parent().condense(this->i_off()+i,
132  this->j_off()+j,
133  val, rhs.parent());
134  }
135 
136 private:
137 
142 
146  unsigned int _i_off;
147 
151  unsigned int _j_off;
152 };
153 
154 
155 // --------------------------------------------------
156 // Constructor
157 template<typename T>
158 inline
160  const unsigned int ioff,
161  const unsigned int joff,
162  const unsigned int new_m,
163  const unsigned int new_n) :
164  DenseMatrixBase<T>(new_m,new_n),
165  _parent_matrix(new_parent)
166 {
167  this->reposition (ioff, joff, new_m, new_n);
168 }
169 
170 
171 template<typename T>
172 inline
173 void DenseSubMatrix<T>::reposition(const unsigned int ioff,
174  const unsigned int joff,
175  const unsigned int new_m,
176  const unsigned int new_n)
177 {
178  _i_off = ioff;
179  _j_off = joff;
180  this->_m = new_m;
181  this->_n = new_n;
182 
183  // Make sure we still fit in the parent matrix.
184  libmesh_assert_less_equal ((this->i_off() + this->m()), _parent_matrix.m());
185  libmesh_assert_less_equal ((this->j_off() + this->n()), _parent_matrix.n());
186 }
187 
188 
189 
190 template<typename T>
191 inline
193 {
194  for (auto i : IntRange<unsigned int>(0, this->m()))
195  for (auto j : IntRange<unsigned int>(0, this->n()))
196  _parent_matrix(i + this->i_off(),
197  j + this->j_off()) = 0.;
198 }
199 
200 
201 
202 template<typename T>
203 inline
204 T DenseSubMatrix<T>::operator () (const unsigned int i,
205  const unsigned int j) const
206 {
207  libmesh_assert_less (i, this->m());
208  libmesh_assert_less (j, this->n());
209  libmesh_assert_less (i + this->i_off(), _parent_matrix.m());
210  libmesh_assert_less (j + this->j_off(), _parent_matrix.n());
211 
212  return _parent_matrix (i + this->i_off(),
213  j + this->j_off());
214 }
215 
216 
217 template<typename T>
218 inline
219 T & DenseSubMatrix<T>::operator () (const unsigned int i,
220  const unsigned int j)
221 {
222  libmesh_assert_less (i, this->m());
223  libmesh_assert_less (j, this->n());
224  libmesh_assert_less (i + this->i_off(), _parent_matrix.m());
225  libmesh_assert_less (j + this->j_off(), _parent_matrix.n());
226 
227  return _parent_matrix (i + this->i_off(),
228  j + this->j_off());
229 }
230 
231 
232 } // namespace libMesh
233 
234 
235 #endif // LIBMESH_DENSE_SUBMATRIX_H
libMesh::DenseSubMatrix::_parent_matrix
DenseMatrix< T > & _parent_matrix
The parent matrix that contains this submatrix.
Definition: dense_submatrix.h:141
libMesh::DenseSubMatrix
Defines a dense submatrix for use in Finite Element-type computations.
Definition: dense_submatrix.h:45
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::DenseSubMatrix::el
virtual T el(const unsigned int i, const unsigned int j) const override
Definition: dense_submatrix.h:90
libMesh::DenseSubMatrix::_i_off
unsigned int _i_off
The row offset into the parent matrix.
Definition: dense_submatrix.h:146
libMesh::DenseMatrix
Defines a dense matrix for use in Finite Element-type computations.
Definition: dof_map.h:73
libMesh::DenseMatrixBase::n
unsigned int n() const
Definition: dense_matrix_base.h:109
libMesh::DenseSubMatrix::zero
virtual void zero() override
Set every element in the matrix to 0.
Definition: dense_submatrix.h:192
libMesh::DenseSubMatrix::i_off
unsigned int i_off() const
Definition: dense_submatrix.h:113
libMesh::DenseMatrixBase::m
unsigned int m() const
Definition: dense_matrix_base.h:104
libMesh::DenseSubMatrix::j_off
unsigned int j_off() const
Definition: dense_submatrix.h:118
libMesh::IntRange
The IntRange templated class is intended to make it easy to loop over integers which are indices of a...
Definition: int_range.h:53
libMesh::DenseSubMatrix::operator=
DenseSubMatrix & operator=(const DenseSubMatrix &)=default
libMesh::DenseMatrixBase
Defines an abstract dense matrix base class for use in Finite Element-type computations.
Definition: dense_matrix_base.h:46
libMesh::DenseSubMatrix::right_multiply
virtual void right_multiply(const DenseMatrixBase< T > &M3) override
Performs the operation: (*this) <- (*this) * M3.
Definition: dense_submatrix.C:49
libMesh::DenseSubMatrix::DenseSubMatrix
DenseSubMatrix(DenseMatrix< T > &new_parent, const unsigned int ioff=0, const unsigned int joff=0, const unsigned int m=0, const unsigned int n=0)
Constructor.
Definition: dense_submatrix.h:159
libMesh::DenseSubMatrix::el
virtual T & el(const unsigned int i, const unsigned int j) override
Definition: dense_submatrix.h:94
libMesh::DenseSubVector
Defines a dense subvector for use in finite element computations.
Definition: dense_subvector.h:43
libMesh::DenseSubMatrix::~DenseSubMatrix
virtual ~DenseSubMatrix()=default
libMesh::DenseSubVector::parent
DenseVector< T > & parent()
Definition: dense_subvector.h:69
libMesh::DenseSubMatrix::_j_off
unsigned int _j_off
The column offset into the parent matrix.
Definition: dense_submatrix.h:151
libMesh::DenseSubMatrix::operator()
T operator()(const unsigned int i, const unsigned int j) const
Definition: dense_submatrix.h:204
libMesh::DenseSubMatrix::left_multiply
virtual void left_multiply(const DenseMatrixBase< T > &M2) override
Performs the operation: (*this) <- M2 * (*this)
Definition: dense_submatrix.C:31
libMesh::DenseSubMatrix::parent
DenseMatrix< T > & parent()
Definition: dense_submatrix.h:74
libMesh::DenseSubMatrix::condense
void condense(const unsigned int i, const unsigned int j, const T val, DenseSubVector< T > &rhs)
Condense-out the (i,j) entry of the matrix, forcing it to take on the value val.
Definition: dense_submatrix.h:126
libMesh::DenseSubMatrix::reposition
void reposition(const unsigned int ioff, const unsigned int joff, const unsigned int new_m, const unsigned int new_n)
Changes the location of the submatrix in the parent matrix.
Definition: dense_submatrix.h:173