Reference documentation for deal.II version 9.0.0
tria.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 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 
17 #include <deal.II/base/std_cxx14/memory.h>
18 #include <deal.II/base/memory_consumption.h>
19 #include <deal.II/base/table.h>
20 #include <deal.II/base/geometry_info.h>
21 
22 #include <deal.II/grid/tria.h>
23 #include <deal.II/grid/tria_levels.h>
24 #include <deal.II/grid/tria_faces.h>
25 #include <deal.II/grid/manifold.h>
26 #include <deal.II/grid/tria_boundary.h>
27 #include <deal.II/grid/tria_accessor.h>
28 #include <deal.II/grid/tria_iterator.h>
29 #include <deal.II/grid/grid_tools.h>
30 #include <deal.II/grid/magic_numbers.h>
31 #include <deal.II/fe/mapping_q1.h>
32 #include <deal.II/lac/vector.h>
33 #include <deal.II/lac/full_matrix.h>
34 
35 #include <algorithm>
36 #include <numeric>
37 #include <map>
38 #include <list>
39 #include <cmath>
40 #include <functional>
41 #include <array>
42 
43 
44 DEAL_II_NAMESPACE_OPEN
45 
46 bool
47 SubCellData::check_consistency (const unsigned int dim) const
48 {
49  switch (dim)
50  {
51  case 1:
52  return ((boundary_lines.size() == 0) &&
53  (boundary_quads.size() == 0));
54  case 2:
55  return (boundary_quads.size() == 0);
56  };
57  return true;
58 }
59 
60 
61 namespace internal
62 {
63  namespace TriangulationImplementation
64  {
65 
67  :
68  n_levels (0),
69  n_lines (0),
70  n_active_lines (0)
71  // all other fields are
72  // default constructed
73  {}
74 
75 
76 
77  std::size_t
79  {
80  return (MemoryConsumption::memory_consumption (n_levels) +
83  MemoryConsumption::memory_consumption (n_active_lines) +
84  MemoryConsumption::memory_consumption (n_active_lines_level));
85  }
86 
87 
89  n_quads (0),
90  n_active_quads (0)
91  // all other fields are
92  // default constructed
93  {}
94 
95 
96 
97  std::size_t
99  {
102  MemoryConsumption::memory_consumption (n_quads_level) +
103  MemoryConsumption::memory_consumption (n_active_quads) +
104  MemoryConsumption::memory_consumption (n_active_quads_level));
105  }
106 
107 
108 
110  n_hexes (0),
111  n_active_hexes (0)
112  // all other fields are
113  // default constructed
114  {}
115 
116 
117 
118  std::size_t
120  {
123  MemoryConsumption::memory_consumption (n_hexes_level) +
124  MemoryConsumption::memory_consumption (n_active_hexes) +
125  MemoryConsumption::memory_consumption (n_active_hexes_level));
126  }
127  }
128 }
129 
130 // anonymous namespace for internal helper functions
131 namespace
132 {
133  // return whether the given cell is
134  // patch_level_1, i.e. determine
135  // whether either all or none of
136  // its children are further
137  // refined. this function can only
138  // be called for non-active cells.
139  template <int dim, int spacedim>
140  bool cell_is_patch_level_1 (const TriaIterator<::CellAccessor<dim, spacedim> > &cell)
141  {
142  Assert (cell->active() == false, ExcInternalError());
143 
144  unsigned int n_active_children = 0;
145  for (unsigned int i=0; i<cell->n_children(); ++i)
146  if (cell->child(i)->active())
147  ++n_active_children;
148 
149  return (n_active_children == 0) || (n_active_children == cell->n_children());
150  }
151 
152 
153 
154  // return, whether a given @p cell will be
155  // coarsened, which is the case if all
156  // children are active and have their coarsen
157  // flag set. In case only part of the coarsen
158  // flags are set, remove them.
159  template <int dim, int spacedim>
160  bool cell_will_be_coarsened (const TriaIterator<::CellAccessor<dim,spacedim> > &cell)
161  {
162  // only cells with children should be
163  // considered for coarsening
164 
165  if (cell->has_children())
166  {
167  unsigned int children_to_coarsen=0;
168  const unsigned int n_children=cell->n_children();
169 
170  for (unsigned int c=0; c<n_children; ++c)
171  if (cell->child(c)->active() &&
172  cell->child(c)->coarsen_flag_set())
173  ++children_to_coarsen;
174  if (children_to_coarsen==n_children)
175  return true;
176  else
177  for (unsigned int c=0; c<n_children; ++c)
178  if (cell->child(c)->active())
179  cell->child(c)->clear_coarsen_flag();
180  }
181  // no children, so no coarsening
182  // possible. however, no children also
183  // means that this cell will be in the same
184  // state as if it had children and was
185  // coarsened. So, what should we return -
186  // false or true?
187  // make sure we do not have to do this at
188  // all...
189  Assert(cell->has_children(), ExcInternalError());
190  // ... and then simply return false
191  return false;
192  }
193 
194 
195  // return, whether the face @p face_no of the
196  // given @p cell will be refined after the
197  // current refinement step, considering
198  // refine and coarsen flags and considering
199  // only those refinemnts that will be caused
200  // by the neighboring cell.
201 
202  // this function is used on both active cells
203  // and cells with children. on cells with
204  // children it also of interest to know 'how'
205  // the face will be refined. thus there is an
206  // additional third argument @p
207  // expected_face_ref_case returning just
208  // that. be aware, that this vriable will
209  // only contain useful information if this
210  // function is called for an active cell.
211  //
212  // thus, this is an internal function, users
213  // should call one of the two alternatives
214  // following below.
215  template <int dim, int spacedim>
216  bool
217  face_will_be_refined_by_neighbor_internal(const TriaIterator<::CellAccessor<dim,spacedim> > &cell,
218  const unsigned int face_no,
219  RefinementCase<dim-1> &expected_face_ref_case)
220  {
221  // first of all: set the default value for
222  // expected_face_ref_case, which is no
223  // refinement at all
224  expected_face_ref_case=RefinementCase<dim-1>::no_refinement;
225 
226  const typename Triangulation<dim,spacedim>::cell_iterator neighbor=cell->neighbor(face_no);
227 
228  // If we are at the boundary, there is no
229  // neighbor which could refine the face
230  if (neighbor.state()!=IteratorState::valid)
231  return false;
232 
233  if (neighbor->has_children())
234  {
235  // if the neighbor is refined, it may be
236  // coarsened. if so, then it won't refine
237  // the face, no matter what else happens
238  if (cell_will_be_coarsened(neighbor))
239  return false;
240  else
241  // if the neighor is refined, then he
242  // is also refined at our current
243  // face. He will stay so without
244  // coarsening, so return true in that
245  // case.
246  {
247  expected_face_ref_case=cell->face(face_no)->refinement_case();
248  return true;
249  }
250  }
251 
252  // now, the neighbor is not refined, but
253  // perhaps he will be
254  const RefinementCase<dim> nb_ref_flag=neighbor->refine_flag_set();
255  if (nb_ref_flag != RefinementCase<dim>::no_refinement)
256  {
257  // now we need to know, which of the
258  // neighbors faces points towards us
259  const unsigned int neighbor_neighbor=cell->neighbor_face_no(face_no);
260  // check, whether the cell will be
261  // refined in a way that refines our
262  // face
263  const RefinementCase<dim-1> face_ref_case=
265  neighbor_neighbor,
266  neighbor->face_orientation(neighbor_neighbor),
267  neighbor->face_flip(neighbor_neighbor),
268  neighbor->face_rotation(neighbor_neighbor));
269  if (face_ref_case != RefinementCase<dim-1>::no_refinement)
270  {
271  const typename Triangulation<dim,spacedim>::face_iterator neighbor_face=neighbor->face(neighbor_neighbor);
272  const int this_face_index=cell->face_index(face_no);
273 
274  // there are still two basic
275  // possibilities here: the neighbor
276  // might be coarser or as coarse
277  // as we are
278  if (neighbor_face->index()==this_face_index)
279  // the neighbor is as coarse as
280  // we are and will be refined at
281  // the face of consideration, so
282  // return true
283  {
284  expected_face_ref_case = face_ref_case;
285  return true;
286  }
287  else
288  {
289 
290  // the neighbor is coarser.
291  // this is the most complicated
292  // case. It might be, that the
293  // neighbor's face will be
294  // refined, but that we will
295  // not see this, as we are
296  // refined in a similar way.
297 
298  // so, the neighbor's face must
299  // have children. check, if our
300  // cell's face is one of these
301  // (it could also be a
302  // grand_child)
303  for (unsigned int c=0; c<neighbor_face->n_children(); ++c)
304  if (neighbor_face->child_index(c)==this_face_index)
305  {
306  // if the flagged refine
307  // case of the face is a
308  // subset or the same as
309  // the current refine case,
310  // then the face, as seen
311  // from our cell, won't be
312  // refined by the neighbor
313  if ((neighbor_face->refinement_case() | face_ref_case)
314  == neighbor_face->refinement_case())
315  return false;
316  else
317  {
318  // if we are active, we
319  // must be an
320  // anisotropic child
321  // and the coming
322  // face_ref_case is
323  // isotropic. Thus,
324  // from our cell we
325  // will see exactly the
326  // opposite refine case
327  // that the face has
328  // now...
330  expected_face_ref_case = ~neighbor_face->refinement_case();
331  return true;
332  }
333  }
334 
335  // so, obviously we were not
336  // one of the children, but a
337  // grandchild. This is only
338  // possible in 3d.
339  Assert(dim==3, ExcInternalError());
340  // In that case, however, no
341  // matter what the neighbor
342  // does, he won't be finer
343  // after the next refinement
344  // step.
345  return false;
346  }
347  }// if face will be refined
348  }// if neighbor is flagged for refinement
349 
350  // no cases left, so the neighbor will not
351  // refine the face
352  return false;
353  }
354 
355  // version of above function for both active
356  // and non-active cells
357  template <int dim, int spacedim>
358  bool
359  face_will_be_refined_by_neighbor(const TriaIterator<::CellAccessor<dim, spacedim> > &cell,
360  const unsigned int face_no)
361  {
362  RefinementCase<dim-1> dummy = RefinementCase<dim-1>::no_refinement;
363  return face_will_be_refined_by_neighbor_internal(cell, face_no, dummy);
364  }
365 
366  // version of above function for active cells
367  // only. Additionally returning the refine
368  // case (to come) of the face under
369  // consideration
370  template <int dim, int spacedim>
371  bool
372  face_will_be_refined_by_neighbor(const TriaActiveIterator<::CellAccessor<dim,spacedim> > &cell,
373  const unsigned int face_no,
374  RefinementCase<dim-1> &expected_face_ref_case)
375  {
376  return face_will_be_refined_by_neighbor_internal(cell, face_no,
377  expected_face_ref_case);
378  }
379 
380 
381 
382  template <int dim, int spacedim>
383  bool
384  satisfies_level1_at_vertex_rule (const Triangulation<dim,spacedim> &triangulation)
385  {
386  std::vector<unsigned int> min_adjacent_cell_level (triangulation.n_vertices(),
387  triangulation.n_levels());
388  std::vector<unsigned int> max_adjacent_cell_level (triangulation.n_vertices(),
389  0);
390 
392  cell = triangulation.begin_active();
393  cell != triangulation.end(); ++cell)
394  for (unsigned int v=0; v<GeometryInfo<dim>::vertices_per_cell; ++v)
395  {
396  min_adjacent_cell_level[cell->vertex_index(v)]
397  = std::min<unsigned int>
398  (min_adjacent_cell_level[cell->vertex_index(v)],
399  cell->level());
400  max_adjacent_cell_level[cell->vertex_index(v)]
401  = std::max<unsigned int> (min_adjacent_cell_level[cell->vertex_index(v)],
402  cell->level());
403  }
404 
405  for (unsigned int k=0; k<triangulation.n_vertices(); ++k)
406  if (triangulation.vertex_used(k))
407  if (max_adjacent_cell_level[k] -
408  min_adjacent_cell_level[k] > 1)
409  return false;
410  return true;
411  }
412 
413 
414 
421  template <int dim, int spacedim>
422  std::vector<unsigned int>
423  count_cells_bounded_by_line (const Triangulation<dim,spacedim> &triangulation)
424  {
425  if (dim >= 2)
426  {
427  std::vector<unsigned int> line_cell_count(triangulation.n_raw_lines(),0);
429  cell=triangulation.begin(),
430  endc=triangulation.end();
431  for (; cell!=endc; ++cell)
432  for (unsigned int l=0; l<GeometryInfo<dim>::lines_per_cell; ++l)
433  ++line_cell_count[cell->line_index(l)];
434  return line_cell_count;
435  }
436  else
437  return std::vector<unsigned int>();
438  }
439 
440 
441 
442 
449  template <int dim, int spacedim>
450  std::vector<unsigned int>
451  count_cells_bounded_by_quad (const Triangulation<dim,spacedim> &triangulation)
452  {
453  if (dim >= 3)
454  {
455  std::vector<unsigned int> quad_cell_count (triangulation.n_raw_quads(),0);
457  cell=triangulation.begin(),
458  endc=triangulation.end();
459  for (; cell!=endc; ++cell)
460  for (unsigned int q=0; q<GeometryInfo<dim>::faces_per_cell; ++q)
461  ++quad_cell_count[cell->quad_index(q)];
462  return quad_cell_count;
463  }
464  else
465  return std::vector<unsigned int>();
466  }
467 
468 
469 
481  void
482  reorder_compatibility (const std::vector<CellData<1> > &,
483  const SubCellData &)
484  {
485  // nothing to do here: the format
486  // hasn't changed for 1d
487  }
488 
489 
490  void
491  reorder_compatibility (std::vector<CellData<2> > &cells,
492  const SubCellData &)
493  {
494  for (unsigned int cell=0; cell<cells.size(); ++cell)
495  std::swap(cells[cell].vertices[2],cells[cell].vertices[3]);
496  }
497 
498 
499  void
500  reorder_compatibility (std::vector<CellData<3> > &cells,
501  SubCellData &subcelldata)
502  {
503  unsigned int tmp[GeometryInfo<3>::vertices_per_cell];
504  for (unsigned int cell=0; cell<cells.size(); ++cell)
505  {
506  for (unsigned int i=0; i<GeometryInfo<3>::vertices_per_cell; ++i)
507  tmp[i] = cells[cell].vertices[i];
508  for (unsigned int i=0; i<GeometryInfo<3>::vertices_per_cell; ++i)
509  cells[cell].vertices[GeometryInfo<3>::ucd_to_deal[i]] = tmp[i];
510  }
511 
512  // now points in boundary quads
513  std::vector<CellData<2> >::iterator boundary_quad
514  = subcelldata.boundary_quads.begin();
515  std::vector<CellData<2> >::iterator end_quad
516  = subcelldata.boundary_quads.end();
517  for (unsigned int quad_no=0; boundary_quad!=end_quad; ++boundary_quad, ++quad_no)
518  std::swap(boundary_quad->vertices[2], boundary_quad->vertices[3]);
519  }
520 
521 
522 
540  template <int dim, int spacedim>
541  unsigned int
542  middle_vertex_index(const typename Triangulation<dim,spacedim>::line_iterator &line)
543  {
544  if (line->has_children())
545  return line->child(0)->vertex_index(1);
547  }
548 
549 
550  template <int dim, int spacedim>
551  unsigned int
552  middle_vertex_index(const typename Triangulation<dim,spacedim>::quad_iterator &quad)
553  {
554  switch (static_cast<unsigned char> (quad->refinement_case()))
555  {
557  return middle_vertex_index<dim,spacedim>(quad->child(0)->line(1));
558  break;
560  return middle_vertex_index<dim,spacedim>(quad->child(0)->line(3));
561  break;
563  return quad->child(0)->vertex_index(3);
564  break;
565  default:
566  break;
567  }
569  }
570 
571 
572  template <int dim, int spacedim>
573  unsigned int
574  middle_vertex_index(const typename Triangulation<dim,spacedim>::hex_iterator &hex)
575  {
576  switch (static_cast<unsigned char> (hex->refinement_case()))
577  {
579  return middle_vertex_index<dim,spacedim>(hex->child(0)->quad(1));
580  break;
582  return middle_vertex_index<dim,spacedim>(hex->child(0)->quad(3));
583  break;
585  return middle_vertex_index<dim,spacedim>(hex->child(0)->quad(5));
586  break;
588  return middle_vertex_index<dim,spacedim>(hex->child(0)->line(11));
589  break;
591  return middle_vertex_index<dim,spacedim>(hex->child(0)->line(5));
592  break;
594  return middle_vertex_index<dim,spacedim>(hex->child(0)->line(7));
595  break;
597  return hex->child(0)->vertex_index(7);
598  break;
599  default:
600  break;
601  }
603  }
604 
605 
618  template <class TRIANGULATION>
619  inline
620  typename TRIANGULATION::DistortedCellList
621  collect_distorted_coarse_cells (const TRIANGULATION &)
622  {
623  return typename TRIANGULATION::DistortedCellList();
624  }
625 
626 
627 
636  template <int dim>
637  inline
639  collect_distorted_coarse_cells (const Triangulation<dim,dim> &triangulation)
640  {
641  typename Triangulation<dim,dim>::DistortedCellList distorted_cells;
643  cell = triangulation.begin(0); cell != triangulation.end(0); ++cell)
644  {
646  for (unsigned int i=0; i<GeometryInfo<dim>::vertices_per_cell; ++i)
647  vertices[i] = cell->vertex(i);
648 
651  determinants);
652 
653  for (unsigned int i=0; i<GeometryInfo<dim>::vertices_per_cell; ++i)
654  if (determinants[i] <= 1e-9 * std::pow (cell->diameter(),
655  1.*dim))
656  {
657  distorted_cells.distorted_cells.push_back (cell);
658  break;
659  }
660  }
661 
662  return distorted_cells;
663  }
664 
665 
672  template <int dim>
673  bool
674  has_distorted_children (const typename Triangulation<dim,dim>::cell_iterator &cell,
675  std::integral_constant<int, dim>,
676  std::integral_constant<int, dim>)
677  {
678  Assert (cell->has_children(), ExcInternalError());
679 
680  for (unsigned int c=0; c<cell->n_children(); ++c)
681  {
683  for (unsigned int i=0; i<GeometryInfo<dim>::vertices_per_cell; ++i)
684  vertices[i] = cell->child(c)->vertex(i);
685 
688  determinants);
689 
690  for (unsigned int i=0; i<GeometryInfo<dim>::vertices_per_cell; ++i)
691  if (determinants[i] <= 1e-9 * std::pow (cell->child(c)->diameter(),
692  1.*dim))
693  return true;
694  }
695 
696  return false;
697  }
698 
699 
707  template <int dim, int spacedim>
708  bool
709  has_distorted_children (const typename Triangulation<dim,spacedim>::cell_iterator &,
710  std::integral_constant<int, dim>,
711  std::integral_constant<int, spacedim>)
712  {
713  return false;
714  }
715 
716 
717 
722  template <int spacedim>
723  void
724  update_neighbors (Triangulation<1,spacedim> &)
725  {
726  }
727 
728 
729  template <int dim, int spacedim>
730  void
731  update_neighbors (Triangulation<dim,spacedim> &triangulation)
732  {
733  // each face can be neighbored on two sides
734  // by cells. according to the face's
735  // intrinsic normal we define the left
736  // neighbor as the one for which the face
737  // normal points outward, and store that
738  // one first; the second one is then
739  // the right neighbor for which the
740  // face normal points inward. This
741  // information depends on the type of cell
742  // and local number of face for the
743  // 'standard ordering and orientation' of
744  // faces and then on the face_orientation
745  // information for the real mesh. Set up a
746  // table to have fast access to those
747  // offsets (0 for left and 1 for
748  // right). Some of the values are invalid
749  // as they reference too large face
750  // numbers, but we just leave them at a
751  // zero value.
752  //
753  // Note, that in 2d for lines as faces the
754  // normal direction given in the
755  // GeometryInfo class is not consistent. We
756  // thus define here that the normal for a
757  // line points to the right if the line
758  // points upwards.
759  //
760  // There is one more point to
761  // consider, however: if we have
762  // dim<spacedim, then we may have
763  // cases where cells are
764  // inverted. In effect, both
765  // cells think they are the left
766  // neighbor of an edge, for
767  // example, which leads us to
768  // forget neighborship
769  // information (a case that shows
770  // this is
771  // codim_one/hanging_nodes_02). We
772  // store whether a cell is
773  // inverted using the
774  // direction_flag, so if a cell
775  // has a false direction_flag,
776  // then we need to invert our
777  // selection whether we are a
778  // left or right neighbor in all
779  // following computations.
780  //
781  // first index: dimension (minus 2)
782  // second index: local face index
783  // third index: face_orientation (false and true)
784  static const unsigned int left_right_offset[2][6][2] =
785  {
786  // quadrilateral
787  { {0,1}, // face 0, face_orientation = false and true
788  {1,0}, // face 1, face_orientation = false and true
789  {1,0}, // face 2, face_orientation = false and true
790  {0,1}, // face 3, face_orientation = false and true
791  {0,0}, // face 4, invalid face
792  {0,0}
793  },// face 5, invalid face
794  // hexahedron
795  { {0,1},
796  {1,0},
797  {0,1},
798  {1,0},
799  {0,1},
800  {1,0}
801  }
802  };
803 
804  // now create a vector of the two active
805  // neighbors (left and right) for each face
806  // and fill it by looping over all cells. For
807  // cases with anisotropic refinement and more
808  // then one cell neighboring at a given side
809  // of the face we will automatically get the
810  // active one on the highest level as we loop
811  // over cells from lower levels first.
812  const typename Triangulation<dim,spacedim>::cell_iterator dummy;
813  std::vector<typename Triangulation<dim,spacedim>::cell_iterator>
814  adjacent_cells(2*triangulation.n_raw_faces(), dummy);
815 
817  cell = triangulation.begin(),
818  endc = triangulation.end();
819  for (; cell != endc; ++cell)
820  for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
821  {
823  face=cell->face(f);
824 
825  const unsigned int
826  offset = (cell->direction_flag()
827  ?
828  left_right_offset[dim-2][f][cell->face_orientation(f)]
829  :
830  1-left_right_offset[dim-2][f][cell->face_orientation(f)]);
831 
832  adjacent_cells[2*face->index() + offset] = cell;
833 
834  // if this cell is not refined, but the
835  // face is, then we'll have to set our
836  // cell as neighbor for the child faces
837  // as well. Fortunately the normal
838  // orientation of children will be just
839  // the same.
840  if (dim==2)
841  {
842  if (cell->active() && face->has_children())
843  {
844  adjacent_cells[2*face->child(0)->index() + offset] = cell;
845  adjacent_cells[2*face->child(1)->index() + offset] = cell;
846  }
847  }
848  else // -> dim == 3
849  {
850  // We need the same as in 2d
851  // here. Furthermore, if the face is
852  // refined with cut_x or cut_y then
853  // those children again in the other
854  // direction, and if this cell is
855  // refined isotropically (along the
856  // face) then the neighbor will
857  // (probably) be refined as cut_x or
858  // cut_y along the face. For those
859  // neighboring children cells, their
860  // neighbor will be the current,
861  // inactive cell, as our children are
862  // too fine to be neighbors. Catch that
863  // case by also acting on inactive
864  // cells with isotropic refinement
865  // along the face. If the situation
866  // described is not present, the data
867  // will be overwritten later on when we
868  // visit cells on finer levels, so no
869  // harm will be done.
870  if (face->has_children() &&
871  (cell->active() ||
873  {
874 
875  for (unsigned int c=0; c<face->n_children(); ++c)
876  adjacent_cells[2*face->child(c)->index() + offset] = cell;
877  if (face->child(0)->has_children())
878  {
879  adjacent_cells[2*face->child(0)->child(0)->index() + offset] = cell;
880  adjacent_cells[2*face->child(0)->child(1)->index() + offset] = cell;
881  }
882  if (face->child(1)->has_children())
883  {
884  adjacent_cells[2*face->child(1)->child(0)->index() + offset] = cell;
885  adjacent_cells[2*face->child(1)->child(1)->index() + offset] = cell;
886  }
887  } // if cell active and face refined
888  } // else -> dim==3
889  } // for all faces of all cells
890 
891  // now loop again over all cells and set the
892  // corresponding neighbor cell. Note, that we
893  // have to use the opposite of the
894  // left_right_offset in this case as we want
895  // the offset of the neighbor, not our own.
896  for (cell=triangulation.begin(); cell != endc; ++cell)
897  for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
898  {
899  const unsigned int
900  offset = (cell->direction_flag()
901  ?
902  left_right_offset[dim-2][f][cell->face_orientation(f)]
903  :
904  1-left_right_offset[dim-2][f][cell->face_orientation(f)]);
905  cell->set_neighbor(f,
906  adjacent_cells[2*cell->face(f)->index() + 1 - offset]);
907  }
908  }
909 
910 
911  template <int dim, int spacedim>
912  void update_periodic_face_map_recursively
913  (const typename Triangulation<dim,spacedim>::cell_iterator &cell_1,
914  const typename Triangulation<dim,spacedim>::cell_iterator &cell_2,
915  unsigned int n_face_1, unsigned int n_face_2,
916  const std::bitset<3> &orientation,
917  typename std::map<std::pair<typename Triangulation<dim, spacedim>::cell_iterator, unsigned int>,
918  std::pair<std::pair<typename Triangulation<dim, spacedim>::cell_iterator, unsigned int>, std::bitset<3> > > &periodic_face_map)
919  {
920  typedef typename Triangulation<dim, spacedim>::face_iterator FaceIterator;
921  const FaceIterator face_1 = cell_1->face(n_face_1);
922  const FaceIterator face_2 = cell_2->face(n_face_2);
923 
924  const bool face_orientation = orientation[0];
925  const bool face_flip = orientation[1];
926  const bool face_rotation = orientation[2];
927 
928  Assert((dim != 1) || (face_orientation == true && face_flip == false && face_rotation == false),
929  ExcMessage ("The supplied orientation " "(face_orientation, face_flip, face_rotation) " "is invalid for 1D"));
930 
931  Assert((dim != 2) || (face_orientation == true && face_rotation == false),
932  ExcMessage ("The supplied orientation " "(face_orientation, face_flip, face_rotation) " "is invalid for 2D"));
933 
934  Assert(face_1 != face_2,
935  ExcMessage ("face_1 and face_2 are equal!"));
936 
937  Assert(face_1->at_boundary() &&face_2->at_boundary(),
938  ExcMessage ("Periodic faces must be on the boundary"));
939 
940  // insert periodic face pair for both cells
941  typedef std::pair<typename Triangulation<dim,spacedim>::cell_iterator, unsigned int> CellFace;
942  const CellFace cell_face_1 (cell_1, n_face_1);
943  const CellFace cell_face_2 (cell_2, n_face_2);
944  const std::pair<CellFace, std::bitset<3> > cell_face_orientation_2 (cell_face_2, orientation);
945 
946  const std::pair<CellFace, std::pair<CellFace, std::bitset<3> > > periodic_faces (cell_face_1, cell_face_orientation_2);
947 
948  // Only one periodic neighbor is allowed
949  Assert(periodic_face_map.count(cell_face_1) == 0, ExcInternalError());
950  periodic_face_map.insert(periodic_faces);
951 
952  // A lookup table on how to go through the child cells depending on the
953  // orientation:
954  // see Documentation of GeometryInfo for details
955 
956  static const int lookup_table_2d[2][2] =
957  // flip:
958  {
959  { 0, 1 }, // false
960  { 1, 0 } // true
961  };
962 
963  static const int lookup_table_3d[2][2][2][4] =
964  // orientation flip rotation
965  {
966  { { { 0, 2, 1, 3 }, // false false false
967  { 2, 3, 0, 1 } // false false true
968  },
969  { { 3, 1, 2, 0 }, // false true false
970  { 1, 0, 3, 2 } // false true true
971  }
972  },
973  { { { 0, 1, 2, 3 }, // true false false
974  { 1, 3, 0, 2 } // true false true
975  },
976  { { 3, 2, 1, 0 }, // true true false
977  { 2, 0, 3, 1 } // true true true
978  }
979  }
980  };
981 
982  if (cell_1->has_children())
983  {
984  if (cell_2->has_children())
985  {
986  // In the case that both faces have children, we loop over all
987  // children and apply update_periodic_face_map_recursively recursively:
988 
989  Assert(face_1->n_children() == GeometryInfo<dim>::max_children_per_face
992 
993  for (unsigned int i = 0; i < GeometryInfo<dim>::max_children_per_face; ++i)
994  {
995  // Lookup the index for the second face
996  unsigned int j = 0;
997  switch (dim)
998  {
999  case 2:
1000  j = lookup_table_2d[face_flip][i];
1001  break;
1002  case 3:
1003  j = lookup_table_3d[face_orientation][face_flip][face_rotation][i];
1004  break;
1005  default:
1006  AssertThrow(false, ExcNotImplemented());
1007  }
1008 
1009  // find subcell ids that belong to the subface indices
1010  unsigned int child_cell_1
1012  (cell_1->refinement_case(), n_face_1, i, cell_1->face_orientation(n_face_1),
1013  cell_1->face_flip(n_face_1), cell_1->face_rotation(n_face_1), face_1->refinement_case());
1014  unsigned int child_cell_2
1016  (cell_2->refinement_case(), n_face_2, j, cell_2->face_orientation(n_face_2),
1017  cell_2->face_flip(n_face_2), cell_2->face_rotation(n_face_2), face_2->refinement_case());
1018 
1019  Assert(cell_1->child(child_cell_1)->face(n_face_1) == face_1->child(i), ExcInternalError());
1020  Assert(cell_2->child(child_cell_2)->face(n_face_2) == face_2->child(j), ExcInternalError());
1021 
1022  // precondition: subcell has the same orientation as cell (so that the face numbers coincide)
1023  // recursive call
1024  update_periodic_face_map_recursively<dim, spacedim>
1025  (cell_1->child(child_cell_1), cell_2->child(child_cell_2),
1026  n_face_1, n_face_2, orientation, periodic_face_map);
1027  }
1028  }
1029  else //only face_1 has children
1030  {
1031  for (unsigned int i = 0; i < GeometryInfo<dim>::max_children_per_face; ++i)
1032  {
1033  // find subcell ids that belong to the subface indices
1034  unsigned int child_cell_1
1035  = GeometryInfo<dim>::child_cell_on_face(cell_1->refinement_case(), n_face_1, i, cell_1->face_orientation(n_face_1),
1036  cell_1->face_flip(n_face_1), cell_1->face_rotation(n_face_1), face_1->refinement_case());
1037 
1038  // recursive call
1039  update_periodic_face_map_recursively<dim, spacedim>
1040  (cell_1->child(child_cell_1), cell_2,
1041  n_face_1, n_face_2, orientation, periodic_face_map);
1042  }
1043  }
1044 
1045  }
1046  }
1047 
1048 
1049 }// end of anonymous namespace
1050 
1051 
1052 namespace internal
1053 {
1054  namespace TriangulationImplementation
1055  {
1056  // make sure that if in the following we
1057  // write Triangulation<dim,spacedim>
1058  // we mean the *class*
1059  // ::Triangulation, not the
1060  // enclosing namespace
1061  // internal::TriangulationImplementation
1062  using ::Triangulation;
1063 
1069  int,
1070  << "Something went wrong when making cell " << arg1
1071  << ". Read the docs and the source code "
1072  << "for more information.");
1078  int,
1079  << "Something went wrong upon construction of cell "
1080  << arg1);
1091  int,
1092  << "Cell " << arg1 << " has negative measure. This typically "
1093  << "indicates some distortion in the cell, or a mistakenly "
1094  << "swapped pair of vertices in the input to "
1095  << "Triangulation::create_triangulation().");
1104  int, int, int,
1105  << "Error while creating cell " << arg1
1106  << ": the vertex index " << arg2 << " must be between 0 and "
1107  << arg3 << ".");
1113  int, int,
1114  << "While trying to assign a boundary indicator to a line: "
1115  << "the line with end vertices " << arg1 << " and "
1116  << arg2 << " does not exist.");
1122  int, int, int, int,
1123  << "While trying to assign a boundary indicator to a quad: "
1124  << "the quad with bounding lines " << arg1 << ", " << arg2
1125  << ", " << arg3 << ", " << arg4 << " does not exist.");
1131  int, int,
1133  << "The input data for creating a triangulation contained "
1134  << "information about a line with indices "
1135  << arg1 << " and " << arg2
1136  << " that is described to have boundary indicator "
1137  << (int)arg3
1138  << ". However, this is an internal line not located on the "
1139  << "boundary. You cannot assign a boundary indicator to it."
1140  << std::endl
1141  << std::endl
1142  << "If this happened at a place where you call "
1143  << "Triangulation::create_triangulation() yourself, you need "
1144  << "to check the SubCellData object you pass to this function."
1145  << std::endl
1146  << std::endl
1147  << "If this happened in a place where you are reading a mesh "
1148  << "from a file, then you need to investigate why such a line "
1149  << "ended up in the input file. A typical case is a geometry "
1150  << "that consisted of multiple parts and for which the mesh "
1151  << "generator program assumes that the interface between "
1152  << "two parts is a boundary when that isn't supposed to be "
1153  << "the case, or where the mesh generator simply assigns "
1154  << "'geometry indicators' to lines at the perimeter of "
1155  << "a part that are not supposed to be interpreted as "
1156  << "'boundary indicators'.");
1162  int, int, int, int,
1164  << "The input data for creating a triangulation contained "
1165  << "information about a quad with indices "
1166  << arg1 << ", " << arg2 << ", " << arg3 << ", and " << arg4
1167  << " that is described to have boundary indicator "
1168  << (int)arg5
1169  << ". However, this is an internal quad not located on the "
1170  << "boundary. You cannot assign a boundary indicator to it."
1171  << std::endl
1172  << std::endl
1173  << "If this happened at a place where you call "
1174  << "Triangulation::create_triangulation() yourself, you need "
1175  << "to check the SubCellData object you pass to this function."
1176  << std::endl
1177  << std::endl
1178  << "If this happened in a place where you are reading a mesh "
1179  << "from a file, then you need to investigate why such a quad "
1180  << "ended up in the input file. A typical case is a geometry "
1181  << "that consisted of multiple parts and for which the mesh "
1182  << "generator program assumes that the interface between "
1183  << "two parts is a boundary when that isn't supposed to be "
1184  << "the case, or where the mesh generator simply assigns "
1185  << "'geometry indicators' to quads at the surface of "
1186  << "a part that are not supposed to be interpreted as "
1187  << "'boundary indicators'.");
1193  int, int,
1194  << "In SubCellData the line info of the line with vertex indices "
1195  << arg1 << " and " << arg2 << " appears more than once. "
1196  << "This is not allowed.");
1197 
1198 
1295  {
1307  template <int dim, int spacedim>
1308  static
1310  const unsigned int level_objects,
1312  {
1313  typedef
1314  typename Triangulation<dim,spacedim>::line_iterator line_iterator;
1315 
1316  number_cache.n_levels = 0;
1317  if (level_objects > 0)
1318  // find the last level on which there are used cells
1319  for (unsigned int level=0; level<level_objects; ++level)
1320  if (triangulation.begin(level) !=
1321  triangulation.end(level))
1322  number_cache.n_levels = level+1;
1323 
1324  // no cells at all?
1325  Assert (number_cache.n_levels > 0, ExcInternalError());
1326 
1328  // update the number of lines on the different levels in the
1329  // cache
1330  number_cache.n_lines = 0;
1331  number_cache.n_active_lines = 0;
1332 
1333  // for 1d, lines have levels so take count the objects per
1334  // level and globally
1335  if (dim == 1)
1336  {
1337  number_cache.n_lines_level.resize (number_cache.n_levels);
1338  number_cache.n_active_lines_level.resize (number_cache.n_levels);
1339 
1340  for (unsigned int level=0; level<number_cache.n_levels; ++level)
1341  {
1342  // count lines on this level
1343  number_cache.n_lines_level[level] = 0;
1344  number_cache.n_active_lines_level[level] = 0;
1345 
1346  line_iterator line = triangulation.begin_line (level),
1347  endc = (level == number_cache.n_levels-1 ?
1348  line_iterator(triangulation.end_line()) :
1349  triangulation.begin_line (level+1));
1350  for (; line!=endc; ++line)
1351  {
1352  ++number_cache.n_lines_level[level];
1353  if (line->has_children() == false)
1354  ++number_cache.n_active_lines_level[level];
1355  }
1356 
1357  // update total number of lines
1358  number_cache.n_lines += number_cache.n_lines_level[level];
1359  number_cache.n_active_lines += number_cache.n_active_lines_level[level];
1360  }
1361  }
1362  else
1363  {
1364  // for dim>1, there are no levels for lines
1365  number_cache.n_lines_level.clear ();
1366  number_cache.n_active_lines_level.clear ();
1367 
1368  line_iterator line = triangulation.begin_line (),
1369  endc = triangulation.end_line();
1370  for (; line!=endc; ++line)
1371  {
1372  ++number_cache.n_lines;
1373  if (line->has_children() == false)
1374  ++number_cache.n_active_lines;
1375  }
1376  }
1377  }
1378 
1393  template <int dim, int spacedim>
1394  static
1396  const unsigned int level_objects,
1398  {
1399  // update lines and n_levels in number_cache. since we don't
1400  // access any of these numbers, we can do this in the
1401  // background
1402  Threads::Task<void> update_lines
1403  = Threads::new_task ((void (*)(const Triangulation<dim,spacedim> &,
1404  const unsigned int,
1406  (&compute_number_cache<dim,spacedim>),
1407  triangulation,
1408  level_objects,
1410  (number_cache));
1411 
1412  typedef
1413  typename Triangulation<dim,spacedim>::quad_iterator quad_iterator;
1414 
1416  // update the number of quads on the different levels in the
1417  // cache
1418  number_cache.n_quads = 0;
1419  number_cache.n_active_quads = 0;
1420 
1421  // for 2d, quads have levels so take count the objects per
1422  // level and globally
1423  if (dim == 2)
1424  {
1425  // count the number of levels; the function we called above
1426  // on a separate Task for lines also does this and puts it into
1427  // number_cache.n_levels, but this datum may not yet be
1428  // available as we call the function on a separate task
1429  unsigned int n_levels = 0;
1430  if (level_objects > 0)
1431  // find the last level on which there are used cells
1432  for (unsigned int level=0; level<level_objects; ++level)
1433  if (triangulation.begin(level) !=
1434  triangulation.end(level))
1435  n_levels = level+1;
1436 
1437  number_cache.n_quads_level.resize (n_levels);
1438  number_cache.n_active_quads_level.resize (n_levels);
1439 
1440  for (unsigned int level=0; level<n_levels; ++level)
1441  {
1442  // count quads on this level
1443  number_cache.n_quads_level[level] = 0;
1444  number_cache.n_active_quads_level[level] = 0;
1445 
1446  quad_iterator quad = triangulation.begin_quad (level),
1447  endc = (level == n_levels-1 ?
1448  quad_iterator(triangulation.end_quad()) :
1449  triangulation.begin_quad (level+1));
1450  for (; quad!=endc; ++quad)
1451  {
1452  ++number_cache.n_quads_level[level];
1453  if (quad->has_children() == false)
1454  ++number_cache.n_active_quads_level[level];
1455  }
1456 
1457  // update total number of quads
1458  number_cache.n_quads += number_cache.n_quads_level[level];
1459  number_cache.n_active_quads += number_cache.n_active_quads_level[level];
1460  }
1461  }
1462  else
1463  {
1464  // for dim>2, there are no levels for quads
1465  number_cache.n_quads_level.clear ();
1466  number_cache.n_active_quads_level.clear ();
1467 
1468  quad_iterator quad = triangulation.begin_quad (),
1469  endc = triangulation.end_quad();
1470  for (; quad!=endc; ++quad)
1471  {
1472  ++number_cache.n_quads;
1473  if (quad->has_children() == false)
1474  ++number_cache.n_active_quads;
1475  }
1476  }
1477 
1478  // wait for the background computation for lines
1479  update_lines.join ();
1480  }
1481 
1497  template <int dim, int spacedim>
1498  static
1500  const unsigned int level_objects,
1502  {
1503  // update quads, lines and n_levels in number_cache. since we
1504  // don't access any of these numbers, we can do this in the
1505  // background
1506  Threads::Task<void> update_quads_and_lines
1507  = Threads::new_task ((void (*)(const Triangulation<dim,spacedim> &,
1508  const unsigned int,
1510  (&compute_number_cache<dim,spacedim>),
1511  triangulation,
1512  level_objects,
1514  (number_cache));
1515 
1516  typedef
1517  typename Triangulation<dim,spacedim>::hex_iterator hex_iterator;
1518 
1520  // update the number of hexes on the different levels in the
1521  // cache
1522  number_cache.n_hexes = 0;
1523  number_cache.n_active_hexes = 0;
1524 
1525  // for 3d, hexes have levels so take count the objects per
1526  // level and globally
1527  if (dim == 3)
1528  {
1529  // count the number of levels; the function we called
1530  // above on a separate Task for quads (recursively, via
1531  // the lines function) also does this and puts it into
1532  // number_cache.n_levels, but this datum may not yet be
1533  // available as we call the function on a separate task
1534  unsigned int n_levels = 0;
1535  if (level_objects > 0)
1536  // find the last level on which there are used cells
1537  for (unsigned int level=0; level<level_objects; ++level)
1538  if (triangulation.begin(level) !=
1539  triangulation.end(level))
1540  n_levels = level+1;
1541 
1542  number_cache.n_hexes_level.resize (n_levels);
1543  number_cache.n_active_hexes_level.resize (n_levels);
1544 
1545  for (unsigned int level=0; level<n_levels; ++level)
1546  {
1547  // count hexes on this level
1548  number_cache.n_hexes_level[level] = 0;
1549  number_cache.n_active_hexes_level[level] = 0;
1550 
1551  hex_iterator hex = triangulation.begin_hex (level),
1552  endc = (level == n_levels-1 ?
1553  hex_iterator(triangulation.end_hex()) :
1554  triangulation.begin_hex (level+1));
1555  for (; hex!=endc; ++hex)
1556  {
1557  ++number_cache.n_hexes_level[level];
1558  if (hex->has_children() == false)
1559  ++number_cache.n_active_hexes_level[level];
1560  }
1561 
1562  // update total number of hexes
1563  number_cache.n_hexes += number_cache.n_hexes_level[level];
1564  number_cache.n_active_hexes += number_cache.n_active_hexes_level[level];
1565  }
1566  }
1567  else
1568  {
1569  // for dim>3, there are no levels for hexes
1570  number_cache.n_hexes_level.clear ();
1571  number_cache.n_active_hexes_level.clear ();
1572 
1573  hex_iterator hex = triangulation.begin_hex (),
1574  endc = triangulation.end_hex();
1575  for (; hex!=endc; ++hex)
1576  {
1577  ++number_cache.n_hexes;
1578  if (hex->has_children() == false)
1579  ++number_cache.n_active_hexes;
1580  }
1581  }
1582 
1583  // wait for the background computation for quads
1584  update_quads_and_lines.join ();
1585  }
1586 
1587 
1595  template <int spacedim>
1596  static
1597  void
1598  create_triangulation (const std::vector<Point<spacedim> > &v,
1599  const std::vector<CellData<1> > &cells,
1600  const SubCellData &/*subcelldata*/,
1601  Triangulation<1,spacedim> &triangulation)
1602  {
1603  AssertThrow (v.size() > 0, ExcMessage ("No vertices given"));
1604  AssertThrow (cells.size() > 0, ExcMessage ("No cells given"));
1605 
1606  // note: since no boundary
1607  // information can be given in one
1608  // dimension, the @p{subcelldata}
1609  // field is ignored. (only used for
1610  // error checking, which is a good
1611  // idea in any case)
1612  const unsigned int dim=1;
1613 
1614  // copy vertices
1615  triangulation.vertices = v;
1616  triangulation.vertices_used = std::vector<bool> (v.size(), true);
1617 
1618  // Check that all cells have positive volume. This check is not run in
1619  // the codimension one or two cases since cell_measure is not
1620  // implemented for those.
1621 #ifndef _MSC_VER
1622  //TODO: The following code does not compile with MSVC. Find a way around it
1623  if (dim == spacedim)
1624  {
1625  for (unsigned int cell_no = 0; cell_no<cells.size(); ++cell_no)
1626  {
1627  // If we should check for distorted cells, then we permit them
1628  // to exist. If a cell has negative measure, then it must be
1629  // distorted (the converse is not necessarily true); hence
1630  // throw an exception if no such cells should exist.
1631  if (!triangulation.check_for_distorted_cells)
1632  {
1633  const double cell_measure = GridTools::cell_measure<1>
1634  (triangulation.vertices, cells[cell_no].vertices);
1635  AssertThrow(cell_measure > 0, ExcGridHasInvalidCell(cell_no));
1636  }
1637  }
1638  }
1639 #endif
1640 
1641 
1642  // store the indices of the lines
1643  // which are adjacent to a given
1644  // vertex
1645  std::vector<std::vector<int> > lines_at_vertex (v.size());
1646 
1647  // reserve enough space
1648  triangulation.levels.push_back (std_cxx14::make_unique<internal::TriangulationImplementation::TriaLevel<dim>>());
1649  triangulation.levels[0]->reserve_space (cells.size(), dim, spacedim);
1650  triangulation.levels[0]->cells.reserve_space (0, cells.size());
1651 
1652  // make up cells
1653  typename Triangulation<dim,spacedim>::raw_line_iterator
1654  next_free_line = triangulation.begin_raw_line ();
1655  for (unsigned int cell=0; cell<cells.size(); ++cell)
1656  {
1657  while (next_free_line->used())
1658  ++next_free_line;
1659 
1660  next_free_line->set (internal::TriangulationImplementation
1661  ::TriaObject<1> (cells[cell].vertices[0],
1662  cells[cell].vertices[1]));
1663  next_free_line->set_used_flag ();
1664  next_free_line->set_material_id (cells[cell].material_id);
1665  next_free_line->set_manifold_id (cells[cell].manifold_id);
1666  next_free_line->clear_user_data ();
1667  next_free_line->set_subdomain_id (0);
1668 
1669  // note that this cell is
1670  // adjacent to these vertices
1671  lines_at_vertex[cells[cell].vertices[0]].push_back (cell);
1672  lines_at_vertex[cells[cell].vertices[1]].push_back (cell);
1673  }
1674 
1675 
1676  // some security tests
1677  {
1678  unsigned int boundary_nodes = 0;
1679  for (unsigned int i=0; i<lines_at_vertex.size(); ++i)
1680  switch (lines_at_vertex[i].size())
1681  {
1682  case 1:
1683  // this vertex has only
1684  // one adjacent line
1685  ++boundary_nodes;
1686  break;
1687  case 2:
1688  break;
1689  default:
1690  AssertThrow (false,
1691  ExcMessage ("You have a vertex in your triangulation "
1692  "at which more than two cells come together. "
1693  "(For one dimensional triangulation, cells are "
1694  "line segments.)"
1695  "\n\n"
1696  "This is not currently supported because the "
1697  "Triangulation class makes the assumption that "
1698  "every cell has zero or one neighbors behind "
1699  "each face (here, behind each vertex), but in your "
1700  "situation there would be more than one."
1701  "\n\n"
1702  "Support for this is not currently implemented. "
1703  "If you need to work with triangulations where "
1704  "more than two cells come together at a vertex, "
1705  "duplicate the vertices once per cell (i.e., put "
1706  "multiple vertices at the same physical location, "
1707  "but using different vertex indices for each) "
1708  "and then ensure continuity of the solution by "
1709  "explicitly creating constraints that the degrees "
1710  "of freedom at these vertices have the same "
1711  "value, using the ConstraintMatrix class."));
1712  }
1713 
1714  // assert there are no more
1715  // than two boundary
1716  // nodes. note that if the
1717  // space dimension is
1718  // bigger than 1, then we
1719  // can have fewer than 2
1720  // nodes (for example a
1721  // ring of cells -- no end
1722  // points at all)
1723  AssertThrow (((spacedim == 1) && (boundary_nodes == 2))
1724  ||
1725  (spacedim > 1),
1726  ExcMessage("The Triangulation has too many end points"));
1727  }
1728 
1729 
1730 
1731  // update neighborship info
1732  typename Triangulation<dim,spacedim>::active_line_iterator
1733  line = triangulation.begin_active_line ();
1734  // for all lines
1735  for (; line!=triangulation.end(); ++line)
1736  // for each of the two vertices
1737  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell; ++vertex)
1738  // if first cell adjacent to
1739  // this vertex is the present
1740  // one, then the neighbor is
1741  // the second adjacent cell and
1742  // vice versa
1743  if (lines_at_vertex[line->vertex_index(vertex)][0] == line->index())
1744  if (lines_at_vertex[line->vertex_index(vertex)].size() == 2)
1745  {
1747  neighbor (&triangulation,
1748  0, // level
1749  lines_at_vertex[line->vertex_index(vertex)][1]);
1750  line->set_neighbor (vertex, neighbor);
1751  }
1752  else
1753  // no second adjacent cell
1754  // entered -> cell at
1755  // boundary
1756  line->set_neighbor (vertex, triangulation.end());
1757  else
1758  // present line is not first
1759  // adjacent one -> first
1760  // adjacent one is neighbor
1761  {
1763  neighbor (&triangulation,
1764  0, // level
1765  lines_at_vertex[line->vertex_index(vertex)][0]);
1766  line->set_neighbor (vertex, neighbor);
1767  }
1768 
1769  // finally set the
1770  // vertex_to_boundary_id_map_1d
1771  // and vertex_to_manifold_id_map_1d
1772  // maps
1773  triangulation.vertex_to_boundary_id_map_1d->clear();
1774  triangulation.vertex_to_manifold_id_map_1d->clear();
1776  cell = triangulation.begin_active();
1777  cell != triangulation.end(); ++cell)
1778  for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
1779  {
1780  (*triangulation
1781  .vertex_to_manifold_id_map_1d)[cell->face(f)->vertex_index()]
1783 
1784  if (cell->at_boundary(f))
1785  (*triangulation
1786  .vertex_to_boundary_id_map_1d)[cell->face(f)->vertex_index()]
1787  = f;
1788  }
1789  }
1790 
1791 
1799  template <int spacedim>
1800  static
1801  void
1802  create_triangulation (const std::vector<Point<spacedim> > &v,
1803  const std::vector<CellData<2> > &cells,
1804  const SubCellData &subcelldata,
1805  Triangulation<2,spacedim> &triangulation)
1806  {
1807  AssertThrow (v.size() > 0, ExcMessage ("No vertices given"));
1808  AssertThrow (cells.size() > 0, ExcMessage ("No cells given"));
1809 
1810  const unsigned int dim=2;
1811 
1812  // copy vertices
1813  triangulation.vertices = v;
1814  triangulation.vertices_used = std::vector<bool> (v.size(), true);
1815 
1816  // Check that all cells have positive volume. This check is not run in
1817  // the codimension one or two cases since cell_measure is not
1818  // implemented for those.
1819 #ifndef _MSC_VER
1820  //TODO: The following code does not compile with MSVC. Find a way around it
1821  if (dim == spacedim)
1822  {
1823  for (unsigned int cell_no = 0; cell_no<cells.size(); ++cell_no)
1824  {
1825  // See the note in the 1D function on this if statement.
1826  if (!triangulation.check_for_distorted_cells)
1827  {
1828  const double cell_measure = GridTools::cell_measure<2>
1829  (triangulation.vertices, cells[cell_no].vertices);
1830  AssertThrow(cell_measure > 0, ExcGridHasInvalidCell(cell_no));
1831  }
1832  }
1833  }
1834 #endif
1835 
1836  // make up a list of the needed
1837  // lines each line is a pair of
1838  // vertices. The list is kept
1839  // sorted and it is guaranteed that
1840  // each line is inserted only once.
1841  // While the key of such an entry
1842  // is the pair of vertices, the
1843  // thing it points to is an
1844  // iterator pointing to the line
1845  // object itself. In the first run,
1846  // these iterators are all invalid
1847  // ones, but they are filled
1848  // afterwards
1849  std::map<std::pair<int,int>,
1850  typename Triangulation<dim,spacedim>::line_iterator> needed_lines;
1851  for (unsigned int cell=0; cell<cells.size(); ++cell)
1852  {
1853  for (unsigned int vertex=0; vertex<4; ++vertex)
1854  AssertThrow (cells[cell].vertices[vertex] < triangulation.vertices.size(),
1855  ExcInvalidVertexIndex (cell, cells[cell].vertices[vertex],
1856  triangulation.vertices.size()));
1857 
1858  for (unsigned int line=0; line<GeometryInfo<dim>::faces_per_cell; ++line)
1859  {
1860  // given a line vertex number (0,1) on a specific line
1861  // we get the cell vertex number (0-4) through the
1862  // line_to_cell_vertices function
1863  std::pair<int,int> line_vertices(
1864  cells[cell].vertices[GeometryInfo<dim>::line_to_cell_vertices(line, 0)],
1865  cells[cell].vertices[GeometryInfo<dim>::line_to_cell_vertices(line, 1)]);
1866 
1867  // assert that the line was not already inserted in
1868  // reverse order. This happens in spite of the vertex
1869  // rotation above, if the sense of the cell was
1870  // incorrect.
1871  //
1872  // Here is what usually happened when this exception
1873  // is thrown: consider these two cells and the
1874  // vertices
1875  // 3---4---5
1876  // | | |
1877  // 0---1---2
1878  // If in the input vector the two cells are given with
1879  // vertices <0 1 3 4> and <4 1 5 2>, in the first cell
1880  // the middle line would have direction 1->4, while in
1881  // the second it would be 4->1. This will cause the
1882  // exception.
1883  AssertThrow (needed_lines.find(std::make_pair(line_vertices.second,
1884  line_vertices.first))
1885  ==
1886  needed_lines.end(),
1887  ExcGridHasInvalidCell(cell));
1888 
1889  // insert line, with
1890  // invalid iterator if line
1891  // already exists, then
1892  // nothing bad happens here
1893  needed_lines[line_vertices] = triangulation.end_line();
1894  }
1895  }
1896 
1897 
1898  // check that every vertex has at
1899  // least two adjacent lines
1900  {
1901  std::vector<unsigned short int> vertex_touch_count (v.size(), 0);
1902  typename std::map<std::pair<int,int>,
1903  typename Triangulation<dim,spacedim>::line_iterator>::iterator i;
1904  for (i=needed_lines.begin(); i!=needed_lines.end(); ++i)
1905  {
1906  // touch the vertices of
1907  // this line
1908  ++vertex_touch_count[i->first.first];
1909  ++vertex_touch_count[i->first.second];
1910  }
1911 
1912  // assert minimum touch count
1913  // is at least two. if not so,
1914  // then clean triangulation and
1915  // exit with an exception
1916  AssertThrow (* (std::min_element(vertex_touch_count.begin(),
1917  vertex_touch_count.end())) >= 2,
1918  ExcMessage("During creation of a triangulation, a part of the "
1919  "algorithm encountered a vertex that is part of only "
1920  "a single adjacent line. However, in 2d, every vertex "
1921  "needs to be at least part of two lines."));
1922  }
1923 
1924  // reserve enough space
1925  triangulation.levels.push_back (std_cxx14::make_unique<internal::TriangulationImplementation::TriaLevel<dim>>());
1926  triangulation.faces = std_cxx14::make_unique<internal::TriangulationImplementation::TriaFaces<dim>> ();
1927  triangulation.levels[0]->reserve_space (cells.size(), dim, spacedim);
1928  triangulation.faces->lines.reserve_space (0,needed_lines.size());
1929  triangulation.levels[0]->cells.reserve_space (0,cells.size());
1930 
1931  // make up lines
1932  {
1933  typename Triangulation<dim,spacedim>::raw_line_iterator
1934  line = triangulation.begin_raw_line();
1935  typename std::map<std::pair<int,int>,
1936  typename Triangulation<dim,spacedim>::line_iterator>::iterator i;
1937  for (i = needed_lines.begin();
1938  line!=triangulation.end_line(); ++line, ++i)
1939  {
1940  line->set (internal::TriangulationImplementation::TriaObject<1>(i->first.first,
1941  i->first.second));
1942  line->set_used_flag ();
1943  line->clear_user_flag ();
1944  line->clear_user_data ();
1945  i->second = line;
1946  }
1947  }
1948 
1949 
1950  // store for each line index
1951  // the adjacent cells
1952  std::map<int,std::vector<typename Triangulation<dim,spacedim>::cell_iterator> >
1953  adjacent_cells;
1954 
1955  // finally make up cells
1956  {
1958  cell = triangulation.begin_raw_quad();
1959  for (unsigned int c=0; c<cells.size(); ++c, ++cell)
1960  {
1961  typename Triangulation<dim,spacedim>::line_iterator
1963  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
1964  lines[line]=needed_lines[std::make_pair(
1965  cells[c].vertices[GeometryInfo<dim>::line_to_cell_vertices(line, 0)],
1966  cells[c].vertices[GeometryInfo<dim>::line_to_cell_vertices(line, 1)])];
1967 
1968  cell->set (internal::TriangulationImplementation::TriaObject<2> (lines[0]->index(),
1969  lines[1]->index(),
1970  lines[2]->index(),
1971  lines[3]->index()));
1972 
1973  cell->set_used_flag ();
1974  cell->set_material_id (cells[c].material_id);
1975  cell->set_manifold_id (cells[c].manifold_id);
1976  cell->clear_user_data ();
1977  cell->set_subdomain_id (0);
1978 
1979  // note that this cell is
1980  // adjacent to the four
1981  // lines
1982  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
1983  adjacent_cells[lines[line]->index()].push_back (cell);
1984  }
1985  }
1986 
1987 
1988  for (typename Triangulation<dim,spacedim>::line_iterator
1989  line=triangulation.begin_line();
1990  line!=triangulation.end_line(); ++line)
1991  {
1992  const unsigned int n_adj_cells = adjacent_cells[line->index()].size();
1993 
1994  // assert that every line has one or two adjacent cells.
1995  // this has to be the case for 2d triangulations in 2d.
1996  // in higher dimensions, this may happen but is not
1997  // implemented
1998  if (spacedim==2)
1999  AssertThrow ((n_adj_cells >= 1) &&
2000  (n_adj_cells <= 2),
2001  ExcInternalError())
2002  else
2003  AssertThrow ((n_adj_cells >= 1) &&
2004  (n_adj_cells <= 2),
2005  ExcMessage ("You have a line in your triangulation "
2006  "at which more than two cells come together. "
2007  "\n\n"
2008  "This is not currently supported because the "
2009  "Triangulation class makes the assumption that "
2010  "every cell has zero or one neighbors behind "
2011  "each face (here, behind each line), but in your "
2012  "situation there would be more than one."
2013  "\n\n"
2014  "Support for this is not currently implemented. "
2015  "If you need to work with triangulations where "
2016  "more than two cells come together at a line, "
2017  "duplicate the vertices once per cell (i.e., put "
2018  "multiple vertices at the same physical location, "
2019  "but using different vertex indices for each) "
2020  "and then ensure continuity of the solution by "
2021  "explicitly creating constraints that the degrees "
2022  "of freedom at these lines have the same "
2023  "value, using the ConstraintMatrix class."));
2024 
2025  // if only one cell: line is at boundary -> give it the boundary
2026  // indicator zero by default
2027  line->set_boundary_id_internal((n_adj_cells == 1) ? 0 : numbers::internal_face_boundary_id);
2028  line->set_manifold_id(numbers::flat_manifold_id);
2029  }
2030 
2031  // set boundary indicators where given
2032  std::vector<CellData<1> >::const_iterator boundary_line
2033  = subcelldata.boundary_lines.begin();
2034  std::vector<CellData<1> >::const_iterator end_boundary_line
2035  = subcelldata.boundary_lines.end();
2036  for (; boundary_line!=end_boundary_line; ++boundary_line)
2037  {
2038  typename Triangulation<dim,spacedim>::line_iterator line;
2039  std::pair<int,int> line_vertices(std::make_pair(boundary_line->vertices[0],
2040  boundary_line->vertices[1]));
2041  if (needed_lines.find(line_vertices) != needed_lines.end())
2042  // line found in this direction
2043  line = needed_lines[line_vertices];
2044  else
2045  {
2046  // look whether it exists in reverse direction
2047  std::swap (line_vertices.first, line_vertices.second);
2048  if (needed_lines.find(line_vertices) != needed_lines.end())
2049  line = needed_lines[line_vertices];
2050  else
2051  // line does not exist
2052  AssertThrow (false, ExcLineInexistant(line_vertices.first,
2053  line_vertices.second));
2054  }
2055 
2056  // assert that we only set boundary info once
2057  AssertThrow (! (line->boundary_id() != 0 &&
2058  line->boundary_id() != numbers::internal_face_boundary_id),
2059  ExcMultiplySetLineInfoOfLine(line_vertices.first,
2060  line_vertices.second));
2061 
2062  // Assert that only exterior lines are given a boundary
2063  // indicator; however, it is possible that someone may
2064  // want to give an interior line a manifold id (and thus
2065  // lists this line in the subcell_data structure), and we
2066  // need to allow that
2067  if (boundary_line->boundary_id != numbers::internal_face_boundary_id)
2068  {
2069  if (line->boundary_id() == numbers::internal_face_boundary_id)
2070  {
2071  // if we are here, it means that we want to assign a boundary indicator
2072  // different from numbers::internal_face_boundary_id to an internal line.
2073  // As said, this would be not allowed, and an exception should be immediately
2074  // thrown. Still, there is the possibility that one only wants to specify a
2075  // manifold_id here. If that is the case (manifold_id != numbers::flat_manifold_id)
2076  // the operation is allowed. Otherwise, we really tried to specify a boundary_id
2077  // (and not a manifold_id) to an internal face. The exception must be thrown.
2078  if (boundary_line->manifold_id == numbers::flat_manifold_id)
2079  {
2080  // If we are here, this assertion will surely fail, for the aforementioned
2081  // reasons
2082  AssertThrow (! (line->boundary_id() == numbers::internal_face_boundary_id),
2083  ExcInteriorLineCantBeBoundary(line->vertex_index(0),
2084  line->vertex_index(1),
2085  boundary_line->boundary_id));
2086  }
2087  else
2088  {
2089  line->set_manifold_id (boundary_line->manifold_id);
2090  }
2091  }
2092  else
2093  line->set_boundary_id_internal(boundary_line->boundary_id);
2094  }
2095  line->set_manifold_id (boundary_line->manifold_id);
2096  }
2097 
2098 
2099  // finally update neighborship info
2101  cell=triangulation.begin(); cell!=triangulation.end(); ++cell)
2102  for (unsigned int side=0; side<4; ++side)
2103  if (adjacent_cells[cell->line(side)->index()][0] == cell)
2104  // first adjacent cell is
2105  // this one
2106  {
2107  if (adjacent_cells[cell->line(side)->index()].size() == 2)
2108  // there is another
2109  // adjacent cell
2110  cell->set_neighbor (side,
2111  adjacent_cells[cell->line(side)->index()][1]);
2112  }
2113  // first adjacent cell is not this
2114  // one, -> it must be the neighbor
2115  // we are looking for
2116  else
2117  cell->set_neighbor (side,
2118  adjacent_cells[cell->line(side)->index()][0]);
2119  }
2120 
2121 
2132  {
2133  inline bool operator () (const internal::TriangulationImplementation::TriaObject<2> &q1,
2135  {
2136  // here is room to
2137  // optimize the repeated
2138  // equality test of the
2139  // previous lines; the
2140  // compiler will probably
2141  // take care of most of
2142  // it anyway
2143  if ((q1.face(0) < q2.face(0)) ||
2144  ((q1.face(0) == q2.face(0)) &&
2145  (q1.face(1) < q2.face(1))) ||
2146  ((q1.face(0) == q2.face(0)) &&
2147  (q1.face(1) == q2.face(1)) &&
2148  (q1.face(2) < q2.face(2))) ||
2149  ((q1.face(0) == q2.face(0)) &&
2150  (q1.face(1) == q2.face(1)) &&
2151  (q1.face(2) == q2.face(2)) &&
2152  (q1.face(3) < q2.face(3))))
2153  return true;
2154  else
2155  return false;
2156  }
2157  };
2158 
2159 
2167  template <int spacedim>
2168  static
2169  void
2170  create_triangulation (const std::vector<Point<spacedim> > &v,
2171  const std::vector<CellData<3> > &cells,
2172  const SubCellData &subcelldata,
2173  Triangulation<3,spacedim> &triangulation)
2174  {
2175  AssertThrow (v.size() > 0, ExcMessage ("No vertices given"));
2176  AssertThrow (cells.size() > 0, ExcMessage ("No cells given"));
2177 
2178  const unsigned int dim=3;
2179 
2180  // copy vertices
2181  triangulation.vertices = v;
2182  triangulation.vertices_used = std::vector<bool> (v.size(), true);
2183 
2184  // Check that all cells have positive volume.
2185 #ifndef _MSC_VER
2186  //TODO: The following code does not compile with MSVC. Find a way around it
2187  for (unsigned int cell_no = 0; cell_no<cells.size(); ++cell_no)
2188  {
2189  // See the note in the 1D function on this if statement.
2190  if (!triangulation.check_for_distorted_cells)
2191  {
2192  const double cell_measure = GridTools::cell_measure<3>
2193  (triangulation.vertices, cells[cell_no].vertices);
2194  AssertThrow(cell_measure > 0, ExcGridHasInvalidCell(cell_no));
2195  }
2196  }
2197 #endif
2198 
2200  // first set up some collections of data
2201  //
2202  // make up a list of the needed
2203  // lines
2204  //
2205  // each line is a pair of
2206  // vertices. The list is kept
2207  // sorted and it is guaranteed that
2208  // each line is inserted only once.
2209  // While the key of such an entry
2210  // is the pair of vertices, the
2211  // thing it points to is an
2212  // iterator pointing to the line
2213  // object itself. In the first run,
2214  // these iterators are all invalid
2215  // ones, but they are filled
2216  // afterwards same applies for the
2217  // quads
2218  typename std::map<std::pair<int,int>,
2219  typename Triangulation<dim,spacedim>::line_iterator> needed_lines;
2220  for (unsigned int cell=0; cell<cells.size(); ++cell)
2221  {
2222  // check whether vertex indices
2223  // are valid ones
2224  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell; ++vertex)
2225  AssertThrow (cells[cell].vertices[vertex] < triangulation.vertices.size(),
2226  ExcInvalidVertexIndex (cell, cells[cell].vertices[vertex],
2227  triangulation.vertices.size()));
2228 
2229  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
2230  {
2231  // given a line vertex number
2232  // (0,1) on a specific line we
2233  // get the cell vertex number
2234  // (0-7) through the
2235  // line_to_cell_vertices
2236  // function
2237  std::pair<int,int> line_vertices(
2238  cells[cell].vertices[GeometryInfo<dim>::line_to_cell_vertices(line, 0)],
2239  cells[cell].vertices[GeometryInfo<dim>::line_to_cell_vertices(line, 1)]);
2240 
2241  // if that line was already inserted
2242  // in reverse order do nothing, else
2243  // insert the line
2244  if ( (needed_lines.find(std::make_pair(line_vertices.second,
2245  line_vertices.first))
2246  ==
2247  needed_lines.end()))
2248  {
2249  // insert line, with
2250  // invalid iterator. if line
2251  // already exists, then
2252  // nothing bad happens here
2253  needed_lines[line_vertices] = triangulation.end_line();
2254  }
2255  }
2256  }
2257 
2258 
2260  // now for some sanity-checks:
2261  //
2262  // check that every vertex has at
2263  // least tree adjacent lines
2264  {
2265  std::vector<unsigned short int> vertex_touch_count (v.size(), 0);
2266  typename std::map<std::pair<int,int>,
2267  typename Triangulation<dim,spacedim>::line_iterator>::iterator i;
2268  for (i=needed_lines.begin(); i!=needed_lines.end(); ++i)
2269  {
2270  // touch the vertices of
2271  // this line
2272  ++vertex_touch_count[i->first.first];
2273  ++vertex_touch_count[i->first.second];
2274  }
2275 
2276  // assert minimum touch count
2277  // is at least three. if not so,
2278  // then clean triangulation and
2279  // exit with an exception
2280  AssertThrow (* (std::min_element(vertex_touch_count.begin(),
2281  vertex_touch_count.end())) >= 3,
2282  ExcMessage("During creation of a triangulation, a part of the "
2283  "algorithm encountered a vertex that is part of only "
2284  "one or two adjacent lines. However, in 3d, every vertex "
2285  "needs to be at least part of three lines."));
2286  }
2287 
2288 
2290  // actually set up data structures
2291  // for the lines
2292  // reserve enough space
2293  triangulation.levels.push_back (std_cxx14::make_unique<internal::TriangulationImplementation::TriaLevel<dim>>());
2294  triangulation.faces = std_cxx14::make_unique<internal::TriangulationImplementation::TriaFaces<dim>> ();
2295  triangulation.levels[0]->reserve_space (cells.size(), dim, spacedim);
2296  triangulation.faces->lines.reserve_space (0,needed_lines.size());
2297 
2298  // make up lines
2299  {
2300  typename Triangulation<dim,spacedim>::raw_line_iterator
2301  line = triangulation.begin_raw_line();
2302  typename std::map<std::pair<int,int>,
2303  typename Triangulation<dim,spacedim>::line_iterator>::iterator i;
2304  for (i = needed_lines.begin(); line!=triangulation.end_line(); ++line, ++i)
2305  {
2306  line->set (internal::TriangulationImplementation::TriaObject<1>(i->first.first,
2307  i->first.second));
2308  line->set_used_flag ();
2309  line->clear_user_flag ();
2310  line->clear_user_data ();
2311 
2312  // now set the iterator for
2313  // this line
2314  i->second = line;
2315  }
2316  }
2317 
2318 
2320  // make up the quads of this triangulation
2321  //
2322  // same thing: the iterators are
2323  // set to the invalid value at
2324  // first, we only collect the data
2325  // now
2326 
2327  // the bool array stores, whether the lines
2328  // are in the standard orientation or not
2329 
2330  // note that QuadComparator is a
2331  // class declared and defined in
2332  // this file
2333  std::map<internal::TriangulationImplementation::TriaObject<2>,
2334  std::pair<typename Triangulation<dim,spacedim>::quad_iterator,
2335  std::array<bool,GeometryInfo<dim>::lines_per_face> >,
2337  needed_quads;
2338  for (unsigned int cell=0; cell<cells.size(); ++cell)
2339  {
2340  // the faces are quads which
2341  // consist of four numbers
2342  // denoting the index of the
2343  // four lines bounding the
2344  // quad. we can get this index
2345  // by asking @p{needed_lines}
2346  // for an iterator to this
2347  // line, dereferencing it and
2348  // thus return an iterator into
2349  // the @p{lines} array of the
2350  // triangulation, which is
2351  // already set up. we can then
2352  // ask this iterator for its
2353  // index within the present
2354  // level (the level is zero, of
2355  // course)
2356  //
2357  // to make things easier, we
2358  // don't create the lines
2359  // (pairs of their vertex
2360  // indices) in place, but
2361  // before they are really
2362  // needed.
2363  std::pair<int,int> line_list[GeometryInfo<dim>::lines_per_cell],
2364  inverse_line_list[GeometryInfo<dim>::lines_per_cell];
2365  unsigned int face_line_list[GeometryInfo<dim>::lines_per_face];
2366  std::array<bool,GeometryInfo<dim>::lines_per_face> orientation;
2367 
2368  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
2369  {
2370  line_list[line]=std::pair<int,int> (
2371  cells[cell].vertices[GeometryInfo<dim>::line_to_cell_vertices(line, 0)],
2372  cells[cell].vertices[GeometryInfo<dim>::line_to_cell_vertices(line, 1)]);
2373  inverse_line_list[line]=std::pair<int,int> (
2374  cells[cell].vertices[GeometryInfo<dim>::line_to_cell_vertices(line, 1)],
2375  cells[cell].vertices[GeometryInfo<dim>::line_to_cell_vertices(line, 0)]);
2376  }
2377 
2378  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
2379  {
2380  // set up a list of the lines to be
2381  // used for this face. check the
2382  // direction for each line
2383  //
2384  // given a face line number (0-3) on
2385  // a specific face we get the cell
2386  // line number (0-11) through the
2387  // face_to_cell_lines function
2388  for (unsigned int l=0; l<GeometryInfo<dim>::lines_per_face; ++l)
2389  if (needed_lines.find (inverse_line_list[GeometryInfo<dim>::
2390  face_to_cell_lines(face,l)]) == needed_lines.end())
2391  {
2392  face_line_list[l]=needed_lines[line_list[GeometryInfo<dim>::
2393  face_to_cell_lines(face,l)]]->index();
2394  orientation[l]=true;
2395  }
2396  else
2397  {
2398  face_line_list[l]=needed_lines[inverse_line_list[GeometryInfo<dim>::
2399  face_to_cell_lines(face,l)]]->index();
2400  orientation[l]=false;
2401  }
2402 
2403 
2405  quad(face_line_list[0],
2406  face_line_list[1],
2407  face_line_list[2],
2408  face_line_list[3]);
2409 
2410  // insert quad, with
2411  // invalid iterator
2412  //
2413  // if quad already exists,
2414  // then nothing bad happens
2415  // here, as this will then
2416  // simply become an
2417  // interior face of the
2418  // triangulation. however,
2419  // we will run into major
2420  // trouble if the face was
2421  // already inserted in the
2422  // opposite
2423  // direction. there are
2424  // really only two
2425  // orientations for a face
2426  // to be in, since the edge
2427  // directions are already
2428  // set. thus, vertex 0 is
2429  // the one from which two
2430  // edges originate, and
2431  // vertex 3 is the one to
2432  // which they converge. we
2433  // are then left with
2434  // orientations 0-1-2-3 and
2435  // 2-3-0-1 for the order of
2436  // lines. the
2437  // corresponding quad can
2438  // be easily constructed by
2439  // exchanging lines. we do
2440  // so here, just to check
2441  // that that flipped quad
2442  // isn't already in the
2443  // triangulation. if it is,
2444  // then don't insert the
2445  // new one and instead
2446  // later set the
2447  // face_orientation flag
2449  test_quad_1(quad.face(2), quad.face(3),
2450  quad.face(0), quad.face(1)),//face_orientation=false, face_flip=false, face_rotation=false
2451  test_quad_2(quad.face(0), quad.face(1),
2452  quad.face(3), quad.face(2)),//face_orientation=false, face_flip=false, face_rotation=true
2453  test_quad_3(quad.face(3), quad.face(2),
2454  quad.face(1), quad.face(0)),//face_orientation=false, face_flip=true, face_rotation=false
2455  test_quad_4(quad.face(1), quad.face(0),
2456  quad.face(2), quad.face(3)),//face_orientation=false, face_flip=true, face_rotation=true
2457  test_quad_5(quad.face(2), quad.face(3),
2458  quad.face(1), quad.face(0)),//face_orientation=true, face_flip=false, face_rotation=true
2459  test_quad_6(quad.face(1), quad.face(0),
2460  quad.face(3), quad.face(2)),//face_orientation=true, face_flip=true, face_rotation=false
2461  test_quad_7(quad.face(3), quad.face(2),
2462  quad.face(0), quad.face(1));//face_orientation=true, face_flip=true, face_rotation=true
2463  if (needed_quads.find (test_quad_1) == needed_quads.end() &&
2464  needed_quads.find (test_quad_2) == needed_quads.end() &&
2465  needed_quads.find (test_quad_3) == needed_quads.end() &&
2466  needed_quads.find (test_quad_4) == needed_quads.end() &&
2467  needed_quads.find (test_quad_5) == needed_quads.end() &&
2468  needed_quads.find (test_quad_6) == needed_quads.end() &&
2469  needed_quads.find (test_quad_7) == needed_quads.end())
2470  needed_quads[quad] = std::make_pair(triangulation.end_quad(),orientation);
2471  }
2472  }
2473 
2474 
2476  // enter the resulting quads into
2477  // the arrays of the Triangulation
2478  //
2479  // first reserve enough space
2480  triangulation.faces->quads.reserve_space (0,needed_quads.size());
2481 
2482  {
2483  typename Triangulation<dim,spacedim>::raw_quad_iterator
2484  quad = triangulation.begin_raw_quad();
2485  typename std::map<internal::TriangulationImplementation::TriaObject<2>,
2486  std::pair<typename Triangulation<dim,spacedim>::quad_iterator,
2487  std::array<bool,GeometryInfo<dim>::lines_per_face> >,
2489  ::iterator q;
2490  for (q = needed_quads.begin(); quad!=triangulation.end_quad(); ++quad, ++q)
2491  {
2492  quad->set (q->first);
2493  quad->set_used_flag ();
2494  quad->clear_user_flag ();
2495  quad->clear_user_data ();
2496  // set the line orientation
2497  quad->set_line_orientation(0,q->second.second[0]);
2498  quad->set_line_orientation(1,q->second.second[1]);
2499  quad->set_line_orientation(2,q->second.second[2]);
2500  quad->set_line_orientation(3,q->second.second[3]);
2501 
2502 
2503  // now set the iterator for
2504  // this quad
2505  q->second.first = quad;
2506  }
2507  }
2508 
2510  // finally create the cells
2511  triangulation.levels[0]->cells.reserve_space (cells.size());
2512 
2513  // store for each quad index the
2514  // adjacent cells
2515  std::map<int,std::vector<typename Triangulation<dim,spacedim>::cell_iterator> >
2516  adjacent_cells;
2517 
2518  // finally make up cells
2519  {
2521  cell = triangulation.begin_raw_hex();
2522  for (unsigned int c=0; c<cells.size(); ++c, ++cell)
2523  {
2524  // first find for each of
2525  // the cells the quad
2526  // iterator of the
2527  // respective faces.
2528  //
2529  // to this end, set up the
2530  // lines of this cell and
2531  // find the quads that are
2532  // bounded by these lines;
2533  // these are then the faces
2534  // of the present cell
2535  std::pair<int,int> line_list[GeometryInfo<dim>::lines_per_cell],
2536  inverse_line_list[GeometryInfo<dim>::lines_per_cell];
2537  unsigned int face_line_list[4];
2538  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
2539  {
2540  line_list[line]=std::make_pair(
2541  cells[c].vertices[GeometryInfo<dim>::line_to_cell_vertices(line, 0)],
2542  cells[c].vertices[GeometryInfo<dim>::line_to_cell_vertices(line, 1)]);
2543  inverse_line_list[line]=std::pair<int,int> (
2544  cells[c].vertices[GeometryInfo<dim>::line_to_cell_vertices(line, 1)],
2545  cells[c].vertices[GeometryInfo<dim>::line_to_cell_vertices(line, 0)]);
2546  }
2547 
2548  // get the iterators
2549  // corresponding to the
2550  // faces. also store
2551  // whether they are
2552  // reversed or not
2553  typename Triangulation<dim,spacedim>::quad_iterator
2554  face_iterator[GeometryInfo<dim>::faces_per_cell];
2555  bool face_orientation[GeometryInfo<dim>::faces_per_cell];
2556  bool face_flip[GeometryInfo<dim>::faces_per_cell];
2557  bool face_rotation[GeometryInfo<dim>::faces_per_cell];
2558  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
2559  {
2560  for (unsigned int l=0; l<GeometryInfo<dim>::lines_per_face; ++l)
2561  if (needed_lines.find (inverse_line_list[GeometryInfo<dim>::
2562  face_to_cell_lines(face,l)]) == needed_lines.end())
2563  face_line_list[l]=needed_lines[line_list[GeometryInfo<dim>::
2564  face_to_cell_lines(face,l)]]->index();
2565  else
2566  face_line_list[l]=needed_lines[inverse_line_list[GeometryInfo<dim>::
2567  face_to_cell_lines(face,l)]]->index();
2568 
2570  quad(face_line_list[0],
2571  face_line_list[1],
2572  face_line_list[2],
2573  face_line_list[3]);
2574 
2575  if (needed_quads.find (quad) != needed_quads.end())
2576  {
2577  // face is in standard
2578  // orientation (and not
2579  // flipped or rotated). this
2580  // must be true for at least
2581  // one of the two cells
2582  // containing this face
2583  // (i.e. for the cell which
2584  // originally inserted the
2585  // face)
2586  face_iterator[face] = needed_quads[quad].first;
2587  face_orientation[face] = true;
2588  face_flip[face]=false;
2589  face_rotation[face]=false;
2590  }
2591  else
2592  {
2593  // face must be available in
2594  // reverse order
2595  // then. construct all
2596  // possibilities and check
2597  // them one after the other
2599  test_quad_1(quad.face(2), quad.face(3),
2600  quad.face(0), quad.face(1)),//face_orientation=false, face_flip=false, face_rotation=false
2601  test_quad_2(quad.face(0), quad.face(1),
2602  quad.face(3), quad.face(2)),//face_orientation=false, face_flip=false, face_rotation=true
2603  test_quad_3(quad.face(3), quad.face(2),
2604  quad.face(1), quad.face(0)),//face_orientation=false, face_flip=true, face_rotation=false
2605  test_quad_4(quad.face(1), quad.face(0),
2606  quad.face(2), quad.face(3)),//face_orientation=false, face_flip=true, face_rotation=true
2607  test_quad_5(quad.face(2), quad.face(3),
2608  quad.face(1), quad.face(0)),//face_orientation=true, face_flip=false, face_rotation=true
2609  test_quad_6(quad.face(1), quad.face(0),
2610  quad.face(3), quad.face(2)),//face_orientation=true, face_flip=true, face_rotation=false
2611  test_quad_7(quad.face(3), quad.face(2),
2612  quad.face(0), quad.face(1));//face_orientation=true, face_flip=true, face_rotation=true
2613  if (needed_quads.find (test_quad_1) != needed_quads.end())
2614  {
2615  face_iterator[face] = needed_quads[test_quad_1].first;
2616  face_orientation[face] = false;
2617  face_flip[face]=false;
2618  face_rotation[face]=false;
2619  }
2620  else if (needed_quads.find (test_quad_2) != needed_quads.end())
2621  {
2622  face_iterator[face] = needed_quads[test_quad_2].first;
2623  face_orientation[face] = false;
2624  face_flip[face]=false;
2625  face_rotation[face]=true;
2626  }
2627  else if (needed_quads.find (test_quad_3) != needed_quads.end())
2628  {
2629  face_iterator[face] = needed_quads[test_quad_3].first;
2630  face_orientation[face] = false;
2631  face_flip[face]=true;
2632  face_rotation[face]=false;
2633  }
2634  else if (needed_quads.find (test_quad_4) != needed_quads.end())
2635  {
2636  face_iterator[face] = needed_quads[test_quad_4].first;
2637  face_orientation[face] = false;
2638  face_flip[face]=true;
2639  face_rotation[face]=true;
2640  }
2641  else if (needed_quads.find (test_quad_5) != needed_quads.end())
2642  {
2643  face_iterator[face] = needed_quads[test_quad_5].first;
2644  face_orientation[face] = true;
2645  face_flip[face]=false;
2646  face_rotation[face]=true;
2647  }
2648  else if (needed_quads.find (test_quad_6) != needed_quads.end())
2649  {
2650  face_iterator[face] = needed_quads[test_quad_6].first;
2651  face_orientation[face] = true;
2652  face_flip[face]=true;
2653  face_rotation[face]=false;
2654  }
2655  else if (needed_quads.find (test_quad_7) != needed_quads.end())
2656  {
2657  face_iterator[face] = needed_quads[test_quad_7].first;
2658  face_orientation[face] = true;
2659  face_flip[face]=true;
2660  face_rotation[face]=true;
2661  }
2662 
2663  else
2664  // we didn't find the
2665  // face in any direction,
2666  // so something went
2667  // wrong above
2668  Assert(false,ExcInternalError());
2669 
2670  }
2671  }// for all faces
2672 
2673  // make the cell out of
2674  // these iterators
2676  ::TriaObject<3> (face_iterator[0]->index(),
2677  face_iterator[1]->index(),
2678  face_iterator[2]->index(),
2679  face_iterator[3]->index(),
2680  face_iterator[4]->index(),
2681  face_iterator[5]->index()));
2682 
2683  cell->set_used_flag ();
2684  cell->set_material_id (cells[c].material_id);
2685  cell->set_manifold_id (cells[c].manifold_id);
2686  cell->clear_user_flag ();
2687  cell->clear_user_data ();
2688  cell->set_subdomain_id (0);
2689 
2690  // set orientation flag for
2691  // each of the faces
2692  for (unsigned int quad=0; quad<GeometryInfo<dim>::faces_per_cell; ++quad)
2693  {
2694  cell->set_face_orientation (quad, face_orientation[quad]);
2695  cell->set_face_flip (quad, face_flip[quad]);
2696  cell->set_face_rotation (quad, face_rotation[quad]);
2697  }
2698 
2699 
2700  // note that this cell is
2701  // adjacent to the six
2702  // quads
2703  for (unsigned int quad=0; quad<GeometryInfo<dim>::faces_per_cell; ++quad)
2704  adjacent_cells[face_iterator[quad]->index()].push_back (cell);
2705 
2706 #ifdef DEBUG
2707  // make some checks on the
2708  // lines and their
2709  // ordering
2710 
2711  // first map all cell lines
2712  // to the two face lines
2713  // which should
2714  // coincide. all face lines
2715  // are included with a cell
2716  // line number (0-11)
2717  // key. At the end all keys
2718  // will be included twice
2719  // (for each of the two
2720  // coinciding lines once)
2721  std::multimap<unsigned int, std::pair<unsigned int, unsigned int> >
2722  cell_to_face_lines;
2723  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
2724  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_face; ++line)
2725  cell_to_face_lines.insert(
2726  std::pair<unsigned int, std::pair<unsigned int, unsigned int> > (
2728  std::pair<unsigned int, unsigned int> (face,line)));
2729  std::multimap<unsigned int, std::pair<unsigned int, unsigned int> >::const_iterator
2730  map_iter=cell_to_face_lines.begin();
2731 
2732  for (; map_iter!=cell_to_face_lines.end(); ++map_iter)
2733  {
2734  const unsigned int cell_line=map_iter->first;
2735  const unsigned int face1=map_iter->second.first;
2736  const unsigned int line1=map_iter->second.second;
2737  ++map_iter;
2738  Assert(map_iter!=cell_to_face_lines.end(), ExcInternalErrorOnCell(c));
2739  Assert(map_iter->first==cell_line, ExcInternalErrorOnCell(c));
2740  const unsigned int face2=map_iter->second.first;
2741  const unsigned int line2=map_iter->second.second;
2742 
2743  // check that the pair
2744  // of lines really
2745  // coincide. Take care
2746  // about the face
2747  // orientation;
2748  Assert (face_iterator[face1]->line(GeometryInfo<dim>::standard_to_real_face_line(
2749  line1,
2750  face_orientation[face1],
2751  face_flip[face1],
2752  face_rotation[face1])) ==
2753  face_iterator[face2]->line(GeometryInfo<dim>::standard_to_real_face_line(
2754  line2,
2755  face_orientation[face2],
2756  face_flip[face2],
2757  face_rotation[face2])),
2759  }
2760 #endif
2761  }
2762  }
2763 
2764 
2766  // find those quads which are at the
2767  // boundary and mark them appropriately
2768  for (typename Triangulation<dim,spacedim>::quad_iterator
2769  quad=triangulation.begin_quad(); quad!=triangulation.end_quad(); ++quad)
2770  {
2771  const unsigned int n_adj_cells = adjacent_cells[quad->index()].size();
2772  // assert that every quad has
2773  // one or two adjacent cells
2774  AssertThrow ((n_adj_cells >= 1) &&
2775  (n_adj_cells <= 2),
2776  ExcInternalError());
2777 
2778  // if only one cell: quad is at boundary -> give it the boundary
2779  // indicator zero by default
2780  quad->set_boundary_id_internal((n_adj_cells == 1) ? 0 : numbers::internal_face_boundary_id);
2781 
2782  // Manifold ids are set independently of where they are
2783  quad->set_manifold_id(numbers::flat_manifold_id);
2784  }
2785 
2787  // next find those lines which are at
2788  // the boundary and mark all others as
2789  // interior ones
2790  //
2791  // for this: first mark all lines as interior. use this loop
2792  // to also set all manifold ids of all lines
2793  for (typename Triangulation<dim,spacedim>::line_iterator
2794  line=triangulation.begin_line(); line!=triangulation.end_line(); ++line)
2795  {
2796  line->set_boundary_id_internal(numbers::internal_face_boundary_id);
2797  line->set_manifold_id(numbers::flat_manifold_id);
2798  }
2799 
2800  // next reset all lines bounding
2801  // boundary quads as on the
2802  // boundary also. note that since
2803  // we are in 3d, there are cases
2804  // where one or more lines of a
2805  // quad that is not on the
2806  // boundary, are actually boundary
2807  // lines. they will not be marked
2808  // when visiting this
2809  // face. however, since we do not
2810  // support dim-2 dimensional
2811  // boundaries (i.e. internal lines
2812  // constituting boundaries), every
2813  // such line is also part of a face
2814  // that is actually on the
2815  // boundary, so sooner or later we
2816  // get to mark that line for being
2817  // on the boundary
2818  for (typename Triangulation<dim,spacedim>::quad_iterator
2819  quad=triangulation.begin_quad(); quad!=triangulation.end_quad(); ++quad)
2820  if (quad->at_boundary())
2821  {
2822  for (unsigned int l=0; l<4; ++l)
2823  {
2824  typename Triangulation<dim,spacedim>::line_iterator line = quad->line(l);
2825  line->set_boundary_id_internal(0);
2826  }
2827  }
2828 
2830  // now set boundary indicators
2831  // where given
2832  //
2833  // first do so for lines
2834  std::vector<CellData<1> >::const_iterator boundary_line
2835  = subcelldata.boundary_lines.begin();
2836  std::vector<CellData<1> >::const_iterator end_boundary_line
2837  = subcelldata.boundary_lines.end();
2838  for (; boundary_line!=end_boundary_line; ++boundary_line)
2839  {
2840  typename Triangulation<dim,spacedim>::line_iterator line;
2841  std::pair <int, int> line_vertices(std::make_pair(boundary_line->vertices[0],
2842  boundary_line->vertices[1]));
2843  if (needed_lines.find(line_vertices) != needed_lines.end())
2844  // line found in this
2845  // direction
2846  line = needed_lines[line_vertices];
2847 
2848  else
2849  {
2850  // look whether it exists in
2851  // reverse direction
2852  std::swap (line_vertices.first, line_vertices.second);
2853  if (needed_lines.find(line_vertices) != needed_lines.end())
2854  line = needed_lines[line_vertices];
2855  else
2856  // line does not exist
2857  AssertThrow (false, ExcLineInexistant(line_vertices.first,
2858  line_vertices.second));
2859  }
2860  // Assert that only exterior
2861  // lines are given a boundary
2862  // indicator
2863  AssertThrow (line->at_boundary(),
2864  ExcInteriorLineCantBeBoundary(line->vertex_index(0),
2865  line->vertex_index(1),
2866  boundary_line->boundary_id));
2867 
2868  // and make sure that we don't
2869  // attempt to reset the
2870  // boundary indicator to a
2871  // different than the
2872  // previously set value
2873  if (line->boundary_id() != 0)
2874  AssertThrow (line->boundary_id() == boundary_line->boundary_id,
2875  ExcMessage ("Duplicate boundary lines are only allowed "
2876  "if they carry the same boundary indicator."));
2877 
2878  line->set_boundary_id_internal(boundary_line->boundary_id);
2879  // Set manifold id if given
2880  line->set_manifold_id(boundary_line->manifold_id);
2881  }
2882 
2883 
2884  // now go on with boundary faces
2885  std::vector<CellData<2> >::const_iterator boundary_quad
2886  = subcelldata.boundary_quads.begin();
2887  std::vector<CellData<2> >::const_iterator end_boundary_quad
2888  = subcelldata.boundary_quads.end();
2889  for (; boundary_quad!=end_boundary_quad; ++boundary_quad)
2890  {
2891  typename Triangulation<dim,spacedim>::quad_iterator quad;
2892  typename Triangulation<dim,spacedim>::line_iterator line[4];
2893 
2894  // first find the lines that
2895  // are made up of the given
2896  // vertices, then build up a
2897  // quad from these lines
2898  // finally use the find
2899  // function of the map template
2900  // to find the quad
2901  for (unsigned int i=0; i<4; ++i)
2902  {
2903  std::pair<int, int> line_vertices(
2904  boundary_quad->vertices[GeometryInfo<dim-1>::line_to_cell_vertices(i,0)],
2905  boundary_quad->vertices[GeometryInfo<dim-1>::line_to_cell_vertices(i,1)]);
2906 
2907  // check whether line
2908  // already exists
2909  if (needed_lines.find(line_vertices) != needed_lines.end())
2910  line[i] = needed_lines[line_vertices];
2911  else
2912  // look whether it exists
2913  // in reverse direction
2914  {
2915  std::swap (line_vertices.first, line_vertices.second);
2916  if (needed_lines.find(line_vertices) != needed_lines.end())
2917  line[i] = needed_lines[line_vertices];
2918  else
2919  // line does
2920  // not exist
2921  AssertThrow (false, ExcLineInexistant(line_vertices.first,
2922  line_vertices.second));
2923  }
2924  }
2925 
2926 
2927  // Set up 2 quads that are
2928  // built up from the lines for
2929  // reasons of comparison to
2930  // needed_quads. The second
2931  // quad is the reversed version
2932  // of the first quad in order
2933  // find the quad regardless of
2934  // its orientation. This is
2935  // introduced for convenience
2936  // and because boundary quad
2937  // orientation does not carry
2938  // any information.
2940  quad_compare_1(line[0]->index(), line[1]->index(),
2941  line[2]->index(), line[3]->index());
2943  quad_compare_2(line[2]->index(), line[3]->index(),
2944  line[0]->index(), line[1]->index());
2945 
2946  // try to find the quad with
2947  // lines situated as
2948  // constructed above. if it
2949  // could not be found, rotate
2950  // the boundary lines 3 times
2951  // until it is found or it does
2952  // not exist.
2953 
2954  // mapping from counterclock to
2955  // lexicographic ordering of
2956  // quad lines
2957  static const unsigned int lex2cclock[4]= {3,1,0,2};
2958  // copy lines from
2959  // lexicographic to
2960  // counterclock ordering, as
2961  // rotation is much simpler in
2962  // counterclock ordering
2963  typename Triangulation<dim,spacedim>::line_iterator
2964  line_counterclock[4];
2965  for (unsigned int i=0; i<4; ++i)
2966  line_counterclock[lex2cclock[i]]=line[i];
2967  unsigned int n_rotations=0;
2968  bool not_found_quad_1;
2969  while ( (not_found_quad_1=(needed_quads.find(quad_compare_1) == needed_quads.end())) &&
2970  ( needed_quads.find(quad_compare_2) == needed_quads.end()) &&
2971  (n_rotations<4))
2972  {
2973  // use the rotate defined
2974  // in <algorithms>
2975  std::rotate(line_counterclock, line_counterclock+1, line_counterclock+4);
2976  // update the quads with
2977  // rotated lines (i runs in
2978  // lexicographic ordering)
2979  for (unsigned int i=0; i<4; ++i)
2980  {
2981  quad_compare_1.set_face(i, line_counterclock[lex2cclock[i]]->index());
2982  quad_compare_2.set_face((i+2)%4, line_counterclock[lex2cclock[i]]->index());
2983  }
2984 
2985  ++n_rotations;
2986  }
2987 
2988  AssertThrow (n_rotations!=4,
2989  ExcQuadInexistant(line[0]->index(), line[1]->index(),
2990  line[2]->index(), line[3]->index()));
2991 
2992  if (not_found_quad_1)
2993  quad = needed_quads[quad_compare_2].first;
2994  else
2995  quad = needed_quads[quad_compare_1].first;
2996 
2997  // check whether this face is
2998  // really an exterior one
2999  AssertThrow (quad->at_boundary(),
3000  ExcInteriorQuadCantBeBoundary(quad->vertex_index(0),
3001  quad->vertex_index(1),
3002  quad->vertex_index(2),
3003  quad->vertex_index(3),
3004  boundary_quad->boundary_id));
3005 
3006  // and make sure that we don't
3007  // attempt to reset the
3008  // boundary indicator to a
3009  // different than the
3010  // previously set value
3011  if (quad->boundary_id() != 0)
3012  AssertThrow (quad->boundary_id() == boundary_quad->boundary_id,
3013  ExcMessage ("Duplicate boundary quads are only allowed "
3014  "if they carry the same boundary indicator."));
3015 
3016  quad->set_boundary_id_internal (boundary_quad->boundary_id);
3017  quad->set_manifold_id (boundary_quad->manifold_id);
3018  }
3019 
3020 
3022  // finally update neighborship info
3024  cell=triangulation.begin(); cell!=triangulation.end(); ++cell)
3025  for (unsigned int face=0; face<6; ++face)
3026  if (adjacent_cells[cell->quad(face)->index()][0] == cell)
3027  // first adjacent cell is
3028  // this one
3029  {
3030  if (adjacent_cells[cell->quad(face)->index()].size() == 2)
3031  // there is another
3032  // adjacent cell
3033  cell->set_neighbor (face,
3034  adjacent_cells[cell->quad(face)->index()][1]);
3035  }
3036  // first adjacent cell is not this
3037  // one, -> it must be the neighbor
3038  // we are looking for
3039  else
3040  cell->set_neighbor (face,
3041  adjacent_cells[cell->quad(face)->index()][0]);
3042  }
3043 
3044 
3060  template <int spacedim>
3061  static
3062  void
3065  std::vector<unsigned int> &,
3066  std::vector<unsigned int> &)
3067  {
3068  const unsigned int dim = 1;
3069 
3070  // first we need to reset the
3071  // neighbor pointers of the
3072  // neighbors of this cell's
3073  // children to this cell. This is
3074  // different for one dimension,
3075  // since there neighbors can have a
3076  // refinement level differing from
3077  // that of this cell's children by
3078  // more than one level.
3079 
3080  Assert (!cell->child(0)->has_children() && !cell->child(1)->has_children(),
3081  ExcInternalError());
3082 
3083  // first do it for the cells to the
3084  // left
3085  if (cell->neighbor(0).state() == IteratorState::valid)
3086  if (cell->neighbor(0)->has_children())
3087  {
3089  neighbor = cell->neighbor(0);
3090  Assert (neighbor->level() == cell->level(), ExcInternalError());
3091 
3092  // right child
3093  neighbor = neighbor->child(1);
3094  while (true)
3095  {
3096  Assert (neighbor->neighbor(1) == cell->child(0),
3097  ExcInternalError());
3098  neighbor->set_neighbor (1, cell);
3099 
3100  // move on to further
3101  // children on the
3102  // boundary between this
3103  // cell and its neighbor
3104  if (neighbor->has_children())
3105  neighbor = neighbor->child(1);
3106  else
3107  break;
3108  }
3109  }
3110 
3111  // now do it for the cells to the
3112  // left
3113  if (cell->neighbor(1).state() == IteratorState::valid)
3114  if (cell->neighbor(1)->has_children())
3115  {
3117  neighbor = cell->neighbor(1);
3118  Assert (neighbor->level() == cell->level(), ExcInternalError());
3119 
3120  // left child
3121  neighbor = neighbor->child(0);
3122  while (true)
3123  {
3124  Assert (neighbor->neighbor(0) == cell->child(1),
3125  ExcInternalError());
3126  neighbor->set_neighbor (0, cell);
3127 
3128  // move on to further
3129  // children on the
3130  // boundary between this
3131  // cell and its neighbor
3132  if (neighbor->has_children())
3133  neighbor = neighbor->child(0);
3134  else
3135  break;
3136  }
3137  }
3138 
3139 
3140  // delete the vertex which will not
3141  // be needed anymore. This vertex
3142  // is the second of the first child
3143  triangulation.vertices_used[cell->child(0)->vertex_index(1)] = false;
3144 
3145  // invalidate children. clear user
3146  // pointers, to avoid that they may
3147  // appear at unwanted places later
3148  // on...
3149  for (unsigned int child=0; child<cell->n_children(); ++child)
3150  {
3151  cell->child(child)->clear_user_data();
3152  cell->child(child)->clear_user_flag();
3153  cell->child(child)->clear_used_flag();
3154  }
3155 
3156 
3157  // delete pointer to children
3158  cell->clear_children ();
3159  cell->clear_user_flag();
3160  }
3161 
3162 
3163 
3164  template <int spacedim>
3165  static
3166  void
3169  std::vector<unsigned int> &line_cell_count,
3170  std::vector<unsigned int> &)
3171  {
3172  const unsigned int dim=2;
3173  const RefinementCase<dim> ref_case=cell->refinement_case();
3174 
3175  Assert(line_cell_count.size()==triangulation.n_raw_lines(), ExcInternalError());
3176 
3177  // vectors to hold all lines which
3178  // may be deleted
3179  std::vector<typename Triangulation<dim,spacedim>::line_iterator>
3180  lines_to_delete(0);
3181 
3182  lines_to_delete.reserve(4*2+4);
3183 
3184  // now we decrease the counters for
3185  // lines contained in the child
3186  // cells
3187  for (unsigned int c=0; c<cell->n_children(); ++c)
3188  {
3190  child=cell->child(c);
3191  for (unsigned int l=0; l<GeometryInfo<dim>::lines_per_cell; ++l)
3192  --line_cell_count[child->line_index(l)];
3193  }
3194 
3195 
3196  // delete the vertex which will not
3197  // be needed anymore. This vertex
3198  // is the second of the second line
3199  // of the first child, if the cell
3200  // is refined with cut_xy, else there
3201  // is no inner vertex.
3202  // additionally delete unneeded inner
3203  // lines
3204  if (ref_case==RefinementCase<dim>::cut_xy)
3205  {
3206  triangulation.vertices_used[cell->child(0)->line(1)->vertex_index(1)] = false;
3207 
3208  lines_to_delete.push_back(cell->child(0)->line(1));
3209  lines_to_delete.push_back(cell->child(0)->line(3));
3210  lines_to_delete.push_back(cell->child(3)->line(0));
3211  lines_to_delete.push_back(cell->child(3)->line(2));
3212  }
3213  else
3214  {
3215  unsigned int inner_face_no=ref_case==RefinementCase<dim>::cut_x ? 1 : 3;
3216 
3217  // the inner line will not be
3218  // used any more
3219  lines_to_delete.push_back(cell->child(0)->line(inner_face_no));
3220  }
3221 
3222  // invalidate children
3223  for (unsigned int child=0; child<cell->n_children(); ++child)
3224  {
3225  cell->child(child)->clear_user_data();
3226  cell->child(child)->clear_user_flag();
3227  cell->child(child)->clear_used_flag();
3228  }
3229 
3230 
3231  // delete pointer to children
3232  cell->clear_children ();
3233  cell->clear_refinement_case();
3234  cell->clear_user_flag();
3235 
3236  // look at the refinement of outer
3237  // lines. if nobody needs those
3238  // anymore we can add them to the
3239  // list of lines to be deleted.
3240  for (unsigned int line_no=0; line_no<GeometryInfo<dim>::lines_per_cell; ++line_no)
3241  {
3242  typename Triangulation<dim,spacedim>::line_iterator
3243  line=cell->line(line_no);
3244 
3245  if (line->has_children())
3246  {
3247  // if one of the cell counters is
3248  // zero, the other has to be as well
3249 
3250  Assert((line_cell_count[line->child_index(0)] == 0 &&
3251  line_cell_count[line->child_index(1)] == 0) ||
3252  (line_cell_count[line->child_index(0)] > 0 &&
3253  line_cell_count[line->child_index(1)] > 0),
3254  ExcInternalError());
3255 
3256  if (line_cell_count[line->child_index(0)]==0)
3257  {
3258  for (unsigned int c=0; c<2; ++c)
3259  Assert (!line->child(c)->has_children(),
3260  ExcInternalError());
3261 
3262  // we may delete the line's
3263  // children and the middle vertex
3264  // as no cell references them
3265  // anymore
3266  triangulation.vertices_used[line->child(0)->vertex_index(1)] = false;
3267 
3268  lines_to_delete.push_back(line->child(0));
3269  lines_to_delete.push_back(line->child(1));
3270 
3271  line->clear_children();
3272  }
3273  }
3274  }
3275 
3276  // finally, delete unneeded lines
3277 
3278  // clear user pointers, to avoid that
3279  // they may appear at unwanted places
3280  // later on...
3281  // same for user flags, then finally
3282  // delete the lines
3283  typename std::vector<typename Triangulation<dim,spacedim>::line_iterator>::iterator
3284  line=lines_to_delete.begin(),
3285  endline=lines_to_delete.end();
3286  for (; line!=endline; ++line)
3287  {
3288  (*line)->clear_user_data();
3289  (*line)->clear_user_flag();
3290  (*line)->clear_used_flag();
3291  }
3292  }
3293 
3294 
3295 
3296  template <int spacedim>
3297  static
3298  void
3301  std::vector<unsigned int> &line_cell_count,
3302  std::vector<unsigned int> &quad_cell_count)
3303  {
3304  const unsigned int dim=3;
3305 
3306  Assert(line_cell_count.size()==triangulation.n_raw_lines(), ExcInternalError());
3307  Assert(quad_cell_count.size()==triangulation.n_raw_quads(), ExcInternalError());
3308 
3309  // first of all, we store the RefineCase of
3310  // this cell
3311  const RefinementCase<dim> ref_case=cell->refinement_case();
3312  // vectors to hold all lines and quads which
3313  // may be deleted
3314  std::vector<typename Triangulation<dim,spacedim>::line_iterator>
3315  lines_to_delete(0);
3316  std::vector<typename Triangulation<dim,spacedim>::quad_iterator>
3317  quads_to_delete(0);
3318 
3319  lines_to_delete.reserve(12*2+6*4+6);
3320  quads_to_delete.reserve(6*4+12);
3321 
3322  // now we decrease the counters for lines and
3323  // quads contained in the child cells
3324  for (unsigned int c=0; c<cell->n_children(); ++c)
3325  {
3327  child=cell->child(c);
3328  for (unsigned int l=0; l<GeometryInfo<dim>::lines_per_cell; ++l)
3329  --line_cell_count[child->line_index(l)];
3330  for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
3331  --quad_cell_count[child->quad_index(f)];
3332  }
3333 
3335  // delete interior quads and lines and the
3336  // interior vertex, depending on the
3337  // refinement case of the cell
3338  //
3339  // for append quads and lines: only append
3340  // them to the list of objects to be deleted
3341 
3342  switch (ref_case)
3343  {
3345  quads_to_delete.push_back(cell->child(0)->face(1));
3346  break;
3348  quads_to_delete.push_back(cell->child(0)->face(3));
3349  break;
3351  quads_to_delete.push_back(cell->child(0)->face(5));
3352  break;
3354  quads_to_delete.push_back(cell->child(0)->face(1));
3355  quads_to_delete.push_back(cell->child(0)->face(3));
3356  quads_to_delete.push_back(cell->child(3)->face(0));
3357  quads_to_delete.push_back(cell->child(3)->face(2));
3358 
3359  lines_to_delete.push_back(cell->child(0)->line(11));
3360  break;
3362  quads_to_delete.push_back(cell->child(0)->face(1));
3363  quads_to_delete.push_back(cell->child(0)->face(5));
3364  quads_to_delete.push_back(cell->child(3)->face(0));
3365  quads_to_delete.push_back(cell->child(3)->face(4));
3366 
3367  lines_to_delete.push_back(cell->child(0)->line(5));
3368  break;
3370  quads_to_delete.push_back(cell->child(0)->face(3));
3371  quads_to_delete.push_back(cell->child(0)->face(5));
3372  quads_to_delete.push_back(cell->child(3)->face(2));
3373  quads_to_delete.push_back(cell->child(3)->face(4));
3374 
3375  lines_to_delete.push_back(cell->child(0)->line(7));
3376  break;
3378  quads_to_delete.push_back(cell->child(0)->face(1));
3379  quads_to_delete.push_back(cell->child(2)->face(1));
3380  quads_to_delete.push_back(cell->child(4)->face(1));
3381  quads_to_delete.push_back(cell->child(6)->face(1));
3382 
3383  quads_to_delete.push_back(cell->child(0)->face(3));
3384  quads_to_delete.push_back(cell->child(1)->face(3));
3385  quads_to_delete.push_back(cell->child(4)->face(3));
3386  quads_to_delete.push_back(cell->child(5)->face(3));
3387 
3388  quads_to_delete.push_back(cell->child(0)->face(5));
3389  quads_to_delete.push_back(cell->child(1)->face(5));
3390  quads_to_delete.push_back(cell->child(2)->face(5));
3391  quads_to_delete.push_back(cell->child(3)->face(5));
3392 
3393  lines_to_delete.push_back(cell->child(0)->line(5));
3394  lines_to_delete.push_back(cell->child(0)->line(7));
3395  lines_to_delete.push_back(cell->child(0)->line(11));
3396  lines_to_delete.push_back(cell->child(7)->line(0));
3397  lines_to_delete.push_back(cell->child(7)->line(2));
3398  lines_to_delete.push_back(cell->child(7)->line(8));
3399  // delete the vertex which will not
3400  // be needed anymore. This vertex
3401  // is the vertex at the heart of
3402  // this cell, which is the sixth of
3403  // the first child
3404  triangulation.vertices_used[cell->child(0)->vertex_index(7)] = false;
3405  break;
3406  default:
3407  // only remaining case is
3408  // no_refinement, thus an error
3409  Assert(false, ExcInternalError());
3410  break;
3411  }
3412 
3413 
3414  // invalidate children
3415  for (unsigned int child=0; child<cell->n_children(); ++child)
3416  {
3417  cell->child(child)->clear_user_data();
3418  cell->child(child)->clear_user_flag();
3419 
3420  for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
3421  {
3422  // set flags denoting deviations from
3423  // standard orientation of faces back
3424  // to initialization values
3425  cell->child(child)->set_face_orientation (f, true);
3426  cell->child(child)->set_face_flip(f,false);
3427  cell->child(child)->set_face_rotation(f,false);
3428  }
3429 
3430  cell->child(child)->clear_used_flag();
3431  }
3432 
3433 
3434  // delete pointer to children
3435  cell->clear_children ();
3436  cell->clear_refinement_case ();
3437  cell->clear_user_flag();
3438 
3439  // so far we only looked at inner quads,
3440  // lines and vertices. Now we have to
3441  // consider outer ones as well. here, we have
3442  // to check, whether there are other cells
3443  // still needing these objects. oherwise we
3444  // can delete them. first for quads (and
3445  // their inner lines).
3446 
3447  for (unsigned int quad_no=0; quad_no<GeometryInfo<dim>::faces_per_cell; ++quad_no)
3448  {
3449  typename Triangulation<dim,spacedim>::quad_iterator
3450  quad=cell->face(quad_no);
3451 
3452  Assert((GeometryInfo<dim>::face_refinement_case(ref_case,quad_no) && quad->has_children()) ||
3453  GeometryInfo<dim>::face_refinement_case(ref_case,quad_no)==RefinementCase<dim-1>::no_refinement,
3454  ExcInternalError());
3455 
3456  switch (quad->refinement_case())
3457  {
3458  case RefinementCase<dim-1>::no_refinement:
3459  // nothing to do as the quad
3460  // is not refined
3461  break;
3462  case RefinementCase<dim-1>::cut_x:
3463  case RefinementCase<dim-1>::cut_y:
3464  {
3465  // if one of the cell counters is
3466  // zero, the other has to be as
3467  // well
3468  Assert((quad_cell_count[quad->child_index(0)] == 0 &&
3469  quad_cell_count[quad->child_index(1)] == 0) ||
3470  (quad_cell_count[quad->child_index(0)] > 0 &&
3471  quad_cell_count[quad->child_index(1)] > 0),
3472  ExcInternalError());
3473  // it might be, that the quad is
3474  // refined twice anisotropically,
3475  // first check, whether we may
3476  // delete possible grand_children
3477  unsigned int deleted_grandchildren=0;
3478  unsigned int number_of_child_refinements=0;
3479 
3480  for (unsigned int c=0; c<2; ++c)
3481  if (quad->child(c)->has_children())
3482  {
3483  ++number_of_child_refinements;
3484  // if one of the cell counters is
3485  // zero, the other has to be as
3486  // well
3487  Assert((quad_cell_count[quad->child(c)->child_index(0)] == 0 &&
3488  quad_cell_count[quad->child(c)->child_index(1)] == 0) ||
3489  (quad_cell_count[quad->child(c)->child_index(0)] > 0 &&
3490  quad_cell_count[quad->child(c)->child_index(1)] > 0),
3491  ExcInternalError());
3492  if (quad_cell_count[quad->child(c)->child_index(0)]==0)
3493  {
3494  // Assert, that the two
3495  // anisotropic
3496  // refinements add up to
3497  // isotropic refinement
3498  Assert(quad->refinement_case()+quad->child(c)->refinement_case()==RefinementCase<dim>::cut_xy,
3499  ExcInternalError());
3500  // we may delete the
3501  // quad's children and
3502  // the inner line as no
3503  // cell references them
3504  // anymore
3505  quads_to_delete.push_back(quad->child(c)->child(0));
3506  quads_to_delete.push_back(quad->child(c)->child(1));
3507  if (quad->child(c)->refinement_case()==RefinementCase<2>::cut_x)
3508  lines_to_delete.push_back(quad->child(c)->child(0)->line(1));
3509  else
3510  lines_to_delete.push_back(quad->child(c)->child(0)->line(3));
3511  quad->child(c)->clear_children();
3512  quad->child(c)->clear_refinement_case();
3513  ++deleted_grandchildren;
3514  }
3515  }
3516  // if no grandchildren are left, we
3517  // may as well delete the
3518  // refinement of the inner line
3519  // between our children and the
3520  // corresponding vertex
3521  if (number_of_child_refinements>0 &&
3522  deleted_grandchildren==number_of_child_refinements)
3523  {
3524  typename Triangulation<dim,spacedim>::line_iterator
3525  middle_line;
3526  if (quad->refinement_case()==RefinementCase<2>::cut_x)
3527  middle_line=quad->child(0)->line(1);
3528  else
3529  middle_line=quad->child(0)->line(3);
3530 
3531  lines_to_delete.push_back(middle_line->child(0));
3532  lines_to_delete.push_back(middle_line->child(1));
3533  triangulation.vertices_used[middle_vertex_index<dim,spacedim>(middle_line)]
3534  = false;
3535  middle_line->clear_children();
3536  }
3537 
3538  // now consider the direct children
3539  // of the given quad
3540  if (quad_cell_count[quad->child_index(0)]==0)
3541  {
3542  // we may delete the quad's
3543  // children and the inner line
3544  // as no cell references them
3545  // anymore
3546  quads_to_delete.push_back(quad->child(0));
3547  quads_to_delete.push_back(quad->child(1));
3548  if (quad->refinement_case()==RefinementCase<2>::cut_x)
3549  lines_to_delete.push_back(quad->child(0)->line(1));
3550  else
3551  lines_to_delete.push_back(quad->child(0)->line(3));
3552 
3553  // if the counters just dropped
3554  // to zero, otherwise the
3555  // children would have been
3556  // deleted earlier, then this
3557  // cell's children must have
3558  // contained the anisotropic
3559  // quad children. thus, if
3560  // those have again anisotropic
3561  // children, which are in
3562  // effect isotropic children of
3563  // the original quad, those are
3564  // still needed by a
3565  // neighboring cell and we
3566  // cannot delete them. instead,
3567  // we have to reset this quad's
3568  // refine case to isotropic and
3569  // set the children
3570  // accordingly.
3571  if (quad->child(0)->has_children())
3572  if (quad->refinement_case()==RefinementCase<2>::cut_x)
3573  {
3574  // now evereything is
3575  // quite complicated. we
3576  // have the children
3577  // numbered according to
3578  //
3579  // *---*---*
3580  // |n+1|m+1|
3581  // *---*---*
3582  // | n | m |
3583  // *---*---*
3584  //
3585  // from the original
3586  // anisotropic
3587  // refinement. we have to
3588  // reorder them as
3589  //
3590  // *---*---*
3591  // | m |m+1|
3592  // *---*---*
3593  // | n |n+1|
3594  // *---*---*
3595  //
3596  // for isotropic refinement.
3597  //
3598  // this is a bit ugly, of
3599  // course: loop over all
3600  // cells on all levels
3601  // and look for faces n+1
3602  // (switch_1) and m
3603  // (switch_2).
3604  const typename Triangulation<dim,spacedim>::quad_iterator
3605  switch_1=quad->child(0)->child(1),
3606  switch_2=quad->child(1)->child(0);
3607 
3608  Assert(!switch_1->has_children(), ExcInternalError());
3609  Assert(!switch_2->has_children(), ExcInternalError());
3610 
3611  const int switch_1_index=switch_1->index();
3612  const int switch_2_index=switch_2->index();
3613  for (unsigned int l=0; l<triangulation.levels.size(); ++l)
3614  for (unsigned int h=0; h<triangulation.levels[l]->cells.cells.size(); ++h)
3615  for (unsigned int q=0; q<GeometryInfo<dim>::faces_per_cell; ++q)
3616  {
3617  const int index=triangulation.levels[l]->cells.cells[h].face(q);
3618  if (index==switch_1_index)
3619  triangulation.levels[l]->cells.cells[h].set_face(q,switch_2_index);
3620  else if (index==switch_2_index)
3621  triangulation.levels[l]->cells.cells[h].set_face(q,switch_1_index);
3622  }
3623  // now we have to copy
3624  // all information of the
3625  // two quads
3626  const int switch_1_lines[4]=
3627  {
3628  static_cast<signed int>(switch_1->line_index(0)),
3629  static_cast<signed int>(switch_1->line_index(1)),
3630  static_cast<signed int>(switch_1->line_index(2)),
3631  static_cast<signed int>(switch_1->line_index(3))
3632  };
3633  const bool switch_1_line_orientations[4]=
3634  {
3635  switch_1->line_orientation(0),
3636  switch_1->line_orientation(1),
3637  switch_1->line_orientation(2),
3638  switch_1->line_orientation(3)
3639  };
3640  const types::boundary_id switch_1_boundary_id=switch_1->boundary_id();
3641  const unsigned int switch_1_user_index=switch_1->user_index();
3642  const bool switch_1_user_flag=switch_1->user_flag_set();
3643 
3644  switch_1->set(internal::TriangulationImplementation::TriaObject<2>(switch_2->line_index(0),
3645  switch_2->line_index(1),
3646  switch_2->line_index(2),
3647  switch_2->line_index(3)));
3648  switch_1->set_line_orientation(0, switch_2->line_orientation(0));
3649  switch_1->set_line_orientation(1, switch_2->line_orientation(1));
3650  switch_1->set_line_orientation(2, switch_2->line_orientation(2));
3651  switch_1->set_line_orientation(3, switch_2->line_orientation(3));
3652  switch_1->set_boundary_id_internal(switch_2->boundary_id());
3653  switch_1->set_manifold_id(switch_2->manifold_id());
3654  switch_1->set_user_index(switch_2->user_index());
3655  if (switch_2->user_flag_set())
3656  switch_1->set_user_flag();
3657  else
3658  switch_1->clear_user_flag();
3659 
3660  switch_2->set(internal::TriangulationImplementation::TriaObject<2>(switch_1_lines[0],
3661  switch_1_lines[1],
3662  switch_1_lines[2],
3663  switch_1_lines[3]));
3664  switch_2->set_line_orientation(0, switch_1_line_orientations[0]);
3665  switch_2->set_line_orientation(1, switch_1_line_orientations[1]);
3666  switch_2->set_line_orientation(2, switch_1_line_orientations[2]);
3667  switch_2->set_line_orientation(3, switch_1_line_orientations[3]);
3668  switch_2->set_boundary_id_internal(switch_1_boundary_id);
3669  switch_2->set_manifold_id(switch_1->manifold_id());
3670  switch_2->set_user_index(switch_1_user_index);
3671  if (switch_1_user_flag)
3672  switch_2->set_user_flag();
3673  else
3674  switch_2->clear_user_flag();
3675 
3676  const unsigned int child_0=quad->child(0)->child_index(0);
3677  const unsigned int child_2=quad->child(1)->child_index(0);
3678  quad->clear_children();
3679  quad->clear_refinement_case();
3680  quad->set_refinement_case(RefinementCase<2>::cut_xy);
3681  quad->set_children(0,child_0);
3682  quad->set_children(2,child_2);
3683  std::swap(quad_cell_count[child_0+1],quad_cell_count[child_2]);
3684  }
3685  else
3686  {
3687  // the face was refined
3688  // with cut_y, thus the
3689  // children are already
3690  // in correct order. we
3691  // only have to set them
3692  // correctly, deleting
3693  // the indirection of two
3694  // anisotropic refinement
3695  // and going directly
3696  // from the quad to
3697  // isotropic children
3698  const unsigned int child_0=quad->child(0)->child_index(0);
3699  const unsigned int child_2=quad->child(1)->child_index(0);
3700  quad->clear_children();
3701  quad->clear_refinement_case();
3702  quad->set_refinement_case(RefinementCase<2>::cut_xy);
3703  quad->set_children(0,child_0);
3704  quad->set_children(2,child_2);
3705  }
3706  else
3707  {
3708  quad->clear_children();
3709  quad->clear_refinement_case();
3710  }
3711 
3712 
3713  }
3714  break;
3715  }
3716  case RefinementCase<dim-1>::cut_xy:
3717  {
3718  // if one of the cell counters is
3719  // zero, the others have to be as
3720  // well
3721 
3722  Assert((quad_cell_count[quad->child_index(0)] == 0 &&
3723  quad_cell_count[quad->child_index(1)] == 0 &&
3724  quad_cell_count[quad->child_index(2)] == 0 &&
3725  quad_cell_count[quad->child_index(3)] == 0) ||
3726  (quad_cell_count[quad->child_index(0)] > 0 &&
3727  quad_cell_count[quad->child_index(1)] > 0 &&
3728  quad_cell_count[quad->child_index(2)] > 0 &&
3729  quad_cell_count[quad->child_index(3)] > 0),
3730  ExcInternalError());
3731 
3732  if (quad_cell_count[quad->child_index(0)]==0)
3733  {
3734  // we may delete the quad's
3735  // children, the inner lines
3736  // and the middle vertex as no
3737  // cell references them anymore
3738  lines_to_delete.push_back(quad->child(0)->line(1));
3739  lines_to_delete.push_back(quad->child(3)->line(0));
3740  lines_to_delete.push_back(quad->child(0)->line(3));
3741  lines_to_delete.push_back(quad->child(3)->line(2));
3742 
3743  for (unsigned int child=0; child<quad->n_children(); ++child)
3744  quads_to_delete.push_back(quad->child(child));
3745 
3746  triangulation.vertices_used[quad->child(0)->vertex_index(3)] = false;
3747 
3748  quad->clear_children();
3749  quad->clear_refinement_case();
3750  }
3751  }
3752  break;
3753 
3754  default:
3755  Assert(false, ExcInternalError());
3756  break;
3757  }
3758 
3759  }
3760 
3761  // now we repeat a similar procedure
3762  // for the outer lines of this cell.
3763 
3764  // if in debug mode: check that each
3765  // of the lines for which we consider
3766  // deleting the children in fact has
3767  // children (the bits/coarsening_3d
3768  // test tripped over this initially)
3769  for (unsigned int line_no=0; line_no<GeometryInfo<dim>::lines_per_cell; ++line_no)
3770  {
3771  typename Triangulation<dim,spacedim>::line_iterator
3772  line=cell->line(line_no);
3773 
3774  Assert((GeometryInfo<dim>::line_refinement_case(ref_case,line_no) && line->has_children()) ||
3776  ExcInternalError());
3777 
3778  if (line->has_children())
3779  {
3780  // if one of the cell counters is
3781  // zero, the other has to be as well
3782 
3783  Assert((line_cell_count[line->child_index(0)] == 0 &&
3784  line_cell_count[line->child_index(1)] == 0) ||
3785  (line_cell_count[line->child_index(0)] > 0 &&
3786  line_cell_count[line->child_index(1)] > 0),
3787  ExcInternalError());
3788 
3789  if (line_cell_count[line->child_index(0)]==0)
3790  {
3791  for (unsigned int c=0; c<2; ++c)
3792  Assert (!line->child(c)->has_children(),
3793  ExcInternalError());
3794 
3795  // we may delete the line's
3796  // children and the middle vertex
3797  // as no cell references them
3798  // anymore
3799  triangulation.vertices_used[line->child(0)->vertex_index(1)] = false;
3800 
3801  lines_to_delete.push_back(line->child(0));
3802  lines_to_delete.push_back(line->child(1));
3803 
3804  line->clear_children();
3805  }
3806  }
3807  }
3808 
3809  // finally, delete unneeded quads and lines
3810 
3811  // clear user pointers, to avoid that
3812  // they may appear at unwanted places
3813  // later on...
3814  // same for user flags, then finally
3815  // delete the quads and lines
3816  typename std::vector<typename Triangulation<dim,spacedim>::line_iterator>::iterator
3817  line=lines_to_delete.begin(),
3818  endline=lines_to_delete.end();
3819  for (; line!=endline; ++line)
3820  {
3821  (*line)->clear_user_data();
3822  (*line)->clear_user_flag();
3823  (*line)->clear_used_flag();
3824  }
3825 
3826  typename std::vector<typename Triangulation<dim,spacedim>::quad_iterator>::iterator
3827  quad=quads_to_delete.begin(),
3828  endquad=quads_to_delete.end();
3829  for (; quad!=endquad; ++quad)
3830  {
3831  (*quad)->clear_user_data();
3832  (*quad)->clear_children();
3833  (*quad)->clear_refinement_case();
3834  (*quad)->clear_user_flag();
3835  (*quad)->clear_used_flag();
3836  }
3837  }
3838 
3839 
3857  template <int spacedim>
3858  static
3859  void
3861  unsigned int &next_unused_vertex,
3862  typename Triangulation<2,spacedim>::raw_line_iterator &next_unused_line,
3863  typename Triangulation<2,spacedim>::raw_cell_iterator &next_unused_cell,
3865  {
3866  const unsigned int dim=2;
3867  // clear refinement flag
3868  const RefinementCase<dim> ref_case=cell->refine_flag_set();
3869  cell->clear_refine_flag ();
3870 
3871  /* For the refinement process: since we go the levels up from the lowest, there
3872  are (unlike above) only two possibilities: a neighbor cell is on the same
3873  level or one level up (in both cases, it may or may not be refined later on,
3874  but we don't care here).
3875 
3876  First:
3877  Set up an array of the 3x3 vertices, which are distributed on the cell
3878  (the array consists of indices into the @p{vertices} std::vector
3879 
3880  2--7--3
3881  | | |
3882  4--8--5
3883  | | |
3884  0--6--1
3885 
3886  note: in case of cut_x or cut_y not all these vertices are needed for the new
3887  cells
3888 
3889  Second:
3890  Set up an array of the new lines (the array consists of iterator pointers
3891  into the lines arrays)
3892 
3893  .-6-.-7-. The directions are: .->-.->-.
3894  1 9 3 ^ ^ ^
3895  .-10.11-. .->-.->-.
3896  0 8 2 ^ ^ ^
3897  .-4-.-5-. .->-.->-.
3898 
3899  cut_x:
3900  .-4-.-5-.
3901  | | |
3902  0 6 1
3903  | | |
3904  .-2-.-3-.
3905 
3906  cut_y:
3907  .---5---.
3908  1 3
3909  .---6---.
3910  0 2
3911  .---4---.
3912 
3913 
3914  Third:
3915  Set up an array of neighbors:
3916 
3917  6 7
3918  .--.--.
3919  1| | |3
3920  .--.--.
3921  0| | |2
3922  .--.--.
3923  4 5
3924 
3925  We need this array for two reasons: first to get the lines which will
3926  bound the four subcells (if the neighboring cell is refined, these
3927  lines already exist), and second to update neighborship information.
3928  Since if a neighbor is not refined, its neighborship record only
3929  points to the present, unrefined, cell rather than the children we
3930  are presently creating, we only need the neighborship information
3931  if the neighbor cells are refined. In all other cases, we store
3932  the unrefined neighbor address
3933 
3934  We also need for every neighbor (if refined) which number among its
3935  neighbors the present (unrefined) cell has, since that number is to
3936  be replaced and because that also is the number of the subline which
3937  will be the interface between that neighbor and the to be created cell.
3938  We will store this number (between 0 and 3) in the field
3939  @p{neighbors_neighbor}.
3940 
3941  It would be sufficient to use the children of the common line to the
3942  neighbor, if we only wanted to get the new sublines and the new vertex,
3943  but because we need to update the neighborship information of the
3944  two refined subcells of the neighbor, we need to search these anyway.
3945 
3946  Convention:
3947  The created children are numbered like this:
3948 
3949  .--.--.
3950  |2 . 3|
3951  .--.--.
3952  |0 | 1|
3953  .--.--.
3954  */
3955  // collect the
3956  // indices of the
3957  // eight
3958  // surrounding
3959  // vertices
3960  // 2--7--3
3961  // | | |
3962  // 4--9--5
3963  // | | |
3964  // 0--6--1
3965  int new_vertices[9];
3966  for (unsigned int vertex_no=0; vertex_no<4; ++vertex_no)
3967  new_vertices[vertex_no]=cell->vertex_index(vertex_no);
3968  for (unsigned int line_no=0; line_no<4; ++line_no)
3969  if (cell->line(line_no)->has_children())
3970  new_vertices[4+line_no]=cell->line(line_no)->child(0)->vertex_index(1);
3971 
3972  if (ref_case==RefinementCase<dim>::cut_xy)
3973  {
3974 
3975  // find the next
3976  // unused vertex and
3977  // allocate it for
3978  // the new vertex we
3979  // need here
3980  while (triangulation.vertices_used[next_unused_vertex] == true)
3981  ++next_unused_vertex;
3982  Assert (next_unused_vertex < triangulation.vertices.size(),
3983  ExcMessage("Internal error: During refinement, the triangulation wants to access an element of the 'vertices' array but it turns out that the array is not large enough."));
3984  triangulation.vertices_used[next_unused_vertex] = true;
3985 
3986  new_vertices[8] = next_unused_vertex;
3987 
3988  // if this quad lives
3989  // in 2d, then we can
3990  // compute the new
3991  // central vertex
3992  // location just from
3993  // the surrounding
3994  // ones. If this is
3995  // not the case, then
3996  // we need to ask a
3997  // boundary object
3998  if (dim == spacedim)
3999  {
4000  // triangulation.vertices[next_unused_vertex] = new_point;
4001  triangulation.vertices[next_unused_vertex] = cell->center(true);
4002 
4003  // if the user_flag is set, i.e. if the cell is at the
4004  // boundary, use a different calculation of the middle vertex
4005  // here. this is of advantage if the boundary is strongly
4006  // curved (whereas the cell is not) and the cell has a high
4007  // aspect ratio.
4008  if (cell->user_flag_set())
4009  {
4010  // first reset the user_flag and then refine
4011  cell->clear_user_flag();
4012  triangulation.vertices[next_unused_vertex]
4013  =cell->center(true, true);
4014  }
4015  }
4016  else
4017  {
4018  // if this quad lives in a higher dimensional space
4019  // then we don't need to worry if it is at the
4020  // boundary of the manifold -- we always have to use
4021  // the boundary object anyway; so ignore whether the
4022  // user flag is set or not
4023  cell->clear_user_flag();
4024 
4025  // new vertex is placed on the surface according to
4026  // the information stored in the boundary class
4027  triangulation.vertices[next_unused_vertex] =
4028  cell->center(true);
4029  }
4030  }
4031 
4032 
4033  // Now the lines:
4034  typename Triangulation<dim,spacedim>::raw_line_iterator new_lines[12];
4035  unsigned int lmin=8;
4036  unsigned int lmax=12;
4037  if (ref_case!=RefinementCase<dim>::cut_xy)
4038  {
4039  lmin=6;
4040  lmax=7;
4041  }
4042 
4043  for (unsigned int l=lmin; l<lmax; ++l)
4044  {
4045  while (next_unused_line->used() == true)
4046  ++next_unused_line;
4047  new_lines[l] = next_unused_line;
4048  ++next_unused_line;
4049 
4050  Assert (new_lines[l]->used() == false,
4051  ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
4052  }
4053 
4054  if (ref_case==RefinementCase<dim>::cut_xy)
4055  {
4056  // .-6-.-7-.
4057  // 1 9 3
4058  // .-10.11-.
4059  // 0 8 2
4060  // .-4-.-5-.
4061 
4062  // lines 0-7 already exist, create only the four interior
4063  // lines 8-11
4064  unsigned int l=0;
4065  for (unsigned int face_no=0; face_no<GeometryInfo<dim>::faces_per_cell; ++face_no)
4066  for (unsigned int c=0; c<2; ++c, ++l)
4067  new_lines[l]=cell->line(face_no)->child(c);
4068  Assert(l==8, ExcInternalError());
4069 
4070  new_lines[8] ->set (internal::TriangulationImplementation::
4071  TriaObject<1>(new_vertices[6], new_vertices[8]));
4072  new_lines[9] ->set (internal::TriangulationImplementation::
4073  TriaObject<1>(new_vertices[8], new_vertices[7]));
4074  new_lines[10]->set (internal::TriangulationImplementation::
4075  TriaObject<1>(new_vertices[4], new_vertices[8]));
4076  new_lines[11]->set (internal::TriangulationImplementation::
4077  TriaObject<1>(new_vertices[8], new_vertices[5]));
4078  }
4079  else if (ref_case==RefinementCase<dim>::cut_x)
4080  {
4081  // .-4-.-5-.
4082  // | | |
4083  // 0 6 1
4084  // | | |
4085  // .-2-.-3-.
4086  new_lines[0]=cell->line(0);
4087  new_lines[1]=cell->line(1);
4088  new_lines[2]=cell->line(2)->child(0);
4089  new_lines[3]=cell->line(2)->child(1);
4090  new_lines[4]=cell->line(3)->child(0);
4091  new_lines[5]=cell->line(3)->child(1);
4092  new_lines[6]->set (internal::TriangulationImplementation::
4093  TriaObject<1>(new_vertices[6], new_vertices[7]));
4094  }
4095  else
4096  {
4098  // .---5---.
4099  // 1 3
4100  // .---6---.
4101  // 0 2
4102  // .---4---.
4103  new_lines[0]=cell->line(0)->child(0);
4104  new_lines[1]=cell->line(0)->child(1);
4105  new_lines[2]=cell->line(1)->child(0);
4106  new_lines[3]=cell->line(1)->child(1);
4107  new_lines[4]=cell->line(2);
4108  new_lines[5]=cell->line(3);
4109  new_lines[6]->set (internal::TriangulationImplementation::
4110  TriaObject<1>(new_vertices[4], new_vertices[5]));
4111  }
4112 
4113  for (unsigned int l=lmin; l<lmax; ++l)
4114  {
4115  new_lines[l]->set_used_flag();
4116  new_lines[l]->clear_user_flag();
4117  new_lines[l]->clear_user_data();
4118  new_lines[l]->clear_children();
4119  // interior line
4120  new_lines[l]->set_boundary_id_internal(numbers::internal_face_boundary_id);
4121  new_lines[l]->set_manifold_id(cell->manifold_id());
4122  }
4123 
4124  // Now add the four (two)
4125  // new cells!
4128  while (next_unused_cell->used() == true)
4129  ++next_unused_cell;
4130 
4131  const unsigned int n_children=
4133  for (unsigned int i=0; i<n_children; ++i)
4134  {
4135  Assert (next_unused_cell->used() == false,
4136  ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
4137  subcells[i] = next_unused_cell;
4138  ++next_unused_cell;
4139  if (i%2==1 && i<n_children-1)
4140  while (next_unused_cell->used() == true)
4141  ++next_unused_cell;
4142  }
4143 
4144  if (ref_case==RefinementCase<dim>::cut_xy)
4145  {
4146  // children:
4147  // .--.--.
4148  // |2 . 3|
4149  // .--.--.
4150  // |0 | 1|
4151  // .--.--.
4152  // lines:
4153  // .-6-.-7-.
4154  // 1 9 3
4155  // .-10.11-.
4156  // 0 8 2
4157  // .-4-.-5-.
4158  subcells[0]->set (internal::TriangulationImplementation::
4159  TriaObject<2>(new_lines[0]->index(),
4160  new_lines[8]->index(),
4161  new_lines[4]->index(),
4162  new_lines[10]->index()));
4163  subcells[1]->set (internal::TriangulationImplementation::
4164  TriaObject<2>(new_lines[8]->index(),
4165  new_lines[2]->index(),
4166  new_lines[5]->index(),
4167  new_lines[11]->index()));
4168  subcells[2]->set (internal::TriangulationImplementation::
4169  TriaObject<2>(new_lines[1]->index(),
4170  new_lines[9]->index(),
4171  new_lines[10]->index(),
4172  new_lines[6]->index()));
4173  subcells[3]->set (internal::TriangulationImplementation::
4174  TriaObject<2>(new_lines[9]->index(),
4175  new_lines[3]->index(),
4176  new_lines[11]->index(),
4177  new_lines[7]->index()));
4178  }
4179  else if (ref_case==RefinementCase<dim>::cut_x)
4180  {
4181  // children:
4182  // .--.--.
4183  // | . |
4184  // .0 . 1.
4185  // | | |
4186  // .--.--.
4187  // lines:
4188  // .-4-.-5-.
4189  // | | |
4190  // 0 6 1
4191  // | | |
4192  // .-2-.-3-.
4193  subcells[0]->set (internal::TriangulationImplementation::
4194  TriaObject<2>(new_lines[0]->index(),
4195  new_lines[6]->index(),
4196  new_lines[2]->index(),
4197  new_lines[4]->index()));
4198  subcells[1]->set (internal::TriangulationImplementation::
4199  TriaObject<2>(new_lines[6]->index(),
4200  new_lines[1]->index(),
4201  new_lines[3]->index(),
4202  new_lines[5]->index()));
4203  }
4204  else
4205  {
4207  // children:
4208  // .-----.
4209  // | 1 |
4210  // .-----.
4211  // | 0 |
4212  // .-----.
4213  // lines:
4214  // .---5---.
4215  // 1 3
4216  // .---6---.
4217  // 0 2
4218  // .---4---.
4219  subcells[0]->set (internal::TriangulationImplementation::
4220  TriaObject<2>(new_lines[0]->index(),
4221  new_lines[2]->index(),
4222  new_lines[4]->index(),
4223  new_lines[6]->index()));
4224  subcells[1]->set (internal::TriangulationImplementation::
4225  TriaObject<2>(new_lines[1]->index(),
4226  new_lines[3]->index(),
4227  new_lines[6]->index(),
4228  new_lines[5]->index()));
4229  }
4230 
4231  types::subdomain_id subdomainid = cell->subdomain_id();
4232 
4233  for (unsigned int i=0; i<n_children; ++i)
4234  {
4235  subcells[i]->set_used_flag();
4236  subcells[i]->clear_refine_flag();
4237  subcells[i]->clear_user_flag();
4238  subcells[i]->clear_user_data();
4239  subcells[i]->clear_children();
4240  // inherit material
4241  // properties
4242  subcells[i]->set_material_id (cell->material_id());
4243  subcells[i]->set_manifold_id (cell->manifold_id());
4244  subcells[i]->set_subdomain_id (subdomainid);
4245 
4246  if (i%2==0)
4247  subcells[i]->set_parent (cell->index ());
4248  }
4249 
4250 
4251 
4252  // set child index for
4253  // even children children
4254  // i=0,2 (0)
4255  for (unsigned int i=0; i<n_children/2; ++i)
4256  cell->set_children (2*i, subcells[2*i]->index());
4257  // set the refine case
4258  cell->set_refinement_case(ref_case);
4259 
4260  // note that the
4261  // refinement flag was
4262  // already cleared at the
4263  // beginning of this function
4264 
4265  if (dim < spacedim)
4266  for (unsigned int c=0; c<n_children; ++c)
4267  cell->child(c)->set_direction_flag (cell->direction_flag());
4268 
4269  }
4270 
4271 
4272 
4277  template <int spacedim>
4278  static
4281  const bool /*check_for_distorted_cells*/)
4282  {
4283  const unsigned int dim = 1;
4284 
4285  // check whether a new level is needed we have to check for
4286  // this on the highest level only (on this, all used cells are
4287  // also active, so we only have to check for this)
4288  {
4290  cell = triangulation.begin_active (triangulation.levels.size()-1),
4291  endc = triangulation.end();
4292  for (; cell != endc; ++cell)
4293  if (cell->used())
4294  if (cell->refine_flag_set())
4295  {
4296  triangulation.levels
4297  .push_back (std_cxx14::make_unique<internal::TriangulationImplementation::TriaLevel<dim>>());
4298  break;
4299  }
4300  }
4301 
4302 
4303  // check how much space is needed on every level we need not
4304  // check the highest level since either - on the highest level
4305  // no cells are flagged for refinement - there are, but
4306  // prepare_refinement added another empty level
4307  unsigned int needed_vertices = 0;
4308  for (int level=triangulation.levels.size()-2; level>=0; --level)
4309  {
4310  // count number of flagged
4311  // cells on this level
4312  unsigned int flagged_cells = 0;
4314  acell = triangulation.begin_active(level),
4315  aendc = triangulation.begin_active(level+1);
4316  for (; acell!=aendc; ++acell)
4317  if (acell->refine_flag_set())
4318  ++flagged_cells;
4319 
4320  // count number of used cells
4321  // on the next higher level
4322  const unsigned int used_cells
4323  = std::count_if (triangulation.levels[level+1]->cells.used.begin(),
4324  triangulation.levels[level+1]->cells.used.end(),
4325  std::bind (std::equal_to<bool>(), std::placeholders::_1, true));
4326 
4327  // reserve space for the used_cells cells already existing
4328  // on the next higher level as well as for the
4329  // 2*flagged_cells that will be created on that level
4330  triangulation.levels[level+1]
4331  ->reserve_space(used_cells+
4333  flagged_cells,
4334  1,
4335  spacedim);
4336  // reserve space for 2*flagged_cells new lines on the next
4337  // higher level
4338  triangulation.levels[level+1]->cells
4339  .reserve_space (GeometryInfo<1>::max_children_per_cell *
4340  flagged_cells,
4341  0);
4342 
4343  needed_vertices += flagged_cells;
4344  }
4345 
4346  // add to needed vertices how many
4347  // vertices are already in use
4348  needed_vertices += std::count_if (triangulation.vertices_used.begin(),
4349  triangulation.vertices_used.end(),
4350  std::bind (std::equal_to<bool>(),
4351  std::placeholders::_1,
4352  true));
4353  // if we need more vertices: create them, if not: leave the
4354  // array as is, since shrinking is not really possible because
4355  // some of the vertices at the end may be in use
4356  if (needed_vertices > triangulation.vertices.size())
4357  {
4358  triangulation.vertices.resize (needed_vertices,
4359  Point<spacedim>());
4360  triangulation.vertices_used.resize (needed_vertices, false);
4361  }
4362 
4363 
4364  // Do REFINEMENT on every level; exclude highest level as
4365  // above
4366 
4367  // index of next unused vertex
4368  unsigned int next_unused_vertex = 0;
4369 
4370  for (int level=triangulation.levels.size()-2; level>=0; --level)
4371  {
4373  cell = triangulation.begin_active(level),
4374  endc = triangulation.begin_active(level+1);
4375 
4377  next_unused_cell = triangulation.begin_raw (level+1);
4378 
4379  for (; (cell!=endc) && (cell->level()==level); ++cell)
4380  if (cell->refine_flag_set())
4381  {
4382  // clear refinement flag
4383  cell->clear_refine_flag ();
4384 
4385  // search for next unused
4386  // vertex
4387  while (triangulation.vertices_used[next_unused_vertex] == true)
4388  ++next_unused_vertex;
4389  Assert (next_unused_vertex < triangulation.vertices.size(),
4390  ExcMessage("Internal error: During refinement, the triangulation wants to access an element of the 'vertices' array but it turns out that the array is not large enough."));
4391 
4392  // Now we always ask the cell itself where to put
4393  // the new point. The cell in turn will query the
4394  // manifold object internally.
4395  triangulation.vertices[next_unused_vertex] =
4396  cell->center(true);
4397 
4398  triangulation.vertices_used[next_unused_vertex] = true;
4399 
4400  // search for next two unused cell (++ takes care of
4401  // the end of the vector)
4403  first_child,
4404  second_child;
4405  while (next_unused_cell->used() == true)
4406  ++next_unused_cell;
4407  first_child = next_unused_cell;
4408  first_child->set_used_flag ();
4409  first_child->clear_user_data ();
4410  ++next_unused_cell;
4411  Assert (next_unused_cell->used() == false,
4412  ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
4413  second_child = next_unused_cell;
4414  second_child->set_used_flag ();
4415  second_child->clear_user_data ();
4416 
4417  types::subdomain_id subdomainid = cell->subdomain_id();
4418 
4419  // insert first child
4420  cell->set_children (0, first_child->index());
4421  first_child->clear_children ();
4422  first_child->set (internal::TriangulationImplementation
4423  ::TriaObject<1> (cell->vertex_index(0),
4424  next_unused_vertex));
4425  first_child->set_material_id (cell->material_id());
4426  first_child->set_manifold_id (cell->manifold_id());
4427  first_child->set_subdomain_id (subdomainid);
4428  first_child->set_direction_flag (cell->direction_flag());
4429 
4430  first_child->set_parent (cell->index ());
4431 
4432  // Set manifold id of the right face. Only do this
4433  // on the first child.
4434  first_child->face(1)->set_manifold_id(cell->manifold_id());
4435 
4436  // reset neighborship info (refer to
4437  // internal::TriangulationImplementation::TriaLevel<0> for
4438  // details)
4439  first_child->set_neighbor (1, second_child);
4440  if (cell->neighbor(0).state() != IteratorState::valid)
4441  first_child->set_neighbor (0, cell->neighbor(0));
4442  else if (cell->neighbor(0)->active())
4443  {
4444  // since the neighbors level is always <=level,
4445  // if the cell is active, then there are no
4446  // cells to the left which may want to know
4447  // about this new child cell.
4448  Assert (cell->neighbor (0)->level () <= cell->level (),
4449  ExcInternalError ());
4450  first_child->set_neighbor (0, cell->neighbor(0));
4451  }
4452  else
4453  // left neighbor is refined
4454  {
4455  // set neighbor to cell on same level
4456  const unsigned int nbnb = cell->neighbor_of_neighbor (0);
4457  first_child->set_neighbor (0, cell->neighbor(0)->child(nbnb));
4458 
4459  // reset neighbor info of all right descendant
4460  // of the left neighbor of cell
4462  left_neighbor = cell->neighbor(0);
4463  while (left_neighbor->has_children())
4464  {
4465  left_neighbor = left_neighbor->child(nbnb);
4466  left_neighbor->set_neighbor (nbnb, first_child);
4467  }
4468  }
4469 
4470  // insert second child
4471  second_child->clear_children ();
4472  second_child->set (internal::TriangulationImplementation
4473  ::TriaObject<1>(next_unused_vertex,
4474  cell->vertex_index(1)));
4475  second_child->set_neighbor (0, first_child);
4476  second_child->set_material_id (cell->material_id());
4477  second_child->set_manifold_id (cell->manifold_id());
4478  second_child->set_subdomain_id (subdomainid);
4479  second_child->set_direction_flag (cell->direction_flag());
4480 
4481  if (cell->neighbor(1).state() != IteratorState::valid)
4482  second_child->set_neighbor (1, cell->neighbor(1));
4483  else if (cell->neighbor(1)->active())
4484  {
4485  Assert (cell->neighbor (1)->level () <= cell->level (),
4486  ExcInternalError ());
4487  second_child->set_neighbor (1, cell->neighbor(1));
4488  }
4489  else
4490  // right neighbor is refined same as above
4491  {
4492  const unsigned int nbnb = cell->neighbor_of_neighbor (1);
4493  second_child->set_neighbor (1, cell->neighbor(1)->child(nbnb));
4494 
4496  right_neighbor = cell->neighbor(1);
4497  while (right_neighbor->has_children())
4498  {
4499  right_neighbor = right_neighbor->child(nbnb);
4500  right_neighbor->set_neighbor (nbnb, second_child);
4501  }
4502  }
4503  // inform all listeners that cell refinement is done
4504  triangulation.signals.post_refinement_on_cell(cell);
4505  }
4506  }
4507 
4508  // in 1d, we can not have distorted children unless the parent
4509  // was already distorted (that is because we don't use
4510  // boundary information for 1d triangulations). so return an
4511  // empty list
4513  }
4514 
4515 
4520  template <int spacedim>
4521  static
4524  const bool check_for_distorted_cells)
4525  {
4526  const unsigned int dim = 2;
4527 
4528  // check whether a new level is needed we have to check for
4529  // this on the highest level only (on this, all used cells are
4530  // also active, so we only have to check for this)
4531  if (true)
4532  {
4534  cell = triangulation.begin_active (triangulation.levels.size()-1),
4535  endc = triangulation.end();
4536  for (; cell != endc; ++cell)
4537  if (cell->used())
4538  if (cell->refine_flag_set())
4539  {
4540  triangulation.levels.push_back (std_cxx14::make_unique<internal::TriangulationImplementation::TriaLevel<dim>>());
4541  break;
4542  }
4543  }
4544 
4545 
4546  // first clear user flags and pointers of lines; we're going
4547  // to use them to flag which lines need refinement
4548  for (typename Triangulation<dim,spacedim>::line_iterator
4549  line=triangulation.begin_line(); line!=triangulation.end_line(); ++line)
4550  {
4551  line->clear_user_flag();
4552  line->clear_user_data();
4553  }
4554  // running over all cells and lines count the number
4555  // n_single_lines of lines which can be stored as single
4556  // lines, e.g. inner lines
4557  unsigned int n_single_lines=0;
4558 
4559  // New lines to be created: number lines which are stored in
4560  // pairs (the children of lines must be stored in pairs)
4561  unsigned int n_lines_in_pairs = 0;
4562 
4563  // check how much space is needed on every level we need not
4564  // check the highest level since either - on the highest level
4565  // no cells are flagged for refinement - there are, but
4566  // prepare_refinement added another empty level
4567  unsigned int needed_vertices = 0;
4568  for (int level=triangulation.levels.size()-2; level>=0; --level)
4569  {
4570  // count number of flagged cells on this level and compute
4571  // how many new vertices and new lines will be needed
4572  unsigned int needed_cells = 0;
4573 
4575  cell = triangulation.begin_active(level),
4576  endc = triangulation.begin_active(level+1);
4577  for (; cell!=endc; ++cell)
4578  if (cell->refine_flag_set())
4579  {
4580  if (cell->refine_flag_set()==RefinementCase<dim>::cut_xy)
4581  {
4582  needed_cells += 4;
4583 
4584  // new vertex at center of cell is needed in any
4585  // case
4586  ++needed_vertices;
4587 
4588  // the four inner lines can be stored as singles
4589  n_single_lines += 4;
4590  }
4591  else // cut_x || cut_y
4592  {
4593  // set the flag showing that anisotropic
4594  // refinement is used for at least one cell
4595  triangulation.anisotropic_refinement = true;
4596 
4597  needed_cells += 2;
4598  // no vertex at center
4599 
4600  // the inner line can be stored as single
4601  n_single_lines += 1;
4602 
4603  }
4604 
4605  // mark all faces (lines) for refinement; checking
4606  // locally whether the neighbor would also like to
4607  // refine them is rather difficult for lines so we
4608  // only flag them and after visiting all cells, we
4609  // decide which lines need refinement;
4610  for (unsigned int line_no=0; line_no<GeometryInfo<dim>::faces_per_cell;
4611  ++line_no)
4612  {
4614  cell->refine_flag_set(), line_no)==RefinementCase<1>::cut_x)
4615  {
4616  typename Triangulation<dim,spacedim>::line_iterator
4617  line = cell->line(line_no);
4618  if (line->has_children() == false)
4619  {
4620  line->set_user_flag ();
4621 //TODO[WB]: we overwrite the user_index here because we later on need
4622 // to find out which boundary object we have to ask to refine this
4623 // line. we can't use the boundary_id field because that can
4624 // only be used for lines at the boundary of the domain, but we also
4625 // need a domain description for interior lines in the codim-1 case
4626  if (spacedim > dim)
4627  {
4628  if (line->at_boundary())
4629  // if possible honor boundary
4630  // indicator
4631  line->set_user_index(line->boundary_id());
4632  else
4633  // otherwise take manifold
4634  // description from the adjacent
4635  // cell
4636  line->set_user_index(cell->material_id());
4637  }
4638  }
4639  }
4640  }
4641  }
4642 
4643 
4644  // count number of used cells on the next higher level
4645  const unsigned int used_cells
4646  = std::count_if (triangulation.levels[level+1]->cells.used.begin(),
4647  triangulation.levels[level+1]->cells.used.end(),
4648  std::bind (std::equal_to<bool>(), std::placeholders::_1, true));
4649 
4650 
4651  // reserve space for the used_cells cells already existing
4652  // on the next higher level as well as for the
4653  // needed_cells that will be created on that level
4654  triangulation.levels[level+1]
4655  ->reserve_space (used_cells+needed_cells, 2, spacedim);
4656 
4657  // reserve space for needed_cells new quads on the next
4658  // higher level
4659  triangulation.levels[level+1]->cells.
4660  reserve_space (needed_cells,0);
4661  }
4662 
4663  // now count the lines which were flagged for refinement
4664  for (typename Triangulation<dim,spacedim>::line_iterator
4665  line=triangulation.begin_line(); line!=triangulation.end_line(); ++line)
4666  if (line->user_flag_set())
4667  {
4668  Assert (line->has_children() == false, ExcInternalError());
4669  n_lines_in_pairs += 2;
4670  needed_vertices += 1;
4671  }
4672  // reserve space for n_lines_in_pairs new lines. note, that
4673  // we can't reserve space for the single lines here as well,
4674  // as all the space reserved for lines in pairs would be
4675  // counted as unused and we would end up with too little space
4676  // to store all lines. memory reservation for n_single_lines
4677  // can only be done AFTER we refined the lines of the current
4678  // cells
4679  triangulation.faces->lines.
4680  reserve_space (n_lines_in_pairs, 0);
4681 
4682  // add to needed vertices how many vertices are already in use
4683  needed_vertices += std::count_if (triangulation.vertices_used.begin(), triangulation.vertices_used.end(),
4684  std::bind (std::equal_to<bool>(), std::placeholders::_1, true));
4685  // if we need more vertices: create them, if not: leave the
4686  // array as is, since shrinking is not really possible because
4687  // some of the vertices at the end may be in use
4688  if (needed_vertices > triangulation.vertices.size())
4689  {
4690  triangulation.vertices.resize (needed_vertices, Point<spacedim>());
4691  triangulation.vertices_used.resize (needed_vertices, false);
4692  }
4693 
4694 
4695  // Do REFINEMENT on every level; exclude highest level as
4696  // above
4697 
4698  // index of next unused vertex
4699  unsigned int next_unused_vertex = 0;
4700 
4701  // first the refinement of lines. children are stored
4702  // pairwise
4703  if (true)
4704  {
4705  // only active objects can be refined further
4706  typename Triangulation<dim,spacedim>::active_line_iterator
4707  line = triangulation.begin_active_line(),
4708  endl = triangulation.end_line();
4709  typename Triangulation<dim,spacedim>::raw_line_iterator
4710  next_unused_line = triangulation.begin_raw_line ();
4711 
4712  for (; line!=endl; ++line)
4713  if (line->user_flag_set())
4714  {
4715  // this line needs to be refined
4716 
4717  // find the next unused vertex and set it
4718  // appropriately
4719  while (triangulation.vertices_used[next_unused_vertex] == true)
4720  ++next_unused_vertex;
4721  Assert (next_unused_vertex < triangulation.vertices.size(),
4722  ExcMessage("Internal error: During refinement, the triangulation wants to access an element of the 'vertices' array but it turns out that the array is not large enough."));
4723  triangulation.vertices_used[next_unused_vertex] = true;
4724 
4725  if (spacedim == dim)
4726  {
4727  // for the case of a domain in an
4728  // equal-dimensional space we only have to treat
4729  // boundary lines differently; for interior
4730  // lines we can compute the midpoint as the mean
4731  // of the two vertices: if (line->at_boundary())
4732  triangulation.vertices[next_unused_vertex]
4733  = line->center(true);
4734  }
4735  else
4736  // however, if spacedim>dim, we always have to ask
4737  // the boundary object for its answer. We use the
4738  // same object of the cell (which was stored in
4739  // line->user_index() before) unless a manifold_id
4740  // has been set on this very line.
4741  if (line->manifold_id() == numbers::invalid_manifold_id)
4742  triangulation.vertices[next_unused_vertex]
4743  = triangulation.get_manifold(line->user_index()).get_new_point_on_line (line);
4744  else
4745  triangulation.vertices[next_unused_vertex]
4746  = line->center(true);
4747 
4748  // now that we created the right point, make up the
4749  // two child lines. To this end, find a pair of
4750  // unused lines
4751  bool pair_found=false;
4752  (void)pair_found;
4753  for (; next_unused_line!=endl; ++next_unused_line)
4754  if (!next_unused_line->used() &&
4755  !(++next_unused_line)->used())
4756  {
4757  // go back to the first of the two unused
4758  // lines
4759  --next_unused_line;
4760  pair_found=true;
4761  break;
4762  }
4763  Assert (pair_found, ExcInternalError());
4764 
4765  // there are now two consecutive unused lines, such
4766  // that the children of a line will be consecutive.
4767  // then set the child pointer of the present line
4768  line->set_children (0, next_unused_line->index());
4769 
4770  // set the two new lines
4771  const typename Triangulation<dim,spacedim>::raw_line_iterator
4772  children[2] = { next_unused_line,
4773  ++next_unused_line
4774  };
4775  // some tests; if any of the iterators should be
4776  // invalid, then already dereferencing will fail
4777  Assert (children[0]->used() == false, ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
4778  Assert (children[1]->used() == false, ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
4779 
4780  children[0]->set (internal::TriangulationImplementation
4781  ::TriaObject<1>(line->vertex_index(0),
4782  next_unused_vertex));
4783  children[1]->set (internal::TriangulationImplementation
4784  ::TriaObject<1>(next_unused_vertex,
4785  line->vertex_index(1)));
4786 
4787  children[0]->set_used_flag();
4788  children[1]->set_used_flag();
4789  children[0]->clear_children();
4790  children[1]->clear_children();
4791  children[0]->clear_user_data();
4792  children[1]->clear_user_data();
4793  children[0]->clear_user_flag();
4794  children[1]->clear_user_flag();
4795 
4796 
4797  children[0]->set_boundary_id_internal(line->boundary_id());
4798  children[1]->set_boundary_id_internal(line->boundary_id());
4799 
4800  children[0]->set_manifold_id (line->manifold_id());
4801  children[1]->set_manifold_id (line->manifold_id());
4802 
4803  // finally clear flag indicating the need for
4804  // refinement
4805  line->clear_user_flag ();
4806  }
4807  }
4808 
4809 
4810  // Now set up the new cells
4811 
4812  // reserve space for inner lines (can be stored as single
4813  // lines)
4814  triangulation.faces->lines.
4815  reserve_space (0,n_single_lines);
4816 
4818  cells_with_distorted_children;
4819 
4820  // reset next_unused_line, as now also single empty places in
4821  // the vector can be used
4822  typename Triangulation<dim,spacedim>::raw_line_iterator
4823  next_unused_line = triangulation.begin_raw_line ();
4824 
4825  for (int level=0; level<static_cast<int>(triangulation.levels.size())-1; ++level)
4826  {
4827 
4828  // Remember: as we don't operate on the finest level,
4829  // begin_*(level+1) is allowed
4831  cell = triangulation.begin_active(level),
4832  endc = triangulation.begin_active(level+1);
4833 
4835  next_unused_cell = triangulation.begin_raw (level+1);
4836 
4837  for (; cell!=endc; ++cell)
4838  if (cell->refine_flag_set())
4839  {
4840  // set the user flag to indicate, that at least one
4841  // line is at the boundary
4842 
4843  // TODO[Tobias Leicht] find a better place to set
4844  // this flag, so that we do not need so much time to
4845  // check each cell here
4846  if (cell->at_boundary())
4847  cell->set_user_flag();
4848 
4849  // actually set up the children and update neighbor
4850  // information
4851  create_children (triangulation,
4852  next_unused_vertex,
4853  next_unused_line,
4854  next_unused_cell,
4855  cell);
4856 
4857  if ((check_for_distorted_cells == true)
4858  &&
4859  has_distorted_children (cell,
4860  std::integral_constant<int, dim>(),
4861  std::integral_constant<int, spacedim>()))
4862  cells_with_distorted_children.distorted_cells.push_back (cell);
4863  // inform all listeners that cell refinement is done
4864  triangulation.signals.post_refinement_on_cell(cell);
4865  }
4866  }
4867 
4868  return cells_with_distorted_children;
4869  }
4870 
4871 
4876  template <int spacedim>
4877  static
4880  const bool check_for_distorted_cells)
4881  {
4882  const unsigned int dim = 3;
4883 
4884  // this function probably also works for spacedim>3 but it
4885  // isn't tested. it will probably be necessary to pull new
4886  // vertices onto the manifold just as we do for the other
4887  // functions above.
4888  Assert (spacedim == 3, ExcNotImplemented());
4889 
4890  // check whether a new level is needed we have to check for
4891  // this on the highest level only (on this, all used cells are
4892  // also active, so we only have to check for this)
4893  if (true)
4894  {
4896  cell = triangulation.begin_active (triangulation.levels.size()-1),
4897  endc = triangulation.end();
4898  for (; cell != endc; ++cell)
4899  if (cell->used())
4900  if (cell->refine_flag_set())
4901  {
4902  triangulation.levels.push_back (std_cxx14::make_unique<internal::TriangulationImplementation::TriaLevel<dim>>());
4903  break;
4904  }
4905  }
4906 
4907 
4908  // first clear user flags for quads and lines; we're going to
4909  // use them to flag which lines and quads need refinement
4910  triangulation.faces->quads.clear_user_data();
4911 
4912  for (typename Triangulation<dim,spacedim>::line_iterator
4913  line=triangulation.begin_line(); line!=triangulation.end_line(); ++line)
4914  line->clear_user_flag();
4915  for (typename Triangulation<dim,spacedim>::quad_iterator
4916  quad=triangulation.begin_quad(); quad!=triangulation.end_quad(); ++quad)
4917  quad->clear_user_flag();
4918 
4919  // create an array of face refine cases. User indices of faces
4920  // will be set to values corresponding with indices in this
4921  // array.
4922  const RefinementCase<dim-1> face_refinement_cases[4]=
4923  {
4924  RefinementCase<dim-1>::no_refinement,
4925  RefinementCase<dim-1>::cut_x,
4926  RefinementCase<dim-1>::cut_y,
4927  RefinementCase<dim-1>::cut_xy
4928  };
4929 
4930  // check how much space is needed on every level we need not
4931  // check the highest level since either
4932  // - on the highest level no cells are flagged for refinement
4933  // - there are, but prepare_refinement added another empty
4934  // level which then is the highest level
4935 
4936  // variables to hold the number of newly to be created
4937  // vertices, lines and quads. as these are stored globally,
4938  // declare them outside the loop over al levels. we need lines
4939  // and quads in pairs for refinement of old ones and lines and
4940  // quads, that can be stored as single ones, as they are newly
4941  // created in the inside of an existing cell
4942  unsigned int needed_vertices = 0;
4943  unsigned int needed_lines_single = 0;
4944  unsigned int needed_quads_single = 0;
4945  unsigned int needed_lines_pair = 0;
4946  unsigned int needed_quads_pair = 0;
4947  for (int level=triangulation.levels.size()-2; level>=0; --level)
4948  {
4949  // count number of flagged cells on this level and compute
4950  // how many new vertices and new lines will be needed
4951  unsigned int new_cells = 0;
4952 
4954  acell = triangulation.begin_active(level),
4955  aendc = triangulation.begin_active(level+1);
4956  for (; acell!=aendc; ++acell)
4957  if (acell->refine_flag_set())
4958  {
4959  RefinementCase<dim> ref_case=acell->refine_flag_set();
4960 
4961  // now for interior vertices, lines and quads, which
4962  // are needed in any case
4963  if (ref_case==RefinementCase<dim>::cut_x ||
4964  ref_case==RefinementCase<dim>::cut_y ||
4965  ref_case==RefinementCase<dim>::cut_z)
4966  {
4967  ++needed_quads_single;
4968  new_cells+=2;
4969  triangulation.anisotropic_refinement=true;
4970  }
4971  else if (ref_case==RefinementCase<dim>::cut_xy ||
4972  ref_case==RefinementCase<dim>::cut_xz ||
4973  ref_case==RefinementCase<dim>::cut_yz)
4974  {
4975  ++needed_lines_single;
4976  needed_quads_single += 4;
4977  new_cells+=4;
4978  triangulation.anisotropic_refinement=true;
4979  }
4980  else if (ref_case==RefinementCase<dim>::cut_xyz)
4981  {
4982  ++needed_vertices;
4983  needed_lines_single += 6;
4984  needed_quads_single += 12;
4985  new_cells+=8;
4986  }
4987  else
4988  {
4989  // we should never get here
4990  Assert(false, ExcInternalError());
4991  }
4992 
4993  // mark all faces for refinement; checking locally
4994  // if and how the neighbor would like to refine
4995  // these is difficult so we only flag them and after
4996  // visiting all cells, we decide which faces need
4997  // which refinement;
4998  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell;
4999  ++face)
5000  {
5002  aface = acell->face(face);
5003  // get the RefineCase this faces has for the
5004  // given RefineCase of the cell
5005  RefinementCase<dim-1> face_ref_case=
5007  face,
5008  acell->face_orientation(face),
5009  acell->face_flip(face),
5010  acell->face_rotation(face));
5011  // only do something, if this face has to be
5012  // refined
5013  if (face_ref_case)
5014  {
5016  {
5017  if (aface->number_of_children()<4)
5018  // we use user_flags to denote needed
5019  // isotropic refinement
5020  aface->set_user_flag();
5021  }
5022  else if (aface->refinement_case()!=face_ref_case)
5023  // we use user_indices to denote needed
5024  // anisotropic refinement. note, that we
5025  // can have at most one anisotropic
5026  // refinement case for this face, as
5027  // otherwise prepare_refinement() would
5028  // have changed one of the cells to yield
5029  // isotropic refinement at this
5030  // face. therefore we set the user_index
5031  // uniquely
5032  {
5033  Assert(aface->refinement_case()==RefinementCase<dim-1>::isotropic_refinement ||
5034  aface->refinement_case()==RefinementCase<dim-1>::no_refinement,
5035  ExcInternalError());
5036  aface->set_user_index(face_ref_case);
5037  }
5038  }
5039  }// for all faces
5040 
5041  // flag all lines, that have to be refined
5042  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
5043  if (GeometryInfo<dim>::line_refinement_case(ref_case,line) &&
5044  !acell->line(line)->has_children())
5045  acell->line(line)->set_user_flag();
5046 
5047  }// if refine_flag set and for all cells on this level
5048 
5049 
5050  // count number of used cells on the next higher level
5051  const unsigned int used_cells
5052  = std::count_if (triangulation.levels[level+1]->cells.used.begin(),
5053  triangulation.levels[level+1]->cells.used.end(),
5054  std::bind (std::equal_to<bool>(), std::placeholders::_1, true));
5055 
5056 
5057  // reserve space for the used_cells cells already existing
5058  // on the next higher level as well as for the
5059  // 8*flagged_cells that will be created on that level
5060  triangulation.levels[level+1]
5061  ->reserve_space (used_cells+new_cells, 3, spacedim);
5062  // reserve space for 8*flagged_cells new hexes on the next
5063  // higher level
5064  triangulation.levels[level+1]->cells.reserve_space (new_cells);
5065  }// for all levels
5066  // now count the quads and lines which were flagged for
5067  // refinement
5068  for (typename Triangulation<dim,spacedim>::quad_iterator
5069  quad=triangulation.begin_quad(); quad!=triangulation.end_quad(); ++quad)
5070  {
5071  if (quad->user_flag_set())
5072  {
5073  // isotropic refinement: 1 interior vertex, 4 quads
5074  // and 4 interior lines. we store the interior lines
5075  // in pairs in case the face is already or will be
5076  // refined anisotropically
5077  needed_quads_pair += 4;
5078  needed_lines_pair += 4;
5079  needed_vertices += 1;
5080  }
5081  if (quad->user_index())
5082  {
5083  // anisotropic refinement: 1 interior
5084  // line and two quads
5085  needed_quads_pair += 2;
5086  needed_lines_single += 1;
5087  // there is a kind of complicated situation here which
5088  // requires our attention. if the quad is refined
5089  // isotropcally, two of the interior lines will get a
5090  // new mother line - the interior line of our
5091  // anisotropically refined quad. if those two lines
5092  // are not consecutive, we cannot do so and have to
5093  // replace them by two lines that are consecutive. we
5094  // try to avoid that situation, but it may happen
5095  // nevertheless through repeated refinement and
5096  // coarsening. thus we have to check here, as we will
5097  // need some additional space to store those new lines
5098  // in case we need them...
5099  if (quad->has_children())
5100  {
5101  Assert(quad->refinement_case()==RefinementCase<dim-1>::isotropic_refinement, ExcInternalError());
5102  if ((face_refinement_cases[quad->user_index()]==RefinementCase<dim-1>::cut_x
5103  && (quad->child(0)->line_index(1)+1!=quad->child(2)->line_index(1))) ||
5104  (face_refinement_cases[quad->user_index()]==RefinementCase<dim-1>::cut_y
5105  && (quad->child(0)->line_index(3)+1!=quad->child(1)->line_index(3))))
5106  needed_lines_pair +=2;
5107  }
5108  }
5109  }
5110 
5111  for (typename Triangulation<dim,spacedim>::line_iterator
5112  line=triangulation.begin_line(); line!=triangulation.end_line(); ++line)
5113  if (line->user_flag_set())
5114  {
5115  needed_lines_pair += 2;
5116  needed_vertices += 1;
5117  }
5118 
5119  // reserve space for needed_lines new lines stored in pairs
5120  triangulation.faces->lines.
5121  reserve_space (needed_lines_pair,needed_lines_single);
5122  // reserve space for needed_quads new quads stored in pairs
5123  triangulation.faces->quads.
5124  reserve_space (needed_quads_pair,needed_quads_single);
5125 
5126 
5127  // add to needed vertices how many vertices are already in use
5128  needed_vertices += std::count_if (triangulation.vertices_used.begin(), triangulation.vertices_used.end(),
5129  std::bind (std::equal_to<bool>(), std::placeholders::_1, true));
5130  // if we need more vertices: create them, if not: leave the
5131  // array as is, since shrinking is not really possible because
5132  // some of the vertices at the end may be in use
5133  if (needed_vertices > triangulation.vertices.size())
5134  {
5135  triangulation.vertices.resize (needed_vertices, Point<spacedim>());
5136  triangulation.vertices_used.resize (needed_vertices, false);
5137  }
5138 
5139 
5141  // Before we start with the actual refinement, we do some
5142  // sanity checks if in debug mode. especially, we try to catch
5143  // the notorious problem with lines being twice refined,
5144  // i.e. there are cells adjacent at one line ("around the
5145  // edge", but not at a face), with two cells differing by more
5146  // than one refinement level
5147  //
5148  // this check is very simple to implement here, since we have
5149  // all lines flagged if they shall be refined
5150 #ifdef DEBUG
5152  cell=triangulation.begin_active(); cell!=triangulation.end(); ++cell)
5153  if (!cell->refine_flag_set())
5154  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
5155  if (cell->line(line)->has_children())
5156  for (unsigned int c=0; c<2; ++c)
5157  Assert (cell->line(line)->child(c)->user_flag_set() == false,
5158  ExcInternalError());
5159 #endif
5160 
5162  // Do refinement on every level
5163  //
5164  // To make life a bit easier, we first refine those lines and
5165  // quads that were flagged for refinement and then compose the
5166  // newly to be created cells.
5167  //
5168  // index of next unused vertex
5169  unsigned int next_unused_vertex = 0;
5170 
5171  // first for lines
5172  if (true)
5173  {
5174  // only active objects can be refined further
5175  typename Triangulation<dim,spacedim>::active_line_iterator
5176  line = triangulation.begin_active_line(),
5177  endl = triangulation.end_line();
5178  typename Triangulation<dim,spacedim>::raw_line_iterator
5179  next_unused_line = triangulation.begin_raw_line ();
5180 
5181  for (; line!=endl; ++line)
5182  if (line->user_flag_set())
5183  {
5184  // this line needs to be refined
5185 
5186  // find the next unused vertex and set it
5187  // appropriately
5188  while (triangulation.vertices_used[next_unused_vertex] == true)
5189  ++next_unused_vertex;
5190  Assert (next_unused_vertex < triangulation.vertices.size(),
5191  ExcMessage("Internal error: During refinement, the triangulation wants to access an element of the 'vertices' array but it turns out that the array is not large enough."));
5192  triangulation.vertices_used[next_unused_vertex] = true;
5193 
5194  triangulation.vertices[next_unused_vertex]
5195  = line->center(true);
5196 
5197  // now that we created the right point, make up the
5198  // two child lines (++ takes care of the end of the
5199  // vector)
5200  next_unused_line=triangulation.faces->lines.next_free_pair_object(triangulation);
5201  Assert(next_unused_line.state() == IteratorState::valid,
5202  ExcInternalError());
5203 
5204  // now we found two consecutive unused lines, such
5205  // that the children of a line will be consecutive.
5206  // then set the child pointer of the present line
5207  line->set_children (0, next_unused_line->index());
5208 
5209  // set the two new lines
5210  const typename Triangulation<dim,spacedim>::raw_line_iterator
5211  children[2] = { next_unused_line,
5212  ++next_unused_line
5213  };
5214 
5215  // some tests; if any of the iterators should be
5216  // invalid, then already dereferencing will fail
5217  Assert (children[0]->used() == false, ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
5218  Assert (children[1]->used() == false, ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
5219 
5220  children[0]->set (internal::TriangulationImplementation
5221  ::TriaObject<1>(line->vertex_index(0),
5222  next_unused_vertex));
5223  children[1]->set (internal::TriangulationImplementation
5224  ::TriaObject<1>(next_unused_vertex,
5225  line->vertex_index(1)));
5226 
5227  children[0]->set_used_flag();
5228  children[1]->set_used_flag();
5229  children[0]->clear_children();
5230  children[1]->clear_children();
5231  children[0]->clear_user_data();
5232  children[1]->clear_user_data();
5233  children[0]->clear_user_flag();
5234  children[1]->clear_user_flag();
5235 
5236  children[0]->set_boundary_id_internal(line->boundary_id());
5237  children[1]->set_boundary_id_internal(line->boundary_id());
5238 
5239  children[0]->set_manifold_id (line->manifold_id());
5240  children[1]->set_manifold_id (line->manifold_id());
5241 
5242  // finally clear flag
5243  // indicating the need
5244  // for refinement
5245  line->clear_user_flag ();
5246  }
5247  }
5248 
5249 
5251  // now refine marked quads
5253 
5254  // here we encounter several cases:
5255 
5256  // a) the quad is unrefined and shall be refined isotropically
5257 
5258  // b) the quad is unrefined and shall be refined
5259  // anisotropically
5260 
5261  // c) the quad is unrefined and shall be refined both
5262  // anisotropically and isotropically (this is reduced to case
5263  // b) and then case b) for the children again)
5264 
5265  // d) the quad is refined anisotropically and shall be refined
5266  // isotropically (this is reduced to case b) for the
5267  // anisotropic children)
5268 
5269  // e) the quad is refined isotropically and shall be refined
5270  // anisotropically (this is transformed to case c), however we
5271  // might have to renumber/rename children...)
5272 
5273  // we need a loop in cases c) and d), as the anisotropic
5274  // children migt have a lower index than the mother quad
5275  for (unsigned int loop=0; loop<2; ++loop)
5276  {
5277  // usually, only active objects can be refined
5278  // further. however, in cases d) and e) that is not true,
5279  // so we have to use 'normal' iterators here
5280  typename Triangulation<dim,spacedim>::quad_iterator
5281  quad = triangulation.begin_quad(),
5282  endq = triangulation.end_quad();
5283  typename Triangulation<dim,spacedim>::raw_line_iterator
5284  next_unused_line = triangulation.begin_raw_line ();
5285  typename Triangulation<dim,spacedim>::raw_quad_iterator
5286  next_unused_quad = triangulation.begin_raw_quad ();
5287 
5288  for (; quad!=endq; ++quad)
5289  {
5290  if (quad->user_index())
5291  {
5292  RefinementCase<dim-1> aniso_quad_ref_case=face_refinement_cases[quad->user_index()];
5293  // there is one unlikely event here, where we
5294  // already have refind the face: if the face was
5295  // refined anisotropically and we want to refine
5296  // it isotropically, both children are flagged for
5297  // anisotropic refinement. however, if those
5298  // children were already flagged for anisotropic
5299  // refinement, they might already be processed and
5300  // refined.
5301  if (aniso_quad_ref_case == quad->refinement_case())
5302  continue;
5303 
5304  Assert(quad->refinement_case()==RefinementCase<dim-1>::cut_xy ||
5305  quad->refinement_case()==RefinementCase<dim-1>::no_refinement,
5306  ExcInternalError());
5307 
5308  // this quad needs to be refined anisotropically
5309  Assert(quad->user_index() == RefinementCase<dim-1>::cut_x ||
5310  quad->user_index() == RefinementCase<dim-1>::cut_y,
5311  ExcInternalError());
5312 
5313  // make the new line interior to the quad
5314  typename Triangulation<dim,spacedim>::raw_line_iterator new_line;
5315 
5316  new_line=triangulation.faces->lines.next_free_single_object(triangulation);
5317  Assert (new_line->used() == false,
5318  ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
5319 
5320  // first collect the
5321  // indices of the vertices:
5322  // *--1--*
5323  // | | |
5324  // | | | cut_x
5325  // | | |
5326  // *--0--*
5327  //
5328  // *-----*
5329  // | |
5330  // 0-----1 cut_y
5331  // | |
5332  // *-----*
5333  unsigned int vertex_indices[2];
5334  if (aniso_quad_ref_case==RefinementCase<dim-1>::cut_x)
5335  {
5336  vertex_indices[0]=quad->line(2)->child(0)->vertex_index(1);
5337  vertex_indices[1]=quad->line(3)->child(0)->vertex_index(1);
5338  }
5339  else
5340  {
5341  vertex_indices[0]=quad->line(0)->child(0)->vertex_index(1);
5342  vertex_indices[1]=quad->line(1)->child(0)->vertex_index(1);
5343  }
5344 
5345  new_line->set (internal::TriangulationImplementation::
5346  TriaObject<1>(vertex_indices[0], vertex_indices[1]));
5347  new_line->set_used_flag();
5348  new_line->clear_user_flag();
5349  new_line->clear_user_data();
5350  new_line->clear_children();
5351  new_line->set_boundary_id_internal(quad->boundary_id());
5352  new_line->set_manifold_id(quad->manifold_id());
5353 
5354  // child 0 and 1 of a line are switched if the
5355  // line orientation is false. set up a miniature
5356  // table, indicating which child to take for line
5357  // orientations false and true. first index: child
5358  // index in standard orientation, second index:
5359  // line orientation
5360  const unsigned int index[2][2]=
5361  {
5362  {1,0}, // child 0, line_orientation=false and true
5363  {0,1}
5364  }; // child 1, line_orientation=false and true
5365 
5366  // find some space (consecutive) for the two newly
5367  // to be created quads.
5368  typename Triangulation<dim,spacedim>::raw_quad_iterator new_quads[2];
5369 
5370  next_unused_quad=triangulation.faces->quads.next_free_pair_object(triangulation);
5371  new_quads[0] = next_unused_quad;
5372  Assert (new_quads[0]->used() == false, ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
5373 
5374  ++next_unused_quad;
5375  new_quads[1] = next_unused_quad;
5376  Assert (new_quads[1]->used() == false, ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
5377 
5378 
5379  if (aniso_quad_ref_case==RefinementCase<dim-1>::cut_x)
5380  {
5381  new_quads[0]->set (internal::TriangulationImplementation
5382  ::TriaObject<2>(quad->line_index(0),
5383  new_line->index(),
5384  quad->line(2)->child(index[0][quad->line_orientation(2)])->index(),
5385  quad->line(3)->child(index[0][quad->line_orientation(3)])->index()));
5386  new_quads[1]->set (internal::TriangulationImplementation
5387  ::TriaObject<2>(new_line->index(),
5388  quad->line_index(1),
5389  quad->line(2)->child(index[1][quad->line_orientation(2)])->index(),
5390  quad->line(3)->child(index[1][quad->line_orientation(3)])->index()));
5391  }
5392  else
5393  {
5394  new_quads[0]->set (internal::TriangulationImplementation
5395  ::TriaObject<2>(quad->line(0)->child(index[0][quad->line_orientation(0)])->index(),
5396  quad->line(1)->child(index[0][quad->line_orientation(1)])->index(),
5397  quad->line_index(2),
5398  new_line->index()));
5399  new_quads[1]->set (internal::TriangulationImplementation
5400  ::TriaObject<2>(quad->line(0)->child(index[1][quad->line_orientation(0)])->index(),
5401  quad->line(1)->child(index[1][quad->line_orientation(1)])->index(),
5402  new_line->index(),
5403  quad->line_index(3)));
5404  }
5405 
5406  for (unsigned int i=0; i<2; ++i)
5407  {
5408  new_quads[i]->set_used_flag();
5409  new_quads[i]->clear_user_flag();
5410  new_quads[i]->clear_user_data();
5411  new_quads[i]->clear_children();
5412  new_quads[i]->set_boundary_id_internal(quad->boundary_id());
5413  new_quads[i]->set_manifold_id (quad->manifold_id());
5414  // set all line orientations to true, change
5415  // this after the loop, as we have to consider
5416  // different lines for each child
5417  for (unsigned int j=0; j<GeometryInfo<dim>::lines_per_face; ++j)
5418  new_quads[i]->set_line_orientation(j,true);
5419  }
5420  // now set the line orientation of children of
5421  // outer lines correctly, the lines in the
5422  // interior of the refined quad are automatically
5423  // oriented conforming to the standard
5424  new_quads[0]->set_line_orientation(0,quad->line_orientation(0));
5425  new_quads[0]->set_line_orientation(2,quad->line_orientation(2));
5426  new_quads[1]->set_line_orientation(1,quad->line_orientation(1));
5427  new_quads[1]->set_line_orientation(3,quad->line_orientation(3));
5428  if (aniso_quad_ref_case==RefinementCase<dim-1>::cut_x)
5429  {
5430  new_quads[0]->set_line_orientation(3,quad->line_orientation(3));
5431  new_quads[1]->set_line_orientation(2,quad->line_orientation(2));
5432  }
5433  else
5434  {
5435  new_quads[0]->set_line_orientation(1,quad->line_orientation(1));
5436  new_quads[1]->set_line_orientation(0,quad->line_orientation(0));
5437  }
5438 
5439  // test, whether this face is refined
5440  // isotropically already. if so, set the correct
5441  // children pointers.
5442  if (quad->refinement_case()==RefinementCase<dim-1>::cut_xy)
5443  {
5444  // we will put a new refinemnt level of
5445  // anisotropic refinement between the
5446  // unrefined and isotropically refined quad
5447  // ending up with the same fine quads but
5448  // introducing anisotropically refined ones as
5449  // children of the unrefined quad and mother
5450  // cells of the original fine ones.
5451 
5452  // this process includes the creation of a new
5453  // middle line which we will assign as the
5454  // mother line of two of the existing inner
5455  // lines. If those inner lines are not
5456  // consecutive in memory, we won't find them
5457  // later on, so we have to create new ones
5458  // instead and replace all occurrences of the
5459  // old ones with those new ones. As this is
5460  // kind of ugly, we hope we don't have to do
5461  // it often...
5462  typename Triangulation<dim,spacedim>::line_iterator old_child[2];
5463  if (aniso_quad_ref_case==RefinementCase<dim-1>::cut_x)
5464  {
5465  old_child[0]=quad->child(0)->line(1);
5466  old_child[1]=quad->child(2)->line(1);
5467  }
5468  else
5469  {
5470  Assert(aniso_quad_ref_case==RefinementCase<dim-1>::cut_y, ExcInternalError());
5471 
5472  old_child[0]=quad->child(0)->line(3);
5473  old_child[1]=quad->child(1)->line(3);
5474  }
5475 
5476  if (old_child[0]->index()+1 != old_child[1]->index())
5477  {
5478  // this is exactly the ugly case we taked
5479  // about. so, no coimplaining, lets get
5480  // two new lines and copy all info
5481  typename Triangulation<dim,spacedim>::raw_line_iterator new_child[2];
5482 
5483  new_child[0]=new_child[1]=triangulation.faces->lines.next_free_pair_object(triangulation);
5484  ++new_child[1];
5485 
5486  new_child[0]->set_used_flag();
5487  new_child[1]->set_used_flag();
5488 
5489  const int old_index_0=old_child[0]->index(),
5490  old_index_1=old_child[1]->index(),
5491  new_index_0=new_child[0]->index(),
5492  new_index_1=new_child[1]->index();
5493 
5494  // loop over all quads and replace the old
5495  // lines
5496  for (unsigned int q=0; q<triangulation.faces->quads.cells.size(); ++q)
5497  for (unsigned int l=0; l<GeometryInfo<dim>::lines_per_face; ++l)
5498  {
5499  const int this_index=triangulation.faces->quads.cells[q].face(l);
5500  if (this_index==old_index_0)
5501  triangulation.faces->quads.cells[q].set_face(l,new_index_0);
5502  else if (this_index==old_index_1)
5503  triangulation.faces->quads.cells[q].set_face(l,new_index_1);
5504  }
5505  // now we have to copy all information of
5506  // the two lines
5507  for (unsigned int i=0; i<2; ++i)
5508  {
5509  Assert(!old_child[i]->has_children(), ExcInternalError());
5510 
5511  new_child[i]->set(internal::TriangulationImplementation::TriaObject<1>(old_child[i]->vertex_index(0),
5512  old_child[i]->vertex_index(1)));
5513  new_child[i]->set_boundary_id_internal(old_child[i]->boundary_id());
5514  new_child[i]->set_manifold_id(old_child[i]->manifold_id());
5515  new_child[i]->set_user_index(old_child[i]->user_index());
5516  if (old_child[i]->user_flag_set())
5517  new_child[i]->set_user_flag();
5518  else
5519  new_child[i]->clear_user_flag();
5520 
5521  new_child[i]->clear_children();
5522 
5523  old_child[i]->clear_user_flag();
5524  old_child[i]->clear_user_index();
5525  old_child[i]->clear_used_flag();
5526  }
5527  }
5528  // now that we cared about the lines, go on
5529  // with the quads themselves, where we might
5530  // encounter similar situations...
5531  if (aniso_quad_ref_case==RefinementCase<dim-1>::cut_x)
5532  {
5533  new_line->set_children(0, quad->child(0)->line_index(1));
5534  Assert(new_line->child(1)==quad->child(2)->line(1),
5535  ExcInternalError());
5536  // now evereything is quite
5537  // complicated. we have the children
5538  // numbered according to
5539  //
5540  // *---*---*
5541  // |n+2|n+3|
5542  // *---*---*
5543  // | n |n+1|
5544  // *---*---*
5545  //
5546  // from the original isotropic
5547  // refinement. we have to reorder them as
5548  //
5549  // *---*---*
5550  // |n+1|n+3|
5551  // *---*---*
5552  // | n |n+2|
5553  // *---*---*
5554  //
5555  // such that n and n+1 are consecutive
5556  // children of m and n+2 and n+3 are
5557  // consecutive children of m+1, where m
5558  // and m+1 are given as in
5559  //
5560  // *---*---*
5561  // | | |
5562  // | m |m+1|
5563  // | | |
5564  // *---*---*
5565  //
5566  // this is a bit ugly, of course: loop
5567  // over all cells on all levels and look
5568  // for faces n+1 (switch_1) and n+2
5569  // (switch_2).
5570  const typename Triangulation<dim,spacedim>::quad_iterator
5571  switch_1=quad->child(1),
5572  switch_2=quad->child(2);
5573  const int switch_1_index=switch_1->index();
5574  const int switch_2_index=switch_2->index();
5575  for (unsigned int l=0; l<triangulation.levels.size(); ++l)
5576  for (unsigned int h=0; h<triangulation.levels[l]->cells.cells.size(); ++h)
5577  for (unsigned int q=0; q<GeometryInfo<dim>::faces_per_cell; ++q)
5578  {
5579  const int face_index=triangulation.levels[l]->cells.cells[h].face(q);
5580  if (face_index==switch_1_index)
5581  triangulation.levels[l]->cells.cells[h].set_face(q,switch_2_index);
5582  else if (face_index==switch_2_index)
5583  triangulation.levels[l]->cells.cells[h].set_face(q,switch_1_index);
5584  }
5585  // now we have to copy all information of
5586  // the two quads
5587  const unsigned int switch_1_lines[4]=
5588  {
5589  switch_1->line_index(0),
5590  switch_1->line_index(1),
5591  switch_1->line_index(2),
5592  switch_1->line_index(3)
5593  };
5594  const bool switch_1_line_orientations[4]=
5595  {
5596  switch_1->line_orientation(0),
5597  switch_1->line_orientation(1),
5598  switch_1->line_orientation(2),
5599  switch_1->line_orientation(3)
5600  };
5601  const types::boundary_id switch_1_boundary_id=switch_1->boundary_id();
5602  const unsigned int switch_1_user_index=switch_1->user_index();
5603  const bool switch_1_user_flag=switch_1->user_flag_set();
5604  const RefinementCase<dim-1> switch_1_refinement_case=switch_1->refinement_case();
5605  const int switch_1_first_child_pair=(switch_1_refinement_case ? switch_1->child_index(0) : -1);
5606  const int switch_1_second_child_pair=(switch_1_refinement_case==RefinementCase<dim-1>::cut_xy ? switch_1->child_index(2) : -1);
5607 
5608  switch_1->set(internal::TriangulationImplementation::TriaObject<2>(switch_2->line_index(0),
5609  switch_2->line_index(1),
5610  switch_2->line_index(2),
5611  switch_2->line_index(3)));
5612  switch_1->set_line_orientation(0, switch_2->line_orientation(0));
5613  switch_1->set_line_orientation(1, switch_2->line_orientation(1));
5614  switch_1->set_line_orientation(2, switch_2->line_orientation(2));
5615  switch_1->set_line_orientation(3, switch_2->line_orientation(3));
5616  switch_1->set_boundary_id_internal(switch_2->boundary_id());
5617  switch_1->set_manifold_id(switch_2->manifold_id());
5618  switch_1->set_user_index(switch_2->user_index());
5619  if (switch_2->user_flag_set())
5620  switch_1->set_user_flag();
5621  else
5622  switch_1->clear_user_flag();
5623  switch_1->clear_refinement_case();
5624  switch_1->set_refinement_case(switch_2->refinement_case());
5625  switch_1->clear_children();
5626  if (switch_2->refinement_case())
5627  switch_1->set_children(0, switch_2->child_index(0));
5628  if (switch_2->refinement_case()==RefinementCase<dim-1>::cut_xy)
5629  switch_1->set_children(2, switch_2->child_index(2));
5630 
5631  switch_2->set(internal::TriangulationImplementation::TriaObject<2>(switch_1_lines[0],
5632  switch_1_lines[1],
5633  switch_1_lines[2],
5634  switch_1_lines[3]));
5635  switch_2->set_line_orientation(0, switch_1_line_orientations[0]);
5636  switch_2->set_line_orientation(1, switch_1_line_orientations[1]);
5637  switch_2->set_line_orientation(2, switch_1_line_orientations[2]);
5638  switch_2->set_line_orientation(3, switch_1_line_orientations[3]);
5639  switch_2->set_boundary_id_internal(switch_1_boundary_id);
5640  switch_2->set_manifold_id(switch_1->manifold_id());
5641  switch_2->set_user_index(switch_1_user_index);
5642  if (switch_1_user_flag)
5643  switch_2->set_user_flag();
5644  else
5645  switch_2->clear_user_flag();
5646  switch_2->clear_refinement_case();
5647  switch_2->set_refinement_case(switch_1_refinement_case);
5648  switch_2->clear_children();
5649  switch_2->set_children(0, switch_1_first_child_pair);
5650  switch_2->set_children(2, switch_1_second_child_pair);
5651 
5652  new_quads[0]->set_refinement_case(RefinementCase<2>::cut_y);
5653  new_quads[0]->set_children(0, quad->child_index(0));
5654  new_quads[1]->set_refinement_case(RefinementCase<2>::cut_y);
5655  new_quads[1]->set_children(0, quad->child_index(2));
5656  }
5657  else
5658  {
5659  new_quads[0]->set_refinement_case(RefinementCase<2>::cut_x);
5660  new_quads[0]->set_children(0, quad->child_index(0));
5661  new_quads[1]->set_refinement_case(RefinementCase<2>::cut_x);
5662  new_quads[1]->set_children(0, quad->child_index(2));
5663  new_line->set_children(0, quad->child(0)->line_index(3));
5664  Assert(new_line->child(1)==quad->child(1)->line(3),
5665  ExcInternalError());
5666  }
5667  quad->clear_children();
5668  }
5669 
5670  // note these quads as children to the present one
5671  quad->set_children (0, new_quads[0]->index());
5672 
5673  quad->set_refinement_case(aniso_quad_ref_case);
5674 
5675  // finally clear flag indicating the need for
5676  // refinement
5677  quad->clear_user_data ();
5678  } // if (anisotropic refinement)
5679 
5680  if (quad->user_flag_set())
5681  {
5682  // this quad needs to be refined isotropically
5683 
5684  // first of all: we only get here in the first run
5685  // of the loop
5687 
5688  // find the next unused vertex. we'll need this in
5689  // any case
5690  while (triangulation.vertices_used[next_unused_vertex] == true)
5691  ++next_unused_vertex;
5692  Assert (next_unused_vertex < triangulation.vertices.size(),
5693  ExcMessage("Internal error: During refinement, the triangulation wants to access an element of the 'vertices' array but it turns out that the array is not large enough."));
5694 
5695  // now: if the quad is refined anisotropically
5696  // already, set the anisotropic refinement flag
5697  // for both children. Additionally, we have to
5698  // refine the inner line, as it is an outer line
5699  // of the two (anisotropic) children
5700  const RefinementCase<dim-1> quad_ref_case=quad->refinement_case();
5701 
5702  if (quad_ref_case==RefinementCase<dim-1>::cut_x ||
5703  quad_ref_case==RefinementCase<dim-1>::cut_y)
5704  {
5705  // set the 'opposite' refine case for children
5706  quad->child(0)->set_user_index(RefinementCase<dim-1>::cut_xy-quad_ref_case);
5707  quad->child(1)->set_user_index(RefinementCase<dim-1>::cut_xy-quad_ref_case);
5708  // refine the inner line
5709  typename Triangulation<dim,spacedim>::line_iterator middle_line;
5710  if (quad_ref_case==RefinementCase<dim-1>::cut_x)
5711  middle_line=quad->child(0)->line(1);
5712  else
5713  middle_line=quad->child(0)->line(3);
5714 
5715  // if the face has been refined
5716  // anisotropically in the last refinement step
5717  // it might be, that it is flagged already and
5718  // that the middle line is thus refined
5719  // already. if not create children.
5720  if (!middle_line->has_children())
5721  {
5722  // set the middle vertex
5723  // appropriately. double refinement of
5724  // quads can only happen in the interior
5725  // of the domain, so we need not care
5726  // about boundary quads here
5727  triangulation.vertices[next_unused_vertex]
5728  = middle_line->center(true);
5729  triangulation.vertices_used[next_unused_vertex] = true;
5730 
5731  // now search a slot for the two
5732  // child lines
5733  next_unused_line=triangulation.faces->lines.next_free_pair_object(triangulation);
5734 
5735  // set the child pointer of the present
5736  // line
5737  middle_line->set_children (0, next_unused_line->index());
5738 
5739  // set the two new lines
5740  const typename Triangulation<dim,spacedim>::raw_line_iterator
5741  children[2] = { next_unused_line,
5742  ++next_unused_line
5743  };
5744 
5745  // some tests; if any of the iterators
5746  // should be invalid, then already
5747  // dereferencing will fail
5748  Assert (children[0]->used() == false, ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
5749  Assert (children[1]->used() == false, ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
5750 
5751  children[0]->set (internal::TriangulationImplementation::
5752  TriaObject<1>(middle_line->vertex_index(0),
5753  next_unused_vertex));
5754  children[1]->set (internal::TriangulationImplementation::
5755  TriaObject<1>(next_unused_vertex,
5756  middle_line->vertex_index(1)));
5757 
5758  children[0]->set_used_flag();
5759  children[1]->set_used_flag();
5760  children[0]->clear_children();
5761  children[1]->clear_children();
5762  children[0]->clear_user_data();
5763  children[1]->clear_user_data();
5764  children[0]->clear_user_flag();
5765  children[1]->clear_user_flag();
5766 
5767  children[0]->set_boundary_id_internal (middle_line->boundary_id());
5768  children[1]->set_boundary_id_internal (middle_line->boundary_id());
5769 
5770  children[0]->set_manifold_id (middle_line->manifold_id());
5771  children[1]->set_manifold_id (middle_line->manifold_id());
5772  }
5773  // now remove the flag from the quad and go to
5774  // the next quad, the actual refinement of the
5775  // quad takes place later on in this pass of
5776  // the loop or in the next one
5777  quad->clear_user_flag();
5778  continue;
5779  } // if (several refinement cases)
5780 
5781  // if we got here, we have an unrefined quad and
5782  // have to do the usual work like in an purely
5783  // isotropic refinement
5785 
5786  // set the middle vertex appropriately: it might be that
5787  // the quad itself is not at the boundary, but that one of
5788  // its lines actually is. in this case, the newly created
5789  // vertices at the centers of the lines are not
5790  // necessarily the mean values of the adjacent vertices,
5791  // so do not compute the new vertex as the mean value of
5792  // the 4 vertices of the face, but rather as a weighted
5793  // mean value of the 8 vertices which we already have (the
5794  // four old ones, and the four ones inserted as middle
5795  // points for the four lines). summing up some more points
5796  // is generally cheaper than first asking whether one of
5797  // the lines is at the boundary
5798  //
5799  // note that the exact weights are chosen such as to
5800  // minimize the distortion of the four new quads from the
5801  // optimal shape. their description uses the formulas
5802  // underlying the TransfiniteInterpolationManifold
5803  // implementation
5804  triangulation.vertices[next_unused_vertex] =
5805  quad->center(true, true);
5806  triangulation.vertices_used[next_unused_vertex] = true;
5807 
5808  // now that we created the right point, make up
5809  // the four lines interior to the quad (++ takes
5810  // care of the end of the vector)
5811  typename Triangulation<dim,spacedim>::raw_line_iterator new_lines[4];
5812 
5813  for (unsigned int i=0; i<4; ++i)
5814  {
5815  if (i%2==0)
5816  // search a free pair of lines for 0. and
5817  // 2. line, so that two of them end up
5818  // together, which is necessary if later on
5819  // we want to refine the quad
5820  // anisotropically and the two lines end up
5821  // as children of new line
5822  next_unused_line=triangulation.faces->lines.next_free_pair_object(triangulation);
5823 
5824  new_lines[i] = next_unused_line;
5825  ++next_unused_line;
5826 
5827  Assert (new_lines[i]->used() == false,
5828  ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
5829  }
5830 
5831  // set the data of the four lines. first collect
5832  // the indices of the five vertices:
5833  //
5834  // *--3--*
5835  // | | |
5836  // 0--4--1
5837  // | | |
5838  // *--2--*
5839  //
5840  // the lines are numbered as follows:
5841  //
5842  // *--*--*
5843  // | 1 |
5844  // *2-*-3*
5845  // | 0 |
5846  // *--*--*
5847 
5848  const unsigned int vertex_indices[5]
5849  = { quad->line(0)->child(0)->vertex_index(1),
5850  quad->line(1)->child(0)->vertex_index(1),
5851  quad->line(2)->child(0)->vertex_index(1),
5852  quad->line(3)->child(0)->vertex_index(1),
5853  next_unused_vertex
5854  };
5855 
5856  new_lines[0]->set (internal::TriangulationImplementation::
5857  TriaObject<1>(vertex_indices[2], vertex_indices[4]));
5858  new_lines[1]->set (internal::TriangulationImplementation::
5859  TriaObject<1>(vertex_indices[4], vertex_indices[3]));
5860  new_lines[2]->set (internal::TriangulationImplementation::
5861  TriaObject<1>(vertex_indices[0], vertex_indices[4]));
5862  new_lines[3]->set (internal::TriangulationImplementation::
5863  TriaObject<1>(vertex_indices[4], vertex_indices[1]));
5864 
5865  for (unsigned int i=0; i<4; ++i)
5866  {
5867  new_lines[i]->set_used_flag();
5868  new_lines[i]->clear_user_flag();
5869  new_lines[i]->clear_user_data();
5870  new_lines[i]->clear_children();
5871  new_lines[i]->set_boundary_id_internal(quad->boundary_id());
5872  new_lines[i]->set_manifold_id(quad->manifold_id());
5873  }
5874 
5875  // now for the quads. again, first collect some
5876  // data about the indices of the lines, with the
5877  // following numbering:
5878  //
5879  // .-6-.-7-.
5880  // 1 9 3
5881  // .-10.11-.
5882  // 0 8 2
5883  // .-4-.-5-.
5884 
5885  // child 0 and 1 of a line are switched if the
5886  // line orientation is false. set up a miniature
5887  // table, indicating which child to take for line
5888  // orientations false and true. first index: child
5889  // index in standard orientation, second index:
5890  // line orientation
5891  const unsigned int index[2][2]=
5892  {
5893  {1,0}, // child 0, line_orientation=false and true
5894  {0,1}
5895  }; // child 1, line_orientation=false and true
5896 
5897  const int line_indices[12]
5898  = { quad->line(0)->child(index[0][quad->line_orientation(0)])->index(),
5899  quad->line(0)->child(index[1][quad->line_orientation(0)])->index(),
5900  quad->line(1)->child(index[0][quad->line_orientation(1)])->index(),
5901  quad->line(1)->child(index[1][quad->line_orientation(1)])->index(),
5902  quad->line(2)->child(index[0][quad->line_orientation(2)])->index(),
5903  quad->line(2)->child(index[1][quad->line_orientation(2)])->index(),
5904  quad->line(3)->child(index[0][quad->line_orientation(3)])->index(),
5905  quad->line(3)->child(index[1][quad->line_orientation(3)])->index(),
5906  new_lines[0]->index(),
5907  new_lines[1]->index(),
5908  new_lines[2]->index(),
5909  new_lines[3]->index()
5910  };
5911 
5912  // find some space (consecutive)
5913  // for the first two newly to be
5914  // created quads.
5915  typename Triangulation<dim,spacedim>::raw_quad_iterator new_quads[4];
5916 
5917  next_unused_quad=triangulation.faces->quads.next_free_pair_object(triangulation);
5918 
5919  new_quads[0] = next_unused_quad;
5920  Assert (new_quads[0]->used() == false, ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
5921 
5922  ++next_unused_quad;
5923  new_quads[1] = next_unused_quad;
5924  Assert (new_quads[1]->used() == false, ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
5925 
5926  next_unused_quad=triangulation.faces->quads.next_free_pair_object(triangulation);
5927  new_quads[2] = next_unused_quad;
5928  Assert (new_quads[2]->used() == false, ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
5929 
5930  ++next_unused_quad;
5931  new_quads[3] = next_unused_quad;
5932  Assert (new_quads[3]->used() == false, ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
5933 
5934  // note these quads as children to the present one
5935  quad->set_children (0, new_quads[0]->index());
5936  quad->set_children (2, new_quads[2]->index());
5937  new_quads[0]->set (internal::TriangulationImplementation
5938  ::TriaObject<2>(line_indices[0],
5939  line_indices[8],
5940  line_indices[4],
5941  line_indices[10]));
5942 
5943  quad->set_refinement_case(RefinementCase<2>::cut_xy);
5944 
5945  new_quads[0]->set (internal::TriangulationImplementation
5946  ::TriaObject<2>(line_indices[0],
5947  line_indices[8],
5948  line_indices[4],
5949  line_indices[10]));
5950  new_quads[1]->set (internal::TriangulationImplementation
5951  ::TriaObject<2>(line_indices[8],
5952  line_indices[2],
5953  line_indices[5],
5954  line_indices[11]));
5955  new_quads[2]->set (internal::TriangulationImplementation
5956  ::TriaObject<2>(line_indices[1],
5957  line_indices[9],
5958  line_indices[10],
5959  line_indices[6]));
5960  new_quads[3]->set (internal::TriangulationImplementation
5961  ::TriaObject<2>(line_indices[9],
5962  line_indices[3],
5963  line_indices[11],
5964  line_indices[7]));
5965  for (unsigned int i=0; i<4; ++i)
5966  {
5967  new_quads[i]->set_used_flag();
5968  new_quads[i]->clear_user_flag();
5969  new_quads[i]->clear_user_data();
5970  new_quads[i]->clear_children();
5971  new_quads[i]->set_boundary_id_internal (quad->boundary_id());
5972  new_quads[i]->set_manifold_id (quad->manifold_id());
5973  // set all line orientations to true, change
5974  // this after the loop, as we have to consider
5975  // different lines for each child
5976  for (unsigned int j=0; j<GeometryInfo<dim>::lines_per_face; ++j)
5977  new_quads[i]->set_line_orientation(j,true);
5978  }
5979  // now set the line orientation of children of
5980  // outer lines correctly, the lines in the
5981  // interior of the refined quad are automatically
5982  // oriented conforming to the standard
5983  new_quads[0]->set_line_orientation(0,quad->line_orientation(0));
5984  new_quads[0]->set_line_orientation(2,quad->line_orientation(2));
5985  new_quads[1]->set_line_orientation(1,quad->line_orientation(1));
5986  new_quads[1]->set_line_orientation(2,quad->line_orientation(2));
5987  new_quads[2]->set_line_orientation(0,quad->line_orientation(0));
5988  new_quads[2]->set_line_orientation(3,quad->line_orientation(3));
5989  new_quads[3]->set_line_orientation(1,quad->line_orientation(1));
5990  new_quads[3]->set_line_orientation(3,quad->line_orientation(3));
5991 
5992  // finally clear flag indicating the need for
5993  // refinement
5994  quad->clear_user_flag ();
5995  } // if (isotropic refinement)
5996  } // for all quads
5997  } // looped two times over all quads, all quads refined now
5998 
6000  // Now, finally, set up the new
6001  // cells
6003 
6005  cells_with_distorted_children;
6006 
6007  for (unsigned int level=0; level!=triangulation.levels.size()-1; ++level)
6008  {
6009  // only active objects can be refined further; remember
6010  // that we won't operate on the finest level, so
6011  // triangulation.begin_*(level+1) is allowed
6012  typename Triangulation<dim,spacedim>::active_hex_iterator
6013  hex = triangulation.begin_active_hex(level),
6014  endh = triangulation.begin_active_hex(level+1);
6015  typename Triangulation<dim,spacedim>::raw_hex_iterator
6016  next_unused_hex = triangulation.begin_raw_hex (level+1);
6017 
6018  for (; hex!=endh; ++hex)
6019  if (hex->refine_flag_set())
6020  {
6021  // this hex needs to be refined
6022 
6023  // clear flag indicating the need for refinement. do
6024  // it here already, since we can't do it anymore
6025  // once the cell has children
6026  const RefinementCase<dim> ref_case=hex->refine_flag_set();
6027  hex->clear_refine_flag ();
6028  hex->set_refinement_case(ref_case);
6029 
6030  // depending on the refine case we might have to
6031  // create additional vertices, lines and quads
6032  // interior of the hex before the actual children
6033  // can be set up.
6034 
6035  // in a first step: reserve the needed space for
6036  // lines, quads and hexes and initialize them
6037  // correctly
6038 
6039  unsigned int n_new_lines=0;
6040  unsigned int n_new_quads=0;
6041  unsigned int n_new_hexes=0;
6042  switch (ref_case)
6043  {
6047  n_new_lines=0;
6048  n_new_quads=1;
6049  n_new_hexes=2;
6050  break;
6054  n_new_lines=1;
6055  n_new_quads=4;
6056  n_new_hexes=4;
6057  break;
6059  n_new_lines=6;
6060  n_new_quads=12;
6061  n_new_hexes=8;
6062  break;
6063  default:
6064  Assert(false, ExcInternalError());
6065  break;
6066  }
6067 
6068  // find some space for the newly to be created
6069  // interior lines and initialize them.
6070  std::vector<typename Triangulation<dim,spacedim>::raw_line_iterator>
6071  new_lines(n_new_lines);
6072  for (unsigned int i=0; i<n_new_lines; ++i)
6073  {
6074  new_lines[i] = triangulation.faces->lines.next_free_single_object(triangulation);
6075 
6076  Assert (new_lines[i]->used() == false,
6077  ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
6078  new_lines[i]->set_used_flag();
6079  new_lines[i]->clear_user_flag();
6080  new_lines[i]->clear_user_data();
6081  new_lines[i]->clear_children();
6082  // interior line
6083  new_lines[i]->set_boundary_id_internal(numbers::internal_face_boundary_id);
6084  // they inherit geometry description of the hex they belong to
6085  new_lines[i]->set_manifold_id(hex->manifold_id());
6086  }
6087 
6088  // find some space for the newly to be created
6089  // interior quads and initialize them.
6090  std::vector<typename Triangulation<dim,spacedim>::raw_quad_iterator>
6091  new_quads(n_new_quads);
6092  for (unsigned int i=0; i<n_new_quads; ++i)
6093  {
6094  new_quads[i] = triangulation.faces->quads.next_free_single_object(triangulation);
6095 
6096  Assert (new_quads[i]->used() == false,
6097  ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
6098  new_quads[i]->set_used_flag();
6099  new_quads[i]->clear_user_flag();
6100  new_quads[i]->clear_user_data();
6101  new_quads[i]->clear_children();
6102  // interior quad
6103  new_quads[i]->set_boundary_id_internal(numbers::internal_face_boundary_id);
6104  // they inherit geometry description of the hex they belong to
6105  new_quads[i]->set_manifold_id (hex->manifold_id());
6106  // set all line orientation flags to true by
6107  // default, change this afterwards, if necessary
6108  for (unsigned int j=0; j<GeometryInfo<dim>::lines_per_face; ++j)
6109  new_quads[i]->set_line_orientation(j,true);
6110  }
6111 
6112  types::subdomain_id subdomainid = hex->subdomain_id();
6113 
6114  // find some space for the newly to be created hexes
6115  // and initialize them.
6116  std::vector<typename Triangulation<dim,spacedim>::raw_hex_iterator>
6117  new_hexes(n_new_hexes);
6118  for (unsigned int i=0; i<n_new_hexes; ++i)
6119  {
6120  if (i%2==0)
6121  next_unused_hex=triangulation.levels[level+1]->cells.next_free_hex(triangulation,level+1);
6122  else
6123  ++next_unused_hex;
6124 
6125  new_hexes[i]=next_unused_hex;
6126 
6127  Assert (new_hexes[i]->used() == false,
6128  ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
6129  new_hexes[i]->set_used_flag();
6130  new_hexes[i]->clear_user_flag();
6131  new_hexes[i]->clear_user_data();
6132  new_hexes[i]->clear_children();
6133  // inherit material
6134  // properties
6135  new_hexes[i]->set_material_id (hex->material_id());
6136  new_hexes[i]->set_manifold_id (hex->manifold_id());
6137  new_hexes[i]->set_subdomain_id (subdomainid);
6138 
6139  if (i%2)
6140  new_hexes[i]->set_parent (hex->index ());
6141  // set the face_orientation flag to true for all
6142  // faces initially, as this is the default value
6143  // which is true for all faces interior to the
6144  // hex. later on go the other way round and
6145  // reset faces that are at the boundary of the
6146  // mother cube
6147  //
6148  // the same is true for the face_flip and
6149  // face_rotation flags. however, the latter two
6150  // are set to false by default as this is the
6151  // standard value
6152  for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
6153  {
6154  new_hexes[i]->set_face_orientation(f, true);
6155  new_hexes[i]->set_face_flip(f, false);
6156  new_hexes[i]->set_face_rotation(f, false);
6157  }
6158  }
6159  // note these hexes as children to the present cell
6160  for (unsigned int i=0; i<n_new_hexes/2; ++i)
6161  hex->set_children (2*i, new_hexes[2*i]->index());
6162 
6163  // we have to take into account whether the
6164  // different faces are oriented correctly or in the
6165  // opposite direction, so store that up front
6166 
6167  // face_orientation
6168  const bool f_or[6]
6169  = { hex->face_orientation (0),
6170  hex->face_orientation (1),
6171  hex->face_orientation (2),
6172  hex->face_orientation (3),
6173  hex->face_orientation (4),
6174  hex->face_orientation (5)
6175  };
6176 
6177  // face_flip
6178  const bool f_fl[6]
6179  = { hex->face_flip (0),
6180  hex->face_flip (1),
6181  hex->face_flip (2),
6182  hex->face_flip (3),
6183  hex->face_flip (4),
6184  hex->face_flip (5)
6185  };
6186 
6187  // face_rotation
6188  const bool f_ro[6]
6189  = { hex->face_rotation (0),
6190  hex->face_rotation (1),
6191  hex->face_rotation (2),
6192  hex->face_rotation (3),
6193  hex->face_rotation (4),
6194  hex->face_rotation (5)
6195  };
6196 
6197  // little helper table, indicating, whether the
6198  // child with index 0 or with index 1 can be found
6199  // at the standard origin of an anisotropically
6200  // refined quads in real orientation index 1:
6201  // (RefineCase - 1) index 2: face_flip
6202 
6203  // index 3: face rotation
6204  // note: face orientation has no influence
6205  const unsigned int child_at_origin[2][2][2]=
6206  {
6207  { { 0, 0 }, // RefinementCase<dim>::cut_x, face_flip=false, face_rotation=false and true
6208  { 1, 1 }
6209  }, // RefinementCase<dim>::cut_x, face_flip=true, face_rotation=false and true
6210  { { 0, 1 }, // RefinementCase<dim>::cut_y, face_flip=false, face_rotation=false and true
6211  { 1, 0 }
6212  }
6213  };// RefinementCase<dim>::cut_y, face_flip=true, face_rotation=false and true
6214 
6216  //
6217  // in the following we will do the same thing for
6218  // each refinement case: create a new vertex (if
6219  // needed), create new interior lines (if needed),
6220  // create new interior quads and afterwards build
6221  // the children hexes out of these and the existing
6222  // subfaces of the outer quads (which have been
6223  // created above). However, even if the steps are
6224  // quite similar, the actual work strongly depends
6225  // on the actual refinement case. therefore, we use
6226  // separate blocks of code for each of these cases,
6227  // which hopefully increases the readability to some
6228  // extend.
6229 
6230  switch (ref_case)
6231  {
6233  {
6235  //
6236  // RefinementCase<dim>::cut_x
6237  //
6238  // the refined cube will look
6239  // like this:
6240  //
6241  // *----*----*
6242  // / / /|
6243  // / / / |
6244  // / / / |
6245  // *----*----* |
6246  // | | | |
6247  // | | | *
6248  // | | | /
6249  // | | | /
6250  // | | |/
6251  // *----*----*
6252  //
6253  // again, first collect some data about the
6254  // indices of the lines, with the following
6255  // numbering:
6256 
6257  // face 2: front plane
6258  // (note: x,y exchanged)
6259  // *---*---*
6260  // | | |
6261  // | 0 |
6262  // | | |
6263  // *---*---*
6264  // m0
6265  // face 3: back plane
6266  // (note: x,y exchanged)
6267  // m1
6268  // *---*---*
6269  // | | |
6270  // | 1 |
6271  // | | |
6272  // *---*---*
6273  // face 4: bottom plane
6274  // *---*---*
6275  // / / /
6276  // / 2 /
6277  // / / /
6278  // *---*---*
6279  // m0
6280  // face 5: top plane
6281  // m1
6282  // *---*---*
6283  // / / /
6284  // / 3 /
6285  // / / /
6286  // *---*---*
6287 
6288  // set up a list of line iterators first. from
6289  // this, construct lists of line_indices and
6290  // line orientations later on
6291  const typename Triangulation<dim,spacedim>::raw_line_iterator
6292  lines[4]
6293  =
6294  {
6295  hex->face(2)->child(0)
6296  ->line((hex->face(2)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //0
6297  hex->face(3)->child(0)
6298  ->line((hex->face(3)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //1
6299  hex->face(4)->child(0)
6300  ->line((hex->face(4)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //2
6301  hex->face(5)->child(0)
6302  ->line((hex->face(5)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3) //3
6303  };
6304 
6305  unsigned int line_indices[4];
6306  for (unsigned int i=0; i<4; ++i)
6307  line_indices[i] = lines[i]->index();
6308 
6309  // the orientation of lines for the inner quads
6310  // is quite tricky. as these lines are newly
6311  // created ones and thus have no parents, they
6312  // cannot inherit this property. set up an array
6313  // and fill it with the respective values
6314  bool line_orientation[4];
6315 
6316  // the middle vertice marked as m0 above is the
6317  // start vertex for lines 0 and 2 in standard
6318  // orientation, whereas m1 is the end vertex of
6319  // lines 1 and 3 in standard orientation
6320  const unsigned int middle_vertices[2]=
6321  {
6322  hex->line(2)->child(0)->vertex_index(1),
6323  hex->line(7)->child(0)->vertex_index(1)
6324  };
6325 
6326  for (unsigned int i=0; i<4; ++i)
6327  if (lines[i]->vertex_index(i%2)==middle_vertices[i%2])
6328  line_orientation[i]=true;
6329  else
6330  {
6331  // it must be the other
6332  // way round then
6333  Assert(lines[i]->vertex_index((i+1)%2)==middle_vertices[i%2],
6334  ExcInternalError());
6335  line_orientation[i]=false;
6336  }
6337 
6338  // set up the new quad, line numbering is as
6339  // indicated above
6340  new_quads[0]->set (internal::TriangulationImplementation
6341  ::TriaObject<2>(line_indices[0],
6342  line_indices[1],
6343  line_indices[2],
6344  line_indices[3]));
6345 
6346  new_quads[0]->set_line_orientation(0,line_orientation[0]);
6347  new_quads[0]->set_line_orientation(1,line_orientation[1]);
6348  new_quads[0]->set_line_orientation(2,line_orientation[2]);
6349  new_quads[0]->set_line_orientation(3,line_orientation[3]);
6350 
6351  // the quads are numbered as follows:
6352  //
6353  // planes in the interior of the old hex:
6354  //
6355  // *
6356  // /|
6357  // / | x
6358  // / | *-------* *---------*
6359  // * | | | / /
6360  // | 0 | | | / /
6361  // | * | | / /
6362  // | / *-------*y *---------*x
6363  // | /
6364  // |/
6365  // *
6366  //
6367  // children of the faces of the old hex
6368  //
6369  // *---*---* *---*---*
6370  // /| | | / / /|
6371  // / | | | / 9 / 10/ |
6372  // / | 5 | 6 | / / / |
6373  // * | | | *---*---* |
6374  // | 1 *---*---* | | | 2 *
6375  // | / / / | | | /
6376  // | / 7 / 8 / | 3 | 4 | /
6377  // |/ / / | | |/
6378  // *---*---* *---*---*
6379  //
6380  // note that we have to take care of the
6381  // orientation of faces.
6382  const int quad_indices[11]
6383  =
6384  {
6385  new_quads[0]->index(), //0
6386 
6387  hex->face(0)->index(), //1
6388 
6389  hex->face(1)->index(), //2
6390 
6391  hex->face(2)->child_index( child_at_origin[hex->face(2)->refinement_case()-1][f_fl[2]][f_ro[2]]), //3
6392  hex->face(2)->child_index(1-child_at_origin[hex->face(2)->refinement_case()-1][f_fl[2]][f_ro[2]]),
6393 
6394  hex->face(3)->child_index( child_at_origin[hex->face(3)->refinement_case()-1][f_fl[3]][f_ro[3]]), //5
6395  hex->face(3)->child_index(1-child_at_origin[hex->face(3)->refinement_case()-1][f_fl[3]][f_ro[3]]),
6396 
6397  hex->face(4)->child_index( child_at_origin[hex->face(4)->refinement_case()-1][f_fl[4]][f_ro[4]]), //7
6398  hex->face(4)->child_index(1-child_at_origin[hex->face(4)->refinement_case()-1][f_fl[4]][f_ro[4]]),
6399 
6400  hex->face(5)->child_index( child_at_origin[hex->face(5)->refinement_case()-1][f_fl[5]][f_ro[5]]), //9
6401  hex->face(5)->child_index(1-child_at_origin[hex->face(5)->refinement_case()-1][f_fl[5]][f_ro[5]])
6402 
6403  };
6404 
6405  new_hexes[0]->set (internal::TriangulationImplementation
6406  ::TriaObject<3>(quad_indices[1],
6407  quad_indices[0],
6408  quad_indices[3],
6409  quad_indices[5],
6410  quad_indices[7],
6411  quad_indices[9]));
6412  new_hexes[1]->set (internal::TriangulationImplementation
6413  ::TriaObject<3>(quad_indices[0],
6414  quad_indices[2],
6415  quad_indices[4],
6416  quad_indices[6],
6417  quad_indices[8],
6418  quad_indices[10]));
6419  break;
6420  }
6421 
6423  {
6425  //
6426  // RefinementCase<dim>::cut_y
6427  //
6428  // the refined cube will look like this:
6429  //
6430  // *---------*
6431  // / /|
6432  // *---------* |
6433  // / /| |
6434  // *---------* | |
6435  // | | | |
6436  // | | | *
6437  // | | |/
6438  // | | *
6439  // | |/
6440  // *---------*
6441  //
6442  // again, first collect some data about the
6443  // indices of the lines, with the following
6444  // numbering:
6445 
6446  // face 0: left plane
6447  // *
6448  // /|
6449  // * |
6450  // /| |
6451  // * | |
6452  // | 0 |
6453  // | | *
6454  // | |/
6455  // | *m0
6456  // |/
6457  // *
6458  // face 1: right plane
6459  // *
6460  // /|
6461  // m1* |
6462  // /| |
6463  // * | |
6464  // | 1 |
6465  // | | *
6466  // | |/
6467  // | *
6468  // |/
6469  // *
6470  // face 4: bottom plane
6471  // *-------*
6472  // / /
6473  // m0*---2---*
6474  // / /
6475  // *-------*
6476  // face 5: top plane
6477  // *-------*
6478  // / /
6479  // *---3---*m1
6480  // / /
6481  // *-------*
6482 
6483  // set up a list of line iterators first. from
6484  // this, construct lists of line_indices and
6485  // line orientations later on
6486  const typename Triangulation<dim,spacedim>::raw_line_iterator
6487  lines[4]
6488  =
6489  {
6490  hex->face(0)->child(0)
6491  ->line((hex->face(0)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //0
6492  hex->face(1)->child(0)
6493  ->line((hex->face(1)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //1
6494  hex->face(4)->child(0)
6495  ->line((hex->face(4)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //2
6496  hex->face(5)->child(0)
6497  ->line((hex->face(5)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3) //3
6498  };
6499 
6500  unsigned int line_indices[4];
6501  for (unsigned int i=0; i<4; ++i)
6502  line_indices[i]=lines[i]->index();
6503 
6504  // the orientation of lines for the inner quads
6505  // is quite tricky. as these lines are newly
6506  // created ones and thus have no parents, they
6507  // cannot inherit this property. set up an array
6508  // and fill it with the respective values
6509  bool line_orientation[4];
6510 
6511  // the middle vertice marked as m0 above is the
6512  // start vertex for lines 0 and 2 in standard
6513  // orientation, whereas m1 is the end vertex of
6514  // lines 1 and 3 in standard orientation
6515  const unsigned int middle_vertices[2]=
6516  {
6517  hex->line(0)->child(0)->vertex_index(1),
6518  hex->line(5)->child(0)->vertex_index(1)
6519  };
6520 
6521  for (unsigned int i=0; i<4; ++i)
6522  if (lines[i]->vertex_index(i%2)==middle_vertices[i%2])
6523  line_orientation[i]=true;
6524  else
6525  {
6526  // it must be the other way round then
6527  Assert(lines[i]->vertex_index((i+1)%2)==middle_vertices[i%2],
6528  ExcInternalError());
6529  line_orientation[i]=false;
6530  }
6531 
6532  // set up the new quad, line numbering is as
6533  // indicated above
6534  new_quads[0]->set (internal::TriangulationImplementation
6535  ::TriaObject<2>(line_indices[2],
6536  line_indices[3],
6537  line_indices[0],
6538  line_indices[1]));
6539 
6540  new_quads[0]->set_line_orientation(0,line_orientation[2]);
6541  new_quads[0]->set_line_orientation(1,line_orientation[3]);
6542  new_quads[0]->set_line_orientation(2,line_orientation[0]);
6543  new_quads[0]->set_line_orientation(3,line_orientation[1]);
6544 
6545  // the quads are numbered as follows:
6546  //
6547  // planes in the interior of the old hex:
6548  //
6549  // *
6550  // /|
6551  // / | x
6552  // / | *-------* *---------*
6553  // * | | | / /
6554  // | | | 0 | / /
6555  // | * | | / /
6556  // | / *-------*y *---------*x
6557  // | /
6558  // |/
6559  // *
6560  //
6561  // children of the faces of the old hex
6562  //
6563  // *-------* *-------*
6564  // /| | / 10 /|
6565  // * | | *-------* |
6566  // /| | 6 | / 9 /| |
6567  // * |2| | *-------* |4|
6568  // | | *-------* | | | *
6569  // |1|/ 8 / | |3|/
6570  // | *-------* | 5 | *
6571  // |/ 7 / | |/
6572  // *-------* *-------*
6573  //
6574  // note that we have to take care of the
6575  // orientation of faces.
6576  const int quad_indices[11]
6577  =
6578  {
6579  new_quads[0]->index(), //0
6580 
6581  hex->face(0)->child_index( child_at_origin[hex->face(0)->refinement_case()-1][f_fl[0]][f_ro[0]]), //1
6582  hex->face(0)->child_index(1-child_at_origin[hex->face(0)->refinement_case()-1][f_fl[0]][f_ro[0]]),
6583 
6584  hex->face(1)->child_index( child_at_origin[hex->face(1)->refinement_case()-1][f_fl[1]][f_ro[1]]), //3
6585  hex->face(1)->child_index(1-child_at_origin[hex->face(1)->refinement_case()-1][f_fl[1]][f_ro[1]]),
6586 
6587  hex->face(2)->index(), //5
6588 
6589  hex->face(3)->index(), //6
6590 
6591  hex->face(4)->child_index( child_at_origin[hex->face(4)->refinement_case()-1][f_fl[4]][f_ro[4]]), //7
6592  hex->face(4)->child_index(1-child_at_origin[hex->face(4)->refinement_case()-1][f_fl[4]][f_ro[4]]),
6593 
6594  hex->face(5)->child_index( child_at_origin[hex->face(5)->refinement_case()-1][f_fl[5]][f_ro[5]]), //9
6595  hex->face(5)->child_index(1-child_at_origin[hex->face(5)->refinement_case()-1][f_fl[5]][f_ro[5]])
6596 
6597  };
6598 
6599  new_hexes[0]->set (internal::TriangulationImplementation
6600  ::TriaObject<3>(quad_indices[1],
6601  quad_indices[3],
6602  quad_indices[5],
6603  quad_indices[0],
6604  quad_indices[7],
6605  quad_indices[9]));
6606  new_hexes[1]->set (internal::TriangulationImplementation
6607  ::TriaObject<3>(quad_indices[2],
6608  quad_indices[4],
6609  quad_indices[0],
6610  quad_indices[6],
6611  quad_indices[8],
6612  quad_indices[10]));
6613  break;
6614  }
6615 
6617  {
6619  //
6620  // RefinementCase<dim>::cut_z
6621  //
6622  // the refined cube will look like this:
6623  //
6624  // *---------*
6625  // / /|
6626  // / / |
6627  // / / *
6628  // *---------* /|
6629  // | | / |
6630  // | |/ *
6631  // *---------* /
6632  // | | /
6633  // | |/
6634  // *---------*
6635  //
6636  // again, first collect some data about the
6637  // indices of the lines, with the following
6638  // numbering:
6639 
6640  // face 0: left plane
6641  // *
6642  // /|
6643  // / |
6644  // / *
6645  // * /|
6646  // | 0 |
6647  // |/ *
6648  // m0* /
6649  // | /
6650  // |/
6651  // *
6652  // face 1: right plane
6653  // *
6654  // /|
6655  // / |
6656  // / *m1
6657  // * /|
6658  // | 1 |
6659  // |/ *
6660  // * /
6661  // | /
6662  // |/
6663  // *
6664  // face 2: front plane
6665  // (note: x,y exchanged)
6666  // *-------*
6667  // | |
6668  // m0*---2---*
6669  // | |
6670  // *-------*
6671  // face 3: back plane
6672  // (note: x,y exchanged)
6673  // *-------*
6674  // | |
6675  // *---3---*m1
6676  // | |
6677  // *-------*
6678 
6679  // set up a list of line iterators first. from
6680  // this, construct lists of line_indices and
6681  // line orientations later on
6682  const typename Triangulation<dim,spacedim>::raw_line_iterator
6683  lines[4]
6684  =
6685  {
6686  hex->face(0)->child(0)
6687  ->line((hex->face(0)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //0
6688  hex->face(1)->child(0)
6689  ->line((hex->face(1)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //1
6690  hex->face(2)->child(0)
6691  ->line((hex->face(2)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //2
6692  hex->face(3)->child(0)
6693  ->line((hex->face(3)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3) //3
6694  };
6695 
6696  unsigned int line_indices[4];
6697  for (unsigned int i=0; i<4; ++i)
6698  line_indices[i]=lines[i]->index();
6699 
6700  // the orientation of lines for the inner quads
6701  // is quite tricky. as these lines are newly
6702  // created ones and thus have no parents, they
6703  // cannot inherit this property. set up an array
6704  // and fill it with the respective values
6705  bool line_orientation[4];
6706 
6707  // the middle vertex marked as m0 above is the
6708  // start vertex for lines 0 and 2 in standard
6709  // orientation, whereas m1 is the end vertex of
6710  // lines 1 and 3 in standard orientation
6711  const unsigned int middle_vertices[2]=
6712  {
6713  middle_vertex_index<dim,spacedim>(hex->line(8)),
6714  middle_vertex_index<dim,spacedim>(hex->line(11))
6715  };
6716 
6717  for (unsigned int i=0; i<4; ++i)
6718  if (lines[i]->vertex_index(i%2)==middle_vertices[i%2])
6719  line_orientation[i]=true;
6720  else
6721  {
6722  // it must be the other way round then
6723  Assert(lines[i]->vertex_index((i+1)%2)==middle_vertices[i%2],
6724  ExcInternalError());
6725  line_orientation[i]=false;
6726  }
6727 
6728  // set up the new quad, line numbering is as
6729  // indicated above
6730  new_quads[0]->set (internal::TriangulationImplementation
6731  ::TriaObject<2>(line_indices[0],
6732  line_indices[1],
6733  line_indices[2],
6734  line_indices[3]));
6735 
6736  new_quads[0]->set_line_orientation(0,line_orientation[0]);
6737  new_quads[0]->set_line_orientation(1,line_orientation[1]);
6738  new_quads[0]->set_line_orientation(2,line_orientation[2]);
6739  new_quads[0]->set_line_orientation(3,line_orientation[3]);
6740 
6741  // the quads are numbered as follows:
6742  //
6743  // planes in the interior of the old hex:
6744  //
6745  // *
6746  // /|
6747  // / | x
6748  // / | *-------* *---------*
6749  // * | | | / /
6750  // | | | | / 0 /
6751  // | * | | / /
6752  // | / *-------*y *---------*x
6753  // | /
6754  // |/
6755  // *
6756  //
6757  // children of the faces of the old hex
6758  //
6759  // *---*---* *-------*
6760  // /| 8 | / /|
6761  // / | | / 10 / |
6762  // / *-------* / / *
6763  // * 2/| | *-------* 4/|
6764  // | / | 7 | | 6 | / |
6765  // |/1 *-------* | |/3 *
6766  // * / / *-------* /
6767  // | / 9 / | | /
6768  // |/ / | 5 |/
6769  // *-------* *---*---*
6770  //
6771  // note that we have to take care of the
6772  // orientation of faces.
6773  const int quad_indices[11]
6774  =
6775  {
6776  new_quads[0]->index(), //0
6777 
6778  hex->face(0)->child_index( child_at_origin[hex->face(0)->refinement_case()-1][f_fl[0]][f_ro[0]]), //1
6779  hex->face(0)->child_index(1-child_at_origin[hex->face(0)->refinement_case()-1][f_fl[0]][f_ro[0]]),
6780 
6781  hex->face(1)->child_index( child_at_origin[hex->face(1)->refinement_case()-1][f_fl[1]][f_ro[1]]), //3
6782  hex->face(1)->child_index(1-child_at_origin[hex->face(1)->refinement_case()-1][f_fl[1]][f_ro[1]]),
6783 
6784  hex->face(2)->child_index( child_at_origin[hex->face(2)->refinement_case()-1][f_fl[2]][f_ro[2]]), //5
6785  hex->face(2)->child_index(1-child_at_origin[hex->face(2)->refinement_case()-1][f_fl[2]][f_ro[2]]),
6786 
6787  hex->face(3)->child_index( child_at_origin[hex->face(3)->refinement_case()-1][f_fl[3]][f_ro[3]]), //7
6788  hex->face(3)->child_index(1-child_at_origin[hex->face(3)->refinement_case()-1][f_fl[3]][f_ro[3]]),
6789 
6790  hex->face(4)->index(), //9
6791 
6792  hex->face(5)->index() //10
6793  };
6794 
6795  new_hexes[0]->set (internal::TriangulationImplementation
6796  ::TriaObject<3>(quad_indices[1],
6797  quad_indices[3],
6798  quad_indices[5],
6799  quad_indices[7],
6800  quad_indices[9],
6801  quad_indices[0]));
6802  new_hexes[1]->set (internal::TriangulationImplementation
6803  ::TriaObject<3>(quad_indices[2],
6804  quad_indices[4],
6805  quad_indices[6],
6806  quad_indices[8],
6807  quad_indices[0],
6808  quad_indices[10]));
6809  break;
6810  }
6811 
6813  {
6815  //
6816  // RefinementCase<dim>::cut_xy
6817  //
6818  // the refined cube will look like this:
6819  //
6820  // *----*----*
6821  // / / /|
6822  // *----*----* |
6823  // / / /| |
6824  // *----*----* | |
6825  // | | | | |
6826  // | | | | *
6827  // | | | |/
6828  // | | | *
6829  // | | |/
6830  // *----*----*
6831  //
6832 
6833  // first, create the new internal line
6834  new_lines[0]->set (internal::TriangulationImplementation::
6835  TriaObject<1>(middle_vertex_index<dim,spacedim>(hex->face(4)),
6836  middle_vertex_index<dim,spacedim>(hex->face(5))));
6837 
6838  // again, first collect some data about the
6839  // indices of the lines, with the following
6840  // numbering:
6841 
6842  // face 0: left plane
6843  // *
6844  // /|
6845  // * |
6846  // /| |
6847  // * | |
6848  // | 0 |
6849  // | | *
6850  // | |/
6851  // | *
6852  // |/
6853  // *
6854  // face 1: right plane
6855  // *
6856  // /|
6857  // * |
6858  // /| |
6859  // * | |
6860  // | 1 |
6861  // | | *
6862  // | |/
6863  // | *
6864  // |/
6865  // *
6866  // face 2: front plane
6867  // (note: x,y exchanged)
6868  // *---*---*
6869  // | | |
6870  // | 2 |
6871  // | | |
6872  // *-------*
6873  // face 3: back plane
6874  // (note: x,y exchanged)
6875  // *---*---*
6876  // | | |
6877  // | 3 |
6878  // | | |
6879  // *---*---*
6880  // face 4: bottom plane
6881  // *---*---*
6882  // / 5 /
6883  // *-6-*-7-*
6884  // / 4 /
6885  // *---*---*
6886  // face 5: top plane
6887  // *---*---*
6888  // / 9 /
6889  // *10-*-11*
6890  // / 8 /
6891  // *---*---*
6892  // middle planes
6893  // *-------* *---*---*
6894  // / / | | |
6895  // / / | 12 |
6896  // / / | | |
6897  // *-------* *---*---*
6898 
6899  // set up a list of line iterators first. from
6900  // this, construct lists of line_indices and
6901  // line orientations later on
6902  const typename Triangulation<dim,spacedim>::raw_line_iterator
6903  lines[13]
6904  =
6905  {
6906  hex->face(0)->child(0)
6907  ->line((hex->face(0)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //0
6908  hex->face(1)->child(0)
6909  ->line((hex->face(1)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //1
6910  hex->face(2)->child(0)
6911  ->line((hex->face(2)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //2
6912  hex->face(3)->child(0)
6913  ->line((hex->face(3)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //3
6914 
6915  hex->face(4)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[4],f_fl[4],f_ro[4]))
6916  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[4],f_fl[4],f_ro[4])), //4
6917  hex->face(4)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[4],f_fl[4],f_ro[4]))
6918  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[4],f_fl[4],f_ro[4])), //5
6919  hex->face(4)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[4],f_fl[4],f_ro[4]))
6920  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[4],f_fl[4],f_ro[4])), //6
6921  hex->face(4)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[4],f_fl[4],f_ro[4]))
6922  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[4],f_fl[4],f_ro[4])), //7
6923 
6924  hex->face(5)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[5],f_fl[5],f_ro[5]))
6925  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[5],f_fl[5],f_ro[5])), //8
6926  hex->face(5)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[5],f_fl[5],f_ro[5]))
6927  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[5],f_fl[5],f_ro[5])), //9
6928  hex->face(5)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[5],f_fl[5],f_ro[5]))
6929  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[5],f_fl[5],f_ro[5])), //10
6930  hex->face(5)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[5],f_fl[5],f_ro[5]))
6931  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[5],f_fl[5],f_ro[5])), //11
6932 
6933  new_lines[0] //12
6934  };
6935 
6936  unsigned int line_indices[13];
6937  for (unsigned int i=0; i<13; ++i)
6938  line_indices[i]=lines[i]->index();
6939 
6940  // the orientation of lines for the inner quads
6941  // is quite tricky. as these lines are newly
6942  // created ones and thus have no parents, they
6943  // cannot inherit this property. set up an array
6944  // and fill it with the respective values
6945  bool line_orientation[13];
6946 
6947  // the middle vertices of the lines of our
6948  // bottom face
6949  const unsigned int middle_vertices[4]=
6950  {
6951  hex->line(0)->child(0)->vertex_index(1),
6952  hex->line(1)->child(0)->vertex_index(1),
6953  hex->line(2)->child(0)->vertex_index(1),
6954  hex->line(3)->child(0)->vertex_index(1),
6955  };
6956 
6957  // note: for lines 0 to 3 the orientation of the
6958  // line is 'true', if vertex 0 is on the bottom
6959  // face
6960  for (unsigned int i=0; i<4; ++i)
6961  if (lines[i]->vertex_index(0)==middle_vertices[i])
6962  line_orientation[i]=true;
6963  else
6964  {
6965  // it must be the other way round then
6966  Assert(lines[i]->vertex_index(1)==middle_vertices[i],
6967  ExcInternalError());
6968  line_orientation[i]=false;
6969  }
6970 
6971  // note: for lines 4 to 11 (inner lines of the
6972  // outer quads) the following holds: the second
6973  // vertex of the even lines in standard
6974  // orientation is the vertex in the middle of
6975  // the quad, whereas for odd lines the first
6976  // vertex is the same middle vertex.
6977  for (unsigned int i=4; i<12; ++i)
6978  if (lines[i]->vertex_index((i+1)%2) ==
6979  middle_vertex_index<dim,spacedim>(hex->face(3+i/4)))
6980  line_orientation[i]=true;
6981  else
6982  {
6983  // it must be the other way
6984  // round then
6985  Assert(lines[i]->vertex_index(i%2) ==
6986  (middle_vertex_index<dim,spacedim>(hex->face(3+i/4))),
6987  ExcInternalError());
6988  line_orientation[i]=false;
6989  }
6990  // for the last line the line orientation is
6991  // always true, since it was just constructed
6992  // that way
6993  line_orientation[12]=true;
6994 
6995  // set up the 4 quads, numbered as follows (left
6996  // quad numbering, right line numbering
6997  // extracted from above)
6998  //
6999  // * *
7000  // /| 9|
7001  // * | * |
7002  // y/| | 8| 3
7003  // * |1| * | |
7004  // | | |x | 12|
7005  // |0| * | | *
7006  // | |/ 2 |5
7007  // | * | *
7008  // |/ |4
7009  // * *
7010  //
7011  // x
7012  // *---*---* *10-*-11*
7013  // | | | | | |
7014  // | 2 | 3 | 0 12 1
7015  // | | | | | |
7016  // *---*---*y *-6-*-7-*
7017 
7018  new_quads[0]->set (internal::TriangulationImplementation
7019  ::TriaObject<2>(line_indices[2],
7020  line_indices[12],
7021  line_indices[4],
7022  line_indices[8]));
7023  new_quads[1]->set (internal::TriangulationImplementation
7024  ::TriaObject<2>(line_indices[12],
7025  line_indices[3],
7026  line_indices[5],
7027  line_indices[9]));
7028  new_quads[2]->set (internal::TriangulationImplementation
7029  ::TriaObject<2>(line_indices[6],
7030  line_indices[10],
7031  line_indices[0],
7032  line_indices[12]));
7033  new_quads[3]->set (internal::TriangulationImplementation
7034  ::TriaObject<2>(line_indices[7],
7035  line_indices[11],
7036  line_indices[12],
7037  line_indices[1]));
7038 
7039  new_quads[0]->set_line_orientation(0,line_orientation[2]);
7040  new_quads[0]->set_line_orientation(2,line_orientation[4]);
7041  new_quads[0]->set_line_orientation(3,line_orientation[8]);
7042 
7043  new_quads[1]->set_line_orientation(1,line_orientation[3]);
7044  new_quads[1]->set_line_orientation(2,line_orientation[5]);
7045  new_quads[1]->set_line_orientation(3,line_orientation[9]);
7046 
7047  new_quads[2]->set_line_orientation(0,line_orientation[6]);
7048  new_quads[2]->set_line_orientation(1,line_orientation[10]);
7049  new_quads[2]->set_line_orientation(2,line_orientation[0]);
7050 
7051  new_quads[3]->set_line_orientation(0,line_orientation[7]);
7052  new_quads[3]->set_line_orientation(1,line_orientation[11]);
7053  new_quads[3]->set_line_orientation(3,line_orientation[1]);
7054 
7055  // the quads are numbered as follows:
7056  //
7057  // planes in the interior of the old hex:
7058  //
7059  // *
7060  // /|
7061  // * | x
7062  // /| | *---*---* *---------*
7063  // * |1| | | | / /
7064  // | | | | 2 | 3 | / /
7065  // |0| * | | | / /
7066  // | |/ *---*---*y *---------*x
7067  // | *
7068  // |/
7069  // *
7070  //
7071  // children of the faces of the old hex
7072  //
7073  // *---*---* *---*---*
7074  // /| | | /18 / 19/|
7075  // * |10 | 11| /---/---* |
7076  // /| | | | /16 / 17/| |
7077  // * |5| | | *---*---* |7|
7078  // | | *---*---* | | | | *
7079  // |4|/14 / 15/ | | |6|/
7080  // | *---/---/ | 8 | 9 | *
7081  // |/12 / 13/ | | |/
7082  // *---*---* *---*---*
7083  //
7084  // note that we have to take care of the
7085  // orientation of faces.
7086  const int quad_indices[20]
7087  =
7088  {
7089  new_quads[0]->index(), //0
7090  new_quads[1]->index(),
7091  new_quads[2]->index(),
7092  new_quads[3]->index(),
7093 
7094  hex->face(0)->child_index( child_at_origin[hex->face(0)->refinement_case()-1][f_fl[0]][f_ro[0]]), //4
7095  hex->face(0)->child_index(1-child_at_origin[hex->face(0)->refinement_case()-1][f_fl[0]][f_ro[0]]),
7096 
7097  hex->face(1)->child_index( child_at_origin[hex->face(1)->refinement_case()-1][f_fl[1]][f_ro[1]]), //6
7098  hex->face(1)->child_index(1-child_at_origin[hex->face(1)->refinement_case()-1][f_fl[1]][f_ro[1]]),
7099 
7100  hex->face(2)->child_index( child_at_origin[hex->face(2)->refinement_case()-1][f_fl[2]][f_ro[2]]), //8
7101  hex->face(2)->child_index(1-child_at_origin[hex->face(2)->refinement_case()-1][f_fl[2]][f_ro[2]]),
7102 
7103  hex->face(3)->child_index( child_at_origin[hex->face(3)->refinement_case()-1][f_fl[3]][f_ro[3]]), //10
7104  hex->face(3)->child_index(1-child_at_origin[hex->face(3)->refinement_case()-1][f_fl[3]][f_ro[3]]),
7105 
7106  hex->face(4)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[4],f_fl[4],f_ro[4])), //12
7107  hex->face(4)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[4],f_fl[4],f_ro[4])),
7108  hex->face(4)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[4],f_fl[4],f_ro[4])),
7109  hex->face(4)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[4],f_fl[4],f_ro[4])),
7110 
7111  hex->face(5)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[5],f_fl[5],f_ro[5])), //16
7112  hex->face(5)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[5],f_fl[5],f_ro[5])),
7113  hex->face(5)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[5],f_fl[5],f_ro[5])),
7114  hex->face(5)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[5],f_fl[5],f_ro[5]))
7115  };
7116 
7117  new_hexes[0]->set (internal::TriangulationImplementation
7118  ::TriaObject<3>(quad_indices[4],
7119  quad_indices[0],
7120  quad_indices[8],
7121  quad_indices[2],
7122  quad_indices[12],
7123  quad_indices[16]));
7124  new_hexes[1]->set (internal::TriangulationImplementation
7125  ::TriaObject<3>(quad_indices[0],
7126  quad_indices[6],
7127  quad_indices[9],
7128  quad_indices[3],
7129  quad_indices[13],
7130  quad_indices[17]));
7131  new_hexes[2]->set (internal::TriangulationImplementation
7132  ::TriaObject<3>(quad_indices[5],
7133  quad_indices[1],
7134  quad_indices[2],
7135  quad_indices[10],
7136  quad_indices[14],
7137  quad_indices[18]));
7138  new_hexes[3]->set (internal::TriangulationImplementation
7139  ::TriaObject<3>(quad_indices[1],
7140  quad_indices[7],
7141  quad_indices[3],
7142  quad_indices[11],
7143  quad_indices[15],
7144  quad_indices[19]));
7145  break;
7146  }
7147 
7149  {
7151  //
7152  // RefinementCase<dim>::cut_xz
7153  //
7154  // the refined cube will look like this:
7155  //
7156  // *----*----*
7157  // / / /|
7158  // / / / |
7159  // / / / *
7160  // *----*----* /|
7161  // | | | / |
7162  // | | |/ *
7163  // *----*----* /
7164  // | | | /
7165  // | | |/
7166  // *----*----*
7167  //
7168 
7169  // first, create the new internal line
7170  new_lines[0]->set (internal::TriangulationImplementation::
7171  TriaObject<1>(middle_vertex_index<dim,spacedim>(hex->face(2)),
7172  middle_vertex_index<dim,spacedim>(hex->face(3))));
7173 
7174  // again, first collect some data about the
7175  // indices of the lines, with the following
7176  // numbering:
7177 
7178  // face 0: left plane
7179  // *
7180  // /|
7181  // / |
7182  // / *
7183  // * /|
7184  // | 0 |
7185  // |/ *
7186  // * /
7187  // | /
7188  // |/
7189  // *
7190  // face 1: right plane
7191  // *
7192  // /|
7193  // / |
7194  // / *
7195  // * /|
7196  // | 1 |
7197  // |/ *
7198  // * /
7199  // | /
7200  // |/
7201  // *
7202  // face 2: front plane
7203  // (note: x,y exchanged)
7204  // *---*---*
7205  // | 5 |
7206  // *-6-*-7-*
7207  // | 4 |
7208  // *---*---*
7209  // face 3: back plane
7210  // (note: x,y exchanged)
7211  // *---*---*
7212  // | 9 |
7213  // *10-*-11*
7214  // | 8 |
7215  // *---*---*
7216  // face 4: bottom plane
7217  // *---*---*
7218  // / / /
7219  // / 2 /
7220  // / / /
7221  // *---*---*
7222  // face 5: top plane
7223  // *---*---*
7224  // / / /
7225  // / 3 /
7226  // / / /
7227  // *---*---*
7228  // middle planes
7229  // *---*---* *-------*
7230  // / / / | |
7231  // / 12 / | |
7232  // / / / | |
7233  // *---*---* *-------*
7234 
7235  // set up a list of line iterators first. from
7236  // this, construct lists of line_indices and
7237  // line orientations later on
7238  const typename Triangulation<dim,spacedim>::raw_line_iterator
7239  lines[13]
7240  =
7241  {
7242  hex->face(0)->child(0)
7243  ->line((hex->face(0)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //0
7244  hex->face(1)->child(0)
7245  ->line((hex->face(1)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //1
7246  hex->face(4)->child(0)
7247  ->line((hex->face(4)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //2
7248  hex->face(5)->child(0)
7249  ->line((hex->face(5)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //3
7250 
7251  hex->face(2)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[2],f_fl[2],f_ro[2]))
7252  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[2],f_fl[2],f_ro[2])), //4
7253  hex->face(2)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[2],f_fl[2],f_ro[2]))
7254  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[2],f_fl[2],f_ro[2])), //5
7255  hex->face(2)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[2],f_fl[2],f_ro[2]))
7256  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[2],f_fl[2],f_ro[2])), //6
7257  hex->face(2)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[2],f_fl[2],f_ro[2]))
7258  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[2],f_fl[2],f_ro[2])), //7
7259 
7260  hex->face(3)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[3],f_fl[3],f_ro[3]))
7261  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[3],f_fl[3],f_ro[3])), //8
7262  hex->face(3)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[3],f_fl[3],f_ro[3]))
7263  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[3],f_fl[3],f_ro[3])), //9
7264  hex->face(3)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[3],f_fl[3],f_ro[3]))
7265  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[3],f_fl[3],f_ro[3])), //10
7266  hex->face(3)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[3],f_fl[3],f_ro[3]))
7267  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[3],f_fl[3],f_ro[3])), //11
7268 
7269  new_lines[0] //12
7270  };
7271 
7272  unsigned int line_indices[13];
7273  for (unsigned int i=0; i<13; ++i)
7274  line_indices[i]=lines[i]->index();
7275 
7276  // the orientation of lines for the inner quads
7277  // is quite tricky. as these lines are newly
7278  // created ones and thus have no parents, they
7279  // cannot inherit this property. set up an array
7280  // and fill it with the respective values
7281  bool line_orientation[13];
7282 
7283  // the middle vertices of the
7284  // lines of our front face
7285  const unsigned int middle_vertices[4]=
7286  {
7287  hex->line(8)->child(0)->vertex_index(1),
7288  hex->line(9)->child(0)->vertex_index(1),
7289  hex->line(2)->child(0)->vertex_index(1),
7290  hex->line(6)->child(0)->vertex_index(1),
7291  };
7292 
7293  // note: for lines 0 to 3 the orientation of the
7294  // line is 'true', if vertex 0 is on the front
7295  for (unsigned int i=0; i<4; ++i)
7296  if (lines[i]->vertex_index(0)==middle_vertices[i])
7297  line_orientation[i]=true;
7298  else
7299  {
7300  // it must be the other way round then
7301  Assert(lines[i]->vertex_index(1)==middle_vertices[i],
7302  ExcInternalError());
7303  line_orientation[i]=false;
7304  }
7305 
7306  // note: for lines 4 to 11 (inner lines of the
7307  // outer quads) the following holds: the second
7308  // vertex of the even lines in standard
7309  // orientation is the vertex in the middle of
7310  // the quad, whereas for odd lines the first
7311  // vertex is the same middle vertex.
7312  for (unsigned int i=4; i<12; ++i)
7313  if (lines[i]->vertex_index((i+1)%2) ==
7314  middle_vertex_index<dim,spacedim>(hex->face(1+i/4)))
7315  line_orientation[i]=true;
7316  else
7317  {
7318  // it must be the other way
7319  // round then
7320  Assert(lines[i]->vertex_index(i%2) ==
7321  (middle_vertex_index<dim,spacedim>(hex->face(1+i/4))),
7322  ExcInternalError());
7323  line_orientation[i]=false;
7324  }
7325  // for the last line the line orientation is
7326  // always true, since it was just constructed
7327  // that way
7328  line_orientation[12]=true;
7329 
7330  // set up the 4 quads, numbered as follows (left
7331  // quad numbering, right line numbering
7332  // extracted from above), the drawings denote
7333  // middle planes
7334  //
7335  // * *
7336  // /| /|
7337  // / | 3 9
7338  // y/ * / *
7339  // * 3/| * /|
7340  // | / |x 5 12|8
7341  // |/ * |/ *
7342  // * 2/ * /
7343  // | / 4 2
7344  // |/ |/
7345  // * *
7346  //
7347  // y
7348  // *----*----* *-10-*-11-*
7349  // / / / / / /
7350  // / 0 / 1 / 0 12 1
7351  // / / / / / /
7352  // *----*----*x *--6-*--7-*
7353 
7354  new_quads[0]->set (internal::TriangulationImplementation
7355  ::TriaObject<2>(line_indices[0],
7356  line_indices[12],
7357  line_indices[6],
7358  line_indices[10]));
7359  new_quads[1]->set (internal::TriangulationImplementation
7360  ::TriaObject<2>(line_indices[12],
7361  line_indices[1],
7362  line_indices[7],
7363  line_indices[11]));
7364  new_quads[2]->set (internal::TriangulationImplementation
7365  ::TriaObject<2>(line_indices[4],
7366  line_indices[8],
7367  line_indices[2],
7368  line_indices[12]));
7369  new_quads[3]->set (internal::TriangulationImplementation
7370  ::TriaObject<2>(line_indices[5],
7371  line_indices[9],
7372  line_indices[12],
7373  line_indices[3]));
7374 
7375  new_quads[0]->set_line_orientation(0,line_orientation[0]);
7376  new_quads[0]->set_line_orientation(2,line_orientation[6]);
7377  new_quads[0]->set_line_orientation(3,line_orientation[10]);
7378 
7379  new_quads[1]->set_line_orientation(1,line_orientation[1]);
7380  new_quads[1]->set_line_orientation(2,line_orientation[7]);
7381  new_quads[1]->set_line_orientation(3,line_orientation[11]);
7382 
7383  new_quads[2]->set_line_orientation(0,line_orientation[4]);
7384  new_quads[2]->set_line_orientation(1,line_orientation[8]);
7385  new_quads[2]->set_line_orientation(2,line_orientation[2]);
7386 
7387  new_quads[3]->set_line_orientation(0,line_orientation[5]);
7388  new_quads[3]->set_line_orientation(1,line_orientation[9]);
7389  new_quads[3]->set_line_orientation(3,line_orientation[3]);
7390 
7391  // the quads are numbered as follows:
7392  //
7393  // planes in the interior of the old hex:
7394  //
7395  // *
7396  // /|
7397  // / | x
7398  // /3 * *-------* *----*----*
7399  // * /| | | / / /
7400  // | / | | | / 0 / 1 /
7401  // |/ * | | / / /
7402  // * 2/ *-------*y *----*----*x
7403  // | /
7404  // |/
7405  // *
7406  //
7407  // children of the faces
7408  // of the old hex
7409  // *---*---* *---*---*
7410  // /|13 | 15| / / /|
7411  // / | | | /18 / 19/ |
7412  // / *---*---* / / / *
7413  // * 5/| | | *---*---* 7/|
7414  // | / |12 | 14| | 9 | 11| / |
7415  // |/4 *---*---* | | |/6 *
7416  // * / / / *---*---* /
7417  // | /16 / 17/ | | | /
7418  // |/ / / | 8 | 10|/
7419  // *---*---* *---*---*
7420  //
7421  // note that we have to take care of the
7422  // orientation of faces.
7423  const int quad_indices[20]
7424  =
7425  {
7426  new_quads[0]->index(), //0
7427  new_quads[1]->index(),
7428  new_quads[2]->index(),
7429  new_quads[3]->index(),
7430 
7431  hex->face(0)->child_index( child_at_origin[hex->face(0)->refinement_case()-1][f_fl[0]][f_ro[0]]), //4
7432  hex->face(0)->child_index(1-child_at_origin[hex->face(0)->refinement_case()-1][f_fl[0]][f_ro[0]]),
7433 
7434  hex->face(1)->child_index( child_at_origin[hex->face(1)->refinement_case()-1][f_fl[1]][f_ro[1]]), //6
7435  hex->face(1)->child_index(1-child_at_origin[hex->face(1)->refinement_case()-1][f_fl[1]][f_ro[1]]),
7436 
7437  hex->face(2)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[2],f_fl[2],f_ro[2])), //8
7438  hex->face(2)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[2],f_fl[2],f_ro[2])),
7439  hex->face(2)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[2],f_fl[2],f_ro[2])),
7440  hex->face(2)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[2],f_fl[2],f_ro[2])),
7441 
7442  hex->face(3)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[3],f_fl[3],f_ro[3])), //12
7443  hex->face(3)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[3],f_fl[3],f_ro[3])),
7444  hex->face(3)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[3],f_fl[3],f_ro[3])),
7445  hex->face(3)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[3],f_fl[3],f_ro[3])),
7446 
7447  hex->face(4)->child_index( child_at_origin[hex->face(4)->refinement_case()-1][f_fl[4]][f_ro[4]]), //16
7448  hex->face(4)->child_index(1-child_at_origin[hex->face(4)->refinement_case()-1][f_fl[4]][f_ro[4]]),
7449 
7450  hex->face(5)->child_index( child_at_origin[hex->face(5)->refinement_case()-1][f_fl[5]][f_ro[5]]), //18
7451  hex->face(5)->child_index(1-child_at_origin[hex->face(5)->refinement_case()-1][f_fl[5]][f_ro[5]])
7452  };
7453 
7454  // due to the exchange of x and y for the front
7455  // and back face, we order the children
7456  // according to
7457  //
7458  // *---*---*
7459  // | 1 | 3 |
7460  // *---*---*
7461  // | 0 | 2 |
7462  // *---*---*
7463  new_hexes[0]->set (internal::TriangulationImplementation
7464  ::TriaObject<3>(quad_indices[4],
7465  quad_indices[2],
7466  quad_indices[8],
7467  quad_indices[12],
7468  quad_indices[16],
7469  quad_indices[0]));
7470  new_hexes[1]->set (internal::TriangulationImplementation
7471  ::TriaObject<3>(quad_indices[5],
7472  quad_indices[3],
7473  quad_indices[9],
7474  quad_indices[13],
7475  quad_indices[0],
7476  quad_indices[18]));
7477  new_hexes[2]->set (internal::TriangulationImplementation
7478  ::TriaObject<3>(quad_indices[2],
7479  quad_indices[6],
7480  quad_indices[10],
7481  quad_indices[14],
7482  quad_indices[17],
7483  quad_indices[1]));
7484  new_hexes[3]->set (internal::TriangulationImplementation
7485  ::TriaObject<3>(quad_indices[3],
7486  quad_indices[7],
7487  quad_indices[11],
7488  quad_indices[15],
7489  quad_indices[1],
7490  quad_indices[19]));
7491  break;
7492  }
7493 
7495  {
7497  //
7498  // RefinementCase<dim>::cut_yz
7499  //
7500  // the refined cube will look like this:
7501  //
7502  // *---------*
7503  // / /|
7504  // *---------* |
7505  // / /| |
7506  // *---------* |/|
7507  // | | * |
7508  // | |/| *
7509  // *---------* |/
7510  // | | *
7511  // | |/
7512  // *---------*
7513  //
7514 
7515  // first, create the new
7516  // internal line
7517  new_lines[0]->set (internal::TriangulationImplementation::
7518  TriaObject<1>(middle_vertex_index<dim,spacedim>(hex->face(0)),
7519  middle_vertex_index<dim,spacedim>(hex->face(1))));
7520 
7521  // again, first collect some data about the
7522  // indices of the lines, with the following
7523  // numbering: (note that face 0 and 1 each are
7524  // shown twice for better readability)
7525 
7526  // face 0: left plane
7527  // * *
7528  // /| /|
7529  // * | * |
7530  // /| * /| *
7531  // * 5/| * |7|
7532  // | * | | * |
7533  // |/| * |6| *
7534  // * 4/ * |/
7535  // | * | *
7536  // |/ |/
7537  // * *
7538  // face 1: right plane
7539  // * *
7540  // /| /|
7541  // * | * |
7542  // /| * /| *
7543  // * 9/| * |11
7544  // | * | | * |
7545  // |/| * |10 *
7546  // * 8/ * |/
7547  // | * | *
7548  // |/ |/
7549  // * *
7550  // face 2: front plane
7551  // (note: x,y exchanged)
7552  // *-------*
7553  // | |
7554  // *---0---*
7555  // | |
7556  // *-------*
7557  // face 3: back plane
7558  // (note: x,y exchanged)
7559  // *-------*
7560  // | |
7561  // *---1---*
7562  // | |
7563  // *-------*
7564  // face 4: bottom plane
7565  // *-------*
7566  // / /
7567  // *---2---*
7568  // / /
7569  // *-------*
7570  // face 5: top plane
7571  // *-------*
7572  // / /
7573  // *---3---*
7574  // / /
7575  // *-------*
7576  // middle planes
7577  // *-------* *-------*
7578  // / / | |
7579  // *---12--* | |
7580  // / / | |
7581  // *-------* *-------*
7582 
7583  // set up a list of line iterators first. from
7584  // this, construct lists of line_indices and
7585  // line orientations later on
7586  const typename Triangulation<dim,spacedim>::raw_line_iterator
7587  lines[13]
7588  =
7589  {
7590  hex->face(2)->child(0)
7591  ->line((hex->face(2)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //0
7592  hex->face(3)->child(0)
7593  ->line((hex->face(3)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //1
7594  hex->face(4)->child(0)
7595  ->line((hex->face(4)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //2
7596  hex->face(5)->child(0)
7597  ->line((hex->face(5)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //3
7598 
7599  hex->face(0)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[0],f_fl[0],f_ro[0]))
7600  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[0],f_fl[0],f_ro[0])), //4
7601  hex->face(0)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[0],f_fl[0],f_ro[0]))
7602  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[0],f_fl[0],f_ro[0])), //5
7603  hex->face(0)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[0],f_fl[0],f_ro[0]))
7604  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[0],f_fl[0],f_ro[0])), //6
7605  hex->face(0)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[0],f_fl[0],f_ro[0]))
7606  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[0],f_fl[0],f_ro[0])), //7
7607 
7608  hex->face(1)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[1],f_fl[1],f_ro[1]))
7609  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[1],f_fl[1],f_ro[1])), //8
7610  hex->face(1)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[1],f_fl[1],f_ro[1]))
7611  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[1],f_fl[1],f_ro[1])), //9
7612  hex->face(1)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[1],f_fl[1],f_ro[1]))
7613  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[1],f_fl[1],f_ro[1])), //10
7614  hex->face(1)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[1],f_fl[1],f_ro[1]))
7615  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[1],f_fl[1],f_ro[1])), //11
7616 
7617  new_lines[0] //12
7618  };
7619 
7620  unsigned int line_indices[13];
7621 
7622  for (unsigned int i=0; i<13; ++i)
7623  line_indices[i]=lines[i]->index();
7624 
7625  // the orientation of lines for the inner quads
7626  // is quite tricky. as these lines are newly
7627  // created ones and thus have no parents, they
7628  // cannot inherit this property. set up an array
7629  // and fill it with the respective values
7630  bool line_orientation[13];
7631 
7632  // the middle vertices of the lines of our front
7633  // face
7634  const unsigned int middle_vertices[4]=
7635  {
7636  hex->line(8)->child(0)->vertex_index(1),
7637  hex->line(10)->child(0)->vertex_index(1),
7638  hex->line(0)->child(0)->vertex_index(1),
7639  hex->line(4)->child(0)->vertex_index(1),
7640  };
7641 
7642  // note: for lines 0 to 3 the orientation of the
7643  // line is 'true', if vertex 0 is on the front
7644  for (unsigned int i=0; i<4; ++i)
7645  if (lines[i]->vertex_index(0)==middle_vertices[i])
7646  line_orientation[i]=true;
7647  else
7648  {
7649  // it must be the other way round then
7650  Assert(lines[i]->vertex_index(1)==middle_vertices[i],
7651  ExcInternalError());
7652  line_orientation[i]=false;
7653  }
7654 
7655  // note: for lines 4 to 11 (inner lines of the
7656  // outer quads) the following holds: the second
7657  // vertex of the even lines in standard
7658  // orientation is the vertex in the middle of
7659  // the quad, whereas for odd lines the first
7660  // vertex is the same middle vertex.
7661  for (unsigned int i=4; i<12; ++i)
7662  if (lines[i]->vertex_index((i+1)%2) ==
7663  middle_vertex_index<dim,spacedim>(hex->face(i/4-1)))
7664  line_orientation[i]=true;
7665  else
7666  {
7667  // it must be the other way
7668  // round then
7669  Assert(lines[i]->vertex_index(i%2) ==
7670  (middle_vertex_index<dim,spacedim>(hex->face(i/4-1))),
7671  ExcInternalError());
7672  line_orientation[i]=false;
7673  }
7674  // for the last line the line orientation is
7675  // always true, since it was just constructed
7676  // that way
7677  line_orientation[12]=true;
7678 
7679  // set up the 4 quads, numbered as follows (left
7680  // quad numbering, right line numbering
7681  // extracted from above)
7682  //
7683  // x
7684  // *-------* *---3---*
7685  // | 3 | 5 9
7686  // *-------* *---12--*
7687  // | 2 | 4 8
7688  // *-------*y *---2---*
7689  //
7690  // y
7691  // *---------* *----1----*
7692  // / 1 / 7 11
7693  // *---------* *----12---*
7694  // / 0 / 6 10
7695  // *---------*x *----0----*
7696 
7697  new_quads[0]->set (internal::TriangulationImplementation
7698  ::TriaObject<2>(line_indices[6],
7699  line_indices[10],
7700  line_indices[0],
7701  line_indices[12]));
7702  new_quads[1]->set (internal::TriangulationImplementation
7703  ::TriaObject<2>(line_indices[7],
7704  line_indices[11],
7705  line_indices[12],
7706  line_indices[1]));
7707  new_quads[2]->set (internal::TriangulationImplementation
7708  ::TriaObject<2>(line_indices[2],
7709  line_indices[12],
7710  line_indices[4],
7711  line_indices[8]));
7712  new_quads[3]->set (internal::TriangulationImplementation
7713  ::TriaObject<2>(line_indices[12],
7714  line_indices[3],
7715  line_indices[5],
7716  line_indices[9]));
7717 
7718  new_quads[0]->set_line_orientation(0,line_orientation[6]);
7719  new_quads[0]->set_line_orientation(1,line_orientation[10]);
7720  new_quads[0]->set_line_orientation(2,line_orientation[0]);
7721 
7722  new_quads[1]->set_line_orientation(0,line_orientation[7]);
7723  new_quads[1]->set_line_orientation(1,line_orientation[11]);
7724  new_quads[1]->set_line_orientation(3,line_orientation[1]);
7725 
7726  new_quads[2]->set_line_orientation(0,line_orientation[2]);
7727  new_quads[2]->set_line_orientation(2,line_orientation[4]);
7728  new_quads[2]->set_line_orientation(3,line_orientation[8]);
7729 
7730  new_quads[3]->set_line_orientation(1,line_orientation[3]);
7731  new_quads[3]->set_line_orientation(2,line_orientation[5]);
7732  new_quads[3]->set_line_orientation(3,line_orientation[9]);
7733 
7734  // the quads are numbered as follows:
7735  //
7736  // planes in the interior of the old hex:
7737  //
7738  // *
7739  // /|
7740  // / | x
7741  // / | *-------* *---------*
7742  // * | | 3 | / 1 /
7743  // | | *-------* *---------*
7744  // | * | 2 | / 0 /
7745  // | / *-------*y *---------*x
7746  // | /
7747  // |/
7748  // *
7749  //
7750  // children of the faces
7751  // of the old hex
7752  // *-------* *-------*
7753  // /| | / 19 /|
7754  // * | 15 | *-------* |
7755  // /|7*-------* / 18 /|11
7756  // * |/| | *-------* |/|
7757  // |6* | 14 | | 10* |
7758  // |/|5*-------* | 13 |/|9*
7759  // * |/ 17 / *-------* |/
7760  // |4*-------* | |8*
7761  // |/ 16 / | 12 |/
7762  // *-------* *-------*
7763  //
7764  // note that we have to take care of the
7765  // orientation of faces.
7766  const int quad_indices[20]
7767  =
7768  {
7769  new_quads[0]->index(), //0
7770  new_quads[1]->index(),
7771  new_quads[2]->index(),
7772  new_quads[3]->index(),
7773 
7774  hex->face(0)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[0],f_fl[0],f_ro[0])), //4
7775  hex->face(0)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[0],f_fl[0],f_ro[0])),
7776  hex->face(0)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[0],f_fl[0],f_ro[0])),
7777  hex->face(0)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[0],f_fl[0],f_ro[0])),
7778 
7779  hex->face(1)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[1],f_fl[1],f_ro[1])), //8
7780  hex->face(1)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[1],f_fl[1],f_ro[1])),
7781  hex->face(1)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[1],f_fl[1],f_ro[1])),
7782  hex->face(1)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[1],f_fl[1],f_ro[1])),
7783 
7784  hex->face(2)->child_index( child_at_origin[hex->face(2)->refinement_case()-1][f_fl[2]][f_ro[2]]), //12
7785  hex->face(2)->child_index(1-child_at_origin[hex->face(2)->refinement_case()-1][f_fl[2]][f_ro[2]]),
7786 
7787  hex->face(3)->child_index( child_at_origin[hex->face(3)->refinement_case()-1][f_fl[3]][f_ro[3]]), //14
7788  hex->face(3)->child_index(1-child_at_origin[hex->face(3)->refinement_case()-1][f_fl[3]][f_ro[3]]),
7789 
7790  hex->face(4)->child_index( child_at_origin[hex->face(4)->refinement_case()-1][f_fl[4]][f_ro[4]]), //16
7791  hex->face(4)->child_index(1-child_at_origin[hex->face(4)->refinement_case()-1][f_fl[4]][f_ro[4]]),
7792 
7793  hex->face(5)->child_index( child_at_origin[hex->face(5)->refinement_case()-1][f_fl[5]][f_ro[5]]), //18
7794  hex->face(5)->child_index(1-child_at_origin[hex->face(5)->refinement_case()-1][f_fl[5]][f_ro[5]])
7795  };
7796 
7797  new_hexes[0]->set (internal::TriangulationImplementation
7798  ::TriaObject<3>(quad_indices[4],
7799  quad_indices[8],
7800  quad_indices[12],
7801  quad_indices[2],
7802  quad_indices[16],
7803  quad_indices[0]));
7804  new_hexes[1]->set (internal::TriangulationImplementation
7805  ::TriaObject<3>(quad_indices[5],
7806  quad_indices[9],
7807  quad_indices[2],
7808  quad_indices[14],
7809  quad_indices[17],
7810  quad_indices[1]));
7811  new_hexes[2]->set (internal::TriangulationImplementation
7812  ::TriaObject<3>(quad_indices[6],
7813  quad_indices[10],
7814  quad_indices[13],
7815  quad_indices[3],
7816  quad_indices[0],
7817  quad_indices[18]));
7818  new_hexes[3]->set (internal::TriangulationImplementation
7819  ::TriaObject<3>(quad_indices[7],
7820  quad_indices[11],
7821  quad_indices[3],
7822  quad_indices[15],
7823  quad_indices[1],
7824  quad_indices[19]));
7825  break;
7826  }
7827 
7829  {
7831  //
7832  // RefinementCase<dim>::cut_xyz
7833  // isotropic refinement
7834  //
7835  // the refined cube will look
7836  // like this:
7837  //
7838  // *----*----*
7839  // / / /|
7840  // *----*----* |
7841  // / / /| *
7842  // *----*----* |/|
7843  // | | | * |
7844  // | | |/| *
7845  // *----*----* |/
7846  // | | | *
7847  // | | |/
7848  // *----*----*
7849  //
7850 
7851  // find the next unused vertex and set it
7852  // appropriately
7853  while (triangulation.vertices_used[next_unused_vertex] == true)
7854  ++next_unused_vertex;
7855  Assert (next_unused_vertex < triangulation.vertices.size(),
7856  ExcMessage("Internal error: During refinement, the triangulation wants to access an element of the 'vertices' array but it turns out that the array is not large enough."));
7857  triangulation.vertices_used[next_unused_vertex] = true;
7858 
7859  // the new vertex is definitely in the interior,
7860  // so we need not worry about the
7861  // boundary. However we need to worry about
7862  // Manifolds. Let the cell compute its own
7863  // center, by querying the underlying manifold
7864  // object.
7865  triangulation.vertices[next_unused_vertex] =
7866  hex->center(true, true);
7867 
7868  // set the data of the six lines. first collect
7869  // the indices of the seven vertices (consider
7870  // the two planes to be crossed to form the
7871  // planes cutting the hex in two vertically and
7872  // horizontally)
7873  //
7874  // *--3--* *--5--*
7875  // / / / | | |
7876  // 0--6--1 0--6--1
7877  // / / / | | |
7878  // *--2--* *--4--*
7879  // the lines are numbered
7880  // as follows:
7881  // *--*--* *--*--*
7882  // / 1 / | 5 |
7883  // *2-*-3* *2-*-3*
7884  // / 0 / | 4 |
7885  // *--*--* *--*--*
7886  //
7887  const unsigned int vertex_indices[7]
7888  = { middle_vertex_index<dim,spacedim>(hex->face(0)),
7889  middle_vertex_index<dim,spacedim>(hex->face(1)),
7890  middle_vertex_index<dim,spacedim>(hex->face(2)),
7891  middle_vertex_index<dim,spacedim>(hex->face(3)),
7892  middle_vertex_index<dim,spacedim>(hex->face(4)),
7893  middle_vertex_index<dim,spacedim>(hex->face(5)),
7894  next_unused_vertex
7895  };
7896 
7897  new_lines[0]->set (internal::TriangulationImplementation::
7898  TriaObject<1>(vertex_indices[2], vertex_indices[6]));
7899  new_lines[1]->set (internal::TriangulationImplementation::
7900  TriaObject<1>(vertex_indices[6], vertex_indices[3]));
7901  new_lines[2]->set (internal::TriangulationImplementation::
7902  TriaObject<1>(vertex_indices[0], vertex_indices[6]));
7903  new_lines[3]->set (internal::TriangulationImplementation::
7904  TriaObject<1>(vertex_indices[6], vertex_indices[1]));
7905  new_lines[4]->set (internal::TriangulationImplementation::
7906  TriaObject<1>(vertex_indices[4], vertex_indices[6]));
7907  new_lines[5]->set (internal::TriangulationImplementation::
7908  TriaObject<1>(vertex_indices[6], vertex_indices[5]));
7909 
7910  // again, first collect some data about the
7911  // indices of the lines, with the following
7912  // numbering: (note that face 0 and 1 each are
7913  // shown twice for better readability)
7914 
7915  // face 0: left plane
7916  // * *
7917  // /| /|
7918  // * | * |
7919  // /| * /| *
7920  // * 1/| * |3|
7921  // | * | | * |
7922  // |/| * |2| *
7923  // * 0/ * |/
7924  // | * | *
7925  // |/ |/
7926  // * *
7927  // face 1: right plane
7928  // * *
7929  // /| /|
7930  // * | * |
7931  // /| * /| *
7932  // * 5/| * |7|
7933  // | * | | * |
7934  // |/| * |6| *
7935  // * 4/ * |/
7936  // | * | *
7937  // |/ |/
7938  // * *
7939  // face 2: front plane
7940  // (note: x,y exchanged)
7941  // *---*---*
7942  // | 11 |
7943  // *-8-*-9-*
7944  // | 10 |
7945  // *---*---*
7946  // face 3: back plane
7947  // (note: x,y exchanged)
7948  // *---*---*
7949  // | 15 |
7950  // *12-*-13*
7951  // | 14 |
7952  // *---*---*
7953  // face 4: bottom plane
7954  // *---*---*
7955  // / 17 /
7956  // *18-*-19*
7957  // / 16 /
7958  // *---*---*
7959  // face 5: top plane
7960  // *---*---*
7961  // / 21 /
7962  // *22-*-23*
7963  // / 20 /
7964  // *---*---*
7965  // middle planes
7966  // *---*---* *---*---*
7967  // / 25 / | 29 |
7968  // *26-*-27* *26-*-27*
7969  // / 24 / | 28 |
7970  // *---*---* *---*---*
7971 
7972  // set up a list of line iterators first. from
7973  // this, construct lists of line_indices and
7974  // line orientations later on
7975  const typename Triangulation<dim,spacedim>::raw_line_iterator
7976  lines[30]
7977  =
7978  {
7979  hex->face(0)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[0],f_fl[0],f_ro[0]))
7980  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[0],f_fl[0],f_ro[0])), //0
7981  hex->face(0)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[0],f_fl[0],f_ro[0]))
7982  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[0],f_fl[0],f_ro[0])), //1
7983  hex->face(0)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[0],f_fl[0],f_ro[0]))
7984  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[0],f_fl[0],f_ro[0])), //2
7985  hex->face(0)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[0],f_fl[0],f_ro[0]))
7986  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[0],f_fl[0],f_ro[0])), //3
7987 
7988  hex->face(1)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[1],f_fl[1],f_ro[1]))
7989  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[1],f_fl[1],f_ro[1])), //4
7990  hex->face(1)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[1],f_fl[1],f_ro[1]))
7991  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[1],f_fl[1],f_ro[1])), //5
7992  hex->face(1)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[1],f_fl[1],f_ro[1]))
7993  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[1],f_fl[1],f_ro[1])), //6
7994  hex->face(1)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[1],f_fl[1],f_ro[1]))
7995  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[1],f_fl[1],f_ro[1])), //7
7996 
7997  hex->face(2)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[2],f_fl[2],f_ro[2]))
7998  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[2],f_fl[2],f_ro[2])), //8
7999  hex->face(2)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[2],f_fl[2],f_ro[2]))
8000  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[2],f_fl[2],f_ro[2])), //9
8001  hex->face(2)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[2],f_fl[2],f_ro[2]))
8002  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[2],f_fl[2],f_ro[2])), //10
8003  hex->face(2)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[2],f_fl[2],f_ro[2]))
8004  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[2],f_fl[2],f_ro[2])), //11
8005 
8006  hex->face(3)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[3],f_fl[3],f_ro[3]))
8007  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[3],f_fl[3],f_ro[3])), //12
8008  hex->face(3)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[3],f_fl[3],f_ro[3]))
8009  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[3],f_fl[3],f_ro[3])), //13
8010  hex->face(3)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[3],f_fl[3],f_ro[3]))
8011  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[3],f_fl[3],f_ro[3])), //14
8012  hex->face(3)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[3],f_fl[3],f_ro[3]))
8013  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[3],f_fl[3],f_ro[3])), //15
8014 
8015  hex->face(4)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[4],f_fl[4],f_ro[4]))
8016  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[4],f_fl[4],f_ro[4])), //16
8017  hex->face(4)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[4],f_fl[4],f_ro[4]))
8018  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[4],f_fl[4],f_ro[4])), //17
8019  hex->face(4)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[4],f_fl[4],f_ro[4]))
8020  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[4],f_fl[4],f_ro[4])), //18
8021  hex->face(4)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[4],f_fl[4],f_ro[4]))
8022  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[4],f_fl[4],f_ro[4])), //19
8023 
8024  hex->face(5)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[5],f_fl[5],f_ro[5]))
8025  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[5],f_fl[5],f_ro[5])), //20
8026  hex->face(5)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[5],f_fl[5],f_ro[5]))
8027  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[5],f_fl[5],f_ro[5])), //21
8028  hex->face(5)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[5],f_fl[5],f_ro[5]))
8029  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[5],f_fl[5],f_ro[5])), //22
8030  hex->face(5)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[5],f_fl[5],f_ro[5]))
8031  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[5],f_fl[5],f_ro[5])), //23
8032 
8033  new_lines[0], //24
8034  new_lines[1], //25
8035  new_lines[2], //26
8036  new_lines[3], //27
8037  new_lines[4], //28
8038  new_lines[5] //29
8039  };
8040 
8041  unsigned int line_indices[30];
8042  for (unsigned int i=0; i<30; ++i)
8043  line_indices[i]=lines[i]->index();
8044 
8045  // the orientation of lines for the inner quads
8046  // is quite tricky. as these lines are newly
8047  // created ones and thus have no parents, they
8048  // cannot inherit this property. set up an array
8049  // and fill it with the respective values
8050  bool line_orientation[30];
8051 
8052  // note: for the first 24 lines (inner lines of
8053  // the outer quads) the following holds: the
8054  // second vertex of the even lines in standard
8055  // orientation is the vertex in the middle of
8056  // the quad, whereas for odd lines the first
8057  // vertex is the same middle vertex.
8058  for (unsigned int i=0; i<24; ++i)
8059  if (lines[i]->vertex_index((i+1)%2)==vertex_indices[i/4])
8060  line_orientation[i]=true;
8061  else
8062  {
8063  // it must be the other way
8064  // round then
8065  Assert(lines[i]->vertex_index(i%2)==vertex_indices[i/4],
8066  ExcInternalError());
8067  line_orientation[i]=false;
8068  }
8069  // for the last 6 lines the line orientation is
8070  // always true, since they were just constructed
8071  // that way
8072  for (unsigned int i=24; i<30; ++i)
8073  line_orientation[i]=true;
8074 
8075  // set up the 12 quads, numbered as follows
8076  // (left quad numbering, right line numbering
8077  // extracted from above)
8078  //
8079  // * *
8080  // /| 21|
8081  // * | * 15
8082  // y/|3* 20| *
8083  // * |/| * |/|
8084  // |2* |x 11 * 14
8085  // |/|1* |/| *
8086  // * |/ * |17
8087  // |0* 10 *
8088  // |/ |16
8089  // * *
8090  //
8091  // x
8092  // *---*---* *22-*-23*
8093  // | 5 | 7 | 1 29 5
8094  // *---*---* *26-*-27*
8095  // | 4 | 6 | 0 28 4
8096  // *---*---*y *18-*-19*
8097  //
8098  // y
8099  // *----*----* *-12-*-13-*
8100  // / 10 / 11 / 3 25 7
8101  // *----*----* *-26-*-27-*
8102  // / 8 / 9 / 2 24 6
8103  // *----*----*x *--8-*--9-*
8104 
8105  new_quads[0]->set (internal::TriangulationImplementation
8106  ::TriaObject<2>(line_indices[10],
8107  line_indices[28],
8108  line_indices[16],
8109  line_indices[24]));
8110  new_quads[1]->set (internal::TriangulationImplementation
8111  ::TriaObject<2>(line_indices[28],
8112  line_indices[14],
8113  line_indices[17],
8114  line_indices[25]));
8115  new_quads[2]->set (internal::TriangulationImplementation
8116  ::TriaObject<2>(line_indices[11],
8117  line_indices[29],
8118  line_indices[24],
8119  line_indices[20]));
8120  new_quads[3]->set (internal::TriangulationImplementation
8121  ::TriaObject<2>(line_indices[29],
8122  line_indices[15],
8123  line_indices[25],
8124  line_indices[21]));
8125  new_quads[4]->set (internal::TriangulationImplementation
8126  ::TriaObject<2>(line_indices[18],
8127  line_indices[26],
8128  line_indices[0],
8129  line_indices[28]));
8130  new_quads[5]->set (internal::TriangulationImplementation
8131  ::TriaObject<2>(line_indices[26],
8132  line_indices[22],
8133  line_indices[1],
8134  line_indices[29]));
8135  new_quads[6]->set (internal::TriangulationImplementation
8136  ::TriaObject<2>(line_indices[19],
8137  line_indices[27],
8138  line_indices[28],
8139  line_indices[4]));
8140  new_quads[7]->set (internal::TriangulationImplementation
8141  ::TriaObject<2>(line_indices[27],
8142  line_indices[23],
8143  line_indices[29],
8144  line_indices[5]));
8145  new_quads[8]->set (internal::TriangulationImplementation
8146  ::TriaObject<2>(line_indices[2],
8147  line_indices[24],
8148  line_indices[8],
8149  line_indices[26]));
8150  new_quads[9]->set (internal::TriangulationImplementation
8151  ::TriaObject<2>(line_indices[24],
8152  line_indices[6],
8153  line_indices[9],
8154  line_indices[27]));
8155  new_quads[10]->set (internal::TriangulationImplementation
8156  ::TriaObject<2>(line_indices[3],
8157  line_indices[25],
8158  line_indices[26],
8159  line_indices[12]));
8160  new_quads[11]->set (internal::TriangulationImplementation
8161  ::TriaObject<2>(line_indices[25],
8162  line_indices[7],
8163  line_indices[27],
8164  line_indices[13]));
8165 
8166  // now reset the line_orientation flags of outer
8167  // lines as they cannot be set in a loop (at
8168  // least not easily)
8169  new_quads[0]->set_line_orientation(0,line_orientation[10]);
8170  new_quads[0]->set_line_orientation(2,line_orientation[16]);
8171 
8172  new_quads[1]->set_line_orientation(1,line_orientation[14]);
8173  new_quads[1]->set_line_orientation(2,line_orientation[17]);
8174 
8175  new_quads[2]->set_line_orientation(0,line_orientation[11]);
8176  new_quads[2]->set_line_orientation(3,line_orientation[20]);
8177 
8178  new_quads[3]->set_line_orientation(1,line_orientation[15]);
8179  new_quads[3]->set_line_orientation(3,line_orientation[21]);
8180 
8181  new_quads[4]->set_line_orientation(0,line_orientation[18]);
8182  new_quads[4]->set_line_orientation(2,line_orientation[0]);
8183 
8184  new_quads[5]->set_line_orientation(1,line_orientation[22]);
8185  new_quads[5]->set_line_orientation(2,line_orientation[1]);
8186 
8187  new_quads[6]->set_line_orientation(0,line_orientation[19]);
8188  new_quads[6]->set_line_orientation(3,line_orientation[4]);
8189 
8190  new_quads[7]->set_line_orientation(1,line_orientation[23]);
8191  new_quads[7]->set_line_orientation(3,line_orientation[5]);
8192 
8193  new_quads[8]->set_line_orientation(0,line_orientation[2]);
8194  new_quads[8]->set_line_orientation(2,line_orientation[8]);
8195 
8196  new_quads[9]->set_line_orientation(1,line_orientation[6]);
8197  new_quads[9]->set_line_orientation(2,line_orientation[9]);
8198 
8199  new_quads[10]->set_line_orientation(0,line_orientation[3]);
8200  new_quads[10]->set_line_orientation(3,line_orientation[12]);
8201 
8202  new_quads[11]->set_line_orientation(1,line_orientation[7]);
8203  new_quads[11]->set_line_orientation(3,line_orientation[13]);
8204 
8206  // create the eight new hexes
8207  //
8208  // again first collect some data. here, we need
8209  // the indices of a whole lotta quads.
8210 
8211  // the quads are numbered as follows:
8212  //
8213  // planes in the interior of the old hex:
8214  //
8215  // *
8216  // /|
8217  // * |
8218  // /|3* *---*---* *----*----*
8219  // * |/| | 5 | 7 | / 10 / 11 /
8220  // |2* | *---*---* *----*----*
8221  // |/|1* | 4 | 6 | / 8 / 9 /
8222  // * |/ *---*---*y *----*----*x
8223  // |0*
8224  // |/
8225  // *
8226  //
8227  // children of the faces
8228  // of the old hex
8229  // *-------* *-------*
8230  // /|25 27| /34 35/|
8231  // 15| | / /19
8232  // / | | /32 33/ |
8233  // * |24 26| *-------*18 |
8234  // 1413*-------* |21 23| 17*
8235  // | /30 31/ | | /
8236  // 12/ / | |16
8237  // |/28 29/ |20 22|/
8238  // *-------* *-------*
8239  //
8240  // note that we have to
8241  // take care of the
8242  // orientation of
8243  // faces.
8244  const int quad_indices[36]
8245  =
8246  {
8247  new_quads[0]->index(), //0
8248  new_quads[1]->index(),
8249  new_quads[2]->index(),
8250  new_quads[3]->index(),
8251  new_quads[4]->index(),
8252  new_quads[5]->index(),
8253  new_quads[6]->index(),
8254  new_quads[7]->index(),
8255  new_quads[8]->index(),
8256  new_quads[9]->index(),
8257  new_quads[10]->index(),
8258  new_quads[11]->index(), //11
8259 
8260  hex->face(0)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[0],f_fl[0],f_ro[0])), //12
8261  hex->face(0)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[0],f_fl[0],f_ro[0])),
8262  hex->face(0)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[0],f_fl[0],f_ro[0])),
8263  hex->face(0)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[0],f_fl[0],f_ro[0])),
8264 
8265  hex->face(1)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[1],f_fl[1],f_ro[1])), //16
8266  hex->face(1)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[1],f_fl[1],f_ro[1])),
8267  hex->face(1)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[1],f_fl[1],f_ro[1])),
8268  hex->face(1)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[1],f_fl[1],f_ro[1])),
8269 
8270  hex->face(2)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[2],f_fl[2],f_ro[2])), //20
8271  hex->face(2)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[2],f_fl[2],f_ro[2])),
8272  hex->face(2)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[2],f_fl[2],f_ro[2])),
8273  hex->face(2)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[2],f_fl[2],f_ro[2])),
8274 
8275  hex->face(3)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[3],f_fl[3],f_ro[3])), //24
8276  hex->face(3)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[3],f_fl[3],f_ro[3])),
8277  hex->face(3)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[3],f_fl[3],f_ro[3])),
8278  hex->face(3)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[3],f_fl[3],f_ro[3])),
8279 
8280  hex->face(4)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[4],f_fl[4],f_ro[4])), //28
8281  hex->face(4)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[4],f_fl[4],f_ro[4])),
8282  hex->face(4)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[4],f_fl[4],f_ro[4])),
8283  hex->face(4)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[4],f_fl[4],f_ro[4])),
8284 
8285  hex->face(5)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[5],f_fl[5],f_ro[5])), //32
8286  hex->face(5)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[5],f_fl[5],f_ro[5])),
8287  hex->face(5)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[5],f_fl[5],f_ro[5])),
8288  hex->face(5)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[5],f_fl[5],f_ro[5]))
8289  };
8290 
8291  // bottom children
8292  new_hexes[0]->set (internal::TriangulationImplementation
8293  ::TriaObject<3>(quad_indices[12],
8294  quad_indices[0],
8295  quad_indices[20],
8296  quad_indices[4],
8297  quad_indices[28],
8298  quad_indices[8]));
8299  new_hexes[1]->set (internal::TriangulationImplementation
8300  ::TriaObject<3>(quad_indices[0],
8301  quad_indices[16],
8302  quad_indices[22],
8303  quad_indices[6],
8304  quad_indices[29],
8305  quad_indices[9]));
8306  new_hexes[2]->set (internal::TriangulationImplementation
8307  ::TriaObject<3>(quad_indices[13],
8308  quad_indices[1],
8309  quad_indices[4],
8310  quad_indices[24],
8311  quad_indices[30],
8312  quad_indices[10]));
8313  new_hexes[3]->set (internal::TriangulationImplementation
8314  ::TriaObject<3>(quad_indices[1],
8315  quad_indices[17],
8316  quad_indices[6],
8317  quad_indices[26],
8318  quad_indices[31],
8319  quad_indices[11]));
8320 
8321  // top children
8322  new_hexes[4]->set (internal::TriangulationImplementation
8323  ::TriaObject<3>(quad_indices[14],
8324  quad_indices[2],
8325  quad_indices[21],
8326  quad_indices[5],
8327  quad_indices[8],
8328  quad_indices[32]));
8329  new_hexes[5]->set (internal::TriangulationImplementation
8330  ::TriaObject<3>(quad_indices[2],
8331  quad_indices[18],
8332  quad_indices[23],
8333  quad_indices[7],
8334  quad_indices[9],
8335  quad_indices[33]));
8336  new_hexes[6]->set (internal::TriangulationImplementation
8337  ::TriaObject<3>(quad_indices[15],
8338  quad_indices[3],
8339  quad_indices[5],
8340  quad_indices[25],
8341  quad_indices[10],
8342  quad_indices[34]));
8343  new_hexes[7]->set (internal::TriangulationImplementation
8344  ::TriaObject<3>(quad_indices[3],
8345  quad_indices[19],
8346  quad_indices[7],
8347  quad_indices[27],
8348  quad_indices[11],
8349  quad_indices[35]));
8350  break;
8351  }
8352  default:
8353  // all refinement cases have been treated, there
8354  // only remains
8355  // RefinementCase<dim>::no_refinement as
8356  // untreated enumeration value. However, in that
8357  // case we should have aborted much
8358  // earlier. thus we should never get here
8359  Assert(false, ExcInternalError());
8360  break;
8361  }//switch (ref_case)
8362 
8363  // and set face orientation flags. note that new
8364  // faces in the interior of the mother cell always
8365  // have a correctly oriented face, but the ones on
8366  // the outer faces will inherit this flag
8367  //
8368  // the flag have been set to true for all faces
8369  // initially, now go the other way round and reset
8370  // faces that are at the boundary of the mother cube
8371  //
8372  // the same is true for the face_flip and
8373  // face_rotation flags. however, the latter two are
8374  // set to false by default as this is the standard
8375  // value
8376 
8377  // loop over all faces and all (relevant) subfaces
8378  // of that in order to set the correct values for
8379  // face_orientation, face_flip and face_rotation,
8380  // which are inherited from the corresponding face
8381  // of the mother cube
8382  for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
8383  for (unsigned int s=0;
8385  1U);
8386  ++s)
8387  {
8388  const unsigned int current_child
8390  f,
8391  s,
8392  f_or[f],
8393  f_fl[f],
8394  f_ro[f],
8396  f,
8397  f_or[f],
8398  f_fl[f],
8399  f_ro[f]));
8400  new_hexes[current_child]->set_face_orientation (f, f_or[f]);
8401  new_hexes[current_child]->set_face_flip (f, f_fl[f]);
8402  new_hexes[current_child]->set_face_rotation (f, f_ro[f]);
8403  }
8404 
8405  // now see if we have created cells that are
8406  // distorted and if so add them to our list
8407  if ((check_for_distorted_cells == true)
8408  &&
8409  has_distorted_children (hex,
8410  std::integral_constant<int, dim>(),
8411  std::integral_constant<int, spacedim>()))
8412  cells_with_distorted_children.distorted_cells.push_back (hex);
8413 
8414  // note that the refinement flag was already cleared
8415  // at the beginning of this loop
8416 
8417  // inform all listeners that cell refinement is done
8418  triangulation.signals.post_refinement_on_cell(hex);
8419  }
8420  }
8421 
8422  // clear user data on quads. we used some of this data to
8423  // indicate anisotropic refinemnt cases on faces. all data
8424  // should be cleared by now, but the information whether we
8425  // used indices or pointers is still present. reset it now to
8426  // enable the user to use whichever he likes later on.
8427  triangulation.faces->quads.clear_user_data();
8428 
8429  // return the list with distorted children
8430  return cells_with_distorted_children;
8431  }
8432 
8433 
8446  template <int spacedim>
8447  static
8448  void
8450 
8451 
8452  template <int dim, int spacedim>
8453  static
8454  void
8456  {
8457  // If the codimension is one, we cannot perform this check
8458  // yet.
8459  if (spacedim>dim) return;
8460 
8462  cell=triangulation.begin(); cell!=triangulation.end(); ++cell)
8463  if (cell->at_boundary() &&
8464  cell->refine_flag_set() &&
8465  cell->refine_flag_set()!=RefinementCase<dim>::isotropic_refinement)
8466  {
8467  // The cell is at the boundary and it is flagged for
8468  // anisotropic refinement. Therefore, we have a closer
8469  // look
8470  const RefinementCase<dim> ref_case=cell->refine_flag_set();
8471  for (unsigned int face_no=0;
8472  face_no<GeometryInfo<dim>::faces_per_cell;
8473  ++face_no)
8474  if (cell->face(face_no)->at_boundary())
8475  {
8476  // this is the critical face at the boundary.
8477  if (GeometryInfo<dim>::face_refinement_case(ref_case,face_no)
8478  !=RefinementCase<dim-1>::isotropic_refinement)
8479  {
8480  // up to now, we do not want to refine this
8481  // cell along the face under consideration
8482  // here.
8484  face = cell->face(face_no);
8485  // the new point on the boundary would be this
8486  // one.
8487  const Point<spacedim> new_bound
8488  = face->center(true);
8489  // to check it, transform to the unit cell
8490  // with Q1Mapping
8491  const Point<dim> new_unit
8493  transform_real_to_unit_cell(cell,
8494  new_bound);
8495 
8496  // Now, we have to calculate the distance from
8497  // the face in the unit cell.
8498 
8499  // take the correct coordinate direction (0
8500  // for faces 0 and 1, 1 for faces 2 and 3, 2
8501  // for faces 4 and 5) and subtract the correct
8502  // boundary value of the face (0 for faces 0,
8503  // 2, and 4; 1 for faces 1, 3 and 5)
8504  const double dist = std::fabs(new_unit[face_no/2] - face_no%2);
8505 
8506  // compare this with the empirical value
8507  // allowed. if it is too big, flag the face
8508  // for isotropic refinement
8509  const double allowed=0.25;
8510 
8511  if (dist>allowed)
8512  cell->flag_for_face_refinement(face_no);
8513  }//if flagged for anistropic refinement
8514  }//if (cell->face(face)->at_boundary())
8515  }//for all cells
8516  }
8517 
8518 
8531  template <int dim, int spacedim>
8532  static
8533  void
8535  {
8536  Assert (dim < 3,
8537  ExcMessage ("Wrong function called -- there should "
8538  "be a specialization."));
8539  }
8540 
8541 
8542  template <int spacedim>
8543  static
8544  void
8546  {
8547  const unsigned int dim = 3;
8548 
8549  // first clear flags on lines, since we need them to determine
8550  // which lines will be refined
8551  triangulation.clear_user_flags_line();
8552 
8553  // also clear flags on hexes, since we need them to mark those
8554  // cells which are to be coarsened
8555  triangulation.clear_user_flags_hex();
8556 
8557  // variable to store whether the mesh was changed in the
8558  // present loop and in the whole process
8559  bool mesh_changed = false;
8560 
8561  do
8562  {
8563  mesh_changed = false;
8564 
8565  // for this following, we need to know which cells are
8566  // going to be coarsened, if we had to make a
8567  // decision. the following function sets these flags:
8568  triangulation.fix_coarsen_flags ();
8569 
8570 
8571  // flag those lines that are refined and will not be
8572  // coarsened and those that will be refined
8574  cell=triangulation.begin(); cell!=triangulation.end(); ++cell)
8575  if (cell->refine_flag_set())
8576  {
8577  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
8578  if (GeometryInfo<dim>::line_refinement_case(cell->refine_flag_set(), line)
8580  // flag a line, that will be
8581  // refined
8582  cell->line(line)->set_user_flag();
8583  }
8584  else if (cell->has_children() && !cell->child(0)->coarsen_flag_set())
8585  {
8586  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
8587  if (GeometryInfo<dim>::line_refinement_case(cell->refinement_case(), line)
8589  // flag a line, that is refined
8590  // and will stay so
8591  cell->line(line)->set_user_flag();
8592  }
8593  else if (cell->has_children() && cell->child(0)->coarsen_flag_set())
8594  cell->set_user_flag();
8595 
8596 
8597  // now check whether there are cells with lines that are
8598  // more than once refined or that will be more than once
8599  // refined. The first thing should never be the case, in
8600  // the second case we flag the cell for refinement
8602  cell=triangulation.last_active(); cell!=triangulation.end(); --cell)
8603  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
8604  {
8605  if (cell->line(line)->has_children())
8606  {
8607  // if this line is refined, its children should
8608  // not have further children
8609  //
8610  // however, if any of the children is flagged
8611  // for further refinement, we need to refine
8612  // this cell also (at least, if the cell is not
8613  // already flagged)
8614  bool offending_line_found = false;
8615 
8616  for (unsigned int c=0; c<2; ++c)
8617  {
8618  Assert (cell->line(line)->child(c)->has_children() == false,
8619  ExcInternalError());
8620 
8621  if (cell->line(line)->child(c)->user_flag_set () &&
8622  (GeometryInfo<dim>::line_refinement_case(cell->refine_flag_set(),
8623  line)
8625  {
8626  // tag this cell for refinement
8627  cell->clear_coarsen_flag ();
8628  // if anisotropic coarsening is allowed:
8629  // extend the refine_flag in the needed
8630  // direction, else set refine_flag
8631  // (isotropic)
8632  if (triangulation.smooth_grid &
8634  cell->flag_for_line_refinement(line);
8635  else
8636  cell->set_refine_flag();
8637 
8638  for (unsigned int l=0; l<GeometryInfo<dim>::lines_per_cell; ++l)
8639  if (GeometryInfo<dim>::line_refinement_case(cell->refine_flag_set(), line)
8641  // flag a line, that will be refined
8642  cell->line(l)->set_user_flag();
8643 
8644  // note that we have changed the grid
8645  offending_line_found = true;
8646 
8647  // it may save us several loop
8648  // iterations if we flag all lines of
8649  // this cell now (and not at the outset
8650  // of the next iteration) for refinement
8651  for (unsigned int l=0;
8652  l<GeometryInfo<dim>::lines_per_cell; ++l)
8653  if (!cell->line(l)->has_children() &&
8654  (GeometryInfo<dim>::line_refinement_case(cell->refine_flag_set(),
8655  l)
8657  cell->line(l)->set_user_flag();
8658 
8659  break;
8660  }
8661  }
8662 
8663  if (offending_line_found)
8664  {
8665  mesh_changed = true;
8666  break;
8667  }
8668  }
8669  }
8670 
8671 
8672  // there is another thing here: if any of the lines will
8673  // be refined, then we may not coarsen the present cell
8674  // similarly, if any of the lines *is* already refined, we
8675  // may not coarsen the current cell. however, there's a
8676  // catch: if the line is refined, but the cell behind it
8677  // is going to be coarsened, then the situation
8678  // changes. if we forget this second condition, the
8679  // refine_and_coarsen_3d test will start to fail. note
8680  // that to know which cells are going to be coarsened, the
8681  // call for fix_coarsen_flags above is necessary
8683  cell=triangulation.last(); cell!=triangulation.end(); --cell)
8684  {
8685  if (cell->user_flag_set())
8686  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
8687  if (cell->line(line)->has_children() &&
8688  (cell->line(line)->child(0)->user_flag_set() ||
8689  cell->line(line)->child(1)->user_flag_set()))
8690  {
8691  for (unsigned int c=0; c<cell->n_children(); ++c)
8692  cell->child(c)->clear_coarsen_flag ();
8693  cell->clear_user_flag();
8694  for (unsigned int l=0; l<GeometryInfo<dim>::lines_per_cell; ++l)
8695  if (GeometryInfo<dim>::line_refinement_case(cell->refinement_case(), l)
8697  // flag a line, that is refined
8698  // and will stay so
8699  cell->line(l)->set_user_flag();
8700  mesh_changed = true;
8701  break;
8702  }
8703  }
8704  }
8705  while (mesh_changed == true);
8706  }
8707 
8708 
8709 
8716  template <int dim, int spacedim>
8717  static
8718  bool
8720  {
8721  // in 1d, coarsening is always allowed since we don't enforce
8722  // the 2:1 constraint there
8723  if (dim == 1)
8724  return true;
8725 
8726  const RefinementCase<dim> ref_case = cell->refinement_case();
8727  for (unsigned int n=0; n<GeometryInfo<dim>::faces_per_cell; ++n)
8728  {
8729 
8730  // if the cell is not refined along that face, coarsening
8731  // will not change anything, so do nothing. the same
8732  // applies, if the face is at the boandary
8733  const RefinementCase<dim-1> face_ref_case =
8734  GeometryInfo<dim>::face_refinement_case(cell->refinement_case(), n);
8735 
8736  const unsigned int n_subfaces
8737  = GeometryInfo<dim-1>::n_children(face_ref_case);
8738 
8739  if (n_subfaces == 0 || cell->at_boundary(n))
8740  continue;
8741  for (unsigned int c=0; c<n_subfaces; ++c)
8742  {
8744  child = cell->child(GeometryInfo<dim>::
8745  child_cell_on_face(ref_case,
8746  n,c));
8747 
8749  child_neighbor = child->neighbor(n);
8750  if (!child->neighbor_is_coarser(n))
8751  // in 2d, if the child's neighbor is coarser, then
8752  // it has no children. however, in 3d it might be
8753  // otherwise. consider for example, that our face
8754  // might be refined with cut_x, but the neighbor is
8755  // refined with cut_xy at that face. then the
8756  // neighbor pointers of the children of our cell
8757  // will point to the common neighbor cell, not to
8758  // its children. what we really want to know in the
8759  // following is, whether the neighbor cell is
8760  // refined twice with reference to our cell. that
8761  // only has to be asked, if the child's neighbor is
8762  // not a coarser one.
8763  if ((child_neighbor->has_children() &&
8764  !child_neighbor->user_flag_set())||
8765  // neighbor has children, which are further
8766  // refined along the face, otherwise something
8767  // went wrong in the construction of neighbor
8768  // pointers. then only allow coarsening if this
8769  // neighbor will be coarsened as well
8770  // (user_pointer is set). the same applies, if
8771  // the neighbors children are not refined but
8772  // will be after refinement
8773  child_neighbor->refine_flag_set())
8774  return false;
8775  }
8776  }
8777  return true;
8778  }
8779  };
8780 
8781 
8782 
8783  template <int dim, int spacedim>
8784  const Manifold<dim, spacedim> &
8785  get_default_flat_manifold()
8786  {
8787  static const FlatManifold<dim, spacedim> flat_manifold;
8788  return flat_manifold;
8789  }
8790  }
8791 }
8792 
8793 
8794 template <int dim, int spacedim>
8797 
8798 
8799 
8800 template <int dim, int spacedim>
8801 const unsigned int
8803 
8804 
8805 
8806 template <int dim, int spacedim>
8808 Triangulation (const MeshSmoothing smooth_grid,
8809  const bool check_for_distorted_cells)
8810  :
8811  smooth_grid(smooth_grid),
8812  anisotropic_refinement(false),
8813  check_for_distorted_cells(check_for_distorted_cells)
8814 {
8815  if (dim == 1)
8816  {
8817  vertex_to_boundary_id_map_1d = std_cxx14::make_unique<std::map<unsigned int, types::boundary_id>> ();
8818  vertex_to_manifold_id_map_1d = std_cxx14::make_unique<std::map<unsigned int, types::manifold_id>> ();
8819  }
8820 
8821  // connect the any_change signal to the other top level signals
8822  signals.create.connect (signals.any_change);
8824  signals.clear.connect (signals.any_change);
8826 }
8827 
8828 
8829 
8830 template <int dim, int spacedim>
8833 :
8834 Subscriptor(std::move(tria)),
8835  smooth_grid(tria.smooth_grid),
8836  periodic_face_pairs_level_0(std::move(tria.periodic_face_pairs_level_0)),
8837  periodic_face_map(std::move(tria.periodic_face_map)),
8838  levels(std::move(tria.levels)),
8839  faces(std::move(tria.faces)),
8840  vertices(std::move(tria.vertices)),
8841  vertices_used(std::move(tria.vertices_used)),
8842  manifold(std::move(tria.manifold)),
8843  anisotropic_refinement(tria.anisotropic_refinement),
8844  check_for_distorted_cells(tria.check_for_distorted_cells),
8845  number_cache(std::move(tria.number_cache)),
8846  vertex_to_boundary_id_map_1d(std::move(tria.vertex_to_boundary_id_map_1d)),
8847  vertex_to_manifold_id_map_1d(std::move(tria.vertex_to_manifold_id_map_1d))
8848 {
8850 }
8851 
8852 
8853 template <int dim, int spacedim>
8856 {
8857  Subscriptor::operator=(std::move(tria));
8858 
8859  smooth_grid = tria.smooth_grid;
8860  periodic_face_pairs_level_0 = std::move(tria.periodic_face_pairs_level_0);
8861  periodic_face_map = std::move(tria.periodic_face_map);
8862  levels = std::move(tria.levels);
8863  faces = std::move(tria.faces);
8864  vertices = std::move(tria.vertices);
8865  vertices_used = std::move(tria.vertices_used);
8866  manifold = std::move(tria.manifold);
8867  anisotropic_refinement = tria.anisotropic_refinement;
8868  number_cache = tria.number_cache;
8869  vertex_to_boundary_id_map_1d = std::move(tria.vertex_to_boundary_id_map_1d);
8870  vertex_to_manifold_id_map_1d = std::move(tria.vertex_to_manifold_id_map_1d);
8871 
8873 
8874  return *this;
8875 }
8876 
8877 
8878 
8879 template <int dim, int spacedim>
8881 {
8882  // notify listeners that the triangulation is going down...
8883  try
8884  {
8885  signals.clear();
8886  }
8887  catch (...)
8888  {}
8889 
8890  levels.clear ();
8891 
8892  // the vertex_to_boundary_id_map_1d field should be unused except in
8893  // 1d. double check this here, as destruction is a good place to
8894  // ensure that what we've done over the course of the lifetime of
8895  // this object makes sense
8896  AssertNothrow ((dim == 1)
8897  ||
8898  (vertex_to_boundary_id_map_1d == nullptr),
8899  ExcInternalError());
8900 
8901  // the vertex_to_manifold_id_map_1d field should be also unused
8902  // except in 1d. check this as well
8903  AssertNothrow ((dim == 1)
8904  ||
8905  (vertex_to_manifold_id_map_1d == nullptr),
8906  ExcInternalError());
8907 }
8908 
8909 
8910 
8911 template <int dim, int spacedim>
8913 {
8914  // notify listeners that the triangulation is going down...
8915  signals.clear();
8916 
8917  // ...and then actually clear all content of it
8918  clear_despite_subscriptions();
8919  periodic_face_pairs_level_0.clear();
8920  periodic_face_map.clear();
8921 }
8922 
8923 
8924 
8925 template <int dim, int spacedim>
8926 void
8928 {
8929  Assert (n_levels() == 0,
8930  ExcTriangulationNotEmpty (vertices.size(), levels.size()));
8931  smooth_grid=mesh_smoothing;
8932 }
8933 
8934 
8935 
8936 template <int dim, int spacedim>
8939 {
8940  return smooth_grid;
8941 }
8942 
8943 
8944 
8945 template <int dim, int spacedim>
8946 void
8948  const Boundary<dim, spacedim> &boundary_object)
8949 {
8950  set_manifold(m_number, boundary_object);
8951 }
8952 
8953 template <int dim, int spacedim>
8954 void
8956  const Manifold<dim, spacedim> &manifold_object)
8957 {
8960 
8961  manifold[m_number] = manifold_object.clone();
8962 }
8963 
8964 
8965 template <int dim, int spacedim>
8966 void
8968 {
8969  set_manifold(m_number);
8970 }
8971 
8972 
8973 template <int dim, int spacedim>
8974 void
8976 {
8977  reset_manifold(m_number);
8978 }
8979 
8980 
8981 template<int dim, int spacedim>
8983 {
8986 
8987  //delete the entry located at number.
8988  manifold.erase(m_number);
8989 }
8990 
8991 
8992 template<int dim, int spacedim>
8994 {
8995  manifold.clear();
8996 }
8997 
8998 
8999 template <int dim, int spacedim>
9000 void
9002 {
9003  Assert(n_cells()>0,
9004  ExcMessage("Error: set_all_manifold_ids() can not be called on an empty Triangulation."));
9005 
9007  cell=this->begin_active(), endc=this->end();
9008 
9009  for (; cell != endc; ++cell)
9010  cell->set_all_manifold_ids(m_number);
9011 }
9012 
9013 
9014 template <int dim, int spacedim>
9015 void
9017 {
9018  Assert(n_cells()>0,
9019  ExcMessage("Error: set_all_manifold_ids_on_boundary() can not be called on an empty Triangulation."));
9020 
9022  cell=this->begin_active(), endc=this->end();
9023 
9024  for (; cell != endc; ++cell)
9025  for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
9026  if (cell->face(f)->at_boundary())
9027  cell->face(f)->set_all_manifold_ids(m_number);
9028 }
9029 
9030 
9031 template <int dim, int spacedim>
9032 void
9034  const types::manifold_id m_number)
9035 {
9036  Assert(n_cells()>0,
9037  ExcMessage("Error: set_all_manifold_ids_on_boundary() can not be called on an empty Triangulation."));
9038 
9039  bool boundary_found = false;
9041  cell=this->begin_active(), endc=this->end();
9042 
9043  for (; cell != endc; ++cell)
9044  {
9045  // loop on faces
9046  for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
9047  if (cell->face(f)->at_boundary() && cell->face(f)->boundary_id()==b_id)
9048  {
9049  boundary_found = true;
9050  cell->face(f)->set_manifold_id(m_number);
9051  }
9052 
9053  // loop on edges if dim >= 3
9054  if (dim>=3)
9055  for (unsigned int e=0; e<GeometryInfo<dim>::lines_per_cell; ++e)
9056  if (cell->line(e)->at_boundary() && cell->line(e)->boundary_id()==b_id)
9057  {
9058  boundary_found = true;
9059  cell->line(e)->set_manifold_id(m_number);
9060  }
9061  }
9062 
9063  (void)boundary_found;
9064  Assert(boundary_found, ExcBoundaryIdNotFound(b_id));
9065 }
9066 
9067 
9068 template <int dim, int spacedim>
9069 const Boundary<dim,spacedim> &
9071 {
9072  const Boundary<dim, spacedim> *man =
9073  dynamic_cast<const Boundary<dim, spacedim> *>(&get_manifold(m_number));
9074  Assert(man != nullptr,
9075  ExcMessage("You tried to get a Boundary, but I only have a Manifold."));
9076 
9077  return *man;
9078 }
9079 
9080 
9081 template <int dim, int spacedim>
9082 const Manifold<dim,spacedim> &
9084 {
9085  //look, if there is a manifold stored at
9086  //manifold_id number.
9087  const auto it = manifold.find(m_number);
9088 
9089  if (it != manifold.end())
9090  {
9091  //if we have found an entry, return it
9092  return *(it->second);
9093  }
9094 
9095  // if we have not found an entry connected with number, we return
9096  // the default (flat) manifold
9097  return internal::TriangulationImplementation::get_default_flat_manifold<dim,spacedim>();
9098 }
9099 
9100 
9101 
9102 
9103 template <int dim, int spacedim>
9104 std::vector<types::boundary_id>
9106 {
9107  // in 1d, we store a map of all used boundary indicators. use it for
9108  // our purposes
9109  if (dim == 1)
9110  {
9111  std::vector<types::boundary_id> boundary_ids;
9112  for (std::map<unsigned int, types::boundary_id>::const_iterator
9113  p = vertex_to_boundary_id_map_1d->begin();
9114  p != vertex_to_boundary_id_map_1d->end();
9115  ++p)
9116  boundary_ids.push_back (p->second);
9117 
9118  return boundary_ids;
9119  }
9120  else
9121  {
9122  std::set<types::boundary_id> b_ids;
9123  active_cell_iterator cell=begin_active();
9124  for (; cell!=end(); ++cell)
9125  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
9126  if (cell->at_boundary(face))
9127  b_ids.insert(cell->face(face)->boundary_id());
9128  std::vector<types::boundary_id> boundary_ids(b_ids.begin(), b_ids.end());
9129  return boundary_ids;
9130  }
9131 }
9132 
9133 
9134 
9135 template <int dim, int spacedim>
9136 std::vector<types::manifold_id>
9138 {
9139  std::set<types::manifold_id> m_ids;
9140  active_cell_iterator cell=begin_active();
9141  for (; cell!=end(); ++cell)
9142  {
9143  m_ids.insert(cell->manifold_id());
9144  if (dim>1)
9145  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
9146  if (cell->at_boundary(face))
9147  m_ids.insert(cell->face(face)->manifold_id());
9148  }
9149  std::vector<types::manifold_id> manifold_indicators(m_ids.begin(), m_ids.end());
9150  return manifold_indicators;
9151 }
9152 
9153 /*-----------------------------------------------------------------*/
9154 
9155 
9156 template <int dim, int spacedim>
9157 void
9160 {
9161  Assert ((vertices.size() == 0) &&
9162  (levels.size () == 0) &&
9163  (faces == nullptr),
9164  ExcTriangulationNotEmpty(vertices.size(), levels.size()));
9165  Assert ((other_tria.levels.size() != 0) &&
9166  (other_tria.vertices.size() != 0) &&
9167  (dim == 1 || other_tria.faces != nullptr),
9168  ExcMessage("When calling Triangulation::copy_triangulation(), "
9169  "the target triangulation must be empty but the source "
9170  "triangulation (the argument to this function) must contain "
9171  "something. Here, it seems like the source does not "
9172  "contain anything at all."));
9173 
9174 
9175  // copy normal elements
9176  vertices = other_tria.vertices;
9177  vertices_used = other_tria.vertices_used;
9178  anisotropic_refinement = other_tria.anisotropic_refinement;
9179  smooth_grid = other_tria.smooth_grid;
9180 
9181  if (dim > 1)
9182  faces = std_cxx14::make_unique<internal::TriangulationImplementation::TriaFaces<dim>> (*other_tria.faces);
9183 
9184  auto bdry_iterator = other_tria.manifold.begin();
9185  for (; bdry_iterator != other_tria.manifold.end() ; ++bdry_iterator)
9186  manifold[bdry_iterator->first] = bdry_iterator->second->clone();
9187 
9188 
9189  levels.reserve (other_tria.levels.size());
9190  for (unsigned int level=0; level<other_tria.levels.size(); ++level)
9191  levels.push_back (std_cxx14::make_unique<internal::TriangulationImplementation::TriaLevel<dim>>
9192  (*other_tria.levels[level]));
9193 
9194  number_cache = other_tria.number_cache;
9195 
9196  if (dim == 1)
9197  {
9198  vertex_to_boundary_id_map_1d = std_cxx14::make_unique<std::map<unsigned int, types::boundary_id>>
9199  (*other_tria.vertex_to_boundary_id_map_1d);
9200 
9201  vertex_to_manifold_id_map_1d = std_cxx14::make_unique<std::map<unsigned int, types::manifold_id>>
9202  (*other_tria.vertex_to_manifold_id_map_1d);
9203  }
9204 
9205  // inform those who are listening on other_tria of the copy operation
9206  other_tria.signals.copy (*this);
9207  // also inform all listeners of the current triangulation that the
9208  // triangulation has been created
9209  signals.create();
9210 
9211  // note that we need not copy the
9212  // subscriptor!
9213 }
9214 
9215 
9216 
9217 template <int dim, int spacedim>
9218 void
9221  const std::vector<CellData<dim> > &cells,
9222  const SubCellData &subcelldata)
9223 {
9224  std::vector<CellData<dim> > reordered_cells (cells); // NOLINT
9225  SubCellData reordered_subcelldata (subcelldata); // NOLINT
9226 
9227  // in-place reordering of data
9228  reorder_compatibility (reordered_cells, reordered_subcelldata);
9229 
9230  // now create triangulation from
9231  // reordered data
9232  create_triangulation(v, reordered_cells, reordered_subcelldata);
9233 }
9234 
9235 
9236 
9237 template <int dim, int spacedim>
9238 void
9240 create_triangulation (const std::vector<Point<spacedim> > &v,
9241  const std::vector<CellData<dim> > &cells,
9242  const SubCellData &subcelldata)
9243 {
9244  Assert ((vertices.size() == 0) &&
9245  (levels.size () == 0) &&
9246  (faces == nullptr),
9247  ExcTriangulationNotEmpty(vertices.size(), levels.size()));
9248  // check that no forbidden arrays
9249  // are used
9250  Assert (subcelldata.check_consistency(dim), ExcInternalError());
9251 
9252  // try to create a triangulation; if this fails, we still want to
9253  // throw an exception but if we just do so we'll get into trouble
9254  // because sometimes other objects are already attached to it:
9255  try
9256  {
9258  }
9259  catch (...)
9260  {
9261  clear_despite_subscriptions();
9262  throw;
9263  }
9264 
9265  // update our counts of the various elements of a triangulation, and set
9266  // active_cell_indices of all cells
9268  ::compute_number_cache (*this, levels.size(), number_cache);
9269  reset_active_cell_indices ();
9270 
9271  // now verify that there are indeed no distorted cells. as per the
9272  // documentation of this class, we first collect all distorted cells
9273  // and then throw an exception if there are any
9274  if (check_for_distorted_cells == true)
9275  {
9276  DistortedCellList distorted_cells = collect_distorted_coarse_cells (*this);
9277  // throw the array (and fill the various location fields) if
9278  // there are distorted cells. otherwise, just fall off the end
9279  // of the function
9280  AssertThrow (distorted_cells.distorted_cells.size() == 0,
9281  distorted_cells);
9282  }
9283 
9284 
9285  /*
9286  When the triangulation is a manifold (dim < spacedim), the normal field
9287  provided from the map class depends on the order of the vertices.
9288  It may happen that this normal field is discontinuous.
9289  The following code takes care that this is not the case by setting the
9290  cell direction flag on those cell that produce the wrong orientation.
9291 
9292  To determine if 2 neighbours have the same or opposite orientation
9293  we use a table of truth.
9294  Its entries are indexes by the local indices of the common face.
9295  For example if two elements share a face, and this face is
9296  face 0 for element 0 and face 1 for element 1, then
9297  table(0,1) will tell whether the orientation are the same (true) or
9298  opposite (false).
9299 
9300  Even though there may be a combinatorial/graph theory argument to get
9301  this table in any dimension, I tested by hand all the different possible
9302  cases in 1D and 2D to generate the table.
9303 
9304  Assuming that a surface respects the standard orientation for 2d meshes,
9305  the tables of truth are symmetric and their true values are the following
9306  1D curves: (0,1)
9307  2D surface: (0,1),(0,2),(1,3),(2,3)
9308 
9309  We store this data using an n_faces x n_faces full matrix, which is actually
9310  much bigger than the minimal data required, but it makes the code more readable.
9311 
9312  */
9313  if (dim < spacedim)
9314  {
9317  switch (dim)
9318  {
9319  case 1:
9320  {
9321  bool values [][2] = {{false,true},
9322  {true,false}
9323  };
9324  for (unsigned int i=0; i< GeometryInfo< dim >::faces_per_cell; ++i)
9325  for (unsigned int j=0; j< GeometryInfo< dim >::faces_per_cell; ++j)
9326  correct(i,j) = ( values[i][j]);
9327  break;
9328  }
9329  case 2:
9330  {
9331  bool values [][4]= {{false,true,true, false},
9332  {true,false,false, true },
9333  {true,false,false, true },
9334  {false,true,true, false}
9335  };
9336  for (unsigned int i=0; i< GeometryInfo< dim >::faces_per_cell; ++i)
9337  for (unsigned int j=0; j< GeometryInfo< dim >::faces_per_cell; ++j)
9338  correct(i,j) = ( values[i][j]);
9339  break;
9340  }
9341  default:
9342  Assert (false, ExcNotImplemented());
9343  }
9344 
9345 
9346  std::list<active_cell_iterator> this_round, next_round;
9347  active_cell_iterator neighbor;
9348 
9349  this_round.push_back (begin_active());
9350  begin_active()->set_direction_flag (true);
9351  begin_active()->set_user_flag ();
9352 
9353  while (this_round.size() > 0)
9354  {
9355  for ( typename std::list<active_cell_iterator>::iterator cell = this_round.begin();
9356  cell != this_round.end(); ++cell)
9357  {
9358  for (unsigned int i = 0; i < GeometryInfo< dim >::faces_per_cell; ++i)
9359  {
9360  if ( !((*cell)->face(i)->at_boundary()) )
9361  {
9362  neighbor = (*cell)->neighbor(i);
9363 
9364  unsigned int cf = (*cell)->face_index(i);
9365  unsigned int j = 0;
9366  while (neighbor->face_index(j) != cf)
9367  {
9368  ++j;
9369  }
9370 
9371 
9372  // If we already saw this guy, check that everything is fine
9373  if (neighbor->user_flag_set())
9374  {
9375  // If we have visited this guy, then the ordering and the orientation should
9376  // agree
9377  Assert(!(correct(i,j) ^ (neighbor->direction_flag() == (*cell)->direction_flag() )),
9378  ExcNonOrientableTriangulation());
9379  }
9380  else
9381  {
9382  next_round.push_back (neighbor);
9383  neighbor->set_user_flag ();
9384  if ( (correct(i,j) ^ ( neighbor->direction_flag() == (*cell)->direction_flag() ) ) )
9385  neighbor->set_direction_flag(!neighbor->direction_flag());
9386  }
9387  }
9388  }
9389  }
9390 
9391  // Before we quit let's check
9392  // that if the triangulation
9393  // is disconnected that we
9394  // still get all cells
9395  if (next_round.size() == 0)
9396  for (active_cell_iterator cell = begin_active();
9397  cell != end(); ++cell)
9398  if (cell->user_flag_set() == false)
9399  {
9400  next_round.push_back (cell);
9401  cell->set_direction_flag (true);
9402  cell->set_user_flag ();
9403  break;
9404  }
9405 
9406  this_round = next_round;
9407  next_round.clear();
9408  }
9409  }
9410 
9411  // inform all listeners that the triangulation has been created
9412  signals.create();
9413 }
9414 
9415 
9416 
9417 
9418 template <int dim, int spacedim>
9419 void
9422 {
9423  AssertThrow (dim+1 == spacedim, ExcMessage ("Only works for dim == spacedim-1"));
9424  for (active_cell_iterator cell = begin_active();
9425  cell != end(); ++cell)
9426  cell->set_direction_flag (!cell->direction_flag());
9427 }
9428 
9429 
9430 
9431 template <int dim, int spacedim>
9433 {
9434  Assert(n_cells()>0, ExcMessage("Error: An empty Triangulation can not be refined."));
9435  active_cell_iterator cell = begin_active(),
9436  endc = end();
9437 
9438  for (; cell != endc; ++cell)
9439  {
9440  cell->clear_coarsen_flag();
9441  cell->set_refine_flag ();
9442  }
9443 }
9444 
9445 
9446 
9447 template <int dim, int spacedim>
9448 void Triangulation<dim, spacedim>::refine_global (const unsigned int times)
9449 {
9450  for (unsigned int i=0; i<times; ++i)
9451  {
9452  set_all_refine_flags();
9453  execute_coarsening_and_refinement ();
9454  }
9455 }
9456 
9457 
9458 
9459 /*-------------------- refine/coarsen flags -------------------------*/
9460 
9461 
9462 
9463 template <int dim, int spacedim>
9464 void Triangulation<dim, spacedim>::save_refine_flags (std::vector<bool> &v) const
9465 {
9466  v.resize (dim*n_active_cells(), false);
9467  std::vector<bool>::iterator i = v.begin();
9468  active_cell_iterator cell = begin_active(),
9469  endc = end();
9470  for (; cell!=endc; ++cell)
9471  for (unsigned int j=0; j<dim; ++j,++i)
9472  if (cell->refine_flag_set() & (1<<j) )
9473  *i = true;
9474 
9475  Assert (i == v.end(), ExcInternalError());
9476 }
9477 
9478 
9479 
9480 template <int dim, int spacedim>
9482 {
9483  std::vector<bool> v;
9484  save_refine_flags (v);
9485  write_bool_vector (mn_tria_refine_flags_begin, v, mn_tria_refine_flags_end,
9486  out);
9487 }
9488 
9489 
9490 
9491 template <int dim, int spacedim>
9493 {
9494  std::vector<bool> v;
9495  read_bool_vector (mn_tria_refine_flags_begin, v, mn_tria_refine_flags_end,
9496  in);
9497  load_refine_flags (v);
9498 }
9499 
9500 
9501 
9502 template <int dim, int spacedim>
9503 void Triangulation<dim, spacedim>::load_refine_flags (const std::vector<bool> &v)
9504 {
9505  AssertThrow (v.size() == dim*n_active_cells(), ExcGridReadError());
9506 
9507  active_cell_iterator cell = begin_active(),
9508  endc = end();
9509  std::vector<bool>::const_iterator i = v.begin();
9510  for (; cell!=endc; ++cell)
9511  {
9512  unsigned int ref_case=0;
9513 
9514  for (unsigned int j=0; j<dim; ++j, ++i)
9515  if (*i == true)
9516  ref_case+=1<<j;
9518  ExcGridReadError());
9519  if (ref_case>0)
9520  cell->set_refine_flag(RefinementCase<dim>(ref_case));
9521  else
9522  cell->clear_refine_flag();
9523  }
9524 
9525  Assert (i == v.end(), ExcInternalError());
9526 }
9527 
9528 
9529 
9530 template <int dim, int spacedim>
9531 void Triangulation<dim, spacedim>::save_coarsen_flags (std::vector<bool> &v) const
9532 {
9533  v.resize (n_active_cells(), false);
9534  std::vector<bool>::iterator i = v.begin();
9535  active_cell_iterator cell = begin_active(),
9536  endc = end();
9537  for (; cell!=endc; ++cell, ++i)
9538  *i = cell->coarsen_flag_set();
9539 
9540  Assert (i == v.end(), ExcInternalError());
9541 }
9542 
9543 
9544 
9545 template <int dim, int spacedim>
9547 {
9548  std::vector<bool> v;
9549  save_coarsen_flags (v);
9550  write_bool_vector (mn_tria_coarsen_flags_begin, v, mn_tria_coarsen_flags_end,
9551  out);
9552 }
9553 
9554 
9555 
9556 template <int dim, int spacedim>
9558 {
9559  std::vector<bool> v;
9560  read_bool_vector (mn_tria_coarsen_flags_begin, v, mn_tria_coarsen_flags_end,
9561  in);
9562  load_coarsen_flags (v);
9563 }
9564 
9565 
9566 
9567 template <int dim, int spacedim>
9568 void Triangulation<dim, spacedim>::load_coarsen_flags (const std::vector<bool> &v)
9569 {
9570  Assert (v.size() == n_active_cells(), ExcGridReadError());
9571 
9572  active_cell_iterator cell = begin_active(),
9573  endc = end();
9574  std::vector<bool>::const_iterator i = v.begin();
9575  for (; cell!=endc; ++cell, ++i)
9576  if (*i == true)
9577  cell->set_coarsen_flag();
9578  else
9579  cell->clear_coarsen_flag();
9580 
9581  Assert (i == v.end(), ExcInternalError());
9582 }
9583 
9584 
9585 template <int dim, int spacedim>
9587 {
9588  return anisotropic_refinement;
9589 }
9590 
9591 
9592 
9593 /*-------------------- user data/flags -------------------------*/
9594 
9595 
9596 namespace
9597 {
9598  // clear user data of cells
9599  template <int dim>
9600  void clear_user_data (std::vector<std::unique_ptr<internal::TriangulationImplementation::TriaLevel<dim>>> &levels)
9601  {
9602  for (unsigned int level=0; level<levels.size(); ++level)
9603  levels[level]->cells.clear_user_data();
9604  }
9605 
9606 
9607  // clear user data of faces
9609  {
9610  // nothing to do in 1d
9611  }
9612 
9613 
9614  void clear_user_data (internal::TriangulationImplementation::TriaFaces<2> *faces)
9615  {
9616  faces->lines.clear_user_data();
9617  }
9618 
9619 
9620  void clear_user_data (internal::TriangulationImplementation::TriaFaces<3> *faces)
9621  {
9622  faces->lines.clear_user_data();
9623  faces->quads.clear_user_data();
9624  }
9625 }
9626 
9627 
9628 template <int dim, int spacedim>
9630 {
9631  // let functions in anonymous namespace do their work
9632  ::clear_user_data (levels);
9633  ::clear_user_data (faces.get());
9634 }
9635 
9636 
9637 
9638 namespace
9639 {
9640  void clear_user_flags_line (std::vector<std::unique_ptr<internal::TriangulationImplementation::TriaLevel<1>>> &levels,
9642  {
9643  for (unsigned int level=0; level<levels.size(); ++level)
9644  levels[level]->cells.clear_user_flags();
9645  }
9646 
9647  template <int dim>
9648  void clear_user_flags_line (std::vector<std::unique_ptr<internal::TriangulationImplementation::TriaLevel<dim>>> &,
9650  {
9651  faces->lines.clear_user_flags();
9652  }
9653 }
9654 
9655 
9656 template <int dim, int spacedim>
9658 {
9659  ::clear_user_flags_line (levels, faces.get());
9660 }
9661 
9662 
9663 
9664 namespace
9665 {
9666  void clear_user_flags_quad (std::vector<std::unique_ptr<internal::TriangulationImplementation::TriaLevel<1>>> &,
9668  {
9669  // nothing to do in 1d
9670  }
9671 
9672  void clear_user_flags_quad (std::vector<std::unique_ptr<internal::TriangulationImplementation::TriaLevel<2>>> &levels,
9674  {
9675  for (unsigned int level=0; level<levels.size(); ++level)
9676  levels[level]->cells.clear_user_flags();
9677  }
9678 
9679  template <int dim>
9680  void clear_user_flags_quad (std::vector<std::unique_ptr<internal::TriangulationImplementation::TriaLevel<dim>>> &,
9682  {
9683  faces->quads.clear_user_flags();
9684  }
9685 }
9686 
9687 
9688 template <int dim, int spacedim>
9690 {
9691  ::clear_user_flags_quad (levels, faces.get());
9692 }
9693 
9694 
9695 
9696 namespace
9697 {
9698  void clear_user_flags_hex (std::vector<std::unique_ptr<internal::TriangulationImplementation::TriaLevel<1>>> &,
9700  {
9701  // nothing to do in 1d
9702  }
9703 
9704 
9705  void clear_user_flags_hex (std::vector<std::unique_ptr<internal::TriangulationImplementation::TriaLevel<2>>> &,
9707  {
9708  // nothing to do in 2d
9709  }
9710 
9711  void clear_user_flags_hex (std::vector<std::unique_ptr<internal::TriangulationImplementation::TriaLevel<3>>> &levels,
9713  {
9714  for (unsigned int level=0; level<levels.size(); ++level)
9715  levels[level]->cells.clear_user_flags();
9716  }
9717 }
9718 
9719 
9720 template <int dim, int spacedim>
9722 {
9723  ::clear_user_flags_hex (levels, faces.get());
9724 }
9725 
9726 
9727 
9728 template <int dim, int spacedim>
9730 {
9731  clear_user_flags_line ();
9732  clear_user_flags_quad ();
9733  clear_user_flags_hex ();
9734 }
9735 
9736 
9737 
9738 template <int dim, int spacedim>
9740 {
9741  save_user_flags_line (out);
9742 
9743  if (dim>=2)
9744  save_user_flags_quad (out);
9745 
9746  if (dim>=3)
9747  save_user_flags_hex (out);
9748 
9749  if (dim >= 4)
9750  Assert (false, ExcNotImplemented());
9751 }
9752 
9753 
9754 
9755 template <int dim, int spacedim>
9756 void Triangulation<dim, spacedim>::save_user_flags (std::vector<bool> &v) const
9757 {
9758  // clear vector and append
9759  // all the stuff later on
9760  v.clear ();
9761 
9762  std::vector<bool> tmp;
9763 
9764  save_user_flags_line (tmp);
9765  v.insert (v.end(), tmp.begin(), tmp.end());
9766 
9767  if (dim >= 2)
9768  {
9769  save_user_flags_quad (tmp);
9770  v.insert (v.end(), tmp.begin(), tmp.end());
9771  }
9772 
9773  if (dim >= 3)
9774  {
9775  save_user_flags_hex (tmp);
9776  v.insert (v.end(), tmp.begin(), tmp.end());
9777  }
9778 
9779  if (dim >= 4)
9780  Assert (false, ExcNotImplemented());
9781 }
9782 
9783 
9784 
9785 template <int dim, int spacedim>
9787 {
9788  load_user_flags_line (in);
9789 
9790  if (dim>=2)
9791  load_user_flags_quad (in);
9792 
9793  if (dim>=3)
9794  load_user_flags_hex (in);
9795 
9796  if (dim >= 4)
9797  Assert (false, ExcNotImplemented());
9798 }
9799 
9800 
9801 
9802 template <int dim, int spacedim>
9803 void Triangulation<dim, spacedim>::load_user_flags (const std::vector<bool> &v)
9804 {
9805  Assert (v.size() == n_lines()+n_quads()+n_hexs(), ExcInternalError());
9806  std::vector<bool> tmp;
9807 
9808  // first extract the flags
9809  // belonging to lines
9810  tmp.insert (tmp.end(),
9811  v.begin(), v.begin()+n_lines());
9812  // and set the lines
9813  load_user_flags_line (tmp);
9814 
9815  if (dim >= 2)
9816  {
9817  tmp.clear ();
9818  tmp.insert (tmp.end(),
9819  v.begin()+n_lines(), v.begin()+n_lines()+n_quads());
9820  load_user_flags_quad (tmp);
9821  }
9822 
9823  if (dim >= 3)
9824  {
9825  tmp.clear();
9826  tmp.insert (tmp.end(),
9827  v.begin()+n_lines()+n_quads(), v.begin()+n_lines()+n_quads()+n_hexs());
9828  load_user_flags_hex (tmp);
9829  }
9830 
9831  if (dim >= 4)
9832  Assert (false, ExcNotImplemented());
9833 }
9834 
9835 
9836 
9837 template <int dim, int spacedim>
9839 {
9840  v.resize (n_lines(), false);
9841  std::vector<bool>::iterator i = v.begin();
9842  line_iterator line = begin_line(),
9843  endl = end_line();
9844  for (; line!=endl; ++line, ++i)
9845  *i = line->user_flag_set();
9846 
9847  Assert (i == v.end(), ExcInternalError());
9848 }
9849 
9850 
9851 
9852 template <int dim, int spacedim>
9854 {
9855  std::vector<bool> v;
9856  save_user_flags_line (v);
9857  write_bool_vector (mn_tria_line_user_flags_begin, v, mn_tria_line_user_flags_end,
9858  out);
9859 }
9860 
9861 
9862 
9863 template <int dim, int spacedim>
9865 {
9866  std::vector<bool> v;
9867  read_bool_vector (mn_tria_line_user_flags_begin, v, mn_tria_line_user_flags_end,
9868  in);
9869  load_user_flags_line (v);
9870 }
9871 
9872 
9873 
9874 template <int dim, int spacedim>
9876 {
9877  Assert (v.size() == n_lines(), ExcGridReadError());
9878 
9879  line_iterator line = begin_line(),
9880  endl = end_line();
9881  std::vector<bool>::const_iterator i = v.begin();
9882  for (; line!=endl; ++line, ++i)
9883  if (*i == true)
9884  line->set_user_flag();
9885  else
9886  line->clear_user_flag();
9887 
9888  Assert (i == v.end(), ExcInternalError());
9889 }
9890 
9891 
9892 namespace
9893 {
9894  template <typename Iterator>
9895  bool get_user_flag (const Iterator &i)
9896  {
9897  return i->user_flag_set();
9898  }
9899 
9900 
9901 
9902  template <int structdim, int dim, int spacedim>
9903  bool get_user_flag (const TriaIterator<InvalidAccessor<structdim,dim,spacedim> > &)
9904  {
9905  Assert (false, ExcInternalError());
9906  return false;
9907  }
9908 
9909 
9910 
9911  template <typename Iterator>
9912  void set_user_flag (const Iterator &i)
9913  {
9914  i->set_user_flag();
9915  }
9916 
9917 
9918 
9919  template <int structdim, int dim, int spacedim>
9920  void set_user_flag (const TriaIterator<InvalidAccessor<structdim,dim,spacedim> > &)
9921  {
9922  Assert (false, ExcInternalError());
9923  }
9924 
9925 
9926 
9927  template <typename Iterator>
9928  void clear_user_flag (const Iterator &i)
9929  {
9930  i->clear_user_flag();
9931  }
9932 
9933 
9934 
9935  template <int structdim, int dim, int spacedim>
9936  void clear_user_flag (const TriaIterator<InvalidAccessor<structdim,dim,spacedim> > &)
9937  {
9938  Assert (false, ExcInternalError());
9939  }
9940 }
9941 
9942 
9943 template <int dim, int spacedim>
9945 {
9946  v.resize (n_quads(), false);
9947 
9948  if (dim >= 2)
9949  {
9950  std::vector<bool>::iterator i = v.begin();
9951  quad_iterator quad = begin_quad(),
9952  endq = end_quad();
9953  for (; quad!=endq; ++quad, ++i)
9954  *i = get_user_flag (quad);
9955 
9956  Assert (i == v.end(), ExcInternalError());
9957  }
9958 }
9959 
9960 
9961 
9962 template <int dim, int spacedim>
9964 {
9965  std::vector<bool> v;
9966  save_user_flags_quad (v);
9967  write_bool_vector (mn_tria_quad_user_flags_begin, v, mn_tria_quad_user_flags_end,
9968  out);
9969 }
9970 
9971 
9972 
9973 template <int dim, int spacedim>
9975 {
9976  std::vector<bool> v;
9977  read_bool_vector (mn_tria_quad_user_flags_begin, v, mn_tria_quad_user_flags_end,
9978  in);
9979  load_user_flags_quad (v);
9980 }
9981 
9982 
9983 
9984 template <int dim, int spacedim>
9986 {
9987  Assert (v.size() == n_quads(), ExcGridReadError());
9988 
9989  if (dim >= 2)
9990  {
9991  quad_iterator quad = begin_quad(),
9992  endq = end_quad();
9993  std::vector<bool>::const_iterator i = v.begin();
9994  for (; quad!=endq; ++quad, ++i)
9995  if (*i == true)
9996  set_user_flag(quad);
9997  else
9998  clear_user_flag(quad);
9999 
10000  Assert (i == v.end(), ExcInternalError());
10001  }
10002 }
10003 
10004 
10005 
10006 template <int dim, int spacedim>
10007 void Triangulation<dim, spacedim>::save_user_flags_hex (std::vector<bool> &v) const
10008 {
10009  v.resize (n_hexs(), false);
10010 
10011  if (dim >= 3)
10012  {
10013  std::vector<bool>::iterator i = v.begin();
10014  hex_iterator hex = begin_hex(),
10015  endh = end_hex();
10016  for (; hex!=endh; ++hex, ++i)
10017  *i = get_user_flag (hex);
10018 
10019  Assert (i == v.end(), ExcInternalError());
10020  }
10021 }
10022 
10023 
10024 
10025 template <int dim, int spacedim>
10027 {
10028  std::vector<bool> v;
10029  save_user_flags_hex (v);
10030  write_bool_vector (mn_tria_hex_user_flags_begin, v, mn_tria_hex_user_flags_end,
10031  out);
10032 }
10033 
10034 
10035 
10036 template <int dim, int spacedim>
10038 {
10039  std::vector<bool> v;
10040  read_bool_vector (mn_tria_hex_user_flags_begin, v, mn_tria_hex_user_flags_end,
10041  in);
10042  load_user_flags_hex (v);
10043 }
10044 
10045 
10046 
10047 template <int dim, int spacedim>
10048 void Triangulation<dim, spacedim>::load_user_flags_hex (const std::vector<bool> &v)
10049 {
10050  Assert (v.size() == n_hexs(), ExcGridReadError());
10051 
10052  if (dim >= 3)
10053  {
10054  hex_iterator hex = begin_hex(),
10055  endh = end_hex();
10056  std::vector<bool>::const_iterator i = v.begin();
10057  for (; hex!=endh; ++hex, ++i)
10058  if (*i == true)
10059  set_user_flag(hex);
10060  else
10061  clear_user_flag(hex);
10062 
10063  Assert (i == v.end(), ExcInternalError());
10064  }
10065 }
10066 
10067 
10068 
10069 template <int dim, int spacedim>
10070 void Triangulation<dim, spacedim>::save_user_indices (std::vector<unsigned int> &v) const
10071 {
10072  // clear vector and append all the
10073  // stuff later on
10074  v.clear ();
10075 
10076  std::vector<unsigned int> tmp;
10077 
10078  save_user_indices_line (tmp);
10079  v.insert (v.end(), tmp.begin(), tmp.end());
10080 
10081  if (dim >= 2)
10082  {
10083  save_user_indices_quad (tmp);
10084  v.insert (v.end(), tmp.begin(), tmp.end());
10085  }
10086 
10087  if (dim >= 3)
10088  {
10089  save_user_indices_hex (tmp);
10090  v.insert (v.end(), tmp.begin(), tmp.end());
10091  }
10092 
10093  if (dim >= 4)
10094  Assert (false, ExcNotImplemented());
10095 }
10096 
10097 
10098 
10099 template <int dim, int spacedim>
10100 void Triangulation<dim, spacedim>::load_user_indices (const std::vector<unsigned int> &v)
10101 {
10102  Assert (v.size() == n_lines()+n_quads()+n_hexs(), ExcInternalError());
10103  std::vector<unsigned int> tmp;
10104 
10105  // first extract the indices
10106  // belonging to lines
10107  tmp.insert (tmp.end(),
10108  v.begin(), v.begin()+n_lines());
10109  // and set the lines
10110  load_user_indices_line (tmp);
10111 
10112  if (dim >= 2)
10113  {
10114  tmp.clear ();
10115  tmp.insert (tmp.end(),
10116  v.begin()+n_lines(), v.begin()+n_lines()+n_quads());
10117  load_user_indices_quad (tmp);
10118  }
10119 
10120  if (dim >= 3)
10121  {
10122  tmp.clear ();
10123  tmp.insert (tmp.end(),
10124  v.begin()+n_lines()+n_quads(), v.begin()+n_lines()+n_quads()+n_hexs());
10125  load_user_indices_hex (tmp);
10126  }
10127 
10128  if (dim >= 4)
10129  Assert (false, ExcNotImplemented());
10130 }
10131 
10132 
10133 
10134 namespace
10135 {
10136  template <typename Iterator>
10137  unsigned int get_user_index (const Iterator &i)
10138  {
10139  return i->user_index();
10140  }
10141 
10142 
10143 
10144  template <int structdim, int dim, int spacedim>
10145  unsigned int get_user_index (const TriaIterator<InvalidAccessor<structdim,dim,spacedim> > &)
10146  {
10147  Assert (false, ExcInternalError());
10149  }
10150 
10151 
10152 
10153  template <typename Iterator>
10154  void set_user_index (const Iterator &i,
10155  const unsigned int x)
10156  {
10157  i->set_user_index(x);
10158  }
10159 
10160 
10161 
10162  template <int structdim, int dim, int spacedim>
10163  void set_user_index (const TriaIterator<InvalidAccessor<structdim,dim,spacedim> > &,
10164  const unsigned int)
10165  {
10166  Assert (false, ExcInternalError());
10167  }
10168 }
10169 
10170 
10171 template <int dim, int spacedim>
10172 void Triangulation<dim, spacedim>::save_user_indices_line (std::vector<unsigned int> &v) const
10173 {
10174  v.resize (n_lines(), 0);
10175  std::vector<unsigned int>::iterator i = v.begin();
10176  line_iterator line = begin_line(),
10177  endl = end_line();
10178  for (; line!=endl; ++line, ++i)
10179  *i = line->user_index();
10180 }
10181 
10182 
10183 
10184 template <int dim, int spacedim>
10185 void Triangulation<dim, spacedim>::load_user_indices_line (const std::vector<unsigned int> &v)
10186 {
10187  Assert (v.size() == n_lines(), ExcGridReadError());
10188 
10189  line_iterator line = begin_line(),
10190  endl = end_line();
10191  std::vector<unsigned int>::const_iterator i = v.begin();
10192  for (; line!=endl; ++line, ++i)
10193  line->set_user_index(*i);
10194 }
10195 
10196 
10197 template <int dim, int spacedim>
10198 void Triangulation<dim, spacedim>::save_user_indices_quad (std::vector<unsigned int> &v) const
10199 {
10200  v.resize (n_quads(), 0);
10201 
10202  if (dim >= 2)
10203  {
10204  std::vector<unsigned int>::iterator i = v.begin();
10205  quad_iterator quad = begin_quad(),
10206  endq = end_quad();
10207  for (; quad!=endq; ++quad, ++i)
10208  *i = get_user_index(quad);
10209  }
10210 }
10211 
10212 
10213 
10214 template <int dim, int spacedim>
10215 void Triangulation<dim, spacedim>::load_user_indices_quad (const std::vector<unsigned int> &v)
10216 {
10217  Assert (v.size() == n_quads(), ExcGridReadError());
10218 
10219  if (dim >= 2)
10220  {
10221  quad_iterator quad = begin_quad(),
10222  endq = end_quad();
10223  std::vector<unsigned int>::const_iterator i = v.begin();
10224  for (; quad!=endq; ++quad, ++i)
10225  set_user_index(quad, *i);
10226  }
10227 }
10228 
10229 
10230 template <int dim, int spacedim>
10231 void Triangulation<dim, spacedim>::save_user_indices_hex (std::vector<unsigned int> &v) const
10232 {
10233  v.resize (n_hexs(), 0);
10234 
10235  if (dim >= 3)
10236  {
10237  std::vector<unsigned int>::iterator i = v.begin();
10238  hex_iterator hex = begin_hex(),
10239  endh = end_hex();
10240  for (; hex!=endh; ++hex, ++i)
10241  *i = get_user_index(hex);
10242  }
10243 }
10244 
10245 
10246 
10247 template <int dim, int spacedim>
10248 void Triangulation<dim, spacedim>::load_user_indices_hex (const std::vector<unsigned int> &v)
10249 {
10250  Assert (v.size() == n_hexs(), ExcGridReadError());
10251 
10252  if (dim >= 3)
10253  {
10254  hex_iterator hex = begin_hex(),
10255  endh = end_hex();
10256  std::vector<unsigned int>::const_iterator i = v.begin();
10257  for (; hex!=endh; ++hex, ++i)
10258  set_user_index(hex, *i);
10259  }
10260 }
10261 
10262 
10263 
10264 //---------------- user pointers ----------------------------------------//
10265 
10266 
10267 namespace
10268 {
10269  template <typename Iterator>
10270  void *get_user_pointer (const Iterator &i)
10271  {
10272  return i->user_pointer();
10273  }
10274 
10275 
10276 
10277  template <int structdim, int dim, int spacedim>
10278  void *get_user_pointer (const TriaIterator<InvalidAccessor<structdim,dim,spacedim> > &)
10279  {
10280  Assert (false, ExcInternalError());
10281  return nullptr;
10282  }
10283 
10284 
10285 
10286  template <typename Iterator>
10287  void set_user_pointer (const Iterator &i,
10288  void *x)
10289  {
10290  i->set_user_pointer(x);
10291  }
10292 
10293 
10294 
10295  template <int structdim, int dim, int spacedim>
10296  void set_user_pointer (const TriaIterator<InvalidAccessor<structdim,dim,spacedim> > &,
10297  void *)
10298  {
10299  Assert (false, ExcInternalError());
10300  }
10301 }
10302 
10303 
10304 template <int dim, int spacedim>
10305 void Triangulation<dim, spacedim>::save_user_pointers (std::vector<void *> &v) const
10306 {
10307  // clear vector and append all the
10308  // stuff later on
10309  v.clear ();
10310 
10311  std::vector<void *> tmp;
10312 
10313  save_user_pointers_line (tmp);
10314  v.insert (v.end(), tmp.begin(), tmp.end());
10315 
10316  if (dim >= 2)
10317  {
10318  save_user_pointers_quad (tmp);
10319  v.insert (v.end(), tmp.begin(), tmp.end());
10320  }
10321 
10322  if (dim >= 3)
10323  {
10324  save_user_pointers_hex (tmp);
10325  v.insert (v.end(), tmp.begin(), tmp.end());
10326  }
10327 
10328  if (dim >= 4)
10329  Assert (false, ExcNotImplemented());
10330 }
10331 
10332 
10333 
10334 template <int dim, int spacedim>
10335 void Triangulation<dim, spacedim>::load_user_pointers (const std::vector<void *> &v)
10336 {
10337  Assert (v.size() == n_lines()+n_quads()+n_hexs(), ExcInternalError());
10338  std::vector<void *> tmp;
10339 
10340  // first extract the pointers
10341  // belonging to lines
10342  tmp.insert (tmp.end(),
10343  v.begin(), v.begin()+n_lines());
10344  // and set the lines
10345  load_user_pointers_line (tmp);
10346 
10347  if (dim >= 2)
10348  {
10349  tmp.clear ();
10350  tmp.insert (tmp.end(),
10351  v.begin()+n_lines(), v.begin()+n_lines()+n_quads());
10352  load_user_pointers_quad (tmp);
10353  }
10354 
10355  if (dim >= 3)
10356  {
10357  tmp.clear ();
10358  tmp.insert (tmp.end(),
10359  v.begin()+n_lines()+n_quads(), v.begin()+n_lines()+n_quads()+n_hexs());
10360  load_user_pointers_hex (tmp);
10361  }
10362 
10363  if (dim >= 4)
10364  Assert (false, ExcNotImplemented());
10365 }
10366 
10367 
10368 
10369 template <int dim, int spacedim>
10371 {
10372  v.resize (n_lines(), nullptr);
10373  std::vector<void *>::iterator i = v.begin();
10374  line_iterator line = begin_line(),
10375  endl = end_line();
10376  for (; line!=endl; ++line, ++i)
10377  *i = line->user_pointer();
10378 }
10379 
10380 
10381 
10382 template <int dim, int spacedim>
10384 {
10385  Assert (v.size() == n_lines(), ExcGridReadError());
10386 
10387  line_iterator line = begin_line(),
10388  endl = end_line();
10389  std::vector<void *>::const_iterator i = v.begin();
10390  for (; line!=endl; ++line, ++i)
10391  line->set_user_pointer(*i);
10392 }
10393 
10394 
10395 
10396 template <int dim, int spacedim>
10398 {
10399  v.resize (n_quads(), nullptr);
10400 
10401  if (dim >= 2)
10402  {
10403  std::vector<void *>::iterator i = v.begin();
10404  quad_iterator quad = begin_quad(),
10405  endq = end_quad();
10406  for (; quad!=endq; ++quad, ++i)
10407  *i = get_user_pointer(quad);
10408  }
10409 }
10410 
10411 
10412 
10413 template <int dim, int spacedim>
10415 {
10416  Assert (v.size() == n_quads(), ExcGridReadError());
10417 
10418  if (dim >= 2)
10419  {
10420  quad_iterator quad = begin_quad(),
10421  endq = end_quad();
10422  std::vector<void *>::const_iterator i = v.begin();
10423  for (; quad!=endq; ++quad, ++i)
10424  set_user_pointer(quad, *i);
10425  }
10426 }
10427 
10428 
10429 template <int dim, int spacedim>
10430 void Triangulation<dim, spacedim>::save_user_pointers_hex (std::vector<void *> &v) const
10431 {
10432  v.resize (n_hexs(), nullptr);
10433 
10434  if (dim >= 3)
10435  {
10436  std::vector<void *>::iterator i = v.begin();
10437  hex_iterator hex = begin_hex(),
10438  endh = end_hex();
10439  for (; hex!=endh; ++hex, ++i)
10440  *i = get_user_pointer(hex);
10441  }
10442 }
10443 
10444 
10445 
10446 template <int dim, int spacedim>
10447 void Triangulation<dim, spacedim>::load_user_pointers_hex (const std::vector<void *> &v)
10448 {
10449  Assert (v.size() == n_hexs(), ExcGridReadError());
10450 
10451  if (dim >= 3)
10452  {
10453  hex_iterator hex = begin_hex(),
10454  endh = end_hex();
10455  std::vector<void *>::const_iterator i = v.begin();
10456  for (; hex!=endh; ++hex, ++i)
10457  set_user_pointer(hex, *i);
10458  }
10459 }
10460 
10461 
10462 
10463 /*------------------------ Cell iterator functions ------------------------*/
10464 
10465 
10466 template <int dim, int spacedim>
10468 Triangulation<dim,spacedim>::begin_raw (const unsigned int level) const
10469 {
10470  switch (dim)
10471  {
10472  case 1:
10473  return begin_raw_line (level);
10474  case 2:
10475  return begin_raw_quad (level);
10476  case 3:
10477  return begin_raw_hex (level);
10478  default:
10479  Assert (false, ExcNotImplemented());
10480  return raw_cell_iterator();
10481  }
10482 }
10483 
10484 
10485 
10486 template <int dim, int spacedim>
10488 Triangulation<dim,spacedim>::begin (const unsigned int level) const
10489 {
10490  switch (dim)
10491  {
10492  case 1:
10493  return begin_line (level);
10494  case 2:
10495  return begin_quad (level);
10496  case 3:
10497  return begin_hex (level);
10498  default:
10499  Assert (false, ExcImpossibleInDim(dim));
10500  return cell_iterator();
10501  }
10502 }
10503 
10504 
10505 
10506 template <int dim, int spacedim>
10508 Triangulation<dim,spacedim>::begin_active (const unsigned int level) const
10509 {
10510  switch (dim)
10511  {
10512  case 1:
10513  return begin_active_line (level);
10514  case 2:
10515  return begin_active_quad (level);
10516  case 3:
10517  return begin_active_hex (level);
10518  default:
10519  Assert (false, ExcNotImplemented());
10520  return active_cell_iterator();
10521  }
10522 }
10523 
10524 
10525 
10526 template <int dim, int spacedim>
10529 {
10530  const unsigned int level = levels.size()-1;
10531 
10532  Assert (level<n_global_levels() || level<levels.size(), ExcInvalidLevel(level));
10533  if (levels[level]->cells.cells.size() ==0)
10534  return end(level);
10535 
10536  // find the last raw iterator on
10537  // this level
10538  raw_cell_iterator ri (const_cast<Triangulation<dim,spacedim>*>(this),
10539  level,
10540  levels[level]->cells.cells.size()-1);
10541 
10542  // then move to the last used one
10543  if (ri->used()==true)
10544  return ri;
10545  while ((--ri).state() == IteratorState::valid)
10546  if (ri->used()==true)
10547  return ri;
10548  return ri;
10549 }
10550 
10551 
10552 
10553 template <int dim, int spacedim>
10556 {
10557  // get the last used cell
10558  cell_iterator cell = last();
10559 
10560  if (cell != end())
10561  {
10562  // then move to the last active one
10563  if (cell->active()==true)
10564  return cell;
10565  while ((--cell).state() == IteratorState::valid)
10566  if (cell->active()==true)
10567  return cell;
10568  }
10569  return cell;
10570 }
10571 
10572 
10573 
10574 template <int dim, int spacedim>
10577 {
10578  return cell_iterator (const_cast<Triangulation<dim, spacedim>*>(this),
10579  -1,
10580  -1);
10581 }
10582 
10583 
10584 
10585 template <int dim, int spacedim>
10587 Triangulation<dim, spacedim>::end_raw (const unsigned int level) const
10588 {
10589  Assert (level<n_global_levels(), ExcInvalidLevel(level));
10590  if (level < levels.size()-1)
10591  return begin_raw (level+1);
10592  else
10593  return end();
10594 }
10595 
10596 
10597 template <int dim, int spacedim>
10599 Triangulation<dim, spacedim>::end (const unsigned int level) const
10600 {
10601  if (level < levels.size()-1)
10602  return begin (level+1);
10603  Assert (level<n_global_levels() || level<levels.size(), ExcInvalidLevel(level));
10604  return end();
10605 }
10606 
10607 
10608 template <int dim, int spacedim>
10610 Triangulation<dim, spacedim>::end_active (const unsigned int level) const
10611 {
10612  Assert (level<n_global_levels() || level < levels.size(), ExcInvalidLevel(level));
10613  return (level >= levels.size()-1 ?
10614  active_cell_iterator(end()) :
10615  begin_active (level+1));
10616 }
10617 
10618 
10619 
10620 template <int dim, int spacedim>
10623 {
10624  return
10626  (begin(), end());
10627 }
10628 
10629 
10630 template <int dim, int spacedim>
10633 {
10634  return
10636  (begin_active(), end());
10637 }
10638 
10639 
10640 
10641 template <int dim, int spacedim>
10644 {
10645  return
10647  (begin(level), end(level));
10648 }
10649 
10650 
10651 
10652 template <int dim, int spacedim>
10655 {
10656  return
10658  (begin_active(level), end_active(level));
10659 }
10660 
10661 
10662 /*------------------------ Face iterator functions ------------------------*/
10663 
10664 
10665 template <int dim, int spacedim>
10668 {
10669  switch (dim)
10670  {
10671  case 1:
10672  Assert (false, ExcImpossibleInDim(1));
10673  return raw_face_iterator();
10674  case 2:
10675  return begin_line ();
10676  case 3:
10677  return begin_quad ();
10678  default:
10679  Assert (false, ExcNotImplemented());
10680  return face_iterator ();
10681  }
10682 }
10683 
10684 
10685 
10686 template <int dim, int spacedim>
10689 {
10690  switch (dim)
10691  {
10692  case 1:
10693  Assert (false, ExcImpossibleInDim(1));
10694  return raw_face_iterator();
10695  case 2:
10696  return begin_active_line ();
10697  case 3:
10698  return begin_active_quad ();
10699  default:
10700  Assert (false, ExcNotImplemented());
10701  return active_face_iterator ();
10702  }
10703 }
10704 
10705 
10706 
10707 template <int dim, int spacedim>
10710 {
10711  switch (dim)
10712  {
10713  case 1:
10714  Assert (false, ExcImpossibleInDim(1));
10715  return raw_face_iterator();
10716  case 2:
10717  return end_line ();
10718  case 3:
10719  return end_quad ();
10720  default:
10721  Assert (false, ExcNotImplemented());
10722  return raw_face_iterator ();
10723  }
10724 }
10725 
10726 
10727 /*------------------------ Vertex iterator functions ------------------------*/
10728 
10729 
10730 template <int dim, int spacedim>
10733 {
10734  if (dim==1)
10735  {
10736  // This does not work if dim==1 because TriaAccessor<0,1,spacedim> does not
10737  // implement operator++
10738  Assert(false, ExcNotImplemented());
10739  return raw_vertex_iterator();
10740  }
10741  else
10742  {
10744  0,
10745  0);
10746  if (i.state() != IteratorState::valid)
10747  return i;
10748  // This loop will end because every triangulation has used vertices.
10749  while (i->used() == false)
10750  if ((++i).state() != IteratorState::valid)
10751  return i;
10752  return i;
10753  }
10754 }
10755 
10756 
10757 
10758 template <int dim, int spacedim>
10761 {
10762  return begin_vertex();
10763 }
10764 
10765 
10766 
10767 template <int dim, int spacedim>
10770 {
10771  if (dim==1)
10772  {
10773  Assert(false, ExcNotImplemented());
10774  return raw_vertex_iterator();
10775  }
10776  else
10777  return raw_vertex_iterator(const_cast<Triangulation<dim, spacedim>*>(this),
10778  -1,
10780 }
10781 
10782 
10783 
10784 
10785 /*------------------------ Line iterator functions ------------------------*/
10786 
10787 
10788 
10789 template <int dim, int spacedim>
10790 typename Triangulation<dim, spacedim>::raw_line_iterator
10791 Triangulation<dim, spacedim>::begin_raw_line (const unsigned int level) const
10792 {
10793  switch (dim)
10794  {
10795  case 1:
10796  Assert (level<n_global_levels() || level<levels.size(), ExcInvalidLevel(level));
10797 
10798  if (level >= levels.size() || levels[level]->cells.cells.size() == 0)
10799  return end_line();
10800 
10801  return raw_line_iterator (const_cast<Triangulation<dim,spacedim>*>(this),
10802  level,
10803  0);
10804 
10805  default:
10806  Assert (level == 0, ExcFacesHaveNoLevel());
10807  return raw_line_iterator (const_cast<Triangulation<dim, spacedim>*>(this),
10808  0,
10809  0);
10810  }
10811 }
10812 
10813 
10814 template <int dim, int spacedim>
10815 typename Triangulation<dim, spacedim>::line_iterator
10816 Triangulation<dim, spacedim>::begin_line (const unsigned int level) const
10817 {
10818  // level is checked in begin_raw
10819  raw_line_iterator ri = begin_raw_line (level);
10820  if (ri.state() != IteratorState::valid)
10821  return ri;
10822  while (ri->used() == false)
10823  if ((++ri).state() != IteratorState::valid)
10824  return ri;
10825  return ri;
10826 }
10827 
10828 
10829 
10830 template <int dim, int spacedim>
10831 typename Triangulation<dim, spacedim>::active_line_iterator
10832 Triangulation<dim, spacedim>::begin_active_line (const unsigned int level) const
10833 {
10834  // level is checked in begin_raw
10835  line_iterator i = begin_line (level);
10836  if (i.state() != IteratorState::valid)
10837  return i;
10838  while (i->has_children())
10839  if ((++i).state() != IteratorState::valid)
10840  return i;
10841  return i;
10842 }
10843 
10844 
10845 
10846 template <int dim, int spacedim>
10847 typename Triangulation<dim, spacedim>::line_iterator
10849 {
10850  return raw_line_iterator (const_cast<Triangulation<dim, spacedim>*>(this),
10851  -1,
10852  -1);
10853 }
10854 
10855 
10856 
10857 /*------------------------ Quad iterator functions ------------------------*/
10858 
10859 
10860 template <int dim, int spacedim>
10861 typename Triangulation<dim,spacedim>::raw_quad_iterator
10862 Triangulation<dim,spacedim>::begin_raw_quad (const unsigned int level) const
10863 {
10864  switch (dim)
10865  {
10866  case 1:
10867  Assert (false, ExcImpossibleInDim(1));
10868  return raw_hex_iterator();
10869  case 2:
10870  {
10871  Assert (level<n_global_levels() || level<levels.size(), ExcInvalidLevel(level));
10872 
10873  if (level >= levels.size() || levels[level]->cells.cells.size() == 0)
10874  return end_quad();
10875 
10876  return raw_quad_iterator (const_cast<Triangulation<dim,spacedim>*>(this),
10877  level,
10878  0);
10879  }
10880 
10881  case 3:
10882  {
10883  Assert (level == 0, ExcFacesHaveNoLevel());
10884 
10885  return raw_quad_iterator (const_cast<Triangulation<dim,spacedim>*>(this),
10886  0,
10887  0);
10888  }
10889 
10890 
10891  default:
10892  Assert (false, ExcNotImplemented());
10893  return raw_hex_iterator();
10894  }
10895 }
10896 
10897 
10898 
10899 template <int dim, int spacedim>
10900 typename Triangulation<dim,spacedim>::quad_iterator
10901 Triangulation<dim,spacedim>::begin_quad (const unsigned int level) const
10902 {
10903  // level is checked in begin_raw
10904  raw_quad_iterator ri = begin_raw_quad (level);
10905  if (ri.state() != IteratorState::valid)
10906  return ri;
10907  while (ri->used() == false)
10908  if ((++ri).state() != IteratorState::valid)
10909  return ri;
10910  return ri;
10911 }
10912 
10913 
10914 
10915 template <int dim, int spacedim>
10916 typename Triangulation<dim,spacedim>::active_quad_iterator
10917 Triangulation<dim,spacedim>::begin_active_quad (const unsigned int level) const
10918 {
10919  // level is checked in begin_raw
10920  quad_iterator i = begin_quad (level);
10921  if (i.state() != IteratorState::valid)
10922  return i;
10923  while (i->has_children())
10924  if ((++i).state() != IteratorState::valid)
10925  return i;
10926  return i;
10927 }
10928 
10929 
10930 
10931 template <int dim, int spacedim>
10932 typename Triangulation<dim,spacedim>::quad_iterator
10934 {
10935  return raw_quad_iterator (const_cast<Triangulation<dim, spacedim>*>(this),
10936  -1,
10937  -1);
10938 }
10939 
10940 
10941 /*------------------------ Hex iterator functions ------------------------*/
10942 
10943 
10944 template <int dim, int spacedim>
10945 typename Triangulation<dim,spacedim>::raw_hex_iterator
10946 Triangulation<dim,spacedim>::begin_raw_hex (const unsigned int level) const
10947 {
10948  switch (dim)
10949  {
10950  case 1:
10951  case 2:
10952  Assert (false, ExcImpossibleInDim(1));
10953  return raw_hex_iterator();
10954  case 3:
10955  {
10956  Assert (level<n_global_levels() || level<levels.size(), ExcInvalidLevel(level));
10957 
10958  if (level >= levels.size() || levels[level]->cells.cells.size() == 0)
10959  return end_hex();
10960 
10961  return raw_hex_iterator (const_cast<Triangulation<dim,spacedim>*>(this),
10962  level,
10963  0);
10964  }
10965 
10966  default:
10967  Assert (false, ExcNotImplemented());
10968  return raw_hex_iterator();
10969  }
10970 }
10971 
10972 
10973 
10974 template <int dim, int spacedim>
10975 typename Triangulation<dim,spacedim>::hex_iterator
10976 Triangulation<dim,spacedim>::begin_hex (const unsigned int level) const
10977 {
10978  // level is checked in begin_raw
10979  raw_hex_iterator ri = begin_raw_hex (level);
10980  if (ri.state() != IteratorState::valid)
10981  return ri;
10982  while (ri->used() == false)
10983  if ((++ri).state() != IteratorState::valid)
10984  return ri;
10985  return ri;
10986 }
10987 
10988 
10989 
10990 template <int dim, int spacedim>
10991 typename Triangulation<dim, spacedim>::active_hex_iterator
10992 Triangulation<dim, spacedim>::begin_active_hex (const unsigned int level) const
10993 {
10994  // level is checked in begin_raw
10995  hex_iterator i = begin_hex (level);
10996  if (i.state() != IteratorState::valid)
10997  return i;
10998  while (i->has_children())
10999  if ((++i).state() != IteratorState::valid)
11000  return i;
11001  return i;
11002 }
11003 
11004 
11005 
11006 template <int dim, int spacedim>
11007 typename Triangulation<dim, spacedim>::hex_iterator
11009 {
11010  return raw_hex_iterator (const_cast<Triangulation<dim,spacedim>*>(this),
11011  -1,
11012  -1);
11013 }
11014 
11015 
11016 
11017 
11018 // -------------------------------- number of cells etc ---------------
11019 
11020 
11021 namespace internal
11022 {
11023  namespace TriangulationImplementation
11024  {
11025  inline
11026  unsigned int
11028  {
11029  return c.n_lines;
11030  }
11031 
11032 
11033  inline
11034  unsigned int
11036  {
11037  return c.n_active_lines;
11038  }
11039 
11040 
11041  inline
11042  unsigned int
11044  {
11045  return c.n_quads;
11046  }
11047 
11048 
11049  inline
11050  unsigned int
11052  {
11053  return c.n_active_quads;
11054  }
11055 
11056 
11057  inline
11058  unsigned int
11060  {
11061  return c.n_hexes;
11062  }
11063 
11064 
11065  inline
11066  unsigned int
11068  {
11069  return c.n_active_hexes;
11070  }
11071  }
11072 }
11073 
11074 
11075 
11076 template <int dim, int spacedim>
11078 {
11079  return internal::TriangulationImplementation::n_cells (number_cache);
11080 }
11081 
11082 
11083 template <int dim, int spacedim>
11085 {
11086  return internal::TriangulationImplementation::n_active_cells (number_cache);
11087 }
11088 
11089 template <int dim, int spacedim>
11091 {
11092  return n_active_cells();
11093 }
11094 
11095 
11096 
11097 template <int dim, int spacedim>
11099 {
11100  switch (dim)
11101  {
11102  case 1:
11103  return 0;
11104  case 2:
11105  return n_lines();
11106  case 3:
11107  return n_quads();
11108  default:
11109  Assert (false, ExcNotImplemented());
11110  }
11111  return 0;
11112 }
11113 
11114 
11115 template <int dim, int spacedim>
11117 {
11118  switch (dim)
11119  {
11120  case 2:
11121  return n_raw_lines();
11122  case 3:
11123  return n_raw_quads();
11124  default:
11125  Assert (false, ExcNotImplemented());
11126  }
11127  return 0;
11128 }
11129 
11130 
11131 template <int dim, int spacedim>
11133 {
11134  switch (dim)
11135  {
11136  case 1:
11137  return 0;
11138  case 2:
11139  return n_active_lines();
11140  case 3:
11141  return n_active_quads();
11142  default:
11143  Assert (false, ExcNotImplemented());
11144  }
11145  return 0;
11146 }
11147 
11148 
11149 template <int dim, int spacedim>
11150 unsigned int Triangulation<dim, spacedim>::n_raw_cells (const unsigned int level) const
11151 {
11152  switch (dim)
11153  {
11154  case 1:
11155  return n_raw_lines(level);
11156  case 2:
11157  return n_raw_quads(level);
11158  case 3:
11159  return n_raw_hexs(level);
11160  default:
11161  Assert (false, ExcNotImplemented());
11162  }
11163  return 0;
11164 }
11165 
11166 
11167 
11168 template <int dim, int spacedim>
11169 unsigned int Triangulation<dim, spacedim>::n_cells (const unsigned int level) const
11170 {
11171  switch (dim)
11172  {
11173  case 1:
11174  return n_lines(level);
11175  case 2:
11176  return n_quads(level);
11177  case 3:
11178  return n_hexs(level);
11179  default:
11180  Assert (false, ExcNotImplemented());
11181  }
11182  return 0;
11183 }
11184 
11185 
11186 
11187 template <int dim, int spacedim>
11188 unsigned int Triangulation<dim, spacedim>::n_active_cells (const unsigned int level) const
11189 {
11190  switch (dim)
11191  {
11192  case 1:
11193  return n_active_lines(level);
11194  case 2:
11195  return n_active_quads(level);
11196  case 3:
11197  return n_active_hexs(level);
11198  default:
11199  Assert (false, ExcNotImplemented());
11200  }
11201  return 0;
11202 }
11203 
11204 
11205 template <int dim, int spacedim>
11207 {
11208  for (unsigned int lvl = 0; lvl<n_global_levels()-1; lvl++)
11209  if (n_active_cells(lvl) != 0)
11210  return true;
11211 
11212  return false;
11213 }
11214 
11215 
11216 template <int dim, int spacedim>
11218 {
11219  return number_cache.n_lines;
11220 }
11221 
11222 
11223 //TODO: Merge the following 6 functions somehow
11224 template <>
11225 unsigned int Triangulation<1,1>::n_raw_lines (const unsigned int level) const
11226 {
11227  Assert(level < n_levels(), ExcIndexRange(level,0,n_levels()));
11228  return levels[level]->cells.cells.size();
11229 }
11230 
11231 
11232 template <>
11233 unsigned int Triangulation<1,1>::n_raw_lines () const
11234 {
11235  Assert(false, ExcNotImplemented());
11236  return 0;
11237 }
11238 
11239 
11240 
11241 template <>
11242 unsigned int Triangulation<1,2>::n_raw_lines (const unsigned int level) const
11243 {
11244  Assert(level < n_levels(), ExcIndexRange(level,0,n_levels()));
11245  return levels[level]->cells.cells.size();
11246 }
11247 
11248 
11249 template <>
11250 unsigned int Triangulation<1,2>::n_raw_lines () const
11251 {
11252  Assert(false, ExcNotImplemented());
11253  return 0;
11254 }
11255 
11256 
11257 template <>
11258 unsigned int Triangulation<1,3>::n_raw_lines (const unsigned int level) const
11259 {
11260  Assert(level < n_levels(), ExcIndexRange(level,0,n_levels()));
11261  return levels[level]->cells.cells.size();
11262 }
11263 
11264 template <>
11265 unsigned int Triangulation<1,3>::n_raw_lines () const
11266 {
11267  Assert(false, ExcNotImplemented());
11268  return 0;
11269 }
11270 
11271 
11272 
11273 template <int dim, int spacedim>
11274 unsigned int Triangulation<dim, spacedim>::n_raw_lines (const unsigned int) const
11275 {
11276  Assert(false, ExcFacesHaveNoLevel());
11277  return 0;
11278 }
11279 
11280 
11281 template <int dim, int spacedim>
11283 {
11284  return faces->lines.cells.size();
11285 }
11286 
11287 
11288 template <int dim, int spacedim>
11289 unsigned int Triangulation<dim, spacedim>::n_lines (const unsigned int level) const
11290 {
11291  Assert (level < number_cache.n_lines_level.size(),
11292  ExcIndexRange (level, 0, number_cache.n_lines_level.size()));
11293  Assert (dim == 1, ExcFacesHaveNoLevel());
11294  return number_cache.n_lines_level[level];
11295 }
11296 
11297 
11298 template <int dim, int spacedim>
11300 {
11301  return number_cache.n_active_lines;
11302 }
11303 
11304 
11305 template <int dim, int spacedim>
11306 unsigned int Triangulation<dim, spacedim>::n_active_lines (const unsigned int level) const
11307 {
11308  Assert (level < number_cache.n_lines_level.size(),
11309  ExcIndexRange (level, 0, number_cache.n_lines_level.size()));
11310  Assert (dim == 1, ExcFacesHaveNoLevel());
11311 
11312  return number_cache.n_active_lines_level[level];
11313 }
11314 
11315 
11316 template <>
11317 unsigned int Triangulation<1,1>::n_quads () const
11318 {
11319  return 0;
11320 }
11321 
11322 
11323 template <>
11324 unsigned int Triangulation<1,1>::n_quads (const unsigned int) const
11325 {
11326  return 0;
11327 }
11328 
11329 
11330 template <>
11331 unsigned int Triangulation<1,1>::n_raw_quads (const unsigned int) const
11332 {
11333  return 0;
11334 }
11335 
11336 
11337 template <>
11338 unsigned int Triangulation<1,1>::n_raw_hexs (const unsigned int) const
11339 {
11340  return 0;
11341 }
11342 
11343 
11344 template <>
11345 unsigned int Triangulation<1,1>::n_active_quads (const unsigned int) const
11346 {
11347  return 0;
11348 }
11349 
11350 
11351 template <>
11352 unsigned int Triangulation<1,1>::n_active_quads () const
11353 {
11354  return 0;
11355 }
11356 
11357 
11358 
11359 
11360 template <>
11361 unsigned int Triangulation<1,2>::n_quads () const
11362 {
11363  return 0;
11364 }
11365 
11366 
11367 template <>
11368 unsigned int Triangulation<1,2>::n_quads (const unsigned int) const
11369 {
11370  return 0;
11371 }
11372 
11373 
11374 template <>
11375 unsigned int Triangulation<1,2>::n_raw_quads (const unsigned int) const
11376 {
11377  return 0;
11378 }
11379 
11380 
11381 template <>
11382 unsigned int Triangulation<1,2>::n_raw_hexs (const unsigned int) const
11383 {
11384  return 0;
11385 }
11386 
11387 
11388 template <>
11389 unsigned int Triangulation<1,2>::n_active_quads (const unsigned int) const
11390 {
11391  return 0;
11392 }
11393 
11394 
11395 template <>
11396 unsigned int Triangulation<1,2>::n_active_quads () const
11397 {
11398  return 0;
11399 }
11400 
11401 
11402 template <>
11403 unsigned int Triangulation<1,3>::n_quads () const
11404 {
11405  return 0;
11406 }
11407 
11408 
11409 template <>
11410 unsigned int Triangulation<1,3>::n_quads (const unsigned int) const
11411 {
11412  return 0;
11413 }
11414 
11415 
11416 template <>
11417 unsigned int Triangulation<1,3>::n_raw_quads (const unsigned int) const
11418 {
11419  return 0;
11420 }
11421 
11422 
11423 template <>
11424 unsigned int Triangulation<1,3>::n_raw_hexs (const unsigned int) const
11425 {
11426  return 0;
11427 }
11428 
11429 
11430 template <>
11431 unsigned int Triangulation<1,3>::n_active_quads (const unsigned int) const
11432 {
11433  return 0;
11434 }
11435 
11436 
11437 template <>
11438 unsigned int Triangulation<1,3>::n_active_quads () const
11439 {
11440  return 0;
11441 }
11442 
11443 
11444 
11445 template <int dim, int spacedim>
11447 {
11448  return number_cache.n_quads;
11449 }
11450 
11451 
11452 template <int dim, int spacedim>
11453 unsigned int Triangulation<dim, spacedim>::n_quads (const unsigned int level) const
11454 {
11455  Assert (dim == 2, ExcFacesHaveNoLevel());
11456  Assert (level < number_cache.n_quads_level.size(),
11457  ExcIndexRange (level, 0, number_cache.n_quads_level.size()));
11458  return number_cache.n_quads_level[level];
11459 }
11460 
11461 
11462 
11463 template <>
11464 unsigned int Triangulation<2,2>::n_raw_quads (const unsigned int level) const
11465 {
11466  Assert(level < n_levels(), ExcIndexRange(level,0,n_levels()));
11467  return levels[level]->cells.cells.size();
11468 }
11469 
11470 
11471 
11472 template <>
11473 unsigned int Triangulation<2,3>::n_raw_quads (const unsigned int level) const
11474 {
11475  Assert(level < n_levels(), ExcIndexRange(level,0,n_levels()));
11476  return levels[level]->cells.cells.size();
11477 }
11478 
11479 
11480 template <>
11481 unsigned int Triangulation<3,3>::n_raw_quads (const unsigned int) const
11482 {
11483  Assert(false, ExcFacesHaveNoLevel());
11484  return 0;
11485 }
11486 
11487 
11488 
11489 
11490 
11491 template <int dim, int spacedim>
11493 {
11494  Assert (false, ExcNotImplemented());
11495  return 0;
11496 }
11497 
11498 
11499 
11500 template <>
11501 unsigned int Triangulation<3,3>::n_raw_quads () const
11502 {
11503  return faces->quads.cells.size();
11504 }
11505 
11506 
11507 
11508 template <int dim, int spacedim>
11510 {
11511  return number_cache.n_active_quads;
11512 }
11513 
11514 
11515 template <int dim, int spacedim>
11516 unsigned int Triangulation<dim, spacedim>::n_active_quads (const unsigned int level) const
11517 {
11518  Assert (level < number_cache.n_quads_level.size(),
11519  ExcIndexRange (level, 0, number_cache.n_quads_level.size()));
11520  Assert (dim == 2, ExcFacesHaveNoLevel());
11521 
11522  return number_cache.n_active_quads_level[level];
11523 }
11524 
11525 
11526 template <int dim, int spacedim>
11528 {
11529  return 0;
11530 }
11531 
11532 
11533 
11534 template <int dim, int spacedim>
11535 unsigned int Triangulation<dim, spacedim>::n_hexs (const unsigned int) const
11536 {
11537  return 0;
11538 }
11539 
11540 
11541 
11542 template <int dim, int spacedim>
11543 unsigned int Triangulation<dim, spacedim>::n_raw_hexs (const unsigned int) const
11544 {
11545  return 0;
11546 }
11547 
11548 
11549 template <int dim, int spacedim>
11551 {
11552  return 0;
11553 }
11554 
11555 
11556 
11557 template <int dim, int spacedim>
11558 unsigned int Triangulation<dim, spacedim>::n_active_hexs (const unsigned int) const
11559 {
11560  return 0;
11561 }
11562 
11563 
11564 template <>
11565 unsigned int Triangulation<3,3>::n_hexs () const
11566 {
11567  return number_cache.n_hexes;
11568 }
11569 
11570 
11571 
11572 template <>
11573 unsigned int Triangulation<3,3>::n_hexs (const unsigned int level) const
11574 {
11575  Assert (level < number_cache.n_hexes_level.size(),
11576  ExcIndexRange (level, 0, number_cache.n_hexes_level.size()));
11577 
11578  return number_cache.n_hexes_level[level];
11579 }
11580 
11581 
11582 
11583 template <>
11584 unsigned int Triangulation<3,3>::n_raw_hexs (const unsigned int level) const
11585 {
11586  Assert(level < n_levels(), ExcIndexRange(level,0,n_levels()));
11587  return levels[level]->cells.cells.size();
11588 }
11589 
11590 
11591 template <>
11592 unsigned int Triangulation<3,3>::n_active_hexs () const
11593 {
11594  return number_cache.n_active_hexes;
11595 }
11596 
11597 
11598 
11599 template <>
11600 unsigned int Triangulation<3,3>::n_active_hexs (const unsigned int level) const
11601 {
11602  Assert (level < number_cache.n_hexes_level.size(),
11603  ExcIndexRange (level, 0, number_cache.n_hexes_level.size()));
11604 
11605  return number_cache.n_active_hexes_level[level];
11606 }
11607 
11608 
11609 
11610 template <int dim, int spacedim>
11611 unsigned int
11613 {
11614  return std::count_if (vertices_used.begin(), vertices_used.end(),
11615  std::bind (std::equal_to<bool>(), std::placeholders::_1, true));
11616 }
11617 
11618 
11619 
11620 template <int dim, int spacedim>
11621 const std::vector<bool> &
11623 {
11624  return vertices_used;
11625 }
11626 
11627 
11628 
11629 
11630 template <>
11631 unsigned int Triangulation<1,1>::max_adjacent_cells () const
11632 {
11633  return 2;
11634 }
11635 
11636 
11637 
11638 template <>
11639 unsigned int Triangulation<1,2>::max_adjacent_cells () const
11640 {
11641  return 2;
11642 }
11643 
11644 
11645 template <>
11646 unsigned int Triangulation<1,3>::max_adjacent_cells () const
11647 {
11648  return 2;
11649 }
11650 
11651 
11652 template <int dim, int spacedim>
11654 {
11655  cell_iterator cell = begin(0),
11656  endc = (n_levels() > 1 ? begin(1) : cell_iterator(end()));
11657  // store the largest index of the
11658  // vertices used on level 0
11659  unsigned int max_vertex_index = 0;
11660  for (; cell!=endc; ++cell)
11661  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell; ++vertex)
11662  if (cell->vertex_index(vertex) > max_vertex_index)
11663  max_vertex_index = cell->vertex_index(vertex);
11664 
11665  // store the number of times a cell
11666  // touches a vertex. An unsigned
11667  // int should suffice, even for
11668  // larger dimensions
11669  std::vector<unsigned short int> usage_count (max_vertex_index+1, 0);
11670  // touch a vertex's usage count
11671  // every time we find an adjacent
11672  // element
11673  for (cell=begin(); cell!=endc; ++cell)
11674  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell; ++vertex)
11675  ++usage_count[cell->vertex_index(vertex)];
11676 
11677  return std::max (GeometryInfo<dim>::vertices_per_cell,
11678  static_cast<unsigned int>(*std::max_element (usage_count.begin(),
11679  usage_count.end())));
11680 }
11681 
11682 
11683 
11684 template <int dim, int spacedim>
11687 {
11689 }
11690 
11691 
11692 
11693 template <int dim, int spacedim>
11696 {
11697  return *this;
11698 }
11699 
11700 
11701 
11702 template <int dim, int spacedim>
11705 {
11706  return *this;
11707 }
11708 
11709 
11710 
11711 template <int dim, int spacedim>
11712 void
11715  periodicity_vector)
11716 {
11717  periodic_face_pairs_level_0.insert(periodic_face_pairs_level_0.end(),
11718  periodicity_vector.begin(),
11719  periodicity_vector.end());
11720 
11721  //Now initialize periodic_face_map
11722  update_periodic_face_map();
11723 }
11724 
11725 
11726 
11727 template <int dim, int spacedim>
11728 const typename std::map<std::pair<typename Triangulation<dim, spacedim>::cell_iterator, unsigned int>,
11729  std::pair<std::pair<typename Triangulation<dim, spacedim>::cell_iterator, unsigned int>, std::bitset<3> > > &
11731 {
11732  return periodic_face_map;
11733 }
11734 
11735 
11736 
11737 template <int dim, int spacedim>
11738 void
11740 {
11741  prepare_coarsening_and_refinement ();
11742 
11743  // verify a case with which we have had
11744  // some difficulty in the past (see the
11745  // deal.II/coarsening_* tests)
11746  if (smooth_grid & limit_level_difference_at_vertices)
11747  Assert (satisfies_level1_at_vertex_rule (*this) == true,
11748  ExcInternalError());
11749 
11750  // Inform all listeners about beginning of refinement.
11751  signals.pre_refinement();
11752 
11753  execute_coarsening();
11754 
11755  const DistortedCellList
11756  cells_with_distorted_children = execute_refinement();
11757 
11758  // verify a case with which we have had
11759  // some difficulty in the past (see the
11760  // deal.II/coarsening_* tests)
11761  if (smooth_grid & limit_level_difference_at_vertices)
11762  Assert (satisfies_level1_at_vertex_rule (*this) == true,
11763  ExcInternalError());
11764 
11765  // finally build up neighbor connectivity information, and set
11766  // active cell indices
11767  update_neighbors(*this);
11768  reset_active_cell_indices ();
11769 
11770  // Inform all listeners about end of refinement.
11771  signals.post_refinement();
11772 
11773  AssertThrow (cells_with_distorted_children.distorted_cells.size() == 0,
11774  cells_with_distorted_children);
11775 
11776  update_periodic_face_map();
11777 }
11778 
11779 
11780 
11781 template <int dim, int spacedim>
11782 void
11784 {
11785  unsigned int active_cell_index = 0;
11786  for (raw_cell_iterator cell=begin_raw(); cell!=end(); ++cell)
11787  if ((cell->used() == false) || cell->has_children())
11788  cell->set_active_cell_index (numbers::invalid_unsigned_int);
11789  else
11790  {
11791  cell->set_active_cell_index (active_cell_index);
11792  ++active_cell_index;
11793  }
11794 
11795  Assert (active_cell_index == n_active_cells(), ExcInternalError());
11796 }
11797 
11798 
11799 template <int dim, int spacedim>
11800 void
11802 {
11803  //first empty the currently stored objects
11804  periodic_face_map.clear();
11805 
11806  typename std::vector<GridTools::PeriodicFacePair<cell_iterator> >::const_iterator it;
11807  for (it=periodic_face_pairs_level_0.begin(); it!=periodic_face_pairs_level_0.end(); ++it)
11808  {
11809  update_periodic_face_map_recursively<dim, spacedim>
11810  (it->cell[0], it->cell[1], it->face_idx[0], it->face_idx[1],
11811  it->orientation, periodic_face_map);
11812 
11813  //for the other way, we need to invert the orientation
11814  std::bitset<3> inverted_orientation;
11815  {
11816  bool orientation, flip, rotation;
11817  orientation = it->orientation[0];
11818  rotation = it->orientation[2];
11819  flip = orientation ? rotation ^ it->orientation[1] : it->orientation[1];
11820  inverted_orientation[0] = orientation;
11821  inverted_orientation[1] = flip;
11822  inverted_orientation[2] = rotation;
11823  }
11824  update_periodic_face_map_recursively<dim, spacedim>
11825  (it->cell[1], it->cell[0], it->face_idx[1], it->face_idx[0],
11826  inverted_orientation, periodic_face_map);
11827  }
11828 
11829  //check consistency
11830  typename std::map<std::pair<cell_iterator, unsigned int>,
11831  std::pair<std::pair<cell_iterator, unsigned int>, std::bitset<3> > >::const_iterator it_test;
11832  for (it_test=periodic_face_map.begin(); it_test!=periodic_face_map.end(); ++it_test)
11833  {
11834  const Triangulation<dim, spacedim>::cell_iterator cell_1 = it_test->first.first;
11835  const Triangulation<dim, spacedim>::cell_iterator cell_2 = it_test->second.first.first;
11836  if (cell_1->level() == cell_2->level())
11837  {
11838  // if both cells have the same neighbor, then the same pair
11839  // order swapped has to be in the map
11840  Assert (periodic_face_map[it_test->second.first].first == it_test->first,
11841  ExcInternalError());
11842  }
11843  }
11844 }
11845 
11846 
11847 
11848 template <int dim, int spacedim>
11849 void
11851 {
11852  levels.clear ();
11853  faces.reset ();
11854 
11855  vertices.clear ();
11856  vertices_used.clear ();
11857 
11858  manifold.clear();
11859 
11861 }
11862 
11863 
11864 template <int dim, int spacedim>
11867 {
11868  const DistortedCellList
11869  cells_with_distorted_children
11870  =
11872  execute_refinement (*this, check_for_distorted_cells);
11873 
11874 
11875 
11876  // re-compute number of lines
11878  ::compute_number_cache (*this, levels.size(), number_cache);
11879 
11880 #ifdef DEBUG
11881  for (unsigned int level=0; level<levels.size(); ++level)
11882  levels[level]->cells.monitor_memory (dim);
11883 
11884  // check whether really all refinement flags are reset (also of
11885  // previously non-active cells which we may not have touched. If the
11886  // refinement flag of a non-active cell is set, something went wrong
11887  // since the cell-accessors should have caught this)
11888  cell_iterator cell = begin(),
11889  endc = end();
11890  while (cell != endc)
11891  Assert (!(cell++)->refine_flag_set(), ExcInternalError ());
11892 #endif
11893 
11894  return cells_with_distorted_children;
11895 }
11896 
11897 
11898 
11899 template <int dim, int spacedim>
11901 {
11902  // create a vector counting for each line how many cells contain
11903  // this line. in 3D, this is used later on to decide which lines can
11904  // be deleted after coarsening a cell. in other dimensions it will
11905  // be ignored
11906  std::vector<unsigned int> line_cell_count = count_cells_bounded_by_line (*this);
11907  std::vector<unsigned int> quad_cell_count = count_cells_bounded_by_quad (*this);
11908 
11909  // loop over all cells. Flag all cells of which all children are
11910  // flagged for coarsening and delete the childrens' flags. In
11911  // effect, only those cells are flagged of which originally all
11912  // children were flagged and for which all children are on the same
11913  // refinement level. For flagging, the user flags are used, to avoid
11914  // confusion and because non-active cells can't be flagged for
11915  // coarsening. Note that because of the effects of
11916  // @p{fix_coarsen_flags}, of a cell either all or no children must
11917  // be flagged for coarsening, so it is ok to only check the first
11918  // child
11919  clear_user_flags ();
11920 
11921  cell_iterator cell = begin(),
11922  endc = end();
11923  for (; cell!=endc; ++cell)
11924  if (!cell->active())
11925  if (cell->child(0)->coarsen_flag_set())
11926  {
11927  cell->set_user_flag();
11928  for (unsigned int child=0; child<cell->n_children(); ++child)
11929  {
11930  Assert (cell->child(child)->coarsen_flag_set(),
11931  ExcInternalError());
11932  cell->child(child)->clear_coarsen_flag();
11933  }
11934  }
11935 
11936 
11937  // now do the actual coarsening step. Since the loop goes over used
11938  // cells we only need not worry about deleting some cells since the
11939  // ++operator will then just hop over them if we should hit one. Do
11940  // the loop in the reverse way since we may only delete some cells
11941  // if their neighbors have already been deleted (if the latter are
11942  // on a higher level for example)
11943  //
11944  // since we delete the *children* of cells, we can ignore cells
11945  // on the highest level, i.e., level must be less than or equal
11946  // to n_levels()-2.
11947  if (levels.size() >= 2)
11948  for (cell = last(); cell!=endc; --cell)
11949  if (cell->level()<=static_cast<int>(levels.size()-2) && cell->user_flag_set())
11950  {
11951  // inform all listeners that cell coarsening is going to happen
11952  signals.pre_coarsening_on_cell(cell);
11953  // use a separate function, since this is dimension specific
11955  ::delete_children (*this, cell, line_cell_count, quad_cell_count);
11956  }
11957 
11958  // re-compute number of lines and quads
11960  ::compute_number_cache (*this, levels.size(), number_cache);
11961 
11962  // in principle no user flags should be set any more at this point
11963 #if DEBUG
11964  for (cell=begin(); cell!=endc; ++cell)
11965  Assert (cell->user_flag_set() == false, ExcInternalError());
11966 #endif
11967 }
11968 
11969 
11970 
11971 template <int dim, int spacedim>
11973 {
11974  // copy a piece of code from prepare_coarsening_and_refinement that
11975  // ensures that the level difference at vertices is limited if so
11976  // desired. we need this code here since at least in 1d we don't
11977  // call the dimension-independent version of
11978  // prepare_coarsening_and_refinement function. in 2d and 3d, having
11979  // this hunk here makes our lives a bit easier as well as it takes
11980  // care of these cases earlier than it would otherwise happen.
11981  //
11982  // the main difference to the code in p_c_and_r is that here we
11983  // absolutely have to make sure that we get things right, i.e. that
11984  // in particular we set flags right if
11985  // limit_level_difference_at_vertices is set. to do so we iterate
11986  // until the flags don't change any more
11987  std::vector<bool> previous_coarsen_flags (n_active_cells());
11988  save_coarsen_flags (previous_coarsen_flags);
11989 
11990  std::vector<int> vertex_level (vertices.size(), 0);
11991 
11992  bool continue_iterating = true;
11993 
11994  do
11995  {
11996  if (smooth_grid & limit_level_difference_at_vertices)
11997  {
11998  Assert(!anisotropic_refinement,
11999  ExcMessage("In case of anisotropic refinement the "
12000  "limit_level_difference_at_vertices flag for "
12001  "mesh smoothing must not be set!"));
12002 
12003  // store highest level one of the cells adjacent to a vertex
12004  // belongs to
12005  std::fill (vertex_level.begin(), vertex_level.end(), 0);
12006  active_cell_iterator cell = begin_active(),
12007  endc = end();
12008  for (; cell!=endc; ++cell)
12009  {
12010  if (cell->refine_flag_set())
12011  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell;
12012  ++vertex)
12013  vertex_level[cell->vertex_index(vertex)]
12014  = std::max (vertex_level[cell->vertex_index(vertex)],
12015  cell->level()+1);
12016  else if (!cell->coarsen_flag_set())
12017  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell;
12018  ++vertex)
12019  vertex_level[cell->vertex_index(vertex)]
12020  = std::max (vertex_level[cell->vertex_index(vertex)],
12021  cell->level());
12022  else
12023  {
12024  // if coarsen flag is set then tentatively assume
12025  // that the cell will be coarsened. this isn't
12026  // always true (the coarsen flag could be removed
12027  // again) and so we may make an error here. we try
12028  // to correct this by iterating over the entire
12029  // process until we are converged
12030  Assert (cell->coarsen_flag_set(), ExcInternalError());
12031  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell;
12032  ++vertex)
12033  vertex_level[cell->vertex_index(vertex)]
12034  = std::max (vertex_level[cell->vertex_index(vertex)],
12035  cell->level()-1);
12036  }
12037  }
12038 
12039 
12040  // loop over all cells in reverse order. do so because we
12041  // can then update the vertex levels on the adjacent
12042  // vertices and maybe already flag additional cells in this
12043  // loop
12044  //
12045  // note that not only may we have to add additional
12046  // refinement flags, but we will also have to remove
12047  // coarsening flags on cells adjacent to vertices that will
12048  // see refinement
12049  for (cell=last_active(); cell != endc; --cell)
12050  if (cell->refine_flag_set() == false)
12051  {
12052  for (unsigned int vertex=0;
12053  vertex<GeometryInfo<dim>::vertices_per_cell; ++vertex)
12054  if (vertex_level[cell->vertex_index(vertex)] >=
12055  cell->level()+1)
12056  {
12057  // remove coarsen flag...
12058  cell->clear_coarsen_flag();
12059 
12060  // ...and if necessary also refine the current
12061  // cell, at the same time updating the level
12062  // information about vertices
12063  if (vertex_level[cell->vertex_index(vertex)] >
12064  cell->level()+1)
12065  {
12066  cell->set_refine_flag();
12067 
12068  for (unsigned int v=0; v<GeometryInfo<dim>::vertices_per_cell;
12069  ++v)
12070  vertex_level[cell->vertex_index(v)]
12071  = std::max (vertex_level[cell->vertex_index(v)],
12072  cell->level()+1);
12073  }
12074 
12075  // continue and see whether we may, for example,
12076  // go into the inner 'if' above based on a
12077  // different vertex
12078  }
12079  }
12080  }
12081 
12082  // loop over all cells. Flag all cells of which all children are
12083  // flagged for coarsening and delete the childrens' flags. Also
12084  // delete all flags of cells for which not all children of a
12085  // cell are flagged. In effect, only those cells are flagged of
12086  // which originally all children were flagged and for which all
12087  // children are on the same refinement level. For flagging, the
12088  // user flags are used, to avoid confusion and because
12089  // non-active cells can't be flagged for coarsening
12090  //
12091  // In effect, all coarsen flags are turned into user flags of
12092  // the mother cell if coarsening is possible or deleted
12093  // otherwise.
12094  clear_user_flags ();
12095  // Coarsen flags of cells with no mother cell, i.e. on the
12096  // coarsest level are deleted explicitly.
12097  active_cell_iterator acell = begin_active(0),
12098  end_ac = end_active(0);
12099  for (; acell!=end_ac; ++acell)
12100  acell->clear_coarsen_flag();
12101 
12102  cell_iterator cell = begin(),
12103  endc = end();
12104  for (; cell!=endc; ++cell)
12105  {
12106  // nothing to do if we are already on the finest level
12107  if (cell->active())
12108  continue;
12109 
12110  const unsigned int n_children=cell->n_children();
12111  unsigned int flagged_children=0;
12112  for (unsigned int child=0; child<n_children; ++child)
12113  if (cell->child(child)->active() &&
12114  cell->child(child)->coarsen_flag_set())
12115  {
12116  ++flagged_children;
12117  // clear flag since we don't need it anymore
12118  cell->child(child)->clear_coarsen_flag();
12119  }
12120 
12121  // flag this cell for coarsening if all children were
12122  // flagged
12123  if (flagged_children == n_children)
12124  cell->set_user_flag();
12125  }
12126 
12127  // in principle no coarsen flags should be set any more at this
12128  // point
12129 #if DEBUG
12130  for (cell=begin(); cell!=endc; ++cell)
12131  Assert (cell->coarsen_flag_set() == false, ExcInternalError());
12132 #endif
12133 
12134  // now loop over all cells which have the user flag set. their
12135  // children were flagged for coarsening. set the coarsen flag
12136  // again if we are sure that none of the neighbors of these
12137  // children are refined, or will be refined, since then we would
12138  // get a two-level jump in refinement. on the other hand, if one
12139  // of the children's neighbors has their user flag set, then we
12140  // know that its children will go away by coarsening, and we
12141  // will be ok.
12142  //
12143  // note on the other hand that we do allow level-2 jumps in
12144  // refinement between neighbors in 1d, so this whole procedure
12145  // is only necessary if we are not in 1d
12146  //
12147  // since we remove some coarsening/user flags in the process, we
12148  // have to work from the finest level to the coarsest one, since
12149  // we occasionally inspect user flags of cells on finer levels
12150  // and need to be sure that these flags are final
12151  for (cell=last(); cell!=endc; --cell)
12152  if (cell->user_flag_set())
12153  // if allowed: flag the
12154  // children for coarsening
12155  if (internal::TriangulationImplementation::Implementation::template coarsening_allowed<dim,spacedim>(cell))
12156  for (unsigned int c=0; c<cell->n_children(); ++c)
12157  {
12158  Assert (cell->child(c)->refine_flag_set()==false,
12159  ExcInternalError());
12160 
12161  cell->child(c)->set_coarsen_flag();
12162  }
12163 
12164  // clear all user flags again, now that we don't need them any
12165  // more
12166  clear_user_flags ();
12167 
12168 
12169  // now see if anything has changed in the last iteration of this
12170  // function
12171  std::vector<bool> current_coarsen_flags (n_active_cells());
12172  save_coarsen_flags (current_coarsen_flags);
12173 
12174  continue_iterating = (current_coarsen_flags != previous_coarsen_flags);
12175  previous_coarsen_flags = current_coarsen_flags;
12176  }
12177  while (continue_iterating == true);
12178 }
12179 
12180 
12181 //TODO: merge the following 3 functions since they are the same
12182 template <>
12184 {
12185  // save the flags to determine whether something was changed in the
12186  // course of this function
12187  std::vector<bool> flags_before;
12188  save_coarsen_flags (flags_before);
12189 
12190  // do nothing in 1d, except setting the coarsening flags correctly
12191  fix_coarsen_flags ();
12192 
12193  std::vector<bool> flags_after;
12194  save_coarsen_flags (flags_after);
12195 
12196  return (flags_before != flags_after);
12197 }
12198 
12199 
12200 template <>
12202 {
12203  // save the flags to determine whether something was changed in the
12204  // course of this function
12205  std::vector<bool> flags_before;
12206  save_coarsen_flags (flags_before);
12207 
12208  // do nothing in 1d, except setting the coarsening flags correctly
12209  fix_coarsen_flags ();
12210 
12211  std::vector<bool> flags_after;
12212  save_coarsen_flags (flags_after);
12213 
12214  return (flags_before != flags_after);
12215 }
12216 
12217 
12218 template <>
12220 {
12221  // save the flags to determine whether something was changed in the
12222  // course of this function
12223  std::vector<bool> flags_before;
12224  save_coarsen_flags (flags_before);
12225 
12226  // do nothing in 1d, except setting the coarsening flags correctly
12227  fix_coarsen_flags ();
12228 
12229  std::vector<bool> flags_after;
12230  save_coarsen_flags (flags_after);
12231 
12232  return (flags_before != flags_after);
12233 }
12234 
12235 
12236 
12237 
12238 namespace
12239 {
12240 
12241  // check if the given @param cell marked for coarsening would
12242  // produce an unrefined island. To break up long chains of these
12243  // cells we recursively check our neighbors in case we change this
12244  // cell. This reduces the number of outer iterations dramatically.
12245  template <int dim, int spacedim>
12246  void
12247  possibly_do_not_produce_unrefined_islands(
12248  const typename Triangulation<dim,spacedim>::cell_iterator &cell)
12249  {
12250  Assert (cell->has_children(), ExcInternalError());
12251 
12252  unsigned int n_neighbors=0;
12253  // count all neighbors that will be refined along the face of our
12254  // cell after the next step
12255  unsigned int count=0;
12256  for (unsigned int n=0; n<GeometryInfo<dim>::faces_per_cell; ++n)
12257  {
12258  const typename Triangulation<dim,spacedim>::cell_iterator neighbor = cell->neighbor(n);
12259  if (neighbor.state() == IteratorState::valid)
12260  {
12261  ++n_neighbors;
12262  if (face_will_be_refined_by_neighbor(cell,n))
12263  ++count;
12264  }
12265  }
12266  // clear coarsen flags if either all existing neighbors will be
12267  // refined or all but one will be and the cell is in the interior
12268  // of the domain
12269  if (count==n_neighbors ||
12270  (count>=n_neighbors-1 &&
12271  n_neighbors == GeometryInfo<dim>::faces_per_cell) )
12272  {
12273  for (unsigned int c=0; c<cell->n_children(); ++c)
12274  cell->child(c)->clear_coarsen_flag();
12275 
12276  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
12277  if (!cell->at_boundary(face)
12278  &&
12279  ( !cell->neighbor(face)->active() )
12280  && (cell_will_be_coarsened(cell->neighbor(face))) )
12281  possibly_do_not_produce_unrefined_islands<dim,spacedim>( cell->neighbor(face) );
12282  }
12283  }
12284 
12285 
12286  // see if the current cell needs to be refined to avoid unrefined
12287  // islands.
12288  //
12289  // there are sometimes chains of cells that induce refinement of
12290  // each other. to avoid running the loop in
12291  // prepare_coarsening_and_refinement over and over again for each
12292  // one of them, at least for the isotropic refinement case we seek
12293  // to flag neighboring elements as well as necessary. this takes
12294  // care of (slightly pathological) cases like
12295  // deal.II/mesh_smoothing_03
12296  template <int dim, int spacedim>
12297  void
12298  possibly_refine_unrefined_island
12299  (const typename Triangulation<dim,spacedim>::cell_iterator &cell,
12300  const bool allow_anisotropic_smoothing)
12301  {
12302  Assert (cell->has_children() == false, ExcInternalError());
12303  Assert (cell->refine_flag_set() == false, ExcInternalError());
12304 
12305 
12306  // now we provide two algorithms. the first one is the standard
12307  // one, coming from the time, where only isotropic refinement was
12308  // possible. it simply counts the neighbors that are or will be
12309  // refined and compares to the number of other ones. the second
12310  // one does this check independently for each direction: if all
12311  // neighbors in one direction (normally two, at the boundary only
12312  // one) are refined, the current cell is flagged to be refined in
12313  // an according direction.
12314 
12315  if (allow_anisotropic_smoothing == false)
12316  {
12317  // use first algorithm
12318  unsigned int refined_neighbors = 0,
12319  unrefined_neighbors = 0;
12320  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
12321  if (!cell->at_boundary(face))
12322  {
12323  if (face_will_be_refined_by_neighbor(cell,face))
12324  ++refined_neighbors;
12325  else
12326  ++unrefined_neighbors;
12327  }
12328 
12329  if (unrefined_neighbors < refined_neighbors)
12330  {
12331  cell->clear_coarsen_flag();
12332  cell->set_refine_flag ();
12333 
12334  // ok, so now we have flagged this cell. if we know that
12335  // there were any unrefined neighbors at all, see if any
12336  // of those will have to be refined as well
12337  if (unrefined_neighbors > 0)
12338  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
12339  if (!cell->at_boundary(face)
12340  &&
12341  (face_will_be_refined_by_neighbor(cell,face) == false)
12342  &&
12343  (cell->neighbor(face)->has_children() == false)
12344  &&
12345  (cell->neighbor(face)->refine_flag_set() == false))
12346  possibly_refine_unrefined_island<dim,spacedim>
12347  (cell->neighbor(face),
12348  allow_anisotropic_smoothing);
12349  }
12350  }
12351  else
12352  {
12353  // variable to store the cell refine case needed to fulfill
12354  // all smoothing requirements
12355  RefinementCase<dim> smoothing_cell_refinement_case
12357 
12358  // use second algorithm, do the check individually for each
12359  // direction
12360  for (unsigned int face_pair=0;
12361  face_pair<GeometryInfo<dim>::faces_per_cell/2; ++face_pair)
12362  {
12363  // variable to store the cell refine case needed to refine
12364  // at the current face pair in the same way as the
12365  // neighbors do...
12366  RefinementCase<dim> directional_cell_refinement_case
12368 
12369  for (unsigned int face_index=0; face_index<2; ++face_index)
12370  {
12371  unsigned int face=2*face_pair+face_index;
12372  // variable to store the refine case (to come) of the
12373  // face under consideration
12374  RefinementCase<dim-1> expected_face_ref_case
12375  = RefinementCase<dim-1>::no_refinement;
12376 
12377  if (cell->neighbor(face).state() == IteratorState::valid)
12378  face_will_be_refined_by_neighbor<dim,spacedim>(cell,face,expected_face_ref_case);
12379  // now extract which refine case would be necessary to
12380  // achieve the same face refinement. set the
12381  // intersection with other requirements for the same
12382  // direction.
12383 
12384  // note: using the intersection is not an obvious
12385  // decision, we could also argue that it is more
12386  // natural to use the union. however, intersection is
12387  // the less aggressive tactic and favours a smaller
12388  // number of refined cells over an intensive
12389  // smoothing. this way we try not to lose too much of
12390  // the effort we put in anisotropic refinement
12391  // indicators due to overly aggressive smoothing...
12392  directional_cell_refinement_case
12393  = (directional_cell_refinement_case &
12395  expected_face_ref_case,
12396  face,
12397  cell->face_orientation(face),
12398  cell->face_flip(face),
12399  cell->face_rotation(face)));
12400  }//for both face indices
12401  // if both requirements sum up to something useful, add
12402  // this to the refine case for smoothing. note: if
12403  // directional_cell_refinement_case is isotropic still,
12404  // then something went wrong...
12405  Assert(directional_cell_refinement_case <
12407  ExcInternalError());
12408  smoothing_cell_refinement_case = smoothing_cell_refinement_case |
12409  directional_cell_refinement_case;
12410  }//for all face_pairs
12411  // no we collected contributions from all directions. combine
12412  // the new flags with the existing refine case, but only if
12413  // smoothing is required
12414  if (smoothing_cell_refinement_case)
12415  {
12416  cell->clear_coarsen_flag();
12417  cell->set_refine_flag(cell->refine_flag_set() |
12418  smoothing_cell_refinement_case);
12419  }
12420  }
12421  }
12422 }
12423 
12424 
12425 template <int dim, int spacedim>
12427 {
12428  // save the flags to determine whether something was changed in the
12429  // course of this function
12430  std::vector<bool> flags_before[2];
12431  save_coarsen_flags (flags_before[0]);
12432  save_refine_flags (flags_before[1]);
12433 
12434  // save the flags at the outset of each loop. we do so in order to
12435  // find out whether something was changed in the present loop, in
12436  // which case we would have to re-run the loop. the other
12437  // possibility to find this out would be to set a flag
12438  // @p{something_changed} to true each time we change something.
12439  // however, sometimes one change in one of the parts of the loop is
12440  // undone by another one, so we might end up in an endless loop. we
12441  // could be tempted to break this loop at an arbitrary number of
12442  // runs, but that would not be a clean solution, since we would
12443  // either have to 1/ break the loop too early, in which case the
12444  // promise that a second call to this function immediately after the
12445  // first one does not change anything, would be broken, or 2/ we do
12446  // as many loops as there are levels. we know that information is
12447  // transported over one level in each run of the loop, so this is
12448  // enough. Unfortunately, each loop is rather expensive, so we chose
12449  // the way presented here
12450  std::vector<bool> flags_before_loop[2] = {flags_before[0],
12451  flags_before[1]
12452  };
12453 
12454  // now for what is done in each loop: we have to fulfill several
12455  // tasks at the same time, namely several mesh smoothing algorithms
12456  // and mesh regularization, by which we mean that the next mesh
12457  // fulfills several requirements such as no double refinement at
12458  // each face or line, etc.
12459  //
12460  // since doing these things at once seems almost impossible (in the
12461  // first year of this library, they were done in two functions, one
12462  // for refinement and one for coarsening, and most things within
12463  // these were done at once, so the code was rather impossible to
12464  // join into this, only, function), we do them one after each
12465  // other. the order in which we do them is such that the important
12466  // tasks, namely regularization, are done last and the least
12467  // important things are done the first. the following order is
12468  // chosen:
12469  //
12470  // 0/ Only if coarsest_level_1 or patch_level_1 is set: clear all
12471  // coarsen flags on level 1 to avoid level 0 cells being created
12472  // by coarsening. As coarsen flags will never be added, this can
12473  // be done once and for all before the actual loop starts.
12474  //
12475  // 1/ do not coarsen a cell if 'most of the neighbors' will be
12476  // refined after the step. This is to prevent occurrence of
12477  // unrefined islands.
12478  //
12479  // 2/ eliminate refined islands in the interior and at the
12480  // boundary. since they don't do much harm besides increasing the
12481  // number of degrees of freedom, doing this has a rather low
12482  // priority.
12483  //
12484  // 3/ limit the level difference of neighboring cells at each
12485  // vertex.
12486  //
12487  // 4/ eliminate unrefined islands. this has higher priority since
12488  // this diminishes the approximation properties not only of the
12489  // unrefined island, but also of the surrounding patch.
12490  //
12491  // 5/ ensure patch level 1. Then the triangulation consists of
12492  // patches, i.e. of cells that are refined once. It follows that
12493  // if at least one of the children of a cell is or will be
12494  // refined than all children need to be refined. This step only
12495  // sets refinement flags and does not set coarsening flags. If
12496  // the patch_level_1 flag is set, then
12497  // eliminate_unrefined_islands, eliminate_refined_inner_islands
12498  // and eliminate_refined_boundary_islands will be fulfilled
12499  // automatically and do not need to be enforced separately.
12500  //
12501  // 6/ take care of the requirement that no double refinement is done
12502  // at each face
12503  //
12504  // 7/ take care that no double refinement is done at each line in 3d
12505  // or higher dimensions.
12506  //
12507  // 8/ make sure that all children of each cell are either flagged
12508  // for coarsening or none of the children is
12509  //
12510  // For some of these steps, it is known that they interact. Namely,
12511  // it is not possible to guarantee that after step 6 another step 5
12512  // would have no effect; the same holds for the opposite order and
12513  // also when taking into account step 7. however, it is important to
12514  // guarantee that step five or six do not undo something that step 5
12515  // did, and step 7 not something of step 6, otherwise the
12516  // requirements will not be satisfied even if the loop
12517  // terminates. this is accomplished by the fact that steps 5 and 6
12518  // only *add* refinement flags and delete coarsening flags
12519  // (therefore, step 6 can't undo something that step 4 already did),
12520  // and step 7 only deletes coarsening flags, never adds some. step 7
12521  // needs also take care that it won't tag cells for refinement for
12522  // which some neighbors are more refined or will be refined.
12523 
12525  // STEP 0:
12526  // Only if coarsest_level_1 or patch_level_1 is set: clear all
12527  // coarsen flags on level 1 to avoid level 0 cells being created
12528  // by coarsening.
12529  if (((smooth_grid & coarsest_level_1) ||
12530  (smooth_grid & patch_level_1)) && n_levels()>=2)
12531  {
12533  cell=begin_active(1),
12534  endc=end_active(1);
12535 
12536  for (; cell!=endc; ++cell)
12537  cell->clear_coarsen_flag();
12538  }
12539 
12540  bool mesh_changed_in_this_loop = false;
12541  do
12542  {
12544  // STEP 1:
12545  // do not coarsen a cell if 'most of the neighbors' will be
12546  // refined after the step. This is to prevent the occurrence
12547  // of unrefined islands. If patch_level_1 is set, this will
12548  // be automatically fulfilled.
12549  if (smooth_grid & do_not_produce_unrefined_islands &&
12550  !(smooth_grid & patch_level_1))
12551  {
12552  cell_iterator cell;
12553  const cell_iterator endc = end();
12554 
12555  for (cell=begin(); cell!=endc; ++cell)
12556  {
12557  // only do something if this
12558  // cell will be coarsened
12559  if (!cell->active() && cell_will_be_coarsened(cell))
12560  possibly_do_not_produce_unrefined_islands<dim,spacedim>(cell);
12561  }
12562  }
12563 
12564 
12566  // STEP 2:
12567  // eliminate refined islands in the interior and at the
12568  // boundary. since they don't do much harm besides increasing
12569  // the number of degrees of freedom, doing this has a rather
12570  // low priority. If patch_level_1 is set, this will be
12571  // automatically fulfilled.
12572  //
12573  // there is one corner case to consider: if this is a
12574  // distributed triangulation, there may be refined islands on
12575  // the boundary of which we own only part (e.g. a single cell
12576  // in the corner of a domain). the rest of the island is
12577  // ghost cells and it *looks* like the area around it
12578  // (artificial cells) are coarser but this is only because
12579  // they may actually be equally fine on other
12580  // processors. it's hard to detect this case but we can do
12581  // the following: only set coarsen flags to remove this
12582  // refined island if all cells we want to set flags on are
12583  // locally owned
12584  if (smooth_grid & (eliminate_refined_inner_islands |
12585  eliminate_refined_boundary_islands) &&
12586  !(smooth_grid & patch_level_1))
12587  {
12588  cell_iterator cell;
12589  const cell_iterator endc = end();
12590 
12591  for (cell=begin(); cell!=endc; ++cell)
12592  if (!cell->active() ||
12593  (cell->active() &&
12594  cell->refine_flag_set() &&
12595  cell->is_locally_owned()))
12596  {
12597  // check whether all children are active, i.e. not
12598  // refined themselves. This is a precondition that the
12599  // children may be coarsened away. If the cell is only
12600  // flagged for refinement, then all future children
12601  // will be active
12602  bool all_children_active = true;
12603  if (!cell->active())
12604  for (unsigned int c=0; c<cell->n_children(); ++c)
12605  if (!cell->child(c)->active() ||
12606  cell->child(c)->is_ghost() ||
12607  cell->child(c)->is_artificial())
12608  {
12609  all_children_active = false;
12610  break;
12611  }
12612 
12613  if (all_children_active)
12614  {
12615  // count number of refined and unrefined neighbors
12616  // of cell. neighbors on lower levels are counted
12617  // as unrefined since they can only get to the
12618  // same level as this cell by the next refinement
12619  // cycle
12620  unsigned int unrefined_neighbors = 0,
12621  total_neighbors = 0;
12622 
12623  for (unsigned int n=0; n<GeometryInfo<dim>::faces_per_cell; ++n)
12624  {
12625  const cell_iterator neighbor = cell->neighbor(n);
12626  if (neighbor.state() == IteratorState::valid)
12627  {
12628  ++total_neighbors;
12629 
12630  if (!face_will_be_refined_by_neighbor(cell,n))
12631  ++unrefined_neighbors;
12632  }
12633 
12634  }
12635 
12636  // if all neighbors unrefined: mark this cell for
12637  // coarsening or don't refine if marked for that
12638  //
12639  // also do the distinction between the two
12640  // versions of the eliminate_refined_*_islands
12641  // flag
12642  //
12643  // the last check is whether there are any
12644  // neighbors at all. if not so, then we are (e.g.)
12645  // on the coarsest grid with one cell, for which,
12646  // of course, we do not remove the refine flag.
12647  if ((unrefined_neighbors == total_neighbors)
12648  &&
12649  (((unrefined_neighbors==GeometryInfo<dim>::faces_per_cell) &&
12650  (smooth_grid & eliminate_refined_inner_islands)) ||
12651  ((unrefined_neighbors<GeometryInfo<dim>::faces_per_cell) &&
12652  (smooth_grid & eliminate_refined_boundary_islands)) )
12653  &&
12654  (total_neighbors != 0))
12655  {
12656  if (!cell->active())
12657  for (unsigned int c=0; c<cell->n_children(); ++c)
12658  {
12659  cell->child(c)->clear_refine_flag ();
12660  cell->child(c)->set_coarsen_flag ();
12661  }
12662  else
12663  cell->clear_refine_flag();
12664  }
12665  }
12666  }
12667  }
12668 
12670  // STEP 3:
12671  // limit the level difference of neighboring cells at each
12672  // vertex.
12673  //
12674  // in case of anisotropic refinement this does not make
12675  // sense. as soon as one cell is anisotropically refined, an
12676  // Assertion is thrown. therefore we can ignore this problem
12677  // later on
12678  if (smooth_grid & limit_level_difference_at_vertices)
12679  {
12680  Assert(!anisotropic_refinement,
12681  ExcMessage("In case of anisotropic refinement the "
12682  "limit_level_difference_at_vertices flag for "
12683  "mesh smoothing must not be set!"));
12684 
12685  // store highest level one of the cells adjacent to a vertex
12686  // belongs to
12687  std::vector<int> vertex_level (vertices.size(), 0);
12688  active_cell_iterator cell = begin_active(),
12689  endc = end();
12690  for (; cell!=endc; ++cell)
12691  {
12692  if (cell->refine_flag_set())
12693  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell;
12694  ++vertex)
12695  vertex_level[cell->vertex_index(vertex)]
12696  = std::max (vertex_level[cell->vertex_index(vertex)],
12697  cell->level()+1);
12698  else if (!cell->coarsen_flag_set())
12699  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell;
12700  ++vertex)
12701  vertex_level[cell->vertex_index(vertex)]
12702  = std::max (vertex_level[cell->vertex_index(vertex)],
12703  cell->level());
12704  else
12705  {
12706  // if coarsen flag is set then tentatively assume
12707  // that the cell will be coarsened. this isn't
12708  // always true (the coarsen flag could be removed
12709  // again) and so we may make an error here
12710  Assert (cell->coarsen_flag_set(), ExcInternalError());
12711  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell;
12712  ++vertex)
12713  vertex_level[cell->vertex_index(vertex)]
12714  = std::max (vertex_level[cell->vertex_index(vertex)],
12715  cell->level()-1);
12716  }
12717  }
12718 
12719 
12720  // loop over all cells in reverse order. do so because we
12721  // can then update the vertex levels on the adjacent
12722  // vertices and maybe already flag additional cells in this
12723  // loop
12724  //
12725  // note that not only may we have to add additional
12726  // refinement flags, but we will also have to remove
12727  // coarsening flags on cells adjacent to vertices that will
12728  // see refinement
12729  for (cell=last_active(); cell != endc; --cell)
12730  if (cell->refine_flag_set() == false)
12731  {
12732  for (unsigned int vertex=0;
12733  vertex<GeometryInfo<dim>::vertices_per_cell; ++vertex)
12734  if (vertex_level[cell->vertex_index(vertex)] >=
12735  cell->level()+1)
12736  {
12737  // remove coarsen flag...
12738  cell->clear_coarsen_flag();
12739 
12740  // ...and if necessary also refine the current
12741  // cell, at the same time updating the level
12742  // information about vertices
12743  if (vertex_level[cell->vertex_index(vertex)] >
12744  cell->level()+1)
12745  {
12746  cell->set_refine_flag();
12747 
12748  for (unsigned int v=0; v<GeometryInfo<dim>::vertices_per_cell;
12749  ++v)
12750  vertex_level[cell->vertex_index(v)]
12751  = std::max (vertex_level[cell->vertex_index(v)],
12752  cell->level()+1);
12753  }
12754 
12755  // continue and see whether we may, for example,
12756  // go into the inner'if'
12757  // above based on a
12758  // different vertex
12759  }
12760  }
12761  }
12762 
12764  // STEP 4:
12765  // eliminate unrefined islands. this has higher priority
12766  // since this diminishes the approximation properties not
12767  // only of the unrefined island, but also of the surrounding
12768  // patch.
12769  //
12770  // do the loop from finest to coarsest cells since we may
12771  // trigger a cascade by marking cells for refinement which
12772  // may trigger more cells further down below
12773  if (smooth_grid & eliminate_unrefined_islands)
12774  {
12775  active_cell_iterator cell=last_active(),
12776  endc=end();
12777 
12778  for (; cell != endc; --cell)
12779  // only do something if cell is not already flagged for
12780  // (isotropic) refinement
12781  if (cell->refine_flag_set() != RefinementCase<dim>::isotropic_refinement)
12782  possibly_refine_unrefined_island<dim,spacedim>
12783  (cell,
12784  (smooth_grid & allow_anisotropic_smoothing) != 0);
12785  }
12786 
12788  // STEP 5:
12789  // ensure patch level 1.
12790  //
12791  // Introduce some terminology:
12792  // - a cell that is refined
12793  // once is a patch of
12794  // level 1 simply called patch.
12795  // - a cell that is globally
12796  // refined twice is called
12797  // a patch of level 2.
12798  // - patch level n says that
12799  // the triangulation consists
12800  // of patches of level n.
12801  // This makes sense only
12802  // if the grid is already at
12803  // least n times globally
12804  // refined.
12805  //
12806  // E.g. from patch level 1 follows: if at least one of the
12807  // children of a cell is or will be refined than enforce all
12808  // children to be refined.
12809 
12810  // This step 4 only sets refinement flags and does not set
12811  // coarsening flags.
12812  if (smooth_grid & patch_level_1)
12813  {
12814 
12815  // An important assumption (A) is that before calling this
12816  // function the grid was already of patch level 1.
12817 
12818  // loop over all cells whose children are all active. (By
12819  // assumption (A) either all or none of the children are
12820  // active). If the refine flag of at least one of the
12821  // children is set then set_refine_flag and
12822  // clear_coarsen_flag of all children.
12823  for (cell_iterator cell = begin(); cell != end(); ++cell)
12824  if (!cell->active())
12825  {
12826  // ensure the invariant. we can then check whether all
12827  // of its children are further refined or not by
12828  // simply looking at the first child
12829  Assert (cell_is_patch_level_1(cell),
12830  ExcInternalError());
12831  if (cell->child(0)->has_children() == true)
12832  continue;
12833 
12834  // cell is found to be a patch. combine the refine
12835  // cases of all children
12837  for (unsigned int i=0; i<cell->n_children(); ++i)
12838  combined_ref_case = combined_ref_case |
12839  cell->child(i)->refine_flag_set();
12840  if (combined_ref_case != RefinementCase<dim>::no_refinement)
12841  for (unsigned int i=0; i<cell->n_children(); ++i)
12842  {
12843  cell_iterator child = cell->child(i);
12844 
12845  child->clear_coarsen_flag();
12846  child->set_refine_flag(combined_ref_case);
12847  }
12848  }
12849 
12850  // The code above dealt with the case where we may get a
12851  // non-patch_level_1 mesh from refinement. Now also deal
12852  // with the case where we could get such a mesh by
12853  // coarsening. Coarsen the children (and remove the
12854  // grandchildren) only if all cell->grandchild(i)
12855  // ->coarsen_flag_set() are set.
12856  //
12857  // for a case where this is a bit tricky, take a look at the
12858  // mesh_smoothing_0[12] testcases
12859  for (cell_iterator cell = begin(); cell != end(); ++cell)
12860  {
12861  // check if this cell has active grandchildren. note
12862  // that we know that it is patch_level_1, i.e. if one of
12863  // its children is active then so are all, and it isn't
12864  // going to have any grandchildren at all:
12865  if (cell->active()
12866  ||
12867  cell->child(0)->active())
12868  continue;
12869 
12870  // cell is not active, and so are none of its
12871  // children. check the grandchildren. note that the
12872  // children are also patch_level_1, and so we only ever
12873  // need to check their first child
12874  const unsigned int n_children=cell->n_children();
12875  bool has_active_grandchildren = false;
12876 
12877  for (unsigned int i=0; i<n_children; ++i)
12878  if (cell->child(i)->child(0)->active())
12879  {
12880  has_active_grandchildren = true;
12881  break;
12882  }
12883 
12884  if (has_active_grandchildren == false)
12885  continue;
12886 
12887 
12888  // ok, there are active grandchildren. see if either all
12889  // or none of them are flagged for coarsening
12890  unsigned int n_grandchildren=0;
12891 
12892  // count all coarsen flags of the grandchildren.
12893  unsigned int n_coarsen_flags=0;
12894 
12895  // cell is not a patch (of level 1) as it has a
12896  // grandchild. Is cell a patch of level 2?? Therefore:
12897  // find out whether all cell->child(i) are patches
12898  for (unsigned int c=0; c<n_children; ++c)
12899  {
12900  // get at the child. by assumption (A), and the
12901  // check by which we got here, the child is not
12902  // active
12903  cell_iterator child=cell->child(c);
12904 
12905  const unsigned int nn_children=child->n_children();
12906  n_grandchildren += nn_children;
12907 
12908  // if child is found to be a patch of active cells
12909  // itself, then add up how many of its children are
12910  // supposed to be coarsened
12911  if (child->child(0)->active())
12912  for (unsigned int cc=0; cc<nn_children; ++cc)
12913  if (child->child(cc)->coarsen_flag_set())
12914  ++n_coarsen_flags;
12915  }
12916 
12917  // if not all grandchildren are supposed to be coarsened
12918  // (e.g. because some simply don't have the flag set, or
12919  // because they are not active and therefore cannot
12920  // carry the flag), then remove the coarsen flag from
12921  // all of the active grandchildren. note that there may
12922  // be coarsen flags on the grandgrandchildren -- we
12923  // don't clear them here, but we'll get to them in later
12924  // iterations if necessary
12925  //
12926  // there is nothing we have to do if no coarsen flags
12927  // have been set at all
12928  if ((n_coarsen_flags != n_grandchildren)
12929  &&
12930  (n_coarsen_flags > 0))
12931  for (unsigned int c=0; c<n_children; ++c)
12932  {
12933  const cell_iterator child = cell->child(c);
12934  if (child->child(0)->active())
12935  for (unsigned int cc=0; cc<child->n_children(); ++cc)
12936  child->child(cc)->clear_coarsen_flag();
12937  }
12938  }
12939  }
12940 
12942  //
12943  // at the boundary we could end up with cells with negative
12944  // volume or at least with a part, that is negative, if the
12945  // cell is refined anisotropically. we have to check, whether
12946  // that can happen
12948 
12950  // STEP 6:
12951  // take care of the requirement that no
12952  // double refinement is done at each face
12953  //
12954  // in case of anisotropic refinement it is only likely, but
12955  // not sure, that the cells, which are more refined along a
12956  // certain face common to two cells are on a higher
12957  // level. therefore we cannot be sure, that the requirement
12958  // of no double refinement is fulfilled after a single pass
12959  // of the following actions. We could just wait for the next
12960  // global loop. when this function terminates, the
12961  // requirement will be fulfilled. However, it might be faster
12962  // to insert an inner loop here.
12963  bool changed = true;
12964  while (changed)
12965  {
12966  changed=false;
12967  active_cell_iterator cell=last_active(),
12968  endc=end();
12969 
12970  for (; cell != endc; --cell)
12971  if (cell->refine_flag_set())
12972  {
12973  // loop over neighbors of cell
12974  for (unsigned int i=0; i<GeometryInfo<dim>::faces_per_cell; ++i)
12975  {
12976  // only do something if the face is not at the
12977  // boundary and if the face will be refined with
12978  // the RefineCase currently flagged for
12979  if (cell->neighbor(i).state() == IteratorState::valid &&
12980  (GeometryInfo<dim>::face_refinement_case(cell->refine_flag_set(),
12981  i)
12983  {
12984  // 1) if the neighbor has children: nothing to
12985  // worry about. 2) if the neighbor is active
12986  // and a coarser one, ensure, that its
12987  // refine_flag is set 3) if the neighbor is
12988  // active and as refined along the face as our
12989  // current cell, make sure, that no
12990  // coarsen_flag is set. if we remove the
12991  // coarsen flag of our neighbor,
12992  // fix_coarsen_flags() makes sure, that the
12993  // mother cell will not be coarsened
12994  if (cell->neighbor(i)->active())
12995  {
12996  if (cell->neighbor_is_coarser(i))
12997  {
12998  if (cell->neighbor(i)->coarsen_flag_set())
12999  cell->neighbor(i)->clear_coarsen_flag();
13000  // we'll set the refine flag for this
13001  // neighbor below. we note, that we
13002  // have changed something by setting
13003  // the changed flag to true. We do not
13004  // need to do so, if we just removed
13005  // the coarsen flag, as the changed
13006  // flag only indicates the need to
13007  // re-run the inner loop. however, we
13008  // only loop over cells flagged for
13009  // refinement here, so nothing to
13010  // worry about if we remove coarsen
13011  // flags
13012 
13013  if (dim==2)
13014  {
13015  if (smooth_grid & allow_anisotropic_smoothing)
13016  changed=cell->neighbor(i)->flag_for_face_refinement(cell->neighbor_of_coarser_neighbor(i).first,
13017  RefinementCase<dim-1>::cut_x);
13018  else
13019  {
13020  if (!cell->neighbor(i)->refine_flag_set())
13021  changed=true;
13022  cell->neighbor(i)->set_refine_flag();
13023  }
13024  }
13025  else //i.e. if (dim==3)
13026  {
13027 // ugly situations might arise here, consider the following situation, which
13028 // shows neighboring cells at the common face, where the upper right element is
13029 // coarser at the given face. Now the upper child element of the lower left
13030 // wants to refine according to cut_z, such that there is a 'horizontal'
13031 // refinement of the face marked with #####
13032 //
13033 // / /
13034 // / /
13035 // *---------------*
13036 // | |
13037 // | |
13038 // | |
13039 // | |
13040 // | |
13041 // | | /
13042 // | |/
13043 // *---------------*
13044 //
13045 //
13046 // *---------------*
13047 // /| /|
13048 // / | ##### / |
13049 // | |
13050 // *---------------*
13051 // /| /|
13052 // / | / |
13053 // | |
13054 // *---------------*
13055 // / /
13056 // / /
13057 //
13058 // this introduces too many hanging nodes and the neighboring (coarser) cell
13059 // (upper right) has to be refined. If it is only refined according to cut_z,
13060 // then everything is ok:
13061 //
13062 // / /
13063 // / /
13064 // *---------------*
13065 // | |
13066 // | | /
13067 // | |/
13068 // *---------------*
13069 // | |
13070 // | | /
13071 // | |/
13072 // *---------------*
13073 //
13074 //
13075 // *---------------*
13076 // /| /|
13077 // / *---------------*
13078 // /| /|
13079 // *---------------*
13080 // /| /|
13081 // / | / |
13082 // | |
13083 // *---------------*
13084 // / /
13085 // / /
13086 //
13087 // if however the cell wants to refine itself in an other way, or if we disallow
13088 // anisotropic smoothing, then simply refining the neighbor isotropically is not
13089 // going to work, since this introduces a refinement of face ##### with both
13090 // cut_x and cut_y, which is not possible:
13091 //
13092 // / / /
13093 // / / /
13094 // *-------*-------*
13095 // | | |
13096 // | | | /
13097 // | | |/
13098 // *-------*-------*
13099 // | | |
13100 // | | | /
13101 // | | |/
13102 // *-------*-------*
13103 //
13104 //
13105 // *---------------*
13106 // /| /|
13107 // / *---------------*
13108 // /| /|
13109 // *---------------*
13110 // /| /|
13111 // / | / |
13112 // | |
13113 // *---------------*
13114 // / /
13115 // / /
13116 //
13117 // thus, in this case we also need to refine our current cell in the new
13118 // direction:
13119 //
13120 // / / /
13121 // / / /
13122 // *-------*-------*
13123 // | | |
13124 // | | | /
13125 // | | |/
13126 // *-------*-------*
13127 // | | |
13128 // | | | /
13129 // | | |/
13130 // *-------*-------*
13131 //
13132 //
13133 // *-------*-------*
13134 // /| /| /|
13135 // / *-------*-------*
13136 // /| /| /|
13137 // *-------*-------*
13138 // /| / /|
13139 // / | / |
13140 // | |
13141 // *---------------*
13142 // / /
13143 // / /
13144 
13145  std::pair<unsigned int, unsigned int> nb_indices
13146  =cell->neighbor_of_coarser_neighbor(i);
13147  unsigned int refined_along_x=0,
13148  refined_along_y=0,
13149  to_be_refined_along_x=0,
13150  to_be_refined_along_y=0;
13151 
13152  const int this_face_index=cell->face_index(i);
13153 
13154 // step 1: detect, along which axis the face is currently refined
13155  if ((this_face_index
13156  == cell->neighbor(i)->face(nb_indices.first)->child_index(0)) ||
13157  (this_face_index
13158  == cell->neighbor(i)->face(nb_indices.first)->child_index(1)))
13159  {
13160  // this might be an
13161  // anisotropic child. get the
13162  // face refine case of the
13163  // neighbors face and count
13164  // refinements in x and y
13165  // direction.
13166  RefinementCase<dim-1> frc=cell->neighbor(i)->face(nb_indices.first)->refinement_case();
13167  if (frc & RefinementCase<dim>::cut_x)
13168  ++refined_along_x;
13169  if (frc & RefinementCase<dim>::cut_y)
13170  ++refined_along_y;
13171  }
13172  else
13173  // this has to be an isotropic
13174  // child
13175  {
13176  ++refined_along_x;
13177  ++refined_along_y;
13178  }
13179 // step 2: detect, along which axis the face has to be refined given the current
13180 // refine flag
13181  RefinementCase<dim-1> flagged_frc=
13182  GeometryInfo<dim>::face_refinement_case(cell->refine_flag_set(),
13183  i,
13184  cell->face_orientation(i),
13185  cell->face_flip(i),
13186  cell->face_rotation(i));
13187  if (flagged_frc & RefinementCase<dim>::cut_x)
13188  ++to_be_refined_along_x;
13189  if (flagged_frc & RefinementCase<dim>::cut_y)
13190  ++to_be_refined_along_y;
13191 
13192 // step 3: set the refine flag of the (coarser and active) neighbor.
13193  if ((smooth_grid & allow_anisotropic_smoothing) ||
13194  cell->neighbor(i)->refine_flag_set())
13195  {
13196  if (refined_along_x + to_be_refined_along_x > 1)
13197  changed |= cell->neighbor(i)->flag_for_face_refinement(nb_indices.first,
13199  if (refined_along_y + to_be_refined_along_y > 1)
13200  changed |= cell->neighbor(i)->flag_for_face_refinement(nb_indices.first,
13202  }
13203  else
13204  {
13205  if (cell->neighbor(i)->refine_flag_set()!=RefinementCase<dim>::isotropic_refinement)
13206  changed=true;
13207  cell->neighbor(i)->set_refine_flag();
13208  }
13209 
13210 // step 4: if necessary (see above) add to the refine flag of the current cell
13211  cell_iterator nb=cell->neighbor(i);
13212  RefinementCase<dim-1> nb_frc
13213  = GeometryInfo<dim>::face_refinement_case(nb->refine_flag_set(),
13214  nb_indices.first,
13215  nb->face_orientation(nb_indices.first),
13216  nb->face_flip(nb_indices.first),
13217  nb->face_rotation(nb_indices.first));
13218  if ((nb_frc & RefinementCase<dim>::cut_x) &&
13219  !(refined_along_x || to_be_refined_along_x))
13220  changed |= cell->flag_for_face_refinement(i,RefinementCase<dim-1>::cut_axis(0));
13221  if ((nb_frc & RefinementCase<dim>::cut_y) &&
13222  !(refined_along_y || to_be_refined_along_y))
13223  changed |= cell->flag_for_face_refinement(i,RefinementCase<dim-1>::cut_axis(1));
13224  }
13225  }// if neighbor is coarser
13226  else // -> now the neighbor is not coarser
13227  {
13228  cell->neighbor(i)->clear_coarsen_flag();
13229  const unsigned int nb_nb=cell->neighbor_of_neighbor(i);
13230  const cell_iterator neighbor=cell->neighbor(i);
13231  RefinementCase<dim-1> face_ref_case=
13232  GeometryInfo<dim>::face_refinement_case(neighbor->refine_flag_set(),
13233  nb_nb,
13234  neighbor->face_orientation(nb_nb),
13235  neighbor->face_flip(nb_nb),
13236  neighbor->face_rotation(nb_nb));
13237  RefinementCase<dim-1> needed_face_ref_case
13238  =GeometryInfo<dim>::face_refinement_case(cell->refine_flag_set(),
13239  i,
13240  cell->face_orientation(i),
13241  cell->face_flip(i),
13242  cell->face_rotation(i));
13243  // if the neighbor wants to refine the
13244  // face with cut_x and we want cut_y
13245  // or vice versa, we have to refine
13246  // isotropically at the given face
13247  if ((face_ref_case==RefinementCase<dim>::cut_x && needed_face_ref_case==RefinementCase<dim>::cut_y) ||
13248  (face_ref_case==RefinementCase<dim>::cut_y && needed_face_ref_case==RefinementCase<dim>::cut_x))
13249  {
13250  changed=cell->flag_for_face_refinement(i, face_ref_case);
13251  neighbor->flag_for_face_refinement(nb_nb, needed_face_ref_case);
13252  }
13253  }
13254  }
13255  else //-> the neighbor is not active
13256  {
13257  RefinementCase<dim-1> face_ref_case = cell->face(i)->refinement_case(),
13258  needed_face_ref_case = GeometryInfo<dim>::face_refinement_case(cell->refine_flag_set(),
13259  i,
13260  cell->face_orientation(i),
13261  cell->face_flip(i),
13262  cell->face_rotation(i));
13263  // if the face is refined with cut_x and
13264  // we want cut_y or vice versa, we have to
13265  // refine isotropically at the given face
13266  if ((face_ref_case==RefinementCase<dim>::cut_x && needed_face_ref_case==RefinementCase<dim>::cut_y) ||
13267  (face_ref_case==RefinementCase<dim>::cut_y && needed_face_ref_case==RefinementCase<dim>::cut_x))
13268  changed=cell->flag_for_face_refinement(i, face_ref_case);
13269  }
13270  }
13271  }
13272  }
13273  }
13274 
13276  // STEP 7:
13277  // take care that no double refinement
13278  // is done at each line in 3d or higher
13279  // dimensions.
13281 
13283  // STEP 8:
13284  // make sure that all children of each
13285  // cell are either flagged for coarsening
13286  // or none of the children is
13287  fix_coarsen_flags ();
13288  // get the refinement and coarsening
13289  // flags
13290  std::vector<bool> flags_after_loop[2];
13291  save_coarsen_flags (flags_after_loop[0]);
13292  save_refine_flags (flags_after_loop[1]);
13293 
13294  // find out whether something was
13295  // changed in this loop
13296  mesh_changed_in_this_loop
13297  = ((flags_before_loop[0] != flags_after_loop[0]) ||
13298  (flags_before_loop[1] != flags_after_loop[1]));
13299 
13300  // set the flags for the next loop
13301  // already
13302  flags_before_loop[0].swap(flags_after_loop[0]);
13303  flags_before_loop[1].swap(flags_after_loop[1]);
13304  }
13305  while (mesh_changed_in_this_loop);
13306 
13307 
13308  // find out whether something was really changed in this
13309  // function. Note that @p{flags_before_loop} represents the state
13310  // after the last loop, i.e. the present state
13311  return ((flags_before[0] != flags_before_loop[0]) ||
13312  (flags_before[1] != flags_before_loop[1]));
13313 }
13314 
13315 
13316 
13317 
13318 template <int dim, int spacedim>
13319 void Triangulation<dim, spacedim>::write_bool_vector (const unsigned int magic_number1,
13320  const std::vector<bool> &v,
13321  const unsigned int magic_number2,
13322  std::ostream &out)
13323 {
13324  const unsigned int N = v.size();
13325  unsigned char *flags = new unsigned char[N/8+1];
13326  for (unsigned int i=0; i<N/8+1; ++i) flags[i]=0;
13327 
13328  for (unsigned int position=0; position<N; ++position)
13329  flags[position/8] |= (v[position] ? (1<<(position%8)) : 0);
13330 
13331  AssertThrow (out, ExcIO());
13332 
13333  // format:
13334  // 0. magic number
13335  // 1. number of flags
13336  // 2. the flags
13337  // 3. magic number
13338  out << magic_number1 << ' ' << N << std::endl;
13339  for (unsigned int i=0; i<N/8+1; ++i)
13340  out << static_cast<unsigned int>(flags[i]) << ' ';
13341 
13342  out << std::endl << magic_number2 << std::endl;
13343 
13344  delete[] flags;
13345 
13346  AssertThrow (out, ExcIO());
13347 }
13348 
13349 
13350 template <int dim, int spacedim>
13351 void Triangulation<dim, spacedim>::read_bool_vector (const unsigned int magic_number1,
13352  std::vector<bool> &v,
13353  const unsigned int magic_number2,
13354  std::istream &in)
13355 {
13356  AssertThrow (in, ExcIO());
13357 
13358  unsigned int magic_number;
13359  in >> magic_number;
13360  AssertThrow (magic_number==magic_number1, ExcGridReadError());
13361 
13362  unsigned int N;
13363  in >> N;
13364  v.resize (N);
13365 
13366  unsigned char *flags = new unsigned char[N/8+1];
13367  unsigned short int tmp;
13368  for (unsigned int i=0; i<N/8+1; ++i)
13369  {
13370  in >> tmp;
13371  flags[i] = tmp;
13372  }
13373 
13374  for (unsigned int position=0; position!=N; ++position)
13375  v[position] = (flags[position/8] & (1<<(position%8)));
13376 
13377  in >> magic_number;
13378  AssertThrow (magic_number==magic_number2, ExcGridReadError());
13379 
13380  delete[] flags;
13381 
13382  AssertThrow (in, ExcIO());
13383 }
13384 
13385 
13386 
13387 template <int dim, int spacedim>
13388 std::size_t
13390 {
13391  std::size_t mem = 0;
13393  for (unsigned int i=0; i<levels.size(); ++i)
13394  mem += MemoryConsumption::memory_consumption (*levels[i]);
13395  mem += MemoryConsumption::memory_consumption (vertices);
13396  mem += MemoryConsumption::memory_consumption (vertices_used);
13397  mem += sizeof(manifold);
13398  mem += sizeof(smooth_grid);
13399  mem += MemoryConsumption::memory_consumption (number_cache);
13400  mem += sizeof (faces);
13401  if (faces)
13402  mem += MemoryConsumption::memory_consumption (*faces);
13403 
13404  return mem;
13405 }
13406 
13407 
13408 
13409 
13410 template <int dim, int spacedim>
13412 {
13413  // don't do anything here. the compiler will automatically convert
13414  // any exceptions created by the destructors of the member variables
13415  // into abort() in order to satisfy the throw()/noexcept
13416  // specification
13417 }
13418 
13419 
13420 // explicit instantiations
13421 #include "tria.inst"
13422 
13423 DEAL_II_NAMESPACE_CLOSE
std::vector< CellData< 1 > > boundary_lines
Definition: tria.h:249
boost::signals2::signal< void()> any_change
Definition: tria.h:2264
::internal::TriangulationImplementation::NumberCache< dim > number_cache
Definition: tria.h:3621
unsigned int n_active_cells() const
Definition: tria.cc:11084
void set_boundary(const types::manifold_id number, const Boundary< dim, spacedim > &boundary_object)
Definition: tria.cc:8947
virtual void copy_triangulation(const Triangulation< dim, spacedim > &other_tria)
Definition: tria.cc:9159
void loop(ITERATOR begin, typename identity< ITERATOR >::type end, DOFINFO &dinfo, INFOBOX &info, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &cell_worker, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &boundary_worker, const std::function< void(DOFINFO &, DOFINFO &, typename INFOBOX::CellInfo &, typename INFOBOX::CellInfo &)> &face_worker, ASSEMBLER &assembler, const LoopControl &lctrl=LoopControl())
Definition: loop.h:386
unsigned int n_used_vertices() const
Definition: tria.cc:11612
const types::manifold_id flat_manifold_id
Definition: types.h:237
static const unsigned int invalid_unsigned_int
Definition: types.h:173
void load_user_flags_line(std::istream &in)
Definition: tria.cc:9864
#define AssertNothrow(cond, exc)
Definition: exceptions.h:1183
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:358
static ::ExceptionBase & ExcInternalErrorOnCell(int arg1)
hex_iterator begin_hex(const unsigned int level=0) const
Definition: tria.cc:10976
const types::subdomain_id invalid_subdomain_id
Definition: types.h:248
virtual ~DistortedCellList() noexcept
Definition: tria.cc:13411
void clear_user_flags()
Definition: tria.cc:9729
active_face_iterator begin_active_face() const
Definition: tria.cc:10688
void save_user_flags_quad(std::ostream &out) const
Definition: tria.cc:9963
virtual bool has_hanging_nodes() const
Definition: tria.cc:11206
unsigned int n_raw_cells(const unsigned int level) const
Definition: tria.cc:11150
cell_iterator last() const
Definition: tria.cc:10528
void save_user_pointers_hex(std::vector< void *> &v) const
Definition: tria.cc:10430
vertex_iterator begin_vertex() const
Definition: tria.cc:10732
static void prepare_refinement_dim_dependent(const Triangulation< dim, spacedim > &)
Definition: tria.cc:8534
static RefinementCase< 1 > line_refinement_case(const RefinementCase< dim > &cell_refinement_case, const unsigned int line_no)
unsigned int n_cells() const
Definition: tria.cc:11077
static unsigned int face_to_cell_lines(const unsigned int face, const unsigned int line, const bool face_orientation=true, const bool face_flip=false, const bool face_rotation=false)
static ::ExceptionBase & ExcIO()
Subscriptor & operator=(const Subscriptor &)
Definition: subscriptor.cc:129
Task< RT > new_task(const std::function< RT()> &function)
std::list< typename Triangulation< dim, spacedim >::cell_iterator > distorted_cells
Definition: tria.h:1623
static Triangulation< 1, spacedim >::DistortedCellList execute_refinement(Triangulation< 1, spacedim > &triangulation, const bool)
Definition: tria.cc:4280
std::vector< Point< spacedim > > vertices
Definition: tria.h:3587
static void create_triangulation(const std::vector< Point< spacedim > > &v, const std::vector< CellData< 1 > > &cells, const SubCellData &, Triangulation< 1, spacedim > &triangulation)
Definition: tria.cc:1598
boost::signals2::signal< void()> clear
Definition: tria.h:2253
static unsigned int line_to_cell_vertices(const unsigned int line, const unsigned int vertex)
IteratorRange< active_cell_iterator > active_cell_iterators() const
Definition: tria.cc:10632
void set_all_refine_flags()
Definition: tria.cc:9432
void load_user_flags(std::istream &in)
Definition: tria.cc:9786
unsigned int boundary_id
Definition: types.h:110
bool anisotropic_refinement
Definition: tria.h:3603
int face(const unsigned int i) const
Definition: tria_object.h:168
static void read_bool_vector(const unsigned int magic_number1, std::vector< bool > &v, const unsigned int magic_number2, std::istream &in)
Definition: tria.cc:13351
line_iterator begin_line(const unsigned int level=0) const
Definition: tria.cc:10816
line_iterator end_line() const
Definition: tria.cc:10848
boost::signals2::signal< void()> mesh_movement
Definition: tria.h:2213
void join() const
void clear_user_flags_quad()
Definition: tria.cc:9689
static void create_children(Triangulation< 2, spacedim > &triangulation, unsigned int &next_unused_vertex, typename Triangulation< 2, spacedim >::raw_line_iterator &next_unused_line, typename Triangulation< 2, spacedim >::raw_cell_iterator &next_unused_cell, typename Triangulation< 2, spacedim >::cell_iterator &cell)
Definition: tria.cc:3860
unsigned int n_faces() const
Definition: tria.cc:11098
static ::ExceptionBase & ExcCellHasNegativeMeasure(int arg1)
void flip_all_direction_flags()
Definition: tria.cc:9421
static RefinementCase< dim-1 > face_refinement_case(const RefinementCase< dim > &cell_refinement_case, const unsigned int face_no, const bool face_orientation=true, const bool face_flip=false, const bool face_rotation=false)
static Triangulation< 2, spacedim >::DistortedCellList execute_refinement(Triangulation< 2, spacedim > &triangulation, const bool check_for_distorted_cells)
Definition: tria.cc:4523
active_cell_iterator begin_active(const unsigned int level=0) const
Definition: tria.cc:10508
#define AssertThrow(cond, exc)
Definition: exceptions.h:1221
static ::ExceptionBase & ExcInvalidVertexIndex(int arg1, int arg2, int arg3)
static void compute_number_cache(const Triangulation< dim, spacedim > &triangulation, const unsigned int level_objects, internal::TriangulationImplementation::NumberCache< 1 > &number_cache)
Definition: tria.cc:1309
static void write_bool_vector(const unsigned int magic_number1, const std::vector< bool > &v, const unsigned int magic_number2, std::ostream &out)
Definition: tria.cc:13319
unsigned int n_active_hexs() const
Definition: tria.cc:11550
void save_user_pointers_quad(std::vector< void *> &v) const
Definition: tria.cc:10397
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
static ::ExceptionBase & ExcFacesHaveNoLevel()
void execute_coarsening()
Definition: tria.cc:11900
const Boundary< dim, spacedim > & get_boundary(const types::manifold_id number) const
Definition: tria.cc:9070
cell_iterator begin(const unsigned int level=0) const
Definition: tria.cc:10488
void load_user_flags_quad(std::istream &in)
Definition: tria.cc:9974
void set_manifold(const types::manifold_id number, const Manifold< dim, spacedim > &manifold_object)
Definition: tria.cc:8955
static Triangulation< 3, spacedim >::DistortedCellList execute_refinement(Triangulation< 3, spacedim > &triangulation, const bool check_for_distorted_cells)
Definition: tria.cc:4879
raw_hex_iterator begin_raw_hex(const unsigned int level=0) const
Definition: tria.cc:10946
std::vector< std::unique_ptr<::internal::TriangulationImplementation::TriaLevel< dim > > > levels
Definition: tria.h:3574
void load_user_pointers_line(const std::vector< void *> &v)
Definition: tria.cc:10383
unsigned int n_active_faces() const
Definition: tria.cc:11132
raw_quad_iterator begin_raw_quad(const unsigned int level=0) const
Definition: tria.cc:10862
std::unique_ptr<::internal::TriangulationImplementation::TriaFaces< dim > > faces
Definition: tria.h:3581
static ::ExceptionBase & ExcInteriorQuadCantBeBoundary(int arg1, int arg2, int arg3, int arg4, types::boundary_id arg5)
cell_iterator end() const
Definition: tria.cc:10576
void load_coarsen_flags(std::istream &out)
Definition: tria.cc:9557
void load_user_flags_hex(std::istream &in)
Definition: tria.cc:10037
active_quad_iterator begin_active_quad(const unsigned int level=0) const
Definition: tria.cc:10917
virtual void execute_coarsening_and_refinement()
Definition: tria.cc:11739
static ::ExceptionBase & ExcGridHasInvalidCell(int arg1)
unsigned int n_active_lines() const
Definition: tria.cc:11299
IteratorRange< cell_iterator > cell_iterators_on_level(const unsigned int level) const
Definition: tria.cc:10643
Definition: tria.h:45
virtual bool prepare_coarsening_and_refinement()
Definition: tria.cc:12426
static ::ExceptionBase & ExcMessage(std::string arg1)
virtual std::unique_ptr< Manifold< dim, spacedim > > clone() const =0
bool check_consistency(const unsigned int dim) const
Definition: tria.cc:47
static ::ExceptionBase & ExcImpossibleInDim(int arg1)
void reset_active_cell_indices()
Definition: tria.cc:11783
DistortedCellList execute_refinement()
Definition: tria.cc:11866
void save_user_flags_line(std::ostream &out) const
Definition: tria.cc:9853
void save_user_pointers_line(std::vector< void *> &v) const
Definition: tria.cc:10370
Triangulation(const MeshSmoothing smooth_grid=none, const bool check_for_distorted_cells=false)
Definition: tria.cc:8808
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:346
void clear_user_flags_hex()
Definition: tria.cc:9721
void load_user_indices_line(const std::vector< unsigned int > &v)
Definition: tria.cc:10185
unsigned int global_dof_index
Definition: types.h:88
vertex_iterator end_vertex() const
Definition: tria.cc:10769
unsigned int n_raw_quads() const
Definition: tria.cc:11492
static unsigned int child_cell_on_face(const RefinementCase< dim > &ref_case, const unsigned int face, const unsigned int subface, const bool face_orientation=true, const bool face_flip=false, const bool face_rotation=false, const RefinementCase< dim-1 > &face_refinement_case=RefinementCase< dim-1 >::isotropic_refinement)
#define Assert(cond, exc)
Definition: exceptions.h:1142
Signals signals
Definition: tria.h:2335
active_hex_iterator begin_active_hex(const unsigned int level=0) const
Definition: tria.cc:10992
void save_user_indices_line(std::vector< unsigned int > &v) const
Definition: tria.cc:10172
static void prevent_distorted_boundary_cells(const Triangulation< 1, spacedim > &)
void set_all_manifold_ids(const types::manifold_id number)
Definition: tria.cc:9001
static ::ExceptionBase & ExcInteriorLineCantBeBoundary(int arg1, int arg2, types::boundary_id arg3)
boost::signals2::signal< void()> create
Definition: tria.h:2188
unsigned int n_raw_hexs(const unsigned int level) const
Definition: tria.cc:11543
unsigned int max_adjacent_cells() const
Definition: tria.cc:11653
bool get_anisotropic_refinement_flag() const
Definition: tria.cc:9586
unsigned int n_quads() const
Definition: tria.cc:11446
std::unique_ptr< std::map< unsigned int, types::manifold_id > > vertex_to_manifold_id_map_1d
Definition: tria.h:3659
std::vector< bool > vertices_used
Definition: tria.h:3592
virtual void create_triangulation(const std::vector< Point< spacedim > > &vertices, const std::vector< CellData< dim > > &cells, const SubCellData &subcelldata)
Definition: tria.cc:9240
void save_coarsen_flags(std::ostream &out) const
Definition: tria.cc:9546
void reset_all_manifolds()
Definition: tria.cc:8993
void save_user_indices(std::vector< unsigned int > &v) const
Definition: tria.cc:10070
void load_user_indices(const std::vector< unsigned int > &v)
Definition: tria.cc:10100
void save_refine_flags(std::ostream &out) const
Definition: tria.cc:9481
unsigned int n_lines() const
Definition: tria.cc:11217
static ::ExceptionBase & ExcMultiplySetLineInfoOfLine(int arg1, int arg2)
void clear_despite_subscriptions()
Definition: tria.cc:11850
raw_cell_iterator begin_raw(const unsigned int level=0) const
Definition: tria.cc:10468
unsigned int n_raw_lines() const
Definition: tria.cc:11282
virtual ~Triangulation()
Definition: tria.cc:8880
virtual std::size_t memory_consumption() const
Definition: tria.cc:13389
#define DeclException5(Exception5, type1, type2, type3, type4, type5, outsequence)
Definition: exceptions.h:394
virtual void set_mesh_smoothing(const MeshSmoothing mesh_smoothing)
Definition: tria.cc:8927
void load_user_pointers_hex(const std::vector< void *> &v)
Definition: tria.cc:10447
void save_user_flags_hex(std::ostream &out) const
Definition: tria.cc:10026
face_iterator begin_face() const
Definition: tria.cc:10667
unsigned int subdomain_id
Definition: types.h:42
boost::signals2::signal< void()> post_refinement
Definition: tria.h:2204
std::unique_ptr< std::map< unsigned int, types::boundary_id > > vertex_to_boundary_id_map_1d
Definition: tria.h:3637
virtual types::global_dof_index n_global_active_cells() const
Definition: tria.cc:11090
unsigned int n_hexs() const
Definition: tria.cc:11527
static void create_triangulation(const std::vector< Point< spacedim > > &v, const std::vector< CellData< 2 > > &cells, const SubCellData &subcelldata, Triangulation< 2, spacedim > &triangulation)
Definition: tria.cc:1802
unsigned int n_active_quads() const
Definition: tria.cc:11509
active_line_iterator begin_active_line(const unsigned int level=0) const
Definition: tria.cc:10832
raw_cell_iterator end_raw(const unsigned int level) const
Definition: tria.cc:10587
unsigned int n_raw_faces() const
Definition: tria.cc:11116
void load_user_indices_hex(const std::vector< unsigned int > &v)
Definition: tria.cc:10248
static ::ExceptionBase & ExcLineInexistant(int arg1, int arg2)
static unsigned int n_children(const RefinementCase< dim > &refinement_case)
void swap(Vector< Number > &u, Vector< Number > &v)
Definition: vector.h:1312
active_vertex_iterator begin_active_vertex() const
Definition: tria.cc:10760
void reset_manifold(const types::manifold_id manifold_number)
Definition: tria.cc:8982
static void alternating_form_at_vertices(const Point< spacedim >(&vertices)[vertices_per_cell], Tensor< spacedim-dim, spacedim >(&forms)[vertices_per_cell])
static bool coarsening_allowed(const typename Triangulation< dim, spacedim >::cell_iterator &cell)
Definition: tria.cc:8719
std::vector< types::boundary_id > get_boundary_ids() const
Definition: tria.cc:9105
const types::manifold_id invalid_manifold_id
Definition: types.h:229
unsigned int manifold_id
Definition: types.h:122
void load_user_pointers_quad(const std::vector< void *> &v)
Definition: tria.cc:10414
void fix_coarsen_flags()
Definition: tria.cc:11972
active_cell_iterator end_active(const unsigned int level) const
Definition: tria.cc:10610
void update_periodic_face_map()
Definition: tria.cc:11801
std::vector< unsigned int > n_active_hexes_level
Definition: tria.h:456
static void compute_number_cache(const Triangulation< dim, spacedim > &triangulation, const unsigned int level_objects, internal::TriangulationImplementation::NumberCache< 3 > &number_cache)
Definition: tria.cc:1499
Definition: mpi.h:53
void save_user_indices_quad(std::vector< unsigned int > &v) const
Definition: tria.cc:10198
virtual void add_periodicity(const std::vector< GridTools::PeriodicFacePair< cell_iterator > > &)
Definition: tria.cc:11714
const std::vector< bool > & get_used_vertices() const
Definition: tria.cc:11622
hex_iterator end_hex() const
Definition: tria.cc:11008
std::vector< CellData< 2 > > boundary_quads
Definition: tria.h:257
MeshSmoothing smooth_grid
Definition: tria.h:3325
#define DeclException4(Exception4, type1, type2, type3, type4, outsequence)
Definition: exceptions.h:382
IteratorRange< active_cell_iterator > active_cell_iterators_on_level(const unsigned int level) const
Definition: tria.cc:10654
void load_user_pointers(const std::vector< void *> &v)
Definition: tria.cc:10335
std::vector< unsigned int > n_active_lines_level
Definition: tria.h:339
void refine_global(const unsigned int times=1)
Definition: tria.cc:9448
void save_user_pointers(std::vector< void *> &v) const
Definition: tria.cc:10305
void save_user_flags(std::ostream &out) const
Definition: tria.cc:9739
static ::ExceptionBase & ExcNotImplemented()
Iterator points to a valid object.
quad_iterator end_quad() const
Definition: tria.cc:10933
IteratorRange< cell_iterator > cell_iterators() const
Definition: tria.cc:10622
#define DeclException3(Exception3, type1, type2, type3, outsequence)
Definition: exceptions.h:370
face_iterator end_face() const
Definition: tria.cc:10709
void set_all_manifold_ids_on_boundary(const types::manifold_id number)
Definition: tria.cc:9016
static ::ExceptionBase & ExcQuadInexistant(int arg1, int arg2, int arg3, int arg4)
active_cell_iterator last_active() const
Definition: tria.cc:10555
const types::boundary_id internal_face_boundary_id
Definition: types.h:219
std::map< types::manifold_id, std::unique_ptr< const Manifold< dim, spacedim > > > manifold
Definition: tria.h:3598
std::vector< unsigned int > n_active_quads_level
Definition: tria.h:397
void clear_user_flags_line()
Definition: tria.cc:9657
IteratorState::IteratorStates state() const
quad_iterator begin_quad(const unsigned int level=0) const
Definition: tria.cc:10901
void clear()
Definition: tensor.h:1356
void set_face(const unsigned int i, const int index)
Definition: tria_object.h:179
void clear_user_data()
Definition: tria.cc:9629
Triangulation & operator=(Triangulation< dim, spacedim > &&tria) noexcept
Definition: tria.cc:8855
std::vector< types::manifold_id > get_manifold_ids() const
Definition: tria.cc:9137
static void delete_children(Triangulation< 1, spacedim > &triangulation, typename Triangulation< 1, spacedim >::cell_iterator &cell, std::vector< unsigned int > &, std::vector< unsigned int > &)
Definition: tria.cc:3063
const Manifold< dim, spacedim > & get_manifold(const types::manifold_id number) const
Definition: tria.cc:9083
virtual void create_triangulation_compatibility(const std::vector< Point< spacedim > > &vertices, const std::vector< CellData< dim > > &cells, const SubCellData &subcelldata)
Definition: tria.cc:9220
static RefinementCase< dim > min_cell_refinement_case_for_face_refinement(const RefinementCase< dim-1 > &face_refinement_case, const unsigned int face_no, const bool face_orientation=true, const bool face_flip=false, const bool face_rotation=false)
void load_user_indices_quad(const std::vector< unsigned int > &v)
Definition: tria.cc:10215
virtual types::subdomain_id locally_owned_subdomain() const
Definition: tria.cc:11686
raw_line_iterator begin_raw_line(const unsigned int level=0) const
Definition: tria.cc:10791
virtual void clear()
Definition: tria.cc:8912
virtual const MeshSmoothing & get_mesh_smoothing() const
Definition: tria.cc:8938
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)
const std::map< std::pair< cell_iterator, unsigned int >, std::pair< std::pair< cell_iterator, unsigned int >, std::bitset< 3 > > > & get_periodic_face_map() const
Definition: tria.cc:11730
void load_refine_flags(std::istream &in)
Definition: tria.cc:9492
Tensor< 2, dim, Number > l(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
static ::ExceptionBase & ExcInternalError()
Triangulation< dim, spacedim > & get_triangulation()
Definition: tria.cc:11695
static void create_triangulation(const std::vector< Point< spacedim > > &v, const std::vector< CellData< 3 > > &cells, const SubCellData &subcelldata, Triangulation< 3, spacedim > &triangulation)
Definition: tria.cc:2170
static void compute_number_cache(const Triangulation< dim, spacedim > &triangulation, const unsigned int level_objects, internal::TriangulationImplementation::NumberCache< 2 > &number_cache)
Definition: tria.cc:1395
void save_user_indices_hex(std::vector< unsigned int > &v) const
Definition: tria.cc:10231