Reference documentation for deal.II version 8.5.1
tria.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2017 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 
17 #include <deal.II/base/memory_consumption.h>
18 #include <deal.II/base/table.h>
19 #include <deal.II/base/geometry_info.h>
20 #include <deal.II/base/std_cxx11/bind.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 
42 #include <deal.II/base/std_cxx11/array.h>
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 Triangulation
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,
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 &,
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 Triangulation
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::Triangulation
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 (new internal::Triangulation::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::Triangulation
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 (new internal::Triangulation::TriaLevel<dim>);
1926  triangulation.faces.reset (new internal::Triangulation::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::Triangulation::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::Triangulation::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 specifiy 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::Triangulation::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 (new internal::Triangulation::TriaLevel<dim>);
2294  triangulation.faces.reset (new internal::Triangulation::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::Triangulation::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::Triangulation::TriaObject<2>,
2334  std::pair<typename Triangulation<dim,spacedim>::quad_iterator,
2335  std_cxx11::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_cxx11::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::Triangulation::TriaObject<2>,
2486  std::pair<typename Triangulation<dim,spacedim>::quad_iterator,
2487  std_cxx11::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
2675  cell->set (internal::Triangulation
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  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 (1)
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 (1)
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::Triangulation::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::Triangulation::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
4004  // cell is at the boundary, use a
4005  // different calculation of the middle
4006  // vertex here. this is of advantage, if
4007  // the boundary is strongly curved and
4008  // the cell has a high aspect ratio. this
4009  // can happen for example, if it was
4010  // refined anisotropically before.
4011  if (cell->user_flag_set())
4012  {
4013  // first reset the user_flag
4014  cell->clear_user_flag();
4015  // the user flag indicates: at least
4016  // one face is at the boundary. if it
4017  // is only one, set the new middle
4018  // vertex in a different way to avoid
4019  // some mis-shaped elements if the
4020  // new point on the boundary is not
4021  // where we expect it, especially if
4022  // it is to far inside the current
4023  // cell
4024  unsigned int boundary_face=GeometryInfo<dim>::faces_per_cell;
4025  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
4026  if (cell->face(face)->at_boundary())
4027  {
4028  if (boundary_face == GeometryInfo<dim>::faces_per_cell)
4029  // no boundary face found so
4030  // far, so set it now
4031  boundary_face=face;
4032  else
4033  // there is another boundary
4034  // face, so reset boundary_face to
4035  // invalid value as a flag to
4036  // do nothing in the following
4037  boundary_face=GeometryInfo<dim>::faces_per_cell+1;
4038  }
4039 
4040  if (boundary_face<GeometryInfo<dim>::faces_per_cell)
4041  // reset the cell's middle vertex to the middle
4042  // of the straight connection between the new
4043  // points on this face and on the opposite face,
4044  // as returned by the underlying manifold
4045  // object.
4046  {
4047  std::vector<Point<spacedim> > ps(2);
4048  std::vector<double> ws(2, 0.5);
4049  ps[0] = cell->face(boundary_face)
4050  ->child(0)->vertex(1);
4051  ps[1] = cell->face(GeometryInfo<dim>
4052  ::opposite_face[boundary_face])
4053  ->child(0)->vertex(1);
4054  triangulation.vertices[next_unused_vertex]
4055  = cell->get_manifold().get_new_point(ps,ws);
4056  }
4057  }
4058  }
4059  else
4060  {
4061  // if this quad lives in a higher dimensional space
4062  // then we don't need to worry if it is at the
4063  // boundary of the manifold -- we always have to use
4064  // the boundary object anyway; so ignore whether the
4065  // user flag is set or not
4066  cell->clear_user_flag();
4067 
4068  // An assert to make sure that the static_cast in the
4069  // next line has the chance to give reasonable
4070  // results.
4071  Assert(cell->material_id()<= std::numeric_limits<types::material_id>::max(),
4072  ExcIndexRange(cell->material_id(),0,std::numeric_limits<types::material_id>::max()));
4073 
4074  // new vertex is placed on the surface according to
4075  // the information stored in the boundary class
4076  triangulation.vertices[next_unused_vertex] =
4077  cell->center(true);
4078  }
4079  }
4080 
4081 
4082  // Now the lines:
4083  typename Triangulation<dim,spacedim>::raw_line_iterator new_lines[12];
4084  unsigned int lmin=8;
4085  unsigned int lmax=12;
4086  if (ref_case!=RefinementCase<dim>::cut_xy)
4087  {
4088  lmin=6;
4089  lmax=7;
4090  }
4091 
4092  for (unsigned int l=lmin; l<lmax; ++l)
4093  {
4094  while (next_unused_line->used() == true)
4095  ++next_unused_line;
4096  new_lines[l] = next_unused_line;
4097  ++next_unused_line;
4098 
4099  Assert (new_lines[l]->used() == false,
4100  ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
4101  }
4102 
4103  if (ref_case==RefinementCase<dim>::cut_xy)
4104  {
4105  // .-6-.-7-.
4106  // 1 9 3
4107  // .-10.11-.
4108  // 0 8 2
4109  // .-4-.-5-.
4110 
4111  // lines 0-7 already exist, create only the four interior
4112  // lines 8-11
4113  unsigned int l=0;
4114  for (unsigned int face_no=0; face_no<GeometryInfo<dim>::faces_per_cell; ++face_no)
4115  for (unsigned int c=0; c<2; ++c, ++l)
4116  new_lines[l]=cell->line(face_no)->child(c);
4117  Assert(l==8, ExcInternalError());
4118 
4119  new_lines[8] ->set (internal::Triangulation::
4120  TriaObject<1>(new_vertices[6], new_vertices[8]));
4121  new_lines[9] ->set (internal::Triangulation::
4122  TriaObject<1>(new_vertices[8], new_vertices[7]));
4123  new_lines[10]->set (internal::Triangulation::
4124  TriaObject<1>(new_vertices[4], new_vertices[8]));
4125  new_lines[11]->set (internal::Triangulation::
4126  TriaObject<1>(new_vertices[8], new_vertices[5]));
4127  }
4128  else if (ref_case==RefinementCase<dim>::cut_x)
4129  {
4130  // .-4-.-5-.
4131  // | | |
4132  // 0 6 1
4133  // | | |
4134  // .-2-.-3-.
4135  new_lines[0]=cell->line(0);
4136  new_lines[1]=cell->line(1);
4137  new_lines[2]=cell->line(2)->child(0);
4138  new_lines[3]=cell->line(2)->child(1);
4139  new_lines[4]=cell->line(3)->child(0);
4140  new_lines[5]=cell->line(3)->child(1);
4141  new_lines[6]->set (internal::Triangulation::
4142  TriaObject<1>(new_vertices[6], new_vertices[7]));
4143  }
4144  else
4145  {
4147  // .---5---.
4148  // 1 3
4149  // .---6---.
4150  // 0 2
4151  // .---4---.
4152  new_lines[0]=cell->line(0)->child(0);
4153  new_lines[1]=cell->line(0)->child(1);
4154  new_lines[2]=cell->line(1)->child(0);
4155  new_lines[3]=cell->line(1)->child(1);
4156  new_lines[4]=cell->line(2);
4157  new_lines[5]=cell->line(3);
4158  new_lines[6]->set (internal::Triangulation::
4159  TriaObject<1>(new_vertices[4], new_vertices[5]));
4160  }
4161 
4162  for (unsigned int l=lmin; l<lmax; ++l)
4163  {
4164  new_lines[l]->set_used_flag();
4165  new_lines[l]->clear_user_flag();
4166  new_lines[l]->clear_user_data();
4167  new_lines[l]->clear_children();
4168  // interior line
4169  new_lines[l]->set_boundary_id_internal(numbers::internal_face_boundary_id);
4170  new_lines[l]->set_manifold_id(cell->manifold_id());
4171  }
4172 
4173  // Now add the four (two)
4174  // new cells!
4177  while (next_unused_cell->used() == true)
4178  ++next_unused_cell;
4179 
4180  const unsigned int n_children=
4182  for (unsigned int i=0; i<n_children; ++i)
4183  {
4184  Assert (next_unused_cell->used() == false,
4185  ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
4186  subcells[i] = next_unused_cell;
4187  ++next_unused_cell;
4188  if (i%2==1 && i<n_children-1)
4189  while (next_unused_cell->used() == true)
4190  ++next_unused_cell;
4191  }
4192 
4193  if (ref_case==RefinementCase<dim>::cut_xy)
4194  {
4195  // children:
4196  // .--.--.
4197  // |2 . 3|
4198  // .--.--.
4199  // |0 | 1|
4200  // .--.--.
4201  // lines:
4202  // .-6-.-7-.
4203  // 1 9 3
4204  // .-10.11-.
4205  // 0 8 2
4206  // .-4-.-5-.
4207  subcells[0]->set (internal::Triangulation::
4208  TriaObject<2>(new_lines[0]->index(),
4209  new_lines[8]->index(),
4210  new_lines[4]->index(),
4211  new_lines[10]->index()));
4212  subcells[1]->set (internal::Triangulation::
4213  TriaObject<2>(new_lines[8]->index(),
4214  new_lines[2]->index(),
4215  new_lines[5]->index(),
4216  new_lines[11]->index()));
4217  subcells[2]->set (internal::Triangulation::
4218  TriaObject<2>(new_lines[1]->index(),
4219  new_lines[9]->index(),
4220  new_lines[10]->index(),
4221  new_lines[6]->index()));
4222  subcells[3]->set (internal::Triangulation::
4223  TriaObject<2>(new_lines[9]->index(),
4224  new_lines[3]->index(),
4225  new_lines[11]->index(),
4226  new_lines[7]->index()));
4227  }
4228  else if (ref_case==RefinementCase<dim>::cut_x)
4229  {
4230  // children:
4231  // .--.--.
4232  // | . |
4233  // .0 . 1.
4234  // | | |
4235  // .--.--.
4236  // lines:
4237  // .-4-.-5-.
4238  // | | |
4239  // 0 6 1
4240  // | | |
4241  // .-2-.-3-.
4242  subcells[0]->set (internal::Triangulation::
4243  TriaObject<2>(new_lines[0]->index(),
4244  new_lines[6]->index(),
4245  new_lines[2]->index(),
4246  new_lines[4]->index()));
4247  subcells[1]->set (internal::Triangulation::
4248  TriaObject<2>(new_lines[6]->index(),
4249  new_lines[1]->index(),
4250  new_lines[3]->index(),
4251  new_lines[5]->index()));
4252  }
4253  else
4254  {
4256  // children:
4257  // .-----.
4258  // | 1 |
4259  // .-----.
4260  // | 0 |
4261  // .-----.
4262  // lines:
4263  // .---5---.
4264  // 1 3
4265  // .---6---.
4266  // 0 2
4267  // .---4---.
4268  subcells[0]->set (internal::Triangulation::
4269  TriaObject<2>(new_lines[0]->index(),
4270  new_lines[2]->index(),
4271  new_lines[4]->index(),
4272  new_lines[6]->index()));
4273  subcells[1]->set (internal::Triangulation::
4274  TriaObject<2>(new_lines[1]->index(),
4275  new_lines[3]->index(),
4276  new_lines[6]->index(),
4277  new_lines[5]->index()));
4278  }
4279 
4280  types::subdomain_id subdomainid = cell->subdomain_id();
4281 
4282  for (unsigned int i=0; i<n_children; ++i)
4283  {
4284  subcells[i]->set_used_flag();
4285  subcells[i]->clear_refine_flag();
4286  subcells[i]->clear_user_flag();
4287  subcells[i]->clear_user_data();
4288  subcells[i]->clear_children();
4289  // inherit material
4290  // properties
4291  subcells[i]->set_material_id (cell->material_id());
4292  subcells[i]->set_manifold_id (cell->manifold_id());
4293  subcells[i]->set_subdomain_id (subdomainid);
4294 
4295  if (i%2==0)
4296  subcells[i]->set_parent (cell->index ());
4297  }
4298 
4299 
4300 
4301  // set child index for
4302  // even children children
4303  // i=0,2 (0)
4304  for (unsigned int i=0; i<n_children/2; ++i)
4305  cell->set_children (2*i, subcells[2*i]->index());
4306  // set the refine case
4307  cell->set_refinement_case(ref_case);
4308 
4309  // note that the
4310  // refinement flag was
4311  // already cleared at the
4312  // beginning of this function
4313 
4314  if (dim < spacedim)
4315  for (unsigned int c=0; c<n_children; ++c)
4316  cell->child(c)->set_direction_flag (cell->direction_flag());
4317 
4318  }
4319 
4320 
4321 
4326  template <int spacedim>
4327  static
4330  const bool /*check_for_distorted_cells*/)
4331  {
4332  const unsigned int dim = 1;
4333 
4334  // check whether a new level is needed we have to check for
4335  // this on the highest level only (on this, all used cells are
4336  // also active, so we only have to check for this)
4337  {
4339  cell = triangulation.begin_active (triangulation.levels.size()-1),
4340  endc = triangulation.end();
4341  for (; cell != endc; ++cell)
4342  if (cell->used())
4343  if (cell->refine_flag_set())
4344  {
4345  triangulation.levels
4346  .push_back (new internal::Triangulation::TriaLevel<dim>);
4347  break;
4348  }
4349  }
4350 
4351 
4352  // check how much space is needed on every level we need not
4353  // check the highest level since either - on the highest level
4354  // no cells are flagged for refinement - there are, but
4355  // prepare_refinement added another empty level
4356  unsigned int needed_vertices = 0;
4357  for (int level=triangulation.levels.size()-2; level>=0; --level)
4358  {
4359  // count number of flagged
4360  // cells on this level
4361  unsigned int flagged_cells = 0;
4363  acell = triangulation.begin_active(level),
4364  aendc = triangulation.begin_active(level+1);
4365  for (; acell!=aendc; ++acell)
4366  if (acell->refine_flag_set())
4367  ++flagged_cells;
4368 
4369  // count number of used cells
4370  // on the next higher level
4371  const unsigned int used_cells
4372  = std::count_if (triangulation.levels[level+1]->cells.used.begin(),
4373  triangulation.levels[level+1]->cells.used.end(),
4374  std_cxx11::bind (std::equal_to<bool>(), std_cxx11::_1, true));
4375 
4376  // reserve space for the used_cells cells already existing
4377  // on the next higher level as well as for the
4378  // 2*flagged_cells that will be created on that level
4379  triangulation.levels[level+1]
4380  ->reserve_space(used_cells+
4382  flagged_cells,
4383  1,
4384  spacedim);
4385  // reserve space for 2*flagged_cells new lines on the next
4386  // higher level
4387  triangulation.levels[level+1]->cells
4388  .reserve_space (GeometryInfo<1>::max_children_per_cell *
4389  flagged_cells,
4390  0);
4391 
4392  needed_vertices += flagged_cells;
4393  }
4394 
4395  // add to needed vertices how many
4396  // vertices are already in use
4397  needed_vertices += std::count_if (triangulation.vertices_used.begin(),
4398  triangulation.vertices_used.end(),
4399  std_cxx11::bind (std::equal_to<bool>(),
4400  std_cxx11::_1,
4401  true));
4402  // if we need more vertices: create them, if not: leave the
4403  // array as is, since shrinking is not really possible because
4404  // some of the vertices at the end may be in use
4405  if (needed_vertices > triangulation.vertices.size())
4406  {
4407  triangulation.vertices.resize (needed_vertices,
4408  Point<spacedim>());
4409  triangulation.vertices_used.resize (needed_vertices, false);
4410  }
4411 
4412 
4413  // Do REFINEMENT on every level; exclude highest level as
4414  // above
4415 
4416  // index of next unused vertex
4417  unsigned int next_unused_vertex = 0;
4418 
4419  for (int level=triangulation.levels.size()-2; level>=0; --level)
4420  {
4422  cell = triangulation.begin_active(level),
4423  endc = triangulation.begin_active(level+1);
4424 
4426  next_unused_cell = triangulation.begin_raw (level+1);
4427 
4428  for (; (cell!=endc) && (cell->level()==level); ++cell)
4429  if (cell->refine_flag_set())
4430  {
4431  // clear refinement flag
4432  cell->clear_refine_flag ();
4433 
4434  // search for next unused
4435  // vertex
4436  while (triangulation.vertices_used[next_unused_vertex] == true)
4437  ++next_unused_vertex;
4438  Assert (next_unused_vertex < triangulation.vertices.size(),
4439  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."));
4440 
4441  // Now we always ask the cell itself where to put
4442  // the new point. The cell in turn will query the
4443  // manifold object internally.
4444  triangulation.vertices[next_unused_vertex] =
4445  cell->center(true);
4446 
4447  triangulation.vertices_used[next_unused_vertex] = true;
4448 
4449  // search for next two unused cell (++ takes care of
4450  // the end of the vector)
4452  first_child,
4453  second_child;
4454  while (next_unused_cell->used() == true)
4455  ++next_unused_cell;
4456  first_child = next_unused_cell;
4457  first_child->set_used_flag ();
4458  first_child->clear_user_data ();
4459  ++next_unused_cell;
4460  Assert (next_unused_cell->used() == false,
4461  ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
4462  second_child = next_unused_cell;
4463  second_child->set_used_flag ();
4464  second_child->clear_user_data ();
4465 
4466  types::subdomain_id subdomainid = cell->subdomain_id();
4467 
4468  // insert first child
4469  cell->set_children (0, first_child->index());
4470  first_child->clear_children ();
4471  first_child->set (internal::Triangulation
4472  ::TriaObject<1> (cell->vertex_index(0),
4473  next_unused_vertex));
4474  first_child->set_material_id (cell->material_id());
4475  first_child->set_manifold_id (cell->manifold_id());
4476  first_child->set_subdomain_id (subdomainid);
4477  first_child->set_direction_flag (cell->direction_flag());
4478 
4479  first_child->set_parent (cell->index ());
4480 
4481  // Set manifold id of the right face. Only do this
4482  // on the first child.
4483  first_child->face(1)->set_manifold_id(cell->manifold_id());
4484 
4485  // reset neighborship info (refer to
4486  // internal::Triangulation::TriaLevel<0> for
4487  // details)
4488  first_child->set_neighbor (1, second_child);
4489  if (cell->neighbor(0).state() != IteratorState::valid)
4490  first_child->set_neighbor (0, cell->neighbor(0));
4491  else if (cell->neighbor(0)->active())
4492  {
4493  // since the neighbors level is always <=level,
4494  // if the cell is active, then there are no
4495  // cells to the left which may want to know
4496  // about this new child cell.
4497  Assert (cell->neighbor (0)->level () <= cell->level (),
4498  ExcInternalError ());
4499  first_child->set_neighbor (0, cell->neighbor(0));
4500  }
4501  else
4502  // left neighbor is refined
4503  {
4504  // set neighbor to cell on same level
4505  const unsigned int nbnb = cell->neighbor_of_neighbor (0);
4506  first_child->set_neighbor (0, cell->neighbor(0)->child(nbnb));
4507 
4508  // reset neighbor info of all right descendant
4509  // of the left neighbor of cell
4511  left_neighbor = cell->neighbor(0);
4512  while (left_neighbor->has_children())
4513  {
4514  left_neighbor = left_neighbor->child(nbnb);
4515  left_neighbor->set_neighbor (nbnb, first_child);
4516  }
4517  }
4518 
4519  // insert second child
4520  second_child->clear_children ();
4521  second_child->set (internal::Triangulation
4522  ::TriaObject<1>(next_unused_vertex,
4523  cell->vertex_index(1)));
4524  second_child->set_neighbor (0, first_child);
4525  second_child->set_material_id (cell->material_id());
4526  second_child->set_manifold_id (cell->manifold_id());
4527  second_child->set_subdomain_id (subdomainid);
4528  second_child->set_direction_flag (cell->direction_flag());
4529 
4530  if (cell->neighbor(1).state() != IteratorState::valid)
4531  second_child->set_neighbor (1, cell->neighbor(1));
4532  else if (cell->neighbor(1)->active())
4533  {
4534  Assert (cell->neighbor (1)->level () <= cell->level (),
4535  ExcInternalError ());
4536  second_child->set_neighbor (1, cell->neighbor(1));
4537  }
4538  else
4539  // right neighbor is refined same as above
4540  {
4541  const unsigned int nbnb = cell->neighbor_of_neighbor (1);
4542  second_child->set_neighbor (1, cell->neighbor(1)->child(nbnb));
4543 
4545  right_neighbor = cell->neighbor(1);
4546  while (right_neighbor->has_children())
4547  {
4548  right_neighbor = right_neighbor->child(nbnb);
4549  right_neighbor->set_neighbor (nbnb, second_child);
4550  }
4551  }
4552  // inform all listeners that cell refinement is done
4553  triangulation.signals.post_refinement_on_cell(cell);
4554  }
4555  }
4556 
4557  // in 1d, we can not have distorted children unless the parent
4558  // was already distorted (that is because we don't use
4559  // boundary information for 1d triangulations). so return an
4560  // empty list
4562  }
4563 
4564 
4569  template <int spacedim>
4570  static
4573  const bool check_for_distorted_cells)
4574  {
4575  const unsigned int dim = 2;
4576 
4577  // check whether a new level is needed we have to check for
4578  // this on the highest level only (on this, all used cells are
4579  // also active, so we only have to check for this)
4580  if (true)
4581  {
4583  cell = triangulation.begin_active (triangulation.levels.size()-1),
4584  endc = triangulation.end();
4585  for (; cell != endc; ++cell)
4586  if (cell->used())
4587  if (cell->refine_flag_set())
4588  {
4589  triangulation.levels.push_back (new internal::Triangulation::TriaLevel<dim>);
4590  break;
4591  }
4592  }
4593 
4594 
4595  // first clear user flags and pointers of lines; we're going
4596  // to use them to flag which lines need refinement
4597  for (typename Triangulation<dim,spacedim>::line_iterator
4598  line=triangulation.begin_line(); line!=triangulation.end_line(); ++line)
4599  {
4600  line->clear_user_flag();
4601  line->clear_user_data();
4602  }
4603  // running over all cells and lines count the number
4604  // n_single_lines of lines which can be stored as single
4605  // lines, e.g. inner lines
4606  unsigned int n_single_lines=0;
4607 
4608  // New lines to be created: number lines which are stored in
4609  // pairs (the children of lines must be stored in pairs)
4610  unsigned int n_lines_in_pairs = 0;
4611 
4612  // check how much space is needed on every level we need not
4613  // check the highest level since either - on the highest level
4614  // no cells are flagged for refinement - there are, but
4615  // prepare_refinement added another empty level
4616  unsigned int needed_vertices = 0;
4617  for (int level=triangulation.levels.size()-2; level>=0; --level)
4618  {
4619  // count number of flagged cells on this level and compute
4620  // how many new vertices and new lines will be needed
4621  unsigned int needed_cells = 0;
4622 
4624  cell = triangulation.begin_active(level),
4625  endc = triangulation.begin_active(level+1);
4626  for (; cell!=endc; ++cell)
4627  if (cell->refine_flag_set())
4628  {
4629  if (cell->refine_flag_set()==RefinementCase<dim>::cut_xy)
4630  {
4631  needed_cells += 4;
4632 
4633  // new vertex at center of cell is needed in any
4634  // case
4635  ++needed_vertices;
4636 
4637  // the four inner lines can be stored as singles
4638  n_single_lines += 4;
4639  }
4640  else // cut_x || cut_y
4641  {
4642  // set the flag showing that anisotropic
4643  // refinement is used for at least one cell
4644  triangulation.anisotropic_refinement = true;
4645 
4646  needed_cells += 2;
4647  // no vertex at center
4648 
4649  // the inner line can be stored as single
4650  n_single_lines += 1;
4651 
4652  }
4653 
4654  // mark all faces (lines) for refinement; checking
4655  // locally whether the neighbor would also like to
4656  // refine them is rather difficult for lines so we
4657  // only flag them and after visiting all cells, we
4658  // decide which lines need refinement;
4659  for (unsigned int line_no=0; line_no<GeometryInfo<dim>::faces_per_cell;
4660  ++line_no)
4661  {
4663  cell->refine_flag_set(), line_no)==RefinementCase<1>::cut_x)
4664  {
4665  typename Triangulation<dim,spacedim>::line_iterator
4666  line = cell->line(line_no);
4667  if (line->has_children() == false)
4668  {
4669  line->set_user_flag ();
4670 //TODO[WB]: we overwrite the user_index here because we later on need
4671 // to find out which boundary object we have to ask to refine this
4672 // line. we can't use the boundary_id field because that can
4673 // only be used for lines at the boundary of the domain, but we also
4674 // need a domain description for interior lines in the codim-1 case
4675  if (spacedim > dim)
4676  {
4677  if (line->at_boundary())
4678  // if possible honor boundary
4679  // indicator
4680  line->set_user_index(line->boundary_id());
4681  else
4682  // otherwise take manifold
4683  // description from the adjacent
4684  // cell
4685  line->set_user_index(cell->material_id());
4686  }
4687  }
4688  }
4689  }
4690  }
4691 
4692 
4693  // count number of used cells on the next higher level
4694  const unsigned int used_cells
4695  = std::count_if (triangulation.levels[level+1]->cells.used.begin(),
4696  triangulation.levels[level+1]->cells.used.end(),
4697  std_cxx11::bind (std::equal_to<bool>(), std_cxx11::_1, true));
4698 
4699 
4700  // reserve space for the used_cells cells already existing
4701  // on the next higher level as well as for the
4702  // needed_cells that will be created on that level
4703  triangulation.levels[level+1]
4704  ->reserve_space (used_cells+needed_cells, 2, spacedim);
4705 
4706  // reserve space for needed_cells new quads on the next
4707  // higher level
4708  triangulation.levels[level+1]->cells.
4709  reserve_space (needed_cells,0);
4710  }
4711 
4712  // now count the lines which were flagged for refinement
4713  for (typename Triangulation<dim,spacedim>::line_iterator
4714  line=triangulation.begin_line(); line!=triangulation.end_line(); ++line)
4715  if (line->user_flag_set())
4716  {
4717  Assert (line->has_children() == false, ExcInternalError());
4718  n_lines_in_pairs += 2;
4719  needed_vertices += 1;
4720  }
4721  // reserve space for n_lines_in_pairs new lines. note, that
4722  // we can't reserve space for the single lines here as well,
4723  // as all the space reserved for lines in pairs would be
4724  // counted as unused and we would end up with too little space
4725  // to store all lines. memory reservation for n_single_lines
4726  // can only be done AFTER we refined the lines of the current
4727  // cells
4728  triangulation.faces->lines.
4729  reserve_space (n_lines_in_pairs, 0);
4730 
4731  // add to needed vertices how many vertices are already in use
4732  needed_vertices += std::count_if (triangulation.vertices_used.begin(), triangulation.vertices_used.end(),
4733  std_cxx11::bind (std::equal_to<bool>(), std_cxx11::_1, true));
4734  // if we need more vertices: create them, if not: leave the
4735  // array as is, since shrinking is not really possible because
4736  // some of the vertices at the end may be in use
4737  if (needed_vertices > triangulation.vertices.size())
4738  {
4739  triangulation.vertices.resize (needed_vertices, Point<spacedim>());
4740  triangulation.vertices_used.resize (needed_vertices, false);
4741  }
4742 
4743 
4744  // Do REFINEMENT on every level; exclude highest level as
4745  // above
4746 
4747  // index of next unused vertex
4748  unsigned int next_unused_vertex = 0;
4749 
4750  // first the refinement of lines. children are stored
4751  // pairwise
4752  if (true)
4753  {
4754  // only active objects can be refined further
4755  typename Triangulation<dim,spacedim>::active_line_iterator
4756  line = triangulation.begin_active_line(),
4757  endl = triangulation.end_line();
4758  typename Triangulation<dim,spacedim>::raw_line_iterator
4759  next_unused_line = triangulation.begin_raw_line ();
4760 
4761  for (; line!=endl; ++line)
4762  if (line->user_flag_set())
4763  {
4764  // this line needs to be refined
4765 
4766  // find the next unused vertex and set it
4767  // appropriately
4768  while (triangulation.vertices_used[next_unused_vertex] == true)
4769  ++next_unused_vertex;
4770  Assert (next_unused_vertex < triangulation.vertices.size(),
4771  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."));
4772  triangulation.vertices_used[next_unused_vertex] = true;
4773 
4774  if (spacedim == dim)
4775  {
4776  // for the case of a domain in an
4777  // equal-dimensional space we only have to treat
4778  // boundary lines differently; for interior
4779  // lines we can compute the midpoint as the mean
4780  // of the two vertices: if (line->at_boundary())
4781  triangulation.vertices[next_unused_vertex]
4782  = line->center(true);
4783  }
4784  else
4785  // however, if spacedim>dim, we always have to ask
4786  // the boundary object for its answer. We use the
4787  // same object of the cell (which was stored in
4788  // line->user_index() before) unless a manifold_id
4789  // has been set on this very line.
4790  if (line->manifold_id() == numbers::invalid_manifold_id)
4791  triangulation.vertices[next_unused_vertex]
4792  = triangulation.get_manifold(line->user_index()).get_new_point_on_line (line);
4793  else
4794  triangulation.vertices[next_unused_vertex]
4795  = line->center(true);
4796 
4797  // now that we created the right point, make up the
4798  // two child lines. To this end, find a pair of
4799  // unused lines
4800  bool pair_found=false;
4801  (void)pair_found;
4802  for (; next_unused_line!=endl; ++next_unused_line)
4803  if (!next_unused_line->used() &&
4804  !(++next_unused_line)->used())
4805  {
4806  // go back to the first of the two unused
4807  // lines
4808  --next_unused_line;
4809  pair_found=true;
4810  break;
4811  }
4812  Assert (pair_found, ExcInternalError());
4813 
4814  // there are now two consecutive unused lines, such
4815  // that the children of a line will be consecutive.
4816  // then set the child pointer of the present line
4817  line->set_children (0, next_unused_line->index());
4818 
4819  // set the two new lines
4820  const typename Triangulation<dim,spacedim>::raw_line_iterator
4821  children[2] = { next_unused_line,
4822  ++next_unused_line
4823  };
4824  // some tests; if any of the iterators should be
4825  // invalid, then already dereferencing will fail
4826  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."));
4827  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."));
4828 
4829  children[0]->set (internal::Triangulation
4830  ::TriaObject<1>(line->vertex_index(0),
4831  next_unused_vertex));
4832  children[1]->set (internal::Triangulation
4833  ::TriaObject<1>(next_unused_vertex,
4834  line->vertex_index(1)));
4835 
4836  children[0]->set_used_flag();
4837  children[1]->set_used_flag();
4838  children[0]->clear_children();
4839  children[1]->clear_children();
4840  children[0]->clear_user_data();
4841  children[1]->clear_user_data();
4842  children[0]->clear_user_flag();
4843  children[1]->clear_user_flag();
4844 
4845 
4846  children[0]->set_boundary_id_internal(line->boundary_id());
4847  children[1]->set_boundary_id_internal(line->boundary_id());
4848 
4849  children[0]->set_manifold_id (line->manifold_id());
4850  children[1]->set_manifold_id (line->manifold_id());
4851 
4852  // finally clear flag indicating the need for
4853  // refinement
4854  line->clear_user_flag ();
4855  }
4856  }
4857 
4858 
4859  // Now set up the new cells
4860 
4861  // reserve space for inner lines (can be stored as single
4862  // lines)
4863  triangulation.faces->lines.
4864  reserve_space (0,n_single_lines);
4865 
4867  cells_with_distorted_children;
4868 
4869  // reset next_unused_line, as now also single empty places in
4870  // the vector can be used
4871  typename Triangulation<dim,spacedim>::raw_line_iterator
4872  next_unused_line = triangulation.begin_raw_line ();
4873 
4874  for (int level=0; level<static_cast<int>(triangulation.levels.size())-1; ++level)
4875  {
4876 
4877  // Remember: as we don't operate on the finest level,
4878  // begin_*(level+1) is allowed
4880  cell = triangulation.begin_active(level),
4881  endc = triangulation.begin_active(level+1);
4882 
4884  next_unused_cell = triangulation.begin_raw (level+1);
4885 
4886  for (; cell!=endc; ++cell)
4887  if (cell->refine_flag_set())
4888  {
4889  // set the user flag to indicate, that at least one
4890  // line is at the boundary
4891 
4892  // TODO[Tobias Leicht] find a better place to set
4893  // this flag, so that we do not need so much time to
4894  // check each cell here
4895  if (cell->at_boundary())
4896  cell->set_user_flag();
4897 
4898  // actually set up the children and update neighbor
4899  // information
4900  create_children (triangulation,
4901  next_unused_vertex,
4902  next_unused_line,
4903  next_unused_cell,
4904  cell);
4905 
4906  if ((check_for_distorted_cells == true)
4907  &&
4908  has_distorted_children (cell,
4911  cells_with_distorted_children.distorted_cells.push_back (cell);
4912  // inform all listeners that cell refinement is done
4913  triangulation.signals.post_refinement_on_cell(cell);
4914  }
4915  }
4916 
4917  return cells_with_distorted_children;
4918  }
4919 
4920 
4925  template <int spacedim>
4926  static
4929  const bool check_for_distorted_cells)
4930  {
4931  const unsigned int dim = 3;
4932 
4933  // this function probably also works for spacedim>3 but it
4934  // isn't tested. it will probably be necessary to pull new
4935  // vertices onto the manifold just as we do for the other
4936  // functions above.
4937  Assert (spacedim == 3, ExcNotImplemented());
4938 
4939  // check whether a new level is needed we have to check for
4940  // this on the highest level only (on this, all used cells are
4941  // also active, so we only have to check for this)
4942  if (true)
4943  {
4945  cell = triangulation.begin_active (triangulation.levels.size()-1),
4946  endc = triangulation.end();
4947  for (; cell != endc; ++cell)
4948  if (cell->used())
4949  if (cell->refine_flag_set())
4950  {
4951  triangulation.levels.push_back (new internal::Triangulation::TriaLevel<dim>);
4952  break;
4953  }
4954  }
4955 
4956 
4957  // first clear user flags for quads and lines; we're going to
4958  // use them to flag which lines and quads need refinement
4959  triangulation.faces->quads.clear_user_data();
4960 
4961  for (typename Triangulation<dim,spacedim>::line_iterator
4962  line=triangulation.begin_line(); line!=triangulation.end_line(); ++line)
4963  line->clear_user_flag();
4964  for (typename Triangulation<dim,spacedim>::quad_iterator
4965  quad=triangulation.begin_quad(); quad!=triangulation.end_quad(); ++quad)
4966  quad->clear_user_flag();
4967 
4968  // create an array of face refine cases. User indices of faces
4969  // will be set to values corresponding with indices in this
4970  // array.
4971  const RefinementCase<dim-1> face_refinement_cases[4]=
4972  {
4973  RefinementCase<dim-1>::no_refinement,
4974  RefinementCase<dim-1>::cut_x,
4975  RefinementCase<dim-1>::cut_y,
4976  RefinementCase<dim-1>::cut_xy
4977  };
4978 
4979  // check how much space is needed on every level we need not
4980  // check the highest level since either
4981  // - on the highest level no cells are flagged for refinement
4982  // - there are, but prepare_refinement added another empty
4983  // level which then is the highest level
4984 
4985  // variables to hold the number of newly to be created
4986  // vertices, lines and quads. as these are stored globally,
4987  // declare them outside the loop over al levels. we need lines
4988  // and quads in pairs for refinement of old ones and lines and
4989  // quads, that can be stored as single ones, as they are newly
4990  // created in the inside of an existing cell
4991  unsigned int needed_vertices = 0;
4992  unsigned int needed_lines_single = 0;
4993  unsigned int needed_quads_single = 0;
4994  unsigned int needed_lines_pair = 0;
4995  unsigned int needed_quads_pair = 0;
4996  for (int level=triangulation.levels.size()-2; level>=0; --level)
4997  {
4998  // count number of flagged cells on this level and compute
4999  // how many new vertices and new lines will be needed
5000  unsigned int new_cells = 0;
5001 
5003  acell = triangulation.begin_active(level),
5004  aendc = triangulation.begin_active(level+1);
5005  for (; acell!=aendc; ++acell)
5006  if (acell->refine_flag_set())
5007  {
5008  RefinementCase<dim> ref_case=acell->refine_flag_set();
5009 
5010  // now for interior vertices, lines and quads, which
5011  // are needed in any case
5012  if (ref_case==RefinementCase<dim>::cut_x ||
5013  ref_case==RefinementCase<dim>::cut_y ||
5014  ref_case==RefinementCase<dim>::cut_z)
5015  {
5016  ++needed_quads_single;
5017  new_cells+=2;
5018  triangulation.anisotropic_refinement=true;
5019  }
5020  else if (ref_case==RefinementCase<dim>::cut_xy ||
5021  ref_case==RefinementCase<dim>::cut_xz ||
5022  ref_case==RefinementCase<dim>::cut_yz)
5023  {
5024  ++needed_lines_single;
5025  needed_quads_single += 4;
5026  new_cells+=4;
5027  triangulation.anisotropic_refinement=true;
5028  }
5029  else if (ref_case==RefinementCase<dim>::cut_xyz)
5030  {
5031  ++needed_vertices;
5032  needed_lines_single += 6;
5033  needed_quads_single += 12;
5034  new_cells+=8;
5035  }
5036  else
5037  {
5038  // we should never get here
5039  Assert(false, ExcInternalError());
5040  }
5041 
5042  // mark all faces for refinement; checking locally
5043  // if and how the neighbor would like to refine
5044  // these is difficult so we only flag them and after
5045  // visiting all cells, we decide which faces need
5046  // which refinement;
5047  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell;
5048  ++face)
5049  {
5051  aface = acell->face(face);
5052  // get the RefineCase this faces has for the
5053  // given RefineCase of the cell
5054  RefinementCase<dim-1> face_ref_case=
5056  face,
5057  acell->face_orientation(face),
5058  acell->face_flip(face),
5059  acell->face_rotation(face));
5060  // only do something, if this face has to be
5061  // refined
5062  if (face_ref_case)
5063  {
5065  {
5066  if (aface->number_of_children()<4)
5067  // we use user_flags to denote needed
5068  // isotropic refinement
5069  aface->set_user_flag();
5070  }
5071  else if (aface->refinement_case()!=face_ref_case)
5072  // we use user_indices to denote needed
5073  // anisotropic refinement. note, that we
5074  // can have at most one anisotropic
5075  // refinement case for this face, as
5076  // otherwise prepare_refinement() would
5077  // have changed one of the cells to yield
5078  // isotropic refinement at this
5079  // face. therefore we set the user_index
5080  // uniquely
5081  {
5082  Assert(aface->refinement_case()==RefinementCase<dim-1>::isotropic_refinement ||
5083  aface->refinement_case()==RefinementCase<dim-1>::no_refinement,
5084  ExcInternalError());
5085  aface->set_user_index(face_ref_case);
5086  }
5087  }
5088  }// for all faces
5089 
5090  // flag all lines, that have to be refined
5091  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
5092  if (GeometryInfo<dim>::line_refinement_case(ref_case,line) &&
5093  !acell->line(line)->has_children())
5094  acell->line(line)->set_user_flag();
5095 
5096  }// if refine_flag set and for all cells on this level
5097 
5098 
5099  // count number of used cells on the next higher level
5100  const unsigned int used_cells
5101  = std::count_if (triangulation.levels[level+1]->cells.used.begin(),
5102  triangulation.levels[level+1]->cells.used.end(),
5103  std_cxx11::bind (std::equal_to<bool>(), std_cxx11::_1, true));
5104 
5105 
5106  // reserve space for the used_cells cells already existing
5107  // on the next higher level as well as for the
5108  // 8*flagged_cells that will be created on that level
5109  triangulation.levels[level+1]
5110  ->reserve_space (used_cells+new_cells, 3, spacedim);
5111  // reserve space for 8*flagged_cells new hexes on the next
5112  // higher level
5113  triangulation.levels[level+1]->cells.reserve_space (new_cells);
5114  }// for all levels
5115  // now count the quads and lines which were flagged for
5116  // refinement
5117  for (typename Triangulation<dim,spacedim>::quad_iterator
5118  quad=triangulation.begin_quad(); quad!=triangulation.end_quad(); ++quad)
5119  {
5120  if (quad->user_flag_set())
5121  {
5122  // isotropic refinement: 1 interior vertex, 4 quads
5123  // and 4 interior lines. we store the interior lines
5124  // in pairs in case the face is already or will be
5125  // refined anisotropically
5126  needed_quads_pair += 4;
5127  needed_lines_pair += 4;
5128  needed_vertices += 1;
5129  }
5130  if (quad->user_index())
5131  {
5132  // anisotropic refinement: 1 interior
5133  // line and two quads
5134  needed_quads_pair += 2;
5135  needed_lines_single += 1;
5136  // there is a kind of complicated situation here which
5137  // requires our attention. if the quad is refined
5138  // isotropcally, two of the interior lines will get a
5139  // new mother line - the interior line of our
5140  // anisotropically refined quad. if those two lines
5141  // are not consecutive, we cannot do so and have to
5142  // replace them by two lines that are consecutive. we
5143  // try to avoid that situation, but it may happen
5144  // nevertheless throug repeated refinement and
5145  // coarsening. thus we have to check here, as we will
5146  // need some additional space to store those new lines
5147  // in case we need them...
5148  if (quad->has_children())
5149  {
5150  Assert(quad->refinement_case()==RefinementCase<dim-1>::isotropic_refinement, ExcInternalError());
5151  if ((face_refinement_cases[quad->user_index()]==RefinementCase<dim-1>::cut_x
5152  && (quad->child(0)->line_index(1)+1!=quad->child(2)->line_index(1))) ||
5153  (face_refinement_cases[quad->user_index()]==RefinementCase<dim-1>::cut_y
5154  && (quad->child(0)->line_index(3)+1!=quad->child(1)->line_index(3))))
5155  needed_lines_pair +=2;
5156  }
5157  }
5158  }
5159 
5160  for (typename Triangulation<dim,spacedim>::line_iterator
5161  line=triangulation.begin_line(); line!=triangulation.end_line(); ++line)
5162  if (line->user_flag_set())
5163  {
5164  needed_lines_pair += 2;
5165  needed_vertices += 1;
5166  }
5167 
5168  // reserve space for needed_lines new lines stored in pairs
5169  triangulation.faces->lines.
5170  reserve_space (needed_lines_pair,needed_lines_single);
5171  // reserve space for needed_quads new quads stored in pairs
5172  triangulation.faces->quads.
5173  reserve_space (needed_quads_pair,needed_quads_single);
5174 
5175 
5176  // add to needed vertices how many vertices are already in use
5177  needed_vertices += std::count_if (triangulation.vertices_used.begin(), triangulation.vertices_used.end(),
5178  std_cxx11::bind (std::equal_to<bool>(), std_cxx11::_1, true));
5179  // if we need more vertices: create them, if not: leave the
5180  // array as is, since shrinking is not really possible because
5181  // some of the vertices at the end may be in use
5182  if (needed_vertices > triangulation.vertices.size())
5183  {
5184  triangulation.vertices.resize (needed_vertices, Point<spacedim>());
5185  triangulation.vertices_used.resize (needed_vertices, false);
5186  }
5187 
5188 
5190  // Before we start with the actual refinement, we do some
5191  // sanity checks if in debug mode. especially, we try to catch
5192  // the notorious problem with lines being twice refined,
5193  // i.e. there are cells adjacent at one line ("around the
5194  // edge", but not at a face), with two cells differing by more
5195  // than one refinement level
5196  //
5197  // this check is very simple to implement here, since we have
5198  // all lines flagged if they shall be refined
5199 #ifdef DEBUG
5201  cell=triangulation.begin_active(); cell!=triangulation.end(); ++cell)
5202  if (!cell->refine_flag_set())
5203  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
5204  if (cell->line(line)->has_children())
5205  for (unsigned int c=0; c<2; ++c)
5206  Assert (cell->line(line)->child(c)->user_flag_set() == false,
5207  ExcInternalError());
5208 #endif
5209 
5211  // Do refinement on every level
5212  //
5213  // To make life a bit easier, we first refine those lines and
5214  // quads that were flagged for refinement and then compose the
5215  // newly to be created cells.
5216  //
5217  // index of next unused vertex
5218  unsigned int next_unused_vertex = 0;
5219 
5220  // first for lines
5221  if (true)
5222  {
5223  // only active objects can be refined further
5224  typename Triangulation<dim,spacedim>::active_line_iterator
5225  line = triangulation.begin_active_line(),
5226  endl = triangulation.end_line();
5227  typename Triangulation<dim,spacedim>::raw_line_iterator
5228  next_unused_line = triangulation.begin_raw_line ();
5229 
5230  for (; line!=endl; ++line)
5231  if (line->user_flag_set())
5232  {
5233  // this line needs to be refined
5234 
5235  // find the next unused vertex and set it
5236  // appropriately
5237  while (triangulation.vertices_used[next_unused_vertex] == true)
5238  ++next_unused_vertex;
5239  Assert (next_unused_vertex < triangulation.vertices.size(),
5240  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."));
5241  triangulation.vertices_used[next_unused_vertex] = true;
5242 
5243  triangulation.vertices[next_unused_vertex]
5244  = line->center(true);
5245 
5246  // now that we created the right point, make up the
5247  // two child lines (++ takes care of the end of the
5248  // vector)
5249  next_unused_line=triangulation.faces->lines.next_free_pair_object(triangulation);
5250  Assert(next_unused_line.state() == IteratorState::valid,
5251  ExcInternalError());
5252 
5253  // now we found two consecutive unused lines, such
5254  // that the children of a line will be consecutive.
5255  // then set the child pointer of the present line
5256  line->set_children (0, next_unused_line->index());
5257 
5258  // set the two new lines
5259  const typename Triangulation<dim,spacedim>::raw_line_iterator
5260  children[2] = { next_unused_line,
5261  ++next_unused_line
5262  };
5263 
5264  // some tests; if any of the iterators should be
5265  // invalid, then already dereferencing will fail
5266  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."));
5267  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."));
5268 
5269  children[0]->set (internal::Triangulation
5270  ::TriaObject<1>(line->vertex_index(0),
5271  next_unused_vertex));
5272  children[1]->set (internal::Triangulation
5273  ::TriaObject<1>(next_unused_vertex,
5274  line->vertex_index(1)));
5275 
5276  children[0]->set_used_flag();
5277  children[1]->set_used_flag();
5278  children[0]->clear_children();
5279  children[1]->clear_children();
5280  children[0]->clear_user_data();
5281  children[1]->clear_user_data();
5282  children[0]->clear_user_flag();
5283  children[1]->clear_user_flag();
5284 
5285  children[0]->set_boundary_id_internal(line->boundary_id());
5286  children[1]->set_boundary_id_internal(line->boundary_id());
5287 
5288  children[0]->set_manifold_id (line->manifold_id());
5289  children[1]->set_manifold_id (line->manifold_id());
5290 
5291  // finally clear flag
5292  // indicating the need
5293  // for refinement
5294  line->clear_user_flag ();
5295  }
5296  }
5297 
5298 
5300  // now refine marked quads
5302 
5303  // here we encounter several cases:
5304 
5305  // a) the quad is unrefined and shall be refined isotropically
5306 
5307  // b) the quad is unrefined and shall be refined
5308  // anisotropically
5309 
5310  // c) the quad is unrefined and shall be refined both
5311  // anisotropically and isotropically (this is reduced to case
5312  // b) and then case b) for the children again)
5313 
5314  // d) the quad is refined anisotropically and shall be refined
5315  // isotropically (this is reduced to case b) for the
5316  // anisotropic children)
5317 
5318  // e) the quad is refined isotropically and shall be refined
5319  // anisotropically (this is transformed to case c), however we
5320  // might have to renumber/rename children...)
5321 
5322  // we need a loop in cases c) and d), as the anisotropic
5323  // children migt have a lower index than the mother quad
5324  for (unsigned int loop=0; loop<2; ++loop)
5325  {
5326  // usually, only active objects can be refined
5327  // further. however, in cases d) and e) that is not true,
5328  // so we have to use 'normal' iterators here
5329  typename Triangulation<dim,spacedim>::quad_iterator
5330  quad = triangulation.begin_quad(),
5331  endq = triangulation.end_quad();
5332  typename Triangulation<dim,spacedim>::raw_line_iterator
5333  next_unused_line = triangulation.begin_raw_line ();
5334  typename Triangulation<dim,spacedim>::raw_quad_iterator
5335  next_unused_quad = triangulation.begin_raw_quad ();
5336 
5337  for (; quad!=endq; ++quad)
5338  {
5339  if (quad->user_index())
5340  {
5341  RefinementCase<dim-1> aniso_quad_ref_case=face_refinement_cases[quad->user_index()];
5342  // there is one unlikely event here, where we
5343  // already have refind the face: if the face was
5344  // refined anisotropically and we want to refine
5345  // it isotropically, both children are flagged for
5346  // anisotropic refinement. however, if those
5347  // children were already flagged for anisotropic
5348  // refinement, they might already be processed and
5349  // refined.
5350  if (aniso_quad_ref_case == quad->refinement_case())
5351  continue;
5352 
5353  Assert(quad->refinement_case()==RefinementCase<dim-1>::cut_xy ||
5354  quad->refinement_case()==RefinementCase<dim-1>::no_refinement,
5355  ExcInternalError());
5356 
5357  // this quad needs to be refined anisotropically
5358  Assert(quad->user_index() == RefinementCase<dim-1>::cut_x ||
5359  quad->user_index() == RefinementCase<dim-1>::cut_y,
5360  ExcInternalError());
5361 
5362  // make the new line interior to the quad
5363  typename Triangulation<dim,spacedim>::raw_line_iterator new_line;
5364 
5365  new_line=triangulation.faces->lines.next_free_single_object(triangulation);
5366  Assert (new_line->used() == false,
5367  ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
5368 
5369  // first collect the
5370  // indices of the vertices:
5371  // *--1--*
5372  // | | |
5373  // | | | cut_x
5374  // | | |
5375  // *--0--*
5376  //
5377  // *-----*
5378  // | |
5379  // 0-----1 cut_y
5380  // | |
5381  // *-----*
5382  unsigned int vertex_indices[2];
5383  if (aniso_quad_ref_case==RefinementCase<dim-1>::cut_x)
5384  {
5385  vertex_indices[0]=quad->line(2)->child(0)->vertex_index(1);
5386  vertex_indices[1]=quad->line(3)->child(0)->vertex_index(1);
5387  }
5388  else
5389  {
5390  vertex_indices[0]=quad->line(0)->child(0)->vertex_index(1);
5391  vertex_indices[1]=quad->line(1)->child(0)->vertex_index(1);
5392  }
5393 
5394  new_line->set (internal::Triangulation::
5395  TriaObject<1>(vertex_indices[0], vertex_indices[1]));
5396  new_line->set_used_flag();
5397  new_line->clear_user_flag();
5398  new_line->clear_user_data();
5399  new_line->clear_children();
5400  new_line->set_boundary_id_internal(quad->boundary_id());
5401  new_line->set_manifold_id(quad->manifold_id());
5402 
5403  // child 0 and 1 of a line are switched if the
5404  // line orientation is false. set up a miniature
5405  // table, indicating which child to take for line
5406  // orientations false and true. first index: child
5407  // index in standard orientation, second index:
5408  // line orientation
5409  const unsigned int index[2][2]=
5410  {
5411  {1,0}, // child 0, line_orientation=false and true
5412  {0,1}
5413  }; // child 1, line_orientation=false and true
5414 
5415  // find some space (consecutive) for the two newly
5416  // to be created quads.
5417  typename Triangulation<dim,spacedim>::raw_quad_iterator new_quads[2];
5418 
5419  next_unused_quad=triangulation.faces->quads.next_free_pair_object(triangulation);
5420  new_quads[0] = next_unused_quad;
5421  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."));
5422 
5423  ++next_unused_quad;
5424  new_quads[1] = next_unused_quad;
5425  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."));
5426 
5427 
5428  if (aniso_quad_ref_case==RefinementCase<dim-1>::cut_x)
5429  {
5430  new_quads[0]->set (internal::Triangulation
5431  ::TriaObject<2>(quad->line_index(0),
5432  new_line->index(),
5433  quad->line(2)->child(index[0][quad->line_orientation(2)])->index(),
5434  quad->line(3)->child(index[0][quad->line_orientation(3)])->index()));
5435  new_quads[1]->set (internal::Triangulation
5436  ::TriaObject<2>(new_line->index(),
5437  quad->line_index(1),
5438  quad->line(2)->child(index[1][quad->line_orientation(2)])->index(),
5439  quad->line(3)->child(index[1][quad->line_orientation(3)])->index()));
5440  }
5441  else
5442  {
5443  new_quads[0]->set (internal::Triangulation
5444  ::TriaObject<2>(quad->line(0)->child(index[0][quad->line_orientation(0)])->index(),
5445  quad->line(1)->child(index[0][quad->line_orientation(1)])->index(),
5446  quad->line_index(2),
5447  new_line->index()));
5448  new_quads[1]->set (internal::Triangulation
5449  ::TriaObject<2>(quad->line(0)->child(index[1][quad->line_orientation(0)])->index(),
5450  quad->line(1)->child(index[1][quad->line_orientation(1)])->index(),
5451  new_line->index(),
5452  quad->line_index(3)));
5453  }
5454 
5455  for (unsigned int i=0; i<2; ++i)
5456  {
5457  new_quads[i]->set_used_flag();
5458  new_quads[i]->clear_user_flag();
5459  new_quads[i]->clear_user_data();
5460  new_quads[i]->clear_children();
5461  new_quads[i]->set_boundary_id_internal(quad->boundary_id());
5462  new_quads[i]->set_manifold_id (quad->manifold_id());
5463  // set all line orientations to true, change
5464  // this after the loop, as we have to consider
5465  // different lines for each child
5466  for (unsigned int j=0; j<GeometryInfo<dim>::lines_per_face; ++j)
5467  new_quads[i]->set_line_orientation(j,true);
5468  }
5469  // now set the line orientation of children of
5470  // outer lines correctly, the lines in the
5471  // interior of the refined quad are automatically
5472  // oriented conforming to the standard
5473  new_quads[0]->set_line_orientation(0,quad->line_orientation(0));
5474  new_quads[0]->set_line_orientation(2,quad->line_orientation(2));
5475  new_quads[1]->set_line_orientation(1,quad->line_orientation(1));
5476  new_quads[1]->set_line_orientation(3,quad->line_orientation(3));
5477  if (aniso_quad_ref_case==RefinementCase<dim-1>::cut_x)
5478  {
5479  new_quads[0]->set_line_orientation(3,quad->line_orientation(3));
5480  new_quads[1]->set_line_orientation(2,quad->line_orientation(2));
5481  }
5482  else
5483  {
5484  new_quads[0]->set_line_orientation(1,quad->line_orientation(1));
5485  new_quads[1]->set_line_orientation(0,quad->line_orientation(0));
5486  }
5487 
5488  // test, whether this face is refined
5489  // isotropically already. if so, set the correct
5490  // children pointers.
5491  if (quad->refinement_case()==RefinementCase<dim-1>::cut_xy)
5492  {
5493  // we will put a new refinemnt level of
5494  // anisotropic refinement between the
5495  // unrefined and isotropically refined quad
5496  // ending up with the same fine quads but
5497  // introducing anisotropically refined ones as
5498  // children of the unrefined quad and mother
5499  // cells of the original fine ones.
5500 
5501  // this process includes the creation of a new
5502  // middle line which we will assign as the
5503  // mother line of two of the existing inner
5504  // lines. If those inner lines are not
5505  // consecutive in memory, we won't find them
5506  // later on, so we have to create new ones
5507  // instead and replace all occurrences of the
5508  // old ones with those new ones. As this is
5509  // kind of ugly, we hope we don't have to do
5510  // it often...
5511  typename Triangulation<dim,spacedim>::line_iterator old_child[2];
5512  if (aniso_quad_ref_case==RefinementCase<dim-1>::cut_x)
5513  {
5514  old_child[0]=quad->child(0)->line(1);
5515  old_child[1]=quad->child(2)->line(1);
5516  }
5517  else
5518  {
5519  Assert(aniso_quad_ref_case==RefinementCase<dim-1>::cut_y, ExcInternalError());
5520 
5521  old_child[0]=quad->child(0)->line(3);
5522  old_child[1]=quad->child(1)->line(3);
5523  }
5524 
5525  if (old_child[0]->index()+1 != old_child[1]->index())
5526  {
5527  // this is exactly the ugly case we taked
5528  // about. so, no coimplaining, lets get
5529  // two new lines and copy all info
5530  typename Triangulation<dim,spacedim>::raw_line_iterator new_child[2];
5531 
5532  new_child[0]=new_child[1]=triangulation.faces->lines.next_free_pair_object(triangulation);
5533  ++new_child[1];
5534 
5535  new_child[0]->set_used_flag();
5536  new_child[1]->set_used_flag();
5537 
5538  const int old_index_0=old_child[0]->index(),
5539  old_index_1=old_child[1]->index(),
5540  new_index_0=new_child[0]->index(),
5541  new_index_1=new_child[1]->index();
5542 
5543  // loop over all quads and replace the old
5544  // lines
5545  for (unsigned int q=0; q<triangulation.faces->quads.cells.size(); ++q)
5546  for (unsigned int l=0; l<GeometryInfo<dim>::lines_per_face; ++l)
5547  {
5548  const int this_index=triangulation.faces->quads.cells[q].face(l);
5549  if (this_index==old_index_0)
5550  triangulation.faces->quads.cells[q].set_face(l,new_index_0);
5551  else if (this_index==old_index_1)
5552  triangulation.faces->quads.cells[q].set_face(l,new_index_1);
5553  }
5554  // now we have to copy all information of
5555  // the two lines
5556  for (unsigned int i=0; i<2; ++i)
5557  {
5558  Assert(!old_child[i]->has_children(), ExcInternalError());
5559 
5560  new_child[i]->set(internal::Triangulation::TriaObject<1>(old_child[i]->vertex_index(0),
5561  old_child[i]->vertex_index(1)));
5562  new_child[i]->set_boundary_id_internal(old_child[i]->boundary_id());
5563  new_child[i]->set_manifold_id(old_child[i]->manifold_id());
5564  new_child[i]->set_user_index(old_child[i]->user_index());
5565  if (old_child[i]->user_flag_set())
5566  new_child[i]->set_user_flag();
5567  else
5568  new_child[i]->clear_user_flag();
5569 
5570  new_child[i]->clear_children();
5571 
5572  old_child[i]->clear_user_flag();
5573  old_child[i]->clear_user_index();
5574  old_child[i]->clear_used_flag();
5575  }
5576  }
5577  // now that we cared about the lines, go on
5578  // with the quads themselves, where we might
5579  // encounter similar situations...
5580  if (aniso_quad_ref_case==RefinementCase<dim-1>::cut_x)
5581  {
5582  new_line->set_children(0, quad->child(0)->line_index(1));
5583  Assert(new_line->child(1)==quad->child(2)->line(1),
5584  ExcInternalError());
5585  // now evereything is quite
5586  // complicated. we have the children
5587  // numbered according to
5588  //
5589  // *---*---*
5590  // |n+2|n+3|
5591  // *---*---*
5592  // | n |n+1|
5593  // *---*---*
5594  //
5595  // from the original isotropic
5596  // refinement. we have to reorder them as
5597  //
5598  // *---*---*
5599  // |n+1|n+3|
5600  // *---*---*
5601  // | n |n+2|
5602  // *---*---*
5603  //
5604  // such that n and n+1 are consecutive
5605  // children of m and n+2 and n+3 are
5606  // consecutive children of m+1, where m
5607  // and m+1 are given as in
5608  //
5609  // *---*---*
5610  // | | |
5611  // | m |m+1|
5612  // | | |
5613  // *---*---*
5614  //
5615  // this is a bit ugly, of course: loop
5616  // over all cells on all levels and look
5617  // for faces n+1 (switch_1) and n+2
5618  // (switch_2).
5619  const typename Triangulation<dim,spacedim>::quad_iterator
5620  switch_1=quad->child(1),
5621  switch_2=quad->child(2);
5622  const int switch_1_index=switch_1->index();
5623  const int switch_2_index=switch_2->index();
5624  for (unsigned int l=0; l<triangulation.levels.size(); ++l)
5625  for (unsigned int h=0; h<triangulation.levels[l]->cells.cells.size(); ++h)
5626  for (unsigned int q=0; q<GeometryInfo<dim>::faces_per_cell; ++q)
5627  {
5628  const int face_index=triangulation.levels[l]->cells.cells[h].face(q);
5629  if (face_index==switch_1_index)
5630  triangulation.levels[l]->cells.cells[h].set_face(q,switch_2_index);
5631  else if (face_index==switch_2_index)
5632  triangulation.levels[l]->cells.cells[h].set_face(q,switch_1_index);
5633  }
5634  // now we have to copy all information of
5635  // the two quads
5636  const unsigned int switch_1_lines[4]=
5637  {
5638  switch_1->line_index(0),
5639  switch_1->line_index(1),
5640  switch_1->line_index(2),
5641  switch_1->line_index(3)
5642  };
5643  const bool switch_1_line_orientations[4]=
5644  {
5645  switch_1->line_orientation(0),
5646  switch_1->line_orientation(1),
5647  switch_1->line_orientation(2),
5648  switch_1->line_orientation(3)
5649  };
5650  const types::boundary_id switch_1_boundary_id=switch_1->boundary_id();
5651  const unsigned int switch_1_user_index=switch_1->user_index();
5652  const bool switch_1_user_flag=switch_1->user_flag_set();
5653  const RefinementCase<dim-1> switch_1_refinement_case=switch_1->refinement_case();
5654  const int switch_1_first_child_pair=(switch_1_refinement_case ? switch_1->child_index(0) : -1);
5655  const int switch_1_second_child_pair=(switch_1_refinement_case==RefinementCase<dim-1>::cut_xy ? switch_1->child_index(2) : -1);
5656 
5657  switch_1->set(internal::Triangulation::TriaObject<2>(switch_2->line_index(0),
5658  switch_2->line_index(1),
5659  switch_2->line_index(2),
5660  switch_2->line_index(3)));
5661  switch_1->set_line_orientation(0, switch_2->line_orientation(0));
5662  switch_1->set_line_orientation(1, switch_2->line_orientation(1));
5663  switch_1->set_line_orientation(2, switch_2->line_orientation(2));
5664  switch_1->set_line_orientation(3, switch_2->line_orientation(3));
5665  switch_1->set_boundary_id_internal(switch_2->boundary_id());
5666  switch_1->set_manifold_id(switch_2->manifold_id());
5667  switch_1->set_user_index(switch_2->user_index());
5668  if (switch_2->user_flag_set())
5669  switch_1->set_user_flag();
5670  else
5671  switch_1->clear_user_flag();
5672  switch_1->clear_refinement_case();
5673  switch_1->set_refinement_case(switch_2->refinement_case());
5674  switch_1->clear_children();
5675  if (switch_2->refinement_case())
5676  switch_1->set_children(0, switch_2->child_index(0));
5677  if (switch_2->refinement_case()==RefinementCase<dim-1>::cut_xy)
5678  switch_1->set_children(2, switch_2->child_index(2));
5679 
5680  switch_2->set(internal::Triangulation::TriaObject<2>(switch_1_lines[0],
5681  switch_1_lines[1],
5682  switch_1_lines[2],
5683  switch_1_lines[3]));
5684  switch_2->set_line_orientation(0, switch_1_line_orientations[0]);
5685  switch_2->set_line_orientation(1, switch_1_line_orientations[1]);
5686  switch_2->set_line_orientation(2, switch_1_line_orientations[2]);
5687  switch_2->set_line_orientation(3, switch_1_line_orientations[3]);
5688  switch_2->set_boundary_id_internal(switch_1_boundary_id);
5689  switch_2->set_manifold_id(switch_1->manifold_id());
5690  switch_2->set_user_index(switch_1_user_index);
5691  if (switch_1_user_flag)
5692  switch_2->set_user_flag();
5693  else
5694  switch_2->clear_user_flag();
5695  switch_2->clear_refinement_case();
5696  switch_2->set_refinement_case(switch_1_refinement_case);
5697  switch_2->clear_children();
5698  switch_2->set_children(0, switch_1_first_child_pair);
5699  switch_2->set_children(2, switch_1_second_child_pair);
5700 
5701  new_quads[0]->set_refinement_case(RefinementCase<2>::cut_y);
5702  new_quads[0]->set_children(0, quad->child_index(0));
5703  new_quads[1]->set_refinement_case(RefinementCase<2>::cut_y);
5704  new_quads[1]->set_children(0, quad->child_index(2));
5705  }
5706  else
5707  {
5708  new_quads[0]->set_refinement_case(RefinementCase<2>::cut_x);
5709  new_quads[0]->set_children(0, quad->child_index(0));
5710  new_quads[1]->set_refinement_case(RefinementCase<2>::cut_x);
5711  new_quads[1]->set_children(0, quad->child_index(2));
5712  new_line->set_children(0, quad->child(0)->line_index(3));
5713  Assert(new_line->child(1)==quad->child(1)->line(3),
5714  ExcInternalError());
5715  }
5716  quad->clear_children();
5717  }
5718 
5719  // note these quads as children to the present one
5720  quad->set_children (0, new_quads[0]->index());
5721 
5722  quad->set_refinement_case(aniso_quad_ref_case);
5723 
5724  // finally clear flag indicating the need for
5725  // refinement
5726  quad->clear_user_data ();
5727  } // if (anisotropic refinement)
5728 
5729  if (quad->user_flag_set())
5730  {
5731  // this quad needs to be refined isotropically
5732 
5733  // first of all: we only get here in the first run
5734  // of the loop
5736 
5737  // find the next unused vertex. we'll need this in
5738  // any case
5739  while (triangulation.vertices_used[next_unused_vertex] == true)
5740  ++next_unused_vertex;
5741  Assert (next_unused_vertex < triangulation.vertices.size(),
5742  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."));
5743 
5744  // now: if the quad is refined anisotropically
5745  // already, set the anisotropic refinement flag
5746  // for both children. Additionally, we have to
5747  // refine the inner line, as it is an outer line
5748  // of the two (anisotropic) children
5749  const RefinementCase<dim-1> quad_ref_case=quad->refinement_case();
5750 
5751  if (quad_ref_case==RefinementCase<dim-1>::cut_x ||
5752  quad_ref_case==RefinementCase<dim-1>::cut_y)
5753  {
5754  // set the 'opposite' refine case for children
5755  quad->child(0)->set_user_index(RefinementCase<dim-1>::cut_xy-quad_ref_case);
5756  quad->child(1)->set_user_index(RefinementCase<dim-1>::cut_xy-quad_ref_case);
5757  // refine the inner line
5758  typename Triangulation<dim,spacedim>::line_iterator middle_line;
5759  if (quad_ref_case==RefinementCase<dim-1>::cut_x)
5760  middle_line=quad->child(0)->line(1);
5761  else
5762  middle_line=quad->child(0)->line(3);
5763 
5764  // if the face has been refined
5765  // anisotropically in the last refinement step
5766  // it might be, that it is flagged already and
5767  // that the middle line is thus refined
5768  // already. if not create children.
5769  if (!middle_line->has_children())
5770  {
5771  // set the middle vertex
5772  // appropriately. double refinement of
5773  // quads can only happen in the interior
5774  // of the domain, so we need not care
5775  // about boundary quads here
5776  triangulation.vertices[next_unused_vertex]
5777  = middle_line->center(true);
5778  triangulation.vertices_used[next_unused_vertex] = true;
5779 
5780  // now search a slot for the two
5781  // child lines
5782  next_unused_line=triangulation.faces->lines.next_free_pair_object(triangulation);
5783 
5784  // set the child pointer of the present
5785  // line
5786  middle_line->set_children (0, next_unused_line->index());
5787 
5788  // set the two new lines
5789  const typename Triangulation<dim,spacedim>::raw_line_iterator
5790  children[2] = { next_unused_line,
5791  ++next_unused_line
5792  };
5793 
5794  // some tests; if any of the iterators
5795  // should be invalid, then already
5796  // dereferencing will fail
5797  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."));
5798  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."));
5799 
5800  children[0]->set (internal::Triangulation::
5801  TriaObject<1>(middle_line->vertex_index(0),
5802  next_unused_vertex));
5803  children[1]->set (internal::Triangulation::
5804  TriaObject<1>(next_unused_vertex,
5805  middle_line->vertex_index(1)));
5806 
5807  children[0]->set_used_flag();
5808  children[1]->set_used_flag();
5809  children[0]->clear_children();
5810  children[1]->clear_children();
5811  children[0]->clear_user_data();
5812  children[1]->clear_user_data();
5813  children[0]->clear_user_flag();
5814  children[1]->clear_user_flag();
5815 
5816  children[0]->set_boundary_id_internal (middle_line->boundary_id());
5817  children[1]->set_boundary_id_internal (middle_line->boundary_id());
5818 
5819  children[0]->set_manifold_id (middle_line->manifold_id());
5820  children[1]->set_manifold_id (middle_line->manifold_id());
5821  }
5822  // now remove the flag from the quad and go to
5823  // the next quad, the actual refinement of the
5824  // quad takes place later on in this pass of
5825  // the loop or in the next one
5826  quad->clear_user_flag();
5827  continue;
5828  } // if (several refinement cases)
5829 
5830  // if we got here, we have an unrefined quad and
5831  // have to do the usual work like in an purely
5832  // isotropic refinement
5834 
5835  // set the middle vertex
5836  // appropriately
5837  if (quad->at_boundary() ||
5838  (quad->manifold_id() != numbers::invalid_manifold_id) )
5839  triangulation.vertices[next_unused_vertex]
5840  = quad->center(true);
5841  else
5842  {
5843  // it might be that the quad itself is not at
5844  // the boundary, but that one of its lines
5845  // actually is. in this case, the newly
5846  // created vertices at the centers of the
5847  // lines are not necessarily the mean values
5848  // of the adjacent vertices, so do not compute
5849  // the new vertex as the mean value of the 4
5850  // vertices of the face, but rather as a
5851  // weighted mean value of the 8 vertices which
5852  // we already have (the four old ones, and the
5853  // four ones inserted as middle points for the
5854  // four lines). summing up some more points is
5855  // generally cheaper than first asking whether
5856  // one of the lines is at the boundary
5857  //
5858  // note that the exact weights are chosen such
5859  // as to minimize the distortion of the four
5860  // new quads from the optimal shape; their
5861  // derivation and values is copied over from
5862  // the @p{MappingQ::set_laplace_on_vector}
5863  // function
5864  triangulation.vertices[next_unused_vertex] =
5865  quad->center(true, true);
5866  }
5867  triangulation.vertices_used[next_unused_vertex] = true;
5868  // now that we created the right point, make up
5869  // the four lines interior to the quad (++ takes
5870  // care of the end of the vector)
5871  typename Triangulation<dim,spacedim>::raw_line_iterator new_lines[4];
5872 
5873  for (unsigned int i=0; i<4; ++i)
5874  {
5875  if (i%2==0)
5876  // search a free pair of lines for 0. and
5877  // 2. line, so that two of them end up
5878  // together, which is necessary if later on
5879  // we want to refine the quad
5880  // anisotropically and the two lines end up
5881  // as children of new line
5882  next_unused_line=triangulation.faces->lines.next_free_pair_object(triangulation);
5883 
5884  new_lines[i] = next_unused_line;
5885  ++next_unused_line;
5886 
5887  Assert (new_lines[i]->used() == false,
5888  ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
5889  }
5890 
5891  // set the data of the four lines. first collect
5892  // the indices of the five vertices:
5893  //
5894  // *--3--*
5895  // | | |
5896  // 0--4--1
5897  // | | |
5898  // *--2--*
5899  //
5900  // the lines are numbered as follows:
5901  //
5902  // *--*--*
5903  // | 1 |
5904  // *2-*-3*
5905  // | 0 |
5906  // *--*--*
5907 
5908  const unsigned int vertex_indices[5]
5909  = { quad->line(0)->child(0)->vertex_index(1),
5910  quad->line(1)->child(0)->vertex_index(1),
5911  quad->line(2)->child(0)->vertex_index(1),
5912  quad->line(3)->child(0)->vertex_index(1),
5913  next_unused_vertex
5914  };
5915 
5916  new_lines[0]->set (internal::Triangulation::
5917  TriaObject<1>(vertex_indices[2], vertex_indices[4]));
5918  new_lines[1]->set (internal::Triangulation::
5919  TriaObject<1>(vertex_indices[4], vertex_indices[3]));
5920  new_lines[2]->set (internal::Triangulation::
5921  TriaObject<1>(vertex_indices[0], vertex_indices[4]));
5922  new_lines[3]->set (internal::Triangulation::
5923  TriaObject<1>(vertex_indices[4], vertex_indices[1]));
5924 
5925  for (unsigned int i=0; i<4; ++i)
5926  {
5927  new_lines[i]->set_used_flag();
5928  new_lines[i]->clear_user_flag();
5929  new_lines[i]->clear_user_data();
5930  new_lines[i]->clear_children();
5931  new_lines[i]->set_boundary_id_internal(quad->boundary_id());
5932  new_lines[i]->set_manifold_id(quad->manifold_id());
5933  }
5934 
5935  // now for the quads. again, first collect some
5936  // data about the indices of the lines, with the
5937  // following numbering:
5938  //
5939  // .-6-.-7-.
5940  // 1 9 3
5941  // .-10.11-.
5942  // 0 8 2
5943  // .-4-.-5-.
5944 
5945  // child 0 and 1 of a line are switched if the
5946  // line orientation is false. set up a miniature
5947  // table, indicating which child to take for line
5948  // orientations false and true. first index: child
5949  // index in standard orientation, second index:
5950  // line orientation
5951  const unsigned int index[2][2]=
5952  {
5953  {1,0}, // child 0, line_orientation=false and true
5954  {0,1}
5955  }; // child 1, line_orientation=false and true
5956 
5957  const int line_indices[12]
5958  = { quad->line(0)->child(index[0][quad->line_orientation(0)])->index(),
5959  quad->line(0)->child(index[1][quad->line_orientation(0)])->index(),
5960  quad->line(1)->child(index[0][quad->line_orientation(1)])->index(),
5961  quad->line(1)->child(index[1][quad->line_orientation(1)])->index(),
5962  quad->line(2)->child(index[0][quad->line_orientation(2)])->index(),
5963  quad->line(2)->child(index[1][quad->line_orientation(2)])->index(),
5964  quad->line(3)->child(index[0][quad->line_orientation(3)])->index(),
5965  quad->line(3)->child(index[1][quad->line_orientation(3)])->index(),
5966  new_lines[0]->index(),
5967  new_lines[1]->index(),
5968  new_lines[2]->index(),
5969  new_lines[3]->index()
5970  };
5971 
5972  // find some space (consecutive)
5973  // for the first two newly to be
5974  // created quads.
5975  typename Triangulation<dim,spacedim>::raw_quad_iterator new_quads[4];
5976 
5977  next_unused_quad=triangulation.faces->quads.next_free_pair_object(triangulation);
5978 
5979  new_quads[0] = next_unused_quad;
5980  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."));
5981 
5982  ++next_unused_quad;
5983  new_quads[1] = next_unused_quad;
5984  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."));
5985 
5986  next_unused_quad=triangulation.faces->quads.next_free_pair_object(triangulation);
5987  new_quads[2] = next_unused_quad;
5988  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."));
5989 
5990  ++next_unused_quad;
5991  new_quads[3] = next_unused_quad;
5992  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."));
5993 
5994  // note these quads as children to the present one
5995  quad->set_children (0, new_quads[0]->index());
5996  quad->set_children (2, new_quads[2]->index());
5997  new_quads[0]->set (internal::Triangulation
5998  ::TriaObject<2>(line_indices[0],
5999  line_indices[8],
6000  line_indices[4],
6001  line_indices[10]));
6002 
6003  quad->set_refinement_case(RefinementCase<2>::cut_xy);
6004 
6005  new_quads[0]->set (internal::Triangulation
6006  ::TriaObject<2>(line_indices[0],
6007  line_indices[8],
6008  line_indices[4],
6009  line_indices[10]));
6010  new_quads[1]->set (internal::Triangulation
6011  ::TriaObject<2>(line_indices[8],
6012  line_indices[2],
6013  line_indices[5],
6014  line_indices[11]));
6015  new_quads[2]->set (internal::Triangulation
6016  ::TriaObject<2>(line_indices[1],
6017  line_indices[9],
6018  line_indices[10],
6019  line_indices[6]));
6020  new_quads[3]->set (internal::Triangulation
6021  ::TriaObject<2>(line_indices[9],
6022  line_indices[3],
6023  line_indices[11],
6024  line_indices[7]));
6025  for (unsigned int i=0; i<4; ++i)
6026  {
6027  new_quads[i]->set_used_flag();
6028  new_quads[i]->clear_user_flag();
6029  new_quads[i]->clear_user_data();
6030  new_quads[i]->clear_children();
6031  new_quads[i]->set_boundary_id_internal (quad->boundary_id());
6032  new_quads[i]->set_manifold_id (quad->manifold_id());
6033  // set all line orientations to true, change
6034  // this after the loop, as we have to consider
6035  // different lines for each child
6036  for (unsigned int j=0; j<GeometryInfo<dim>::lines_per_face; ++j)
6037  new_quads[i]->set_line_orientation(j,true);
6038  }
6039  // now set the line orientation of children of
6040  // outer lines correctly, the lines in the
6041  // interior of the refined quad are automatically
6042  // oriented conforming to the standard
6043  new_quads[0]->set_line_orientation(0,quad->line_orientation(0));
6044  new_quads[0]->set_line_orientation(2,quad->line_orientation(2));
6045  new_quads[1]->set_line_orientation(1,quad->line_orientation(1));
6046  new_quads[1]->set_line_orientation(2,quad->line_orientation(2));
6047  new_quads[2]->set_line_orientation(0,quad->line_orientation(0));
6048  new_quads[2]->set_line_orientation(3,quad->line_orientation(3));
6049  new_quads[3]->set_line_orientation(1,quad->line_orientation(1));
6050  new_quads[3]->set_line_orientation(3,quad->line_orientation(3));
6051 
6052  // finally clear flag indicating the need for
6053  // refinement
6054  quad->clear_user_flag ();
6055  } // if (isotropic refinement)
6056  } // for all quads
6057  } // looped two times over all quads, all quads refined now
6058 
6060  // Now, finally, set up the new
6061  // cells
6063 
6065  cells_with_distorted_children;
6066 
6067  for (unsigned int level=0; level!=triangulation.levels.size()-1; ++level)
6068  {
6069  // only active objects can be refined further; remember
6070  // that we won't operate on the finest level, so
6071  // triangulation.begin_*(level+1) is allowed
6072  typename Triangulation<dim,spacedim>::active_hex_iterator
6073  hex = triangulation.begin_active_hex(level),
6074  endh = triangulation.begin_active_hex(level+1);
6075  typename Triangulation<dim,spacedim>::raw_hex_iterator
6076  next_unused_hex = triangulation.begin_raw_hex (level+1);
6077 
6078  for (; hex!=endh; ++hex)
6079  if (hex->refine_flag_set())
6080  {
6081  // this hex needs to be refined
6082 
6083  // clear flag indicating the need for refinement. do
6084  // it here already, since we can't do it anymore
6085  // once the cell has children
6086  const RefinementCase<dim> ref_case=hex->refine_flag_set();
6087  hex->clear_refine_flag ();
6088  hex->set_refinement_case(ref_case);
6089 
6090  // depending on the refine case we might have to
6091  // create additional vertices, lines and quads
6092  // interior of the hex before the actual children
6093  // can be set up.
6094 
6095  // in a first step: reserve the needed space for
6096  // lines, quads and hexes and initialize them
6097  // correctly
6098 
6099  unsigned int n_new_lines=0;
6100  unsigned int n_new_quads=0;
6101  unsigned int n_new_hexes=0;
6102  switch (ref_case)
6103  {
6107  n_new_lines=0;
6108  n_new_quads=1;
6109  n_new_hexes=2;
6110  break;
6114  n_new_lines=1;
6115  n_new_quads=4;
6116  n_new_hexes=4;
6117  break;
6119  n_new_lines=6;
6120  n_new_quads=12;
6121  n_new_hexes=8;
6122  break;
6123  default:
6124  Assert(false, ExcInternalError());
6125  break;
6126  }
6127 
6128  // find some space for the newly to be created
6129  // interior lines and initialize them.
6130  std::vector<typename Triangulation<dim,spacedim>::raw_line_iterator>
6131  new_lines(n_new_lines);
6132  for (unsigned int i=0; i<n_new_lines; ++i)
6133  {
6134  new_lines[i] = triangulation.faces->lines.next_free_single_object(triangulation);
6135 
6136  Assert (new_lines[i]->used() == false,
6137  ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
6138  new_lines[i]->set_used_flag();
6139  new_lines[i]->clear_user_flag();
6140  new_lines[i]->clear_user_data();
6141  new_lines[i]->clear_children();
6142  // interior line
6143  new_lines[i]->set_boundary_id_internal(numbers::internal_face_boundary_id);
6144  // they inherit geometry description of the hex they belong to
6145  new_lines[i]->set_manifold_id(hex->manifold_id());
6146  }
6147 
6148  // find some space for the newly to be created
6149  // interior quads and initialize them.
6150  std::vector<typename Triangulation<dim,spacedim>::raw_quad_iterator>
6151  new_quads(n_new_quads);
6152  for (unsigned int i=0; i<n_new_quads; ++i)
6153  {
6154  new_quads[i] = triangulation.faces->quads.next_free_single_object(triangulation);
6155 
6156  Assert (new_quads[i]->used() == false,
6157  ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
6158  new_quads[i]->set_used_flag();
6159  new_quads[i]->clear_user_flag();
6160  new_quads[i]->clear_user_data();
6161  new_quads[i]->clear_children();
6162  // interior quad
6163  new_quads[i]->set_boundary_id_internal(numbers::internal_face_boundary_id);
6164  // they inherit geometry description of the hex they belong to
6165  new_quads[i]->set_manifold_id (hex->manifold_id());
6166  // set all line orientation flags to true by
6167  // default, change this afterwards, if necessary
6168  for (unsigned int j=0; j<GeometryInfo<dim>::lines_per_face; ++j)
6169  new_quads[i]->set_line_orientation(j,true);
6170  }
6171 
6172  types::subdomain_id subdomainid = hex->subdomain_id();
6173 
6174  // find some space for the newly to be created hexes
6175  // and initialize them.
6176  std::vector<typename Triangulation<dim,spacedim>::raw_hex_iterator>
6177  new_hexes(n_new_hexes);
6178  for (unsigned int i=0; i<n_new_hexes; ++i)
6179  {
6180  if (i%2==0)
6181  next_unused_hex=triangulation.levels[level+1]->cells.next_free_hex(triangulation,level+1);
6182  else
6183  ++next_unused_hex;
6184 
6185  new_hexes[i]=next_unused_hex;
6186 
6187  Assert (new_hexes[i]->used() == false,
6188  ExcMessage("Internal error: We want to use a cell during refinement that should be unused, but turns out not to be."));
6189  new_hexes[i]->set_used_flag();
6190  new_hexes[i]->clear_user_flag();
6191  new_hexes[i]->clear_user_data();
6192  new_hexes[i]->clear_children();
6193  // inherit material
6194  // properties
6195  new_hexes[i]->set_material_id (hex->material_id());
6196  new_hexes[i]->set_manifold_id (hex->manifold_id());
6197  new_hexes[i]->set_subdomain_id (subdomainid);
6198 
6199  if (i%2)
6200  new_hexes[i]->set_parent (hex->index ());
6201  // set the face_orientation flag to true for all
6202  // faces initially, as this is the default value
6203  // which is true for all faces interior to the
6204  // hex. later on go the other way round and
6205  // reset faces that are at the boundary of the
6206  // mother cube
6207  //
6208  // the same is true for the face_flip and
6209  // face_rotation flags. however, the latter two
6210  // are set to false by default as this is the
6211  // standard value
6212  for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
6213  {
6214  new_hexes[i]->set_face_orientation(f, true);
6215  new_hexes[i]->set_face_flip(f, false);
6216  new_hexes[i]->set_face_rotation(f, false);
6217  }
6218  }
6219  // note these hexes as children to the present cell
6220  for (unsigned int i=0; i<n_new_hexes/2; ++i)
6221  hex->set_children (2*i, new_hexes[2*i]->index());
6222 
6223  // we have to take into account whether the
6224  // different faces are oriented correctly or in the
6225  // opposite direction, so store that up front
6226 
6227  // face_orientation
6228  const bool f_or[6]
6229  = { hex->face_orientation (0),
6230  hex->face_orientation (1),
6231  hex->face_orientation (2),
6232  hex->face_orientation (3),
6233  hex->face_orientation (4),
6234  hex->face_orientation (5)
6235  };
6236 
6237  // face_flip
6238  const bool f_fl[6]
6239  = { hex->face_flip (0),
6240  hex->face_flip (1),
6241  hex->face_flip (2),
6242  hex->face_flip (3),
6243  hex->face_flip (4),
6244  hex->face_flip (5)
6245  };
6246 
6247  // face_rotation
6248  const bool f_ro[6]
6249  = { hex->face_rotation (0),
6250  hex->face_rotation (1),
6251  hex->face_rotation (2),
6252  hex->face_rotation (3),
6253  hex->face_rotation (4),
6254  hex->face_rotation (5)
6255  };
6256 
6257  // little helper table, indicating, whether the
6258  // child with index 0 or with index 1 can be found
6259  // at the standard origin of an anisotropically
6260  // refined quads in real orientation index 1:
6261  // (RefineCase - 1) index 2: face_flip
6262 
6263  // index 3: face rotation
6264  // note: face orientation has no influence
6265  const unsigned int child_at_origin[2][2][2]=
6266  {
6267  { { 0, 0 }, // RefinementCase<dim>::cut_x, face_flip=false, face_rotation=false and true
6268  { 1, 1 }
6269  }, // RefinementCase<dim>::cut_x, face_flip=true, face_rotation=false and true
6270  { { 0, 1 }, // RefinementCase<dim>::cut_y, face_flip=false, face_rotation=false and true
6271  { 1, 0 }
6272  }
6273  };// RefinementCase<dim>::cut_y, face_flip=true, face_rotation=false and true
6274 
6276  //
6277  // in the following we will do the same thing for
6278  // each refinement case: create a new vertex (if
6279  // needed), create new interior lines (if needed),
6280  // create new interior quads and afterwards build
6281  // the children hexes out of these and the existing
6282  // subfaces of the outer quads (which have been
6283  // created above). However, even if the steps are
6284  // quite similar, the actual work strongly depends
6285  // on the actual refinement case. therefore, we use
6286  // separate blocks of code for each of these cases,
6287  // which hopefully increases the readability to some
6288  // extend.
6289 
6290  switch (ref_case)
6291  {
6293  {
6294  const typename Triangulation<dim,spacedim>::raw_line_iterator
6295  *lines = NULL;
6296  const unsigned int *line_indices = NULL;
6297  const bool *line_orientation = NULL;
6298  const int *quad_indices = NULL;
6299 
6301  //
6302  // RefinementCase<dim>::cut_x
6303  //
6304  // the refined cube will look
6305  // like this:
6306  //
6307  // *----*----*
6308  // / / /|
6309  // / / / |
6310  // / / / |
6311  // *----*----* |
6312  // | | | |
6313  // | | | *
6314  // | | | /
6315  // | | | /
6316  // | | |/
6317  // *----*----*
6318  //
6319  // again, first collect some data about the
6320  // indices of the lines, with the following
6321  // numbering:
6322 
6323  // face 2: front plane
6324  // (note: x,y exchanged)
6325  // *---*---*
6326  // | | |
6327  // | 0 |
6328  // | | |
6329  // *---*---*
6330  // m0
6331  // face 3: back plane
6332  // (note: x,y exchanged)
6333  // m1
6334  // *---*---*
6335  // | | |
6336  // | 1 |
6337  // | | |
6338  // *---*---*
6339  // face 4: bottom plane
6340  // *---*---*
6341  // / / /
6342  // / 2 /
6343  // / / /
6344  // *---*---*
6345  // m0
6346  // face 5: top plane
6347  // m1
6348  // *---*---*
6349  // / / /
6350  // / 3 /
6351  // / / /
6352  // *---*---*
6353 
6354  // set up a list of line iterators first. from
6355  // this, construct lists of line_indices and
6356  // line orientations later on
6357  const typename Triangulation<dim,spacedim>::raw_line_iterator
6358  lines_x[4]
6359  =
6360  {
6361  hex->face(2)->child(0)
6362  ->line((hex->face(2)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //0
6363  hex->face(3)->child(0)
6364  ->line((hex->face(3)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //1
6365  hex->face(4)->child(0)
6366  ->line((hex->face(4)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //2
6367  hex->face(5)->child(0)
6368  ->line((hex->face(5)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3) //3
6369  };
6370 
6371  lines = &lines_x[0];
6372 
6373  unsigned int line_indices_x[4];
6374 
6375  for (unsigned int i=0; i<4; ++i)
6376  line_indices_x[i] = lines[i]->index();
6377  line_indices = &line_indices_x[0];
6378 
6379  // the orientation of lines for the inner quads
6380  // is quite tricky. as these lines are newly
6381  // created ones and thus have no parents, they
6382  // cannot inherit this property. set up an array
6383  // and fill it with the respective values
6384  bool line_orientation_x[4];
6385 
6386  // the middle vertice marked as m0 above is the
6387  // start vertex for lines 0 and 2 in standard
6388  // orientation, whereas m1 is the end vertex of
6389  // lines 1 and 3 in standard orientation
6390  const unsigned int middle_vertices[2]=
6391  {
6392  hex->line(2)->child(0)->vertex_index(1),
6393  hex->line(7)->child(0)->vertex_index(1)
6394  };
6395 
6396  for (unsigned int i=0; i<4; ++i)
6397  if (lines[i]->vertex_index(i%2)==middle_vertices[i%2])
6398  line_orientation_x[i]=true;
6399  else
6400  {
6401  // it must be the other
6402  // way round then
6403  Assert(lines[i]->vertex_index((i+1)%2)==middle_vertices[i%2],
6404  ExcInternalError());
6405  line_orientation_x[i]=false;
6406  }
6407 
6408  line_orientation=&line_orientation_x[0];
6409 
6410  // set up the new quad, line numbering is as
6411  // indicated above
6412  new_quads[0]->set (internal::Triangulation
6413  ::TriaObject<2>(line_indices[0],
6414  line_indices[1],
6415  line_indices[2],
6416  line_indices[3]));
6417 
6418  new_quads[0]->set_line_orientation(0,line_orientation[0]);
6419  new_quads[0]->set_line_orientation(1,line_orientation[1]);
6420  new_quads[0]->set_line_orientation(2,line_orientation[2]);
6421  new_quads[0]->set_line_orientation(3,line_orientation[3]);
6422 
6423  // the quads are numbered as follows:
6424  //
6425  // planes in the interior of the old hex:
6426  //
6427  // *
6428  // /|
6429  // / | x
6430  // / | *-------* *---------*
6431  // * | | | / /
6432  // | 0 | | | / /
6433  // | * | | / /
6434  // | / *-------*y *---------*x
6435  // | /
6436  // |/
6437  // *
6438  //
6439  // children of the faces of the old hex
6440  //
6441  // *---*---* *---*---*
6442  // /| | | / / /|
6443  // / | | | / 9 / 10/ |
6444  // / | 5 | 6 | / / / |
6445  // * | | | *---*---* |
6446  // | 1 *---*---* | | | 2 *
6447  // | / / / | | | /
6448  // | / 7 / 8 / | 3 | 4 | /
6449  // |/ / / | | |/
6450  // *---*---* *---*---*
6451  //
6452  // note that we have to take care of the
6453  // orientation of faces.
6454  const int quad_indices_x[11]
6455  =
6456  {
6457  new_quads[0]->index(), //0
6458 
6459  hex->face(0)->index(), //1
6460 
6461  hex->face(1)->index(), //2
6462 
6463  hex->face(2)->child_index( child_at_origin[hex->face(2)->refinement_case()-1][f_fl[2]][f_ro[2]]), //3
6464  hex->face(2)->child_index(1-child_at_origin[hex->face(2)->refinement_case()-1][f_fl[2]][f_ro[2]]),
6465 
6466  hex->face(3)->child_index( child_at_origin[hex->face(3)->refinement_case()-1][f_fl[3]][f_ro[3]]), //5
6467  hex->face(3)->child_index(1-child_at_origin[hex->face(3)->refinement_case()-1][f_fl[3]][f_ro[3]]),
6468 
6469  hex->face(4)->child_index( child_at_origin[hex->face(4)->refinement_case()-1][f_fl[4]][f_ro[4]]), //7
6470  hex->face(4)->child_index(1-child_at_origin[hex->face(4)->refinement_case()-1][f_fl[4]][f_ro[4]]),
6471 
6472  hex->face(5)->child_index( child_at_origin[hex->face(5)->refinement_case()-1][f_fl[5]][f_ro[5]]), //9
6473  hex->face(5)->child_index(1-child_at_origin[hex->face(5)->refinement_case()-1][f_fl[5]][f_ro[5]])
6474 
6475  };
6476  quad_indices = &quad_indices_x[0];
6477 
6478  new_hexes[0]->set (internal::Triangulation
6479  ::TriaObject<3>(quad_indices[1],
6480  quad_indices[0],
6481  quad_indices[3],
6482  quad_indices[5],
6483  quad_indices[7],
6484  quad_indices[9]));
6485  new_hexes[1]->set (internal::Triangulation
6486  ::TriaObject<3>(quad_indices[0],
6487  quad_indices[2],
6488  quad_indices[4],
6489  quad_indices[6],
6490  quad_indices[8],
6491  quad_indices[10]));
6492  break;
6493  }
6494 
6496  {
6497  const typename Triangulation<dim,spacedim>::raw_line_iterator
6498  *lines = NULL;
6499  const unsigned int *line_indices = NULL;
6500  const bool *line_orientation = NULL;
6501  const int *quad_indices = NULL;
6502 
6504  //
6505  // RefinementCase<dim>::cut_y
6506  //
6507  // the refined cube will look like this:
6508  //
6509  // *---------*
6510  // / /|
6511  // *---------* |
6512  // / /| |
6513  // *---------* | |
6514  // | | | |
6515  // | | | *
6516  // | | |/
6517  // | | *
6518  // | |/
6519  // *---------*
6520  //
6521  // again, first collect some data about the
6522  // indices of the lines, with the following
6523  // numbering:
6524 
6525  // face 0: left plane
6526  // *
6527  // /|
6528  // * |
6529  // /| |
6530  // * | |
6531  // | 0 |
6532  // | | *
6533  // | |/
6534  // | *m0
6535  // |/
6536  // *
6537  // face 1: right plane
6538  // *
6539  // /|
6540  // m1* |
6541  // /| |
6542  // * | |
6543  // | 1 |
6544  // | | *
6545  // | |/
6546  // | *
6547  // |/
6548  // *
6549  // face 4: bottom plane
6550  // *-------*
6551  // / /
6552  // m0*---2---*
6553  // / /
6554  // *-------*
6555  // face 5: top plane
6556  // *-------*
6557  // / /
6558  // *---3---*m1
6559  // / /
6560  // *-------*
6561 
6562  // set up a list of line iterators first. from
6563  // this, construct lists of line_indices and
6564  // line orientations later on
6565  const typename Triangulation<dim,spacedim>::raw_line_iterator
6566  lines_y[4]
6567  =
6568  {
6569  hex->face(0)->child(0)
6570  ->line((hex->face(0)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //0
6571  hex->face(1)->child(0)
6572  ->line((hex->face(1)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //1
6573  hex->face(4)->child(0)
6574  ->line((hex->face(4)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //2
6575  hex->face(5)->child(0)
6576  ->line((hex->face(5)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3) //3
6577  };
6578 
6579  lines=&lines_y[0];
6580 
6581  unsigned int line_indices_y[4];
6582 
6583  for (unsigned int i=0; i<4; ++i)
6584  line_indices_y[i]=lines[i]->index();
6585  line_indices=&line_indices_y[0];
6586 
6587  // the orientation of lines for the inner quads
6588  // is quite tricky. as these lines are newly
6589  // created ones and thus have no parents, they
6590  // cannot inherit this property. set up an array
6591  // and fill it with the respective values
6592  bool line_orientation_y[4];
6593 
6594  // the middle vertice marked as m0 above is the
6595  // start vertex for lines 0 and 2 in standard
6596  // orientation, whereas m1 is the end vertex of
6597  // lines 1 and 3 in standard orientation
6598  const unsigned int middle_vertices[2]=
6599  {
6600  hex->line(0)->child(0)->vertex_index(1),
6601  hex->line(5)->child(0)->vertex_index(1)
6602  };
6603 
6604  for (unsigned int i=0; i<4; ++i)
6605  if (lines[i]->vertex_index(i%2)==middle_vertices[i%2])
6606  line_orientation_y[i]=true;
6607  else
6608  {
6609  // it must be the other way round then
6610  Assert(lines[i]->vertex_index((i+1)%2)==middle_vertices[i%2],
6611  ExcInternalError());
6612  line_orientation_y[i]=false;
6613  }
6614 
6615  line_orientation=&line_orientation_y[0];
6616 
6617  // set up the new quad, line numbering is as
6618  // indicated above
6619  new_quads[0]->set (internal::Triangulation
6620  ::TriaObject<2>(line_indices[2],
6621  line_indices[3],
6622  line_indices[0],
6623  line_indices[1]));
6624 
6625  new_quads[0]->set_line_orientation(0,line_orientation[2]);
6626  new_quads[0]->set_line_orientation(1,line_orientation[3]);
6627  new_quads[0]->set_line_orientation(2,line_orientation[0]);
6628  new_quads[0]->set_line_orientation(3,line_orientation[1]);
6629 
6630  // the quads are numbered as follows:
6631  //
6632  // planes in the interior of the old hex:
6633  //
6634  // *
6635  // /|
6636  // / | x
6637  // / | *-------* *---------*
6638  // * | | | / /
6639  // | | | 0 | / /
6640  // | * | | / /
6641  // | / *-------*y *---------*x
6642  // | /
6643  // |/
6644  // *
6645  //
6646  // children of the faces of the old hex
6647  //
6648  // *-------* *-------*
6649  // /| | / 10 /|
6650  // * | | *-------* |
6651  // /| | 6 | / 9 /| |
6652  // * |2| | *-------* |4|
6653  // | | *-------* | | | *
6654  // |1|/ 8 / | |3|/
6655  // | *-------* | 5 | *
6656  // |/ 7 / | |/
6657  // *-------* *-------*
6658  //
6659  // note that we have to take care of the
6660  // orientation of faces.
6661  const int quad_indices_y[11]
6662  =
6663  {
6664  new_quads[0]->index(), //0
6665 
6666  hex->face(0)->child_index( child_at_origin[hex->face(0)->refinement_case()-1][f_fl[0]][f_ro[0]]), //1
6667  hex->face(0)->child_index(1-child_at_origin[hex->face(0)->refinement_case()-1][f_fl[0]][f_ro[0]]),
6668 
6669  hex->face(1)->child_index( child_at_origin[hex->face(1)->refinement_case()-1][f_fl[1]][f_ro[1]]), //3
6670  hex->face(1)->child_index(1-child_at_origin[hex->face(1)->refinement_case()-1][f_fl[1]][f_ro[1]]),
6671 
6672  hex->face(2)->index(), //5
6673 
6674  hex->face(3)->index(), //6
6675 
6676  hex->face(4)->child_index( child_at_origin[hex->face(4)->refinement_case()-1][f_fl[4]][f_ro[4]]), //7
6677  hex->face(4)->child_index(1-child_at_origin[hex->face(4)->refinement_case()-1][f_fl[4]][f_ro[4]]),
6678 
6679  hex->face(5)->child_index( child_at_origin[hex->face(5)->refinement_case()-1][f_fl[5]][f_ro[5]]), //9
6680  hex->face(5)->child_index(1-child_at_origin[hex->face(5)->refinement_case()-1][f_fl[5]][f_ro[5]])
6681 
6682  };
6683  quad_indices=&quad_indices_y[0];
6684 
6685  new_hexes[0]->set (internal::Triangulation
6686  ::TriaObject<3>(quad_indices[1],
6687  quad_indices[3],
6688  quad_indices[5],
6689  quad_indices[0],
6690  quad_indices[7],
6691  quad_indices[9]));
6692  new_hexes[1]->set (internal::Triangulation
6693  ::TriaObject<3>(quad_indices[2],
6694  quad_indices[4],
6695  quad_indices[0],
6696  quad_indices[6],
6697  quad_indices[8],
6698  quad_indices[10]));
6699  break;
6700  }
6701 
6703  {
6704  const typename Triangulation<dim,spacedim>::raw_line_iterator
6705  *lines = NULL;
6706  const unsigned int *line_indices = NULL;
6707  const bool *line_orientation = NULL;
6708  const int *quad_indices = NULL;
6709 
6711  //
6712  // RefinementCase<dim>::cut_z
6713  //
6714  // the refined cube will look like this:
6715  //
6716  // *---------*
6717  // / /|
6718  // / / |
6719  // / / *
6720  // *---------* /|
6721  // | | / |
6722  // | |/ *
6723  // *---------* /
6724  // | | /
6725  // | |/
6726  // *---------*
6727  //
6728  // again, first collect some data about the
6729  // indices of the lines, with the following
6730  // numbering:
6731 
6732  // face 0: left plane
6733  // *
6734  // /|
6735  // / |
6736  // / *
6737  // * /|
6738  // | 0 |
6739  // |/ *
6740  // m0* /
6741  // | /
6742  // |/
6743  // *
6744  // face 1: right plane
6745  // *
6746  // /|
6747  // / |
6748  // / *m1
6749  // * /|
6750  // | 1 |
6751  // |/ *
6752  // * /
6753  // | /
6754  // |/
6755  // *
6756  // face 2: front plane
6757  // (note: x,y exchanged)
6758  // *-------*
6759  // | |
6760  // m0*---2---*
6761  // | |
6762  // *-------*
6763  // face 3: back plane
6764  // (note: x,y exchanged)
6765  // *-------*
6766  // | |
6767  // *---3---*m1
6768  // | |
6769  // *-------*
6770 
6771  // set up a list of line iterators first. from
6772  // this, construct lists of line_indices and
6773  // line orientations later on
6774  const typename Triangulation<dim,spacedim>::raw_line_iterator
6775  lines_z[4]
6776  =
6777  {
6778  hex->face(0)->child(0)
6779  ->line((hex->face(0)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //0
6780  hex->face(1)->child(0)
6781  ->line((hex->face(1)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //1
6782  hex->face(2)->child(0)
6783  ->line((hex->face(2)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //2
6784  hex->face(3)->child(0)
6785  ->line((hex->face(3)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3) //3
6786  };
6787 
6788  lines=&lines_z[0];
6789 
6790  unsigned int line_indices_z[4];
6791 
6792  for (unsigned int i=0; i<4; ++i)
6793  line_indices_z[i]=lines[i]->index();
6794  line_indices=&line_indices_z[0];
6795 
6796  // the orientation of lines for the inner quads
6797  // is quite tricky. as these lines are newly
6798  // created ones and thus have no parents, they
6799  // cannot inherit this property. set up an array
6800  // and fill it with the respective values
6801  bool line_orientation_z[4];
6802 
6803  // the middle vertex marked as m0 above is the
6804  // start vertex for lines 0 and 2 in standard
6805  // orientation, whereas m1 is the end vertex of
6806  // lines 1 and 3 in standard orientation
6807  const unsigned int middle_vertices[2]=
6808  {
6809  middle_vertex_index<dim,spacedim>(hex->line(8)),
6810  middle_vertex_index<dim,spacedim>(hex->line(11))
6811  };
6812 
6813  for (unsigned int i=0; i<4; ++i)
6814  if (lines[i]->vertex_index(i%2)==middle_vertices[i%2])
6815  line_orientation_z[i]=true;
6816  else
6817  {
6818  // it must be the other way round then
6819  Assert(lines[i]->vertex_index((i+1)%2)==middle_vertices[i%2],
6820  ExcInternalError());
6821  line_orientation_z[i]=false;
6822  }
6823 
6824  line_orientation=&line_orientation_z[0];
6825 
6826  // set up the new quad, line numbering is as
6827  // indicated above
6828  new_quads[0]->set (internal::Triangulation
6829  ::TriaObject<2>(line_indices[0],
6830  line_indices[1],
6831  line_indices[2],
6832  line_indices[3]));
6833 
6834  new_quads[0]->set_line_orientation(0,line_orientation[0]);
6835  new_quads[0]->set_line_orientation(1,line_orientation[1]);
6836  new_quads[0]->set_line_orientation(2,line_orientation[2]);
6837  new_quads[0]->set_line_orientation(3,line_orientation[3]);
6838 
6839  // the quads are numbered as follows:
6840  //
6841  // planes in the interior of the old hex:
6842  //
6843  // *
6844  // /|
6845  // / | x
6846  // / | *-------* *---------*
6847  // * | | | / /
6848  // | | | | / 0 /
6849  // | * | | / /
6850  // | / *-------*y *---------*x
6851  // | /
6852  // |/
6853  // *
6854  //
6855  // children of the faces of the old hex
6856  //
6857  // *---*---* *-------*
6858  // /| 8 | / /|
6859  // / | | / 10 / |
6860  // / *-------* / / *
6861  // * 2/| | *-------* 4/|
6862  // | / | 7 | | 6 | / |
6863  // |/1 *-------* | |/3 *
6864  // * / / *-------* /
6865  // | / 9 / | | /
6866  // |/ / | 5 |/
6867  // *-------* *---*---*
6868  //
6869  // note that we have to take care of the
6870  // orientation of faces.
6871  const int quad_indices_z[11]
6872  =
6873  {
6874  new_quads[0]->index(), //0
6875 
6876  hex->face(0)->child_index( child_at_origin[hex->face(0)->refinement_case()-1][f_fl[0]][f_ro[0]]), //1
6877  hex->face(0)->child_index(1-child_at_origin[hex->face(0)->refinement_case()-1][f_fl[0]][f_ro[0]]),
6878 
6879  hex->face(1)->child_index( child_at_origin[hex->face(1)->refinement_case()-1][f_fl[1]][f_ro[1]]), //3
6880  hex->face(1)->child_index(1-child_at_origin[hex->face(1)->refinement_case()-1][f_fl[1]][f_ro[1]]),
6881 
6882  hex->face(2)->child_index( child_at_origin[hex->face(2)->refinement_case()-1][f_fl[2]][f_ro[2]]), //5
6883  hex->face(2)->child_index(1-child_at_origin[hex->face(2)->refinement_case()-1][f_fl[2]][f_ro[2]]),
6884 
6885  hex->face(3)->child_index( child_at_origin[hex->face(3)->refinement_case()-1][f_fl[3]][f_ro[3]]), //7
6886  hex->face(3)->child_index(1-child_at_origin[hex->face(3)->refinement_case()-1][f_fl[3]][f_ro[3]]),
6887 
6888  hex->face(4)->index(), //9
6889 
6890  hex->face(5)->index() //10
6891  };
6892  quad_indices=&quad_indices_z[0];
6893 
6894  new_hexes[0]->set (internal::Triangulation
6895  ::TriaObject<3>(quad_indices[1],
6896  quad_indices[3],
6897  quad_indices[5],
6898  quad_indices[7],
6899  quad_indices[9],
6900  quad_indices[0]));
6901  new_hexes[1]->set (internal::Triangulation
6902  ::TriaObject<3>(quad_indices[2],
6903  quad_indices[4],
6904  quad_indices[6],
6905  quad_indices[8],
6906  quad_indices[0],
6907  quad_indices[10]));
6908  break;
6909  }
6910 
6912  {
6913  const typename Triangulation<dim,spacedim>::raw_line_iterator
6914  *lines = NULL;
6915  const unsigned int *line_indices = NULL;
6916  const bool *line_orientation = NULL;
6917  const int *quad_indices = NULL;
6918 
6920  //
6921  // RefinementCase<dim>::cut_xy
6922  //
6923  // the refined cube will look like this:
6924  //
6925  // *----*----*
6926  // / / /|
6927  // *----*----* |
6928  // / / /| |
6929  // *----*----* | |
6930  // | | | | |
6931  // | | | | *
6932  // | | | |/
6933  // | | | *
6934  // | | |/
6935  // *----*----*
6936  //
6937 
6938  // first, create the new internal line
6939  new_lines[0]->set (internal::Triangulation::
6940  TriaObject<1>(middle_vertex_index<dim,spacedim>(hex->face(4)),
6941  middle_vertex_index<dim,spacedim>(hex->face(5))));
6942 
6943  // again, first collect some data about the
6944  // indices of the lines, with the following
6945  // numbering:
6946 
6947  // face 0: left plane
6948  // *
6949  // /|
6950  // * |
6951  // /| |
6952  // * | |
6953  // | 0 |
6954  // | | *
6955  // | |/
6956  // | *
6957  // |/
6958  // *
6959  // face 1: right plane
6960  // *
6961  // /|
6962  // * |
6963  // /| |
6964  // * | |
6965  // | 1 |
6966  // | | *
6967  // | |/
6968  // | *
6969  // |/
6970  // *
6971  // face 2: front plane
6972  // (note: x,y exchanged)
6973  // *---*---*
6974  // | | |
6975  // | 2 |
6976  // | | |
6977  // *-------*
6978  // face 3: back plane
6979  // (note: x,y exchanged)
6980  // *---*---*
6981  // | | |
6982  // | 3 |
6983  // | | |
6984  // *---*---*
6985  // face 4: bottom plane
6986  // *---*---*
6987  // / 5 /
6988  // *-6-*-7-*
6989  // / 4 /
6990  // *---*---*
6991  // face 5: top plane
6992  // *---*---*
6993  // / 9 /
6994  // *10-*-11*
6995  // / 8 /
6996  // *---*---*
6997  // middle planes
6998  // *-------* *---*---*
6999  // / / | | |
7000  // / / | 12 |
7001  // / / | | |
7002  // *-------* *---*---*
7003 
7004  // set up a list of line iterators first. from
7005  // this, construct lists of line_indices and
7006  // line orientations later on
7007  const typename Triangulation<dim,spacedim>::raw_line_iterator
7008  lines_xy[13]
7009  =
7010  {
7011  hex->face(0)->child(0)
7012  ->line((hex->face(0)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //0
7013  hex->face(1)->child(0)
7014  ->line((hex->face(1)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //1
7015  hex->face(2)->child(0)
7016  ->line((hex->face(2)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //2
7017  hex->face(3)->child(0)
7018  ->line((hex->face(3)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //3
7019 
7020  hex->face(4)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[4],f_fl[4],f_ro[4]))
7021  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[4],f_fl[4],f_ro[4])), //4
7022  hex->face(4)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[4],f_fl[4],f_ro[4]))
7023  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[4],f_fl[4],f_ro[4])), //5
7024  hex->face(4)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[4],f_fl[4],f_ro[4]))
7025  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[4],f_fl[4],f_ro[4])), //6
7026  hex->face(4)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[4],f_fl[4],f_ro[4]))
7027  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[4],f_fl[4],f_ro[4])), //7
7028 
7029  hex->face(5)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[5],f_fl[5],f_ro[5]))
7030  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[5],f_fl[5],f_ro[5])), //8
7031  hex->face(5)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[5],f_fl[5],f_ro[5]))
7032  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[5],f_fl[5],f_ro[5])), //9
7033  hex->face(5)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[5],f_fl[5],f_ro[5]))
7034  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[5],f_fl[5],f_ro[5])), //10
7035  hex->face(5)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[5],f_fl[5],f_ro[5]))
7036  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[5],f_fl[5],f_ro[5])), //11
7037 
7038  new_lines[0] //12
7039  };
7040 
7041  lines=&lines_xy[0];
7042 
7043  unsigned int line_indices_xy[13];
7044 
7045  for (unsigned int i=0; i<13; ++i)
7046  line_indices_xy[i]=lines[i]->index();
7047  line_indices=&line_indices_xy[0];
7048 
7049  // the orientation of lines for the inner quads
7050  // is quite tricky. as these lines are newly
7051  // created ones and thus have no parents, they
7052  // cannot inherit this property. set up an array
7053  // and fill it with the respective values
7054  bool line_orientation_xy[13];
7055 
7056  // the middle vertices of the lines of our
7057  // bottom face
7058  const unsigned int middle_vertices[4]=
7059  {
7060  hex->line(0)->child(0)->vertex_index(1),
7061  hex->line(1)->child(0)->vertex_index(1),
7062  hex->line(2)->child(0)->vertex_index(1),
7063  hex->line(3)->child(0)->vertex_index(1),
7064  };
7065 
7066  // note: for lines 0 to 3 the orientation of the
7067  // line is 'true', if vertex 0 is on the bottom
7068  // face
7069  for (unsigned int i=0; i<4; ++i)
7070  if (lines[i]->vertex_index(0)==middle_vertices[i])
7071  line_orientation_xy[i]=true;
7072  else
7073  {
7074  // it must be the other way round then
7075  Assert(lines[i]->vertex_index(1)==middle_vertices[i],
7076  ExcInternalError());
7077  line_orientation_xy[i]=false;
7078  }
7079 
7080  // note: for lines 4 to 11 (inner lines of the
7081  // outer quads) the following holds: the second
7082  // vertex of the even lines in standard
7083  // orientation is the vertex in the middle of
7084  // the quad, whereas for odd lines the first
7085  // vertex is the same middle vertex.
7086  for (unsigned int i=4; i<12; ++i)
7087  if (lines[i]->vertex_index((i+1)%2) ==
7088  middle_vertex_index<dim,spacedim>(hex->face(3+i/4)))
7089  line_orientation_xy[i]=true;
7090  else
7091  {
7092  // it must be the other way
7093  // round then
7094  Assert(lines[i]->vertex_index(i%2) ==
7095  (middle_vertex_index<dim,spacedim>(hex->face(3+i/4))),
7096  ExcInternalError());
7097  line_orientation_xy[i]=false;
7098  }
7099  // for the last line the line orientation is
7100  // always true, since it was just constructed
7101  // that way
7102 
7103  line_orientation_xy[12]=true;
7104  line_orientation=&line_orientation_xy[0];
7105 
7106  // set up the 4 quads, numbered as follows (left
7107  // quad numbering, right line numbering
7108  // extracted from above)
7109  //
7110  // * *
7111  // /| 9|
7112  // * | * |
7113  // y/| | 8| 3
7114  // * |1| * | |
7115  // | | |x | 12|
7116  // |0| * | | *
7117  // | |/ 2 |5
7118  // | * | *
7119  // |/ |4
7120  // * *
7121  //
7122  // x
7123  // *---*---* *10-*-11*
7124  // | | | | | |
7125  // | 2 | 3 | 0 12 1
7126  // | | | | | |
7127  // *---*---*y *-6-*-7-*
7128 
7129  new_quads[0]->set (internal::Triangulation
7130  ::TriaObject<2>(line_indices[2],
7131  line_indices[12],
7132  line_indices[4],
7133  line_indices[8]));
7134  new_quads[1]->set (internal::Triangulation
7135  ::TriaObject<2>(line_indices[12],
7136  line_indices[3],
7137  line_indices[5],
7138  line_indices[9]));
7139  new_quads[2]->set (internal::Triangulation
7140  ::TriaObject<2>(line_indices[6],
7141  line_indices[10],
7142  line_indices[0],
7143  line_indices[12]));
7144  new_quads[3]->set (internal::Triangulation
7145  ::TriaObject<2>(line_indices[7],
7146  line_indices[11],
7147  line_indices[12],
7148  line_indices[1]));
7149 
7150  new_quads[0]->set_line_orientation(0,line_orientation[2]);
7151  new_quads[0]->set_line_orientation(2,line_orientation[4]);
7152  new_quads[0]->set_line_orientation(3,line_orientation[8]);
7153 
7154  new_quads[1]->set_line_orientation(1,line_orientation[3]);
7155  new_quads[1]->set_line_orientation(2,line_orientation[5]);
7156  new_quads[1]->set_line_orientation(3,line_orientation[9]);
7157 
7158  new_quads[2]->set_line_orientation(0,line_orientation[6]);
7159  new_quads[2]->set_line_orientation(1,line_orientation[10]);
7160  new_quads[2]->set_line_orientation(2,line_orientation[0]);
7161 
7162  new_quads[3]->set_line_orientation(0,line_orientation[7]);
7163  new_quads[3]->set_line_orientation(1,line_orientation[11]);
7164  new_quads[3]->set_line_orientation(3,line_orientation[1]);
7165 
7166  // the quads are numbered as follows:
7167  //
7168  // planes in the interior of the old hex:
7169  //
7170  // *
7171  // /|
7172  // * | x
7173  // /| | *---*---* *---------*
7174  // * |1| | | | / /
7175  // | | | | 2 | 3 | / /
7176  // |0| * | | | / /
7177  // | |/ *---*---*y *---------*x
7178  // | *
7179  // |/
7180  // *
7181  //
7182  // children of the faces of the old hex
7183  //
7184  // *---*---* *---*---*
7185  // /| | | /18 / 19/|
7186  // * |10 | 11| /---/---* |
7187  // /| | | | /16 / 17/| |
7188  // * |5| | | *---*---* |7|
7189  // | | *---*---* | | | | *
7190  // |4|/14 / 15/ | | |6|/
7191  // | *---/---/ | 8 | 9 | *
7192  // |/12 / 13/ | | |/
7193  // *---*---* *---*---*
7194  //
7195  // note that we have to take care of the
7196  // orientation of faces.
7197  const int quad_indices_xy[20]
7198  =
7199  {
7200  new_quads[0]->index(), //0
7201  new_quads[1]->index(),
7202  new_quads[2]->index(),
7203  new_quads[3]->index(),
7204 
7205  hex->face(0)->child_index( child_at_origin[hex->face(0)->refinement_case()-1][f_fl[0]][f_ro[0]]), //4
7206  hex->face(0)->child_index(1-child_at_origin[hex->face(0)->refinement_case()-1][f_fl[0]][f_ro[0]]),
7207 
7208  hex->face(1)->child_index( child_at_origin[hex->face(1)->refinement_case()-1][f_fl[1]][f_ro[1]]), //6
7209  hex->face(1)->child_index(1-child_at_origin[hex->face(1)->refinement_case()-1][f_fl[1]][f_ro[1]]),
7210 
7211  hex->face(2)->child_index( child_at_origin[hex->face(2)->refinement_case()-1][f_fl[2]][f_ro[2]]), //8
7212  hex->face(2)->child_index(1-child_at_origin[hex->face(2)->refinement_case()-1][f_fl[2]][f_ro[2]]),
7213 
7214  hex->face(3)->child_index( child_at_origin[hex->face(3)->refinement_case()-1][f_fl[3]][f_ro[3]]), //10
7215  hex->face(3)->child_index(1-child_at_origin[hex->face(3)->refinement_case()-1][f_fl[3]][f_ro[3]]),
7216 
7217  hex->face(4)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[4],f_fl[4],f_ro[4])), //12
7218  hex->face(4)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[4],f_fl[4],f_ro[4])),
7219  hex->face(4)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[4],f_fl[4],f_ro[4])),
7220  hex->face(4)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[4],f_fl[4],f_ro[4])),
7221 
7222  hex->face(5)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[5],f_fl[5],f_ro[5])), //16
7223  hex->face(5)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[5],f_fl[5],f_ro[5])),
7224  hex->face(5)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[5],f_fl[5],f_ro[5])),
7225  hex->face(5)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[5],f_fl[5],f_ro[5]))
7226  };
7227  quad_indices=&quad_indices_xy[0];
7228 
7229  new_hexes[0]->set (internal::Triangulation
7230  ::TriaObject<3>(quad_indices[4],
7231  quad_indices[0],
7232  quad_indices[8],
7233  quad_indices[2],
7234  quad_indices[12],
7235  quad_indices[16]));
7236  new_hexes[1]->set (internal::Triangulation
7237  ::TriaObject<3>(quad_indices[0],
7238  quad_indices[6],
7239  quad_indices[9],
7240  quad_indices[3],
7241  quad_indices[13],
7242  quad_indices[17]));
7243  new_hexes[2]->set (internal::Triangulation
7244  ::TriaObject<3>(quad_indices[5],
7245  quad_indices[1],
7246  quad_indices[2],
7247  quad_indices[10],
7248  quad_indices[14],
7249  quad_indices[18]));
7250  new_hexes[3]->set (internal::Triangulation
7251  ::TriaObject<3>(quad_indices[1],
7252  quad_indices[7],
7253  quad_indices[3],
7254  quad_indices[11],
7255  quad_indices[15],
7256  quad_indices[19]));
7257  break;
7258  }
7259 
7261  {
7262  const typename Triangulation<dim,spacedim>::raw_line_iterator
7263  *lines = NULL;
7264  const unsigned int *line_indices = NULL;
7265  const bool *line_orientation = NULL;
7266  const int *quad_indices = NULL;
7267 
7269  //
7270  // RefinementCase<dim>::cut_xz
7271  //
7272  // the refined cube will look like this:
7273  //
7274  // *----*----*
7275  // / / /|
7276  // / / / |
7277  // / / / *
7278  // *----*----* /|
7279  // | | | / |
7280  // | | |/ *
7281  // *----*----* /
7282  // | | | /
7283  // | | |/
7284  // *----*----*
7285  //
7286 
7287  // first, create the new internal line
7288  new_lines[0]->set (internal::Triangulation::
7289  TriaObject<1>(middle_vertex_index<dim,spacedim>(hex->face(2)),
7290  middle_vertex_index<dim,spacedim>(hex->face(3))));
7291 
7292  // again, first collect some data about the
7293  // indices of the lines, with the following
7294  // numbering:
7295 
7296  // face 0: left plane
7297  // *
7298  // /|
7299  // / |
7300  // / *
7301  // * /|
7302  // | 0 |
7303  // |/ *
7304  // * /
7305  // | /
7306  // |/
7307  // *
7308  // face 1: right plane
7309  // *
7310  // /|
7311  // / |
7312  // / *
7313  // * /|
7314  // | 1 |
7315  // |/ *
7316  // * /
7317  // | /
7318  // |/
7319  // *
7320  // face 2: front plane
7321  // (note: x,y exchanged)
7322  // *---*---*
7323  // | 5 |
7324  // *-6-*-7-*
7325  // | 4 |
7326  // *---*---*
7327  // face 3: back plane
7328  // (note: x,y exchanged)
7329  // *---*---*
7330  // | 9 |
7331  // *10-*-11*
7332  // | 8 |
7333  // *---*---*
7334  // face 4: bottom plane
7335  // *---*---*
7336  // / / /
7337  // / 2 /
7338  // / / /
7339  // *---*---*
7340  // face 5: top plane
7341  // *---*---*
7342  // / / /
7343  // / 3 /
7344  // / / /
7345  // *---*---*
7346  // middle planes
7347  // *---*---* *-------*
7348  // / / / | |
7349  // / 12 / | |
7350  // / / / | |
7351  // *---*---* *-------*
7352 
7353  // set up a list of line iterators first. from
7354  // this, construct lists of line_indices and
7355  // line orientations later on
7356  const typename Triangulation<dim,spacedim>::raw_line_iterator
7357  lines_xz[13]
7358  =
7359  {
7360  hex->face(0)->child(0)
7361  ->line((hex->face(0)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //0
7362  hex->face(1)->child(0)
7363  ->line((hex->face(1)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //1
7364  hex->face(4)->child(0)
7365  ->line((hex->face(4)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //2
7366  hex->face(5)->child(0)
7367  ->line((hex->face(5)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //3
7368 
7369  hex->face(2)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[2],f_fl[2],f_ro[2]))
7370  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[2],f_fl[2],f_ro[2])), //4
7371  hex->face(2)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[2],f_fl[2],f_ro[2]))
7372  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[2],f_fl[2],f_ro[2])), //5
7373  hex->face(2)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[2],f_fl[2],f_ro[2]))
7374  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[2],f_fl[2],f_ro[2])), //6
7375  hex->face(2)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[2],f_fl[2],f_ro[2]))
7376  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[2],f_fl[2],f_ro[2])), //7
7377 
7378  hex->face(3)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[3],f_fl[3],f_ro[3]))
7379  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[3],f_fl[3],f_ro[3])), //8
7380  hex->face(3)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[3],f_fl[3],f_ro[3]))
7381  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[3],f_fl[3],f_ro[3])), //9
7382  hex->face(3)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[3],f_fl[3],f_ro[3]))
7383  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[3],f_fl[3],f_ro[3])), //10
7384  hex->face(3)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[3],f_fl[3],f_ro[3]))
7385  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[3],f_fl[3],f_ro[3])), //11
7386 
7387  new_lines[0] //12
7388  };
7389 
7390  lines=&lines_xz[0];
7391 
7392  unsigned int line_indices_xz[13];
7393 
7394  for (unsigned int i=0; i<13; ++i)
7395  line_indices_xz[i]=lines[i]->index();
7396  line_indices=&line_indices_xz[0];
7397 
7398  // the orientation of lines for the inner quads
7399  // is quite tricky. as these lines are newly
7400  // created ones and thus have no parents, they
7401  // cannot inherit this property. set up an array
7402  // and fill it with the respective values
7403  bool line_orientation_xz[13];
7404 
7405  // the middle vertices of the
7406  // lines of our front face
7407  const unsigned int middle_vertices[4]=
7408  {
7409  hex->line(8)->child(0)->vertex_index(1),
7410  hex->line(9)->child(0)->vertex_index(1),
7411  hex->line(2)->child(0)->vertex_index(1),
7412  hex->line(6)->child(0)->vertex_index(1),
7413  };
7414 
7415  // note: for lines 0 to 3 the orientation of the
7416  // line is 'true', if vertex 0 is on the front
7417  for (unsigned int i=0; i<4; ++i)
7418  if (lines[i]->vertex_index(0)==middle_vertices[i])
7419  line_orientation_xz[i]=true;
7420  else
7421  {
7422  // it must be the other way round then
7423  Assert(lines[i]->vertex_index(1)==middle_vertices[i],
7424  ExcInternalError());
7425  line_orientation_xz[i]=false;
7426  }
7427 
7428  // note: for lines 4 to 11 (inner lines of the
7429  // outer quads) the following holds: the second
7430  // vertex of the even lines in standard
7431  // orientation is the vertex in the middle of
7432  // the quad, whereas for odd lines the first
7433  // vertex is the same middle vertex.
7434  for (unsigned int i=4; i<12; ++i)
7435  if (lines[i]->vertex_index((i+1)%2) ==
7436  middle_vertex_index<dim,spacedim>(hex->face(1+i/4)))
7437  line_orientation_xz[i]=true;
7438  else
7439  {
7440  // it must be the other way
7441  // round then
7442  Assert(lines[i]->vertex_index(i%2) ==
7443  (middle_vertex_index<dim,spacedim>(hex->face(1+i/4))),
7444  ExcInternalError());
7445  line_orientation_xz[i]=false;
7446  }
7447  // for the last line the line orientation is
7448  // always true, since it was just constructed
7449  // that way
7450 
7451  line_orientation_xz[12]=true;
7452  line_orientation=&line_orientation_xz[0];
7453 
7454  // set up the 4 quads, numbered as follows (left
7455  // quad numbering, right line numbering
7456  // extracted from above), the drawings denote
7457  // middle planes
7458  //
7459  // * *
7460  // /| /|
7461  // / | 3 9
7462  // y/ * / *
7463  // * 3/| * /|
7464  // | / |x 5 12|8
7465  // |/ * |/ *
7466  // * 2/ * /
7467  // | / 4 2
7468  // |/ |/
7469  // * *
7470  //
7471  // y
7472  // *----*----* *-10-*-11-*
7473  // / / / / / /
7474  // / 0 / 1 / 0 12 1
7475  // / / / / / /
7476  // *----*----*x *--6-*--7-*
7477 
7478  new_quads[0]->set (internal::Triangulation
7479  ::TriaObject<2>(line_indices[0],
7480  line_indices[12],
7481  line_indices[6],
7482  line_indices[10]));
7483  new_quads[1]->set (internal::Triangulation
7484  ::TriaObject<2>(line_indices[12],
7485  line_indices[1],
7486  line_indices[7],
7487  line_indices[11]));
7488  new_quads[2]->set (internal::Triangulation
7489  ::TriaObject<2>(line_indices[4],
7490  line_indices[8],
7491  line_indices[2],
7492  line_indices[12]));
7493  new_quads[3]->set (internal::Triangulation
7494  ::TriaObject<2>(line_indices[5],
7495  line_indices[9],
7496  line_indices[12],
7497  line_indices[3]));
7498 
7499  new_quads[0]->set_line_orientation(0,line_orientation[0]);
7500  new_quads[0]->set_line_orientation(2,line_orientation[6]);
7501  new_quads[0]->set_line_orientation(3,line_orientation[10]);
7502 
7503  new_quads[1]->set_line_orientation(1,line_orientation[1]);
7504  new_quads[1]->set_line_orientation(2,line_orientation[7]);
7505  new_quads[1]->set_line_orientation(3,line_orientation[11]);
7506 
7507  new_quads[2]->set_line_orientation(0,line_orientation[4]);
7508  new_quads[2]->set_line_orientation(1,line_orientation[8]);
7509  new_quads[2]->set_line_orientation(2,line_orientation[2]);
7510 
7511  new_quads[3]->set_line_orientation(0,line_orientation[5]);
7512  new_quads[3]->set_line_orientation(1,line_orientation[9]);
7513  new_quads[3]->set_line_orientation(3,line_orientation[3]);
7514 
7515  // the quads are numbered as follows:
7516  //
7517  // planes in the interior of the old hex:
7518  //
7519  // *
7520  // /|
7521  // / | x
7522  // /3 * *-------* *----*----*
7523  // * /| | | / / /
7524  // | / | | | / 0 / 1 /
7525  // |/ * | | / / /
7526  // * 2/ *-------*y *----*----*x
7527  // | /
7528  // |/
7529  // *
7530  //
7531  // children of the faces
7532  // of the old hex
7533  // *---*---* *---*---*
7534  // /|13 | 15| / / /|
7535  // / | | | /18 / 19/ |
7536  // / *---*---* / / / *
7537  // * 5/| | | *---*---* 7/|
7538  // | / |12 | 14| | 9 | 11| / |
7539  // |/4 *---*---* | | |/6 *
7540  // * / / / *---*---* /
7541  // | /16 / 17/ | | | /
7542  // |/ / / | 8 | 10|/
7543  // *---*---* *---*---*
7544  //
7545  // note that we have to take care of the
7546  // orientation of faces.
7547  const int quad_indices_xz[20]
7548  =
7549  {
7550  new_quads[0]->index(), //0
7551  new_quads[1]->index(),
7552  new_quads[2]->index(),
7553  new_quads[3]->index(),
7554 
7555  hex->face(0)->child_index( child_at_origin[hex->face(0)->refinement_case()-1][f_fl[0]][f_ro[0]]), //4
7556  hex->face(0)->child_index(1-child_at_origin[hex->face(0)->refinement_case()-1][f_fl[0]][f_ro[0]]),
7557 
7558  hex->face(1)->child_index( child_at_origin[hex->face(1)->refinement_case()-1][f_fl[1]][f_ro[1]]), //6
7559  hex->face(1)->child_index(1-child_at_origin[hex->face(1)->refinement_case()-1][f_fl[1]][f_ro[1]]),
7560 
7561  hex->face(2)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[2],f_fl[2],f_ro[2])), //8
7562  hex->face(2)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[2],f_fl[2],f_ro[2])),
7563  hex->face(2)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[2],f_fl[2],f_ro[2])),
7564  hex->face(2)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[2],f_fl[2],f_ro[2])),
7565 
7566  hex->face(3)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[3],f_fl[3],f_ro[3])), //12
7567  hex->face(3)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[3],f_fl[3],f_ro[3])),
7568  hex->face(3)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[3],f_fl[3],f_ro[3])),
7569  hex->face(3)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[3],f_fl[3],f_ro[3])),
7570 
7571  hex->face(4)->child_index( child_at_origin[hex->face(4)->refinement_case()-1][f_fl[4]][f_ro[4]]), //16
7572  hex->face(4)->child_index(1-child_at_origin[hex->face(4)->refinement_case()-1][f_fl[4]][f_ro[4]]),
7573 
7574  hex->face(5)->child_index( child_at_origin[hex->face(5)->refinement_case()-1][f_fl[5]][f_ro[5]]), //18
7575  hex->face(5)->child_index(1-child_at_origin[hex->face(5)->refinement_case()-1][f_fl[5]][f_ro[5]])
7576  };
7577  quad_indices=&quad_indices_xz[0];
7578 
7579  // due to the exchange of x and y for the front
7580  // and back face, we order the children
7581  // according to
7582  //
7583  // *---*---*
7584  // | 1 | 3 |
7585  // *---*---*
7586  // | 0 | 2 |
7587  // *---*---*
7588  new_hexes[0]->set (internal::Triangulation
7589  ::TriaObject<3>(quad_indices[4],
7590  quad_indices[2],
7591  quad_indices[8],
7592  quad_indices[12],
7593  quad_indices[16],
7594  quad_indices[0]));
7595  new_hexes[1]->set (internal::Triangulation
7596  ::TriaObject<3>(quad_indices[5],
7597  quad_indices[3],
7598  quad_indices[9],
7599  quad_indices[13],
7600  quad_indices[0],
7601  quad_indices[18]));
7602  new_hexes[2]->set (internal::Triangulation
7603  ::TriaObject<3>(quad_indices[2],
7604  quad_indices[6],
7605  quad_indices[10],
7606  quad_indices[14],
7607  quad_indices[17],
7608  quad_indices[1]));
7609  new_hexes[3]->set (internal::Triangulation
7610  ::TriaObject<3>(quad_indices[3],
7611  quad_indices[7],
7612  quad_indices[11],
7613  quad_indices[15],
7614  quad_indices[1],
7615  quad_indices[19]));
7616  break;
7617  }
7618 
7620  {
7621  const typename Triangulation<dim,spacedim>::raw_line_iterator
7622  *lines = NULL;
7623  const unsigned int *line_indices = NULL;
7624  const bool *line_orientation = NULL;
7625  const int *quad_indices = NULL;
7626 
7628  //
7629  // RefinementCase<dim>::cut_yz
7630  //
7631  // the refined cube will look like this:
7632  //
7633  // *---------*
7634  // / /|
7635  // *---------* |
7636  // / /| |
7637  // *---------* |/|
7638  // | | * |
7639  // | |/| *
7640  // *---------* |/
7641  // | | *
7642  // | |/
7643  // *---------*
7644  //
7645 
7646  // first, create the new
7647  // internal line
7648  new_lines[0]->set (internal::Triangulation::
7649  TriaObject<1>(middle_vertex_index<dim,spacedim>(hex->face(0)),
7650  middle_vertex_index<dim,spacedim>(hex->face(1))));
7651 
7652  // again, first collect some data about the
7653  // indices of the lines, with the following
7654  // numbering: (note that face 0 and 1 each are
7655  // shown twice for better readability)
7656 
7657  // face 0: left plane
7658  // * *
7659  // /| /|
7660  // * | * |
7661  // /| * /| *
7662  // * 5/| * |7|
7663  // | * | | * |
7664  // |/| * |6| *
7665  // * 4/ * |/
7666  // | * | *
7667  // |/ |/
7668  // * *
7669  // face 1: right plane
7670  // * *
7671  // /| /|
7672  // * | * |
7673  // /| * /| *
7674  // * 9/| * |11
7675  // | * | | * |
7676  // |/| * |10 *
7677  // * 8/ * |/
7678  // | * | *
7679  // |/ |/
7680  // * *
7681  // face 2: front plane
7682  // (note: x,y exchanged)
7683  // *-------*
7684  // | |
7685  // *---0---*
7686  // | |
7687  // *-------*
7688  // face 3: back plane
7689  // (note: x,y exchanged)
7690  // *-------*
7691  // | |
7692  // *---1---*
7693  // | |
7694  // *-------*
7695  // face 4: bottom plane
7696  // *-------*
7697  // / /
7698  // *---2---*
7699  // / /
7700  // *-------*
7701  // face 5: top plane
7702  // *-------*
7703  // / /
7704  // *---3---*
7705  // / /
7706  // *-------*
7707  // middle planes
7708  // *-------* *-------*
7709  // / / | |
7710  // *---12--* | |
7711  // / / | |
7712  // *-------* *-------*
7713 
7714  // set up a list of line iterators first. from
7715  // this, construct lists of line_indices and
7716  // line orientations later on
7717  const typename Triangulation<dim,spacedim>::raw_line_iterator
7718  lines_yz[13]
7719  =
7720  {
7721  hex->face(2)->child(0)
7722  ->line((hex->face(2)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //0
7723  hex->face(3)->child(0)
7724  ->line((hex->face(3)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //1
7725  hex->face(4)->child(0)
7726  ->line((hex->face(4)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //2
7727  hex->face(5)->child(0)
7728  ->line((hex->face(5)->refinement_case() == RefinementCase<2>::cut_x) ? 1 : 3), //3
7729 
7730  hex->face(0)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[0],f_fl[0],f_ro[0]))
7731  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[0],f_fl[0],f_ro[0])), //4
7732  hex->face(0)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[0],f_fl[0],f_ro[0]))
7733  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[0],f_fl[0],f_ro[0])), //5
7734  hex->face(0)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[0],f_fl[0],f_ro[0]))
7735  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[0],f_fl[0],f_ro[0])), //6
7736  hex->face(0)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[0],f_fl[0],f_ro[0]))
7737  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[0],f_fl[0],f_ro[0])), //7
7738 
7739  hex->face(1)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[1],f_fl[1],f_ro[1]))
7740  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[1],f_fl[1],f_ro[1])), //8
7741  hex->face(1)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[1],f_fl[1],f_ro[1]))
7742  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[1],f_fl[1],f_ro[1])), //9
7743  hex->face(1)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[1],f_fl[1],f_ro[1]))
7744  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[1],f_fl[1],f_ro[1])), //10
7745  hex->face(1)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[1],f_fl[1],f_ro[1]))
7746  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[1],f_fl[1],f_ro[1])), //11
7747 
7748  new_lines[0] //12
7749  };
7750 
7751  lines=&lines_yz[0];
7752 
7753  unsigned int line_indices_yz[13];
7754 
7755  for (unsigned int i=0; i<13; ++i)
7756  line_indices_yz[i]=lines[i]->index();
7757  line_indices=&line_indices_yz[0];
7758 
7759  // the orientation of lines for the inner quads
7760  // is quite tricky. as these lines are newly
7761  // created ones and thus have no parents, they
7762  // cannot inherit this property. set up an array
7763  // and fill it with the respective values
7764  bool line_orientation_yz[13];
7765 
7766  // the middle vertices of the lines of our front
7767  // face
7768  const unsigned int middle_vertices[4]=
7769  {
7770  hex->line(8)->child(0)->vertex_index(1),
7771  hex->line(10)->child(0)->vertex_index(1),
7772  hex->line(0)->child(0)->vertex_index(1),
7773  hex->line(4)->child(0)->vertex_index(1),
7774  };
7775 
7776  // note: for lines 0 to 3 the orientation of the
7777  // line is 'true', if vertex 0 is on the front
7778  for (unsigned int i=0; i<4; ++i)
7779  if (lines[i]->vertex_index(0)==middle_vertices[i])
7780  line_orientation_yz[i]=true;
7781  else
7782  {
7783  // it must be the other way round then
7784  Assert(lines[i]->vertex_index(1)==middle_vertices[i],
7785  ExcInternalError());
7786  line_orientation_yz[i]=false;
7787  }
7788 
7789  // note: for lines 4 to 11 (inner lines of the
7790  // outer quads) the following holds: the second
7791  // vertex of the even lines in standard
7792  // orientation is the vertex in the middle of
7793  // the quad, whereas for odd lines the first
7794  // vertex is the same middle vertex.
7795  for (unsigned int i=4; i<12; ++i)
7796  if (lines[i]->vertex_index((i+1)%2) ==
7797  middle_vertex_index<dim,spacedim>(hex->face(i/4-1)))
7798  line_orientation_yz[i]=true;
7799  else
7800  {
7801  // it must be the other way
7802  // round then
7803  Assert(lines[i]->vertex_index(i%2) ==
7804  (middle_vertex_index<dim,spacedim>(hex->face(i/4-1))),
7805  ExcInternalError());
7806  line_orientation_yz[i]=false;
7807  }
7808  // for the last line the line orientation is
7809  // always true, since it was just constructed
7810  // that way
7811 
7812  line_orientation_yz[12]=true;
7813  line_orientation=&line_orientation_yz[0];
7814 
7815  // set up the 4 quads, numbered as follows (left
7816  // quad numbering, right line numbering
7817  // extracted from above)
7818  //
7819  // x
7820  // *-------* *---3---*
7821  // | 3 | 5 9
7822  // *-------* *---12--*
7823  // | 2 | 4 8
7824  // *-------*y *---2---*
7825  //
7826  // y
7827  // *---------* *----1----*
7828  // / 1 / 7 11
7829  // *---------* *----12---*
7830  // / 0 / 6 10
7831  // *---------*x *----0----*
7832 
7833  new_quads[0]->set (internal::Triangulation
7834  ::TriaObject<2>(line_indices[6],
7835  line_indices[10],
7836  line_indices[0],
7837  line_indices[12]));
7838  new_quads[1]->set (internal::Triangulation
7839  ::TriaObject<2>(line_indices[7],
7840  line_indices[11],
7841  line_indices[12],
7842  line_indices[1]));
7843  new_quads[2]->set (internal::Triangulation
7844  ::TriaObject<2>(line_indices[2],
7845  line_indices[12],
7846  line_indices[4],
7847  line_indices[8]));
7848  new_quads[3]->set (internal::Triangulation
7849  ::TriaObject<2>(line_indices[12],
7850  line_indices[3],
7851  line_indices[5],
7852  line_indices[9]));
7853 
7854  new_quads[0]->set_line_orientation(0,line_orientation[6]);
7855  new_quads[0]->set_line_orientation(1,line_orientation[10]);
7856  new_quads[0]->set_line_orientation(2,line_orientation[0]);
7857 
7858  new_quads[1]->set_line_orientation(0,line_orientation[7]);
7859  new_quads[1]->set_line_orientation(1,line_orientation[11]);
7860  new_quads[1]->set_line_orientation(3,line_orientation[1]);
7861 
7862  new_quads[2]->set_line_orientation(0,line_orientation[2]);
7863  new_quads[2]->set_line_orientation(2,line_orientation[4]);
7864  new_quads[2]->set_line_orientation(3,line_orientation[8]);
7865 
7866  new_quads[3]->set_line_orientation(1,line_orientation[3]);
7867  new_quads[3]->set_line_orientation(2,line_orientation[5]);
7868  new_quads[3]->set_line_orientation(3,line_orientation[9]);
7869 
7870  // the quads are numbered as follows:
7871  //
7872  // planes in the interior of the old hex:
7873  //
7874  // *
7875  // /|
7876  // / | x
7877  // / | *-------* *---------*
7878  // * | | 3 | / 1 /
7879  // | | *-------* *---------*
7880  // | * | 2 | / 0 /
7881  // | / *-------*y *---------*x
7882  // | /
7883  // |/
7884  // *
7885  //
7886  // children of the faces
7887  // of the old hex
7888  // *-------* *-------*
7889  // /| | / 19 /|
7890  // * | 15 | *-------* |
7891  // /|7*-------* / 18 /|11
7892  // * |/| | *-------* |/|
7893  // |6* | 14 | | 10* |
7894  // |/|5*-------* | 13 |/|9*
7895  // * |/ 17 / *-------* |/
7896  // |4*-------* | |8*
7897  // |/ 16 / | 12 |/
7898  // *-------* *-------*
7899  //
7900  // note that we have to take care of the
7901  // orientation of faces.
7902  const int quad_indices_yz[20]
7903  =
7904  {
7905  new_quads[0]->index(), //0
7906  new_quads[1]->index(),
7907  new_quads[2]->index(),
7908  new_quads[3]->index(),
7909 
7910  hex->face(0)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[0],f_fl[0],f_ro[0])), //4
7911  hex->face(0)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[0],f_fl[0],f_ro[0])),
7912  hex->face(0)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[0],f_fl[0],f_ro[0])),
7913  hex->face(0)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[0],f_fl[0],f_ro[0])),
7914 
7915  hex->face(1)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[1],f_fl[1],f_ro[1])), //8
7916  hex->face(1)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[1],f_fl[1],f_ro[1])),
7917  hex->face(1)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[1],f_fl[1],f_ro[1])),
7918  hex->face(1)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[1],f_fl[1],f_ro[1])),
7919 
7920  hex->face(2)->child_index( child_at_origin[hex->face(2)->refinement_case()-1][f_fl[2]][f_ro[2]]), //12
7921  hex->face(2)->child_index(1-child_at_origin[hex->face(2)->refinement_case()-1][f_fl[2]][f_ro[2]]),
7922 
7923  hex->face(3)->child_index( child_at_origin[hex->face(3)->refinement_case()-1][f_fl[3]][f_ro[3]]), //14
7924  hex->face(3)->child_index(1-child_at_origin[hex->face(3)->refinement_case()-1][f_fl[3]][f_ro[3]]),
7925 
7926  hex->face(4)->child_index( child_at_origin[hex->face(4)->refinement_case()-1][f_fl[4]][f_ro[4]]), //16
7927  hex->face(4)->child_index(1-child_at_origin[hex->face(4)->refinement_case()-1][f_fl[4]][f_ro[4]]),
7928 
7929  hex->face(5)->child_index( child_at_origin[hex->face(5)->refinement_case()-1][f_fl[5]][f_ro[5]]), //18
7930  hex->face(5)->child_index(1-child_at_origin[hex->face(5)->refinement_case()-1][f_fl[5]][f_ro[5]])
7931  };
7932  quad_indices=&quad_indices_yz[0];
7933 
7934  new_hexes[0]->set (internal::Triangulation
7935  ::TriaObject<3>(quad_indices[4],
7936  quad_indices[8],
7937  quad_indices[12],
7938  quad_indices[2],
7939  quad_indices[16],
7940  quad_indices[0]));
7941  new_hexes[1]->set (internal::Triangulation
7942  ::TriaObject<3>(quad_indices[5],
7943  quad_indices[9],
7944  quad_indices[2],
7945  quad_indices[14],
7946  quad_indices[17],
7947  quad_indices[1]));
7948  new_hexes[2]->set (internal::Triangulation
7949  ::TriaObject<3>(quad_indices[6],
7950  quad_indices[10],
7951  quad_indices[13],
7952  quad_indices[3],
7953  quad_indices[0],
7954  quad_indices[18]));
7955  new_hexes[3]->set (internal::Triangulation
7956  ::TriaObject<3>(quad_indices[7],
7957  quad_indices[11],
7958  quad_indices[3],
7959  quad_indices[15],
7960  quad_indices[1],
7961  quad_indices[19]));
7962  break;
7963  }
7964 
7966  {
7967  const typename Triangulation<dim,spacedim>::raw_line_iterator
7968  *lines = NULL;
7969  const unsigned int *vertex_indices = NULL;
7970  const unsigned int *line_indices = NULL;
7971  const bool *line_orientation = NULL;
7972  const int *quad_indices = NULL;
7973 
7975  //
7976  // RefinementCase<dim>::cut_xyz
7977  // isotropic refinement
7978  //
7979  // the refined cube will look
7980  // like this:
7981  //
7982  // *----*----*
7983  // / / /|
7984  // *----*----* |
7985  // / / /| *
7986  // *----*----* |/|
7987  // | | | * |
7988  // | | |/| *
7989  // *----*----* |/
7990  // | | | *
7991  // | | |/
7992  // *----*----*
7993  //
7994 
7995  // find the next unused vertex and set it
7996  // appropriately
7997  while (triangulation.vertices_used[next_unused_vertex] == true)
7998  ++next_unused_vertex;
7999  Assert (next_unused_vertex < triangulation.vertices.size(),
8000  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."));
8001  triangulation.vertices_used[next_unused_vertex] = true;
8002 
8003  // the new vertex is definitely in the interior,
8004  // so we need not worry about the
8005  // boundary. However we need to worry about
8006  // Manifolds. Let the cell compute its own
8007  // center, by querying the underlying manifold
8008  // object.
8009  triangulation.vertices[next_unused_vertex] =
8010  hex->center(true, true);
8011 
8012  // set the data of the six lines. first collect
8013  // the indices of the seven vertices (consider
8014  // the two planes to be crossed to form the
8015  // planes cutting the hex in two vertically and
8016  // horizontally)
8017  //
8018  // *--3--* *--5--*
8019  // / / / | | |
8020  // 0--6--1 0--6--1
8021  // / / / | | |
8022  // *--2--* *--4--*
8023  // the lines are numbered
8024  // as follows:
8025  // *--*--* *--*--*
8026  // / 1 / | 5 |
8027  // *2-*-3* *2-*-3*
8028  // / 0 / | 4 |
8029  // *--*--* *--*--*
8030  //
8031  const unsigned int vertex_indices_xyz[7]
8032  = { middle_vertex_index<dim,spacedim>(hex->face(0)),
8033  middle_vertex_index<dim,spacedim>(hex->face(1)),
8034  middle_vertex_index<dim,spacedim>(hex->face(2)),
8035  middle_vertex_index<dim,spacedim>(hex->face(3)),
8036  middle_vertex_index<dim,spacedim>(hex->face(4)),
8037  middle_vertex_index<dim,spacedim>(hex->face(5)),
8038  next_unused_vertex
8039  };
8040  vertex_indices=&vertex_indices_xyz[0];
8041 
8042  new_lines[0]->set (internal::Triangulation::
8043  TriaObject<1>(vertex_indices[2], vertex_indices[6]));
8044  new_lines[1]->set (internal::Triangulation::
8045  TriaObject<1>(vertex_indices[6], vertex_indices[3]));
8046  new_lines[2]->set (internal::Triangulation::
8047  TriaObject<1>(vertex_indices[0], vertex_indices[6]));
8048  new_lines[3]->set (internal::Triangulation::
8049  TriaObject<1>(vertex_indices[6], vertex_indices[1]));
8050  new_lines[4]->set (internal::Triangulation::
8051  TriaObject<1>(vertex_indices[4], vertex_indices[6]));
8052  new_lines[5]->set (internal::Triangulation::
8053  TriaObject<1>(vertex_indices[6], vertex_indices[5]));
8054 
8055  // again, first collect some data about the
8056  // indices of the lines, with the following
8057  // numbering: (note that face 0 and 1 each are
8058  // shown twice for better readability)
8059 
8060  // face 0: left plane
8061  // * *
8062  // /| /|
8063  // * | * |
8064  // /| * /| *
8065  // * 1/| * |3|
8066  // | * | | * |
8067  // |/| * |2| *
8068  // * 0/ * |/
8069  // | * | *
8070  // |/ |/
8071  // * *
8072  // face 1: right plane
8073  // * *
8074  // /| /|
8075  // * | * |
8076  // /| * /| *
8077  // * 5/| * |7|
8078  // | * | | * |
8079  // |/| * |6| *
8080  // * 4/ * |/
8081  // | * | *
8082  // |/ |/
8083  // * *
8084  // face 2: front plane
8085  // (note: x,y exchanged)
8086  // *---*---*
8087  // | 11 |
8088  // *-8-*-9-*
8089  // | 10 |
8090  // *---*---*
8091  // face 3: back plane
8092  // (note: x,y exchanged)
8093  // *---*---*
8094  // | 15 |
8095  // *12-*-13*
8096  // | 14 |
8097  // *---*---*
8098  // face 4: bottom plane
8099  // *---*---*
8100  // / 17 /
8101  // *18-*-19*
8102  // / 16 /
8103  // *---*---*
8104  // face 5: top plane
8105  // *---*---*
8106  // / 21 /
8107  // *22-*-23*
8108  // / 20 /
8109  // *---*---*
8110  // middle planes
8111  // *---*---* *---*---*
8112  // / 25 / | 29 |
8113  // *26-*-27* *26-*-27*
8114  // / 24 / | 28 |
8115  // *---*---* *---*---*
8116 
8117  // set up a list of line iterators first. from
8118  // this, construct lists of line_indices and
8119  // line orientations later on
8120  const typename Triangulation<dim,spacedim>::raw_line_iterator
8121  lines_xyz[30]
8122  =
8123  {
8124  hex->face(0)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[0],f_fl[0],f_ro[0]))
8125  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[0],f_fl[0],f_ro[0])), //0
8126  hex->face(0)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[0],f_fl[0],f_ro[0]))
8127  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[0],f_fl[0],f_ro[0])), //1
8128  hex->face(0)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[0],f_fl[0],f_ro[0]))
8129  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[0],f_fl[0],f_ro[0])), //2
8130  hex->face(0)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[0],f_fl[0],f_ro[0]))
8131  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[0],f_fl[0],f_ro[0])), //3
8132 
8133  hex->face(1)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[1],f_fl[1],f_ro[1]))
8134  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[1],f_fl[1],f_ro[1])), //4
8135  hex->face(1)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[1],f_fl[1],f_ro[1]))
8136  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[1],f_fl[1],f_ro[1])), //5
8137  hex->face(1)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[1],f_fl[1],f_ro[1]))
8138  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[1],f_fl[1],f_ro[1])), //6
8139  hex->face(1)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[1],f_fl[1],f_ro[1]))
8140  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[1],f_fl[1],f_ro[1])), //7
8141 
8142  hex->face(2)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[2],f_fl[2],f_ro[2]))
8143  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[2],f_fl[2],f_ro[2])), //8
8144  hex->face(2)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[2],f_fl[2],f_ro[2]))
8145  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[2],f_fl[2],f_ro[2])), //9
8146  hex->face(2)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[2],f_fl[2],f_ro[2]))
8147  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[2],f_fl[2],f_ro[2])), //10
8148  hex->face(2)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[2],f_fl[2],f_ro[2]))
8149  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[2],f_fl[2],f_ro[2])), //11
8150 
8151  hex->face(3)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[3],f_fl[3],f_ro[3]))
8152  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[3],f_fl[3],f_ro[3])), //12
8153  hex->face(3)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[3],f_fl[3],f_ro[3]))
8154  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[3],f_fl[3],f_ro[3])), //13
8155  hex->face(3)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[3],f_fl[3],f_ro[3]))
8156  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[3],f_fl[3],f_ro[3])), //14
8157  hex->face(3)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[3],f_fl[3],f_ro[3]))
8158  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[3],f_fl[3],f_ro[3])), //15
8159 
8160  hex->face(4)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[4],f_fl[4],f_ro[4]))
8161  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[4],f_fl[4],f_ro[4])), //16
8162  hex->face(4)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[4],f_fl[4],f_ro[4]))
8163  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[4],f_fl[4],f_ro[4])), //17
8164  hex->face(4)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[4],f_fl[4],f_ro[4]))
8165  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[4],f_fl[4],f_ro[4])), //18
8166  hex->face(4)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[4],f_fl[4],f_ro[4]))
8167  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[4],f_fl[4],f_ro[4])), //19
8168 
8169  hex->face(5)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[5],f_fl[5],f_ro[5]))
8170  ->line(GeometryInfo<dim>::standard_to_real_face_line(1,f_or[5],f_fl[5],f_ro[5])), //20
8171  hex->face(5)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[5],f_fl[5],f_ro[5]))
8172  ->line(GeometryInfo<dim>::standard_to_real_face_line(0,f_or[5],f_fl[5],f_ro[5])), //21
8173  hex->face(5)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[5],f_fl[5],f_ro[5]))
8174  ->line(GeometryInfo<dim>::standard_to_real_face_line(3,f_or[5],f_fl[5],f_ro[5])), //22
8175  hex->face(5)->isotropic_child(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[5],f_fl[5],f_ro[5]))
8176  ->line(GeometryInfo<dim>::standard_to_real_face_line(2,f_or[5],f_fl[5],f_ro[5])), //23
8177 
8178  new_lines[0], //24
8179  new_lines[1], //25
8180  new_lines[2], //26
8181  new_lines[3], //27
8182  new_lines[4], //28
8183  new_lines[5] //29
8184  };
8185 
8186  lines=&lines_xyz[0];
8187 
8188  unsigned int line_indices_xyz[30];
8189  for (unsigned int i=0; i<30; ++i)
8190  line_indices_xyz[i]=lines[i]->index();
8191  line_indices=&line_indices_xyz[0];
8192 
8193  // the orientation of lines for the inner quads
8194  // is quite tricky. as these lines are newly
8195  // created ones and thus have no parents, they
8196  // cannot inherit this property. set up an array
8197  // and fill it with the respective values
8198  bool line_orientation_xyz[30];
8199 
8200  // note: for the first 24 lines (inner lines of
8201  // the outer quads) the following holds: the
8202  // second vertex of the even lines in standard
8203  // orientation is the vertex in the middle of
8204  // the quad, whereas for odd lines the first
8205  // vertex is the same middle vertex.
8206  for (unsigned int i=0; i<24; ++i)
8207  if (lines[i]->vertex_index((i+1)%2)==vertex_indices[i/4])
8208  line_orientation_xyz[i]=true;
8209  else
8210  {
8211  // it must be the other way
8212  // round then
8213  Assert(lines[i]->vertex_index(i%2)==vertex_indices[i/4],
8214  ExcInternalError());
8215  line_orientation_xyz[i]=false;
8216  }
8217  // for the last 6 lines the line orientation is
8218  // always true, since they were just constructed
8219  // that way
8220  for (unsigned int i=24; i<30; ++i)
8221  line_orientation_xyz[i]=true;
8222  line_orientation=&line_orientation_xyz[0];
8223 
8224  // set up the 12 quads, numbered as follows
8225  // (left quad numbering, right line numbering
8226  // extracted from above)
8227  //
8228  // * *
8229  // /| 21|
8230  // * | * 15
8231  // y/|3* 20| *
8232  // * |/| * |/|
8233  // |2* |x 11 * 14
8234  // |/|1* |/| *
8235  // * |/ * |17
8236  // |0* 10 *
8237  // |/ |16
8238  // * *
8239  //
8240  // x
8241  // *---*---* *22-*-23*
8242  // | 5 | 7 | 1 29 5
8243  // *---*---* *26-*-27*
8244  // | 4 | 6 | 0 28 4
8245  // *---*---*y *18-*-19*
8246  //
8247  // y
8248  // *----*----* *-12-*-13-*
8249  // / 10 / 11 / 3 25 7
8250  // *----*----* *-26-*-27-*
8251  // / 8 / 9 / 2 24 6
8252  // *----*----*x *--8-*--9-*
8253 
8254  new_quads[0]->set (internal::Triangulation
8255  ::TriaObject<2>(line_indices[10],
8256  line_indices[28],
8257  line_indices[16],
8258  line_indices[24]));
8259  new_quads[1]->set (internal::Triangulation
8260  ::TriaObject<2>(line_indices[28],
8261  line_indices[14],
8262  line_indices[17],
8263  line_indices[25]));
8264  new_quads[2]->set (internal::Triangulation
8265  ::TriaObject<2>(line_indices[11],
8266  line_indices[29],
8267  line_indices[24],
8268  line_indices[20]));
8269  new_quads[3]->set (internal::Triangulation
8270  ::TriaObject<2>(line_indices[29],
8271  line_indices[15],
8272  line_indices[25],
8273  line_indices[21]));
8274  new_quads[4]->set (internal::Triangulation
8275  ::TriaObject<2>(line_indices[18],
8276  line_indices[26],
8277  line_indices[0],
8278  line_indices[28]));
8279  new_quads[5]->set (internal::Triangulation
8280  ::TriaObject<2>(line_indices[26],
8281  line_indices[22],
8282  line_indices[1],
8283  line_indices[29]));
8284  new_quads[6]->set (internal::Triangulation
8285  ::TriaObject<2>(line_indices[19],
8286  line_indices[27],
8287  line_indices[28],
8288  line_indices[4]));
8289  new_quads[7]->set (internal::Triangulation
8290  ::TriaObject<2>(line_indices[27],
8291  line_indices[23],
8292  line_indices[29],
8293  line_indices[5]));
8294  new_quads[8]->set (internal::Triangulation
8295  ::TriaObject<2>(line_indices[2],
8296  line_indices[24],
8297  line_indices[8],
8298  line_indices[26]));
8299  new_quads[9]->set (internal::Triangulation
8300  ::TriaObject<2>(line_indices[24],
8301  line_indices[6],
8302  line_indices[9],
8303  line_indices[27]));
8304  new_quads[10]->set (internal::Triangulation
8305  ::TriaObject<2>(line_indices[3],
8306  line_indices[25],
8307  line_indices[26],
8308  line_indices[12]));
8309  new_quads[11]->set (internal::Triangulation
8310  ::TriaObject<2>(line_indices[25],
8311  line_indices[7],
8312  line_indices[27],
8313  line_indices[13]));
8314 
8315  // now reset the line_orientation flags of outer
8316  // lines as they cannot be set in a loop (at
8317  // least not easily)
8318  new_quads[0]->set_line_orientation(0,line_orientation[10]);
8319  new_quads[0]->set_line_orientation(2,line_orientation[16]);
8320 
8321  new_quads[1]->set_line_orientation(1,line_orientation[14]);
8322  new_quads[1]->set_line_orientation(2,line_orientation[17]);
8323 
8324  new_quads[2]->set_line_orientation(0,line_orientation[11]);
8325  new_quads[2]->set_line_orientation(3,line_orientation[20]);
8326 
8327  new_quads[3]->set_line_orientation(1,line_orientation[15]);
8328  new_quads[3]->set_line_orientation(3,line_orientation[21]);
8329 
8330  new_quads[4]->set_line_orientation(0,line_orientation[18]);
8331  new_quads[4]->set_line_orientation(2,line_orientation[0]);
8332 
8333  new_quads[5]->set_line_orientation(1,line_orientation[22]);
8334  new_quads[5]->set_line_orientation(2,line_orientation[1]);
8335 
8336  new_quads[6]->set_line_orientation(0,line_orientation[19]);
8337  new_quads[6]->set_line_orientation(3,line_orientation[4]);
8338 
8339  new_quads[7]->set_line_orientation(1,line_orientation[23]);
8340  new_quads[7]->set_line_orientation(3,line_orientation[5]);
8341 
8342  new_quads[8]->set_line_orientation(0,line_orientation[2]);
8343  new_quads[8]->set_line_orientation(2,line_orientation[8]);
8344 
8345  new_quads[9]->set_line_orientation(1,line_orientation[6]);
8346  new_quads[9]->set_line_orientation(2,line_orientation[9]);
8347 
8348  new_quads[10]->set_line_orientation(0,line_orientation[3]);
8349  new_quads[10]->set_line_orientation(3,line_orientation[12]);
8350 
8351  new_quads[11]->set_line_orientation(1,line_orientation[7]);
8352  new_quads[11]->set_line_orientation(3,line_orientation[13]);
8353 
8355  // create the eight new hexes
8356  //
8357  // again first collect some data. here, we need
8358  // the indices of a whole lotta quads.
8359 
8360  // the quads are numbered as follows:
8361  //
8362  // planes in the interior of the old hex:
8363  //
8364  // *
8365  // /|
8366  // * |
8367  // /|3* *---*---* *----*----*
8368  // * |/| | 5 | 7 | / 10 / 11 /
8369  // |2* | *---*---* *----*----*
8370  // |/|1* | 4 | 6 | / 8 / 9 /
8371  // * |/ *---*---*y *----*----*x
8372  // |0*
8373  // |/
8374  // *
8375  //
8376  // children of the faces
8377  // of the old hex
8378  // *-------* *-------*
8379  // /|25 27| /34 35/|
8380  // 15| | / /19
8381  // / | | /32 33/ |
8382  // * |24 26| *-------*18 |
8383  // 1413*-------* |21 23| 17*
8384  // | /30 31/ | | /
8385  // 12/ / | |16
8386  // |/28 29/ |20 22|/
8387  // *-------* *-------*
8388  //
8389  // note that we have to
8390  // take care of the
8391  // orientation of
8392  // faces.
8393  const int quad_indices_xyz[36]
8394  =
8395  {
8396  new_quads[0]->index(), //0
8397  new_quads[1]->index(),
8398  new_quads[2]->index(),
8399  new_quads[3]->index(),
8400  new_quads[4]->index(),
8401  new_quads[5]->index(),
8402  new_quads[6]->index(),
8403  new_quads[7]->index(),
8404  new_quads[8]->index(),
8405  new_quads[9]->index(),
8406  new_quads[10]->index(),
8407  new_quads[11]->index(), //11
8408 
8409  hex->face(0)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[0],f_fl[0],f_ro[0])), //12
8410  hex->face(0)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[0],f_fl[0],f_ro[0])),
8411  hex->face(0)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[0],f_fl[0],f_ro[0])),
8412  hex->face(0)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[0],f_fl[0],f_ro[0])),
8413 
8414  hex->face(1)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[1],f_fl[1],f_ro[1])), //16
8415  hex->face(1)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[1],f_fl[1],f_ro[1])),
8416  hex->face(1)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[1],f_fl[1],f_ro[1])),
8417  hex->face(1)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[1],f_fl[1],f_ro[1])),
8418 
8419  hex->face(2)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[2],f_fl[2],f_ro[2])), //20
8420  hex->face(2)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[2],f_fl[2],f_ro[2])),
8421  hex->face(2)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[2],f_fl[2],f_ro[2])),
8422  hex->face(2)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[2],f_fl[2],f_ro[2])),
8423 
8424  hex->face(3)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[3],f_fl[3],f_ro[3])), //24
8425  hex->face(3)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[3],f_fl[3],f_ro[3])),
8426  hex->face(3)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[3],f_fl[3],f_ro[3])),
8427  hex->face(3)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[3],f_fl[3],f_ro[3])),
8428 
8429  hex->face(4)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[4],f_fl[4],f_ro[4])), //28
8430  hex->face(4)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[4],f_fl[4],f_ro[4])),
8431  hex->face(4)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[4],f_fl[4],f_ro[4])),
8432  hex->face(4)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[4],f_fl[4],f_ro[4])),
8433 
8434  hex->face(5)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(0,f_or[5],f_fl[5],f_ro[5])), //32
8435  hex->face(5)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(1,f_or[5],f_fl[5],f_ro[5])),
8436  hex->face(5)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(2,f_or[5],f_fl[5],f_ro[5])),
8437  hex->face(5)->isotropic_child_index(GeometryInfo<dim>::standard_to_real_face_vertex(3,f_or[5],f_fl[5],f_ro[5]))
8438  };
8439  quad_indices=&quad_indices_xyz[0];
8440 
8441  // bottom children
8442  new_hexes[0]->set (internal::Triangulation
8443  ::TriaObject<3>(quad_indices[12],
8444  quad_indices[0],
8445  quad_indices[20],
8446  quad_indices[4],
8447  quad_indices[28],
8448  quad_indices[8]));
8449  new_hexes[1]->set (internal::Triangulation
8450  ::TriaObject<3>(quad_indices[0],
8451  quad_indices[16],
8452  quad_indices[22],
8453  quad_indices[6],
8454  quad_indices[29],
8455  quad_indices[9]));
8456  new_hexes[2]->set (internal::Triangulation
8457  ::TriaObject<3>(quad_indices[13],
8458  quad_indices[1],
8459  quad_indices[4],
8460  quad_indices[24],
8461  quad_indices[30],
8462  quad_indices[10]));
8463  new_hexes[3]->set (internal::Triangulation
8464  ::TriaObject<3>(quad_indices[1],
8465  quad_indices[17],
8466  quad_indices[6],
8467  quad_indices[26],
8468  quad_indices[31],
8469  quad_indices[11]));
8470 
8471  // top children
8472  new_hexes[4]->set (internal::Triangulation
8473  ::TriaObject<3>(quad_indices[14],
8474  quad_indices[2],
8475  quad_indices[21],
8476  quad_indices[5],
8477  quad_indices[8],
8478  quad_indices[32]));
8479  new_hexes[5]->set (internal::Triangulation
8480  ::TriaObject<3>(quad_indices[2],
8481  quad_indices[18],
8482  quad_indices[23],
8483  quad_indices[7],
8484  quad_indices[9],
8485  quad_indices[33]));
8486  new_hexes[6]->set (internal::Triangulation
8487  ::TriaObject<3>(quad_indices[15],
8488  quad_indices[3],
8489  quad_indices[5],
8490  quad_indices[25],
8491  quad_indices[10],
8492  quad_indices[34]));
8493  new_hexes[7]->set (internal::Triangulation
8494  ::TriaObject<3>(quad_indices[3],
8495  quad_indices[19],
8496  quad_indices[7],
8497  quad_indices[27],
8498  quad_indices[11],
8499  quad_indices[35]));
8500  break;
8501  }
8502  default:
8503  // all refinement cases have been treated, there
8504  // only remains
8505  // RefinementCase<dim>::no_refinement as
8506  // untreated enumeration value. However, in that
8507  // case we should have aborted much
8508  // earlier. thus we should never get here
8509  Assert(false, ExcInternalError());
8510  break;
8511  }//switch (ref_case)
8512 
8513  // and set face orientation flags. note that new
8514  // faces in the interior of the mother cell always
8515  // have a correctly oriented face, but the ones on
8516  // the outer faces will inherit this flag
8517  //
8518  // the flag have been set to true for all faces
8519  // initially, now go the other way round and reset
8520  // faces that are at the boundary of the mother cube
8521  //
8522  // the same is true for the face_flip and
8523  // face_rotation flags. however, the latter two are
8524  // set to false by default as this is the standard
8525  // value
8526 
8527  // loop over all faces and all (relevant) subfaces
8528  // of that in order to set the correct values for
8529  // face_orientation, face_flip and face_rotation,
8530  // which are inherited from the corresponding face
8531  // of the mother cube
8532  for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
8533  for (unsigned int s=0;
8535  1U);
8536  ++s)
8537  {
8538  const unsigned int current_child
8540  f,
8541  s,
8542  f_or[f],
8543  f_fl[f],
8544  f_ro[f],
8546  f,
8547  f_or[f],
8548  f_fl[f],
8549  f_ro[f]));
8550  new_hexes[current_child]->set_face_orientation (f, f_or[f]);
8551  new_hexes[current_child]->set_face_flip (f, f_fl[f]);
8552  new_hexes[current_child]->set_face_rotation (f, f_ro[f]);
8553  }
8554 
8555  // now see if we have created cells that are
8556  // distorted and if so add them to our list
8557  if ((check_for_distorted_cells == true)
8558  &&
8559  has_distorted_children (hex,
8562  cells_with_distorted_children.distorted_cells.push_back (hex);
8563 
8564  // note that the refinement flag was already cleared
8565  // at the beginning of this loop
8566 
8567  // inform all listeners that cell refinement is done
8568  triangulation.signals.post_refinement_on_cell(hex);
8569  }
8570  }
8571 
8572  // clear user data on quads. we used some of this data to
8573  // indicate anisotropic refinemnt cases on faces. all data
8574  // should be cleared by now, but the information whether we
8575  // used indices or pointers is still present. reset it now to
8576  // enable the user to use whichever he likes later on.
8577  triangulation.faces->quads.clear_user_data();
8578 
8579  // return the list with distorted children
8580  return cells_with_distorted_children;
8581  }
8582 
8583 
8596  template <int spacedim>
8597  static
8598  void
8600 
8601 
8602  template <int dim, int spacedim>
8603  static
8604  void
8606  {
8607  // If the codimension is one, we cannot perform this check
8608  // yet.
8609  if (spacedim>dim) return;
8610 
8612  cell=triangulation.begin(); cell!=triangulation.end(); ++cell)
8613  if (cell->at_boundary() &&
8614  cell->refine_flag_set() &&
8615  cell->refine_flag_set()!=RefinementCase<dim>::isotropic_refinement)
8616  {
8617  // The cell is at the boundary and it is flagged for
8618  // anisotropic refinement. Therefore, we have a closer
8619  // look
8620  const RefinementCase<dim> ref_case=cell->refine_flag_set();
8621  for (unsigned int face_no=0;
8622  face_no<GeometryInfo<dim>::faces_per_cell;
8623  ++face_no)
8624  if (cell->face(face_no)->at_boundary())
8625  {
8626  // this is the critical face at the boundary.
8627  if (GeometryInfo<dim>::face_refinement_case(ref_case,face_no)
8628  !=RefinementCase<dim-1>::isotropic_refinement)
8629  {
8630  // up to now, we do not want to refine this
8631  // cell along the face under consideration
8632  // here.
8634  face = cell->face(face_no);
8635  // the new point on the boundary would be this
8636  // one.
8637  const Point<spacedim> new_bound
8638  = face->center(true);
8639  // to check it, transform to the unit cell
8640  // with Q1Mapping
8641  const Point<dim> new_unit
8643  transform_real_to_unit_cell(cell,
8644  new_bound);
8645 
8646  // Now, we have to calculate the distance from
8647  // the face in the unit cell.
8648 
8649  // take the correct coordinate direction (0
8650  // for faces 0 and 1, 1 for faces 2 and 3, 2
8651  // for faces 4 and 5) and subtract the correct
8652  // boundary value of the face (0 for faces 0,
8653  // 2, and 4; 1 for faces 1, 3 and 5)
8654  const double dist = std::fabs(new_unit[face_no/2] - face_no%2);
8655 
8656  // compare this with the empirical value
8657  // allowed. if it is too big, flag the face
8658  // for isotropic refinement
8659  const double allowed=0.25;
8660 
8661  if (dist>allowed)
8662  cell->flag_for_face_refinement(face_no);
8663  }//if flagged for anistropic refinement
8664  }//if (cell->face(face)->at_boundary())
8665  }//for all cells
8666  }
8667 
8668 
8681  template <int dim, int spacedim>
8682  static
8683  void
8685  {
8686  Assert (dim < 3,
8687  ExcMessage ("Wrong function called -- there should "
8688  "be a specialization."));
8689  }
8690 
8691 
8692  template <int spacedim>
8693  static
8694  void
8696  {
8697  const unsigned int dim = 3;
8698 
8699  // first clear flags on lines, since we need them to determine
8700  // which lines will be refined
8701  triangulation.clear_user_flags_line();
8702 
8703  // also clear flags on hexes, since we need them to mark those
8704  // cells which are to be coarsened
8705  triangulation.clear_user_flags_hex();
8706 
8707  // variable to store whether the mesh was changed in the
8708  // present loop and in the whole process
8709  bool mesh_changed = false;
8710 
8711  do
8712  {
8713  mesh_changed = false;
8714 
8715  // for this following, we need to know which cells are
8716  // going to be coarsened, if we had to make a
8717  // decision. the following function sets these flags:
8718  triangulation.fix_coarsen_flags ();
8719 
8720 
8721  // flag those lines that are refined and will not be
8722  // coarsened and those that will be refined
8724  cell=triangulation.begin(); cell!=triangulation.end(); ++cell)
8725  if (cell->refine_flag_set())
8726  {
8727  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
8728  if (GeometryInfo<dim>::line_refinement_case(cell->refine_flag_set(), line)
8730  // flag a line, that will be
8731  // refined
8732  cell->line(line)->set_user_flag();
8733  }
8734  else if (cell->has_children() && !cell->child(0)->coarsen_flag_set())
8735  {
8736  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
8737  if (GeometryInfo<dim>::line_refinement_case(cell->refinement_case(), line)
8739  // flag a line, that is refined
8740  // and will stay so
8741  cell->line(line)->set_user_flag();
8742  }
8743  else if (cell->has_children() && cell->child(0)->coarsen_flag_set())
8744  cell->set_user_flag();
8745 
8746 
8747  // now check whether there are cells with lines that are
8748  // more than once refined or that will be more than once
8749  // refined. The first thing should never be the case, in
8750  // the second case we flag the cell for refinement
8752  cell=triangulation.last_active(); cell!=triangulation.end(); --cell)
8753  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
8754  {
8755  if (cell->line(line)->has_children())
8756  {
8757  // if this line is refined, its children should
8758  // not have further children
8759  //
8760  // however, if any of the children is flagged
8761  // for further refinement, we need to refine
8762  // this cell also (at least, if the cell is not
8763  // already flagged)
8764  bool offending_line_found = false;
8765 
8766  for (unsigned int c=0; c<2; ++c)
8767  {
8768  Assert (cell->line(line)->child(c)->has_children() == false,
8769  ExcInternalError());
8770 
8771  if (cell->line(line)->child(c)->user_flag_set () &&
8772  (GeometryInfo<dim>::line_refinement_case(cell->refine_flag_set(),
8773  line)
8775  {
8776  // tag this cell for refinement
8777  cell->clear_coarsen_flag ();
8778  // if anisotropic coarsening is allowed:
8779  // extend the refine_flag in the needed
8780  // direction, else set refine_flag
8781  // (isotropic)
8782  if (triangulation.smooth_grid &
8784  cell->flag_for_line_refinement(line);
8785  else
8786  cell->set_refine_flag();
8787 
8788  for (unsigned int l=0; l<GeometryInfo<dim>::lines_per_cell; ++l)
8789  if (GeometryInfo<dim>::line_refinement_case(cell->refine_flag_set(), line)
8791  // flag a line, that will be refined
8792  cell->line(l)->set_user_flag();
8793 
8794  // note that we have changed the grid
8795  offending_line_found = true;
8796 
8797  // it may save us several loop
8798  // iterations if we flag all lines of
8799  // this cell now (and not at the outset
8800  // of the next iteration) for refinement
8801  for (unsigned int l=0;
8802  l<GeometryInfo<dim>::lines_per_cell; ++l)
8803  if (!cell->line(l)->has_children() &&
8804  (GeometryInfo<dim>::line_refinement_case(cell->refine_flag_set(),
8805  l)
8807  cell->line(l)->set_user_flag();
8808 
8809  break;
8810  }
8811  }
8812 
8813  if (offending_line_found)
8814  {
8815  mesh_changed = true;
8816  break;
8817  }
8818  }
8819  }
8820 
8821 
8822  // there is another thing here: if any of the lines will
8823  // be refined, then we may not coarsen the present cell
8824  // similarly, if any of the lines *is* already refined, we
8825  // may not coarsen the current cell. however, there's a
8826  // catch: if the line is refined, but the cell behind it
8827  // is going to be coarsened, then the situation
8828  // changes. if we forget this second condition, the
8829  // refine_and_coarsen_3d test will start to fail. note
8830  // that to know which cells are going to be coarsened, the
8831  // call for fix_coarsen_flags above is necessary
8833  cell=triangulation.last(); cell!=triangulation.end(); --cell)
8834  {
8835  if (cell->user_flag_set())
8836  for (unsigned int line=0; line<GeometryInfo<dim>::lines_per_cell; ++line)
8837  if (cell->line(line)->has_children() &&
8838  (cell->line(line)->child(0)->user_flag_set() ||
8839  cell->line(line)->child(1)->user_flag_set()))
8840  {
8841  for (unsigned int c=0; c<cell->n_children(); ++c)
8842  cell->child(c)->clear_coarsen_flag ();
8843  cell->clear_user_flag();
8844  for (unsigned int l=0; l<GeometryInfo<dim>::lines_per_cell; ++l)
8845  if (GeometryInfo<dim>::line_refinement_case(cell->refinement_case(), l)
8847  // flag a line, that is refined
8848  // and will stay so
8849  cell->line(l)->set_user_flag();
8850  mesh_changed = true;
8851  break;
8852  }
8853  }
8854  }
8855  while (mesh_changed == true);
8856  }
8857 
8858 
8859 
8866  template <int dim, int spacedim>
8867  static
8868  bool
8870  {
8871  // in 1d, coarsening is always allowed since we don't enforce
8872  // the 2:1 constraint there
8873  if (dim == 1)
8874  return true;
8875 
8876  const RefinementCase<dim> ref_case = cell->refinement_case();
8877  for (unsigned int n=0; n<GeometryInfo<dim>::faces_per_cell; ++n)
8878  {
8879 
8880  // if the cell is not refined along that face, coarsening
8881  // will not change anything, so do nothing. the same
8882  // applies, if the face is at the boandary
8883  const RefinementCase<dim-1> face_ref_case =
8884  GeometryInfo<dim>::face_refinement_case(cell->refinement_case(), n);
8885 
8886  const unsigned int n_subfaces
8887  = GeometryInfo<dim-1>::n_children(face_ref_case);
8888 
8889  if (n_subfaces == 0 || cell->at_boundary(n))
8890  continue;
8891  for (unsigned int c=0; c<n_subfaces; ++c)
8892  {
8894  child = cell->child(GeometryInfo<dim>::
8895  child_cell_on_face(ref_case,
8896  n,c));
8897 
8899  child_neighbor = child->neighbor(n);
8900  if (!child->neighbor_is_coarser(n))
8901  // in 2d, if the child's neighbor is coarser, then
8902  // it has no children. however, in 3d it might be
8903  // otherwise. consider for example, that our face
8904  // might be refined with cut_x, but the neighbor is
8905  // refined with cut_xy at that face. then the
8906  // neighbor pointers of the children of our cell
8907  // will point to the common neighbor cell, not to
8908  // its children. what we really want to know in the
8909  // following is, whether the neighbor cell is
8910  // refined twice with reference to our cell. that
8911  // only has to be asked, if the child's neighbor is
8912  // not a coarser one.
8913  if ((child_neighbor->has_children() &&
8914  !child_neighbor->user_flag_set())||
8915  // neighbor has children, which are further
8916  // refined along the face, otherwise something
8917  // went wrong in the construction of neighbor
8918  // pointers. then only allow coarsening if this
8919  // neighbor will be coarsened as well
8920  // (user_pointer is set). the same applies, if
8921  // the neighbors children are not refined but
8922  // will be after refinement
8923  child_neighbor->refine_flag_set())
8924  return false;
8925  }
8926  }
8927  return true;
8928  }
8929  };
8930  }
8931 }
8932 
8933 
8934 template <int dim, int spacedim>
8937 
8938 
8939 
8940 template <int dim, int spacedim>
8941 const unsigned int
8943 
8944 
8945 
8946 template <int dim, int spacedim>
8948 Triangulation (const MeshSmoothing smooth_grid,
8949  const bool check_for_distorted_cells)
8950  :
8951  smooth_grid(smooth_grid),
8952  anisotropic_refinement(false),
8953  check_for_distorted_cells(check_for_distorted_cells)
8954 {
8955  if (dim == 1)
8956  {
8958  .reset (new std::map<unsigned int, types::boundary_id>());
8960  .reset (new std::map<unsigned int, types::manifold_id>());
8961  }
8962 
8963  // connect the any_change signal to the other top level signals
8964  signals.create.connect (signals.any_change);
8966  signals.clear.connect (signals.any_change);
8968 }
8969 
8970 
8971 template <int dim, int spacedim>
8974 // do not set any subscriptors;
8975 // anyway, calling this constructor
8976 // is an error!
8977  :
8978  Subscriptor(),
8979  signals (),
8980  anisotropic_refinement(false),
8981  check_for_distorted_cells(other.check_for_distorted_cells)
8982 {
8983  Assert (false, ExcMessage ("You are not allowed to call this constructor "
8984  "because copying Triangulation objects is not "
8985  "allowed. Use Triangulation::copy_from() instead."));
8986 }
8987 
8988 
8989 
8990 #ifdef DEAL_II_WITH_CXX11
8991 
8992 template <int dim, int spacedim>
8995  :
8996  Subscriptor(tria),
8997  smooth_grid(tria.smooth_grid),
8998  periodic_face_pairs_level_0(std::move(tria.periodic_face_pairs_level_0)),
8999  periodic_face_map(std::move(tria.periodic_face_map)),
9000  levels(std::move(tria.levels)),
9001  faces(std::move(tria.faces)),
9002  vertices(std::move(tria.vertices)),
9003  vertices_used(std::move(tria.vertices_used)),
9004  manifold(std::move(tria.manifold)),
9005  anisotropic_refinement(tria.anisotropic_refinement),
9006  check_for_distorted_cells(tria.check_for_distorted_cells),
9007  number_cache(tria.number_cache),
9008  vertex_to_boundary_id_map_1d(std::move(tria.vertex_to_boundary_id_map_1d)),
9009  vertex_to_manifold_id_map_1d(std::move(tria.vertex_to_manifold_id_map_1d))
9010 {
9011  for (unsigned int i=0; i<tria.levels.size(); ++i)
9012  tria.levels[i] = nullptr;
9013 
9014  tria.number_cache = internal::Triangulation::NumberCache<dim>();
9015 }
9016 
9017 
9018 template <int dim, int spacedim>
9021 {
9022  Subscriptor::operator=(std::move(tria));
9023 
9024  smooth_grid = tria.smooth_grid;
9025  periodic_face_pairs_level_0 = std::move(tria.periodic_face_pairs_level_0);
9026  periodic_face_map = std::move(tria.periodic_face_map);
9027  levels = std::move(tria.levels);
9028  faces = std::move(tria.faces);
9029  vertices = std::move(tria.vertices);
9030  vertices_used = std::move(tria.vertices_used);
9031  manifold = std::move(tria.manifold);
9032  anisotropic_refinement = tria.anisotropic_refinement;
9033  number_cache = tria.number_cache;
9034  vertex_to_boundary_id_map_1d = std::move(tria.vertex_to_boundary_id_map_1d);
9035  vertex_to_manifold_id_map_1d = std::move(tria.vertex_to_manifold_id_map_1d);
9036 
9037  for (unsigned int i=0; i<tria.levels.size(); ++i)
9038  tria.levels[i] = nullptr;
9039 
9040  tria.number_cache = internal::Triangulation::NumberCache<dim>();
9041 
9042  return *this;
9043 }
9044 
9045 #endif
9046 
9047 
9048 
9049 template <int dim, int spacedim>
9051 {
9052  for (unsigned int i=0; i<levels.size(); ++i)
9053  if (levels[i])
9054  {
9055  delete levels[i];
9056  levels[i] = 0;
9057  }
9058  levels.clear ();
9059 
9060  // the vertex_to_boundary_id_map_1d field should be unused except in
9061  // 1d. double check this here, as destruction is a good place to
9062  // ensure that what we've done over the course of the lifetime of
9063  // this object makes sense
9064  Assert ((dim == 1)
9065  ||
9066  (vertex_to_boundary_id_map_1d == 0),
9067  ExcInternalError());
9068 
9069  // the vertex_to_manifold_id_map_1d field should be also unused
9070  // except in 1d. check this as well
9071  Assert ((dim == 1)
9072  ||
9073  (vertex_to_manifold_id_map_1d == 0),
9074  ExcInternalError());
9075 }
9076 
9077 
9078 
9079 template <int dim, int spacedim>
9081 {
9082  // notify listeners that the triangulation is going down...
9083  signals.clear();
9084 
9085  // ...and then actually clear all content of it
9086  clear_despite_subscriptions();
9087  periodic_face_pairs_level_0.clear();
9088  periodic_face_map.clear();
9089 }
9090 
9091 
9092 
9093 template <int dim, int spacedim>
9094 void
9096 {
9097  Assert (n_levels() == 0,
9098  ExcTriangulationNotEmpty (vertices.size(), levels.size()));
9099  smooth_grid=mesh_smoothing;
9100 }
9101 
9102 
9103 
9104 template <int dim, int spacedim>
9107 {
9108  return smooth_grid;
9109 }
9110 
9111 
9112 
9113 template <int dim, int spacedim>
9114 void
9116  const Boundary<dim, spacedim> &boundary_object)
9117 {
9118  set_manifold(m_number, boundary_object);
9119 }
9120 
9121 template <int dim, int spacedim>
9122 void
9124  const Manifold<dim, spacedim> &manifold_object)
9125 {
9128 
9129  manifold[m_number] = &manifold_object;
9130 }
9131 
9132 
9133 template <int dim, int spacedim>
9134 void
9136 {
9137  set_manifold(m_number);
9138 }
9139 
9140 
9141 template <int dim, int spacedim>
9142 void
9144 {
9147 
9148  //delete the entry located at number.
9149  manifold.erase(m_number);
9150 }
9151 
9152 template <int dim, int spacedim>
9153 void
9155 {
9156  Assert(n_cells()>0,
9157  ExcMessage("Error: set_all_manifold_ids() can not be called on an empty Triangulation."));
9158 
9160  cell=this->begin_active(), endc=this->end();
9161 
9162  for (; cell != endc; ++cell)
9163  cell->set_all_manifold_ids(m_number);
9164 }
9165 
9166 
9167 template <int dim, int spacedim>
9168 void
9170 {
9171  Assert(n_cells()>0,
9172  ExcMessage("Error: set_all_manifold_ids_on_boundary() can not be called on an empty Triangulation."));
9173 
9175  cell=this->begin_active(), endc=this->end();
9176 
9177  for (; cell != endc; ++cell)
9178  for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
9179  if (cell->face(f)->at_boundary())
9180  cell->face(f)->set_all_manifold_ids(m_number);
9181 }
9182 
9183 
9184 template <int dim, int spacedim>
9185 void
9187  const types::manifold_id m_number)
9188 {
9189  Assert(n_cells()>0,
9190  ExcMessage("Error: set_all_manifold_ids_on_boundary() can not be called on an empty Triangulation."));
9191 
9192  bool boundary_found = false;
9194  cell=this->begin_active(), endc=this->end();
9195 
9196  for (; cell != endc; ++cell)
9197  {
9198  // loop on faces
9199  for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
9200  if (cell->face(f)->at_boundary() && cell->face(f)->boundary_id()==b_id)
9201  {
9202  boundary_found = true;
9203  cell->face(f)->set_manifold_id(m_number);
9204  }
9205 
9206  // loop on edges if dim >= 3
9207  if (dim>=3)
9208  for (unsigned int e=0; e<GeometryInfo<dim>::lines_per_cell; ++e)
9209  if (cell->line(e)->at_boundary() && cell->line(e)->boundary_id()==b_id)
9210  {
9211  boundary_found = true;
9212  cell->line(e)->set_manifold_id(m_number);
9213  }
9214  }
9215 
9216  (void)boundary_found;
9217  Assert(boundary_found, ExcBoundaryIdNotFound(b_id));
9218 }
9219 
9220 
9221 template <int dim, int spacedim>
9222 const Boundary<dim,spacedim> &
9224 {
9225  const Boundary<dim, spacedim> *man =
9226  dynamic_cast<const Boundary<dim, spacedim> *>(&get_manifold(m_number));
9227  Assert(man != NULL,
9228  ExcMessage("You tried to get a Boundary, but I only have a Manifold."));
9229 
9230  return *man;
9231 }
9232 
9233 
9234 template <int dim, int spacedim>
9235 const Manifold<dim,spacedim> &
9237 {
9238  //look, if there is a manifold stored at
9239  //manifold_id number.
9240  typename std::map<types::manifold_id, SmartPointer<const Manifold<dim,spacedim>, Triangulation<dim, spacedim> > >::const_iterator it
9241  = manifold.find(m_number);
9242 
9243  if (it != manifold.end())
9244  {
9245  //if we have found an entry, return it
9246  return *(it->second);
9247  }
9248  else
9249  {
9250  //if we have not found an entry connected with number, we return
9251  //straight_boundary
9252  return straight_boundary;
9253  }
9254 }
9255 
9256 
9257 
9258 
9259 template <int dim, int spacedim>
9260 std::vector<types::boundary_id>
9262 {
9263  // in 1d, we store a map of all used boundary indicators. use it for
9264  // our purposes
9265  if (dim == 1)
9266  {
9267  std::vector<types::boundary_id> boundary_ids;
9268  for (std::map<unsigned int, types::boundary_id>::const_iterator
9269  p = vertex_to_boundary_id_map_1d->begin();
9270  p != vertex_to_boundary_id_map_1d->end();
9271  ++p)
9272  boundary_ids.push_back (p->second);
9273 
9274  return boundary_ids;
9275  }
9276  else
9277  {
9278  std::set<types::boundary_id> b_ids;
9279  active_cell_iterator cell=begin_active();
9280  for (; cell!=end(); ++cell)
9281  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
9282  if (cell->at_boundary(face))
9283  b_ids.insert(cell->face(face)->boundary_id());
9284  std::vector<types::boundary_id> boundary_ids(b_ids.begin(), b_ids.end());
9285  return boundary_ids;
9286  }
9287 }
9288 
9289 
9290 
9291 template <int dim, int spacedim>
9292 std::vector<types::manifold_id>
9294 {
9295  std::set<types::manifold_id> m_ids;
9296  active_cell_iterator cell=begin_active();
9297  for (; cell!=end(); ++cell)
9298  {
9299  m_ids.insert(cell->manifold_id());
9300  if (dim>1)
9301  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
9302  if (cell->at_boundary(face))
9303  m_ids.insert(cell->face(face)->manifold_id());
9304  }
9305  std::vector<types::manifold_id> manifold_indicators(m_ids.begin(), m_ids.end());
9306  return manifold_indicators;
9307 }
9308 
9309 /*-----------------------------------------------------------------*/
9310 
9311 
9312 template <int dim, int spacedim>
9313 void
9316 {
9317  Assert ((vertices.size() == 0) &&
9318  (levels.size () == 0) &&
9319  (faces == NULL),
9320  ExcTriangulationNotEmpty(vertices.size(), levels.size()));
9321  Assert ((other_tria.levels.size() != 0) &&
9322  (other_tria.vertices.size() != 0) &&
9323  (dim == 1 || other_tria.faces != NULL),
9324  ExcMessage("When calling Triangulation::copy_triangulation(), "
9325  "the target triangulation must be empty but the source "
9326  "triangulation (the argument to this function) must contain "
9327  "something. Here, it seems like the source does not "
9328  "contain anything at all."));
9329 
9330 
9331  // copy normal elements
9332  vertices = other_tria.vertices;
9333  vertices_used = other_tria.vertices_used;
9334  anisotropic_refinement = other_tria.anisotropic_refinement;
9335  smooth_grid = other_tria.smooth_grid;
9336 
9337  if (dim > 1)
9338  faces.reset (new internal::Triangulation::TriaFaces<dim>(*other_tria.faces));
9339 
9340  typename std::map<types::manifold_id,
9342  bdry_iterator = other_tria.manifold.begin();
9343  for (; bdry_iterator != other_tria.manifold.end() ; ++bdry_iterator)
9344  manifold[bdry_iterator->first] = bdry_iterator->second;
9345 
9346 
9347  levels.reserve (other_tria.levels.size());
9348  for (unsigned int level=0; level<other_tria.levels.size(); ++level)
9349  levels.push_back (new
9351  TriaLevel<dim>(*other_tria.levels[level]));
9352 
9353  number_cache = other_tria.number_cache;
9354 
9355  if (dim == 1)
9356  {
9357  vertex_to_boundary_id_map_1d
9358  .reset(new std::map<unsigned int, types::boundary_id>
9359  (*other_tria.vertex_to_boundary_id_map_1d));
9360 
9361  vertex_to_manifold_id_map_1d
9362  .reset(new std::map<unsigned int, types::manifold_id>
9363  (*other_tria.vertex_to_manifold_id_map_1d));
9364  }
9365 
9366  // inform those who are listening on other_tria of the copy operation
9367  other_tria.signals.copy (*this);
9368  // also inform all listeners of the current triangulation that the
9369  // triangulation has been created
9370  signals.create();
9371 
9372  // note that we need not copy the
9373  // subscriptor!
9374 }
9375 
9376 
9377 
9378 template <int dim, int spacedim>
9379 void
9382  const std::vector<CellData<dim> > &cells,
9383  const SubCellData &subcelldata)
9384 {
9385  std::vector<CellData<dim> > reordered_cells (cells);
9386  SubCellData reordered_subcelldata (subcelldata);
9387 
9388  // in-place reordering of data
9389  reorder_compatibility (reordered_cells, reordered_subcelldata);
9390 
9391  // now create triangulation from
9392  // reordered data
9393  create_triangulation(v, reordered_cells, reordered_subcelldata);
9394 }
9395 
9396 
9397 
9398 template <int dim, int spacedim>
9399 void
9401 create_triangulation (const std::vector<Point<spacedim> > &v,
9402  const std::vector<CellData<dim> > &cells,
9403  const SubCellData &subcelldata)
9404 {
9405  Assert ((vertices.size() == 0) &&
9406  (levels.size () == 0) &&
9407  (faces == NULL),
9408  ExcTriangulationNotEmpty(vertices.size(), levels.size()));
9409  // check that no forbidden arrays
9410  // are used
9411  Assert (subcelldata.check_consistency(dim), ExcInternalError());
9412 
9413  // try to create a triangulation; if this fails, we still want to
9414  // throw an exception but if we just do so we'll get into trouble
9415  // because sometimes other objects are already attached to it:
9416  try
9417  {
9419  }
9420  catch (...)
9421  {
9422  clear_despite_subscriptions();
9423  throw;
9424  }
9425 
9426  // update our counts of the various elements of a triangulation, and set
9427  // active_cell_indices of all cells
9429  ::compute_number_cache (*this, levels.size(), number_cache);
9430  reset_active_cell_indices ();
9431 
9432  // now verify that there are indeed no distorted cells. as per the
9433  // documentation of this class, we first collect all distorted cells
9434  // and then throw an exception if there are any
9435  if (check_for_distorted_cells == true)
9436  {
9437  DistortedCellList distorted_cells = collect_distorted_coarse_cells (*this);
9438  // throw the array (and fill the various location fields) if
9439  // there are distorted cells. otherwise, just fall off the end
9440  // of the function
9441  AssertThrow (distorted_cells.distorted_cells.size() == 0,
9442  distorted_cells);
9443  }
9444 
9445 
9446  /*
9447  When the triangulation is a manifold (dim < spacedim), the normal field
9448  provided from the map class depends on the order of the vertices.
9449  It may happen that this normal field is discontinous.
9450  The following code takes care that this is not the case by setting the
9451  cell direction flag on those cell that produce the wrong orientation.
9452 
9453  To determine if 2 neighbours have the same or opposite orientation
9454  we use a table of truth.
9455  Its entries are indexes by the local indeces of the common face.
9456  For example if two elements share a face, and this face is
9457  face 0 for element 0 and face 1 for element 1, then
9458  table(0,1) will tell whether the orientation are the same (true) or
9459  opposite (false).
9460 
9461  Even though there may be a combinatorial/graph theory argument to get
9462  this table in any dimension, I tested by hand all the different possible
9463  cases in 1D and 2D to generate the table.
9464 
9465  Assuming that a surface respects the standard orientation for 2d meshes,
9466  the tables of truth are symmetric and their true values are the following
9467  1D curves: (0,1)
9468  2D surface: (0,1),(0,2),(1,3),(2,3)
9469 
9470  We store this data using an n_faces x n_faces full matrix, which is actually
9471  much bigger than the minimal data required, but it makes the code more readable.
9472 
9473  */
9474  if (dim < spacedim)
9475  {
9478  switch (dim)
9479  {
9480  case 1:
9481  {
9482  bool values [][2] = {{false,true},
9483  {true,false}
9484  };
9485  for (unsigned int i=0; i< GeometryInfo< dim >::faces_per_cell; ++i)
9486  for (unsigned int j=0; j< GeometryInfo< dim >::faces_per_cell; ++j)
9487  correct(i,j) = ( values[i][j]);
9488  break;
9489  }
9490  case 2:
9491  {
9492  bool values [][4]= {{false,true ,true , false},
9493  {true ,false,false, true },
9494  {true ,false,false, true },
9495  {false,true ,true , false}
9496  };
9497  for (unsigned int i=0; i< GeometryInfo< dim >::faces_per_cell; ++i)
9498  for (unsigned int j=0; j< GeometryInfo< dim >::faces_per_cell; ++j)
9499  correct(i,j) = ( values[i][j]);
9500  break;
9501  }
9502  default:
9503  Assert (false, ExcNotImplemented());
9504  }
9505 
9506 
9507  std::list<active_cell_iterator> this_round, next_round;
9508  active_cell_iterator neighbor;
9509 
9510  this_round.push_back (begin_active());
9511  begin_active()->set_direction_flag (true);
9512  begin_active()->set_user_flag ();
9513 
9514  while (this_round.size() > 0)
9515  {
9516  for ( typename std::list<active_cell_iterator>::iterator cell = this_round.begin();
9517  cell != this_round.end(); ++cell)
9518  {
9519  for (unsigned int i = 0; i < GeometryInfo< dim >::faces_per_cell; ++i)
9520  {
9521  if ( !((*cell)->face(i)->at_boundary()) )
9522  {
9523  neighbor = (*cell)->neighbor(i);
9524 
9525  unsigned int cf = (*cell)->face_index(i);
9526  unsigned int j = 0;
9527  while (neighbor->face_index(j) != cf)
9528  {
9529  ++j;
9530  }
9531 
9532  if ( (correct(i,j) && !(*cell)->direction_flag())
9533  ||
9534  (!correct(i,j) && (*cell)->direction_flag()) )
9535  {
9536  if (neighbor->user_flag_set() == false)
9537  {
9538  neighbor->set_direction_flag (false);
9539  neighbor->set_user_flag ();
9540  next_round.push_back (neighbor);
9541  }
9542  else
9543  Assert (neighbor->direction_flag() == false,
9544  ExcNonOrientableTriangulation());
9545 
9546  }
9547  }
9548  }
9549  }
9550 
9551  // Before we quit let's check
9552  // that if the triangulation
9553  // is disconnected that we
9554  // still get all cells
9555  if (next_round.size() == 0)
9556  for (active_cell_iterator cell = begin_active();
9557  cell != end(); ++cell)
9558  if (cell->user_flag_set() == false)
9559  {
9560  next_round.push_back (cell);
9561  cell->set_direction_flag (true);
9562  cell->set_user_flag ();
9563  break;
9564  }
9565 
9566  this_round = next_round;
9567  next_round.clear();
9568  }
9569  }
9570 
9571  // inform all listeners that the triangulation has been created
9572  signals.create();
9573 }
9574 
9575 
9576 
9577 
9578 template <int dim, int spacedim>
9579 void
9582 {
9583  AssertThrow (dim+1 == spacedim, ExcMessage ("Only works for dim == spacedim-1"));
9584  for (active_cell_iterator cell = begin_active();
9585  cell != end(); ++cell)
9586  cell->set_direction_flag (!cell->direction_flag());
9587 }
9588 
9589 
9590 
9591 template <int dim, int spacedim>
9593 {
9594  Assert(n_cells()>0, ExcMessage("Error: An empty Triangulation can not be refined."));
9595  active_cell_iterator cell = begin_active(),
9596  endc = end();
9597 
9598  for (; cell != endc; ++cell)
9599  {
9600  cell->clear_coarsen_flag();
9601  cell->set_refine_flag ();
9602  }
9603 }
9604 
9605 
9606 
9607 template <int dim, int spacedim>
9608 void Triangulation<dim, spacedim>::refine_global (const unsigned int times)
9609 {
9610  for (unsigned int i=0; i<times; ++i)
9611  {
9612  set_all_refine_flags();
9613  execute_coarsening_and_refinement ();
9614  }
9615 }
9616 
9617 
9618 
9619 /*-------------------- refine/coarsen flags -------------------------*/
9620 
9621 
9622 
9623 template <int dim, int spacedim>
9624 void Triangulation<dim, spacedim>::save_refine_flags (std::vector<bool> &v) const
9625 {
9626  v.resize (dim*n_active_cells(), false);
9627  std::vector<bool>::iterator i = v.begin();
9628  active_cell_iterator cell = begin_active(),
9629  endc = end();
9630  for (; cell!=endc; ++cell)
9631  for (unsigned int j=0; j<dim; ++j,++i)
9632  if (cell->refine_flag_set() & (1<<j) )
9633  *i = true;
9634 
9635  Assert (i == v.end(), ExcInternalError());
9636 }
9637 
9638 
9639 
9640 template <int dim, int spacedim>
9642 {
9643  std::vector<bool> v;
9644  save_refine_flags (v);
9645  write_bool_vector (mn_tria_refine_flags_begin, v, mn_tria_refine_flags_end,
9646  out);
9647 }
9648 
9649 
9650 
9651 template <int dim, int spacedim>
9653 {
9654  std::vector<bool> v;
9655  read_bool_vector (mn_tria_refine_flags_begin, v, mn_tria_refine_flags_end,
9656  in);
9657  load_refine_flags (v);
9658 }
9659 
9660 
9661 
9662 template <int dim, int spacedim>
9663 void Triangulation<dim, spacedim>::load_refine_flags (const std::vector<bool> &v)
9664 {
9665  AssertThrow (v.size() == dim*n_active_cells(), ExcGridReadError());
9666 
9667  active_cell_iterator cell = begin_active(),
9668  endc = end();
9669  std::vector<bool>::const_iterator i = v.begin();
9670  for (; cell!=endc; ++cell)
9671  {
9672  unsigned int ref_case=0;
9673 
9674  for (unsigned int j=0; j<dim; ++j, ++i)
9675  if (*i == true)
9676  ref_case+=1<<j;
9678  ExcGridReadError());
9679  if (ref_case>0)
9680  cell->set_refine_flag(RefinementCase<dim>(ref_case));
9681  else
9682  cell->clear_refine_flag();
9683  }
9684 
9685  Assert (i == v.end(), ExcInternalError());
9686 }
9687 
9688 
9689 
9690 template <int dim, int spacedim>
9691 void Triangulation<dim, spacedim>::save_coarsen_flags (std::vector<bool> &v) const
9692 {
9693  v.resize (n_active_cells(), false);
9694  std::vector<bool>::iterator i = v.begin();
9695  active_cell_iterator cell = begin_active(),
9696  endc = end();
9697  for (; cell!=endc; ++cell, ++i)
9698  *i = cell->coarsen_flag_set();
9699 
9700  Assert (i == v.end(), ExcInternalError());
9701 }
9702 
9703 
9704 
9705 template <int dim, int spacedim>
9707 {
9708  std::vector<bool> v;
9709  save_coarsen_flags (v);
9710  write_bool_vector (mn_tria_coarsen_flags_begin, v, mn_tria_coarsen_flags_end,
9711  out);
9712 }
9713 
9714 
9715 
9716 template <int dim, int spacedim>
9718 {
9719  std::vector<bool> v;
9720  read_bool_vector (mn_tria_coarsen_flags_begin, v, mn_tria_coarsen_flags_end,
9721  in);
9722  load_coarsen_flags (v);
9723 }
9724 
9725 
9726 
9727 template <int dim, int spacedim>
9728 void Triangulation<dim, spacedim>::load_coarsen_flags (const std::vector<bool> &v)
9729 {
9730  Assert (v.size() == n_active_cells(), ExcGridReadError());
9731 
9732  active_cell_iterator cell = begin_active(),
9733  endc = end();
9734  std::vector<bool>::const_iterator i = v.begin();
9735  for (; cell!=endc; ++cell, ++i)
9736  if (*i == true)
9737  cell->set_coarsen_flag();
9738  else
9739  cell->clear_coarsen_flag();
9740 
9741  Assert (i == v.end(), ExcInternalError());
9742 }
9743 
9744 
9745 template <int dim, int spacedim>
9747 {
9748  return anisotropic_refinement;
9749 }
9750 
9751 
9752 
9753 /*-------------------- user data/flags -------------------------*/
9754 
9755 
9756 namespace
9757 {
9758  // clear user data of cells
9759  template <int dim>
9760  void clear_user_data (std::vector<internal::Triangulation::TriaLevel<dim>*> &levels)
9761  {
9762  for (unsigned int level=0; level<levels.size(); ++level)
9763  levels[level]->cells.clear_user_data();
9764  }
9765 
9766 
9767  // clear user data of faces
9768  void clear_user_data (internal::Triangulation::TriaFaces<1> *)
9769  {
9770  // nothing to do in 1d
9771  }
9772 
9773 
9774  void clear_user_data (internal::Triangulation::TriaFaces<2> *faces)
9775  {
9776  faces->lines.clear_user_data();
9777  }
9778 
9779 
9780  void clear_user_data (internal::Triangulation::TriaFaces<3> *faces)
9781  {
9782  faces->lines.clear_user_data();
9783  faces->quads.clear_user_data();
9784  }
9785 }
9786 
9787 
9788 template <int dim, int spacedim>
9790 {
9791  // let functions in anonymous namespace do their work
9792  ::clear_user_data (levels);
9793  ::clear_user_data (faces.get());
9794 }
9795 
9796 
9797 
9798 namespace
9799 {
9800  void clear_user_flags_line (std::vector<internal::Triangulation::TriaLevel<1>*> &levels,
9802  {
9803  for (unsigned int level=0; level<levels.size(); ++level)
9804  levels[level]->cells.clear_user_flags();
9805  }
9806 
9807  template <int dim>
9808  void clear_user_flags_line (std::vector<internal::Triangulation::TriaLevel<dim>*> &,
9810  {
9811  faces->lines.clear_user_flags();
9812  }
9813 }
9814 
9815 
9816 template <int dim, int spacedim>
9818 {
9819  ::clear_user_flags_line (levels, faces.get());
9820 }
9821 
9822 
9823 
9824 namespace
9825 {
9826  void clear_user_flags_quad (std::vector<internal::Triangulation::TriaLevel<1>*> &,
9828  {
9829  // nothing to do in 1d
9830  }
9831 
9832  void clear_user_flags_quad (std::vector<internal::Triangulation::TriaLevel<2>*> &levels,
9834  {
9835  for (unsigned int level=0; level<levels.size(); ++level)
9836  levels[level]->cells.clear_user_flags();
9837  }
9838 
9839  template <int dim>
9840  void clear_user_flags_quad (std::vector<internal::Triangulation::TriaLevel<dim>*> &,
9842  {
9843  faces->quads.clear_user_flags();
9844  }
9845 }
9846 
9847 
9848 template <int dim, int spacedim>
9850 {
9851  ::clear_user_flags_quad (levels, faces.get());
9852 }
9853 
9854 
9855 
9856 namespace
9857 {
9858  void clear_user_flags_hex (std::vector<internal::Triangulation::TriaLevel<1>*> &,
9860  {
9861  // nothing to do in 1d
9862  }
9863 
9864 
9865  void clear_user_flags_hex (std::vector<internal::Triangulation::TriaLevel<2>*> &,
9867  {
9868  // nothing to do in 2d
9869  }
9870 
9871  void clear_user_flags_hex (std::vector<internal::Triangulation::TriaLevel<3>*> &levels,
9873  {
9874  for (unsigned int level=0; level<levels.size(); ++level)
9875  levels[level]->cells.clear_user_flags();
9876  }
9877 }
9878 
9879 
9880 template <int dim, int spacedim>
9882 {
9883  ::clear_user_flags_hex (levels, faces.get());
9884 }
9885 
9886 
9887 
9888 template <int dim, int spacedim>
9890 {
9891  clear_user_flags_line ();
9892  clear_user_flags_quad ();
9893  clear_user_flags_hex ();
9894 }
9895 
9896 
9897 
9898 template <int dim, int spacedim>
9900 {
9901  save_user_flags_line (out);
9902 
9903  if (dim>=2)
9904  save_user_flags_quad (out);
9905 
9906  if (dim>=3)
9907  save_user_flags_hex (out);
9908 
9909  if (dim >= 4)
9910  Assert (false, ExcNotImplemented());
9911 }
9912 
9913 
9914 
9915 template <int dim, int spacedim>
9916 void Triangulation<dim, spacedim>::save_user_flags (std::vector<bool> &v) const
9917 {
9918  // clear vector and append
9919  // all the stuff later on
9920  v.clear ();
9921 
9922  std::vector<bool> tmp;
9923 
9924  save_user_flags_line (tmp);
9925  v.insert (v.end(), tmp.begin(), tmp.end());
9926 
9927  if (dim >= 2)
9928  {
9929  save_user_flags_quad (tmp);
9930  v.insert (v.end(), tmp.begin(), tmp.end());
9931  }
9932 
9933  if (dim >= 3)
9934  {
9935  save_user_flags_hex (tmp);
9936  v.insert (v.end(), tmp.begin(), tmp.end());
9937  }
9938 
9939  if (dim >= 4)
9940  Assert (false, ExcNotImplemented());
9941 }
9942 
9943 
9944 
9945 template <int dim, int spacedim>
9947 {
9948  load_user_flags_line (in);
9949 
9950  if (dim>=2)
9951  load_user_flags_quad (in);
9952 
9953  if (dim>=3)
9954  load_user_flags_hex (in);
9955 
9956  if (dim >= 4)
9957  Assert (false, ExcNotImplemented());
9958 }
9959 
9960 
9961 
9962 template <int dim, int spacedim>
9963 void Triangulation<dim, spacedim>::load_user_flags (const std::vector<bool> &v)
9964 {
9965  Assert (v.size() == n_lines()+n_quads()+n_hexs(), ExcInternalError());
9966  std::vector<bool> tmp;
9967 
9968  // first extract the flags
9969  // belonging to lines
9970  tmp.insert (tmp.end(),
9971  v.begin(), v.begin()+n_lines());
9972  // and set the lines
9973  load_user_flags_line (tmp);
9974 
9975  if (dim >= 2)
9976  {
9977  tmp.clear ();
9978  tmp.insert (tmp.end(),
9979  v.begin()+n_lines(), v.begin()+n_lines()+n_quads());
9980  load_user_flags_quad (tmp);
9981  }
9982 
9983  if (dim >= 3)
9984  {
9985  tmp.clear();
9986  tmp.insert (tmp.end(),
9987  v.begin()+n_lines()+n_quads(), v.begin()+n_lines()+n_quads()+n_hexs());
9988  load_user_flags_hex (tmp);
9989  }
9990 
9991  if (dim >= 4)
9992  Assert (false, ExcNotImplemented());
9993 }
9994 
9995 
9996 
9997 template <int dim, int spacedim>
9999 {
10000  v.resize (n_lines(), false);
10001  std::vector<bool>::iterator i = v.begin();
10002  line_iterator line = begin_line(),
10003  endl = end_line();
10004  for (; line!=endl; ++line, ++i)
10005  *i = line->user_flag_set();
10006 
10007  Assert (i == v.end(), ExcInternalError());
10008 }
10009 
10010 
10011 
10012 template <int dim, int spacedim>
10014 {
10015  std::vector<bool> v;
10016  save_user_flags_line (v);
10017  write_bool_vector (mn_tria_line_user_flags_begin, v, mn_tria_line_user_flags_end,
10018  out);
10019 }
10020 
10021 
10022 
10023 template <int dim, int spacedim>
10025 {
10026  std::vector<bool> v;
10027  read_bool_vector (mn_tria_line_user_flags_begin, v, mn_tria_line_user_flags_end,
10028  in);
10029  load_user_flags_line (v);
10030 }
10031 
10032 
10033 
10034 template <int dim, int spacedim>
10036 {
10037  Assert (v.size() == n_lines(), ExcGridReadError());
10038 
10039  line_iterator line = begin_line(),
10040  endl = end_line();
10041  std::vector<bool>::const_iterator i = v.begin();
10042  for (; line!=endl; ++line, ++i)
10043  if (*i == true)
10044  line->set_user_flag();
10045  else
10046  line->clear_user_flag();
10047 
10048  Assert (i == v.end(), ExcInternalError());
10049 }
10050 
10051 
10052 namespace
10053 {
10054  template <typename Iterator>
10055  bool get_user_flag (const Iterator &i)
10056  {
10057  return i->user_flag_set();
10058  }
10059 
10060 
10061 
10062  template <int structdim, int dim, int spacedim>
10063  bool get_user_flag (const TriaIterator<InvalidAccessor<structdim,dim,spacedim> > &)
10064  {
10065  Assert (false, ExcInternalError());
10066  return false;
10067  }
10068 
10069 
10070 
10071  template <typename Iterator>
10072  void set_user_flag (const Iterator &i)
10073  {
10074  i->set_user_flag();
10075  }
10076 
10077 
10078 
10079  template <int structdim, int dim, int spacedim>
10080  void set_user_flag (const TriaIterator<InvalidAccessor<structdim,dim,spacedim> > &)
10081  {
10082  Assert (false, ExcInternalError());
10083  }
10084 
10085 
10086 
10087  template <typename Iterator>
10088  void clear_user_flag (const Iterator &i)
10089  {
10090  i->clear_user_flag();
10091  }
10092 
10093 
10094 
10095  template <int structdim, int dim, int spacedim>
10096  void clear_user_flag (const TriaIterator<InvalidAccessor<structdim,dim,spacedim> > &)
10097  {
10098  Assert (false, ExcInternalError());
10099  }
10100 }
10101 
10102 
10103 template <int dim, int spacedim>
10105 {
10106  v.resize (n_quads(), false);
10107 
10108  if (dim >= 2)
10109  {
10110  std::vector<bool>::iterator i = v.begin();
10111  quad_iterator quad = begin_quad(),
10112  endq = end_quad();
10113  for (; quad!=endq; ++quad, ++i)
10114  *i = get_user_flag (quad);
10115 
10116  Assert (i == v.end(), ExcInternalError());
10117  }
10118 }
10119 
10120 
10121 
10122 template <int dim, int spacedim>
10124 {
10125  std::vector<bool> v;
10126  save_user_flags_quad (v);
10127  write_bool_vector (mn_tria_quad_user_flags_begin, v, mn_tria_quad_user_flags_end,
10128  out);
10129 }
10130 
10131 
10132 
10133 template <int dim, int spacedim>
10135 {
10136  std::vector<bool> v;
10137  read_bool_vector (mn_tria_quad_user_flags_begin, v, mn_tria_quad_user_flags_end,
10138  in);
10139  load_user_flags_quad (v);
10140 }
10141 
10142 
10143 
10144 template <int dim, int spacedim>
10146 {
10147  Assert (v.size() == n_quads(), ExcGridReadError());
10148 
10149  if (dim >= 2)
10150  {
10151  quad_iterator quad = begin_quad(),
10152  endq = end_quad();
10153  std::vector<bool>::const_iterator i = v.begin();
10154  for (; quad!=endq; ++quad, ++i)
10155  if (*i == true)
10156  set_user_flag(quad);
10157  else
10158  clear_user_flag(quad);
10159 
10160  Assert (i == v.end(), ExcInternalError());
10161  }
10162 }
10163 
10164 
10165 
10166 template <int dim, int spacedim>
10167 void Triangulation<dim, spacedim>::save_user_flags_hex (std::vector<bool> &v) const
10168 {
10169  v.resize (n_hexs(), false);
10170 
10171  if (dim >= 3)
10172  {
10173  std::vector<bool>::iterator i = v.begin();
10174  hex_iterator hex = begin_hex(),
10175  endh = end_hex();
10176  for (; hex!=endh; ++hex, ++i)
10177  *i = get_user_flag (hex);
10178 
10179  Assert (i == v.end(), ExcInternalError());
10180  }
10181 }
10182 
10183 
10184 
10185 template <int dim, int spacedim>
10187 {
10188  std::vector<bool> v;
10189  save_user_flags_hex (v);
10190  write_bool_vector (mn_tria_hex_user_flags_begin, v, mn_tria_hex_user_flags_end,
10191  out);
10192 }
10193 
10194 
10195 
10196 template <int dim, int spacedim>
10198 {
10199  std::vector<bool> v;
10200  read_bool_vector (mn_tria_hex_user_flags_begin, v, mn_tria_hex_user_flags_end,
10201  in);
10202  load_user_flags_hex (v);
10203 }
10204 
10205 
10206 
10207 template <int dim, int spacedim>
10208 void Triangulation<dim, spacedim>::load_user_flags_hex (const std::vector<bool> &v)
10209 {
10210  Assert (v.size() == n_hexs(), ExcGridReadError());
10211 
10212  if (dim >= 3)
10213  {
10214  hex_iterator hex = begin_hex(),
10215  endh = end_hex();
10216  std::vector<bool>::const_iterator i = v.begin();
10217  for (; hex!=endh; ++hex, ++i)
10218  if (*i == true)
10219  set_user_flag(hex);
10220  else
10221  clear_user_flag(hex);
10222 
10223  Assert (i == v.end(), ExcInternalError());
10224  }
10225 }
10226 
10227 
10228 
10229 template <int dim, int spacedim>
10230 void Triangulation<dim, spacedim>::save_user_indices (std::vector<unsigned int> &v) const
10231 {
10232  // clear vector and append all the
10233  // stuff later on
10234  v.clear ();
10235 
10236  std::vector<unsigned int> tmp;
10237 
10238  save_user_indices_line (tmp);
10239  v.insert (v.end(), tmp.begin(), tmp.end());
10240 
10241  if (dim >= 2)
10242  {
10243  save_user_indices_quad (tmp);
10244  v.insert (v.end(), tmp.begin(), tmp.end());
10245  }
10246 
10247  if (dim >= 3)
10248  {
10249  save_user_indices_hex (tmp);
10250  v.insert (v.end(), tmp.begin(), tmp.end());
10251  }
10252 
10253  if (dim >= 4)
10254  Assert (false, ExcNotImplemented());
10255 }
10256 
10257 
10258 
10259 template <int dim, int spacedim>
10260 void Triangulation<dim, spacedim>::load_user_indices (const std::vector<unsigned int> &v)
10261 {
10262  Assert (v.size() == n_lines()+n_quads()+n_hexs(), ExcInternalError());
10263  std::vector<unsigned int> tmp;
10264 
10265  // first extract the indices
10266  // belonging to lines
10267  tmp.insert (tmp.end(),
10268  v.begin(), v.begin()+n_lines());
10269  // and set the lines
10270  load_user_indices_line (tmp);
10271 
10272  if (dim >= 2)
10273  {
10274  tmp.clear ();
10275  tmp.insert (tmp.end(),
10276  v.begin()+n_lines(), v.begin()+n_lines()+n_quads());
10277  load_user_indices_quad (tmp);
10278  }
10279 
10280  if (dim >= 3)
10281  {
10282  tmp.clear ();
10283  tmp.insert (tmp.end(),
10284  v.begin()+n_lines()+n_quads(), v.begin()+n_lines()+n_quads()+n_hexs());
10285  load_user_indices_hex (tmp);
10286  }
10287 
10288  if (dim >= 4)
10289  Assert (false, ExcNotImplemented());
10290 }
10291 
10292 
10293 
10294 namespace
10295 {
10296  template <typename Iterator>
10297  unsigned int get_user_index (const Iterator &i)
10298  {
10299  return i->user_index();
10300  }
10301 
10302 
10303 
10304  template <int structdim, int dim, int spacedim>
10305  unsigned int get_user_index (const TriaIterator<InvalidAccessor<structdim,dim,spacedim> > &)
10306  {
10307  Assert (false, ExcInternalError());
10309  }
10310 
10311 
10312 
10313  template <typename Iterator>
10314  void set_user_index (const Iterator &i,
10315  const unsigned int x)
10316  {
10317  i->set_user_index(x);
10318  }
10319 
10320 
10321 
10322  template <int structdim, int dim, int spacedim>
10323  void set_user_index (const TriaIterator<InvalidAccessor<structdim,dim,spacedim> > &,
10324  const unsigned int)
10325  {
10326  Assert (false, ExcInternalError());
10327  }
10328 }
10329 
10330 
10331 template <int dim, int spacedim>
10332 void Triangulation<dim, spacedim>::save_user_indices_line (std::vector<unsigned int> &v) const
10333 {
10334  v.resize (n_lines(), 0);
10335  std::vector<unsigned int>::iterator i = v.begin();
10336  line_iterator line = begin_line(),
10337  endl = end_line();
10338  for (; line!=endl; ++line, ++i)
10339  *i = line->user_index();
10340 }
10341 
10342 
10343 
10344 template <int dim, int spacedim>
10345 void Triangulation<dim, spacedim>::load_user_indices_line (const std::vector<unsigned int> &v)
10346 {
10347  Assert (v.size() == n_lines(), ExcGridReadError());
10348 
10349  line_iterator line = begin_line(),
10350  endl = end_line();
10351  std::vector<unsigned int>::const_iterator i = v.begin();
10352  for (; line!=endl; ++line, ++i)
10353  line->set_user_index(*i);
10354 }
10355 
10356 
10357 template <int dim, int spacedim>
10358 void Triangulation<dim, spacedim>::save_user_indices_quad (std::vector<unsigned int> &v) const
10359 {
10360  v.resize (n_quads(), 0);
10361 
10362  if (dim >= 2)
10363  {
10364  std::vector<unsigned int>::iterator i = v.begin();
10365  quad_iterator quad = begin_quad(),
10366  endq = end_quad();
10367  for (; quad!=endq; ++quad, ++i)
10368  *i = get_user_index(quad);
10369  }
10370 }
10371 
10372 
10373 
10374 template <int dim, int spacedim>
10375 void Triangulation<dim, spacedim>::load_user_indices_quad (const std::vector<unsigned int> &v)
10376 {
10377  Assert (v.size() == n_quads(), ExcGridReadError());
10378 
10379  if (dim >= 2)
10380  {
10381  quad_iterator quad = begin_quad(),
10382  endq = end_quad();
10383  std::vector<unsigned int>::const_iterator i = v.begin();
10384  for (; quad!=endq; ++quad, ++i)
10385  set_user_index(quad, *i);
10386  }
10387 }
10388 
10389 
10390 template <int dim, int spacedim>
10391 void Triangulation<dim, spacedim>::save_user_indices_hex (std::vector<unsigned int> &v) const
10392 {
10393  v.resize (n_hexs(), 0);
10394 
10395  if (dim >= 3)
10396  {
10397  std::vector<unsigned int>::iterator i = v.begin();
10398  hex_iterator hex = begin_hex(),
10399  endh = end_hex();
10400  for (; hex!=endh; ++hex, ++i)
10401  *i = get_user_index(hex);
10402  }
10403 }
10404 
10405 
10406 
10407 template <int dim, int spacedim>
10408 void Triangulation<dim, spacedim>::load_user_indices_hex (const std::vector<unsigned int> &v)
10409 {
10410  Assert (v.size() == n_hexs(), ExcGridReadError());
10411 
10412  if (dim >= 3)
10413  {
10414  hex_iterator hex = begin_hex(),
10415  endh = end_hex();
10416  std::vector<unsigned int>::const_iterator i = v.begin();
10417  for (; hex!=endh; ++hex, ++i)
10418  set_user_index(hex, *i);
10419  }
10420 }
10421 
10422 
10423 
10424 //---------------- user pointers ----------------------------------------//
10425 
10426 
10427 namespace
10428 {
10429  template <typename Iterator>
10430  void *get_user_pointer (const Iterator &i)
10431  {
10432  return i->user_pointer();
10433  }
10434 
10435 
10436 
10437  template <int structdim, int dim, int spacedim>
10438  void *get_user_pointer (const TriaIterator<InvalidAccessor<structdim,dim,spacedim> > &)
10439  {
10440  Assert (false, ExcInternalError());
10441  return 0;
10442  }
10443 
10444 
10445 
10446  template <typename Iterator>
10447  void set_user_pointer (const Iterator &i,
10448  void *x)
10449  {
10450  i->set_user_pointer(x);
10451  }
10452 
10453 
10454 
10455  template <int structdim, int dim, int spacedim>
10456  void set_user_pointer (const TriaIterator<InvalidAccessor<structdim,dim,spacedim> > &,
10457  void *)
10458  {
10459  Assert (false, ExcInternalError());
10460  }
10461 }
10462 
10463 
10464 template <int dim, int spacedim>
10465 void Triangulation<dim, spacedim>::save_user_pointers (std::vector<void *> &v) const
10466 {
10467  // clear vector and append all the
10468  // stuff later on
10469  v.clear ();
10470 
10471  std::vector<void *> tmp;
10472 
10473  save_user_pointers_line (tmp);
10474  v.insert (v.end(), tmp.begin(), tmp.end());
10475 
10476  if (dim >= 2)
10477  {
10478  save_user_pointers_quad (tmp);
10479  v.insert (v.end(), tmp.begin(), tmp.end());
10480  }
10481 
10482  if (dim >= 3)
10483  {
10484  save_user_pointers_hex (tmp);
10485  v.insert (v.end(), tmp.begin(), tmp.end());
10486  }
10487 
10488  if (dim >= 4)
10489  Assert (false, ExcNotImplemented());
10490 }
10491 
10492 
10493 
10494 template <int dim, int spacedim>
10495 void Triangulation<dim, spacedim>::load_user_pointers (const std::vector<void *> &v)
10496 {
10497  Assert (v.size() == n_lines()+n_quads()+n_hexs(), ExcInternalError());
10498  std::vector<void *> tmp;
10499 
10500  // first extract the pointers
10501  // belonging to lines
10502  tmp.insert (tmp.end(),
10503  v.begin(), v.begin()+n_lines());
10504  // and set the lines
10505  load_user_pointers_line (tmp);
10506 
10507  if (dim >= 2)
10508  {
10509  tmp.clear ();
10510  tmp.insert (tmp.end(),
10511  v.begin()+n_lines(), v.begin()+n_lines()+n_quads());
10512  load_user_pointers_quad (tmp);
10513  }
10514 
10515  if (dim >= 3)
10516  {
10517  tmp.clear ();
10518  tmp.insert (tmp.end(),
10519  v.begin()+n_lines()+n_quads(), v.begin()+n_lines()+n_quads()+n_hexs());
10520  load_user_pointers_hex (tmp);
10521  }
10522 
10523  if (dim >= 4)
10524  Assert (false, ExcNotImplemented());
10525 }
10526 
10527 
10528 
10529 template <int dim, int spacedim>
10531 {
10532  v.resize (n_lines(), 0);
10533  std::vector<void *>::iterator i = v.begin();
10534  line_iterator line = begin_line(),
10535  endl = end_line();
10536  for (; line!=endl; ++line, ++i)
10537  *i = line->user_pointer();
10538 }
10539 
10540 
10541 
10542 template <int dim, int spacedim>
10544 {
10545  Assert (v.size() == n_lines(), ExcGridReadError());
10546 
10547  line_iterator line = begin_line(),
10548  endl = end_line();
10549  std::vector<void *>::const_iterator i = v.begin();
10550  for (; line!=endl; ++line, ++i)
10551  line->set_user_pointer(*i);
10552 }
10553 
10554 
10555 
10556 template <int dim, int spacedim>
10558 {
10559  v.resize (n_quads(), 0);
10560 
10561  if (dim >= 2)
10562  {
10563  std::vector<void *>::iterator i = v.begin();
10564  quad_iterator quad = begin_quad(),
10565  endq = end_quad();
10566  for (; quad!=endq; ++quad, ++i)
10567  *i = get_user_pointer(quad);
10568  }
10569 }
10570 
10571 
10572 
10573 template <int dim, int spacedim>
10575 {
10576  Assert (v.size() == n_quads(), ExcGridReadError());
10577 
10578  if (dim >= 2)
10579  {
10580  quad_iterator quad = begin_quad(),
10581  endq = end_quad();
10582  std::vector<void *>::const_iterator i = v.begin();
10583  for (; quad!=endq; ++quad, ++i)
10584  set_user_pointer(quad, *i);
10585  }
10586 }
10587 
10588 
10589 template <int dim, int spacedim>
10590 void Triangulation<dim, spacedim>::save_user_pointers_hex (std::vector<void *> &v) const
10591 {
10592  v.resize (n_hexs(), 0);
10593 
10594  if (dim >= 3)
10595  {
10596  std::vector<void *>::iterator i = v.begin();
10597  hex_iterator hex = begin_hex(),
10598  endh = end_hex();
10599  for (; hex!=endh; ++hex, ++i)
10600  *i = get_user_pointer(hex);
10601  }
10602 }
10603 
10604 
10605 
10606 template <int dim, int spacedim>
10607 void Triangulation<dim, spacedim>::load_user_pointers_hex (const std::vector<void *> &v)
10608 {
10609  Assert (v.size() == n_hexs(), ExcGridReadError());
10610 
10611  if (dim >= 3)
10612  {
10613  hex_iterator hex = begin_hex(),
10614  endh = end_hex();
10615  std::vector<void *>::const_iterator i = v.begin();
10616  for (; hex!=endh; ++hex, ++i)
10617  set_user_pointer(hex, *i);
10618  }
10619 }
10620 
10621 
10622 
10623 /*------------------------ Cell iterator functions ------------------------*/
10624 
10625 
10626 template <int dim, int spacedim>
10628 Triangulation<dim,spacedim>::begin_raw (const unsigned int level) const
10629 {
10630  switch (dim)
10631  {
10632  case 1:
10633  return begin_raw_line (level);
10634  case 2:
10635  return begin_raw_quad (level);
10636  case 3:
10637  return begin_raw_hex (level);
10638  default:
10639  Assert (false, ExcNotImplemented());
10640  return raw_cell_iterator();
10641  }
10642 }
10643 
10644 
10645 
10646 template <int dim, int spacedim>
10648 Triangulation<dim,spacedim>::begin (const unsigned int level) const
10649 {
10650  switch (dim)
10651  {
10652  case 1:
10653  return begin_line (level);
10654  case 2:
10655  return begin_quad (level);
10656  case 3:
10657  return begin_hex (level);
10658  default:
10659  Assert (false, ExcImpossibleInDim(dim));
10660  return cell_iterator();
10661  }
10662 }
10663 
10664 
10665 
10666 template <int dim, int spacedim>
10668 Triangulation<dim,spacedim>::begin_active (const unsigned int level) const
10669 {
10670  switch (dim)
10671  {
10672  case 1:
10673  return begin_active_line (level);
10674  case 2:
10675  return begin_active_quad (level);
10676  case 3:
10677  return begin_active_hex (level);
10678  default:
10679  Assert (false, ExcNotImplemented());
10680  return active_cell_iterator();
10681  }
10682 }
10683 
10684 
10685 
10686 template <int dim, int spacedim>
10689 {
10690  const unsigned int level = levels.size()-1;
10691 
10692  Assert (level<n_global_levels() || level<levels.size(), ExcInvalidLevel(level));
10693  if (levels[level]->cells.cells.size() ==0)
10694  return end(level);
10695 
10696  // find the last raw iterator on
10697  // this level
10698  raw_cell_iterator ri (const_cast<Triangulation<dim,spacedim>*>(this),
10699  level,
10700  levels[level]->cells.cells.size()-1);
10701 
10702  // then move to the last used one
10703  if (ri->used()==true)
10704  return ri;
10705  while ((--ri).state() == IteratorState::valid)
10706  if (ri->used()==true)
10707  return ri;
10708  return ri;
10709 }
10710 
10711 
10712 
10713 template <int dim, int spacedim>
10716 {
10717  // get the last used cell
10718  cell_iterator cell = last();
10719 
10720  if (cell != end())
10721  {
10722  // then move to the last active one
10723  if (cell->active()==true)
10724  return cell;
10725  while ((--cell).state() == IteratorState::valid)
10726  if (cell->active()==true)
10727  return cell;
10728  }
10729  return cell;
10730 }
10731 
10732 
10733 
10734 template <int dim, int spacedim>
10737 {
10738  return cell_iterator (const_cast<Triangulation<dim, spacedim>*>(this),
10739  -1,
10740  -1);
10741 }
10742 
10743 
10744 
10745 template <int dim, int spacedim>
10747 Triangulation<dim, spacedim>::end_raw (const unsigned int level) const
10748 {
10749  Assert (level<n_global_levels(), ExcInvalidLevel(level));
10750  if (level < levels.size()-1)
10751  return begin_raw (level+1);
10752  else
10753  return end();
10754 }
10755 
10756 
10757 template <int dim, int spacedim>
10759 Triangulation<dim, spacedim>::end (const unsigned int level) const
10760 {
10761  if (level < levels.size()-1)
10762  return begin (level+1);
10763  Assert (level<n_global_levels() || level<levels.size(), ExcInvalidLevel(level));
10764  return end();
10765 }
10766 
10767 
10768 template <int dim, int spacedim>
10770 Triangulation<dim, spacedim>::end_active (const unsigned int level) const
10771 {
10772  Assert (level<n_global_levels() || level < levels.size(), ExcInvalidLevel(level));
10773  return (level >= levels.size()-1 ?
10774  active_cell_iterator(end()) :
10775  begin_active (level+1));
10776 }
10777 
10778 
10779 
10780 template <int dim, int spacedim>
10783 {
10784  return
10786  (begin(), end());
10787 }
10788 
10789 
10790 template <int dim, int spacedim>
10793 {
10794  return
10796  (begin_active(), end());
10797 }
10798 
10799 
10800 
10801 template <int dim, int spacedim>
10804 {
10805  return
10807  (begin(level), end(level));
10808 }
10809 
10810 
10811 
10812 template <int dim, int spacedim>
10815 {
10816  return
10818  (begin_active(level), end_active(level));
10819 }
10820 
10821 
10822 /*------------------------ Face iterator functions ------------------------*/
10823 
10824 
10825 template <int dim, int spacedim>
10828 {
10829  switch (dim)
10830  {
10831  case 1:
10832  Assert (false, ExcImpossibleInDim(1));
10833  return raw_face_iterator();
10834  case 2:
10835  return begin_line ();
10836  case 3:
10837  return begin_quad ();
10838  default:
10839  Assert (false, ExcNotImplemented());
10840  return face_iterator ();
10841  }
10842 }
10843 
10844 
10845 
10846 template <int dim, int spacedim>
10849 {
10850  switch (dim)
10851  {
10852  case 1:
10853  Assert (false, ExcImpossibleInDim(1));
10854  return raw_face_iterator();
10855  case 2:
10856  return begin_active_line ();
10857  case 3:
10858  return begin_active_quad ();
10859  default:
10860  Assert (false, ExcNotImplemented());
10861  return active_face_iterator ();
10862  }
10863 }
10864 
10865 
10866 
10867 template <int dim, int spacedim>
10870 {
10871  switch (dim)
10872  {
10873  case 1:
10874  Assert (false, ExcImpossibleInDim(1));
10875  return raw_face_iterator();
10876  case 2:
10877  return end_line ();
10878  case 3:
10879  return end_quad ();
10880  default:
10881  Assert (false, ExcNotImplemented());
10882  return raw_face_iterator ();
10883  }
10884 }
10885 
10886 
10887 /*------------------------ Vertex iterator functions ------------------------*/
10888 
10889 
10890 template <int dim, int spacedim>
10891 typename Triangulation<dim,spacedim>::vertex_iterator
10893 {
10894  if (dim==1)
10895  {
10896  // This does not work if dim==1 because TriaAccessor<0,1,spacedim> does not
10897  // implement operator++
10898  Assert(false, ExcNotImplemented());
10899  return raw_vertex_iterator();
10900  }
10901  else
10902  {
10903  vertex_iterator i = raw_vertex_iterator(const_cast<Triangulation<dim, spacedim>*>(this),
10904  0,
10905  0);
10906  if (i.state() != IteratorState::valid)
10907  return i;
10908  // This loop will end because every triangulation has used vertices.
10909  while (i->used() == false)
10910  if ((++i).state() != IteratorState::valid)
10911  return i;
10912  return i;
10913  }
10914 }
10915 
10916 
10917 
10918 template <int dim, int spacedim>
10919 typename Triangulation<dim,spacedim>::active_vertex_iterator
10921 {
10922  return begin_vertex();
10923 }
10924 
10925 
10926 
10927 template <int dim, int spacedim>
10928 typename Triangulation<dim,spacedim>::vertex_iterator
10930 {
10931  if (dim==1)
10932  {
10933  Assert(false, ExcNotImplemented());
10934  return raw_vertex_iterator();
10935  }
10936  else
10937  return raw_vertex_iterator(const_cast<Triangulation<dim, spacedim>*>(this),
10938  -1,
10940 }
10941 
10942 
10943 
10944 
10945 /*------------------------ Line iterator functions ------------------------*/
10946 
10947 
10948 
10949 template <int dim, int spacedim>
10950 typename Triangulation<dim, spacedim>::raw_line_iterator
10951 Triangulation<dim, spacedim>::begin_raw_line (const unsigned int level) const
10952 {
10953  switch (dim)
10954  {
10955  case 1:
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_line();
10960 
10961  return raw_line_iterator (const_cast<Triangulation<dim,spacedim>*>(this),
10962  level,
10963  0);
10964 
10965  default:
10966  Assert (level == 0, ExcFacesHaveNoLevel());
10967  return raw_line_iterator (const_cast<Triangulation<dim, spacedim>*>(this),
10968  0,
10969  0);
10970  }
10971 }
10972 
10973 
10974 template <int dim, int spacedim>
10975 typename Triangulation<dim, spacedim>::line_iterator
10976 Triangulation<dim, spacedim>::begin_line (const unsigned int level) const
10977 {
10978  // level is checked in begin_raw
10979  raw_line_iterator ri = begin_raw_line (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_line_iterator
10992 Triangulation<dim, spacedim>::begin_active_line (const unsigned int level) const
10993 {
10994  // level is checked in begin_raw
10995  line_iterator i = begin_line (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>::line_iterator
11009 {
11010  return raw_line_iterator (const_cast<Triangulation<dim, spacedim>*>(this),
11011  -1,
11012  -1);
11013 }
11014 
11015 
11016 
11017 /*------------------------ Quad iterator functions ------------------------*/
11018 
11019 
11020 template <int dim, int spacedim>
11021 typename Triangulation<dim,spacedim>::raw_quad_iterator
11022 Triangulation<dim,spacedim>::begin_raw_quad (const unsigned int level) const
11023 {
11024  switch (dim)
11025  {
11026  case 1:
11027  Assert (false, ExcImpossibleInDim(1));
11028  return raw_hex_iterator();
11029  case 2:
11030  {
11031  Assert (level<n_global_levels() || level<levels.size(), ExcInvalidLevel(level));
11032 
11033  if (level >= levels.size() || levels[level]->cells.cells.size() == 0)
11034  return end_quad();
11035 
11036  return raw_quad_iterator (const_cast<Triangulation<dim,spacedim>*>(this),
11037  level,
11038  0);
11039  }
11040 
11041  case 3:
11042  {
11043  Assert (level == 0, ExcFacesHaveNoLevel());
11044 
11045  return raw_quad_iterator (const_cast<Triangulation<dim,spacedim>*>(this),
11046  0,
11047  0);
11048  }
11049 
11050 
11051  default:
11052  Assert (false, ExcNotImplemented());
11053  return raw_hex_iterator();
11054  }
11055 }
11056 
11057 
11058 
11059 template <int dim, int spacedim>
11060 typename Triangulation<dim,spacedim>::quad_iterator
11061 Triangulation<dim,spacedim>::begin_quad (const unsigned int level) const
11062 {
11063  // level is checked in begin_raw
11064  raw_quad_iterator ri = begin_raw_quad (level);
11065  if (ri.state() != IteratorState::valid)
11066  return ri;
11067  while (ri->used() == false)
11068  if ((++ri).state() != IteratorState::valid)
11069  return ri;
11070  return ri;
11071 }
11072 
11073 
11074 
11075 template <int dim, int spacedim>
11076 typename Triangulation<dim,spacedim>::active_quad_iterator
11077 Triangulation<dim,spacedim>::begin_active_quad (const unsigned int level) const
11078 {
11079  // level is checked in begin_raw
11080  quad_iterator i = begin_quad (level);
11081  if (i.state() != IteratorState::valid)
11082  return i;
11083  while (i->has_children())
11084  if ((++i).state() != IteratorState::valid)
11085  return i;
11086  return i;
11087 }
11088 
11089 
11090 
11091 template <int dim, int spacedim>
11092 typename Triangulation<dim,spacedim>::quad_iterator
11094 {
11095  return raw_quad_iterator (const_cast<Triangulation<dim, spacedim>*>(this),
11096  -1,
11097  -1);
11098 }
11099 
11100 
11101 /*------------------------ Hex iterator functions ------------------------*/
11102 
11103 
11104 template <int dim, int spacedim>
11105 typename Triangulation<dim,spacedim>::raw_hex_iterator
11106 Triangulation<dim,spacedim>::begin_raw_hex (const unsigned int level) const
11107 {
11108  switch (dim)
11109  {
11110  case 1:
11111  case 2:
11112  Assert (false, ExcImpossibleInDim(1));
11113  return raw_hex_iterator();
11114  case 3:
11115  {
11116  Assert (level<n_global_levels() || level<levels.size(), ExcInvalidLevel(level));
11117 
11118  if (level >= levels.size() || levels[level]->cells.cells.size() == 0)
11119  return end_hex();
11120 
11121  return raw_hex_iterator (const_cast<Triangulation<dim,spacedim>*>(this),
11122  level,
11123  0);
11124  }
11125 
11126  default:
11127  Assert (false, ExcNotImplemented());
11128  return raw_hex_iterator();
11129  }
11130 }
11131 
11132 
11133 
11134 template <int dim, int spacedim>
11135 typename Triangulation<dim,spacedim>::hex_iterator
11136 Triangulation<dim,spacedim>::begin_hex (const unsigned int level) const
11137 {
11138  // level is checked in begin_raw
11139  raw_hex_iterator ri = begin_raw_hex (level);
11140  if (ri.state() != IteratorState::valid)
11141  return ri;
11142  while (ri->used() == false)
11143  if ((++ri).state() != IteratorState::valid)
11144  return ri;
11145  return ri;
11146 }
11147 
11148 
11149 
11150 template <int dim, int spacedim>
11151 typename Triangulation<dim, spacedim>::active_hex_iterator
11152 Triangulation<dim, spacedim>::begin_active_hex (const unsigned int level) const
11153 {
11154  // level is checked in begin_raw
11155  hex_iterator i = begin_hex (level);
11156  if (i.state() != IteratorState::valid)
11157  return i;
11158  while (i->has_children())
11159  if ((++i).state() != IteratorState::valid)
11160  return i;
11161  return i;
11162 }
11163 
11164 
11165 
11166 template <int dim, int spacedim>
11167 typename Triangulation<dim, spacedim>::hex_iterator
11169 {
11170  return raw_hex_iterator (const_cast<Triangulation<dim,spacedim>*>(this),
11171  -1,
11172  -1);
11173 }
11174 
11175 
11176 
11177 
11178 // -------------------------------- number of cells etc ---------------
11179 
11180 
11181 namespace internal
11182 {
11183  namespace Triangulation
11184  {
11185  inline
11186  unsigned int
11187  n_cells (const internal::Triangulation::NumberCache<1> &c)
11188  {
11189  return c.n_lines;
11190  }
11191 
11192 
11193  inline
11194  unsigned int
11195  n_active_cells (const internal::Triangulation::NumberCache<1> &c)
11196  {
11197  return c.n_active_lines;
11198  }
11199 
11200 
11201  inline
11202  unsigned int
11203  n_cells (const internal::Triangulation::NumberCache<2> &c)
11204  {
11205  return c.n_quads;
11206  }
11207 
11208 
11209  inline
11210  unsigned int
11211  n_active_cells (const internal::Triangulation::NumberCache<2> &c)
11212  {
11213  return c.n_active_quads;
11214  }
11215 
11216 
11217  inline
11218  unsigned int
11219  n_cells (const internal::Triangulation::NumberCache<3> &c)
11220  {
11221  return c.n_hexes;
11222  }
11223 
11224 
11225  inline
11226  unsigned int
11227  n_active_cells (const internal::Triangulation::NumberCache<3> &c)
11228  {
11229  return c.n_active_hexes;
11230  }
11231  }
11232 }
11233 
11234 
11235 
11236 template <int dim, int spacedim>
11238 {
11239  return internal::Triangulation::n_cells (number_cache);
11240 }
11241 
11242 
11243 template <int dim, int spacedim>
11245 {
11246  return internal::Triangulation::n_active_cells (number_cache);
11247 }
11248 
11249 template <int dim, int spacedim>
11251 {
11252  return n_active_cells();
11253 }
11254 
11255 
11256 
11257 template <int dim, int spacedim>
11259 {
11260  switch (dim)
11261  {
11262  case 1:
11263  return 0;
11264  case 2:
11265  return n_lines();
11266  case 3:
11267  return n_quads();
11268  default:
11269  Assert (false, ExcNotImplemented());
11270  }
11271  return 0;
11272 }
11273 
11274 
11275 template <int dim, int spacedim>
11277 {
11278  switch (dim)
11279  {
11280  case 2:
11281  return n_raw_lines();
11282  case 3:
11283  return n_raw_quads();
11284  default:
11285  Assert (false, ExcNotImplemented());
11286  }
11287  return 0;
11288 }
11289 
11290 
11291 template <int dim, int spacedim>
11293 {
11294  switch (dim)
11295  {
11296  case 1:
11297  return 0;
11298  case 2:
11299  return n_active_lines();
11300  case 3:
11301  return n_active_quads();
11302  default:
11303  Assert (false, ExcNotImplemented());
11304  }
11305  return 0;
11306 }
11307 
11308 
11309 template <int dim, int spacedim>
11310 unsigned int Triangulation<dim, spacedim>::n_raw_cells (const unsigned int level) const
11311 {
11312  switch (dim)
11313  {
11314  case 1:
11315  return n_raw_lines(level);
11316  case 2:
11317  return n_raw_quads(level);
11318  case 3:
11319  return n_raw_hexs(level);
11320  default:
11321  Assert (false, ExcNotImplemented());
11322  }
11323  return 0;
11324 }
11325 
11326 
11327 
11328 template <int dim, int spacedim>
11329 unsigned int Triangulation<dim, spacedim>::n_cells (const unsigned int level) const
11330 {
11331  switch (dim)
11332  {
11333  case 1:
11334  return n_lines(level);
11335  case 2:
11336  return n_quads(level);
11337  case 3:
11338  return n_hexs(level);
11339  default:
11340  Assert (false, ExcNotImplemented());
11341  }
11342  return 0;
11343 }
11344 
11345 
11346 
11347 template <int dim, int spacedim>
11348 unsigned int Triangulation<dim, spacedim>::n_active_cells (const unsigned int level) const
11349 {
11350  switch (dim)
11351  {
11352  case 1:
11353  return n_active_lines(level);
11354  case 2:
11355  return n_active_quads(level);
11356  case 3:
11357  return n_active_hexs(level);
11358  default:
11359  Assert (false, ExcNotImplemented());
11360  }
11361  return 0;
11362 }
11363 
11364 
11365 template <int dim, int spacedim>
11367 {
11368  for (unsigned int lvl = 0; lvl<n_global_levels()-1; lvl++)
11369  if (n_active_cells(lvl) != 0)
11370  return true;
11371 
11372  return false;
11373 }
11374 
11375 
11376 template <int dim, int spacedim>
11378 {
11379  return number_cache.n_lines;
11380 }
11381 
11382 
11383 //TODO: Merge the following 6 functions somehow
11384 template <>
11385 unsigned int Triangulation<1,1>::n_raw_lines (const unsigned int level) const
11386 {
11387  Assert(level < n_levels(), ExcIndexRange(level,0,n_levels()));
11388  return levels[level]->cells.cells.size();
11389 }
11390 
11391 
11392 template <>
11393 unsigned int Triangulation<1,1>::n_raw_lines () const
11394 {
11395  Assert(false, ExcNotImplemented());
11396  return 0;
11397 }
11398 
11399 
11400 
11401 template <>
11402 unsigned int Triangulation<1,2>::n_raw_lines (const unsigned int level) const
11403 {
11404  Assert(level < n_levels(), ExcIndexRange(level,0,n_levels()));
11405  return levels[level]->cells.cells.size();
11406 }
11407 
11408 
11409 template <>
11410 unsigned int Triangulation<1,2>::n_raw_lines () const
11411 {
11412  Assert(false, ExcNotImplemented());
11413  return 0;
11414 }
11415 
11416 
11417 template <>
11418 unsigned int Triangulation<1,3>::n_raw_lines (const unsigned int level) const
11419 {
11420  Assert(level < n_levels(), ExcIndexRange(level,0,n_levels()));
11421  return levels[level]->cells.cells.size();
11422 }
11423 
11424 template <>
11425 unsigned int Triangulation<1,3>::n_raw_lines () const
11426 {
11427  Assert(false, ExcNotImplemented());
11428  return 0;
11429 }
11430 
11431 
11432 
11433 template <int dim, int spacedim>
11434 unsigned int Triangulation<dim, spacedim>::n_raw_lines (const unsigned int) const
11435 {
11436  Assert(false, ExcFacesHaveNoLevel());
11437  return 0;
11438 }
11439 
11440 
11441 template <int dim, int spacedim>
11443 {
11444  return faces->lines.cells.size();
11445 }
11446 
11447 
11448 template <int dim, int spacedim>
11449 unsigned int Triangulation<dim, spacedim>::n_lines (const unsigned int level) const
11450 {
11451  Assert (level < number_cache.n_lines_level.size(),
11452  ExcIndexRange (level, 0, number_cache.n_lines_level.size()));
11453  Assert (dim == 1, ExcFacesHaveNoLevel());
11454  return number_cache.n_lines_level[level];
11455 }
11456 
11457 
11458 template <int dim, int spacedim>
11460 {
11461  return number_cache.n_active_lines;
11462 }
11463 
11464 
11465 template <int dim, int spacedim>
11466 unsigned int Triangulation<dim, spacedim>::n_active_lines (const unsigned int level) const
11467 {
11468  Assert (level < number_cache.n_lines_level.size(),
11469  ExcIndexRange (level, 0, number_cache.n_lines_level.size()));
11470  Assert (dim == 1, ExcFacesHaveNoLevel());
11471 
11472  return number_cache.n_active_lines_level[level];
11473 }
11474 
11475 
11476 template <>
11477 unsigned int Triangulation<1,1>::n_quads () const
11478 {
11479  return 0;
11480 }
11481 
11482 
11483 template <>
11484 unsigned int Triangulation<1,1>::n_quads (const unsigned int) const
11485 {
11486  return 0;
11487 }
11488 
11489 
11490 template <>
11491 unsigned int Triangulation<1,1>::n_raw_quads (const unsigned int) const
11492 {
11493  return 0;
11494 }
11495 
11496 
11497 template <>
11498 unsigned int Triangulation<1,1>::n_raw_hexs (const unsigned int) const
11499 {
11500  return 0;
11501 }
11502 
11503 
11504 template <>
11505 unsigned int Triangulation<1,1>::n_active_quads (const unsigned int) const
11506 {
11507  return 0;
11508 }
11509 
11510 
11511 template <>
11512 unsigned int Triangulation<1,1>::n_active_quads () const
11513 {
11514  return 0;
11515 }
11516 
11517 
11518 
11519 
11520 template <>
11521 unsigned int Triangulation<1,2>::n_quads () const
11522 {
11523  return 0;
11524 }
11525 
11526 
11527 template <>
11528 unsigned int Triangulation<1,2>::n_quads (const unsigned int) const
11529 {
11530  return 0;
11531 }
11532 
11533 
11534 template <>
11535 unsigned int Triangulation<1,2>::n_raw_quads (const unsigned int) const
11536 {
11537  return 0;
11538 }
11539 
11540 
11541 template <>
11542 unsigned int Triangulation<1,2>::n_raw_hexs (const unsigned int) const
11543 {
11544  return 0;
11545 }
11546 
11547 
11548 template <>
11549 unsigned int Triangulation<1,2>::n_active_quads (const unsigned int) const
11550 {
11551  return 0;
11552 }
11553 
11554 
11555 template <>
11556 unsigned int Triangulation<1,2>::n_active_quads () const
11557 {
11558  return 0;
11559 }
11560 
11561 
11562 template <>
11563 unsigned int Triangulation<1,3>::n_quads () const
11564 {
11565  return 0;
11566 }
11567 
11568 
11569 template <>
11570 unsigned int Triangulation<1,3>::n_quads (const unsigned int) const
11571 {
11572  return 0;
11573 }
11574 
11575 
11576 template <>
11577 unsigned int Triangulation<1,3>::n_raw_quads (const unsigned int) const
11578 {
11579  return 0;
11580 }
11581 
11582 
11583 template <>
11584 unsigned int Triangulation<1,3>::n_raw_hexs (const unsigned int) const
11585 {
11586  return 0;
11587 }
11588 
11589 
11590 template <>
11591 unsigned int Triangulation<1,3>::n_active_quads (const unsigned int) const
11592 {
11593  return 0;
11594 }
11595 
11596 
11597 template <>
11598 unsigned int Triangulation<1,3>::n_active_quads () const
11599 {
11600  return 0;
11601 }
11602 
11603 
11604 
11605 template <int dim, int spacedim>
11607 {
11608  return number_cache.n_quads;
11609 }
11610 
11611 
11612 template <int dim, int spacedim>
11613 unsigned int Triangulation<dim, spacedim>::n_quads (const unsigned int level) const
11614 {
11615  Assert (dim == 2, ExcFacesHaveNoLevel());
11616  Assert (level < number_cache.n_quads_level.size(),
11617  ExcIndexRange (level, 0, number_cache.n_quads_level.size()));
11618  return number_cache.n_quads_level[level];
11619 }
11620 
11621 
11622 
11623 template <>
11624 unsigned int Triangulation<2,2>::n_raw_quads (const unsigned int level) const
11625 {
11626  Assert(level < n_levels(), ExcIndexRange(level,0,n_levels()));
11627  return levels[level]->cells.cells.size();
11628 }
11629 
11630 
11631 
11632 template <>
11633 unsigned int Triangulation<2,3>::n_raw_quads (const unsigned int level) const
11634 {
11635  Assert(level < n_levels(), ExcIndexRange(level,0,n_levels()));
11636  return levels[level]->cells.cells.size();
11637 }
11638 
11639 
11640 template <>
11641 unsigned int Triangulation<3,3>::n_raw_quads (const unsigned int) const
11642 {
11643  Assert(false, ExcFacesHaveNoLevel());
11644  return 0;
11645 }
11646 
11647 
11648 
11649 
11650 
11651 template <int dim, int spacedim>
11653 {
11654  Assert (false, ExcNotImplemented());
11655  return 0;
11656 }
11657 
11658 
11659 
11660 template <>
11661 unsigned int Triangulation<3,3>::n_raw_quads () const
11662 {
11663  return faces->quads.cells.size();
11664 }
11665 
11666 
11667 
11668 template <int dim, int spacedim>
11670 {
11671  return number_cache.n_active_quads;
11672 }
11673 
11674 
11675 template <int dim, int spacedim>
11676 unsigned int Triangulation<dim, spacedim>::n_active_quads (const unsigned int level) const
11677 {
11678  Assert (level < number_cache.n_quads_level.size(),
11679  ExcIndexRange (level, 0, number_cache.n_quads_level.size()));
11680  Assert (dim == 2, ExcFacesHaveNoLevel());
11681 
11682  return number_cache.n_active_quads_level[level];
11683 }
11684 
11685 
11686 template <int dim, int spacedim>
11688 {
11689  return 0;
11690 }
11691 
11692 
11693 
11694 template <int dim, int spacedim>
11695 unsigned int Triangulation<dim, spacedim>::n_hexs (const unsigned int) const
11696 {
11697  return 0;
11698 }
11699 
11700 
11701 
11702 template <int dim, int spacedim>
11703 unsigned int Triangulation<dim, spacedim>::n_raw_hexs (const unsigned int) const
11704 {
11705  return 0;
11706 }
11707 
11708 
11709 template <int dim, int spacedim>
11711 {
11712  return 0;
11713 }
11714 
11715 
11716 
11717 template <int dim, int spacedim>
11718 unsigned int Triangulation<dim, spacedim>::n_active_hexs (const unsigned int) const
11719 {
11720  return 0;
11721 }
11722 
11723 
11724 template <>
11725 unsigned int Triangulation<3,3>::n_hexs () const
11726 {
11727  return number_cache.n_hexes;
11728 }
11729 
11730 
11731 
11732 template <>
11733 unsigned int Triangulation<3,3>::n_hexs (const unsigned int level) const
11734 {
11735  Assert (level < number_cache.n_hexes_level.size(),
11736  ExcIndexRange (level, 0, number_cache.n_hexes_level.size()));
11737 
11738  return number_cache.n_hexes_level[level];
11739 }
11740 
11741 
11742 
11743 template <>
11744 unsigned int Triangulation<3,3>::n_raw_hexs (const unsigned int level) const
11745 {
11746  Assert(level < n_levels(), ExcIndexRange(level,0,n_levels()));
11747  return levels[level]->cells.cells.size();
11748 }
11749 
11750 
11751 template <>
11752 unsigned int Triangulation<3,3>::n_active_hexs () const
11753 {
11754  return number_cache.n_active_hexes;
11755 }
11756 
11757 
11758 
11759 template <>
11760 unsigned int Triangulation<3,3>::n_active_hexs (const unsigned int level) const
11761 {
11762  Assert (level < number_cache.n_hexes_level.size(),
11763  ExcIndexRange (level, 0, number_cache.n_hexes_level.size()));
11764 
11765  return number_cache.n_active_hexes_level[level];
11766 }
11767 
11768 
11769 
11770 template <int dim, int spacedim>
11771 unsigned int
11773 {
11774  return std::count_if (vertices_used.begin(), vertices_used.end(),
11775  std_cxx11::bind (std::equal_to<bool>(), std_cxx11::_1, true));
11776 }
11777 
11778 
11779 
11780 template <int dim, int spacedim>
11781 const std::vector<bool> &
11783 {
11784  return vertices_used;
11785 }
11786 
11787 
11788 
11789 
11790 template <>
11791 unsigned int Triangulation<1,1>::max_adjacent_cells () const
11792 {
11793  return 2;
11794 }
11795 
11796 
11797 
11798 template <>
11799 unsigned int Triangulation<1,2>::max_adjacent_cells () const
11800 {
11801  return 2;
11802 }
11803 
11804 
11805 template <>
11806 unsigned int Triangulation<1,3>::max_adjacent_cells () const
11807 {
11808  return 2;
11809 }
11810 
11811 
11812 template <int dim, int spacedim>
11814 {
11815  cell_iterator cell = begin(0),
11816  endc = (n_levels() > 1 ? begin(1) : cell_iterator(end()));
11817  // store the largest index of the
11818  // vertices used on level 0
11819  unsigned int max_vertex_index = 0;
11820  for (; cell!=endc; ++cell)
11821  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell; ++vertex)
11822  if (cell->vertex_index(vertex) > max_vertex_index)
11823  max_vertex_index = cell->vertex_index(vertex);
11824 
11825  // store the number of times a cell
11826  // touches a vertex. An unsigned
11827  // int should suffice, even for
11828  // larger dimensions
11829  std::vector<unsigned short int> usage_count (max_vertex_index+1, 0);
11830  // touch a vertex's usage count
11831  // every time we find an adjacent
11832  // element
11833  for (cell=begin(); cell!=endc; ++cell)
11834  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell; ++vertex)
11835  ++usage_count[cell->vertex_index(vertex)];
11836 
11837  return std::max (GeometryInfo<dim>::vertices_per_cell,
11838  static_cast<unsigned int>(*std::max_element (usage_count.begin(),
11839  usage_count.end())));
11840 }
11841 
11842 
11843 
11844 template <int dim, int spacedim>
11847 {
11849 }
11850 
11851 
11852 
11853 template <int dim, int spacedim>
11856 {
11857  return *this;
11858 }
11859 
11860 
11861 
11862 template <int dim, int spacedim>
11865 {
11866  return *this;
11867 }
11868 
11869 
11870 
11871 template <int dim, int spacedim>
11872 void
11875  periodicity_vector)
11876 {
11877  periodic_face_pairs_level_0.insert(periodic_face_pairs_level_0.end(),
11878  periodicity_vector.begin(),
11879  periodicity_vector.end());
11880 
11881  //Now initialize periodic_face_map
11882  update_periodic_face_map();
11883 }
11884 
11885 
11886 
11887 template <int dim, int spacedim>
11888 const typename std::map<std::pair<typename Triangulation<dim, spacedim>::cell_iterator, unsigned int>,
11889  std::pair<std::pair<typename Triangulation<dim, spacedim>::cell_iterator, unsigned int>, std::bitset<3> > > &
11891 {
11892  return periodic_face_map;
11893 }
11894 
11895 
11896 
11897 template <int dim, int spacedim>
11898 void
11900 {
11901  prepare_coarsening_and_refinement ();
11902 
11903  // verify a case with which we have had
11904  // some difficulty in the past (see the
11905  // deal.II/coarsening_* tests)
11906  if (smooth_grid & limit_level_difference_at_vertices)
11907  Assert (satisfies_level1_at_vertex_rule (*this) == true,
11908  ExcInternalError());
11909 
11910  // Inform all listeners about beginning of refinement.
11911  signals.pre_refinement();
11912 
11913  execute_coarsening();
11914 
11915  const DistortedCellList
11916  cells_with_distorted_children = execute_refinement();
11917 
11918  // verify a case with which we have had
11919  // some difficulty in the past (see the
11920  // deal.II/coarsening_* tests)
11921  if (smooth_grid & limit_level_difference_at_vertices)
11922  Assert (satisfies_level1_at_vertex_rule (*this) == true,
11923  ExcInternalError());
11924 
11925  // finally build up neighbor connectivity information, and set
11926  // active cell indices
11927  update_neighbors(*this);
11928  reset_active_cell_indices ();
11929 
11930  // Inform all listeners about end of refinement.
11931  signals.post_refinement();
11932 
11933  AssertThrow (cells_with_distorted_children.distorted_cells.size() == 0,
11934  cells_with_distorted_children);
11935 
11936  update_periodic_face_map();
11937 }
11938 
11939 
11940 
11941 template <int dim, int spacedim>
11942 void
11944 {
11945  unsigned int active_cell_index = 0;
11946  for (raw_cell_iterator cell=begin_raw(); cell!=end(); ++cell)
11947  if ((cell->used() == false) || cell->has_children())
11948  cell->set_active_cell_index (numbers::invalid_unsigned_int);
11949  else
11950  {
11951  cell->set_active_cell_index (active_cell_index);
11952  ++active_cell_index;
11953  }
11954 
11955  Assert (active_cell_index == n_active_cells(), ExcInternalError());
11956 }
11957 
11958 
11959 template <int dim, int spacedim>
11960 void
11962 {
11963  //first empty the currently stored objects
11964  periodic_face_map.clear();
11965 
11966  typename std::vector<GridTools::PeriodicFacePair<cell_iterator> >::const_iterator it;
11967  for (it=periodic_face_pairs_level_0.begin(); it!=periodic_face_pairs_level_0.end(); ++it)
11968  {
11969  update_periodic_face_map_recursively<dim, spacedim>
11970  (it->cell[0], it->cell[1], it->face_idx[0], it->face_idx[1],
11971  it->orientation, periodic_face_map);
11972 
11973  //for the other way, we need to invert the orientation
11974  std::bitset<3> inverted_orientation;
11975  {
11976  bool orientation, flip, rotation;
11977  orientation = it->orientation[0];
11978  rotation = it->orientation[2];
11979  flip = orientation ? rotation ^ it->orientation[1] : it->orientation[1];
11980  inverted_orientation[0] = orientation;
11981  inverted_orientation[1] = flip;
11982  inverted_orientation[2] = rotation;
11983  }
11984  update_periodic_face_map_recursively<dim, spacedim>
11985  (it->cell[1], it->cell[0], it->face_idx[1], it->face_idx[0],
11986  inverted_orientation, periodic_face_map);
11987  }
11988 
11989  //check consistency
11990  typename std::map<std::pair<cell_iterator, unsigned int>,
11991  std::pair<std::pair<cell_iterator, unsigned int>, std::bitset<3> > >::const_iterator it_test;
11992  for (it_test=periodic_face_map.begin(); it_test!=periodic_face_map.end(); ++it_test)
11993  {
11994  const Triangulation<dim, spacedim>::cell_iterator cell_1 = it_test->first.first;
11995  const Triangulation<dim, spacedim>::cell_iterator cell_2 = it_test->second.first.first;
11996  if (cell_1->level() == cell_2->level())
11997  {
11998  // if both cells have the same neighbor, then the same pair
11999  // order swapped has to be in the map
12000  Assert (periodic_face_map[it_test->second.first].first == it_test->first,
12001  ExcInternalError());
12002  }
12003  }
12004 }
12005 
12006 
12007 
12008 template<int dim, int spacedim>
12009 void
12011 {
12012  // This is the former function
12013  // clear without the assertion in
12014  // the beginning.
12015  for (unsigned int i=0; i<levels.size(); ++i)
12016  delete levels[i];
12017  levels.clear ();
12018 
12019  faces.reset ();
12020 
12021  vertices.clear ();
12022  vertices_used.clear ();
12023 
12024  manifold.clear();
12025 
12027 }
12028 
12029 
12030 template <int dim, int spacedim>
12033 {
12034  const DistortedCellList
12035  cells_with_distorted_children
12036  =
12038  execute_refinement (*this, check_for_distorted_cells);
12039 
12040 
12041 
12042  // re-compute number of lines
12044  ::compute_number_cache (*this, levels.size(), number_cache);
12045 
12046 #ifdef DEBUG
12047  for (unsigned int level=0; level<levels.size(); ++level)
12048  levels[level]->cells.monitor_memory (dim);
12049 
12050  // check whether really all refinement flags are reset (also of
12051  // previously non-active cells which we may not have touched. If the
12052  // refinement flag of a non-active cell is set, something went wrong
12053  // since the cell-accessors should have caught this)
12054  cell_iterator cell = begin(),
12055  endc = end();
12056  while (cell != endc)
12057  Assert (!(cell++)->refine_flag_set(), ExcInternalError ());
12058 #endif
12059 
12060  return cells_with_distorted_children;
12061 }
12062 
12063 
12064 
12065 template <int dim, int spacedim>
12067 {
12068  // create a vector counting for each line how many cells contain
12069  // this line. in 3D, this is used later on to decide which lines can
12070  // be deleted after coarsening a cell. in other dimensions it will
12071  // be ignored
12072  std::vector<unsigned int> line_cell_count = count_cells_bounded_by_line (*this);
12073  std::vector<unsigned int> quad_cell_count = count_cells_bounded_by_quad (*this);
12074 
12075  // loop over all cells. Flag all cells of which all children are
12076  // flagged for coarsening and delete the childrens' flags. In
12077  // effect, only those cells are flagged of which originally all
12078  // children were flagged and for which all children are on the same
12079  // refinement level. For flagging, the user flags are used, to avoid
12080  // confusion and because non-active cells can't be flagged for
12081  // coarsening. Note that because of the effects of
12082  // @p{fix_coarsen_flags}, of a cell either all or no children must
12083  // be flagged for coarsening, so it is ok to only check the first
12084  // child
12085  clear_user_flags ();
12086 
12087  cell_iterator cell = begin(),
12088  endc = end();
12089  for (; cell!=endc; ++cell)
12090  if (!cell->active())
12091  if (cell->child(0)->coarsen_flag_set())
12092  {
12093  cell->set_user_flag();
12094  for (unsigned int child=0; child<cell->n_children(); ++child)
12095  {
12096  Assert (cell->child(child)->coarsen_flag_set(),
12097  ExcInternalError());
12098  cell->child(child)->clear_coarsen_flag();
12099  }
12100  }
12101 
12102 
12103  // now do the actual coarsening step. Since the loop goes over used
12104  // cells we only need not worry about deleting some cells since the
12105  // ++operator will then just hop over them if we should hit one. Do
12106  // the loop in the reverse way since we may only delete some cells
12107  // if their neighbors have already been deleted (if the latter are
12108  // on a higher level for example)
12109  //
12110  // since we delete the *children* of cells, we can ignore cells
12111  // on the highest level, i.e., level must be less than or equal
12112  // to n_levels()-2.
12113  if (levels.size() >= 2)
12114  for (cell = last(); cell!=endc; --cell)
12115  if (cell->level()<=static_cast<int>(levels.size()-2) && cell->user_flag_set())
12116  {
12117  // inform all listeners that cell coarsening is going to happen
12118  signals.pre_coarsening_on_cell(cell);
12119  // use a separate function, since this is dimension specific
12121  ::delete_children (*this, cell, line_cell_count, quad_cell_count);
12122  }
12123 
12124  // re-compute number of lines and quads
12126  ::compute_number_cache (*this, levels.size(), number_cache);
12127 
12128  // in principle no user flags should be set any more at this point
12129 #if DEBUG
12130  for (cell=begin(); cell!=endc; ++cell)
12131  Assert (cell->user_flag_set() == false, ExcInternalError());
12132 #endif
12133 }
12134 
12135 
12136 
12137 template <int dim, int spacedim>
12139 {
12140  // copy a piece of code from prepare_coarsening_and_refinement that
12141  // ensures that the level difference at vertices is limited if so
12142  // desired. we need this code here since at least in 1d we don't
12143  // call the dimension-independent version of
12144  // prepare_coarsening_and_refinement function. in 2d and 3d, having
12145  // this hunk here makes our lives a bit easier as well as it takes
12146  // care of these cases earlier than it would otherwise happen.
12147  //
12148  // the main difference to the code in p_c_and_r is that here we
12149  // absolutely have to make sure that we get things right, i.e. that
12150  // in particular we set flags right if
12151  // limit_level_difference_at_vertices is set. to do so we iterate
12152  // until the flags don't change any more
12153  std::vector<bool> previous_coarsen_flags (n_active_cells());
12154  save_coarsen_flags (previous_coarsen_flags);
12155 
12156  std::vector<int> vertex_level (vertices.size(), 0);
12157 
12158  bool continue_iterating = true;
12159 
12160  do
12161  {
12162  if (smooth_grid & limit_level_difference_at_vertices)
12163  {
12164  Assert(!anisotropic_refinement,
12165  ExcMessage("In case of anisotropic refinement the "
12166  "limit_level_difference_at_vertices flag for "
12167  "mesh smoothing must not be set!"));
12168 
12169  // store highest level one of the cells adjacent to a vertex
12170  // belongs to
12171  std::fill (vertex_level.begin(), vertex_level.end(), 0);
12172  active_cell_iterator cell = begin_active(),
12173  endc = end();
12174  for (; cell!=endc; ++cell)
12175  {
12176  if (cell->refine_flag_set())
12177  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell;
12178  ++vertex)
12179  vertex_level[cell->vertex_index(vertex)]
12180  = std::max (vertex_level[cell->vertex_index(vertex)],
12181  cell->level()+1);
12182  else if (!cell->coarsen_flag_set())
12183  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell;
12184  ++vertex)
12185  vertex_level[cell->vertex_index(vertex)]
12186  = std::max (vertex_level[cell->vertex_index(vertex)],
12187  cell->level());
12188  else
12189  {
12190  // if coarsen flag is set then tentatively assume
12191  // that the cell will be coarsened. this isn't
12192  // always true (the coarsen flag could be removed
12193  // again) and so we may make an error here. we try
12194  // to correct this by iterating over the entire
12195  // process until we are converged
12196  Assert (cell->coarsen_flag_set(), ExcInternalError());
12197  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell;
12198  ++vertex)
12199  vertex_level[cell->vertex_index(vertex)]
12200  = std::max (vertex_level[cell->vertex_index(vertex)],
12201  cell->level()-1);
12202  }
12203  }
12204 
12205 
12206  // loop over all cells in reverse order. do so because we
12207  // can then update the vertex levels on the adjacent
12208  // vertices and maybe already flag additional cells in this
12209  // loop
12210  //
12211  // note that not only may we have to add additional
12212  // refinement flags, but we will also have to remove
12213  // coarsening flags on cells adjacent to vertices that will
12214  // see refinement
12215  for (cell=last_active(); cell != endc; --cell)
12216  if (cell->refine_flag_set() == false)
12217  {
12218  for (unsigned int vertex=0;
12219  vertex<GeometryInfo<dim>::vertices_per_cell; ++vertex)
12220  if (vertex_level[cell->vertex_index(vertex)] >=
12221  cell->level()+1)
12222  {
12223  // remove coarsen flag...
12224  cell->clear_coarsen_flag();
12225 
12226  // ...and if necessary also refine the current
12227  // cell, at the same time updating the level
12228  // information about vertices
12229  if (vertex_level[cell->vertex_index(vertex)] >
12230  cell->level()+1)
12231  {
12232  cell->set_refine_flag();
12233 
12234  for (unsigned int v=0; v<GeometryInfo<dim>::vertices_per_cell;
12235  ++v)
12236  vertex_level[cell->vertex_index(v)]
12237  = std::max (vertex_level[cell->vertex_index(v)],
12238  cell->level()+1);
12239  }
12240 
12241  // continue and see whether we may, for example,
12242  // go into the inner 'if' above based on a
12243  // different vertex
12244  }
12245  }
12246  }
12247 
12248  // loop over all cells. Flag all cells of which all children are
12249  // flagged for coarsening and delete the childrens' flags. Also
12250  // delete all flags of cells for which not all children of a
12251  // cell are flagged. In effect, only those cells are flagged of
12252  // which originally all children were flagged and for which all
12253  // children are on the same refinement level. For flagging, the
12254  // user flags are used, to avoid confusion and because
12255  // non-active cells can't be flagged for coarsening
12256  //
12257  // In effect, all coarsen flags are turned into user flags of
12258  // the mother cell if coarsening is possible or deleted
12259  // otherwise.
12260  clear_user_flags ();
12261  // Coarsen flags of cells with no mother cell, i.e. on the
12262  // coarsest level are deleted explicitly.
12263  active_cell_iterator acell = begin_active(0),
12264  end_ac = end_active(0);
12265  for (; acell!=end_ac; ++acell)
12266  acell->clear_coarsen_flag();
12267 
12268  cell_iterator cell = begin(),
12269  endc = end();
12270  for (; cell!=endc; ++cell)
12271  {
12272  // nothing to do if we are already on the finest level
12273  if (cell->active())
12274  continue;
12275 
12276  const unsigned int n_children=cell->n_children();
12277  unsigned int flagged_children=0;
12278  for (unsigned int child=0; child<n_children; ++child)
12279  if (cell->child(child)->active() &&
12280  cell->child(child)->coarsen_flag_set())
12281  {
12282  ++flagged_children;
12283  // clear flag since we don't need it anymore
12284  cell->child(child)->clear_coarsen_flag();
12285  }
12286 
12287  // flag this cell for coarsening if all children were
12288  // flagged
12289  if (flagged_children == n_children)
12290  cell->set_user_flag();
12291  }
12292 
12293  // in principle no coarsen flags should be set any more at this
12294  // point
12295 #if DEBUG
12296  for (cell=begin(); cell!=endc; ++cell)
12297  Assert (cell->coarsen_flag_set() == false, ExcInternalError());
12298 #endif
12299 
12300  // now loop over all cells which have the user flag set. their
12301  // children were flagged for coarsening. set the coarsen flag
12302  // again if we are sure that none of the neighbors of these
12303  // children are refined, or will be refined, since then we would
12304  // get a two-level jump in refinement. on the other hand, if one
12305  // of the children's neighbors has their user flag set, then we
12306  // know that its children will go away by coarsening, and we
12307  // will be ok.
12308  //
12309  // note on the other hand that we do allow level-2 jumps in
12310  // refinement between neighbors in 1d, so this whole procedure
12311  // is only necessary if we are not in 1d
12312  //
12313  // since we remove some coarsening/user flags in the process, we
12314  // have to work from the finest level to the coarsest one, since
12315  // we occasionally inspect user flags of cells on finer levels
12316  // and need to be sure that these flags are final
12317  for (cell=last(); cell!=endc; --cell)
12318  if (cell->user_flag_set())
12319  // if allowed: flag the
12320  // children for coarsening
12321  if (internal::Triangulation::Implementation::template coarsening_allowed<dim,spacedim>(cell))
12322  for (unsigned int c=0; c<cell->n_children(); ++c)
12323  {
12324  Assert (cell->child(c)->refine_flag_set()==false,
12325  ExcInternalError());
12326 
12327  cell->child(c)->set_coarsen_flag();
12328  }
12329 
12330  // clear all user flags again, now that we don't need them any
12331  // more
12332  clear_user_flags ();
12333 
12334 
12335  // now see if anything has changed in the last iteration of this
12336  // function
12337  std::vector<bool> current_coarsen_flags (n_active_cells());
12338  save_coarsen_flags (current_coarsen_flags);
12339 
12340  continue_iterating = (current_coarsen_flags != previous_coarsen_flags);
12341  previous_coarsen_flags = current_coarsen_flags;
12342  }
12343  while (continue_iterating == true);
12344 }
12345 
12346 
12347 //TODO: merge the following 3 functions since they are the same
12348 template <>
12350 {
12351  // save the flags to determine whether something was changed in the
12352  // course of this function
12353  std::vector<bool> flags_before;
12354  save_coarsen_flags (flags_before);
12355 
12356  // do nothing in 1d, except setting the coarsening flags correctly
12357  fix_coarsen_flags ();
12358 
12359  std::vector<bool> flags_after;
12360  save_coarsen_flags (flags_after);
12361 
12362  return (flags_before != flags_after);
12363 }
12364 
12365 
12366 template <>
12368 {
12369  // save the flags to determine whether something was changed in the
12370  // course of this function
12371  std::vector<bool> flags_before;
12372  save_coarsen_flags (flags_before);
12373 
12374  // do nothing in 1d, except setting the coarsening flags correctly
12375  fix_coarsen_flags ();
12376 
12377  std::vector<bool> flags_after;
12378  save_coarsen_flags (flags_after);
12379 
12380  return (flags_before != flags_after);
12381 }
12382 
12383 
12384 template <>
12386 {
12387  // save the flags to determine whether something was changed in the
12388  // course of this function
12389  std::vector<bool> flags_before;
12390  save_coarsen_flags (flags_before);
12391 
12392  // do nothing in 1d, except setting the coarsening flags correctly
12393  fix_coarsen_flags ();
12394 
12395  std::vector<bool> flags_after;
12396  save_coarsen_flags (flags_after);
12397 
12398  return (flags_before != flags_after);
12399 }
12400 
12401 
12402 
12403 
12404 namespace
12405 {
12406 
12407  // check if the given @param cell marked for coarsening would
12408  // produce an unrefined island. To break up long chains of these
12409  // cells we recursively check our neighbors in case we change this
12410  // cell. This reduces the number of outer iterations dramatically.
12411  template <int dim, int spacedim>
12412  void
12413  possibly_do_not_produce_unrefined_islands(
12414  const typename Triangulation<dim,spacedim>::cell_iterator &cell)
12415  {
12416  Assert (cell->has_children(), ExcInternalError());
12417 
12418  unsigned int n_neighbors=0;
12419  // count all neighbors that will be refined along the face of our
12420  // cell after the next step
12421  unsigned int count=0;
12422  for (unsigned int n=0; n<GeometryInfo<dim>::faces_per_cell; ++n)
12423  {
12424  const typename Triangulation<dim,spacedim>::cell_iterator neighbor = cell->neighbor(n);
12425  if (neighbor.state() == IteratorState::valid)
12426  {
12427  ++n_neighbors;
12428  if (face_will_be_refined_by_neighbor(cell,n))
12429  ++count;
12430  }
12431  }
12432  // clear coarsen flags if either all existing neighbors will be
12433  // refined or all but one will be and the cell is in the interior
12434  // of the domain
12435  if (count==n_neighbors ||
12436  (count>=n_neighbors-1 &&
12437  n_neighbors == GeometryInfo<dim>::faces_per_cell) )
12438  {
12439  for (unsigned int c=0; c<cell->n_children(); ++c)
12440  cell->child(c)->clear_coarsen_flag();
12441 
12442  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
12443  if (!cell->at_boundary(face)
12444  &&
12445  ( !cell->neighbor(face)->active() )
12446  && (cell_will_be_coarsened(cell->neighbor(face))) )
12447  possibly_do_not_produce_unrefined_islands<dim,spacedim>( cell->neighbor(face) );
12448  }
12449  }
12450 
12451 
12452  // see if the current cell needs to be refined to avoid unrefined
12453  // islands.
12454  //
12455  // there are sometimes chains of cells that induce refinement of
12456  // each other. to avoid running the loop in
12457  // prepare_coarsening_and_refinement over and over again for each
12458  // one of them, at least for the isotropic refinement case we seek
12459  // to flag neighboring elements as well as necessary. this takes
12460  // care of (slightly pathological) cases like
12461  // deal.II/mesh_smoothing_03
12462  template <int dim, int spacedim>
12463  void
12464  possibly_refine_unrefined_island
12465  (const typename Triangulation<dim,spacedim>::cell_iterator &cell,
12466  const bool allow_anisotropic_smoothing)
12467  {
12468  Assert (cell->has_children() == false, ExcInternalError());
12469  Assert (cell->refine_flag_set() == false, ExcInternalError());
12470 
12471 
12472  // now we provide two algorithms. the first one is the standard
12473  // one, coming from the time, where only isotropic refinement was
12474  // possible. it simply counts the neighbors that are or will be
12475  // refined and compares to the number of other ones. the second
12476  // one does this check independently for each direction: if all
12477  // neighbors in one direction (normally two, at the boundary only
12478  // one) are refined, the current cell is flagged to be refined in
12479  // an according direction.
12480 
12481  if (allow_anisotropic_smoothing == false)
12482  {
12483  // use first algorithm
12484  unsigned int refined_neighbors = 0,
12485  unrefined_neighbors = 0;
12486  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
12487  if (!cell->at_boundary(face))
12488  {
12489  if (face_will_be_refined_by_neighbor(cell,face))
12490  ++refined_neighbors;
12491  else
12492  ++unrefined_neighbors;
12493  }
12494 
12495  if (unrefined_neighbors < refined_neighbors)
12496  {
12497  cell->clear_coarsen_flag();
12498  cell->set_refine_flag ();
12499 
12500  // ok, so now we have flagged this cell. if we know that
12501  // there were any unrefined neighbors at all, see if any
12502  // of those will have to be refined as well
12503  if (unrefined_neighbors > 0)
12504  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
12505  if (!cell->at_boundary(face)
12506  &&
12507  (face_will_be_refined_by_neighbor(cell,face) == false)
12508  &&
12509  (cell->neighbor(face)->has_children() == false)
12510  &&
12511  (cell->neighbor(face)->refine_flag_set() == false))
12512  possibly_refine_unrefined_island<dim,spacedim>
12513  (cell->neighbor(face),
12514  allow_anisotropic_smoothing);
12515  }
12516  }
12517  else
12518  {
12519  // variable to store the cell refine case needed to fulfill
12520  // all smoothing requirements
12521  RefinementCase<dim> smoothing_cell_refinement_case
12523 
12524  // use second algorithm, do the check individually for each
12525  // direction
12526  for (unsigned int face_pair=0;
12527  face_pair<GeometryInfo<dim>::faces_per_cell/2; ++face_pair)
12528  {
12529  // variable to store the cell refine case needed to refine
12530  // at the current face pair in the same way as the
12531  // neighbors do...
12532  RefinementCase<dim> directional_cell_refinement_case
12534 
12535  for (unsigned int face_index=0; face_index<2; ++face_index)
12536  {
12537  unsigned int face=2*face_pair+face_index;
12538  // variable to store the refine case (to come) of the
12539  // face under consideration
12540  RefinementCase<dim-1> expected_face_ref_case
12541  = RefinementCase<dim-1>::no_refinement;
12542 
12543  if (cell->neighbor(face).state() == IteratorState::valid)
12544  face_will_be_refined_by_neighbor<dim,spacedim>(cell,face,expected_face_ref_case);
12545  // now extract which refine case would be necessary to
12546  // achieve the same face refinement. set the
12547  // intersection with other requirements for the same
12548  // direction.
12549 
12550  // note: using the intersection is not an obvious
12551  // decision, we could also argue that it is more
12552  // natural to use the union. however, intersection is
12553  // the less aggressive tactic and favours a smaller
12554  // number of refined cells over an intensive
12555  // smoothing. this way we try not to lose too much of
12556  // the effort we put in anisotropic refinement
12557  // indicators due to overly aggressive smoothing...
12558  directional_cell_refinement_case
12559  = (directional_cell_refinement_case &
12561  expected_face_ref_case,
12562  face,
12563  cell->face_orientation(face),
12564  cell->face_flip(face),
12565  cell->face_rotation(face)));
12566  }//for both face indices
12567  // if both requirements sum up to something useful, add
12568  // this to the refine case for smoothing. note: if
12569  // directional_cell_refinement_case is isotropic still,
12570  // then something went wrong...
12571  Assert(directional_cell_refinement_case <
12573  ExcInternalError());
12574  smoothing_cell_refinement_case = smoothing_cell_refinement_case |
12575  directional_cell_refinement_case;
12576  }//for all face_pairs
12577  // no we collected contributions from all directions. combine
12578  // the new flags with the existing refine case, but only if
12579  // smoothing is required
12580  if (smoothing_cell_refinement_case)
12581  {
12582  cell->clear_coarsen_flag();
12583  cell->set_refine_flag(cell->refine_flag_set() |
12584  smoothing_cell_refinement_case);
12585  }
12586  }
12587  }
12588 }
12589 
12590 
12591 template <int dim, int spacedim>
12593 {
12594  // save the flags to determine whether something was changed in the
12595  // course of this function
12596  std::vector<bool> flags_before[2];
12597  save_coarsen_flags (flags_before[0]);
12598  save_refine_flags (flags_before[1]);
12599 
12600  // save the flags at the outset of each loop. we do so in order to
12601  // find out whether something was changed in the present loop, in
12602  // which case we would have to re-run the loop. the other
12603  // possibility to find this out would be to set a flag
12604  // @p{something_changed} to true each time we change something.
12605  // however, sometimes one change in one of the parts of the loop is
12606  // undone by another one, so we might end up in an endless loop. we
12607  // could be tempted to break this loop at an arbitrary number of
12608  // runs, but that would not be a clean solution, since we would
12609  // either have to 1/ break the loop too early, in which case the
12610  // promise that a second call to this function immediately after the
12611  // first one does not change anything, would be broken, or 2/ we do
12612  // as many loops as there are levels. we know that information is
12613  // transported over one level in each run of the loop, so this is
12614  // enough. Unfortunately, each loop is rather expensive, so we chose
12615  // the way presented here
12616  std::vector<bool> flags_before_loop[2] = {flags_before[0],
12617  flags_before[1]
12618  };
12619 
12620  // now for what is done in each loop: we have to fulfill several
12621  // tasks at the same time, namely several mesh smoothing algorithms
12622  // and mesh regularisation, by which we mean that the next mesh
12623  // fulfills several requirements such as no double refinement at
12624  // each face or line, etc.
12625  //
12626  // since doing these things at once seems almost impossible (in the
12627  // first year of this library, they were done in two functions, one
12628  // for refinement and one for coarsening, and most things within
12629  // these were done at once, so the code was rather impossible to
12630  // join into this, only, function), we do them one after each
12631  // other. the order in which we do them is such that the important
12632  // tasks, namely regularisation, are done last and the least
12633  // important things are done the first. the following order is
12634  // chosen:
12635  //
12636  // 0/ Only if coarsest_level_1 or patch_level_1 is set: clear all
12637  // coarsen flags on level 1 to avoid level 0 cells being created
12638  // by coarsening. As coarsen flags will never be added, this can
12639  // be done once and for all before the actual loop starts.
12640  //
12641  // 1/ do not coarsen a cell if 'most of the neighbors' will be
12642  // refined after the step. This is to prevent occurrence of
12643  // unrefined islands.
12644  //
12645  // 2/ eliminate refined islands in the interior and at the
12646  // boundary. since they don't do much harm besides increasing the
12647  // number of degrees of freedom, doing this has a rather low
12648  // priority.
12649  //
12650  // 3/ limit the level difference of neighboring cells at each
12651  // vertex.
12652  //
12653  // 4/ eliminate unrefined islands. this has higher priority since
12654  // this diminishes the approximation properties not only of the
12655  // unrefined island, but also of the surrounding patch.
12656  //
12657  // 5/ ensure patch level 1. Then the triangulation consists of
12658  // patches, i.e. of cells that are refined once. It follows that
12659  // if at least one of the children of a cell is or will be
12660  // refined than all children need to be refined. This step only
12661  // sets refinement flags and does not set coarsening flags. If
12662  // the patch_level_1 flag is set, then
12663  // eliminate_unrefined_islands, eliminate_refined_inner_islands
12664  // and eliminate_refined_boundary_islands will be fulfilled
12665  // automatically and do not need to be enforced separately.
12666  //
12667  // 6/ take care of the requirement that no double refinement is done
12668  // at each face
12669  //
12670  // 7/ take care that no double refinement is done at each line in 3d
12671  // or higher dimensions.
12672  //
12673  // 8/ make sure that all children of each cell are either flagged
12674  // for coarsening or none of the children is
12675  //
12676  // For some of these steps, it is known that they interact. Namely,
12677  // it is not possible to guarantee that after step 6 another step 5
12678  // would have no effect; the same holds for the opposite order and
12679  // also when taking into account step 7. however, it is important to
12680  // guarantee that step five or six do not undo something that step 5
12681  // did, and step 7 not something of step 6, otherwise the
12682  // requirements will not be satisfied even if the loop
12683  // terminates. this is accomplished by the fact that steps 5 and 6
12684  // only *add* refinement flags and delete coarsening flags
12685  // (therefore, step 6 can't undo something that step 4 already did),
12686  // and step 7 only deletes coarsening flags, never adds some. step 7
12687  // needs also take care that it won't tag cells for refinement for
12688  // which some neighbors are more refined or will be refined.
12689 
12691  // STEP 0:
12692  // Only if coarsest_level_1 or patch_level_1 is set: clear all
12693  // coarsen flags on level 1 to avoid level 0 cells being created
12694  // by coarsening.
12695  if (((smooth_grid & coarsest_level_1) ||
12696  (smooth_grid & patch_level_1)) && n_levels()>=2)
12697  {
12699  cell=begin_active(1),
12700  endc=end_active(1);
12701 
12702  for (; cell!=endc; ++cell)
12703  cell->clear_coarsen_flag();
12704  }
12705 
12706  bool mesh_changed_in_this_loop = false;
12707  do
12708  {
12710  // STEP 1:
12711  // do not coarsen a cell if 'most of the neighbors' will be
12712  // refined after the step. This is to prevent the occurrence
12713  // of unrefined islands. If patch_level_1 is set, this will
12714  // be automatically fulfilled.
12715  if (smooth_grid & do_not_produce_unrefined_islands &&
12716  !(smooth_grid & patch_level_1))
12717  {
12718  cell_iterator cell;
12719  const cell_iterator endc = end();
12720 
12721  for (cell=begin(); cell!=endc; ++cell)
12722  {
12723  // only do something if this
12724  // cell will be coarsened
12725  if (!cell->active() && cell_will_be_coarsened(cell))
12726  possibly_do_not_produce_unrefined_islands<dim,spacedim>(cell);
12727  }
12728  }
12729 
12730 
12732  // STEP 2:
12733  // eliminate refined islands in the interior and at the
12734  // boundary. since they don't do much harm besides increasing
12735  // the number of degrees of freedom, doing this has a rather
12736  // low priority. If patch_level_1 is set, this will be
12737  // automatically fulfilled.
12738  //
12739  // there is one corner case to consider: if this is a
12740  // distributed triangulation, there may be refined islands on
12741  // the boundary of which we own only part (e.g. a single cell
12742  // in the corner of a domain). the rest of the island is
12743  // ghost cells and it *looks* like the area around it
12744  // (artificial cells) are coarser but this is only because
12745  // they may actually be equally fine on other
12746  // processors. it's hard to detect this case but we can do
12747  // the following: only set coarsen flags to remove this
12748  // refined island if all cells we want to set flags on are
12749  // locally owned
12750  if (smooth_grid & (eliminate_refined_inner_islands |
12751  eliminate_refined_boundary_islands) &&
12752  !(smooth_grid & patch_level_1))
12753  {
12754  cell_iterator cell;
12755  const cell_iterator endc = end();
12756 
12757  for (cell=begin(); cell!=endc; ++cell)
12758  if (!cell->active() ||
12759  (cell->active() &&
12760  cell->refine_flag_set() &&
12761  cell->is_locally_owned()))
12762  {
12763  // check whether all children are active, i.e. not
12764  // refined themselves. This is a precondition that the
12765  // children may be coarsened away. If the cell is only
12766  // flagged for refinement, then all future children
12767  // will be active
12768  bool all_children_active = true;
12769  if (!cell->active())
12770  for (unsigned int c=0; c<cell->n_children(); ++c)
12771  if (!cell->child(c)->active() ||
12772  cell->child(c)->is_ghost() ||
12773  cell->child(c)->is_artificial())
12774  {
12775  all_children_active = false;
12776  break;
12777  }
12778 
12779  if (all_children_active)
12780  {
12781  // count number of refined and unrefined neighbors
12782  // of cell. neighbors on lower levels are counted
12783  // as unrefined since they can only get to the
12784  // same level as this cell by the next refinement
12785  // cycle
12786  unsigned int unrefined_neighbors = 0,
12787  total_neighbors = 0;
12788 
12789  for (unsigned int n=0; n<GeometryInfo<dim>::faces_per_cell; ++n)
12790  {
12791  const cell_iterator neighbor = cell->neighbor(n);
12792  if (neighbor.state() == IteratorState::valid)
12793  {
12794  ++total_neighbors;
12795 
12796  if (!face_will_be_refined_by_neighbor(cell,n))
12797  ++unrefined_neighbors;
12798  }
12799 
12800  }
12801 
12802  // if all neighbors unrefined: mark this cell for
12803  // coarsening or don't refine if marked for that
12804  //
12805  // also do the distinction between the two
12806  // versions of the eliminate_refined_*_islands
12807  // flag
12808  //
12809  // the last check is whether there are any
12810  // neighbors at all. if not so, then we are (e.g.)
12811  // on the coarsest grid with one cell, for which,
12812  // of course, we do not remove the refine flag.
12813  if ((unrefined_neighbors == total_neighbors)
12814  &&
12815  (((unrefined_neighbors==GeometryInfo<dim>::faces_per_cell) &&
12816  (smooth_grid & eliminate_refined_inner_islands)) ||
12817  ((unrefined_neighbors<GeometryInfo<dim>::faces_per_cell) &&
12818  (smooth_grid & eliminate_refined_boundary_islands)) )
12819  &&
12820  (total_neighbors != 0))
12821  {
12822  if (!cell->active())
12823  for (unsigned int c=0; c<cell->n_children(); ++c)
12824  {
12825  cell->child(c)->clear_refine_flag ();
12826  cell->child(c)->set_coarsen_flag ();
12827  }
12828  else
12829  cell->clear_refine_flag();
12830  }
12831  }
12832  }
12833  }
12834 
12836  // STEP 3:
12837  // limit the level difference of neighboring cells at each
12838  // vertex.
12839  //
12840  // in case of anisotropic refinement this does not make
12841  // sense. as soon as one cell is anisotropically refined, an
12842  // Assertion is thrown. therefore we can ignore this problem
12843  // later on
12844  if (smooth_grid & limit_level_difference_at_vertices)
12845  {
12846  Assert(!anisotropic_refinement,
12847  ExcMessage("In case of anisotropic refinement the "
12848  "limit_level_difference_at_vertices flag for "
12849  "mesh smoothing must not be set!"));
12850 
12851  // store highest level one of the cells adjacent to a vertex
12852  // belongs to
12853  std::vector<int> vertex_level (vertices.size(), 0);
12854  active_cell_iterator cell = begin_active(),
12855  endc = end();
12856  for (; cell!=endc; ++cell)
12857  {
12858  if (cell->refine_flag_set())
12859  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell;
12860  ++vertex)
12861  vertex_level[cell->vertex_index(vertex)]
12862  = std::max (vertex_level[cell->vertex_index(vertex)],
12863  cell->level()+1);
12864  else if (!cell->coarsen_flag_set())
12865  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell;
12866  ++vertex)
12867  vertex_level[cell->vertex_index(vertex)]
12868  = std::max (vertex_level[cell->vertex_index(vertex)],
12869  cell->level());
12870  else
12871  {
12872  // if coarsen flag is set then tentatively assume
12873  // that the cell will be coarsened. this isn't
12874  // always true (the coarsen flag could be removed
12875  // again) and so we may make an error here
12876  Assert (cell->coarsen_flag_set(), ExcInternalError());
12877  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell;
12878  ++vertex)
12879  vertex_level[cell->vertex_index(vertex)]
12880  = std::max (vertex_level[cell->vertex_index(vertex)],
12881  cell->level()-1);
12882  }
12883  }
12884 
12885 
12886  // loop over all cells in reverse order. do so because we
12887  // can then update the vertex levels on the adjacent
12888  // vertices and maybe already flag additional cells in this
12889  // loop
12890  //
12891  // note that not only may we have to add additional
12892  // refinement flags, but we will also have to remove
12893  // coarsening flags on cells adjacent to vertices that will
12894  // see refinement
12895  for (cell=last_active(); cell != endc; --cell)
12896  if (cell->refine_flag_set() == false)
12897  {
12898  for (unsigned int vertex=0;
12899  vertex<GeometryInfo<dim>::vertices_per_cell; ++vertex)
12900  if (vertex_level[cell->vertex_index(vertex)] >=
12901  cell->level()+1)
12902  {
12903  // remove coarsen flag...
12904  cell->clear_coarsen_flag();
12905 
12906  // ...and if necessary also refine the current
12907  // cell, at the same time updating the level
12908  // information about vertices
12909  if (vertex_level[cell->vertex_index(vertex)] >
12910  cell->level()+1)
12911  {
12912  cell->set_refine_flag();
12913 
12914  for (unsigned int v=0; v<GeometryInfo<dim>::vertices_per_cell;
12915  ++v)
12916  vertex_level[cell->vertex_index(v)]
12917  = std::max (vertex_level[cell->vertex_index(v)],
12918  cell->level()+1);
12919  }
12920 
12921  // continue and see whether we may, for example,
12922  // go into the inner'if'
12923  // above based on a
12924  // different vertex
12925  }
12926  }
12927  }
12928 
12930  // STEP 4:
12931  // eliminate unrefined islands. this has higher priority
12932  // since this diminishes the approximation properties not
12933  // only of the unrefined island, but also of the surrounding
12934  // patch.
12935  //
12936  // do the loop from finest to coarsest cells since we may
12937  // trigger a cascade by marking cells for refinement which
12938  // may trigger more cells further down below
12939  if (smooth_grid & eliminate_unrefined_islands)
12940  {
12941  active_cell_iterator cell=last_active(),
12942  endc=end();
12943 
12944  for (; cell != endc; --cell)
12945  // only do something if cell is not already flagged for
12946  // (isotropic) refinement
12947  if (cell->refine_flag_set() != RefinementCase<dim>::isotropic_refinement)
12948  possibly_refine_unrefined_island<dim,spacedim>
12949  (cell,
12950  (smooth_grid & allow_anisotropic_smoothing) != 0);
12951  }
12952 
12954  // STEP 5:
12955  // ensure patch level 1.
12956  //
12957  // Introduce some terminology:
12958  // - a cell that is refined
12959  // once is a patch of
12960  // level 1 simply called patch.
12961  // - a cell that is globally
12962  // refined twice is called
12963  // a patch of level 2.
12964  // - patch level n says that
12965  // the triangulation consists
12966  // of patches of level n.
12967  // This makes sense only
12968  // if the grid is already at
12969  // least n times globally
12970  // refined.
12971  //
12972  // E.g. from patch level 1 follows: if at least one of the
12973  // children of a cell is or will be refined than enforce all
12974  // children to be refined.
12975 
12976  // This step 4 only sets refinement flags and does not set
12977  // coarsening flags.
12978  if (smooth_grid & patch_level_1)
12979  {
12980 
12981  // An important assumption (A) is that before calling this
12982  // function the grid was already of patch level 1.
12983 
12984  // loop over all cells whose children are all active. (By
12985  // assumption (A) either all or none of the children are
12986  // active). If the refine flag of at least one of the
12987  // children is set then set_refine_flag and
12988  // clear_coarsen_flag of all children.
12989  for (cell_iterator cell = begin(); cell != end(); ++cell)
12990  if (!cell->active())
12991  {
12992  // ensure the invariant. we can then check whether all
12993  // of its children are further refined or not by
12994  // simply looking at the first child
12995  Assert (cell_is_patch_level_1(cell),
12996  ExcInternalError());
12997  if (cell->child(0)->has_children() == true)
12998  continue;
12999 
13000  // cell is found to be a patch. combine the refine
13001  // cases of all children
13003  for (unsigned int i=0; i<cell->n_children(); ++i)
13004  combined_ref_case = combined_ref_case |
13005  cell->child(i)->refine_flag_set();
13006  if (combined_ref_case != RefinementCase<dim>::no_refinement)
13007  for (unsigned int i=0; i<cell->n_children(); ++i)
13008  {
13009  cell_iterator child = cell->child(i);
13010 
13011  child->clear_coarsen_flag();
13012  child->set_refine_flag(combined_ref_case);
13013  }
13014  }
13015 
13016  // The code above dealt with the case where we may get a
13017  // non-patch_level_1 mesh from refinement. Now also deal
13018  // with the case where we could get such a mesh by
13019  // coarsening. Coarsen the children (and remove the
13020  // grandchildren) only if all cell->grandchild(i)
13021  // ->coarsen_flag_set() are set.
13022  //
13023  // for a case where this is a bit tricky, take a look at the
13024  // mesh_smoothing_0[12] testcases
13025  for (cell_iterator cell = begin(); cell != end(); ++cell)
13026  {
13027  // check if this cell has active grandchildren. note
13028  // that we know that it is patch_level_1, i.e. if one of
13029  // its children is active then so are all, and it isn't
13030  // going to have any grandchildren at all:
13031  if (cell->active()
13032  ||
13033  cell->child(0)->active())
13034  continue;
13035 
13036  // cell is not active, and so are none of its
13037  // children. check the grandchildren. note that the
13038  // children are also patch_level_1, and so we only ever
13039  // need to check their first child
13040  const unsigned int n_children=cell->n_children();
13041  bool has_active_grandchildren = false;
13042 
13043  for (unsigned int i=0; i<n_children; ++i)
13044  if (cell->child(i)->child(0)->active())
13045  {
13046  has_active_grandchildren = true;
13047  break;
13048  }
13049 
13050  if (has_active_grandchildren == false)
13051  continue;
13052 
13053 
13054  // ok, there are active grandchildren. see if either all
13055  // or none of them are flagged for coarsening
13056  unsigned int n_grandchildren=0;
13057 
13058  // count all coarsen flags of the grandchildren.
13059  unsigned int n_coarsen_flags=0;
13060 
13061  // cell is not a patch (of level 1) as it has a
13062  // grandchild. Is cell a patch of level 2?? Therefore:
13063  // find out whether all cell->child(i) are patches
13064  for (unsigned int c=0; c<n_children; ++c)
13065  {
13066  // get at the child. by assumption (A), and the
13067  // check by which we got here, the child is not
13068  // active
13069  cell_iterator child=cell->child(c);
13070 
13071  const unsigned int nn_children=child->n_children();
13072  n_grandchildren += nn_children;
13073 
13074  // if child is found to be a patch of active cells
13075  // itself, then add up how many of its children are
13076  // supposed to be coarsened
13077  if (child->child(0)->active())
13078  for (unsigned int cc=0; cc<nn_children; ++cc)
13079  if (child->child(cc)->coarsen_flag_set())
13080  ++n_coarsen_flags;
13081  }
13082 
13083  // if not all grandchildren are supposed to be coarsened
13084  // (e.g. because some simply don't have the flag set, or
13085  // because they are not active and therefore cannot
13086  // carry the flag), then remove the coarsen flag from
13087  // all of the active grandchildren. note that there may
13088  // be coarsen flags on the grandgrandchildren -- we
13089  // don't clear them here, but we'll get to them in later
13090  // iterations if necessary
13091  //
13092  // there is nothing we have to do if no coarsen flags
13093  // have been set at all
13094  if ((n_coarsen_flags != n_grandchildren)
13095  &&
13096  (n_coarsen_flags > 0))
13097  for (unsigned int c=0; c<n_children; ++c)
13098  {
13099  const cell_iterator child = cell->child(c);
13100  if (child->child(0)->active())
13101  for (unsigned int cc=0; cc<child->n_children(); ++cc)
13102  child->child(cc)->clear_coarsen_flag();
13103  }
13104  }
13105  }
13106 
13108  //
13109  // at the boundary we could end up with cells with negative
13110  // volume or at least with a part, that is negative, if the
13111  // cell is refined anisotropically. we have to check, whether
13112  // that can happen
13114 
13116  // STEP 6:
13117  // take care of the requirement that no
13118  // double refinement is done at each face
13119  //
13120  // in case of anisotropic refinement it is only likely, but
13121  // not sure, that the cells, which are more refined along a
13122  // certain face common to two cells are on a higher
13123  // level. therefore we cannot be sure, that the requirement
13124  // of no double refinement is fulfilled after a single pass
13125  // of the following actions. We could just wait for the next
13126  // global loop. when this function terminates, the
13127  // requirement will be fulfilled. However, it might be faster
13128  // to insert an inner loop here.
13129  bool changed = true;
13130  while (changed)
13131  {
13132  changed=false;
13133  active_cell_iterator cell=last_active(),
13134  endc=end();
13135 
13136  for (; cell != endc; --cell)
13137  if (cell->refine_flag_set())
13138  {
13139  // loop over neighbors of cell
13140  for (unsigned int i=0; i<GeometryInfo<dim>::faces_per_cell; ++i)
13141  {
13142  // only do something if the face is not at the
13143  // boundary and if the face will be refined with
13144  // the RefineCase currently flagged for
13145  if (cell->neighbor(i).state() == IteratorState::valid &&
13146  (GeometryInfo<dim>::face_refinement_case(cell->refine_flag_set(),
13147  i)
13149  {
13150  // 1) if the neighbor has children: nothing to
13151  // worry about. 2) if the neighbor is active
13152  // and a coarser one, ensure, that its
13153  // refine_flag is set 3) if the neighbor is
13154  // active and as refined along the face as our
13155  // current cell, make sure, that no
13156  // coarsen_flag is set. if we remove the
13157  // coarsen flag of our neighbor,
13158  // fix_coarsen_flags() makes sure, that the
13159  // mother cell will not be coarsened
13160  if (cell->neighbor(i)->active())
13161  {
13162  if (cell->neighbor_is_coarser(i))
13163  {
13164  if (cell->neighbor(i)->coarsen_flag_set())
13165  cell->neighbor(i)->clear_coarsen_flag();
13166  // we'll set the refine flag for this
13167  // neighbor below. we note, that we
13168  // have changed something by setting
13169  // the changed flag to true. We do not
13170  // need to do so, if we just removed
13171  // the coarsen flag, as the changed
13172  // flag only indicates the need to
13173  // re-run the inner loop. however, we
13174  // only loop over cells flagged for
13175  // refinement here, so nothing to
13176  // worry about if we remove coarsen
13177  // flags
13178 
13179  if (dim==2)
13180  {
13181  if (smooth_grid & allow_anisotropic_smoothing)
13182  changed=cell->neighbor(i)->flag_for_face_refinement(cell->neighbor_of_coarser_neighbor(i).first,
13183  RefinementCase<dim-1>::cut_x);
13184  else
13185  {
13186  if (!cell->neighbor(i)->refine_flag_set())
13187  changed=true;
13188  cell->neighbor(i)->set_refine_flag();
13189  }
13190  }
13191  else //i.e. if (dim==3)
13192  {
13193 // ugly situations might arise here, consider the following situation, which
13194 // shows neighboring cells at the common face, where the upper right element is
13195 // coarser at the given face. Now the upper child element of the lower left
13196 // wants to refine according to cut_z, such that there is a 'horizontal'
13197 // refinement of the face marked with #####
13198 //
13199 // / /
13200 // / /
13201 // *---------------*
13202 // | |
13203 // | |
13204 // | |
13205 // | |
13206 // | |
13207 // | | /
13208 // | |/
13209 // *---------------*
13210 //
13211 //
13212 // *---------------*
13213 // /| /|
13214 // / | ##### / |
13215 // | |
13216 // *---------------*
13217 // /| /|
13218 // / | / |
13219 // | |
13220 // *---------------*
13221 // / /
13222 // / /
13223 //
13224 // this introduces too many hanging nodes and the neighboring (coarser) cell
13225 // (upper right) has to be refined. If it is only refined according to cut_z,
13226 // then everything is ok:
13227 //
13228 // / /
13229 // / /
13230 // *---------------*
13231 // | |
13232 // | | /
13233 // | |/
13234 // *---------------*
13235 // | |
13236 // | | /
13237 // | |/
13238 // *---------------*
13239 //
13240 //
13241 // *---------------*
13242 // /| /|
13243 // / *---------------*
13244 // /| /|
13245 // *---------------*
13246 // /| /|
13247 // / | / |
13248 // | |
13249 // *---------------*
13250 // / /
13251 // / /
13252 //
13253 // if however the cell wants to refine itself in an other way, or if we disallow
13254 // anisotropic smoothing, then simply refining the neighbor isotropically is not
13255 // going to work, since this introduces a refinement of face ##### with both
13256 // cut_x and cut_y, which is not possible:
13257 //
13258 // / / /
13259 // / / /
13260 // *-------*-------*
13261 // | | |
13262 // | | | /
13263 // | | |/
13264 // *-------*-------*
13265 // | | |
13266 // | | | /
13267 // | | |/
13268 // *-------*-------*
13269 //
13270 //
13271 // *---------------*
13272 // /| /|
13273 // / *---------------*
13274 // /| /|
13275 // *---------------*
13276 // /| /|
13277 // / | / |
13278 // | |
13279 // *---------------*
13280 // / /
13281 // / /
13282 //
13283 // thus, in this case we also need to refine our current cell in the new
13284 // direction:
13285 //
13286 // / / /
13287 // / / /
13288 // *-------*-------*
13289 // | | |
13290 // | | | /
13291 // | | |/
13292 // *-------*-------*
13293 // | | |
13294 // | | | /
13295 // | | |/
13296 // *-------*-------*
13297 //
13298 //
13299 // *-------*-------*
13300 // /| /| /|
13301 // / *-------*-------*
13302 // /| /| /|
13303 // *-------*-------*
13304 // /| / /|
13305 // / | / |
13306 // | |
13307 // *---------------*
13308 // / /
13309 // / /
13310 
13311  std::pair<unsigned int, unsigned int> nb_indices
13312  =cell->neighbor_of_coarser_neighbor(i);
13313  unsigned int refined_along_x=0,
13314  refined_along_y=0,
13315  to_be_refined_along_x=0,
13316  to_be_refined_along_y=0;
13317 
13318  const int this_face_index=cell->face_index(i);
13319 
13320 // step 1: detect, along which axis the face is currently refined
13321  if ((this_face_index
13322  == cell->neighbor(i)->face(nb_indices.first)->child_index(0)) ||
13323  (this_face_index
13324  == cell->neighbor(i)->face(nb_indices.first)->child_index(1)))
13325  {
13326  // this might be an
13327  // anisotropic child. get the
13328  // face refine case of the
13329  // neighbors face and count
13330  // refinements in x and y
13331  // direction.
13332  RefinementCase<dim-1> frc=cell->neighbor(i)->face(nb_indices.first)->refinement_case();
13333  if (frc & RefinementCase<dim>::cut_x)
13334  ++refined_along_x;
13335  if (frc & RefinementCase<dim>::cut_y)
13336  ++refined_along_y;
13337  }
13338  else
13339  // this has to be an isotropic
13340  // child
13341  {
13342  ++refined_along_x;
13343  ++refined_along_y;
13344  }
13345 // step 2: detect, along which axis the face has to be refined given the current
13346 // refine flag
13347  RefinementCase<dim-1> flagged_frc=
13348  GeometryInfo<dim>::face_refinement_case(cell->refine_flag_set(),
13349  i,
13350  cell->face_orientation(i),
13351  cell->face_flip(i),
13352  cell->face_rotation(i));
13353  if (flagged_frc & RefinementCase<dim>::cut_x)
13354  ++to_be_refined_along_x;
13355  if (flagged_frc & RefinementCase<dim>::cut_y)
13356  ++to_be_refined_along_y;
13357 
13358 // step 3: set the refine flag of the (coarser and active) neighbor.
13359  if ((smooth_grid & allow_anisotropic_smoothing) ||
13360  cell->neighbor(i)->refine_flag_set())
13361  {
13362  if (refined_along_x + to_be_refined_along_x > 1)
13363  changed |= cell->neighbor(i)->flag_for_face_refinement(nb_indices.first,
13365  if (refined_along_y + to_be_refined_along_y > 1)
13366  changed |= cell->neighbor(i)->flag_for_face_refinement(nb_indices.first,
13368  }
13369  else
13370  {
13371  if (cell->neighbor(i)->refine_flag_set()!=RefinementCase<dim>::isotropic_refinement)
13372  changed=true;
13373  cell->neighbor(i)->set_refine_flag();
13374  }
13375 
13376 // step 4: if necessary (see above) add to the refine flag of the current cell
13377  cell_iterator nb=cell->neighbor(i);
13378  RefinementCase<dim-1> nb_frc
13379  = GeometryInfo<dim>::face_refinement_case(nb->refine_flag_set(),
13380  nb_indices.first,
13381  nb->face_orientation(nb_indices.first),
13382  nb->face_flip(nb_indices.first),
13383  nb->face_rotation(nb_indices.first));
13384  if ((nb_frc & RefinementCase<dim>::cut_x) &&
13385  !(refined_along_x || to_be_refined_along_x))
13386  changed |= cell->flag_for_face_refinement(i,RefinementCase<dim-1>::cut_axis(0));
13387  if ((nb_frc & RefinementCase<dim>::cut_y) &&
13388  !(refined_along_y || to_be_refined_along_y))
13389  changed |= cell->flag_for_face_refinement(i,RefinementCase<dim-1>::cut_axis(1));
13390  }
13391  }// if neighbor is coarser
13392  else // -> now the neighbor is not coarser
13393  {
13394  cell->neighbor(i)->clear_coarsen_flag();
13395  const unsigned int nb_nb=cell->neighbor_of_neighbor(i);
13396  const cell_iterator neighbor=cell->neighbor(i);
13397  RefinementCase<dim-1> face_ref_case=
13398  GeometryInfo<dim>::face_refinement_case(neighbor->refine_flag_set(),
13399  nb_nb,
13400  neighbor->face_orientation(nb_nb),
13401  neighbor->face_flip(nb_nb),
13402  neighbor->face_rotation(nb_nb));
13403  RefinementCase<dim-1> needed_face_ref_case
13404  =GeometryInfo<dim>::face_refinement_case(cell->refine_flag_set(),
13405  i,
13406  cell->face_orientation(i),
13407  cell->face_flip(i),
13408  cell->face_rotation(i));
13409  // if the neighbor wants to refine the
13410  // face with cut_x and we want cut_y
13411  // or vice versa, we have to refine
13412  // isotropically at the given face
13413  if ((face_ref_case==RefinementCase<dim>::cut_x && needed_face_ref_case==RefinementCase<dim>::cut_y) ||
13414  (face_ref_case==RefinementCase<dim>::cut_y && needed_face_ref_case==RefinementCase<dim>::cut_x))
13415  {
13416  changed=cell->flag_for_face_refinement(i, face_ref_case);
13417  neighbor->flag_for_face_refinement(nb_nb, needed_face_ref_case);
13418  }
13419  }
13420  }
13421  else //-> the neighbor is not active
13422  {
13423  RefinementCase<dim-1> face_ref_case = cell->face(i)->refinement_case(),
13424  needed_face_ref_case = GeometryInfo<dim>::face_refinement_case(cell->refine_flag_set(),
13425  i,
13426  cell->face_orientation(i),
13427  cell->face_flip(i),
13428  cell->face_rotation(i));
13429  // if the face is refined with cut_x and
13430  // we want cut_y or vice versa, we have to
13431  // refine isotropically at the given face
13432  if ((face_ref_case==RefinementCase<dim>::cut_x && needed_face_ref_case==RefinementCase<dim>::cut_y) ||
13433  (face_ref_case==RefinementCase<dim>::cut_y && needed_face_ref_case==RefinementCase<dim>::cut_x))
13434  changed=cell->flag_for_face_refinement(i, face_ref_case);
13435  }
13436  }
13437  }
13438  }
13439  }
13440 
13442  // STEP 7:
13443  // take care that no double refinement
13444  // is done at each line in 3d or higher
13445  // dimensions.
13447 
13449  // STEP 8:
13450  // make sure that all children of each
13451  // cell are either flagged for coarsening
13452  // or none of the children is
13453  fix_coarsen_flags ();
13454  // get the refinement and coarsening
13455  // flags
13456  std::vector<bool> flags_after_loop[2];
13457  save_coarsen_flags (flags_after_loop[0]);
13458  save_refine_flags (flags_after_loop[1]);
13459 
13460  // find out whether something was
13461  // changed in this loop
13462  mesh_changed_in_this_loop
13463  = ((flags_before_loop[0] != flags_after_loop[0]) ||
13464  (flags_before_loop[1] != flags_after_loop[1]));
13465 
13466  // set the flags for the next loop
13467  // already
13468  flags_before_loop[0].swap(flags_after_loop[0]);
13469  flags_before_loop[1].swap(flags_after_loop[1]);
13470  }
13471  while (mesh_changed_in_this_loop);
13472 
13473 
13474  // find out whether something was really changed in this
13475  // function. Note that @p{flags_before_loop} represents the state
13476  // after the last loop, i.e. the present state
13477  return ((flags_before[0] != flags_before_loop[0]) ||
13478  (flags_before[1] != flags_before_loop[1]));
13479 }
13480 
13481 
13482 
13483 
13484 template <int dim, int spacedim>
13485 void Triangulation<dim, spacedim>::write_bool_vector (const unsigned int magic_number1,
13486  const std::vector<bool> &v,
13487  const unsigned int magic_number2,
13488  std::ostream &out)
13489 {
13490  const unsigned int N = v.size();
13491  unsigned char *flags = new unsigned char[N/8+1];
13492  for (unsigned int i=0; i<N/8+1; ++i) flags[i]=0;
13493 
13494  for (unsigned int position=0; position<N; ++position)
13495  flags[position/8] |= (v[position] ? (1<<(position%8)) : 0);
13496 
13497  AssertThrow (out, ExcIO());
13498 
13499  // format:
13500  // 0. magic number
13501  // 1. number of flags
13502  // 2. the flags
13503  // 3. magic number
13504  out << magic_number1 << ' ' << N << std::endl;
13505  for (unsigned int i=0; i<N/8+1; ++i)
13506  out << static_cast<unsigned int>(flags[i]) << ' ';
13507 
13508  out << std::endl << magic_number2 << std::endl;
13509 
13510  delete[] flags;
13511 
13512  AssertThrow (out, ExcIO());
13513 }
13514 
13515 
13516 template <int dim, int spacedim>
13517 void Triangulation<dim, spacedim>::read_bool_vector (const unsigned int magic_number1,
13518  std::vector<bool> &v,
13519  const unsigned int magic_number2,
13520  std::istream &in)
13521 {
13522  AssertThrow (in, ExcIO());
13523 
13524  unsigned int magic_number;
13525  in >> magic_number;
13526  AssertThrow (magic_number==magic_number1, ExcGridReadError());
13527 
13528  unsigned int N;
13529  in >> N;
13530  v.resize (N);
13531 
13532  unsigned char *flags = new unsigned char[N/8+1];
13533  unsigned short int tmp;
13534  for (unsigned int i=0; i<N/8+1; ++i)
13535  {
13536  in >> tmp;
13537  flags[i] = tmp;
13538  }
13539 
13540  for (unsigned int position=0; position!=N; ++position)
13541  v[position] = (flags[position/8] & (1<<(position%8)));
13542 
13543  in >> magic_number;
13544  AssertThrow (magic_number==magic_number2, ExcGridReadError());
13545 
13546  delete[] flags;
13547 
13548  AssertThrow (in, ExcIO());
13549 }
13550 
13551 
13552 
13553 template <int dim, int spacedim>
13554 std::size_t
13556 {
13557  std::size_t mem = 0;
13559  for (unsigned int i=0; i<levels.size(); ++i)
13560  mem += MemoryConsumption::memory_consumption (*levels[i]);
13561  mem += MemoryConsumption::memory_consumption (vertices);
13562  mem += MemoryConsumption::memory_consumption (vertices_used);
13563  mem += sizeof(manifold);
13564  mem += sizeof(smooth_grid);
13565  mem += MemoryConsumption::memory_consumption (number_cache);
13566  mem += sizeof (faces);
13567  if (faces)
13568  mem += MemoryConsumption::memory_consumption (*faces);
13569 
13570  return mem;
13571 }
13572 
13573 
13574 
13575 
13576 template<int dim, int spacedim>
13578 {
13579  // don't do anything here. the compiler will automatically convert
13580  // any exceptions created by the destructors of the member variables
13581  // into abort() in order to satisfy the throw()/noexcept
13582  // specification
13583 }
13584 
13585 
13586 // explicit instantiations
13587 #include "tria.inst"
13588 
13589 DEAL_II_NAMESPACE_CLOSE
std::vector< CellData< 1 > > boundary_lines
Definition: tria.h:256
std::vector<::internal::Triangulation::TriaLevel< dim > * > levels
Definition: tria.h:3434
static void compute_number_cache(const Triangulation< dim, spacedim > &triangulation, const unsigned int level_objects, internal::Triangulation::NumberCache< 1 > &number_cache)
Definition: tria.cc:1309
boost::signals2::signal< void()> any_change
Definition: tria.h:2150
unsigned int n_active_cells() const
Definition: tria.cc:11244
void set_boundary(const types::manifold_id number, const Boundary< dim, spacedim > &boundary_object)
Definition: tria.cc:9115
virtual void copy_triangulation(const Triangulation< dim, spacedim > &other_tria)
Definition: tria.cc:9315
unsigned int n_vertices() const
unsigned int n_used_vertices() const
Definition: tria.cc:11772
const types::manifold_id flat_manifold_id
Definition: types.h:234
static const unsigned int invalid_unsigned_int
Definition: types.h:170
void load_user_flags_line(std::istream &in)
Definition: tria.cc:10024
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:576
hex_iterator begin_hex(const unsigned int level=0) const
Definition: tria.cc:11136
static ::ExceptionBase & ExcInteriorLineCantBeBoundary(int arg1, int arg2, types::boundary_id arg3)
const types::subdomain_id invalid_subdomain_id
Definition: types.h:245
void clear_user_flags()
Definition: tria.cc:9889
active_face_iterator begin_active_face() const
Definition: tria.cc:10848
::internal::Triangulation::NumberCache< dim > number_cache
Definition: tria.h:3482
void save_user_flags_quad(std::ostream &out) const
Definition: tria.cc:10123
virtual bool has_hanging_nodes() const
Definition: tria.cc:11366
unsigned int n_raw_cells(const unsigned int level) const
Definition: tria.cc:11310
cell_iterator last() const
Definition: tria.cc:10688
void save_user_pointers_hex(std::vector< void *> &v) const
Definition: tria.cc:10590
vertex_iterator begin_vertex() const
Definition: tria.cc:10892
static RefinementCase< 1 > line_refinement_case(const RefinementCase< dim > &cell_refinement_case, const unsigned int line_no)
static ::ExceptionBase & ExcMultiplySetLineInfoOfLine(int arg1, int arg2)
unsigned int n_cells() const
Definition: tria.cc:11237
std_cxx11::unique_ptr<::internal::Triangulation::TriaFaces< dim > > faces
Definition: tria.h:3441
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
int face(const unsigned int i) const
Definition: tria_object.h:168
unsigned int n_raw_hexs() const
static ::ExceptionBase & ExcIO()
Subscriptor & operator=(const Subscriptor &)
Definition: subscriptor.cc:154
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
std::list< typename Triangulation< dim, spacedim >::cell_iterator > distorted_cells
Definition: tria.h:1549
std::vector< Point< spacedim > > vertices
Definition: tria.h:3447
boost::signals2::signal< void()> clear
Definition: tria.h:2139
static unsigned int line_to_cell_vertices(const unsigned int line, const unsigned int vertex)
static void compute_number_cache(const Triangulation< dim, spacedim > &triangulation, const unsigned int level_objects, internal::Triangulation::NumberCache< 3 > &number_cache)
Definition: tria.cc:1499
IteratorRange< active_cell_iterator > active_cell_iterators() const
Definition: tria.cc:10792
void set_all_refine_flags()
Definition: tria.cc:9592
void load_user_flags(std::istream &in)
Definition: tria.cc:9946
static void compute_number_cache(const Triangulation< dim, spacedim > &triangulation, const unsigned int level_objects, internal::Triangulation::NumberCache< 2 > &number_cache)
Definition: tria.cc:1395
bool anisotropic_refinement
Definition: tria.h:3464
std::vector< unsigned int > n_active_lines_level
Definition: tria.h:346
std::vector< unsigned int > n_hexes_level
Definition: tria.h:453
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:13517
line_iterator begin_line(const unsigned int level=0) const
Definition: tria.cc:10976
virtual ~DistortedCellList() DEAL_II_NOEXCEPT
Definition: tria.cc:13577
line_iterator end_line() const
Definition: tria.cc:11008
boost::signals2::signal< void()> mesh_movement
Definition: tria.h:2100
void join() const
void clear_user_flags_quad()
Definition: tria.cc:9849
unsigned int n_faces() const
Definition: tria.cc:11258
std::vector< unsigned int > n_quads_level
Definition: tria.h:394
static ::ExceptionBase & ExcLineInexistant(int arg1, int arg2)
void flip_all_direction_flags()
Definition: tria.cc:9581
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< 3, spacedim >::DistortedCellList execute_refinement(Triangulation< 3, spacedim > &triangulation, const bool check_for_distorted_cells)
Definition: tria.cc:4928
active_cell_iterator begin_active(const unsigned int level=0) const
Definition: tria.cc:10668
STL namespace.
#define AssertThrow(cond, exc)
Definition: exceptions.h:369
static Triangulation< 1, spacedim >::DistortedCellList execute_refinement(Triangulation< 1, spacedim > &triangulation, const bool)
Definition: tria.cc:4329
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
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:13485
unsigned int n_active_hexs() const
Definition: tria.cc:11710
void save_user_pointers_quad(std::vector< void *> &v) const
Definition: tria.cc:10557
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
static ::ExceptionBase & ExcFacesHaveNoLevel()
std::vector< unsigned int > n_active_quads_level
Definition: tria.h:404
void execute_coarsening()
Definition: tria.cc:12066
const Boundary< dim, spacedim > & get_boundary(const types::manifold_id number) const
Definition: tria.cc:9223
cell_iterator begin(const unsigned int level=0) const
Definition: tria.cc:10648
void load_user_flags_quad(std::istream &in)
Definition: tria.cc:10134
unsigned int n_levels() const
void set_manifold(const types::manifold_id number, const Manifold< dim, spacedim > &manifold_object)
Definition: tria.cc:9123
raw_hex_iterator begin_raw_hex(const unsigned int level=0) const
Definition: tria.cc:11106
std_cxx11::unique_ptr< std::map< unsigned int, types::boundary_id > > vertex_to_boundary_id_map_1d
Definition: tria.h:3498
void load_user_pointers_line(const std::vector< void *> &v)
Definition: tria.cc:10543
unsigned int n_active_faces() const
Definition: tria.cc:11292
raw_quad_iterator begin_raw_quad(const unsigned int level=0) const
Definition: tria.cc:11022
cell_iterator end() const
Definition: tria.cc:10736
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
void load_coarsen_flags(std::istream &out)
Definition: tria.cc:9717
void load_user_flags_hex(std::istream &in)
Definition: tria.cc:10197
active_quad_iterator begin_active_quad(const unsigned int level=0) const
Definition: tria.cc:11077
virtual void execute_coarsening_and_refinement()
Definition: tria.cc:11899
unsigned int n_active_lines() const
Definition: tria.cc:11459
IteratorRange< cell_iterator > cell_iterators_on_level(const unsigned int level) const
Definition: tria.cc:10803
Definition: tria.h:52
virtual bool prepare_coarsening_and_refinement()
Definition: tria.cc:12592
static ::ExceptionBase & ExcMessage(std::string arg1)
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:11943
static unsigned int n_children(const RefinementCase< dim > &refinement_case)
DistortedCellList execute_refinement()
Definition: tria.cc:12032
const bool check_for_distorted_cells
Definition: tria.h:3471
void save_user_flags_line(std::ostream &out) const
Definition: tria.cc:10013
void save_user_pointers_line(std::vector< void *> &v) const
Definition: tria.cc:10530
static bool coarsening_allowed(const typename Triangulation< dim, spacedim >::cell_iterator &cell)
Definition: tria.cc:8869
Triangulation(const MeshSmoothing smooth_grid=none, const bool check_for_distorted_cells=false)
Definition: tria.cc:8948
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:564
static ::ExceptionBase & ExcGridHasInvalidCell(int arg1)
Triangulation & operator=(Triangulation< dim, spacedim > &&tria)
Definition: tria.cc:9020
void clear_user_flags_hex()
Definition: tria.cc:9881
void load_user_indices_line(const std::vector< unsigned int > &v)
Definition: tria.cc:10345
unsigned int global_dof_index
Definition: types.h:88
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)
vertex_iterator end_vertex() const
Definition: tria.cc:10929
unsigned int n_raw_quads() const
Definition: tria.cc:11652
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:313
Signals signals
Definition: tria.h:2183
active_hex_iterator begin_active_hex(const unsigned int level=0) const
Definition: tria.cc:11152
void save_user_indices_line(std::vector< unsigned int > &v) const
Definition: tria.cc:10332
std::map< types::manifold_id, SmartPointer< const Manifold< dim, spacedim >, Triangulation< dim, spacedim > > > manifold
Definition: tria.h:3458
void set_all_manifold_ids(const types::manifold_id number)
Definition: tria.cc:9154
static ::ExceptionBase & ExcCellHasNegativeMeasure(int arg1)
boost::signals2::signal< void()> create
Definition: tria.h:2075
unsigned int max_adjacent_cells() const
Definition: tria.cc:11813
static ::ExceptionBase & ExcInvalidVertexIndex(int arg1, int arg2, int arg3)
bool get_anisotropic_refinement_flag() const
Definition: tria.cc:9746
unsigned int n_quads() const
Definition: tria.cc:11606
void clear_user_data(const unsigned int i)
Definition: tria_objects.h:659
std::vector< bool > vertices_used
Definition: tria.h:3452
void loop(ITERATOR begin, typename identity< ITERATOR >::type end, DOFINFO &dinfo, INFOBOX &info, const std_cxx11::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &cell_worker, const std_cxx11::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &boundary_worker, const std_cxx11::function< void(DOFINFO &, DOFINFO &, typename INFOBOX::CellInfo &, typename INFOBOX::CellInfo &)> &face_worker, ASSEMBLER &assembler, const LoopControl &lctrl=LoopControl())
Definition: loop.h:383
virtual void create_triangulation(const std::vector< Point< spacedim > > &vertices, const std::vector< CellData< dim > > &cells, const SubCellData &subcelldata)
Definition: tria.cc:9401
void save_coarsen_flags(std::ostream &out) const
Definition: tria.cc:9706
void save_user_indices(std::vector< unsigned int > &v) const
Definition: tria.cc:10230
TriaObjects< TriaObject< 1 > > lines
Definition: tria_faces.h:97
void load_user_indices(const std::vector< unsigned int > &v)
Definition: tria.cc:10260
void save_refine_flags(std::ostream &out) const
Definition: tria.cc:9641
unsigned int n_lines() const
Definition: tria.cc:11377
void clear_despite_subscriptions()
Definition: tria.cc:12010
raw_cell_iterator begin_raw(const unsigned int level=0) const
Definition: tria.cc:10628
unsigned int n_raw_lines() const
Definition: tria.cc:11442
virtual ~Triangulation()
Definition: tria.cc:9050
virtual std::size_t memory_consumption() const
Definition: tria.cc:13555
#define DeclException5(Exception5, type1, type2, type3, type4, type5, outsequence)
Definition: exceptions.h:612
virtual void set_mesh_smoothing(const MeshSmoothing mesh_smoothing)
Definition: tria.cc:9095
void load_user_pointers_hex(const std::vector< void *> &v)
Definition: tria.cc:10607
void save_user_flags_hex(std::ostream &out) const
Definition: tria.cc:10186
face_iterator begin_face() const
Definition: tria.cc:10827
unsigned int subdomain_id
Definition: types.h:42
boost::signals2::signal< void()> post_refinement
Definition: tria.h:2091
virtual types::global_dof_index n_global_active_cells() const
Definition: tria.cc:11250
unsigned int n_hexs() const
Definition: tria.cc:11687
void set_face(const unsigned int i, const int index)
Definition: tria_object.h:179
unsigned int n_active_quads() const
Definition: tria.cc:11669
static ::ExceptionBase & ExcInteriorQuadCantBeBoundary(int arg1, int arg2, int arg3, int arg4, types::boundary_id arg5)
TriaObjects< TriaObject< 1 > > lines
Definition: tria_faces.h:132
active_line_iterator begin_active_line(const unsigned int level=0) const
Definition: tria.cc:10992
raw_cell_iterator end_raw(const unsigned int level) const
Definition: tria.cc:10747
unsigned int n_raw_faces() const
Definition: tria.cc:11276
void load_user_indices_hex(const std::vector< unsigned int > &v)
Definition: tria.cc:10408
active_vertex_iterator begin_active_vertex() const
Definition: tria.cc:10920
static void alternating_form_at_vertices(const Point< spacedim >(&vertices)[vertices_per_cell], Tensor< spacedim-dim, spacedim >(&forms)[vertices_per_cell])
std_cxx11::enable_if< std_cxx11::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)
std::vector< types::boundary_id > get_boundary_ids() const
Definition: tria.cc:9261
const types::manifold_id invalid_manifold_id
Definition: types.h:226
std::vector< unsigned int > n_active_hexes_level
Definition: tria.h:463
unsigned int manifold_id
Definition: types.h:122
void load_user_pointers_quad(const std::vector< void *> &v)
Definition: tria.cc:10574
void fix_coarsen_flags()
Definition: tria.cc:12138
active_cell_iterator end_active(const unsigned int level) const
Definition: tria.cc:10770
void update_periodic_face_map()
Definition: tria.cc:11961
static void prevent_distorted_boundary_cells(const Triangulation< 1, spacedim > &)
Definition: mpi.h:41
void save_user_indices_quad(std::vector< unsigned int > &v) const
Definition: tria.cc:10358
virtual void add_periodicity(const std::vector< GridTools::PeriodicFacePair< cell_iterator > > &)
Definition: tria.cc:11874
const std::vector< bool > & get_used_vertices() const
Definition: tria.cc:11782
hex_iterator end_hex() const
Definition: tria.cc:11168
std::vector< CellData< 2 > > boundary_quads
Definition: tria.h:264
MeshSmoothing smooth_grid
Definition: tria.h:3185
#define DeclException4(Exception4, type1, type2, type3, type4, outsequence)
Definition: exceptions.h:600
IteratorRange< active_cell_iterator > active_cell_iterators_on_level(const unsigned int level) const
Definition: tria.cc:10814
void load_user_pointers(const std::vector< void *> &v)
Definition: tria.cc:10495
static ::ExceptionBase & ExcQuadInexistant(int arg1, int arg2, int arg3, int arg4)
void refine_global(const unsigned int times=1)
Definition: tria.cc:9608
void save_user_pointers(std::vector< void *> &v) const
Definition: tria.cc:10465
void save_user_flags(std::ostream &out) const
Definition: tria.cc:9899
static ::ExceptionBase & ExcNotImplemented()
Iterator points to a valid object.
quad_iterator end_quad() const
Definition: tria.cc:11093
std_cxx11::unique_ptr< std::map< unsigned int, types::manifold_id > > vertex_to_manifold_id_map_1d
Definition: tria.h:3520
IteratorRange< cell_iterator > cell_iterators() const
Definition: tria.cc:10782
unsigned char boundary_id
Definition: types.h:110
#define DeclException3(Exception3, type1, type2, type3, outsequence)
Definition: exceptions.h:588
face_iterator end_face() const
Definition: tria.cc:10869
void set_all_manifold_ids_on_boundary(const types::manifold_id number)
Definition: tria.cc:9169
active_cell_iterator last_active() const
Definition: tria.cc:10715
const types::boundary_id internal_face_boundary_id
Definition: types.h:216
static ::ExceptionBase & ExcInternalErrorOnCell(int arg1)
void clear_user_flags_line()
Definition: tria.cc:9817
IteratorState::IteratorStates state() const
static Triangulation< 2, spacedim >::DistortedCellList execute_refinement(Triangulation< 2, spacedim > &triangulation, const bool check_for_distorted_cells)
Definition: tria.cc:4572
quad_iterator begin_quad(const unsigned int level=0) const
Definition: tria.cc:11061
static void prepare_refinement_dim_dependent(const Triangulation< dim, spacedim > &)
Definition: tria.cc:8684
void clear()
Definition: tensor.h:1072
void clear_user_data()
Definition: tria.cc:9789
std::vector< types::manifold_id > get_manifold_ids() const
Definition: tria.cc:9293
bool vertex_used(const unsigned int index) const
const Manifold< dim, spacedim > & get_manifold(const types::manifold_id number) const
Definition: tria.cc:9236
virtual void create_triangulation_compatibility(const std::vector< Point< spacedim > > &vertices, const std::vector< CellData< dim > > &cells, const SubCellData &subcelldata)
Definition: tria.cc:9381
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:10375
virtual types::subdomain_id locally_owned_subdomain() const
Definition: tria.cc:11846
raw_line_iterator begin_raw_line(const unsigned int level=0) const
Definition: tria.cc:10951
virtual void clear()
Definition: tria.cc:9080
virtual const MeshSmoothing & get_mesh_smoothing() const
Definition: tria.cc:9106
Task< RT > new_task(const std_cxx11::function< RT()> &function)
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:11890
void load_refine_flags(std::istream &in)
Definition: tria.cc:9652
Tensor< 2, dim, Number > l(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
std::vector< unsigned int > n_lines_level
Definition: tria.h:336
static ::ExceptionBase & ExcInternalError()
Triangulation< dim, spacedim > & get_triangulation()
Definition: tria.cc:11855
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
void save_user_indices_hex(std::vector< unsigned int > &v) const
Definition: tria.cc:10391