deal.II version GIT relicensing-7182-g951947b1c4 2026-11-26 10:30: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
shared_tria.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) 2015 - 2025 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
13#include <deal.II/base/mpi.h>
14#include <deal.II/base/mpi.templates.h>
16
19
22#include <deal.II/grid/tria.h>
25
27
28#include <type_traits>
29
30
32
33#ifdef DEAL_II_WITH_MPI
34namespace parallel
35{
36 namespace shared
37 {
38 template <int dim, int spacedim>
41 const MPI_Comm mpi_communicator,
42 const typename ::Triangulation<dim, spacedim>::MeshSmoothing
43 smooth_grid,
44 const bool allow_artificial_cells,
45 const Settings settings)
46 : ::parallel::TriangulationBase<dim, spacedim>(mpi_communicator,
47 smooth_grid,
48 false)
49 , settings(settings)
50 , allow_artificial_cells(allow_artificial_cells)
51 {
52 const auto partition_settings =
53 (partition_zoltan | partition_metis | partition_zorder |
54 partition_custom_signal) &
55 settings;
56 Assert(partition_settings == partition_auto ||
57 partition_settings == partition_metis ||
58 partition_settings == partition_zoltan ||
59 partition_settings == partition_zorder ||
60 partition_settings == partition_custom_signal,
61 ExcMessage("Settings must contain exactly one type of the active "
62 "cell partitioning scheme."));
63
64 if (settings & construct_multigrid_hierarchy)
65 Assert(allow_artificial_cells,
66 ExcMessage("construct_multigrid_hierarchy requires "
67 "allow_artificial_cells to be set to true."));
68 }
69
70
71
72 template <int dim, int spacedim>
74 bool Triangulation<dim, spacedim>::is_multilevel_hierarchy_constructed()
75 const
76 {
77 return (settings & construct_multigrid_hierarchy);
78 }
79
80
81
82 template <int dim, int spacedim>
84 void Triangulation<dim, spacedim>::partition()
85 {
86 if constexpr (running_in_debug_mode())
87 {
88 // Check that all meshes are the same (or at least have the same
89 // total number of active cells):
90 const unsigned int max_active_cells =
91 Utilities::MPI::max(this->n_active_cells(),
92 this->get_mpi_communicator());
93 Assert(
94 max_active_cells == this->n_active_cells(),
96 "A parallel::shared::Triangulation needs to be refined in the same "
97 "way on all processors, but the participating processors don't "
98 "agree on the number of active cells."));
99 }
100
101 auto partition_settings = (partition_zoltan | partition_metis |
102 partition_zorder | partition_custom_signal) &
103 settings;
104 if (partition_settings == partition_auto)
105# ifdef DEAL_II_TRILINOS_WITH_ZOLTAN
106 partition_settings = partition_zoltan;
107# elif defined DEAL_II_WITH_METIS
108 partition_settings = partition_metis;
109# else
110 partition_settings = partition_zorder;
111# endif
112
113 if (partition_settings == partition_zoltan)
114 {
115# ifndef DEAL_II_TRILINOS_WITH_ZOLTAN
116 AssertThrow(false,
118 "Choosing 'partition_zoltan' requires the library "
119 "to be compiled with support for Zoltan! "
120 "Instead, you might use 'partition_auto' to select "
121 "a partitioning algorithm that is supported "
122 "by your current configuration."));
123# else
125 this->n_subdomains, *this, SparsityTools::Partitioner::zoltan);
126# endif
127 }
128 else if (partition_settings == partition_metis)
129 {
130# ifndef DEAL_II_WITH_METIS
131 AssertThrow(false,
133 "Choosing 'partition_metis' requires the library "
134 "to be compiled with support for METIS! "
135 "Instead, you might use 'partition_auto' to select "
136 "a partitioning algorithm that is supported "
137 "by your current configuration."));
138# else
139 GridTools::partition_triangulation(this->n_subdomains,
140 *this,
142# endif
143 }
144 else if (partition_settings == partition_zorder)
145 {
146 GridTools::partition_triangulation_zorder(this->n_subdomains, *this);
147 }
148 else if (partition_settings == partition_custom_signal)
149 {
150 // User partitions mesh manually
151 }
152 else
153 {
155 }
156
157 // do not partition multigrid levels if user is
158 // defining a custom partition
159 if ((settings & construct_multigrid_hierarchy) &&
160 !(settings & partition_custom_signal))
162
163 mark_artificial_cells();
164
165 if constexpr (running_in_debug_mode())
166 {
167 {
168 // Assert that each cell is owned by a processor
169 const unsigned int n_my_cells = std::count_if(
170 this->begin_active(),
172 this->end()),
173 [](const auto &i) { return (i.is_locally_owned()); });
174
175 const unsigned int total_cells =
176 Utilities::MPI::sum(n_my_cells, this->get_mpi_communicator());
177 Assert(total_cells == this->n_active_cells(),
178 ExcMessage("Not all cells are assigned to a processor."));
179 }
180
181 // If running with multigrid, assert that each level
182 // cell is owned by a processor
183 if (settings & construct_multigrid_hierarchy)
184 {
185 const unsigned int n_my_cells =
186 std::count_if(this->begin(), this->end(), [](const auto &i) {
187 return (i.is_locally_owned_on_level());
188 });
189
190
191 const unsigned int total_cells =
192 Utilities::MPI::sum(n_my_cells, this->get_mpi_communicator());
193 Assert(total_cells == this->n_cells(),
194 ExcMessage("Not all cells are assigned to a processor."));
195 }
196 }
197 }
198
199
200
201 template <int dim, int spacedim>
203 void Triangulation<dim, spacedim>::mark_artificial_cells()
204 {
205 true_subdomain_ids_of_cells.resize(this->n_active_cells());
206
207 // loop over all cells and mark artificial:
209 spacedim>::active_cell_iterator
210 cell = this->begin_active(),
211 endc = this->end();
212
213 if (allow_artificial_cells)
214 {
215 // get active halo layer of (ghost) cells
216 // parallel::shared::Triangulation<dim>::
217 std::function<bool(
220 predicate = IteratorFilters::SubdomainEqualTo(this->my_subdomain);
221
222 const std::vector<typename parallel::shared::Triangulation<
223 dim,
224 spacedim>::active_cell_iterator>
225 active_halo_layer_vector =
227 predicate);
228 std::set<typename parallel::shared::Triangulation<dim, spacedim>::
229 active_cell_iterator>
230 active_halo_layer(active_halo_layer_vector.begin(),
231 active_halo_layer_vector.end());
232
233 for (unsigned int index = 0; cell != endc; cell++, index++)
234 {
235 // store original/true subdomain ids:
236 true_subdomain_ids_of_cells[index] = cell->subdomain_id();
237
238 if (cell->is_locally_owned() == false &&
239 active_halo_layer.find(cell) == active_halo_layer.end())
240 cell->set_subdomain_id(numbers::artificial_subdomain_id);
241 }
242
243 // loop over all cells in multigrid hierarchy and mark artificial:
244 if (settings & construct_multigrid_hierarchy)
245 {
246 true_level_subdomain_ids_of_cells.resize(this->n_levels());
247
248 std::function<bool(
250 cell_iterator &)>
252 for (unsigned int lvl = 0; lvl < this->n_levels(); ++lvl)
253 {
254 true_level_subdomain_ids_of_cells[lvl].resize(
255 this->n_cells(lvl));
256
257 const std::vector<typename parallel::shared::Triangulation<
258 dim,
259 spacedim>::cell_iterator>
260 level_halo_layer_vector =
262 *this, predicate, lvl);
263 std::set<typename parallel::shared::
264 Triangulation<dim, spacedim>::cell_iterator>
265 level_halo_layer(level_halo_layer_vector.begin(),
266 level_halo_layer_vector.end());
267
269 cell_iterator cell = this->begin(lvl),
270 endc = this->end(lvl);
271 for (unsigned int index = 0; cell != endc; cell++, index++)
272 {
273 // Store true level subdomain IDs before setting
274 // artificial
275 true_level_subdomain_ids_of_cells[lvl][index] =
276 cell->level_subdomain_id();
277
278 // for active cells, we must have knowledge of level
279 // subdomain ids of all neighbors to our subdomain, not
280 // just neighbors on the same level. if the cells
281 // subdomain id was not set to artitficial above, we will
282 // also keep its level subdomain id since it is either
283 // owned by this processor or in the ghost layer of the
284 // active mesh.
285 if (cell->is_active() &&
286 cell->subdomain_id() !=
288 continue;
289
290 // we must have knowledge of our parent in the hierarchy
291 if (cell->has_children())
292 {
293 bool keep_cell = false;
294 for (unsigned int c = 0; c < cell->n_children(); ++c)
295 if (cell->child(c)->level_subdomain_id() ==
296 this->my_subdomain)
297 {
298 keep_cell = true;
299 break;
300 }
301 if (keep_cell)
302 continue;
303 }
304
305 // we must have knowledge of our neighbors on the same
306 // level
307 if (!cell->is_locally_owned_on_level() &&
308 level_halo_layer.find(cell) != level_halo_layer.end())
309 continue;
310
311 // mark all other cells to artificial
312 cell->set_level_subdomain_id(
314 }
315 }
316 }
317 }
318 else
319 {
320 // just store true subdomain ids
321 for (unsigned int index = 0; cell != endc; cell++, index++)
322 true_subdomain_ids_of_cells[index] = cell->subdomain_id();
323 }
324 }
325
326
327
328 template <int dim, int spacedim>
330 bool Triangulation<dim, spacedim>::with_artificial_cells() const
331 {
332 return allow_artificial_cells;
333 }
334
335
336
337 template <int dim, int spacedim>
339 const std::vector<types::subdomain_id>
341 {
342 return true_subdomain_ids_of_cells;
343 }
344
345
346
347 template <int dim, int spacedim>
349 const std::vector<types::subdomain_id>
351 const unsigned int level) const
352 {
353 Assert(level < true_level_subdomain_ids_of_cells.size(),
355 Assert(true_level_subdomain_ids_of_cells[level].size() ==
356 this->n_cells(level),
358 return true_level_subdomain_ids_of_cells[level];
359 }
360
361
362
363 template <int dim, int spacedim>
365 void Triangulation<dim,
366 spacedim>::communicate_coarsening_and_refinement_flags()
367 {
368 // make sure that all refinement/coarsening flags are the same on all
369 // processes
370 {
371 // Obtain the type used to store the different possibilities
372 // a cell can be refined. This is a bit awkward because
373 // what `cell->refine_flag_set()` returns is a struct
374 // type, RefinementCase, which internally stores a
375 // std::uint8_t, which actually holds integers of
376 // enum type RefinementPossibilities<dim>::Possibilities.
377 // In the following, use the actual name of the enum, but
378 // make sure that it is in fact a `std::uint8_t` or
379 // equally sized type.
380 using int_type = std::underlying_type_t<
382 static_assert(sizeof(int_type) == sizeof(std::uint8_t),
383 "Internal type mismatch.");
384
385 std::vector<int_type> refinement_configurations(this->n_active_cells() *
386 2,
387 int_type(0));
388 for (const auto &cell : this->active_cell_iterators())
389 if (cell->is_locally_owned())
390 {
391 refinement_configurations[cell->active_cell_index() * 2 + 0] =
392 static_cast<int_type>(cell->refine_flag_set());
393 refinement_configurations[cell->active_cell_index() * 2 + 1] =
394 static_cast<int_type>(cell->coarsen_flag_set() ? 1 : 0);
395 }
396
397 Utilities::MPI::max(refinement_configurations,
398 this->get_mpi_communicator(),
399 refinement_configurations);
400
401 for (const auto &cell : this->active_cell_iterators())
402 {
403 cell->clear_refine_flag();
404 cell->clear_coarsen_flag();
405
406 Assert(
407 (refinement_configurations[cell->active_cell_index() * 2 + 0] >
408 0 ?
409 1 :
410 0) +
411 refinement_configurations[cell->active_cell_index() * 2 +
412 1] <=
413 1,
415 "Refinement/coarsening flags of cells are not consistent in parallel!"));
416
417 if (refinement_configurations[cell->active_cell_index() * 2 + 0] !=
418 0)
419 cell->set_refine_flag(RefinementCase<dim>(
420 refinement_configurations[cell->active_cell_index() * 2 + 0]));
421
422 if (refinement_configurations[cell->active_cell_index() * 2 + 1] >
423 0)
424 cell->set_coarsen_flag();
425 }
426 }
427 }
428
429
430
431 template <int dim, int spacedim>
433 bool Triangulation<dim, spacedim>::prepare_coarsening_and_refinement()
434 {
435 communicate_coarsening_and_refinement_flags();
436
437 return ::Triangulation<dim, spacedim>::
438 prepare_coarsening_and_refinement();
439 }
440
441
442
443 template <int dim, int spacedim>
445 void Triangulation<dim, spacedim>::execute_coarsening_and_refinement()
446 {
447 communicate_coarsening_and_refinement_flags();
448
450 partition();
451 this->update_number_cache();
452 }
453
454
455
456 template <int dim, int spacedim>
458 void Triangulation<dim, spacedim>::create_triangulation(
459 const std::vector<Point<spacedim>> &vertices,
460 const std::vector<CellData<dim>> &cells,
461 const SubCellData &subcelldata)
462 {
463 try
464 {
466 vertices, cells, subcelldata);
467 }
468 catch (
469 const typename ::Triangulation<dim, spacedim>::DistortedCellList
470 &)
471 {
472 // the underlying triangulation should not be checking for distorted
473 // cells
475 }
476 partition();
477 this->update_number_cache();
478 }
479
480
481
482 template <int dim, int spacedim>
484 void Triangulation<dim, spacedim>::create_triangulation(
485 const TriangulationDescription::Description<dim, spacedim>
486 &construction_data)
487 {
488 (void)construction_data;
489
491 }
492
493
494
495 template <int dim, int spacedim>
497 void Triangulation<dim, spacedim>::copy_triangulation(
498 const ::Triangulation<dim, spacedim> &other_tria)
499 {
500 Assert(
501 (dynamic_cast<
502 const ::parallel::DistributedTriangulationBase<dim, spacedim>
503 *>(&other_tria) == nullptr),
505 "Cannot use this function on parallel::distributed::Triangulation."));
506
508 other_tria);
509 partition();
510 this->update_number_cache();
511 }
512
513
514
515 template <int dim, int spacedim>
517 void Triangulation<dim, spacedim>::add_periodicity(
518 const std::vector<::GridTools::PeriodicFacePair<cell_iterator>>
519 &periodicity_vector)
520 {
522
523 if (!allow_artificial_cells)
524 return;
525
526 //
527 // The purpose of Triangulation::add_periodicity() is to ensure that
528 // periodic neighbors become part of the ghost layer (of the current
529 // rank). Unfortunately, we might have already marked such cells as
530 // artificial during the original call to mark_artificial_cells()
531 // which happens early during the call to partition() (and prior to
532 // this call to add_periodicity()).
533 //
534 // Let us simply restore the true subdomain ids and let
535 // mark_artificial_cells() reclassify all artificial cells as it can
536 // now see periodic neighbors.
537 //
538
539 for (const auto &cell : this->active_cell_iterators())
540 cell->set_subdomain_id(
541 true_subdomain_ids_of_cells[cell->active_cell_index()]);
542
543 if (settings & construct_multigrid_hierarchy)
544 for (unsigned int lvl = 0; lvl < this->n_levels(); ++lvl)
545 for (const auto &cell : this->cell_iterators_on_level(lvl))
546 cell->set_level_subdomain_id(
547 true_level_subdomain_ids_of_cells[lvl][cell->index()]);
548
549 mark_artificial_cells();
550 this->update_number_cache();
551 }
552 } // namespace shared
553} // namespace parallel
554
555#else
556
557namespace parallel
558{
559 namespace shared
560 {
561 template <int dim, int spacedim>
564 {
566 return true;
567 }
568
569
570
571 template <int dim, int spacedim>
574 const
575 {
576 return false;
577 }
578
579
580
581 template <int dim, int spacedim>
583 const std::vector<unsigned int>
585 {
587 return true_subdomain_ids_of_cells;
588 }
589
590
591
592 template <int dim, int spacedim>
594 const std::vector<unsigned int>
596 const unsigned int) const
597 {
599 return true_level_subdomain_ids_of_cells;
600 }
601 } // namespace shared
602} // namespace parallel
603
604
605#endif
606
607
608
609namespace internal
610{
611 namespace parallel
612 {
613 namespace shared
614 {
615 template <int dim, int spacedim>
618 : shared_tria(
619 dynamic_cast<
620 const ::parallel::shared::Triangulation<dim, spacedim> *>(
621 &tria))
622 {
623 if (shared_tria && shared_tria->with_artificial_cells())
624 {
625 // Save the current set of subdomain IDs, and set subdomain IDs
626 // to the "true" owner of each cell.
627 const std::vector<types::subdomain_id> &true_subdomain_ids =
628 shared_tria->get_true_subdomain_ids_of_cells();
629
630 saved_subdomain_ids.resize(shared_tria->n_active_cells());
631 for (const auto &cell : shared_tria->active_cell_iterators())
632 {
633 const unsigned int index = cell->active_cell_index();
634 saved_subdomain_ids[index] = cell->subdomain_id();
635 cell->set_subdomain_id(true_subdomain_ids[index]);
636 }
637 }
638 }
639
640
641
642 template <int dim, int spacedim>
645 {
646 if (shared_tria && shared_tria->with_artificial_cells())
647 {
648 // Undo the subdomain modification.
649 for (const auto &cell : shared_tria->active_cell_iterators())
650 {
651 const unsigned int index = cell->active_cell_index();
652 cell->set_subdomain_id(saved_subdomain_ids[index]);
653 }
654 }
655 }
656 } // namespace shared
657 } // namespace parallel
658} // namespace internal
659
660
661/*-------------- Explicit Instantiations -------------------------------*/
662#include "distributed/shared_tria.inst"
663
*  iterator end()
*  *  iterator begin()
Definition point.h:111
cell_iterator end() const
const ObserverPointer< const ::parallel::shared::Triangulation< dim, spacedim > > shared_tria
TemporarilyRestoreSubdomainIds(const Triangulation< dim, spacedim > &tria)
types::subdomain_id my_subdomain
Definition tria_base.h:344
virtual void copy_triangulation(const ::Triangulation< dim, spacedim > &old_tria) override
Definition tria_base.cc:65
virtual void execute_coarsening_and_refinement() override
virtual void add_periodicity(const std::vector<::GridTools::PeriodicFacePair< cell_iterator > > &) override
typename ::Triangulation< dim, spacedim >::active_cell_iterator active_cell_iterator
const std::vector< types::subdomain_id > & get_true_subdomain_ids_of_cells() const
typename ::Triangulation< dim, spacedim >::cell_iterator cell_iterator
const std::vector< types::subdomain_id > & get_true_level_subdomain_ids_of_cells(const unsigned int level) const
virtual bool is_multilevel_hierarchy_constructed() const override
virtual void create_triangulation(const std::vector< Point< spacedim > > &vertices, const std::vector< CellData< dim > > &cells, const SubCellData &subcelldata) override
#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()
unsigned int level
Definition grid_out.cc:4642
#define Assert(cond, exc)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
std::size_t size
Definition mpi.cc:733
void partition_triangulation_zorder(const unsigned int n_partitions, Triangulation< dim, spacedim > &triangulation, const bool group_siblings=true)
std::vector< typename MeshType::active_cell_iterator > compute_active_cell_halo_layer(const MeshType &mesh, const std::function< bool(const typename MeshType::active_cell_iterator &)> &predicate)
void partition_multigrid_levels(Triangulation< dim, spacedim > &triangulation)
std::vector< typename MeshType::cell_iterator > compute_cell_halo_layer_on_level(const MeshType &mesh, const std::function< bool(const typename MeshType::cell_iterator &)> &predicate, const unsigned int level)
void partition_triangulation(const unsigned int n_partitions, Triangulation< dim, spacedim > &triangulation, const SparsityTools::Partitioner partitioner=SparsityTools::Partitioner::metis)
T sum(const T &t, const MPI_Comm mpi_communicator)
T max(const T &t, const MPI_Comm mpi_communicator)
constexpr types::subdomain_id artificial_subdomain_id
Definition types.h:406
STL namespace.