Reference documentation for deal.II version 8.4.2
dof_handler.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2016 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 at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__dof_handler_h
17 #define dealii__dof_handler_h
18 
19 
20 
21 #include <deal.II/base/config.h>
22 #include <deal.II/base/exceptions.h>
23 #include <deal.II/base/smartpointer.h>
24 #include <deal.II/base/index_set.h>
25 #include <deal.II/base/iterator_range.h>
26 #include <deal.II/base/std_cxx11/shared_ptr.h>
27 #include <deal.II/dofs/block_info.h>
28 #include <deal.II/dofs/dof_iterator_selector.h>
29 #include <deal.II/dofs/number_cache.h>
30 #include <deal.II/dofs/dof_faces.h>
31 #include <deal.II/dofs/dof_levels.h>
32 #include <deal.II/dofs/function_map.h>
33 
34 #include <boost/serialization/split_member.hpp>
35 
36 #include <vector>
37 #include <map>
38 #include <set>
39 
40 DEAL_II_NAMESPACE_OPEN
41 
42 namespace internal
43 {
44  namespace DoFHandler
45  {
46  struct Implementation;
47 
48  namespace Policy
49  {
50  template <int dim, int spacedim> class PolicyBase;
51  struct Implementation;
52  }
53  }
54 
55  namespace DoFAccessor
56  {
57  struct Implementation;
58  }
59 
60  namespace DoFCellAccessor
61  {
62  struct Implementation;
63  }
64 }
65 
66 
183 template <int dim, int spacedim=dim>
184 class DoFHandler : public Subscriptor
185 {
186  typedef ::internal::DoFHandler::Iterators<DoFHandler<dim,spacedim>, false> ActiveSelector;
187  typedef ::internal::DoFHandler::Iterators<DoFHandler<dim,spacedim>, true> LevelSelector;
188 public:
189  typedef typename ActiveSelector::CellAccessor cell_accessor;
190  typedef typename ActiveSelector::FaceAccessor face_accessor;
191 
192  typedef typename ActiveSelector::line_iterator line_iterator;
193  typedef typename ActiveSelector::active_line_iterator active_line_iterator;
194 
195  typedef typename ActiveSelector::quad_iterator quad_iterator;
196  typedef typename ActiveSelector::active_quad_iterator active_quad_iterator;
197 
198  typedef typename ActiveSelector::hex_iterator hex_iterator;
199  typedef typename ActiveSelector::active_hex_iterator active_hex_iterator;
200 
221  typedef typename ActiveSelector::active_cell_iterator active_cell_iterator;
222 
249  typedef typename ActiveSelector::cell_iterator cell_iterator;
250 
251  typedef typename ActiveSelector::face_iterator face_iterator;
252  typedef typename ActiveSelector::active_face_iterator active_face_iterator;
253 
254  typedef typename LevelSelector::CellAccessor level_cell_accessor;
255  typedef typename LevelSelector::FaceAccessor level_face_accessor;
256 
257  typedef typename LevelSelector::cell_iterator level_cell_iterator;
258  typedef typename LevelSelector::face_iterator level_face_iterator;
259 
260 
264  typedef typename ::FunctionMap<spacedim>::type FunctionMap;
265 
269  static const unsigned int dimension = dim;
270 
274  static const unsigned int space_dimension = spacedim;
275 
285  static const types::global_dof_index invalid_dof_index = numbers::invalid_dof_index;
286 
296  static const unsigned int default_fe_index = 0;
297 
303  DoFHandler ();
304 
309 
313  virtual ~DoFHandler ();
314 
319  void initialize(const Triangulation<dim,spacedim> &tria,
320  const FiniteElement<dim,spacedim> &fe);
321 
342  virtual void distribute_dofs (const FiniteElement<dim,spacedim> &fe);
343 
350  virtual void distribute_mg_dofs (const FiniteElement<dim, spacedim> &fe);
351 
357  bool has_level_dofs() const;
358 
373  bool has_active_dofs() const;
374 
381  void initialize_local_block_info();
382 
388  virtual void clear ();
389 
419  void renumber_dofs (const std::vector<types::global_dof_index> &new_numbers);
420 
425  void renumber_dofs (const unsigned int level,
426  const std::vector<types::global_dof_index> &new_numbers);
427 
456  unsigned int max_couplings_between_dofs () const;
457 
470  unsigned int max_couplings_between_boundary_dofs () const;
471 
472  /*--------------------------------------*/
473 
478  /*
479  * @{
480  */
481 
485  cell_iterator begin (const unsigned int level = 0) const;
486 
499  active_cell_iterator begin_active(const unsigned int level = 0) const;
500 
505  cell_iterator end () const;
506 
511  cell_iterator end (const unsigned int level) const;
512 
518  active_cell_iterator end_active (const unsigned int level) const;
519 
520 
525  level_cell_iterator begin_mg (const unsigned int level = 0) const;
526 
531  level_cell_iterator end_mg (const unsigned int level) const;
532 
537  level_cell_iterator end_mg () const;
538 
553  IteratorRange<cell_iterator> cell_iterators () const;
554 
596  IteratorRange<active_cell_iterator> active_cell_iterators () const;
597 
609  IteratorRange<level_cell_iterator> mg_cell_iterators () const;
610 
626  IteratorRange<cell_iterator> cell_iterators_on_level (const unsigned int level) const;
627 
643  IteratorRange<active_cell_iterator> active_cell_iterators_on_level (const unsigned int level) const;
644 
661  IteratorRange<level_cell_iterator> mg_cell_iterators_on_level (const unsigned int level) const;
662 
663  /*
664  * @}
665  */
666 
667 
668  /*---------------------------------------*/
669 
670 
687  types::global_dof_index n_dofs () const;
688 
696  types::global_dof_index n_dofs (const unsigned int level) const;
697 
701  types::global_dof_index n_boundary_dofs () const;
702 
711  n_boundary_dofs (const FunctionMap &boundary_ids) const;
712 
718  n_boundary_dofs (const std::set<types::boundary_id> &boundary_ids) const;
719 
735  const BlockInfo &block_info() const;
736 
737 
751  unsigned int n_locally_owned_dofs() const;
752 
758  const IndexSet &locally_owned_dofs() const;
759 
764  const IndexSet &locally_owned_mg_dofs(const unsigned int level) const;
765 
766 
775  const std::vector<IndexSet> &
776  locally_owned_dofs_per_processor () const;
777 
778  const std::vector<IndexSet> &
779  locally_owned_mg_dofs_per_processor (const unsigned int level) const;
780 
793  const std::vector<types::global_dof_index> &
794  n_locally_owned_dofs_per_processor () const;
795 
799  const FiniteElement<dim,spacedim> &get_fe () const;
800 
806  const Triangulation<dim,spacedim> &get_tria () const DEAL_II_DEPRECATED;
807 
811  const Triangulation<dim,spacedim> &get_triangulation () const;
812 
821  virtual std::size_t memory_consumption () const;
822 
827  template <class Archive>
828  void save (Archive &ar, const unsigned int version) const;
829 
834  template <class Archive>
835  void load (Archive &ar, const unsigned int version);
836 
837  BOOST_SERIALIZATION_SPLIT_MEMBER()
838 
845  DeclException0 (ExcRenumberingIncomplete);
850  DeclException0 (ExcGridsDoNotMatch);
855  DeclException0 (ExcInvalidBoundaryIndicator);
860  DeclException1 (ExcNewNumbersNotConsecutive,
861  types::global_dof_index,
862  << "The given list of new dof indices is not consecutive: "
863  << "the index " << arg1 << " does not exist.");
868  DeclException1 (ExcInvalidLevel,
869  int,
870  << "The given level " << arg1
871  << " is not in the valid range!");
876  DeclException0 (ExcFacesHaveNoLevel);
881  DeclException1 (ExcEmptyLevel,
882  int,
883  << "You tried to do something on level " << arg1
884  << ", but this level is empty.");
885 
886 
887 private:
894  DoFHandler (const DoFHandler &);
895 
902  DoFHandler &operator = (const DoFHandler &);
903 
904 
908  BlockInfo block_info_object;
909 
913  SmartPointer<const Triangulation<dim,spacedim>,DoFHandler<dim,spacedim> >
914  tria;
915 
924  SmartPointer<const FiniteElement<dim,spacedim>,DoFHandler<dim,spacedim> >
925  selected_fe;
926 
931  std_cxx11::shared_ptr<::internal::DoFHandler::Policy::PolicyBase<dim,spacedim> > policy;
932 
940  ::internal::DoFHandler::NumberCache number_cache;
941 
945  std::vector<::internal::DoFHandler::NumberCache> mg_number_cache;
946 
954  {
955  public:
959  MGVertexDoFs ();
960 
964  ~MGVertexDoFs ();
965 
971  void init (const unsigned int coarsest_level,
972  const unsigned int finest_level,
973  const unsigned int dofs_per_vertex);
974 
978  unsigned int get_coarsest_level () const;
979 
983  unsigned int get_finest_level () const;
984 
990  get_index (const unsigned int level,
991  const unsigned int dof_number) const;
992 
997  void set_index (const unsigned int level,
998  const unsigned int dof_number,
999  const types::global_dof_index index);
1000 
1004  DeclException0 (ExcNoMemory);
1005 
1006  private:
1010  unsigned int coarsest_level;
1011 
1015  unsigned int finest_level;
1016 
1022 
1029  };
1030 
1031  void clear_mg_space ();
1032 
1036  void clear_space ();
1037 
1038  void reserve_space ();
1039 
1040  template <int structdim>
1041  types::global_dof_index get_dof_index (const unsigned int obj_level,
1042  const unsigned int obj_index,
1043  const unsigned int fe_index,
1044  const unsigned int local_index) const;
1045 
1046  template<int structdim>
1047  void set_dof_index (const unsigned int obj_level,
1048  const unsigned int obj_index,
1049  const unsigned int fe_index,
1050  const unsigned int local_index,
1051  const types::global_dof_index global_index) const;
1052 
1056  std::vector<types::global_dof_index> vertex_dofs;
1057 
1062  std::vector<MGVertexDoFs> mg_vertex_dofs;
1063 
1068  std::vector<::internal::DoFHandler::DoFLevel<dim>*> levels;
1069 
1070  std::vector<::internal::DoFHandler::DoFLevel<dim>*> mg_levels;
1071 
1078 
1080 
1084  template <int, class, bool> friend class DoFAccessor;
1085  template <class, bool> friend class DoFCellAccessor;
1086  friend struct ::internal::DoFAccessor::Implementation;
1087  friend struct ::internal::DoFCellAccessor::Implementation;
1088 
1089  friend struct ::internal::DoFHandler::Implementation;
1090  friend struct ::internal::DoFHandler::Policy::Implementation;
1091 };
1092 
1093 
1094 
1095 
1096 /* -------------- declaration of explicit specializations ------------- */
1097 
1098 #ifndef DOXYGEN
1099 
1102 template <> types::global_dof_index DoFHandler<1>::n_boundary_dofs (const std::set<types::boundary_id> &) const;
1103 
1104 template <> void DoFHandler<1>::renumber_dofs(unsigned int,const std::vector<types::global_dof_index> &new_numbers);
1105 template <> void DoFHandler<2>::renumber_dofs(unsigned int,const std::vector<types::global_dof_index> &new_numbers);
1106 template <> void DoFHandler<3>::renumber_dofs(unsigned int,const std::vector<types::global_dof_index> &new_numbers);
1107 
1108 
1109 /* ----------------------- Inline functions ---------------------------------- */
1110 
1111 
1112 template <int dim, int spacedim>
1113 inline
1114 bool
1116 {
1117  return mg_number_cache.size()>0;
1118 }
1119 
1120 template <int dim, int spacedim>
1121 inline
1122 bool
1124 {
1125  return number_cache.n_global_dofs>0;
1126 }
1127 
1128 template <int dim, int spacedim>
1129 inline
1132 {
1133  return number_cache.n_global_dofs;
1134 }
1135 
1136 template<int dim, int spacedim>
1137 inline
1139 {
1140  Assert(has_level_dofs(), ExcMessage("n_dofs(level) can only be called after distribute_mg_dofs()"));
1141  Assert (level < mg_number_cache.size (), ExcInvalidLevel (level));
1142  return mg_number_cache[level].n_global_dofs;
1143 }
1144 
1145 
1146 template <int dim, int spacedim>
1147 unsigned int
1149 {
1150  return number_cache.n_locally_owned_dofs;
1151 }
1152 
1153 
1154 template <int dim, int spacedim>
1155 const IndexSet &
1157 {
1158  return number_cache.locally_owned_dofs;
1159 }
1160 
1161 template <int dim, int spacedim>
1162 const IndexSet &
1163 DoFHandler<dim, spacedim>::locally_owned_mg_dofs(const unsigned int level) const
1164 {
1165  Assert(level < this->get_triangulation().n_global_levels(), ExcMessage("invalid level in locally_owned_mg_dofs"));
1166  return mg_number_cache[level].locally_owned_dofs;
1167 }
1168 
1169 template <int dim, int spacedim>
1170 const std::vector<types::global_dof_index> &
1172 {
1173  return number_cache.n_locally_owned_dofs_per_processor;
1174 }
1175 
1176 
1177 template <int dim, int spacedim>
1178 const std::vector<IndexSet> &
1180 {
1181  return number_cache.locally_owned_dofs_per_processor;
1182 }
1183 
1184 template <int dim, int spacedim>
1185 const std::vector<IndexSet> &
1187 {
1188  Assert(level < this->get_triangulation().n_global_levels(), ExcMessage("invalid level in locally_owned_mg_dofs_per_processor"));
1189  return mg_number_cache[level].locally_owned_dofs_per_processor;
1190 }
1191 
1192 
1193 template <int dim, int spacedim>
1194 inline
1197 {
1198  Assert(selected_fe!=0, ExcMessage("You are trying to access the DoFHandler's FiniteElement object before it has been initialized."));
1199  return *selected_fe;
1200 }
1201 
1202 
1203 
1204 template <int dim, int spacedim>
1205 inline
1208 {
1209  Assert(tria != 0, ExcNotInitialized());
1210  return *tria;
1211 }
1212 
1213 
1214 
1215 template <int dim, int spacedim>
1216 inline
1219 {
1220  Assert(tria != 0, ExcNotInitialized());
1221  return *tria;
1222 }
1223 
1224 
1225 
1226 template <int dim, int spacedim>
1227 inline
1228 const BlockInfo &
1230 {
1231  return block_info_object;
1232 }
1233 
1234 
1235 namespace internal
1236 {
1244  template<int dim, int spacedim>
1245  std::string policy_to_string(const ::internal::DoFHandler::Policy::PolicyBase<dim,spacedim> &policy);
1246 
1247 }
1248 
1249 
1250 template <int dim, int spacedim>
1251 template <class Archive>
1252 void DoFHandler<dim,spacedim>::save (Archive &ar,
1253  const unsigned int) const
1254 {
1255  ar &block_info_object;
1256  ar &vertex_dofs;
1257  ar &number_cache;
1258  ar &levels;
1259  ar &faces;
1260 
1261  // write out the number of triangulation cells and later check during
1262  // loading that this number is indeed correct; same with something that
1263  // identifies the FE and the policy
1264  unsigned int n_cells = tria->n_cells();
1265  std::string fe_name = selected_fe->get_name();
1266  std::string policy_name = internal::policy_to_string(*policy);
1267 
1268  ar &n_cells &fe_name &policy_name;
1269 }
1270 
1271 
1272 template <int dim, int spacedim>
1273 template <class Archive>
1274 void DoFHandler<dim,spacedim>::load (Archive &ar,
1275  const unsigned int)
1276 {
1277  ar &block_info_object;
1278  ar &vertex_dofs;
1279  ar &number_cache;
1280 
1281  // boost::serialization can restore pointers just fine, but if the
1282  // pointer object still points to something useful, that object is not
1283  // destroyed and we end up with a memory leak. consequently, first delete
1284  // previous content before re-loading stuff
1285  for (unsigned int i=0; i<levels.size(); ++i)
1286  delete levels[i];
1287  levels.resize (0);
1288  delete faces;
1289  faces = 0;
1290 
1291  ar &levels;
1292  ar &faces;
1293 
1294  // these are the checks that correspond to the last block in the save()
1295  // function
1296  unsigned int n_cells;
1297  std::string fe_name;
1298  std::string policy_name;
1299 
1300  ar &n_cells &fe_name &policy_name;
1301 
1302  AssertThrow (n_cells == tria->n_cells(),
1303  ExcMessage ("The object being loaded into does not match the triangulation "
1304  "that has been stored previously."));
1305  AssertThrow (fe_name == selected_fe->get_name(),
1306  ExcMessage ("The finite element associated with this DoFHandler does not match "
1307  "the one that was associated with the DoFHandler previously stored."));
1308  AssertThrow (policy_name == internal::policy_to_string(*policy),
1309  ExcMessage (std::string ("The policy currently associated with this DoFHandler (")
1310  + internal::policy_to_string(*policy)
1311  +std::string(") does not match the one that was associated with the "
1312  "DoFHandler previously stored (")
1313  + policy_name
1314  + ")."));
1315 }
1316 
1317 
1318 template<int dim, int spacedim>
1319 inline
1321  const unsigned int level,
1322  const unsigned int dof_number) const
1323 {
1324  Assert ((level >= coarsest_level) && (level <= finest_level), ExcInvalidLevel (level));
1325  return indices[indices_offset[level - coarsest_level] + dof_number];
1326 }
1327 
1328 
1329 template<int dim, int spacedim>
1330 inline
1332  const unsigned int level,
1333  const unsigned int dof_number,
1334  const types::global_dof_index index)
1335 {
1336  Assert ((level >= coarsest_level) && (level <= finest_level), ExcInvalidLevel (level));
1337  indices[indices_offset[level - coarsest_level] + dof_number] = index;
1338 }
1339 
1340 #endif // DOXYGEN
1341 
1342 DEAL_II_NAMESPACE_CLOSE
1343 
1344 #endif
std::vector< MGVertexDoFs > mg_vertex_dofs
Definition: dof_handler.h:1062
::FunctionMap< spacedim >::type FunctionMap
Definition: dof_handler.h:264
types::global_dof_index * indices
Definition: dof_handler.h:1021
void set_index(const unsigned int level, const unsigned int dof_number, const types::global_dof_index index)
::ExceptionBase & ExcMessage(std::string arg1)
const BlockInfo & block_info() const
std::vector<::internal::DoFHandler::DoFLevel< dim > * > levels
Definition: dof_handler.h:1068
ActiveSelector::active_cell_iterator active_cell_iterator
Definition: dof_handler.h:221
STL namespace.
#define AssertThrow(cond, exc)
Definition: exceptions.h:358
const FiniteElement< dim, spacedim > & get_fe() const
int level() const
BlockDynamicSparsityPattern BlockCompressedSparsityPattern DEAL_II_DEPRECATED
unsigned int n_locally_owned_dofs() const
const std::vector< types::global_dof_index > & n_locally_owned_dofs_per_processor() const
ActiveSelector::cell_iterator cell_iterator
Definition: dof_handler.h:249
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:542
Definition: types.h:30
unsigned int global_dof_index
Definition: types.h:88
types::global_dof_index * indices_offset
Definition: dof_handler.h:1028
bool has_active_dofs() const
#define Assert(cond, exc)
Definition: exceptions.h:294
types::global_dof_index get_index(const unsigned int level, const unsigned int dof_number) const
const Triangulation< dim, spacedim > * tria
#define DeclException0(Exception0)
Definition: exceptions.h:522
types::global_dof_index n_boundary_dofs() const
types::global_dof_index n_dofs() const
::internal::DoFHandler::DoFFaces< dim > * faces
Definition: dof_handler.h:1077
const IndexSet & locally_owned_mg_dofs(const unsigned int level) const
void renumber_dofs(const std::vector< types::global_dof_index > &new_numbers)
const Triangulation< dim, spacedim > & get_tria() const DEAL_II_DEPRECATED
const std::vector< IndexSet > & locally_owned_dofs_per_processor() const
unsigned int coarsest_level
Definition: dof_handler.h:1010
void load(Archive &ar, const unsigned int version)
A small class collecting the different BlockIndices involved in global, multilevel and local computat...
Definition: block_info.h:93
const Triangulation< dim, spacedim > & get_triangulation() const
const Triangulation< dim, spacedim > & get_triangulation() const
const types::global_dof_index invalid_dof_index
Definition: types.h:178
const IndexSet & locally_owned_dofs() const
bool has_level_dofs() const
void save(Archive &ar, const unsigned int version) const
std::vector< types::global_dof_index > vertex_dofs
Definition: dof_handler.h:1056