00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __deal2__petsc_block_vector_h
00013 #define __deal2__petsc_block_vector_h
00014
00015
00016 #include <deal.II/base/config.h>
00017
00018 #ifdef DEAL_II_USE_PETSC
00019
00020 # include <deal.II/lac/petsc_vector.h>
00021 # include <deal.II/lac/petsc_parallel_block_vector.h>
00022 # include <deal.II/lac/block_indices.h>
00023 # include <deal.II/lac/block_vector_base.h>
00024 # include <deal.II/lac/exceptions.h>
00025
00026 DEAL_II_NAMESPACE_OPEN
00027
00028
00029
00030 namespace PETScWrappers
00031 {
00046 class BlockVector : public BlockVectorBase<Vector>
00047 {
00048 public:
00053 typedef BlockVectorBase<Vector> BaseClass;
00054
00059 typedef BaseClass::BlockType BlockType;
00060
00065 typedef BaseClass::value_type value_type;
00066 typedef BaseClass::pointer pointer;
00067 typedef BaseClass::const_pointer const_pointer;
00068 typedef BaseClass::reference reference;
00069 typedef BaseClass::const_reference const_reference;
00070 typedef BaseClass::size_type size_type;
00071 typedef BaseClass::iterator iterator;
00072 typedef BaseClass::const_iterator const_iterator;
00073
00093 explicit BlockVector (const unsigned int num_blocks = 0,
00094 const unsigned int block_size = 0);
00095
00101 BlockVector (const BlockVector &V);
00102
00118 explicit BlockVector (const MPI::BlockVector &v);
00119
00126 BlockVector (const std::vector<unsigned int> &n);
00127
00146 template <typename InputIterator>
00147 BlockVector (const std::vector<unsigned int> &n,
00148 const InputIterator first,
00149 const InputIterator end);
00150
00154 ~BlockVector ();
00155
00161 BlockVector & operator = (const value_type s);
00162
00167 BlockVector &
00168 operator= (const BlockVector &V);
00169
00183 BlockVector &
00184 operator = (const MPI::BlockVector &v);
00185
00194 void reinit (const unsigned int num_blocks,
00195 const unsigned int block_size,
00196 const bool fast = false);
00197
00230 void reinit (const std::vector<unsigned int> &N,
00231 const bool fast=false);
00232
00259 void reinit (const BlockVector &V,
00260 const bool fast=false);
00261
00292 void swap (BlockVector &v);
00293
00297 void print (std::ostream &out,
00298 const unsigned int precision = 3,
00299 const bool scientific = true,
00300 const bool across = true) const;
00301
00308 DeclException0 (ExcIteratorRangeDoesNotMatchVectorSize);
00310 };
00311
00314
00315
00316
00317
00318 inline
00319 BlockVector::BlockVector (const unsigned int n_blocks,
00320 const unsigned int block_size)
00321 {
00322 reinit (n_blocks, block_size);
00323 }
00324
00325
00326
00327 inline
00328 BlockVector::BlockVector (const std::vector<unsigned int> &n)
00329 {
00330 reinit (n, false);
00331 }
00332
00333
00334 inline
00335 BlockVector::BlockVector (const BlockVector& v)
00336 :
00337 BlockVectorBase<Vector > ()
00338 {
00339 this->components.resize (v.n_blocks());
00340 block_indices = v.block_indices;
00341
00342 for (unsigned int i=0; i<this->n_blocks(); ++i)
00343 this->components[i] = v.components[i];
00344 }
00345
00346
00347
00348 inline
00349 BlockVector::BlockVector (const MPI::BlockVector& v)
00350 :
00351 BlockVectorBase<Vector > ()
00352 {
00353 this->components.resize (v.get_block_indices().size());
00354 block_indices = v.get_block_indices();
00355
00356 for (unsigned int i=0; i<this->n_blocks(); ++i)
00357 this->components[i] = v.block(i);
00358 }
00359
00360
00361
00362 template <typename InputIterator>
00363 BlockVector::BlockVector (const std::vector<unsigned int> &n,
00364 const InputIterator first,
00365 const InputIterator end)
00366 {
00367
00368
00369
00370 reinit (n, true);
00371 InputIterator start = first;
00372 for (unsigned int b=0; b<n.size(); ++b)
00373 {
00374 InputIterator end = start;
00375 std::advance (end, static_cast<signed int>(n[b]));
00376
00377 for (unsigned int i=0; i<n[b]; ++i, ++start)
00378 this->block(b)(i) = *start;
00379 }
00380 Assert (start == end, ExcIteratorRangeDoesNotMatchVectorSize());
00381 }
00382
00383
00384
00385 inline
00386 BlockVector &
00387 BlockVector::operator = (const value_type s)
00388 {
00389 BaseClass::operator = (s);
00390 return *this;
00391 }
00392
00393
00394
00395 inline
00396 BlockVector &
00397 BlockVector::operator = (const BlockVector &v)
00398 {
00399 BaseClass::operator = (v);
00400 return *this;
00401 }
00402
00403
00404
00405 inline
00406 BlockVector &
00407 BlockVector::operator = (const MPI::BlockVector &v)
00408 {
00409 BaseClass::operator = (v);
00410 return *this;
00411 }
00412
00413
00414
00415 inline
00416 BlockVector::~BlockVector ()
00417 {}
00418
00419
00420 inline
00421 void
00422 BlockVector::reinit (const unsigned int n_bl,
00423 const unsigned int bl_sz,
00424 const bool fast)
00425 {
00426 std::vector<unsigned int> n(n_bl, bl_sz);
00427 reinit(n, fast);
00428 }
00429
00430
00431
00432 inline
00433 void
00434 BlockVector::reinit (const std::vector<unsigned int> &n,
00435 const bool fast)
00436 {
00437 block_indices.reinit (n);
00438 if (this->components.size() != this->n_blocks())
00439 this->components.resize(this->n_blocks());
00440
00441 for (unsigned int i=0; i<this->n_blocks(); ++i)
00442 this->components[i].reinit(n[i], fast);
00443 }
00444
00445
00446 inline
00447 void
00448 BlockVector::reinit (const BlockVector& v,
00449 const bool fast)
00450 {
00451 block_indices = v.get_block_indices();
00452 if (this->components.size() != this->n_blocks())
00453 this->components.resize(this->n_blocks());
00454
00455 for (unsigned int i=0;i<this->n_blocks();++i)
00456 block(i).reinit(v.block(i), fast);
00457 }
00458
00459
00460
00461 inline
00462 void
00463 BlockVector::swap (BlockVector &v)
00464 {
00465 Assert (this->n_blocks() == v.n_blocks(),
00466 ExcDimensionMismatch(this->n_blocks(), v.n_blocks()));
00467
00468 for (unsigned int i=0; i<this->n_blocks(); ++i)
00469 this->components[i].swap (v.components[i]);
00470 ::swap (this->block_indices, v.block_indices);
00471 }
00472
00473
00474
00475 inline
00476 void
00477 BlockVector::print (std::ostream &out,
00478 const unsigned int precision,
00479 const bool scientific,
00480 const bool across) const
00481 {
00482 for (unsigned int i=0;i<this->n_blocks();++i)
00483 {
00484 if (across)
00485 out << 'C' << i << ':';
00486 else
00487 out << "Component " << i << std::endl;
00488 this->components[i].print(out, precision, scientific, across);
00489 }
00490 }
00491
00492
00493
00494
00503 inline
00504 void swap (BlockVector &u,
00505 BlockVector &v)
00506 {
00507 u.swap (v);
00508 }
00509
00510 }
00511
00512
00513 DEAL_II_NAMESPACE_CLOSE
00514
00515 #endif // DEAL_II_USE_PETSC
00516
00517 #endif
00518