File: multigrid.cc

package info (click to toggle)
deal.ii 9.7.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 326,024 kB
  • sloc: cpp: 440,899; ansic: 77,337; python: 3,307; perl: 1,041; sh: 1,022; xml: 252; makefile: 97; javascript: 14
file content (317 lines) | stat: -rw-r--r-- 10,259 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// ------------------------------------------------------------------------
//
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) 2000 - 2025 by the deal.II authors
//
// This file is part of the deal.II library.
//
// Part of the source code is dual licensed under Apache-2.0 WITH
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
// governing the source code and code contributions can be found in
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
//
// ------------------------------------------------------------------------


#include <deal.II/lac/block_sparse_matrix.h>
#include <deal.II/lac/la_parallel_block_vector.h>
#include <deal.II/lac/la_parallel_vector.h>
#include <deal.II/lac/petsc_block_vector.h>
#include <deal.II/lac/petsc_vector.h>
#include <deal.II/lac/sparse_matrix.h>
#include <deal.II/lac/trilinos_parallel_block_vector.h>
#include <deal.II/lac/trilinos_vector.h>
#include <deal.II/lac/vector.h>

#include <deal.II/multigrid/mg_smoother.h>
#include <deal.II/multigrid/mg_transfer.h>
#include <deal.II/multigrid/mg_transfer_block.h>
#include <deal.II/multigrid/mg_transfer_block.templates.h>
#include <deal.II/multigrid/mg_transfer_component.h>
#include <deal.II/multigrid/mg_transfer_component.templates.h>
#include <deal.II/multigrid/multigrid.templates.h>

DEAL_II_NAMESPACE_OPEN


MGTransferBlockBase::MGTransferBlockBase()
  : n_mg_blocks(0)
{}



MGTransferBlockBase::MGTransferBlockBase(const MGConstrainedDoFs &mg_c)
  : n_mg_blocks(0)
  , mg_constrained_dofs(&mg_c)
{}



template <typename number>
MGTransferBlock<number>::MGTransferBlock()
  : memory(nullptr, typeid(*this).name())
{}


template <typename number>
MGTransferBlock<number>::~MGTransferBlock()
{
  if (memory != nullptr)
    memory = nullptr;
}


template <typename number>
void
MGTransferBlock<number>::initialize(const std::vector<number>    &f,
                                    VectorMemory<Vector<number>> &mem)
{
  factors = f;
  memory  = &mem;
}


template <typename number>
void
MGTransferBlock<number>::prolongate(const unsigned int         to_level,
                                    BlockVector<number>       &dst,
                                    const BlockVector<number> &src) const
{
  Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
         ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));
  Assert(src.n_blocks() == this->n_mg_blocks,
         ExcDimensionMismatch(src.n_blocks(), this->n_mg_blocks));
  Assert(dst.n_blocks() == this->n_mg_blocks,
         ExcDimensionMismatch(dst.n_blocks(), this->n_mg_blocks));

  if constexpr (running_in_debug_mode())
    {
      if (this->mg_constrained_dofs != nullptr)
        Assert(this->mg_constrained_dofs
                   ->get_user_constraint_matrix(to_level - 1)
                   .get_local_lines()
                   .size() == 0,
               ExcNotImplemented());
    }

  // Multiplicate with prolongation
  // matrix, but only those blocks
  // selected.
  for (unsigned int b = 0; b < this->mg_block.size(); ++b)
    {
      if (this->selected[b])
        prolongation_matrices[to_level - 1]->block(b, b).vmult(
          dst.block(this->mg_block[b]), src.block(this->mg_block[b]));
    }
}


template <typename number>
void
MGTransferBlock<number>::restrict_and_add(const unsigned int         from_level,
                                          BlockVector<number>       &dst,
                                          const BlockVector<number> &src) const
{
  Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
         ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));
  Assert(src.n_blocks() == this->n_mg_blocks,
         ExcDimensionMismatch(src.n_blocks(), this->n_mg_blocks));
  Assert(dst.n_blocks() == this->n_mg_blocks,
         ExcDimensionMismatch(dst.n_blocks(), this->n_mg_blocks));

  for (unsigned int b = 0; b < this->mg_block.size(); ++b)
    {
      if (this->selected[b])
        {
          if (factors.size() != 0)
            {
              Assert(memory != nullptr, ExcNotInitialized());
              Vector<number> *aux = memory->alloc();
              aux->reinit(dst.block(this->mg_block[b]));
              prolongation_matrices[from_level - 1]->block(b, b).Tvmult(
                *aux, src.block(this->mg_block[b]));

              dst.block(this->mg_block[b]).add(factors[b], *aux);
              memory->free(aux);
            }
          else
            {
              prolongation_matrices[from_level - 1]->block(b, b).Tvmult_add(
                dst.block(this->mg_block[b]), src.block(this->mg_block[b]));
            }
        }
    }
}



std::size_t
MGTransferComponentBase::memory_consumption() const
{
  std::size_t result = sizeof(*this);
  result += MemoryConsumption::memory_consumption(component_mask) -
            sizeof(ComponentMask);
  result += MemoryConsumption::memory_consumption(target_component) -
            sizeof(mg_target_component);
  result += MemoryConsumption::memory_consumption(sizes) - sizeof(sizes);
  result += MemoryConsumption::memory_consumption(component_start) -
            sizeof(component_start);
  result += MemoryConsumption::memory_consumption(mg_component_start) -
            sizeof(mg_component_start);
  result += MemoryConsumption::memory_consumption(prolongation_sparsities) -
            sizeof(prolongation_sparsities);
  result += MemoryConsumption::memory_consumption(prolongation_matrices) -
            sizeof(prolongation_matrices);
  // TODO:[GK] Add this.
  //   result += MemoryConsumption::memory_consumption(copy_to_and_from_indices)
  //          - sizeof(copy_to_and_from_indices);
  return result;
}


// TODO:[GK] Add all those little vectors.
std::size_t
MGTransferBlockBase::memory_consumption() const
{
  std::size_t result = sizeof(*this);
  result += sizeof(unsigned int) * sizes.size();
  result += MemoryConsumption::memory_consumption(selected) - sizeof(selected);
  result += MemoryConsumption::memory_consumption(mg_block) - sizeof(mg_block);
  result +=
    MemoryConsumption::memory_consumption(block_start) - sizeof(block_start);
  result += MemoryConsumption::memory_consumption(mg_block_start) -
            sizeof(mg_block_start);
  result += MemoryConsumption::memory_consumption(prolongation_sparsities) -
            sizeof(prolongation_sparsities);
  result += MemoryConsumption::memory_consumption(prolongation_matrices) -
            sizeof(prolongation_matrices);
  // TODO:[GK] Add this.
  //   result += MemoryConsumption::memory_consumption(copy_indices)
  //          - sizeof(copy_indices);
  return result;
}


//----------------------------------------------------------------------//

template <typename number>
MGTransferSelect<number>::MGTransferSelect()
  : selected_component(0)
  , mg_selected_component(0)
{}


template <typename number>
MGTransferSelect<number>::MGTransferSelect(const AffineConstraints<double> &c)
  : selected_component(0)
  , mg_selected_component(0)
  , constraints(&c)
{}



template <typename number>
void
MGTransferSelect<number>::prolongate(const unsigned int    to_level,
                                     Vector<number>       &dst,
                                     const Vector<number> &src) const
{
  Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
         ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));

  prolongation_matrices[to_level - 1]
    ->block(mg_target_component[mg_selected_component],
            mg_target_component[mg_selected_component])
    .vmult(dst, src);
}



template <typename number>
void
MGTransferSelect<number>::restrict_and_add(const unsigned int    from_level,
                                           Vector<number>       &dst,
                                           const Vector<number> &src) const
{
  Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
         ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));

  prolongation_matrices[from_level - 1]
    ->block(mg_target_component[mg_selected_component],
            mg_target_component[mg_selected_component])
    .Tvmult_add(dst, src);
}


//----------------------------------------------------------------------//

template <typename number>
MGTransferBlockSelect<number>::MGTransferBlockSelect()
  : selected_block(0)
{}



template <typename number>
MGTransferBlockSelect<number>::MGTransferBlockSelect(
  const MGConstrainedDoFs &mg_c)
  : MGTransferBlockBase(mg_c)
  , selected_block(0)
{}



template <typename number>
void
MGTransferBlockSelect<number>::prolongate(const unsigned int    to_level,
                                          Vector<number>       &dst,
                                          const Vector<number> &src) const
{
  Assert((to_level >= 1) && (to_level <= prolongation_matrices.size()),
         ExcIndexRange(to_level, 1, prolongation_matrices.size() + 1));

  if constexpr (running_in_debug_mode())
    {
      if (this->mg_constrained_dofs != nullptr)
        Assert(this->mg_constrained_dofs
                   ->get_user_constraint_matrix(to_level - 1)
                   .get_local_lines()
                   .size() == 0,
               ExcNotImplemented());
    }

  prolongation_matrices[to_level - 1]
    ->block(selected_block, selected_block)
    .vmult(dst, src);
}


template <typename number>
void
MGTransferBlockSelect<number>::restrict_and_add(const unsigned int from_level,
                                                Vector<number>    &dst,
                                                const Vector<number> &src) const
{
  Assert((from_level >= 1) && (from_level <= prolongation_matrices.size()),
         ExcIndexRange(from_level, 1, prolongation_matrices.size() + 1));

  prolongation_matrices[from_level - 1]
    ->block(selected_block, selected_block)
    .Tvmult_add(dst, src);
}



// Explicit instantiations

#include "multigrid/multigrid.inst"

template class MGTransferBlock<float>;
template class MGTransferBlock<double>;
template class MGTransferSelect<float>;
template class MGTransferSelect<double>;
template class MGTransferBlockSelect<float>;
template class MGTransferBlockSelect<double>;


DEAL_II_NAMESPACE_CLOSE