Debian Codegraph @

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

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 1999 - 2024 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_block_vector_templates_h
16 #define dealii_block_vector_templates_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/lac/block_vector.h>
22 #include <deal.II/lac/trilinos_parallel_block_vector.h>
23
24 #include <algorithm>
25 #include <cmath>
26
27 DEAL_II_NAMESPACE_OPEN
28
29 template <typename Number>
30 BlockVector<Number>::BlockVector(const unsigned int n_blocks,
31 const size_type block_size)
32 {
33 reinit(n_blocks, block_size);
34 }
35
36
37
38 template <typename Number>
39 BlockVector<Number>::BlockVector(const std::vector<size_type> &block_sizes)
40 {
41 reinit(block_sizes, false);
42 }
43
44
45 template <typename Number>
46 BlockVector<Number>::BlockVector(const BlockIndices &n)
47 {
48 reinit(n, false);
49 }
50
51
52 template <typename Number>
53 BlockVector<Number>::BlockVector(const BlockVector<Number> &v)
54 : BlockVectorBase<Vector<Number>>()
55 {
56 this->components.resize(v.n_blocks());
57 this->block_indices = v.block_indices;
58
59 for (size_type i = 0; i < this->n_blocks(); ++i)
60 this->components[i] = v.components[i];
61 }
62
63
64
65 template <typename Number>
66 template <typename OtherNumber>
67 BlockVector<Number>::BlockVector(const BlockVector<OtherNumber> &v)
68 {
69 reinit(v, true);
70 *this = v;
71 }
72
73
74
75 #ifdef DEAL_II_WITH_TRILINOS
76
77 template <typename Number>
78 BlockVector<Number>::BlockVector(const TrilinosWrappers::MPI::BlockVector &v)
79 {
80 this->block_indices = v.get_block_indices();
81 this->components.resize(this->n_blocks());
82
83 for (size_type i = 0; i < this->n_blocks(); ++i)
84 this->components[i] = v.block(i);
85
86 BaseClass::collect_sizes();
87 }
88
89 #endif
90
91
92 template <typename Number>
93 void
94 BlockVector<Number>::reinit(const unsigned int n_blocks,
95 const size_type block_size,
96 const bool omit_zeroing_entries)
97 {
98 std::vector<size_type> block_sizes(n_blocks, block_size);
99 reinit(block_sizes, omit_zeroing_entries);
100 }
101
102
103 template <typename Number>
104 void
105 BlockVector<Number>::reinit(const std::vector<size_type> &block_sizes,
106 const bool omit_zeroing_entries)
107 {
108 this->block_indices.reinit(block_sizes);
109 if (this->components.size() != this->n_blocks())
110 this->components.resize(this->n_blocks());
111
112 for (size_type i = 0; i < this->n_blocks(); ++i)
113 this->components[i].reinit(block_sizes[i], omit_zeroing_entries);
114 }
115
116
117 template <typename Number>
118 void
119 BlockVector<Number>::reinit(const BlockIndices &n,
120 const bool omit_zeroing_entries)
121 {
122 this->block_indices = n;
123 if (this->components.size() != this->n_blocks())
124 this->components.resize(this->n_blocks());
125
126 for (size_type i = 0; i < this->n_blocks(); ++i)
127 this->components[i].reinit(n.block_size(i), omit_zeroing_entries);
128 }
129
130
131 template <typename Number>
132 template <typename Number2>
133 void
134 BlockVector<Number>::reinit(const BlockVector<Number2> &v,
135 const bool omit_zeroing_entries)
136 {
137 this->block_indices = v.get_block_indices();
138 if (this->components.size() != this->n_blocks())
139 this->components.resize(this->n_blocks());
140
141 for (size_type i = 0; i < this->n_blocks(); ++i)
142 this->block(i).reinit(v.block(i), omit_zeroing_entries);
143 }
144
145
146
147 #ifdef DEAL_II_WITH_TRILINOS
148 template <typename Number>
149 inline BlockVector<Number> &
150 BlockVector<Number>::operator=(const TrilinosWrappers::MPI::BlockVector &v)
151 {
152 BaseClass::operator=(v);
153 return *this;
154 }
155 #endif
156
157
158 template <typename Number>
159 void
160 BlockVector<Number>::swap(BlockVector<Number> &v) noexcept
161 {
162 std::swap(this->components, v.components);
163
164 dealii::swap(this->block_indices, v.block_indices);
165 }
166
167
168
169 template <typename Number>
170 void
171 BlockVector<Number>::print(std::ostream &out,
172 const unsigned int precision,
173 const bool scientific,
174 const bool across) const
175 {
176 for (size_type i = 0; i < this->n_blocks(); ++i)
177 {
178 if (across)
179 out << 'C' << i << ':';
180 else
181 out << "Component " << i << std::endl;
182 this->components[i].print(out, precision, scientific, across);
183 }
184 }
185
186
187
188 template <typename Number>
189 void
190 BlockVector<Number>::block_write(std::ostream &out) const
191 {
192 for (size_type i = 0; i < this->n_blocks(); ++i)
193 this->components[i].block_write(out);
194 }
195
196
197
198 template <typename Number>
199 void
200 BlockVector<Number>::block_read(std::istream &in)
201 {
202 for (size_type i = 0; i < this->n_blocks(); ++i)
203 this->components[i].block_read(in);
204 }
205
206
207
208 DEAL_II_NAMESPACE_CLOSE
209
210 #endif