Debian Codegraph @

include/deal.II/numerics/data_out_dof_data.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 1999 - 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_data_out_dof_data_templates_h
16 #define dealii_data_out_dof_data_templates_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/memory_consumption.h>
22 #include <deal.II/base/numbers.h>
23 #include <deal.II/base/quadrature_lib.h>
24 #include <deal.II/base/signaling_nan.h>
25 #include <deal.II/base/work_stream.h>
26
27 #include <deal.II/dofs/dof_accessor.h>
28 #include <deal.II/dofs/dof_handler.h>
29 #include <deal.II/dofs/dof_tools.h>
30
31 #include <deal.II/fe/fe_dgq.h>
32 #include <deal.II/fe/fe_pyramid_p.h>
33 #include <deal.II/fe/fe_simplex_p.h>
34 #include <deal.II/fe/fe_values.h>
35 #include <deal.II/fe/fe_wedge_p.h>
36 #include <deal.II/fe/mapping.h>
37
38 #include <deal.II/grid/tria.h>
39 #include <deal.II/grid/tria_iterator.h>
40
41 #include <deal.II/hp/fe_collection.h>
42 #include <deal.II/hp/fe_values.h>
43 #include <deal.II/hp/q_collection.h>
44
45 #include <deal.II/lac/block_vector_base.h>
46 #include <deal.II/lac/la_parallel_block_vector.h>
47 #include <deal.II/lac/la_parallel_vector.h>
48 #include <deal.II/lac/read_write_vector.h>
49 #include <deal.II/lac/vector.h>
50
51 #include <deal.II/numerics/data_out.h>
52 #include <deal.II/numerics/data_out_dof_data.h>
53
54 #include <algorithm>
55 #include <memory>
56 #include <string>
57 #include <utility>
58 #include <vector>
59
60 DEAL_II_NAMESPACE_OPEN
61
62
63 namespace internal
64 {
65 namespace DataOutImplementation
66 {
67 template <int dim, int spacedim>
68 ParallelDataBase<dim, spacedim>::ParallelDataBase(
69 const unsigned int n_datasets,
70 const unsigned int n_subdivisions,
71 const std::vector<unsigned int> &n_postprocessor_outputs,
72 const dealii::Mapping<dim, spacedim> &mapping,
73 const std::vector<
74 std::shared_ptr<dealii::hp::FECollection<dim, spacedim>>>
75 &finite_elements,
76 const UpdateFlags update_flags,
77 const bool use_face_values)
78 : ParallelDataBase<dim, spacedim>(
79 n_datasets,
80 n_subdivisions,
81 n_postprocessor_outputs,
82 dealii::hp::MappingCollection<dim, spacedim>(mapping),
83 finite_elements,
84 update_flags,
85 use_face_values)
86 {}
87
88
89
90 /**
91 * Generate evaluation points on a simplex with arbitrary number of
92 * subdivisions.
93 */
94 template <int dim>
95 inline std::vector<Point<dim>>
96 generate_simplex_evaluation_points(const unsigned int /*n_subdivisions*/)
97 {
98 DEAL_II_NOT_IMPLEMENTED();
99
100 return {};
101 }
102
103
104
105 /**
106 * Helper function to create evaluation points recursively with
107 * subdivisions=0,1,2 being the base case:
108 * +
109 * |\
110 * + +-+
111 * |\ |\|\
112 * + +-+ +-+-+
113 * |\ |\|\ |\|\|\
114 * + +-+ +-+-+ +-+-+-+
115 * |\ |\|\ |\|\|\ |\|\|\|\
116 * + +-+ +-+-+ +-+-+-+ +-+-+-+-+
117 *
118 * 0 1 2 3 4
119 * ^ ^ | |
120 * | | | |
121 * +--------------------------+ |
122 * | |
123 * +--------------------------------+
124 */
125 inline void
126 generate_simplex_evaluation_points_recursively(
127 const std::vector<Point<2>> &bounding_vertices,
128 const unsigned int n_subdivisions,
129 std::vector<Point<2>> &evaluation_points)
130 {
131 if (n_subdivisions == 0)
132 {
133 evaluation_points.push_back(bounding_vertices[0]);
134 return;
135 }
136
137 for (const auto &p : bounding_vertices)
138 evaluation_points.push_back(p);
139
140 if (n_subdivisions == 1)
141 return;
142
143 // Helper functions to create intermediate points between p0 and p1 with
144 // n_subdivisions. This function appends these new points to the vector
145 // evaluation_points but also returns the points as a vector to be able
146 // to easily access specific points on the line.
147 const auto generate_inbetween_points = [&](const Point<2> &p0,
148 const Point<2> &p1) {
149 std::vector<Point<2>> line_points;
150
151 for (unsigned int i = 1; i < n_subdivisions; ++i)
152 line_points.push_back(p0 + (p1 - p0) / n_subdivisions * i);
153
154 evaluation_points.insert(evaluation_points.end(),
155 line_points.begin(),
156 line_points.end());
157
158 return line_points;
159 };
160
161 const auto line_points_0 =
162 generate_inbetween_points(bounding_vertices[0], bounding_vertices[1]);
163 const auto line_points_1 =
164 generate_inbetween_points(bounding_vertices[1], bounding_vertices[2]);
165 const auto line_points_2 =
166 generate_inbetween_points(bounding_vertices[2], bounding_vertices[0]);
167
168 if (n_subdivisions == 2)
169 return;
170
171 generate_simplex_evaluation_points_recursively(
172 // create new inner triangle (see ASCII art above)
173 {{Point<2>(line_points_0[line_points_0.size() - 2][0],
174 line_points_1[0][1]),
175 Point<2>(line_points_0[0][0],
176 line_points_1[line_points_1.size() - 2][1]),
177 Point<2>(line_points_0[0][0], line_points_1[0][1])}},
178 n_subdivisions - 3,
179 evaluation_points);
180 }
181
182
183
184 /**
185 * Specialization for triangles.
186 */
187 template <>
188 inline std::vector<Point<2>>
189 generate_simplex_evaluation_points(const unsigned int n_subdivisions)
190 {
191 std::vector<Point<2>> evaluation_points;
192
193 generate_simplex_evaluation_points_recursively(
194 {{Point<2>(0.0, 0.0), Point<2>(1.0, 0.0), Point<2>(0.0, 1.0)}},
195 n_subdivisions,
196 evaluation_points);
197
198 return evaluation_points;
199 }
200
201
202
203 /**
204 * Set up vectors of FEValues and FEFaceValues needed inside of
205 * ParallelDataBase and return the maximum number of quadrature points
206 * needed to allocate enough memory for the scratch data.
207 */
208 template <int dim, int spacedim>
209 unsigned int
210 setup_parallel_data_base_internal(
211 const unsigned int n_subdivisions,
212 const dealii::hp::MappingCollection<dim, spacedim> &mapping_collection,
213 const std::vector<
214 std::shared_ptr<dealii::hp::FECollection<dim, spacedim>>>
215 &finite_elements,
216 const UpdateFlags update_flags,
217 const bool use_face_values,
218 std::vector<std::shared_ptr<dealii::hp::FEValues<dim, spacedim>>>
219 &x_fe_values,
220 std::vector<std::shared_ptr<dealii::hp::FEFaceValues<dim, spacedim>>>
221 &x_fe_face_values)
222 {
223 // First figure out which reference cell types are present in the
224 // FECollections we got as arguments. To this end, use a lambda
225 // function that for a FECollection object tests whether one of the
226 // elements uses a specific reference cell. Then use this
227 // lambda function in a std::any_of() call that accumulates over
228 // all of the FECollection objects we were given.
229 //
230 // Note that in 1d, we will count a line segment as a hypercube
231 // even though it is *also* a simplex. Furthermore, wedges and
232 // pyramids can only appear in 3d set ups, so we do not need
233 // to test there if dim<3.
234 static const auto has_fe_with_reference_cell =
235 [](const dealii::hp::FECollection<dim, spacedim> &fe_collection,
236 const ReferenceCell &reference_cell) {
237 for (unsigned int i = 0; i < fe_collection.size(); ++i)
238 if (fe_collection[i].reference_cell() == reference_cell)
239 return true;
240 return false;
241 };
242
243 const bool needs_hypercube_setup = std::any_of(
244 finite_elements.begin(),
245 finite_elements.end(),
246 [](const std::shared_ptr<dealii::hp::FECollection<dim, spacedim>>
247 &fe_collection) {
248 return has_fe_with_reference_cell(
249 *fe_collection, ReferenceCells::get_hypercube<dim>());
250 });
251 const bool needs_simplex_setup =
252 (dim > 1 &&
253 std::any_of(
254 finite_elements.begin(),
255 finite_elements.end(),
256 [](const std::shared_ptr<dealii::hp::FECollection<dim, spacedim>>
257 &fe_collection) {
258 return has_fe_with_reference_cell(
259 *fe_collection, ReferenceCells::get_simplex<dim>());
260 }));
261 const bool needs_wedge_setup =
262 (dim == 3 &&
263 std::any_of(
264 finite_elements.begin(),
265 finite_elements.end(),
266 [](const std::shared_ptr<dealii::hp::FECollection<dim, spacedim>>
267 &fe_collection) {
268 return has_fe_with_reference_cell(*fe_collection,
269 ReferenceCells::Wedge);
270 }));
271 const bool needs_pyramid_setup =
272 (dim == 3 &&
273 std::any_of(
274 finite_elements.begin(),
275 finite_elements.end(),
276 [](const std::shared_ptr<dealii::hp::FECollection<dim, spacedim>>
277 &fe_collection) {
278 return has_fe_with_reference_cell(*fe_collection,
279 ReferenceCells::Pyramid);
280 }));
281
282 // Decide whether we want to work on cell or face FE(Face)Values objects:
283 if (use_face_values == false)
284 {
285 std::unique_ptr<dealii::Quadrature<dim>> quadrature_simplex;
286 std::unique_ptr<dealii::Quadrature<dim>> quadrature_hypercube;
287 std::unique_ptr<dealii::Quadrature<dim>> quadrature_wedge;
288 std::unique_ptr<dealii::Quadrature<dim>> quadrature_pyramid;
289
290 if (needs_simplex_setup)
291 {
292 if (dim == 2)
293 quadrature_simplex = std::make_unique<Quadrature<dim>>(
294 generate_simplex_evaluation_points<dim>(n_subdivisions));
295 else
296 quadrature_simplex = std::make_unique<Quadrature<dim>>(
297 FE_SimplexP<dim>(n_subdivisions).get_unit_support_points());
298 }
299
300 if (needs_hypercube_setup)
301 {
302 quadrature_hypercube =
303 std::make_unique<QIterated<dim>>(QTrapezoid<1>(),
304 n_subdivisions);
305 }
306
307 if (needs_wedge_setup)
308 {
309 Assert(n_subdivisions == 1, ExcNotImplemented());
310
311 quadrature_wedge = std::make_unique<Quadrature<dim>>(
312 ReferenceCells::Wedge.get_nodal_type_quadrature<dim>());
313 }
314
315 if (needs_pyramid_setup)
316 {
317 Assert(n_subdivisions == 1, ExcNotImplemented());
318
319 quadrature_pyramid = std::make_unique<Quadrature<dim>>(
320 ReferenceCells::Pyramid.get_nodal_type_quadrature<dim>());
321 }
322
323 x_fe_values.resize(finite_elements.size());
324 for (unsigned int i = 0; i < finite_elements.size(); ++i)
325 {
326 // Check if one of the previous finite elements is equal to the
327 // present one. If so, re-use the FEValues object.
328 for (unsigned int j = 0; j < i; ++j)
329 if (finite_elements[i].get() == finite_elements[j].get())
330 {
331 x_fe_values[i] = x_fe_values[j];
332 break;
333 }
334
335 // If none was found, create an FEValues object:
336 if (x_fe_values[i].get() == nullptr)
337 {
338 dealii::hp::QCollection<dim> quadrature;
339
340 for (unsigned int j = 0; j < finite_elements[i]->size(); ++j)
341 {
342 const ReferenceCell reference_cell =
343 (*finite_elements[i])[j].reference_cell();
344
345 if (reference_cell.is_hyper_cube())
346 quadrature.push_back(*quadrature_hypercube);
347 else if (reference_cell.is_simplex())
348 quadrature.push_back(*quadrature_simplex);
349 else if (reference_cell == ReferenceCells::Wedge)
350 quadrature.push_back(*quadrature_wedge);
351 else if (reference_cell == ReferenceCells::Pyramid)
352 quadrature.push_back(*quadrature_pyramid);
353 else
354 DEAL_II_NOT_IMPLEMENTED();
355 }
356
357 x_fe_values[i] =
358 std::make_shared<dealii::hp::FEValues<dim, spacedim>>(
359 mapping_collection,
360 *finite_elements[i],
361 quadrature,
362 // certain reference-cell kinds take by default the
363 // curved code path in DataOut::build_one_patch, for
364 // which the quadrature points need to be enabled
365 (needs_simplex_setup || needs_wedge_setup ||
366 needs_pyramid_setup) ?
367 (update_flags | update_quadrature_points) :
368 update_flags);
369 }
370 }
371
372 // Return maximal number of evaluation points:
373 return std::max(
374 {needs_wedge_setup ? quadrature_wedge->size() : 0,
375 needs_simplex_setup ? quadrature_simplex->size() : 0,
376 needs_hypercube_setup ? quadrature_hypercube->size() : 0,
377 needs_pyramid_setup ? quadrature_pyramid->size() : 0});
378 }
379 else // build FEFaceValues objects instead
380 {
381 // The code following is not quite right for wedges and pyramids.
382 // Assert that we don't have these kinds of meshes.
383 Assert(needs_pyramid_setup == false && needs_wedge_setup == false,
384 ExcNotImplemented());
385
386 std::unique_ptr<dealii::Quadrature<dim - 1>> quadrature_simplex;
387 std::unique_ptr<dealii::Quadrature<dim - 1>> quadrature_hypercube;
388
389 // See whether we need simplex or hypercube quadrature formulas.
390 // This is only an issue in 3d (in 2d every face integral is just
391 // a line integral, so we can deal with that via the usual hypercube
392 // quadrature rule).
393 if ((dim == 3) &&
394 (needs_simplex_setup || needs_pyramid_setup || needs_wedge_setup))
395 {
396 quadrature_simplex = std::make_unique<Quadrature<dim - 1>>(
397 generate_simplex_evaluation_points<dim - 1>(n_subdivisions));
398 }
399
400 if ((dim < 3) || (needs_hypercube_setup || needs_pyramid_setup ||
401 needs_wedge_setup))
402 {
403 quadrature_hypercube =
404 std::make_unique<QIterated<dim - 1>>(QTrapezoid<1>(),
405 n_subdivisions);
406 }
407
408 x_fe_face_values.resize(finite_elements.size());
409 for (unsigned int i = 0; i < finite_elements.size(); ++i)
410 {
411 // Check if one of the previous finite elements is equal to the
412 // present one. If so, re-use the FEValues object.
413 for (unsigned int j = 0; j < i; ++j)
414 if (finite_elements[i].get() == finite_elements[j].get())
415 {
416 x_fe_face_values[i] = x_fe_face_values[j];
417 break;
418 }
419
420 // If none was found, create an FEFaceValues object:
421 if (x_fe_face_values[i].get() == nullptr)
422 {
423 dealii::hp::QCollection<dim - 1> quadrature;
424
425 for (unsigned int j = 0; j < finite_elements[i]->size(); ++j)
426 {
427 const ReferenceCell reference_cell =
428 (*finite_elements[i])[j].reference_cell();
429
430 // In 1d/2d and for hypercube/wedge/pyramid elements, we
431 // need hypercube quadratures.
432 if ((dim < 3) ||
433 (reference_cell.is_hyper_cube() ||
434 (reference_cell == ReferenceCells::Wedge) ||
435 (reference_cell == ReferenceCells::Pyramid)))
436 quadrature.push_back(*quadrature_hypercube);
437
438 // In 3d, if the element is for simplex/wedge/pyramid
439 // cells, then we also need simplex quadratures
440 if ((dim == 3) &&
441 (reference_cell.is_simplex() ||
442 (reference_cell == ReferenceCells::Wedge) ||
443 (reference_cell == ReferenceCells::Pyramid)))
444 quadrature.push_back(*quadrature_simplex);
445 }
446
447 x_fe_face_values[i] =
448 std::make_shared<dealii::hp::FEFaceValues<dim, spacedim>>(
449 mapping_collection,
450 *finite_elements[i],
451 quadrature,
452 update_flags);
453 }
454 }
455
456 // Return maximal number of evaluation points:
457 return std::max(
458 {(dim == 3) && (needs_simplex_setup || needs_pyramid_setup ||
459 needs_wedge_setup) ?
460 quadrature_simplex->size() :
461 0,
462 (dim < 3) || needs_hypercube_setup ? quadrature_hypercube->size() :
463 0});
464 }
465 }
466
467
468
469 template <int dim, int spacedim>
470 ParallelDataBase<dim, spacedim>::ParallelDataBase(
471 const unsigned int n_datasets,
472 const unsigned int n_subdivisions,
473 const std::vector<unsigned int> &n_postprocessor_outputs,
474 const dealii::hp::MappingCollection<dim, spacedim> &mapping,
475 const std::vector<
476 std::shared_ptr<dealii::hp::FECollection<dim, spacedim>>>
477 &finite_elements,
478 const UpdateFlags update_flags,
479 const bool use_face_values)
480 : n_datasets(n_datasets)
481 , n_subdivisions(n_subdivisions)
482 , postprocessed_values(n_postprocessor_outputs.size())
483 , mapping_collection(mapping)
484 , finite_elements(finite_elements)
485 , update_flags(update_flags)
486 {
487 const unsigned int n_q_points =
488 setup_parallel_data_base_internal(n_subdivisions,
489 mapping,
490 finite_elements,
491 update_flags,
492 use_face_values,
493 x_fe_values,
494 x_fe_face_values);
495
496 patch_values_scalar.solution_values.resize(n_q_points);
497 patch_values_scalar.solution_gradients.resize(n_q_points);
498 patch_values_scalar.solution_hessians.resize(n_q_points);
499 patch_values_system.solution_values.resize(n_q_points);
500 patch_values_system.solution_gradients.resize(n_q_points);
501 patch_values_system.solution_hessians.resize(n_q_points);
502
503 for (unsigned int dataset = 0; dataset < n_postprocessor_outputs.size();
504 ++dataset)
505 if (n_postprocessor_outputs[dataset] != 0)
506 postprocessed_values[dataset].resize(
507 n_q_points,
508 dealii::Vector<double>(n_postprocessor_outputs[dataset]));
509 }
510
511
512
513 // implement copy constructor to create a thread's own version of
514 // x_fe_values
515 template <int dim, int spacedim>
516 ParallelDataBase<dim, spacedim>::ParallelDataBase(
517 const ParallelDataBase<dim, spacedim> &data)
518 : n_datasets(data.n_datasets)
519 , n_subdivisions(data.n_subdivisions)
520 , patch_values_scalar(data.patch_values_scalar)
521 , patch_values_system(data.patch_values_system)
522 , postprocessed_values(data.postprocessed_values)
523 , mapping_collection(data.mapping_collection)
524 , finite_elements(data.finite_elements)
525 , update_flags(data.update_flags)
526 {
527 setup_parallel_data_base_internal(n_subdivisions,
528 mapping_collection,
529 finite_elements,
530 update_flags,
531 data.x_fe_values.empty(),
532 x_fe_values,
533 x_fe_face_values);
534 }
535
536
537
538 template <int dim, int spacedim>
539 void
540 ParallelDataBase<dim, spacedim>::reinit_all_fe_values(
541 std::vector<std::shared_ptr<DataEntryBase<dim, spacedim>>> &dof_data,
542 const typename dealii::Triangulation<dim, spacedim>::cell_iterator &cell,
543 const unsigned int face)
544 {
545 for (unsigned int dataset = 0; dataset < dof_data.size(); ++dataset)
546 {
547 const bool is_duplicate = std::any_of(
548 finite_elements.cbegin(),
549 finite_elements.cbegin() + dataset,
550 [&](const std::shared_ptr<dealii::hp::FECollection<dim, spacedim>>
551 &fe) { return finite_elements[dataset].get() == fe.get(); });
552 if (is_duplicate == false)
553 {
554 if (cell->is_active())
555 {
556 const typename DoFHandler<dim, spacedim>::active_cell_iterator
557 dh_cell(&cell->get_triangulation(),
558 cell->level(),
559 cell->index(),
560 dof_data[dataset]->dof_handler);
561
562 // Check whether we need cell or face FEValues objects by
563 // testing which of the two arrays actually has any content.
564 if (x_fe_values.empty())
565 {
566 AssertIndexRange(face, GeometryInfo<dim>::faces_per_cell);
567 x_fe_face_values[dataset]->reinit(dh_cell, face);
568 }
569 else
570 x_fe_values[dataset]->reinit(dh_cell);
571 }
572 else
573 x_fe_values[dataset]->reinit(cell);
574 }
575 }
576
577 // If there is are no DoF-associated data (just cell-associated ones),
578 // then the loop above will not execute any iterations. In that case,
579 // do the initialization for the first FE(Face)Values object by
580 // hand, using only the (triangulation) cell without a DoFHandler.
581 if (dof_data.empty())
582 {
583 if (x_fe_values.empty())
584 {
585 AssertIndexRange(face, GeometryInfo<dim>::faces_per_cell);
586 x_fe_face_values[0]->reinit(cell, face);
587 }
588 else
589 x_fe_values[0]->reinit(cell);
590 }
591 }
592
593
594
595 template <int dim, int spacedim>
596 const FEValuesBase<dim, spacedim> &
597 ParallelDataBase<dim, spacedim>::get_present_fe_values(
598 const unsigned int dataset) const
599 {
600 AssertIndexRange(dataset, finite_elements.size());
601
602 // Check whether we need cell or face FEValues objects by testing
603 // which of the two arrays actually has any content.
604 if (x_fe_values.empty())
605 return x_fe_face_values[dataset]->get_present_fe_values();
606 else
607 return x_fe_values[dataset]->get_present_fe_values();
608 }
609
610
611
612 template <int dim, int spacedim>
613 void
614 ParallelDataBase<dim, spacedim>::resize_system_vectors(
615 const unsigned int n_components)
616 {
617 Assert(patch_values_system.solution_values.size() > 0,
618 ExcInternalError());
619 AssertDimension(patch_values_system.solution_values.size(),
620 patch_values_system.solution_gradients.size());
621 AssertDimension(patch_values_system.solution_values.size(),
622 patch_values_system.solution_hessians.size());
623 for (unsigned int k = 0; k < patch_values_system.solution_values.size();
624 ++k)
625 {
626 patch_values_system.solution_values[k].reinit(n_components);
627 patch_values_system.solution_gradients[k].resize(n_components);
628 patch_values_system.solution_hessians[k].resize(n_components);
629 }
630 }
631
632
633
634 /**
635 * In a WorkStream context, use this function to append the patch computed
636 * by the parallel stage to the array of patches.
637 */
638 template <int dim, int spacedim>
639 void
640 append_patch_to_list(
641 const DataOutBase::Patch<dim, spacedim> &patch,
642 std::vector<DataOutBase::Patch<dim, spacedim>> &patches)
643 {
644 patches.push_back(patch);
645 patches.back().patch_index = patches.size() - 1;
646 }
647 } // namespace DataOutImplementation
648 } // namespace internal
649
650 namespace internal
651 {
652 namespace DataOutImplementation
653 {
654 /**
655 * Extract the specified component of a number. This template is used when
656 * the given value is assumed to be a real scalar, so asking for the real
657 * part is the only valid choice for the second argument.
658 */
659 template <typename NumberType>
660 double
661 get_component(const NumberType value,
662 const ComponentExtractor extract_component)
663 {
664 (void)extract_component;
665 static_assert(
666 numbers::NumberTraits<NumberType>::is_complex == false,
667 "This function must not be called for complex-valued data types.");
668 Assert(extract_component == ComponentExtractor::real_part,
669 ExcMessage("You cannot extract anything other than the real "
670 "part from a real number."));
671 return value;
672 }
673
674
675
676 /**
677 * Extract the specified component of a number. This template is used when
678 * the given value is a complex number
679 */
680 template <typename NumberType>
681 double
682 get_component(const std::complex<NumberType> &value,
683 const ComponentExtractor extract_component)
684 {
685 switch (extract_component)
686 {
687 case ComponentExtractor::real_part:
688 return value.real();
689
690 case ComponentExtractor::imaginary_part:
691 return value.imag();
692
693 default:
694 DEAL_II_ASSERT_UNREACHABLE();
695 }
696
697 return numbers::signaling_nan<double>();
698 }
699
700
701
702 template <int rank, int dim, typename NumberType>
703 Tensor<rank, dim>
704 get_component(const Tensor<rank, dim, NumberType> &value,
705 const ComponentExtractor extract_component)
706 {
707 Assert(extract_component == ComponentExtractor::real_part,
708 ExcMessage("You cannot extract anything other than the real "
709 "part from a real number."));
710
711 Tensor<rank, dim, double> t;
712 for (unsigned int d = 0; d < dim; ++d)
713 t[d] = get_component(value[d], extract_component);
714
715 return t;
716 }
717
718
719
720 template <int dim, int spacedim>
721 DataEntryBase<dim, spacedim>::DataEntryBase(
722 const DoFHandler<dim, spacedim> *dofs,
723 const std::vector<std::string> &names_in,
724 const std::vector<
725 DataComponentInterpretation::DataComponentInterpretation>
726 &data_component_interpretation)
727 : dof_handler(
728 dofs,
729 typeid(dealii::DataOut_DoFData<dim, dim, spacedim, spacedim>).name())
730 , names(names_in)
731 , data_component_interpretation(data_component_interpretation)
732 , postprocessor(nullptr, typeid(*this).name())
733 , n_output_variables(names.size())
734 {
735 Assert(names.size() == data_component_interpretation.size(),
736 ExcDimensionMismatch(data_component_interpretation.size(),
737 names.size()));
738
739 // check that the names use only allowed characters
740 for (const auto &name : names)
741 {
742 (void)name;
743 Assert(name.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
744 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
745 "0123456789_<>()") == std::string::npos,
746 Exceptions::DataOutImplementation::ExcInvalidCharacter(
747 name,
748 name.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
749 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
750 "0123456789_<>()")));
751 }
752 }
753
754
755
756 template <int dim, int spacedim>
757 DataEntryBase<dim, spacedim>::DataEntryBase(
758 const DoFHandler<dim, spacedim> *dofs,
759 const DataPostprocessor<spacedim> *data_postprocessor)
760 : dof_handler(
761 dofs,
762 typeid(dealii::DataOut_DoFData<dim, dim, spacedim, spacedim>).name())
763 , names(data_postprocessor->get_names())
764 , data_component_interpretation(
765 data_postprocessor->get_data_component_interpretation())
766 , postprocessor(data_postprocessor, typeid(*this).name())
767 , n_output_variables(names.size())
768 {
769 Assert(data_postprocessor->get_names().size() ==
770 data_postprocessor->get_data_component_interpretation().size(),
771 ExcDimensionMismatch(
772 data_postprocessor->get_names().size(),
773 data_postprocessor->get_data_component_interpretation().size()));
774
775 // check that the names use only allowed characters
776 for (const auto &name : names)
777 {
778 (void)name;
779 Assert(name.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
780 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
781 "0123456789_<>()") == std::string::npos,
782 Exceptions::DataOutImplementation::ExcInvalidCharacter(
783 name,
784 name.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
785 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
786 "0123456789_<>()")));
787 }
788 }
789
790
791
792 namespace CreateVectors
793 {
794 /**
795 * Copy the data from an arbitrary non-block vector to a
796 * LinearAlgebra::distributed::Vector.
797 */
798 template <typename VectorType, typename Number>
799 void
800 copy_locally_owned_data_from(
801 const VectorType &src,
802 LinearAlgebra::distributed::Vector<Number> &dst)
803 {
804 // If source and destination vector have the same underlying scalar,
805 // we can directly import elements by using only one temporary vector:
806 if constexpr (std::is_same_v<typename VectorType::value_type, Number>)
807 {
808 LinearAlgebra::ReadWriteVector<typename VectorType::value_type>
809 temp;
810 temp.reinit(src.locally_owned_elements());
811 temp.import_elements(src, VectorOperation::insert);
812
813 dst.import_elements(temp, VectorOperation::insert);
814 }
815 else
816 // The source and destination vector have different scalar types. We
817 // need to split the parallel import and local copy operations into
818 // two phases
819 {
820 LinearAlgebra::ReadWriteVector<typename VectorType::value_type>
821 temp;
822 temp.reinit(src.locally_owned_elements());
823 temp.import_elements(src, VectorOperation::insert);
824
825 LinearAlgebra::ReadWriteVector<Number> temp2;
826 temp2.reinit(temp, true);
827 temp2 = temp;
828
829 dst.import_elements(temp2, VectorOperation::insert);
830 }
831 }
832
833 #ifdef DEAL_II_WITH_TRILINOS
834 template <typename Number>
835 void
836 copy_locally_owned_data_from(
837 const TrilinosWrappers::MPI::Vector &src,
838 LinearAlgebra::distributed::Vector<Number> &dst)
839 {
840 // ReadWriteVector does not work for ghosted
841 // TrilinosWrappers::MPI::Vector objects. Fall back to copy the
842 // entries manually.
843 for (const auto i : dst.locally_owned_elements())
844 dst[i] = src[i];
845 }
846 #endif
847
848 #ifdef DEAL_II_TRILINOS_WITH_TPETRA
849 template <typename Number, typename MemorySpace>
850 void
851 copy_locally_owned_data_from(
852 const LinearAlgebra::TpetraWrappers::Vector<Number, MemorySpace> &src,
853 LinearAlgebra::distributed::Vector<Number> &dst)
854 {
855 // ReadWriteVector does not work for ghosted
856 // TrilinosWrappers::MPI::Vector objects. Fall back to copy the
857 // entries manually.
858 for (const auto i : dst.locally_owned_elements())
859 dst[i] = src[i];
860 }
861 #endif
862
863 /**
864 * Create a ghosted-copy of a block dof vector.
865 */
866 template <int dim,
867 int spacedim,
868 typename VectorType,
869 typename Number,
870 std::enable_if_t<IsBlockVector<VectorType>::value, VectorType>
871 * = nullptr>
872 void
873 create_dof_vector(
874 const DoFHandler<dim, spacedim> &dof_handler,
875 const VectorType &src,
876 LinearAlgebra::distributed::BlockVector<Number> &dst,
877 const unsigned int level = numbers::invalid_unsigned_int)
878 {
879 const IndexSet &locally_owned_dofs =
880 (level == numbers::invalid_unsigned_int) ?
881 dof_handler.locally_owned_dofs() :
882 dof_handler.locally_owned_mg_dofs(level);
883
884 const IndexSet locally_relevant_dofs =
885 (level == numbers::invalid_unsigned_int) ?
886 DoFTools::extract_locally_relevant_dofs(dof_handler) :
887 DoFTools::extract_locally_relevant_level_dofs(dof_handler, level);
888
889 std::vector<types::global_dof_index> n_indices_per_block(
890 src.n_blocks());
891
892 for (unsigned int b = 0; b < src.n_blocks(); ++b)
893 n_indices_per_block[b] = src.get_block_indices().block_size(b);
894
895 const auto locally_owned_dofs_b =
896 locally_owned_dofs.split_by_block(n_indices_per_block);
897 const auto locally_relevant_dofs_b =
898 locally_relevant_dofs.split_by_block(n_indices_per_block);
899
900 dst.reinit(src.n_blocks());
901
902 for (unsigned int b = 0; b < src.n_blocks(); ++b)
903 {
904 Assert(src.block(b).locally_owned_elements().is_contiguous(),
905 ExcMessage(
906 "Using non-contiguous vector blocks is not currently "
907 "supported by DataOut and related classes. The typical "
908 "way you may end up with such vectors is if you order "
909 "degrees of freedom via DoFRenumber::component_wise() "
910 "but then group several vector components into one "
911 "vector block -- for example, all 'dim' components "
912 "of a velocity are grouped into the same vector block. "
913 "If you do this, you don't want to renumber degrees "
914 "of freedom based on vector component, but instead "
915 "based on the 'blocks' you will later use for grouping "
916 "things into vectors. Take a look at step-32 and step-55 "
917 "and how they use the second argument of "
918 "DoFRenumber::component_wise()."));
919
920 dst.block(b).reinit(locally_owned_dofs_b[b],
921 locally_relevant_dofs_b[b],
922 dof_handler.get_mpi_communicator());
923 copy_locally_owned_data_from(src.block(b), dst.block(b));
924 }
925
926 dst.collect_sizes();
927
928 dst.update_ghost_values();
929 }
930
931 /**
932 * Create a ghosted-copy of a non-block dof vector.
933 */
934 template <int dim,
935 int spacedim,
936 typename VectorType,
937 typename Number,
938 std::enable_if_t<!IsBlockVector<VectorType>::value, VectorType>
939 * = nullptr>
940 void
941 create_dof_vector(
942 const DoFHandler<dim, spacedim> &dof_handler,
943 const VectorType &src,
944 LinearAlgebra::distributed::BlockVector<Number> &dst,
945 const unsigned int level = numbers::invalid_unsigned_int)
946 {
947 const IndexSet &locally_owned_dofs =
948 (level == numbers::invalid_unsigned_int) ?
949 dof_handler.locally_owned_dofs() :
950 dof_handler.locally_owned_mg_dofs(level);
951
952 const IndexSet locally_relevant_dofs =
953 (level == numbers::invalid_unsigned_int) ?
954 DoFTools::extract_locally_relevant_dofs(dof_handler) :
955 DoFTools::extract_locally_relevant_level_dofs(dof_handler, level);
956
957
958 Assert(locally_owned_dofs.is_contiguous(),
959 ExcMessage(
960 "You are trying to add a non-block vector with non-contiguous "
961 "locally-owned index sets. This is not possible. Please "
962 "consider to use an adequate block vector!"));
963
964 dst.reinit(1);
965
966 dst.block(0).reinit(locally_owned_dofs,
967 locally_relevant_dofs,
968 dof_handler.get_mpi_communicator());
969 copy_locally_owned_data_from(src, dst.block(0));
970
971 dst.collect_sizes();
972
973 dst.update_ghost_values();
974 }
975
976 /**
977 * Create a ghosted-copy of a block cell vector.
978 */
979 template <typename VectorType,
980 typename Number,
981 std::enable_if_t<IsBlockVector<VectorType>::value, VectorType>
982 * = nullptr>
983 void
984 create_cell_vector(const VectorType &src,
985 LinearAlgebra::distributed::BlockVector<Number> &dst)
986 {
987 dst.reinit(src.n_blocks());
988
989 for (unsigned int b = 0; b < src.n_blocks(); ++b)
990 {
991 dst.block(b).reinit(src.get_block_indices().block_size(b));
992 copy_locally_owned_data_from(src.block(b), dst.block(b));
993 }
994
995 dst.collect_sizes();
996 }
997
998
999 /**
1000 * Create a ghosted-copy of a non-block cell vector.
1001 */
1002 template <typename VectorType,
1003 typename Number,
1004 std::enable_if_t<!IsBlockVector<VectorType>::value, VectorType>
1005 * = nullptr>
1006 void
1007 create_cell_vector(const VectorType &src,
1008 LinearAlgebra::distributed::BlockVector<Number> &dst)
1009 {
1010 dst.reinit(1);
1011
1012 dst.block(0).reinit(src.size());
1013 copy_locally_owned_data_from(src, dst.block(0));
1014
1015 dst.collect_sizes();
1016
1017 dst.update_ghost_values();
1018 }
1019 } // namespace CreateVectors
1020
1021
1022
1023 /**
1024 * Class that stores a pointer to a vector of type equal to the template
1025 * argument, and provides the functions to extract data from it.
1026 */
1027 template <int dim, int spacedim, typename ScalarType>
1028 class DataEntry : public DataEntryBase<dim, spacedim>
1029 {
1030 public:
1031 /**
1032 * Constructor. Give a list of names for the individual components of
1033 * the vector and their interpretation as scalar or vector data. This
1034 * constructor assumes that no postprocessor is going to be used.
1035 */
1036 template <typename DataVectorType, typename VectorType>
1037 DataEntry(const DoFHandler<dim, spacedim> *dofs,
1038 const VectorType *data,
1039 const std::vector<std::string> &names,
1040 const std::vector<
1041 DataComponentInterpretation::DataComponentInterpretation>
1042 &data_component_interpretation,
1043 const DataVectorType actual_type);
1044
1045 /**
1046 * Constructor when a data postprocessor is going to be used. In that
1047 * case, the names and vector declarations are going to be acquired from
1048 * the postprocessor.
1049 */
1050 template <typename VectorType>
1051 DataEntry(const DoFHandler<dim, spacedim> *dofs,
1052 const VectorType *data,
1053 const DataPostprocessor<spacedim> *data_postprocessor);
1054
1055 /**
1056 * Assuming that the stored vector is a cell vector, extract the given
1057 * element from it.
1058 */
1059 virtual double
1060 get_cell_data_value(
1061 const unsigned int cell_number,
1062 const ComponentExtractor extract_component) const override;
1063
1064 /**
1065 * Given a FEValuesBase object, extract the values on the present cell
1066 * from the vector we actually store.
1067 */
1068 virtual void
1069 get_function_values(const FEValuesBase<dim, spacedim> &fe_patch_values,
1070 const ComponentExtractor extract_component,
1071 std::vector<double> &patch_values) const override;
1072
1073 /**
1074 * Given a FEValuesBase object, extract the values on the present cell
1075 * from the vector we actually store. This function does the same as the
1076 * one above but for vector-valued finite elements.
1077 */
1078 virtual void
1079 get_function_values(const FEValuesBase<dim, spacedim> &fe_patch_values,
1080 const ComponentExtractor extract_component,
1081 std::vector<dealii::Vector<double>>
1082 &patch_values_system) const override;
1083
1084 /**
1085 * Given a FEValuesBase object, extract the gradients on the present
1086 * cell from the vector we actually store.
1087 */
1088 virtual void
1089 get_function_gradients(
1090 const FEValuesBase<dim, spacedim> &fe_patch_values,
1091 const ComponentExtractor extract_component,
1092 std::vector<Tensor<1, spacedim>> &patch_gradients) const override;
1093
1094 /**
1095 * Given a FEValuesBase object, extract the gradients on the present
1096 * cell from the vector we actually store. This function does the same
1097 * as the one above but for vector-valued finite elements.
1098 */
1099 virtual void
1100 get_function_gradients(const FEValuesBase<dim, spacedim> &fe_patch_values,
1101 const ComponentExtractor extract_component,
1102 std::vector<std::vector<Tensor<1, spacedim>>>
1103 &patch_gradients_system) const override;
1104
1105 /**
1106 * Given a FEValuesBase object, extract the second derivatives on the
1107 * present cell from the vector we actually store.
1108 */
1109 virtual void
1110 get_function_hessians(
1111 const FEValuesBase<dim, spacedim> &fe_patch_values,
1112 const ComponentExtractor extract_component,
1113 std::vector<Tensor<2, spacedim>> &patch_hessians) const override;
1114
1115 /**
1116 * Given a FEValuesBase object, extract the second derivatives on the
1117 * present cell from the vector we actually store. This function does
1118 * the same as the one above but for vector-valued finite elements.
1119 */
1120 virtual void
1121 get_function_hessians(const FEValuesBase<dim, spacedim> &fe_patch_values,
1122 const ComponentExtractor extract_component,
1123 std::vector<std::vector<Tensor<2, spacedim>>>
1124 &patch_hessians_system) const override;
1125
1126 /**
1127 * Return whether the data represented by (a derived class of) this object
1128 * represents a complex-valued (as opposed to real-valued) information.
1129 */
1130 virtual bool
1131 is_complex_valued() const override;
1132
1133 /**
1134 * Clear all references to the vectors.
1135 */
1136 virtual void
1137 clear() override;
1138
1139 /**
1140 * Determine an estimate for the memory consumption (in bytes) of this
1141 * object.
1142 */
1143 virtual std::size_t
1144 memory_consumption() const override;
1145
1146 /**
1147 * Copy of the data of the vector passed to DataOutDoFData::add_vector().
1148 * This vector imports all elements necessary to create output from the
1149 * source vector and stores it until we no longer need it. No reference
1150 * to the original source vector is necessary nor stored.
1151 */
1152 LinearAlgebra::distributed::BlockVector<ScalarType> vector;
1153 };
1154
1155
1156
1157 template <int dim, int spacedim, typename ScalarType>
1158 template <typename DataVectorType, typename VectorType>
1159 DataEntry<dim, spacedim, ScalarType>::DataEntry(
1160 const DoFHandler<dim, spacedim> *dofs,
1161 const VectorType *data,
1162 const std::vector<std::string> &names,
1163 const std::vector<
1164 DataComponentInterpretation::DataComponentInterpretation>
1165 &data_component_interpretation,
1166 const DataVectorType actual_type)
1167 : DataEntryBase<dim, spacedim>(dofs, names, data_component_interpretation)
1168 {
1169 if (actual_type == DataVectorType::type_dof_data)
1170 CreateVectors::create_dof_vector(*dofs, *data, vector);
1171 else if (actual_type == DataVectorType::type_cell_data)
1172 CreateVectors::create_cell_vector(*data, vector);
1173 else
1174 DEAL_II_ASSERT_UNREACHABLE();
1175 }
1176
1177
1178
1179 template <int dim, int spacedim, typename ScalarType>
1180 template <typename VectorType>
1181 DataEntry<dim, spacedim, ScalarType>::DataEntry(
1182 const DoFHandler<dim, spacedim> *dofs,
1183 const VectorType *data,
1184 const DataPostprocessor<spacedim> *data_postprocessor)
1185 : DataEntryBase<dim, spacedim>(dofs, data_postprocessor)
1186 {
1187 CreateVectors::create_dof_vector(*dofs, *data, vector);
1188 }
1189
1190
1191
1192 template <int dim, int spacedim, typename ScalarType>
1193 double
1194 DataEntry<dim, spacedim, ScalarType>::get_cell_data_value(
1195 const unsigned int cell_number,
1196 const ComponentExtractor extract_component) const
1197 {
1198 return get_component(
1199 internal::ElementAccess<LinearAlgebra::distributed::BlockVector<
1200 ScalarType>>::get(vector, cell_number),
1201 extract_component);
1202 }
1203
1204
1205
1206 template <int dim, int spacedim, typename ScalarType>
1207 void
1208 DataEntry<dim, spacedim, ScalarType>::get_function_values(
1209 const FEValuesBase<dim, spacedim> &fe_patch_values,
1210 const ComponentExtractor extract_component,
1211 std::vector<dealii::Vector<double>> &patch_values_system) const
1212 {
1213 if constexpr (std::is_same_v<ScalarType, double>)
1214 {
1215 (void)extract_component;
1216 Assert(extract_component == ComponentExtractor::real_part,
1217 ExcMessage("You cannot extract anything other than the real "
1218 "part from a real number."));
1219 fe_patch_values.get_function_values(vector, patch_values_system);
1220 }
1221 else
1222 {
1223 // OK, so we know that the data type stored by the user-provided
1224 // vector is not simply a double. In that case, we need to ask the
1225 // FEValuesBase object to extract function values for us from the
1226 // evaluation points in the provided data type, and then copy them by
1227 // hand into the output location.
1228 const unsigned int n_components =
1229 fe_patch_values.get_fe().n_components();
1230 const unsigned int n_eval_points =
1231 fe_patch_values.n_quadrature_points;
1232
1233 std::vector<dealii::Vector<ScalarType>> tmp(n_eval_points);
1234 for (unsigned int i = 0; i < n_eval_points; ++i)
1235 tmp[i].reinit(n_components);
1236
1237 fe_patch_values.get_function_values(vector, tmp);
1238
1239 AssertDimension(patch_values_system.size(), n_eval_points);
1240 for (unsigned int i = 0; i < n_eval_points; ++i)
1241 {
1242 AssertDimension(patch_values_system[i].size(), n_components);
1243
1244 for (unsigned int j = 0; j < n_components; ++j)
1245 patch_values_system[i](j) =
1246 get_component(tmp[i](j), extract_component);
1247 }
1248 }
1249 }
1250
1251
1252
1253 template <int dim, int spacedim, typename ScalarType>
1254 void
1255 DataEntry<dim, spacedim, ScalarType>::get_function_values(
1256 const FEValuesBase<dim, spacedim> &fe_patch_values,
1257 const ComponentExtractor extract_component,
1258 std::vector<double> &patch_values) const
1259 {
1260 if constexpr (std::is_same_v<ScalarType, double>)
1261 {
1262 (void)extract_component;
1263 Assert(extract_component == ComponentExtractor::real_part,
1264 ExcMessage("You cannot extract anything other than the real "
1265 "part from a real number."));
1266
1267 fe_patch_values.get_function_values(vector, patch_values);
1268 }
1269 else
1270 {
1271 std::vector<ScalarType> tmp(patch_values.size());
1272
1273 fe_patch_values.get_function_values(vector, tmp);
1274
1275 for (unsigned int i = 0; i < tmp.size(); ++i)
1276 patch_values[i] = get_component(tmp[i], extract_component);
1277 }
1278 }
1279
1280
1281
1282 template <int dim, int spacedim, typename ScalarType>
1283 void
1284 DataEntry<dim, spacedim, ScalarType>::get_function_gradients(
1285 const FEValuesBase<dim, spacedim> &fe_patch_values,
1286 const ComponentExtractor extract_component,
1287 std::vector<std::vector<Tensor<1, spacedim>>> &patch_gradients_system)
1288 const
1289 {
1290 if constexpr (std::is_same_v<ScalarType, double>)
1291 {
1292 (void)extract_component;
1293 Assert(extract_component == ComponentExtractor::real_part,
1294 ExcMessage("You cannot extract anything other than the real "
1295 "part from a real number."));
1296
1297 fe_patch_values.get_function_gradients(vector,
1298 patch_gradients_system);
1299 }
1300 else
1301 {
1302 // OK, so we know that the data type stored by the user-provided
1303 // vector is not simply a double. In that case, we need to ask the
1304 // FEValuesBase object to extract function values for us from the
1305 // evaluation points in the provided data type, and then copy them by
1306 // hand into the output location.
1307 const unsigned int n_components =
1308 fe_patch_values.get_fe().n_components();
1309 const unsigned int n_eval_points =
1310 fe_patch_values.n_quadrature_points;
1311
1312 std::vector<std::vector<Tensor<1, spacedim, ScalarType>>> tmp(
1313 n_eval_points);
1314 for (unsigned int i = 0; i < n_eval_points; ++i)
1315 tmp[i].resize(n_components);
1316
1317 fe_patch_values.get_function_gradients(vector, tmp);
1318
1319 AssertDimension(patch_gradients_system.size(), n_eval_points);
1320 for (unsigned int i = 0; i < n_eval_points; ++i)
1321 {
1322 AssertDimension(patch_gradients_system[i].size(), n_components);
1323
1324 for (unsigned int j = 0; j < n_components; ++j)
1325 patch_gradients_system[i][j] =
1326 get_component(tmp[i][j], extract_component);
1327 }
1328 }
1329 }
1330
1331
1332
1333 template <int dim, int spacedim, typename ScalarType>
1334 void
1335 DataEntry<dim, spacedim, ScalarType>::get_function_gradients(
1336 const FEValuesBase<dim, spacedim> &fe_patch_values,
1337 const ComponentExtractor extract_component,
1338 std::vector<Tensor<1, spacedim>> &patch_gradients) const
1339 {
1340 if constexpr (std::is_same_v<ScalarType, double>)
1341 {
1342 (void)extract_component;
1343 Assert(extract_component == ComponentExtractor::real_part,
1344 ExcMessage("You cannot extract anything other than the real "
1345 "part from a real number."));
1346
1347 fe_patch_values.get_function_gradients(vector, patch_gradients);
1348 }
1349 else
1350 {
1351 std::vector<Tensor<1, spacedim, ScalarType>> tmp;
1352 tmp.resize(patch_gradients.size());
1353
1354 fe_patch_values.get_function_gradients(vector, tmp);
1355
1356 for (unsigned int i = 0; i < tmp.size(); ++i)
1357 patch_gradients[i] = get_component(tmp[i], extract_component);
1358 }
1359 }
1360
1361
1362
1363 template <int dim, int spacedim, typename ScalarType>
1364 void
1365 DataEntry<dim, spacedim, ScalarType>::get_function_hessians(
1366 const FEValuesBase<dim, spacedim> &fe_patch_values,
1367 const ComponentExtractor extract_component,
1368 std::vector<std::vector<Tensor<2, spacedim>>> &patch_hessians_system)
1369 const
1370 {
1371 if constexpr (std::is_same_v<ScalarType, double>)
1372 {
1373 (void)extract_component;
1374 Assert(extract_component == ComponentExtractor::real_part,
1375 ExcMessage("You cannot extract anything other than the real "
1376 "part from a real number."));
1377
1378 fe_patch_values.get_function_hessians(vector, patch_hessians_system);
1379 }
1380 else
1381 {
1382 // OK, so we know that the data type stored by the user-provided
1383 // vector is not simply a double. In that case, we need to ask the
1384 // FEValuesBase object to extract function values for us from the
1385 // evaluation points in the provided data type, and then copy them by
1386 // hand into the output location.
1387 const unsigned int n_components =
1388 fe_patch_values.get_fe().n_components();
1389 const unsigned int n_eval_points =
1390 fe_patch_values.n_quadrature_points;
1391
1392 std::vector<std::vector<Tensor<2, spacedim, ScalarType>>> tmp(
1393 n_eval_points);
1394 for (unsigned int i = 0; i < n_eval_points; ++i)
1395 tmp[i].resize(n_components);
1396
1397 fe_patch_values.get_function_hessians(vector, tmp);
1398
1399 AssertDimension(patch_hessians_system.size(), n_eval_points);
1400 for (unsigned int i = 0; i < n_eval_points; ++i)
1401 {
1402 AssertDimension(patch_hessians_system[i].size(), n_components);
1403
1404 for (unsigned int j = 0; j < n_components; ++j)
1405 patch_hessians_system[i][j] =
1406 get_component(tmp[i][j], extract_component);
1407 }
1408 }
1409 }
1410
1411
1412
1413 template <int dim, int spacedim, typename ScalarType>
1414 void
1415 DataEntry<dim, spacedim, ScalarType>::get_function_hessians(
1416 const FEValuesBase<dim, spacedim> &fe_patch_values,
1417 const ComponentExtractor extract_component,
1418 std::vector<Tensor<2, spacedim>> &patch_hessians) const
1419 {
1420 if constexpr (std::is_same_v<ScalarType, double>)
1421 {
1422 (void)extract_component;
1423 Assert(extract_component == ComponentExtractor::real_part,
1424 ExcMessage("You cannot extract anything other than the real "
1425 "part from a real number."));
1426
1427 fe_patch_values.get_function_hessians(vector, patch_hessians);
1428 }
1429 else
1430 {
1431 std::vector<Tensor<2, spacedim, ScalarType>> tmp(
1432 patch_hessians.size());
1433
1434 fe_patch_values.get_function_hessians(vector, tmp);
1435
1436 for (unsigned int i = 0; i < tmp.size(); ++i)
1437 patch_hessians[i] = get_component(tmp[i], extract_component);
1438 }
1439 }
1440
1441
1442
1443 template <int dim, int spacedim, typename ScalarType>
1444 bool
1445 DataEntry<dim, spacedim, ScalarType>::is_complex_valued() const
1446 {
1447 return numbers::NumberTraits<ScalarType>::is_complex;
1448 }
1449
1450
1451
1452 template <int dim, int spacedim, typename ScalarType>
1453 std::size_t
1454 DataEntry<dim, spacedim, ScalarType>::memory_consumption() const
1455 {
1456 return (vector.memory_consumption() +
1457 MemoryConsumption::memory_consumption(this->names));
1458 }
1459
1460
1461
1462 template <int dim, int spacedim, typename ScalarType>
1463 void
1464 DataEntry<dim, spacedim, ScalarType>::clear()
1465 {
1466 vector.reinit(0, 0);
1467 this->dof_handler = nullptr;
1468 }
1469
1470
1471
1472 /**
1473 * Like DataEntry, but used to look up data from multigrid computations.
1474 * Data will use level-DoF indices to look up in a
1475 * MGLevelObject<VectorType> given on the specific level instead of
1476 * interpolating data to coarser cells.
1477 */
1478 template <int dim, int spacedim, typename ScalarType>
1479 class MGDataEntry : public DataEntryBase<dim, spacedim>
1480 {
1481 public:
1482 template <typename VectorType>
1483 MGDataEntry(const DoFHandler<dim, spacedim> *dofs,
1484 const MGLevelObject<VectorType> *vectors,
1485 const std::vector<std::string> &names,
1486 const std::vector<
1487 DataComponentInterpretation::DataComponentInterpretation>
1488 &data_component_interpretation)
1489 : DataEntryBase<dim, spacedim>(dofs,
1490 names,
1491 data_component_interpretation)
1492 {
1493 AssertDimension(vectors->min_level(), 0);
1494
1495 this->vectors.resize(vectors->min_level(), vectors->max_level());
1496
1497 for (unsigned int l = vectors->min_level(); l <= vectors->max_level();
1498 ++l)
1499 CreateVectors::create_dof_vector(*dofs,
1500 (*vectors)[l],
1501 this->vectors[l],
1502 l);
1503 }
1504
1505 virtual double
1506 get_cell_data_value(
1507 const unsigned int cell_number,
1508 const ComponentExtractor extract_component) const override;
1509
1510 virtual void
1511 get_function_values(const FEValuesBase<dim, spacedim> &fe_patch_values,
1512 const ComponentExtractor extract_component,
1513 std::vector<double> &patch_values) const override;
1514
1515 /**
1516 * Given a FEValuesBase object, extract the values on the present cell
1517 * from the vector we actually store. This function does the same as the
1518 * one above but for vector-valued finite elements.
1519 */
1520 virtual void
1521 get_function_values(const FEValuesBase<dim, spacedim> &fe_patch_values,
1522 const ComponentExtractor extract_component,
1523 std::vector<dealii::Vector<double>>
1524 &patch_values_system) const override;
1525
1526 /**
1527 * Given a FEValuesBase object, extract the gradients on the present
1528 * cell from the vector we actually store.
1529 */
1530 virtual void
1531 get_function_gradients(
1532 const FEValuesBase<dim, spacedim> & /*fe_patch_values*/,
1533 const ComponentExtractor /*extract_component*/,
1534 std::vector<Tensor<1, spacedim>> & /*patch_gradients*/) const override
1535 {
1536 DEAL_II_NOT_IMPLEMENTED();
1537 }
1538
1539 /**
1540 * Given a FEValuesBase object, extract the gradients on the present
1541 * cell from the vector we actually store. This function does the same
1542 * as the one above but for vector-valued finite elements.
1543 */
1544 virtual void
1545 get_function_gradients(
1546 const FEValuesBase<dim, spacedim> & /*fe_patch_values*/,
1547 const ComponentExtractor /*extract_component*/,
1548 std::vector<std::vector<Tensor<1, spacedim>>>
1549 & /*patch_gradients_system*/) const override
1550 {
1551 DEAL_II_NOT_IMPLEMENTED();
1552 }
1553
1554
1555 /**
1556 * Given a FEValuesBase object, extract the second derivatives on the
1557 * present cell from the vector we actually store.
1558 */
1559 virtual void
1560 get_function_hessians(
1561 const FEValuesBase<dim, spacedim> & /*fe_patch_values*/,
1562 const ComponentExtractor /*extract_component*/,
1563 std::vector<Tensor<2, spacedim>> & /*patch_hessians*/) const override
1564 {
1565 DEAL_II_NOT_IMPLEMENTED();
1566 }
1567
1568 /**
1569 * Given a FEValuesBase object, extract the second derivatives on the
1570 * present cell from the vector we actually store. This function does
1571 * the same as the one above but for vector-valued finite elements.
1572 */
1573 virtual void
1574 get_function_hessians(
1575 const FEValuesBase<dim, spacedim> & /*fe_patch_values*/,
1576 const ComponentExtractor /*extract_component*/,
1577 std::vector<std::vector<Tensor<2, spacedim>>>
1578 & /*patch_hessians_system*/) const override
1579 {
1580 DEAL_II_NOT_IMPLEMENTED();
1581 }
1582
1583 /**
1584 * Return whether the data represented by (a derived class of) this object
1585 * represents a complex-valued (as opposed to real-valued) information.
1586 */
1587 virtual bool
1588 is_complex_valued() const override
1589 {
1590 Assert(numbers::NumberTraits<ScalarType>::is_complex == false,
1591 ExcNotImplemented());
1592 return numbers::NumberTraits<ScalarType>::is_complex;
1593 }
1594
1595 /**
1596 * Clear all references to the vectors.
1597 */
1598 virtual void
1599 clear() override
1600 {
1601 vectors.clear();
1602 }
1603
1604 /**
1605 * Determine an estimate for the memory consumption (in bytes) of this
1606 * object.
1607 */
1608 virtual std::size_t
1609 memory_consumption() const override
1610 {
1611 return sizeof(vectors);
1612 }
1613
1614 private:
1615 MGLevelObject<LinearAlgebra::distributed::BlockVector<ScalarType>>
1616 vectors;
1617
1618 /**
1619 * Extract the @p indices from @p vector and put them into @p values.
1620 */
1621 void
1622 extract(const LinearAlgebra::distributed::BlockVector<ScalarType> &vector,
1623 const std::vector<types::global_dof_index> &indices,
1624 const ComponentExtractor extract_component,
1625 std::vector<double> &values) const
1626 {
1627 for (unsigned int i = 0; i < values.size(); ++i)
1628 values[i] = get_component(vector[indices[i]], extract_component);
1629 }
1630 };
1631
1632
1633
1634 template <int dim, int spacedim, typename ScalarType>
1635 double
1636 MGDataEntry<dim, spacedim, ScalarType>::get_cell_data_value(
1637 const unsigned int /*cell_number*/,
1638 const ComponentExtractor /*extract_component*/) const
1639 {
1640 DEAL_II_NOT_IMPLEMENTED();
1641
1642 return 0.0;
1643 }
1644
1645
1646
1647 template <int dim, int spacedim, typename ScalarType>
1648 void
1649 MGDataEntry<dim, spacedim, ScalarType>::get_function_values(
1650 const FEValuesBase<dim, spacedim> &fe_patch_values,
1651 const ComponentExtractor extract_component,
1652 std::vector<double> &patch_values) const
1653 {
1654 Assert(extract_component == ComponentExtractor::real_part,
1655 ExcNotImplemented());
1656
1657 const typename DoFHandler<dim, spacedim>::level_cell_iterator dof_cell(
1658 &fe_patch_values.get_cell()->get_triangulation(),
1659 fe_patch_values.get_cell()->level(),
1660 fe_patch_values.get_cell()->index(),
1661 this->dof_handler);
1662
1663 const auto *vector = &(vectors[dof_cell->level()]);
1664
1665 const unsigned int dofs_per_cell =
1666 this->dof_handler->get_fe(0).n_dofs_per_cell();
1667
1668 std::vector<types::global_dof_index> dof_indices(dofs_per_cell);
1669 dof_cell->get_mg_dof_indices(dof_indices);
1670 std::vector<double> values(dofs_per_cell);
1671 extract(*vector, dof_indices, extract_component, values);
1672 std::fill(patch_values.begin(), patch_values.end(), 0.0);
1673
1674 for (unsigned int i = 0; i < dofs_per_cell; ++i)
1675 for (unsigned int q_point = 0; q_point < patch_values.size(); ++q_point)
1676 patch_values[q_point] +=
1677 values[i] * fe_patch_values.shape_value(i, q_point);
1678 }
1679
1680
1681
1682 template <int dim, int spacedim, typename ScalarType>
1683 void
1684 MGDataEntry<dim, spacedim, ScalarType>::get_function_values(
1685 const FEValuesBase<dim, spacedim> &fe_patch_values,
1686 const ComponentExtractor extract_component,
1687 std::vector<dealii::Vector<double>> &patch_values_system) const
1688 {
1689 Assert(extract_component == ComponentExtractor::real_part,
1690 ExcNotImplemented());
1691
1692 typename DoFHandler<dim, spacedim>::level_cell_iterator dof_cell(
1693 &fe_patch_values.get_cell()->get_triangulation(),
1694 fe_patch_values.get_cell()->level(),
1695 fe_patch_values.get_cell()->index(),
1696 this->dof_handler);
1697
1698 const auto *vector = &(vectors[dof_cell->level()]);
1699
1700 const unsigned int dofs_per_cell =
1701 this->dof_handler->get_fe(0).n_dofs_per_cell();
1702
1703 std::vector<types::global_dof_index> dof_indices(dofs_per_cell);
1704 dof_cell->get_mg_dof_indices(dof_indices);
1705 std::vector<double> values(dofs_per_cell);
1706 extract(*vector, dof_indices, extract_component, values);
1707
1708 const unsigned int n_components = fe_patch_values.get_fe().n_components();
1709 const unsigned int n_eval_points = fe_patch_values.n_quadrature_points;
1710
1711 AssertDimension(patch_values_system.size(), n_eval_points);
1712 for (unsigned int q = 0; q < n_eval_points; ++q)
1713 {
1714 AssertDimension(patch_values_system[q].size(), n_components);
1715 patch_values_system[q] = 0.0;
1716
1717 for (unsigned int i = 0; i < dofs_per_cell; ++i)
1718 for (unsigned int c = 0; c < n_components; ++c)
1719 patch_values_system[q](c) +=
1720 values[i] * fe_patch_values.shape_value_component(i, q, c);
1721 }
1722 }
1723
1724 } // namespace DataOutImplementation
1725 } // namespace internal
1726
1727
1728
1729 template <int dim, int patch_dim, int spacedim, int patch_spacedim>
1730 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::DataOut_DoFData()
1731 : triangulation(nullptr, typeid(*this).name())
1732 , dofs(nullptr, typeid(*this).name())
1733 {}
1734
1735
1736
1737 template <int dim, int patch_dim, int spacedim, int patch_spacedim>
1738 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::~DataOut_DoFData()
1739 {
1740 // virtual functions called in constructors and destructors never use the
1741 // override in a derived class for clarity be explicit on which function is
1742 // called
1743 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::clear();
1744 }
1745
1746
1747
1748 template <int dim, int patch_dim, int spacedim, int patch_spacedim>
1749 void
1750 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::attach_dof_handler(
1751 const DoFHandler<dim, spacedim> &d)
1752 {
1753 Assert(dof_data.empty(),
1754 Exceptions::DataOutImplementation::ExcOldDataStillPresent());
1755 Assert(cell_data.empty(),
1756 Exceptions::DataOutImplementation::ExcOldDataStillPresent());
1757
1758 triangulation =
1759 ObserverPointer<const Triangulation<dim, spacedim>>(&d.get_triangulation(),
1760 typeid(*this).name());
1761 dofs =
1762 ObserverPointer<const DoFHandler<dim, spacedim>>(&d, typeid(*this).name());
1763 }
1764
1765
1766
1767 template <int dim, int patch_dim, int spacedim, int patch_spacedim>
1768 void
1769 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::attach_triangulation(
1770 const Triangulation<dim, spacedim> &tria)
1771 {
1772 Assert(dof_data.empty(),
1773 Exceptions::DataOutImplementation::ExcOldDataStillPresent());
1774 Assert(cell_data.empty(),
1775 Exceptions::DataOutImplementation::ExcOldDataStillPresent());
1776
1777 triangulation =
1778 ObserverPointer<const Triangulation<dim, spacedim>>(&tria,
1779 typeid(*this).name());
1780 }
1781
1782
1783
1784 template <int dim, int patch_dim, int spacedim, int patch_spacedim>
1785 template <typename VectorType>
1786 void
1787 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::add_data_vector(
1788 const DoFHandler<dim, spacedim> &dof_handler,
1789 const VectorType &vec,
1790 const DataPostprocessor<spacedim> &data_postprocessor)
1791 {
1792 // this is a specialized version of the other function where we have a
1793 // postprocessor. if we do, we know that we have type_dof_data, which makes
1794 // things a bit simpler, we also don't need to deal with some of the other
1795 // stuff and use a different constructor of DataEntry
1796 if (triangulation != nullptr)
1797 {
1798 Assert(&dof_handler.get_triangulation() == triangulation,
1799 ExcMessage("The triangulation attached to the DoFHandler does not "
1800 "match with the one set previously"));
1801 }
1802 else
1803 {
1804 triangulation = ObserverPointer<const Triangulation<dim, spacedim>>(
1805 &dof_handler.get_triangulation(), typeid(*this).name());
1806 }
1807
1808 Assert(vec.size() == dof_handler.n_dofs(),
1809 Exceptions::DataOutImplementation::ExcInvalidVectorSize(
1810 vec.size(),
1811 dof_handler.n_dofs(),
1812 dof_handler.get_triangulation().n_active_cells()));
1813
1814
1815 auto new_entry = std::make_unique<internal::DataOutImplementation::DataEntry<
1816 dim,
1817 spacedim,
1818 typename numbers::NumberTraits<
1819 typename VectorType::value_type>::double_type>>(&dof_handler,
1820 &vec,
1821 &data_postprocessor);
1822 dof_data.emplace_back(std::move(new_entry));
1823 }
1824
1825
1826
1827 template <int dim, int patch_dim, int spacedim, int patch_spacedim>
1828 template <typename VectorType>
1829 void
1830 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::
1831 add_data_vector_internal(
1832 const DoFHandler<dim, spacedim> *dof_handler,
1833 const VectorType &data_vector,
1834 const std::vector<std::string> &names,
1835 const DataVectorType type,
1836 const std::vector<DataComponentInterpretation::DataComponentInterpretation>
1837 &data_component_interpretation_,
1838 const bool deduce_output_names)
1839 {
1840 // Check available mesh information:
1841 if (triangulation == nullptr)
1842 {
1843 Assert(dof_handler != nullptr, ExcInternalError());
1844 triangulation = ObserverPointer<const Triangulation<dim, spacedim>>(
1845 &dof_handler->get_triangulation(), typeid(*this).name());
1846 }
1847
1848 if (dof_handler != nullptr)
1849 {
1850 Assert(&dof_handler->get_triangulation() == triangulation,
1851 ExcMessage("The triangulation attached to the DoFHandler does not "
1852 "match with the one set previously"));
1853 }
1854
1855 // Figure out the data type:
1856 DataVectorType actual_type = type;
1857 if (type == type_automatic)
1858 {
1859 Assert(
1860 (dof_handler == nullptr) ||
1861 (triangulation->n_active_cells() != dof_handler->n_dofs()),
1862 ExcMessage(
1863 "Unable to determine the type of vector automatically because the number of DoFs "
1864 "is equal to the number of cells. Please specify DataVectorType."));
1865
1866 if (data_vector.size() == triangulation->n_active_cells())
1867 actual_type = type_cell_data;
1868 else
1869 actual_type = type_dof_data;
1870 }
1871 Assert(actual_type == type_cell_data || actual_type == type_dof_data,
1872 ExcInternalError());
1873
1874 // If necessary, append '_1', '_2', etc. to component names:
1875 std::vector<std::string> deduced_names;
1876 if (deduce_output_names && actual_type == type_dof_data)
1877 {
1878 Assert(names.size() == 1, ExcInternalError());
1879 Assert(dof_handler != nullptr, ExcInternalError());
1880 Assert(dof_handler->n_dofs() > 0,
1881 ExcMessage("The DoF handler attached to the current output vector "
1882 "does not have any degrees of freedom, so it is not "
1883 "possible to output DoF data in this context."));
1884 const std::string &name = names[0];
1885 const unsigned int n_components = dof_handler->get_fe(0).n_components();
1886 deduced_names.resize(n_components);
1887 if (n_components > 1)
1888 {
1889 for (unsigned int i = 0; i < n_components; ++i)
1890 {
1891 deduced_names[i] = name + '_' + std::to_string(i);
1892 }
1893 }
1894 else
1895 {
1896 deduced_names[0] = name;
1897 }
1898 }
1899 else
1900 {
1901 deduced_names = names;
1902 }
1903
1904 // Check that things have the right sizes for the data type:
1905 switch (actual_type)
1906 {
1907 case type_cell_data:
1908 Assert(data_vector.size() == triangulation->n_active_cells(),
1909 ExcDimensionMismatch(data_vector.size(),
1910 triangulation->n_active_cells()));
1911 Assert(deduced_names.size() == 1,
1912 Exceptions::DataOutImplementation::ExcInvalidNumberOfNames(
1913 deduced_names.size(), 1));
1914 break;
1915 case type_dof_data:
1916 Assert(dof_handler != nullptr,
1917 Exceptions::DataOutImplementation::ExcNoDoFHandlerSelected());
1918 Assert(data_vector.size() == dof_handler->n_dofs(),
1919 Exceptions::DataOutImplementation::ExcInvalidVectorSize(
1920 data_vector.size(),
1921 dof_handler->n_dofs(),
1922 triangulation->n_active_cells()));
1923 Assert(deduced_names.size() == dof_handler->get_fe(0).n_components(),
1924 Exceptions::DataOutImplementation::ExcInvalidNumberOfNames(
1925 deduced_names.size(), dof_handler->get_fe(0).n_components()));
1926 break;
1927 default:
1928 DEAL_II_ASSERT_UNREACHABLE();
1929 }
1930
1931 const auto &data_component_interpretation =
1932 (data_component_interpretation_.size() != 0 ?
1933 data_component_interpretation_ :
1934 std::vector<DataComponentInterpretation::DataComponentInterpretation>(
1935 deduced_names.size(),
1936 DataComponentInterpretation::component_is_scalar));
1937
1938 // finally, add the data vector:
1939 auto new_entry = std::make_unique<internal::DataOutImplementation::DataEntry<
1940 dim,
1941 spacedim,
1942 typename numbers::NumberTraits<
1943 typename VectorType::value_type>::double_type>>(
1944 dof_handler,
1945 &data_vector,
1946 deduced_names,
1947 data_component_interpretation,
1948 actual_type);
1949
1950 if (actual_type == type_dof_data)
1951 dof_data.emplace_back(std::move(new_entry));
1952 else
1953 cell_data.emplace_back(std::move(new_entry));
1954 }
1955
1956
1957
1958 template <int dim, int patch_dim, int spacedim, int patch_spacedim>
1959 template <typename VectorType>
1960 void
1961 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::add_mg_data_vector(
1962 const DoFHandler<dim, spacedim> &dof_handler,
1963 const MGLevelObject<VectorType> &data,
1964 const std::string &name)
1965 {
1966 // forward the call to the vector version:
1967 std::vector<std::string> names(1, name);
1968 add_mg_data_vector(dof_handler, data, names);
1969 }
1970
1971
1972
1973 template <int dim, int patch_dim, int spacedim, int patch_spacedim>
1974 template <typename VectorType>
1975 void
1976 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::add_mg_data_vector(
1977 const DoFHandler<dim, spacedim> &dof_handler,
1978 const MGLevelObject<VectorType> &data,
1979 const std::vector<std::string> &names,
1980 const std::vector<DataComponentInterpretation::DataComponentInterpretation>
1981 &data_component_interpretation_)
1982 {
1983 if (triangulation == nullptr)
1984 triangulation = ObserverPointer<const Triangulation<dim, spacedim>>(
1985 &dof_handler.get_triangulation(), typeid(*this).name());
1986
1987 Assert(&dof_handler.get_triangulation() == triangulation,
1988 ExcMessage("The triangulation attached to the DoFHandler does not "
1989 "match with the one set previously"));
1990
1991 const unsigned int n_components = dof_handler.get_fe(0).n_components();
1992 std::vector<std::string> deduced_names = names;
1993
1994 if (names.size() == 1 && n_components > 1)
1995 {
1996 deduced_names.resize(n_components);
1997 for (unsigned int i = 0; i < n_components; ++i)
1998 {
1999 deduced_names[i] = names[0] + '_' + std::to_string(i);
2000 }
2001 }
2002
2003 Assert(deduced_names.size() == n_components,
2004 ExcMessage("Invalid number of names given."));
2005
2006 const std::vector<DataComponentInterpretation::DataComponentInterpretation>
2007 &data_component_interpretation =
2008 (data_component_interpretation_.size() != 0 ?
2009 data_component_interpretation_ :
2010 std::vector<DataComponentInterpretation::DataComponentInterpretation>(
2011 n_components, DataComponentInterpretation::component_is_scalar));
2012
2013 Assert(data_component_interpretation.size() == n_components,
2014 ExcMessage(
2015 "Invalid number of entries in data_component_interpretation."));
2016
2017 auto new_entry = std::make_unique<
2018 internal::DataOutImplementation::
2019 MGDataEntry<dim, spacedim, typename VectorType::value_type>>(
2020 &dof_handler, &data, deduced_names, data_component_interpretation);
2021 dof_data.emplace_back(std::move(new_entry));
2022 }
2023
2024
2025
2026 template <int dim, int patch_dim, int spacedim, int patch_spacedim>
2027 void
2028 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::clear_data_vectors()
2029 {
2030 dof_data.erase(dof_data.begin(), dof_data.end());
2031 cell_data.erase(cell_data.begin(), cell_data.end());
2032
2033 // delete patches
2034 std::vector<Patch> dummy;
2035 patches.swap(dummy);
2036 }
2037
2038
2039
2040 template <int dim, int patch_dim, int spacedim, int patch_spacedim>
2041 void
2042 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::
2043 clear_input_data_references()
2044 {
2045 for (unsigned int i = 0; i < dof_data.size(); ++i)
2046 dof_data[i]->clear();
2047
2048 for (unsigned int i = 0; i < cell_data.size(); ++i)
2049 cell_data[i]->clear();
2050
2051 if (dofs != nullptr)
2052 dofs = nullptr;
2053 }
2054
2055
2056
2057 template <int dim, int patch_dim, int spacedim, int patch_spacedim>
2058 void
2059 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::clear()
2060 {
2061 dof_data.erase(dof_data.begin(), dof_data.end());
2062 cell_data.erase(cell_data.begin(), cell_data.end());
2063
2064 if (dofs != nullptr)
2065 dofs = nullptr;
2066
2067 // delete patches
2068 std::vector<Patch> dummy;
2069 patches.swap(dummy);
2070 }
2071
2072
2073
2074 template <int dim, int patch_dim, int spacedim, int patch_spacedim>
2075 std::vector<std::string>
2076 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::get_dataset_names()
2077 const
2078 {
2079 std::vector<std::string> names;
2080
2081 // Loop over all DoF-data datasets and push the names. If the
2082 // vector underlying a data set is complex-valued, then
2083 // expand it into its real and imaginary part. Note, however,
2084 // that what comes back from a postprocessor is *always*
2085 // real-valued, regardless of what goes in, so we don't
2086 // have this to do this name expansion for data sets that
2087 // have a postprocessor.
2088 //
2089 // The situation is made complicated when considering vector- and
2090 // tensor-valued component sets. This is because if, for example, we have a
2091 // complex-valued vector, we don't want to output Re(u_x), then Im(u_x), then
2092 // Re(u_y), etc. That's because if we did this, then visualization programs
2093 // will not easily be able to patch together the 1st, 3rd, 5th components into
2094 // the vector representing the real part of a vector field, and similarly for
2095 // the 2nd, 4th, 6th component for the imaginary part of the vector field.
2096 // Rather, we need to put all real components of the same vector field into
2097 // consecutive components.
2098 //
2099 // This sort of logic is also explained in some detail in
2100 // DataOut::build_one_patch().
2101 for (const auto &input_data : dof_data)
2102 if (input_data->is_complex_valued() == false ||
2103 (input_data->postprocessor != nullptr))
2104 {
2105 for (const auto &name : input_data->names)
2106 names.push_back(name);
2107 }
2108 else
2109 {
2110 // OK, so we have a complex-valued vector. We then need to go through
2111 // all components and order them appropriately
2112 for (unsigned int i = 0; i < input_data->names.size();
2113 /* increment of i happens below */)
2114 {
2115 switch (input_data->data_component_interpretation[i])
2116 {
2117 case DataComponentInterpretation::component_is_scalar:
2118 {
2119 // It's a scalar. Just output real and imaginary parts one
2120 // after the other:
2121 names.push_back(input_data->names[i] + "_re");
2122 names.push_back(input_data->names[i] + "_im");
2123
2124 // Move forward by one component
2125 ++i;
2126
2127 break;
2128 }
2129
2130 case DataComponentInterpretation::component_is_part_of_vector:
2131 {
2132 // It's a vector. First output all real parts, then all
2133 // imaginary parts:
2134 const unsigned int size = patch_spacedim;
2135 for (unsigned int vec_comp = 0; vec_comp < size; ++vec_comp)
2136 names.push_back(input_data->names[i + vec_comp] + "_re");
2137 for (unsigned int vec_comp = 0; vec_comp < size; ++vec_comp)
2138 names.push_back(input_data->names[i + vec_comp] + "_im");
2139
2140 // Move forward by dim components
2141 i += size;
2142
2143 break;
2144 }
2145
2146 case DataComponentInterpretation::component_is_part_of_tensor:
2147 {
2148 // It's a tensor. First output all real parts, then all
2149 // imaginary parts:
2150 const unsigned int size = patch_spacedim * patch_spacedim;
2151 for (unsigned int tensor_comp = 0; tensor_comp < size;
2152 ++tensor_comp)
2153 names.push_back(input_data->names[i + tensor_comp] +
2154 "_re");
2155 for (unsigned int tensor_comp = 0; tensor_comp < size;
2156 ++tensor_comp)
2157 names.push_back(input_data->names[i + tensor_comp] +
2158 "_im");
2159
2160 // Move forward by dim*dim components
2161 i += size;
2162
2163 break;
2164 }
2165
2166 default:
2167 DEAL_II_ASSERT_UNREACHABLE();
2168 }
2169 }
2170 }
2171
2172 // Do the same as above for cell-type data. This is simpler because it
2173 // is always scalar, and so we don't have to worry about whether some
2174 // components together form vectors or tensors.
2175 for (const auto &input_data : cell_data)
2176 {
2177 Assert(input_data->names.size() == 1, ExcInternalError());
2178 if ((input_data->is_complex_valued() == false) ||
2179 (input_data->postprocessor != nullptr))
2180 names.push_back(input_data->names[0]);
2181 else
2182 {
2183 names.push_back(input_data->names[0] + "_re");
2184 names.push_back(input_data->names[0] + "_im");
2185 }
2186 }
2187
2188 return names;
2189 }
2190
2191
2192
2193 template <int dim, int patch_dim, int spacedim, int patch_spacedim>
2194 std::vector<
2195 std::tuple<unsigned int,
2196 unsigned int,
2197 std::string,
2198 DataComponentInterpretation::DataComponentInterpretation>>
2199 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::
2200 get_nonscalar_data_ranges() const
2201 {
2202 std::vector<
2203 std::tuple<unsigned int,
2204 unsigned int,
2205 std::string,
2206 DataComponentInterpretation::DataComponentInterpretation>>
2207 ranges;
2208
2209 // collect the ranges of dof and cell data
2210 unsigned int output_component = 0;
2211 for (const auto &input_data : dof_data)
2212 for (unsigned int i = 0; i < input_data->n_output_variables;
2213 /* i is updated below */)
2214 // see what kind of data we have here. note that for the purpose of the
2215 // current function all we care about is vector data
2216 switch (input_data->data_component_interpretation[i])
2217 {
2218 case DataComponentInterpretation::component_is_scalar:
2219 {
2220 // Just move component forward by one (or two if the
2221 // component happens to be complex-valued and we don't use a
2222 // postprocessor)
2223 // -- postprocessors always return real-valued things)
2224 ++i;
2225 output_component += (input_data->is_complex_valued() &&
2226 (input_data->postprocessor == nullptr) ?
2227 2 :
2228 1);
2229
2230 break;
2231 }
2232
2233 case DataComponentInterpretation::component_is_part_of_vector:
2234 {
2235 // ensure that there is a continuous number of next space_dim
2236 // components that all deal with vectors
2237 Assert(
2238 i + patch_spacedim <= input_data->n_output_variables,
2239 Exceptions::DataOutImplementation::ExcInvalidVectorDeclaration(
2240 i, input_data->names[i]));
2241 for (unsigned int dd = 1; dd < patch_spacedim; ++dd)
2242 Assert(
2243 input_data->data_component_interpretation[i + dd] ==
2244 DataComponentInterpretation::component_is_part_of_vector,
2245 Exceptions::DataOutImplementation::
2246 ExcInvalidVectorDeclaration(i, input_data->names[i]));
2247
2248 // all seems right, so figure out whether there is a common
2249 // name to these components. if not, leave the name empty and
2250 // let the output format writer decide what to do here
2251 std::string name = input_data->names[i];
2252 for (unsigned int dd = 1; dd < patch_spacedim; ++dd)
2253 if (name != input_data->names[i + dd])
2254 {
2255 name = "";
2256 break;
2257 }
2258
2259 // Finally add a corresponding range. If this is a real-valued
2260 // vector, then we only need to do this once. But if it is a
2261 // complex-valued vector and it is not postprocessed, then we need
2262 // to do it twice -- once for the real parts and once for the
2263 // imaginary parts
2264 //
2265 // This sort of logic is also explained in some detail in
2266 // DataOut::build_one_patch().
2267 if (input_data->is_complex_valued() == false ||
2268 (input_data->postprocessor != nullptr))
2269 {
2270 ranges.emplace_back(std::forward_as_tuple(
2271 output_component,
2272 output_component + patch_spacedim - 1,
2273 name,
2274 DataComponentInterpretation::component_is_part_of_vector));
2275
2276 // increase the 'component' counter by the appropriate amount,
2277 // same for 'i', since we have already dealt with all these
2278 // components
2279 output_component += patch_spacedim;
2280 i += patch_spacedim;
2281 }
2282 else
2283 {
2284 ranges.emplace_back(std::forward_as_tuple(
2285 output_component,
2286 output_component + patch_spacedim - 1,
2287 name + "_re",
2288 DataComponentInterpretation::component_is_part_of_vector));
2289 output_component += patch_spacedim;
2290
2291 ranges.emplace_back(std::forward_as_tuple(
2292 output_component,
2293 output_component + patch_spacedim - 1,
2294 name + "_im",
2295 DataComponentInterpretation::component_is_part_of_vector));
2296 output_component += patch_spacedim;
2297
2298 i += patch_spacedim;
2299 }
2300
2301
2302 break;
2303 }
2304
2305 case DataComponentInterpretation::component_is_part_of_tensor:
2306 {
2307 const unsigned int size = patch_spacedim * patch_spacedim;
2308 // ensure that there is a continuous number of next
2309 // space_dim*space_dim components that all deal with tensors
2310 Assert(
2311 i + size <= input_data->n_output_variables,
2312 Exceptions::DataOutImplementation::ExcInvalidTensorDeclaration(
2313 i, input_data->names[i]));
2314 for (unsigned int dd = 1; dd < size; ++dd)
2315 Assert(
2316 input_data->data_component_interpretation[i + dd] ==
2317 DataComponentInterpretation::component_is_part_of_tensor,
2318 Exceptions::DataOutImplementation::
2319 ExcInvalidTensorDeclaration(i, input_data->names[i]));
2320
2321 // all seems right, so figure out whether there is a common
2322 // name to these components. if not, leave the name empty and
2323 // let the output format writer decide what to do here
2324 std::string name = input_data->names[i];
2325 for (unsigned int dd = 1; dd < size; ++dd)
2326 if (name != input_data->names[i + dd])
2327 {
2328 name = "";
2329 break;
2330 }
2331
2332 // Finally add a corresponding range. If this is a real-valued
2333 // tensor, then we only need to do this once. But if it is a
2334 // complex-valued tensor and it is not postprocessed, then we need
2335 // to do it twice -- once for the real parts and once for the
2336 // imaginary parts
2337 //
2338 // This sort of logic is also explained in some detail in
2339 // DataOut::build_one_patch().
2340 if (input_data->is_complex_valued() == false ||
2341 (input_data->postprocessor != nullptr))
2342 {
2343 ranges.emplace_back(std::forward_as_tuple(
2344 output_component,
2345 output_component + size - 1,
2346 name,
2347 DataComponentInterpretation::component_is_part_of_tensor));
2348
2349 // increase the 'component' counter by the appropriate amount,
2350 // same for 'i', since we have already dealt with all these
2351 // components
2352 output_component += size;
2353 i += size;
2354 }
2355 else
2356 {
2357 ranges.emplace_back(std::forward_as_tuple(
2358 output_component,
2359 output_component + size - 1,
2360 name + "_re",
2361 DataComponentInterpretation::component_is_part_of_tensor));
2362 output_component += size;
2363
2364 ranges.emplace_back(std::forward_as_tuple(
2365 output_component,
2366 output_component + size - 1,
2367 name + "_im",
2368 DataComponentInterpretation::component_is_part_of_tensor));
2369 output_component += size;
2370
2371 i += size;
2372 }
2373 break;
2374 }
2375
2376 default:
2377 DEAL_II_NOT_IMPLEMENTED();
2378 }
2379
2380 // note that we do not have to traverse the list of cell data here because
2381 // cell data is one value per (logical) cell and therefore cannot be a
2382 // vector
2383
2384 return ranges;
2385 }
2386
2387
2388
2389 template <int dim, int patch_dim, int spacedim, int patch_spacedim>
2390 const std::vector<dealii::DataOutBase::Patch<patch_dim, patch_spacedim>> &
2391 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::get_patches() const
2392 {
2393 return patches;
2394 }
2395
2396
2397
2398 template <int dim, int patch_dim, int spacedim, int patch_spacedim>
2399 std::vector<std::shared_ptr<dealii::hp::FECollection<dim, spacedim>>>
2400 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::get_fes() const
2401 {
2402 std::vector<std::shared_ptr<dealii::hp::FECollection<dim, spacedim>>>
2403 finite_elements(this->dof_data.size());
2404 for (unsigned int i = 0; i < this->dof_data.size(); ++i)
2405 {
2406 Assert(dof_data[i]->dof_handler != nullptr,
2407 Exceptions::DataOutImplementation::ExcNoDoFHandlerSelected());
2408
2409 // avoid creating too many finite elements and doing a lot of work on
2410 // initializing FEValues downstream: if two DoFHandlers are the same
2411 // (checked by pointer comparison), we can re-use the shared_ptr object
2412 // for the second one. We cannot check for finite element equalities
2413 // because we need different FEValues objects for different dof
2414 // handlers.
2415 bool duplicate = false;
2416 for (unsigned int j = 0; j < i; ++j)
2417 if (dof_data[i]->dof_handler == dof_data[j]->dof_handler)
2418 {
2419 finite_elements[i] = finite_elements[j];
2420 duplicate = true;
2421 }
2422 if (duplicate == false)
2423 finite_elements[i] =
2424 std::make_shared<dealii::hp::FECollection<dim, spacedim>>(
2425 this->dof_data[i]->dof_handler->get_fe_collection());
2426 }
2427 if (this->dof_data.empty())
2428 {
2429 Assert(triangulation != nullptr, ExcNotImplemented());
2430
2431 const auto &reference_cells = triangulation->get_reference_cells();
2432 finite_elements.reserve(reference_cells.size());
2433
2434 // TODO: below we select linear, discontinuous elements for simplex,
2435 // wedge, and pyramid; we would like to select constant functions, but
2436 // that is not implemented yet
2437 for (const auto &reference_cell : reference_cells)
2438 {
2439 if (reference_cell.is_hyper_cube())
2440 finite_elements.emplace_back(
2441 std::make_shared<dealii::hp::FECollection<dim, spacedim>>(
2442 FE_DGQ<dim, spacedim>(0)));
2443 else if (reference_cell.is_simplex())
2444 finite_elements.emplace_back(
2445 std::make_shared<dealii::hp::FECollection<dim, spacedim>>(
2446 FE_SimplexDGP<dim, spacedim>(1)));
2447 else if (reference_cell == ReferenceCells::Wedge)
2448 finite_elements.emplace_back(
2449 std::make_shared<dealii::hp::FECollection<dim, spacedim>>(
2450 FE_WedgeDGP<dim, spacedim>(1)));
2451 else if (reference_cell == ReferenceCells::Pyramid)
2452 finite_elements.emplace_back(
2453 std::make_shared<dealii::hp::FECollection<dim, spacedim>>(
2454 FE_PyramidDGP<dim, spacedim>(1)));
2455 else
2456 DEAL_II_NOT_IMPLEMENTED();
2457 }
2458 }
2459 return finite_elements;
2460 }
2461
2462
2463
2464 template <int dim, int patch_dim, int spacedim, int patch_spacedim>
2465 std::size_t
2466 DataOut_DoFData<dim, patch_dim, spacedim, patch_spacedim>::memory_consumption()
2467 const
2468 {
2469 return (DataOutInterface<patch_dim, patch_spacedim>::memory_consumption() +
2470 MemoryConsumption::memory_consumption(dofs) +
2471 MemoryConsumption::memory_consumption(patches));
2472 }
2473
2474 DEAL_II_NAMESPACE_CLOSE
2475
2476 #endif