Debian Codegraph @

include/deal.II/meshworker/functional.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2011 - 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
16 #ifndef dealii_mesh_worker_functional_h
17 #define dealii_mesh_worker_functional_h
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/algorithms/any_data.h>
22
23 #include <deal.II/base/mg_level_object.h>
24 #include <deal.II/base/observer_pointer.h>
25
26 #include <deal.II/lac/block_vector.h>
27
28 #include <deal.II/meshworker/dof_info.h>
29
30 #include <deal.II/multigrid/mg_constrained_dofs.h>
31
32
33 DEAL_II_NAMESPACE_OPEN
34
35 namespace MeshWorker
36 {
37 namespace Assembler
38 {
39 /**
40 * The class assembling local contributions to a functional into global
41 * functionals.
42 *
43 *
44 *
45 * @ingroup MeshWorker
46 */
47 template <typename number = double>
48 class Functional
49 {
50 public:
51 /**
52 * Initialize local data to store functionals. The number <tt>n</tt> is
53 * the number of functionals to be computed.
54 */
55 void
56 initialize(const unsigned int n);
57 /**
58 * Initialize the local data in the DoFInfo object used later for
59 * assembling.
60 *
61 * The @p info object refers to a cell if <code>!face</code>, or else to an
62 * interior or boundary face.
63 */
64 template <class DOFINFO>
65 void
66 initialize_info(DOFINFO &info, bool face);
67
68 /**
69 * Assemble the local values into the global vectors.
70 */
71 template <class DOFINFO>
72 void
73 assemble(const DOFINFO &info);
74
75 /**
76 * Assemble both local values into the global vectors.
77 */
78 template <class DOFINFO>
79 void
80 assemble(const DOFINFO &info1, const DOFINFO &info2);
81
82 /**
83 * The value of the ith entry in #results.
84 */
85 number
86 operator()(const unsigned int i) const;
87
88 private:
89 /**
90 * The values into which the results are added.
91 */
92 std::vector<double> results;
93 };
94
95 /**
96 * Compute cell and face contributions of one or several functionals,
97 * typically for error estimates. The information in which component the
98 * result is stored for a given cell or face is transmitted by its
99 * user_index variable. Hence, you need to make sure to set these variables
100 * appropriately before using this class.
101 *
102 * @ingroup MeshWorker
103 */
104 template <typename number = double>
105 class CellsAndFaces
106 {
107 public:
108 /**
109 * Constructor. Initialize the member variables.
110 */
111 CellsAndFaces();
112
113 /**
114 * The initialization function, specifying the @p results vectors and
115 * whether face data should be collected separately.
116 *
117 * @p results should contain two block vectors named "cells" and "faces"
118 * (the latter only if @p separate_faces is true). In each of the two,
119 * each block should have equal size and be large enough to accommodate
120 * all user indices set in the cells and faces covered by the loop it is
121 * used in. Typically, for estimators, this is
122 * Triangulation::n_active_cells() and Triangulation::n_faces(),
123 * respectively.
124 *
125 * The use of BlockVector may seem cumbersome, but it allows us to
126 * assemble several functionals at the same time, one in each block. The
127 * typical situation for error estimate is just having a single block in
128 * each vector.
129 */
130 void
131 initialize(AnyData &results, bool separate_faces = true);
132
133 /**
134 * Initialize the local data in the DoFInfo object used later for
135 * assembling.
136 *
137 * The @p info object refers to a cell if <code>!face</code>, or else to an
138 * interior or boundary face.
139 */
140 template <class DOFINFO>
141 void
142 initialize_info(DOFINFO &info, bool face) const;
143
144 /**
145 * Assemble the local values into the global vectors.
146 */
147 template <class DOFINFO>
148 void
149 assemble(const DOFINFO &info);
150
151 /**
152 * Assemble both local values into the global vectors.
153 */
154 template <class DOFINFO>
155 void
156 assemble(const DOFINFO &info1, const DOFINFO &info2);
157
158 /**
159 * The value of the ith entry in @p results.
160 */
161 number
162 operator()(const unsigned int i) const;
163
164 private:
165 AnyData results;
166 bool separate_faces;
167 };
168 //----------------------------------------------------------------------//
169
170 template <typename number>
171 inline void
172 Functional<number>::initialize(const unsigned int n)
173 {
174 results.resize(n);
175 std::fill(results.begin(), results.end(), 0.);
176 }
177
178
179 template <typename number>
180 template <class DOFINFO>
181 inline void
182 Functional<number>::initialize_info(DOFINFO &info, bool)
183 {
184 info.initialize_numbers(results.size());
185 }
186
187
188 template <typename number>
189 template <class DOFINFO>
190 inline void
191 Functional<number>::assemble(const DOFINFO &info)
192 {
193 for (unsigned int i = 0; i < results.size(); ++i)
194 results[i] += info.value(i);
195 }
196
197
198 template <typename number>
199 template <class DOFINFO>
200 inline void
201 Functional<number>::assemble(const DOFINFO &info1, const DOFINFO &info2)
202 {
203 for (unsigned int i = 0; i < results.size(); ++i)
204 {
205 results[i] += info1.value(i);
206 results[i] += info2.value(i);
207 }
208 }
209
210
211 template <typename number>
212 inline number
213 Functional<number>::operator()(const unsigned int i) const
214 {
215 AssertIndexRange(i, results.size());
216 return results[i];
217 }
218
219 //----------------------------------------------------------------------//
220
221 template <typename number>
222 inline CellsAndFaces<number>::CellsAndFaces()
223 : separate_faces(true)
224 {}
225
226
227
228 template <typename number>
229 inline void
230 CellsAndFaces<number>::initialize(AnyData &r, bool sep)
231 {
232 Assert(r.name(0) == "cells", AnyData::ExcNameMismatch(0, "cells"));
233 if (sep)
234 {
235 Assert(r.name(1) == "faces", AnyData::ExcNameMismatch(1, "faces"));
236 AssertDimension(r.entry<BlockVector<double> *>(0)->n_blocks(),
237 r.entry<BlockVector<double> *>(1)->n_blocks());
238 }
239
240 results = r;
241 separate_faces = sep;
242 }
243
244 template <typename number>
245 template <class DOFINFO>
246 inline void
247 CellsAndFaces<number>::initialize_info(DOFINFO &info, bool) const
248 {
249 info.initialize_numbers(
250 results.entry<BlockVector<double> *>(0)->n_blocks());
251 }
252
253
254 template <typename number>
255 template <class DOFINFO>
256 inline void
257 CellsAndFaces<number>::assemble(const DOFINFO &info)
258 {
259 BlockVector<double> *v;
260 if (separate_faces && info.face_number != numbers::invalid_unsigned_int)
261 v = results.entry<BlockVector<double> *>(1);
262 else
263 v = results.entry<BlockVector<double> *>(0);
264
265 for (unsigned int i = 0; i < info.n_values(); ++i)
266 v->block(i)(info.cell->user_index()) += info.value(i);
267 }
268
269
270 template <typename number>
271 template <class DOFINFO>
272 inline void
273 CellsAndFaces<number>::assemble(const DOFINFO &info1, const DOFINFO &info2)
274 {
275 for (unsigned int i = 0; i < info1.n_values(); ++i)
276 {
277 if (separate_faces)
278 {
279 BlockVector<double> *v1 = results.entry<BlockVector<double> *>(1);
280 const double J = info1.value(i) + info2.value(i);
281 v1->block(i)(info1.face->user_index()) += J;
282 if (info2.face != info1.face)
283 v1->block(i)(info2.face->user_index()) += J;
284 }
285 else
286 {
287 BlockVector<double> *v0 = results.entry<BlockVector<double> *>(0);
288 v0->block(i)(info1.cell->user_index()) += .5 * info1.value(i);
289 v0->block(i)(info2.cell->user_index()) += .5 * info2.value(i);
290 }
291 }
292 }
293 } // namespace Assembler
294 } // namespace MeshWorker
295
296 DEAL_II_NAMESPACE_CLOSE
297
298 #endif