Reference documentation for deal.II version 9.1.0-pre
tria.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2008 - 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_distributed_tria_h
17 #define dealii_distributed_tria_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #include <deal.II/base/smartpointer.h>
23 #include <deal.II/base/subscriptor.h>
24 #include <deal.II/base/template_constraints.h>
25 
26 #include <deal.II/distributed/p4est_wrappers.h>
27 #include <deal.II/distributed/tria_base.h>
28 
29 #include <deal.II/grid/tria.h>
30 
31 #include <boost/range/iterator_range.hpp>
32 
33 #include <functional>
34 #include <list>
35 #include <set>
36 #include <tuple>
37 #include <type_traits>
38 #include <utility>
39 #include <vector>
40 
41 #ifdef DEAL_II_WITH_MPI
42 # include <mpi.h>
43 #endif
44 
45 #ifdef DEAL_II_WITH_P4EST
46 # include <p4est.h>
47 # include <p4est_connectivity.h>
48 # include <p4est_ghost.h>
49 # include <p8est.h>
50 # include <p8est_connectivity.h>
51 # include <p8est_ghost.h>
52 #endif
53 
54 
55 DEAL_II_NAMESPACE_OPEN
56 
57 #ifdef DEAL_II_WITH_P4EST
58 
59 namespace internal
60 {
61  namespace DoFHandlerImplementation
62  {
63  namespace Policy
64  {
65  template <typename>
67  }
68  } // namespace DoFHandlerImplementation
69 } // namespace internal
70 
71 namespace FETools
72 {
73  namespace internal
74  {
75  template <int, int, class>
76  class ExtrapolateImplementation;
77  }
78 } // namespace FETools
79 
80 // forward declaration of the data type for periodic face pairs
81 namespace GridTools
82 {
83  template <typename CellIterator>
85 }
86 
87 namespace parallel
88 {
89  namespace distributed
90  {
247  template <int dim, int spacedim = dim>
248  class Triangulation : public ::parallel::Triangulation<dim, spacedim>
249  {
250  public:
268  using cell_iterator =
269  typename ::Triangulation<dim, spacedim>::cell_iterator;
270 
289  using active_cell_iterator =
290  typename ::Triangulation<dim, spacedim>::active_cell_iterator;
291 
292  using CellStatus =
293  typename ::Triangulation<dim, spacedim>::CellStatus;
294 
299  enum Settings
300  {
304  default_setting = 0x0,
313  mesh_reconstruction_after_repartitioning = 0x1,
320  construct_multigrid_hierarchy = 0x2,
326  no_automatic_repartitioning = 0x4
327  };
328 
329 
330 
363  MPI_Comm mpi_communicator,
364  const typename ::Triangulation<dim, spacedim>::MeshSmoothing
365  smooth_grid = (::Triangulation<dim, spacedim>::none),
366  const Settings settings = default_setting);
367 
371  virtual ~Triangulation() override;
372 
379  virtual void
380  clear() override;
381 
391  virtual void
392  copy_triangulation(
393  const ::Triangulation<dim, spacedim> &other_tria) override;
394 
404  virtual void
405  create_triangulation(const std::vector<Point<spacedim>> &vertices,
406  const std::vector<CellData<dim>> & cells,
407  const SubCellData &subcelldata) override;
408 
438  virtual void
439  execute_coarsening_and_refinement() override;
440 
447  virtual bool
448  prepare_coarsening_and_refinement() override;
449 
490  void
491  repartition();
492 
545  void
546  communicate_locally_moved_vertices(
547  const std::vector<bool> &vertex_locally_moved);
548 
549 
565  virtual bool
566  has_hanging_nodes() const override;
567 
571  virtual std::size_t
572  memory_consumption() const override;
573 
579  virtual std::size_t
580  memory_consumption_p4est() const;
581 
589  void
590  write_mesh_vtk(const char *file_basename) const;
591 
596  unsigned int
597  get_checksum() const;
598 
606  void
607  save(const char *filename) const;
608 
627  void
628  load(const char *filename, const bool autopartition = true);
629 
738  unsigned int
739  register_data_attach(
740  const std::function<std::vector<char>(const cell_iterator &,
741  const CellStatus)> &pack_callback,
742  const bool returns_variable_size_data);
743 
792  void
793  notify_ready_to_unpack(
794  const unsigned int handle,
795  const std::function<void(
796  const cell_iterator &,
797  const CellStatus,
798  const boost::iterator_range<std::vector<char>::const_iterator> &)>
799  &unpack_callback);
800 
807  const std::vector<types::global_dof_index> &
808  get_p4est_tree_to_coarse_cell_permutation() const;
809 
815  const std::vector<types::global_dof_index> &
816  get_coarse_cell_to_p4est_tree_permutation() const;
817 
837  virtual void
838  add_periodicity(
839  const std::vector<::GridTools::PeriodicFacePair<cell_iterator>> &)
840  override;
841 
842 
843  private:
848  virtual void
849  update_number_cache() override;
850 
855 
860 
866  typename ::internal::p4est::types<dim>::connectivity *connectivity;
867 
872  typename ::internal::p4est::types<dim>::forest *parallel_forest;
873 
878  typename ::internal::p4est::types<dim>::ghost *parallel_ghost;
879 
886  {
891  unsigned int n_attached_data_sets;
892 
898 
899  using pack_callback_t = std::function<std::vector<char>(
901  CellStatus)>;
902 
907  std::vector<pack_callback_t> pack_callbacks_fixed;
908  std::vector<pack_callback_t> pack_callbacks_variable;
909  };
910 
911  CellAttachedData cell_attached_data;
912 
919  using quadrant_cell_relation_t = typename std::tuple<
920  typename ::internal::p4est::types<dim>::quadrant *,
921  CellStatus,
923 
932  std::vector<quadrant_cell_relation_t> local_quadrant_cell_relations;
933 
943  void
944  update_quadrant_cell_relations();
945 
956  {
957  public:
958  DataTransfer(MPI_Comm mpi_communicator);
959 
969  void
970  pack_data(
971  const std::vector<quadrant_cell_relation_t> &quad_cell_relations,
972  const std::vector<typename CellAttachedData::pack_callback_t>
973  &pack_callbacks_fixed,
974  const std::vector<typename CellAttachedData::pack_callback_t>
975  &pack_callbacks_variable);
976 
989  void
990  execute_transfer(
991  const typename ::internal::p4est::types<dim>::forest
992  *parallel_forest,
993  const typename ::internal::p4est::types<dim>::gloidx
994  *previous_global_first_quadrant);
995 
1003  void
1004  unpack_cell_status(
1005  std::vector<quadrant_cell_relation_t> &quad_cell_relations) const;
1006 
1019  void
1020  unpack_data(
1021  const std::vector<quadrant_cell_relation_t> &quad_cell_relations,
1022  const unsigned int handle,
1023  const std::function<void(
1024  const typename ::Triangulation<dim, spacedim>::cell_iterator
1025  &,
1026  const typename ::Triangulation<dim, spacedim>::CellStatus &,
1027  const boost::iterator_range<std::vector<char>::const_iterator> &)>
1028  &unpack_callback) const;
1029 
1044  void
1045  save(const typename ::internal::p4est::types<dim>::forest
1046  * parallel_forest,
1047  const char *filename) const;
1048 
1067  void
1068  load(const typename ::internal::p4est::types<dim>::forest
1069  * parallel_forest,
1070  const char * filename,
1071  const unsigned int n_attached_deserialize_fixed,
1072  const unsigned int n_attached_deserialize_variable);
1073 
1080  void
1081  clear();
1082 
1083  private:
1084  MPI_Comm mpi_communicator;
1085 
1090 
1101  std::vector<unsigned int> sizes_fixed_cumulative;
1102 
1107  std::vector<char> src_data_fixed;
1108  std::vector<char> dest_data_fixed;
1109 
1114  std::vector<int> src_sizes_variable;
1115  std::vector<int> dest_sizes_variable;
1116  std::vector<char> src_data_variable;
1117  std::vector<char> dest_data_variable;
1118  };
1119 
1120  DataTransfer data_transfer;
1121 
1135  std::vector<types::global_dof_index>
1137  std::vector<types::global_dof_index>
1138  p4est_tree_to_coarse_cell_permutation;
1139 
1144  typename ::internal::p4est::types<dim>::tree *
1145  init_tree(const int dealii_coarse_cell_index) const;
1146 
1151  void
1152  setup_coarse_cell_to_p4est_tree_permutation();
1153 
1160  void
1161  copy_new_triangulation_to_p4est(std::integral_constant<int, 2>);
1162  void
1163  copy_new_triangulation_to_p4est(std::integral_constant<int, 3>);
1164 
1169  void
1170  copy_local_forest_to_triangulation();
1171 
1185  std::vector<unsigned int>
1186  get_cell_weights() const;
1187 
1195  virtual std::map<unsigned int, std::set<::types::subdomain_id>>
1196  compute_vertices_with_ghost_neighbors() const override;
1197 
1207  std::vector<bool>
1208  mark_locally_active_vertices_on_level(const int level) const;
1209 
1210  template <typename>
1211  friend class ::internal::DoFHandlerImplementation::Policy::
1212  ParallelDistributed;
1213 
1214  template <int, int, class>
1215  friend class ::FETools::internal::ExtrapolateImplementation;
1216  };
1217 
1218 
1224  template <int spacedim>
1225  class Triangulation<1, spacedim>
1226  : public ::parallel::Triangulation<1, spacedim>
1227  {
1228  public:
1233  {
1234  default_setting = 0x0,
1235  mesh_reconstruction_after_repartitioning = 0x1,
1236  construct_multigrid_hierarchy = 0x2
1237  };
1238 
1243  Triangulation(
1244  MPI_Comm mpi_communicator,
1245  const typename ::Triangulation<1, spacedim>::MeshSmoothing
1246  smooth_grid = (::Triangulation<1, spacedim>::none),
1247  const Settings settings = default_setting);
1248 
1252  virtual ~Triangulation() override;
1253 
1260  const std::vector<types::global_dof_index> &
1261  get_p4est_tree_to_coarse_cell_permutation() const;
1262 
1296  void
1297  communicate_locally_moved_vertices(
1298  const std::vector<bool> &vertex_locally_moved);
1299 
1304  void
1305  load(const char *filename, const bool autopartition = true);
1306 
1311  void
1312  save(const char *filename) const;
1313 
1318  unsigned int
1319  register_data_attach(
1320  const std::function<std::vector<char>(
1321  const typename ::Triangulation<1, spacedim>::cell_iterator &,
1322  const typename ::Triangulation<1, spacedim>::CellStatus)>
1323  & pack_callback,
1324  const bool returns_variable_size_data);
1325 
1330  void
1331  notify_ready_to_unpack(
1332  const unsigned int handle,
1333  const std::function<void(
1334  const typename ::Triangulation<1, spacedim>::cell_iterator &,
1335  const typename ::Triangulation<1, spacedim>::CellStatus,
1336  const boost::iterator_range<std::vector<char>::const_iterator> &)>
1337  &unpack_callback);
1338 
1343  std::vector<types::global_dof_index>
1345  std::vector<types::global_dof_index>
1346  p4est_tree_to_coarse_cell_permutation;
1347 
1348 
1349  // TODO: The following variable should really be private, but it is used
1350  // in dof_handler_policy.cc ...
1355 
1360  virtual std::map<unsigned int, std::set<::types::subdomain_id>>
1361  compute_vertices_with_ghost_neighbors() const override;
1362 
1367  virtual std::map<unsigned int, std::set<::types::subdomain_id>>
1368  compute_level_vertices_with_ghost_neighbors(
1369  const unsigned int level) const;
1370 
1375  virtual std::vector<bool>
1376  mark_locally_active_vertices_on_level(const unsigned int level) const;
1377  };
1378  } // namespace distributed
1379 } // namespace parallel
1380 
1381 
1382 #else // DEAL_II_WITH_P4EST
1383 
1384 namespace parallel
1385 {
1386  namespace distributed
1387  {
1399  template <int dim, int spacedim = dim>
1400  class Triangulation : public ::parallel::Triangulation<dim, spacedim>
1401  {
1402  public:
1407  Triangulation() = delete;
1408  };
1409  } // namespace distributed
1410 } // namespace parallel
1411 
1412 
1413 #endif
1414 
1415 
1416 DEAL_II_NAMESPACE_CLOSE
1417 
1418 #endif
Definition: tria.h:71
typename::internal::p4est::types< dim >::connectivity * connectivity
Definition: tria.h:866
std::vector< types::global_dof_index > coarse_cell_to_p4est_tree_permutation
Definition: tria.h:1344
typename std::tuple< typename::internal::p4est::types< dim >::quadrant *, CellStatus, cell_iterator > quadrant_cell_relation_t
Definition: tria.h:922
typename::internal::p4est::types< dim >::ghost * parallel_ghost
Definition: tria.h:878
typename::internal::p4est::types< dim >::forest * parallel_forest
Definition: tria.h:872
Definition: tria.h:81
std::vector< pack_callback_t > pack_callbacks_fixed
Definition: tria.h:907
std::vector< quadrant_cell_relation_t > local_quadrant_cell_relations
Definition: tria.h:932
std::vector< unsigned int > sizes_fixed_cumulative
Definition: tria.h:1101
typename::Triangulation< dim, spacedim >::cell_iterator cell_iterator
Definition: tria.h:269
std::vector< types::global_dof_index > coarse_cell_to_p4est_tree_permutation
Definition: tria.h:1136