Reference documentation for deal.II version 9.0.0
multigrid.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 2017 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 at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 
17 #include <deal.II/lac/vector.h>
18 #include <deal.II/lac/la_vector.h>
19 #include <deal.II/lac/la_parallel_vector.h>
20 #include <deal.II/lac/la_parallel_block_vector.h>
21 #include <deal.II/lac/petsc_parallel_vector.h>
22 #include <deal.II/lac/petsc_parallel_block_vector.h>
23 #include <deal.II/lac/trilinos_vector.h>
24 #include <deal.II/lac/trilinos_parallel_block_vector.h>
25 #include <deal.II/lac/sparse_matrix.h>
26 #include <deal.II/lac/block_sparse_matrix.h>
27 #include <deal.II/multigrid/mg_transfer.h>
28 #include <deal.II/multigrid/mg_transfer_block.h>
29 #include <deal.II/multigrid/mg_transfer_component.h>
30 #include <deal.II/multigrid/mg_smoother.h>
31 #include <deal.II/multigrid/mg_transfer_block.templates.h>
32 #include <deal.II/multigrid/mg_transfer_component.templates.h>
33 #include <deal.II/multigrid/multigrid.templates.h>
34 
35 DEAL_II_NAMESPACE_OPEN
36 
37 
39  :
40  n_mg_blocks (0)
41 {}
42 
43 
44 
46  :
47  n_mg_blocks (0),
48  mg_constrained_dofs(&mg_c)
49 {}
50 
51 
52 
54  const MGConstrainedDoFs &mg_c)
55  :
56  n_mg_blocks (0),
57  mg_constrained_dofs(&mg_c)
58 {}
59 
60 
61 template <typename number>
63  :
64  memory(nullptr, typeid(*this).name())
65 {}
66 
67 
68 template <typename number>
70 {
71  if (memory != nullptr) memory = nullptr;
72 }
73 
74 
75 template <typename number>
76 void
77 MGTransferBlock<number>::initialize (const std::vector<number> &f,
79 {
80  factors = f;
81  memory = &mem;
82 }
83 
84 
85 template <typename number>
87  const unsigned int to_level,
89  const BlockVector<number> &src) const
90 {
91  Assert ((to_level >= 1) && (to_level<=prolongation_matrices.size()),
92  ExcIndexRange (to_level, 1, prolongation_matrices.size()+1));
93  Assert (src.n_blocks() == this->n_mg_blocks,
94  ExcDimensionMismatch(src.n_blocks(), this->n_mg_blocks));
95  Assert (dst.n_blocks() == this->n_mg_blocks,
96  ExcDimensionMismatch(dst.n_blocks(), this->n_mg_blocks));
97 
98  // Multiplicate with prolongation
99  // matrix, but only those blocks
100  // selected.
101  for (unsigned int b=0; b<this->mg_block.size(); ++b)
102  {
103  if (this->selected[b])
104  prolongation_matrices[to_level-1]->block(b,b).vmult (
105  dst.block(this->mg_block[b]), src.block(this->mg_block[b]));
106  }
107 }
108 
109 
110 template <typename number>
112  const unsigned int from_level,
113  BlockVector<number> &dst,
114  const BlockVector<number> &src) const
115 {
116  Assert ((from_level >= 1) && (from_level<=prolongation_matrices.size()),
117  ExcIndexRange (from_level, 1, prolongation_matrices.size()+1));
118  Assert (src.n_blocks() == this->n_mg_blocks,
119  ExcDimensionMismatch(src.n_blocks(), this->n_mg_blocks));
120  Assert (dst.n_blocks() == this->n_mg_blocks,
121  ExcDimensionMismatch(dst.n_blocks(), this->n_mg_blocks));
122 
123  for (unsigned int b=0; b<this->mg_block.size(); ++b)
124  {
125  if (this->selected[b])
126  {
127  if (factors.size() != 0)
128  {
129  Assert (memory != nullptr, ExcNotInitialized());
130  Vector<number> *aux = memory->alloc();
131  aux->reinit(dst.block(this->mg_block[b]));
132  prolongation_matrices[from_level-1]->block(b,b).Tvmult (
133  *aux, src.block(this->mg_block[b]));
134 
135  dst.block(this->mg_block[b]).add(factors[b], *aux);
136  memory->free(aux);
137  }
138  else
139  {
140  prolongation_matrices[from_level-1]->block(b,b).Tvmult_add (
141  dst.block(this->mg_block[b]), src.block(this->mg_block[b]));
142  }
143  }
144  }
145 }
146 
147 
148 
149 std::size_t
151 {
152  std::size_t result = sizeof(*this);
154  - sizeof(ComponentMask);
156  - sizeof(mg_target_component);
158  - sizeof(sizes);
160  - sizeof(component_start);
162  - sizeof(mg_component_start);
163  result += MemoryConsumption::memory_consumption(prolongation_sparsities)
164  - sizeof(prolongation_sparsities);
166  - sizeof(prolongation_matrices);
167 //TODO:[GK] Add this.
168 // result += MemoryConsumption::memory_consumption(copy_to_and_from_indices)
169 // - sizeof(copy_to_and_from_indices);
170  return result;
171 }
172 
173 
174 //TODO:[GK] Add all those little vectors.
175 std::size_t
177 {
178  std::size_t result = sizeof(*this);
179  result += sizeof(unsigned int) * sizes.size();
181  - sizeof(selected);
183  - sizeof(mg_block);
185  - sizeof(block_start);
187  - sizeof(mg_block_start);
188  result += MemoryConsumption::memory_consumption(prolongation_sparsities)
189  - sizeof(prolongation_sparsities);
191  - sizeof(prolongation_matrices);
192 //TODO:[GK] Add this.
193 // result += MemoryConsumption::memory_consumption(copy_indices)
194 // - sizeof(copy_indices);
195  return result;
196 }
197 
198 
199 //----------------------------------------------------------------------//
200 
201 template <typename number>
203  :
204  selected_component (0),
205  mg_selected_component (0)
206 {}
207 
208 
209 template <typename number>
211  :
212  selected_component (0),
213  mg_selected_component (0),
214  constraints(&c)
215 {}
216 
217 
218 
219 template <typename number>
221  const unsigned int to_level,
222  Vector<number> &dst,
223  const Vector<number> &src) const
224 {
225  Assert ((to_level >= 1) && (to_level<=prolongation_matrices.size()),
226  ExcIndexRange (to_level, 1, prolongation_matrices.size()+1));
227 
228  prolongation_matrices[to_level-1]->block(mg_target_component[mg_selected_component],
229  mg_target_component[mg_selected_component])
230  .vmult (dst, src);
231 }
232 
233 
234 
235 template <typename number>
237  const unsigned int from_level,
238  Vector<number> &dst,
239  const Vector<number> &src) const
240 {
241  Assert ((from_level >= 1) && (from_level<=prolongation_matrices.size()),
242  ExcIndexRange (from_level, 1, prolongation_matrices.size()+1));
243 
244  prolongation_matrices[from_level-1]->block(mg_target_component[mg_selected_component],
245  mg_target_component[mg_selected_component])
246  .Tvmult_add (dst, src);
247 }
248 
249 
250 //----------------------------------------------------------------------//
251 
252 template <typename number>
254  :
255  selected_block (0)
256 {}
257 
258 
259 
260 template <typename number>
262  :
263  MGTransferBlockBase(mg_c),
264  selected_block (0)
265 {}
266 
267 
268 
269 template <typename number>
271  const MGConstrainedDoFs &mg_c)
272  :
273  MGTransferBlockBase(mg_c),
274  selected_block (0)
275 {}
276 
277 
278 
279 template <typename number>
280 void
281 MGTransferBlockSelect<number>::prolongate (const unsigned int to_level,
282  Vector<number> &dst,
283  const Vector<number> &src) const
284 {
285  Assert ((to_level >= 1) && (to_level<=prolongation_matrices.size()),
286  ExcIndexRange (to_level, 1, prolongation_matrices.size()+1));
287 
288  prolongation_matrices[to_level-1]->block(selected_block,
289  selected_block)
290  .vmult (dst, src);
291 }
292 
293 
294 template <typename number>
295 void
296 MGTransferBlockSelect<number>::restrict_and_add (const unsigned int from_level,
297  Vector<number> &dst,
298  const Vector<number> &src) const
299 {
300  Assert ((from_level >= 1) && (from_level<=prolongation_matrices.size()),
301  ExcIndexRange (from_level, 1, prolongation_matrices.size()+1));
302 
303  prolongation_matrices[from_level-1]->block(selected_block,
304  selected_block)
305  .Tvmult_add (dst, src);
306 }
307 
308 
309 
310 // Explicit instantiations
311 
312 #include "multigrid.inst"
313 
314 template class MGTransferBlock<float>;
315 template class MGTransferBlock<double>;
316 template class MGTransferSelect<float>;
317 template class MGTransferSelect<double>;
318 template class MGTransferBlockSelect<float>;
319 template class MGTransferBlockSelect<double>;
320 
321 
322 DEAL_II_NAMESPACE_CLOSE
std::vector< std::shared_ptr< BlockSparseMatrix< double > > > prolongation_matrices
std::size_t memory_consumption() const
Definition: multigrid.cc:176
virtual void prolongate(const unsigned int to_level, BlockVector< number > &dst, const BlockVector< number > &src) const
Definition: multigrid.cc:86
std::vector< unsigned int > target_component
static ::ExceptionBase & ExcNotInitialized()
std::vector< types::global_dof_index > block_start
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
std::vector< std::vector< types::global_dof_index > > sizes
std::vector< std::shared_ptr< BlockSparseMatrix< double > > > prolongation_matrices
#define Assert(cond, exc)
Definition: exceptions.h:1142
std::size_t memory_consumption() const
Definition: multigrid.cc:150
virtual void restrict_and_add(const unsigned int from_level, BlockVector< number > &dst, const BlockVector< number > &src) const
Definition: multigrid.cc:111
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
std::vector< types::global_dof_index > component_start
virtual ~MGTransferBlock()
Definition: multigrid.cc:69
virtual void restrict_and_add(const unsigned int from_level, Vector< number > &dst, const Vector< number > &src) const
Definition: multigrid.cc:236
void initialize(const std::vector< number > &factors, VectorMemory< Vector< number > > &memory)
Definition: multigrid.cc:77
std::vector< unsigned int > mg_block
std::vector< unsigned int > mg_target_component
virtual void prolongate(const unsigned int to_level, Vector< number > &dst, const Vector< number > &src) const
Definition: multigrid.cc:281
std::vector< std::vector< types::global_dof_index > > mg_block_start
unsigned int n_blocks() const
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
virtual void restrict_and_add(const unsigned int from_level, Vector< number > &dst, const Vector< number > &src) const
Definition: multigrid.cc:296
std::vector< std::vector< types::global_dof_index > > mg_component_start
BlockType & block(const unsigned int i)
std::vector< bool > selected
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)
std::vector< std::vector< types::global_dof_index > > sizes
virtual void prolongate(const unsigned int to_level, Vector< number > &dst, const Vector< number > &src) const
Definition: multigrid.cc:220