Debian Codegraph @

include/deal.II/meshworker/copy_data.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2019 - 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_meshworker_copy_data_h
16 #define dealii_meshworker_copy_data_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/ndarray.h>
21 #include <deal.II/base/types.h>
22
23 #include <deal.II/lac/full_matrix.h>
24 #include <deal.II/lac/vector.h>
25
26 #include <algorithm>
27 #include <vector>
28
29 DEAL_II_NAMESPACE_OPEN
30
31 namespace MeshWorker
32 {
33 /**
34 * Helper copy data struct.
35 *
36 * This class is a good default drop in CopyData object for the
37 * WorkStream::run() and MeshWorker::mesh_loop() functions.
38 *
39 * It arrays of (local) full matrices, vectors, and local degrees of freedom
40 * index vectors, with size determined by the corresponding template argument.
41 *
42 * In particular, you can specify the following template arguments
43 *
44 * - @tparam n_matrices: Size of the array of matrices
45 * - @tparam n_vectors: size of the array of vectors
46 * - @tparam n_dof_indices: size of the array of local dof indices
47 * - @tparam ScalarType: The data type stored by the vectors and matrices.
48 * This must be a real or complex floating point number.
49 */
50 template <int n_matrices = 1,
51 int n_vectors = n_matrices,
52 int n_dof_indices = n_matrices,
53 typename ScalarType = double>
54 struct CopyData
55 {
56 /**
57 * Default constructor. All of the @p matrices, @p vectors and
58 * @p local_dof_indices are empty, and should be initialized using
59 * one of the reinit() functions.
60 */
61 CopyData() = default;
62
63 /**
64 * Initialize everything with the same @p size. This is usually the number
65 * of local degrees of freedom.
66 */
67 explicit CopyData(const unsigned int size);
68
69 /**
70 * For every object, specify the size they should have.
71 */
72 explicit CopyData(
73 const ndarray<unsigned int, n_matrices, 2> &matrix_sizes,
74 const std::array<unsigned int, n_vectors> &vector_sizes,
75 const std::array<unsigned int, n_dof_indices> &dof_indices_sizes);
76
77 /**
78 * Copy constructor.
79 */
80 CopyData(const CopyData &other) = default;
81
82 /**
83 * Copy operator.
84 */
85 CopyData &
86 operator=(const CopyData &other) = default;
87
88 /**
89 * Reinitialize everything the same @p size. This is usually the number of
90 * local degrees of freedom.
91 */
92 void
93 reinit(const unsigned int size);
94
95 /**
96 * Reinitialize the @p index'th matrix, vector and local DoF index vector
97 * with the same @p size. This is usually the number of local degrees of
98 * freedom.
99 *
100 * @note In the event that there are a different number of @p matrices,
101 * @p vectors and @p local_dof_indices, this function will not throw an
102 * error when the index is over the size of one or two of these data
103 * structures. However, if the index exceeds the size of all three of them
104 * then an error will be thrown.
105 */
106 void
107 reinit(const unsigned int index, const unsigned int size);
108
109 /**
110 * Reinitialize the @p index'th matrix with @p size_rows `x` @p size_columns,
111 * and the vector and local DoF index vector with @p size_columns. These
112 * sizes usually correspond to some local degrees of freedom (e.g., the
113 * number of cell DoFs for @p size_rows and number of neighbor cell DoFs
114 * for @p size_columns).
115 *
116 * @note In the event that there are a different number of @p matrices,
117 * @p vectors and @p local_dof_indices, this function will not throw an
118 * error when the index is over the size of one or two of these data
119 * structures. However, if the index exceeds the size of all three of them
120 * then an error will be thrown.
121 */
122 void
123 reinit(const unsigned int index,
124 const unsigned int size_rows,
125 const unsigned int size_columns);
126
127 /**
128 * An array of local matrices.
129 */
130 std::array<FullMatrix<ScalarType>, n_matrices> matrices;
131
132 /**
133 * An array of local vectors.
134 */
135 std::array<Vector<ScalarType>, n_vectors> vectors;
136
137 /**
138 * An array of local degrees of freedom indices.
139 */
140 std::array<std::vector<types::global_dof_index>, n_dof_indices>
141 local_dof_indices;
142 };
143
144
145 #ifndef DOXYGEN
146 //
147 // Template definitions
148 //
149 template <int n_matrices,
150 int n_vectors,
151 int n_dof_indices,
152 typename ScalarType>
153 CopyData<n_matrices, n_vectors, n_dof_indices, ScalarType>::CopyData(
154 const unsigned int size)
155 {
156 reinit(size);
157 }
158
159
160
161 template <int n_matrices,
162 int n_vectors,
163 int n_dof_indices,
164 typename ScalarType>
165 CopyData<n_matrices, n_vectors, n_dof_indices, ScalarType>::CopyData(
166 const ndarray<unsigned int, n_matrices, 2> &matrix_sizes,
167 const std::array<unsigned int, n_vectors> &vector_sizes,
168 const std::array<unsigned int, n_dof_indices> &dof_indices_sizes)
169 {
170 for (unsigned int i = 0; i < n_matrices; ++i)
171 matrices[i].reinit(matrix_sizes[i++]);
172
173 for (unsigned int i = 0; i < n_vectors; ++i)
174 vectors[i].reinit(vector_sizes[i++]);
175
176 for (unsigned int i = 0; i < n_dof_indices; ++i)
177 local_dof_indices[i].resize(dof_indices_sizes[i++]);
178 }
179
180
181
182 template <int n_matrices,
183 int n_vectors,
184 int n_dof_indices,
185 typename ScalarType>
186 void
187 CopyData<n_matrices, n_vectors, n_dof_indices, ScalarType>::reinit(
188 const unsigned int size)
189 {
190 for (auto &m : matrices)
191 m.reinit({size, size});
192 for (auto &v : vectors)
193 v.reinit(size);
194 for (auto &d : local_dof_indices)
195 d.resize(size);
196 }
197
198
199
200 template <int n_matrices,
201 int n_vectors,
202 int n_dof_indices,
203 typename ScalarType>
204 void
205 CopyData<n_matrices, n_vectors, n_dof_indices, ScalarType>::reinit(
206 const unsigned int index,
207 const unsigned int size)
208 {
209 reinit(index, size, size);
210 }
211
212
213
214 template <int n_matrices,
215 int n_vectors,
216 int n_dof_indices,
217 typename ScalarType>
218 void
219 CopyData<n_matrices, n_vectors, n_dof_indices, ScalarType>::reinit(
220 const unsigned int index,
221 const unsigned int size_rows,
222 const unsigned int size_columns)
223 {
224 // We permit different numbers of matrices, vectors and DoF index vectors.
225 // So we have to be a bit permissive here.
226 constexpr int max_index = std::max({n_matrices, n_vectors, n_dof_indices});
227 Assert(index < max_index, ExcIndexRange(index, 0, max_index));
228
229 if (index < n_matrices)
230 matrices[index].reinit({size_rows, size_columns});
231
232 if (index < n_vectors)
233 vectors[index].reinit(size_columns);
234
235 if (index < n_dof_indices)
236 local_dof_indices[index].resize(size_columns);
237 }
238
239 #endif // DOXYGEN
240 } // namespace MeshWorker
241
242 DEAL_II_NAMESPACE_CLOSE
243
244 #endif