Reference documentation for deal.II version 9.4.1
\(\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\}}\)
Loading...
Searching...
No Matches
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
47template <typename VectorType = Vector<double>>
49{
50public:
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
95
99 const VectorType &
100 get_vector() const;
101
107 m() const;
108
114 n() const;
115
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
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
238private:
242 VectorType diagonal;
243};
244
245/* ---------------------------------- Inline functions ------------------- */
246
247#ifndef DOXYGEN
248
249template <typename VectorType>
251 : diagonal(vec)
252{}
253
254
255
256template <typename VectorType>
257void
259{
260 diagonal.reinit(0);
261}
262
263
264
265template <typename VectorType>
266std::size_t
268{
269 return diagonal.memory_consumption();
270}
271
272
273
274template <typename VectorType>
275void
276DiagonalMatrix<VectorType>::reinit(const VectorType &vec)
277{
278 diagonal = vec;
279}
280
281
282
283template <typename VectorType>
284void
286{
287 dst.reinit(diagonal);
288}
289
290
291
292template <typename VectorType>
293void
295{
296 diagonal.compress(operation);
297}
298
299
300
301template <typename VectorType>
302VectorType &
304{
305 return diagonal;
306}
307
308
309
310template <typename VectorType>
311const VectorType &
313{
314 return diagonal;
315}
316
317
318
319template <typename VectorType>
320typename VectorType::size_type
322{
323 return diagonal.size();
324}
325
326
327
328template <typename VectorType>
329typename VectorType::size_type
331{
332 return diagonal.size();
333}
334
335
336
337template <typename VectorType>
338typename 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
349template <typename VectorType>
350typename VectorType::value_type &
351DiagonalMatrix<VectorType>::operator()(const size_type i, const size_type j)
352{
353 Assert(i == j, ExcIndexRange(j, i, i + 1));
354 (void)j;
355 return diagonal(i);
356}
357
358
359
360template <typename VectorType>
361template <typename number2>
362void
363DiagonalMatrix<VectorType>::add(const size_type row,
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
377template <typename VectorType>
378void
379DiagonalMatrix<VectorType>::add(const size_type i,
380 const size_type j,
381 const value_type value)
382{
383 if (i == j)
384 diagonal(i) += value;
385}
386
387
388
389template <typename VectorType>
390void
391DiagonalMatrix<VectorType>::vmult(VectorType &dst, const VectorType &src) const
392{
393 dst = src;
394 dst.scale(diagonal);
395}
396
397
398
399template <typename VectorType>
400void
401DiagonalMatrix<VectorType>::Tvmult(VectorType &dst, const VectorType &src) const
402{
403 vmult(dst, src);
404}
405
406
407
408template <typename VectorType>
409void
411 const VectorType &src) const
412{
413 VectorType tmp(src);
414 tmp.scale(diagonal);
415 dst += tmp;
416}
417
418
419
420template <typename VectorType>
421void
423 const VectorType &src) const
424{
425 vmult_add(dst, src);
426}
427
428
429
430template <typename VectorType>
431typename VectorType::value_type
432DiagonalMatrix<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
441template <typename VectorType>
442void
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
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
value_type & operator()(const size_type i, const size_type j)
VectorType & get_vector()
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
typename VectorType::size_type size_type
void compress(VectorOperation::values operation)
const VectorType & get_vector() const
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
#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.