deal.II version GIT relicensing-6842-g793a97d2aa 2026-10-02 14:00:01+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\}}\)
Loading...
Searching...
No Matches
petsc_parallel_block_vector.cc
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2004 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
14
15#ifdef DEAL_II_WITH_PETSC
16
18
19
20#endif // DEAL_II_WITH_PETSC
21
23
24#ifdef DEAL_II_WITH_PETSC
25
26namespace PETScWrappers
27{
28 namespace MPI
29 {
30 // Check that the class we declare here satisfies the
31 // vector-space-vector concept. If we catch it here,
32 // any mistake in the vector class declaration would
33 // show up in uses of this class later on as well.
34# ifdef DEAL_II_HAVE_CXX20
36# endif
37
39
41 {
42 PetscErrorCode ierr = VecDestroy(&petsc_nest_vector);
43 AssertThrow(ierr == 0, ExcPETScError(ierr));
44 }
45
46 void
47 BlockVector::reinit(const unsigned int num_blocks)
48 {
49 std::vector<size_type> block_sizes(num_blocks, 0);
50 this->block_indices.reinit(block_sizes);
51 if (this->components.size() != this->n_blocks())
52 this->components.resize(this->n_blocks());
53
54 for (unsigned int i = 0; i < this->n_blocks(); ++i)
55 components[i].reinit(MPI_COMM_SELF, 0, 0);
56
58 }
59
60 void
62 {
63 PetscBool isnest;
64
65 PetscErrorCode ierr =
66 PetscObjectTypeCompare(reinterpret_cast<PetscObject>(v),
67 VECNEST,
68 &isnest);
69 AssertThrow(ierr == 0, ExcPETScError(ierr));
70 std::vector<Vec> sv;
71 if (isnest)
72 {
73 PetscInt nb;
74 ierr = VecNestGetSize(v, &nb);
75 AssertThrow(ierr == 0, ExcPETScError(ierr));
76 for (PetscInt i = 0; i < nb; ++i)
77 {
78 Vec vv;
79 ierr = VecNestGetSubVec(v, i, &vv);
80 sv.push_back(vv);
81 }
82 }
83 else
84 {
85 sv.push_back(v);
86 }
87
88 auto nb = sv.size();
89
90 std::vector<size_type> block_sizes(nb, 0);
91 this->block_indices.reinit(block_sizes);
92
93 this->components.resize(nb);
94 for (unsigned int i = 0; i < nb; ++i)
95 {
96 this->components[i].reinit(sv[i]);
97 }
98
100 if (!isnest)
102 else
103 {
104 ierr = PetscObjectReference(reinterpret_cast<PetscObject>(v));
105 AssertThrow(ierr == 0, ExcPETScError(ierr));
106 PetscErrorCode ierr = VecDestroy(&petsc_nest_vector);
107 AssertThrow(ierr == 0, ExcPETScError(ierr));
109 }
110 }
111
112 Vec &
117
118 BlockVector::operator const Vec &() const
119 {
120 return petsc_nest_vector;
121 }
122
123 void
129
130 void
136
137 void
139 {
140 PetscErrorCode ierr = VecDestroy(&petsc_nest_vector);
141 AssertThrow(ierr == 0, ExcPETScError(ierr));
142
143 auto n = this->n_blocks();
144
145 std::vector<Vec> pcomponents(n);
146 for (unsigned int i = 0; i < n; i++)
147 pcomponents[i] = this->components[i].petsc_vector();
148
149 MPI_Comm comm =
150 pcomponents.size() > 0 ?
151 PetscObjectComm(reinterpret_cast<PetscObject>(pcomponents[0])) :
152 PETSC_COMM_SELF;
153
154 ierr =
155 VecCreateNest(comm, n, nullptr, pcomponents.data(), &petsc_nest_vector);
156 AssertThrow(ierr == 0, ExcPETScError(ierr));
157 }
158 } // namespace MPI
159
160} // namespace PETScWrappers
161
162
163#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 compress(VectorOperation::values operation)
void collect_sizes()
std::vector< Vector > components
void compress(VectorOperation::values operation)
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define AssertThrow(cond, exc)
const MPI_Comm comm
Definition mpi.cc:912
void petsc_increment_state_counter(Vec v)
unsigned int global_dof_index
Definition types.h:92