Reference documentation for deal.II version GIT 2f5445400b 2023-02-05 22:30:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
petsc_parallel_block_vector.cc
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2004 - 2018 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.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
17 
18 #ifdef DEAL_II_WITH_PETSC
19 
21 
22 namespace PETScWrappers
23 {
24  namespace MPI
25  {
27 
29  {
30  PetscErrorCode ierr = VecDestroy(&petsc_nest_vector);
31  AssertThrow(ierr == 0, ExcPETScError(ierr));
32  }
33 
34  void
35  BlockVector::reinit(const unsigned int num_blocks)
36  {
37  std::vector<size_type> block_sizes(num_blocks, 0);
38  this->block_indices.reinit(block_sizes);
39  if (this->components.size() != this->n_blocks())
40  this->components.resize(this->n_blocks());
41 
42  for (unsigned int i = 0; i < this->n_blocks(); ++i)
43  components[i].reinit(MPI_COMM_SELF, 0, 0);
44 
45  collect_sizes();
46  }
47 
48  void
50  {
51  PetscBool isnest;
52 
53  PetscErrorCode ierr =
54  PetscObjectTypeCompare(reinterpret_cast<PetscObject>(v),
55  VECNEST,
56  &isnest);
57  AssertThrow(ierr == 0, ExcPETScError(ierr));
58  std::vector<Vec> sv;
59  if (isnest)
60  {
61  PetscInt nb;
62  ierr = VecNestGetSize(v, &nb);
63  AssertThrow(ierr == 0, ExcPETScError(ierr));
64  for (PetscInt i = 0; i < nb; ++i)
65  {
66  Vec vv;
67  ierr = VecNestGetSubVec(v, i, &vv);
68  sv.push_back(vv);
69  }
70  }
71  else
72  {
73  sv.push_back(v);
74  }
75 
76  auto nb = sv.size();
77 
78  std::vector<size_type> block_sizes(nb, 0);
79  this->block_indices.reinit(block_sizes);
80 
81  this->components.resize(nb);
82  for (unsigned int i = 0; i < nb; ++i)
83  {
84  this->components[i].reinit(sv[i]);
85  }
86 
87  this->collect_sizes();
88  }
89 
90  Vec &
92  {
93  return petsc_nest_vector;
94  }
95 
96  BlockVector::operator const Vec &() const
97  {
98  return petsc_nest_vector;
99  }
100 
101  void
103  {
105  setup_nest_vec();
106  }
107 
108  void
110  {
111  PetscErrorCode ierr = VecDestroy(&petsc_nest_vector);
112  AssertThrow(ierr == 0, ExcPETScError(ierr));
113 
114  auto n = this->n_blocks();
115 
116  std::vector<Vec> pcomponents(n);
117  for (unsigned int i = 0; i < n; i++)
118  pcomponents[i] = this->components[i].petsc_vector();
119 
120  MPI_Comm comm =
121  pcomponents.size() > 0 ?
122  PetscObjectComm(reinterpret_cast<PetscObject>(pcomponents[0])) :
123  PETSC_COMM_SELF;
124 
125  ierr =
126  VecCreateNest(comm, n, nullptr, pcomponents.data(), &petsc_nest_vector);
127  AssertThrow(ierr == 0, ExcPETScError(ierr));
128  }
129  } // namespace MPI
130 
131 } // namespace PETScWrappers
132 
134 
135 #endif // DEAL_II_WITH_PETSC
void reinit(const unsigned int n_blocks, const size_type n_elements_per_block)
unsigned int n_blocks() const
void collect_sizes()
std::vector< Vector > components
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:461
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:462
#define AssertThrow(cond, exc)
Definition: exceptions.h:1672
types::global_dof_index size_type
unsigned int global_dof_index
Definition: types.h:81
const MPI_Comm & comm