Reference documentation for deal.II version 8.5.1
trilinos_block_vector.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2008 - 2015 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 #include <deal.II/lac/trilinos_block_vector.h>
17 
18 #ifdef DEAL_II_WITH_TRILINOS
19 
20 # include <deal.II/lac/trilinos_block_sparse_matrix.h>
21 
22 
23 DEAL_II_NAMESPACE_OPEN
24 
25 namespace TrilinosWrappers
26 {
27  namespace
28  {
29  // define a helper function that queries the size of an Epetra_Map object
30  // by calling either the 32- or 64-bit function necessary, and returns the
31  // result in the correct data type so that we can use it in calling other
32  // Epetra member functions that are overloaded by index type
33 #ifndef DEAL_II_WITH_64BIT_INDICES
34  int n_global_elements (const Epetra_BlockMap &map)
35  {
36  return map.NumGlobalElements();
37  }
38 #else
39  long long int n_global_elements (const Epetra_BlockMap &map)
40  {
41  return map.NumGlobalElements64();
42  }
43 #endif
44  }
45 
46 
47  namespace MPI
48  {
49  BlockVector &
51  {
53  return *this;
54  }
55 
56 
57 
58  BlockVector &
60  {
61  // we only allow assignment to vectors with the same number of blocks
62  // or to an empty BlockVector
63  Assert (n_blocks() == 0 || n_blocks() == v.n_blocks(),
65 
66  if (this->n_blocks() != v.n_blocks())
67  reinit(v.n_blocks());
68 
69  for (size_type i=0; i<this->n_blocks(); ++i)
70  this->components[i] = v.block(i);
71 
72  collect_sizes();
73 
74  return *this;
75  }
76 
77 
78 
79 #ifdef DEAL_II_WITH_CXX11
80  BlockVector &
82  {
83  swap(v);
84  return *this;
85  }
86 #endif
87 
88 
89 
90  BlockVector &
91  BlockVector::operator = (const ::TrilinosWrappers::BlockVector &v)
92  {
93  Assert (n_blocks() == v.n_blocks(),
94  ExcDimensionMismatch(n_blocks(),v.n_blocks()));
95 
96  for (size_type i=0; i<this->n_blocks(); ++i)
97  this->components[i] = v.block(i);
98 
99  return *this;
100  }
101 
102 
103 
105  {}
106 
107 
108 
109  void
110  BlockVector::reinit (const std::vector<Epetra_Map> &input_maps,
111  const bool omit_zeroing_entries)
112  {
113  const size_type no_blocks = input_maps.size();
114  std::vector<size_type> block_sizes (no_blocks);
115 
116  for (size_type i=0; i<no_blocks; ++i)
117  {
118  block_sizes[i] = n_global_elements(input_maps[i]);
119  }
120 
121  this->block_indices.reinit (block_sizes);
122  if (components.size() != n_blocks())
123  components.resize(n_blocks());
124 
125  for (size_type i=0; i<n_blocks(); ++i)
126  components[i].reinit(input_maps[i], omit_zeroing_entries);
127 
128  collect_sizes();
129  }
130 
131 
132 
133  void
134  BlockVector::reinit (const std::vector<IndexSet> &parallel_partitioning,
135  const MPI_Comm &communicator,
136  const bool omit_zeroing_entries)
137  {
138  const size_type no_blocks = parallel_partitioning.size();
139  std::vector<size_type> block_sizes (no_blocks);
140 
141  for (size_type i=0; i<no_blocks; ++i)
142  {
143  block_sizes[i] = parallel_partitioning[i].size();
144  }
145 
146  this->block_indices.reinit (block_sizes);
147  if (components.size() != n_blocks())
148  components.resize(n_blocks());
149 
150  for (size_type i=0; i<n_blocks(); ++i)
151  components[i].reinit(parallel_partitioning[i], communicator, omit_zeroing_entries);
152 
153  collect_sizes();
154  }
155 
156  void
157  BlockVector::reinit (const std::vector<IndexSet> &parallel_partitioning,
158  const std::vector<IndexSet> &ghost_values,
159  const MPI_Comm &communicator,
160  const bool vector_writable)
161  {
162  const size_type no_blocks = parallel_partitioning.size();
163  std::vector<size_type> block_sizes (no_blocks);
164 
165  for (size_type i=0; i<no_blocks; ++i)
166  {
167  block_sizes[i] = parallel_partitioning[i].size();
168  }
169 
170  this->block_indices.reinit (block_sizes);
171  if (components.size() != n_blocks())
172  components.resize(n_blocks());
173 
174  for (size_type i=0; i<n_blocks(); ++i)
175  components[i].reinit(parallel_partitioning[i], ghost_values[i],
176  communicator, vector_writable);
177 
178  collect_sizes();
179  }
180 
181 
182  void
184  const bool omit_zeroing_entries)
185  {
187  if (components.size() != n_blocks())
188  components.resize(n_blocks());
189 
190  for (size_type i=0; i<n_blocks(); ++i)
191  components[i].reinit(v.block(i), omit_zeroing_entries, false);
192 
193  collect_sizes();
194  }
195 
196 
197 
198  void
199  BlockVector::reinit (const size_type num_blocks)
200  {
201  std::vector<size_type> block_sizes (num_blocks, 0);
202  this->block_indices.reinit (block_sizes);
203  if (this->components.size() != this->n_blocks())
204  this->components.resize(this->n_blocks());
205 
206  for (size_type i=0; i<this->n_blocks(); ++i)
207  components[i].clear();
208 
209  collect_sizes();
210  }
211 
212 
213 
214  void
217  const BlockVector &v)
218  {
219  Assert (m.n_block_rows() == v.n_blocks(),
221  Assert (m.n_block_cols() == v.n_blocks(),
223 
224  if (v.n_blocks() != n_blocks())
225  {
226  block_indices = v.get_block_indices();
227  components.resize(v.n_blocks());
228  }
229 
230  for (size_type i=0; i<this->n_blocks(); ++i)
231  components[i].import_nonlocal_data_for_fe(m.block(i,i), v.block(i));
232 
233  collect_sizes();
234  }
235 
236 
237 
238  void BlockVector::print (std::ostream &out,
239  const unsigned int precision,
240  const bool scientific,
241  const bool across) const
242  {
243  for (size_type i=0; i<this->n_blocks(); ++i)
244  {
245  if (across)
246  out << 'C' << i << ':';
247  else
248  out << "Component " << i << std::endl;
249  this->components[i].print(out, precision, scientific, across);
250  }
251  }
252 
253  } /* end of namespace MPI */
254 
255 
256 
257 
258 
259 
260  BlockVector &
262  {
264  return *this;
265  }
266 
267 
268 
269  void
270  BlockVector::reinit (const std::vector<Epetra_Map> &input_maps,
271  const bool omit_zeroing_entries)
272  {
273  size_type no_blocks = input_maps.size();
274  std::vector<size_type> block_sizes (no_blocks);
275 
276  for (size_type i=0; i<no_blocks; ++i)
277  block_sizes[i] = n_global_elements(input_maps[i]);
278 
279 
280  this->block_indices.reinit (block_sizes);
281  if (components.size() != n_blocks())
282  components.resize(n_blocks());
283 
284  for (size_type i=0; i<n_blocks(); ++i)
285  components[i].reinit(input_maps[i], omit_zeroing_entries);
286 
287  collect_sizes();
288  }
289 
290 
291 
292  void
293  BlockVector::reinit (const std::vector<IndexSet> &partitioning,
294  const MPI_Comm &communicator,
295  const bool omit_zeroing_entries)
296  {
297  size_type no_blocks = partitioning.size();
298  std::vector<size_type> block_sizes (no_blocks);
299 
300  for (size_type i=0; i<no_blocks; ++i)
301  block_sizes[i] = partitioning[i].size();
302 
303 
304  this->block_indices.reinit (block_sizes);
305  if (components.size() != n_blocks())
306  components.resize(n_blocks());
307 
308  for (size_type i=0; i<n_blocks(); ++i)
309  components[i].reinit(partitioning[i], communicator, omit_zeroing_entries);
310 
311  collect_sizes();
312  }
313 
314 
315 
316  void
317  BlockVector::reinit (const std::vector<size_type> &block_sizes,
318  const bool omit_zeroing_entries)
319  {
320  this->block_indices.reinit (block_sizes);
321  if (components.size() != n_blocks())
322  components.resize(n_blocks());
323 
324  for (size_type i=0; i<n_blocks(); ++i)
325  components[i].reinit(block_sizes[i], omit_zeroing_entries);
326 
327  collect_sizes();
328  }
329 
330 
331 
332  void
334  {
336  if (components.size() != n_blocks())
337  components.resize(n_blocks());
338 
339  for (size_type i=0; i<n_blocks(); ++i)
340  components[i] = v.block(i);
341  }
342 
343 
344 
345  void
346  BlockVector::reinit (const size_type num_blocks)
347  {
348  std::vector<size_type> block_sizes (num_blocks, 0);
349  block_indices.reinit (block_sizes);
350  if (components.size() != n_blocks())
351  components.resize(n_blocks());
352 
353  for (size_type i=0; i<n_blocks(); ++i)
354  block(i).clear();
355 
356  collect_sizes();
357  }
358 
359 
360 
361  void
363  const bool omit_zeroing_entries)
364  {
366  if (components.size() != n_blocks())
367  components.resize(n_blocks());
368 
369  for (size_type i=0; i<n_blocks(); ++i)
370  components[i].reinit(v.block(i), omit_zeroing_entries);
371 
372  collect_sizes();
373  }
374 
375 
376 
377  BlockVector &
379  {
380  reinit (v);
381 
382  return *this;
383  }
384 
385 
386 
387  BlockVector &
389  {
390  if (n_blocks() != v.n_blocks())
391  {
392  std::vector<size_type> block_sizes (v.n_blocks(), 0);
393  block_indices.reinit (block_sizes);
394  if (components.size() != n_blocks())
395  components.resize(n_blocks());
396  }
397 
398  for (size_type i=0; i<this->n_blocks(); ++i)
399  this->components[i] = v.block(i);
400 
401  collect_sizes();
402 
403  return *this;
404  }
405 
406 
407 
408  void BlockVector::print (std::ostream &out,
409  const unsigned int precision,
410  const bool scientific,
411  const bool across) const
412  {
413  for (size_type i=0; i<this->n_blocks(); ++i)
414  {
415  if (across)
416  out << 'C' << i << ':';
417  else
418  out << "Component " << i << std::endl;
419  this->components[i].print(out, precision, scientific, across);
420  }
421  }
422 
423 }
424 
425 
426 DEAL_II_NAMESPACE_CLOSE
427 
428 #endif
unsigned int n_block_cols() const
std::size_t size() const
const BlockIndices & get_block_indices() const
#define Assert(cond, exc)
Definition: exceptions.h:313
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
void reinit(const unsigned int n_blocks, const size_type n_elements_per_block)
void reinit(const std::vector< Epetra_Map > &parallel_partitioning, const bool omit_zeroing_entries=false) 1
BlockVector & operator=(const value_type s)
std::vector< Vector > components
BlockVector & operator=(const value_type s)
unsigned int n_blocks() const
void reinit(const std::vector< Epetra_Map > &partitioning, const bool omit_zeroing_entries=false)
void import_nonlocal_data_for_fe(const TrilinosWrappers::BlockSparseMatrix &m, const BlockVector &v)
BlockType & block(const unsigned int row, const unsigned int column)
unsigned int n_block_rows() const
BlockType & block(const unsigned int i)
BlockVectorBase & operator=(const value_type s)
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const