| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2013 - 2024 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_mg_sparse_matrix_collection_h |
| 16 |
#define dealii_mg_sparse_matrix_collection_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <deal.II/base/mg_level_object.h> |
| 21 |
|
| 22 |
#include <deal.II/lac/dynamic_sparsity_pattern.h> |
| 23 |
#include <deal.II/lac/sparse_matrix.h> |
| 24 |
#include <deal.II/lac/sparsity_pattern.h> |
| 25 |
#include <deal.II/lac/vector.h> |
| 26 |
|
| 27 |
#include <deal.II/multigrid/mg_base.h> |
| 28 |
#include <deal.II/multigrid/mg_tools.h> |
| 29 |
|
| 30 |
#include <memory> |
| 31 |
|
| 32 |
DEAL_II_NAMESPACE_OPEN |
| 33 |
|
| 34 |
namespace mg |
| 35 |
{ |
| 36 |
/** |
| 37 |
* Handler and storage for all five SparseMatrix object involved in using |
| 38 |
* multigrid with local refinement. |
| 39 |
*/ |
| 40 |
template <typename number> |
| 41 |
class SparseMatrixCollection : public EnableObserverPointer |
| 42 |
{ |
| 43 |
public: |
| 44 |
void |
| 45 |
resize(const unsigned int minlevel, const unsigned int maxlevel); |
| 46 |
|
| 47 |
template <int dim, int spacedim> |
| 48 |
void |
| 49 |
reinit(const DoFHandler<dim, spacedim> &dof_handler); |
| 50 |
|
| 51 |
void |
| 52 |
set_zero(); |
| 53 |
|
| 54 |
MGLevelObject<SparsityPattern> sparsity; |
| 55 |
MGLevelObject<SparsityPattern> sparsity_edge; |
| 56 |
|
| 57 |
MGLevelObject<SparseMatrix<number>> matrix; |
| 58 |
MGLevelObject<SparseMatrix<number>> matrix_down; |
| 59 |
MGLevelObject<SparseMatrix<number>> matrix_up; |
| 60 |
MGLevelObject<SparseMatrix<number>> matrix_in; |
| 61 |
MGLevelObject<SparseMatrix<number>> matrix_out; |
| 62 |
}; |
| 63 |
|
| 64 |
|
| 65 |
template <typename number> |
| 66 |
void |
| 67 |
SparseMatrixCollection<number>::resize(const unsigned int minlevel, |
| 68 |
const unsigned int maxlevel) |
| 69 |
{ |
| 70 |
matrix.resize(minlevel, maxlevel); |
| 71 |
matrix.clear_elements(); |
| 72 |
matrix_up.resize(minlevel + 1, maxlevel); |
| 73 |
matrix_up.clear_elements(); |
| 74 |
matrix_down.resize(minlevel + 1, maxlevel); |
| 75 |
matrix_down.clear_elements(); |
| 76 |
matrix_in.resize(minlevel, maxlevel); |
| 77 |
matrix_in.clear_elements(); |
| 78 |
matrix_out.resize(minlevel, maxlevel); |
| 79 |
matrix_out.clear_elements(); |
| 80 |
sparsity.resize(minlevel, maxlevel); |
| 81 |
sparsity_edge.resize(minlevel, maxlevel); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
template <typename number> |
| 86 |
template <int dim, int spacedim> |
| 87 |
void |
| 88 |
SparseMatrixCollection<number>::reinit( |
| 89 |
const DoFHandler<dim, spacedim> &dof_handler) |
| 90 |
{ |
| 91 |
AssertIndexRange(sparsity.max_level(), |
| 92 |
dof_handler.get_triangulation().n_levels()); |
| 93 |
|
| 94 |
for (unsigned int level = sparsity.min_level(); |
| 95 |
level <= sparsity.max_level(); |
| 96 |
++level) |
| 97 |
{ |
| 98 |
DynamicSparsityPattern dsp(dof_handler.n_dofs(level)); |
| 99 |
MGTools::make_flux_sparsity_pattern(dof_handler, dsp, level); |
| 100 |
sparsity[level].copy_from(dsp); |
| 101 |
matrix[level].reinit(sparsity[level]); |
| 102 |
matrix_in[level].reinit(sparsity[level]); |
| 103 |
matrix_out[level].reinit(sparsity[level]); |
| 104 |
if (level > 0) |
| 105 |
{ |
| 106 |
DynamicSparsityPattern ci_sparsity; |
| 107 |
ci_sparsity.reinit(dof_handler.n_dofs(level - 1), |
| 108 |
dof_handler.n_dofs(level)); |
| 109 |
MGTools::make_flux_sparsity_pattern_edge(dof_handler, |
| 110 |
ci_sparsity, |
| 111 |
level); |
| 112 |
sparsity_edge[level].copy_from(ci_sparsity); |
| 113 |
matrix_up[level].reinit(sparsity_edge[level]); |
| 114 |
matrix_down[level].reinit(sparsity_edge[level]); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
template <typename number> |
| 120 |
void |
| 121 |
SparseMatrixCollection<number>::set_zero() |
| 122 |
{ |
| 123 |
matrix = 0.; |
| 124 |
matrix_in = 0.; |
| 125 |
matrix_out = 0.; |
| 126 |
matrix_up = 0.; |
| 127 |
matrix_down = 0.; |
| 128 |
} |
| 129 |
|
| 130 |
} // namespace mg |
| 131 |
|
| 132 |
DEAL_II_NAMESPACE_CLOSE |
| 133 |
|
| 134 |
#endif |