libMesh
diagonal_matrix.C
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 #include "libmesh/diagonal_matrix.h"
19 #include "libmesh/numeric_vector.h"
20 #include "libmesh/dense_matrix.h"
21 #include "libmesh/dof_map.h"
22 #include "libmesh/libmesh_common.h"
23 
24 namespace libMesh
25 {
26 template <typename T>
27 DiagonalMatrix<T>::DiagonalMatrix(const Parallel::Communicator & comm_in) : SparseMatrix<T>(comm_in)
28 {
30 }
31 
32 template <typename T>
35 {
36  *_diagonal = vec;
37  return *this;
38 }
39 
40 template <typename T>
43 {
44  // Don't get confused by the &&: vec is an lvalue reference; the && just
45  // indicates that we are receiving an object that is safe to move from. Note
46  // that we are not going to use std::move here because we do not have
47  // (virtual) move assignment operations defined for NumericVector sub-classes
48  _diagonal->swap(vec);
49  return *this;
50 }
51 
52 template <typename T>
53 void
55  const numeric_index_type /*n*/,
56  const numeric_index_type m_l,
57  const numeric_index_type /*n_l*/,
58  const numeric_index_type /*nnz*/,
59  const numeric_index_type /*noz*/,
60  const numeric_index_type /*blocksize*/)
61 {
62  _diagonal->init(m, m_l);
63 }
64 
65 template <typename T>
66 void
68 {
69  libmesh_assert(this->_dof_map);
70 
71  _diagonal->init(this->_dof_map->n_dofs());
72 }
73 
74 template <typename T>
75 void
76 DiagonalMatrix<T>::init(const NumericVector<T> & other, const bool fast)
77 {
78  _diagonal->init(other, fast);
79 }
80 
81 template <typename T>
82 void
83 DiagonalMatrix<T>::init(const DiagonalMatrix<T> & other, const bool fast)
84 {
85  init(other.diagonal(), fast);
86 }
87 
88 template <typename T>
89 void
91 {
92  _diagonal->clear();
93 }
94 
95 template <typename T>
96 void
98 {
99  _diagonal->zero();
100 }
101 
102 template <typename T>
103 void
105 {
106  _diagonal->close();
107 }
108 
109 template <typename T>
112 {
113  return _diagonal->size();
114 }
115 
116 template <typename T>
119 {
120  return _diagonal->size();
121 }
122 
123 template <typename T>
126 {
127  return _diagonal->first_local_index();
128 }
129 
130 template <typename T>
133 {
134  return _diagonal->last_local_index();
135 }
136 
137 template <typename T>
138 void
140 {
141  if (i == j)
142  _diagonal->set(i, value);
143 }
144 
145 template <typename T>
146 void
148 {
149  if (i == j)
150  _diagonal->add(i, value);
151 }
152 
153 template <typename T>
154 void
156  const std::vector<numeric_index_type> & rows,
157  const std::vector<numeric_index_type> & cols)
158 {
159  auto m = dm.m();
160  auto n = dm.n();
161 
162  for (decltype(m) i = 0; i < m; ++i)
163  for (decltype(n) j = 0; j < n; ++j)
164  {
165  auto global_i = rows[i];
166  auto global_j = cols[j];
167  if (global_i == global_j)
168  _diagonal->add(global_i, dm(i, j));
169  }
170 }
171 
172 template <typename T>
173 void
175  const std::vector<numeric_index_type> & dof_indices)
176 {
177  _diagonal->add_vector(dm.diagonal(), dof_indices);
178 }
179 
180 template <typename T>
181 void
183 {
184  auto x_diagonal = _diagonal->zero_clone();
185  X.get_diagonal(*x_diagonal);
186  _diagonal->add(a, *x_diagonal);
187 }
188 
189 template <typename T>
190 T
192 {
193  if (i == j)
194  return (*_diagonal)(i);
195  else
196  return 0;
197 }
198 
199 template <typename T>
200 Real
202 {
203  return _diagonal->l1_norm();
204 }
205 
206 template <typename T>
207 Real
209 {
210  return _diagonal->linfty_norm();
211 }
212 
213 template <typename T>
214 bool
216 {
217  return _diagonal->closed();
218 }
219 
220 template <typename T>
221 void
222 DiagonalMatrix<T>::print_personal(std::ostream & os) const
223 {
224  _diagonal->print(os);
225 }
226 
227 template <typename T>
228 void
230 {
231  dest = *_diagonal;
232 }
233 
234 template <typename T>
235 void
237 {
238  auto diagonal_dest = dynamic_cast<DiagonalMatrix<T> *>(&dest);
239  if (diagonal_dest)
240  *diagonal_dest = *_diagonal;
241  else
242  libmesh_error_msg("DenseMatrix<T>::get_transpose currently only accepts another DenseMatrix<T> "
243  "as its argument");
244 }
245 
246 template <typename T>
247 void
248 DiagonalMatrix<T>::zero_rows(std::vector<numeric_index_type> & rows, T val/*=0*/)
249 {
250  for (auto row : rows)
251  _diagonal->set(row, val);
252 }
253 
254 template <typename T>
255 const NumericVector<T> &
257 {
258  return *_diagonal;
259 }
260 
261 template class DiagonalMatrix<Number>;
262 }
libMesh::DiagonalMatrix::DiagonalMatrix
DiagonalMatrix(const Parallel::Communicator &comm)
Constructor; initializes the matrix to be empty, without any structure, i.e.
Definition: diagonal_matrix.C:27
libMesh::DenseMatrixBase::diagonal
DenseVector< T > diagonal() const
Return the matrix diagonal.
Definition: dense_matrix_base_impl.h:37
libMesh::DiagonalMatrix::zero_rows
void zero_rows(std::vector< numeric_index_type > &rows, T val=0) override
Sets all row entries to 0 then puts diag_value in the diagonal entry.
Definition: diagonal_matrix.C:248
libMesh::DiagonalMatrix::row_stop
numeric_index_type row_stop() const override
Definition: diagonal_matrix.C:132
libMesh::DiagonalMatrix::operator=
DiagonalMatrix & operator=(DiagonalMatrix &&)=default
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::DiagonalMatrix::diagonal
const NumericVector< T > & diagonal() const
Definition: diagonal_matrix.C:256
libMesh::SparseMatrix::get_diagonal
virtual void get_diagonal(NumericVector< T > &dest) const =0
Copies the diagonal part of the matrix into dest.
libMesh::DiagonalMatrix::m
numeric_index_type m() const override
Definition: diagonal_matrix.C:111
libMesh::DenseMatrix
Defines a dense matrix for use in Finite Element-type computations.
Definition: dof_map.h:73
libMesh::DiagonalMatrix::print_personal
void print_personal(std::ostream &os=libMesh::out) const override
Print the contents of the matrix to the screen in a package-personalized style, if available.
Definition: diagonal_matrix.C:222
libMesh::DenseMatrixBase::n
unsigned int n() const
Definition: dense_matrix_base.h:109
libMesh::SparseMatrix
Generic sparse matrix.
Definition: vector_fe_ex5.C:45
libMesh::DiagonalMatrix
Diagonal matrix class whose underlying storage is a vector.
Definition: diagonal_matrix.h:42
libMesh::NumericVector::build
static std::unique_ptr< NumericVector< T > > build(const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package())
Builds a NumericVector on the processors in communicator comm using the linear solver package specifi...
Definition: numeric_vector.C:49
libMesh::DiagonalMatrix::set
void set(const numeric_index_type i, const numeric_index_type j, const T value) override
Set the element (i,j) to value.
Definition: diagonal_matrix.C:139
libMesh::NumericVector
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
Definition: vector_fe_ex5.C:43
libMesh::DenseMatrixBase::m
unsigned int m() const
Definition: dense_matrix_base.h:104
libMesh::TriangleWrapper::init
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::DiagonalMatrix::closed
bool closed() const override
Definition: diagonal_matrix.C:215
libMesh::DiagonalMatrix::get_transpose
void get_transpose(SparseMatrix< T > &dest) const override
Copies the transpose of the matrix into dest, which may be *this.
Definition: diagonal_matrix.C:236
libMesh::DiagonalMatrix::get_diagonal
void get_diagonal(NumericVector< T > &dest) const override
Copies the diagonal part of the matrix into dest.
Definition: diagonal_matrix.C:229
libMesh::DiagonalMatrix::l1_norm
Real l1_norm() const override
Definition: diagonal_matrix.C:201
libMesh::DiagonalMatrix::linfty_norm
Real linfty_norm() const override
Definition: diagonal_matrix.C:208
libMesh::DiagonalMatrix::n
numeric_index_type n() const override
Definition: diagonal_matrix.C:118
libMesh::numeric_index_type
dof_id_type numeric_index_type
Definition: id_types.h:99
libMesh::DiagonalMatrix::init
void init() override
Initialize this matrix using the sparsity structure computed by dof_map.
Definition: diagonal_matrix.C:67
libMesh::DiagonalMatrix::_diagonal
std::unique_ptr< NumericVector< T > > _diagonal
Underlying diagonal matrix storage.
Definition: diagonal_matrix.h:145
libMesh::DiagonalMatrix::zero
void zero() override
Set all entries to 0.
Definition: diagonal_matrix.C:97
libMesh::DiagonalMatrix::close
void close() override
Calls the SparseMatrix's internal assembly routines, ensuring that the values are consistent across p...
Definition: diagonal_matrix.C:104
value
static const bool value
Definition: xdr_io.C:56
libMesh::DiagonalMatrix::add
void add(const numeric_index_type i, const numeric_index_type j, const T value) override
Add value to the element (i,j).
Definition: diagonal_matrix.C:147
libMesh::DiagonalMatrix::operator()
T operator()(const numeric_index_type i, const numeric_index_type j) const override
Definition: diagonal_matrix.C:191
libMesh::DiagonalMatrix::add_matrix
void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) override
Add the full matrix dm to the SparseMatrix.
Definition: diagonal_matrix.C:155
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::DiagonalMatrix::row_start
numeric_index_type row_start() const override
Definition: diagonal_matrix.C:125
libMesh::DiagonalMatrix::clear
void clear() override
Restores the SparseMatrix<T> to a pristine state.
Definition: diagonal_matrix.C:90