Reference documentation for deal.II version 8.5.1
multigrid.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 2016 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_vector.h>
22 #include <deal.II/lac/petsc_block_vector.h>
23 #include <deal.II/lac/trilinos_vector.h>
24 #include <deal.II/lac/trilinos_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(0, typeid(*this).name())
65 {}
66 
67 
68 template <typename number>
70 {
71  if (memory != 0) memory = 0;
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 != 0, 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 template <typename number>
219 {}
220 
221 
222 template <typename number>
224  const unsigned int to_level,
225  Vector<number> &dst,
226  const Vector<number> &src) const
227 {
228  Assert ((to_level >= 1) && (to_level<=prolongation_matrices.size()),
229  ExcIndexRange (to_level, 1, prolongation_matrices.size()+1));
230 
231  prolongation_matrices[to_level-1]->block(mg_target_component[mg_selected_component],
232  mg_target_component[mg_selected_component])
233  .vmult (dst, src);
234 }
235 
236 
237 
238 template <typename number>
240  const unsigned int from_level,
241  Vector<number> &dst,
242  const Vector<number> &src) const
243 {
244  Assert ((from_level >= 1) && (from_level<=prolongation_matrices.size()),
245  ExcIndexRange (from_level, 1, prolongation_matrices.size()+1));
246 
247  prolongation_matrices[from_level-1]->block(mg_target_component[mg_selected_component],
248  mg_target_component[mg_selected_component])
249  .Tvmult_add (dst, src);
250 }
251 
252 
253 //----------------------------------------------------------------------//
254 
255 template <typename number>
257  :
258  selected_block (0)
259 {}
260 
261 
262 
263 template <typename number>
265  :
266  MGTransferBlockBase(mg_c),
267  selected_block (0)
268 {}
269 
270 
271 
272 template <typename number>
274  const MGConstrainedDoFs &mg_c)
275  :
276  MGTransferBlockBase(mg_c),
277  selected_block (0)
278 {}
279 
280 
281 
282 template <typename number>
284 {}
285 
286 
287 
288 template <typename number>
289 void
290 MGTransferBlockSelect<number>::prolongate (const unsigned int to_level,
291  Vector<number> &dst,
292  const Vector<number> &src) const
293 {
294  Assert ((to_level >= 1) && (to_level<=prolongation_matrices.size()),
295  ExcIndexRange (to_level, 1, prolongation_matrices.size()+1));
296 
297  prolongation_matrices[to_level-1]->block(selected_block,
298  selected_block)
299  .vmult (dst, src);
300 }
301 
302 
303 template <typename number>
304 void
305 MGTransferBlockSelect<number>::restrict_and_add (const unsigned int from_level,
306  Vector<number> &dst,
307  const Vector<number> &src) const
308 {
309  Assert ((from_level >= 1) && (from_level<=prolongation_matrices.size()),
310  ExcIndexRange (from_level, 1, prolongation_matrices.size()+1));
311 
312  prolongation_matrices[from_level-1]->block(selected_block,
313  selected_block)
314  .Tvmult_add (dst, src);
315 }
316 
317 
318 
319 // Explicit instantiations
320 
321 #include "multigrid.inst"
322 
323 template class MGTransferBlock<float>;
324 template class MGTransferBlock<double>;
325 template class MGTransferSelect<float>;
326 template class MGTransferSelect<double>;
327 template class MGTransferBlockSelect<float>;
328 template class MGTransferBlockSelect<double>;
329 
330 
331 DEAL_II_NAMESPACE_CLOSE
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
std::vector< std_cxx11::shared_ptr< BlockSparseMatrix< double > > > prolongation_matrices
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_cxx11::shared_ptr< BlockSparseMatrix< double > > > prolongation_matrices
#define Assert(cond, exc)
Definition: exceptions.h:313
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:239
void initialize(const std::vector< number > &factors, VectorMemory< Vector< number > > &memory)
Definition: multigrid.cc:77
std::vector< unsigned int > mg_block
std_cxx11::enable_if< std_cxx11::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)
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:290
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 ~MGTransferSelect()
Definition: multigrid.cc:218
virtual ~MGTransferBlockSelect()
Definition: multigrid.cc:283
virtual void restrict_and_add(const unsigned int from_level, Vector< number > &dst, const Vector< number > &src) const
Definition: multigrid.cc:305
std::vector< std::vector< types::global_dof_index > > mg_component_start
BlockType & block(const unsigned int i)
std::vector< bool > selected
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:223