Reference documentation for deal.II version 8.4.2
multigrid.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 2014 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/parallel_vector.h>
19 #include <deal.II/lac/parallel_block_vector.h>
20 #include <deal.II/lac/petsc_vector.h>
21 #include <deal.II/lac/petsc_block_vector.h>
22 #include <deal.II/lac/trilinos_vector.h>
23 #include <deal.II/lac/trilinos_block_vector.h>
24 #include <deal.II/lac/sparse_matrix.h>
25 #include <deal.II/lac/block_sparse_matrix.h>
26 #include <deal.II/multigrid/mg_transfer.h>
27 #include <deal.II/multigrid/mg_transfer_block.h>
28 #include <deal.II/multigrid/mg_transfer_component.h>
29 #include <deal.II/multigrid/mg_smoother.h>
30 #include <deal.II/multigrid/mg_transfer_block.templates.h>
31 #include <deal.II/multigrid/mg_transfer_component.templates.h>
32 #include <deal.II/multigrid/multigrid.templates.h>
33 
34 DEAL_II_NAMESPACE_OPEN
35 
36 
38 {}
39 
40 
42  const ConstraintMatrix &c, const MGConstrainedDoFs &mg_c)
43  :
44  constraints(&c),
45  mg_constrained_dofs(&mg_c)
46 {}
47 
48 
49 template <typename number>
51  :
52  memory(0, typeid(*this).name())
53 {}
54 
55 
56 template <typename number>
58 {
59  if (memory != 0) memory = 0;
60 }
61 
62 
63 template <typename number>
64 void
65 MGTransferBlock<number>::initialize (const std::vector<number> &f,
67 {
68  factors = f;
69  memory = &mem;
70 }
71 
72 
73 template <typename number>
75  const unsigned int to_level,
77  const BlockVector<number> &src) const
78 {
79  Assert ((to_level >= 1) && (to_level<=prolongation_matrices.size()),
80  ExcIndexRange (to_level, 1, prolongation_matrices.size()+1));
81  Assert (src.n_blocks() == this->n_mg_blocks,
82  ExcDimensionMismatch(src.n_blocks(), this->n_mg_blocks));
83  Assert (dst.n_blocks() == this->n_mg_blocks,
84  ExcDimensionMismatch(dst.n_blocks(), this->n_mg_blocks));
85 
86  // Multiplicate with prolongation
87  // matrix, but only those blocks
88  // selected.
89  for (unsigned int b=0; b<this->mg_block.size(); ++b)
90  {
91  if (this->selected[b])
92  prolongation_matrices[to_level-1]->block(b,b).vmult (
93  dst.block(this->mg_block[b]), src.block(this->mg_block[b]));
94  }
95 }
96 
97 
98 template <typename number>
100  const unsigned int from_level,
101  BlockVector<number> &dst,
102  const BlockVector<number> &src) const
103 {
104  Assert ((from_level >= 1) && (from_level<=prolongation_matrices.size()),
105  ExcIndexRange (from_level, 1, prolongation_matrices.size()+1));
106  Assert (src.n_blocks() == this->n_mg_blocks,
107  ExcDimensionMismatch(src.n_blocks(), this->n_mg_blocks));
108  Assert (dst.n_blocks() == this->n_mg_blocks,
109  ExcDimensionMismatch(dst.n_blocks(), this->n_mg_blocks));
110 
111  for (unsigned int b=0; b<this->mg_block.size(); ++b)
112  {
113  if (this->selected[b])
114  {
115  if (factors.size() != 0)
116  {
117  Assert (memory != 0, ExcNotInitialized());
118  Vector<number> *aux = memory->alloc();
119  aux->reinit(dst.block(this->mg_block[b]));
120  prolongation_matrices[from_level-1]->block(b,b).Tvmult (
121  *aux, src.block(this->mg_block[b]));
122 
123  dst.block(this->mg_block[b]).add(factors[b], *aux);
124  memory->free(aux);
125  }
126  else
127  {
128  prolongation_matrices[from_level-1]->block(b,b).Tvmult_add (
129  dst.block(this->mg_block[b]), src.block(this->mg_block[b]));
130  }
131  }
132  }
133 }
134 
135 
136 
137 std::size_t
139 {
140  std::size_t result = sizeof(*this);
141  result += MemoryConsumption::memory_consumption(component_mask)
142  - sizeof(ComponentMask);
143  result += MemoryConsumption::memory_consumption(target_component)
144  - sizeof(mg_target_component);
146  - sizeof(sizes);
147  result += MemoryConsumption::memory_consumption(component_start)
148  - sizeof(component_start);
149  result += MemoryConsumption::memory_consumption(mg_component_start)
150  - sizeof(mg_component_start);
151  result += MemoryConsumption::memory_consumption(prolongation_sparsities)
152  - sizeof(prolongation_sparsities);
154  - sizeof(prolongation_matrices);
155 //TODO:[GK] Add this.
156 // result += MemoryConsumption::memory_consumption(copy_to_and_from_indices)
157 // - sizeof(copy_to_and_from_indices);
158  return result;
159 }
160 
161 
162 //TODO:[GK] Add all those little vectors.
163 std::size_t
165 {
166  std::size_t result = sizeof(*this);
167  result += sizeof(unsigned int) * sizes.size();
169  - sizeof(selected);
171  - sizeof(mg_block);
173  - sizeof(block_start);
175  - sizeof(mg_block_start);
176  result += MemoryConsumption::memory_consumption(prolongation_sparsities)
177  - sizeof(prolongation_sparsities);
179  - sizeof(prolongation_matrices);
180 //TODO:[GK] Add this.
181 // result += MemoryConsumption::memory_consumption(copy_indices)
182 // - sizeof(copy_indices);
183  return result;
184 }
185 
186 
187 //----------------------------------------------------------------------//
188 
189 template<typename number>
191 {}
192 
193 
194 template<typename number>
196  :
197  constraints(&c)
198 {}
199 
200 template <typename number>
202 {}
203 
204 
205 template <typename number>
207  const unsigned int to_level,
208  Vector<number> &dst,
209  const Vector<number> &src) const
210 {
211  Assert ((to_level >= 1) && (to_level<=prolongation_matrices.size()),
212  ExcIndexRange (to_level, 1, prolongation_matrices.size()+1));
213 
215  mg_target_component[mg_selected_component])
216  .vmult (dst, src);
217 }
218 
219 
220 template <typename number>
222  const unsigned int from_level,
223  Vector<number> &dst,
224  const Vector<number> &src) const
225 {
226  Assert ((from_level >= 1) && (from_level<=prolongation_matrices.size()),
227  ExcIndexRange (from_level, 1, prolongation_matrices.size()+1));
228 
230  mg_target_component[mg_selected_component])
231  .Tvmult_add (dst, src);
232 }
233 
234 
235 //----------------------------------------------------------------------//
236 
237 template <typename number>
239 {}
240 
241 
242 template <typename number>
244  const ConstraintMatrix &c, const MGConstrainedDoFs &mg_c)
245  : MGTransferBlockBase(c, mg_c)
246 {}
247 
248 template <typename number>
250 {}
251 
252 
253 template <typename number>
255  const unsigned int to_level,
256  Vector<number> &dst,
257  const Vector<number> &src) const
258 {
259  Assert ((to_level >= 1) && (to_level<=prolongation_matrices.size()),
260  ExcIndexRange (to_level, 1, prolongation_matrices.size()+1));
261 
262  prolongation_matrices[to_level-1]->block(selected_block,
264  .vmult (dst, src);
265 }
266 
267 
268 template <typename number>
270  const unsigned int from_level,
271  Vector<number> &dst,
272  const Vector<number> &src) const
273 {
274  Assert ((from_level >= 1) && (from_level<=prolongation_matrices.size()),
275  ExcIndexRange (from_level, 1, prolongation_matrices.size()+1));
276 
277  prolongation_matrices[from_level-1]->block(selected_block,
279  .Tvmult_add (dst, src);
280 }
281 
282 
283 
284 // Explicit instantiations
285 
286 #include "multigrid.inst"
287 
288 template class MGTransferBlock<float>;
289 template class MGTransferBlock<double>;
290 template class MGTransferSelect<float>;
291 template class MGTransferSelect<double>;
292 template class MGTransferBlockSelect<float>;
293 template class MGTransferBlockSelect<double>;
294 
295 
296 DEAL_II_NAMESPACE_CLOSE
std::size_t memory_consumption() const
Definition: multigrid.cc:164
virtual VectorType * alloc()=0
virtual void prolongate(const unsigned int to_level, BlockVector< number > &dst, const BlockVector< number > &src) const
Definition: multigrid.cc:74
std::vector< std_cxx11::shared_ptr< BlockSparseMatrix< double > > > prolongation_matrices
std::vector< types::global_dof_index > block_start
SmartPointer< const MGConstrainedDoFs, MGTransferBlockBase > mg_constrained_dofs
virtual void free(const VectorType *const)=0
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:294
std::size_t memory_consumption() const
Definition: multigrid.cc:138
virtual void restrict_and_add(const unsigned int from_level, BlockVector< number > &dst, const BlockVector< number > &src) const
Definition: multigrid.cc:99
virtual ~MGTransferBlock()
Definition: multigrid.cc:57
virtual void restrict_and_add(const unsigned int from_level, Vector< number > &dst, const Vector< number > &src) const
Definition: multigrid.cc:221
void initialize(const std::vector< number > &factors, VectorMemory< Vector< number > > &memory)
Definition: multigrid.cc:65
std::vector< unsigned int > mg_block
unsigned int mg_selected_component
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:254
std::vector< std::vector< types::global_dof_index > > mg_block_start
unsigned int n_blocks() const
SmartPointer< VectorMemory< Vector< number > >, MGTransferBlock< number > > memory
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
virtual ~MGTransferSelect()
Definition: multigrid.cc:201
std::vector< number > factors
virtual ~MGTransferBlockSelect()
Definition: multigrid.cc:249
virtual void restrict_and_add(const unsigned int from_level, Vector< number > &dst, const Vector< number > &src) const
Definition: multigrid.cc:269
BlockType & block(const unsigned int i)
SmartPointer< const ConstraintMatrix, MGTransferBlockBase > constraints
std::vector< bool > selected
virtual void prolongate(const unsigned int to_level, Vector< number > &dst, const Vector< number > &src) const
Definition: multigrid.cc:206