Debian Codegraph @

include/deal.II/fe/fe_tools_interpolate.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2013 - 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_fe_tools_interpolate_templates_H
16 #define dealii_fe_tools_interpolate_templates_H
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/index_set.h>
22 #include <deal.II/base/qprojector.h>
23 #include <deal.II/base/quadrature_lib.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 #include <deal.II/fe/fe_tools.h>
31 #include <deal.II/fe/fe_values.h>
32
33 #include <deal.II/grid/grid_generator.h>
34 #include <deal.II/grid/tria.h>
35 #include <deal.II/grid/tria_iterator.h>
36
37 #include <deal.II/lac/affine_constraints.h>
38 #include <deal.II/lac/block_vector.h>
39 #include <deal.II/lac/la_parallel_block_vector.h>
40 #include <deal.II/lac/la_parallel_vector.h>
41 #include <deal.II/lac/petsc_block_vector.h>
42 #include <deal.II/lac/petsc_vector.h>
43 #include <deal.II/lac/trilinos_epetra_vector.h>
44 #include <deal.II/lac/trilinos_parallel_block_vector.h>
45 #include <deal.II/lac/trilinos_tpetra_block_vector.h>
46 #include <deal.II/lac/trilinos_tpetra_vector.h>
47 #include <deal.II/lac/trilinos_vector.h>
48 #include <deal.II/lac/vector.h>
49
50 #include <iostream>
51 #include <memory>
52
53
54 DEAL_II_NAMESPACE_OPEN
55
56 namespace FETools
57 {
58 template <int dim, int spacedim, class InVector, class OutVector>
59 void
60 interpolate(const DoFHandler<dim, spacedim> &dof1,
61 const InVector &u1,
62 const DoFHandler<dim, spacedim> &dof2,
63 OutVector &u2)
64 {
65 AffineConstraints<typename OutVector::value_type> dummy;
66 dummy.close();
67 interpolate(dof1, u1, dof2, dummy, u2);
68 }
69
70
71
72 template <int dim, int spacedim, class InVector, class OutVector>
73 void
74 interpolate(
75 const DoFHandler<dim, spacedim> &dof1,
76 const InVector &u1,
77 const DoFHandler<dim, spacedim> &dof2,
78 const AffineConstraints<typename OutVector::value_type> &constraints,
79 OutVector &u2)
80 {
81 Assert(&dof1.get_triangulation() == &dof2.get_triangulation(),
82 ExcTriangulationMismatch());
83
84 Assert(u1.size() == dof1.n_dofs(),
85 ExcDimensionMismatch(u1.size(), dof1.n_dofs()));
86 Assert(u2.size() == dof2.n_dofs(),
87 ExcDimensionMismatch(u2.size(), dof2.n_dofs()));
88
89
90 const IndexSet u2_elements = u2.locally_owned_elements();
91 if constexpr (running_in_debug_mode())
92 {
93 const IndexSet &dof1_local_dofs = dof1.locally_owned_dofs();
94 const IndexSet &dof2_local_dofs = dof2.locally_owned_dofs();
95 const IndexSet u1_elements = u1.locally_owned_elements();
96 Assert(u1_elements == dof1_local_dofs,
97 ExcMessage(
98 "The provided vector and DoF handler should have the same"
99 " index sets."));
100 Assert(u2_elements == dof2_local_dofs,
101 ExcMessage(
102 "The provided vector and DoF handler should have the same"
103 " index sets."));
104 }
105
106 // allocate vectors at maximal
107 // size. will be reinited in inner
108 // cell, but Vector makes sure that
109 // this does not lead to
110 // reallocation of memory
111 Vector<typename OutVector::value_type> u1_local(
112 dof1.get_fe_collection().max_dofs_per_cell());
113 Vector<typename OutVector::value_type> u2_local(
114 dof2.get_fe_collection().max_dofs_per_cell());
115
116 // have a map for interpolation matrices.
117 // Using a unique_ptr makes sure that the
118 // memory is released again automatically.
119 std::map<const FiniteElement<dim, spacedim> *,
120 std::map<const FiniteElement<dim, spacedim> *,
121 std::unique_ptr<FullMatrix<double>>>>
122 interpolation_matrices;
123
124 typename DoFHandler<dim, spacedim>::active_cell_iterator
125 cell1 = dof1.begin_active(),
126 endc1 = dof1.end();
127 typename DoFHandler<dim, spacedim>::active_cell_iterator
128 cell2 = dof2.begin_active(),
129 endc2 = dof2.end();
130 (void)endc2;
131
132 std::vector<types::global_dof_index> dofs;
133 dofs.reserve(dof2.get_fe_collection().max_dofs_per_cell());
134
135 u2 = typename OutVector::value_type(0.);
136 OutVector touch_count(u2);
137 touch_count = typename OutVector::value_type(0.);
138
139 // for distributed triangulations,
140 // we can only interpolate u1 on
141 // a cell, which this processor owns,
142 // so we have to know the subdomain_id
143 const types::subdomain_id subdomain_id =
144 dof1.get_triangulation().locally_owned_subdomain();
145
146 for (; cell1 != endc1; ++cell1, ++cell2)
147 if ((cell1->subdomain_id() == subdomain_id) ||
148 (subdomain_id == numbers::invalid_subdomain_id))
149 {
150 Assert(cell1->get_fe().n_components() ==
151 cell2->get_fe().n_components(),
152 ExcDimensionMismatch(cell1->get_fe().n_components(),
153 cell2->get_fe().n_components()));
154
155 if constexpr (running_in_debug_mode())
156 {
157 // For continuous elements on grids with hanging nodes we need
158 // hanging node constraints. Consequently, when the elements are
159 // continuous no hanging node constraints are allowed.
160 const bool hanging_nodes_not_allowed =
161 ((cell2->get_fe().n_dofs_per_vertex() != 0) &&
162 (constraints.n_constraints() == 0));
163
164 if (hanging_nodes_not_allowed)
165 for (const unsigned int face : cell1->face_indices())
166 Assert(cell1->at_boundary(face) ||
167 cell1->neighbor(face)->level() == cell1->level(),
168 ExcHangingNodesNotAllowed());
169 }
170
171 const unsigned int dofs_per_cell1 = cell1->get_fe().n_dofs_per_cell();
172 const unsigned int dofs_per_cell2 = cell2->get_fe().n_dofs_per_cell();
173 u1_local.reinit(dofs_per_cell1);
174 u2_local.reinit(dofs_per_cell2);
175
176 // check if interpolation
177 // matrix for this particular
178 // pair of elements is already
179 // there
180 if (interpolation_matrices[&cell1->get_fe()][&cell2->get_fe()]
181 .get() == nullptr)
182 {
183 auto interpolation_matrix =
184 std::make_unique<FullMatrix<double>>(dofs_per_cell2,
185 dofs_per_cell1);
186
187 get_interpolation_matrix(cell1->get_fe(),
188 cell2->get_fe(),
189 *interpolation_matrix);
190
191 interpolation_matrices[&cell1->get_fe()][&cell2->get_fe()] =
192 std::move(interpolation_matrix);
193 }
194
195 cell1->get_dof_values(u1, u1_local);
196 interpolation_matrices[&cell1->get_fe()][&cell2->get_fe()]->vmult(
197 u2_local, u1_local);
198
199 dofs.resize(dofs_per_cell2);
200 cell2->get_dof_indices(dofs);
201
202 for (unsigned int i = 0; i < dofs_per_cell2; ++i)
203 {
204 // if dof is locally_owned
205 const types::global_dof_index gdi = dofs[i];
206 if (u2_elements.is_element(gdi))
207 {
208 ::dealii::internal::ElementAccess<OutVector>::add(u2_local(i),
209 dofs[i],
210 u2);
211 ::dealii::internal::ElementAccess<OutVector>::add(
212 1, dofs[i], touch_count);
213 }
214 }
215 }
216 // cell1 is at the end, so should
217 // be cell2
218 Assert(cell2 == endc2, ExcInternalError());
219
220 u2.compress(VectorOperation::add);
221 touch_count.compress(VectorOperation::add);
222
223 // if we work on parallel distributed
224 // vectors, we have to ensure, that we only
225 // work on dofs this processor owns.
226 const IndexSet &locally_owned_dofs = dof2.locally_owned_dofs();
227
228 // when a discontinuous element is
229 // interpolated to a continuous
230 // one, we take the mean values.
231 // for parallel vectors check,
232 // if this component is owned by
233 // this processor.
234 for (types::global_dof_index i = 0; i < dof2.n_dofs(); ++i)
235 if (locally_owned_dofs.is_element(i))
236 {
237 Assert(static_cast<typename OutVector::value_type>(
238 ::dealii::internal::ElementAccess<OutVector>::get(
239 touch_count, i)) != typename OutVector::value_type(0),
240 ExcInternalError());
241
242
243 const typename OutVector::value_type val =
244 ::dealii::internal::ElementAccess<OutVector>::get(u2, i);
245 ::dealii::internal::ElementAccess<OutVector>::set(
246 val /
247 ::dealii::internal::ElementAccess<OutVector>::get(touch_count, i),
248 i,
249 u2);
250 }
251
252 // finish the work on parallel vectors
253 u2.compress(VectorOperation::insert);
254 // Apply hanging node constraints.
255 constraints.distribute(u2);
256 }
257
258
259
260 template <int dim, class InVector, class OutVector, int spacedim>
261 void
262 back_interpolate(const DoFHandler<dim, spacedim> &dof1,
263 const InVector &u1,
264 const FiniteElement<dim, spacedim> &fe2,
265 OutVector &u1_interpolated)
266 {
267 Assert(dof1.get_fe(0).n_components() == fe2.n_components(),
268 ExcDimensionMismatch(dof1.get_fe(0).n_components(),
269 fe2.n_components()));
270 Assert(u1.size() == dof1.n_dofs(),
271 ExcDimensionMismatch(u1.size(), dof1.n_dofs()));
272 Assert(u1_interpolated.size() == dof1.n_dofs(),
273 ExcDimensionMismatch(u1_interpolated.size(), dof1.n_dofs()));
274
275 if constexpr (running_in_debug_mode())
276 {
277 const IndexSet &dof1_local_dofs = dof1.locally_owned_dofs();
278 const IndexSet u1_elements = u1.locally_owned_elements();
279 const IndexSet u1_interpolated_elements =
280 u1_interpolated.locally_owned_elements();
281 Assert(u1_elements == dof1_local_dofs,
282 ExcMessage(
283 "The provided vector and DoF handler should have the same"
284 " index sets."));
285 Assert(u1_interpolated_elements == dof1_local_dofs,
286 ExcMessage(
287 "The provided vector and DoF handler should have the same"
288 " index sets."));
289 }
290
291 Vector<typename OutVector::value_type> u1_local(
292 dof1.get_fe_collection().max_dofs_per_cell());
293 Vector<typename OutVector::value_type> u1_int_local(
294 dof1.get_fe_collection().max_dofs_per_cell());
295
296 const types::subdomain_id subdomain_id =
297 dof1.get_triangulation().locally_owned_subdomain();
298
299 typename DoFHandler<dim, spacedim>::active_cell_iterator
300 cell = dof1.begin_active(),
301 endc = dof1.end();
302
303 // map from possible FE objects in
304 // dof1 to the back_interpolation
305 // matrices
306 std::map<const FiniteElement<dim> *, std::unique_ptr<FullMatrix<double>>>
307 interpolation_matrices;
308
309 for (; cell != endc; ++cell)
310 if ((cell->subdomain_id() == subdomain_id) ||
311 (subdomain_id == numbers::invalid_subdomain_id))
312 {
313 if constexpr (running_in_debug_mode())
314 {
315 // For continuous elements on grids with hanging nodes we need
316 // hanging node constraints. Consequently, when the elements are
317 // continuous no hanging node constraints are allowed.
318 const bool hanging_nodes_not_allowed =
319 (cell->get_fe().n_dofs_per_vertex() != 0) ||
320 (fe2.n_dofs_per_vertex() != 0);
321
322 if (hanging_nodes_not_allowed)
323 for (const unsigned int face : cell->face_indices())
324 Assert(cell->at_boundary(face) ||
325 cell->neighbor(face)->level() == cell->level(),
326 ExcHangingNodesNotAllowed());
327 }
328
329 const unsigned int dofs_per_cell1 = cell->get_fe().n_dofs_per_cell();
330
331 // make sure back_interpolation matrix is available
332 if (interpolation_matrices[&cell->get_fe()] == nullptr)
333 {
334 interpolation_matrices[&cell->get_fe()] =
335 std::make_unique<FullMatrix<double>>(dofs_per_cell1,
336 dofs_per_cell1);
337 get_back_interpolation_matrix(
338 cell->get_fe(), fe2, *interpolation_matrices[&cell->get_fe()]);
339 }
340
341 u1_local.reinit(dofs_per_cell1);
342 u1_int_local.reinit(dofs_per_cell1);
343
344 cell->get_dof_values(u1, u1_local);
345 interpolation_matrices[&cell->get_fe()]->vmult(u1_int_local,
346 u1_local);
347 cell->set_dof_values(u1_int_local, u1_interpolated);
348 };
349
350 // if we work on a parallel vector, we have to finish the work
351 u1_interpolated.compress(VectorOperation::insert);
352 }
353
354
355
356 namespace internal
357 {
358 template <int dim, int spacedim, class InVector>
359 std::enable_if_t<is_serial_vector<InVector>::value>
360 back_interpolate(
361 const DoFHandler<dim, spacedim> &dof1,
362 const AffineConstraints<typename InVector::value_type> &constraints1,
363 const InVector &u1,
364 const DoFHandler<dim, spacedim> &dof2,
365 const AffineConstraints<typename InVector::value_type> &constraints2,
366 InVector &u1_interpolated)
367 {
368 Vector<typename InVector::value_type> u2(dof2.n_dofs());
369 interpolate(dof1, u1, dof2, constraints2, u2);
370 interpolate(dof2, u2, dof1, constraints1, u1_interpolated);
371 }
372
373
374
375 // special version for PETSc
376 #ifdef DEAL_II_WITH_PETSC
377 template <int dim, int spacedim>
378 void
379 back_interpolate(
380 const DoFHandler<dim, spacedim> &dof1,
381 const AffineConstraints<PETScWrappers::MPI::Vector::value_type>
382 &constraints1,
383 const PETScWrappers::MPI::Vector &u1,
384 const DoFHandler<dim, spacedim> &dof2,
385 const AffineConstraints<PETScWrappers::MPI::Vector::value_type>
386 &constraints2,
387 PETScWrappers::MPI::Vector &u1_interpolated)
388 {
389 // if u1 is a parallel distributed PETSc vector, we create a
390 // vector u2 with based on the sets of locally owned and relevant
391 // dofs of dof2
392 const IndexSet &dof2_locally_owned_dofs = dof2.locally_owned_dofs();
393 const IndexSet dof2_locally_relevant_dofs =
394 DoFTools::extract_locally_relevant_dofs(dof2);
395
396 PETScWrappers::MPI::Vector u2_out(dof2_locally_owned_dofs,
397 u1.get_mpi_communicator());
398 interpolate(dof1, u1, dof2, constraints2, u2_out);
399 PETScWrappers::MPI::Vector u2(dof2_locally_owned_dofs,
400 dof2_locally_relevant_dofs,
401 u1.get_mpi_communicator());
402 u2 = u2_out;
403 interpolate(dof2, u2, dof1, constraints1, u1_interpolated);
404 }
405
406
407
408 template <int dim, int spacedim>
409 void
410 back_interpolate(
411 const DoFHandler<dim, spacedim> &,
412 const AffineConstraints<PETScWrappers::MPI::BlockVector::value_type> &,
413 const PETScWrappers::MPI::BlockVector &,
414 const DoFHandler<dim, spacedim> &,
415 const AffineConstraints<PETScWrappers::MPI::BlockVector::value_type> &,
416 PETScWrappers::MPI::BlockVector &)
417 {
418 DEAL_II_NOT_IMPLEMENTED();
419 }
420 #endif
421
422
423
424 // special version for Trilinos
425 #ifdef DEAL_II_WITH_TRILINOS
426 template <int dim, int spacedim>
427 void
428 back_interpolate(
429 const DoFHandler<dim, spacedim> &dof1,
430 const AffineConstraints<
431 typename TrilinosWrappers::MPI::Vector::value_type> &constraints1,
432 const TrilinosWrappers::MPI::Vector &u1,
433 const DoFHandler<dim, spacedim> &dof2,
434 const AffineConstraints<
435 typename TrilinosWrappers::MPI::Vector::value_type> &constraints2,
436 TrilinosWrappers::MPI::Vector &u1_interpolated)
437 {
438 // if u1 is a parallel distributed Trilinos vector, we create a
439 // vector u2 with based on the sets of locally owned and relevant
440 // dofs of dof2
441 const IndexSet &dof2_locally_owned_dofs = dof2.locally_owned_dofs();
442 const IndexSet dof2_locally_relevant_dofs =
443 DoFTools::extract_locally_relevant_dofs(dof2);
444
445 TrilinosWrappers::MPI::Vector u2_out(dof2_locally_owned_dofs,
446 u1.get_mpi_communicator());
447 interpolate(dof1, u1, dof2, constraints2, u2_out);
448 TrilinosWrappers::MPI::Vector u2(dof2_locally_owned_dofs,
449 dof2_locally_relevant_dofs,
450 u1.get_mpi_communicator());
451 u2 = u2_out;
452 interpolate(dof2, u2, dof1, constraints1, u1_interpolated);
453 }
454
455
456
457 template <int dim, int spacedim>
458 void
459 back_interpolate(
460 const DoFHandler<dim, spacedim> &dof1,
461 const AffineConstraints<
462 typename TrilinosWrappers::MPI::BlockVector::value_type> &constraints1,
463 const TrilinosWrappers::MPI::BlockVector &u1,
464 const DoFHandler<dim, spacedim> &dof2,
465 const AffineConstraints<
466 typename TrilinosWrappers::MPI::BlockVector::value_type> &constraints2,
467 TrilinosWrappers::MPI::BlockVector &u1_interpolated)
468 {
469 if (u1.n_blocks() == 0)
470 return;
471 const MPI_Comm mpi_communicator = u1.block(0).get_mpi_communicator();
472 const IndexSet &dof2_locally_owned_dofs = dof2.locally_owned_dofs();
473 const IndexSet dof2_locally_relevant_dofs =
474 DoFTools::extract_locally_relevant_dofs(dof2);
475
476 TrilinosWrappers::MPI::Vector u2_out(dof2_locally_owned_dofs,
477 mpi_communicator);
478 interpolate(dof1, u1, dof2, constraints2, u2_out);
479 TrilinosWrappers::MPI::Vector u2(dof2_locally_owned_dofs,
480 dof2_locally_relevant_dofs,
481 mpi_communicator);
482 u2 = u2_out;
483 interpolate(dof2, u2, dof1, constraints1, u1_interpolated);
484 }
485
486
487
488 template <int dim, int spacedim>
489 void
490 back_interpolate(
491 const DoFHandler<dim, spacedim> &,
492 const AffineConstraints<
493 typename LinearAlgebra::EpetraWrappers::Vector::value_type> &,
494 const LinearAlgebra::EpetraWrappers::Vector &,
495 const DoFHandler<dim, spacedim> &,
496 const AffineConstraints<
497 typename LinearAlgebra::EpetraWrappers::Vector::value_type> &,
498 LinearAlgebra::EpetraWrappers::Vector &)
499 {
500 AssertThrow(false, ExcNotImplemented());
501 }
502
503 # ifdef DEAL_II_TRILINOS_WITH_TPETRA
504 template <int dim, int spacedim, typename Number, typename MemorySpace>
505 void
506 back_interpolate(
507 const DoFHandler<dim, spacedim> &,
508 const AffineConstraints<typename LinearAlgebra::TpetraWrappers::
509 Vector<Number, MemorySpace>::value_type> &,
510 const LinearAlgebra::TpetraWrappers::Vector<Number, MemorySpace> &,
511 const DoFHandler<dim, spacedim> &,
512 const AffineConstraints<typename LinearAlgebra::TpetraWrappers::
513 Vector<Number, MemorySpace>::value_type> &,
514 LinearAlgebra::TpetraWrappers::Vector<Number, MemorySpace> &)
515 {
516 AssertThrow(false, ExcNotImplemented());
517 }
518
519 template <int dim, int spacedim, typename Number, typename MemorySpace>
520 void
521 back_interpolate(
522 const DoFHandler<dim, spacedim> &,
523 const AffineConstraints<typename LinearAlgebra::TpetraWrappers::
524 BlockVector<Number, MemorySpace>::value_type> &,
525 const LinearAlgebra::TpetraWrappers::BlockVector<Number, MemorySpace> &,
526 const DoFHandler<dim, spacedim> &,
527 const AffineConstraints<typename LinearAlgebra::TpetraWrappers::
528 BlockVector<Number, MemorySpace>::value_type> &,
529 LinearAlgebra::TpetraWrappers::BlockVector<Number, MemorySpace> &)
530 {
531 AssertThrow(false, ExcNotImplemented());
532 }
533 # endif
534 #endif
535
536
537
538 // special version for LinearAlgebra::distributed::Vector
539 template <int dim, int spacedim, typename Number>
540 void
541 back_interpolate(
542 const DoFHandler<dim, spacedim> &dof1,
543 const AffineConstraints<Number> &constraints1,
544 const LinearAlgebra::distributed::Vector<Number> &u1,
545 const DoFHandler<dim, spacedim> &dof2,
546 const AffineConstraints<Number> &constraints2,
547 LinearAlgebra::distributed::Vector<Number> &u1_interpolated)
548 {
549 const IndexSet &dof2_locally_owned_dofs = dof2.locally_owned_dofs();
550 const IndexSet dof2_locally_relevant_dofs =
551 DoFTools::extract_locally_relevant_dofs(dof2);
552
553 LinearAlgebra::distributed::Vector<Number> u2(dof2_locally_owned_dofs,
554 dof2_locally_relevant_dofs,
555 u1.get_mpi_communicator());
556
557 interpolate(dof1, u1, dof2, constraints2, u2);
558 u2.update_ghost_values();
559 interpolate(dof2, u2, dof1, constraints1, u1_interpolated);
560 }
561
562
563
564 // special version for LinearAlgebra::distributed::BlockVector
565 template <int dim, int spacedim, typename Number>
566 void
567 back_interpolate(const DoFHandler<dim, spacedim> &,
568 const AffineConstraints<Number> &,
569 const LinearAlgebra::distributed::BlockVector<Number> &,
570 const DoFHandler<dim, spacedim> &,
571 const AffineConstraints<Number> &,
572 LinearAlgebra::distributed::BlockVector<Number> &)
573 {
574 AssertThrow(false, ExcNotImplemented());
575 }
576 } // namespace internal
577
578
579
580 template <int dim, class InVector, class OutVector, int spacedim>
581 void
582 back_interpolate(
583 const DoFHandler<dim, spacedim> &dof1,
584 const AffineConstraints<typename OutVector::value_type> &constraints1,
585 const InVector &u1,
586 const DoFHandler<dim, spacedim> &dof2,
587 const AffineConstraints<typename OutVector::value_type> &constraints2,
588 OutVector &u1_interpolated)
589 {
590 // For discontinuous elements without constraints take the simpler version
591 // of the back_interpolate function.
592 if (dof1.get_fe().n_dofs_per_vertex() == 0 &&
593 dof2.get_fe().n_dofs_per_vertex() == 0 &&
594 constraints1.n_constraints() == 0 && constraints2.n_constraints() == 0)
595 back_interpolate(dof1, u1, dof2.get_fe(), u1_interpolated);
596 else
597 {
598 Assert(dof1.get_fe(0).n_components() == dof2.get_fe(0).n_components(),
599 ExcDimensionMismatch(dof1.get_fe(0).n_components(),
600 dof2.get_fe(0).n_components()));
601 Assert(u1.size() == dof1.n_dofs(),
602 ExcDimensionMismatch(u1.size(), dof1.n_dofs()));
603 Assert(u1_interpolated.size() == dof1.n_dofs(),
604 ExcDimensionMismatch(u1_interpolated.size(), dof1.n_dofs()));
605
606 // For continuous elements first interpolate to dof2, taking into
607 // account constraints2, and then interpolate back to dof1 taking into
608 // account constraints1
609 internal::back_interpolate(
610 dof1, constraints1, u1, dof2, constraints2, u1_interpolated);
611 }
612 }
613
614
615
616 template <int dim, class InVector, class OutVector, int spacedim>
617 void
618 interpolation_difference(const DoFHandler<dim, spacedim> &dof1,
619 const InVector &u1,
620 const FiniteElement<dim, spacedim> &fe2,
621 OutVector &u1_difference)
622 {
623 Assert(dof1.get_fe(0).n_components() == fe2.n_components(),
624 ExcDimensionMismatch(dof1.get_fe(0).n_components(),
625 fe2.n_components()));
626 Assert(u1.size() == dof1.n_dofs(),
627 ExcDimensionMismatch(u1.size(), dof1.n_dofs()));
628 Assert(u1_difference.size() == dof1.n_dofs(),
629 ExcDimensionMismatch(u1_difference.size(), dof1.n_dofs()));
630
631 if constexpr (running_in_debug_mode())
632 {
633 const IndexSet &dof1_local_dofs = dof1.locally_owned_dofs();
634 const IndexSet u1_elements = u1.locally_owned_elements();
635 const IndexSet u1_difference_elements =
636 u1_difference.locally_owned_elements();
637 Assert(u1_elements == dof1_local_dofs,
638 ExcMessage(
639 "The provided vector and DoF handler should have the same"
640 " index sets."));
641 Assert(u1_difference_elements == dof1_local_dofs,
642 ExcMessage(
643 "The provided vector and DoF handler should have the same"
644 " index sets."));
645 }
646
647 const unsigned int dofs_per_cell = dof1.get_fe().n_dofs_per_cell();
648
649 Vector<typename OutVector::value_type> u1_local(dofs_per_cell);
650 Vector<typename OutVector::value_type> u1_diff_local(dofs_per_cell);
651
652 const types::subdomain_id subdomain_id =
653 dof1.get_triangulation().locally_owned_subdomain();
654
655 FullMatrix<double> difference_matrix(dofs_per_cell, dofs_per_cell);
656 get_interpolation_difference_matrix(dof1.get_fe(), fe2, difference_matrix);
657
658 typename DoFHandler<dim, spacedim>::active_cell_iterator
659 cell = dof1.begin_active(),
660 endc = dof1.end();
661
662 for (; cell != endc; ++cell)
663 if ((cell->subdomain_id() == subdomain_id) ||
664 (subdomain_id == numbers::invalid_subdomain_id))
665 {
666 if constexpr (running_in_debug_mode())
667 {
668 // For continuous elements on grids with hanging nodes we need
669 // hanging node constraints. Consequently, when the elements are
670 // continuous no hanging node constraints are allowed.
671 const bool hanging_nodes_not_allowed =
672 (dof1.get_fe().n_dofs_per_vertex() != 0) ||
673 (fe2.n_dofs_per_vertex() != 0);
674
675 if (hanging_nodes_not_allowed)
676 for (const unsigned int face : cell->face_indices())
677 Assert(cell->at_boundary(face) ||
678 cell->neighbor(face)->level() == cell->level(),
679 ExcHangingNodesNotAllowed());
680 }
681
682 cell->get_dof_values(u1, u1_local);
683 difference_matrix.vmult(u1_diff_local, u1_local);
684 cell->set_dof_values(u1_diff_local, u1_difference);
685 }
686
687 // if we work on a parallel PETSc vector
688 // we have to finish the work and
689 // update ghost values
690 u1_difference.compress(VectorOperation::insert);
691 }
692
693
694
695 namespace internal
696 {
697 template <int dim, class InVector, class OutVector, int spacedim>
698 void
699 interpolation_difference(
700 const DoFHandler<dim, spacedim> &dof1,
701 const AffineConstraints<typename OutVector::value_type> &constraints1,
702 const InVector &u1,
703 const DoFHandler<dim, spacedim> &dof2,
704 const AffineConstraints<typename OutVector::value_type> &constraints2,
705 OutVector &u1_difference)
706 {
707 back_interpolate(
708 dof1, constraints1, u1, dof2, constraints2, u1_difference);
709 u1_difference.sadd(-1., 1., u1);
710 }
711
712 // special version for Trilinos
713 #ifdef DEAL_II_WITH_TRILINOS
714 template <int dim, int spacedim>
715 void
716 interpolation_difference(
717 const DoFHandler<dim, spacedim> &dof1,
718 const AffineConstraints<TrilinosWrappers::MPI::Vector::value_type>
719 &constraints1,
720 const TrilinosWrappers::MPI::Vector &u1,
721 const DoFHandler<dim, spacedim> &dof2,
722 const AffineConstraints<TrilinosWrappers::MPI::Vector::value_type>
723 &constraints2,
724 TrilinosWrappers::MPI::Vector &u1_difference)
725 {
726 back_interpolate(
727 dof1, constraints1, u1, dof2, constraints2, u1_difference);
728
729 // Trilinos vectors with and without ghost entries are very different
730 // and we cannot use the sadd function directly, so we have to create
731 // a completely distributed vector first and copy the local entries
732 // from the vector with ghost entries
733 TrilinosWrappers::MPI::Vector u1_completely_distributed;
734 u1_completely_distributed.reinit(u1_difference, true);
735 u1_completely_distributed = u1;
736
737 u1_difference.sadd(-1, u1_completely_distributed);
738 }
739 #endif
740 } // namespace internal
741
742
743
744 template <int dim, class InVector, class OutVector, int spacedim>
745 void
746 interpolation_difference(
747 const DoFHandler<dim, spacedim> &dof1,
748 const AffineConstraints<typename OutVector::value_type> &constraints1,
749 const InVector &u1,
750 const DoFHandler<dim, spacedim> &dof2,
751 const AffineConstraints<typename OutVector::value_type> &constraints2,
752 OutVector &u1_difference)
753 {
754 // For discontinuous elements
755 // without constraints take the
756 // cheaper version of the
757 // interpolation_difference function.
758 if (dof1.get_fe().n_dofs_per_vertex() == 0 &&
759 dof2.get_fe().n_dofs_per_vertex() == 0 &&
760 constraints1.n_constraints() == 0 && constraints2.n_constraints() == 0)
761 interpolation_difference(dof1, u1, dof2.get_fe(), u1_difference);
762 else
763 {
764 internal::interpolation_difference<dim, InVector, OutVector, spacedim>(
765 dof1, constraints1, u1, dof2, constraints2, u1_difference);
766 }
767 }
768
769
770
771 template <int dim, class InVector, class OutVector, int spacedim>
772 void
773 project_dg(const DoFHandler<dim, spacedim> &dof1,
774 const InVector &u1,
775 const DoFHandler<dim, spacedim> &dof2,
776 OutVector &u2)
777 {
778 Assert(&dof1.get_triangulation() == &dof2.get_triangulation(),
779 ExcTriangulationMismatch());
780 Assert(dof1.get_fe(0).n_components() == dof2.get_fe(0).n_components(),
781 ExcDimensionMismatch(dof1.get_fe(0).n_components(),
782 dof2.get_fe(0).n_components()));
783 Assert(u1.size() == dof1.n_dofs(),
784 ExcDimensionMismatch(u1.size(), dof1.n_dofs()));
785 Assert(u2.size() == dof2.n_dofs(),
786 ExcDimensionMismatch(u2.size(), dof2.n_dofs()));
787
788 typename DoFHandler<dim, spacedim>::active_cell_iterator cell1 =
789 dof1.begin_active();
790 typename DoFHandler<dim, spacedim>::active_cell_iterator cell2 =
791 dof2.begin_active();
792 typename DoFHandler<dim, spacedim>::active_cell_iterator end = dof2.end();
793
794 const unsigned int n1 = dof1.get_fe().n_dofs_per_cell();
795 const unsigned int n2 = dof2.get_fe().n_dofs_per_cell();
796
797 Vector<typename OutVector::value_type> u1_local(n1);
798 Vector<typename OutVector::value_type> u2_local(n2);
799 std::vector<types::global_dof_index> dofs(n2);
800
801 FullMatrix<double> matrix(n2, n1);
802 get_projection_matrix(dof1.get_fe(), dof2.get_fe(), matrix);
803
804 u2 = typename OutVector::value_type(0.);
805 while (cell2 != end)
806 {
807 cell1->get_dof_values(u1, u1_local);
808 matrix.vmult(u2_local, u1_local);
809 cell2->get_dof_indices(dofs);
810 for (unsigned int i = 0; i < n2; ++i)
811 {
812 ::dealii::internal::ElementAccess<OutVector>::add(u2_local(i),
813 dofs[i],
814 u2);
815 }
816
817 ++cell1;
818 ++cell2;
819 }
820 }
821 } // end of namespace FETools
822
823 DEAL_II_NAMESPACE_CLOSE
824
825 #endif