Debian Codegraph @

include/deal.II/numerics/solution_transfer.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_solution_transfer_templates_h
16 #define dealii_solution_transfer_templates_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/memory_consumption.h>
21
22 #include <deal.II/distributed/shared_tria.h>
23 #include <deal.II/distributed/tria.h>
24
25 #include <deal.II/dofs/dof_accessor.h>
26 #include <deal.II/dofs/dof_handler.h>
27 #include <deal.II/dofs/dof_tools.h>
28
29 #include <deal.II/fe/fe.h>
30
31 #include <deal.II/grid/tria.h>
32 #include <deal.II/grid/tria_accessor.h>
33 #include <deal.II/grid/tria_iterator.h>
34
35 #include <deal.II/lac/block_vector.h>
36 #include <deal.II/lac/la_parallel_block_vector.h>
37 #include <deal.II/lac/la_parallel_vector.h>
38 #include <deal.II/lac/petsc_block_vector.h>
39 #include <deal.II/lac/petsc_vector.h>
40 #include <deal.II/lac/trilinos_parallel_block_vector.h>
41 #include <deal.II/lac/trilinos_vector.h>
42 #include <deal.II/lac/vector.h>
43 #include <deal.II/lac/vector_element_access.h>
44
45 #include <deal.II/numerics/solution_transfer.h>
46
47 #include <boost/range/iterator_range_core.hpp>
48
49 #include <functional>
50 #include <numeric>
51
52
53 DEAL_II_NAMESPACE_OPEN
54
55
56 namespace internal
57 {
58 namespace SolutionTransferImplementation
59 {
60 /**
61 * Optimized pack function for values assigned on degrees of freedom.
62 *
63 * Given that the elements of @p dof_values are stored in consecutive
64 * locations, we can just memcpy them. Since floating point values don't
65 * compress well, we also waive the compression that the default
66 * Utilities::pack() and Utilities::unpack() functions offer.
67 */
68 template <typename value_type>
69 std::vector<char>
70 pack_dof_values(std::vector<Vector<value_type>> &dof_values,
71 const unsigned int dofs_per_cell)
72 {
73 for (const auto &values : dof_values)
74 {
75 AssertDimension(values.size(), dofs_per_cell);
76 (void)values;
77 }
78
79 const std::size_t bytes_per_entry = sizeof(value_type) * dofs_per_cell;
80
81 std::vector<char> buffer(dof_values.size() * bytes_per_entry);
82 for (unsigned int i = 0; i < dof_values.size(); ++i)
83 std::memcpy(&buffer[i * bytes_per_entry],
84 &dof_values[i](0),
85 bytes_per_entry);
86
87 return buffer;
88 }
89
90
91
92 /**
93 * Optimized unpack function for values assigned on degrees of freedom.
94 */
95 template <typename value_type>
96 std::vector<Vector<value_type>>
97 unpack_dof_values(
98 const boost::iterator_range<std::vector<char>::const_iterator>
99 &data_range,
100 const unsigned int dofs_per_cell)
101 {
102 const std::size_t bytes_per_entry = sizeof(value_type) * dofs_per_cell;
103 const unsigned int n_elements = data_range.size() / bytes_per_entry;
104
105 Assert((data_range.size() % bytes_per_entry == 0), ExcInternalError());
106
107 std::vector<Vector<value_type>> unpacked_data;
108 unpacked_data.reserve(n_elements);
109 for (unsigned int i = 0; i < n_elements; ++i)
110 {
111 Vector<value_type> dof_values(dofs_per_cell);
112 std::memcpy(&dof_values(0),
113 &(*std::next(data_range.begin(), i * bytes_per_entry)),
114 bytes_per_entry);
115 unpacked_data.emplace_back(std::move(dof_values));
116 }
117
118 return unpacked_data;
119 }
120 } // namespace SolutionTransferImplementation
121 } // namespace internal
122
123
124
125 template <int dim, typename VectorType, int spacedim>
126 SolutionTransfer<dim, VectorType, spacedim>::SolutionTransfer(
127 const DoFHandler<dim, spacedim> &dof,
128 const bool average_values)
129 : dof_handler(&dof, typeid(*this).name())
130 , average_values(average_values)
131 , handle(numbers::invalid_unsigned_int)
132 {}
133
134
135
136 template <int dim, typename VectorType, int spacedim>
137 void
138 SolutionTransfer<dim, VectorType, spacedim>::
139 prepare_for_coarsening_and_refinement(
140 const std::vector<const VectorType *> &all_in)
141 {
142 const dealii::internal::parallel::shared::
143 TemporarilyRestoreSubdomainIds<dim, spacedim>
144 subdomain_modifier(dof_handler->get_triangulation());
145
146 for (unsigned int i = 0; i < all_in.size(); ++i)
147 Assert(all_in[i]->size() == dof_handler->n_dofs(),
148 ExcDimensionMismatch(all_in[i]->size(), dof_handler->n_dofs()));
149
150 input_vectors = all_in;
151 register_data_attach();
152 }
153
154
155
156 template <int dim, typename VectorType, int spacedim>
157 void
158 SolutionTransfer<dim, VectorType, spacedim>::
159 prepare_for_coarsening_and_refinement(const std::vector<VectorType> &all_in)
160 {
161 std::vector<const VectorType *> temp(all_in.size());
162
163 for (std::size_t i = 0; i < temp.size(); ++i)
164 temp[i] = &(all_in[i]);
165
166 this->prepare_for_coarsening_and_refinement(temp);
167 }
168
169
170
171 template <int dim, typename VectorType, int spacedim>
172 void
173 SolutionTransfer<dim, VectorType, spacedim>::register_data_attach()
174 {
175 // TODO: casting away constness is bad
176 auto tria = const_cast<dealii::Triangulation<dim, spacedim> *>(
177 &dof_handler->get_triangulation());
178 Assert(tria != nullptr, ExcInternalError());
179
180 Assert(handle == numbers::invalid_unsigned_int,
181 ExcMessage("You can only add one solution per "
182 "SolutionTransfer object."));
183
184 handle = tria->register_data_attach(
185 [this](const typename Triangulation<dim, spacedim>::cell_iterator &cell_,
186 const CellStatus status) {
187 return this->pack_callback(cell_, status);
188 },
189 /*returns_variable_size_data=*/dof_handler->has_hp_capabilities());
190 }
191
192
193
194 template <int dim, typename VectorType, int spacedim>
195 void
196 SolutionTransfer<dim, VectorType, spacedim>::
197 prepare_for_coarsening_and_refinement(const VectorType &in)
198 {
199 std::vector<const VectorType *> all_in(1, &in);
200 prepare_for_coarsening_and_refinement(all_in);
201 }
202
203
204
205 template <int dim, typename VectorType, int spacedim>
206 void
207 SolutionTransfer<dim, VectorType, spacedim>::prepare_for_serialization(
208 const VectorType &in)
209 {
210 std::vector<const VectorType *> all_in(1, &in);
211 prepare_for_serialization(all_in);
212 }
213
214
215
216 template <int dim, typename VectorType, int spacedim>
217 void
218 SolutionTransfer<dim, VectorType, spacedim>::prepare_for_serialization(
219 const std::vector<const VectorType *> &all_in)
220 {
221 prepare_for_coarsening_and_refinement(all_in);
222 }
223
224
225
226 template <int dim, typename VectorType, int spacedim>
227 void
228 SolutionTransfer<dim, VectorType, spacedim>::deserialize(VectorType &in)
229 {
230 std::vector<VectorType *> all_in(1, &in);
231 deserialize(all_in);
232 }
233
234
235
236 template <int dim, typename VectorType, int spacedim>
237 void
238 SolutionTransfer<dim, VectorType, spacedim>::deserialize(
239 std::vector<VectorType *> &all_in)
240 {
241 register_data_attach();
242
243 // this makes interpolate() happy
244 input_vectors.resize(all_in.size());
245
246 interpolate(all_in);
247 }
248
249
250 template <int dim, typename VectorType, int spacedim>
251 void
252 SolutionTransfer<dim, VectorType, spacedim>::interpolate(
253 std::vector<VectorType *> &all_out)
254 {
255 const dealii::internal::parallel::shared::
256 TemporarilyRestoreSubdomainIds<dim, spacedim>
257 subdomain_modifier(dof_handler->get_triangulation());
258
259 Assert(input_vectors.size() == all_out.size(),
260 ExcDimensionMismatch(input_vectors.size(), all_out.size()));
261 for (unsigned int i = 0; i < all_out.size(); ++i)
262 Assert(all_out[i]->size() == dof_handler->n_dofs(),
263 ExcDimensionMismatch(all_out[i]->size(), dof_handler->n_dofs()));
264
265 // TODO: casting away constness is bad
266 auto tria = const_cast<dealii::Triangulation<dim, spacedim> *>(
267 &dof_handler->get_triangulation());
268 Assert(tria != nullptr, ExcInternalError());
269 Assert(
270 handle != numbers::invalid_unsigned_int,
271 ExcMessage(
272 "You can only call interpolate() once per SolutionTransfer object."));
273
274 using Number = typename VectorType::value_type;
275
276 if (average_values)
277 for (auto *const vec : all_out)
278 *vec = Number();
279
280 VectorType valence;
281
282 // initialize valence vector only if we need to average
283 if (average_values)
284 valence.reinit(*all_out[0]);
285
286 tria->notify_ready_to_unpack(
287 handle,
288 [this, &all_out, &valence](
289 const typename Triangulation<dim, spacedim>::cell_iterator &cell_,
290 const CellStatus status,
291 const boost::iterator_range<std::vector<char>::const_iterator>
292 &data_range) {
293 this->unpack_callback(cell_, status, data_range, all_out, valence);
294 });
295
296 if (average_values)
297 {
298 // finalize valence: compress and invert
299 valence.compress(VectorOperation::add);
300 for (const auto i : valence.locally_owned_elements())
301 valence[i] = (static_cast<Number>(valence[i]) == Number() ?
302 Number() :
303 (Number(1.0) / static_cast<Number>(valence[i])));
304 valence.compress(VectorOperation::insert);
305
306 for (auto *const vec : all_out)
307 {
308 // compress and weight with valence
309 vec->compress(VectorOperation::add);
310 vec->scale(valence);
311 }
312 }
313 else
314 {
315 for (auto *const vec : all_out)
316 vec->compress(VectorOperation::insert);
317 }
318
319 input_vectors.clear();
320 handle = numbers::invalid_unsigned_int;
321 }
322
323
324 template <int dim, typename VectorType, int spacedim>
325 void
326 SolutionTransfer<dim, VectorType, spacedim>::interpolate(
327 std::vector<VectorType> &all_out)
328 {
329 std::vector<VectorType *> temp(all_out.size());
330
331 for (std::size_t i = 0; i < temp.size(); ++i)
332 temp[i] = &(all_out[i]);
333
334 this->interpolate(temp);
335 }
336
337
338
339 template <int dim, typename VectorType, int spacedim>
340 void
341 SolutionTransfer<dim, VectorType, spacedim>::interpolate(VectorType &out)
342 {
343 std::vector<VectorType *> all_out(1, &out);
344 interpolate(all_out);
345 }
346
347
348
349 template <int dim, typename VectorType, int spacedim>
350 std::vector<char>
351 SolutionTransfer<dim, VectorType, spacedim>::pack_callback(
352 const typename Triangulation<dim, spacedim>::cell_iterator &cell_,
353 const CellStatus status)
354 {
355 typename DoFHandler<dim, spacedim>::cell_iterator cell(*cell_, dof_handler);
356
357 // create buffer for each individual object
358 std::vector<::dealii::Vector<typename VectorType::value_type>> dof_values(
359 input_vectors.size());
360
361 unsigned int fe_index = 0;
362 if (dof_handler->has_hp_capabilities())
363 {
364 switch (status)
365 {
366 case CellStatus::cell_will_persist:
367 case CellStatus::cell_will_be_refined:
368 {
369 fe_index = cell->future_fe_index();
370 break;
371 }
372
373 case CellStatus::children_will_be_coarsened:
374 {
375 // In case of coarsening, we need to find a suitable FE index
376 // for the parent cell. We choose the 'least dominant fe'
377 // on all children from the associated FECollection.
378 if constexpr (running_in_debug_mode())
379 {
380 for (const auto &child : cell->child_iterators())
381 Assert(child->is_active() && child->coarsen_flag_set(),
382 typename dealii::Triangulation<
383 dim>::ExcInconsistentCoarseningFlags());
384 }
385
386 fe_index = dealii::internal::hp::DoFHandlerImplementation::
387 dominated_future_fe_on_children<dim, spacedim>(cell);
388 break;
389 }
390
391 default:
392 Assert(false, ExcInternalError());
393 break;
394 }
395 }
396
397 const unsigned int dofs_per_cell =
398 dof_handler->get_fe(fe_index).n_dofs_per_cell();
399
400 if (dofs_per_cell == 0)
401 return std::vector<char>(); // nothing to do for FE_Nothing
402
403 auto it_input = input_vectors.cbegin();
404 auto it_output = dof_values.begin();
405 for (; it_input != input_vectors.cend(); ++it_input, ++it_output)
406 {
407 it_output->reinit(dofs_per_cell);
408 cell->get_interpolated_dof_values(*(*it_input), *it_output, fe_index);
409 }
410
411 return internal::SolutionTransferImplementation::pack_dof_values<
412 typename VectorType::value_type>(dof_values, dofs_per_cell);
413 }
414
415
416
417 template <int dim, typename VectorType, int spacedim>
418 void
419 SolutionTransfer<dim, VectorType, spacedim>::unpack_callback(
420 const typename Triangulation<dim, spacedim>::cell_iterator &cell_,
421 const CellStatus status,
422 const boost::iterator_range<std::vector<char>::const_iterator> &data_range,
423 std::vector<VectorType *> &all_out,
424 VectorType &valence)
425 {
426 typename DoFHandler<dim, spacedim>::cell_iterator cell(*cell_, dof_handler);
427
428 unsigned int fe_index = 0;
429 if (dof_handler->has_hp_capabilities())
430 {
431 switch (status)
432 {
433 case CellStatus::cell_will_persist:
434 case CellStatus::children_will_be_coarsened:
435 {
436 fe_index = cell->active_fe_index();
437 break;
438 }
439
440 case CellStatus::cell_will_be_refined:
441 {
442 // After refinement, this particular cell is no longer active,
443 // and its children have inherited its FE index. However, to
444 // unpack the data on the old cell, we need to recover its FE
445 // index from one of the children. Just to be sure, we also
446 // check if all children have the same FE index.
447 fe_index = cell->child(0)->active_fe_index();
448 for (unsigned int child_index = 1;
449 child_index < cell->n_children();
450 ++child_index)
451 Assert(cell->child(child_index)->active_fe_index() == fe_index,
452 ExcInternalError());
453 break;
454 }
455
456 default:
457 Assert(false, ExcInternalError());
458 break;
459 }
460 }
461
462 const unsigned int dofs_per_cell =
463 dof_handler->get_fe(fe_index).n_dofs_per_cell();
464
465 if (dofs_per_cell == 0)
466 return; // nothing to do for FE_Nothing
467
468 const std::vector<::dealii::Vector<typename VectorType::value_type>>
469 dof_values = internal::SolutionTransferImplementation::unpack_dof_values<
470 typename VectorType::value_type>(data_range, dofs_per_cell);
471
472 // check if sizes match
473 AssertDimension(dof_values.size(), all_out.size());
474
475 // check if we have enough dofs provided by the FE object
476 // to interpolate the transferred data correctly
477 for (auto it_dof_values = dof_values.begin();
478 it_dof_values != dof_values.end();
479 ++it_dof_values)
480 Assert(
481 dofs_per_cell == it_dof_values->size(),
482 ExcMessage(
483 "The transferred data was packed with a different number of dofs than the "
484 "currently registered FE object assigned to the DoFHandler has."));
485
486 // distribute data for each registered vector on mesh
487 auto it_input = dof_values.cbegin();
488 auto it_output = all_out.begin();
489 for (; it_input != dof_values.cend(); ++it_input, ++it_output)
490 if (average_values)
491 cell->distribute_local_to_global_by_interpolation(*it_input,
492 *(*it_output),
493 fe_index);
494 else
495 cell->set_dof_values_by_interpolation(*it_input,
496 *(*it_output),
497 fe_index,
498 true);
499
500 if (average_values)
501 {
502 // compute valence vector if averaging should be performed
503 Vector<typename VectorType::value_type> ones(dofs_per_cell);
504 ones = 1.0;
505 cell->distribute_local_to_global_by_interpolation(ones,
506 valence,
507 fe_index);
508 }
509 }
510
511
512
513 template <int dim, typename VectorType, int spacedim>
514 void
515 SolutionTransfer<dim, VectorType, spacedim>::clear()
516 {
517 // nothing to do
518 }
519
520
521
522 namespace Legacy
523 {
524
525 template <int dim, typename VectorType, int spacedim>
526 SolutionTransfer<dim, VectorType, spacedim>::SolutionTransfer(
527 const DoFHandler<dim, spacedim> &dof)
528 : dof_handler(&dof, typeid(*this).name())
529 , n_dofs_old(0)
530 , prepared_for(none)
531 {
532 Assert((dynamic_cast<
533 const parallel::distributed::Triangulation<dim, spacedim> *>(
534 &dof_handler->get_triangulation()) == nullptr),
535 ExcMessage(
536 "You are calling the dealii::SolutionTransfer class "
537 "with a DoFHandler that is built on a "
538 "parallel::distributed::Triangulation. This will not "
539 "work for parallel computations. You probably want to "
540 "use the parallel::distributed::SolutionTransfer class."));
541 }
542
543
544
545 template <int dim, typename VectorType, int spacedim>
546 SolutionTransfer<dim, VectorType, spacedim>::~SolutionTransfer()
547 {
548 clear();
549 }
550
551
552
553 template <int dim, typename VectorType, int spacedim>
554 void
555 SolutionTransfer<dim, VectorType, spacedim>::clear()
556 {
557 indices_on_cell.clear();
558 dof_values_on_cell.clear();
559 cell_map.clear();
560
561 prepared_for = none;
562 }
563
564
565
566 template <int dim, typename VectorType, int spacedim>
567 void
568 SolutionTransfer<dim, VectorType, spacedim>::prepare_for_pure_refinement()
569 {
570 Assert(prepared_for != pure_refinement, ExcAlreadyPrepForRef());
571 Assert(prepared_for != coarsening_and_refinement,
572 ExcAlreadyPrepForCoarseAndRef());
573
574 clear();
575
576 // We need to access dof indices on the entire domain. For
577 // parallel::shared::Triangulations, ownership of cells might change. If
578 // they allow artificial cells, we need to restore the "true" cell owners
579 // temporarily.
580 // We use the TemporarilyRestoreSubdomainIds class for this purpose: we save
581 // the current set of subdomain ids, set subdomain ids to the "true" owner
582 // of each cell upon construction of the TemporarilyRestoreSubdomainIds
583 // object, and later restore these flags when it is destroyed.
584 const dealii::internal::parallel::shared::
585 TemporarilyRestoreSubdomainIds<dim, spacedim>
586 subdomain_modifier(dof_handler->get_triangulation());
587
588 const unsigned int n_active_cells =
589 dof_handler->get_triangulation().n_active_cells();
590 n_dofs_old = dof_handler->n_dofs();
591
592 // efficient reallocation of indices_on_cell
593 std::vector<std::vector<types::global_dof_index>>(n_active_cells)
594 .swap(indices_on_cell);
595
596 for (const auto &cell : dof_handler->active_cell_iterators())
597 {
598 const unsigned int i = cell->active_cell_index();
599 indices_on_cell[i].resize(cell->get_fe().n_dofs_per_cell());
600 // on each cell store the indices of the
601 // dofs. after refining we get the values
602 // on the children by taking these
603 // indices, getting the respective values
604 // out of the data vectors and prolonging
605 // them to the children
606 cell->get_dof_indices(indices_on_cell[i]);
607 cell_map[std::make_pair(cell->level(), cell->index())] =
608 Pointerstruct(&indices_on_cell[i], cell->active_fe_index());
609 }
610 prepared_for = pure_refinement;
611 }
612
613
614
615 template <int dim, typename VectorType, int spacedim>
616 void
617 SolutionTransfer<dim, VectorType, spacedim>::refine_interpolate(
618 const VectorType &in,
619 VectorType &out) const
620 {
621 Assert(prepared_for == pure_refinement, ExcNotPrepared());
622 Assert(in.size() == n_dofs_old,
623 ExcDimensionMismatch(in.size(), n_dofs_old));
624 Assert(out.size() == dof_handler->n_dofs(),
625 ExcDimensionMismatch(out.size(), dof_handler->n_dofs()));
626 Assert(&in != &out,
627 ExcMessage("Vectors cannot be used as input and output"
628 " at the same time!"));
629
630 // We need to access dof indices on the entire domain. For
631 // parallel::shared::Triangulations, ownership of cells might change. If
632 // they allow artificial cells, we need to restore the "true" cell owners
633 // temporarily.
634 // We use the TemporarilyRestoreSubdomainIds class for this purpose: we save
635 // the current set of subdomain ids, set subdomain ids to the "true" owner
636 // of each cell upon construction of the TemporarilyRestoreSubdomainIds
637 // object, and later restore these flags when it is destroyed.
638 const dealii::internal::parallel::shared::
639 TemporarilyRestoreSubdomainIds<dim, spacedim>
640 subdomain_modifier(dof_handler->get_triangulation());
641
642 Vector<typename VectorType::value_type> local_values(0);
643
644 typename std::map<std::pair<unsigned int, unsigned int>,
645 Pointerstruct>::const_iterator pointerstruct,
646 cell_map_end = cell_map.end();
647
648 for (const auto &cell : dof_handler->cell_iterators())
649 {
650 pointerstruct =
651 cell_map.find(std::make_pair(cell->level(), cell->index()));
652
653 if (pointerstruct != cell_map_end)
654 // this cell was refined or not
655 // touched at all, so we can get
656 // the new values by just setting
657 // or interpolating to the children,
658 // which is both done by one
659 // function
660 {
661 const unsigned int this_fe_index =
662 pointerstruct->second.active_fe_index;
663 const unsigned int dofs_per_cell =
664 cell->get_dof_handler().get_fe(this_fe_index).n_dofs_per_cell();
665 local_values.reinit(dofs_per_cell, true);
666
667 // make sure that the size of the stored indices is the same as
668 // dofs_per_cell. since we store the desired fe_index, we know
669 // what this size should be
670 Assert(dofs_per_cell == (*pointerstruct->second.indices_ptr).size(),
671 ExcInternalError());
672 for (unsigned int i = 0; i < dofs_per_cell; ++i)
673 local_values(i) =
674 dealii::internal::ElementAccess<VectorType>::get(
675 in, (*pointerstruct->second.indices_ptr)[i]);
676 cell->set_dof_values_by_interpolation(local_values,
677 out,
678 this_fe_index,
679 true);
680 }
681 }
682 }
683
684
685
686 namespace internal
687 {
688 /**
689 * Generate a table that contains
690 * interpolation matrices between
691 * each combination of finite
692 * elements used in a DoFHandler of
693 * some kind. Since not all
694 * elements can be interpolated
695 * onto each other, the table may
696 * contain empty matrices for those
697 * combinations of elements for
698 * which no such interpolation is
699 * implemented.
700 */
701 template <int dim, int spacedim>
702 void
703 extract_interpolation_matrices(
704 const DoFHandler<dim, spacedim> &dof,
705 dealii::Table<2, FullMatrix<double>> &matrices)
706 {
707 if (dof.has_hp_capabilities() == false)
708 return;
709
710 const dealii::hp::FECollection<dim, spacedim> &fe =
711 dof.get_fe_collection();
712 matrices.reinit(fe.size(), fe.size());
713 for (unsigned int i = 0; i < fe.size(); ++i)
714 for (unsigned int j = 0; j < fe.size(); ++j)
715 if (i != j)
716 {
717 matrices(i, j).reinit(fe[i].n_dofs_per_cell(),
718 fe[j].n_dofs_per_cell());
719
720 // see if we can get the interpolation matrices for this
721 // combination of elements. if not, reset the matrix sizes to zero
722 // to indicate that this particular combination isn't
723 // supported. this isn't an outright error right away since we may
724 // never need to actually interpolate between these two elements
725 // on actual cells; we simply have to trigger an error if someone
726 // actually tries
727 try
728 {
729 fe[i].get_interpolation_matrix(fe[j], matrices(i, j));
730 }
731 catch (const typename FiniteElement<dim, spacedim>::
732 ExcInterpolationNotImplemented &)
733 {
734 matrices(i, j).reinit(0, 0);
735 }
736 }
737 }
738
739
740 template <int dim, int spacedim>
741 void
742 restriction_additive(const FiniteElement<dim, spacedim> &,
743 std::vector<std::vector<bool>> &)
744 {}
745
746 template <int dim, int spacedim>
747 void
748 restriction_additive(
749 const dealii::hp::FECollection<dim, spacedim> &fe,
750 std::vector<std::vector<bool>> &restriction_is_additive)
751 {
752 restriction_is_additive.resize(fe.size());
753 for (unsigned int f = 0; f < fe.size(); ++f)
754 {
755 restriction_is_additive[f].resize(fe[f].n_dofs_per_cell());
756 for (unsigned int i = 0; i < fe[f].n_dofs_per_cell(); ++i)
757 restriction_is_additive[f][i] = fe[f].restriction_is_additive(i);
758 }
759 }
760 } // namespace internal
761
762
763
764 template <int dim, typename VectorType, int spacedim>
765 void
766 SolutionTransfer<dim, VectorType, spacedim>::
767 prepare_for_coarsening_and_refinement(const std::vector<VectorType> &all_in)
768 {
769 Assert(prepared_for != pure_refinement, ExcAlreadyPrepForRef());
770 Assert(prepared_for != coarsening_and_refinement,
771 ExcAlreadyPrepForCoarseAndRef());
772
773 clear();
774 n_dofs_old = dof_handler->n_dofs();
775 const unsigned int in_size = all_in.size();
776
777 if constexpr (running_in_debug_mode())
778 {
779 Assert(in_size != 0,
780 ExcMessage("The array of input vectors you pass to this "
781 "function has no elements. This is not useful."));
782 for (unsigned int i = 0; i < in_size; ++i)
783 {
784 Assert(all_in[i].size() == n_dofs_old,
785 ExcDimensionMismatch(all_in[i].size(), n_dofs_old));
786 }
787 }
788
789 // We need to access dof indices on the entire domain. For
790 // parallel::shared::Triangulations, ownership of cells might change. If
791 // they allow artificial cells, we need to restore the "true" cell owners
792 // temporarily.
793 // We use the TemporarilyRestoreSubdomainIds class for this purpose: we save
794 // the current set of subdomain ids, set subdomain ids to the "true" owner
795 // of each cell upon construction of the TemporarilyRestoreSubdomainIds
796 // object, and later restore these flags when it is destroyed.
797 const dealii::internal::parallel::shared::
798 TemporarilyRestoreSubdomainIds<dim, spacedim>
799 subdomain_modifier(dof_handler->get_triangulation());
800
801 // first count the number
802 // of cells that will be coarsened
803 // and that'll stay or be refined
804 unsigned int n_cells_to_coarsen = 0;
805 unsigned int n_cells_to_stay_or_refine = 0;
806 for (const auto &act_cell : dof_handler->active_cell_iterators())
807 {
808 if (act_cell->coarsen_flag_set())
809 ++n_cells_to_coarsen;
810 else
811 ++n_cells_to_stay_or_refine;
812 }
813 Assert((n_cells_to_coarsen + n_cells_to_stay_or_refine) ==
814 dof_handler->get_triangulation().n_active_cells(),
815 ExcInternalError());
816
817 unsigned int n_coarsen_fathers = 0;
818 for (const auto &cell : dof_handler->cell_iterators())
819 if (!cell->is_active() && cell->child(0)->coarsen_flag_set())
820 ++n_coarsen_fathers;
821 Assert(n_cells_to_coarsen >= 2 * n_coarsen_fathers, ExcInternalError());
822 (void)n_cells_to_coarsen;
823
824 // allocate the needed memory. initialize
825 // the following arrays in an efficient
826 // way, without copying much
827 std::vector<std::vector<types::global_dof_index>>(n_cells_to_stay_or_refine)
828 .swap(indices_on_cell);
829
830 std::vector<std::vector<Vector<typename VectorType::value_type>>>(
831 n_coarsen_fathers,
832 std::vector<Vector<typename VectorType::value_type>>(in_size))
833 .swap(dof_values_on_cell);
834
835 Table<2, FullMatrix<double>> interpolation_hp;
836 std::vector<std::vector<bool>> restriction_is_additive;
837
838 internal::extract_interpolation_matrices(*dof_handler, interpolation_hp);
839 internal::restriction_additive(dof_handler->get_fe_collection(),
840 restriction_is_additive);
841
842 // we need counters for
843 // the 'to_stay_or_refine' cells 'n_sr' and
844 // the 'coarsen_fathers' cells 'n_cf',
845 unsigned int n_sr = 0, n_cf = 0;
846 for (const auto &cell : dof_handler->cell_iterators())
847 {
848 // CASE 1: active cell that remains as it is
849 if (cell->is_active() && !cell->coarsen_flag_set())
850 {
851 const unsigned int dofs_per_cell = cell->get_fe().n_dofs_per_cell();
852 indices_on_cell[n_sr].resize(dofs_per_cell);
853 // cell will not be coarsened,
854 // so we get away by storing the
855 // dof indices and later
856 // interpolating to the children
857 cell->get_dof_indices(indices_on_cell[n_sr]);
858 cell_map[std::make_pair(cell->level(), cell->index())] =
859 Pointerstruct(&indices_on_cell[n_sr], cell->active_fe_index());
860 ++n_sr;
861 }
862
863 // CASE 2: cell is inactive but will become active
864 else if (cell->has_children() && cell->child(0)->coarsen_flag_set())
865 {
866 // we will need to interpolate from the children of this cell
867 // to the current one. in the hp-context, this also means
868 // we need to figure out which finite element space to interpolate
869 // to since that is not implied by the global FE as in the non-hp-
870 // case. we choose the 'least dominant fe' on all children from
871 // the associated FECollection.
872 std::set<unsigned int> fe_indices_children;
873 for (const auto &child : cell->child_iterators())
874 {
875 Assert(child->is_active() && child->coarsen_flag_set(),
876 typename dealii::Triangulation<
877 dim>::ExcInconsistentCoarseningFlags());
878
879 fe_indices_children.insert(child->active_fe_index());
880 }
881 Assert(!fe_indices_children.empty(), ExcInternalError());
882
883 const unsigned int target_fe_index =
884 dof_handler->get_fe_collection().find_dominated_fe_extended(
885 fe_indices_children, /*codim=*/0);
886
887 Assert(target_fe_index != numbers::invalid_unsigned_int,
888 dealii::internal::hp::DoFHandlerImplementation::
889 ExcNoDominatedFiniteElementOnChildren());
890
891 const unsigned int dofs_per_cell =
892 dof_handler->get_fe(target_fe_index).n_dofs_per_cell();
893
894 std::vector<Vector<typename VectorType::value_type>>(
895 in_size, Vector<typename VectorType::value_type>(dofs_per_cell))
896 .swap(dof_values_on_cell[n_cf]);
897
898
899 // store the data of each of the input vectors. get this data
900 // as interpolated onto a finite element space that encompasses
901 // that of all the children. note that
902 // cell->get_interpolated_dof_values already does all of the
903 // interpolations between spaces
904 for (unsigned int j = 0; j < in_size; ++j)
905 cell->get_interpolated_dof_values(all_in[j],
906 dof_values_on_cell[n_cf][j],
907 target_fe_index);
908 cell_map[std::make_pair(cell->level(), cell->index())] =
909 Pointerstruct(&dof_values_on_cell[n_cf], target_fe_index);
910 ++n_cf;
911 }
912 }
913 Assert(n_sr == n_cells_to_stay_or_refine, ExcInternalError());
914 Assert(n_cf == n_coarsen_fathers, ExcInternalError());
915
916 prepared_for = coarsening_and_refinement;
917 }
918
919
920
921 template <int dim, typename VectorType, int spacedim>
922 void
923 SolutionTransfer<dim, VectorType, spacedim>::
924 prepare_for_coarsening_and_refinement(const VectorType &in)
925 {
926 std::vector<VectorType> all_in(1, in);
927 prepare_for_coarsening_and_refinement(all_in);
928 }
929
930
931
932 template <int dim, typename VectorType, int spacedim>
933 void
934 SolutionTransfer<dim, VectorType, spacedim>::interpolate(
935 const std::vector<VectorType> &all_in,
936 std::vector<VectorType> &all_out) const
937 {
938 const unsigned int size = all_in.size();
939 if constexpr (running_in_debug_mode())
940 {
941 Assert(prepared_for == coarsening_and_refinement, ExcNotPrepared());
942 Assert(all_out.size() == size,
943 ExcDimensionMismatch(all_out.size(), size));
944 for (unsigned int i = 0; i < size; ++i)
945 Assert(all_in[i].size() == n_dofs_old,
946 ExcDimensionMismatch(all_in[i].size(), n_dofs_old));
947 for (unsigned int i = 0; i < all_out.size(); ++i)
948 Assert(all_out[i].size() == dof_handler->n_dofs(),
949 ExcDimensionMismatch(all_out[i].size(),
950 dof_handler->n_dofs()));
951 for (unsigned int i = 0; i < size; ++i)
952 for (unsigned int j = 0; j < size; ++j)
953 Assert(&all_in[i] != &all_out[j],
954 ExcMessage("Vectors cannot be used as input and output"
955 " at the same time!"));
956 }
957
958 // We need to access dof indices on the entire domain. For
959 // parallel::shared::Triangulations, ownership of cells might change. If
960 // they allow artificial cells, we need to restore the "true" cell owners
961 // temporarily.
962 // We use the TemporarilyRestoreSubdomainIds class for this purpose: we save
963 // the current set of subdomain ids, set subdomain ids to the "true" owner
964 // of each cell upon construction of the TemporarilyRestoreSubdomainIds
965 // object, and later restore these flags when it is destroyed.
966 const dealii::internal::parallel::shared::
967 TemporarilyRestoreSubdomainIds<dim, spacedim>
968 subdomain_modifier(dof_handler->get_triangulation());
969
970 Vector<typename VectorType::value_type> local_values;
971 std::vector<types::global_dof_index> dofs;
972
973 typename std::map<std::pair<unsigned int, unsigned int>,
974 Pointerstruct>::const_iterator pointerstruct,
975 cell_map_end = cell_map.end();
976
977 Table<2, FullMatrix<double>> interpolation_hp;
978 internal::extract_interpolation_matrices(*dof_handler, interpolation_hp);
979 Vector<typename VectorType::value_type> tmp, tmp2;
980
981 for (const auto &cell : dof_handler->cell_iterators())
982 {
983 pointerstruct =
984 cell_map.find(std::make_pair(cell->level(), cell->index()));
985
986 if (pointerstruct != cell_map_end)
987 {
988 const std::vector<types::global_dof_index> *const indexptr =
989 pointerstruct->second.indices_ptr;
990
991 const std::vector<Vector<typename VectorType::value_type>>
992 *const valuesptr = pointerstruct->second.dof_values_ptr;
993
994 // cell stayed as it was or was refined
995 if (indexptr != nullptr)
996 {
997 Assert(valuesptr == nullptr, ExcInternalError());
998
999 const unsigned int old_fe_index =
1000 pointerstruct->second.active_fe_index;
1001
1002 // get the values of each of the input data vectors on this cell
1003 // and prolong it to its children
1004 unsigned int in_size = indexptr->size();
1005 for (unsigned int j = 0; j < size; ++j)
1006 {
1007 tmp.reinit(in_size, true);
1008 for (unsigned int i = 0; i < in_size; ++i)
1009 tmp(i) = dealii::internal::ElementAccess<VectorType>::get(
1010 all_in[j], (*indexptr)[i]);
1011
1012 cell->set_dof_values_by_interpolation(tmp,
1013 all_out[j],
1014 old_fe_index,
1015 true);
1016 }
1017 }
1018 else if (valuesptr)
1019 // the children of this cell were deleted
1020 {
1021 Assert(!cell->has_children(), ExcInternalError());
1022 Assert(indexptr == nullptr, ExcInternalError());
1023
1024 const unsigned int dofs_per_cell =
1025 cell->get_fe().n_dofs_per_cell();
1026 dofs.resize(dofs_per_cell);
1027 // get the local
1028 // indices
1029 cell->get_dof_indices(dofs);
1030
1031 // distribute the stored data to the new vectors
1032 for (unsigned int j = 0; j < size; ++j)
1033 {
1034 // make sure that the size of the stored indices is the same
1035 // as dofs_per_cell. this is kind of a test if we use the
1036 // same FE in the hp-case. to really do that test we would
1037 // have to store the fe_index of all cells
1038 const Vector<typename VectorType::value_type> *data =
1039 nullptr;
1040 const unsigned int active_fe_index =
1041 cell->active_fe_index();
1042 if (active_fe_index !=
1043 pointerstruct->second.active_fe_index)
1044 {
1045 const unsigned int old_index =
1046 pointerstruct->second.active_fe_index;
1047 const FullMatrix<double> &interpolation_matrix =
1048 interpolation_hp(active_fe_index, old_index);
1049 // The interpolation matrix might be empty when using
1050 // FE_Nothing.
1051 if (interpolation_matrix.empty())
1052 tmp.reinit(dofs_per_cell, false);
1053 else
1054 {
1055 tmp.reinit(dofs_per_cell, true);
1056 AssertDimension((*valuesptr)[j].size(),
1057 interpolation_matrix.n());
1058 AssertDimension(tmp.size(),
1059 interpolation_matrix.m());
1060 interpolation_matrix.vmult(tmp, (*valuesptr)[j]);
1061 }
1062 data = &tmp;
1063 }
1064 else
1065 data = &(*valuesptr)[j];
1066
1067
1068 for (unsigned int i = 0; i < dofs_per_cell; ++i)
1069 dealii::internal::ElementAccess<VectorType>::set(
1070 (*data)(i), dofs[i], all_out[j]);
1071 }
1072 }
1073 // undefined status
1074 else
1075 Assert(false, ExcInternalError());
1076 }
1077 }
1078
1079 // We have written into the output vectors. If this was a PETSc vector, for
1080 // example, then we need to compress these to make future operations safe:
1081 for (auto &vec : all_out)
1082 vec.compress(VectorOperation::insert);
1083 }
1084
1085
1086
1087 template <int dim, typename VectorType, int spacedim>
1088 void
1089 SolutionTransfer<dim, VectorType, spacedim>::interpolate(
1090 const VectorType &in,
1091 VectorType &out) const
1092 {
1093 Assert(in.size() == n_dofs_old,
1094 ExcDimensionMismatch(in.size(), n_dofs_old));
1095 Assert(out.size() == dof_handler->n_dofs(),
1096 ExcDimensionMismatch(out.size(), dof_handler->n_dofs()));
1097
1098 std::vector<VectorType> all_in = {in};
1099 std::vector<VectorType> all_out = {out};
1100
1101 interpolate(all_in, all_out);
1102
1103 out = all_out[0];
1104 }
1105
1106
1107
1108 template <int dim, typename VectorType, int spacedim>
1109 std::size_t
1110 SolutionTransfer<dim, VectorType, spacedim>::memory_consumption() const
1111 {
1112 // at the moment we do not include the memory
1113 // consumption of the cell_map as we have no
1114 // real idea about memory consumption of a
1115 // std::map
1116 return (MemoryConsumption::memory_consumption(dof_handler) +
1117 MemoryConsumption::memory_consumption(n_dofs_old) +
1118 sizeof(prepared_for) +
1119 MemoryConsumption::memory_consumption(indices_on_cell) +
1120 MemoryConsumption::memory_consumption(dof_values_on_cell));
1121 }
1122
1123
1124
1125 template <int dim, typename VectorType, int spacedim>
1126 std::size_t
1127 SolutionTransfer<dim, VectorType, spacedim>::Pointerstruct::
1128 memory_consumption() const
1129 {
1130 return sizeof(*this);
1131 }
1132
1133 } // namespace Legacy
1134
1135 DEAL_II_NAMESPACE_CLOSE
1136
1137
1138 #endif