Reference documentation for deal.II version GIT c415589cf0 2022-08-14 18:50:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
diagonal_matrix.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2016 - 2022 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_diagonal_matrix_h
17 #define dealii_diagonal_matrix_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #include <deal.II/lac/vector.h>
24 
26 
47 template <typename VectorType = Vector<double>>
49 {
50 public:
51  using value_type = typename VectorType::value_type;
52  using size_type = typename VectorType::size_type;
53 
58  DiagonalMatrix() = default;
59 
65  explicit DiagonalMatrix(const VectorType &vec);
66 
71  void
72  reinit(const VectorType &vec);
73 
80  void
82 
87  VectorType &
89 
93  void
94  clear();
95 
99  const VectorType &
100  get_vector() const;
101 
106  size_type
107  m() const;
108 
113  size_type
114  n() const;
115 
125  value_type
126  operator()(const size_type i, const size_type j) const;
127 
137  value_type &
138  operator()(const size_type i, const size_type j);
139 
151  template <typename number2>
152  void
153  add(const size_type row,
154  const size_type n_cols,
155  const size_type *col_indices,
156  const number2 * values,
157  const bool elide_zero_values = true,
158  const bool col_indices_are_sorted = false);
159 
166  void
167  add(const size_type i, const size_type j, const value_type value);
168 
172  void
173  vmult(VectorType &dst, const VectorType &src) const;
174 
180  void
181  Tvmult(VectorType &dst, const VectorType &src) const;
182 
188  void
189  vmult_add(VectorType &dst, const VectorType &src) const;
190 
196  void
197  Tvmult_add(VectorType &dst, const VectorType &src) const;
198 
204  value_type
205  apply(const unsigned int index, const value_type src) const;
206 
216  void
217  apply_to_subrange(const unsigned int begin_range,
218  const unsigned int end_range,
219  const value_type * src_pointer_to_current_range,
220  value_type * dst_pointer_to_current_range) const;
221 
229  void
230  initialize_dof_vector(VectorType &dst) const;
231 
235  std::size_t
237 
238 private:
242  VectorType diagonal;
243 };
244 
245 /* ---------------------------------- Inline functions ------------------- */
246 
247 #ifndef DOXYGEN
248 
249 template <typename VectorType>
250 DiagonalMatrix<VectorType>::DiagonalMatrix(const VectorType &vec)
251  : diagonal(vec)
252 {}
253 
254 
255 
256 template <typename VectorType>
257 void
259 {
260  diagonal.reinit(0);
261 }
262 
263 
264 
265 template <typename VectorType>
266 std::size_t
268 {
269  return diagonal.memory_consumption();
270 }
271 
272 
273 
274 template <typename VectorType>
275 void
276 DiagonalMatrix<VectorType>::reinit(const VectorType &vec)
277 {
278  diagonal = vec;
279 }
280 
281 
282 
283 template <typename VectorType>
284 void
286 {
287  dst.reinit(diagonal);
288 }
289 
290 
291 
292 template <typename VectorType>
293 void
295 {
296  diagonal.compress(operation);
297 }
298 
299 
300 
301 template <typename VectorType>
302 VectorType &
304 {
305  return diagonal;
306 }
307 
308 
309 
310 template <typename VectorType>
311 const VectorType &
313 {
314  return diagonal;
315 }
316 
317 
318 
319 template <typename VectorType>
320 typename VectorType::size_type
322 {
323  return diagonal.size();
324 }
325 
326 
327 
328 template <typename VectorType>
329 typename VectorType::size_type
331 {
332  return diagonal.size();
333 }
334 
335 
336 
337 template <typename VectorType>
338 typename VectorType::value_type
340  const size_type j) const
341 {
342  Assert(i == j, ExcIndexRange(j, i, i + 1));
343  (void)j;
344  return diagonal(i);
345 }
346 
347 
348 
349 template <typename VectorType>
350 typename VectorType::value_type &
352 {
353  Assert(i == j, ExcIndexRange(j, i, i + 1));
354  (void)j;
355  return diagonal(i);
356 }
357 
358 
359 
360 template <typename VectorType>
361 template <typename number2>
362 void
364  const size_type n_cols,
365  const size_type *col_indices,
366  const number2 * values,
367  const bool,
368  const bool)
369 {
370  for (size_type i = 0; i < n_cols; ++i)
371  if (col_indices[i] == row)
372  diagonal(row) += values[i];
373 }
374 
375 
376 
377 template <typename VectorType>
378 void
380  const size_type j,
381  const value_type value)
382 {
383  if (i == j)
384  diagonal(i) += value;
385 }
386 
387 
388 
389 template <typename VectorType>
390 void
391 DiagonalMatrix<VectorType>::vmult(VectorType &dst, const VectorType &src) const
392 {
393  dst = src;
394  dst.scale(diagonal);
395 }
396 
397 
398 
399 template <typename VectorType>
400 void
401 DiagonalMatrix<VectorType>::Tvmult(VectorType &dst, const VectorType &src) const
402 {
403  vmult(dst, src);
404 }
405 
406 
407 
408 template <typename VectorType>
409 void
411  const VectorType &src) const
412 {
413  VectorType tmp(src);
414  tmp.scale(diagonal);
415  dst += tmp;
416 }
417 
418 
419 
420 template <typename VectorType>
421 void
423  const VectorType &src) const
424 {
425  vmult_add(dst, src);
426 }
427 
428 
429 
430 template <typename VectorType>
431 typename VectorType::value_type
432 DiagonalMatrix<VectorType>::apply(const unsigned int index,
433  const value_type src) const
434 {
435  AssertIndexRange(index, diagonal.locally_owned_elements().n_elements());
436  return diagonal.local_element(index) * src;
437 }
438 
439 
440 
441 template <typename VectorType>
442 void
444  const unsigned int begin_range,
445  const unsigned int end_range,
446  const value_type * src_pointer_to_current_range,
447  value_type * dst_pointer_to_current_range) const
448 {
449  AssertIndexRange(begin_range,
450  diagonal.locally_owned_elements().n_elements() + 1);
451  AssertIndexRange(end_range,
452  diagonal.locally_owned_elements().n_elements() + 1);
453 
454  const value_type * diagonal_entry = diagonal.begin() + begin_range;
455  const unsigned int length = end_range - begin_range;
456 
458  for (unsigned int i = 0; i < length; ++i)
459  dst_pointer_to_current_range[i] =
460  diagonal_entry[i] * src_pointer_to_current_range[i];
461 }
462 
463 
464 #endif
465 
467 
468 #endif
void initialize_dof_vector(VectorType &dst) const
size_type m() const
void Tvmult_add(VectorType &dst, const VectorType &src) const
value_type apply(const unsigned int index, const value_type src) const
VectorType & get_vector()
void apply_to_subrange(const unsigned int begin_range, const unsigned int end_range, const value_type *src_pointer_to_current_range, value_type *dst_pointer_to_current_range) const
VectorType diagonal
std::size_t memory_consumption() const
void add(const size_type i, const size_type j, const value_type value)
DiagonalMatrix(const VectorType &vec)
value_type operator()(const size_type i, const size_type j) const
value_type & operator()(const size_type i, const size_type j)
typename VectorType::size_type size_type
void compress(VectorOperation::values operation)
void vmult(VectorType &dst, const VectorType &src) const
size_type n() const
void reinit(const VectorType &vec)
void add(const size_type row, const size_type n_cols, const size_type *col_indices, const number2 *values, const bool elide_zero_values=true, const bool col_indices_are_sorted=false)
typename VectorType::value_type value_type
void vmult_add(VectorType &dst, const VectorType &src) const
DiagonalMatrix()=default
void Tvmult(VectorType &dst, const VectorType &src) const
const VectorType & get_vector() const
#define DEAL_II_OPENMP_SIMD_PRAGMA
Definition: config.h:142
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:442
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:443
#define Assert(cond, exc)
Definition: exceptions.h:1473
#define AssertIndexRange(index, range)
Definition: exceptions.h:1732
static ::ExceptionBase & ExcIndexRange(std::size_t arg1, std::size_t arg2, std::size_t arg3)
@ diagonal
Matrix is diagonal.
types::global_dof_index size_type
Definition: cuda_kernels.h:45