Debian Codegraph @

include/deal.II/meshworker/dof_info.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2009 - 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_dof_info_h
17 #define dealii_mesh_worker_dof_info_h
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/quadrature_lib.h>
22
23 #include <deal.II/dofs/block_info.h>
24
25 #include <deal.II/fe/fe_values.h>
26
27 #include <deal.II/meshworker/local_results.h>
28 #include <deal.II/meshworker/vector_selector.h>
29
30 #include <memory>
31
32 DEAL_II_NAMESPACE_OPEN
33
34 namespace MeshWorker
35 {
36 // Forward declaration
37 #ifndef DOXYGEN
38 template <int dim, class DOFINFO>
39 class DoFInfoBox;
40 #endif
41
42 /**
43 * A class containing information on geometry and degrees of freedom of a
44 * mesh object.
45 *
46 * The information in these objects is usually used by one of the Assembler
47 * classes. It is also the kind of information which is needed in mesh based
48 * matrices (often referred to as matrix free methods).
49 *
50 * In addition to the information on degrees of freedom stored in this
51 * class, it also provides the local computation space for the worker object
52 * operating on it in LocalResults. This base class will automatically be
53 * reinitialized on each cell, but initial setup is up to the user and
54 * should be done when initialize() for this class is called.
55 *
56 * This class operates in two different modes, corresponding to the data
57 * models discussed in the Assembler namespace documentation.
58 *
59 * The choice of the local data model is triggered by the vector
60 * BlockInfo::local_renumbering, which in turn is usually filled by
61 * BlockInfo::initialize_local(). If this function has been used, or the
62 * vector has been changed from zero-length, then local dof indices stored
63 * in this object will automatically be renumbered to reflect local block
64 * structure. This means, the first entries in @p indices will refer to the
65 * first block of the system, then comes the second block and so on.
66 *
67 * The BlockInfo object is stored as a pointer. Therefore, if the block
68 * structure changes, for instance because of mesh refinement, the DoFInfo
69 * class will automatically use the new structures.
70 *
71 * @ingroup MeshWorker
72 */
73 template <int dim, int spacedim = dim, typename number = double>
74 class DoFInfo : public LocalResults<number>
75 {
76 public:
77 /// The current cell
78 typename Triangulation<dim, spacedim>::cell_iterator cell;
79
80 /// The current face
81 typename Triangulation<dim, spacedim>::face_iterator face;
82
83 /**
84 * The number of the current face on the current cell.
85 *
86 * This number is numbers::invalid_unsigned_int if the @p info object was
87 * initialized with a cell.
88 */
89 unsigned int face_number;
90
91 /**
92 * The number of the current subface on the current face
93 *
94 * This number is numbers::invalid_unsigned_int if the @p info object was not
95 * initialized with a subface.
96 */
97 unsigned int sub_number;
98
99 /**
100 * The DoF indices of the
101 * current cell
102 */
103 std::vector<types::global_dof_index> indices;
104
105 /**
106 * The DoF indices on the current cell, organized by local blocks. The
107 * size of this vector is zero, unless local blocks are used.
108 */
109 std::vector<std::vector<types::global_dof_index>> indices_by_block;
110
111 /**
112 * Constructor setting the #block_info pointer.
113 */
114 DoFInfo(const BlockInfo &block_info);
115
116 /**
117 * Constructor leaving the #block_info pointer empty, but setting the
118 * #aux_local_indices.
119 */
120 DoFInfo(const DoFHandler<dim, spacedim> &dof_handler);
121
122 /**
123 * Set the current cell and fill @p indices.
124 */
125 template <class DHCellIterator>
126 void
127 reinit(const DHCellIterator &c);
128
129 /**
130 * Set the current face and fill @p indices if the #cell changed.
131 */
132 template <class DHCellIterator, class DHFaceIterator>
133 void
134 reinit(const DHCellIterator &c,
135 const DHFaceIterator &f,
136 const unsigned int face_no);
137
138 /**
139 * Set the current subface and fill @p indices if the #cell changed.
140 */
141 template <class DHCellIterator, class DHFaceIterator>
142 void
143 reinit(const DHCellIterator &c,
144 const DHFaceIterator &f,
145 const unsigned int face_no,
146 const unsigned int subface_no);
147
148 /**
149 * Switch to a new face of the same cell. Does not change @p indices and
150 * does not reset data in LocalResults.
151 */
152 template <class DHFaceIterator>
153 void
154 set_face(const DHFaceIterator &f, const unsigned int face_no);
155
156 /**
157 * Switch to a new subface of the same cell. Does not change @p indices
158 * and does not reset data in LocalResults.
159 */
160 template <class DHFaceIterator>
161 void
162 set_subface(const DHFaceIterator &f,
163 const unsigned int face_no,
164 const unsigned int subface_no);
165
166 const BlockIndices &
167 local_indices() const;
168
169
170 /// The block structure of the system
171 ObserverPointer<const BlockInfo, DoFInfo<dim, spacedim>> block_info;
172
173 /**
174 * The structure refers to a cell with level data instead of active data.
175 */
176 bool level_cell;
177
178 private:
179 /**
180 * Standard constructor, not setting any block indices. Use of this
181 * constructor is not recommended, but it is needed for the arrays in
182 * DoFInfoBox.
183 */
184 DoFInfo();
185
186 /// Set up local block indices
187 void
188 set_block_indices();
189
190 /// Fill index vector with active indices
191 template <class DHCellIterator>
192 void
193 get_indices(const DHCellIterator &c);
194
195 /// Auxiliary vector
196 std::vector<types::global_dof_index> indices_org;
197
198 /**
199 * An auxiliary local BlockIndices object created if #block_info is not
200 * set. It contains just a single block of the size of degrees of freedom
201 * per cell.
202 */
203 BlockIndices aux_local_indices;
204
205 friend class DoFInfoBox<dim, DoFInfo<dim, spacedim, number>>;
206 };
207
208
209 /**
210 * A class bundling the MeshWorker::DoFInfo objects used on a cell.
211 *
212 * @todo Currently, we are storing an object for the cells and two for each
213 * face. We could gather all face data pertaining to the cell itself in one
214 * object, saving a bit of memory and a few operations, but sacrificing some
215 * cleanliness.
216 *
217 * @ingroup MeshWorker
218 */
219 template <int dim, class DOFINFO>
220 class DoFInfoBox
221 {
222 public:
223 /**
224 * Constructor copying the seed into all other objects.
225 */
226 DoFInfoBox(const DOFINFO &seed);
227
228 /**
229 * Copy constructor, taking #cell and using it as a seed in the other
230 * constructor.
231 */
232 DoFInfoBox(const DoFInfoBox<dim, DOFINFO> &);
233
234 /**
235 * Copy assignment operator, taking another object as seed.
236 */
237 DoFInfoBox &
238 operator=(const DoFInfoBox<dim, DOFINFO> &);
239
240 /**
241 * Reset all the availability flags.
242 */
243 void
244 reset();
245
246 /**
247 * After all DOFINFO objects have been filled appropriately, use the
248 * ASSEMBLER object to assemble them into the global data. See
249 * MeshWorker::Assembler for available classes.
250 */
251 template <class ASSEMBLER>
252 void
253 assemble(ASSEMBLER &ass) const;
254
255
256 /**
257 * The data for the cell.
258 */
259 DOFINFO cell;
260 /**
261 * The data for the faces from inside.
262 */
263 DOFINFO interior[GeometryInfo<dim>::faces_per_cell];
264 /**
265 * The data for the faces from outside.
266 */
267 DOFINFO exterior[GeometryInfo<dim>::faces_per_cell];
268
269 /**
270 * A set of flags, indicating whether data on an interior face is
271 * available.
272 */
273 bool interior_face_available[GeometryInfo<dim>::faces_per_cell];
274
275 /**
276 * A set of flags, indicating whether data on an exterior face is
277 * available.
278 */
279 bool exterior_face_available[GeometryInfo<dim>::faces_per_cell];
280
281 /**
282 * A flag to specify if the current object has been set to a valid cell.
283 */
284 bool cell_valid;
285 };
286
287 //----------------------------------------------------------------------//
288
289 template <int dim, int spacedim, typename number>
290 DoFInfo<dim, spacedim, number>::DoFInfo()
291 : face_number(numbers::invalid_unsigned_int)
292 , sub_number(numbers::invalid_unsigned_int)
293 , level_cell(false)
294 {}
295
296
297
298 template <int dim, int spacedim, typename number>
299 DoFInfo<dim, spacedim, number>::DoFInfo(
300 const DoFHandler<dim, spacedim> &dof_handler)
301 : face_number(numbers::invalid_unsigned_int)
302 , sub_number(numbers::invalid_unsigned_int)
303 , level_cell(false)
304 {
305 std::vector<types::global_dof_index> aux(1);
306 aux[0] = dof_handler.get_fe().n_dofs_per_cell();
307 aux_local_indices.reinit(aux);
308 }
309
310
311 template <int dim, int spacedim, typename number>
312 template <class DHCellIterator>
313 inline void
314 DoFInfo<dim, spacedim, number>::get_indices(const DHCellIterator &c)
315 {
316 indices.resize(c->get_fe().n_dofs_per_cell());
317 if (block_info == nullptr || block_info->local().size() == 0)
318 c->get_active_or_mg_dof_indices(indices);
319 else
320 {
321 indices_org.resize(c->get_fe().n_dofs_per_cell());
322 c->get_active_or_mg_dof_indices(indices_org);
323 set_block_indices();
324 }
325 }
326
327
328 template <int dim, int spacedim, typename number>
329 template <class DHCellIterator>
330 inline void
331 DoFInfo<dim, spacedim, number>::reinit(const DHCellIterator &c)
332 {
333 get_indices(c);
334 level_cell = c->is_level_cell();
335
336 cell = typename Triangulation<dim, spacedim>::cell_iterator(*c);
337 face_number = numbers::invalid_unsigned_int;
338 sub_number = numbers::invalid_unsigned_int;
339 if (block_info)
340 LocalResults<number>::reinit(block_info->local());
341 else
342 LocalResults<number>::reinit(aux_local_indices);
343 }
344
345
346 template <int dim, int spacedim, typename number>
347 template <class DHFaceIterator>
348 inline void
349 DoFInfo<dim, spacedim, number>::set_face(const DHFaceIterator &f,
350 const unsigned int face_no)
351 {
352 face = static_cast<typename Triangulation<dim, spacedim>::face_iterator>(f);
353 face_number = face_no;
354 sub_number = numbers::invalid_unsigned_int;
355 }
356
357
358 template <int dim, int spacedim, typename number>
359 template <class DHCellIterator, class DHFaceIterator>
360 inline void
361 DoFInfo<dim, spacedim, number>::reinit(const DHCellIterator &c,
362 const DHFaceIterator &f,
363 const unsigned int face_no)
364 {
365 if ((cell.state() != IteratorState::valid) ||
366 cell != typename Triangulation<dim, spacedim>::cell_iterator(*c))
367 get_indices(c);
368 level_cell = c->is_level_cell();
369
370 cell = typename Triangulation<dim, spacedim>::cell_iterator(*c);
371 set_face(f, face_no);
372
373 if (block_info)
374 LocalResults<number>::reinit(block_info->local());
375 else
376 LocalResults<number>::reinit(aux_local_indices);
377 }
378
379
380 template <int dim, int spacedim, typename number>
381 template <class DHFaceIterator>
382 inline void
383 DoFInfo<dim, spacedim, number>::set_subface(const DHFaceIterator &f,
384 const unsigned int face_no,
385 const unsigned int subface_no)
386 {
387 face = static_cast<typename Triangulation<dim, spacedim>::face_iterator>(f);
388 face_number = face_no;
389 sub_number = subface_no;
390 }
391
392
393 template <int dim, int spacedim, typename number>
394 template <class DHCellIterator, class DHFaceIterator>
395 inline void
396 DoFInfo<dim, spacedim, number>::reinit(const DHCellIterator &c,
397 const DHFaceIterator &f,
398 const unsigned int face_no,
399 const unsigned int subface_no)
400 {
401 if (cell.state() != IteratorState::valid ||
402 cell !=
403 static_cast<typename Triangulation<dim, spacedim>::cell_iterator>(c))
404 get_indices(c);
405 level_cell = c->is_level_cell();
406
407 cell = static_cast<typename Triangulation<dim, spacedim>::cell_iterator>(c);
408 set_subface(f, face_no, subface_no);
409
410 if (block_info)
411 LocalResults<number>::reinit(block_info->local());
412 else
413 LocalResults<number>::reinit(aux_local_indices);
414 }
415
416
417 template <int dim, int spacedim, typename number>
418 inline const BlockIndices &
419 DoFInfo<dim, spacedim, number>::local_indices() const
420 {
421 if (block_info)
422 return block_info->local();
423 return aux_local_indices;
424 }
425
426 //----------------------------------------------------------------------//
427
428 template <int dim, class DOFINFO>
429 inline DoFInfoBox<dim, DOFINFO>::DoFInfoBox(const DOFINFO &seed)
430 : cell(seed)
431 , cell_valid(true)
432 {
433 for (const unsigned int i : GeometryInfo<dim>::face_indices())
434 {
435 exterior[i] = seed;
436 interior[i] = seed;
437 interior_face_available[i] = false;
438 exterior_face_available[i] = false;
439 }
440 }
441
442
443 template <int dim, class DOFINFO>
444 inline DoFInfoBox<dim, DOFINFO>::DoFInfoBox(
445 const DoFInfoBox<dim, DOFINFO> &other)
446 : cell(other.cell)
447 , cell_valid(other.cell_valid)
448 {
449 for (const unsigned int i : GeometryInfo<dim>::face_indices())
450 {
451 exterior[i] = other.exterior[i];
452 interior[i] = other.interior[i];
453 interior_face_available[i] = false;
454 exterior_face_available[i] = false;
455 }
456 }
457
458
459 template <int dim, class DOFINFO>
460 inline DoFInfoBox<dim, DOFINFO> &
461 DoFInfoBox<dim, DOFINFO>::operator=(const DoFInfoBox<dim, DOFINFO> &other)
462 {
463 cell = other.cell;
464 cell_valid = other.cell_valid;
465 for (const unsigned int i : GeometryInfo<dim>::face_indices())
466 {
467 exterior[i] = other.exterior[i];
468 interior[i] = other.interior[i];
469 interior_face_available[i] = false;
470 exterior_face_available[i] = false;
471 }
472 return *this;
473 }
474
475
476 template <int dim, class DOFINFO>
477 inline void
478 DoFInfoBox<dim, DOFINFO>::reset()
479 {
480 cell_valid = false;
481 for (const unsigned int i : GeometryInfo<dim>::face_indices())
482 {
483 interior_face_available[i] = false;
484 exterior_face_available[i] = false;
485 }
486 }
487
488
489 template <int dim, class DOFINFO>
490 template <class ASSEMBLER>
491 inline void
492 DoFInfoBox<dim, DOFINFO>::assemble(ASSEMBLER &assembler) const
493 {
494 if (!cell_valid)
495 return;
496
497 assembler.assemble(cell);
498 for (const unsigned int i : GeometryInfo<dim>::face_indices())
499 {
500 // Only do something if data available
501 if (interior_face_available[i])
502 {
503 // If both data
504 // available, it is an
505 // interior face
506 if (exterior_face_available[i])
507 assembler.assemble(interior[i], exterior[i]);
508 else
509 assembler.assemble(interior[i]);
510 }
511 }
512 }
513 } // namespace MeshWorker
514
515 DEAL_II_NAMESPACE_CLOSE
516
517 #endif