deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+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\}}\)
Loading...
Searching...
No Matches
diagonal_matrix.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2016 - 2025 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_diagonal_matrix_h
14#define dealii_diagonal_matrix_h
15
16
17#include <deal.II/base/config.h>
18
19#include <deal.II/lac/vector.h>
21
23
24// forward declarations
25#ifndef DOXYGEN
26template <typename number>
27class Vector;
28namespace LinearAlgebra
29{
30 namespace distributed
31 {
32 template <typename, typename>
33 class Vector;
34 } // namespace distributed
35} // namespace LinearAlgebra
36#endif
37
58template <typename VectorType = Vector<double>>
60{
61public:
62 using value_type = typename VectorType::value_type;
63 using size_type = typename VectorType::size_type;
64
69 DiagonalMatrix() = default;
70
76 explicit DiagonalMatrix(const VectorType &vec);
77
82 void
83 reinit(const VectorType &vec);
84
91 void
93
98 VectorType &
100
104 void
106
110 const VectorType &
111 get_vector() const;
112
118 m() const;
119
125 n() const;
126
137 operator()(const size_type i, const size_type j) const;
138
148 value_type &
149 operator()(const size_type i, const size_type j);
150
162 template <typename number2>
163 void
164 add(const size_type row,
165 const size_type n_cols,
166 const size_type *col_indices,
167 const number2 *values,
168 const bool elide_zero_values = true,
169 const bool col_indices_are_sorted = false);
170
177 void
178 add(const size_type i, const size_type j, const value_type value);
179
183 void
184 vmult(VectorType &dst, const VectorType &src) const;
185
191 void
192 Tvmult(VectorType &dst, const VectorType &src) const;
193
199 void
200 vmult_add(VectorType &dst, const VectorType &src) const;
201
207 void
208 Tvmult_add(VectorType &dst, const VectorType &src) const;
209
216 apply(const unsigned int index, const value_type src) const;
217
227 void
228 apply_to_subrange(const unsigned int begin_range,
229 const unsigned int end_range,
230 const value_type *src_pointer_to_current_range,
231 value_type *dst_pointer_to_current_range) const;
232
240 void
241 initialize_dof_vector(VectorType &dst) const;
242
246 std::size_t
248
249private:
253 VectorType diagonal;
254};
255
256/* ---------------------------------- Inline functions ------------------- */
257
258#ifndef DOXYGEN
259
260template <typename VectorType>
262 : diagonal(vec)
263{}
264
265
266
267template <typename VectorType>
268void
270{
271 diagonal.reinit(0);
272}
273
274
275
276template <typename VectorType>
277std::size_t
279{
280 return diagonal.memory_consumption();
281}
282
283
284
285template <typename VectorType>
286void
287DiagonalMatrix<VectorType>::reinit(const VectorType &vec)
288{
289 diagonal = vec;
290}
291
292
293
294template <typename VectorType>
295void
297{
298 dst.reinit(diagonal);
299}
300
301
302
303template <typename VectorType>
304void
306{
307 diagonal.compress(operation);
308}
309
310
311
312template <typename VectorType>
315{
316 return diagonal;
317}
318
319
320
321template <typename VectorType>
322const VectorType &
324{
325 return diagonal;
326}
327
328
329
330template <typename VectorType>
331typename VectorType::size_type
333{
334 return diagonal.size();
335}
336
337
338
339template <typename VectorType>
340typename VectorType::size_type
342{
343 return diagonal.size();
344}
345
346
347
348template <typename VectorType>
349typename VectorType::value_type
351 const size_type j) const
352{
353 Assert(i == j, ExcIndexRange(j, i, i + 1));
354 return diagonal(i);
355}
356
357
358
359template <typename VectorType>
360typename VectorType::value_type &
361DiagonalMatrix<VectorType>::operator()(const size_type i, const size_type j)
362{
363 Assert(i == j, ExcIndexRange(j, i, i + 1));
364 return diagonal(i);
365}
366
367
368
369template <typename VectorType>
370template <typename number2>
371void
372DiagonalMatrix<VectorType>::add(const size_type row,
373 const size_type n_cols,
374 const size_type *col_indices,
375 const number2 *values,
376 const bool,
377 const bool)
378{
379 for (size_type i = 0; i < n_cols; ++i)
380 if (col_indices[i] == row)
381 diagonal(row) += values[i];
382}
383
384
385
386template <typename VectorType>
387void
388DiagonalMatrix<VectorType>::add(const size_type i,
389 const size_type j,
390 const value_type value)
391{
392 if (i == j)
393 diagonal(i) += value;
394}
395
396
397
398namespace internal
399{
400 namespace DiagonalMatrix
401 {
402 template <typename VectorType>
403 void
404 assign_and_scale(VectorType &dst,
405 const VectorType &src,
406 const VectorType &diagonal)
407 {
408 dst = src;
409 dst.scale(diagonal);
410 }
411
412 template <typename Number>
413 void
414 assign_and_scale(
418 &diagonal)
419 {
420 auto *const dst_ptr = dst.begin();
421 const auto *const src_ptr = src.begin();
422 const auto *const diagonal_ptr = diagonal.begin();
423
425 for (unsigned int i = 0; i < src.locally_owned_size(); ++i)
426 dst_ptr[i] = src_ptr[i] * diagonal_ptr[i];
427 }
428 } // namespace DiagonalMatrix
429} // namespace internal
430
431
432
433template <typename VectorType>
434void
435DiagonalMatrix<VectorType>::vmult(VectorType &dst, const VectorType &src) const
436{
437 internal::DiagonalMatrix::assign_and_scale(dst, src, diagonal);
438}
439
440
441
442template <typename VectorType>
443void
444DiagonalMatrix<VectorType>::Tvmult(VectorType &dst, const VectorType &src) const
445{
446 vmult(dst, src);
447}
448
449
450
451template <typename VectorType>
452void
454 const VectorType &src) const
455{
456 VectorType tmp(src);
457 tmp.scale(diagonal);
458 dst += tmp;
459}
460
461
462
463template <typename VectorType>
464void
466 const VectorType &src) const
467{
468 vmult_add(dst, src);
469}
470
471
472
473template <typename VectorType>
474typename VectorType::value_type
475DiagonalMatrix<VectorType>::apply(const unsigned int index,
476 const value_type src) const
477{
478 AssertIndexRange(index, diagonal.locally_owned_elements().n_elements());
479 return diagonal.local_element(index) * src;
480}
481
482
483
484template <typename VectorType>
485void
487 const unsigned int begin_range,
488 const unsigned int end_range,
489 const value_type *src_pointer_to_current_range,
490 value_type *dst_pointer_to_current_range) const
491{
492 AssertIndexRange(begin_range,
493 diagonal.locally_owned_elements().n_elements() + 1);
494 AssertIndexRange(end_range,
495 diagonal.locally_owned_elements().n_elements() + 1);
496
497 const value_type *diagonal_entry = diagonal.begin() + begin_range;
498 const unsigned int length = end_range - begin_range;
499
501 for (unsigned int i = 0; i < length; ++i)
502 dst_pointer_to_current_range[i] =
503 diagonal_entry[i] * src_pointer_to_current_range[i];
504}
505
506
507#endif
508
510
511#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
size_type locally_owned_size() const
#define DEAL_II_OPENMP_SIMD_PRAGMA
Definition config.h:214
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcIndexRange(std::size_t arg1, std::size_t arg2, std::size_t arg3)
@ diagonal
Matrix is diagonal.
Tpetra::Vector< Number, LO, GO, NodeType< MemorySpace > > VectorType