Debian Codegraph @

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

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2020 - 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_vector_tools_project_templates_h
16 #define dealii_vector_tools_project_templates_h
17
18 #include <deal.II/dofs/dof_tools.h>
19
20 #include <deal.II/fe/fe_dgq.h>
21 #include <deal.II/fe/fe_q.h>
22 #include <deal.II/fe/fe_simplex_p_bubbles.h>
23
24 #include <deal.II/lac/block_vector.h>
25 #include <deal.II/lac/dynamic_sparsity_pattern.h>
26 #include <deal.II/lac/la_parallel_block_vector.h>
27 #include <deal.II/lac/la_parallel_vector.h>
28 #include <deal.II/lac/petsc_block_vector.h>
29 #include <deal.II/lac/petsc_vector.h>
30 #include <deal.II/lac/precondition.h>
31 #include <deal.II/lac/solver_cg.h>
32 #include <deal.II/lac/solver_control.h>
33 #include <deal.II/lac/sparse_matrix.h>
34 #include <deal.II/lac/trilinos_parallel_block_vector.h>
35 #include <deal.II/lac/trilinos_vector.h>
36
37 #include <deal.II/matrix_free/operators.h>
38
39 #include <deal.II/numerics/matrix_tools.h>
40 #include <deal.II/numerics/vector_tools_boundary.h>
41 #include <deal.II/numerics/vector_tools_project.h>
42 #include <deal.II/numerics/vector_tools_rhs.h>
43
44 DEAL_II_NAMESPACE_OPEN
45
46 namespace VectorTools
47 {
48 namespace internal
49 {
50 /**
51 * Interpolate zero boundary values. We don't need to worry about a
52 * mapping here because the function we evaluate for the DoFs is zero in
53 * the mapped locations as well as in the original, unmapped locations
54 */
55 template <int dim, int spacedim, typename Number>
56 void
57 interpolate_zero_boundary_values(
58 const DoFHandler<dim, spacedim> &dof_handler,
59 std::map<types::global_dof_index, Number> &boundary_values)
60 {
61 // loop over all boundary faces
62 // to get all dof indices of
63 // dofs on the boundary. note
64 // that in 3d there are cases
65 // where a face is not at the
66 // boundary, yet one of its
67 // lines is, and we should
68 // consider the degrees of
69 // freedom on it as boundary
70 // nodes. likewise, in 2d and
71 // 3d there are cases where a
72 // cell is only at the boundary
73 // by one vertex. nevertheless,
74 // since we do not support
75 // boundaries with dimension
76 // less or equal to dim-2, each
77 // such boundary dof is also
78 // found from some other face
79 // that is actually wholly on
80 // the boundary, not only by
81 // one line or one vertex
82 std::vector<types::global_dof_index> face_dof_indices;
83 for (const auto &cell : dof_handler.active_cell_iterators())
84 for (auto f : GeometryInfo<dim>::face_indices())
85 if (cell->at_boundary(f))
86 {
87 face_dof_indices.resize(cell->get_fe().n_dofs_per_face(f));
88 cell->face(f)->get_dof_indices(face_dof_indices,
89 cell->active_fe_index());
90 for (unsigned int i = 0; i < cell->get_fe().n_dofs_per_face(f);
91 ++i)
92 // enter zero boundary values
93 // for all boundary nodes
94 //
95 // we need not care about
96 // vector valued elements here,
97 // since we set all components
98 boundary_values[face_dof_indices[i]] = 0.;
99 }
100 }
101
102
103
104 /**
105 * Compute the boundary values to be used in the project() functions.
106 */
107 template <int dim,
108 int spacedim,
109 template <int, int>
110 class M_or_MC,
111 template <int>
112 class Q_or_QC,
113 typename Number>
114 void
115 project_compute_b_v(
116 const M_or_MC<dim, spacedim> &mapping,
117 const DoFHandler<dim, spacedim> &dof,
118 const Function<spacedim, Number> &function,
119 const bool enforce_zero_boundary,
120 const Q_or_QC<dim - 1> &q_boundary,
121 const bool project_to_boundary_first,
122 std::map<types::global_dof_index, Number> &boundary_values)
123 {
124 if (enforce_zero_boundary == true)
125 // no need to project boundary
126 // values, but enforce
127 // homogeneous boundary values
128 // anyway
129 interpolate_zero_boundary_values(dof, boundary_values);
130
131 else
132 // no homogeneous boundary values
133 if (project_to_boundary_first == true)
134 // boundary projection required
135 {
136 // set up a list of boundary
137 // functions for the
138 // different boundary
139 // parts. We want the
140 // function to hold on
141 // all parts of the boundary
142 const std::vector<types::boundary_id> used_boundary_ids =
143 dof.get_triangulation().get_boundary_ids();
144
145 std::map<types::boundary_id, const Function<spacedim, Number> *>
146 boundary_functions;
147 for (const auto used_boundary_id : used_boundary_ids)
148 boundary_functions[used_boundary_id] = &function;
149 project_boundary_values(
150 mapping, dof, boundary_functions, q_boundary, boundary_values);
151 }
152 }
153
154
155
156 /*
157 * MatrixFree implementation of project() for an arbitrary number of
158 * components of the FiniteElement.
159 */
160 template <int components, int dim, typename Number, int spacedim>
161 void
162 project_matrix_free(
163 const Mapping<dim, spacedim> &mapping,
164 const DoFHandler<dim, spacedim> &dof,
165 const AffineConstraints<Number> &constraints,
166 const Quadrature<dim> &quadrature,
167 const Function<
168 spacedim,
169 typename LinearAlgebra::distributed::Vector<Number>::value_type>
170 &function,
171 LinearAlgebra::distributed::Vector<Number> &work_result,
172 const bool enforce_zero_boundary,
173 const Quadrature<dim - 1> & /*q_boundary*/,
174 const bool project_to_boundary_first)
175 {
176 Assert(project_to_boundary_first == false, ExcNotImplemented());
177 Assert(enforce_zero_boundary == false, ExcNotImplemented());
178
179 AssertDimension(dof.get_fe_collection().size(), 1);
180 AssertDimension(dof.get_fe(0).n_components(), function.n_components);
181 AssertDimension(dof.get_fe(0).n_components(), components);
182
183 Quadrature<dim> quadrature_mf;
184
185 if (dof.get_fe(0).reference_cell() ==
186 ReferenceCells::get_hypercube<dim>())
187 quadrature_mf = QGauss<dim>(dof.get_fe().degree + 2);
188 else
189 // TODO: since we have currently only implemented a handful quadrature
190 // rules for non-hypercube objects, we do not construct a new
191 // quadrature rule with degree + 2 here but use the user-provided
192 // quadrature rule (which is guaranteed to be tabulated).
193 quadrature_mf = quadrature;
194
195 // set up mass matrix and right hand side
196 typename MatrixFree<dim, Number>::AdditionalData additional_data;
197 additional_data.tasks_parallel_scheme =
198 MatrixFree<dim, Number>::AdditionalData::partition_color;
199 additional_data.mapping_update_flags =
200 (update_values | update_JxW_values);
201 std::shared_ptr<MatrixFree<dim, Number>> matrix_free(
202 new MatrixFree<dim, Number>());
203 matrix_free->reinit(
204 mapping, dof, constraints, quadrature_mf, additional_data);
205 using MatrixType = MatrixFreeOperators::MassOperator<
206 dim,
207 -1,
208 0,
209 components,
210 LinearAlgebra::distributed::Vector<Number>>;
211 MatrixType mass_matrix;
212 mass_matrix.initialize(matrix_free);
213
214 // For most elements we want to use the lumped mass matrix as the
215 // preconditioner. However, this isn't going to work with a lot of
216 // elements, like almost everything not defined on hypercubes: in that
217 // case use the diagonal.
218 bool use_lumped = false;
219 const auto &fes = dof.get_fe_collection();
220 if (dof.get_triangulation().all_reference_cells_are_hyper_cube())
221 {
222 // A few hypercube elements will always work, so avoid the more
223 // expensive check if we have them
224 bool all_support_mass_lumping = true;
225 for (unsigned int i = 0; i < fes.size(); ++i)
226 if (dynamic_cast<const FE_Q<dim, spacedim> *>(&fes[i]) == nullptr &&
227 dynamic_cast<const FE_DGQ<dim, spacedim> *>(&fes[i]) == nullptr)
228 all_support_mass_lumping = false;
229 if (all_support_mass_lumping)
230 use_lumped = true;
231 else
232 {
233 mass_matrix.compute_lumped_diagonal();
234
235 const auto &lumped_diagonal =
236 mass_matrix.get_matrix_lumped_diagonal_inverse()->get_vector();
237 bool all_entries_positive = true;
238 for (const Number &v : lumped_diagonal)
239 if (v < 0.0)
240 {
241 all_entries_positive = false;
242 break;
243 }
244 if (all_entries_positive)
245 use_lumped = true;
246 }
247 }
248 else
249 {
250 bool all_bubbles = true;
251 bool all_low_order = true;
252 for (unsigned int i = 0; i < fes.size(); ++i)
253 {
254 if (dynamic_cast<const FE_SimplexP_Bubbles<dim, spacedim> *>(
255 &fes[i]) == nullptr)
256 all_bubbles = false;
257 if (fes[i].tensor_degree() > 1)
258 all_low_order = false;
259 }
260 if (all_low_order || all_bubbles)
261 use_lumped = true;
262 }
263 use_lumped =
264 bool(Utilities::MPI::min(int(use_lumped),
265 work_result.get_mpi_communicator()));
266
267 if (use_lumped)
268 mass_matrix.compute_lumped_diagonal();
269 else
270 mass_matrix.compute_diagonal();
271
272 LinearAlgebra::distributed::Vector<Number> rhs, inhomogeneities;
273 matrix_free->initialize_dof_vector(work_result);
274 matrix_free->initialize_dof_vector(rhs);
275 matrix_free->initialize_dof_vector(inhomogeneities);
276 constraints.distribute(inhomogeneities);
277 inhomogeneities *= -1.;
278
279 {
280 create_right_hand_side(
281 mapping, dof, quadrature, function, rhs, constraints);
282
283 // account for inhomogeneous constraints
284 inhomogeneities.update_ghost_values();
285 FEEvaluation<dim, -1, 0, components, Number> phi(*matrix_free);
286 for (unsigned int cell = 0; cell < matrix_free->n_cell_batches();
287 ++cell)
288 {
289 phi.reinit(cell);
290 phi.read_dof_values_plain(inhomogeneities);
291 phi.evaluate(::dealii::EvaluationFlags::values);
292 for (unsigned int q = 0; q < phi.n_q_points; ++q)
293 phi.submit_value(phi.get_value(q), q);
294
295 phi.integrate(::dealii::EvaluationFlags::values);
296 phi.distribute_local_to_global(rhs);
297 }
298 rhs.compress(VectorOperation::add);
299 }
300
301 // now invert the matrix
302 // Allow for a maximum of 6*n steps to reduce the residual by 10^-12. n
303 // steps may not be sufficient, since roundoff errors may accumulate for
304 // badly conditioned matrices. This behavior can be observed, e.g. for
305 // FE_Q_Hierarchical for degree higher than three.
306 ReductionControl control(6 * rhs.size(), 0., 1e-12, false, false);
307 SolverCG<decltype(rhs)> cg(control);
308 const DiagonalMatrix<decltype(rhs)> &preconditioner =
309 use_lumped ? *mass_matrix.get_matrix_lumped_diagonal_inverse() :
310 *mass_matrix.get_matrix_diagonal_inverse();
311 if constexpr (running_in_debug_mode())
312 {
313 // Make sure we picked a valid preconditioner
314 const auto &diagonal = preconditioner.get_vector();
315 for (const Number &v : diagonal)
316 Assert(v > 0.0, ExcInternalError());
317 }
318 cg.solve(mass_matrix, work_result, rhs, preconditioner);
319 work_result += inhomogeneities;
320
321 constraints.distribute(work_result);
322 }
323
324
325
326 /**
327 * Helper interface for the matrix-free implementation of project(). Used
328 * to determine the number of components.
329 */
330 template <int dim, typename Number, int spacedim>
331 void
332 project_matrix_free_component(
333 const Mapping<dim, spacedim> &mapping,
334 const DoFHandler<dim, spacedim> &dof,
335 const AffineConstraints<Number> &constraints,
336 const Quadrature<dim> &quadrature,
337 const Function<
338 spacedim,
339 typename LinearAlgebra::distributed::Vector<Number>::value_type>
340 &function,
341 LinearAlgebra::distributed::Vector<Number> &work_result,
342 const bool enforce_zero_boundary,
343 const Quadrature<dim - 1> &q_boundary,
344 const bool project_to_boundary_first)
345 {
346 switch (dof.get_fe(0).n_components())
347 {
348 case 1:
349 project_matrix_free<1>(mapping,
350 dof,
351 constraints,
352 quadrature,
353 function,
354 work_result,
355 enforce_zero_boundary,
356 q_boundary,
357 project_to_boundary_first);
358 break;
359
360 case 2:
361 project_matrix_free<2>(mapping,
362 dof,
363 constraints,
364 quadrature,
365 function,
366 work_result,
367 enforce_zero_boundary,
368 q_boundary,
369 project_to_boundary_first);
370 break;
371
372 case 3:
373 project_matrix_free<3>(mapping,
374 dof,
375 constraints,
376 quadrature,
377 function,
378 work_result,
379 enforce_zero_boundary,
380 q_boundary,
381 project_to_boundary_first);
382 break;
383
384 case 4:
385 project_matrix_free<4>(mapping,
386 dof,
387 constraints,
388 quadrature,
389 function,
390 work_result,
391 enforce_zero_boundary,
392 q_boundary,
393 project_to_boundary_first);
394 break;
395
396 default:
397 DEAL_II_ASSERT_UNREACHABLE();
398 }
399 }
400
401
402
403 /**
404 * Helper interface for the matrix-free implementation of project(): avoid
405 * instantiating the other helper functions for more than one VectorType
406 * by copying from a LinearAlgebra::distributed::Vector.
407 *
408 * @dealiiConceptRequires{concepts::is_writable_dealii_vector_type<VectorType>}
409 */
410 template <int dim, typename VectorType, int spacedim>
411 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
412 void project_matrix_free_copy_vector(
413 const Mapping<dim, spacedim> &mapping,
414 const DoFHandler<dim, spacedim> &dof,
415 const AffineConstraints<typename VectorType::value_type> &constraints,
416 const Quadrature<dim> &quadrature,
417 const Function<spacedim, typename VectorType::value_type> &function,
418 VectorType &vec_result,
419 const bool enforce_zero_boundary,
420 const Quadrature<dim - 1> &q_boundary,
421 const bool project_to_boundary_first)
422 {
423 AssertDimension(vec_result.size(), dof.n_dofs());
424
425 LinearAlgebra::distributed::Vector<typename VectorType::value_type>
426 work_result;
427 project_matrix_free_component(mapping,
428 dof,
429 constraints,
430 quadrature,
431 function,
432 work_result,
433 enforce_zero_boundary,
434 q_boundary,
435 project_to_boundary_first);
436
437 for (const auto i : dof.locally_owned_dofs())
438 ::dealii::internal::ElementAccess<VectorType>::set(work_result(i),
439 i,
440 vec_result);
441 vec_result.compress(VectorOperation::insert);
442 }
443
444
445
446 /**
447 * Return whether the boundary values try to constrain a degree of freedom
448 * that is already constrained to something else
449 */
450 template <typename Number>
451 bool
452 constraints_and_b_v_are_compatible(
453 const AffineConstraints<Number> &constraints,
454 std::map<types::global_dof_index, Number> &boundary_values)
455 {
456 for (const auto &boundary_value : boundary_values)
457 if (constraints.is_constrained(boundary_value.first))
458 // TODO: This looks wrong -- shouldn't it be ==0 in the first
459 // condition and && ?
460 if (!(constraints.get_constraint_entries(boundary_value.first)
461 ->size() > 0 ||
462 (constraints.get_inhomogeneity(boundary_value.first) ==
463 boundary_value.second)))
464 return false;
465
466 return true;
467 }
468
469
470
471 /**
472 * Generic implementation of the project() function
473 *
474 * @dealiiConceptRequires{concepts::is_writable_dealii_vector_type<VectorType>}
475 */
476 template <int dim,
477 int spacedim,
478 typename VectorType,
479 template <int, int>
480 class M_or_MC,
481 template <int>
482 class Q_or_QC>
483 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
484 void do_project(
485 const M_or_MC<dim, spacedim> &mapping,
486 const DoFHandler<dim, spacedim> &dof,
487 const AffineConstraints<typename VectorType::value_type> &constraints,
488 const Q_or_QC<dim> &quadrature,
489 const Function<spacedim, typename VectorType::value_type> &function,
490 VectorType &vec_result,
491 const bool enforce_zero_boundary,
492 const Q_or_QC<dim - 1> &q_boundary,
493 const bool project_to_boundary_first)
494 {
495 using Number = typename VectorType::value_type;
496 AssertDimension(dof.get_fe(0).n_components(), function.n_components);
497 AssertDimension(vec_result.size(), dof.n_dofs());
498
499 // make up boundary values
500 std::map<types::global_dof_index, Number> boundary_values;
501 project_compute_b_v(mapping,
502 dof,
503 function,
504 enforce_zero_boundary,
505 q_boundary,
506 project_to_boundary_first,
507 boundary_values);
508
509 // check if constraints are compatible (see below)
510 const bool constraints_are_compatible =
511 constraints_and_b_v_are_compatible<Number>(constraints,
512 boundary_values);
513
514 // set up mass matrix and right hand side
515 Vector<Number> vec(dof.n_dofs());
516 SparsityPattern sparsity;
517 {
518 DynamicSparsityPattern dsp(dof.n_dofs(), dof.n_dofs());
519 DoFTools::make_sparsity_pattern(dof,
520 dsp,
521 constraints,
522 !constraints_are_compatible);
523
524 sparsity.copy_from(dsp);
525 }
526 SparseMatrix<Number> mass_matrix(sparsity);
527 Vector<Number> tmp(mass_matrix.n());
528
529 // If the constraints object does not conflict with the given boundary
530 // values (i.e., it either does not contain boundary values or it contains
531 // the same as boundary_values), we can let it call
532 // distribute_local_to_global straight away, otherwise we need to first
533 // interpolate the boundary values and then condense the matrix and vector
534 if (constraints_are_compatible)
535 {
536 const Function<spacedim, Number> *dummy = nullptr;
537 MatrixCreator::create_mass_matrix(mapping,
538 dof,
539 quadrature,
540 mass_matrix,
541 function,
542 tmp,
543 dummy,
544 constraints);
545 if (boundary_values.size() > 0)
546 MatrixTools::apply_boundary_values(
547 boundary_values, mass_matrix, vec, tmp, true);
548 }
549 else
550 {
551 // create mass matrix and rhs at once, which is faster.
552 MatrixCreator::create_mass_matrix(
553 mapping, dof, quadrature, mass_matrix, function, tmp);
554 MatrixTools::apply_boundary_values(
555 boundary_values, mass_matrix, vec, tmp, true);
556 constraints.condense(mass_matrix, tmp);
557 }
558
559 // Allow for a maximum of 5*n steps to reduce the residual by 10^-12. n
560 // steps may not be sufficient, since roundoff errors may accumulate for
561 // badly conditioned matrices
562 ReductionControl control(5 * tmp.size(), 0., 1e-12, false, false);
563 GrowingVectorMemory<Vector<Number>> memory;
564 SolverCG<Vector<Number>> cg(control, memory);
565
566 PreconditionSSOR<SparseMatrix<Number>> prec;
567 prec.initialize(mass_matrix, 1.2);
568
569 cg.solve(mass_matrix, vec, tmp, prec);
570 constraints.distribute(vec);
571
572 // copy vec into vec_result. we can't use vec_result itself above, since
573 // it may be of another type than Vector<double> and that wouldn't
574 // necessarily go together with the matrix and other functions
575 for (unsigned int i = 0; i < vec.size(); ++i)
576 ::dealii::internal::ElementAccess<VectorType>::set(vec(i),
577 i,
578 vec_result);
579 }
580
581
582
583 template <int dim, typename VectorType, int spacedim>
584 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
585 void project_parallel(
586 const Mapping<dim, spacedim> &mapping,
587 const DoFHandler<dim, spacedim> &dof,
588 const AffineConstraints<typename VectorType::value_type> &constraints,
589 const Quadrature<dim> &quadrature,
590 const std::function<typename VectorType::value_type(
591 const typename DoFHandler<dim, spacedim>::active_cell_iterator &,
592 const unsigned int)> &func,
593 VectorType &vec_result)
594 {
595 using Number = typename VectorType::value_type;
596 using LocalVectorType = LinearAlgebra::distributed::Vector<Number>;
597 AssertDimension(dof.get_fe(0).n_components(), 1);
598 AssertDimension(vec_result.size(), dof.n_dofs());
599
600 // set up mass matrix and right hand side
601 typename MatrixFree<dim, Number>::AdditionalData additional_data;
602 additional_data.tasks_parallel_scheme =
603 MatrixFree<dim, Number>::AdditionalData::partition_color;
604 additional_data.mapping_update_flags =
605 (update_values | update_JxW_values);
606 std::shared_ptr<MatrixFree<dim, Number>> matrix_free(
607 new MatrixFree<dim, Number>());
608 matrix_free->reinit(mapping,
609 dof,
610 constraints,
611 QGauss<1>(dof.get_fe().degree + 2),
612 additional_data);
613 using MatrixType =
614 MatrixFreeOperators::MassOperator<dim, -1, 0, 1, LocalVectorType>;
615 MatrixType mass_matrix;
616 mass_matrix.initialize(matrix_free);
617 mass_matrix.compute_diagonal();
618
619 LocalVectorType vec, rhs, inhomogeneities;
620 matrix_free->initialize_dof_vector(vec);
621 matrix_free->initialize_dof_vector(rhs);
622 matrix_free->initialize_dof_vector(inhomogeneities);
623 constraints.distribute(inhomogeneities);
624 inhomogeneities *= -1.;
625
626 // assemble right hand side:
627 {
628 FEValues<dim> fe_values(mapping,
629 dof.get_fe(),
630 quadrature,
631 update_values | update_JxW_values);
632
633 const unsigned int dofs_per_cell = dof.get_fe().n_dofs_per_cell();
634 const unsigned int n_q_points = quadrature.size();
635 Vector<Number> cell_rhs(dofs_per_cell);
636 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
637 for (const auto &cell : dof.active_cell_iterators())
638 if (cell->is_locally_owned())
639 {
640 cell_rhs = 0;
641 fe_values.reinit(cell);
642 for (unsigned int q_point = 0; q_point < n_q_points; ++q_point)
643 {
644 const double val_q = func(cell, q_point);
645 for (unsigned int i = 0; i < dofs_per_cell; ++i)
646 cell_rhs(i) += (fe_values.shape_value(i, q_point) * val_q *
647 fe_values.JxW(q_point));
648 }
649
650 cell->get_dof_indices(local_dof_indices);
651 constraints.distribute_local_to_global(cell_rhs,
652 local_dof_indices,
653 rhs);
654 }
655 rhs.compress(VectorOperation::add);
656 }
657
658 mass_matrix.vmult_add(rhs, inhomogeneities);
659
660 // now invert the matrix
661 // Allow for a maximum of 5*n steps to reduce the residual by 10^-12. n
662 // steps may not be sufficient, since roundoff errors may accumulate for
663 // badly conditioned matrices. This behavior can be observed, e.g. for
664 // FE_Q_Hierarchical for degree higher than three.
665 ReductionControl control(5 * rhs.size(), 0., 1e-12, false, false);
666 SolverCG<LocalVectorType> cg(control);
667 typename PreconditionJacobi<MatrixType>::AdditionalData data(0.8);
668 PreconditionJacobi<MatrixType> preconditioner;
669 preconditioner.initialize(mass_matrix, data);
670 cg.solve(mass_matrix, vec, rhs, preconditioner);
671 vec += inhomogeneities;
672
673 constraints.distribute(vec);
674
675 const IndexSet &locally_owned_dofs = dof.locally_owned_dofs();
676 IndexSet::ElementIterator it = locally_owned_dofs.begin();
677 for (; it != locally_owned_dofs.end(); ++it)
678 ::dealii::internal::ElementAccess<VectorType>::set(vec(*it),
679 *it,
680 vec_result);
681 vec_result.compress(VectorOperation::insert);
682 }
683
684
685
686 template <int dim, typename VectorType, int spacedim>
687 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
688 void project_parallel(
689 std::shared_ptr<const MatrixFree<dim, typename VectorType::value_type>>
690 matrix_free,
691 const AffineConstraints<typename VectorType::value_type> &constraints,
692 const std::function<VectorizedArray<typename VectorType::value_type>(
693 const unsigned int,
694 const unsigned int)> &func,
695 VectorType &vec_result,
696 const unsigned int fe_component)
697 {
698 const DoFHandler<dim, spacedim> &dof =
699 matrix_free->get_dof_handler(fe_component);
700
701 using Number = typename VectorType::value_type;
702 using LocalVectorType = LinearAlgebra::distributed::Vector<Number>;
703 AssertDimension(dof.get_fe(0).n_components(), 1);
704 AssertDimension(vec_result.size(), dof.n_dofs());
705
706 using MatrixType =
707 MatrixFreeOperators::MassOperator<dim, -1, 0, 1, LocalVectorType>;
708 MatrixType mass_matrix;
709 mass_matrix.initialize(matrix_free, {fe_component});
710 mass_matrix.compute_diagonal();
711
712 LocalVectorType vec, rhs, inhomogeneities;
713 matrix_free->initialize_dof_vector(vec, fe_component);
714 matrix_free->initialize_dof_vector(rhs, fe_component);
715 matrix_free->initialize_dof_vector(inhomogeneities, fe_component);
716 constraints.distribute(inhomogeneities);
717 inhomogeneities *= -1.;
718
719 // assemble right hand side:
720 {
721 FEEvaluation<dim, -1, 0, 1, Number> fe_eval(*matrix_free, fe_component);
722 const unsigned int n_cells = matrix_free->n_cell_batches();
723 const unsigned int n_q_points = fe_eval.n_q_points;
724
725 for (unsigned int cell = 0; cell < n_cells; ++cell)
726 {
727 fe_eval.reinit(cell);
728 for (unsigned int q = 0; q < n_q_points; ++q)
729 fe_eval.submit_value(func(cell, q), q);
730
731 fe_eval.integrate(::dealii::EvaluationFlags::values);
732 fe_eval.distribute_local_to_global(rhs);
733 }
734 rhs.compress(VectorOperation::add);
735 }
736
737 mass_matrix.vmult_add(rhs, inhomogeneities);
738
739 // now invert the matrix
740 // Allow for a maximum of 5*n steps to reduce the residual by 10^-12. n
741 // steps may not be sufficient, since roundoff errors may accumulate for
742 // badly conditioned matrices. This behavior can be observed, e.g. for
743 // FE_Q_Hierarchical for degree higher than three.
744 ReductionControl control(5 * rhs.size(), 0., 1e-12, false, false);
745 SolverCG<LocalVectorType> cg(control);
746 typename PreconditionJacobi<MatrixType>::AdditionalData data(0.8);
747 PreconditionJacobi<MatrixType> preconditioner;
748 preconditioner.initialize(mass_matrix, data);
749 cg.solve(mass_matrix, vec, rhs, preconditioner);
750 vec += inhomogeneities;
751
752 constraints.distribute(vec);
753
754 const IndexSet &locally_owned_dofs = dof.locally_owned_dofs();
755 IndexSet::ElementIterator it = locally_owned_dofs.begin();
756 for (; it != locally_owned_dofs.end(); ++it)
757 ::dealii::internal::ElementAccess<VectorType>::set(vec(*it),
758 *it,
759 vec_result);
760 vec_result.compress(VectorOperation::insert);
761 }
762
763
764
765 /**
766 * Specialization of project() for the case dim==spacedim and with correct
767 * number types for MatrixFree support. Check if we actually can use the
768 * MatrixFree implementation or need to use the matrix based one nonetheless
769 * based on the number of components.
770 *
771 * @dealiiConceptRequires{concepts::is_writable_dealii_vector_type<VectorType>}
772 */
773 template <typename VectorType, int dim, int spacedim>
774 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
775 std::enable_if_t<
776 !numbers::NumberTraits<typename VectorType::value_type>::is_complex &&
777 (dim == spacedim),
778 void> project(const Mapping<dim, spacedim> &mapping,
779 const DoFHandler<dim, spacedim> &dof,
780 const AffineConstraints<typename VectorType::value_type>
781 &constraints,
782 const Quadrature<dim> &quadrature,
783 const Function<spacedim, typename VectorType::value_type>
784 &function,
785 VectorType &vec_result,
786 const bool enforce_zero_boundary,
787 const Quadrature<dim - 1> &q_boundary,
788 const bool project_to_boundary_first)
789 {
790 // If we can, use the matrix-free implementation
791 bool use_matrix_free =
792 MatrixFree<dim, typename VectorType::value_type>::is_supported(
793 dof.get_fe()) &&
794 dof.get_fe().n_base_elements() == 1;
795
796 // enforce_zero_boundary and project_to_boundary_first
797 // are not yet supported.
798 // We have explicit instantiations only if
799 // the number of components is not too high.
800 if (enforce_zero_boundary || project_to_boundary_first ||
801 dof.get_fe(0).n_components() > 4)
802 use_matrix_free = false;
803
804 if (use_matrix_free)
805 project_matrix_free_copy_vector(mapping,
806 dof,
807 constraints,
808 quadrature,
809 function,
810 vec_result,
811 enforce_zero_boundary,
812 q_boundary,
813 project_to_boundary_first);
814 else
815 {
816 Assert((dynamic_cast<const parallel::TriangulationBase<dim> *>(
817 &(dof.get_triangulation())) == nullptr),
818 ExcNotImplemented());
819 do_project(mapping,
820 dof,
821 constraints,
822 quadrature,
823 function,
824 vec_result,
825 enforce_zero_boundary,
826 q_boundary,
827 project_to_boundary_first);
828 }
829 }
830
831
832
833 /**
834 * Specialization of project() for complex numbers or `dim < spacedim`,
835 * for which we are sure that we cannot use the MatrixFree implementation.
836 *
837 * @dealiiConceptRequires{concepts::is_writable_dealii_vector_type<VectorType>}
838 */
839 template <typename VectorType, int dim, int spacedim>
840 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
841 std::enable_if_t<
842 numbers::NumberTraits<typename VectorType::value_type>::is_complex ||
843 (dim < spacedim),
844 void> project(const Mapping<dim, spacedim> &mapping,
845 const DoFHandler<dim, spacedim> &dof,
846 const AffineConstraints<typename VectorType::value_type>
847 &constraints,
848 const Quadrature<dim> &quadrature,
849 const Function<spacedim, typename VectorType::value_type>
850 &function,
851 VectorType &vec_result,
852 const bool enforce_zero_boundary,
853 const Quadrature<dim - 1> &q_boundary,
854 const bool project_to_boundary_first)
855 {
856 Assert((dynamic_cast<const parallel::TriangulationBase<dim> *>(
857 &(dof.get_triangulation())) == nullptr),
858 ExcNotImplemented());
859 do_project(mapping,
860 dof,
861 constraints,
862 quadrature,
863 function,
864 vec_result,
865 enforce_zero_boundary,
866 q_boundary,
867 project_to_boundary_first);
868 }
869
870 } // namespace internal
871
872
873
874 template <int dim, typename VectorType, int spacedim>
875 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
876 void project(
877 const Mapping<dim, spacedim> &mapping,
878 const DoFHandler<dim, spacedim> &dof,
879 const AffineConstraints<typename VectorType::value_type> &constraints,
880 const Quadrature<dim> &quadrature,
881 const std::function<typename VectorType::value_type(
882 const typename DoFHandler<dim, spacedim>::active_cell_iterator &,
883 const unsigned int)> &func,
884 VectorType &vec_result)
885 {
886 internal::project_parallel<dim, VectorType, spacedim>(
887 mapping, dof, constraints, quadrature, func, vec_result);
888 }
889
890
891
892 template <int dim, typename VectorType>
893 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
894 void project(
895 std::shared_ptr<const MatrixFree<
896 dim,
897 typename VectorType::value_type,
898 VectorizedArray<typename VectorType::value_type>>> matrix_free,
899 const AffineConstraints<typename VectorType::value_type> &constraints,
900 const unsigned int,
901 const std::function<VectorizedArray<typename VectorType::value_type>(
902 const unsigned int,
903 const unsigned int)> &func,
904 VectorType &vec_result,
905 const unsigned int fe_component)
906 {
907 internal::project_parallel<dim, VectorType, dim>(
908 matrix_free, constraints, func, vec_result, fe_component);
909 }
910
911
912
913 template <int dim, typename VectorType>
914 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
915 void project(
916 std::shared_ptr<const MatrixFree<
917 dim,
918 typename VectorType::value_type,
919 VectorizedArray<typename VectorType::value_type>>> matrix_free,
920 const AffineConstraints<typename VectorType::value_type> &constraints,
921 const std::function<VectorizedArray<typename VectorType::value_type>(
922 const unsigned int,
923 const unsigned int)> &func,
924 VectorType &vec_result,
925 const unsigned int fe_component)
926 {
927 project(matrix_free,
928 constraints,
929 matrix_free->get_dof_handler(fe_component).get_fe().degree + 1,
930 func,
931 vec_result,
932 fe_component);
933 }
934
935
936
937 template <int dim, typename VectorType, int spacedim>
938 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
939 void project(
940 const Mapping<dim, spacedim> &mapping,
941 const DoFHandler<dim, spacedim> &dof,
942 const AffineConstraints<typename VectorType::value_type> &constraints,
943 const Quadrature<dim> &quadrature,
944 const Function<spacedim, typename VectorType::value_type> &function,
945 VectorType &vec_result,
946 const bool enforce_zero_boundary,
947 const Quadrature<dim - 1> &q_boundary,
948 const bool project_to_boundary_first)
949 {
950 internal::project(mapping,
951 dof,
952 constraints,
953 quadrature,
954 function,
955 vec_result,
956 enforce_zero_boundary,
957 q_boundary,
958 project_to_boundary_first);
959 }
960
961
962
963 template <int dim, typename VectorType, int spacedim>
964 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
965 void project(
966 const DoFHandler<dim, spacedim> &dof,
967 const AffineConstraints<typename VectorType::value_type> &constraints,
968 const Quadrature<dim> &quadrature,
969 const Function<spacedim, typename VectorType::value_type> &function,
970 VectorType &vec,
971 const bool enforce_zero_boundary,
972 const Quadrature<dim - 1> &q_boundary,
973 const bool project_to_boundary_first)
974 {
975 project(get_default_linear_mapping(dof.get_triangulation()),
976 dof,
977 constraints,
978 quadrature,
979 function,
980 vec,
981 enforce_zero_boundary,
982 q_boundary,
983 project_to_boundary_first);
984 }
985
986
987
988 template <int dim, typename VectorType, int spacedim>
989 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
990 void project(
991 const hp::MappingCollection<dim, spacedim> &mapping,
992 const DoFHandler<dim, spacedim> &dof,
993 const AffineConstraints<typename VectorType::value_type> &constraints,
994 const hp::QCollection<dim> &quadrature,
995 const Function<spacedim, typename VectorType::value_type> &function,
996 VectorType &vec_result,
997 const bool enforce_zero_boundary,
998 const hp::QCollection<dim - 1> &q_boundary,
999 const bool project_to_boundary_first)
1000 {
1001 Assert((dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(
1002 &(dof.get_triangulation())) == nullptr),
1003 ExcNotImplemented());
1004
1005 internal::do_project(mapping,
1006 dof,
1007 constraints,
1008 quadrature,
1009 function,
1010 vec_result,
1011 enforce_zero_boundary,
1012 q_boundary,
1013 project_to_boundary_first);
1014 }
1015
1016
1017
1018 template <int dim, typename VectorType, int spacedim>
1019 DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>)
1020 void project(
1021 const DoFHandler<dim, spacedim> &dof,
1022 const AffineConstraints<typename VectorType::value_type> &constraints,
1023 const hp::QCollection<dim> &quadrature,
1024 const Function<spacedim, typename VectorType::value_type> &function,
1025 VectorType &vec,
1026 const bool enforce_zero_boundary,
1027 const hp::QCollection<dim - 1> &q_boundary,
1028 const bool project_to_boundary_first)
1029 {
1030 project(hp::StaticMappingQ1<dim, spacedim>::mapping_collection,
1031 dof,
1032 constraints,
1033 quadrature,
1034 function,
1035 vec,
1036 enforce_zero_boundary,
1037 q_boundary,
1038 project_to_boundary_first);
1039 }
1040
1041 } // namespace VectorTools
1042
1043 DEAL_II_NAMESPACE_CLOSE
1044
1045 #endif // dealii_vector_tools_project_templates_h