deal.II version GIT relicensing-7229-gca0444b871 2026-11-28 02:50:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
grid_tools.cc
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2001 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
14#include <deal.II/base/mpi.h>
15#include <deal.II/base/mpi.templates.h>
19
20#ifdef DEAL_II_WITH_ARBORX
23#endif
24
25#ifdef DEAL_II_WITH_CGAL
28#endif
29
34
38
40#include <deal.II/fe/fe_q.h>
44
49#include <deal.II/grid/tria.h>
52
61#include <deal.II/lac/vector.h>
63
66
68
69#include <boost/random/mersenne_twister.hpp>
70#include <boost/random/uniform_real_distribution.hpp>
71
72#include <array>
73#include <cmath>
74#include <iostream>
75#include <limits>
76#include <list>
77#include <numeric>
78#include <set>
79#include <tuple>
80#include <unordered_map>
81
83
84#ifndef DEAL_II_WITH_ARBORX
85
86// If we configured without ArborX, we still need to have a couple of
87// dummy types that we can reference in code below. They do not
88// actually do anything useful.
89template <int dim, typename Number>
90class BoundingBox;
91
92namespace ArborXWrappers
93{
94 class DistributedTree
95 {
96 public:
97 template <int dim, typename Number>
99 const std::vector<BoundingBox<dim, Number>> &);
100
101 template <typename QueryType>
102 std::pair<std::vector<std::pair<int, int>>, std::vector<int>>
103 query(const QueryType &queries);
104 };
105
106 class BoundingBoxIntersectPredicate
107 {};
108} // namespace ArborXWrappers
109#endif
110
111
112namespace GridTools
113{
114 // define some transformations
115 namespace internal
116 {
117 template <int spacedim>
118 class Shift
119 {
120 public:
122 : shift(shift)
123 {}
126 {
127 return p + shift;
128 }
129
130 private:
132 };
133
134
135 // Transformation to rotate around one of the cartesian z-axis in 2d.
137 {
138 public:
139 explicit Rotate2d(const double angle)
141 Physics::Transformations::Rotations::rotation_matrix_2d(angle))
142 {}
144 operator()(const Point<2> &p) const
145 {
146 return static_cast<Point<2>>(rotation_matrix * p);
147 }
148
149 private:
151 };
152
153
154 // Transformation to rotate around one of the cartesian axes.
156 {
157 public:
158 Rotate3d(const Tensor<1, 3, double> &axis, const double angle)
160 Physics::Transformations::Rotations::rotation_matrix_3d(axis,
161 angle))
162 {}
163
165 operator()(const Point<3> &p) const
166 {
167 return static_cast<Point<3>>(rotation_matrix * p);
168 }
169
170 private:
172 };
173
174
175 template <int spacedim>
176 class Scale
177 {
178 public:
179 explicit Scale(const double factor)
180 : factor(factor)
181 {}
184 {
185 return p * factor;
186 }
187
188 private:
189 const double factor;
190 };
191 } // namespace internal
192
193
194 template <int dim, int spacedim>
195 void
196 shift(const Tensor<1, spacedim> &shift_vector,
197 Triangulation<dim, spacedim> &triangulation)
198 {
199 transform(internal::Shift<spacedim>(shift_vector), triangulation);
200 }
201
202
203
204 template <int dim, int spacedim>
205 void
206 rotate(const double /*angle*/,
207 Triangulation<dim, spacedim> & /*triangulation*/)
208 {
209 AssertThrow(false,
211 "GridTools::rotate() is only available for spacedim = 2."));
212 }
213
214
215
216 template <>
217 void
218 rotate(const double angle, Triangulation<1, 2> &triangulation)
219 {
220 transform(internal::Rotate2d(angle), triangulation);
221 }
222
223
224
225 template <>
226 void
227 rotate(const double angle, Triangulation<2, 2> &triangulation)
228 {
229 transform(internal::Rotate2d(angle), triangulation);
230 }
231
232
233 template <int dim>
234 void
236 const double angle,
237 Triangulation<dim, 3> &triangulation)
238 {
239 transform(internal::Rotate3d(axis, angle), triangulation);
240 }
241
242
243 template <int dim, int spacedim>
244 void
245 scale(const double scaling_factor,
246 Triangulation<dim, spacedim> &triangulation)
247 {
248 Assert(scaling_factor > 0, ExcScalingFactorNotPositive(scaling_factor));
249 transform(internal::Scale<spacedim>(scaling_factor), triangulation);
250 }
251
252
253 namespace internal
254 {
260 void
262 const AffineConstraints<double> &constraints,
264 {
265 const unsigned int n_dofs = S.n();
266 const auto op = linear_operator(S);
267 const auto SF = constrained_linear_operator(constraints, op);
269 prec.initialize(S, 1.2);
270
271 SolverControl control(n_dofs, 1.e-10, false, false);
273 SolverCG<Vector<double>> solver(control, mem);
274
275 Vector<double> f(n_dofs);
276
277 const auto constrained_rhs =
278 constrained_right_hand_side(constraints, op, f);
279 solver.solve(SF, u, constrained_rhs, prec);
280
281 constraints.distribute(u);
282 }
283 } // namespace internal
284
285
286 // Implementation for dimensions except 1
287 template <int dim>
288 void
289 laplace_transform(const std::map<unsigned int, Point<dim>> &new_points,
290 Triangulation<dim> &triangulation,
291 const Function<dim> *coefficient,
292 const bool solve_for_absolute_positions)
293 {
294 if (dim == 1)
296
297 // first provide everything that is needed for solving a Laplace
298 // equation.
299 FE_Q<dim> q1(1);
300
301 DoFHandler<dim> dof_handler(triangulation);
302 dof_handler.distribute_dofs(q1);
303
304 DynamicSparsityPattern dsp(dof_handler.n_dofs(), dof_handler.n_dofs());
305 DoFTools::make_sparsity_pattern(dof_handler, dsp);
306 dsp.compress();
307
308 SparsityPattern sparsity_pattern;
309 sparsity_pattern.copy_from(dsp);
310 sparsity_pattern.compress();
311
312 SparseMatrix<double> S(sparsity_pattern);
313
314 const QGauss<dim> quadrature(4);
315
318 const auto reference_cell = ReferenceCells::get_hypercube<dim>();
320 reference_cell.template get_default_linear_mapping<dim>(),
321 dof_handler,
322 quadrature,
323 S,
324 coefficient);
325
326 // set up the boundary values for the laplace problem
327 std::array<AffineConstraints<double>, dim> constraints;
328 typename std::map<unsigned int, Point<dim>>::const_iterator map_end =
329 new_points.end();
330
331 // Fill these maps using the data given by new_points
332 for (const auto &cell : dof_handler.active_cell_iterators())
333 {
334 // Loop over all vertices of the cell and see if it is listed in the map
335 // given as first argument of the function. We visit vertices multiple
336 // times, so also check that if we have already added a constraint, we
337 // don't do it a second time again.
338 for (const unsigned int vertex_no : cell->vertex_indices())
339 {
340 const unsigned int vertex_index = cell->vertex_index(vertex_no);
341 const Point<dim> &vertex_point = cell->vertex(vertex_no);
342
343 const typename std::map<unsigned int, Point<dim>>::const_iterator
344 map_iter = new_points.find(vertex_index);
345
346 if (map_iter != map_end)
347 for (unsigned int i = 0; i < dim; ++i)
348 if (constraints[i].is_constrained(
349 cell->vertex_dof_index(vertex_no, 0)) == false)
350 {
351 constraints[i].add_constraint(
352 cell->vertex_dof_index(vertex_no, 0),
353 {},
354 (solve_for_absolute_positions ?
355 map_iter->second[i] :
356 map_iter->second[i] - vertex_point[i]));
357 }
358 }
359 }
360
361 for (unsigned int i = 0; i < dim; ++i)
362 constraints[i].close();
363
364 // solve the dim problems with different right hand sides.
365 Vector<double> us[dim];
366 for (unsigned int i = 0; i < dim; ++i)
367 us[i].reinit(dof_handler.n_dofs());
368
369 // solve linear systems in parallel
371 for (unsigned int i = 0; i < dim; ++i)
372 tasks +=
373 Threads::new_task(&internal::laplace_solve, S, constraints[i], us[i]);
374 tasks.join_all();
375
376 // change the coordinates of the points of the triangulation
377 // according to the computed values
378 std::vector<bool> vertex_touched(triangulation.n_vertices(), false);
379 for (const auto &cell : dof_handler.active_cell_iterators())
380 for (const unsigned int vertex_no : cell->vertex_indices())
381 if (vertex_touched[cell->vertex_index(vertex_no)] == false)
382 {
383 Point<dim> &v = cell->vertex(vertex_no);
384
385 const types::global_dof_index dof_index =
386 cell->vertex_dof_index(vertex_no, 0);
387 for (unsigned int i = 0; i < dim; ++i)
388 if (solve_for_absolute_positions)
389 v[i] = us[i](dof_index);
390 else
391 v[i] += us[i](dof_index);
392
393 vertex_touched[cell->vertex_index(vertex_no)] = true;
394 }
395 }
396
401 template <int dim, int spacedim>
402 void
403 distort_random(const double factor,
404 Triangulation<dim, spacedim> &triangulation,
405 const bool keep_boundary,
406 const unsigned int seed)
407 {
408 // if spacedim>dim we need to make sure that we perturb
409 // points but keep them on
410 // the manifold. however, this isn't implemented right now
411 Assert(spacedim == dim, ExcNotImplemented());
412
413
414 // find the smallest length of the
415 // lines adjacent to the
416 // vertex. take the initial value
417 // to be larger than anything that
418 // might be found: the diameter of
419 // the triangulation, here
420 // estimated by adding up the
421 // diameters of the coarse grid
422 // cells.
423 double almost_infinite_length = 0;
425 triangulation.begin(0);
426 cell != triangulation.end(0);
427 ++cell)
428 almost_infinite_length += cell->diameter();
429
430 std::vector<double> minimal_length(triangulation.n_vertices(),
431 almost_infinite_length);
432
433 // also note if a vertex is at the boundary
434 std::vector<bool> at_boundary(keep_boundary ? triangulation.n_vertices() :
435 0,
436 false);
437 // for parallel::shared::Triangulation we need to work on all vertices,
438 // not just the ones related to locally owned cells;
439 const bool is_parallel_shared =
441 &triangulation) != nullptr);
442 for (const auto &cell : triangulation.active_cell_iterators())
443 if (is_parallel_shared || cell->is_locally_owned())
444 {
445 if (dim > 1)
446 {
447 for (unsigned int i = 0; i < cell->n_lines(); ++i)
448 {
450 line = cell->line(i);
451
452 if (keep_boundary && line->at_boundary())
453 {
454 at_boundary[line->vertex_index(0)] = true;
455 at_boundary[line->vertex_index(1)] = true;
456 }
457
458 minimal_length[line->vertex_index(0)] =
459 std::min(line->diameter(),
460 minimal_length[line->vertex_index(0)]);
461 minimal_length[line->vertex_index(1)] =
462 std::min(line->diameter(),
463 minimal_length[line->vertex_index(1)]);
464 }
465 }
466 else // dim==1
467 {
468 if (keep_boundary)
469 for (unsigned int vertex = 0; vertex < 2; ++vertex)
470 if (cell->at_boundary(vertex) == true)
471 at_boundary[cell->vertex_index(vertex)] = true;
472
473 minimal_length[cell->vertex_index(0)] =
474 std::min(cell->diameter(),
475 minimal_length[cell->vertex_index(0)]);
476 minimal_length[cell->vertex_index(1)] =
477 std::min(cell->diameter(),
478 minimal_length[cell->vertex_index(1)]);
479 }
480 }
481
482 // create a random number generator for the interval [-1,1]
483 boost::random::mt19937 rng(seed);
484 boost::random::uniform_real_distribution<> uniform_distribution(-1, 1);
485
486 // If the triangulation is distributed, we need to
487 // exchange the moved vertices across mpi processes
488 if (auto distributed_triangulation =
490 &triangulation))
491 {
492 const std::vector<bool> locally_owned_vertices =
493 get_locally_owned_vertices(triangulation);
494 std::vector<bool> vertex_moved(triangulation.n_vertices(), false);
495
496 // Next move vertices on locally owned cells
497 for (const auto &cell : triangulation.active_cell_iterators())
498 if (cell->is_locally_owned())
499 {
500 for (const unsigned int vertex_no : cell->vertex_indices())
501 {
502 const unsigned global_vertex_no =
503 cell->vertex_index(vertex_no);
504
505 // ignore this vertex if we shall keep the boundary and
506 // this vertex *is* at the boundary, if it is already moved
507 // or if another process moves this vertex
508 if ((keep_boundary && at_boundary[global_vertex_no]) ||
509 vertex_moved[global_vertex_no] ||
510 !locally_owned_vertices[global_vertex_no])
511 continue;
512
513 // first compute a random shift vector
514 Point<spacedim> shift_vector;
515 for (unsigned int d = 0; d < spacedim; ++d)
516 shift_vector[d] = uniform_distribution(rng);
517
518 shift_vector *= factor * minimal_length[global_vertex_no] /
519 std::sqrt(shift_vector.square());
520
521 // finally move the vertex
522 cell->vertex(vertex_no) += shift_vector;
523 vertex_moved[global_vertex_no] = true;
524 }
525 }
526
527 distributed_triangulation->communicate_locally_moved_vertices(
528 locally_owned_vertices);
529 }
530 else
531 // if this is a sequential triangulation, we could in principle
532 // use the algorithm above, but we'll use an algorithm that we used
533 // before the parallel::distributed::Triangulation was introduced
534 // in order to preserve backward compatibility
535 {
536 // loop over all vertices and compute their new locations
537 const unsigned int n_vertices = triangulation.n_vertices();
538 std::vector<Point<spacedim>> new_vertex_locations(n_vertices);
539 const std::vector<Point<spacedim>> &old_vertex_locations =
540 triangulation.get_vertices();
541
542 for (unsigned int vertex = 0; vertex < n_vertices; ++vertex)
543 {
544 // ignore this vertex if we will keep the boundary and
545 // this vertex *is* at the boundary
546 if (keep_boundary && at_boundary[vertex])
547 new_vertex_locations[vertex] = old_vertex_locations[vertex];
548 else
549 {
550 // compute a random shift vector
551 Point<spacedim> shift_vector;
552 for (unsigned int d = 0; d < spacedim; ++d)
553 shift_vector[d] = uniform_distribution(rng);
554
555 shift_vector *= factor * minimal_length[vertex] /
556 std::sqrt(shift_vector.square());
557
558 // record new vertex location
559 new_vertex_locations[vertex] =
560 old_vertex_locations[vertex] + shift_vector;
561 }
562 }
563
564 // now do the actual move of the vertices
565 for (const auto &cell : triangulation.active_cell_iterators())
566 for (const unsigned int vertex_no : cell->vertex_indices())
567 cell->vertex(vertex_no) =
568 new_vertex_locations[cell->vertex_index(vertex_no)];
569 }
570
571 // Correct hanging nodes if necessary
572 if (dim >= 2)
573 {
574 // We do the same as in GridTools::transform
575 //
576 // exclude hanging nodes at the boundaries of artificial cells:
577 // these may belong to ghost cells for which we know the exact
578 // location of vertices, whereas the artificial cell may or may
579 // not be further refined, and so we cannot know whether
580 // the location of the hanging node is correct or not
582 cell = triangulation.begin_active(),
583 endc = triangulation.end();
584 for (; cell != endc; ++cell)
585 if (!cell->is_artificial())
586 for (const unsigned int face : cell->face_indices())
587 if (cell->face(face)->has_children() &&
588 !cell->face(face)->at_boundary())
589 {
590 // this face has hanging nodes
591 if (dim == 2)
592 cell->face(face)->child(0)->vertex(1) =
593 (cell->face(face)->vertex(0) +
594 cell->face(face)->vertex(1)) /
595 2;
596 else if (dim == 3)
597 {
598 cell->face(face)->child(0)->vertex(1) =
599 .5 * (cell->face(face)->vertex(0) +
600 cell->face(face)->vertex(1));
601 cell->face(face)->child(0)->vertex(2) =
602 .5 * (cell->face(face)->vertex(0) +
603 cell->face(face)->vertex(2));
604 cell->face(face)->child(1)->vertex(3) =
605 .5 * (cell->face(face)->vertex(1) +
606 cell->face(face)->vertex(3));
607 cell->face(face)->child(2)->vertex(3) =
608 .5 * (cell->face(face)->vertex(2) +
609 cell->face(face)->vertex(3));
610
611 // center of the face
612 cell->face(face)->child(0)->vertex(3) =
613 .25 * (cell->face(face)->vertex(0) +
614 cell->face(face)->vertex(1) +
615 cell->face(face)->vertex(2) +
616 cell->face(face)->vertex(3));
617 }
618 }
619 }
620 }
621
622
623
624 template <int dim, template <int, int> class MeshType, int spacedim>
626 (concepts::is_triangulation_or_dof_handler<MeshType<dim, spacedim>>))
627 unsigned int find_closest_vertex(const MeshType<dim, spacedim> &mesh,
628 const Point<spacedim> &p,
629 const std::vector<bool> &marked_vertices)
630 {
631 // first get the underlying triangulation from the mesh and determine
632 // vertices and used vertices
634
635 const std::vector<Point<spacedim>> &vertices = tria.get_vertices();
636
637 Assert(tria.get_vertices().size() == marked_vertices.size() ||
638 marked_vertices.empty(),
640 marked_vertices.size()));
641
642 // marked_vertices is expected to be a subset of used_vertices. Thus,
643 // comparing the range marked_vertices.begin() to marked_vertices.end() with
644 // the range used_vertices.begin() to used_vertices.end() the element in the
645 // second range must be valid if the element in the first range is valid.
646 Assert(
647 marked_vertices.empty() ||
648 std::equal(marked_vertices.begin(),
649 marked_vertices.end(),
650 tria.get_used_vertices().begin(),
651 [](bool p, bool q) { return !p || q; }),
653 "marked_vertices should be a subset of used vertices in the triangulation "
654 "but marked_vertices contains one or more vertices that are not used vertices!"));
655
656 // If marked_indices is empty, consider all used_vertices for finding the
657 // closest vertex to the point. Otherwise, marked_indices is used.
658 const std::vector<bool> &vertices_to_use =
659 (marked_vertices.empty()) ? tria.get_used_vertices() : marked_vertices;
660
661 // At the beginning, the first used vertex is considered to be the closest
662 // one.
663 std::vector<bool>::const_iterator first =
664 std::find(vertices_to_use.begin(), vertices_to_use.end(), true);
665
666 // Assert that at least one vertex is actually used
667 Assert(first != vertices_to_use.end(), ExcInternalError());
668
669 unsigned int best_vertex = std::distance(vertices_to_use.begin(), first);
670 double best_dist = (p - vertices[best_vertex]).norm_square();
671
672 // For all remaining vertices, test
673 // whether they are any closer
674 for (unsigned int j = best_vertex + 1; j < vertices.size(); ++j)
675 if (vertices_to_use[j])
676 {
677 const double dist = (p - vertices[j]).norm_square();
678 if (dist < best_dist)
679 {
680 best_vertex = j;
681 best_dist = dist;
682 }
683 }
684
685 return best_vertex;
686 }
687
688
689
690 template <int dim, template <int, int> class MeshType, int spacedim>
692 (concepts::is_triangulation_or_dof_handler<MeshType<dim, spacedim>>))
693 unsigned int find_closest_vertex(const Mapping<dim, spacedim> &mapping,
694 const MeshType<dim, spacedim> &mesh,
695 const Point<spacedim> &p,
696 const std::vector<bool> &marked_vertices)
697 {
698 // Take a shortcut in the simple case.
699 if (mapping.preserves_vertex_locations() == true)
700 return find_closest_vertex(mesh, p, marked_vertices);
701
702 // first get the underlying triangulation from the mesh and determine
703 // vertices and used vertices
705
706 auto vertices = extract_used_vertices(tria, mapping);
707
708 Assert(tria.get_vertices().size() == marked_vertices.size() ||
709 marked_vertices.empty(),
711 marked_vertices.size()));
712
713 // marked_vertices is expected to be a subset of used_vertices. Thus,
714 // comparing the range marked_vertices.begin() to marked_vertices.end()
715 // with the range used_vertices.begin() to used_vertices.end() the element
716 // in the second range must be valid if the element in the first range is
717 // valid.
718 Assert(
719 marked_vertices.empty() ||
720 std::equal(marked_vertices.begin(),
721 marked_vertices.end(),
722 tria.get_used_vertices().begin(),
723 [](bool p, bool q) { return !p || q; }),
725 "marked_vertices should be a subset of used vertices in the triangulation "
726 "but marked_vertices contains one or more vertices that are not used vertices!"));
727
728 // Remove from the map unwanted elements.
729 if (marked_vertices.size() != 0)
730 for (auto it = vertices.begin(); it != vertices.end();)
731 {
732 if (marked_vertices[it->first] == false)
733 {
734 it = vertices.erase(it);
735 }
736 else
737 {
738 ++it;
739 }
740 }
741
742 return find_closest_vertex(vertices, p);
743 }
744
745
746
747 template <int dim, int spacedim>
748 std::vector<std::vector<Tensor<1, spacedim>>>
751 const std::vector<
753 &vertex_to_cells)
754 {
755 const std::vector<Point<spacedim>> &vertices = mesh.get_vertices();
756 const unsigned int n_vertices = vertex_to_cells.size();
757
758 AssertDimension(vertices.size(), n_vertices);
759
760
761 std::vector<std::vector<Tensor<1, spacedim>>> vertex_to_cell_centers(
762 n_vertices);
763 for (unsigned int vertex = 0; vertex < n_vertices; ++vertex)
764 if (mesh.vertex_used(vertex))
765 {
766 const unsigned int n_neighbor_cells = vertex_to_cells[vertex].size();
767 vertex_to_cell_centers[vertex].resize(n_neighbor_cells);
768
769 typename std::set<typename Triangulation<dim, spacedim>::
770 active_cell_iterator>::iterator it =
771 vertex_to_cells[vertex].begin();
772 for (unsigned int cell = 0; cell < n_neighbor_cells; ++cell, ++it)
773 {
774 vertex_to_cell_centers[vertex][cell] =
775 (*it)->center() - vertices[vertex];
776 vertex_to_cell_centers[vertex][cell] /=
777 vertex_to_cell_centers[vertex][cell].norm();
778 }
779 }
780 return vertex_to_cell_centers;
781 }
782
783
784
785 template <int dim, template <int, int> class MeshType, int spacedim>
787 (concepts::is_triangulation_or_dof_handler<MeshType<dim, spacedim>>))
788#ifndef _MSC_VER
789 std::pair<typename MeshType<dim, spacedim>::active_cell_iterator, Point<dim>>
790#else
791 std::pair<typename ::internal::
792 ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type,
794#endif
796 const Mapping<dim, spacedim> &mapping,
797 const MeshType<dim, spacedim> &mesh,
798 const Point<spacedim> &p,
799 const std::vector<
800 std::set<typename MeshType<dim, spacedim>::active_cell_iterator>>
801 &vertex_to_cells,
802 const std::vector<std::vector<Tensor<1, spacedim>>>
803 &vertex_to_cell_centers,
804 const typename MeshType<dim, spacedim>::active_cell_iterator &cell_hint,
805 const std::vector<bool> &marked_vertices,
806 const RTree<std::pair<Point<spacedim>, unsigned int>>
807 &used_vertices_rtree,
808 const double tolerance,
809 const RTree<
810 std::pair<BoundingBox<spacedim>,
812 *relevant_cell_bounding_boxes_rtree)
813 {
814 std::pair<typename MeshType<dim, spacedim>::active_cell_iterator,
816 cell_and_position;
817 cell_and_position.first = mesh.end();
818
819 // To handle points at the border we keep track of points which are close to
820 // the unit cell:
821 std::pair<typename MeshType<dim, spacedim>::active_cell_iterator,
823 cell_and_position_approx;
824
825 if (relevant_cell_bounding_boxes_rtree != nullptr &&
826 !relevant_cell_bounding_boxes_rtree->empty())
827 {
828 // create a bounding box around point p with 2*tolerance as side length.
829 const auto bb = BoundingBox<spacedim>(p).create_extended(tolerance);
830
831 if (relevant_cell_bounding_boxes_rtree->qbegin(
832 boost::geometry::index::intersects(bb)) ==
833 relevant_cell_bounding_boxes_rtree->qend())
834 return cell_and_position;
835 }
836
837 bool found_cell = false;
838 bool approx_cell = false;
839
840 unsigned int closest_vertex_index = 0;
841 // ensure closest vertex index is a marked one, otherwise cell (with vertex
842 // 0) might be found even though it is not marked. This is only relevant if
843 // searching with rtree, using find_closest_vertex already can manage not
844 // finding points
845 if (marked_vertices.size() && !used_vertices_rtree.empty())
846 {
847 const auto itr =
848 std::find(marked_vertices.begin(), marked_vertices.end(), true);
849 Assert(itr != marked_vertices.end(),
850 ::ExcMessage("No vertex has been marked!"));
851 closest_vertex_index = std::distance(marked_vertices.begin(), itr);
852 }
853
854 Tensor<1, spacedim> vertex_to_point;
855 auto current_cell = cell_hint;
856
857 // check whether cell has at least one marked vertex
858 const auto cell_marked = [&mesh, &marked_vertices](const auto &cell) {
859 if (marked_vertices.empty())
860 return true;
861
862 if (cell != mesh.active_cell_iterators().end())
863 for (unsigned int i = 0; i < cell->n_vertices(); ++i)
864 if (marked_vertices[cell->vertex_index(i)])
865 return true;
866
867 return false;
868 };
869
870 // check whether any cell in collection is marked
871 const auto any_cell_marked = [&cell_marked](const auto &cells) {
872 return std::any_of(cells.begin(),
873 cells.end(),
874 [&cell_marked](const auto &cell) {
875 return cell_marked(cell);
876 });
877 };
878 (void)any_cell_marked;
879
880 while (found_cell == false)
881 {
882 // First look at the vertices of the cell cell_hint. If it's an
883 // invalid cell, then query for the closest global vertex
884 if (current_cell.state() == IteratorState::valid &&
885 cell_marked(cell_hint))
886 {
887 const auto cell_vertices = mapping.get_vertices(current_cell);
888 const unsigned int closest_vertex =
889 find_closest_vertex_of_cell<dim, spacedim>(current_cell,
890 p,
891 mapping);
892 vertex_to_point = p - cell_vertices[closest_vertex];
893 closest_vertex_index = current_cell->vertex_index(closest_vertex);
894 }
895 else
896 {
897 // For some clang-based compilers and boost versions the call to
898 // RTree::query doesn't compile. Since using an rtree here is just a
899 // performance improvement disabling this branch is OK.
900 // This is fixed in boost in
901 // https://github.com/boostorg/numeric_conversion/commit/50a1eae942effb0a9b90724323ef8f2a67e7984a
902#if defined(DEAL_II_WITH_BOOST_BUNDLED) || \
903 !(defined(__clang_major__) && __clang_major__ >= 16) || \
904 BOOST_VERSION >= 108100
905 if (!used_vertices_rtree.empty())
906 {
907 // If we have an rtree at our disposal, use it.
908 using ValueType = std::pair<Point<spacedim>, unsigned int>;
909 std::function<bool(const ValueType &)> marked;
910 if (marked_vertices.size() == mesh.n_vertices())
911 marked = [&marked_vertices](const ValueType &value) -> bool {
912 return marked_vertices[value.second];
913 };
914 else
915 marked = [](const ValueType &) -> bool { return true; };
916
917 std::vector<std::pair<Point<spacedim>, unsigned int>> res;
918 used_vertices_rtree.query(
919 boost::geometry::index::nearest(p, 1) &&
920 boost::geometry::index::satisfies(marked),
921 std::back_inserter(res));
922
923 // Searching for a point which is located outside the
924 // triangulation results in res.size() = 0
925 Assert(res.size() < 2,
926 ::ExcMessage("There can not be multiple results"));
927
928 if (res.size() > 0)
929 if (any_cell_marked(vertex_to_cells[res[0].second]))
930 closest_vertex_index = res[0].second;
931 }
932 else
933#endif
934 {
935 closest_vertex_index = GridTools::find_closest_vertex(
936 mapping, mesh, p, marked_vertices);
937 }
938 vertex_to_point = p - mesh.get_vertices()[closest_vertex_index];
939 }
940
941 if constexpr (running_in_debug_mode())
942 {
943 {
944 // Double-check if found index is at marked cell
945 Assert(any_cell_marked(vertex_to_cells[closest_vertex_index]),
946 ::ExcMessage("Found non-marked vertex"));
947 }
948 }
949
950 const double vertex_point_norm = vertex_to_point.norm();
951 if (vertex_point_norm > 0)
952 vertex_to_point /= vertex_point_norm;
953
954 const unsigned int n_neighbor_cells =
955 vertex_to_cells[closest_vertex_index].size();
956
957 // Sort the points, placing the neighbor with the best alignment to
958 // 'vertex_to_point', which we measure in descending order with
959 // largest scalar product coming first. Pre-computing the scalar
960 // product outside the 'sort' function is not just a convenience here,
961 // but also crucial to make the sorting decisions stable with respect
962 // to roundoff errors: If computing the scalar products within the
963 // 'sort' function, roundoff errors around fused multiply-add (FMA)
964 // instructions sometimes generated by optimizing compilers with
965 // relaxed floating point rules could lead to a check of the form 'x >
966 // x' to return true, which sends the std::sort down the wrong path.
967 std::vector<std::pair<double, unsigned int>> neighbor_permutation(
968 n_neighbor_cells);
969 for (unsigned int i = 0; i < n_neighbor_cells; ++i)
970 neighbor_permutation[i] =
971 std::make_pair(vertex_to_cell_centers[closest_vertex_index][i] *
972 vertex_to_point,
973 i);
974 std::sort(neighbor_permutation.begin(),
975 neighbor_permutation.end(),
976 [](const std::pair<double, unsigned int> &a,
977 const std::pair<double, unsigned int> &b) {
978 return a.first > b.first;
979 });
980
981 // It is possible the vertex is close
982 // to an edge, thus we add a tolerance
983 // to keep also the "best" cell
984 double best_distance = tolerance;
985
986 // Search all of the cells adjacent to the closest vertex of the cell
987 // hint. Most likely we will find the point in them.
988 for (unsigned int i = 0; i < n_neighbor_cells; ++i)
989 {
990 try
991 {
992 auto cell = vertex_to_cells[closest_vertex_index].begin();
993 std::advance(cell, neighbor_permutation[i].second);
994
995 if (!(*cell)->is_artificial())
996 {
997 const Point<dim> p_unit =
998 mapping.transform_real_to_unit_cell(*cell, p);
999 if ((*cell)->reference_cell().contains_point(p_unit,
1000 tolerance))
1001 {
1002 cell_and_position.first = *cell;
1003 cell_and_position.second = p_unit;
1004 found_cell = true;
1005 approx_cell = false;
1006 break;
1007 }
1008 // The point is not inside this cell: checking how far
1009 // outside it is and whether we want to use this cell as a
1010 // backup if we can't find a cell within which the point
1011 // lies.
1012 const double dist = p_unit.distance(
1013 (*cell)->reference_cell().closest_point(p_unit));
1014 if (dist < best_distance)
1015 {
1016 best_distance = dist;
1017 cell_and_position_approx.first = *cell;
1018 cell_and_position_approx.second = p_unit;
1019 approx_cell = true;
1020 }
1021 }
1022 }
1023 catch (typename Mapping<dim>::ExcTransformationFailed &)
1024 {}
1025 }
1026
1027 if (found_cell == true)
1028 return cell_and_position;
1029 else if (approx_cell == true)
1030 return cell_and_position_approx;
1031
1032 // The first time around, we check for vertices in the hint_cell. If
1033 // that does not work, we set the cell iterator to an invalid one, and
1034 // look for a global vertex close to the point. If that does not work,
1035 // we are in trouble, and just throw an exception.
1036 //
1037 // If we got here, then we did not find the point. If the
1038 // current_cell.state() here is not IteratorState::valid, it means that
1039 // the user did not provide a hint_cell, and at the beginning of the
1040 // while loop we performed an actual global search on the mesh
1041 // vertices. Not finding the point then means the point is outside the
1042 // domain, or that we've had problems with the algorithm above. Try as a
1043 // last resort the other (simpler) algorithm.
1044 if (current_cell.state() != IteratorState::valid)
1046 mapping, mesh, p, marked_vertices, tolerance);
1047
1048 current_cell = typename MeshType<dim, spacedim>::active_cell_iterator();
1049 }
1050 return cell_and_position;
1051 }
1052
1053
1054
1055 template <int dim, int spacedim>
1056 unsigned int
1059 const Point<spacedim> &position,
1060 const Mapping<dim, spacedim> &mapping)
1061 {
1062 const auto vertices = mapping.get_vertices(cell);
1063 double minimum_distance = position.distance_square(vertices[0]);
1064 unsigned int closest_vertex = 0;
1065 const unsigned int n_vertices = cell->n_vertices();
1066
1067 for (unsigned int v = 1; v < n_vertices; ++v)
1068 {
1069 const double vertex_distance = position.distance_square(vertices[v]);
1070 if (vertex_distance < minimum_distance)
1071 {
1072 closest_vertex = v;
1073 minimum_distance = vertex_distance;
1074 }
1075 }
1076 return closest_vertex;
1077 }
1078
1079
1080
1081 namespace internal
1082 {
1083 namespace BoundingBoxPredicate
1084 {
1085 template <typename MeshType>
1088 std::tuple<
1090 bool> compute_cell_predicate_bounding_box(const typename MeshType::
1091 cell_iterator &parent_cell,
1092 const std::function<bool(
1093 const typename MeshType::
1094 active_cell_iterator &)>
1095 &predicate)
1096 {
1097 bool has_predicate =
1098 false; // Start assuming there's no cells with predicate inside
1099 std::vector<typename MeshType::active_cell_iterator> active_cells;
1100 if (parent_cell->is_active())
1101 active_cells = {parent_cell};
1102 else
1103 // Finding all active cells descendants of the current one (or the
1104 // current one if it is active)
1105 active_cells = get_active_child_cells<MeshType>(parent_cell);
1106
1107 const unsigned int spacedim = MeshType::space_dimension;
1108
1109 // Looking for the first active cell which has the property predicate
1110 unsigned int i = 0;
1111 while (i < active_cells.size() && !predicate(active_cells[i]))
1112 ++i;
1113
1114 // No active cells or no active cells with property
1115 if (active_cells.empty() || i == active_cells.size())
1116 {
1118 return std::make_tuple(bbox, has_predicate);
1119 }
1120
1121 // The two boundary points defining the boundary box
1122 Point<spacedim> maxp = active_cells[i]->vertex(0);
1123 Point<spacedim> minp = active_cells[i]->vertex(0);
1124
1125 for (; i < active_cells.size(); ++i)
1126 if (predicate(active_cells[i]))
1127 for (const unsigned int v : active_cells[i]->vertex_indices())
1128 for (unsigned int d = 0; d < spacedim; ++d)
1129 {
1130 minp[d] = std::min(minp[d], active_cells[i]->vertex(v)[d]);
1131 maxp[d] = std::max(maxp[d], active_cells[i]->vertex(v)[d]);
1132 }
1133
1134 has_predicate = true;
1135 BoundingBox<spacedim> bbox(std::make_pair(minp, maxp));
1136 return std::make_tuple(bbox, has_predicate);
1137 }
1138 } // namespace BoundingBoxPredicate
1139 } // namespace internal
1140
1141
1142
1143 template <typename MeshType>
1145 std::
1146 vector<BoundingBox<MeshType::space_dimension>> compute_mesh_predicate_bounding_box(
1147 const MeshType &mesh,
1148 const std::function<bool(const typename MeshType::active_cell_iterator &)>
1149 &predicate,
1150 const unsigned int refinement_level,
1151 const bool allow_merge,
1152 const unsigned int max_boxes)
1153 {
1154 // Algorithm brief description: begin with creating bounding boxes of all
1155 // cells at refinement_level (and coarser levels if there are active cells)
1156 // which have the predicate property. These are then merged
1157
1158 Assert(
1159 refinement_level <= mesh.n_levels(),
1160 ExcMessage(
1161 "Error: refinement level is higher then total levels in the triangulation!"));
1162
1163 const unsigned int spacedim = MeshType::space_dimension;
1164 std::vector<BoundingBox<spacedim>> bounding_boxes;
1165
1166 // Creating a bounding box for all active cell on coarser level
1167
1168 for (unsigned int i = 0; i < refinement_level; ++i)
1169 for (const typename MeshType::cell_iterator &cell :
1170 mesh.active_cell_iterators_on_level(i))
1171 {
1172 bool has_predicate = false;
1174 std::tie(bbox, has_predicate) =
1176 MeshType>(cell, predicate);
1177 if (has_predicate)
1178 bounding_boxes.push_back(bbox);
1179 }
1180
1181 // Creating a Bounding Box for all cells on the chosen refinement_level
1182 for (const typename MeshType::cell_iterator &cell :
1183 mesh.cell_iterators_on_level(refinement_level))
1184 {
1185 bool has_predicate = false;
1187 std::tie(bbox, has_predicate) =
1189 MeshType>(cell, predicate);
1190 if (has_predicate)
1191 bounding_boxes.push_back(bbox);
1192 }
1193
1194 if (!allow_merge)
1195 // If merging is not requested return the created bounding_boxes
1196 return bounding_boxes;
1197 else
1198 {
1199 // Merging part of the algorithm
1200 // Part 1: merging neighbors
1201 // This array stores the indices of arrays we have already merged
1202 std::vector<unsigned int> merged_boxes_idx;
1203 bool found_neighbors = true;
1204
1205 // We merge only neighbors which can be expressed by a single bounding
1206 // box e.g. in 1d [0,1] and [1,2] can be described with [0,2] without
1207 // losing anything
1208 while (found_neighbors)
1209 {
1210 found_neighbors = false;
1211 for (unsigned int i = 0; i < bounding_boxes.size() - 1; ++i)
1212 {
1213 if (std::find(merged_boxes_idx.begin(),
1214 merged_boxes_idx.end(),
1215 i) == merged_boxes_idx.end())
1216 for (unsigned int j = i + 1; j < bounding_boxes.size(); ++j)
1217 if (std::find(merged_boxes_idx.begin(),
1218 merged_boxes_idx.end(),
1219 j) == merged_boxes_idx.end() &&
1220 bounding_boxes[i].get_neighbor_type(
1221 bounding_boxes[j]) ==
1223 {
1224 bounding_boxes[i].merge_with(bounding_boxes[j]);
1225 merged_boxes_idx.push_back(j);
1226 found_neighbors = true;
1227 }
1228 }
1229 }
1230
1231 // Copying the merged boxes into merged_b_boxes
1232 std::vector<BoundingBox<spacedim>> merged_b_boxes;
1233 for (unsigned int i = 0; i < bounding_boxes.size(); ++i)
1234 if (std::find(merged_boxes_idx.begin(), merged_boxes_idx.end(), i) ==
1235 merged_boxes_idx.end())
1236 merged_b_boxes.push_back(bounding_boxes[i]);
1237
1238 // Part 2: if there are too many bounding boxes, merging smaller boxes
1239 // This has sense only in dimension 2 or greater, since in dimension 1,
1240 // neighboring intervals can always be merged without problems
1241 if ((merged_b_boxes.size() > max_boxes) && (spacedim > 1))
1242 {
1243 std::vector<double> volumes;
1244 volumes.reserve(merged_b_boxes.size());
1245 for (unsigned int i = 0; i < merged_b_boxes.size(); ++i)
1246 volumes.push_back(merged_b_boxes[i].volume());
1247
1248 while (merged_b_boxes.size() > max_boxes)
1249 {
1250 unsigned int min_idx =
1251 std::min_element(volumes.begin(), volumes.end()) -
1252 volumes.begin();
1253 volumes.erase(volumes.begin() + min_idx);
1254 // Finding a neighbor
1255 bool not_removed = true;
1256 for (unsigned int i = 0;
1257 i < merged_b_boxes.size() && not_removed;
1258 ++i)
1259 // We merge boxes if we have "attached" or "mergeable"
1260 // neighbors, even though mergeable should be dealt with in
1261 // Part 1
1262 if (i != min_idx && (merged_b_boxes[i].get_neighbor_type(
1263 merged_b_boxes[min_idx]) ==
1265 merged_b_boxes[i].get_neighbor_type(
1266 merged_b_boxes[min_idx]) ==
1268 {
1269 merged_b_boxes[i].merge_with(merged_b_boxes[min_idx]);
1270 merged_b_boxes.erase(merged_b_boxes.begin() + min_idx);
1271 not_removed = false;
1272 }
1273 Assert(!not_removed,
1274 ExcMessage("Error: couldn't merge bounding boxes!"));
1275 }
1276 }
1277 Assert(merged_b_boxes.size() <= max_boxes,
1278 ExcMessage(
1279 "Error: couldn't reach target number of bounding boxes!"));
1280 return merged_b_boxes;
1281 }
1282 }
1283
1284
1285
1286 template <int spacedim>
1287#ifndef DOXYGEN
1288 std::tuple<std::vector<std::vector<unsigned int>>,
1289 std::map<unsigned int, unsigned int>,
1290 std::map<unsigned int, std::vector<unsigned int>>>
1291#else
1292 return_type
1293#endif
1295 const std::vector<std::vector<BoundingBox<spacedim>>> &global_bboxes,
1296 const std::vector<Point<spacedim>> &points)
1297 {
1298 unsigned int n_procs = global_bboxes.size();
1299 std::vector<std::vector<unsigned int>> point_owners(n_procs);
1300 std::map<unsigned int, unsigned int> map_owners_found;
1301 std::map<unsigned int, std::vector<unsigned int>> map_owners_guessed;
1302
1303 unsigned int n_points = points.size();
1304 for (unsigned int pt = 0; pt < n_points; ++pt)
1305 {
1306 // Keep track of how many processes we guess to own the point
1307 std::vector<unsigned int> owners_found;
1308 // Check in which other processes the point might be
1309 for (unsigned int rk = 0; rk < n_procs; ++rk)
1310 {
1311 for (const BoundingBox<spacedim> &bbox : global_bboxes[rk])
1312 if (bbox.point_inside(points[pt]))
1313 {
1314 point_owners[rk].emplace_back(pt);
1315 owners_found.emplace_back(rk);
1316 break; // We can check now the next process
1317 }
1318 }
1319 Assert(owners_found.size() > 0,
1320 ExcMessage("No owners found for the point " +
1321 std::to_string(pt)));
1322 if (owners_found.size() == 1)
1323 map_owners_found[pt] = owners_found[0];
1324 else
1325 // Multiple owners
1326 map_owners_guessed[pt] = owners_found;
1327 }
1328
1329 return std::make_tuple(std::move(point_owners),
1330 std::move(map_owners_found),
1331 std::move(map_owners_guessed));
1332 }
1333
1334 template <int spacedim>
1335#ifndef DOXYGEN
1336 std::tuple<std::map<unsigned int, std::vector<unsigned int>>,
1337 std::map<unsigned int, unsigned int>,
1338 std::map<unsigned int, std::vector<unsigned int>>>
1339#else
1340 return_type
1341#endif
1343 const RTree<std::pair<BoundingBox<spacedim>, unsigned int>> &covering_rtree,
1344 const std::vector<Point<spacedim>> &points)
1345 {
1346 std::map<unsigned int, std::vector<unsigned int>> point_owners;
1347 std::map<unsigned int, unsigned int> map_owners_found;
1348 std::map<unsigned int, std::vector<unsigned int>> map_owners_guessed;
1349 std::vector<std::pair<BoundingBox<spacedim>, unsigned int>> search_result;
1350
1351 unsigned int n_points = points.size();
1352 for (unsigned int pt_n = 0; pt_n < n_points; ++pt_n)
1353 {
1354 search_result.clear(); // clearing last output
1355
1356 // Running tree search
1357 covering_rtree.query(boost::geometry::index::intersects(points[pt_n]),
1358 std::back_inserter(search_result));
1359
1360 // Keep track of how many processes we guess to own the point
1361 std::set<unsigned int> owners_found;
1362 // Check in which other processes the point might be
1363 for (const auto &rank_bbox : search_result)
1364 {
1365 // Try to add the owner to the owners found,
1366 // and check if it was already present
1367 const bool pt_inserted = owners_found.insert(pt_n).second;
1368 if (pt_inserted)
1369 point_owners[rank_bbox.second].emplace_back(pt_n);
1370 }
1371 Assert(owners_found.size() > 0,
1372 ExcMessage("No owners found for the point " +
1373 std::to_string(pt_n)));
1374 if (owners_found.size() == 1)
1375 map_owners_found[pt_n] = *owners_found.begin();
1376 else
1377 // Multiple owners
1378 std::copy(owners_found.begin(),
1379 owners_found.end(),
1380 std::back_inserter(map_owners_guessed[pt_n]));
1381 }
1382
1383 return std::make_tuple(std::move(point_owners),
1384 std::move(map_owners_found),
1385 std::move(map_owners_guessed));
1386 }
1387
1388
1389
1390 template <int dim, int spacedim>
1391 std::map<unsigned int, types::global_vertex_index>
1393 const Triangulation<dim, spacedim> &triangulation)
1394 {
1395 std::map<unsigned int, types::global_vertex_index>
1396 local_to_global_vertex_index;
1397
1398#ifndef DEAL_II_WITH_MPI
1399
1400 // If we don't have MPI then all vertices are local
1401 for (unsigned int i = 0; i < triangulation.n_vertices(); ++i)
1402 local_to_global_vertex_index[i] = i;
1403
1404#else
1405
1406 using active_cell_iterator =
1408 const std::vector<std::set<active_cell_iterator>> vertex_to_cell =
1409 vertex_to_cell_map(triangulation);
1410
1411 // Create a local index for the locally "owned" vertices
1412 types::global_vertex_index next_index = 0;
1413 unsigned int max_cellid_size = 0;
1414 std::set<std::pair<types::subdomain_id, types::global_vertex_index>>
1415 vertices_added;
1416 std::map<types::subdomain_id, std::set<unsigned int>> vertices_to_recv;
1417 std::map<types::subdomain_id,
1418 std::vector<std::tuple<types::global_vertex_index,
1420 std::string>>>
1421 vertices_to_send;
1422 std::set<active_cell_iterator> missing_vert_cells;
1423 std::set<unsigned int> used_vertex_index;
1424 for (const auto &cell : triangulation.active_cell_iterators())
1425 {
1426 if (cell->is_locally_owned())
1427 {
1428 for (const unsigned int i : cell->vertex_indices())
1429 {
1430 types::subdomain_id lowest_subdomain_id = cell->subdomain_id();
1431 for (const auto &adjacent_cell :
1432 vertex_to_cell[cell->vertex_index(i)])
1433 lowest_subdomain_id = std::min(lowest_subdomain_id,
1434 adjacent_cell->subdomain_id());
1435
1436 // See if this process "owns" this vertex
1437 if (lowest_subdomain_id == cell->subdomain_id())
1438 {
1439 // Check that the vertex we are working on is a vertex that
1440 // has not been dealt with yet
1441 if (used_vertex_index.find(cell->vertex_index(i)) ==
1442 used_vertex_index.end())
1443 {
1444 // Set the local index
1445 local_to_global_vertex_index[cell->vertex_index(i)] =
1446 next_index++;
1447
1448 // Store the information that will be sent to the
1449 // adjacent cells on other subdomains
1450 for (const auto &adjacent_cell :
1451 vertex_to_cell[cell->vertex_index(i)])
1452 if (adjacent_cell->subdomain_id() !=
1453 cell->subdomain_id())
1454 {
1455 std::pair<types::subdomain_id,
1457 tmp(adjacent_cell->subdomain_id(),
1458 cell->vertex_index(i));
1459 if (vertices_added.find(tmp) ==
1460 vertices_added.end())
1461 {
1462 vertices_to_send[adjacent_cell
1463 ->subdomain_id()]
1464 .emplace_back(i,
1465 cell->vertex_index(i),
1466 cell->id().to_string());
1467 if (cell->id().to_string().size() >
1468 max_cellid_size)
1469 max_cellid_size =
1470 cell->id().to_string().size();
1471 vertices_added.insert(tmp);
1472 }
1473 }
1474 used_vertex_index.insert(cell->vertex_index(i));
1475 }
1476 }
1477 else
1478 {
1479 // We don't own the vertex so we will receive its global
1480 // index
1481 vertices_to_recv[lowest_subdomain_id].insert(
1482 cell->vertex_index(i));
1483 missing_vert_cells.insert(cell);
1484 }
1485 }
1486 }
1487
1488 // Some hanging nodes are vertices of ghost cells. They need to be
1489 // received.
1490 if (cell->is_ghost())
1491 {
1492 for (const unsigned int i : cell->face_indices())
1493 {
1494 if (cell->at_boundary(i) == false)
1495 {
1496 if (cell->neighbor(i)->is_active())
1497 {
1498 typename Triangulation<dim,
1499 spacedim>::active_cell_iterator
1500 adjacent_cell = cell->neighbor(i);
1501 if ((adjacent_cell->is_locally_owned()))
1502 {
1503 types::subdomain_id adj_subdomain_id =
1504 adjacent_cell->subdomain_id();
1505 if (cell->subdomain_id() < adj_subdomain_id)
1506 for (unsigned int j = 0;
1507 j < cell->face(i)->n_vertices();
1508 ++j)
1509 {
1510 vertices_to_recv[cell->subdomain_id()].insert(
1511 cell->face(i)->vertex_index(j));
1512 missing_vert_cells.insert(cell);
1513 }
1514 }
1515 }
1516 }
1517 }
1518 }
1519 }
1520
1521 // Get the size of the largest CellID string
1522 max_cellid_size = Utilities::MPI::max(max_cellid_size,
1523 triangulation.get_mpi_communicator());
1524
1525 // Make indices global by getting the number of vertices owned by each
1526 // processors and shifting the indices accordingly
1528 int ierr = MPI_Exscan(
1529 &next_index,
1530 &shift,
1531 1,
1532 Utilities::MPI::mpi_type_id_for_type<types::global_vertex_index>,
1533 MPI_SUM,
1534 triangulation.get_mpi_communicator());
1535 AssertThrowMPI(ierr);
1536
1537 for (auto &global_index_it : local_to_global_vertex_index)
1538 global_index_it.second += shift;
1539
1540
1541 const int mpi_tag = Utilities::MPI::internal::Tags::
1543 const int mpi_tag2 = Utilities::MPI::internal::Tags::
1545
1546
1547 // In a first message, send the global ID of the vertices and the local
1548 // positions in the cells. In a second messages, send the cell ID as a
1549 // resize string. This is done in two messages so that types are not mixed
1550
1551 // Send the first message
1552 std::vector<std::vector<types::global_vertex_index>> vertices_send_buffers(
1553 vertices_to_send.size());
1554 std::vector<MPI_Request> first_requests(vertices_to_send.size());
1555 typename std::map<types::subdomain_id,
1556 std::vector<std::tuple<types::global_vertex_index,
1558 std::string>>>::iterator
1559 vert_to_send_it = vertices_to_send.begin(),
1560 vert_to_send_end = vertices_to_send.end();
1561 for (unsigned int i = 0; vert_to_send_it != vert_to_send_end;
1562 ++vert_to_send_it, ++i)
1563 {
1564 int destination = vert_to_send_it->first;
1565 const unsigned int n_vertices = vert_to_send_it->second.size();
1566 const int buffer_size = 2 * n_vertices;
1567 vertices_send_buffers[i].resize(buffer_size);
1568
1569 // fill the buffer
1570 for (unsigned int j = 0; j < n_vertices; ++j)
1571 {
1572 vertices_send_buffers[i][2 * j] =
1573 std::get<0>(vert_to_send_it->second[j]);
1574 vertices_send_buffers[i][2 * j + 1] =
1575 local_to_global_vertex_index[std::get<1>(
1576 vert_to_send_it->second[j])];
1577 }
1578
1579 // Send the message
1580 ierr = MPI_Isend(
1581 vertices_send_buffers[i].data(),
1582 buffer_size,
1583 Utilities::MPI::mpi_type_id_for_type<types::global_vertex_index>,
1584 destination,
1585 mpi_tag,
1586 triangulation.get_mpi_communicator(),
1587 &first_requests[i]);
1588 AssertThrowMPI(ierr);
1589 }
1590
1591 // Receive the first message
1592 std::vector<std::vector<types::global_vertex_index>> vertices_recv_buffers(
1593 vertices_to_recv.size());
1594 typename std::map<types::subdomain_id, std::set<unsigned int>>::iterator
1595 vert_to_recv_it = vertices_to_recv.begin(),
1596 vert_to_recv_end = vertices_to_recv.end();
1597 for (unsigned int i = 0; vert_to_recv_it != vert_to_recv_end;
1598 ++vert_to_recv_it, ++i)
1599 {
1600 int source = vert_to_recv_it->first;
1601 const unsigned int n_vertices = vert_to_recv_it->second.size();
1602 const int buffer_size = 2 * n_vertices;
1603 vertices_recv_buffers[i].resize(buffer_size);
1604
1605 // Receive the message
1606 ierr = MPI_Recv(
1607 vertices_recv_buffers[i].data(),
1608 buffer_size,
1609 Utilities::MPI::mpi_type_id_for_type<types::global_vertex_index>,
1610 source,
1611 mpi_tag,
1612 triangulation.get_mpi_communicator(),
1613 MPI_STATUS_IGNORE);
1614 AssertThrowMPI(ierr);
1615 }
1616
1617 // At this point, wait for all of the isend operations to finish:
1618 MPI_Waitall(first_requests.size(),
1619 first_requests.data(),
1620 MPI_STATUSES_IGNORE);
1621
1622
1623 // Send second message
1624 std::vector<std::vector<char>> cellids_send_buffers(
1625 vertices_to_send.size());
1626 std::vector<MPI_Request> second_requests(vertices_to_send.size());
1627 vert_to_send_it = vertices_to_send.begin();
1628 for (unsigned int i = 0; vert_to_send_it != vert_to_send_end;
1629 ++vert_to_send_it, ++i)
1630 {
1631 int destination = vert_to_send_it->first;
1632 const unsigned int n_vertices = vert_to_send_it->second.size();
1633 const int buffer_size = max_cellid_size * n_vertices;
1634 cellids_send_buffers[i].resize(buffer_size);
1635
1636 // fill the buffer
1637 unsigned int pos = 0;
1638 for (unsigned int j = 0; j < n_vertices; ++j)
1639 {
1640 std::string cell_id = std::get<2>(vert_to_send_it->second[j]);
1641 for (unsigned int k = 0; k < max_cellid_size; ++k, ++pos)
1642 {
1643 if (k < cell_id.size())
1644 cellids_send_buffers[i][pos] = cell_id[k];
1645 // if necessary fill up the reserved part of the buffer with an
1646 // invalid value
1647 else
1648 cellids_send_buffers[i][pos] = '-';
1649 }
1650 }
1651
1652 // Send the message
1653 ierr = MPI_Isend(cellids_send_buffers[i].data(),
1654 buffer_size,
1655 MPI_CHAR,
1656 destination,
1657 mpi_tag2,
1658 triangulation.get_mpi_communicator(),
1659 &second_requests[i]);
1660 AssertThrowMPI(ierr);
1661 }
1662
1663 // Receive the second message
1664 std::vector<std::vector<char>> cellids_recv_buffers(
1665 vertices_to_recv.size());
1666 vert_to_recv_it = vertices_to_recv.begin();
1667 for (unsigned int i = 0; vert_to_recv_it != vert_to_recv_end;
1668 ++vert_to_recv_it, ++i)
1669 {
1670 int source = vert_to_recv_it->first;
1671 const unsigned int n_vertices = vert_to_recv_it->second.size();
1672 const int buffer_size = max_cellid_size * n_vertices;
1673 cellids_recv_buffers[i].resize(buffer_size);
1674
1675 // Receive the message
1676 ierr = MPI_Recv(cellids_recv_buffers[i].data(),
1677 buffer_size,
1678 MPI_CHAR,
1679 source,
1680 mpi_tag2,
1681 triangulation.get_mpi_communicator(),
1682 MPI_STATUS_IGNORE);
1683 AssertThrowMPI(ierr);
1684 }
1685
1686
1687 // Match the data received with the required vertices
1688 vert_to_recv_it = vertices_to_recv.begin();
1689 for (unsigned int i = 0; vert_to_recv_it != vert_to_recv_end;
1690 ++i, ++vert_to_recv_it)
1691 {
1692 for (unsigned int j = 0; j < vert_to_recv_it->second.size(); ++j)
1693 {
1694 const unsigned int local_pos_recv = vertices_recv_buffers[i][2 * j];
1695 const types::global_vertex_index global_id_recv =
1696 vertices_recv_buffers[i][2 * j + 1];
1697 const std::string cellid_recv(
1698 &cellids_recv_buffers[i][max_cellid_size * j],
1699 &cellids_recv_buffers[i][max_cellid_size * j] + max_cellid_size);
1700 bool found = false;
1701 typename std::set<active_cell_iterator>::iterator
1702 cell_set_it = missing_vert_cells.begin(),
1703 end_cell_set = missing_vert_cells.end();
1704 for (; (found == false) && (cell_set_it != end_cell_set);
1705 ++cell_set_it)
1706 {
1707 typename std::set<active_cell_iterator>::iterator
1708 candidate_cell =
1709 vertex_to_cell[(*cell_set_it)->vertex_index(i)].begin(),
1710 end_cell =
1711 vertex_to_cell[(*cell_set_it)->vertex_index(i)].end();
1712 for (; candidate_cell != end_cell; ++candidate_cell)
1713 {
1714 std::string current_cellid =
1715 (*candidate_cell)->id().to_string();
1716 current_cellid.resize(max_cellid_size, '-');
1717 if (current_cellid.compare(cellid_recv) == 0)
1718 {
1719 local_to_global_vertex_index
1720 [(*candidate_cell)->vertex_index(local_pos_recv)] =
1721 global_id_recv;
1722 found = true;
1723
1724 break;
1725 }
1726 }
1727 }
1728 }
1729 }
1730
1731 // At this point, wait for all of the isend operations of the second round
1732 // to finish:
1733 MPI_Waitall(second_requests.size(),
1734 second_requests.data(),
1735 MPI_STATUSES_IGNORE);
1736#endif
1737
1738 return local_to_global_vertex_index;
1739 }
1740
1741
1742
1743 template <int dim, int spacedim>
1744 std::vector<types::global_vertex_index>
1746 const Triangulation<dim, spacedim> &serial_tria,
1747 const Triangulation<dim, spacedim> &parallel_tria)
1748 {
1749 AssertDimension(serial_tria.n_active_cells(),
1750 parallel_tria.n_global_active_cells());
1751
1752 const auto locally_owned_indices =
1754 std::vector<types::global_vertex_index> vertex_map(
1755 parallel_tria.n_vertices(), numbers::invalid_unsigned_int);
1756
1757 // Assumption: serial and parallel meshes have the same ordering of cells.
1758 auto parallel_cell = parallel_tria.begin_active();
1759 for (; parallel_cell != parallel_tria.end(); ++parallel_cell)
1760 if (parallel_cell->is_locally_owned())
1761 {
1762 const auto serial_cell =
1763 serial_tria.create_cell_iterator(parallel_cell->id());
1764 for (const unsigned int &v : serial_cell->vertex_indices())
1765 {
1766 const auto serial_index = serial_cell->vertex_index(v);
1767 const auto parallel_index = parallel_cell->vertex_index(v);
1768 if (locally_owned_indices[parallel_index])
1769 vertex_map[parallel_index] = serial_index;
1770 }
1771 }
1772 return vertex_map;
1773 }
1774
1775
1776
1777 template <int dim, int spacedim>
1778 void
1779 partition_triangulation(const unsigned int n_partitions,
1780 Triangulation<dim, spacedim> &triangulation,
1781 const SparsityTools::Partitioner partitioner)
1782 {
1784 &triangulation) == nullptr),
1785 ExcMessage("Objects of type parallel::distributed::Triangulation "
1786 "are already partitioned implicitly and can not be "
1787 "partitioned again explicitly."));
1788
1789 std::vector<unsigned int> cell_weights;
1790
1791 // Get cell weighting if a signal has been attached to the triangulation
1792 if (!triangulation.signals.weight.empty())
1793 {
1794 cell_weights.resize(triangulation.n_active_cells(), 0U);
1795
1796 // In a first step, obtain the weights of the locally owned
1797 // cells. For all others, the weight remains at the zero the
1798 // vector was initialized with above.
1799 for (const auto &cell : triangulation.active_cell_iterators())
1800 if (cell->is_locally_owned())
1801 cell_weights[cell->active_cell_index()] =
1802 triangulation.signals.weight(cell, CellStatus::cell_will_persist);
1803
1804 // If this is a parallel triangulation, we then need to also
1805 // get the weights for all other cells. We have asserted above
1806 // that this function can't be used for
1807 // parallel::distributed::Triangulation objects, so the only
1808 // ones we have to worry about here are
1809 // parallel::shared::Triangulation
1810 if (const auto shared_tria =
1812 &triangulation))
1813 Utilities::MPI::sum(cell_weights,
1814 shared_tria->get_mpi_communicator(),
1815 cell_weights);
1816
1817 // verify that the global sum of weights is larger than 0
1818 Assert(std::accumulate(cell_weights.begin(),
1819 cell_weights.end(),
1820 std::uint64_t(0)) > 0,
1821 ExcMessage("The global sum of weights over all active cells "
1822 "is zero. Please verify how you generate weights."));
1823 }
1824
1825 // Call the other more general function
1826 partition_triangulation(n_partitions,
1827 cell_weights,
1828 triangulation,
1829 partitioner);
1830 }
1831
1832
1833
1834 template <int dim, int spacedim>
1835 void
1836 partition_triangulation(const unsigned int n_partitions,
1837 const std::vector<unsigned int> &cell_weights,
1838 Triangulation<dim, spacedim> &triangulation,
1839 const SparsityTools::Partitioner partitioner)
1840 {
1842 &triangulation) == nullptr),
1843 ExcMessage("Objects of type parallel::distributed::Triangulation "
1844 "are already partitioned implicitly and can not be "
1845 "partitioned again explicitly."));
1846 Assert(n_partitions > 0, ExcInvalidNumberOfPartitions(n_partitions));
1847
1848 // check for an easy return
1849 if (n_partitions == 1)
1850 {
1851 for (const auto &cell : triangulation.active_cell_iterators())
1852 cell->set_subdomain_id(0);
1853 return;
1854 }
1855
1856 // we decompose the domain by first
1857 // generating the connection graph of all
1858 // cells with their neighbors, and then
1859 // passing this graph off to METIS.
1860 // finally defer to the other function for
1861 // partitioning and assigning subdomain ids
1862 DynamicSparsityPattern cell_connectivity;
1863 get_face_connectivity_of_cells(triangulation, cell_connectivity);
1864
1865 SparsityPattern sp_cell_connectivity;
1866 sp_cell_connectivity.copy_from(cell_connectivity);
1867 partition_triangulation(n_partitions,
1868 cell_weights,
1869 sp_cell_connectivity,
1870 triangulation,
1871 partitioner);
1872 }
1873
1874
1875
1876 template <int dim, int spacedim>
1877 void
1878 partition_triangulation(const unsigned int n_partitions,
1879 const SparsityPattern &cell_connection_graph,
1880 Triangulation<dim, spacedim> &triangulation,
1881 const SparsityTools::Partitioner partitioner)
1882 {
1884 &triangulation) == nullptr),
1885 ExcMessage("Objects of type parallel::distributed::Triangulation "
1886 "are already partitioned implicitly and can not be "
1887 "partitioned again explicitly."));
1888
1889 std::vector<unsigned int> cell_weights;
1890
1891 // Get cell weighting if a signal has been attached to the triangulation
1892 if (!triangulation.signals.weight.empty())
1893 {
1894 cell_weights.resize(triangulation.n_active_cells(), 0U);
1895
1896 // In a first step, obtain the weights of the locally owned
1897 // cells. For all others, the weight remains at the zero the
1898 // vector was initialized with above.
1899 for (const auto &cell : triangulation.active_cell_iterators() |
1901 cell_weights[cell->active_cell_index()] =
1902 triangulation.signals.weight(cell, CellStatus::cell_will_persist);
1903
1904 // If this is a parallel triangulation, we then need to also
1905 // get the weights for all other cells. We have asserted above
1906 // that this function can't be used for
1907 // parallel::distribute::Triangulation objects, so the only
1908 // ones we have to worry about here are
1909 // parallel::shared::Triangulation
1910 if (const auto shared_tria =
1912 &triangulation))
1913 Utilities::MPI::sum(cell_weights,
1914 shared_tria->get_mpi_communicator(),
1915 cell_weights);
1916
1917 // verify that the global sum of weights is larger than 0
1918 Assert(std::accumulate(cell_weights.begin(),
1919 cell_weights.end(),
1920 std::uint64_t(0)) > 0,
1921 ExcMessage("The global sum of weights over all active cells "
1922 "is zero. Please verify how you generate weights."));
1923 }
1924
1925 // Call the other more general function
1926 partition_triangulation(n_partitions,
1927 cell_weights,
1928 cell_connection_graph,
1929 triangulation,
1930 partitioner);
1931 }
1932
1933
1934
1935 template <int dim, int spacedim>
1936 void
1937 partition_triangulation(const unsigned int n_partitions,
1938 const std::vector<unsigned int> &cell_weights,
1939 const SparsityPattern &cell_connection_graph,
1940 Triangulation<dim, spacedim> &triangulation,
1941 const SparsityTools::Partitioner partitioner)
1942 {
1944 &triangulation) == nullptr),
1945 ExcMessage("Objects of type parallel::distributed::Triangulation "
1946 "are already partitioned implicitly and can not be "
1947 "partitioned again explicitly."));
1948 Assert(n_partitions > 0, ExcInvalidNumberOfPartitions(n_partitions));
1949 Assert(cell_connection_graph.n_rows() == triangulation.n_active_cells(),
1950 ExcMessage("Connectivity graph has wrong size"));
1951 Assert(cell_connection_graph.n_cols() == triangulation.n_active_cells(),
1952 ExcMessage("Connectivity graph has wrong size"));
1953
1954 // signal that partitioning is going to happen
1955 triangulation.signals.pre_partition();
1956
1957 // check for an easy return
1958 if (n_partitions == 1)
1959 {
1960 for (const auto &cell : triangulation.active_cell_iterators())
1961 cell->set_subdomain_id(0);
1962 return;
1963 }
1964
1965 // partition this connection graph and get
1966 // back a vector of indices, one per degree
1967 // of freedom (which is associated with a
1968 // cell)
1969 std::vector<unsigned int> partition_indices(triangulation.n_active_cells());
1970 SparsityTools::partition(cell_connection_graph,
1971 cell_weights,
1972 n_partitions,
1973 partition_indices,
1974 partitioner);
1975
1976 // finally loop over all cells and set the subdomain ids
1977 for (const auto &cell : triangulation.active_cell_iterators())
1978 cell->set_subdomain_id(partition_indices[cell->active_cell_index()]);
1979 }
1980
1981
1982 namespace internal
1983 {
1987 template <class IT>
1988 void
1990 unsigned int &current_proc_idx,
1991 unsigned int &current_cell_idx,
1992 const unsigned int n_active_cells,
1993 const unsigned int n_partitions)
1994 {
1995 if (cell->is_active())
1996 {
1997 while (current_cell_idx >=
1998 std::floor(static_cast<std::uint_least64_t>(n_active_cells) *
1999 (current_proc_idx + 1) / n_partitions))
2000 ++current_proc_idx;
2001 cell->set_subdomain_id(current_proc_idx);
2002 ++current_cell_idx;
2003 }
2004 else
2005 {
2006 for (unsigned int n = 0; n < cell->n_children(); ++n)
2008 current_proc_idx,
2009 current_cell_idx,
2010 n_active_cells,
2011 n_partitions);
2012 }
2013 }
2014 } // namespace internal
2015
2016 template <int dim, int spacedim>
2017 void
2018 partition_triangulation_zorder(const unsigned int n_partitions,
2019 Triangulation<dim, spacedim> &triangulation,
2020 const bool group_siblings)
2021 {
2023 &triangulation) == nullptr),
2024 ExcMessage("Objects of type parallel::distributed::Triangulation "
2025 "are already partitioned implicitly and can not be "
2026 "partitioned again explicitly."));
2027 Assert(n_partitions > 0, ExcInvalidNumberOfPartitions(n_partitions));
2028 Assert(triangulation.signals.weight.empty(), ExcNotImplemented());
2029
2030 // signal that partitioning is going to happen
2031 triangulation.signals.pre_partition();
2032
2033 // check for an easy return
2034 if (n_partitions == 1)
2035 {
2036 for (const auto &cell : triangulation.active_cell_iterators())
2037 cell->set_subdomain_id(0);
2038 return;
2039 }
2040
2041 // Duplicate the coarse cell reordoring
2042 // as done in p4est
2043 std::vector<types::global_dof_index> coarse_cell_to_p4est_tree_permutation;
2044 std::vector<types::global_dof_index> p4est_tree_to_coarse_cell_permutation;
2045
2046 DynamicSparsityPattern cell_connectivity;
2048 0,
2049 cell_connectivity);
2050 coarse_cell_to_p4est_tree_permutation.resize(triangulation.n_cells(0));
2051 SparsityTools::reorder_hierarchical(cell_connectivity,
2052 coarse_cell_to_p4est_tree_permutation);
2053
2054 p4est_tree_to_coarse_cell_permutation =
2055 Utilities::invert_permutation(coarse_cell_to_p4est_tree_permutation);
2056
2057 unsigned int current_proc_idx = 0;
2058 unsigned int current_cell_idx = 0;
2059 const unsigned int n_active_cells = triangulation.n_active_cells();
2060
2061 // set subdomain id for active cell descendants
2062 // of each coarse cell in permuted order
2063 for (unsigned int idx = 0; idx < triangulation.n_cells(0); ++idx)
2064 {
2065 const unsigned int coarse_cell_idx =
2066 p4est_tree_to_coarse_cell_permutation[idx];
2068 &triangulation, 0, coarse_cell_idx);
2069
2071 current_proc_idx,
2072 current_cell_idx,
2073 n_active_cells,
2074 n_partitions);
2075 }
2076
2077 // if all children of a cell are active (e.g. we
2078 // have a cell that is refined once and no part
2079 // is refined further), p4est places all of them
2080 // on the same processor. The new owner will be
2081 // the processor with the largest number of children
2082 // (ties are broken by picking the lower rank).
2083 // Duplicate this logic here.
2084 if (group_siblings)
2085 {
2087 cell = triangulation.begin(),
2088 endc = triangulation.end();
2089 for (; cell != endc; ++cell)
2090 {
2091 if (cell->is_active())
2092 continue;
2093 bool all_children_active = true;
2094 std::map<unsigned int, unsigned int> map_cpu_n_cells;
2095 for (unsigned int n = 0; n < cell->n_children(); ++n)
2096 if (!cell->child(n)->is_active())
2097 {
2098 all_children_active = false;
2099 break;
2100 }
2101 else
2102 ++map_cpu_n_cells[cell->child(n)->subdomain_id()];
2103
2104 if (!all_children_active)
2105 continue;
2106
2107 unsigned int new_owner = cell->child(0)->subdomain_id();
2108 for (std::map<unsigned int, unsigned int>::iterator it =
2109 map_cpu_n_cells.begin();
2110 it != map_cpu_n_cells.end();
2111 ++it)
2112 if (it->second > map_cpu_n_cells[new_owner])
2113 new_owner = it->first;
2114
2115 for (unsigned int n = 0; n < cell->n_children(); ++n)
2116 cell->child(n)->set_subdomain_id(new_owner);
2117 }
2118 }
2119 }
2120
2121
2122 template <int dim, int spacedim>
2123 void
2125 {
2126 unsigned int n_levels = triangulation.n_levels();
2127 for (int lvl = n_levels - 1; lvl >= 0; --lvl)
2128 {
2129 for (const auto &cell : triangulation.cell_iterators_on_level(lvl))
2130 {
2131 if (cell->is_active())
2132 cell->set_level_subdomain_id(cell->subdomain_id());
2133 else
2134 {
2135 Assert(cell->child(0)->level_subdomain_id() !=
2138 cell->set_level_subdomain_id(
2139 cell->child(0)->level_subdomain_id());
2140 }
2141 }
2142 }
2143 }
2144
2145 namespace internal
2146 {
2147 namespace
2148 {
2149 // Split get_subdomain_association() for p::d::T since we want to compile
2150 // it in 1d but none of the p4est stuff is available in 1d.
2151 template <int dim, int spacedim>
2152 void
2155 &triangulation,
2156 const std::vector<CellId> &cell_ids,
2157 [[maybe_unused]] std::vector<types::subdomain_id> &subdomain_ids)
2158 {
2159#ifndef DEAL_II_WITH_P4EST
2160 (void)triangulation;
2161 (void)cell_ids;
2162 Assert(
2163 false,
2164 ExcMessage(
2165 "You are attempting to use a functionality that is only available "
2166 "if deal.II was configured to use p4est, but cmake did not find a "
2167 "valid p4est library."));
2168#else
2169 // for parallel distributed triangulations, we will ask the p4est oracle
2170 // about the global partitioning of active cells since this information
2171 // is stored on every process
2172 for (const auto &cell_id : cell_ids)
2173 {
2174 // find descendent from coarse quadrant
2175 typename ::internal::p4est::types<dim>::quadrant p4est_cell,
2177
2178 ::internal::p4est::init_coarse_quadrant<dim>(p4est_cell);
2179 for (const auto &child_index : cell_id.get_child_indices())
2180 {
2181 ::internal::p4est::init_quadrant_children<dim>(
2182 p4est_cell, p4est_children);
2183 p4est_cell =
2184 p4est_children[static_cast<unsigned int>(child_index)];
2185 }
2186
2187 // find owning process, i.e., the subdomain id
2188 const int owner =
2190 const_cast<typename ::internal::p4est::types<dim>::forest
2191 *>(triangulation.get_p4est()),
2192 cell_id.get_coarse_cell_id(),
2193 &p4est_cell,
2195 triangulation.get_mpi_communicator()));
2196
2197 Assert(owner >= 0, ExcMessage("p4est should know the owner."));
2198
2199 subdomain_ids.push_back(owner);
2200 }
2201#endif
2202 }
2203
2204
2205
2206 template <int spacedim>
2207 void
2210 const std::vector<CellId> &,
2211 std::vector<types::subdomain_id> &)
2212 {
2214 }
2215 } // anonymous namespace
2216 } // namespace internal
2217
2218
2219
2220 template <int dim, int spacedim>
2221 std::vector<types::subdomain_id>
2223 const std::vector<CellId> &cell_ids)
2224 {
2225 std::vector<types::subdomain_id> subdomain_ids;
2226 subdomain_ids.reserve(cell_ids.size());
2227
2228 if (dynamic_cast<
2230 &triangulation) != nullptr)
2231 {
2233 }
2235 *parallel_tria = dynamic_cast<
2237 &triangulation))
2238 {
2239 internal::get_subdomain_association(*parallel_tria,
2240 cell_ids,
2241 subdomain_ids);
2242 }
2243 else if (const parallel::shared::Triangulation<dim, spacedim> *shared_tria =
2245 *>(&triangulation))
2246 {
2247 // for parallel shared triangulations, we need to access true subdomain
2248 // ids which are also valid for artificial cells
2249 const std::vector<types::subdomain_id> &true_subdomain_ids_of_cells =
2250 shared_tria->get_true_subdomain_ids_of_cells();
2251
2252 for (const auto &cell_id : cell_ids)
2253 {
2254 const unsigned int active_cell_index =
2255 shared_tria->create_cell_iterator(cell_id)->active_cell_index();
2256 subdomain_ids.push_back(
2257 true_subdomain_ids_of_cells[active_cell_index]);
2258 }
2259 }
2260 else
2261 {
2262 // the most general type of triangulation is the serial one. here, all
2263 // subdomain information is directly available
2264 for (const auto &cell_id : cell_ids)
2265 {
2266 subdomain_ids.push_back(
2267 triangulation.create_cell_iterator(cell_id)->subdomain_id());
2268 }
2269 }
2270
2271 return subdomain_ids;
2272 }
2273
2274
2275
2276 template <int dim, int spacedim>
2277 void
2279 std::vector<types::subdomain_id> &subdomain)
2280 {
2281 Assert(subdomain.size() == triangulation.n_active_cells(),
2282 ExcDimensionMismatch(subdomain.size(),
2283 triangulation.n_active_cells()));
2284 for (const auto &cell : triangulation.active_cell_iterators())
2285 subdomain[cell->active_cell_index()] = cell->subdomain_id();
2286 }
2287
2288
2289
2290 template <int dim, int spacedim>
2291 unsigned int
2293 const Triangulation<dim, spacedim> &triangulation,
2294 const types::subdomain_id subdomain)
2295 {
2296 unsigned int count = 0;
2297 for (const auto &cell : triangulation.active_cell_iterators())
2298 if (cell->subdomain_id() == subdomain)
2299 ++count;
2300
2301 return count;
2302 }
2303
2304
2305
2306 template <int dim, int spacedim>
2307 std::vector<bool>
2309 {
2310 // start with all vertices
2311 std::vector<bool> locally_owned_vertices =
2312 triangulation.get_used_vertices();
2313
2314 // if the triangulation is distributed, eliminate those that
2315 // are owned by other processors -- either because the vertex is
2316 // on an artificial cell, or because it is on a ghost cell with
2317 // a smaller subdomain
2318 if (const auto *tr = dynamic_cast<
2320 &triangulation))
2321 for (const auto &cell : triangulation.active_cell_iterators())
2322 if (cell->is_artificial() ||
2323 (cell->is_ghost() &&
2324 (cell->subdomain_id() < tr->locally_owned_subdomain())))
2325 for (const unsigned int v : cell->vertex_indices())
2326 locally_owned_vertices[cell->vertex_index(v)] = false;
2327
2328 return locally_owned_vertices;
2329 }
2330
2331
2332
2333 namespace internal
2334 {
2335 namespace FixUpDistortedChildCells
2336 {
2337 // compute the mean square
2338 // deviation of the alternating
2339 // forms of the children of the
2340 // given object from that of
2341 // the object itself. for
2342 // objects with
2343 // structdim==spacedim, the
2344 // alternating form is the
2345 // determinant of the jacobian,
2346 // whereas for faces with
2347 // structdim==spacedim-1, the
2348 // alternating form is the
2349 // (signed and scaled) normal
2350 // vector
2351 //
2352 // this average square
2353 // deviation is computed for an
2354 // object where the center node
2355 // has been replaced by the
2356 // second argument to this
2357 // function
2358 template <typename Iterator, int spacedim>
2359 double
2360 objective_function(const Iterator &object,
2361 const Point<spacedim> &object_mid_point)
2362 {
2363 const unsigned int structdim =
2364 Iterator::AccessorType::structure_dimension;
2365 Assert(spacedim == Iterator::AccessorType::dimension,
2367
2368 // everything below is wrong
2369 // if not for the following
2370 // condition
2371 Assert(object->refinement_case() ==
2374 // first calculate the
2375 // average alternating form
2376 // for the parent cell/face
2379 Tensor<spacedim - structdim, spacedim>
2380 parent_alternating_forms[GeometryInfo<structdim>::vertices_per_cell];
2381
2382 for (const unsigned int i : object->vertex_indices())
2383 parent_vertices[i] = object->vertex(i);
2384
2386 parent_vertices, parent_alternating_forms);
2387
2388 const Tensor<spacedim - structdim, spacedim>
2389 average_parent_alternating_form =
2390 std::accumulate(parent_alternating_forms,
2391 parent_alternating_forms +
2394
2395 // now do the same
2396 // computation for the
2397 // children where we use the
2398 // given location for the
2399 // object mid point instead of
2400 // the one the triangulation
2401 // currently reports
2405 Tensor<spacedim - structdim, spacedim> child_alternating_forms
2408
2409 for (unsigned int c = 0; c < object->n_children(); ++c)
2410 for (const unsigned int i : object->child(c)->vertex_indices())
2411 child_vertices[c][i] = object->child(c)->vertex(i);
2412
2413 // replace mid-object
2414 // vertex. note that for
2415 // child i, the mid-object
2416 // vertex happens to have the
2417 // number
2418 // max_children_per_cell-i
2419 for (unsigned int c = 0; c < object->n_children(); ++c)
2421 1] = object_mid_point;
2422
2423 for (unsigned int c = 0; c < object->n_children(); ++c)
2425 child_vertices[c], child_alternating_forms[c]);
2426
2427 // on a uniformly refined
2428 // hypercube object, the child
2429 // alternating forms should
2430 // all be smaller by a factor
2431 // of 2^structdim than the
2432 // ones of the parent. as a
2433 // consequence, we'll use the
2434 // squared deviation from
2435 // this ideal value as an
2436 // objective function
2437 double objective = 0;
2438 for (unsigned int c = 0; c < object->n_children(); ++c)
2439 for (const unsigned int i : object->child(c)->vertex_indices())
2440 objective += (child_alternating_forms[c][i] -
2441 average_parent_alternating_form /
2442 Utilities::fixed_power<structdim>(2))
2443 .norm_square();
2444
2445 return objective;
2446 }
2447
2448
2454 template <typename Iterator>
2456 get_face_midpoint(const Iterator &object,
2457 const unsigned int f,
2458 std::integral_constant<int, 1>)
2459 {
2460 return object->vertex(f);
2461 }
2462
2463
2464
2470 template <typename Iterator>
2472 get_face_midpoint(const Iterator &object,
2473 const unsigned int f,
2474 std::integral_constant<int, 2>)
2475 {
2476 return object->line(f)->center();
2477 }
2478
2479
2480
2486 template <typename Iterator>
2488 get_face_midpoint(const Iterator &object,
2489 const unsigned int f,
2490 std::integral_constant<int, 3>)
2491 {
2492 return object->face(f)->center();
2493 }
2494
2495
2496
2519 template <typename Iterator>
2520 double
2521 minimal_diameter(const Iterator &object)
2522 {
2523 const unsigned int structdim =
2524 Iterator::AccessorType::structure_dimension;
2525
2526 double diameter = object->diameter();
2527 for (const unsigned int f : object->face_indices())
2528 for (unsigned int e = f + 1; e < object->n_faces(); ++e)
2530 diameter,
2531 get_face_midpoint(object,
2532 f,
2533 std::integral_constant<int, structdim>())
2534 .distance(get_face_midpoint(
2535 object, e, std::integral_constant<int, structdim>())));
2536
2537 return diameter;
2538 }
2539
2540
2541
2546 template <typename Iterator>
2547 bool
2548 fix_up_object(const Iterator &object)
2549 {
2550 const unsigned int structdim =
2551 Iterator::AccessorType::structure_dimension;
2552 const unsigned int spacedim = Iterator::AccessorType::space_dimension;
2553
2554 // right now we can only deal with cells that have been refined
2555 // isotropically because that is the only case where we have a cell
2556 // mid-point that can be moved around without having to consider
2557 // boundary information
2558 Assert(object->has_children(), ExcInternalError());
2559 Assert(object->refinement_case() ==
2562
2563 // get the current location of the object mid-vertex:
2564 Point<spacedim> object_mid_point = object->child(0)->vertex(
2566
2567 // now do a few steepest descent steps to reduce the objective
2568 // function. compute the diameter in the helper function above
2569 unsigned int iteration = 0;
2570 const double diameter = minimal_diameter(object);
2571
2572 // current value of objective function and initial delta
2573 double current_value = objective_function(object, object_mid_point);
2574 double initial_delta = 0;
2575
2576 do
2577 {
2578 // choose a step length that is initially 1/4 of the child
2579 // objects' diameter, and a sequence whose sum does not converge
2580 // (to avoid premature termination of the iteration)
2581 const double step_length = diameter / 4 / (iteration + 1);
2582
2583 // compute the objective function's derivative using a two-sided
2584 // difference formula with eps=step_length/10
2585 Tensor<1, spacedim> gradient;
2586 for (unsigned int d = 0; d < spacedim; ++d)
2587 {
2588 const double eps = step_length / 10;
2589
2591 h[d] = eps / 2;
2592
2593 gradient[d] =
2595 object, project_to_object(object, object_mid_point + h)) -
2597 object, project_to_object(object, object_mid_point - h))) /
2598 eps;
2599 }
2600
2601 // there is nowhere to go
2602 if (gradient.norm() == 0)
2603 break;
2604
2605 // We need to go in direction -gradient. the optimal value of the
2606 // objective function is zero, so assuming that the model is
2607 // quadratic we would have to go -2*val/||gradient|| in this
2608 // direction, make sure we go at most step_length into this
2609 // direction
2610 object_mid_point -=
2611 std::min(2 * current_value / (gradient * gradient),
2612 step_length / gradient.norm()) *
2613 gradient;
2614 object_mid_point = project_to_object(object, object_mid_point);
2615
2616 // compute current value of the objective function
2617 const double previous_value = current_value;
2618 current_value = objective_function(object, object_mid_point);
2619
2620 if (iteration == 0)
2621 initial_delta = (previous_value - current_value);
2622
2623 // stop if we aren't moving much any more
2624 if ((iteration >= 1) &&
2625 ((previous_value - current_value < 0) ||
2626 (std::fabs(previous_value - current_value) <
2627 0.001 * initial_delta)))
2628 break;
2629
2630 ++iteration;
2631 }
2632 while (iteration < 20);
2633
2634 // verify that the new
2635 // location is indeed better
2636 // than the one before. check
2637 // this by comparing whether
2638 // the minimum value of the
2639 // products of parent and
2640 // child alternating forms is
2641 // positive. for cells this
2642 // means that the
2643 // determinants have the same
2644 // sign, for faces that the
2645 // face normals of parent and
2646 // children point in the same
2647 // general direction
2648 double old_min_product, new_min_product;
2649
2652 for (const unsigned int i : GeometryInfo<structdim>::vertex_indices())
2653 parent_vertices[i] = object->vertex(i);
2654
2655 Tensor<spacedim - structdim, spacedim>
2656 parent_alternating_forms[GeometryInfo<structdim>::vertices_per_cell];
2658 parent_vertices, parent_alternating_forms);
2659
2663
2664 for (unsigned int c = 0; c < object->n_children(); ++c)
2665 for (const unsigned int i : object->child(c)->vertex_indices())
2666 child_vertices[c][i] = object->child(c)->vertex(i);
2667
2668 Tensor<spacedim - structdim, spacedim> child_alternating_forms
2671
2672 for (unsigned int c = 0; c < object->n_children(); ++c)
2674 child_vertices[c], child_alternating_forms[c]);
2675
2676 old_min_product =
2677 child_alternating_forms[0][0] * parent_alternating_forms[0];
2678 for (unsigned int c = 0; c < object->n_children(); ++c)
2679 for (const unsigned int i : object->child(c)->vertex_indices())
2680 for (const unsigned int j : object->vertex_indices())
2681 old_min_product = std::min<double>(old_min_product,
2682 child_alternating_forms[c][i] *
2683 parent_alternating_forms[j]);
2684
2685 // for the new minimum value,
2686 // replace mid-object
2687 // vertex. note that for child
2688 // i, the mid-object vertex
2689 // happens to have the number
2690 // max_children_per_cell-i
2691 for (unsigned int c = 0; c < object->n_children(); ++c)
2693 1] = object_mid_point;
2694
2695 for (unsigned int c = 0; c < object->n_children(); ++c)
2697 child_vertices[c], child_alternating_forms[c]);
2698
2699 new_min_product =
2700 child_alternating_forms[0][0] * parent_alternating_forms[0];
2701 for (unsigned int c = 0; c < object->n_children(); ++c)
2702 for (const unsigned int i : object->child(c)->vertex_indices())
2703 for (const unsigned int j : object->vertex_indices())
2704 new_min_product = std::min<double>(new_min_product,
2705 child_alternating_forms[c][i] *
2706 parent_alternating_forms[j]);
2707
2708 // if new minimum value is
2709 // better than before, then set the
2710 // new mid point. otherwise
2711 // return this object as one of
2712 // those that can't apparently
2713 // be fixed
2714 if (new_min_product >= old_min_product)
2715 object->child(0)->vertex(
2717 object_mid_point;
2718
2719 // return whether after this
2720 // operation we have an object that
2721 // is well oriented
2722 return (std::max(new_min_product, old_min_product) > 0);
2723 }
2724
2725
2726
2727 // possibly fix up the faces of a cell by moving around its mid-points
2728 template <int dim, int spacedim>
2729 void
2731 const typename ::Triangulation<dim, spacedim>::cell_iterator
2732 &cell,
2733 std::integral_constant<int, dim>,
2734 std::integral_constant<int, spacedim>)
2735 {
2736 // see if we first can fix up some of the faces of this object. We can
2737 // mess with faces if and only if the neighboring cell is not even
2738 // more refined than we are (since in that case the sub-faces have
2739 // themselves children that we can't move around any more). however,
2740 // the latter case shouldn't happen anyway: if the current face is
2741 // distorted but the neighbor is even more refined, then the face had
2742 // been deformed before already, and had been ignored at the time; we
2743 // should then also be able to ignore it this time as well
2744 for (auto f : cell->face_indices())
2745 {
2746 Assert(cell->face(f)->has_children(), ExcInternalError());
2747 Assert(cell->face(f)->refinement_case() ==
2750
2751 bool subface_is_more_refined = false;
2752 for (unsigned int g = 0;
2753 g < GeometryInfo<dim>::max_children_per_face;
2754 ++g)
2755 if (cell->face(f)->child(g)->has_children())
2756 {
2757 subface_is_more_refined = true;
2758 break;
2759 }
2760
2761 if (subface_is_more_refined == true)
2762 continue;
2763
2764 // we finally know that we can do something about this face
2765 fix_up_object(cell->face(f));
2766 }
2767 }
2768 } /* namespace FixUpDistortedChildCells */
2769 } /* namespace internal */
2770
2771
2772 template <int dim, int spacedim>
2773 void
2775 const bool reset_boundary_ids)
2776 {
2777 const auto src_boundary_ids = tria.get_boundary_ids();
2778 std::vector<types::manifold_id> dst_manifold_ids(src_boundary_ids.size());
2779 auto m_it = dst_manifold_ids.begin();
2780 for (const auto b : src_boundary_ids)
2781 {
2782 *m_it = static_cast<types::manifold_id>(b);
2783 ++m_it;
2784 }
2785 const std::vector<types::boundary_id> reset_boundary_id =
2786 reset_boundary_ids ?
2787 std::vector<types::boundary_id>(src_boundary_ids.size(), 0) :
2788 src_boundary_ids;
2789 map_boundary_to_manifold_ids(src_boundary_ids,
2790 dst_manifold_ids,
2791 tria,
2792 reset_boundary_id);
2793 }
2794
2795
2796
2797 template <int dim, int spacedim>
2798 void
2800 const std::vector<types::boundary_id> &src_boundary_ids,
2801 const std::vector<types::manifold_id> &dst_manifold_ids,
2803 const std::vector<types::boundary_id> &reset_boundary_ids_)
2804 {
2805 AssertDimension(src_boundary_ids.size(), dst_manifold_ids.size());
2806 const auto reset_boundary_ids =
2807 reset_boundary_ids_.size() ? reset_boundary_ids_ : src_boundary_ids;
2808 AssertDimension(reset_boundary_ids.size(), src_boundary_ids.size());
2809
2810 // in 3d, we not only have to copy boundary ids of faces, but also of edges
2811 // because we see them twice (once from each adjacent boundary face),
2812 // we cannot immediately reset their boundary ids. thus, copy first
2813 // and reset later
2814 if (dim >= 3)
2815 for (const auto &cell : tria.active_cell_iterators())
2816 for (auto f : cell->face_indices())
2817 if (cell->face(f)->at_boundary())
2818 for (unsigned int e = 0; e < cell->face(f)->n_lines(); ++e)
2819 {
2820 const auto bid = cell->face(f)->line(e)->boundary_id();
2821 const unsigned int ind = std::find(src_boundary_ids.begin(),
2822 src_boundary_ids.end(),
2823 bid) -
2824 src_boundary_ids.begin();
2825 if (ind < src_boundary_ids.size())
2826 cell->face(f)->line(e)->set_manifold_id(
2827 dst_manifold_ids[ind]);
2828 }
2829
2830 // now do cells
2831 for (const auto &cell : tria.active_cell_iterators())
2832 for (auto f : cell->face_indices())
2833 if (cell->face(f)->at_boundary())
2834 {
2835 const auto bid = cell->face(f)->boundary_id();
2836 const unsigned int ind =
2837 std::find(src_boundary_ids.begin(), src_boundary_ids.end(), bid) -
2838 src_boundary_ids.begin();
2839
2840 if (ind < src_boundary_ids.size())
2841 {
2842 // assign the manifold id
2843 cell->face(f)->set_manifold_id(dst_manifold_ids[ind]);
2844 // then reset boundary id
2845 cell->face(f)->set_boundary_id(reset_boundary_ids[ind]);
2846 }
2847
2848 if (dim >= 3)
2849 for (unsigned int e = 0; e < cell->face(f)->n_lines(); ++e)
2850 {
2851 const auto bid = cell->face(f)->line(e)->boundary_id();
2852 const unsigned int ind = std::find(src_boundary_ids.begin(),
2853 src_boundary_ids.end(),
2854 bid) -
2855 src_boundary_ids.begin();
2856 if (ind < src_boundary_ids.size())
2857 cell->face(f)->line(e)->set_boundary_id(
2858 reset_boundary_ids[ind]);
2859 }
2860 }
2861 }
2862
2863
2864 template <int dim, int spacedim>
2865 void
2867 const bool compute_face_ids)
2868 {
2870 cell = tria.begin_active(),
2871 endc = tria.end();
2872
2873 for (; cell != endc; ++cell)
2874 {
2875 cell->set_manifold_id(cell->material_id());
2876 if (compute_face_ids == true)
2877 {
2878 for (auto f : cell->face_indices())
2879 {
2880 if (cell->at_boundary(f) == false)
2881 cell->face(f)->set_manifold_id(
2882 std::min(cell->material_id(),
2883 cell->neighbor(f)->material_id()));
2884 else
2885 cell->face(f)->set_manifold_id(cell->material_id());
2886 }
2887 }
2888 }
2889 }
2890
2891
2892 template <int dim, int spacedim>
2893 void
2896 const std::function<types::manifold_id(
2897 const std::set<types::manifold_id> &)> &disambiguation_function,
2898 bool overwrite_only_flat_manifold_ids)
2899 {
2900 // Easy case first:
2901 if (dim == 1)
2902 return;
2903 const unsigned int n_subobjects =
2904 dim == 2 ? tria.n_lines() : tria.n_lines() + tria.n_quads();
2905
2906 // If user index is zero, then it has not been set.
2907 std::vector<std::set<types::manifold_id>> manifold_ids(n_subobjects + 1);
2908 std::vector<unsigned int> backup;
2909 tria.save_user_indices(backup);
2910 tria.clear_user_data();
2911
2912 unsigned next_index = 1;
2913 for (auto &cell : tria.active_cell_iterators())
2914 {
2915 if (dim > 1)
2916 for (unsigned int l = 0; l < cell->n_lines(); ++l)
2917 {
2918 if (cell->line(l)->user_index() == 0)
2919 {
2920 AssertIndexRange(next_index, n_subobjects + 1);
2921 manifold_ids[next_index].insert(cell->manifold_id());
2922 cell->line(l)->set_user_index(next_index++);
2923 }
2924 else
2925 manifold_ids[cell->line(l)->user_index()].insert(
2926 cell->manifold_id());
2927 }
2928 if (dim > 2)
2929 for (unsigned int l = 0; l < cell->n_faces(); ++l)
2930 {
2931 if (cell->quad(l)->user_index() == 0)
2932 {
2933 AssertIndexRange(next_index, n_subobjects + 1);
2934 manifold_ids[next_index].insert(cell->manifold_id());
2935 cell->quad(l)->set_user_index(next_index++);
2936 }
2937 else
2938 manifold_ids[cell->quad(l)->user_index()].insert(
2939 cell->manifold_id());
2940 }
2941 }
2942 for (auto &cell : tria.active_cell_iterators())
2943 {
2944 if (dim > 1)
2945 for (unsigned int l = 0; l < cell->n_lines(); ++l)
2946 {
2947 const auto id = cell->line(l)->user_index();
2948 // Make sure we change the manifold indicator only once
2949 if (id != 0)
2950 {
2951 if (cell->line(l)->manifold_id() ==
2953 overwrite_only_flat_manifold_ids == false)
2954 cell->line(l)->set_manifold_id(
2955 disambiguation_function(manifold_ids[id]));
2956 cell->line(l)->set_user_index(0);
2957 }
2958 }
2959 if (dim > 2)
2960 for (unsigned int l = 0; l < cell->n_faces(); ++l)
2961 {
2962 const auto id = cell->quad(l)->user_index();
2963 // Make sure we change the manifold indicator only once
2964 if (id != 0)
2965 {
2966 if (cell->quad(l)->manifold_id() ==
2968 overwrite_only_flat_manifold_ids == false)
2969 cell->quad(l)->set_manifold_id(
2970 disambiguation_function(manifold_ids[id]));
2971 cell->quad(l)->set_user_index(0);
2972 }
2973 }
2974 }
2975 tria.load_user_indices(backup);
2976 }
2977
2978
2979
2980 template <int dim, int spacedim>
2981 void
2983 const double limit_angle_fraction)
2984 {
2985 if (dim == 1)
2986 return; // Nothing to do
2987
2988 // Check that we don't have hanging nodes
2990 ExcMessage("The input Triangulation cannot "
2991 "have hanging nodes."));
2992
2994
2995 bool has_cells_with_more_than_dim_faces_on_boundary = true;
2996 bool has_cells_with_dim_faces_on_boundary = false;
2997
2998 unsigned int refinement_cycles = 0;
2999
3000 while (has_cells_with_more_than_dim_faces_on_boundary)
3001 {
3002 has_cells_with_more_than_dim_faces_on_boundary = false;
3003
3004 for (const auto &cell : tria.active_cell_iterators())
3005 {
3006 unsigned int boundary_face_counter = 0;
3007 for (auto f : cell->face_indices())
3008 if (cell->face(f)->at_boundary())
3009 ++boundary_face_counter;
3010 if (boundary_face_counter > dim)
3011 {
3012 has_cells_with_more_than_dim_faces_on_boundary = true;
3013 break;
3014 }
3015 else if (boundary_face_counter == dim)
3016 has_cells_with_dim_faces_on_boundary = true;
3017 }
3018 if (has_cells_with_more_than_dim_faces_on_boundary)
3019 {
3020 tria.refine_global(1);
3021 ++refinement_cycles;
3022 }
3023 }
3024
3025 if (has_cells_with_dim_faces_on_boundary)
3026 {
3027 tria.refine_global(1);
3028 ++refinement_cycles;
3029 }
3030 else
3031 {
3032 while (refinement_cycles > 0)
3033 {
3034 for (const auto &cell : tria.active_cell_iterators())
3035 cell->set_coarsen_flag();
3037 refinement_cycles--;
3038 }
3039 return;
3040 }
3041
3042 std::vector<bool> cells_to_remove(tria.n_active_cells(), false);
3043 std::vector<Point<spacedim>> vertices = tria.get_vertices();
3044
3045 std::vector<bool> faces_to_remove(tria.n_raw_faces(), false);
3046
3047 std::vector<CellData<dim>> cells_to_add;
3048 SubCellData subcelldata_to_add;
3049
3050 // Trick compiler for dimension independent things
3051 const unsigned int v0 = 0, v1 = 1, v2 = (dim > 1 ? 2 : 0),
3052 v3 = (dim > 1 ? 3 : 0);
3053
3054 for (const auto &cell : tria.active_cell_iterators())
3055 {
3056 double angle_fraction = 0;
3057 unsigned int vertex_at_corner = numbers::invalid_unsigned_int;
3058
3059 if (dim == 2)
3060 {
3062 p0[spacedim > 1 ? 1 : 0] = 1;
3064 p1[0] = 1;
3065
3066 if (cell->face(v0)->at_boundary() && cell->face(v3)->at_boundary())
3067 {
3068 p0 = cell->vertex(v0) - cell->vertex(v2);
3069 p1 = cell->vertex(v3) - cell->vertex(v2);
3070 vertex_at_corner = v2;
3071 }
3072 else if (cell->face(v3)->at_boundary() &&
3073 cell->face(v1)->at_boundary())
3074 {
3075 p0 = cell->vertex(v2) - cell->vertex(v3);
3076 p1 = cell->vertex(v1) - cell->vertex(v3);
3077 vertex_at_corner = v3;
3078 }
3079 else if (cell->face(1)->at_boundary() &&
3080 cell->face(2)->at_boundary())
3081 {
3082 p0 = cell->vertex(v0) - cell->vertex(v1);
3083 p1 = cell->vertex(v3) - cell->vertex(v1);
3084 vertex_at_corner = v1;
3085 }
3086 else if (cell->face(2)->at_boundary() &&
3087 cell->face(0)->at_boundary())
3088 {
3089 p0 = cell->vertex(v2) - cell->vertex(v0);
3090 p1 = cell->vertex(v1) - cell->vertex(v0);
3091 vertex_at_corner = v0;
3092 }
3093 p0 /= p0.norm();
3094 p1 /= p1.norm();
3095 angle_fraction = std::acos(p0 * p1) / numbers::PI;
3096 }
3097 else
3098 {
3100 }
3101
3102 if (angle_fraction > limit_angle_fraction)
3103 {
3104 auto flags_removal = [&](unsigned int f1,
3105 unsigned int f2,
3106 unsigned int n1,
3107 unsigned int n2) -> void {
3108 cells_to_remove[cell->active_cell_index()] = true;
3109 cells_to_remove[cell->neighbor(n1)->active_cell_index()] = true;
3110 cells_to_remove[cell->neighbor(n2)->active_cell_index()] = true;
3111
3112 faces_to_remove[cell->face(f1)->index()] = true;
3113 faces_to_remove[cell->face(f2)->index()] = true;
3114
3115 faces_to_remove[cell->neighbor(n1)->face(f1)->index()] = true;
3116 faces_to_remove[cell->neighbor(n2)->face(f2)->index()] = true;
3117 };
3118
3119 auto cell_creation = [&](const unsigned int vv0,
3120 const unsigned int vv1,
3121 const unsigned int f0,
3122 const unsigned int f1,
3123
3124 const unsigned int n0,
3125 const unsigned int v0n0,
3126 const unsigned int v1n0,
3127
3128 const unsigned int n1,
3129 const unsigned int v0n1,
3130 const unsigned int v1n1) {
3131 CellData<dim> c1, c2;
3132 CellData<1> l1, l2;
3133
3134 c1.vertices[v0] = cell->vertex_index(vv0);
3135 c1.vertices[v1] = cell->vertex_index(vv1);
3136 c1.vertices[v2] = cell->neighbor(n0)->vertex_index(v0n0);
3137 c1.vertices[v3] = cell->neighbor(n0)->vertex_index(v1n0);
3138
3139 c1.manifold_id = cell->manifold_id();
3140 c1.material_id = cell->material_id();
3141
3142 c2.vertices[v0] = cell->vertex_index(vv0);
3143 c2.vertices[v1] = cell->neighbor(n1)->vertex_index(v0n1);
3144 c2.vertices[v2] = cell->vertex_index(vv1);
3145 c2.vertices[v3] = cell->neighbor(n1)->vertex_index(v1n1);
3146
3147 c2.manifold_id = cell->manifold_id();
3148 c2.material_id = cell->material_id();
3149
3150 l1.vertices[0] = cell->vertex_index(vv0);
3151 l1.vertices[1] = cell->neighbor(n0)->vertex_index(v0n0);
3152
3153 l1.boundary_id = cell->line(f0)->boundary_id();
3154 l1.manifold_id = cell->line(f0)->manifold_id();
3155 subcelldata_to_add.boundary_lines.push_back(l1);
3156
3157 l2.vertices[0] = cell->vertex_index(vv0);
3158 l2.vertices[1] = cell->neighbor(n1)->vertex_index(v0n1);
3159
3160 l2.boundary_id = cell->line(f1)->boundary_id();
3161 l2.manifold_id = cell->line(f1)->manifold_id();
3162 subcelldata_to_add.boundary_lines.push_back(l2);
3163
3164 cells_to_add.push_back(c1);
3165 cells_to_add.push_back(c2);
3166 };
3167
3168 if (dim == 2)
3169 {
3170 switch (vertex_at_corner)
3171 {
3172 case 0:
3173 flags_removal(0, 2, 3, 1);
3174 cell_creation(0, 3, 0, 2, 3, 2, 3, 1, 1, 3);
3175 break;
3176 case 1:
3177 flags_removal(1, 2, 3, 0);
3178 cell_creation(1, 2, 2, 1, 0, 0, 2, 3, 3, 2);
3179 break;
3180 case 2:
3181 flags_removal(3, 0, 1, 2);
3182 cell_creation(2, 1, 3, 0, 1, 3, 1, 2, 0, 1);
3183 break;
3184 case 3:
3185 flags_removal(3, 1, 0, 2);
3186 cell_creation(3, 0, 1, 3, 2, 1, 0, 0, 2, 0);
3187 break;
3188 }
3189 }
3190 else
3191 {
3193 }
3194 }
3195 }
3196
3197 // if no cells need to be added, then no regularization is necessary.
3198 // Restore things as they were before this function was called.
3199 if (cells_to_add.empty())
3200 {
3201 while (refinement_cycles > 0)
3202 {
3203 for (const auto &cell : tria.active_cell_iterators())
3204 cell->set_coarsen_flag();
3206 refinement_cycles--;
3207 }
3208 return;
3209 }
3210
3211 // add the cells that were not marked as skipped
3212 for (const auto &cell : tria.active_cell_iterators())
3213 {
3214 if (cells_to_remove[cell->active_cell_index()] == false)
3215 {
3216 CellData<dim> c(cell->n_vertices());
3217 for (const unsigned int v : cell->vertex_indices())
3218 c.vertices[v] = cell->vertex_index(v);
3219 c.manifold_id = cell->manifold_id();
3220 c.material_id = cell->material_id();
3221 cells_to_add.push_back(c);
3222 }
3223 }
3224
3225 // Face counter for both dim == 2 and dim == 3
3227 face = tria.begin_active_face(),
3228 endf = tria.end_face();
3229 for (; face != endf; ++face)
3230 if ((face->at_boundary() ||
3231 face->manifold_id() != numbers::flat_manifold_id) &&
3232 faces_to_remove[face->index()] == false)
3233 {
3234 for (unsigned int l = 0; l < face->n_lines(); ++l)
3235 {
3236 CellData<1> line;
3237 if (dim == 2)
3238 {
3239 for (const unsigned int v : face->vertex_indices())
3240 line.vertices[v] = face->vertex_index(v);
3241 line.boundary_id = face->boundary_id();
3242 line.manifold_id = face->manifold_id();
3243 }
3244 else
3245 {
3246 for (const unsigned int v : face->line(l)->vertex_indices())
3247 line.vertices[v] = face->line(l)->vertex_index(v);
3248 line.boundary_id = face->line(l)->boundary_id();
3249 line.manifold_id = face->line(l)->manifold_id();
3250 }
3251 subcelldata_to_add.boundary_lines.push_back(line);
3252 }
3253 if (dim == 3)
3254 {
3255 CellData<2> quad(face->n_vertices());
3256 for (const unsigned int v : face->vertex_indices())
3257 quad.vertices[v] = face->vertex_index(v);
3258 quad.boundary_id = face->boundary_id();
3259 quad.manifold_id = face->manifold_id();
3260 subcelldata_to_add.boundary_quads.push_back(quad);
3261 }
3262 }
3264 cells_to_add,
3265 subcelldata_to_add);
3267
3268 // Save manifolds
3269 auto manifold_ids = tria.get_manifold_ids();
3270 std::map<types::manifold_id, std::unique_ptr<Manifold<dim, spacedim>>>
3271 manifolds;
3272 // Set manifolds in new Triangulation
3273 for (const auto manifold_id : manifold_ids)
3274 if (manifold_id != numbers::flat_manifold_id)
3275 manifolds[manifold_id] = tria.get_manifold(manifold_id).clone();
3276
3277 tria.clear();
3278
3279 tria.create_triangulation(vertices, cells_to_add, subcelldata_to_add);
3280
3281 // Restore manifolds
3282 for (const auto manifold_id : manifold_ids)
3283 if (manifold_id != numbers::flat_manifold_id)
3284 tria.set_manifold(manifold_id, *manifolds[manifold_id]);
3285 }
3286
3287
3288
3289 template <int dim, int spacedim>
3290#ifndef DOXYGEN
3291 std::tuple<
3292 std::vector<typename Triangulation<dim, spacedim>::active_cell_iterator>,
3293 std::vector<std::vector<Point<dim>>>,
3294 std::vector<std::vector<unsigned int>>>
3295#else
3296 return_type
3297#endif
3299 const Cache<dim, spacedim> &cache,
3300 const std::vector<Point<spacedim>> &points,
3302 &cell_hint)
3303 {
3304 const auto cqmp = compute_point_locations_try_all(cache, points, cell_hint);
3305 // Splitting the tuple's components
3306 auto &cells = std::get<0>(cqmp);
3307 auto &qpoints = std::get<1>(cqmp);
3308 auto &maps = std::get<2>(cqmp);
3309
3310 return std::make_tuple(std::move(cells),
3311 std::move(qpoints),
3312 std::move(maps));
3313 }
3314
3315
3316
3317 template <int dim, int spacedim>
3318#ifndef DOXYGEN
3319 std::tuple<
3320 std::vector<typename Triangulation<dim, spacedim>::active_cell_iterator>,
3321 std::vector<std::vector<Point<dim>>>,
3322 std::vector<std::vector<unsigned int>>,
3323 std::vector<unsigned int>>
3324#else
3325 return_type
3326#endif
3328 const Cache<dim, spacedim> &cache,
3329 const std::vector<Point<spacedim>> &points,
3331 &cell_hint)
3332 {
3333 Assert((dim == spacedim),
3334 ExcMessage("Only implemented for dim==spacedim."));
3335
3336 // Alias
3337 namespace bgi = boost::geometry::index;
3338
3339 // Get the mapping
3340 const auto &mapping = cache.get_mapping();
3341
3342 // How many points are here?
3343 const unsigned int np = points.size();
3344
3345 std::vector<typename Triangulation<dim, spacedim>::active_cell_iterator>
3346 cells_out;
3347 std::vector<std::vector<Point<dim>>> qpoints_out;
3348 std::vector<std::vector<unsigned int>> maps_out;
3349 std::vector<unsigned int> missing_points_out;
3350
3351 // Now the easy case.
3352 if (np == 0)
3353 return std::make_tuple(std::move(cells_out),
3354 std::move(qpoints_out),
3355 std::move(maps_out),
3356 std::move(missing_points_out));
3357
3358 // For the search we shall use the following tree
3359 const auto &b_tree = cache.get_cell_bounding_boxes_rtree();
3360
3361 // Now make a tree of indices for the points
3362 // [TODO] This would work better with pack_rtree_of_indices, but
3363 // windows does not like it. Build a tree with pairs of point and id
3364 std::vector<std::pair<Point<spacedim>, unsigned int>> points_and_ids(np);
3365 for (unsigned int i = 0; i < np; ++i)
3366 points_and_ids[i] = std::make_pair(points[i], i);
3367 const auto p_tree = pack_rtree(points_and_ids);
3368
3369 // Keep track of all found points
3370 std::vector<bool> found_points(points.size(), false);
3371
3372 // Check if a point was found
3373 const auto already_found = [&found_points](const auto &id) {
3374 AssertIndexRange(id.second, found_points.size());
3375 return found_points[id.second];
3376 };
3377
3378 // check if the given cell was already in the vector of cells before. If so,
3379 // insert in the corresponding vectors the reference point and the id.
3380 // Otherwise append a new entry to all vectors.
3381 const auto store_cell_point_and_id =
3382 [&](
3384 const Point<dim> &ref_point,
3385 const unsigned int &id) {
3386 const auto it = std::find(cells_out.rbegin(), cells_out.rend(), cell);
3387 if (it != cells_out.rend())
3388 {
3389 const auto cell_id =
3390 (cells_out.size() - 1 - (it - cells_out.rbegin()));
3391 qpoints_out[cell_id].emplace_back(ref_point);
3392 maps_out[cell_id].emplace_back(id);
3393 }
3394 else
3395 {
3396 cells_out.emplace_back(cell);
3397 qpoints_out.emplace_back(std::vector<Point<dim>>({ref_point}));
3398 maps_out.emplace_back(std::vector<unsigned int>({id}));
3399 }
3400 };
3401
3402 // Check all points within a given pair of box and cell
3403 const auto check_all_points_within_box = [&](const auto &leaf) {
3404 const double relative_tolerance = 1e-12;
3405 const BoundingBox<spacedim> box =
3406 leaf.first.create_extended_relative(relative_tolerance);
3407 const auto &cell_hint = leaf.second;
3408
3409 for (const auto &point_and_id :
3410 p_tree | bgi::adaptors::queried(!bgi::satisfies(already_found) &&
3411 bgi::intersects(box)))
3412 {
3413 const auto id = point_and_id.second;
3414 const auto cell_and_ref =
3416 points[id],
3417 cell_hint);
3418 const auto &cell = cell_and_ref.first;
3419 const auto &ref_point = cell_and_ref.second;
3420
3421 if (cell.state() == IteratorState::valid)
3422 store_cell_point_and_id(cell, ref_point, id);
3423 else
3424 missing_points_out.emplace_back(id);
3425
3426 // Don't look anymore for this point
3427 found_points[id] = true;
3428 }
3429 };
3430
3431 // If a hint cell was given, use it
3432 if (cell_hint.state() == IteratorState::valid)
3433 check_all_points_within_box(
3434 std::make_pair(mapping.get_bounding_box(cell_hint), cell_hint));
3435
3436 // Now loop over all points that have not been found yet
3437 for (unsigned int i = 0; i < np; ++i)
3438 if (found_points[i] == false)
3439 {
3440 // Get the closest cell to this point
3441 const auto leaf = b_tree.qbegin(bgi::nearest(points[i], 1));
3442 // Now checks all points that fall within this box
3443 if (leaf != b_tree.qend())
3444 check_all_points_within_box(*leaf);
3445 else
3446 {
3447 // We should not get here. Throw an error.
3449 }
3450 }
3451 // Now make sure we send out the rest of the points that we did not find.
3452 for (unsigned int i = 0; i < np; ++i)
3453 if (found_points[i] == false)
3454 missing_points_out.emplace_back(i);
3455
3456 // Debug Checking
3457 AssertDimension(cells_out.size(), maps_out.size());
3458 AssertDimension(cells_out.size(), qpoints_out.size());
3459
3460 if constexpr (running_in_debug_mode())
3461 {
3462 unsigned int c = cells_out.size();
3463 unsigned int qps = 0;
3464 // The number of points in all
3465 // the cells must be the same as
3466 // the number of points we
3467 // started off from,
3468 // plus the points which were ignored
3469 for (unsigned int n = 0; n < c; ++n)
3470 {
3471 AssertDimension(qpoints_out[n].size(), maps_out[n].size());
3472 qps += qpoints_out[n].size();
3473 }
3474
3475 Assert(qps + missing_points_out.size() == np,
3476 ExcDimensionMismatch(qps + missing_points_out.size(), np));
3477 }
3478
3479 return std::make_tuple(std::move(cells_out),
3480 std::move(qpoints_out),
3481 std::move(maps_out),
3482 std::move(missing_points_out));
3483 }
3484
3485
3486
3487 template <int dim, int spacedim>
3488#ifndef DOXYGEN
3489 std::tuple<
3490 std::vector<typename Triangulation<dim, spacedim>::active_cell_iterator>,
3491 std::vector<std::vector<Point<dim>>>,
3492 std::vector<std::vector<unsigned int>>,
3493 std::vector<std::vector<Point<spacedim>>>,
3494 std::vector<std::vector<unsigned int>>>
3495#else
3496 return_type
3497#endif
3500 const std::vector<Point<spacedim>> &points,
3501 const std::vector<std::vector<BoundingBox<spacedim>>> &global_bboxes,
3502 const double tolerance,
3503 const std::vector<bool> &marked_vertices,
3504 const bool enforce_unique_mapping)
3505 {
3506 // run internal function ...
3507 const auto all =
3509 points,
3510 global_bboxes,
3511 marked_vertices,
3512 tolerance,
3513 false,
3514 enforce_unique_mapping)
3515 .send_components;
3516
3517 // ... and reshuffle the data
3518 std::tuple<
3519 std::vector<typename Triangulation<dim, spacedim>::active_cell_iterator>,
3520 std::vector<std::vector<Point<dim>>>,
3521 std::vector<std::vector<unsigned int>>,
3522 std::vector<std::vector<Point<spacedim>>>,
3523 std::vector<std::vector<unsigned int>>>
3524 result;
3525
3526 std::pair<int, int> dummy{-1, -1};
3527
3528 for (unsigned int i = 0; i < all.size(); ++i)
3529 {
3530 if (dummy != std::get<0>(all[i]))
3531 {
3532 std::get<0>(result).push_back(
3534 &cache.get_triangulation(),
3535 std::get<0>(all[i]).first,
3536 std::get<0>(all[i]).second});
3537
3538 const unsigned int new_size = std::get<0>(result).size();
3539
3540 std::get<1>(result).resize(new_size);
3541 std::get<2>(result).resize(new_size);
3542 std::get<3>(result).resize(new_size);
3543 std::get<4>(result).resize(new_size);
3544
3545 dummy = std::get<0>(all[i]);
3546 }
3547
3548 std::get<1>(result).back().push_back(
3549 std::get<3>(all[i])); // reference point
3550 std::get<2>(result).back().push_back(std::get<2>(all[i])); // index
3551 std::get<3>(result).back().push_back(std::get<4>(all[i])); // real point
3552 std::get<4>(result).back().push_back(std::get<1>(all[i])); // rank
3553 }
3554
3555 return result;
3556 }
3557
3558
3559
3560 namespace internal
3561 {
3568 template <int spacedim, typename T>
3569 std::tuple<std::vector<unsigned int>,
3570 std::vector<unsigned int>,
3571 std::vector<unsigned int>>
3573 const MPI_Comm comm,
3574 const std::vector<std::vector<BoundingBox<spacedim>>> &global_bboxes,
3575 const std::vector<T> &entities,
3576 const double tolerance)
3577 {
3578 std::vector<std::pair<unsigned int, unsigned int>> ranks_and_indices;
3579 ranks_and_indices.reserve(entities.size());
3580
3581#if defined(DEAL_II_WITH_ARBORX)
3582 static constexpr bool use_arborx = true;
3583#else
3584 static constexpr bool use_arborx = false;
3585#endif
3586 // Lambda to process bboxes if global_bboxes.size()>1 or ArborX not
3587 // available
3588 const auto process_bboxes = [&]() -> void {
3589 std::vector<std::vector<BoundingBox<spacedim>>> global_bboxes_temp;
3590 auto *global_bboxes_to_be_used = &global_bboxes;
3591
3592 if (global_bboxes.size() == 1 && use_arborx == false)
3593 {
3594 global_bboxes_temp =
3595 Utilities::MPI::all_gather(comm, global_bboxes[0]);
3596 global_bboxes_to_be_used = &global_bboxes_temp;
3597 }
3598
3599 // helper function to determine if a bounding box is valid
3600 const auto is_valid = [](const auto &bb) {
3601 for (unsigned int i = 0; i < spacedim; ++i)
3602 if (bb.get_boundary_points().first[i] >
3603 bb.get_boundary_points().second[i])
3604 return false;
3605
3606 return true;
3607 };
3608
3609 // linearize vector of vectors
3610 std::vector<std::pair<BoundingBox<spacedim>, unsigned int>>
3611 boxes_and_ranks;
3612
3613 for (unsigned rank = 0; rank < global_bboxes_to_be_used->size(); ++rank)
3614 for (const auto &box : (*global_bboxes_to_be_used)[rank])
3615 if (is_valid(box))
3616 boxes_and_ranks.emplace_back(box, rank);
3617
3618 // pack boxes into r-tree
3619 const auto tree = pack_rtree(boxes_and_ranks);
3620
3621 // loop over all entities
3622 for (unsigned int i = 0; i < entities.size(); ++i)
3623 {
3624 // create a bounding box with tolerance
3625 const auto bb =
3626 BoundingBox<spacedim>(entities[i]).create_extended(tolerance);
3627
3628 // determine ranks potentially owning point/bounding box
3629 std::set<unsigned int> my_ranks;
3630
3631 for (const auto &box_and_rank :
3632 tree | boost::geometry::index::adaptors::queried(
3633 boost::geometry::index::intersects(bb)))
3634 my_ranks.insert(box_and_rank.second);
3635
3636 for (const auto rank : my_ranks)
3637 ranks_and_indices.emplace_back(rank, i);
3638 }
3639 };
3640
3641 if constexpr (use_arborx)
3642 {
3643 if (global_bboxes.size() == 1)
3644 {
3645 ArborXWrappers::DistributedTree distributed_tree(
3646 comm, global_bboxes[0]);
3647 std::vector<BoundingBox<spacedim>> query_bounding_boxes;
3648 query_bounding_boxes.reserve(entities.size());
3649 for (const auto &entity : entities)
3650 query_bounding_boxes.emplace_back(
3651 BoundingBox<spacedim>(entity).create_extended(tolerance));
3652
3654 query_bounding_boxes);
3655 const auto &[indices_ranks, offsets] =
3656 distributed_tree.query(bb_intersect);
3657
3658 for (unsigned long int i = 0; i < offsets.size() - 1; ++i)
3659 {
3660 std::set<unsigned int> my_ranks;
3661 for (int j = offsets[i]; j < offsets[i + 1]; ++j)
3662 my_ranks.insert(indices_ranks[j].second);
3663
3664 for (const auto rank : my_ranks)
3665 ranks_and_indices.emplace_back(rank, i);
3666 }
3667 }
3668 else
3669 {
3670 // global_bboxes.size()>1
3671 process_bboxes();
3672 }
3673 }
3674 else
3675 {
3676 // No ArborX
3677 process_bboxes();
3678 }
3679
3680
3681 // convert to CRS
3682 std::sort(ranks_and_indices.begin(), ranks_and_indices.end());
3683
3684 std::vector<unsigned int> ranks;
3685 std::vector<unsigned int> ptr;
3686 std::vector<unsigned int> indices;
3687
3688 unsigned int current_rank = numbers::invalid_unsigned_int;
3689
3690 for (const std::pair<unsigned int, unsigned int> &i : ranks_and_indices)
3691 {
3692 if (current_rank != i.first)
3693 {
3694 current_rank = i.first;
3695 ranks.push_back(current_rank);
3696 ptr.push_back(indices.size());
3697 }
3698
3699 indices.push_back(i.second);
3700 }
3701 ptr.push_back(indices.size());
3702
3703 return {std::move(ranks), std::move(ptr), std::move(indices)};
3704 }
3705
3706
3707
3708 template <int dim, int spacedim>
3709 std::vector<
3710 std::pair<typename Triangulation<dim, spacedim>::active_cell_iterator,
3711 Point<dim>>>
3713 const Cache<dim, spacedim> &cache,
3714 const Point<spacedim> &point,
3716 const std::vector<bool> &marked_vertices,
3717 const double tolerance,
3718 const bool enforce_unique_mapping)
3719 {
3720 std::vector<
3721 std::pair<typename Triangulation<dim, spacedim>::active_cell_iterator,
3722 Point<dim>>>
3723 locally_owned_active_cells_around_point;
3724
3725 const auto first_cell = GridTools::find_active_cell_around_point(
3726 cache.get_mapping(),
3727 cache.get_triangulation(),
3728 point,
3729 cache.get_vertex_to_cell_map(),
3731 cell_hint,
3732 marked_vertices,
3734 tolerance,
3736
3737 const unsigned int my_rank = Utilities::MPI::this_mpi_process(
3739
3740 cell_hint = first_cell.first;
3741 if (cell_hint.state() == IteratorState::valid)
3742 {
3743 const auto active_cells_around_point =
3745 cache.get_mapping(),
3746 cache.get_triangulation(),
3747 point,
3748 tolerance,
3749 first_cell,
3750 &cache.get_vertex_to_cell_map());
3751
3752 if (enforce_unique_mapping)
3753 {
3754 // check if the rank of this process is the lowest of all cells
3755 // if not, the other process will handle this cell and we don't
3756 // have to do here anything in the case of unique mapping
3757 unsigned int lowes_rank = numbers::invalid_unsigned_int;
3758
3759 for (const auto &cell : active_cells_around_point)
3760 lowes_rank = std::min(lowes_rank, cell.first->subdomain_id());
3761
3762 if (lowes_rank != my_rank)
3763 return {};
3764 }
3765
3766 locally_owned_active_cells_around_point.reserve(
3767 active_cells_around_point.size());
3768
3769 for (const auto &cell : active_cells_around_point)
3770 if (cell.first->is_locally_owned())
3771 locally_owned_active_cells_around_point.push_back(cell);
3772 }
3773
3774 std::sort(locally_owned_active_cells_around_point.begin(),
3775 locally_owned_active_cells_around_point.end(),
3776 [](const auto &a, const auto &b) { return a.first < b.first; });
3777
3778 if (enforce_unique_mapping &&
3779 locally_owned_active_cells_around_point.size() > 1)
3780 // in the case of unique mapping, we only need a single cell
3781 return {locally_owned_active_cells_around_point.front()};
3782 else
3783 return locally_owned_active_cells_around_point;
3784 }
3785
3786 template <int dim, int spacedim>
3791
3792 template <int dim, int spacedim>
3793 void
3795 {
3796 // before reshuffeling the data check if data.recv_components and
3797 // n_searched_points are in a valid state.
3798 Assert(n_searched_points != numbers::invalid_unsigned_int,
3800 Assert(recv_components.empty() ||
3801 std::get<1>(*std::max_element(recv_components.begin(),
3802 recv_components.end(),
3803 [](const auto &a, const auto &b) {
3804 return std::get<1>(a) <
3805 std::get<1>(b);
3806 })) < n_searched_points,
3808
3809 send_ranks.clear();
3810 recv_ranks.clear();
3811 send_ptrs.clear();
3812 recv_ptrs.clear();
3813
3814 if (true)
3815 {
3816 // sort according to rank (and point index and cell) -> make
3817 // deterministic
3818 std::sort(send_components.begin(),
3819 send_components.end(),
3820 [&](const auto &a, const auto &b) {
3821 if (std::get<1>(a) != std::get<1>(b)) // rank
3822 return std::get<1>(a) < std::get<1>(b);
3823
3824 if (std::get<2>(a) != std::get<2>(b)) // point index
3825 return std::get<2>(a) < std::get<2>(b);
3826
3827 return std::get<0>(a) < std::get<0>(b); // cell
3828 });
3829
3830 // perform enumeration and extract rank information
3831 for (unsigned int i = 0, dummy = numbers::invalid_unsigned_int;
3832 i < send_components.size();
3833 ++i)
3834 {
3835 std::get<5>(send_components[i]) = i;
3836
3837 if (dummy != std::get<1>(send_components[i]))
3838 {
3839 dummy = std::get<1>(send_components[i]);
3840 send_ranks.push_back(dummy);
3841 send_ptrs.push_back(i);
3842 }
3843 }
3844 send_ptrs.push_back(send_components.size());
3845
3846 // sort according to cell, rank, point index (while keeping
3847 // partial ordering)
3848 std::sort(send_components.begin(),
3849 send_components.end(),
3850 [&](const auto &a, const auto &b) {
3851 if (std::get<0>(a) != std::get<0>(b))
3852 return std::get<0>(a) < std::get<0>(b); // cell
3853
3854 if (std::get<1>(a) != std::get<1>(b))
3855 return std::get<1>(a) < std::get<1>(b); // rank
3856
3857 if (std::get<2>(a) != std::get<2>(b))
3858 return std::get<2>(a) < std::get<2>(b); // point index
3859
3860 return std::get<5>(a) < std::get<5>(b); // enumeration
3861 });
3862 }
3863
3864 if (recv_components.size() > 0)
3865 {
3866 // sort according to rank (and point index) -> make deterministic
3867 std::sort(recv_components.begin(),
3868 recv_components.end(),
3869 [&](const auto &a, const auto &b) {
3870 if (std::get<0>(a) != std::get<0>(b))
3871 return std::get<0>(a) < std::get<0>(b); // rank
3872
3873 return std::get<1>(a) < std::get<1>(b); // point index
3874 });
3875
3876 // perform enumeration and extract rank information
3877 for (unsigned int i = 0, dummy = numbers::invalid_unsigned_int;
3878 i < recv_components.size();
3879 ++i)
3880 {
3881 std::get<2>(recv_components[i]) = i;
3882
3883 if (dummy != std::get<0>(recv_components[i]))
3884 {
3885 dummy = std::get<0>(recv_components[i]);
3886 recv_ranks.push_back(dummy);
3887 recv_ptrs.push_back(i);
3888 }
3889 }
3890 recv_ptrs.push_back(recv_components.size());
3891
3892 // sort according to point index and rank (while keeping partial
3893 // ordering)
3894 std::sort(recv_components.begin(),
3895 recv_components.end(),
3896 [&](const auto &a, const auto &b) {
3897 if (std::get<1>(a) != std::get<1>(b))
3898 return std::get<1>(a) < std::get<1>(b); // point index
3899
3900 if (std::get<0>(a) != std::get<0>(b))
3901 return std::get<0>(a) < std::get<0>(b); // rank
3902
3903 return std::get<2>(a) < std::get<2>(b); // enumeration
3904 });
3905 }
3906 }
3907
3908
3909
3910 template <int dim, int spacedim>
3914 const std::vector<Point<spacedim>> &points,
3915 const std::vector<std::vector<BoundingBox<spacedim>>> &global_bboxes,
3916 const std::vector<bool> &marked_vertices,
3917 const double tolerance,
3918 const bool perform_handshake,
3919 const bool enforce_unique_mapping)
3920 {
3922 result.n_searched_points = points.size();
3923
3924 auto &send_components = result.send_components;
3925 auto &recv_components = result.recv_components;
3926
3927 const auto comm = cache.get_triangulation().get_mpi_communicator();
3928
3929 const auto potential_owners = internal::guess_owners_of_entities(
3930 comm, global_bboxes, points, tolerance);
3931
3932 const auto &potential_owners_ranks = std::get<0>(potential_owners);
3933 const auto &potential_owners_ptrs = std::get<1>(potential_owners);
3934 const auto &potential_owners_indices = std::get<2>(potential_owners);
3935
3936 auto cell_hint = cache.get_triangulation().begin_active();
3937
3938 const auto translate = [&](const unsigned int other_rank) {
3939 const auto ptr = std::find(potential_owners_ranks.begin(),
3940 potential_owners_ranks.end(),
3941 other_rank);
3942
3943 Assert(ptr != potential_owners_ranks.end(), ExcInternalError());
3944
3945 const auto other_rank_index =
3946 std::distance(potential_owners_ranks.begin(), ptr);
3947
3948 return other_rank_index;
3949 };
3950
3951 Assert(
3952 (marked_vertices.empty()) ||
3953 (marked_vertices.size() == cache.get_triangulation().n_vertices()),
3954 ExcMessage(
3955 "The marked_vertices vector has to be either empty or its size has "
3956 "to equal the number of vertices of the triangulation."));
3957
3958 using RequestType = std::vector<std::pair<unsigned int, Point<spacedim>>>;
3959 using AnswerType = std::vector<unsigned int>;
3960
3961 // In the case that a marked_vertices vector has been given and none
3962 // of its entries is true, we know that this process does not own
3963 // any of the incoming points (and it will not send any data) so
3964 // that we can take a short cut.
3965 const bool has_relevant_vertices =
3966 (marked_vertices.empty()) ||
3967 (std::find(marked_vertices.begin(), marked_vertices.end(), true) !=
3968 marked_vertices.end());
3969
3970 const auto create_request = [&](const unsigned int other_rank) {
3971 const auto other_rank_index = translate(other_rank);
3972
3973 RequestType request;
3974 request.reserve(potential_owners_ptrs[other_rank_index + 1] -
3975 potential_owners_ptrs[other_rank_index]);
3976
3977 for (unsigned int i = potential_owners_ptrs[other_rank_index];
3978 i < potential_owners_ptrs[other_rank_index + 1];
3979 ++i)
3980 request.emplace_back(potential_owners_indices[i],
3981 points[potential_owners_indices[i]]);
3982
3983 return request;
3984 };
3985
3986 const auto answer_request =
3987 [&](const unsigned int &other_rank,
3988 const RequestType &request) -> AnswerType {
3989 AnswerType answer(request.size(), 0);
3990
3991 if (has_relevant_vertices)
3992 {
3993 cell_hint = cache.get_triangulation().begin_active();
3994
3995 for (unsigned int i = 0; i < request.size(); ++i)
3996 {
3997 const auto &index_and_point = request[i];
3998
3999 const auto cells_and_reference_positions =
4001 cache,
4002 index_and_point.second,
4003 cell_hint,
4004 marked_vertices,
4005 tolerance,
4006 enforce_unique_mapping);
4007
4008 if (cell_hint.state() != IteratorState::valid)
4009 cell_hint = cache.get_triangulation().begin_active();
4010
4011 for (const auto &cell_and_reference_position :
4012 cells_and_reference_positions)
4013 {
4014 const auto cell = cell_and_reference_position.first;
4015 auto reference_position =
4016 cell_and_reference_position.second;
4017
4018 reference_position =
4019 cell->reference_cell().closest_point(reference_position);
4020
4021 send_components.emplace_back(
4022 std::pair<int, int>(cell->level(), cell->index()),
4023 other_rank,
4024 index_and_point.first,
4025 reference_position,
4026 index_and_point.second,
4028 }
4029
4030 answer[i] = cells_and_reference_positions.size();
4031 }
4032 }
4033
4034 if (perform_handshake)
4035 return answer;
4036 else
4037 return {};
4038 };
4039
4040 const auto process_answer = [&](const unsigned int other_rank,
4041 const AnswerType &answer) {
4042 if (perform_handshake)
4043 {
4044 const auto other_rank_index = translate(other_rank);
4045
4046 for (unsigned int i = 0; i < answer.size(); ++i)
4047 for (unsigned int j = 0; j < answer[i]; ++j)
4048 recv_components.emplace_back(
4049 other_rank,
4050 potential_owners_indices
4051 [i + potential_owners_ptrs[other_rank_index]],
4053 }
4054 };
4055
4056 Utilities::MPI::ConsensusAlgorithms::selector<RequestType, AnswerType>(
4057 potential_owners_ranks,
4058 create_request,
4059 answer_request,
4060 process_answer,
4061 comm);
4062
4063 result.finalize_setup();
4064
4065 return result;
4066 }
4067
4068
4069
4070 template <int structdim, int spacedim>
4071 template <int dim>
4072 DistributedComputePointLocationsInternal<dim, spacedim>
4075 const unsigned int n_points_1D,
4076 const Triangulation<dim, spacedim> &tria,
4077 const Mapping<dim, spacedim> &mapping,
4078 std::vector<Quadrature<spacedim>> *mapped_quadratures_recv_comp,
4079 const bool consistent_numbering_of_sender_and_receiver) const
4080 {
4081 using CellIterator =
4083
4084 if (mapped_quadratures_recv_comp != nullptr)
4085 {
4086 AssertDimension(mapped_quadratures_recv_comp->size(), 0);
4087 mapped_quadratures_recv_comp->reserve(recv_components.size());
4088 }
4089
4091 spacedim>
4092 result;
4093
4094 // We need quadrature rules for the intersections. We are using a
4095 // QGaussSimplex quadrature rule since CGAL always returns simplices
4096 // as intersections.
4097 const QGaussSimplex<structdim> quadrature(n_points_1D);
4098
4099 // Resulting quadrature points get different indices. In the case the
4100 // requested intersections are unique also the resulting quadrature
4101 // points are unique and we can simply number the points in an
4102 // ascending way.
4103 for (const auto &recv_component : recv_components)
4104 {
4105 // dependent on the size of the intersection an empty quadrature
4106 // is returned. Therefore, we have to compute the quadrature also
4107 // here.
4108 const Quadrature<spacedim> &quad =
4110 std::get<2>(recv_component));
4111
4112 for (unsigned int i = 0; i < quad.size(); ++i)
4113 {
4114 // the third component of result.recv_components is not needed
4115 // before finalize_setup.
4116 result.recv_components.emplace_back(
4117 std::get<0>(recv_component),
4118 result.recv_components.size(), // number of point
4120 }
4121
4122 // append quadrature
4123 if (mapped_quadratures_recv_comp != nullptr)
4124 mapped_quadratures_recv_comp->push_back(quad);
4125 }
4126
4127 // since empty quadratures might be present we have to set the number
4128 // of searched points after inserting the point indices into
4129 // recv_components
4130 result.n_searched_points = result.recv_components.size();
4131
4132 // send_ranks, counter, and indices_of_rank is only needed if
4133 // consistent_numbering_of_sender_and_receiver==true
4134 // indices_of_rank is always empty if deal.II is compiled without MPI
4135 std::map<unsigned int, std::vector<unsigned int>> indices_of_rank;
4136 std::map<unsigned int, unsigned int> counter;
4137 std::set<unsigned int> send_ranks;
4138 if (consistent_numbering_of_sender_and_receiver)
4139 {
4140 for (const auto &sc : send_components)
4141 send_ranks.insert(std::get<1>(sc));
4142
4143 for (const auto rank : send_ranks)
4144 counter[rank] = 0;
4145
4146 // indices assigned at recv side needed to fill send_components
4147 indices_of_rank = communicate_indices(result.recv_components,
4148 tria.get_mpi_communicator());
4149 }
4150
4151 for (const auto &send_component : send_components)
4152 {
4153 const CellIterator cell(&tria,
4154 std::get<0>(send_component).first,
4155 std::get<0>(send_component).second);
4156
4157 const Quadrature<spacedim> &quad =
4159 std::get<3>(send_component));
4160
4161 const auto rank = std::get<1>(send_component);
4162
4163 for (unsigned int q = 0; q < quad.size(); ++q)
4164 {
4165 // the fifth component of result.send_components is filled
4166 // during sorting the data and initializing the CRS structures
4167 result.send_components.emplace_back(std::make_tuple(
4168 std::get<0>(send_component),
4169 rank,
4170 indices_of_rank.empty() ?
4171 result.send_components.size() :
4172 indices_of_rank.at(rank)[counter.at(rank)],
4173 mapping.transform_real_to_unit_cell(cell, quad.point(q)),
4174 quad.point(q),
4176
4177 if (!indices_of_rank.empty())
4178 ++counter[rank];
4179 }
4180 }
4181
4182 result.finalize_setup();
4183
4184 return result;
4185 }
4186
4187
4188
4189 template <int structdim, int spacedim>
4190 std::map<unsigned int, std::vector<unsigned int>>
4193 [[maybe_unused]] const std::vector<
4194 std::tuple<unsigned int, unsigned int, unsigned int>>
4195 &point_recv_components,
4196 [[maybe_unused]] const MPI_Comm comm) const
4197 {
4198#ifndef DEAL_II_WITH_MPI
4199 Assert(false, ExcNeedsMPI());
4200 return {};
4201#else
4202 // since we are converting to DistributedComputePointLocationsInternal
4203 // we use the RPE tag
4204 const auto mpi_tag =
4206
4207 const unsigned int my_rank = Utilities::MPI::this_mpi_process(comm);
4208
4209 std::set<unsigned int> send_ranks;
4210 for (const auto &sc : send_components)
4211 send_ranks.insert(std::get<1>(sc));
4212 std::set<unsigned int> recv_ranks;
4213 for (const auto &rc : recv_components)
4214 recv_ranks.insert(std::get<0>(rc));
4215
4216 std::vector<MPI_Request> requests;
4217 requests.reserve(send_ranks.size());
4218
4219 // rank to used indices on the rank needed on sending side
4220 std::map<unsigned int, std::vector<unsigned int>> indices_of_rank;
4221 indices_of_rank[my_rank] = std::vector<unsigned int>();
4222
4223 // rank to used indices on the rank known on recv side
4224 std::map<unsigned int, std::vector<unsigned int>> send_indices_of_rank;
4225 for (const auto rank : recv_ranks)
4226 if (rank != my_rank)
4227 send_indices_of_rank[rank] = std::vector<unsigned int>();
4228
4229 // fill the maps
4230 for (const auto &point_recv_component : point_recv_components)
4231 {
4232 const auto rank = std::get<0>(point_recv_component);
4233 const auto idx = std::get<1>(point_recv_component);
4234
4235 if (rank == my_rank)
4236 indices_of_rank[rank].emplace_back(idx);
4237 else
4238 send_indices_of_rank[rank].emplace_back(idx);
4239 }
4240
4241 // send indices to the ranks we normally receive from
4242 for (const auto rank : recv_ranks)
4243 {
4244 if (rank == my_rank)
4245 continue;
4246
4247 auto buffer = Utilities::pack(send_indices_of_rank[rank], false);
4248
4249 requests.push_back(MPI_Request());
4250
4251 const int ierr = MPI_Isend(buffer.data(),
4252 buffer.size(),
4253 MPI_CHAR,
4254 rank,
4255 mpi_tag,
4256 comm,
4257 &requests.back());
4258 AssertThrowMPI(ierr);
4259 }
4260
4261 // receive indices at the ranks we normally send from
4262 for (const auto rank : send_ranks)
4263 {
4264 if (rank == my_rank)
4265 continue;
4266
4267 MPI_Status status;
4268 int ierr = MPI_Probe(MPI_ANY_SOURCE, mpi_tag, comm, &status);
4269 AssertThrowMPI(ierr);
4270
4271 int message_length;
4272 ierr = MPI_Get_count(&status, MPI_CHAR, &message_length);
4273 AssertThrowMPI(ierr);
4274
4275 std::vector<char> buffer(message_length);
4276
4277 ierr = MPI_Recv(buffer.data(),
4278 buffer.size(),
4279 MPI_CHAR,
4280 status.MPI_SOURCE,
4281 mpi_tag,
4282 comm,
4283 MPI_STATUS_IGNORE);
4284 AssertThrowMPI(ierr);
4285
4286 indices_of_rank[status.MPI_SOURCE] =
4287 Utilities::unpack<std::vector<unsigned int>>(buffer, false);
4288 }
4289
4290 // make sure all messages have been sent
4291 const int ierr =
4292 MPI_Waitall(requests.size(), requests.data(), MPI_STATUSES_IGNORE);
4293 AssertThrowMPI(ierr);
4294
4295 return indices_of_rank;
4296#endif
4297 }
4298
4299
4300
4301 template <int structdim, int dim, int spacedim>
4304 const Cache<dim, spacedim> &cache,
4305 const std::vector<std::vector<Point<spacedim>>> &intersection_requests,
4306 const std::vector<std::vector<BoundingBox<spacedim>>> &global_bboxes,
4307 const std::vector<bool> &marked_vertices,
4308 const double tolerance)
4309 {
4310 using IntersectionRequest = std::vector<Point<spacedim>>;
4311
4312 using IntersectionAnswer =
4314 structdim,
4315 spacedim>::IntersectionType;
4316
4317 const auto comm = cache.get_triangulation().get_mpi_communicator();
4318
4320 result;
4321
4322 auto &send_components = result.send_components;
4323 auto &recv_components = result.recv_components;
4324 auto &recv_ptrs = result.recv_ptrs;
4325
4326 // search for potential owners
4327 const auto potential_owners = internal::guess_owners_of_entities(
4328 comm, global_bboxes, intersection_requests, tolerance);
4329
4330 const auto &potential_owners_ranks = std::get<0>(potential_owners);
4331 const auto &potential_owners_ptrs = std::get<1>(potential_owners);
4332 const auto &potential_owners_indices = std::get<2>(potential_owners);
4333
4334 const auto translate = [&](const unsigned int other_rank) {
4335 const auto ptr = std::find(potential_owners_ranks.begin(),
4336 potential_owners_ranks.end(),
4337 other_rank);
4338
4339 Assert(ptr != potential_owners_ranks.end(), ExcInternalError());
4340
4341 const auto other_rank_index =
4342 std::distance(potential_owners_ranks.begin(), ptr);
4343
4344 return other_rank_index;
4345 };
4346
4347 Assert(
4348 (marked_vertices.empty()) ||
4349 (marked_vertices.size() == cache.get_triangulation().n_vertices()),
4350 ExcMessage(
4351 "The marked_vertices vector has to be either empty or its size has "
4352 "to equal the number of vertices of the triangulation."));
4353
4354 // In the case that a marked_vertices vector has been given and none
4355 // of its entries is true, we know that this process does not own
4356 // any of the incoming points (and it will not send any data) so
4357 // that we can take a short cut.
4358 const bool has_relevant_vertices =
4359 (marked_vertices.empty()) ||
4360 (std::find(marked_vertices.begin(), marked_vertices.end(), true) !=
4361 marked_vertices.end());
4362
4363 // intersection between two cells:
4364 // One rank requests all intersections of owning cell:
4365 // owning cell index, cgal vertices of cell
4366 using RequestType =
4367 std::vector<std::pair<unsigned int, IntersectionRequest>>;
4368 // Other ranks send back all found intersections for requesting cell:
4369 // requesting cell index, cgal vertices of found intersections
4370 using AnswerType =
4371 std::vector<std::pair<unsigned int, IntersectionAnswer>>;
4372
4373 const auto create_request = [&](const unsigned int other_rank) {
4374 const auto other_rank_index = translate(other_rank);
4375
4376 RequestType request;
4377 request.reserve(potential_owners_ptrs[other_rank_index + 1] -
4378 potential_owners_ptrs[other_rank_index]);
4379
4380 for (unsigned int i = potential_owners_ptrs[other_rank_index];
4381 i < potential_owners_ptrs[other_rank_index + 1];
4382 ++i)
4383 request.emplace_back(
4384 potential_owners_indices[i],
4385 intersection_requests[potential_owners_indices[i]]);
4386
4387 return request;
4388 };
4389
4390
4391 // TODO: this is potentially useful in many cases and it would be nice to
4392 // have cache.get_locally_owned_cell_bounding_boxes_rtree(marked_vertices)
4393 const auto construct_locally_owned_cell_bounding_boxes_rtree =
4394 [&cache](const std::vector<bool> &marked_verts) {
4395 const auto cell_marked = [&marked_verts](const auto &cell) {
4396 for (const unsigned int v : cell->vertex_indices())
4397 if (marked_verts[cell->vertex_index(v)])
4398 return true;
4399 return false;
4400 };
4401
4402 const auto &boxes_and_cells =
4404
4405 if (marked_verts.empty())
4406 return boxes_and_cells;
4407
4408 std::vector<std::pair<
4411 potential_boxes_and_cells;
4412
4413 for (const auto &box_and_cell : boxes_and_cells)
4414 if (cell_marked(box_and_cell.second))
4415 potential_boxes_and_cells.emplace_back(box_and_cell);
4416
4417 return pack_rtree(potential_boxes_and_cells);
4418 };
4419
4420
4421 RTree<
4422 std::pair<BoundingBox<spacedim>,
4424 marked_cell_tree;
4425
4426 const auto answer_request =
4427 [&]([[maybe_unused]] const unsigned int &other_rank,
4428 const RequestType &request) -> AnswerType {
4429 AnswerType answer;
4430
4431 if (has_relevant_vertices)
4432 {
4433 if (marked_cell_tree.empty())
4434 {
4435 marked_cell_tree =
4436 construct_locally_owned_cell_bounding_boxes_rtree(
4437 marked_vertices);
4438 }
4439
4440 // process requests
4441 for (unsigned int i = 0; i < request.size(); ++i)
4442 {
4443 // create a bounding box with tolerance
4444 const auto bb = BoundingBox<spacedim>(request[i].second)
4445 .create_extended(tolerance);
4446
4447 for ([[maybe_unused]] const auto &box_cell :
4448 marked_cell_tree |
4449 boost::geometry::index::adaptors::queried(
4450 boost::geometry::index::intersects(bb)))
4451 {
4452#ifdef DEAL_II_WITH_CGAL
4453 const auto &cell = box_cell.second;
4454 const auto &request_index = request[i].first;
4455 auto requested_intersection = request[i].second;
4456 CGALWrappers::resort_dealii_vertices_to_cgal_order(
4457 structdim, requested_intersection);
4458
4459 const auto &try_intersection =
4460 CGALWrappers::get_vertices_in_cgal_order(
4461 cell, cache.get_mapping());
4462
4463 const auto &found_intersections = CGALWrappers::
4464 compute_intersection_of_cells<dim, structdim, spacedim>(
4465 try_intersection, requested_intersection, tolerance);
4466
4467 if (found_intersections.size() > 0)
4468 {
4469 for (const auto &found_intersection :
4470 found_intersections)
4471 {
4472 answer.emplace_back(request_index,
4473 found_intersection);
4474
4475 send_components.emplace_back(
4476 std::make_pair(cell->level(), cell->index()),
4477 other_rank,
4478 request_index,
4479 found_intersection);
4480 }
4481 }
4482#else
4483 Assert(false, ExcNeedsCGAL());
4484#endif
4485 }
4486 }
4487 }
4488
4489 return answer;
4490 };
4491
4492 const auto process_answer = [&](const unsigned int other_rank,
4493 const AnswerType &answer) {
4494 for (unsigned int i = 0; i < answer.size(); ++i)
4495 recv_components.emplace_back(other_rank,
4496 answer[i].first,
4497 answer[i].second);
4498 };
4499
4500 Utilities::MPI::ConsensusAlgorithms::selector<RequestType, AnswerType>(
4501 potential_owners_ranks,
4502 create_request,
4503 answer_request,
4504 process_answer,
4505 comm);
4506
4507 // sort according to 1) intersection index and 2) rank (keeping the order
4508 // of recv components with same indices and ranks)
4509 std::stable_sort(recv_components.begin(),
4510 recv_components.end(),
4511 [&](const auto &a, const auto &b) {
4512 // intersection index
4513 if (std::get<1>(a) != std::get<1>(b))
4514 return std::get<1>(a) < std::get<1>(b);
4515
4516 // rank
4517 return std::get<0>(a) < std::get<0>(b);
4518 });
4519
4520 // sort according to 1) rank and 2) intersection index (keeping the
4521 // order of recv components with same indices and ranks)
4522 std::stable_sort(send_components.begin(),
4523 send_components.end(),
4524 [&](const auto &a, const auto &b) {
4525 // rank
4526 if (std::get<1>(a) != std::get<1>(b))
4527 return std::get<1>(a) < std::get<1>(b);
4528
4529 // intersection idx
4530 return std::get<2>(a) < std::get<2>(b);
4531 });
4532
4533 // construct recv_ptrs
4534 recv_ptrs.assign(intersection_requests.size() + 1, 0);
4535 for (const auto &rc : recv_components)
4536 ++recv_ptrs[std::get<1>(rc) + 1];
4537 for (unsigned int i = 0; i < intersection_requests.size(); ++i)
4538 recv_ptrs[i + 1] += recv_ptrs[i];
4539
4540 return result;
4541 }
4542
4543 } // namespace internal
4544
4545
4546
4547 template <int spacedim>
4548 unsigned int
4549 find_closest_vertex(const std::map<unsigned int, Point<spacedim>> &vertices,
4550 const Point<spacedim> &p)
4551 {
4552 auto id_and_v = std::min_element(
4553 vertices.begin(),
4554 vertices.end(),
4555 [&](const std::pair<const unsigned int, Point<spacedim>> &p1,
4556 const std::pair<const unsigned int, Point<spacedim>> &p2) -> bool {
4557 return p1.second.distance(p) < p2.second.distance(p);
4558 });
4559 return id_and_v->first;
4560 }
4561
4562
4563 template <int dim, int spacedim>
4564 std::pair<typename Triangulation<dim, spacedim>::active_cell_iterator,
4565 Point<dim>>
4567 const Cache<dim, spacedim> &cache,
4568 const Point<spacedim> &p,
4570 &cell_hint,
4571 const std::vector<bool> &marked_vertices,
4572 const double tolerance)
4573 {
4574 const auto &mesh = cache.get_triangulation();
4575 const auto &mapping = cache.get_mapping();
4576 const auto &vertex_to_cells = cache.get_vertex_to_cell_map();
4577 const auto &vertex_to_cell_centers =
4579 const auto &used_vertices_rtree = cache.get_used_vertices_rtree();
4580
4581 return find_active_cell_around_point(mapping,
4582 mesh,
4583 p,
4584 vertex_to_cells,
4585 vertex_to_cell_centers,
4586 cell_hint,
4587 marked_vertices,
4588 used_vertices_rtree,
4589 tolerance);
4590 }
4591
4592 template <int spacedim>
4593 std::vector<std::vector<BoundingBox<spacedim>>>
4595 [[maybe_unused]] const std::vector<BoundingBox<spacedim>> &local_bboxes,
4596 [[maybe_unused]] const MPI_Comm mpi_communicator)
4597 {
4598#ifndef DEAL_II_WITH_MPI
4599 Assert(false,
4600 ExcMessage(
4601 "GridTools::exchange_local_bounding_boxes() requires MPI."));
4602 return {};
4603#else
4604 // Step 1: preparing data to be sent
4605 unsigned int n_bboxes = local_bboxes.size();
4606 // Dimension of the array to be exchanged (number of double)
4607 int n_local_data = 2 * spacedim * n_bboxes;
4608 // data array stores each entry of each point describing the bounding
4609 // boxes
4610 std::vector<double> loc_data_array(n_local_data);
4611 for (unsigned int i = 0; i < n_bboxes; ++i)
4612 for (unsigned int d = 0; d < spacedim; ++d)
4613 {
4614 // Extracting the coordinates of each boundary point
4615 loc_data_array[2 * i * spacedim + d] =
4616 local_bboxes[i].get_boundary_points().first[d];
4617 loc_data_array[2 * i * spacedim + spacedim + d] =
4618 local_bboxes[i].get_boundary_points().second[d];
4619 }
4620
4621 const std::vector<std::vector<double>> all_data_arrays =
4622 Utilities::MPI::all_gather(mpi_communicator, loc_data_array);
4623
4624 // Step 2: create the array of bboxes for output
4625 std::vector<std::vector<BoundingBox<spacedim>>> global_bboxes(
4626 all_data_arrays.size());
4627 for (unsigned int i = 0; i < all_data_arrays.size(); ++i)
4628 {
4629 // Number of local bounding boxes
4630 const unsigned int n_bbox_i =
4631 all_data_arrays[i].size() / (spacedim * 2);
4632 global_bboxes[i].resize(n_bbox_i);
4633 for (unsigned int bbox = 0; bbox < n_bbox_i; ++bbox)
4634 {
4635 Point<spacedim> p1, p2; // boundary points for bbox
4636 for (unsigned int d = 0; d < spacedim; ++d)
4637 {
4638 p1[d] = all_data_arrays[i][2 * bbox * spacedim + d];
4639 p2[d] = all_data_arrays[i][2 * bbox * spacedim + spacedim + d];
4640 }
4641 BoundingBox<spacedim> loc_bbox(std::make_pair(p1, p2));
4642 global_bboxes[i][bbox] = loc_bbox;
4643 }
4644 }
4645 return global_bboxes;
4646#endif // DEAL_II_WITH_MPI
4647 }
4648
4649
4650
4651 template <int spacedim>
4654 const std::vector<BoundingBox<spacedim>> &local_description,
4655 [[maybe_unused]] const MPI_Comm mpi_communicator)
4656 {
4657#ifndef DEAL_II_WITH_MPI
4658 // Building a tree with the only boxes available without MPI
4659 std::vector<std::pair<BoundingBox<spacedim>, unsigned int>> boxes_index(
4660 local_description.size());
4661 // Adding to each box the rank of the process owning it
4662 for (unsigned int i = 0; i < local_description.size(); ++i)
4663 boxes_index[i] = std::make_pair(local_description[i], 0u);
4664 return pack_rtree(boxes_index);
4665#else
4666 // Exchanging local bounding boxes
4667 const std::vector<std::vector<BoundingBox<spacedim>>> global_bboxes =
4668 Utilities::MPI::all_gather(mpi_communicator, local_description);
4669
4670 // Preparing to flatten the vector
4671 const unsigned int n_procs =
4672 Utilities::MPI::n_mpi_processes(mpi_communicator);
4673 // The i'th element of the following vector contains the index of the
4674 // first local bounding box from the process of rank i
4675 std::vector<unsigned int> bboxes_position(n_procs);
4676
4677 unsigned int tot_bboxes = 0;
4678 for (const auto &process_bboxes : global_bboxes)
4679 tot_bboxes += process_bboxes.size();
4680
4681 // Now flattening the vector
4682 std::vector<std::pair<BoundingBox<spacedim>, unsigned int>>
4683 flat_global_bboxes;
4684 flat_global_bboxes.reserve(tot_bboxes);
4685 unsigned int process_index = 0;
4686 for (const auto &process_bboxes : global_bboxes)
4687 {
4688 // Initialize a vector containing bounding boxes and rank of a process
4689 std::vector<std::pair<BoundingBox<spacedim>, unsigned int>>
4690 boxes_and_indices(process_bboxes.size());
4691
4692 // Adding to each box the rank of the process owning it
4693 for (unsigned int i = 0; i < process_bboxes.size(); ++i)
4694 boxes_and_indices[i] =
4695 std::make_pair(process_bboxes[i], process_index);
4696
4697 flat_global_bboxes.insert(flat_global_bboxes.end(),
4698 boxes_and_indices.begin(),
4699 boxes_and_indices.end());
4700
4701 ++process_index;
4702 }
4703
4704 // Build a tree out of the bounding boxes. We avoid using the
4705 // insert method so that boost uses the packing algorithm
4706 return RTree<std::pair<BoundingBox<spacedim>, unsigned int>>(
4707 flat_global_bboxes.begin(), flat_global_bboxes.end());
4708#endif // DEAL_II_WITH_MPI
4709 }
4710
4711
4712
4713 template <int dim, int spacedim>
4714 void
4716 const Triangulation<dim, spacedim> &tria,
4717 std::map<unsigned int, std::vector<unsigned int>> &coinciding_vertex_groups,
4718 std::map<unsigned int, unsigned int> &vertex_to_coinciding_vertex_group)
4719 {
4720 // 1) determine for each vertex a vertex it coincides with and
4721 // put it into a map
4722 {
4723 // loop over all periodic face pairs
4724 for (const auto &pair : tria.get_periodic_face_map())
4725 {
4726 if (pair.first.first->level() != pair.second.first.first->level())
4727 continue;
4728
4729 const auto face_a = pair.first.first->face(pair.first.second);
4730 const auto face_b =
4731 pair.second.first.first->face(pair.second.first.second);
4732 const auto reference_cell = pair.first.first->reference_cell();
4733 const auto face_reference_cell = face_a->reference_cell();
4734 const auto combined_orientation = pair.second.second;
4735 const auto inverse_combined_orientation =
4736 face_reference_cell.get_inverse_combined_orientation(
4737 combined_orientation);
4738
4739 AssertDimension(face_a->n_vertices(), face_b->n_vertices());
4740
4741 // loop over all vertices on face
4742 for (unsigned int i = 0; i < face_a->n_vertices(); ++i)
4743 {
4744 // find the right local vertex index for the second face
4745 const unsigned int j =
4746 reference_cell.standard_to_real_face_vertex(
4747 i, pair.first.second, inverse_combined_orientation);
4748
4749 // get vertex indices and store in map
4750 const auto vertex_a = face_a->vertex_index(i);
4751 const auto vertex_b = face_b->vertex_index(j);
4752 unsigned int temp = std::min(vertex_a, vertex_b);
4753
4754 auto it_a = vertex_to_coinciding_vertex_group.find(vertex_a);
4755 if (it_a != vertex_to_coinciding_vertex_group.end())
4756 temp = std::min(temp, it_a->second);
4757
4758 auto it_b = vertex_to_coinciding_vertex_group.find(vertex_b);
4759 if (it_b != vertex_to_coinciding_vertex_group.end())
4760 temp = std::min(temp, it_b->second);
4761
4762 if (it_a != vertex_to_coinciding_vertex_group.end())
4763 it_a->second = temp;
4764 else
4765 vertex_to_coinciding_vertex_group[vertex_a] = temp;
4766
4767 if (it_b != vertex_to_coinciding_vertex_group.end())
4768 it_b->second = temp;
4769 else
4770 vertex_to_coinciding_vertex_group[vertex_b] = temp;
4771 }
4772 }
4773
4774 // 2) compress map: let vertices point to the coinciding vertex with
4775 // the smallest index
4776 for (auto &p : vertex_to_coinciding_vertex_group)
4777 {
4778 if (p.first == p.second)
4779 continue;
4780 unsigned int temp = p.second;
4781 while (temp != vertex_to_coinciding_vertex_group[temp])
4782 temp = vertex_to_coinciding_vertex_group[temp];
4783 p.second = temp;
4784 }
4785
4786 // 3) create a map: smallest index of coinciding index -> all
4787 // coinciding indices
4788 for (auto p : vertex_to_coinciding_vertex_group)
4789 coinciding_vertex_groups[p.second] = {};
4790
4791 for (auto p : vertex_to_coinciding_vertex_group)
4792 coinciding_vertex_groups[p.second].push_back(p.first);
4793 }
4794 }
4795
4796
4797
4798 template <int dim, int spacedim>
4799 std::map<unsigned int, std::set<::types::subdomain_id>>
4801 const Triangulation<dim, spacedim> &tria)
4802 {
4803 if (dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(
4804 &tria) == nullptr) // nothing to do for a serial triangulation
4805 return {};
4806
4807 // 1) collect for each vertex on periodic faces all vertices it coincides
4808 // with
4809 std::map<unsigned int, std::vector<unsigned int>> coinciding_vertex_groups;
4810 std::map<unsigned int, unsigned int> vertex_to_coinciding_vertex_group;
4811
4813 coinciding_vertex_groups,
4814 vertex_to_coinciding_vertex_group);
4815
4816 // 2) collect vertices belonging to local cells
4817 std::vector<bool> vertex_of_own_cell(tria.n_vertices(), false);
4818 for (const auto &cell :
4820 for (const unsigned int v : cell->vertex_indices())
4821 vertex_of_own_cell[cell->vertex_index(v)] = true;
4822
4823 // 3) for each vertex belonging to a locally owned cell, find all ghost
4824 // neighbors (including the periodic own)
4825 std::map<unsigned int, std::set<types::subdomain_id>> result;
4826
4827 // loop over all active ghost cells
4828 for (const auto &cell : tria.active_cell_iterators())
4829 if (cell->is_ghost())
4830 {
4831 const types::subdomain_id owner = cell->subdomain_id();
4832
4833 // loop over all its vertices
4834 for (const unsigned int v : cell->vertex_indices())
4835 {
4836 // set owner if vertex belongs to a local cell
4837 if (vertex_of_own_cell[cell->vertex_index(v)])
4838 result[cell->vertex_index(v)].insert(owner);
4839
4840 // mark also nodes coinciding due to periodicity
4841 auto coinciding_vertex_group =
4842 vertex_to_coinciding_vertex_group.find(cell->vertex_index(v));
4843 if (coinciding_vertex_group !=
4844 vertex_to_coinciding_vertex_group.end())
4845 for (auto coinciding_vertex :
4846 coinciding_vertex_groups[coinciding_vertex_group->second])
4847 if (vertex_of_own_cell[coinciding_vertex])
4848 result[coinciding_vertex].insert(owner);
4849 }
4850 }
4851
4852 return result;
4853 }
4854
4855
4856
4857 namespace internal
4858 {
4859 template <int dim,
4860 unsigned int n_vertices,
4861 unsigned int n_sub_vertices,
4862 unsigned int n_configurations,
4863 unsigned int n_lines,
4864 unsigned int n_cols,
4865 typename value_type>
4866 void
4868 const std::array<unsigned int, n_configurations> &cut_line_table,
4870 const ndarray<unsigned int, n_lines, 2> &line_to_vertex_table,
4871 const std::vector<value_type> &ls_values,
4872 const std::vector<Point<dim>> &points,
4873 const std::vector<unsigned int> &mask,
4874 const double iso_level,
4875 const double tolerance,
4876 std::vector<Point<dim>> &vertices,
4877 std::vector<CellData<dim == 1 ? 1 : dim - 1>> &cells,
4878 const bool write_back_cell_data)
4879 {
4880 // inspired by https://graphics.stanford.edu/~mdfisher/MarchingCubes.html
4881
4882 constexpr unsigned int X = static_cast<unsigned int>(-1);
4883
4884 // determine configuration
4885 unsigned int configuration = 0;
4886 for (unsigned int v = 0; v < n_vertices; ++v)
4887 if (ls_values[mask[v]] < iso_level)
4888 configuration |= (1 << v);
4889
4890 // cell is not cut (nothing to do)
4891 if (cut_line_table[configuration] == 0)
4892 return;
4893
4894 // helper function to determine where an edge (between index i and j) is
4895 // cut - see also: http://paulbourke.net/geometry/polygonise/
4896 const auto interpolate = [&](const unsigned int i, const unsigned int j) {
4897 if (std::abs(iso_level - ls_values[mask[i]]) < tolerance)
4898 return points[mask[i]];
4899 if (std::abs(iso_level - ls_values[mask[j]]) < tolerance)
4900 return points[mask[j]];
4901 if (std::abs(ls_values[mask[i]] - ls_values[mask[j]]) < tolerance)
4902 return points[mask[i]];
4903
4904 const double mu = (iso_level - ls_values[mask[i]]) /
4905 (ls_values[mask[j]] - ls_values[mask[i]]);
4906
4907 return Point<dim>(points[mask[i]] +
4908 mu * (points[mask[j]] - points[mask[i]]));
4909 };
4910
4911 // determine the position where edges are cut (if they are cut)
4912 std::array<Point<dim>, n_lines> vertex_list_all;
4913 for (unsigned int l = 0; l < n_lines; ++l)
4914 if (cut_line_table[configuration] & (1 << l))
4915 vertex_list_all[l] =
4916 interpolate(line_to_vertex_table[l][0], line_to_vertex_table[l][1]);
4917
4918 // merge duplicate vertices if possible
4919 unsigned int local_vertex_count = 0;
4920 std::array<Point<dim>, n_lines> vertex_list_reduced;
4921 std::array<unsigned int, n_lines> local_remap;
4922 std::fill(local_remap.begin(), local_remap.end(), X);
4923 for (int i = 0; new_line_table[configuration][i] != X; ++i)
4924 if (local_remap[new_line_table[configuration][i]] == X)
4925 {
4926 vertex_list_reduced[local_vertex_count] =
4927 vertex_list_all[new_line_table[configuration][i]];
4928 local_remap[new_line_table[configuration][i]] = local_vertex_count;
4929 ++local_vertex_count;
4930 }
4931
4932 // write back vertices
4933 const unsigned int n_vertices_old = vertices.size();
4934 for (unsigned int i = 0; i < local_vertex_count; ++i)
4935 vertices.push_back(vertex_list_reduced[i]);
4936
4937 // write back cells
4938 if (write_back_cell_data && dim > 1)
4939 {
4940 for (unsigned int i = 0; new_line_table[configuration][i] != X;
4941 i += n_sub_vertices)
4942 {
4943 cells.resize(cells.size() + 1);
4944 cells.back().vertices.resize(n_sub_vertices);
4945
4946 for (unsigned int v = 0; v < n_sub_vertices; ++v)
4947 cells.back().vertices[v] =
4948 local_remap[new_line_table[configuration][i + v]] +
4949 n_vertices_old;
4950 }
4951 }
4952 }
4953 } // namespace internal
4954
4955
4956
4957 template <int dim, typename VectorType>
4959 const Mapping<dim, dim> &mapping,
4960 const FiniteElement<dim, dim> &fe,
4961 const unsigned int n_subdivisions,
4962 const double tolerance)
4963 : n_subdivisions(n_subdivisions)
4964 , tolerance(tolerance)
4965 , fe_values(mapping,
4966 fe,
4967 create_quadrature_rule(n_subdivisions),
4969 {}
4970
4971
4972
4973 template <int dim, typename VectorType>
4976 const unsigned int n_subdivisions)
4977 {
4978 std::vector<Point<dim>> quadrature_points;
4979
4980 if (dim == 1)
4981 {
4982 for (unsigned int i = 0; i <= n_subdivisions; ++i)
4983 quadrature_points.emplace_back(1.0 / n_subdivisions * i);
4984 }
4985 else if (dim == 2)
4986 {
4987 for (unsigned int j = 0; j <= n_subdivisions; ++j)
4988 for (unsigned int i = 0; i <= n_subdivisions; ++i)
4989 quadrature_points.emplace_back(1.0 / n_subdivisions * i,
4990 1.0 / n_subdivisions * j);
4991 }
4992 else
4993 {
4994 for (unsigned int k = 0; k <= n_subdivisions; ++k)
4995 for (unsigned int j = 0; j <= n_subdivisions; ++j)
4996 for (unsigned int i = 0; i <= n_subdivisions; ++i)
4997 quadrature_points.emplace_back(1.0 / n_subdivisions * i,
4998 1.0 / n_subdivisions * j,
4999 1.0 / n_subdivisions * k);
5000 }
5001
5002
5003 return {quadrature_points};
5004 }
5005
5006
5007
5008 template <int dim, typename VectorType>
5009 void
5011 const DoFHandler<dim> &background_dof_handler,
5012 const VectorType &ls_vector,
5013 const double iso_level,
5014 std::vector<Point<dim>> &vertices,
5015 std::vector<CellData<dim == 1 ? 1 : dim - 1>> &cells) const
5016 {
5018 dim > 1,
5019 ExcMessage(
5020 "Not implemented for dim==1. Use the alternative process()-function "
5021 "not returning a vector of CellData objects."));
5022
5023 for (const auto &cell : background_dof_handler.active_cell_iterators() |
5025 process_cell(cell, ls_vector, iso_level, vertices, cells);
5026 }
5027
5028 template <int dim, typename VectorType>
5029 void
5031 const DoFHandler<dim> &background_dof_handler,
5032 const VectorType &ls_vector,
5033 const double iso_level,
5034 std::vector<Point<dim>> &vertices) const
5035 {
5036 for (const auto &cell : background_dof_handler.active_cell_iterators() |
5038 process_cell(cell, ls_vector, iso_level, vertices);
5039
5040 delete_duplicated_vertices(vertices, 1e-10 /*tol*/);
5041 }
5042
5043
5044 template <int dim, typename VectorType>
5045 void
5047 const typename DoFHandler<dim>::active_cell_iterator &cell,
5048 const VectorType &ls_vector,
5049 const double iso_level,
5050 std::vector<Point<dim>> &vertices,
5051 std::vector<CellData<dim == 1 ? 1 : dim - 1>> &cells) const
5052 {
5054 dim > 1,
5055 ExcMessage(
5056 "Not implemented for dim==1. Use the alternative process_cell()-function "
5057 "not returning a vector of CellData objects."));
5058
5059 std::vector<value_type> ls_values;
5060
5061 fe_values.reinit(cell);
5062 ls_values.resize(fe_values.n_quadrature_points);
5063 fe_values.get_function_values(ls_vector, ls_values);
5064 process_cell(
5065 ls_values, fe_values.get_quadrature_points(), iso_level, vertices, cells);
5066 }
5067
5068 template <int dim, typename VectorType>
5069 void
5071 const typename DoFHandler<dim>::active_cell_iterator &cell,
5072 const VectorType &ls_vector,
5073 const double iso_level,
5074 std::vector<Point<dim>> &vertices) const
5075 {
5076 // This vector is just a placeholder to reuse the process_cell function.
5077 std::vector<CellData<dim == 1 ? 1 : dim - 1>> dummy_cells;
5078
5079 std::vector<value_type> ls_values;
5080
5081 fe_values.reinit(cell);
5082 ls_values.resize(fe_values.n_quadrature_points);
5083 fe_values.get_function_values(ls_vector, ls_values);
5084
5085 process_cell(ls_values,
5086 fe_values.get_quadrature_points(),
5087 iso_level,
5088 vertices,
5089 dummy_cells,
5090 false /*don't write back cell data*/);
5091 }
5092
5093
5094 template <int dim, typename VectorType>
5095 void
5097 std::vector<value_type> &ls_values,
5098 const std::vector<Point<dim>> &points,
5099 const double iso_level,
5100 std::vector<Point<dim>> &vertices,
5101 std::vector<CellData<dim == 1 ? 1 : dim - 1>> &cells,
5102 const bool write_back_cell_data) const
5103 {
5104 const unsigned p = n_subdivisions + 1;
5105
5106 if (dim == 1)
5107 {
5108 for (unsigned int i = 0; i < n_subdivisions; ++i)
5109 {
5110 std::vector<unsigned int> mask{i + 0, i + 1};
5111
5112 // check if a corner node is cut
5113 if (std::abs(iso_level - ls_values[mask[0]]) < tolerance)
5114 vertices.emplace_back(points[mask[0]]);
5115 else if (std::abs(iso_level - ls_values[mask[1]]) < tolerance)
5116 {
5117 if (i + 1 == n_subdivisions)
5118 vertices.emplace_back(points[mask[1]]);
5119 }
5120 // check if the edge is cut
5121 else if (((ls_values[mask[0]] > iso_level) &&
5122 (ls_values[mask[1]] < iso_level)) ||
5123 ((ls_values[mask[0]] < iso_level) &&
5124 (ls_values[mask[1]] > iso_level)))
5125 {
5126 // determine the interpolation weight (0<mu<1)
5127 const double mu = (iso_level - ls_values[mask[0]]) /
5128 (ls_values[mask[1]] - ls_values[mask[0]]);
5129
5130 // interpolate
5131 vertices.emplace_back(points[mask[0]] +
5132 mu * (points[mask[1]] - points[mask[0]]));
5133 }
5134 }
5135 }
5136 else if (dim == 2)
5137 {
5138 for (unsigned int j = 0; j < n_subdivisions; ++j)
5139 for (unsigned int i = 0; i < n_subdivisions; ++i)
5140 {
5141 std::vector<unsigned int> mask{p * (j + 0) + (i + 0),
5142 p * (j + 0) + (i + 1),
5143 p * (j + 1) + (i + 1),
5144 p * (j + 1) + (i + 0)};
5145
5146 process_sub_cell(ls_values,
5147 points,
5148 mask,
5149 iso_level,
5150 vertices,
5151 cells,
5152 write_back_cell_data);
5153 }
5154 }
5155 else if (dim == 3)
5156 {
5157 for (unsigned int k = 0; k < n_subdivisions; ++k)
5158 for (unsigned int j = 0; j < n_subdivisions; ++j)
5159 for (unsigned int i = 0; i < n_subdivisions; ++i)
5160 {
5161 std::vector<unsigned int> mask{
5162 p * p * (k + 0) + p * (j + 0) + (i + 0),
5163 p * p * (k + 0) + p * (j + 0) + (i + 1),
5164 p * p * (k + 0) + p * (j + 1) + (i + 1),
5165 p * p * (k + 0) + p * (j + 1) + (i + 0),
5166 p * p * (k + 1) + p * (j + 0) + (i + 0),
5167 p * p * (k + 1) + p * (j + 0) + (i + 1),
5168 p * p * (k + 1) + p * (j + 1) + (i + 1),
5169 p * p * (k + 1) + p * (j + 1) + (i + 0)};
5170
5171 process_sub_cell(ls_values,
5172 points,
5173 mask,
5174 iso_level,
5175 vertices,
5176 cells,
5177 write_back_cell_data);
5178 }
5179 }
5180 }
5181
5182
5183
5184 template <int dim, typename VectorType>
5185 void
5187 const std::vector<value_type> &ls_values,
5188 const std::vector<Point<2>> &points,
5189 const std::vector<unsigned int> &mask,
5190 const double iso_level,
5191 std::vector<Point<2>> &vertices,
5192 std::vector<CellData<1>> &cells,
5193 const bool write_back_cell_data) const
5194 {
5195 // set up dimension-dependent sizes and tables
5196 constexpr unsigned int n_vertices = 4;
5197 constexpr unsigned int n_sub_vertices = 2;
5198 constexpr unsigned int n_lines = 4;
5199 constexpr unsigned int n_configurations = Utilities::pow(2, n_vertices);
5200 constexpr unsigned int X = static_cast<unsigned int>(-1);
5201
5202 // table that indicates if an edge is cut (if the i-th bit is set the i-th
5203 // line is cut)
5204 constexpr std::array<unsigned int, n_configurations> cut_line_table = {
5205 {0b0000,
5206 0b0101,
5207 0b0110,
5208 0b0011,
5209 0b1010,
5210 0b0000,
5211 0b1100,
5212 0b1001,
5213 0b1001,
5214 0b1100,
5215 0b0000,
5216 0b1010,
5217 0b0011,
5218 0b0110,
5219 0b0101,
5220 0b0000}};
5221
5222 // list of the definition of the newly created lines (each line is defined
5223 // by two edges it cuts)
5224 constexpr ndarray<unsigned int, n_configurations, 5> new_line_table = {
5225 {{{X, X, X, X, X}},
5226 {{0, 2, X, X, X}},
5227 {{1, 2, X, X, X}},
5228 {{0, 1, X, X, X}},
5229 {{1, 3, X, X, X}},
5230 {{X, X, X, X, X}},
5231 {{2, 3, X, X, X}},
5232 {{0, 3, X, X, X}},
5233 {{0, 3, X, X, X}},
5234 {{2, 3, X, X, X}},
5235 {{X, X, X, X, X}},
5236 {{1, 3, X, X, X}},
5237 {{0, 1, X, X, X}},
5238 {{2, 1, X, X, X}},
5239 {{0, 2, X, X, X}},
5240 {{X, X, X, X, X}}}};
5241
5242 // vertices of each line
5243 constexpr ndarray<unsigned int, n_lines, 2> line_to_vertex_table = {
5244 {{{0, 3}}, {{1, 2}}, {{0, 1}}, {{3, 2}}}};
5245
5246 // run dimension-independent code
5248 n_vertices,
5249 n_sub_vertices,
5250 n_configurations,
5251 n_lines,
5252 5>(cut_line_table,
5253 new_line_table,
5254 line_to_vertex_table,
5255 ls_values,
5256 points,
5257 mask,
5258 iso_level,
5259 tolerance,
5260 vertices,
5261 cells,
5262 write_back_cell_data);
5263 }
5264
5265
5266
5267 template <int dim, typename VectorType>
5268 void
5270 const std::vector<value_type> &ls_values,
5271 const std::vector<Point<3>> &points,
5272 const std::vector<unsigned int> &mask,
5273 const double iso_level,
5274 std::vector<Point<3>> &vertices,
5275 std::vector<CellData<2>> &cells,
5276 const bool write_back_cell_data) const
5277 {
5278 // set up dimension-dependent sizes and tables
5279 constexpr unsigned int n_vertices = 8;
5280 constexpr unsigned int n_sub_vertices = 3;
5281 constexpr unsigned int n_lines = 12;
5282 constexpr unsigned int n_configurations = Utilities::pow(2, n_vertices);
5283 constexpr unsigned int X = static_cast<unsigned int>(-1);
5284
5285 // clang-format off
5286 // table that indicates if an edge is cut (if the i-th bit is set the i-th
5287 // line is cut)
5288 constexpr std::array<unsigned int, n_configurations> cut_line_table = {{
5289 0x0, 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c, 0x80c, 0x905,
5290 0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00, 0x190, 0x99, 0x393, 0x29a,
5291 0x596, 0x49f, 0x795, 0x69c, 0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93,
5292 0xf99, 0xe90, 0x230, 0x339, 0x33, 0x13a, 0x636, 0x73f, 0x435, 0x53c,
5293 0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30, 0x3a0, 0x2a9,
5294 0x1a3, 0xaa, 0x7a6, 0x6af, 0x5a5, 0x4ac, 0xbac, 0xaa5, 0x9af, 0x8a6,
5295 0xfaa, 0xea3, 0xda9, 0xca0, 0x460, 0x569, 0x663, 0x76a, 0x66, 0x16f,
5296 0x265, 0x36c, 0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, 0xa69, 0xb60,
5297 0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0xff, 0x3f5, 0x2fc, 0xdfc, 0xcf5,
5298 0xfff, 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0, 0x650, 0x759, 0x453, 0x55a,
5299 0x256, 0x35f, 0x55, 0x15c, 0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53,
5300 0x859, 0x950, 0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, 0x2cf, 0x1c5, 0xcc,
5301 0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0, 0x8c0, 0x9c9,
5302 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc, 0xcc, 0x1c5, 0x2cf, 0x3c6,
5303 0x4ca, 0x5c3, 0x6c9, 0x7c0, 0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f,
5304 0xf55, 0xe5c, 0x15c, 0x55, 0x35f, 0x256, 0x55a, 0x453, 0x759, 0x650,
5305 0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc, 0x2fc, 0x3f5,
5306 0xff, 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0, 0xb60, 0xa69, 0x963, 0x86a,
5307 0xf66, 0xe6f, 0xd65, 0xc6c, 0x36c, 0x265, 0x16f, 0x66, 0x76a, 0x663,
5308 0x569, 0x460, 0xca0, 0xda9, 0xea3, 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac,
5309 0x4ac, 0x5a5, 0x6af, 0x7a6, 0xaa, 0x1a3, 0x2a9, 0x3a0, 0xd30, 0xc39,
5310 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c, 0x53c, 0x435, 0x73f, 0x636,
5311 0x13a, 0x33, 0x339, 0x230, 0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f,
5312 0x895, 0x99c, 0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x99, 0x190,
5313 0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c, 0x70c, 0x605,
5314 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x0}};
5315 // clang-format on
5316
5317 // list of the definition of the newly created triangles (each triangles is
5318 // defined by two edges it cuts)
5319 constexpr ndarray<unsigned int, n_configurations, 16> new_line_table = {
5320 {{{X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5321 {{0, 8, 3, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5322 {{0, 1, 9, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5323 {{1, 8, 3, 9, 8, 1, X, X, X, X, X, X, X, X, X, X}},
5324 {{1, 2, 10, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5325 {{0, 8, 3, 1, 2, 10, X, X, X, X, X, X, X, X, X, X}},
5326 {{9, 2, 10, 0, 2, 9, X, X, X, X, X, X, X, X, X, X}},
5327 {{2, 8, 3, 2, 10, 8, 10, 9, 8, X, X, X, X, X, X, X}},
5328 {{3, 11, 2, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5329 {{0, 11, 2, 8, 11, 0, X, X, X, X, X, X, X, X, X, X}},
5330 {{1, 9, 0, 2, 3, 11, X, X, X, X, X, X, X, X, X, X}},
5331 {{1, 11, 2, 1, 9, 11, 9, 8, 11, X, X, X, X, X, X, X}},
5332 {{3, 10, 1, 11, 10, 3, X, X, X, X, X, X, X, X, X, X}},
5333 {{0, 10, 1, 0, 8, 10, 8, 11, 10, X, X, X, X, X, X, X}},
5334 {{3, 9, 0, 3, 11, 9, 11, 10, 9, X, X, X, X, X, X, X}},
5335 {{9, 8, 10, 10, 8, 11, X, X, X, X, X, X, X, X, X, X}},
5336 {{4, 7, 8, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5337 {{4, 3, 0, 7, 3, 4, X, X, X, X, X, X, X, X, X, X}},
5338 {{0, 1, 9, 8, 4, 7, X, X, X, X, X, X, X, X, X, X}},
5339 {{4, 1, 9, 4, 7, 1, 7, 3, 1, X, X, X, X, X, X, X}},
5340 {{1, 2, 10, 8, 4, 7, X, X, X, X, X, X, X, X, X, X}},
5341 {{3, 4, 7, 3, 0, 4, 1, 2, 10, X, X, X, X, X, X, X}},
5342 {{9, 2, 10, 9, 0, 2, 8, 4, 7, X, X, X, X, X, X, X}},
5343 {{2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4, X, X, X, X}},
5344 {{8, 4, 7, 3, 11, 2, X, X, X, X, X, X, X, X, X, X}},
5345 {{11, 4, 7, 11, 2, 4, 2, 0, 4, X, X, X, X, X, X, X}},
5346 {{9, 0, 1, 8, 4, 7, 2, 3, 11, X, X, X, X, X, X, X}},
5347 {{4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1, X, X, X, X}},
5348 {{3, 10, 1, 3, 11, 10, 7, 8, 4, X, X, X, X, X, X, X}},
5349 {{1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4, X, X, X, X}},
5350 {{4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3, X, X, X, X}},
5351 {{4, 7, 11, 4, 11, 9, 9, 11, 10, X, X, X, X, X, X, X}},
5352 {{9, 5, 4, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5353 {{9, 5, 4, 0, 8, 3, X, X, X, X, X, X, X, X, X, X}},
5354 {{0, 5, 4, 1, 5, 0, X, X, X, X, X, X, X, X, X, X}},
5355 {{8, 5, 4, 8, 3, 5, 3, 1, 5, X, X, X, X, X, X, X}},
5356 {{1, 2, 10, 9, 5, 4, X, X, X, X, X, X, X, X, X, X}},
5357 {{3, 0, 8, 1, 2, 10, 4, 9, 5, X, X, X, X, X, X, X}},
5358 {{5, 2, 10, 5, 4, 2, 4, 0, 2, X, X, X, X, X, X, X}},
5359 {{2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8, X, X, X, X}},
5360 {{9, 5, 4, 2, 3, 11, X, X, X, X, X, X, X, X, X, X}},
5361 {{0, 11, 2, 0, 8, 11, 4, 9, 5, X, X, X, X, X, X, X}},
5362 {{0, 5, 4, 0, 1, 5, 2, 3, 11, X, X, X, X, X, X, X}},
5363 {{2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5, X, X, X, X}},
5364 {{10, 3, 11, 10, 1, 3, 9, 5, 4, X, X, X, X, X, X, X}},
5365 {{4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10, X, X, X, X}},
5366 {{5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3, X, X, X, X}},
5367 {{5, 4, 8, 5, 8, 10, 10, 8, 11, X, X, X, X, X, X, X}},
5368 {{9, 7, 8, 5, 7, 9, X, X, X, X, X, X, X, X, X, X}},
5369 {{9, 3, 0, 9, 5, 3, 5, 7, 3, X, X, X, X, X, X, X}},
5370 {{0, 7, 8, 0, 1, 7, 1, 5, 7, X, X, X, X, X, X, X}},
5371 {{1, 5, 3, 3, 5, 7, X, X, X, X, X, X, X, X, X, X}},
5372 {{9, 7, 8, 9, 5, 7, 10, 1, 2, X, X, X, X, X, X, X}},
5373 {{10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3, X, X, X, X}},
5374 {{8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2, X, X, X, X}},
5375 {{2, 10, 5, 2, 5, 3, 3, 5, 7, X, X, X, X, X, X, X}},
5376 {{7, 9, 5, 7, 8, 9, 3, 11, 2, X, X, X, X, X, X, X}},
5377 {{9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11, X, X, X, X}},
5378 {{2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7, X, X, X, X}},
5379 {{11, 2, 1, 11, 1, 7, 7, 1, 5, X, X, X, X, X, X, X}},
5380 {{9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11, X, X, X, X}},
5381 {{5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0, X}},
5382 {{11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0, X}},
5383 {{11, 10, 5, 7, 11, 5, X, X, X, X, X, X, X, X, X, X}},
5384 {{10, 6, 5, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5385 {{0, 8, 3, 5, 10, 6, X, X, X, X, X, X, X, X, X, X}},
5386 {{9, 0, 1, 5, 10, 6, X, X, X, X, X, X, X, X, X, X}},
5387 {{1, 8, 3, 1, 9, 8, 5, 10, 6, X, X, X, X, X, X, X}},
5388 {{1, 6, 5, 2, 6, 1, X, X, X, X, X, X, X, X, X, X}},
5389 {{1, 6, 5, 1, 2, 6, 3, 0, 8, X, X, X, X, X, X, X}},
5390 {{9, 6, 5, 9, 0, 6, 0, 2, 6, X, X, X, X, X, X, X}},
5391 {{5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8, X, X, X, X}},
5392 {{2, 3, 11, 10, 6, 5, X, X, X, X, X, X, X, X, X, X}},
5393 {{11, 0, 8, 11, 2, 0, 10, 6, 5, X, X, X, X, X, X, X}},
5394 {{0, 1, 9, 2, 3, 11, 5, 10, 6, X, X, X, X, X, X, X}},
5395 {{5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11, X, X, X, X}},
5396 {{6, 3, 11, 6, 5, 3, 5, 1, 3, X, X, X, X, X, X, X}},
5397 {{0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6, X, X, X, X}},
5398 {{3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9, X, X, X, X}},
5399 {{6, 5, 9, 6, 9, 11, 11, 9, 8, X, X, X, X, X, X, X}},
5400 {{5, 10, 6, 4, 7, 8, X, X, X, X, X, X, X, X, X, X}},
5401 {{4, 3, 0, 4, 7, 3, 6, 5, 10, X, X, X, X, X, X, X}},
5402 {{1, 9, 0, 5, 10, 6, 8, 4, 7, X, X, X, X, X, X, X}},
5403 {{10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4, X, X, X, X}},
5404 {{6, 1, 2, 6, 5, 1, 4, 7, 8, X, X, X, X, X, X, X}},
5405 {{1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7, X, X, X, X}},
5406 {{8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6, X, X, X, X}},
5407 {{7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9, X}},
5408 {{3, 11, 2, 7, 8, 4, 10, 6, 5, X, X, X, X, X, X, X}},
5409 {{5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11, X, X, X, X}},
5410 {{0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6, X, X, X, X}},
5411 {{9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6, X}},
5412 {{8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6, X, X, X, X}},
5413 {{5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11, X}},
5414 {{0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7, X}},
5415 {{6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9, X, X, X, X}},
5416 {{10, 4, 9, 6, 4, 10, X, X, X, X, X, X, X, X, X, X}},
5417 {{4, 10, 6, 4, 9, 10, 0, 8, 3, X, X, X, X, X, X, X}},
5418 {{10, 0, 1, 10, 6, 0, 6, 4, 0, X, X, X, X, X, X, X}},
5419 {{8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10, X, X, X, X}},
5420 {{1, 4, 9, 1, 2, 4, 2, 6, 4, X, X, X, X, X, X, X}},
5421 {{3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4, X, X, X, X}},
5422 {{0, 2, 4, 4, 2, 6, X, X, X, X, X, X, X, X, X, X}},
5423 {{8, 3, 2, 8, 2, 4, 4, 2, 6, X, X, X, X, X, X, X}},
5424 {{10, 4, 9, 10, 6, 4, 11, 2, 3, X, X, X, X, X, X, X}},
5425 {{0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6, X, X, X, X}},
5426 {{3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10, X, X, X, X}},
5427 {{6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1, X}},
5428 {{9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3, X, X, X, X}},
5429 {{8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1, X}},
5430 {{3, 11, 6, 3, 6, 0, 0, 6, 4, X, X, X, X, X, X, X}},
5431 {{6, 4, 8, 11, 6, 8, X, X, X, X, X, X, X, X, X, X}},
5432 {{7, 10, 6, 7, 8, 10, 8, 9, 10, X, X, X, X, X, X, X}},
5433 {{0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10, X, X, X, X}},
5434 {{10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0, X, X, X, X}},
5435 {{10, 6, 7, 10, 7, 1, 1, 7, 3, X, X, X, X, X, X, X}},
5436 {{1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7, X, X, X, X}},
5437 {{2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9, X}},
5438 {{7, 8, 0, 7, 0, 6, 6, 0, 2, X, X, X, X, X, X, X}},
5439 {{7, 3, 2, 6, 7, 2, X, X, X, X, X, X, X, X, X, X}},
5440 {{2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7, X, X, X, X}},
5441 {{2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7, X}},
5442 {{1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11, X}},
5443 {{11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1, X, X, X, X}},
5444 {{8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6, X}},
5445 {{0, 9, 1, 11, 6, 7, X, X, X, X, X, X, X, X, X, X}},
5446 {{7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0, X, X, X, X}},
5447 {{7, 11, 6, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5448 {{7, 6, 11, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5449 {{3, 0, 8, 11, 7, 6, X, X, X, X, X, X, X, X, X, X}},
5450 {{0, 1, 9, 11, 7, 6, X, X, X, X, X, X, X, X, X, X}},
5451 {{8, 1, 9, 8, 3, 1, 11, 7, 6, X, X, X, X, X, X, X}},
5452 {{10, 1, 2, 6, 11, 7, X, X, X, X, X, X, X, X, X, X}},
5453 {{1, 2, 10, 3, 0, 8, 6, 11, 7, X, X, X, X, X, X, X}},
5454 {{2, 9, 0, 2, 10, 9, 6, 11, 7, X, X, X, X, X, X, X}},
5455 {{6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8, X, X, X, X}},
5456 {{7, 2, 3, 6, 2, 7, X, X, X, X, X, X, X, X, X, X}},
5457 {{7, 0, 8, 7, 6, 0, 6, 2, 0, X, X, X, X, X, X, X}},
5458 {{2, 7, 6, 2, 3, 7, 0, 1, 9, X, X, X, X, X, X, X}},
5459 {{1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6, X, X, X, X}},
5460 {{10, 7, 6, 10, 1, 7, 1, 3, 7, X, X, X, X, X, X, X}},
5461 {{10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8, X, X, X, X}},
5462 {{0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7, X, X, X, X}},
5463 {{7, 6, 10, 7, 10, 8, 8, 10, 9, X, X, X, X, X, X, X}},
5464 {{6, 8, 4, 11, 8, 6, X, X, X, X, X, X, X, X, X, X}},
5465 {{3, 6, 11, 3, 0, 6, 0, 4, 6, X, X, X, X, X, X, X}},
5466 {{8, 6, 11, 8, 4, 6, 9, 0, 1, X, X, X, X, X, X, X}},
5467 {{9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6, X, X, X, X}},
5468 {{6, 8, 4, 6, 11, 8, 2, 10, 1, X, X, X, X, X, X, X}},
5469 {{1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6, X, X, X, X}},
5470 {{4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9, X, X, X, X}},
5471 {{10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3, X}},
5472 {{8, 2, 3, 8, 4, 2, 4, 6, 2, X, X, X, X, X, X, X}},
5473 {{0, 4, 2, 4, 6, 2, X, X, X, X, X, X, X, X, X, X}},
5474 {{1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8, X, X, X, X}},
5475 {{1, 9, 4, 1, 4, 2, 2, 4, 6, X, X, X, X, X, X, X}},
5476 {{8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1, X, X, X, X}},
5477 {{10, 1, 0, 10, 0, 6, 6, 0, 4, X, X, X, X, X, X, X}},
5478 {{4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3, X}},
5479 {{10, 9, 4, 6, 10, 4, X, X, X, X, X, X, X, X, X, X}},
5480 {{4, 9, 5, 7, 6, 11, X, X, X, X, X, X, X, X, X, X}},
5481 {{0, 8, 3, 4, 9, 5, 11, 7, 6, X, X, X, X, X, X, X}},
5482 {{5, 0, 1, 5, 4, 0, 7, 6, 11, X, X, X, X, X, X, X}},
5483 {{11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5, X, X, X, X}},
5484 {{9, 5, 4, 10, 1, 2, 7, 6, 11, X, X, X, X, X, X, X}},
5485 {{6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5, X, X, X, X}},
5486 {{7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2, X, X, X, X}},
5487 {{3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6, X}},
5488 {{7, 2, 3, 7, 6, 2, 5, 4, 9, X, X, X, X, X, X, X}},
5489 {{9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7, X, X, X, X}},
5490 {{3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0, X, X, X, X}},
5491 {{6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8, X}},
5492 {{9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7, X, X, X, X}},
5493 {{1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4, X}},
5494 {{4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10, X}},
5495 {{7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10, X, X, X, X}},
5496 {{6, 9, 5, 6, 11, 9, 11, 8, 9, X, X, X, X, X, X, X}},
5497 {{3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5, X, X, X, X}},
5498 {{0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11, X, X, X, X}},
5499 {{6, 11, 3, 6, 3, 5, 5, 3, 1, X, X, X, X, X, X, X}},
5500 {{1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6, X, X, X, X}},
5501 {{0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10, X}},
5502 {{11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5, X}},
5503 {{6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3, X, X, X, X}},
5504 {{5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2, X, X, X, X}},
5505 {{9, 5, 6, 9, 6, 0, 0, 6, 2, X, X, X, X, X, X, X}},
5506 {{1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8, X}},
5507 {{1, 5, 6, 2, 1, 6, X, X, X, X, X, X, X, X, X, X}},
5508 {{1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6, X}},
5509 {{10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0, X, X, X, X}},
5510 {{0, 3, 8, 5, 6, 10, X, X, X, X, X, X, X, X, X, X}},
5511 {{10, 5, 6, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5512 {{11, 5, 10, 7, 5, 11, X, X, X, X, X, X, X, X, X, X}},
5513 {{11, 5, 10, 11, 7, 5, 8, 3, 0, X, X, X, X, X, X, X}},
5514 {{5, 11, 7, 5, 10, 11, 1, 9, 0, X, X, X, X, X, X, X}},
5515 {{10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1, X, X, X, X}},
5516 {{11, 1, 2, 11, 7, 1, 7, 5, 1, X, X, X, X, X, X, X}},
5517 {{0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11, X, X, X, X}},
5518 {{9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7, X, X, X, X}},
5519 {{7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2, X}},
5520 {{2, 5, 10, 2, 3, 5, 3, 7, 5, X, X, X, X, X, X, X}},
5521 {{8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5, X, X, X, X}},
5522 {{9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2, X, X, X, X}},
5523 {{9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2, X}},
5524 {{1, 3, 5, 3, 7, 5, X, X, X, X, X, X, X, X, X, X}},
5525 {{0, 8, 7, 0, 7, 1, 1, 7, 5, X, X, X, X, X, X, X}},
5526 {{9, 0, 3, 9, 3, 5, 5, 3, 7, X, X, X, X, X, X, X}},
5527 {{9, 8, 7, 5, 9, 7, X, X, X, X, X, X, X, X, X, X}},
5528 {{5, 8, 4, 5, 10, 8, 10, 11, 8, X, X, X, X, X, X, X}},
5529 {{5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0, X, X, X, X}},
5530 {{0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5, X, X, X, X}},
5531 {{10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4, X}},
5532 {{2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8, X, X, X, X}},
5533 {{0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11, X}},
5534 {{0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5, X}},
5535 {{9, 4, 5, 2, 11, 3, X, X, X, X, X, X, X, X, X, X}},
5536 {{2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4, X, X, X, X}},
5537 {{5, 10, 2, 5, 2, 4, 4, 2, 0, X, X, X, X, X, X, X}},
5538 {{3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9, X}},
5539 {{5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2, X, X, X, X}},
5540 {{8, 4, 5, 8, 5, 3, 3, 5, 1, X, X, X, X, X, X, X}},
5541 {{0, 4, 5, 1, 0, 5, X, X, X, X, X, X, X, X, X, X}},
5542 {{8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5, X, X, X, X}},
5543 {{9, 4, 5, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5544 {{4, 11, 7, 4, 9, 11, 9, 10, 11, X, X, X, X, X, X, X}},
5545 {{0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11, X, X, X, X}},
5546 {{1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11, X, X, X, X}},
5547 {{3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4, X}},
5548 {{4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2, X, X, X, X}},
5549 {{9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3, X}},
5550 {{11, 7, 4, 11, 4, 2, 2, 4, 0, X, X, X, X, X, X, X}},
5551 {{11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4, X, X, X, X}},
5552 {{2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9, X, X, X, X}},
5553 {{9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7, X}},
5554 {{3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10, X}},
5555 {{1, 10, 2, 8, 7, 4, X, X, X, X, X, X, X, X, X, X}},
5556 {{4, 9, 1, 4, 1, 7, 7, 1, 3, X, X, X, X, X, X, X}},
5557 {{4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1, X, X, X, X}},
5558 {{4, 0, 3, 7, 4, 3, X, X, X, X, X, X, X, X, X, X}},
5559 {{4, 8, 7, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5560 {{9, 10, 8, 10, 11, 8, X, X, X, X, X, X, X, X, X, X}},
5561 {{3, 0, 9, 3, 9, 11, 11, 9, 10, X, X, X, X, X, X, X}},
5562 {{0, 1, 10, 0, 10, 8, 8, 10, 11, X, X, X, X, X, X, X}},
5563 {{3, 1, 10, 11, 3, 10, X, X, X, X, X, X, X, X, X, X}},
5564 {{1, 2, 11, 1, 11, 9, 9, 11, 8, X, X, X, X, X, X, X}},
5565 {{3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9, X, X, X, X}},
5566 {{0, 2, 11, 8, 0, 11, X, X, X, X, X, X, X, X, X, X}},
5567 {{3, 2, 11, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5568 {{2, 3, 8, 2, 8, 10, 10, 8, 9, X, X, X, X, X, X, X}},
5569 {{9, 10, 2, 0, 9, 2, X, X, X, X, X, X, X, X, X, X}},
5570 {{2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8, X, X, X, X}},
5571 {{1, 10, 2, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5572 {{1, 3, 8, 9, 1, 8, X, X, X, X, X, X, X, X, X, X}},
5573 {{0, 9, 1, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5574 {{0, 3, 8, X, X, X, X, X, X, X, X, X, X, X, X, X}},
5575 {{X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X}}}};
5576
5577 // vertices of each line
5578 static constexpr ndarray<unsigned int, n_lines, 2> line_to_vertex_table = {
5579 {{{0, 1}},
5580 {{1, 2}},
5581 {{2, 3}},
5582 {{3, 0}},
5583 {{4, 5}},
5584 {{5, 6}},
5585 {{6, 7}},
5586 {{7, 4}},
5587 {{0, 4}},
5588 {{1, 5}},
5589 {{2, 6}},
5590 {{3, 7}}}};
5591
5592 // run dimension-independent code
5594 n_vertices,
5595 n_sub_vertices,
5596 n_configurations,
5597 n_lines,
5598 16>(cut_line_table,
5599 new_line_table,
5600 line_to_vertex_table,
5601 ls_values,
5602 points,
5603 mask,
5604 iso_level,
5605 tolerance,
5606 vertices,
5607 cells,
5608 write_back_cell_data);
5609 }
5610
5611} /* namespace GridTools */
5612
5613
5614// explicit instantiations
5615#include "grid/grid_tools.inst"
5616
void distribute(VectorType &vec) const
std::pair< std::vector< std::pair< int, int > >, std::vector< int > > query(const QueryType &queries)
DistributedTree(const MPI_Comm comm, const std::vector< BoundingBox< dim, Number > > &bounding_boxes)
BoundingBox< spacedim, Number > create_extended_relative(const Number relative_amount) const
BoundingBox< spacedim, Number > create_extended(const Number amount) const
void distribute_dofs(const FiniteElement< dim, spacedim > &fe)
types::global_dof_index n_dofs() const
Definition fe_q.h:552
const std::vector< std::set< typename Triangulation< dim, spacedim >::active_cell_iterator > > & get_vertex_to_cell_map() const
const std::vector< std::vector< Tensor< 1, spacedim > > > & get_vertex_to_cell_centers_directions() const
const RTree< std::pair< BoundingBox< spacedim >, typename Triangulation< dim, spacedim >::active_cell_iterator > > & get_locally_owned_cell_bounding_boxes_rtree() const
const Mapping< dim, spacedim > & get_mapping() const
const Triangulation< dim, spacedim > & get_triangulation() const
const RTree< std::pair< Point< spacedim >, unsigned int > > & get_used_vertices_rtree() const
const RTree< std::pair< BoundingBox< spacedim >, typename Triangulation< dim, spacedim >::active_cell_iterator > > & get_cell_bounding_boxes_rtree() const
static Quadrature< dim > create_quadrature_rule(const unsigned int n_subdivisions)
void process_cell(const typename DoFHandler< dim >::active_cell_iterator &cell, const VectorType &ls_vector, const double iso_level, std::vector< Point< dim > > &vertices, std::vector< CellData< dim==1 ? 1 :dim - 1 > > &cells) const
void process_sub_cell(const std::vector< value_type > &, const std::vector< Point< 1 > > &, const std::vector< unsigned int > &, const double, std::vector< Point< 1 > > &, std::vector< CellData< 1 > > &, const bool) const
void process(const DoFHandler< dim > &background_dof_handler, const VectorType &ls_vector, const double iso_level, std::vector< Point< dim > > &vertices, std::vector< CellData< dim==1 ? 1 :dim - 1 > > &cells) const
const Tensor< 2, 2, double > rotation_matrix
Point< 2 > operator()(const Point< 2 > &p) const
Rotate2d(const double angle)
Point< 3 > operator()(const Point< 3 > &p) const
Rotate3d(const Tensor< 1, 3, double > &axis, const double angle)
const Tensor< 2, 3, double > rotation_matrix
Scale(const double factor)
Point< spacedim > operator()(const Point< spacedim > p) const
Shift(const Tensor< 1, spacedim > &shift)
Point< spacedim > operator()(const Point< spacedim > p) const
const Tensor< 1, spacedim > shift
virtual std::unique_ptr< Manifold< dim, spacedim > > clone() const =0
Abstract base class for mapping classes.
Definition mapping.h:318
virtual Point< dim > transform_real_to_unit_cell(const typename Triangulation< dim, spacedim >::cell_iterator &cell, const Point< spacedim > &p) const =0
virtual boost::container::small_vector< Point< spacedim >, ReferenceCells::max_n_vertices< dim >() > get_vertices(const typename Triangulation< dim, spacedim >::cell_iterator &cell) const
Definition point.h:111
constexpr numbers::NumberTraits< Number >::real_type distance_square(const Point< dim, Number > &p) const
numbers::NumberTraits< Number >::real_type distance(const Point< dim, Number > &p) const
constexpr numbers::NumberTraits< Number >::real_type square() const
void initialize(const MatrixType &A, const AdditionalData &parameters=AdditionalData())
Quadrature< spacedim > compute_affine_transformation(const std::array< Point< spacedim >, dim+1 > &vertices) const
const Point< dim > & point(const unsigned int i) const
unsigned int size() const
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
size_type n() const
size_type n_rows() const
size_type n_cols() const
void copy_from(const size_type n_rows, const size_type n_cols, const ForwardIterator begin, const ForwardIterator end)
numbers::NumberTraits< Number >::real_type norm() const
IteratorState::IteratorStates state() const
virtual types::global_cell_index n_global_active_cells() const
unsigned int n_quads() const
void load_user_indices(const std::vector< unsigned int > &v)
virtual void clear()
bool all_reference_cells_are_hyper_cube() const
void clear_user_data()
face_iterator end_face() const
cell_iterator create_cell_iterator(const CellId &cell_id) const
cell_iterator begin(const unsigned int level=0) const
virtual MPI_Comm get_mpi_communicator() const
unsigned int n_lines() const
unsigned int n_raw_faces() const
virtual void create_triangulation(const std::vector< Point< spacedim > > &vertices, const std::vector< CellData< dim > > &cells, const SubCellData &subcelldata)
unsigned int n_active_cells() const
void refine_global(const unsigned int times=1)
const std::vector< Point< spacedim > > & get_vertices() const
unsigned int n_levels() const
cell_iterator end() const
virtual bool has_hanging_nodes() const
bool vertex_used(const unsigned int index) const
virtual void execute_coarsening_and_refinement()
unsigned int n_cells() const
const std::vector< bool > & get_used_vertices() const
Triangulation< dim, spacedim > & get_triangulation()
Signals signals
Definition tria.h:2606
unsigned int n_vertices() const
void save_user_indices(std::vector< unsigned int > &v) const
const std::map< std::pair< cell_iterator, unsigned int >, std::pair< std::pair< cell_iterator, unsigned int >, types::geometric_orientation > > & get_periodic_face_map() const
virtual std::vector< types::boundary_id > get_boundary_ids() const
active_face_iterator begin_active_face() const
active_cell_iterator begin_active(const unsigned int level=0) const
virtual MPI_Comm get_mpi_communicator() const override
Definition tria_base.cc:158
const ::internal::p4est::types< dim >::forest * get_p4est() const
Definition tria.cc:2221
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
constexpr bool running_in_debug_mode()
Definition config.h:76
#define DEAL_II_CXX20_REQUIRES(condition)
Definition config.h:249
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define DEAL_II_ASSERT_UNREACHABLE()
#define DEAL_II_NOT_IMPLEMENTED()
Point< 2 > second
Definition grid_out.cc:4640
Point< 2 > first
Definition grid_out.cc:4639
const unsigned int v0
const unsigned int v1
IteratorRange< active_cell_iterator > active_cell_iterators() const
IteratorRange< active_cell_iterator > active_cell_iterators() const
IteratorRange< cell_iterator > cell_iterators_on_level(const unsigned int level) const
static ::ExceptionBase & ExcNotImplemented()
static ::ExceptionBase & ExcNeedsMPI()
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertThrowMPI(error_code)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcNeedsCGAL()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcInvalidNumberOfPartitions(int arg1)
static ::ExceptionBase & ExcMessage(std::string arg1)
static ::ExceptionBase & ExcScalingFactorNotPositive(double arg1)
#define AssertThrow(cond, exc)
typename ActiveSelector::active_cell_iterator active_cell_iterator
typename IteratorSelector::line_iterator line_iterator
Definition tria.h:1726
LinearOperator< Range, Domain, Payload > linear_operator(const OperatorExemplar &, const Matrix &)
PackagedOperation< Range > constrained_right_hand_side(const AffineConstraints< typename Range::value_type > &constraints, const LinearOperator< Range, Domain, Payload > &linop, const Range &right_hand_side)
void make_sparsity_pattern(const DoFHandler< dim, spacedim > &dof_handler, SparsityPatternBase &sparsity_pattern, const AffineConstraints< number > &constraints={}, const bool keep_constrained_dofs=true, const types::subdomain_id subdomain_id=numbers::invalid_subdomain_id)
LinearOperator< Range, Domain, Payload > constrained_linear_operator(const AffineConstraints< typename Range::value_type > &constraints, const LinearOperator< Range, Domain, Payload > &linop)
@ update_values
Shape function values.
@ update_quadrature_points
Transformed quadrature points.
const Manifold< dim, spacedim > & get_manifold(const types::manifold_id number) const
void copy_boundary_to_manifold_id(Triangulation< dim, spacedim > &tria, const bool reset_boundary_ids=false)
void copy_material_to_manifold_id(Triangulation< dim, spacedim > &tria, const bool compute_face_ids=false)
void map_boundary_to_manifold_ids(const std::vector< types::boundary_id > &src_boundary_ids, const std::vector< types::manifold_id > &dst_manifold_ids, Triangulation< dim, spacedim > &tria, const std::vector< types::boundary_id > &reset_boundary_ids={})
virtual std::vector< types::manifold_id > get_manifold_ids() const
void set_manifold(const types::manifold_id number, const Manifold< dim, spacedim > &manifold_object)
void assign_co_dimensional_manifold_indicators(Triangulation< dim, spacedim > &tria, const std::function< types::manifold_id(const std::set< types::manifold_id > &)> &disambiguation_function=[](const std::set< types::manifold_id > &manifold_ids) { if(manifold_ids.size()==1) return *manifold_ids.begin();else return numbers::flat_manifold_id;}, bool overwrite_only_flat_manifold_ids=true)
void consistently_order_cells(std::vector< CellData< dim > > &cells)
Task< RT > new_task(const std::function< RT()> &function)
const unsigned int my_rank
Definition mpi.cc:917
std::vector< index_type > data
Definition mpi.cc:734
std::size_t size
Definition mpi.cc:733
const MPI_Comm comm
Definition mpi.cc:912
const unsigned int n_procs
Definition mpi.cc:923
std::tuple< BoundingBox< MeshType::space_dimension >, bool > compute_cell_predicate_bounding_box(const typename MeshType::cell_iterator &parent_cell, const std::function< bool(const typename MeshType::active_cell_iterator &)> &predicate)
bool fix_up_object(const Iterator &object)
double objective_function(const Iterator &object, const Point< spacedim > &object_mid_point)
void fix_up_faces(const typename ::Triangulation< dim, spacedim >::cell_iterator &cell, std::integral_constant< int, dim >, std::integral_constant< int, spacedim >)
Point< Iterator::AccessorType::space_dimension > get_face_midpoint(const Iterator &object, const unsigned int f, std::integral_constant< int, 1 >)
double minimal_diameter(const Iterator &object)
void laplace_solve(const SparseMatrix< double > &S, const AffineConstraints< double > &constraints, Vector< double > &u)
std::tuple< std::vector< unsigned int >, std::vector< unsigned int >, std::vector< unsigned int > > guess_owners_of_entities(const MPI_Comm comm, const std::vector< std::vector< BoundingBox< spacedim > > > &global_bboxes, const std::vector< T > &entities, const double tolerance)
DistributedComputePointLocationsInternal< dim, spacedim > distributed_compute_point_locations(const GridTools::Cache< dim, spacedim > &cache, const std::vector< Point< spacedim > > &points, const std::vector< std::vector< BoundingBox< spacedim > > > &global_bboxes, const std::vector< bool > &marked_vertices, const double tolerance, const bool perform_handshake, const bool enforce_unique_mapping=false)
std::vector< std::pair< typename Triangulation< dim, spacedim >::active_cell_iterator, Point< dim > > > find_all_locally_owned_active_cells_around_point(const Cache< dim, spacedim > &cache, const Point< spacedim > &point, typename Triangulation< dim, spacedim >::active_cell_iterator &cell_hint, const std::vector< bool > &marked_vertices, const double tolerance, const bool enforce_unique_mapping)
void process_sub_cell(const std::array< unsigned int, n_configurations > &cut_line_table, const ndarray< unsigned int, n_configurations, n_cols > &new_line_table, const ndarray< unsigned int, n_lines, 2 > &line_to_vertex_table, const std::vector< value_type > &ls_values, const std::vector< Point< dim > > &points, const std::vector< unsigned int > &mask, const double iso_level, const double tolerance, std::vector< Point< dim > > &vertices, std::vector< CellData< dim==1 ? 1 :dim - 1 > > &cells, const bool write_back_cell_data)
void set_subdomain_id_in_zorder_recursively(IT cell, unsigned int &current_proc_idx, unsigned int &current_cell_idx, const unsigned int n_active_cells, const unsigned int n_partitions)
DistributedComputeIntersectionLocationsInternal< structdim, spacedim > distributed_compute_intersection_locations(const Cache< dim, spacedim > &cache, const std::vector< std::vector< Point< spacedim > > > &intersection_requests, const std::vector< std::vector< BoundingBox< spacedim > > > &global_bboxes, const std::vector< bool > &marked_vertices, const double tolerance)
void get_face_connectivity_of_cells(const Triangulation< dim, spacedim > &triangulation, DynamicSparsityPattern &connectivity)
void delete_unused_vertices(std::vector< Point< spacedim > > &vertices, std::vector< CellData< dim > > &cells, SubCellData &subcelldata)
std::vector< BoundingBox< MeshType::space_dimension > > compute_mesh_predicate_bounding_box(const MeshType &mesh, const std::function< bool(const typename MeshType::active_cell_iterator &)> &predicate, const unsigned int refinement_level=0, const bool allow_merge=false, const unsigned int max_boxes=numbers::invalid_unsigned_int)
void scale(const double scaling_factor, Triangulation< dim, spacedim > &triangulation)
RTree< std::pair< BoundingBox< spacedim >, unsigned int > > build_global_description_tree(const std::vector< BoundingBox< spacedim > > &local_description, const MPI_Comm mpi_communicator)
void partition_triangulation_zorder(const unsigned int n_partitions, Triangulation< dim, spacedim > &triangulation, const bool group_siblings=true)
void rotate(const double angle, Triangulation< dim, spacedim > &triangulation)
return_type compute_point_locations(const Cache< dim, spacedim > &cache, const std::vector< Point< spacedim > > &points, const typename Triangulation< dim, spacedim >::active_cell_iterator &cell_hint=typename Triangulation< dim, spacedim >::active_cell_iterator())
unsigned int find_closest_vertex(const std::map< unsigned int, Point< spacedim > > &vertices, const Point< spacedim > &p)
void transform(const Transformation &transformation, Triangulation< dim, spacedim > &triangulation)
std::pair< typename MeshType< dim, spacedim >::active_cell_iterator, Point< dim > > find_active_cell_around_point(const Mapping< dim, spacedim > &mapping, const MeshType< dim, spacedim > &mesh, const Point< spacedim > &p, const std::vector< bool > &marked_vertices={}, const double tolerance=1.e-10)
std::vector< bool > get_locally_owned_vertices(const Triangulation< dim, spacedim > &triangulation)
std::vector< types::global_vertex_index > parallel_to_serial_vertex_indices(const Triangulation< dim, spacedim > &serial_tria, const Triangulation< dim, spacedim > &parallel_tria)
void regularize_corner_cells(Triangulation< dim, spacedim > &tria, const double limit_angle_fraction=.75)
void shift(const Tensor< 1, spacedim > &shift_vector, Triangulation< dim, spacedim > &triangulation)
std::map< unsigned int, types::global_vertex_index > compute_local_to_global_vertex_index_map(const Triangulation< dim, spacedim > &triangulation)
Point< Iterator::AccessorType::space_dimension > project_to_object(const Iterator &object, const Point< Iterator::AccessorType::space_dimension > &trial_point)
void laplace_transform(const std::map< unsigned int, Point< dim > > &new_points, Triangulation< dim > &tria, const Function< dim, double > *coefficient=nullptr, const bool solve_for_absolute_positions=false)
void partition_multigrid_levels(Triangulation< dim, spacedim > &triangulation)
void delete_duplicated_vertices(std::vector< Point< spacedim > > &all_vertices, std::vector< CellData< dim > > &cells, SubCellData &subcelldata, std::vector< unsigned int > &considered_vertices, const double tol=1e-12)
unsigned int find_closest_vertex_of_cell(const typename Triangulation< dim, spacedim >::active_cell_iterator &cell, const Point< spacedim > &position, const Mapping< dim, spacedim > &mapping=(ReferenceCells::get_hypercube< dim >() .template get_default_linear_mapping< spacedim >()))
std::map< unsigned int, std::set<::types::subdomain_id > > compute_vertices_with_ghost_neighbors(const Triangulation< dim, spacedim > &tria)
void collect_coinciding_vertices(const Triangulation< dim, spacedim > &tria, std::map< unsigned int, std::vector< unsigned int > > &coinciding_vertex_groups, std::map< unsigned int, unsigned int > &vertex_to_coinciding_vertex_group)
unsigned int count_cells_with_subdomain_association(const Triangulation< dim, spacedim > &triangulation, const types::subdomain_id subdomain)
return_type guess_point_owner(const std::vector< std::vector< BoundingBox< spacedim > > > &global_bboxes, const std::vector< Point< spacedim > > &points)
void partition_triangulation(const unsigned int n_partitions, Triangulation< dim, spacedim > &triangulation, const SparsityTools::Partitioner partitioner=SparsityTools::Partitioner::metis)
std::vector< std::set< typename Triangulation< dim, spacedim >::active_cell_iterator > > vertex_to_cell_map(const Triangulation< dim, spacedim > &triangulation)
return_type compute_point_locations_try_all(const Cache< dim, spacedim > &cache, const std::vector< Point< spacedim > > &points, const typename Triangulation< dim, spacedim >::active_cell_iterator &cell_hint=typename Triangulation< dim, spacedim >::active_cell_iterator())
std::vector< types::subdomain_id > get_subdomain_association(const Triangulation< dim, spacedim > &triangulation, const std::vector< CellId > &cell_ids)
void distort_random(const double factor, Triangulation< dim, spacedim > &triangulation, const bool keep_boundary=true, const unsigned int seed=boost::random::mt19937::default_seed)
std::vector< std::vector< Tensor< 1, spacedim > > > vertex_to_cell_centers_directions(const Triangulation< dim, spacedim > &mesh, const std::vector< std::set< typename Triangulation< dim, spacedim >::active_cell_iterator > > &vertex_to_cells)
std::vector< std::pair< typename MeshType< dim, spacedim >::active_cell_iterator, Point< dim > > > find_all_active_cells_around_point(const Mapping< dim, spacedim > &mapping, const MeshType< dim, spacedim > &mesh, const Point< spacedim > &p, const double tolerance, const std::pair< typename MeshType< dim, spacedim >::active_cell_iterator, Point< dim > > &first_cell, const std::vector< std::set< typename MeshType< dim, spacedim >::active_cell_iterator > > *vertex_to_cells=nullptr)
double diameter(const Triangulation< dim, spacedim > &tria)
void get_vertex_connectivity_of_cells_on_level(const Triangulation< dim, spacedim > &triangulation, const unsigned int level, DynamicSparsityPattern &connectivity)
std::vector< std::vector< BoundingBox< spacedim > > > exchange_local_bounding_boxes(const std::vector< BoundingBox< spacedim > > &local_bboxes, const MPI_Comm mpi_communicator)
std::map< unsigned int, Point< spacedim > > extract_used_vertices(const Triangulation< dim, spacedim > &container, const Mapping< dim, spacedim > &mapping=(ReferenceCells::get_hypercube< dim >() .template get_default_linear_mapping< spacedim >()))
return_type distributed_compute_point_locations(const GridTools::Cache< dim, spacedim > &cache, const std::vector< Point< spacedim > > &local_points, const std::vector< std::vector< BoundingBox< spacedim > > > &global_bboxes, const double tolerance=1e-10, const std::vector< bool > &marked_vertices={}, const bool enforce_unique_mapping=true)
@ valid
Iterator points to a valid object.
void create_laplace_matrix(const Mapping< dim, spacedim > &mapping, const DoFHandler< dim, spacedim > &dof, const Quadrature< dim > &q, MatrixType &matrix, const Function< spacedim, typename MatrixType::value_type > *const a=nullptr, const AffineConstraints< typename MatrixType::value_type > &constraints=AffineConstraints< typename MatrixType::value_type >())
void partition(const SparsityPattern &sparsity_pattern, const unsigned int n_partitions, std::vector< unsigned int > &partition_indices, const Partitioner partitioner=Partitioner::metis)
void reorder_hierarchical(const DynamicSparsityPattern &sparsity, std::vector< DynamicSparsityPattern::size_type > &new_indices)
@ grid_tools_compute_local_to_global_vertex_index_map2
GridTools::compute_local_to_global_vertex_index_map second tag.
Definition mpi_tags.h:105
@ grid_tools_compute_local_to_global_vertex_index_map
GridTools::compute_local_to_global_vertex_index_map.
Definition mpi_tags.h:103
T sum(const T &t, const MPI_Comm mpi_communicator)
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
Definition mpi.cc:103
T max(const T &t, const MPI_Comm mpi_communicator)
std::vector< T > all_gather(const MPI_Comm comm, const T &object_to_send)
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
Definition mpi.cc:118
std::size_t pack(const T &object, std::vector< char > &dest_buffer, const bool allow_compression=true)
Definition utilities.h:1352
constexpr T pow(const T base, const int iexp)
Definition utilities.h:966
std::vector< Integer > invert_permutation(const std::vector< Integer > &permutation)
Definition utilities.h:1670
constexpr double PI
Definition numbers.h:246
constexpr unsigned int invalid_unsigned_int
Definition types.h:228
constexpr types::manifold_id flat_manifold_id
Definition types.h:332
constexpr types::subdomain_id artificial_subdomain_id
Definition types.h:406
STL namespace.
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
inline ::VectorizedArray< Number, width > acos(const ::VectorizedArray< Number, width > &x)
std::uint64_t global_vertex_index
Definition types.h:55
unsigned int subdomain_id
Definition types.h:50
typename internal::ndarray::HelperArray< T, Ns... >::type ndarray
Definition ndarray.h:105
boost::geometry::index::rtree< LeafType, IndexType, IndexableGetter > RTree
Definition rtree.h:159
RTree< typename LeafTypeIterator::value_type, IndexType, IndexableGetter > pack_rtree(const LeafTypeIterator &begin, const LeafTypeIterator &end)
types::manifold_id manifold_id
Definition cell_data.h:125
std_cxx26::inplace_vector< unsigned int, ReferenceCells::max_n_vertices< structdim >()> vertices
Definition cell_data.h:84
types::material_id material_id
Definition cell_data.h:103
types::boundary_id boundary_id
Definition cell_data.h:114
static std_cxx20::ranges::iota_view< unsigned int, unsigned int > vertex_indices()
static void alternating_form_at_vertices(const Point< spacedim >(&vertices)[vertices_per_cell], Tensor< spacedim - dim, spacedim >(&forms)[vertices_per_cell])
std::map< unsigned int, std::vector< unsigned int > > communicate_indices(const std::vector< std::tuple< unsigned int, unsigned int, unsigned int > > &point_recv_components, const MPI_Comm comm) const
GridTools::internal::DistributedComputePointLocationsInternal< dim, spacedim > convert_to_distributed_compute_point_locations_internal(const unsigned int n_points_1D, const Triangulation< dim, spacedim > &tria, const Mapping< dim, spacedim > &mapping, std::vector< Quadrature< spacedim > > *mapped_quadratures_recv_comp=nullptr, const bool consistent_numbering_of_sender_and_receiver=false) const
std::vector< std::tuple< std::pair< int, int >, unsigned int, unsigned int, IntersectionType > > send_components
Definition grid_tools.h:849
std::vector< std::tuple< unsigned int, unsigned int, IntersectionType > > recv_components
Definition grid_tools.h:860
std::vector< std::tuple< unsigned int, unsigned int, unsigned int > > recv_components
Definition grid_tools.h:785
std::vector< std::tuple< std::pair< int, int >, unsigned int, unsigned int, Point< dim >, Point< spacedim >, unsigned int > > send_components
Definition grid_tools.h:760
std::vector< CellData< 2 > > boundary_quads
Definition cell_data.h:247
std::vector< CellData< 1 > > boundary_lines
Definition cell_data.h:231
boost::signals2::signal< unsigned int(const cell_iterator &, const ::CellStatus), CellWeightSum< unsigned int > > weight
Definition tria.h:2519
boost::signals2::signal< void()> pre_partition
Definition tria.h:2412