Reference documentation for deal.II version 9.0.0
tria_objects.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2006 - 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 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 TriangulationImplementation
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 
309  int, int,
310  << "The containers have sizes " << arg1 << " and "
311  << arg2 << ", which is not as expected.");
312 
321 
322  protected:
326  unsigned int next_free_single;
327 
331  unsigned int next_free_pair;
332 
337 
341  struct UserData
342  {
343  union
344  {
347  void *p;
350  unsigned int i;
351  };
352 
357  {
358  p = nullptr;
359  }
360 
365  template <class Archive>
366  void serialize (Archive &ar, const unsigned int version);
367  };
368 
373  {
380  };
381 
382 
387  std::vector<UserData> user_data;
388 
395  };
396 
403  class TriaObjectsHex : public TriaObjects<TriaObject<3> >
404  {
405  public:
413  bool face_orientation(const unsigned int cell, const unsigned int face) const;
414 
415 
438  std::vector<bool> face_orientations;
439 
443  std::vector<bool> face_flips;
444 
448  std::vector<bool> face_rotations;
449 
456  void reserve_space (const unsigned int new_objs);
457 
461  void clear();
462 
468  void monitor_memory (const unsigned int true_dimension) const;
469 
474  std::size_t memory_consumption () const;
475 
480  template <class Archive>
481  void serialize(Archive &ar,
482  const unsigned int version);
483  };
484 
485 
492  class TriaObjectsQuad3D: public TriaObjects<TriaObject<2> >
493  {
494  public:
502  bool face_orientation(const unsigned int cell, const unsigned int face) const;
503 
504 
509  std::vector<bool> line_orientations;
510 
518  void reserve_space (const unsigned int new_quads_in_pairs,
519  const unsigned int new_quads_single = 0);
520 
524  void clear();
525 
531  void monitor_memory (const unsigned int true_dimension) const;
532 
537  std::size_t memory_consumption () const;
538 
543  template <class Archive>
544  void serialize(Archive &ar,
545  const unsigned int version);
546  };
547 
548 //----------------------------------------------------------------------//
549 
550 
551  template <typename G>
552  inline
554  {
555  material_id = numbers::invalid_material_id;
556  }
557 
558 
559 
560  template <typename G>
561  std::size_t
563  {
564  return sizeof(BoundaryOrMaterialId);
565  }
566 
567 
568 
569  template <typename G>
570  template <class Archive>
571  void
573  const unsigned int /*version*/)
574  {
575  // serialize this
576  // structure by
577  // writing and
578  // reading the larger
579  // of the two values,
580  // in order to make
581  // sure we get all
582  // bits
583  if (sizeof(material_id) > sizeof(boundary_id))
584  ar &material_id;
585  else
586  ar &boundary_id;
587  }
588 
589 
590  template <typename G>
591  inline
592  bool
594  face_orientation(const unsigned int, const unsigned int) const
595  {
596  return true;
597  }
598 
599 
600  template <typename G>
601  inline
602  void *&
603  TriaObjects<G>::user_pointer (const unsigned int i)
604  {
608 
609  Assert(i<user_data.size(), ExcIndexRange(i,0,user_data.size()));
610  return user_data[i].p;
611  }
612 
613 
614  template <typename G>
615  inline
616  const void *
617  TriaObjects<G>::user_pointer (const unsigned int i) const
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  unsigned int &
631  TriaObjects<G>::user_index (const unsigned int i)
632  {
636 
637  Assert(i<user_data.size(), ExcIndexRange(i,0,user_data.size()));
638  return user_data[i].i;
639  }
640 
641 
642  template <typename G>
643  inline
644  void
645  TriaObjects<G>::clear_user_data (const unsigned int i)
646  {
647  Assert(i<user_data.size(), ExcIndexRange(i,0,user_data.size()));
648  user_data[i].i = 0;
649  }
650 
651 
652  template <typename G>
653  inline
655  :
656  next_free_single (numbers::invalid_unsigned_int),
657  next_free_pair (numbers::invalid_unsigned_int),
660  {}
661 
662 
663  template <typename G>
664  inline
665  unsigned int TriaObjects<G>::user_index (const unsigned int i) const
666  {
667  Assert(user_data_type == data_unknown || user_data_type == data_index,
668  ExcPointerIndexClash());
669  user_data_type = data_index;
670 
671  Assert(i<user_data.size(), ExcIndexRange(i,0,user_data.size()));
672  return user_data[i].i;
673  }
674 
675 
676  template <typename G>
677  inline
679  {
680  user_data_type = data_unknown;
681  for (unsigned int i=0; i<user_data.size(); ++i)
682  user_data[i].p = nullptr;
683  }
684 
685 
686  template <typename G>
687  inline
689  {
690  user_flags.assign(user_flags.size(),false);
691  }
692 
693 
694  template <typename G>
695  template <class Archive>
696  void
698  const unsigned int)
699  {
700  // serialize this as an integer
701  ar &i;
702  }
703 
704 
705 
706  template <typename G>
707  template <class Archive>
708  void TriaObjects<G>::serialize(Archive &ar,
709  const unsigned int)
710  {
711  ar &cells &children;
712  ar &refinement_cases;
713  ar &used;
714  ar &user_flags;
715  ar &boundary_or_material_id;
716  ar &manifold_id;
717  ar &next_free_single &next_free_pair &reverse_order_next_free_single;
718  ar &user_data &user_data_type;
719  }
720 
721 
722  template <class Archive>
723  void TriaObjectsHex::serialize(Archive &ar,
724  const unsigned int version)
725  {
726  this->TriaObjects<TriaObject<3> >::serialize (ar, version);
727 
728  ar &face_orientations &face_flips &face_rotations;
729  }
730 
731 
732  template <class Archive>
733  void TriaObjectsQuad3D::serialize(Archive &ar,
734  const unsigned int version)
735  {
736  this->TriaObjects<TriaObject<2> >::serialize (ar, version);
737 
738  ar &line_orientations;
739  }
740 
741 
742 //----------------------------------------------------------------------//
743 
744  inline
745  bool
746  TriaObjectsHex::face_orientation(const unsigned int cell,
747  const unsigned int face) const
748  {
749  Assert (cell < face_orientations.size() / GeometryInfo<3>::faces_per_cell,
750  ExcIndexRange(0, cell, face_orientations.size() / GeometryInfo<3>::faces_per_cell));
753 
754  return face_orientations[cell * GeometryInfo<3>::faces_per_cell
755  + face];
756  }
757 
758 //----------------------------------------------------------------------//
759 
760  inline
761  bool
762  TriaObjectsQuad3D::face_orientation(const unsigned int cell, const unsigned int face) const
763  {
764  return line_orientations[cell * GeometryInfo<2>::faces_per_cell
765  + face];
766  }
767 
768 
769 //----------------------------------------------------------------------//
770 
771  template <class G>
772  template <int dim, int spacedim>
774  TriaObjects<G>::next_free_single_object (const ::Triangulation<dim,spacedim> &tria)
775  {
776  // TODO: Think of a way to ensure that we are using the correct triangulation, i.e. the one containing *this.
777 
778  int pos=next_free_single,
779  last=used.size()-1;
780  if (!reverse_order_next_free_single)
781  {
782  // first sweep forward, only use really single slots, do not use
783  // pair slots
784  for (; pos<last; ++pos)
785  if (!used[pos])
786  if (used[++pos])
787  {
788  // this was a single slot
789  pos-=1;
790  break;
791  }
792  if (pos>=last)
793  {
794  reverse_order_next_free_single=true;
795  next_free_single=used.size()-1;
796  pos=used.size()-1;
797  }
798  else
799  next_free_single=pos+1;
800  }
801 
802  if (reverse_order_next_free_single)
803  {
804  // second sweep, use all slots, even
805  // in pairs
806  for (; pos>=0; --pos)
807  if (!used[pos])
808  break;
809  if (pos>0)
810  next_free_single=pos-1;
811  else
812  // no valid single object anymore
813  return ::TriaRawIterator<::TriaAccessor<G::dimension,dim,spacedim> >(&tria, -1, -1);
814  }
815 
816  return ::TriaRawIterator<::TriaAccessor<G::dimension,dim,spacedim> >(&tria, 0, pos);
817  }
818 
819 
820 
821  template <class G>
822  template <int dim, int spacedim>
824  TriaObjects<G>::next_free_pair_object (const ::Triangulation<dim,spacedim> &tria)
825  {
826  // TODO: Think of a way to ensure that we are using the correct triangulation, i.e. the one containing *this.
827 
828  int pos=next_free_pair,
829  last=used.size()-1;
830  for (; pos<last; ++pos)
831  if (!used[pos])
832  if (!used[++pos])
833  {
834  // this was a pair slot
835  pos-=1;
836  break;
837  }
838  if (pos>=last)
839  // no free slot
840  return ::TriaRawIterator<::TriaAccessor<G::dimension,dim,spacedim> >(&tria, -1, -1);
841  else
842  next_free_pair=pos+2;
843 
844  return ::TriaRawIterator<::TriaAccessor<G::dimension,dim,spacedim> >(&tria, 0, pos);
845  }
846 
847 
848 
849 // declaration of explicit specializations
850 
851  template <>
852  void
853  TriaObjects<TriaObject<2> >::monitor_memory (const unsigned int) const;
854 
855  }
856 }
857 
858 
859 
860 DEAL_II_NAMESPACE_CLOSE
861 
862 #endif
void reserve_space(const unsigned int new_objs)
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:358
static ::ExceptionBase & ExcMemoryInexact(int arg1, int arg2)
void serialize(Archive &ar, const unsigned int version)
Definition: tria_objects.h:708
bool face_orientation(const unsigned int cell, const unsigned int face) const
Definition: tria_objects.h:762
unsigned int boundary_id
Definition: types.h:110
void monitor_memory(const unsigned int true_dimension) const
void serialize(Archive &ar, const unsigned int version)
Definition: tria_objects.h:733
bool face_orientation(const unsigned int cell, const unsigned int face) const
Definition: tria_objects.h:594
unsigned int material_id
Definition: types.h:133
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
unsigned int & user_index(const unsigned int i)
Definition: tria_objects.h:631
typename ::Triangulation< dim, spacedim >::raw_hex_iterator next_free_hex(const ::Triangulation< dim, spacedim > &tria, const unsigned int level)
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
#define Assert(cond, exc)
Definition: exceptions.h:1142
#define DeclException0(Exception0)
Definition: exceptions.h:323
void reserve_space(const unsigned int new_objs_in_pairs, const unsigned int new_objs_single=0)
Definition: tria_objects.cc:35
std::vector< RefinementCase< G::dimension > > refinement_cases
Definition: tria_objects.h:94
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:746
std::vector< types::manifold_id > manifold_id
Definition: tria_objects.h:171
void serialize(Archive &ar, const unsigned int version)
Definition: tria_objects.h:723
::TriaRawIterator<::TriaAccessor< G::dimension, dim, spacedim > > next_free_single_object(const ::Triangulation< dim, spacedim > &tria)
const types::material_id invalid_material_id
Definition: types.h:194
::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:697