Reference documentation for deal.II version 8.5.1
tria_objects.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2006 - 2017 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__tria_objects_h
17 #define dealii__tria_objects_h
18 
19 #include <deal.II/base/config.h>
20 #include <deal.II/base/exceptions.h>
21 #include <deal.II/base/geometry_info.h>
22 #include <deal.II/grid/tria_object.h>
23 
24 #include <vector>
25 
26 DEAL_II_NAMESPACE_OPEN
27 
28 //TODO: This should all be cleaned up. Currently, only a single
29 //function in the library makes use of the odd specializations, and
30 //this function is Triangulation::execute_refinement() in 3D. I
31 //assume, that the other refinement functions would profit from using
32 //next_free_single_object() and next_free_pair_object, but they seem
33 //to get around it.
34 
35 //TODO: The TriaObjects class contains a std::vector<G>. This is only an
36 //efficient storage scheme if G is relatively well packed, i.e. it's not a
37 //bool and then an integer and then a double, etc. Verify that this is
38 //actually the case.
39 
40 template <int dim, int spacedim> class Triangulation;
41 template <class Accessor> class TriaRawIterator;
42 template <int, int, int> class TriaAccessor;
43 
44 namespace internal
45 {
46  namespace Triangulation
47  {
48 
61  template <typename G>
62  class TriaObjects
63  {
64  public:
68  TriaObjects();
69 
74  std::vector<G> cells;
75 
87  std::vector<int> children;
88 
94  std::vector<RefinementCase<G::dimension> > refinement_cases;
95 
104  std::vector<bool> used;
105 
115  std::vector<bool> user_flags;
116 
117 
123  {
124  union
125  {
126  types::boundary_id boundary_id;
127  types::material_id material_id;
128  };
129 
130 
135 
139  static
140  std::size_t memory_consumption ();
141 
146  template <class Archive>
147  void serialize(Archive &ar,
148  const unsigned int version);
149  };
150 
165  std::vector<BoundaryOrMaterialId> boundary_or_material_id;
166 
171  std::vector<types::manifold_id> manifold_id;
172 
183  void reserve_space (const unsigned int new_objs_in_pairs,
184  const unsigned int new_objs_single = 0);
185 
197  template <int dim, int spacedim>
199  next_free_single_object (const ::Triangulation<dim,spacedim> &tria);
200 
212  template <int dim, int spacedim>
214  next_free_pair_object (const ::Triangulation<dim,spacedim> &tria);
215 
220  template <int dim, int spacedim>
221  typename ::Triangulation<dim,spacedim>::raw_hex_iterator
222  next_free_hex (const ::Triangulation<dim,spacedim> &tria,
223  const unsigned int level);
224 
228  void clear();
229 
244  bool face_orientation(const unsigned int cell, const unsigned int face) const;
245 
246 
250  void *&user_pointer(const unsigned int i);
251 
255  const void *user_pointer(const unsigned int i) const;
256 
260  unsigned int &user_index(const unsigned int i);
261 
265  unsigned int user_index(const unsigned int i) const;
266 
270  void clear_user_data(const unsigned int i);
271 
276  void clear_user_data();
277 
281  void clear_user_flags();
282 
288  void monitor_memory (const unsigned int true_dimension) const;
289 
294  std::size_t memory_consumption () const;
295 
300  template <class Archive>
301  void serialize(Archive &ar,
302  const unsigned int version);
303 
308  char *, int, int,
309  << "The container " << arg1 << " contains "
310  << arg2 << " elements, but it`s capacity is "
311  << arg3 << ".");
317  int, int,
318  << "The containers have sizes " << arg1 << " and "
319  << arg2 << ", which is not as expected.");
320 
325  char *, char *,
326  << "You asked for the next free " << arg1 << "_iterator, "
327  "but you can only ask for " << arg2 <<"_iterators.");
328 
337 
338  protected:
342  unsigned int next_free_single;
343 
347  unsigned int next_free_pair;
348 
353 
357  struct UserData
358  {
359  union
360  {
363  void *p;
366  unsigned int i;
367  };
368 
373  {
374  p = 0;
375  }
376 
381  template <class Archive>
382  void serialize (Archive &ar, const unsigned int version);
383  };
384 
389  {
396  };
397 
398 
403  std::vector<UserData> user_data;
404 
411  };
412 
419  class TriaObjectsHex : public TriaObjects<TriaObject<3> >
420  {
421  public:
429  bool face_orientation(const unsigned int cell, const unsigned int face) const;
430 
431 
452  std::vector<bool> face_orientations;
453 
457  std::vector<bool> face_flips;
458 
462  std::vector<bool> face_rotations;
463 
470  void reserve_space (const unsigned int new_objs);
471 
475  void clear();
476 
482  void monitor_memory (const unsigned int true_dimension) const;
483 
488  std::size_t memory_consumption () const;
489 
494  template <class Archive>
495  void serialize(Archive &ar,
496  const unsigned int version);
497  };
498 
499 
506  class TriaObjectsQuad3D: public TriaObjects<TriaObject<2> >
507  {
508  public:
516  bool face_orientation(const unsigned int cell, const unsigned int face) const;
517 
518 
523  std::vector<bool> line_orientations;
524 
532  void reserve_space (const unsigned int new_quads_in_pairs,
533  const unsigned int new_quads_single = 0);
534 
538  void clear();
539 
545  void monitor_memory (const unsigned int true_dimension) const;
546 
551  std::size_t memory_consumption () const;
552 
557  template <class Archive>
558  void serialize(Archive &ar,
559  const unsigned int version);
560  };
561 
562 //----------------------------------------------------------------------//
563 
564 
565  template <typename G>
566  inline
568  {
569  material_id = numbers::invalid_material_id;
570  }
571 
572 
573 
574  template <typename G>
575  std::size_t
577  {
578  return sizeof(BoundaryOrMaterialId);
579  }
580 
581 
582 
583  template <typename G>
584  template <class Archive>
585  void
587  const unsigned int /*version*/)
588  {
589  // serialize this
590  // structure by
591  // writing and
592  // reading the larger
593  // of the two values,
594  // in order to make
595  // sure we get all
596  // bits
597  if (sizeof(material_id) > sizeof(boundary_id))
598  ar &material_id;
599  else
600  ar &boundary_id;
601  }
602 
603 
604  template<typename G>
605  inline
606  bool
608  face_orientation(const unsigned int, const unsigned int) const
609  {
610  return true;
611  }
612 
613 
614  template<typename G>
615  inline
616  void *&
617  TriaObjects<G>::user_pointer (const unsigned int i)
618  {
622 
623  Assert(i<user_data.size(), ExcIndexRange(i,0,user_data.size()));
624  return user_data[i].p;
625  }
626 
627 
628  template<typename G>
629  inline
630  const void *
631  TriaObjects<G>::user_pointer (const unsigned int i) const
632  {
636 
637  Assert(i<user_data.size(), ExcIndexRange(i,0,user_data.size()));
638  return user_data[i].p;
639  }
640 
641 
642  template<typename G>
643  inline
644  unsigned int &
645  TriaObjects<G>::user_index (const unsigned int i)
646  {
650 
651  Assert(i<user_data.size(), ExcIndexRange(i,0,user_data.size()));
652  return user_data[i].i;
653  }
654 
655 
656  template<typename G>
657  inline
658  void
659  TriaObjects<G>::clear_user_data (const unsigned int i)
660  {
661  Assert(i<user_data.size(), ExcIndexRange(i,0,user_data.size()));
662  user_data[i].i = 0;
663  }
664 
665 
666  template <typename G>
667  inline
669  :
670  next_free_single (numbers::invalid_unsigned_int),
671  next_free_pair (numbers::invalid_unsigned_int),
674  {}
675 
676 
677  template<typename G>
678  inline
679  unsigned int TriaObjects<G>::user_index (const unsigned int i) const
680  {
681  Assert(user_data_type == data_unknown || user_data_type == data_index,
682  ExcPointerIndexClash());
683  user_data_type = data_index;
684 
685  Assert(i<user_data.size(), ExcIndexRange(i,0,user_data.size()));
686  return user_data[i].i;
687  }
688 
689 
690  template<typename G>
691  inline
693  {
694  user_data_type = data_unknown;
695  for (unsigned int i=0; i<user_data.size(); ++i)
696  user_data[i].p = 0;
697  }
698 
699 
700  template<typename G>
701  inline
703  {
704  user_flags.assign(user_flags.size(),false);
705  }
706 
707 
708  template<typename G>
709  template <class Archive>
710  void
712  const unsigned int)
713  {
714  // serialize this as an integer
715  ar &i;
716  }
717 
718 
719 
720  template <typename G>
721  template <class Archive>
722  void TriaObjects<G>::serialize(Archive &ar,
723  const unsigned int)
724  {
725  ar &cells &children;
726  ar &refinement_cases;
727  ar &used;
728  ar &user_flags;
729  ar &boundary_or_material_id;
730  ar &manifold_id;
731  ar &next_free_single &next_free_pair &reverse_order_next_free_single;
732  ar &user_data &user_data_type;
733  }
734 
735 
736  template <class Archive>
737  void TriaObjectsHex::serialize(Archive &ar,
738  const unsigned int version)
739  {
740  this->TriaObjects<TriaObject<3> >::serialize (ar, version);
741 
742  ar &face_orientations &face_flips &face_rotations;
743  }
744 
745 
746  template <class Archive>
747  void TriaObjectsQuad3D::serialize(Archive &ar,
748  const unsigned int version)
749  {
750  this->TriaObjects<TriaObject<2> >::serialize (ar, version);
751 
752  ar &line_orientations;
753  }
754 
755 
756 //----------------------------------------------------------------------//
757 
758  inline
759  bool
760  TriaObjectsHex::face_orientation(const unsigned int cell,
761  const unsigned int face) const
762  {
763  Assert (cell < face_orientations.size() / GeometryInfo<3>::faces_per_cell,
764  ExcIndexRange(0, cell, face_orientations.size() / GeometryInfo<3>::faces_per_cell));
767 
768  return face_orientations[cell * GeometryInfo<3>::faces_per_cell
769  + face];
770  }
771 
772 //----------------------------------------------------------------------//
773 
774  inline
775  bool
776  TriaObjectsQuad3D::face_orientation(const unsigned int cell, const unsigned int face) const
777  {
778  return line_orientations[cell * GeometryInfo<2>::faces_per_cell
779  + face];
780  }
781 
782 
783 //----------------------------------------------------------------------//
784 
785  template <class G>
786  template <int dim, int spacedim>
788  TriaObjects<G>::next_free_single_object (const ::Triangulation<dim,spacedim> &tria)
789  {
790  // TODO: Think of a way to ensure that we are using the correct triangulation, i.e. the one containing *this.
791 
792  int pos=next_free_single,
793  last=used.size()-1;
794  if (!reverse_order_next_free_single)
795  {
796  // first sweep forward, only use really single slots, do not use
797  // pair slots
798  for (; pos<last; ++pos)
799  if (!used[pos])
800  if (used[++pos])
801  {
802  // this was a single slot
803  pos-=1;
804  break;
805  }
806  if (pos>=last)
807  {
808  reverse_order_next_free_single=true;
809  next_free_single=used.size()-1;
810  pos=used.size()-1;
811  }
812  else
813  next_free_single=pos+1;
814  }
815 
816  if (reverse_order_next_free_single)
817  {
818  // second sweep, use all slots, even
819  // in pairs
820  for (; pos>=0; --pos)
821  if (!used[pos])
822  break;
823  if (pos>0)
824  next_free_single=pos-1;
825  else
826  // no valid single object anymore
827  return ::TriaRawIterator<::TriaAccessor<G::dimension,dim,spacedim> >(&tria, -1, -1);
828  }
829 
830  return ::TriaRawIterator<::TriaAccessor<G::dimension,dim,spacedim> >(&tria, 0, pos);
831  }
832 
833 
834 
835  template <class G>
836  template <int dim, int spacedim>
838  TriaObjects<G>::next_free_pair_object (const ::Triangulation<dim,spacedim> &tria)
839  {
840  // TODO: Think of a way to ensure that we are using the correct triangulation, i.e. the one containing *this.
841 
842  int pos=next_free_pair,
843  last=used.size()-1;
844  for (; pos<last; ++pos)
845  if (!used[pos])
846  if (!used[++pos])
847  {
848  // this was a pair slot
849  pos-=1;
850  break;
851  }
852  if (pos>=last)
853  // no free slot
854  return ::TriaRawIterator<::TriaAccessor<G::dimension,dim,spacedim> >(&tria, -1, -1);
855  else
856  next_free_pair=pos+2;
857 
858  return ::TriaRawIterator<::TriaAccessor<G::dimension,dim,spacedim> >(&tria, 0, pos);
859  }
860 
861 
862 
863 // declaration of explicit specializations
864 
865  template<>
866  void
867  TriaObjects<TriaObject<2> >::monitor_memory (const unsigned int) const;
868 
869  }
870 }
871 
872 
873 
874 DEAL_II_NAMESPACE_CLOSE
875 
876 #endif
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:576
std::vector< UserData > user_data
Definition: tria_objects.h:403
bool face_orientation(const unsigned int cell, const unsigned int face) const
Definition: tria_objects.h:776
static ::ExceptionBase & ExcMemoryInexact(int arg1, int arg2)
unsigned char material_id
Definition: types.h:130
void serialize(Archive &ar, const unsigned int version)
Definition: tria_objects.h:586
void reserve_space(const unsigned int new_quads_in_pairs, const unsigned int new_quads_single=0)
bool face_orientation(const unsigned int cell, const unsigned int face) const
Definition: tria_objects.h:760
static ::ExceptionBase & ExcPointerIndexClash()
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
unsigned int & user_index(const unsigned int i)
Definition: tria_objects.h:645
static ::ExceptionBase & ExcWrongIterator(char *arg1, char *arg2)
void serialize(Archive &ar, const unsigned int version)
Definition: tria_objects.h:722
bool face_orientation(const unsigned int cell, const unsigned int face) const
Definition: tria_objects.h:608
void monitor_memory(const unsigned int true_dimension) const
void serialize(Archive &ar, const unsigned int version)
Definition: tria_objects.h:711
#define Assert(cond, exc)
Definition: exceptions.h:313
void *& user_pointer(const unsigned int i)
Definition: tria_objects.h:617
std::vector< RefinementCase< G::dimension > > refinement_cases
Definition: tria_objects.h:94
#define DeclException0(Exception0)
Definition: exceptions.h:541
void reserve_space(const unsigned int new_objs)
void monitor_memory(const unsigned int true_dimension) const
std::vector< BoundaryOrMaterialId > boundary_or_material_id
Definition: tria_objects.h:165
void monitor_memory(const unsigned int true_dimension) const
::TriaRawIterator<::TriaAccessor< G::dimension, dim, spacedim > > next_free_single_object(const ::Triangulation< dim, spacedim > &tria)
std::size_t memory_consumption() const
typename ::Triangulation< dim, spacedim >::raw_hex_iterator next_free_hex(const ::Triangulation< dim, spacedim > &tria, const unsigned int level)
std::vector< types::manifold_id > manifold_id
Definition: tria_objects.h:171
unsigned char boundary_id
Definition: types.h:110
#define DeclException3(Exception3, type1, type2, type3, outsequence)
Definition: exceptions.h:588
::TriaRawIterator<::TriaAccessor< G::dimension, dim, spacedim > > next_free_pair_object(const ::Triangulation< dim, spacedim > &tria)
void serialize(Archive &ar, const unsigned int version)
Definition: tria_objects.h:747
static ::ExceptionBase & ExcMemoryWasted(char *arg1, int arg2, int arg3)
void serialize(Archive &ar, const unsigned int version)
Definition: tria_objects.h:737
void reserve_space(const unsigned int new_objs_in_pairs, const unsigned int new_objs_single=0)
Definition: tria_objects.cc:36
const types::material_id invalid_material_id
Definition: types.h:191