Debian Codegraph @

include/deal.II/lac/trilinos_tpetra_block_vector.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2024 - 2025 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_trilinos_tpetra_block_vector_templates_h
16 #define dealii_trilinos_tpetra_block_vector_templates_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/lac/trilinos_tpetra_block_vector.h>
21
22 #ifdef DEAL_II_TRILINOS_WITH_TPETRA
23
24 # include <deal.II/base/index_set.h>
25 # include <deal.II/base/trilinos_utilities.h>
26
27 DEAL_II_NAMESPACE_OPEN
28
29 namespace LinearAlgebra
30 {
31 namespace TpetraWrappers
32 {
33 template <typename Number, typename MemorySpace>
34 BlockVector<Number, MemorySpace>::BlockVector(
35 const std::vector<IndexSet> &parallel_partitioning,
36 const MPI_Comm communicator)
37 {
38 reinit(parallel_partitioning, communicator, false);
39 }
40
41
42
43 template <typename Number, typename MemorySpace>
44 BlockVector<Number, MemorySpace>::BlockVector(
45 const std::vector<IndexSet> &parallel_partitioning,
46 const std::vector<IndexSet> &ghost_values,
47 const MPI_Comm communicator,
48 const bool vector_writable)
49 {
50 reinit(parallel_partitioning,
51 ghost_values,
52 communicator,
53 vector_writable);
54 }
55
56
57
58 template <typename Number, typename MemorySpace>
59 BlockVector<Number, MemorySpace>::BlockVector(
60 const BlockVector<Number, MemorySpace> &v)
61 : dealii::BlockVectorBase<TpetraWrappers::Vector<Number, MemorySpace>>()
62 {
63 this->block_indices = v.block_indices;
64
65 this->components.resize(this->n_blocks());
66 for (unsigned int i = 0; i < this->n_blocks(); ++i)
67 this->components[i] = v.components[i];
68
69 this->collect_sizes();
70 }
71
72
73
74 template <typename Number, typename MemorySpace>
75 BlockVector<Number, MemorySpace>::BlockVector(const size_type num_blocks)
76 : dealii::BlockVectorBase<TpetraWrappers::Vector<Number, MemorySpace>>()
77 {
78 reinit(num_blocks);
79 }
80
81
82
83 template <typename Number, typename MemorySpace>
84 BlockVector<Number, MemorySpace> &
85 BlockVector<Number, MemorySpace>::operator=(const Number s)
86 {
87 BaseClass::operator=(s);
88 return *this;
89 }
90
91
92
93 template <typename Number, typename MemorySpace>
94 BlockVector<Number, MemorySpace> &
95 BlockVector<Number, MemorySpace>::operator=(const BlockVector &v)
96 {
97 // we only allow assignment to vectors with the same number of blocks
98 // or to an empty BlockVector
99 Assert(this->n_blocks() == 0 || this->n_blocks() == v.n_blocks(),
100 ExcDimensionMismatch(this->n_blocks(), v.n_blocks()));
101
102 if (this->n_blocks() != v.n_blocks())
103 this->block_indices = v.block_indices;
104
105 this->components.resize(this->n_blocks());
106 for (unsigned int i = 0; i < this->n_blocks(); ++i)
107 this->components[i] = v.components[i];
108
109 this->collect_sizes();
110
111 return *this;
112 }
113
114
115
116 template <typename Number, typename MemorySpace>
117 void
118 BlockVector<Number, MemorySpace>::reinit(
119 const std::vector<IndexSet> &parallel_partitioning,
120 const MPI_Comm communicator,
121 const bool omit_zeroing_entries)
122 {
123 // update the number of blocks
124 this->block_indices.reinit(parallel_partitioning.size(), 0);
125
126 // initialize each block
127 this->components.resize(this->n_blocks());
128 for (unsigned int i = 0; i < this->n_blocks(); ++i)
129 this->components[i].reinit(parallel_partitioning[i],
130 communicator,
131 omit_zeroing_entries);
132
133 // update block_indices content
134 this->collect_sizes();
135 }
136
137
138
139 template <typename Number, typename MemorySpace>
140 void
141 BlockVector<Number, MemorySpace>::reinit(
142 const std::vector<IndexSet> &parallel_partitioning,
143 const std::vector<IndexSet> &ghost_values,
144 const MPI_Comm communicator,
145 const bool vector_writable)
146 {
147 AssertDimension(parallel_partitioning.size(), ghost_values.size());
148
149 // update the number of blocks
150 this->block_indices.reinit(parallel_partitioning.size(), 0);
151
152 // initialize each block
153 this->components.resize(this->n_blocks());
154 for (unsigned int i = 0; i < this->n_blocks(); ++i)
155 this->components[i].reinit(parallel_partitioning[i],
156 ghost_values[i],
157 communicator,
158 vector_writable);
159
160 // update block_indices content
161 this->collect_sizes();
162 }
163
164
165
166 template <typename Number, typename MemorySpace>
167 void
168 BlockVector<Number, MemorySpace>::reinit(
169 const BlockVector<Number, MemorySpace> &v,
170 const bool omit_zeroing_entries)
171 {
172 if (this->n_blocks() != v.n_blocks())
173 this->block_indices = v.get_block_indices();
174
175 this->components.resize(this->n_blocks());
176 for (unsigned int i = 0; i < this->n_blocks(); ++i)
177 this->components[i].reinit(v.components[i], omit_zeroing_entries);
178
179 this->collect_sizes();
180 }
181
182
183
184 template <typename Number, typename MemorySpace>
185 void
186 BlockVector<Number, MemorySpace>::reinit(const size_type num_blocks)
187 {
188 this->block_indices.reinit(num_blocks, 0);
189
190 this->components.resize(this->n_blocks());
191 for (auto &c : this->components)
192 c.clear();
193 }
194
195
196
197 template <typename Number, typename MemorySpace>
198 bool
199 BlockVector<Number, MemorySpace>::has_ghost_elements() const
200 {
201 bool ghosted = this->block(0).has_ghost_elements();
202 if constexpr (running_in_debug_mode())
203 {
204 for (unsigned int i = 0; i < this->n_blocks(); ++i)
205 Assert(this->block(i).has_ghost_elements() == ghosted,
206 ExcInternalError());
207 }
208 return ghosted;
209 }
210
211
212
213 template <typename Number, typename MemorySpace>
214 void
215 BlockVector<Number, MemorySpace>::print(std::ostream &out,
216 const unsigned int precision,
217 const bool scientific,
218 const bool across) const
219 {
220 for (unsigned int i = 0; i < this->n_blocks(); ++i)
221 {
222 if (across)
223 out << 'C' << i << ':';
224 else
225 out << "Component " << i << std::endl;
226 this->components[i].print(out, precision, scientific, across);
227 }
228 }
229
230 } // namespace TpetraWrappers
231 } // namespace LinearAlgebra
232
233 DEAL_II_NAMESPACE_CLOSE
234
235 #else
236
237 // Make sure the scripts that create the C++20 module input files have
238 // something to latch on if the preprocessor #ifdef above would
239 // otherwise lead to an empty content of the file.
240 DEAL_II_NAMESPACE_OPEN
241 DEAL_II_NAMESPACE_CLOSE
242
243 #endif // DEAL_II_TRILINOS_WITH_TPETRA
244
245 #endif // dealii_trilinos_tpetra_block_vector_templates_h