Reference documentation for deal.II version 8.4.1
tria.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2008 - 2016 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 
17 #include <deal.II/base/utilities.h>
18 #include <deal.II/base/memory_consumption.h>
19 #include <deal.II/base/logstream.h>
20 #include <deal.II/lac/sparsity_tools.h>
21 #include <deal.II/lac/dynamic_sparsity_pattern.h>
22 #include <deal.II/grid/tria.h>
23 #include <deal.II/grid/tria_accessor.h>
24 #include <deal.II/grid/tria_iterator.h>
25 #include <deal.II/grid/grid_tools.h>
26 #include <deal.II/distributed/tria.h>
27 
28 #ifdef DEAL_II_WITH_P4EST
29 # include <p4est_bits.h>
30 # include <p4est_extended.h>
31 # include <p4est_vtk.h>
32 # include <p4est_ghost.h>
33 # include <p4est_communication.h>
34 # include <p4est_iterate.h>
35 
36 # include <p8est_bits.h>
37 # include <p8est_extended.h>
38 # include <p8est_vtk.h>
39 # include <p8est_ghost.h>
40 # include <p8est_communication.h>
41 # include <p8est_iterate.h>
42 #endif
43 
44 #include <algorithm>
45 #include <numeric>
46 #include <iostream>
47 #include <fstream>
48 
49 
50 DEAL_II_NAMESPACE_OPEN
51 
52 
53 #ifdef DEAL_II_WITH_P4EST
54 
55 namespace internal
56 {
57  namespace p4est
58  {
66  template <int dim> struct functions;
67 
68  template <> struct functions<2>
69  {
70  static
71  int (&quadrant_compare) (const void *v1, const void *v2);
72 
73  static
74  void (&quadrant_childrenv) (const types<2>::quadrant *q,
75  types<2>::quadrant c[]);
76 
77  static
78  int (&quadrant_overlaps_tree) (types<2>::tree *tree,
79  const types<2>::quadrant *q);
80 
81  static
82  void (&quadrant_set_morton) (types<2>::quadrant *quadrant,
83  int level,
84  uint64_t id);
85 
86  static
87  int (&quadrant_is_equal) (const types<2>::quadrant *q1,
88  const types<2>::quadrant *q2);
89 
90  static
91  int (&quadrant_is_sibling) (const types<2>::quadrant *q1,
92  const types<2>::quadrant *q2);
93 
94  static
95  int (&quadrant_is_ancestor) (const types<2>::quadrant *q1,
96  const types<2>::quadrant *q2);
97 
98  static
99  int (&quadrant_ancestor_id) (const types<2>::quadrant *q,
100  int level);
101 
102  static
103  int (&comm_find_owner) (types<2>::forest *p4est,
104  const types<2>::locidx which_tree,
105  const types<2>::quadrant *q,
106  const int guess);
107 
108  static
109  types<2>::connectivity *(&connectivity_new) (types<2>::topidx num_vertices,
110  types<2>::topidx num_trees,
111  types<2>::topidx num_corners,
112  types<2>::topidx num_vtt);
113 
114  static
115  void (&connectivity_join_faces) (types<2>::connectivity *conn,
116  types<2>::topidx tree_left,
117  types<2>::topidx tree_right,
118  int face_left,
119  int face_right,
120  int orientation);
121 
122 
123 
124  static
125  void (&connectivity_destroy) (p4est_connectivity_t *connectivity);
126 
127  static
128  types<2>::forest *(&new_forest) (MPI_Comm mpicomm,
129  types<2>::connectivity *connectivity,
130  types<2>::locidx min_quadrants,
131  int min_level,
132  int fill_uniform,
133  size_t data_size,
134  p4est_init_t init_fn,
135  void *user_pointer);
136 
137  static
138  void (&destroy) (types<2>::forest *p4est);
139 
140  static
141  void (&refine) (types<2>::forest *p4est,
142  int refine_recursive,
143  p4est_refine_t refine_fn,
144  p4est_init_t init_fn);
145 
146  static
147  void (&coarsen) (types<2>::forest *p4est,
148  int coarsen_recursive,
149  p4est_coarsen_t coarsen_fn,
150  p4est_init_t init_fn);
151 
152  static
153  void (&balance) (types<2>::forest *p4est,
155  p4est_init_t init_fn);
156 
157 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
158  static
159  p4est_gloidx_t (&partition) (types<2>::forest *p4est,
160  int partition_for_coarsening,
161  p4est_weight_t weight_fn);
162 #else
163  static
164  void (&partition) (types<2>::forest *p4est,
165  int partition_for_coarsening,
166  p4est_weight_t weight_fn);
167 #endif
168 
169  static
170  void (&save) (const char *filename,
171  types<2>::forest *p4est,
172  int save_data);
173 
174 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
175  static
176  types<2>::forest *(&load_ext) (const char *filename,
177  MPI_Comm mpicomm,
178  size_t data_size,
179  int load_data,
180  int autopartition,
181  int broadcasthead,
182  void *user_pointer,
183  types<2>::connectivity **p4est);
184 #else
185  static
186  types<2>::forest *(&load) (const char *filename,
187  MPI_Comm mpicomm,
188  size_t data_size,
189  int load_data,
190  void *user_pointer,
191  types<2>::connectivity **p4est);
192 #endif
193 
194 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
195  static
196  int (&connectivity_save) (const char *filename,
197  types<2>::connectivity *connectivity);
198 #else
199  static
200  void (&connectivity_save) (const char *filename,
201  types<2>::connectivity *connectivity);
202 #endif
203 
204  static
205  int (&connectivity_is_valid) (types<2>::connectivity *connectivity);
206 
207 #if DEAL_II_P4EST_VERSION_GTE(1,0,0,0)
208  static
209  types<2>::connectivity *(&connectivity_load) (const char *filename,
210  size_t *length);
211 #elif DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
212  static
213  types<2>::connectivity *(&connectivity_load) (const char *filename,
214  long unsigned *length);
215 #else
216  static
217  types<2>::connectivity *(&connectivity_load) (const char *filename,
218  long *length);
219 #endif
220 
221  static
222  unsigned int (&checksum) (types<2>::forest *p4est);
223 
224  static
225  void (&vtk_write_file) (types<2>::forest *p4est,
226  p4est_geometry_t *,
227  const char *baseName);
228 
229  static
230  types<2>::ghost *(&ghost_new) (types<2>::forest *p4est,
231  types<2>::balance_type btype);
232 
233  static
234  void (&ghost_destroy) (types<2>::ghost *ghost);
235 
236  static
237  void (&reset_data) (types<2>::forest *p4est,
238  size_t data_size,
239  p4est_init_t init_fn,
240  void *user_pointer);
241 
242  static
243  size_t (&forest_memory_used) (types<2>::forest *p4est);
244 
245  static
246  size_t (&connectivity_memory_used) (types<2>::connectivity *p4est);
247 
248  static const unsigned max_level;
249  };
250 
251  int (&functions<2>::quadrant_compare) (const void *v1, const void *v2)
252  = p4est_quadrant_compare;
253 
255  types<2>::quadrant c[])
256  = p4est_quadrant_childrenv;
257 
259  const types<2>::quadrant *q)
260  = p4est_quadrant_overlaps_tree;
261 
263  int level,
264  uint64_t id)
265  = p4est_quadrant_set_morton;
266 
268  const types<2>::quadrant *q2)
269  = p4est_quadrant_is_equal;
270 
272  const types<2>::quadrant *q2)
273  = p4est_quadrant_is_sibling;
274 
276  const types<2>::quadrant *q2)
277  = p4est_quadrant_is_ancestor;
278 
280  int level)
281  = p4est_quadrant_ancestor_id;
282 
284  const types<2>::locidx which_tree,
285  const types<2>::quadrant *q,
286  const int guess)
287  = p4est_comm_find_owner;
288 
290  types<2>::topidx num_trees,
291  types<2>::topidx num_corners,
292  types<2>::topidx num_vtt)
293  = p4est_connectivity_new;
294 
295 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,1)
297  types<2>::topidx tree_left,
298  types<2>::topidx tree_right,
299  int face_left,
300  int face_right,
301  int orientation)
302  = p4est_connectivity_join_faces;
303 #endif
304 
305  void (&functions<2>::connectivity_destroy) (p4est_connectivity_t *connectivity)
306  = p4est_connectivity_destroy;
307 
308  types<2>::forest *(&functions<2>::new_forest) (MPI_Comm mpicomm,
309  types<2>::connectivity *connectivity,
310  types<2>::locidx min_quadrants,
311  int min_level,
312  int fill_uniform,
313  size_t data_size,
314  p4est_init_t init_fn,
315  void *user_pointer)
316  = p4est_new_ext;
317 
318  void (&functions<2>::destroy) (types<2>::forest *p4est)
319  = p4est_destroy;
320 
321  void (&functions<2>::refine) (types<2>::forest *p4est,
322  int refine_recursive,
323  p4est_refine_t refine_fn,
324  p4est_init_t init_fn)
325  = p4est_refine;
326 
327  void (&functions<2>::coarsen) (types<2>::forest *p4est,
328  int coarsen_recursive,
329  p4est_coarsen_t coarsen_fn,
330  p4est_init_t init_fn)
331  = p4est_coarsen;
332 
333  void (&functions<2>::balance) (types<2>::forest *p4est,
335  p4est_init_t init_fn)
336  = p4est_balance;
337 
338 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
339  p4est_gloidx_t (&functions<2>::partition) (types<2>::forest *p4est,
340  int partition_for_coarsening,
341  p4est_weight_t weight_fn)
342  = p4est_partition_ext;
343 #else
345  int partition_for_coarsening,
346  p4est_weight_t weight_fn)
347  = p4est_partition_ext;
348 #endif
349 
350  void (&functions<2>::save) (const char *filename,
351  types<2>::forest *p4est,
352  int save_data)
353  = p4est_save;
354 
355 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
357  (&functions<2>::load_ext) (const char *filename,
358  MPI_Comm mpicomm,
359  std::size_t data_size,
360  int load_data,
361  int autopartition,
362  int broadcasthead,
363  void *user_pointer,
364  types<2>::connectivity **p4est)
365  = p4est_load_ext;
366 #else
368  (&functions<2>::load) (const char *filename,
369  MPI_Comm mpicomm,
370  std::size_t data_size,
371  int load_data,
372  void *user_pointer,
373  types<2>::connectivity **p4est)
374  = p4est_load;
375 #endif
376 
377 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
378  int (&functions<2>::connectivity_save) (const char *filename,
379  types<2>::connectivity *connectivity)
380  = p4est_connectivity_save;
381 #else
382  void (&functions<2>::connectivity_save) (const char *filename,
383  types<2>::connectivity *connectivity)
384  = p4est_connectivity_save;
385 #endif
386 
388  *connectivity)
389  = p4est_connectivity_is_valid;
390 
391 #if DEAL_II_P4EST_VERSION_GTE(1,0,0,0)
393  (&functions<2>::connectivity_load) (const char *filename,
394  size_t *length)
395  = p4est_connectivity_load;
396 #elif DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
398  (&functions<2>::connectivity_load) (const char *filename,
399  long unsigned *length)
400  = p4est_connectivity_load;
401 #else
403  (&functions<2>::connectivity_load) (const char *filename,
404  long *length)
405  = p4est_connectivity_load;
406 #endif
407 
408  unsigned int (&functions<2>::checksum) (types<2>::forest *p4est)
409  = p4est_checksum;
410 
412  p4est_geometry_t *,
413  const char *baseName)
414  = p4est_vtk_write_file;
415 
418  = p4est_ghost_new;
419 
421  = p4est_ghost_destroy;
422 
424  size_t data_size,
425  p4est_init_t init_fn,
426  void *user_pointer)
427  = p4est_reset_data;
428 
430  = p4est_memory_used;
431 
433  = p4est_connectivity_memory_used;
434 
435  const unsigned int functions<2>::max_level = P4EST_MAXLEVEL;
436 
437  template <> struct functions<3>
438  {
439  static
440  int (&quadrant_compare) (const void *v1, const void *v2);
441 
442  static
443  void (&quadrant_childrenv) (const types<3>::quadrant *q,
444  types<3>::quadrant c[]);
445 
446  static
447  int (&quadrant_overlaps_tree) (types<3>::tree *tree,
448  const types<3>::quadrant *q);
449 
450  static
451  void (&quadrant_set_morton) (types<3>::quadrant *quadrant,
452  int level,
453  uint64_t id);
454 
455  static
456  int (&quadrant_is_equal) (const types<3>::quadrant *q1,
457  const types<3>::quadrant *q2);
458 
459  static
460  int (&quadrant_is_sibling) (const types<3>::quadrant *q1,
461  const types<3>::quadrant *q2);
462 
463  static
464  int (&quadrant_is_ancestor) (const types<3>::quadrant *q1,
465  const types<3>::quadrant *q2);
466 
467  static
468  int (&quadrant_ancestor_id) (const types<3>::quadrant *q,
469  int level);
470 
471  static
472  int (&comm_find_owner) (types<3>::forest *p4est,
473  const types<3>::locidx which_tree,
474  const types<3>::quadrant *q,
475  const int guess);
476 
477  static
478  types<3>::connectivity *(&connectivity_new) (types<3>::topidx num_vertices,
479  types<3>::topidx num_trees,
480  types<3>::topidx num_edges,
481  types<3>::topidx num_ett,
482  types<3>::topidx num_corners,
483  types<3>::topidx num_ctt);
484 
485  static
486  void (&connectivity_join_faces) (types<3>::connectivity *conn,
487  types<3>::topidx tree_left,
488  types<3>::topidx tree_right,
489  int face_left,
490  int face_right,
491  int orientation);
492 
493  static
494  void (&connectivity_destroy) (p8est_connectivity_t *connectivity);
495 
496  static
497  types<3>::forest *(&new_forest) (MPI_Comm mpicomm,
498  types<3>::connectivity *connectivity,
499  types<3>::locidx min_quadrants,
500  int min_level,
501  int fill_uniform,
502  size_t data_size,
503  p8est_init_t init_fn,
504  void *user_pointer);
505 
506  static
507  void (&destroy) (types<3>::forest *p8est);
508 
509  static
510  void (&refine) (types<3>::forest *p8est,
511  int refine_recursive,
512  p8est_refine_t refine_fn,
513  p8est_init_t init_fn);
514 
515  static
516  void (&coarsen) (types<3>::forest *p8est,
517  int coarsen_recursive,
518  p8est_coarsen_t coarsen_fn,
519  p8est_init_t init_fn);
520 
521  static
522  void (&balance) (types<3>::forest *p8est,
524  p8est_init_t init_fn);
525 
526 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
527  static
528  p4est_gloidx_t (&partition) (types<3>::forest *p8est,
529  int partition_for_coarsening,
530  p8est_weight_t weight_fn);
531 #else
532  static
533  void (&partition) (types<3>::forest *p8est,
534  int partition_for_coarsening,
535  p8est_weight_t weight_fn);
536 #endif
537 
538  static
539  void (&save) (const char *filename,
540  types<3>::forest *p4est,
541  int save_data);
542 
543 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
544  static
545  types<3>::forest *(&load_ext) (const char *filename,
546  MPI_Comm mpicomm,
547  std::size_t data_size,
548  int load_data,
549  int autopartition,
550  int broadcasthead,
551  void *user_pointer,
552  types<3>::connectivity **p4est);
553 #else
554  static
555  types<3>::forest *(&load) (const char *filename,
556  MPI_Comm mpicomm,
557  std::size_t data_size,
558  int load_data,
559  void *user_pointer,
560  types<3>::connectivity **p4est);
561 #endif
562 
563 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
564  static
565  int (&connectivity_save) (const char *filename,
566  types<3>::connectivity *connectivity);
567 #else
568  static
569  void (&connectivity_save) (const char *filename,
570  types<3>::connectivity *connectivity);
571 #endif
572 
573  static
574  int (&connectivity_is_valid) (types<3>::connectivity *connectivity);
575 
576 #if DEAL_II_P4EST_VERSION_GTE(1,0,0,0)
577  static
578  types<3>::connectivity *(&connectivity_load) (const char *filename,
579  size_t *length);
580 #elif DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
581  static
582  types<3>::connectivity *(&connectivity_load) (const char *filename,
583  long unsigned *length);
584 #else
585  static
586  types<3>::connectivity *(&connectivity_load) (const char *filename,
587  long *length);
588 #endif
589 
590  static
591  unsigned int (&checksum) (types<3>::forest *p8est);
592 
593  static
594  void (&vtk_write_file) (types<3>::forest *p8est,
595  p8est_geometry_t *,
596  const char *baseName);
597 
598  static
599  types<3>::ghost *(&ghost_new) (types<3>::forest *p4est,
600  types<3>::balance_type btype);
601 
602  static
603  void (&ghost_destroy) (types<3>::ghost *ghost);
604 
605  static
606  void (&reset_data) (types<3>::forest *p4est,
607  size_t data_size,
608  p8est_init_t init_fn,
609  void *user_pointer);
610 
611  static
612  size_t (&forest_memory_used) (types<3>::forest *p4est);
613 
614  static
615  size_t (&connectivity_memory_used) (types<3>::connectivity *p4est);
616 
617  static const unsigned max_level;
618  };
619 
620 
621  int (&functions<3>::quadrant_compare) (const void *v1, const void *v2)
622  = p8est_quadrant_compare;
623 
625  types<3>::quadrant c[])
626  = p8est_quadrant_childrenv;
627 
629  const types<3>::quadrant *q)
630  = p8est_quadrant_overlaps_tree;
631 
633  int level,
634  uint64_t id)
635  = p8est_quadrant_set_morton;
636 
638  const types<3>::quadrant *q2)
639  = p8est_quadrant_is_equal;
640 
642  const types<3>::quadrant *q2)
643  = p8est_quadrant_is_sibling;
644 
646  const types<3>::quadrant *q2)
647  = p8est_quadrant_is_ancestor;
648 
650  int level)
651  = p8est_quadrant_ancestor_id;
652 
654  const types<3>::locidx which_tree,
655  const types<3>::quadrant *q,
656  const int guess)
657  = p8est_comm_find_owner;
658 
660  types<3>::topidx num_trees,
661  types<3>::topidx num_edges,
662  types<3>::topidx num_ett,
663  types<3>::topidx num_corners,
664  types<3>::topidx num_ctt)
665  = p8est_connectivity_new;
666 
667  void (&functions<3>::connectivity_destroy) (p8est_connectivity_t *connectivity)
668  = p8est_connectivity_destroy;
669 
670 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,1)
672  types<3>::topidx tree_left,
673  types<3>::topidx tree_right,
674  int face_left,
675  int face_right,
676  int orientation)
677  = p8est_connectivity_join_faces;
678 #endif
679 
680  types<3>::forest *(&functions<3>::new_forest) (MPI_Comm mpicomm,
681  types<3>::connectivity *connectivity,
682  types<3>::locidx min_quadrants,
683  int min_level,
684  int fill_uniform,
685  size_t data_size,
686  p8est_init_t init_fn,
687  void *user_pointer)
688  = p8est_new_ext;
689 
690  void (&functions<3>::destroy) (types<3>::forest *p8est)
691  = p8est_destroy;
692 
693  void (&functions<3>::refine) (types<3>::forest *p8est,
694  int refine_recursive,
695  p8est_refine_t refine_fn,
696  p8est_init_t init_fn)
697  = p8est_refine;
698 
699  void (&functions<3>::coarsen) (types<3>::forest *p8est,
700  int coarsen_recursive,
701  p8est_coarsen_t coarsen_fn,
702  p8est_init_t init_fn)
703  = p8est_coarsen;
704 
705  void (&functions<3>::balance) (types<3>::forest *p8est,
707  p8est_init_t init_fn)
708  = p8est_balance;
709 
710 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
711  p4est_gloidx_t (&functions<3>::partition) (types<3>::forest *p8est,
712  int partition_for_coarsening,
713  p8est_weight_t weight_fn)
714  = p8est_partition_ext;
715 #else
717  int partition_for_coarsening,
718  p8est_weight_t weight_fn)
719  = p8est_partition_ext;
720 #endif
721 
722  void (&functions<3>::save) (const char *filename,
723  types<3>::forest *p4est,
724  int save_data)
725  = p8est_save;
726 
727 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
729  (&functions<3>::load_ext) (const char *filename,
730  MPI_Comm mpicomm,
731  std::size_t data_size,
732  int load_data,
733  int autopartition,
734  int broadcasthead,
735  void *user_pointer,
736  types<3>::connectivity **p4est)
737  = p8est_load_ext;
738 #else
740  (&functions<3>::load) (const char *filename,
741  MPI_Comm mpicomm,
742  std::size_t data_size,
743  int load_data,
744  void *user_pointer,
745  types<3>::connectivity **p4est)
746  = p8est_load;
747 #endif
748 
749 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
750  int (&functions<3>::connectivity_save) (const char *filename,
751  types<3>::connectivity *connectivity)
752  = p8est_connectivity_save;
753 #else
754  void (&functions<3>::connectivity_save) (const char *filename,
755  types<3>::connectivity *connectivity)
756  = p8est_connectivity_save;
757 #endif
758 
760  *connectivity)
761  = p8est_connectivity_is_valid;
762 
763 #if DEAL_II_P4EST_VERSION_GTE(1,0,0,0)
765  (&functions<3>::connectivity_load) (const char *filename,
766  size_t *length)
767  = p8est_connectivity_load;
768 #elif DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
770  (&functions<3>::connectivity_load) (const char *filename,
771  long unsigned *length)
772  = p8est_connectivity_load;
773 #else
775  (&functions<3>::connectivity_load) (const char *filename,
776  long *length)
777  = p8est_connectivity_load;
778 #endif
779 
780  unsigned int (&functions<3>::checksum) (types<3>::forest *p8est)
781  = p8est_checksum;
782 
784  p8est_geometry_t *,
785  const char *baseName)
786  = p8est_vtk_write_file;
787 
790  = p8est_ghost_new;
791 
793  = p8est_ghost_destroy;
794 
796  size_t data_size,
797  p8est_init_t init_fn,
798  void *user_pointer)
799  = p8est_reset_data;
800 
802  = p8est_memory_used;
803 
805  = p8est_connectivity_memory_used;
806 
807  const unsigned int functions<3>::max_level = P8EST_MAXLEVEL;
808 
809 
810  template <int dim>
811  void
812  init_quadrant_children
813  (const typename types<dim>::quadrant &p4est_cell,
815  {
816 
817  for (unsigned int c=0;
818  c<GeometryInfo<dim>::max_children_per_cell; ++c)
819  switch (dim)
820  {
821  case 2:
822  P4EST_QUADRANT_INIT(&p4est_children[c]);
823  break;
824  case 3:
825  P8EST_QUADRANT_INIT(&p4est_children[c]);
826  break;
827  default:
828  Assert (false, ExcNotImplemented());
829  }
830 
831 
833  p4est_children);
834 
835  }
836 
837 
838  template <int dim>
839  void
840  init_coarse_quadrant(typename types<dim>::quadrant &quad)
841  {
842  switch (dim)
843  {
844  case 2:
845  P4EST_QUADRANT_INIT(&quad);
846  break;
847  case 3:
848  P8EST_QUADRANT_INIT(&quad);
849  break;
850  default:
851  Assert (false, ExcNotImplemented());
852  }
854  /*level=*/0,
855  /*index=*/0);
856  }
857 
858 
859  template <int dim>
860  bool
861  quadrant_is_equal (const typename types<dim>::quadrant &q1,
862  const typename types<dim>::quadrant &q2)
863  {
864  return functions<dim>::quadrant_is_equal(&q1, &q2);
865  }
866 
867 
868 
869  template <int dim>
870  bool
871  quadrant_is_ancestor (const typename types<dim>::quadrant &q1,
872  const typename types<dim>::quadrant &q2)
873  {
874  return functions<dim>::quadrant_is_ancestor(&q1, &q2);
875  }
876 
882  template <int dim> struct iter;
883 
884  template <> struct iter<2>
885  {
886  typedef p4est_iter_corner_info_t corner_info;
887  typedef p4est_iter_corner_side_t corner_side;
888  typedef p4est_iter_corner_t corner_iter;
889  typedef p4est_iter_face_info_t face_info;
890  typedef p4est_iter_face_side_t face_side;
891  typedef p4est_iter_face_t face_iter;
892  };
893 
894  template <> struct iter<3>
895  {
896  typedef p8est_iter_corner_info_t corner_info;
897  typedef p8est_iter_corner_side_t corner_side;
898  typedef p8est_iter_corner_t corner_iter;
899  typedef p8est_iter_edge_info_t edge_info;
900  typedef p8est_iter_edge_side_t edge_side;
901  typedef p8est_iter_edge_t edge_iter;
902  typedef p8est_iter_face_info_t face_info;
903  typedef p8est_iter_face_side_t face_side;
904  typedef p8est_iter_face_t face_iter;
905  };
906 
907  }
908 }
909 
910 
911 namespace
912 {
913  template <int dim, int spacedim>
914  void
915  get_vertex_to_cell_mappings (const Triangulation<dim,spacedim> &triangulation,
916  std::vector<unsigned int> &vertex_touch_count,
917  std::vector<std::list<
918  std::pair<typename Triangulation<dim,spacedim>::active_cell_iterator,unsigned int> > >
919  &vertex_to_cell)
920  {
921  vertex_touch_count.resize (triangulation.n_vertices());
922  vertex_to_cell.resize (triangulation.n_vertices());
923 
925  cell = triangulation.begin_active();
926  cell != triangulation.end(); ++cell)
927  for (unsigned int v=0; v<GeometryInfo<dim>::vertices_per_cell; ++v)
928  {
929  ++vertex_touch_count[cell->vertex_index(v)];
930  vertex_to_cell[cell->vertex_index(v)]
931  .push_back (std::make_pair (cell, v));
932  }
933  }
934 
935 
936 
937  template <int dim, int spacedim>
938  void
939  get_edge_to_cell_mappings (const Triangulation<dim,spacedim> &triangulation,
940  std::vector<unsigned int> &edge_touch_count,
941  std::vector<std::list<
942  std::pair<typename Triangulation<dim,spacedim>::active_cell_iterator,unsigned int> > >
943  &edge_to_cell)
944  {
945  Assert (triangulation.n_levels() == 1, ExcInternalError());
946 
947  edge_touch_count.resize (triangulation.n_active_lines());
948  edge_to_cell.resize (triangulation.n_active_lines());
949 
951  cell = triangulation.begin_active();
952  cell != triangulation.end(); ++cell)
953  for (unsigned int l=0; l<GeometryInfo<dim>::lines_per_cell; ++l)
954  {
955  ++edge_touch_count[cell->line(l)->index()];
956  edge_to_cell[cell->line(l)->index()]
957  .push_back (std::make_pair (cell, l));
958  }
959  }
960 
961 
962 
967  template <int dim, int spacedim>
968  void
969  set_vertex_and_cell_info (const Triangulation<dim,spacedim> &triangulation,
970  const std::vector<unsigned int> &vertex_touch_count,
971  const std::vector<std::list<
972  std::pair<typename Triangulation<dim,spacedim>::active_cell_iterator,unsigned int> > >
973  &vertex_to_cell,
974  const std::vector<types::global_dof_index> &coarse_cell_to_p4est_tree_permutation,
975  const bool set_vertex_info,
976  typename internal::p4est::types<dim>::connectivity *connectivity)
977  {
978  // copy the vertices into the connectivity structure. the triangulation
979  // exports the array of vertices, but some of the entries are sometimes
980  // unused; this shouldn't be the case for a newly created triangulation,
981  // but make sure
982  //
983  // note that p4est stores coordinates as a triplet of values even in 2d
984  Assert (triangulation.get_used_vertices().size() ==
985  triangulation.get_vertices().size(),
986  ExcInternalError());
987  Assert (std::find (triangulation.get_used_vertices().begin(),
988  triangulation.get_used_vertices().end(),
989  false)
990  == triangulation.get_used_vertices().end(),
991  ExcInternalError());
992  if (set_vertex_info == true)
993  for (unsigned int v=0; v<triangulation.n_vertices(); ++v)
994  {
995  connectivity->vertices[3*v ] = triangulation.get_vertices()[v][0];
996  connectivity->vertices[3*v+1] = triangulation.get_vertices()[v][1];
997  connectivity->vertices[3*v+2] = (spacedim == 2 ?
998  0
999  :
1000  triangulation.get_vertices()[v][2]);
1001  }
1002 
1003  // next store the tree_to_vertex indices (each tree is here only a single
1004  // cell in the coarse mesh). p4est requires vertex numbering in clockwise
1005  // orientation
1006  //
1007  // while we're at it, also copy the neighborship information between cells
1009  cell = triangulation.begin_active(),
1010  endc = triangulation.end();
1011  for (; cell != endc; ++cell)
1012  {
1013  const unsigned int
1014  index = coarse_cell_to_p4est_tree_permutation[cell->index()];
1015 
1016  for (unsigned int v=0; v<GeometryInfo<dim>::vertices_per_cell; ++v)
1017  {
1018  if (set_vertex_info == true)
1019  connectivity->tree_to_vertex[index*GeometryInfo<dim>::vertices_per_cell+v] = cell->vertex_index(v);
1020  connectivity->tree_to_corner[index*GeometryInfo<dim>::vertices_per_cell+v] = cell->vertex_index(v);
1021  }
1022 
1023  // neighborship information. if a cell is at a boundary, then enter
1024  // the index of the cell itself here
1025  for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
1026  if (cell->face(f)->at_boundary() == false)
1027  connectivity->tree_to_tree[index*GeometryInfo<dim>::faces_per_cell + f]
1028  = coarse_cell_to_p4est_tree_permutation[cell->neighbor(f)->index()];
1029  else
1030  connectivity->tree_to_tree[index*GeometryInfo<dim>::faces_per_cell + f]
1031  = coarse_cell_to_p4est_tree_permutation[cell->index()];
1032 
1033  // fill tree_to_face, which is essentially neighbor_to_neighbor;
1034  // however, we have to remap the resulting face number as well
1035  for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
1036  if (cell->face(f)->at_boundary() == false)
1037  {
1038  switch (dim)
1039  {
1040  case 2:
1041  {
1042  connectivity->tree_to_face[index*GeometryInfo<dim>::faces_per_cell + f]
1043  = cell->neighbor_of_neighbor (f);
1044  break;
1045  }
1046 
1047  case 3:
1048  {
1049  /*
1050  * The values for tree_to_face are in 0..23 where ttf % 6
1051  * gives the face number and ttf / 4 the face orientation
1052  * code. The orientation is determined as follows. Let
1053  * my_face and other_face be the two face numbers of the
1054  * connecting trees in 0..5. Then the first face vertex of
1055  * the lower of my_face and other_face connects to a face
1056  * vertex numbered 0..3 in the higher of my_face and
1057  * other_face. The face orientation is defined as this
1058  * number. If my_face == other_face, treating either of
1059  * both faces as the lower one leads to the same result.
1060  */
1061 
1062  connectivity->tree_to_face[index*6 + f]
1063  = cell->neighbor_of_neighbor (f);
1064 
1065  unsigned int face_idx_list[2] =
1066  {f, cell->neighbor_of_neighbor (f)};
1068  cell_list[2] = {cell, cell->neighbor(f)};
1069  unsigned int smaller_idx = 0;
1070 
1071  if (f>cell->neighbor_of_neighbor (f))
1072  smaller_idx = 1;
1073 
1074  unsigned int larger_idx = (smaller_idx+1) % 2;
1075  //smaller = *_list[smaller_idx]
1076  //larger = *_list[larger_idx]
1077 
1078  unsigned int v = 0;
1079 
1080  // global vertex index of vertex 0 on face of cell with
1081  // smaller local face index
1082  unsigned int g_idx =
1083  cell_list[smaller_idx]->vertex_index(
1085  face_idx_list[smaller_idx],
1086  0,
1087  cell_list[smaller_idx]->face_orientation(face_idx_list[smaller_idx]),
1088  cell_list[smaller_idx]->face_flip(face_idx_list[smaller_idx]),
1089  cell_list[smaller_idx]->face_rotation(face_idx_list[smaller_idx]))
1090  );
1091 
1092  // loop over vertices on face from other cell and compare
1093  // global vertex numbers
1094  for (unsigned int i=0; i<GeometryInfo<dim>::vertices_per_face; ++i)
1095  {
1096  unsigned int idx
1097  =
1098  cell_list[larger_idx]->vertex_index(
1100  face_idx_list[larger_idx],
1101  i)
1102  );
1103 
1104  if (idx==g_idx)
1105  {
1106  v = i;
1107  break;
1108  }
1109  }
1110 
1111  connectivity->tree_to_face[index*6 + f] += 6*v;
1112  break;
1113  }
1114 
1115  default:
1116  Assert (false, ExcNotImplemented());
1117  }
1118  }
1119  else
1120  connectivity->tree_to_face[index*GeometryInfo<dim>::faces_per_cell + f] = f;
1121  }
1122 
1123  // now fill the vertex information
1124  connectivity->ctt_offset[0] = 0;
1125  std::partial_sum (vertex_touch_count.begin(),
1126  vertex_touch_count.end(),
1127  &connectivity->ctt_offset[1]);
1128 
1130  num_vtt = std::accumulate (vertex_touch_count.begin(),
1131  vertex_touch_count.end(),
1132  0);
1133  (void)num_vtt;
1134  Assert (connectivity->ctt_offset[triangulation.n_vertices()] ==
1135  num_vtt,
1136  ExcInternalError());
1137 
1138  for (unsigned int v=0; v<triangulation.n_vertices(); ++v)
1139  {
1140  Assert (vertex_to_cell[v].size() == vertex_touch_count[v],
1141  ExcInternalError());
1142 
1143  typename std::list<std::pair
1145  unsigned int> >::const_iterator
1146  p = vertex_to_cell[v].begin();
1147  for (unsigned int c=0; c<vertex_touch_count[v]; ++c, ++p)
1148  {
1149  connectivity->corner_to_tree[connectivity->ctt_offset[v]+c]
1150  = coarse_cell_to_p4est_tree_permutation[p->first->index()];
1151  connectivity->corner_to_corner[connectivity->ctt_offset[v]+c]
1152  = p->second;
1153  }
1154  }
1155  }
1156 
1157 
1158 
1159  template <int dim, int spacedim>
1160  bool
1161  tree_exists_locally (const typename internal::p4est::types<dim>::forest *parallel_forest,
1162  const typename internal::p4est::types<dim>::topidx coarse_grid_cell)
1163  {
1164  Assert (coarse_grid_cell < parallel_forest->connectivity->num_trees,
1165  ExcInternalError());
1166  return ((coarse_grid_cell >= parallel_forest->first_local_tree)
1167  &&
1168  (coarse_grid_cell <= parallel_forest->last_local_tree));
1169  }
1170 
1171 
1172  template <int dim, int spacedim>
1173  void
1174  delete_all_children_and_self (const typename Triangulation<dim,spacedim>::cell_iterator &cell)
1175  {
1176  if (cell->has_children())
1177  for (unsigned int c=0; c<cell->n_children(); ++c)
1178  delete_all_children_and_self<dim,spacedim> (cell->child(c));
1179  else
1180  cell->set_coarsen_flag ();
1181  }
1182 
1183 
1184 
1185  template <int dim, int spacedim>
1186  void
1187  delete_all_children (const typename Triangulation<dim,spacedim>::cell_iterator &cell)
1188  {
1189  if (cell->has_children())
1190  for (unsigned int c=0; c<cell->n_children(); ++c)
1191  delete_all_children_and_self<dim,spacedim> (cell->child(c));
1192  }
1193 
1194 
1195  template <int dim, int spacedim>
1196  void
1197  determine_level_subdomain_id_recursively (const typename internal::p4est::types<dim>::tree &tree,
1198  const typename internal::p4est::types<dim>::locidx &tree_index,
1199  const typename Triangulation<dim,spacedim>::cell_iterator &dealii_cell,
1200  const typename internal::p4est::types<dim>::quadrant &p4est_cell,
1201  typename internal::p4est::types<dim>::forest &forest,
1202  const types::subdomain_id my_subdomain,
1203  const std::vector<std::vector<bool> > &marked_vertices)
1204  {
1205  if (dealii_cell->level_subdomain_id()==numbers::artificial_subdomain_id)
1206  {
1207  //important: only assign the level_subdomain_id if it is a ghost cell
1208  // even though we could fill in all.
1209  bool used = false;
1210  for (unsigned int v=0; v<GeometryInfo<dim>::vertices_per_cell; ++v)
1211  {
1212  if (marked_vertices[dealii_cell->level()][dealii_cell->vertex_index(v)])
1213  {
1214  used = true;
1215  break;
1216  }
1217  }
1218 
1219  if (used)
1220  {
1222  tree_index,
1223  &p4est_cell,
1224  my_subdomain);
1225  Assert((owner!=-2) && (owner!=-1), ExcMessage("p4est should know the owner."));
1226  dealii_cell->set_level_subdomain_id(owner);
1227  }
1228 
1229  }
1230 
1231  if (dealii_cell->has_children ())
1232  {
1234  p4est_child[GeometryInfo<dim>::max_children_per_cell];
1235  for (unsigned int c=0;
1236  c<GeometryInfo<dim>::max_children_per_cell; ++c)
1237  switch (dim)
1238  {
1239  case 2:
1240  P4EST_QUADRANT_INIT(&p4est_child[c]);
1241  break;
1242  case 3:
1243  P8EST_QUADRANT_INIT(&p4est_child[c]);
1244  break;
1245  default:
1246  Assert (false, ExcNotImplemented());
1247  }
1248 
1249 
1251  quadrant_childrenv (&p4est_cell,
1252  p4est_child);
1253 
1254  for (unsigned int c=0;
1255  c<GeometryInfo<dim>::max_children_per_cell; ++c)
1256  {
1257  determine_level_subdomain_id_recursively <dim,spacedim> (tree,tree_index,
1258  dealii_cell->child(c),
1259  p4est_child[c],
1260  forest,
1261  my_subdomain,
1262  marked_vertices);
1263  }
1264  }
1265  }
1266 
1267 
1268  template <int dim, int spacedim>
1269  void
1270  match_tree_recursively (const typename internal::p4est::types<dim>::tree &tree,
1271  const typename Triangulation<dim,spacedim>::cell_iterator &dealii_cell,
1272  const typename internal::p4est::types<dim>::quadrant &p4est_cell,
1273  const typename internal::p4est::types<dim>::forest &forest,
1274  const types::subdomain_id my_subdomain)
1275  {
1276  // check if this cell exists in the local p4est cell
1277  if (sc_array_bsearch(const_cast<sc_array_t *>(&tree.quadrants),
1278  &p4est_cell,
1280  != -1)
1281  {
1282  // yes, cell found in local part of p4est
1283  delete_all_children<dim,spacedim> (dealii_cell);
1284  if (!dealii_cell->has_children())
1285  dealii_cell->set_subdomain_id(my_subdomain);
1286  }
1287  else
1288  {
1289  // no, cell not found in local part of p4est. this means that the
1290  // local part is more refined than the current cell. if this cell has
1291  // no children of its own, we need to refine it, and if it does
1292  // already have children then loop over all children and see if they
1293  // are locally available as well
1294  if (dealii_cell->has_children () == false)
1295  dealii_cell->set_refine_flag ();
1296  else
1297  {
1299  p4est_child[GeometryInfo<dim>::max_children_per_cell];
1300  for (unsigned int c=0;
1301  c<GeometryInfo<dim>::max_children_per_cell; ++c)
1302  switch (dim)
1303  {
1304  case 2:
1305  P4EST_QUADRANT_INIT(&p4est_child[c]);
1306  break;
1307  case 3:
1308  P8EST_QUADRANT_INIT(&p4est_child[c]);
1309  break;
1310  default:
1311  Assert (false, ExcNotImplemented());
1312  }
1313 
1314 
1316  quadrant_childrenv (&p4est_cell,
1317  p4est_child);
1318 
1319  for (unsigned int c=0;
1320  c<GeometryInfo<dim>::max_children_per_cell; ++c)
1322  quadrant_overlaps_tree (const_cast<typename internal::p4est::types<dim>::tree *>(&tree),
1323  &p4est_child[c])
1324  == false)
1325  {
1326  // no, this child is locally not available in the p4est.
1327  // delete all its children but, because this may not be
1328  // successfull, make sure to mark all children recursively
1329  // as not local.
1330  delete_all_children<dim,spacedim> (dealii_cell->child(c));
1331  dealii_cell->child(c)
1332  ->recursively_set_subdomain_id(numbers::artificial_subdomain_id);
1333  }
1334  else
1335  {
1336  // at least some part of the tree rooted in this child is
1337  // locally available
1338  match_tree_recursively<dim,spacedim> (tree,
1339  dealii_cell->child(c),
1340  p4est_child[c],
1341  forest,
1342  my_subdomain);
1343  }
1344  }
1345  }
1346  }
1347 
1348 
1349  template <int dim, int spacedim>
1350  void
1351  match_quadrant (const ::Triangulation<dim,spacedim> *tria,
1352  unsigned int dealii_index,
1353  typename internal::p4est::types<dim>::quadrant &ghost_quadrant,
1354  unsigned int ghost_owner)
1355  {
1356  int i, child_id;
1357  int l = ghost_quadrant.level;
1358 
1359  for (i = 0; i < l; i++)
1360  {
1361  typename Triangulation<dim,spacedim>::cell_iterator cell (tria, i, dealii_index);
1362  if (cell->has_children () == false)
1363  {
1364  cell->clear_coarsen_flag();
1365  cell->set_refine_flag ();
1366  return;
1367  }
1368 
1369  child_id = internal::p4est::functions<dim>::quadrant_ancestor_id (&ghost_quadrant, i + 1);
1370  dealii_index = cell->child_index(child_id);
1371  }
1372 
1373  typename Triangulation<dim,spacedim>::cell_iterator cell (tria, l, dealii_index);
1374  if (cell->has_children())
1375  delete_all_children<dim,spacedim> (cell);
1376  else
1377  {
1378  cell->clear_coarsen_flag();
1379  cell->set_subdomain_id(ghost_owner);
1380  }
1381  }
1382 
1383 
1384 
1385  template <int dim, int spacedim>
1386  void
1387  attach_mesh_data_recursively (const typename internal::p4est::types<dim>::tree &tree,
1388  const typename Triangulation<dim,spacedim>::cell_iterator &dealii_cell,
1389  const typename internal::p4est::types<dim>::quadrant &p4est_cell,
1390  const typename std::list<std::pair<unsigned int, typename std_cxx11::function<
1392  typename parallel::distributed::Triangulation<dim,spacedim>::CellStatus,
1393  void *)
1394  > > > &attached_data_pack_callbacks)
1395  {
1396  typedef std::list<std::pair<unsigned int, typename std_cxx11::function<
1398  typename parallel::distributed::Triangulation<dim,spacedim>::CellStatus,
1399  void *)
1400  > > > callback_list_t;
1401 
1402  int idx = sc_array_bsearch(const_cast<sc_array_t *>(&tree.quadrants),
1403  &p4est_cell,
1405 
1406  if (idx == -1 && (internal::p4est::functions<dim>::
1407  quadrant_overlaps_tree (const_cast<typename internal::p4est::types<dim>::tree *>(&tree),
1408  &p4est_cell)
1409  == false))
1410  return; //this quadrant and none of its children belongs to us.
1411 
1412  bool p4est_has_children = (idx == -1);
1413 
1414  if (p4est_has_children && dealii_cell->has_children())
1415  {
1416  //recurse further
1418  p4est_child[GeometryInfo<dim>::max_children_per_cell];
1419  for (unsigned int c=0; c<GeometryInfo<dim>::max_children_per_cell; ++c)
1420  switch (dim)
1421  {
1422  case 2:
1423  P4EST_QUADRANT_INIT(&p4est_child[c]);
1424  break;
1425  case 3:
1426  P8EST_QUADRANT_INIT(&p4est_child[c]);
1427  break;
1428  default:
1429  Assert (false, ExcNotImplemented());
1430  }
1431 
1433  quadrant_childrenv (&p4est_cell, p4est_child);
1434 
1435  for (unsigned int c=0;
1436  c<GeometryInfo<dim>::max_children_per_cell; ++c)
1437  {
1438  attach_mesh_data_recursively<dim,spacedim> (tree,
1439  dealii_cell->child(c),
1440  p4est_child[c],
1441  attached_data_pack_callbacks);
1442  }
1443  }
1444  else if (!p4est_has_children && !dealii_cell->has_children())
1445  {
1446  //this active cell didn't change
1448  q = static_cast<typename internal::p4est::types<dim>::quadrant *> (
1449  sc_array_index (const_cast<sc_array_t *>(&tree.quadrants), idx)
1450  );
1451  *static_cast<typename parallel::distributed::Triangulation<dim,spacedim>::CellStatus *>(q->p.user_data) = parallel::distributed::Triangulation<dim,spacedim>::CELL_PERSIST;
1452 
1453  for (typename callback_list_t::const_iterator it = attached_data_pack_callbacks.begin();
1454  it != attached_data_pack_callbacks.end();
1455  ++it)
1456  {
1457  void *ptr = static_cast<char *>(q->p.user_data) + (*it).first; //add offset
1458  ((*it).second)(dealii_cell,
1460  ptr);
1461  }
1462  }
1463  else if (p4est_has_children)
1464  {
1465  //this cell got refined
1466 
1467  //attach to the first child, because we can only attach to active
1468  // quadrants
1470  p4est_child[GeometryInfo<dim>::max_children_per_cell];
1471  for (unsigned int c=0; c<GeometryInfo<dim>::max_children_per_cell; ++c)
1472  switch (dim)
1473  {
1474  case 2:
1475  P4EST_QUADRANT_INIT(&p4est_child[c]);
1476  break;
1477  case 3:
1478  P8EST_QUADRANT_INIT(&p4est_child[c]);
1479  break;
1480  default:
1481  Assert (false, ExcNotImplemented());
1482  }
1483 
1485  quadrant_childrenv (&p4est_cell, p4est_child);
1486  int child0_idx = sc_array_bsearch(const_cast<sc_array_t *>(&tree.quadrants),
1487  &p4est_child[0],
1489  Assert(child0_idx != -1, ExcMessage("the first child should exist as an active quadrant!"));
1490 
1492  q = static_cast<typename internal::p4est::types<dim>::quadrant *> (
1493  sc_array_index (const_cast<sc_array_t *>(&tree.quadrants), child0_idx)
1494  );
1495  *static_cast<typename parallel::distributed::Triangulation<dim,spacedim>::CellStatus *>(q->p.user_data) = parallel::distributed::Triangulation<dim,spacedim>::CELL_REFINE;
1496 
1497  for (typename callback_list_t::const_iterator it = attached_data_pack_callbacks.begin();
1498  it != attached_data_pack_callbacks.end();
1499  ++it)
1500  {
1501  void *ptr = static_cast<char *>(q->p.user_data) + (*it).first; //add offset
1502 
1503  ((*it).second)(dealii_cell,
1505  ptr);
1506  }
1507 
1508  //mark other children as invalid, so that unpack only happens once
1509  for (unsigned int i=1; i<GeometryInfo<dim>::max_children_per_cell; ++i)
1510  {
1511  int child_idx = sc_array_bsearch(const_cast<sc_array_t *>(&tree.quadrants),
1512  &p4est_child[i],
1514  q = static_cast<typename internal::p4est::types<dim>::quadrant *> (
1515  sc_array_index (const_cast<sc_array_t *>(&tree.quadrants), child_idx)
1516  );
1517  *static_cast<typename parallel::distributed::Triangulation<dim,spacedim>::CellStatus *>(q->p.user_data) = parallel::distributed::Triangulation<dim,spacedim>::CELL_INVALID;
1518  }
1519 
1520 
1521  }
1522  else
1523  {
1524  //its children got coarsened into this cell
1526  q = static_cast<typename internal::p4est::types<dim>::quadrant *> (
1527  sc_array_index (const_cast<sc_array_t *>(&tree.quadrants), idx)
1528  );
1529  *static_cast<typename parallel::distributed::Triangulation<dim,spacedim>::CellStatus *>(q->p.user_data) = parallel::distributed::Triangulation<dim,spacedim>::CELL_COARSEN;
1530 
1531  for (typename callback_list_t::const_iterator it = attached_data_pack_callbacks.begin();
1532  it != attached_data_pack_callbacks.end();
1533  ++it)
1534  {
1535  void *ptr = static_cast<char *>(q->p.user_data) + (*it).first; //add offset
1536  ((*it).second)(dealii_cell,
1538  ptr);
1539  }
1540  }
1541  }
1542 
1543  template <int dim, int spacedim>
1544  void
1545  get_cell_weights_recursively (const typename internal::p4est::types<dim>::tree &tree,
1546  const typename Triangulation<dim,spacedim>::cell_iterator &dealii_cell,
1547  const typename internal::p4est::types<dim>::quadrant &p4est_cell,
1548  const typename Triangulation<dim,spacedim>::Signals &signals,
1549  std::vector<unsigned int> &weight)
1550  {
1551  const int idx = sc_array_bsearch(const_cast<sc_array_t *>(&tree.quadrants),
1552  &p4est_cell,
1554 
1555  if (idx == -1 && (internal::p4est::functions<dim>::
1556  quadrant_overlaps_tree (const_cast<typename internal::p4est::types<dim>::tree *>(&tree),
1557  &p4est_cell)
1558  == false))
1559  return; // This quadrant and none of its children belongs to us.
1560 
1561  const bool p4est_has_children = (idx == -1);
1562 
1563  if (p4est_has_children && dealii_cell->has_children())
1564  {
1565  //recurse further
1567  p4est_child[GeometryInfo<dim>::max_children_per_cell];
1568  for (unsigned int c=0; c<GeometryInfo<dim>::max_children_per_cell; ++c)
1569  switch (dim)
1570  {
1571  case 2:
1572  P4EST_QUADRANT_INIT(&p4est_child[c]);
1573  break;
1574  case 3:
1575  P8EST_QUADRANT_INIT(&p4est_child[c]);
1576  break;
1577  default:
1578  Assert (false, ExcNotImplemented());
1579  }
1580 
1582  quadrant_childrenv (&p4est_cell, p4est_child);
1583 
1584  for (unsigned int c=0;
1585  c<GeometryInfo<dim>::max_children_per_cell; ++c)
1586  {
1587  get_cell_weights_recursively<dim,spacedim> (tree,
1588  dealii_cell->child(c),
1589  p4est_child[c],
1590  signals,
1591  weight);
1592  }
1593  }
1594  else if (!p4est_has_children && !dealii_cell->has_children())
1595  {
1596  // This active cell didn't change
1597  weight.push_back(1000);
1598  weight.back() += signals.cell_weight(dealii_cell,
1600  }
1601  else if (p4est_has_children)
1602  {
1603  // This cell will be refined
1604  unsigned int parent_weight(1000);
1605  parent_weight += signals.cell_weight(dealii_cell,
1607 
1608  for (unsigned int c=0; c<GeometryInfo<dim>::max_children_per_cell; ++c)
1609  {
1610  // We assign the weight of the parent cell equally to all children
1611  weight.push_back(parent_weight);
1612  }
1613  }
1614  else
1615  {
1616  // This cell's children will be coarsened into this cell
1617  weight.push_back(1000);
1618  weight.back() += signals.cell_weight(dealii_cell,
1620  }
1621  }
1622 
1623 
1624  template <int dim, int spacedim>
1625  void
1626  post_mesh_data_recursively (const typename internal::p4est::types<dim>::tree &tree,
1627  const typename Triangulation<dim,spacedim>::cell_iterator &dealii_cell,
1628  const typename Triangulation<dim,spacedim>::cell_iterator &parent_cell,
1629  const typename internal::p4est::types<dim>::quadrant &p4est_cell,
1630  const unsigned int offset,
1631  const typename std_cxx11::function<
1632  void(typename parallel::distributed::Triangulation<dim,spacedim>::cell_iterator, typename parallel::distributed::Triangulation<dim,spacedim>::CellStatus, void *)
1633  > &unpack_callback)
1634  {
1635  int idx = sc_array_bsearch(const_cast<sc_array_t *>(&tree.quadrants),
1636  &p4est_cell,
1638  if (idx == -1 && (internal::p4est::functions<dim>::
1639  quadrant_overlaps_tree (const_cast<typename internal::p4est::types<dim>::tree *>(&tree),
1640  &p4est_cell)
1641  == false))
1642  // this quadrant and none of its children belong to us.
1643  return;
1644 
1645 
1646  const bool p4est_has_children = (idx == -1);
1647  if (p4est_has_children)
1648  {
1649  Assert(dealii_cell->has_children(), ExcInternalError());
1650 
1651  //recurse further
1653  p4est_child[GeometryInfo<dim>::max_children_per_cell];
1654  for (unsigned int c=0; c<GeometryInfo<dim>::max_children_per_cell; ++c)
1655  switch (dim)
1656  {
1657  case 2:
1658  P4EST_QUADRANT_INIT(&p4est_child[c]);
1659  break;
1660  case 3:
1661  P8EST_QUADRANT_INIT(&p4est_child[c]);
1662  break;
1663  default:
1664  Assert (false, ExcNotImplemented());
1665  }
1666 
1668  quadrant_childrenv (&p4est_cell, p4est_child);
1669 
1670  for (unsigned int c=0;
1671  c<GeometryInfo<dim>::max_children_per_cell; ++c)
1672  {
1673  post_mesh_data_recursively<dim,spacedim> (tree,
1674  dealii_cell->child(c),
1675  dealii_cell,
1676  p4est_child[c],
1677  offset,
1678  unpack_callback);
1679  }
1680  }
1681  else
1682  {
1683  Assert(! dealii_cell->has_children(), ExcInternalError());
1684 
1686  q = static_cast<typename internal::p4est::types<dim>::quadrant *> (
1687  sc_array_index (const_cast<sc_array_t *>(&tree.quadrants), idx)
1688  );
1689 
1690  void *ptr = static_cast<char *>(q->p.user_data) + offset;
1691  typename parallel::distributed::Triangulation<dim,spacedim>::CellStatus
1692  status = * static_cast<
1693  typename parallel::distributed::Triangulation<dim,spacedim>::CellStatus *
1694  >(q->p.user_data);
1695  switch (status)
1696  {
1698  {
1699  unpack_callback(dealii_cell, status, ptr);
1700  break;
1701  }
1703  {
1704  unpack_callback(parent_cell, status, ptr);
1705  break;
1706  }
1708  {
1709  unpack_callback(dealii_cell, status, ptr);
1710  break;
1711  }
1713  {
1714  break;
1715  }
1716  default:
1717  AssertThrow (false, ExcInternalError());
1718  }
1719  }
1720  }
1721 
1722 
1723 
1729  template <int dim, int spacedim>
1730  class RefineAndCoarsenList
1731  {
1732  public:
1733  RefineAndCoarsenList (const Triangulation<dim,spacedim> &triangulation,
1734  const std::vector<types::global_dof_index> &p4est_tree_to_coarse_cell_permutation,
1735  const types::subdomain_id my_subdomain);
1736 
1745  static
1746  int
1747  refine_callback (typename internal::p4est::types<dim>::forest *forest,
1748  typename internal::p4est::types<dim>::topidx coarse_cell_index,
1749  typename internal::p4est::types<dim>::quadrant *quadrant);
1750 
1755  static
1756  int
1757  coarsen_callback (typename internal::p4est::types<dim>::forest *forest,
1758  typename internal::p4est::types<dim>::topidx coarse_cell_index,
1759  typename internal::p4est::types<dim>::quadrant *children[]);
1760 
1761  bool pointers_are_at_end () const;
1762 
1763  private:
1764  std::vector<typename internal::p4est::types<dim>::quadrant> refine_list;
1765  typename std::vector<typename internal::p4est::types<dim>::quadrant>::const_iterator current_refine_pointer;
1766 
1767  std::vector<typename internal::p4est::types<dim>::quadrant> coarsen_list;
1768  typename std::vector<typename internal::p4est::types<dim>::quadrant>::const_iterator current_coarsen_pointer;
1769 
1770  void build_lists (const typename Triangulation<dim,spacedim>::cell_iterator &cell,
1771  const typename internal::p4est::types<dim>::quadrant &p4est_cell,
1772  const unsigned int myid);
1773  };
1774 
1775 
1776 
1777  template <int dim, int spacedim>
1778  bool
1779  RefineAndCoarsenList<dim,spacedim>::
1780  pointers_are_at_end () const
1781  {
1782  return ((current_refine_pointer == refine_list.end())
1783  &&
1784  (current_coarsen_pointer == coarsen_list.end()));
1785  }
1786 
1787 
1788 
1789  template <int dim, int spacedim>
1790  RefineAndCoarsenList<dim,spacedim>::
1791  RefineAndCoarsenList (const Triangulation<dim,spacedim> &triangulation,
1792  const std::vector<types::global_dof_index> &p4est_tree_to_coarse_cell_permutation,
1793  const types::subdomain_id my_subdomain)
1794  {
1795  // count how many flags are set and allocate that much memory
1796  unsigned int n_refine_flags = 0,
1797  n_coarsen_flags = 0;
1798  for (typename Triangulation<dim,spacedim>::active_cell_iterator
1799  cell = triangulation.begin_active();
1800  cell != triangulation.end(); ++cell)
1801  {
1802  //skip cells that are not local
1803  if (cell->subdomain_id() != my_subdomain)
1804  continue;
1805 
1806  if (cell->refine_flag_set())
1807  ++n_refine_flags;
1808  else if (cell->coarsen_flag_set())
1809  ++n_coarsen_flags;
1810  }
1811 
1812  refine_list.reserve (n_refine_flags);
1813  coarsen_list.reserve (n_coarsen_flags);
1814 
1815 
1816  // now build the lists of cells that are flagged. note that p4est will
1817  // traverse its cells in the order in which trees appear in the
1818  // forest. this order is not the same as the order of coarse cells in the
1819  // deal.II Triangulation because we have translated everything by the
1820  // coarse_cell_to_p4est_tree_permutation permutation. in order to make
1821  // sure that the output array is already in the correct order, traverse
1822  // our coarse cells in the same order in which p4est will:
1823  for (unsigned int c=0; c<triangulation.n_cells(0); ++c)
1824  {
1825  unsigned int coarse_cell_index =
1826  p4est_tree_to_coarse_cell_permutation[c];
1827 
1829  cell (&triangulation, 0, coarse_cell_index);
1830 
1831  typename internal::p4est::types<dim>::quadrant p4est_cell;
1833  quadrant_set_morton (&p4est_cell,
1834  /*level=*/0,
1835  /*index=*/0);
1836  p4est_cell.p.which_tree = c;
1837  build_lists (cell, p4est_cell, my_subdomain);
1838  }
1839 
1840 
1841  Assert(refine_list.size() == n_refine_flags,
1842  ExcInternalError());
1843  Assert(coarsen_list.size() == n_coarsen_flags,
1844  ExcInternalError());
1845 
1846  // make sure that our ordering in fact worked
1847  for (unsigned int i=1; i<refine_list.size(); ++i)
1848  Assert (refine_list[i].p.which_tree >=
1849  refine_list[i-1].p.which_tree,
1850  ExcInternalError());
1851  for (unsigned int i=1; i<coarsen_list.size(); ++i)
1852  Assert (coarsen_list[i].p.which_tree >=
1853  coarsen_list[i-1].p.which_tree,
1854  ExcInternalError());
1855 
1856  current_refine_pointer = refine_list.begin();
1857  current_coarsen_pointer = coarsen_list.begin();
1858  }
1859 
1860 
1861 
1862  template <int dim, int spacedim>
1863  void
1864  RefineAndCoarsenList<dim,spacedim>::
1865  build_lists (const typename Triangulation<dim,spacedim>::cell_iterator &cell,
1866  const typename internal::p4est::types<dim>::quadrant &p4est_cell,
1867  const types::subdomain_id my_subdomain)
1868  {
1869  if (!cell->has_children())
1870  {
1871  if (cell->subdomain_id() == my_subdomain)
1872  {
1873  if (cell->refine_flag_set())
1874  refine_list.push_back (p4est_cell);
1875  else if (cell->coarsen_flag_set())
1876  coarsen_list.push_back (p4est_cell);
1877  }
1878  }
1879  else
1880  {
1882  p4est_child[GeometryInfo<dim>::max_children_per_cell];
1883  for (unsigned int c=0; c<GeometryInfo<dim>::max_children_per_cell; ++c)
1884  switch (dim)
1885  {
1886  case 2:
1887  P4EST_QUADRANT_INIT(&p4est_child[c]);
1888  break;
1889  case 3:
1890  P8EST_QUADRANT_INIT(&p4est_child[c]);
1891  break;
1892  default:
1893  Assert (false, ExcNotImplemented());
1894  }
1896  quadrant_childrenv (&p4est_cell,
1897  p4est_child);
1898  for (unsigned int c=0;
1899  c<GeometryInfo<dim>::max_children_per_cell; ++c)
1900  {
1901  p4est_child[c].p.which_tree = p4est_cell.p.which_tree;
1902  build_lists (cell->child(c),
1903  p4est_child[c],
1904  my_subdomain);
1905  }
1906  }
1907  }
1908 
1909 
1910  template <int dim, int spacedim>
1911  int
1912  RefineAndCoarsenList<dim,spacedim>::
1913  refine_callback (typename internal::p4est::types<dim>::forest *forest,
1914  typename internal::p4est::types<dim>::topidx coarse_cell_index,
1915  typename internal::p4est::types<dim>::quadrant *quadrant)
1916  {
1917  RefineAndCoarsenList<dim,spacedim> *this_object
1918  = reinterpret_cast<RefineAndCoarsenList<dim,spacedim>*>(forest->user_pointer);
1919 
1920  // if there are no more cells in our list the current cell can't be
1921  // flagged for refinement
1922  if (this_object->current_refine_pointer == this_object->refine_list.end())
1923  return false;
1924 
1925  Assert (coarse_cell_index <=
1926  this_object->current_refine_pointer->p.which_tree,
1927  ExcInternalError());
1928 
1929  // if p4est hasn't yet reached the tree of the next flagged cell the
1930  // current cell can't be flagged for refinement
1931  if (coarse_cell_index <
1932  this_object->current_refine_pointer->p.which_tree)
1933  return false;
1934 
1935  // now we're in the right tree in the forest
1936  Assert (coarse_cell_index <=
1937  this_object->current_refine_pointer->p.which_tree,
1938  ExcInternalError());
1939 
1940  // make sure that the p4est loop over cells hasn't gotten ahead of our own
1941  // pointer
1943  quadrant_compare (quadrant,
1944  &*this_object->current_refine_pointer) <= 0,
1945  ExcInternalError());
1946 
1947  // now, if the p4est cell is one in the list, it is supposed to be refined
1949  quadrant_is_equal (quadrant, &*this_object->current_refine_pointer))
1950  {
1951  ++this_object->current_refine_pointer;
1952  return true;
1953  }
1954 
1955  // p4est cell is not in list
1956  return false;
1957  }
1958 
1959 
1960 
1961  template <int dim, int spacedim>
1962  int
1963  RefineAndCoarsenList<dim,spacedim>::
1964  coarsen_callback (typename internal::p4est::types<dim>::forest *forest,
1965  typename internal::p4est::types<dim>::topidx coarse_cell_index,
1966  typename internal::p4est::types<dim>::quadrant *children[])
1967  {
1968  RefineAndCoarsenList<dim,spacedim> *this_object
1969  = reinterpret_cast<RefineAndCoarsenList<dim,spacedim>*>(forest->user_pointer);
1970 
1971  // if there are no more cells in our list the current cell can't be
1972  // flagged for coarsening
1973  if (this_object->current_coarsen_pointer ==
1974  this_object->coarsen_list.end())
1975  return false;
1976 
1977  Assert (coarse_cell_index <=
1978  this_object->current_coarsen_pointer->p.which_tree,
1979  ExcInternalError());
1980 
1981  // if p4est hasn't yet reached the tree of the next flagged cell the
1982  // current cell can't be flagged for coarsening
1983  if (coarse_cell_index <
1984  this_object->current_coarsen_pointer->p.which_tree)
1985  return false;
1986 
1987  // now we're in the right tree in the forest
1988  Assert (coarse_cell_index <=
1989  this_object->current_coarsen_pointer->p.which_tree,
1990  ExcInternalError());
1991 
1992  // make sure that the p4est loop over cells hasn't gotten ahead of our own
1993  // pointer
1995  quadrant_compare (children[0],
1996  &*this_object->current_coarsen_pointer) <= 0,
1997  ExcInternalError());
1998 
1999  // now, if the p4est cell is one in the list, it is supposed to be
2000  // coarsened
2002  quadrant_is_equal (children[0],
2003  &*this_object->current_coarsen_pointer))
2004  {
2005  // move current pointer one up
2006  ++this_object->current_coarsen_pointer;
2007 
2008  // note that the next 3 cells in our list need to correspond to the
2009  // other siblings of the cell we have just found
2010  for (unsigned int c=1; c<GeometryInfo<dim>::max_children_per_cell; ++c)
2011  {
2013  quadrant_is_equal (children[c],
2014  &*this_object->current_coarsen_pointer),
2015  ExcInternalError());
2016  ++this_object->current_coarsen_pointer;
2017  }
2018 
2019  return true;
2020  }
2021 
2022  // p4est cell is not in list
2023  return false;
2024  }
2025 
2026 
2027 
2034  template <int dim, int spacedim>
2035  class PartitionWeights
2036  {
2037  public:
2043  PartitionWeights (const std::vector<unsigned int> &cell_weights);
2044 
2052  static
2053  int
2054  cell_weight (typename internal::p4est::types<dim>::forest *forest,
2055  typename internal::p4est::types<dim>::topidx coarse_cell_index,
2056  typename internal::p4est::types<dim>::quadrant *quadrant);
2057 
2058  private:
2059  std::vector<unsigned int> cell_weights_list;
2060  std::vector<unsigned int>::const_iterator current_pointer;
2061  };
2062 
2063 
2064  template <int dim, int spacedim>
2065  PartitionWeights<dim,spacedim>::
2066  PartitionWeights (const std::vector<unsigned int> &cell_weights)
2067  :
2068  cell_weights_list(cell_weights)
2069  {
2070  // set the current pointer to the first element of the list, given that
2071  // we will walk through it sequentially
2072  current_pointer = cell_weights_list.begin();
2073  }
2074 
2075 
2076  template <int dim, int spacedim>
2077  int
2078  PartitionWeights<dim,spacedim>::
2079  cell_weight (typename internal::p4est::types<dim>::forest *forest,
2082  {
2083  // the function gets two additional arguments, but we don't need them
2084  // since we know in which order p4est will walk through the cells
2085  // and have already built our weight lists in this order
2086 
2087  PartitionWeights<dim,spacedim> *this_object
2088  = reinterpret_cast<PartitionWeights<dim,spacedim>*>(forest->user_pointer);
2089 
2090  Assert (this_object->current_pointer >= this_object->cell_weights_list.begin(),
2091  ExcInternalError());
2092  Assert (this_object->current_pointer < this_object->cell_weights_list.end(),
2093  ExcInternalError());
2094 
2095  // get the weight, increment the pointer, and return the weight
2096  return *this_object->current_pointer++;
2097  }
2098 }
2099 
2100 
2101 // initialize p4est
2102 namespace internal
2103 {
2104  namespace p4est
2105  {
2106  struct InitFinalize
2107  {
2108  private:
2109  struct Singleton
2110  {
2111  Singleton ()
2112  {
2113  // ensure that the initialization code is run only once, even if we
2114  // link with 1d, 2d, and 3d libraries
2115  static bool initialized = false;
2116 
2117  if (initialized == false)
2118  {
2119  sc_init (MPI_COMM_WORLD,
2120  0, 0, 0, SC_LP_SILENT);
2121  p4est_init (0, SC_LP_SILENT);
2122 
2123  initialized = true;
2124  }
2125  }
2126 
2127  ~Singleton ()
2128  {
2129  // same here
2130  static bool deinitialized = false;
2131 
2132  if (deinitialized == false)
2133  {
2134  // p4est has no p4est_finalize function
2135  sc_finalize ();
2136 
2137  deinitialized = true;
2138  }
2139  }
2140  };
2141 
2142  public:
2143  // do run the initialization code, at least the first time around we get
2144  // to this function
2145  static void do_initialize ()
2146  {
2147  static Singleton singleton;
2148  }
2149  };
2150  }
2151 }
2152 
2153 
2154 namespace parallel
2155 {
2156  namespace distributed
2157  {
2158 
2159  /* ---------------------- class Triangulation<dim,spacedim> ------------------------------ */
2160 
2161 
2162  template <int dim, int spacedim>
2164  Triangulation (MPI_Comm mpi_communicator,
2165  const typename ::Triangulation<dim,spacedim>::MeshSmoothing smooth_grid,
2166  const Settings settings_)
2167  :
2168  // do not check for distorted cells
2169  ::parallel::Triangulation<dim,spacedim>
2170  (mpi_communicator,
2171  smooth_grid,
2172  false),
2173  settings(settings_),
2174  triangulation_has_content (false),
2175  connectivity (0),
2176  parallel_forest (0),
2177  refinement_in_progress (false),
2178  attached_data_size(0),
2179  n_attached_datas(0),
2180  n_attached_deserialize(0)
2181  {
2182  // initialize p4est. do this in a separate function since it has to
2183  // happen only once, even if we have triangulation objects for several
2184  // different space dimensions
2185  ::internal::p4est::InitFinalize::do_initialize ();
2186 
2187  parallel_ghost = 0;
2188  }
2189 
2190 
2191 
2192  template <int dim, int spacedim>
2194  {
2195  clear ();
2196 
2198  ExcInternalError());
2199  Assert (connectivity == 0, ExcInternalError());
2200  Assert (parallel_forest == 0, ExcInternalError());
2201  Assert (refinement_in_progress == false, ExcInternalError());
2202  }
2203 
2204 
2205 
2206 
2207  template <int dim, int spacedim>
2208  void
2211  const std::vector<CellData<dim> > &cells,
2212  const SubCellData &subcelldata)
2213  {
2214  try
2215  {
2217  create_triangulation (vertices, cells, subcelldata);
2218  }
2219  catch (const typename ::Triangulation<dim,spacedim>::DistortedCellList &)
2220  {
2221  // the underlying triangulation should not be checking for distorted
2222  // cells
2223  AssertThrow (false, ExcInternalError());
2224  }
2225 
2226  // note that now we have some content in the p4est objects and call the
2227  // functions that do the actual work (which are dimension dependent, so
2228  // separate)
2230 
2232 
2234 
2235  try
2236  {
2238  }
2239  catch (const typename Triangulation<dim>::DistortedCellList &)
2240  {
2241  // the underlying triangulation should not be checking for distorted
2242  // cells
2243  AssertThrow (false, ExcInternalError());
2244  }
2245 
2246  this->update_number_cache ();
2247  }
2248 
2249 
2250  // This anonymous namespace contains utility for
2251  // the function Triangulation::communicate_locally_moved_vertices
2252  namespace CommunicateLocallyMovedVertices
2253  {
2254  namespace
2255  {
2261  template <int dim, int spacedim>
2262  struct CellInfo
2263  {
2264  // store all the tree_indices we send/receive consecutively (n_cells entries)
2265  std::vector<unsigned int> tree_index;
2266  // store all the quadrants we send/receive consecutively (n_cells entries)
2267  std::vector<typename ::internal::p4est::types<dim>::quadrant> quadrants;
2268  // store for each cell the number of vertices we send/receive
2269  // and then the vertex indices (for each cell: n_vertices+1 entries)
2270  std::vector<unsigned int> vertex_indices;
2271  // store for each cell the vertices we send/receive
2272  // (for each cell n_vertices entries)
2273  std::vector<::Point<spacedim> > vertices;
2274  // for receiving and unpacking data we need to store pointers to the
2275  // first vertex and vertex_index on each cell additionally
2276  // both vectors have as many entries as there are cells
2277  std::vector<unsigned int * > first_vertex_indices;
2278  std::vector<::Point<spacedim>* > first_vertices;
2279 
2280  unsigned int bytes_for_buffer () const
2281  {
2282  return (sizeof(unsigned int) +
2283  tree_index.size() * sizeof(unsigned int) +
2284  quadrants.size() * sizeof(typename ::internal::p4est
2285  ::types<dim>::quadrant) +
2286  vertices.size() * sizeof(::Point<spacedim>)) +
2287  vertex_indices.size() * sizeof(unsigned int);
2288  }
2289 
2290  void pack_data (std::vector<char> &buffer) const
2291  {
2292  buffer.resize(bytes_for_buffer());
2293 
2294  char *ptr = &buffer[0];
2295 
2296  const unsigned int num_cells = tree_index.size();
2297  std::memcpy(ptr, &num_cells, sizeof(unsigned int));
2298  ptr += sizeof(unsigned int);
2299 
2300  std::memcpy(ptr,
2301  &tree_index[0],
2302  num_cells*sizeof(unsigned int));
2303  ptr += num_cells*sizeof(unsigned int);
2304 
2305  std::memcpy(ptr,
2306  &quadrants[0],
2307  num_cells * sizeof(typename ::internal::p4est::
2308  types<dim>::quadrant));
2309  ptr += num_cells*sizeof(typename ::internal::p4est::types<dim>::
2310  quadrant);
2311 
2312  std::memcpy(ptr,
2313  &vertex_indices[0],
2314  vertex_indices.size() * sizeof(unsigned int));
2315  ptr += vertex_indices.size() * sizeof(unsigned int);
2316 
2317  std::memcpy(ptr,
2318  &vertices[0],
2319  vertices.size() * sizeof(::Point<spacedim>));
2320  ptr += vertices.size() * sizeof(::Point<spacedim>);
2321 
2322  Assert (ptr == &buffer[0]+buffer.size(),
2323  ExcInternalError());
2324 
2325  }
2326 
2327  void unpack_data (const std::vector<char> &buffer)
2328  {
2329  const char *ptr = &buffer[0];
2330  unsigned int cells;
2331  memcpy(&cells, ptr, sizeof(unsigned int));
2332  ptr += sizeof(unsigned int);
2333 
2334  tree_index.resize(cells);
2335  memcpy(&tree_index[0],ptr,sizeof(unsigned int)*cells);
2336  ptr += sizeof(unsigned int)*cells;
2337 
2338  quadrants.resize(cells);
2339  memcpy(&quadrants[0],ptr,
2340  sizeof(typename ::internal::p4est::types<dim>::quadrant)*cells);
2341  ptr += sizeof(typename ::internal::p4est::types<dim>::quadrant)*cells;
2342 
2343  vertex_indices.clear();
2344  first_vertex_indices.resize(cells);
2345  std::vector<unsigned int> n_vertices_on_cell(cells);
2346  std::vector<unsigned int> first_indices (cells);
2347  for (unsigned int c=0; c<cells; ++c)
2348  {
2349  // The first 'vertex index' is the number of vertices.
2350  // Additionally, we need to store the pointer to this
2351  // vertex index with respect to the std::vector
2352  const unsigned int *const vertex_index
2353  = reinterpret_cast<const unsigned int *const>(ptr);
2354  first_indices[c] = vertex_indices.size();
2355  vertex_indices.push_back(*vertex_index);
2356  n_vertices_on_cell[c] = *vertex_index;
2357  ptr += sizeof(unsigned int);
2358  // Now copy all the 'real' vertex_indices
2359  vertex_indices.resize(vertex_indices.size() + n_vertices_on_cell[c]);
2360  memcpy(&vertex_indices[vertex_indices.size() - n_vertices_on_cell[c]],
2361  ptr, n_vertices_on_cell[c]*sizeof(unsigned int));
2362  ptr += n_vertices_on_cell[c]*sizeof(unsigned int);
2363  }
2364  for (unsigned int c=0; c<cells; ++c)
2365  first_vertex_indices[c] = &vertex_indices[first_indices[c]];
2366 
2367  vertices.clear();
2368  first_vertices.resize(cells);
2369  for (unsigned int c=0; c<cells; ++c)
2370  {
2371  // We need to store a pointer to the first vertex.
2372  const ::Point<spacedim> *const vertex
2373  = reinterpret_cast<const ::Point<spacedim> * const>(ptr);
2374  first_indices[c] = vertices.size();
2375  vertices.push_back(*vertex);
2376  ptr += sizeof(::Point<spacedim>);
2377  vertices.resize(vertices.size() + n_vertices_on_cell[c]-1);
2378  memcpy(&vertices[vertices.size() - (n_vertices_on_cell[c]-1)],
2379  ptr, (n_vertices_on_cell[c]-1)*sizeof(::Point<spacedim>));
2380  ptr += (n_vertices_on_cell[c]-1)*sizeof(::Point<spacedim>);
2381  }
2382  for (unsigned int c=0; c<cells; ++c)
2383  first_vertices[c] = &vertices[first_indices[c]];
2384 
2385  Assert (ptr == &buffer[0]+buffer.size(),
2386  ExcInternalError());
2387  }
2388  };
2389 
2390 
2391 
2392  // This function is responsible for gathering the information
2393  // we want to send to each process.
2394  // For each dealii cell on the coarsest level the corresponding
2395  // p4est_cell has to be provided when calling this function.
2396  // By recursing through all children we consider each active cell.
2397  // vertices_with_ghost_neighbors tells us which vertices
2398  // are in the ghost layer and for which processes they might
2399  // be interesting.
2400  // Whether a vertex has actually been updated locally is
2401  // stored in vertex_locally_moved. Only those are considered
2402  // for sending.
2403  // The gathered information is saved into needs_to_get_cell.
2404  template <int dim, int spacedim>
2405  void
2406  fill_vertices_recursively (const typename parallel::distributed::Triangulation<dim,spacedim> &tria,
2407  const unsigned int tree_index,
2408  const typename Triangulation<dim,spacedim>::cell_iterator &dealii_cell,
2409  const typename ::internal::p4est::types<dim>::quadrant &p4est_cell,
2410  const std::map<unsigned int, std::set<::types::subdomain_id> > &vertices_with_ghost_neighbors,
2411  const std::vector<bool> &vertex_locally_moved,
2412  std::map<::types::subdomain_id, CellInfo<dim, spacedim> > &needs_to_get_cell)
2413  {
2414  // see if we have to
2415  // recurse...
2416  if (dealii_cell->has_children())
2417  {
2418  typename ::internal::p4est::types<dim>::quadrant
2419  p4est_child[GeometryInfo<dim>::max_children_per_cell];
2420  ::internal::p4est::init_quadrant_children<dim>(p4est_cell, p4est_child);
2421 
2422 
2423  for (unsigned int c=0; c<GeometryInfo<dim>::max_children_per_cell; ++c)
2424  fill_vertices_recursively<dim,spacedim>(tria,
2425  tree_index,
2426  dealii_cell->child(c),
2427  p4est_child[c],
2428  vertices_with_ghost_neighbors,
2429  vertex_locally_moved,
2430  needs_to_get_cell);
2431  return;
2432  }
2433 
2434  // We're at a leaf cell. If the cell is locally owned, we may
2435  // have to send its vertices to other processors if any of
2436  // its vertices is adjacent to a ghost cell and has been moved
2437  //
2438  // If one of the vertices of the cell is interesting,
2439  // send all moved vertices of the cell to all processors
2440  // adjacent to all cells adjacent to this vertex
2441  if (dealii_cell->is_locally_owned())
2442  {
2443  std::set<::types::subdomain_id> send_to;
2444  for (unsigned int v=0; v<GeometryInfo<dim>::vertices_per_cell; ++v)
2445  {
2446  const std::map<unsigned int, std::set<::types::subdomain_id> >::const_iterator
2447  neighbor_subdomains_of_vertex
2448  = vertices_with_ghost_neighbors.find (dealii_cell->vertex_index(v));
2449 
2450  if (neighbor_subdomains_of_vertex
2451  != vertices_with_ghost_neighbors.end())
2452  {
2453  Assert(neighbor_subdomains_of_vertex->second.size()!=0,
2454  ExcInternalError());
2455  send_to.insert(neighbor_subdomains_of_vertex->second.begin(),
2456  neighbor_subdomains_of_vertex->second.end());
2457  }
2458  }
2459 
2460  if (send_to.size() > 0)
2461  {
2462  std::vector<unsigned int> vertex_indices;
2463  std::vector<::Point<spacedim> > local_vertices;
2464  for (unsigned int v=0; v<GeometryInfo<dim>::vertices_per_cell; ++v)
2465  if (vertex_locally_moved[dealii_cell->vertex_index(v)])
2466  {
2467  vertex_indices.push_back(v);
2468  local_vertices.push_back(dealii_cell->vertex(v));
2469  }
2470 
2471  if (vertex_indices.size()>0)
2472  for (std::set<::types::subdomain_id>::iterator it=send_to.begin();
2473  it!=send_to.end(); ++it)
2474  {
2475  const ::types::subdomain_id subdomain = *it;
2476 
2477  // get an iterator to what needs to be sent to that
2478  // subdomain (if already exists), or create such an object
2479  const typename std::map<::types::subdomain_id, CellInfo<dim, spacedim> >::iterator
2480  p
2481  = needs_to_get_cell.insert (std::make_pair(subdomain,
2482  CellInfo<dim,spacedim>()))
2483  .first;
2484 
2485  p->second.tree_index.push_back(tree_index);
2486  p->second.quadrants.push_back(p4est_cell);
2487 
2488  p->second.vertex_indices.push_back(vertex_indices.size());
2489  p->second.vertex_indices.insert(p->second.vertex_indices.end(),
2490  vertex_indices.begin(),
2491  vertex_indices.end());
2492 
2493  p->second.vertices.insert(p->second.vertices.end(),
2494  local_vertices.begin(),
2495  local_vertices.end());
2496  }
2497  }
2498  }
2499  }
2500 
2501 
2502 
2503  // After the cell data has been received this function is responsible
2504  // for moving the vertices in the corresponding ghost layer locally.
2505  // As in fill_vertices_recursively for each dealii cell on the
2506  // coarsest level the corresponding p4est_cell has to be provided
2507  // when calling this function. By recursing through through all
2508  // children we consider each active cell.
2509  // Additionally, we need to give a pointer to the first vertex indices
2510  // and vertices. Since the first information saved in vertex_indices
2511  // is the number of vertices this all the information we need.
2512  template <int dim, int spacedim>
2513  void
2514  set_vertices_recursively (
2516  const typename ::internal::p4est::types<dim>::quadrant &p4est_cell,
2517  const typename Triangulation<dim,spacedim>::cell_iterator &dealii_cell,
2518  const typename ::internal::p4est::types<dim>::quadrant &quadrant,
2519  const ::Point<spacedim> *const vertices,
2520  const unsigned int *const vertex_indices)
2521  {
2522  if (::internal::p4est::quadrant_is_equal<dim>(p4est_cell, quadrant))
2523  {
2524  Assert(!dealii_cell->is_artificial(), ExcInternalError());
2525  Assert(!dealii_cell->has_children(), ExcInternalError());
2526  Assert(!dealii_cell->is_locally_owned(), ExcInternalError());
2527 
2528  const unsigned int n_vertices = vertex_indices[0];
2529 
2530  // update dof indices of cell
2531  for (unsigned int i=0; i<n_vertices; ++i)
2532  dealii_cell->vertex(vertex_indices[i+1]) = vertices[i];
2533 
2534  return;
2535  }
2536 
2537  if (! dealii_cell->has_children())
2538  return;
2539 
2540  if (! ::internal::p4est::quadrant_is_ancestor<dim> (p4est_cell, quadrant))
2541  return;
2542 
2543  typename ::internal::p4est::types<dim>::quadrant
2544  p4est_child[GeometryInfo<dim>::max_children_per_cell];
2545  ::internal::p4est::init_quadrant_children<dim>(p4est_cell, p4est_child);
2546 
2547  for (unsigned int c=0; c<GeometryInfo<dim>::max_children_per_cell; ++c)
2548  set_vertices_recursively<dim,spacedim> (tria, p4est_child[c],
2549  dealii_cell->child(c),
2550  quadrant, vertices,
2551  vertex_indices);
2552  }
2553  }
2554  }
2555 
2556 
2557 
2558  template <int dim, int spacedim>
2559  void
2561  {
2562  triangulation_has_content = false;
2563 
2564  if (parallel_ghost != 0)
2565  {
2567  parallel_ghost = 0;
2568  }
2569 
2570  if (parallel_forest != 0)
2571  {
2573  parallel_forest = 0;
2574  }
2575 
2576  if (connectivity != 0)
2577  {
2579  connectivity = 0;
2580  }
2581 
2582  coarse_cell_to_p4est_tree_permutation.resize (0);
2583  p4est_tree_to_coarse_cell_permutation.resize (0);
2584 
2586 
2588 
2589  this->update_number_cache ();
2590  }
2591 
2592  template <int dim, int spacedim>
2593  bool
2595  {
2596  if (this->n_global_levels()<=1)
2597  return false; // can not have hanging nodes without refined cells
2598 
2599  // if there are any active cells with level less than n_global_levels()-1, then
2600  // there is obviously also one with level n_global_levels()-1, and
2601  // consequently there must be a hanging node somewhere.
2602  //
2603  // The problem is that we cannot just ask for the first active cell, but
2604  // instead need to filter over locally owned cells.
2605  bool have_coarser_cell = false;
2607  cell != this->end(this->n_global_levels()-2);
2608  ++cell)
2609  if (cell->is_locally_owned())
2610  {
2611  have_coarser_cell = true;
2612  break;
2613  }
2614 
2615  // return true if at least one process has a coarser cell
2616  return 0<Utilities::MPI::max(have_coarser_cell?1:0, this->mpi_communicator);
2617  }
2618 
2619 
2620 
2621  template <int dim, int spacedim>
2622  void
2624  {
2625  DynamicSparsityPattern cell_connectivity;
2626  GridTools::get_vertex_connectivity_of_cells (*this, cell_connectivity);
2627  coarse_cell_to_p4est_tree_permutation.resize (this->n_cells(0));
2629  reorder_hierarchical (cell_connectivity,
2630  coarse_cell_to_p4est_tree_permutation);
2631 
2632  p4est_tree_to_coarse_cell_permutation
2633  = Utilities::invert_permutation (coarse_cell_to_p4est_tree_permutation);
2634  }
2635 
2636 
2637 
2638  template <int dim, int spacedim>
2639  void
2640  Triangulation<dim,spacedim>::write_mesh_vtk (const char *file_basename) const
2641  {
2642  Assert (parallel_forest != 0,
2643  ExcMessage ("Can't produce output when no forest is created yet."));
2645  vtk_write_file (parallel_forest, 0, file_basename);
2646  }
2647 
2648 
2649  template <int dim, int spacedim>
2650  void
2652  save(const char *filename) const
2653  {
2655  ExcMessage ("not all SolutionTransfer's got deserialized after the last load()"));
2656  int real_data_size = 0;
2657  if (attached_data_size>0)
2658  real_data_size = attached_data_size+sizeof(CellStatus);
2659 
2660  Assert(this->n_cells()>0, ExcMessage("Can not save() an empty Triangulation."));
2661 
2662  if (this->my_subdomain==0)
2663  {
2664  std::string fname=std::string(filename)+".info";
2665  std::ofstream f(fname.c_str());
2666  f << "version nproc attached_bytes n_attached_objs n_coarse_cells" << std::endl
2667  << 2 << " "
2669  << real_data_size << " "
2670  << attached_data_pack_callbacks.size() << " "
2671  << this->n_cells(0)
2672  << std::endl;
2673  }
2674 
2675  if (attached_data_size>0)
2676  {
2678  ->attach_mesh_data();
2679  }
2680 
2681  ::internal::p4est::functions<dim>::save(filename, parallel_forest, attached_data_size>0);
2682 
2685 
2686  tria->n_attached_datas = 0;
2687  tria->attached_data_size = 0;
2688  tria->attached_data_pack_callbacks.clear();
2689 
2690  // and release the data
2691  void *userptr = parallel_forest->user_pointer;
2692  ::internal::p4est::functions<dim>::reset_data (parallel_forest, 0, NULL, NULL);
2693  parallel_forest->user_pointer = userptr;
2694  }
2695 
2696 
2697  template <int dim, int spacedim>
2698  void
2700  load (const char *filename,
2701  const bool autopartition)
2702  {
2703  Assert(this->n_cells()>0, ExcMessage("load() only works if the Triangulation already contains a coarse mesh!"));
2704  Assert(this->n_levels()==1, ExcMessage("Triangulation may only contain coarse cells when calling load()."));
2705 
2706 
2707  if (parallel_ghost != 0)
2708  {
2710  parallel_ghost = 0;
2711  }
2713  parallel_forest = 0;
2715  connectivity = 0;
2716 
2717  unsigned int version, numcpus, attached_size, attached_count, n_coarse_cells;
2718  {
2719  std::string fname=std::string(filename)+".info";
2720  std::ifstream f(fname.c_str());
2721  std::string firstline;
2722  getline(f, firstline); //skip first line
2723  f >> version >> numcpus >> attached_size >> attached_count >> n_coarse_cells;
2724  }
2725 
2726  Assert(version == 2, ExcMessage("Incompatible version found in .info file."));
2727  Assert(this->n_cells(0) == n_coarse_cells, ExcMessage("Number of coarse cells differ!"));
2728 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
2729 #else
2731  ExcMessage("parallel::distributed::Triangulation::load() only supports loading "
2732  "saved data with a greater or equal number of processes than were used to "
2733  "save() when using p4est 0.3.4.2."));
2734 #endif
2735 
2736  attached_data_size = 0;
2737  n_attached_datas = 0;
2738  n_attached_deserialize = attached_count;
2739 
2740 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,3)
2742  filename, this->mpi_communicator,
2743  attached_size, attached_size>0,
2744  autopartition, 0,
2745  this,
2746  &connectivity);
2747 #else
2748  (void)autopartition;
2749  parallel_forest = ::internal::p4est::functions<dim>::load (
2750  filename, this->mpi_communicator,
2751  attached_size, attached_size>0,
2752  this,
2753  &connectivity);
2754 #endif
2755  if (numcpus != Utilities::MPI::n_mpi_processes (this->mpi_communicator))
2756  // We are changing the number of CPUs so we need to repartition.
2757  // Note that p4est actually distributes the cells between the changed
2758  // number of CPUs and so everything works without this call, but
2759  // this command changes the distribution for some reason, so we
2760  // will leave it in here.
2761  repartition();
2762 
2763  try
2764  {
2766  }
2767  catch (const typename Triangulation<dim>::DistortedCellList &)
2768  {
2769  // the underlying
2770  // triangulation should not
2771  // be checking for
2772  // distorted cells
2773  AssertThrow (false, ExcInternalError());
2774  }
2775 
2776  this->update_number_cache ();
2777  }
2778 
2779 
2780 
2781  template <int dim, int spacedim>
2782  unsigned int
2784  {
2785  Assert (parallel_forest != 0,
2786  ExcMessage ("Can't produce a check sum when no forest is created yet."));
2787  return ::internal::p4est::functions<dim>::checksum (parallel_forest);
2788  }
2789 
2790  template <int dim, int spacedim>
2791  void
2794  {
2796 
2797  if (this->n_levels() == 0)
2798  return;
2799 
2801  {
2802  // find level ghost owners
2804  cell = this->begin();
2805  cell != this->end();
2806  ++cell)
2807  if (cell->level_subdomain_id() != numbers::artificial_subdomain_id
2808  && cell->level_subdomain_id() != this->locally_owned_subdomain())
2809  this->number_cache.level_ghost_owners.insert(cell->level_subdomain_id());
2810 
2811  Assert(this->number_cache.level_ghost_owners.size() < Utilities::MPI::n_mpi_processes(this->mpi_communicator), ExcInternalError());
2812  }
2813  }
2814 
2815 
2816  template <int dim, int spacedim>
2817  typename ::internal::p4est::types<dim>::tree *
2819  init_tree(const int dealii_coarse_cell_index) const
2820  {
2821  const unsigned int tree_index
2822  = coarse_cell_to_p4est_tree_permutation[dealii_coarse_cell_index];
2823  typename ::internal::p4est::types<dim>::tree *tree
2824  = static_cast<typename ::internal::p4est::types<dim>::tree *>
2825  (sc_array_index (parallel_forest->trees,
2826  tree_index));
2827 
2828  return tree;
2829  }
2830 
2831 
2832 
2833  template <>
2834  void
2836  {
2837  const unsigned int dim = 2, spacedim = 2;
2838  Assert (this->n_cells(0) > 0, ExcInternalError());
2839  Assert (this->n_levels() == 1, ExcInternalError());
2840 
2841  // data structures that counts how many cells touch each vertex
2842  // (vertex_touch_count), and which cells touch a given vertex (together
2843  // with the local numbering of that vertex within the cells that touch
2844  // it)
2845  std::vector<unsigned int> vertex_touch_count;
2846  std::vector<
2847  std::list<
2848  std::pair<Triangulation<dim,spacedim>::active_cell_iterator,
2849  unsigned int> > >
2850  vertex_to_cell;
2851  get_vertex_to_cell_mappings (*this,
2852  vertex_touch_count,
2853  vertex_to_cell);
2854  const ::internal::p4est::types<2>::locidx
2855  num_vtt = std::accumulate (vertex_touch_count.begin(),
2856  vertex_touch_count.end(),
2857  0);
2858 
2859  // now create a connectivity object with the right sizes for all
2860  // arrays. set vertex information only in debug mode (saves a few bytes
2861  // in optimized mode)
2862  const bool set_vertex_info
2863 #ifdef DEBUG
2864  = true
2865 #else
2866  = false
2867 #endif
2868  ;
2869 
2870  connectivity
2872  connectivity_new ((set_vertex_info == true ? this->n_vertices() : 0),
2873  this->n_cells(0),
2874  this->n_vertices(),
2875  num_vtt);
2876 
2877  set_vertex_and_cell_info (*this,
2878  vertex_touch_count,
2879  vertex_to_cell,
2880  coarse_cell_to_p4est_tree_permutation,
2881  set_vertex_info,
2882  connectivity);
2883 
2884  Assert (p4est_connectivity_is_valid (connectivity) == 1,
2885  ExcInternalError());
2886 
2887  // now create a forest out of the connectivity data structure
2888  parallel_forest
2891  connectivity,
2892  /* minimum initial number of quadrants per tree */ 0,
2893  /* minimum level of upfront refinement */ 0,
2894  /* use uniform upfront refinement */ 1,
2895  /* user_data_size = */ 0,
2896  /* user_data_constructor = */ NULL,
2897  /* user_pointer */ this);
2898  }
2899 
2900 
2901 
2902  // TODO: This is a verbatim copy of the 2,2 case. However, we can't just
2903  // specialize the dim template argument, but let spacedim open
2904  template <>
2905  void
2907  {
2908  const unsigned int dim = 2, spacedim = 3;
2909  Assert (this->n_cells(0) > 0, ExcInternalError());
2910  Assert (this->n_levels() == 1, ExcInternalError());
2911 
2912  // data structures that counts how many cells touch each vertex
2913  // (vertex_touch_count), and which cells touch a given vertex (together
2914  // with the local numbering of that vertex within the cells that touch
2915  // it)
2916  std::vector<unsigned int> vertex_touch_count;
2917  std::vector<
2918  std::list<
2919  std::pair<Triangulation<dim,spacedim>::active_cell_iterator,
2920  unsigned int> > >
2921  vertex_to_cell;
2922  get_vertex_to_cell_mappings (*this,
2923  vertex_touch_count,
2924  vertex_to_cell);
2925  const ::internal::p4est::types<2>::locidx
2926  num_vtt = std::accumulate (vertex_touch_count.begin(),
2927  vertex_touch_count.end(),
2928  0);
2929 
2930  // now create a connectivity object with the right sizes for all
2931  // arrays. set vertex information only in debug mode (saves a few bytes
2932  // in optimized mode)
2933  const bool set_vertex_info
2934 #ifdef DEBUG
2935  = true
2936 #else
2937  = false
2938 #endif
2939  ;
2940 
2941  connectivity
2943  connectivity_new ((set_vertex_info == true ? this->n_vertices() : 0),
2944  this->n_cells(0),
2945  this->n_vertices(),
2946  num_vtt);
2947 
2948  set_vertex_and_cell_info (*this,
2949  vertex_touch_count,
2950  vertex_to_cell,
2951  coarse_cell_to_p4est_tree_permutation,
2952  set_vertex_info,
2953  connectivity);
2954 
2955  Assert (p4est_connectivity_is_valid (connectivity) == 1,
2956  ExcInternalError());
2957 
2958  // now create a forest out of the connectivity data structure
2959  parallel_forest
2962  connectivity,
2963  /* minimum initial number of quadrants per tree */ 0,
2964  /* minimum level of upfront refinement */ 0,
2965  /* use uniform upfront refinement */ 1,
2966  /* user_data_size = */ 0,
2967  /* user_data_constructor = */ NULL,
2968  /* user_pointer */ this);
2969  }
2970 
2971 
2972 
2973  template <>
2974  void
2976  {
2977  const int dim = 3, spacedim = 3;
2978  Assert (this->n_cells(0) > 0, ExcInternalError());
2979  Assert (this->n_levels() == 1, ExcInternalError());
2980 
2981  // data structures that counts how many cells touch each vertex
2982  // (vertex_touch_count), and which cells touch a given vertex (together
2983  // with the local numbering of that vertex within the cells that touch
2984  // it)
2985  std::vector<unsigned int> vertex_touch_count;
2986  std::vector<
2987  std::list<
2988  std::pair<Triangulation<3>::active_cell_iterator,
2989  unsigned int> > >
2990  vertex_to_cell;
2991  get_vertex_to_cell_mappings (*this,
2992  vertex_touch_count,
2993  vertex_to_cell);
2994  const ::internal::p4est::types<2>::locidx
2995  num_vtt = std::accumulate (vertex_touch_count.begin(),
2996  vertex_touch_count.end(),
2997  0);
2998 
2999  std::vector<unsigned int> edge_touch_count;
3000  std::vector<
3001  std::list<
3002  std::pair<Triangulation<3>::active_cell_iterator,
3003  unsigned int> > >
3004  edge_to_cell;
3005  get_edge_to_cell_mappings (*this,
3006  edge_touch_count,
3007  edge_to_cell);
3008  const ::internal::p4est::types<2>::locidx
3009  num_ett = std::accumulate (edge_touch_count.begin(),
3010  edge_touch_count.end(),
3011  0);
3012 
3013  // now create a connectivity object with the right sizes for all arrays
3014  const bool set_vertex_info
3015 #ifdef DEBUG
3016  = true
3017 #else
3018  = false
3019 #endif
3020  ;
3021 
3022  connectivity
3024  connectivity_new ((set_vertex_info == true ? this->n_vertices() : 0),
3025  this->n_cells(0),
3026  this->n_active_lines(),
3027  num_ett,
3028  this->n_vertices(),
3029  num_vtt);
3030 
3031  set_vertex_and_cell_info (*this,
3032  vertex_touch_count,
3033  vertex_to_cell,
3034  coarse_cell_to_p4est_tree_permutation,
3035  set_vertex_info,
3036  connectivity);
3037 
3038  // next to tree-to-edge
3039  // data. note that in p4est lines
3040  // are ordered as follows
3041  // *---3---* *---3---*
3042  // /| | / /|
3043  // 6 | 11 6 7 11
3044  // / 10 | / / |
3045  // * | | *---2---* |
3046  // | *---1---* | | *
3047  // | / / | 9 /
3048  // 8 4 5 8 | 5
3049  // |/ / | |/
3050  // *---0---* *---0---*
3051  // whereas in deal.II they are like this:
3052  // *---7---* *---7---*
3053  // /| | / /|
3054  // 4 | 11 4 5 11
3055  // / 10 | / / |
3056  // * | | *---6---* |
3057  // | *---3---* | | *
3058  // | / / | 9 /
3059  // 8 0 1 8 | 1
3060  // |/ / | |/
3061  // *---2---* *---2---*
3062 
3063  const unsigned int deal_to_p4est_line_index[12]
3064  = { 4, 5, 0, 1, 6, 7, 2, 3, 8, 9, 10, 11 } ;
3065 
3066  for (Triangulation<dim,spacedim>::active_cell_iterator
3067  cell = this->begin_active();
3068  cell != this->end(); ++cell)
3069  {
3070  const unsigned int
3071  index = coarse_cell_to_p4est_tree_permutation[cell->index()];
3072  for (unsigned int e=0; e<GeometryInfo<3>::lines_per_cell; ++e)
3073  connectivity->tree_to_edge[index*GeometryInfo<3>::lines_per_cell+
3074  deal_to_p4est_line_index[e]]
3075  = cell->line(e)->index();
3076  }
3077 
3078  // now also set edge-to-tree
3079  // information
3080  connectivity->ett_offset[0] = 0;
3081  std::partial_sum (edge_touch_count.begin(),
3082  edge_touch_count.end(),
3083  &connectivity->ett_offset[1]);
3084 
3085  Assert (connectivity->ett_offset[this->n_active_lines()] ==
3086  num_ett,
3087  ExcInternalError());
3088 
3089  for (unsigned int v=0; v<this->n_active_lines(); ++v)
3090  {
3091  Assert (edge_to_cell[v].size() == edge_touch_count[v],
3092  ExcInternalError());
3093 
3094  std::list<std::pair
3095  <Triangulation<dim,spacedim>::active_cell_iterator,
3096  unsigned int> >::const_iterator
3097  p = edge_to_cell[v].begin();
3098  for (unsigned int c=0; c<edge_touch_count[v]; ++c, ++p)
3099  {
3100  connectivity->edge_to_tree[connectivity->ett_offset[v]+c]
3101  = coarse_cell_to_p4est_tree_permutation[p->first->index()];
3102  connectivity->edge_to_edge[connectivity->ett_offset[v]+c]
3103  = deal_to_p4est_line_index[p->second];
3104  }
3105  }
3106 
3107  Assert (p8est_connectivity_is_valid (connectivity) == 1,
3108  ExcInternalError());
3109 
3110  // now create a forest out of the connectivity data structure
3111  parallel_forest
3114  connectivity,
3115  /* minimum initial number of quadrants per tree */ 0,
3116  /* minimum level of upfront refinement */ 0,
3117  /* use uniform upfront refinement */ 1,
3118  /* user_data_size = */ 0,
3119  /* user_data_constructor = */ NULL,
3120  /* user_pointer */ this);
3121  }
3122 
3123 
3124 
3125  namespace
3126  {
3127  // this function combines vertices that have different locations (and
3128  // thus, different vertex_index) but represent the same topological
3129  // entity over periodic boundaries. The vector
3130  // topological_vertex_numbering contains a linear map from 0 to
3131  // n_vertices at input and at output relates periodic vertices with only
3132  // one vertex index. The output is used to always identify the same
3133  // vertex according to the periodicity, e.g. when finding the maximum
3134  // cell level around a vertex.
3135  //
3136  // Example: On a 3D cell with vertices numbered from 0 to 7 and periodic
3137  // boundary conditions in x direction, the vector
3138  // topological_vertex_numbering will contain the numbers
3139  // {0,0,2,2,4,4,6,6} (because the vertex pairs {0,1}, {2,3}, {4,5},
3140  // {6,7} belong together, respectively). If periodicity is set in x and
3141  // z direction, the output is {0,0,2,2,0,0,2,2}, and if periodicity is
3142  // in all directions, the output is simply {0,0,0,0,0,0,0,0}.
3143  template <typename ITERATOR>
3144  void
3145  identify_periodic_vertices_recursively(const GridTools::PeriodicFacePair<ITERATOR> &periodic,
3146  std::vector<unsigned int> &topological_vertex_numbering)
3147  {
3148  const unsigned int dim = ITERATOR::AccessorType::dimension;
3149 
3150  // for hanging nodes we will consider all necessary coupling already
3151  // on the parent level, so we just need to consider neighbors of the
3152  // same level
3153  if (periodic.cell[0]->has_children() &&
3154  periodic.cell[1]->has_children())
3155  {
3156  // copy orientations etc. from parent to child
3157  GridTools::PeriodicFacePair<ITERATOR> periodic_child = periodic;
3158 
3159  // find appropriate pairs of child elements
3160  for (unsigned int cf=0; cf<periodic.cell[0]->face(periodic.face_idx[0])->n_children(); ++cf)
3161  {
3162  const unsigned int child_index_0 =
3163  GeometryInfo<dim>::child_cell_on_face(periodic.cell[0]->refinement_case(),
3164  periodic.face_idx[0], cf,
3165  periodic.orientation[0],
3166  periodic.orientation[1],
3167  periodic.orientation[2]);
3168  periodic_child.cell[0] = periodic.cell[0]->child(child_index_0);
3169  periodic_child.face_idx[0] = periodic.face_idx[0];
3170 
3171  // the second face is in standard orientation in terms of the
3172  // periodic face pair
3173  const unsigned int child_index_1 =
3174  GeometryInfo<dim>::child_cell_on_face(periodic.cell[1]->refinement_case(),
3175  periodic.face_idx[1], cf);
3176 
3177  periodic_child.cell[1] = periodic.cell[1]->child(child_index_1);
3178  periodic_child.face_idx[1] = periodic.face_idx[1];
3179 
3180  // recursive call into children
3181  identify_periodic_vertices_recursively (periodic_child,
3182  topological_vertex_numbering);
3183  }
3184  }
3185 
3186  for (unsigned int v=0; v<GeometryInfo<dim-1>::vertices_per_cell; ++v)
3187  {
3188  // take possible non-standard orientation of face on cell[0] into
3189  // account
3190  const unsigned int vface0 =
3192  periodic.orientation[1],
3193  periodic.orientation[2]);
3194  const unsigned int vi0 = topological_vertex_numbering[periodic.cell[0]->face(periodic.face_idx[0])->vertex_index(vface0)];
3195  const unsigned int vi1 = topological_vertex_numbering[periodic.cell[1]->face(periodic.face_idx[1])->vertex_index(v)];
3196  const unsigned int min_index = std::min(vi0, vi1);
3197  topological_vertex_numbering[periodic.cell[0]->face(periodic.face_idx[0])->vertex_index(vface0)]
3198  = topological_vertex_numbering[periodic.cell[1]->face(periodic.face_idx[1])->vertex_index(v)]
3199  = min_index;
3200  }
3201  }
3202 
3203 
3204 
3205  // ensures the 2:1 mesh balance for periodic boundary conditions in the
3206  // artificial cell layer (the active cells are taken care of by p4est)
3207  template <int dim, int spacedim>
3208  bool enforce_mesh_balance_over_periodic_boundaries
3210  const std::vector<GridTools::PeriodicFacePair<typename ::Triangulation<dim,spacedim>::cell_iterator> > periodic_face_pairs_level_0)
3211  {
3212  if (periodic_face_pairs_level_0.empty())
3213  return false;
3214 
3215  std::vector<bool> flags_before[2];
3216  tria.save_coarsen_flags (flags_before[0]);
3217  tria.save_refine_flags (flags_before[1]);
3218 
3219  std::vector<unsigned int> topological_vertex_numbering(tria.n_vertices());
3220  for (unsigned int i=0; i<topological_vertex_numbering.size(); ++i)
3221  topological_vertex_numbering[i] = i;
3222  for (unsigned int i=0; i<periodic_face_pairs_level_0.size(); ++i)
3223  {
3224  identify_periodic_vertices_recursively(periodic_face_pairs_level_0[i],
3225  topological_vertex_numbering);
3226  }
3227 
3228  // this code is replicated from grid/tria.cc but using an indirection
3229  // for periodic boundary conditions
3230  bool continue_iterating = true;
3231  std::vector<int> vertex_level(tria.n_vertices());
3232  while (continue_iterating)
3233  {
3234  // store highest level one of the cells adjacent to a vertex
3235  // belongs to
3236  std::fill (vertex_level.begin(), vertex_level.end(), 0);
3237  typename Triangulation<dim,spacedim>::active_cell_iterator
3238  cell = tria.begin_active(), endc = tria.end();
3239  for (; cell!=endc; ++cell)
3240  {
3241  if (cell->refine_flag_set())
3242  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell;
3243  ++vertex)
3244  vertex_level[topological_vertex_numbering[cell->vertex_index(vertex)]]
3245  = std::max (vertex_level[topological_vertex_numbering[cell->vertex_index(vertex)]],
3246  cell->level()+1);
3247  else if (!cell->coarsen_flag_set())
3248  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell;
3249  ++vertex)
3250  vertex_level[topological_vertex_numbering[cell->vertex_index(vertex)]]
3251  = std::max (vertex_level[topological_vertex_numbering[cell->vertex_index(vertex)]],
3252  cell->level());
3253  else
3254  {
3255  // if coarsen flag is set then tentatively assume
3256  // that the cell will be coarsened. this isn't
3257  // always true (the coarsen flag could be removed
3258  // again) and so we may make an error here. we try
3259  // to correct this by iterating over the entire
3260  // process until we are converged
3261  Assert (cell->coarsen_flag_set(), ExcInternalError());
3262  for (unsigned int vertex=0; vertex<GeometryInfo<dim>::vertices_per_cell;
3263  ++vertex)
3264  vertex_level[topological_vertex_numbering[cell->vertex_index(vertex)]]
3265  = std::max (vertex_level[topological_vertex_numbering[cell->vertex_index(vertex)]],
3266  cell->level()-1);
3267  }
3268  }
3269 
3270  continue_iterating = false;
3271 
3272  // loop over all cells in reverse order. do so because we
3273  // can then update the vertex levels on the adjacent
3274  // vertices and maybe already flag additional cells in this
3275  // loop
3276  //
3277  // note that not only may we have to add additional
3278  // refinement flags, but we will also have to remove
3279  // coarsening flags on cells adjacent to vertices that will
3280  // see refinement
3281  for (cell=tria.last_active(); cell != endc; --cell)
3282  if (cell->refine_flag_set() == false)
3283  {
3284  for (unsigned int vertex=0;
3285  vertex<GeometryInfo<dim>::vertices_per_cell; ++vertex)
3286  if (vertex_level[topological_vertex_numbering[cell->vertex_index(vertex)]] >=
3287  cell->level()+1)
3288  {
3289  // remove coarsen flag...
3290  cell->clear_coarsen_flag();
3291 
3292  // ...and if necessary also refine the current
3293  // cell, at the same time updating the level
3294  // information about vertices
3295  if (vertex_level[topological_vertex_numbering[cell->vertex_index(vertex)]] >
3296  cell->level()+1)
3297  {
3298  cell->set_refine_flag();
3299  continue_iterating = true;
3300 
3301  for (unsigned int v=0; v<GeometryInfo<dim>::vertices_per_cell;
3302  ++v)
3303  vertex_level[topological_vertex_numbering[cell->vertex_index(v)]]
3304  = std::max (vertex_level[topological_vertex_numbering[cell->vertex_index(v)]],
3305  cell->level()+1);
3306  }
3307 
3308  // continue and see whether we may, for example,
3309  // go into the inner 'if' above based on a
3310  // different vertex
3311  }
3312  }
3313 
3314  // clear coarsen flag if not all children were marked
3315  for (typename Triangulation<dim,spacedim>::cell_iterator cell = tria.begin();
3316  cell!=tria.end(); ++cell)
3317  {
3318  // nothing to do if we are already on the finest level
3319  if (cell->active())
3320  continue;
3321 
3322  const unsigned int n_children=cell->n_children();
3323  unsigned int flagged_children=0;
3324  for (unsigned int child=0; child<n_children; ++child)
3325  if (cell->child(child)->active() &&
3326  cell->child(child)->coarsen_flag_set())
3327  ++flagged_children;
3328 
3329  // if not all children were flagged for coarsening, remove
3330  // coarsen flags
3331  if (flagged_children < n_children)
3332  for (unsigned int child=0; child<n_children; ++child)
3333  if (cell->child(child)->active())
3334  cell->child(child)->clear_coarsen_flag();
3335  }
3336  }
3337  std::vector<bool> flags_after[2];
3338  tria.save_coarsen_flags (flags_after[0]);
3339  tria.save_refine_flags (flags_after[1]);
3340  return ((flags_before[0] != flags_after[0]) ||
3341  (flags_before[1] != flags_after[1]));
3342  }
3343  }
3344 
3345 
3346 
3347  template <int dim, int spacedim>
3348  bool
3350  {
3351  std::vector<bool> flags_before[2];
3352  this->save_coarsen_flags (flags_before[0]);
3353  this->save_refine_flags (flags_before[1]);
3354 
3355  bool mesh_changed = false;
3356  do
3357  {
3359 
3360  // enforce 2:1 mesh balance over periodic boundaries
3361  if (this->smooth_grid &
3363  mesh_changed = enforce_mesh_balance_over_periodic_boundaries(*this,
3365  }
3366  while (mesh_changed);
3367 
3368  // check if any of the refinement flags were changed during this
3369  // function and return that value
3370  std::vector<bool> flags_after[2];
3371  this->save_coarsen_flags (flags_after[0]);
3372  this->save_refine_flags (flags_after[1]);
3373  return ((flags_before[0] != flags_after[0]) ||
3374  (flags_before[1] != flags_after[1]));
3375  }
3376 
3377 
3378 
3379  template <int dim, int spacedim>
3380  void
3382  {
3383  // disable mesh smoothing for recreating the deal.II triangulation,
3384  // otherwise we might not be able to reproduce the p4est mesh
3385  // exactly. We restore the original smoothing at the end of this
3386  // function. Note that the smoothing flag is used in the normal
3387  // refinement process.
3389  save_smooth = this->smooth_grid;
3390 
3391  // We will refine manually to match the p4est further down, which
3392  // obeys a level difference of 2 at each vertex (see the balance call
3393  // to p4est). We can disable this here so we store fewer artificial
3394  // cells (in some cases). For geometric multigrid it turns out that
3395  // we will miss level cells at shared vertices if we ignore this.
3396  // See tests/mpi/mg_06.
3399  else
3401 
3402  bool mesh_changed = false;
3403 
3404  // remove all deal.II refinements. Note that we could skip this and
3405  // start from our current state, because the algorithm later coarsens as
3406  // necessary. This has the advantage of being faster when large parts
3407  // of the local partition changes (likely) and gives a deterministic
3408  // ordering of the cells (useful for snapshot/resume).
3409  // TODO: is there a more efficient way to do this?
3411  while (this->begin_active()->level() > 0)
3412  {
3414  cell = this->begin_active();
3415  cell != this->end();
3416  ++cell)
3417  {
3418  cell->set_coarsen_flag();
3419  }
3420 
3422  const bool saved_refinement_in_progress = refinement_in_progress;
3423  refinement_in_progress = true;
3424 
3425  try
3426  {
3428  }
3429  catch (const typename Triangulation<dim, spacedim>::DistortedCellList &)
3430  {
3431  // the underlying triangulation should not be checking for
3432  // distorted cells
3433  AssertThrow (false, ExcInternalError());
3434  }
3435 
3436  refinement_in_progress = saved_refinement_in_progress;
3437  }
3438 
3439 
3440  // query p4est for the ghost cells
3441  if (parallel_ghost != 0)
3442  {
3444  parallel_ghost = 0;
3445  }
3447  (dim == 2
3448  ?
3449  typename ::internal::p4est::types<dim>::
3450  balance_type(P4EST_CONNECT_CORNER)
3451  :
3452  typename ::internal::p4est::types<dim>::
3453  balance_type(P8EST_CONNECT_CORNER)));
3454 
3455  Assert (parallel_ghost, ExcInternalError());
3456 
3457 
3458  // set all cells to artificial. we will later set it to the correct
3459  // subdomain in match_tree_recursively
3461  cell = this->begin(0);
3462  cell != this->end(0);
3463  ++cell)
3464  cell->recursively_set_subdomain_id(numbers::artificial_subdomain_id);
3465 
3466  do
3467  {
3469  cell = this->begin(0);
3470  cell != this->end(0);
3471  ++cell)
3472  {
3473  // if this processor stores no part of the forest that comes out
3474  // of this coarse grid cell, then we need to delete all children
3475  // of this cell (the coarse grid cell remains)
3476  if (tree_exists_locally<dim,spacedim>(parallel_forest,
3477  coarse_cell_to_p4est_tree_permutation[cell->index()])
3478  == false)
3479  {
3480  delete_all_children<dim,spacedim> (cell);
3481  if (!cell->has_children())
3482  cell->set_subdomain_id (numbers::artificial_subdomain_id);
3483  }
3484 
3485  else
3486  {
3487  // this processor stores at least a part of the tree that
3488  // comes out of this cell.
3489 
3490  typename ::internal::p4est::types<dim>::quadrant p4est_coarse_cell;
3491  typename ::internal::p4est::types<dim>::tree *tree =
3492  init_tree(cell->index());
3493 
3494  ::internal::p4est::init_coarse_quadrant<dim>(p4est_coarse_cell);
3495 
3496  match_tree_recursively<dim,spacedim> (*tree, cell,
3497  p4est_coarse_cell,
3498  *parallel_forest,
3499  this->my_subdomain);
3500  }
3501  }
3502 
3503  // check mesh for ghostcells, refine as necessary. iterate over
3504  // every ghostquadrant, find corresponding deal coarsecell and
3505  // recurse.
3506  typename ::internal::p4est::types<dim>::quadrant *quadr;
3507  unsigned int ghost_owner=0;
3508  typename ::internal::p4est::types<dim>::topidx ghost_tree=0;
3509 
3510  for (unsigned int g_idx=0; g_idx<parallel_ghost->ghosts.elem_count; ++g_idx)
3511  {
3512  while (g_idx >= (unsigned int)parallel_ghost->proc_offsets[ghost_owner+1])
3513  ++ghost_owner;
3514  while (g_idx >= (unsigned int)parallel_ghost->tree_offsets[ghost_tree+1])
3515  ++ghost_tree;
3516 
3517  quadr = static_cast<typename ::internal::p4est::types<dim>::quadrant *>
3518  ( sc_array_index(&parallel_ghost->ghosts, g_idx) );
3519 
3520  unsigned int coarse_cell_index =
3521  p4est_tree_to_coarse_cell_permutation[ghost_tree];
3522 
3523  match_quadrant<dim,spacedim> (this, coarse_cell_index, *quadr, ghost_owner);
3524  }
3525 
3526  // fix all the flags to make sure we have a consistent mesh
3528 
3529  // see if any flags are still set
3530  mesh_changed = false;
3531  for (typename Triangulation<dim,spacedim>::active_cell_iterator
3532  cell = this->begin_active();
3533  cell != this->end();
3534  ++cell)
3535  if (cell->refine_flag_set() || cell->coarsen_flag_set())
3536  {
3537  mesh_changed = true;
3538  break;
3539  }
3540 
3541  // actually do the refinement but prevent the refinement hook below
3542  // from taking over
3543  const bool saved_refinement_in_progress = refinement_in_progress;
3544  refinement_in_progress = true;
3545 
3546  try
3547  {
3549  }
3550  catch (const typename Triangulation<dim,spacedim>::DistortedCellList &)
3551  {
3552  // the underlying triangulation should not be checking for
3553  // distorted cells
3554  AssertThrow (false, ExcInternalError());
3555  }
3556 
3557  refinement_in_progress = saved_refinement_in_progress;
3558  }
3559  while (mesh_changed);
3560 
3561 #ifdef DEBUG
3562  // check if correct number of ghosts is created
3563  unsigned int num_ghosts = 0;
3564 
3565  for (typename Triangulation<dim,spacedim>::active_cell_iterator
3566  cell = this->begin_active();
3567  cell != this->end();
3568  ++cell)
3569  {
3570  if (cell->subdomain_id() != this->my_subdomain
3571  &&
3572  cell->subdomain_id() != numbers::artificial_subdomain_id)
3573  ++num_ghosts;
3574  }
3575 
3576  Assert( num_ghosts == parallel_ghost->ghosts.elem_count, ExcInternalError());
3577 #endif
3578 
3579 
3580 
3581  // fill level_subdomain_ids for geometric multigrid
3582  // the level ownership of a cell is defined as the owner if the cell is active or as the owner of child(0)
3583  // we need this information for all our ancestors and the same-level neighbors of our own cells (=level ghosts)
3584  if (settings & construct_multigrid_hierarchy)
3585  {
3586  // step 1: We set our own ids all the way down and all the others to
3587  // -1. Note that we do not fill other cells we could figure out the
3588  // same way, because we might accidentally set an id for a cell that
3589  // is not a ghost cell.
3590  for (unsigned int lvl=this->n_levels(); lvl>0; )
3591  {
3592  --lvl;
3593  for (typename Triangulation<dim,spacedim>::cell_iterator cell = this->begin(lvl); cell!=this->end(lvl); ++cell)
3594  {
3595  if ((!cell->has_children() && cell->subdomain_id()==this->locally_owned_subdomain())
3596  || (cell->has_children() && cell->child(0)->level_subdomain_id()==this->locally_owned_subdomain()))
3597  cell->set_level_subdomain_id(this->locally_owned_subdomain());
3598  else
3599  {
3600  //not our cell
3601  cell->set_level_subdomain_id(numbers::artificial_subdomain_id);
3602  }
3603  }
3604  }
3605 
3606  //step 2: make sure all the neighbors to our level_cells exist. Need
3607  //to look up in p4est...
3608  std::vector<std::vector<bool> > marked_vertices(this->n_levels());
3609  for (unsigned int lvl=0; lvl < this->n_levels(); ++lvl)
3610  marked_vertices[lvl] = mark_locally_active_vertices_on_level(lvl);
3611 
3612  for (typename Triangulation<dim,spacedim>::cell_iterator cell = this->begin(0); cell!=this->end(0); ++cell)
3613  {
3614  typename ::internal::p4est::types<dim>::quadrant p4est_coarse_cell;
3615  const unsigned int tree_index
3616  = coarse_cell_to_p4est_tree_permutation[cell->index()];
3617  typename ::internal::p4est::types<dim>::tree *tree =
3618  init_tree(cell->index());
3619 
3620  ::internal::p4est::init_coarse_quadrant<dim>(p4est_coarse_cell);
3621 
3622  determine_level_subdomain_id_recursively<dim,spacedim> (*tree, tree_index, cell,
3623  p4est_coarse_cell,
3624  *parallel_forest,
3625  this->my_subdomain,
3626  marked_vertices);
3627  }
3628 
3629  //step 3: make sure we have the parent of our level cells
3630  for (unsigned int lvl=this->n_levels(); lvl>0;)
3631  {
3632  --lvl;
3633  for (typename Triangulation<dim,spacedim>::cell_iterator cell = this->begin(lvl); cell!=this->end(lvl); ++cell)
3634  {
3635  if (cell->has_children())
3636  for (unsigned int c=0; c<GeometryInfo<dim>::max_children_per_cell; ++c)
3637  {
3638  if (cell->child(c)->level_subdomain_id()==this->locally_owned_subdomain())
3639  {
3640  //at least one of the children belongs to us, so
3641  //make sure we set the level subdomain id
3643  mark = cell->child(0)->level_subdomain_id();
3644  Assert(mark != numbers::artificial_subdomain_id, ExcInternalError()); //we should know the child(0)
3645  cell->set_level_subdomain_id(mark);
3646  break;
3647  }
3648  }
3649  }
3650  }
3651 
3652  //step 4: Special case: on each level we need all the face neighbors
3653  // of our own level cells these are normally on the same level,
3654  // unless the neighbor is active and coarser. It can end up on a
3655  // different processor. Luckily, the level_subdomain_id can be
3656  // figured out without communication, because the cell is active
3657  // (and so level_subdomain_id=subdomain_id). Finally, also consider
3658  // the opposite case: if we are the coarser neighbor for another
3659  // processor, also mark them.
3660  for (typename Triangulation<dim,spacedim>::cell_iterator cell = this->begin(); cell!=this->end(); ++cell)
3661  {
3662  bool cell_level_mine = cell->level_subdomain_id() == this->locally_owned_subdomain();
3663 
3664  for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
3665  {
3666  if (cell->face(f)->at_boundary() || cell->neighbor(f)->level() >= cell->level())
3667  continue;
3668 
3669  bool neighbor_level_mine = cell->neighbor(f)->level_subdomain_id() == this->locally_owned_subdomain();
3670 
3671  if (cell_level_mine && !neighbor_level_mine)
3672  {
3673  // set the neighbor level_id up
3674  Assert(cell->neighbor(f)->active(), ExcInternalError());
3675  Assert(cell->neighbor(f)->subdomain_id() != numbers::artificial_subdomain_id, ExcInternalError());
3676  Assert(cell->neighbor(f)->level_subdomain_id() == numbers::artificial_subdomain_id
3677  || cell->neighbor(f)->level_subdomain_id() == cell->neighbor(f)->subdomain_id(), ExcInternalError());
3678  cell->neighbor(f)->set_level_subdomain_id(cell->neighbor(f)->subdomain_id());
3679  }
3680  else if (!cell_level_mine && neighbor_level_mine)
3681  {
3682  // set the current cell up because it is a neighbor for us
3683  Assert(cell->active(), ExcInternalError());
3684  Assert(cell->subdomain_id() != numbers::artificial_subdomain_id, ExcInternalError());
3685  Assert(cell->level_subdomain_id() == numbers::artificial_subdomain_id
3686  || cell->level_subdomain_id() == cell->subdomain_id(), ExcInternalError());
3687  cell->set_level_subdomain_id(cell->subdomain_id());
3688  }
3689  }
3690 
3691  }
3692 
3693  }
3694 
3695 
3696 
3697  // check that our local copy has exactly as many cells as the p4est
3698  // original (at least if we are on only one processor); for parallel
3699  // computations, we want to check that we have at least as many as p4est
3700  // stores locally (in the future we should check that we have exactly as
3701  // many non-artificial cells as parallel_forest->local_num_quadrants)
3702  {
3703  const unsigned int total_local_cells = this->n_active_cells();
3704  (void)total_local_cells;
3705 
3707  Assert (static_cast<unsigned int>(parallel_forest->local_num_quadrants) ==
3708  total_local_cells,
3709  ExcInternalError())
3710  else
3711  Assert (static_cast<unsigned int>(parallel_forest->local_num_quadrants) <=
3712  total_local_cells,
3713  ExcInternalError());
3714 
3715  // count the number of owned, active cells and compare with p4est.
3716  unsigned int n_owned = 0;
3717  for (typename Triangulation<dim,spacedim>::active_cell_iterator
3718  cell = this->begin_active();
3719  cell != this->end(); ++cell)
3720  {
3721  if (cell->subdomain_id() == this->my_subdomain)
3722  ++n_owned;
3723  }
3724 
3725  Assert(static_cast<unsigned int>(parallel_forest->local_num_quadrants) ==
3726  n_owned, ExcInternalError());
3727 
3728  }
3729 
3730  this->smooth_grid = save_smooth;
3731  }
3732 
3733 
3734 
3735  template <int dim, int spacedim>
3736  void
3738  {
3739  // first make sure that recursive calls are handled correctly
3740  if (refinement_in_progress == true)
3741  {
3743  return;
3744  }
3745 
3746  // do not allow anisotropic refinement
3747 #ifdef DEBUG
3748  for (typename Triangulation<dim,spacedim>::active_cell_iterator
3749  cell = this->begin_active();
3750  cell != this->end(); ++cell)
3751  if (cell->is_locally_owned() && cell->refine_flag_set())
3752  Assert (cell->refine_flag_set() ==
3754  ExcMessage ("This class does not support anisotropic refinement"));
3755 #endif
3756 
3757 
3758  // safety check: p4est has an upper limit on the level of a cell
3760  {
3761  for (typename Triangulation<dim,spacedim>::active_cell_iterator
3763  cell != this->end(::internal::p4est::functions<dim>::max_level-1); ++cell)
3764  {
3765  AssertThrow(!(cell->refine_flag_set()),
3766  ExcMessage("Fatal Error: maximum refinement level of p4est reached."));
3767  }
3768  }
3769 
3770  // now do the work we're supposed to do when we are in charge
3771  refinement_in_progress = true;
3773 
3774  // make sure all flags are cleared on cells we don't own, since nothing
3775  // good can come of that if they are still around
3776  for (typename Triangulation<dim,spacedim>::active_cell_iterator
3777  cell = this->begin_active();
3778  cell != this->end(); ++cell)
3779  if (cell->is_ghost() || cell->is_artificial())
3780  {
3781  cell->clear_refine_flag ();
3782  cell->clear_coarsen_flag ();
3783  }
3784 
3785 
3786  // count how many cells will be refined and coarsened, and allocate that
3787  // much memory
3788  RefineAndCoarsenList<dim,spacedim>
3789  refine_and_coarsen_list (*this,
3790  p4est_tree_to_coarse_cell_permutation,
3791  this->my_subdomain);
3792 
3793  // copy refine and coarsen flags into p4est and execute the refinement
3794  // and coarsening. this uses the refine_and_coarsen_list just built,
3795  // which is communicated to the callback functions through
3796  // p4est's user_pointer object
3797  Assert (parallel_forest->user_pointer == this,
3798  ExcInternalError());
3799  parallel_forest->user_pointer = &refine_and_coarsen_list;
3800 
3801  if (parallel_ghost != 0)
3802  {
3804  parallel_ghost = 0;
3805  }
3807  refine (parallel_forest, /* refine_recursive */ false,
3808  &RefineAndCoarsenList<dim,spacedim>::refine_callback,
3809  /*init_callback=*/NULL);
3811  coarsen (parallel_forest, /* coarsen_recursive */ false,
3812  &RefineAndCoarsenList<dim,spacedim>::coarsen_callback,
3813  /*init_callback=*/NULL);
3814 
3815  // make sure all cells in the lists have been consumed
3816  Assert (refine_and_coarsen_list.pointers_are_at_end(),
3817  ExcInternalError());
3818 
3819  // reset the pointer
3820  parallel_forest->user_pointer = this;
3821 
3822  // enforce 2:1 hanging node condition
3824  balance (parallel_forest,
3825  /* face and corner balance */
3826  (dim == 2
3827  ?
3828  typename ::internal::p4est::types<dim>::
3829  balance_type(P4EST_CONNECT_FULL)
3830  :
3831  typename ::internal::p4est::types<dim>::
3832  balance_type(P8EST_CONNECT_FULL)),
3833  /*init_callback=*/NULL);
3834 
3835  // before repartitioning the mesh let others attach mesh related info
3836  // (such as SolutionTransfer data) to the p4est
3837  attach_mesh_data();
3838 
3840  {
3841  // partition the new mesh between all processors. If cell weights have
3842  // not been given balance the number of cells.
3843  if (this->signals.cell_weight.num_slots() == 0)
3845  partition (parallel_forest,
3846  /* prepare coarsening */ 1,
3847  /* weight_callback */ NULL);
3848  else
3849  {
3850  // get cell weights for a weighted repartitioning.
3851  const std::vector<unsigned int> cell_weights = get_cell_weights();
3852 
3853  PartitionWeights<dim,spacedim> partition_weights (cell_weights);
3854 
3855  // attach (temporarily) a pointer to the cell weights through p4est's
3856  // user_pointer object
3857  Assert (parallel_forest->user_pointer == this,
3858  ExcInternalError());
3859  parallel_forest->user_pointer = &partition_weights;
3860 
3862  partition (parallel_forest,
3863  /* prepare coarsening */ 1,
3864  /* weight_callback */ &PartitionWeights<dim,spacedim>::cell_weight);
3865 
3866  // reset the user pointer to its previous state
3867  parallel_forest->user_pointer = this;
3868  }
3869  }
3870 
3871  // finally copy back from local part of tree to deal.II
3872  // triangulation. before doing so, make sure there are no refine or
3873  // coarsen flags pending
3874  for (typename Triangulation<dim,spacedim>::active_cell_iterator
3875  cell = this->begin_active();
3876  cell != this->end(); ++cell)
3877  {
3878  cell->clear_refine_flag();
3879  cell->clear_coarsen_flag();
3880  }
3881 
3882  try
3883  {
3885  }
3886  catch (const typename Triangulation<dim>::DistortedCellList &)
3887  {
3888  // the underlying triangulation should not be checking for distorted
3889  // cells
3890  AssertThrow (false, ExcInternalError());
3891  }
3892 
3893 
3894  refinement_in_progress = false;
3895 
3896  this->update_number_cache ();
3897  }
3898 
3899  template <int dim, int spacedim>
3900  void
3902  {
3903 
3904 #ifdef DEBUG
3905  for (typename Triangulation<dim,spacedim>::active_cell_iterator
3906  cell = this->begin_active();
3907  cell != this->end(); ++cell)
3908  if (cell->is_locally_owned())
3909  Assert (
3910  !cell->refine_flag_set() && !cell->coarsen_flag_set(),
3911  ExcMessage ("Error: There shouldn't be any cells flagged for coarsening/refinement when calling repartition()."));
3912 #endif
3913 
3914  refinement_in_progress = true;
3915 
3916  // before repartitioning the mesh let others attach mesh related info
3917  // (such as SolutionTransfer data) to the p4est
3918  attach_mesh_data();
3919 
3920  if (this->signals.cell_weight.num_slots() == 0)
3921  {
3922  // no cell weights given -- call p4est's 'partition' without a
3923  // callback for cell weights
3925  partition (parallel_forest,
3926  /* prepare coarsening */ 1,
3927  /* weight_callback */ NULL);
3928  }
3929  else
3930  {
3931  // get cell weights for a weighted repartitioning.
3932  const std::vector<unsigned int> cell_weights = get_cell_weights();
3933 
3934  PartitionWeights<dim,spacedim> partition_weights (cell_weights);
3935 
3936  // attach (temporarily) a pointer to the cell weights through p4est's
3937  // user_pointer object
3938  Assert (parallel_forest->user_pointer == this,
3939  ExcInternalError());
3940  parallel_forest->user_pointer = &partition_weights;
3941 
3943  partition (parallel_forest,
3944  /* prepare coarsening */ 1,
3945  /* weight_callback */ &PartitionWeights<dim,spacedim>::cell_weight);
3946 
3947  // reset the user pointer to its previous state
3948  parallel_forest->user_pointer = this;
3949  }
3950 
3951  try
3952  {
3954  }
3955  catch (const typename Triangulation<dim>::DistortedCellList &)
3956  {
3957  // the underlying triangulation should not be checking for distorted
3958  // cells
3959  AssertThrow (false, ExcInternalError());
3960  }
3961 
3962  refinement_in_progress = false;
3963 
3964  // update how many cells, edges, etc, we store locally
3965  this->update_number_cache ();
3966  }
3967 
3968 
3969 
3970  template <int dim, int spacedim>
3971  void
3973  communicate_locally_moved_vertices (const std::vector<bool> &vertex_locally_moved)
3974  {
3975  Assert (vertex_locally_moved.size() == this->n_vertices(),
3976  ExcDimensionMismatch(vertex_locally_moved.size(),
3977  this->n_vertices()));
3978 #ifdef DEBUG
3979  {
3980  const std::vector<bool> locally_owned_vertices
3982  for (unsigned int i=0; i<locally_owned_vertices.size(); ++i)
3983  Assert ((vertex_locally_moved[i] == false)
3984  ||
3985  (locally_owned_vertices[i] == true),
3986  ExcMessage ("The vertex_locally_moved argument must not "
3987  "contain vertices that are not locally owned"));
3988  }
3989 #endif
3990 
3991  std::map<unsigned int, std::set<::types::subdomain_id> >
3992  vertices_with_ghost_neighbors;
3993 
3994  // First find out which process should receive which vertices.
3995  // these are specifically the ones that sit on ghost cells and,
3996  // among these, the ones that we own locally
3997  for (typename Triangulation<dim,spacedim>::active_cell_iterator
3998  cell=this->begin_active(); cell!=this->end();
3999  ++cell)
4000  if (cell->is_ghost())
4001  for (unsigned int vertex_no=0;
4002  vertex_no<GeometryInfo<dim>::vertices_per_cell; ++vertex_no)
4003  {
4004  const unsigned int process_local_vertex_no = cell->vertex_index(vertex_no);
4005  vertices_with_ghost_neighbors[process_local_vertex_no].insert
4006  (cell->subdomain_id());
4007  }
4008 
4009  // now collect cells and their vertices
4010  // for the interested neighbors
4011  typedef
4012  std::map<::types::subdomain_id, CommunicateLocallyMovedVertices::CellInfo<dim,spacedim> > cellmap_t;
4013  cellmap_t needs_to_get_cells;
4014 
4016  cell = this->begin(0);
4017  cell != this->end(0);
4018  ++cell)
4019  {
4020  typename ::internal::p4est::types<dim>::quadrant p4est_coarse_cell;
4021  ::internal::p4est::init_coarse_quadrant<dim>(p4est_coarse_cell);
4022 
4023  CommunicateLocallyMovedVertices::fill_vertices_recursively<dim,spacedim>
4024  (*this,
4025  this->get_coarse_cell_to_p4est_tree_permutation()[cell->index()],
4026  cell,
4027  p4est_coarse_cell,
4028  vertices_with_ghost_neighbors,
4029  vertex_locally_moved,
4030  needs_to_get_cells);
4031  }
4032 
4033  // sending
4034  std::vector<std::vector<char> > sendbuffers (needs_to_get_cells.size());
4035  std::vector<std::vector<char> >::iterator buffer = sendbuffers.begin();
4036  std::vector<MPI_Request> requests (needs_to_get_cells.size());
4037  std::vector<unsigned int> destinations;
4038 
4039  unsigned int idx=0;
4040 
4041  for (typename cellmap_t::iterator it=needs_to_get_cells.begin();
4042  it!=needs_to_get_cells.end();
4043  ++it, ++buffer, ++idx)
4044  {
4045  const unsigned int num_cells = it->second.tree_index.size();
4046  (void)num_cells;
4047  destinations.push_back(it->first);
4048 
4049  Assert(num_cells==it->second.quadrants.size(), ExcInternalError());
4050  Assert(num_cells>0, ExcInternalError());
4051 
4052  // pack all the data into
4053  // the buffer for this
4054  // recipient and send
4055  // it. keep data around
4056  // till we can make sure
4057  // that the packet has been
4058  // received
4059  it->second.pack_data (*buffer);
4060  MPI_Isend(&(*buffer)[0], buffer->size(),
4061  MPI_BYTE, it->first,
4062  123, this->get_communicator(), &requests[idx]);
4063  }
4064 
4065  Assert(destinations.size()==needs_to_get_cells.size(), ExcInternalError());
4066 
4067  // collect the neighbors
4068  // that are going to send stuff to us
4069  const std::vector<unsigned int> senders
4071  (this->get_communicator(), destinations);
4072 
4073  // receive ghostcelldata
4074  std::vector<char> receive;
4075  CommunicateLocallyMovedVertices::CellInfo<dim,spacedim> cellinfo;
4076  for (unsigned int i=0; i<senders.size(); ++i)
4077  {
4078  MPI_Status status;
4079  int len;
4080  MPI_Probe(MPI_ANY_SOURCE, 123, this->get_communicator(), &status);
4081  MPI_Get_count(&status, MPI_BYTE, &len);
4082  receive.resize(len);
4083 
4084  char *ptr = &receive[0];
4085  MPI_Recv(ptr, len, MPI_BYTE, status.MPI_SOURCE, status.MPI_TAG,
4086  this->get_communicator(), &status);
4087 
4088  cellinfo.unpack_data(receive);
4089  const unsigned int cells = cellinfo.tree_index.size();
4090  for (unsigned int c=0; c<cells; ++c)
4091  {
4092  typename ::parallel::distributed::Triangulation<dim,spacedim>::cell_iterator
4093  cell (this,
4094  0,
4095  this->get_p4est_tree_to_coarse_cell_permutation()[cellinfo.tree_index[c]]);
4096 
4097  typename ::internal::p4est::types<dim>::quadrant p4est_coarse_cell;
4098  ::internal::p4est::init_coarse_quadrant<dim>(p4est_coarse_cell);
4099 
4100  CommunicateLocallyMovedVertices::set_vertices_recursively<dim,spacedim> (*this,
4101  p4est_coarse_cell,
4102  cell,
4103  cellinfo.quadrants[c],
4104  cellinfo.first_vertices[c],
4105  cellinfo.first_vertex_indices[c]);
4106  }
4107  }
4108 
4109  // complete all sends, so that we can
4110  // safely destroy the buffers.
4111  if (requests.size() > 0)
4112  MPI_Waitall(requests.size(), &requests[0], MPI_STATUSES_IGNORE);
4113 
4114  //check all msgs got sent and received
4115  Assert(Utilities::MPI::sum(needs_to_get_cells.size(), this->get_communicator())
4116  == Utilities::MPI::sum(senders.size(), this->get_communicator()),
4117  ExcInternalError());
4118  }
4119 
4120  template <int dim, int spacedim>
4121  unsigned int
4123  register_data_attach (const std::size_t size,
4124  const std_cxx11::function<void(const cell_iterator &,
4125  const CellStatus,
4126  void *)> &pack_callback)
4127  {
4128  Assert(size>0, ExcMessage("register_data_attach(), size==0"));
4130  ExcMessage("register_data_attach(), not all data has been unpacked last time?"));
4131 
4132  unsigned int offset = attached_data_size+sizeof(CellStatus);
4133  ++n_attached_datas;
4134  attached_data_size+=size;
4135  attached_data_pack_callbacks.push_back(
4136  std::pair<unsigned int, pack_callback_t> (offset, pack_callback)
4137  );
4138  return offset;
4139  }
4140 
4141 
4142 
4143  template <int dim, int spacedim>
4144  void
4146  notify_ready_to_unpack (const unsigned int offset,
4147  const std_cxx11::function<void (const cell_iterator &,
4148  const CellStatus,
4149  const void *)> &unpack_callback)
4150  {
4151  Assert (offset >= sizeof(CellStatus),
4152  ExcMessage ("invalid offset in notify_ready_to_unpack()"));
4153  Assert (offset < sizeof(CellStatus)+attached_data_size,
4154  ExcMessage ("invalid offset in notify_ready_to_unpack()"));
4155  Assert (n_attached_datas > 0, ExcMessage ("notify_ready_to_unpack() called too often"));
4156 
4157  // Recurse over p4est and hand the caller the data back
4159  cell = this->begin (0);
4160  cell != this->end (0);
4161  ++cell)
4162  {
4163  //skip coarse cells, that are not ours
4164  if (tree_exists_locally<dim, spacedim> (parallel_forest,
4165  coarse_cell_to_p4est_tree_permutation[cell->index() ])
4166  == false)
4167  continue;
4168 
4169  typename ::internal::p4est::types<dim>::quadrant p4est_coarse_cell;
4170  typename ::internal::p4est::types<dim>::tree *tree =
4171  init_tree (cell->index());
4172 
4173  ::internal::p4est::init_coarse_quadrant<dim> (p4est_coarse_cell);
4174 
4175  // parent_cell is not correct here, but is only used in a refined
4176  // cell
4177  post_mesh_data_recursively<dim, spacedim> (*tree,
4178  cell,
4179  cell,
4180  p4est_coarse_cell,
4181  offset,
4182  unpack_callback);
4183  }
4184 
4185  --n_attached_datas;
4186  if (n_attached_deserialize > 0)
4187  {
4189  attached_data_pack_callbacks.pop_front();
4190  }
4191 
4192  // important: only remove data if we are not in the deserialization
4193  // process. There, each SolutionTransfer registers and unpacks before
4194  // the next one does this, so n_attached_datas is only 1 here. This
4195  // would destroy the saved data before the second SolutionTransfer can
4196  // get it. This created a bug that is documented in
4197  // tests/mpi/p4est_save_03 with more than one SolutionTransfer.
4198  if (!n_attached_datas && n_attached_deserialize == 0)
4199  {
4200  // everybody got his data, time for cleanup!
4201  attached_data_size = 0;
4203 
4204  // and release the data
4205  void *userptr = parallel_forest->user_pointer;
4206  ::internal::p4est::functions<dim>::reset_data (parallel_forest, 0, NULL, NULL);
4207  parallel_forest->user_pointer = userptr;
4208  }
4209  }
4210 
4211 
4212  template <int dim, int spacedim>
4213  const std::vector<types::global_dof_index> &
4215  {
4216  return p4est_tree_to_coarse_cell_permutation;
4217  }
4218 
4219 
4220 
4221  template <int dim, int spacedim>
4222  const std::vector<types::global_dof_index> &
4224  {
4226  }
4227 
4228 
4229 
4230  namespace
4231  {
4236  template <int dim, int spacedim>
4237  struct FindGhosts
4238  {
4239  typename ::parallel::distributed::Triangulation<dim,spacedim> *triangulation;
4240  sc_array_t *subids;
4241  std::map<unsigned int, std::set<::types::subdomain_id> >
4242  *vertices_with_ghost_neighbors;
4243  };
4244 
4250  template <int dim, int spacedim>
4251  void
4252  find_ghosts_corner
4253  (typename ::internal::p4est::iter<dim>::corner_info *info,
4254  void *user_data)
4255  {
4256  int i, j;
4257  int nsides = info->sides.elem_count;
4258  typename ::internal::p4est::iter<dim>::corner_side *sides =
4259  (typename ::internal::p4est::iter<dim>::corner_side *)
4260  (info->sides.array);
4261  FindGhosts<dim,spacedim> *fg = static_cast<FindGhosts<dim,spacedim> *>(user_data);
4262  sc_array_t *subids = fg->subids;
4263  typename ::parallel::distributed::Triangulation<dim,spacedim> *triangulation = fg->triangulation;
4264  int nsubs;
4265  ::types::subdomain_id *subdomain_ids;
4266  std::map<unsigned int, std::set<::types::subdomain_id> >
4267  *vertices_with_ghost_neighbors = fg->vertices_with_ghost_neighbors;
4268 
4269  subids->elem_count = 0;
4270  for (i = 0; i < nsides; i++)
4271  {
4272  if (sides[i].is_ghost)
4273  {
4274  typename ::parallel::distributed::Triangulation<dim,spacedim>::cell_iterator cell = cell_from_quad (triangulation, sides[i].treeid, *(sides[i].quad));
4275  Assert (cell->is_ghost(), ExcMessage ("ghost quad did not find ghost cell"));
4276  ::types::subdomain_id *subid =
4277  static_cast<::types::subdomain_id *>(sc_array_push (subids));
4278  *subid = cell->subdomain_id();
4279  }
4280  }
4281 
4282  if (!subids->elem_count)
4283  {
4284  return;
4285  }
4286 
4287  nsubs = (int) subids->elem_count;
4288  subdomain_ids = (::types::subdomain_id *) (subids->array);
4289 
4290  for (i = 0; i < nsides; i++)
4291  {
4292  if (!sides[i].is_ghost)
4293  {
4294  typename ::parallel::distributed::Triangulation<dim,spacedim>::cell_iterator cell = cell_from_quad (triangulation, sides[i].treeid, *(sides[i].quad));
4295 
4296  Assert (!cell->is_ghost(), ExcMessage ("local quad found ghost cell"));
4297 
4298  for (j = 0; j < nsubs; j++)
4299  {
4300  (*vertices_with_ghost_neighbors)[cell->vertex_index(sides[i].corner)]
4301  .insert (subdomain_ids[j]);
4302  }
4303  }
4304  }
4305 
4306  subids->elem_count = 0;
4307  }
4308 
4312  template <int dim, int spacedim>
4313  void
4314  find_ghosts_edge
4315  (typename ::internal::p4est::iter<dim>::edge_info *info,
4316  void *user_data)
4317  {
4318  int i, j, k;
4319  int nsides = info->sides.elem_count;
4320  typename ::internal::p4est::iter<dim>::edge_side *sides =
4321  (typename ::internal::p4est::iter<dim>::edge_side *)
4322  (info->sides.array);
4323  FindGhosts<dim,spacedim> *fg = static_cast<FindGhosts<dim,spacedim> *>(user_data);
4324  sc_array_t *subids = fg->subids;
4325  typename ::parallel::distributed::Triangulation<dim,spacedim> *triangulation = fg->triangulation;
4326  int nsubs;
4327  ::types::subdomain_id *subdomain_ids;
4328  std::map<unsigned int, std::set<::types::subdomain_id> >
4329  *vertices_with_ghost_neighbors = fg->vertices_with_ghost_neighbors;
4330 
4331  subids->elem_count = 0;
4332  for (i = 0; i < nsides; i++)
4333  {
4334  if (sides[i].is_hanging)
4335  {
4336  for (j = 0; j < 2; j++)
4337  {
4338  if (sides[i].is.hanging.is_ghost[j])
4339  {
4340  typename ::parallel::distributed::Triangulation<dim,spacedim>::cell_iterator cell = cell_from_quad (triangulation, sides[i].treeid, *(sides[i].is.hanging.quad[j]));
4341  ::types::subdomain_id *subid =
4342  static_cast<::types::subdomain_id *>(sc_array_push (subids));
4343  *subid = cell->subdomain_id();
4344  }
4345  }
4346  }
4347  }
4348 
4349  if (!subids->elem_count)
4350  {
4351  return;
4352  }
4353 
4354  nsubs = (int) subids->elem_count;
4355  subdomain_ids = (::types::subdomain_id *) (subids->array);
4356 
4357  for (i = 0; i < nsides; i++)
4358  {
4359  if (sides[i].is_hanging)
4360  {
4361  for (j = 0; j < 2; j++)
4362  {
4363  if (!sides[i].is.hanging.is_ghost[j])
4364  {
4365  typename ::parallel::distributed::Triangulation<dim,spacedim>::cell_iterator cell = cell_from_quad (triangulation, sides[i].treeid, *(sides[i].is.hanging.quad[j]));
4366 
4367  for (k = 0; k < nsubs; k++)
4368  {
4369  (*vertices_with_ghost_neighbors)[cell->vertex_index(p8est_edge_corners[sides[i].edge][1^j])]
4370  .insert (subdomain_ids[k]);
4371  }
4372  }
4373  }
4374  }
4375  }
4376 
4377  subids->elem_count = 0;
4378  }
4379 
4383  template <int dim, int spacedim>
4384  void
4385  find_ghosts_face
4386  (typename ::internal::p4est::iter<dim>::face_info *info,
4387  void *user_data)
4388  {
4389  int i, j, k;
4390  int nsides = info->sides.elem_count;
4391  typename ::internal::p4est::iter<dim>::face_side *sides =
4392  (typename ::internal::p4est::iter<dim>::face_side *)
4393  (info->sides.array);
4394  FindGhosts<dim,spacedim> *fg = static_cast<FindGhosts<dim,spacedim> *>(user_data);
4395  sc_array_t *subids = fg->subids;
4396  typename ::parallel::distributed::Triangulation<dim,spacedim> *triangulation = fg->triangulation;
4397  int nsubs;
4398  ::types::subdomain_id *subdomain_ids;
4399  std::map<unsigned int, std::set<::types::subdomain_id> >
4400  *vertices_with_ghost_neighbors = fg->vertices_with_ghost_neighbors;
4401  int limit = (dim == 2) ? 2 : 4;
4402 
4403  subids->elem_count = 0;
4404  for (i = 0; i < nsides; i++)
4405  {
4406  if (sides[i].is_hanging)
4407  {
4408  for (j = 0; j < limit; j++)
4409  {
4410  if (sides[i].is.hanging.is_ghost[j])
4411  {
4412  typename ::parallel::distributed::Triangulation<dim,spacedim>::cell_iterator cell = cell_from_quad (triangulation, sides[i].treeid, *(sides[i].is.hanging.quad[j]));
4413  ::types::subdomain_id *subid =
4414  static_cast<::types::subdomain_id *>(sc_array_push (subids));
4415  *subid = cell->subdomain_id();
4416  }
4417  }
4418  }
4419  }
4420 
4421  if (!subids->elem_count)
4422  {
4423  return;
4424  }
4425 
4426  nsubs = (int) subids->elem_count;
4427  subdomain_ids = (::types::subdomain_id *) (subids->array);
4428 
4429  for (i = 0; i < nsides; i++)
4430  {
4431  if (sides[i].is_hanging)
4432  {
4433  for (j = 0; j < limit; j++)
4434  {
4435  if (!sides[i].is.hanging.is_ghost[j])
4436  {
4437  typename ::parallel::distributed::Triangulation<dim,spacedim>::cell_iterator cell = cell_from_quad (triangulation, sides[i].treeid, *(sides[i].is.hanging.quad[j]));
4438 
4439  for (k = 0; k < nsubs; k++)
4440  {
4441  if (dim == 2)
4442  {
4443  (*vertices_with_ghost_neighbors)[cell->vertex_index(p4est_face_corners[sides[i].face][(limit - 1)^j])]
4444  .insert (subdomain_ids[k]);
4445  }
4446  else
4447  {
4448  (*vertices_with_ghost_neighbors)[cell->vertex_index(p8est_face_corners[sides[i].face][(limit - 1)^j])]
4449  .insert (subdomain_ids[k]);
4450  }
4451  }
4452  }
4453  }
4454  }
4455  }
4456 
4457  subids->elem_count = 0;
4458  }
4459  }
4460 
4461 
4462 
4463  namespace
4464  {
4470  template <typename ITERATOR>
4471  void
4472  mark_periodic_vertices_recursively(const GridTools::PeriodicFacePair<ITERATOR> &periodic,
4473  const int target_level,
4474  std::vector<bool> &active_vertices_on_level)
4475  {
4476  if (periodic.cell[0]->level() > target_level)
4477  return;
4478 
4479  const unsigned int dim = ITERATOR::AccessorType::dimension;
4480  // for hanging nodes there is nothing to do since we are interested in
4481  // the connections on the same level...
4482  if (periodic.cell[0]->level() < target_level &&
4483  periodic.cell[0]->has_children() &&
4484  periodic.cell[1]->has_children())
4485  {
4486  // copy orientations etc. from parent to child
4487  GridTools::PeriodicFacePair<ITERATOR> periodic_child = periodic;
4488 
4489  // find appropriate pairs of child elements
4490  for (unsigned int cf=0; cf<periodic.cell[0]->face(periodic.face_idx[0])->n_children(); ++cf)
4491  {
4492  const unsigned int child_index_0 =
4493  GeometryInfo<dim>::child_cell_on_face(periodic.cell[0]->refinement_case(),
4494  periodic.face_idx[0], cf,
4495  periodic.orientation[0],
4496  periodic.orientation[1],
4497  periodic.orientation[2]);
4498  periodic_child.cell[0] = periodic.cell[0]->child(child_index_0);
4499  periodic_child.face_idx[0] = periodic.face_idx[0];
4500 
4501  // the second face is in standard orientation in terms of the
4502  // periodic face pair
4503  const unsigned int child_index_1 =
4504  GeometryInfo<dim>::child_cell_on_face(periodic.cell[1]->refinement_case(),
4505  periodic.face_idx[1], cf);
4506 
4507  periodic_child.cell[1] = periodic.cell[1]->child(child_index_1);
4508  periodic_child.face_idx[1] = periodic.face_idx[1];
4509 
4510  // recursive call into children
4511  mark_periodic_vertices_recursively (periodic_child, target_level,
4512  active_vertices_on_level);
4513  }
4514  return;
4515  }
4516 
4517  if (periodic.cell[0]->level() != target_level)
4518  return;
4519 
4520  for (unsigned int v=0; v<GeometryInfo<dim-1>::vertices_per_cell; ++v)
4521  {
4522  // take possible non-standard orientation of face on cell[0] into
4523  // account
4524  const unsigned int vface0 =
4526  periodic.orientation[1],
4527  periodic.orientation[2]);
4528  if (active_vertices_on_level[periodic.cell[0]->face(periodic.face_idx[0])->vertex_index(vface0)] ||
4529  active_vertices_on_level[periodic.cell[1]->face(periodic.face_idx[1])->vertex_index(v)])
4530  active_vertices_on_level[periodic.cell[0]->face(periodic.face_idx[0])->vertex_index(vface0)]
4531  = active_vertices_on_level[periodic.cell[1]->face(periodic.face_idx[1])->vertex_index(v)]
4532  = true;
4533  }
4534  }
4535 
4536 
4537 
4542  template <typename ITERATOR>
4543  void
4544  set_periodic_ghost_neighbors_recursively(const GridTools::PeriodicFacePair<ITERATOR> &periodic,
4545  const int target_level,
4546  std::map<unsigned int, std::set<::types::subdomain_id> > &vertices_with_ghost_neighbors)
4547  {
4548  if (periodic.cell[0]->level() > target_level)
4549  return;
4550 
4551  // for hanging nodes there is nothing to do since we are interested in
4552  // the connections on the same level...
4553  if (periodic.cell[0]->level() < target_level &&
4554  periodic.cell[0]->has_children() &&
4555  periodic.cell[1]->has_children())
4556  {
4557  // copy orientations etc. from parent to child
4558  GridTools::PeriodicFacePair<ITERATOR> periodic_child = periodic;
4559 
4560  // find appropriate pairs of child elements
4561  for (unsigned int cf=0; cf<periodic.cell[0]->face(periodic.face_idx[0])->n_children(); ++cf)
4562  {
4563  unsigned int c=0;
4564  for (; c<periodic.cell[0]->n_children(); ++c)
4565  {
4566  if (periodic.cell[0]->child(c)->face(periodic.face_idx[0]) ==
4567  periodic.cell[0]->face(periodic.face_idx[0])->child(cf))
4568  break;
4569  }
4570  Assert(c < periodic.cell[0]->n_children(),
4571  ExcMessage("Face child not found"));
4572  periodic_child.cell[0] = periodic.cell[0]->child(c);
4573  periodic_child.face_idx[0] = periodic.face_idx[0];
4574 
4575  c=0;
4576  for (; c<periodic.cell[1]->n_children(); ++c)
4577  {
4578  if (periodic.cell[1]->child(c)->face(periodic.face_idx[1]) ==
4579  periodic.cell[1]->face(periodic.face_idx[1])->child(cf))
4580  break;
4581  }
4582  Assert(c < periodic.cell[1]->n_children(),
4583  ExcMessage("Face child not found"));
4584  periodic_child.cell[1] = periodic.cell[1]->child(c);
4585  periodic_child.face_idx[1] = periodic.face_idx[1];
4586 
4587  // recursive call into children
4588  set_periodic_ghost_neighbors_recursively (periodic_child, target_level,
4589  vertices_with_ghost_neighbors);
4590  }
4591  return;
4592  }
4593 
4594  if (periodic.cell[0]->level() != target_level)
4595  return;
4596 
4597  // TODO: fix non-standard orientation
4598  Assert(periodic.orientation[0] == true &&
4599  periodic.orientation[1] == false &&
4600  periodic.orientation[2] == false,
4601  ExcNotImplemented());
4602 
4603  for (unsigned int v=0; v<GeometryInfo<ITERATOR::AccessorType::dimension-1>::vertices_per_cell; ++v)
4604  {
4605  const unsigned int
4606  idx0 = periodic.cell[0]->face(periodic.face_idx[0])->vertex_index(v),
4607  idx1 = periodic.cell[1]->face(periodic.face_idx[1])->vertex_index(v);
4608  if (vertices_with_ghost_neighbors.find(idx0) !=
4609  vertices_with_ghost_neighbors.end())
4610  vertices_with_ghost_neighbors[idx1].
4611  insert(vertices_with_ghost_neighbors[idx0].begin(),
4612  vertices_with_ghost_neighbors[idx0].end());
4613  if (vertices_with_ghost_neighbors.find(idx1) !=
4614  vertices_with_ghost_neighbors.end())
4615  vertices_with_ghost_neighbors[idx0].
4616  insert(vertices_with_ghost_neighbors[idx1].begin(),
4617  vertices_with_ghost_neighbors[idx1].end());
4618  }
4619  }
4620  }
4621 
4622 
4623 
4628  template <int dim, int spacedim>
4629  void
4632  (std::map<unsigned int, std::set<::types::subdomain_id> >
4633  &vertices_with_ghost_neighbors)
4634  {
4635  Assert (dim>1, ExcNotImplemented());
4636 
4637  FindGhosts<dim,spacedim> fg;
4638  fg.subids = sc_array_new (sizeof (::types::subdomain_id));
4639  fg.triangulation = this;
4640  fg.vertices_with_ghost_neighbors = &vertices_with_ghost_neighbors;
4641 
4642  // switch between functions. to make the compiler happy, we need to cast
4643  // the first two arguments to the type p[48]est_iterate wants to see. this
4644  // cast is the identity cast in each of the two branches, so it is safe.
4645  switch (dim)
4646  {
4647  case 2:
4648  p4est_iterate (reinterpret_cast<::internal::p4est::types<2>::forest *>(this->parallel_forest),
4649  reinterpret_cast<::internal::p4est::types<2>::ghost *>(this->parallel_ghost),
4650  static_cast<void *>(&fg),
4651  NULL, find_ghosts_face<2,spacedim>, find_ghosts_corner<2,spacedim>);
4652  break;
4653 
4654  case 3:
4655  p8est_iterate (reinterpret_cast<::internal::p4est::types<3>::forest *>(this->parallel_forest),
4656  reinterpret_cast<::internal::p4est::types<3>::ghost *>(this->parallel_ghost),
4657  static_cast<void *>(&fg),
4658  NULL, find_ghosts_face<3,spacedim>, find_ghosts_edge<3,spacedim>, find_ghosts_corner<3,spacedim>);
4659  break;
4660 
4661  default:
4662  Assert (false, ExcNotImplemented());
4663  }
4664 
4665  sc_array_destroy (fg.subids);
4666  }
4667 
4668 
4669 
4674  template <int dim, int spacedim>
4675  void
4678  (const unsigned int level,
4679  std::map<unsigned int, std::set<::types::subdomain_id> >
4680  &vertices_with_ghost_neighbors)
4681  {
4682  const std::vector<bool> locally_active_vertices =
4684  for (cell_iterator cell = this->begin(level); cell != this->end(level); ++cell)
4685  if (cell->level_subdomain_id() != ::numbers::artificial_subdomain_id
4686  && cell->level_subdomain_id() != this->locally_owned_subdomain())
4687  for (unsigned int v=0; v<GeometryInfo<dim>::vertices_per_cell; ++v)
4688  if (locally_active_vertices[cell->vertex_index(v)])
4689  vertices_with_ghost_neighbors[cell->vertex_index(v)]
4690  .insert (cell->level_subdomain_id());
4691 
4692  for (unsigned int i=0; i<periodic_face_pairs_level_0.size(); ++i)
4693  set_periodic_ghost_neighbors_recursively(periodic_face_pairs_level_0[i],
4694  level, vertices_with_ghost_neighbors);
4695  }
4696 
4697 
4698 
4699  template<int dim, int spacedim>
4700  std::vector<bool>
4702  ::mark_locally_active_vertices_on_level (const unsigned int level) const
4703  {
4704  Assert (dim>1, ExcNotImplemented());
4705 
4706  std::vector<bool> marked_vertices(this->n_vertices(), false);
4707  for (cell_iterator cell = this->begin(level); cell != this->end(level); ++cell)
4708  if (cell->level_subdomain_id() == this->locally_owned_subdomain())
4709  for (unsigned int v=0; v<GeometryInfo<dim>::vertices_per_cell; ++v)
4710  marked_vertices[cell->vertex_index(v)] = true;
4711 
4712  for (unsigned int i=0; i<periodic_face_pairs_level_0.size(); ++i)
4713  mark_periodic_vertices_recursively(periodic_face_pairs_level_0[i],
4714  level, marked_vertices);
4715 
4716  return marked_vertices;
4717  }
4718 
4719 
4720 
4721  template<int dim, int spacedim>
4722  void
4725  periodicity_vector)
4726  {
4727 #if DEAL_II_P4EST_VERSION_GTE(0,3,4,1)
4729  ExcMessage ("The triangulation is empty!"));
4730  Assert (this->n_levels() == 1,
4731  ExcMessage ("The triangulation is refined!"));
4732 
4733  typedef std::vector<GridTools::PeriodicFacePair<cell_iterator> >
4734  FaceVector;
4735  typename FaceVector::const_iterator it, periodic_end;
4736  it = periodicity_vector.begin();
4737  periodic_end = periodicity_vector.end();
4738 
4739  for (; it<periodic_end; ++it)
4740  {
4741  const cell_iterator first_cell = it->cell[0];
4742  const cell_iterator second_cell = it->cell[1];
4743  const unsigned int face_left = it->face_idx[0];
4744  const unsigned int face_right = it->face_idx[1];
4745 
4746  //respective cells of the matching faces in p4est
4747  const unsigned int tree_left
4748  = coarse_cell_to_p4est_tree_permutation[std::distance(this->begin(),
4749  first_cell)];
4750  const unsigned int tree_right
4751  = coarse_cell_to_p4est_tree_permutation[std::distance(this->begin(),
4752  second_cell)];
4753 
4754  // p4est wants to know which corner the first corner on
4755  // the face with the lower id is mapped to on the face with
4756  // with the higher id. For d==2 there are only two possibilities
4757  // that are determined by it->orientation[1].
4758  // For d==3 we have to use GridTools::OrientationLookupTable.
4759  // The result is given below.
4760 
4761  unsigned int p4est_orientation = 0;
4762  if (dim==2)
4763  p4est_orientation = it->orientation[1];
4764  else
4765  {
4766  const unsigned int face_idx_list[] = {face_left, face_right};
4767  const cell_iterator cell_list[] = {first_cell, second_cell};
4768  unsigned int lower_idx, higher_idx;
4769  if (face_left<=face_right)
4770  {
4771  higher_idx = 1;
4772  lower_idx = 0;
4773  }
4774  else
4775  {
4776  higher_idx = 0;
4777  lower_idx = 1;
4778  }
4779 
4780  // get the cell index of the first index on the face with the lower id
4781  unsigned int first_p4est_idx_on_cell = p8est_face_corners[face_idx_list[lower_idx]][0];
4782  unsigned int first_dealii_idx_on_face = numbers::invalid_unsigned_int;
4783  for (unsigned int i=0; i<GeometryInfo<dim>::vertices_per_face; ++i)
4784  {
4785  const unsigned int first_dealii_idx_on_cell
4787  (face_idx_list[lower_idx], i,
4788  cell_list[lower_idx]->face_orientation(face_idx_list[lower_idx]),
4789  cell_list[lower_idx]->face_flip(face_idx_list[lower_idx]),
4790  cell_list[lower_idx]->face_rotation(face_idx_list[lower_idx]));
4791  if (first_p4est_idx_on_cell == first_dealii_idx_on_cell)
4792  {
4793  first_dealii_idx_on_face = i;
4794  break;
4795  }
4796  }
4797  Assert( first_dealii_idx_on_face != numbers::invalid_unsigned_int, ExcInternalError());
4798  // Now map dealii_idx_on_face according to the orientation
4799  const unsigned int left_to_right [8][4] = {{0,2,1,3},{0,1,2,3},{3,1,2,0},{3,2,1,0},
4800  {2,3,0,1},{1,3,0,2},{1,0,3,2},{2,0,3,1}
4801  };
4802  const unsigned int right_to_left [8][4] = {{0,2,1,3},{0,1,2,3},{3,1,2,0},{3,2,1,0},
4803  {2,3,0,1},{2,0,3,1},{1,0,3,2},{1,3,0,2}
4804  };
4805  const unsigned int second_dealii_idx_on_face
4806  = lower_idx==0?left_to_right[it->orientation.to_ulong()][first_dealii_idx_on_face]:
4807  right_to_left[it->orientation.to_ulong()][first_dealii_idx_on_face];
4808  const unsigned int second_dealii_idx_on_cell
4810  (face_idx_list[higher_idx], second_dealii_idx_on_face,
4811  cell_list[higher_idx]->face_orientation(face_idx_list[higher_idx]),
4812  cell_list[higher_idx]->face_flip(face_idx_list[higher_idx]),
4813  cell_list[higher_idx]->face_rotation(face_idx_list[higher_idx]));
4814  //map back to p4est
4815  const unsigned int second_p4est_idx_on_face
4816  = p8est_corner_face_corners[second_dealii_idx_on_cell][face_idx_list[higher_idx]];
4817  p4est_orientation = second_p4est_idx_on_face;
4818  }
4819 
4821  connectivity_join_faces (connectivity,
4822  tree_left,
4823  tree_right,
4824  face_left,
4825  face_right,
4826  p4est_orientation);
4827  }
4828 
4829 
4831  (connectivity) == 1, ExcInternalError());
4832 
4833  // now create a forest out of the connectivity data structure
4835  parallel_forest
4838  connectivity,
4839  /* minimum initial number of quadrants per tree */ 0,
4840  /* minimum level of upfront refinement */ 0,
4841  /* use uniform upfront refinement */ 1,
4842  /* user_data_size = */ 0,
4843  /* user_data_constructor = */ NULL,
4844  /* user_pointer */ this);
4845 
4846 
4847  try
4848  {
4850  }
4851  catch (const typename Triangulation<dim>::DistortedCellList &)
4852  {
4853  // the underlying triangulation should not be checking for distorted
4854  // cells
4855  AssertThrow (false, ExcInternalError());
4856  }
4857 
4859  periodicity_vector.begin(),
4860  periodicity_vector.end());
4861 
4862 #else
4863  Assert(false, ExcMessage ("Need p4est version >= 0.3.4.1!"));
4864 #endif
4865  }
4866 
4867 
4868 
4869  template <int dim, int spacedim>
4870  std::size_t
4872  {
4873  std::size_t mem=
4877  + MemoryConsumption::memory_consumption(parallel_forest)
4881 // + MemoryConsumption::memory_consumption(attached_data_pack_callbacks) //TODO[TH]: how?
4882  + MemoryConsumption::memory_consumption(coarse_cell_to_p4est_tree_permutation)
4883  + MemoryConsumption::memory_consumption(p4est_tree_to_coarse_cell_permutation)
4885 
4886  return mem;
4887  }
4888 
4889 
4890 
4891  template <int dim, int spacedim>
4892  std::size_t
4894  {
4895  return ::internal::p4est::functions<dim>::forest_memory_used(parallel_forest)
4897  }
4898 
4899 
4900 
4901  template <int dim, int spacedim>
4902  void
4904  copy_triangulation (const ::Triangulation<dim, spacedim> &old_tria)
4905  {
4906  try
4907  {
4909  copy_triangulation (old_tria);
4910  }
4911  catch (const typename ::Triangulation<dim,spacedim>::DistortedCellList &)
4912  {
4913  // the underlying triangulation should not be checking for distorted
4914  // cells
4915  AssertThrow (false, ExcInternalError());
4916  }
4917 
4918  // note that now we have some content in the p4est objects and call the
4919  // functions that do the actual work (which are dimension dependent, so
4920  // separate)
4922 
4923  Assert (old_tria.n_levels() == 1,
4924  ExcMessage ("Parallel distributed triangulations can only be copied, "
4925  "if they are not refined!"));
4926 
4927  if (const ::parallel::distributed::Triangulation<dim,spacedim> *
4928  old_tria_x = dynamic_cast<const ::parallel::distributed::Triangulation<dim,spacedim> *>(&old_tria))
4929  {
4930  Assert (!old_tria_x->refinement_in_progress,
4931  ExcMessage ("Parallel distributed triangulations can only "
4932  "be copied, if no refinement is in progress!"));
4933 
4934  // duplicate MPI communicator, stored in the base class
4936 
4937  coarse_cell_to_p4est_tree_permutation = old_tria_x->coarse_cell_to_p4est_tree_permutation;
4938  p4est_tree_to_coarse_cell_permutation = old_tria_x->p4est_tree_to_coarse_cell_permutation;
4939  attached_data_size = old_tria_x->attached_data_size;
4940  n_attached_datas = old_tria_x->n_attached_datas;
4941 
4942  settings = old_tria_x->settings;
4943  }
4944  else
4945  {
4947  }
4948 
4950 
4951  try
4952  {
4954  }
4955  catch (const typename Triangulation<dim>::DistortedCellList &)
4956  {
4957  // the underlying triangulation should not be checking for distorted
4958  // cells
4959  AssertThrow (false, ExcInternalError());
4960  }
4961 
4962  this->update_number_cache ();
4963  }
4964 
4965 
4966  template <int dim, int spacedim>
4967  void
4970  {
4971  // determine size of memory in bytes to attach to each cell. This needs
4972  // to be constant because of p4est.
4973  if (attached_data_size==0)
4974  {
4975  Assert(n_attached_datas==0, ExcInternalError());
4976 
4977  //nothing to do
4978  return;
4979  }
4980 
4981  // realloc user_data in p4est
4982  void *userptr = parallel_forest->user_pointer;
4985  NULL, NULL);
4986  parallel_forest->user_pointer = userptr;
4987 
4988 
4989  // Recurse over p4est and Triangulation
4990  // to find refined/coarsened/kept
4991  // cells. Then query and attach the data.
4993  cell = this->begin(0);
4994  cell != this->end(0);
4995  ++cell)
4996  {
4997  //skip coarse cells, that are not ours
4998  if (tree_exists_locally<dim,spacedim>(parallel_forest,
4999  coarse_cell_to_p4est_tree_permutation[cell->index()])
5000  == false)
5001  continue;
5002 
5003  typename ::internal::p4est::types<dim>::quadrant p4est_coarse_cell;
5004  typename ::internal::p4est::types<dim>::tree *tree =
5005  init_tree(cell->index());
5006 
5007  ::internal::p4est::init_coarse_quadrant<dim>(p4est_coarse_cell);
5008 
5009  attach_mesh_data_recursively<dim,spacedim>(*tree,
5010  cell,
5011  p4est_coarse_cell,
5013  }
5014  }
5015 
5016  template <int dim, int spacedim>
5017  std::vector<unsigned int>
5020  {
5021  // Allocate the space for the weights. In fact we do not know yet, how
5022  // many cells we own after the refinement (only p4est knows that
5023  // at this point). We simply reserve n_active_cells space and if many
5024  // more cells are refined than coarsened than additional reallocation
5025  // will be done inside get_cell_weights_recursively.
5026  std::vector<unsigned int> weights;
5027  weights.reserve(this->n_active_cells());
5028 
5029  // Recurse over p4est and Triangulation
5030  // to find refined/coarsened/kept
5031  // cells. Then append cell_weight.
5032  // Note that we need to follow the p4est ordering
5033  // instead of the deal.II ordering to get the cell_weights
5034  // in the same order p4est will encounter them during repartitioning.
5035  for (unsigned int c=0; c<this->n_cells(0); ++c)
5036  {
5037  // skip coarse cells, that are not ours
5038  if (tree_exists_locally<dim,spacedim>(parallel_forest,c) == false)
5039  continue;
5040 
5041  const unsigned int coarse_cell_index =
5042  p4est_tree_to_coarse_cell_permutation[c];
5043 
5045  dealii_coarse_cell (this, 0, coarse_cell_index);
5046 
5047  typename ::internal::p4est::types<dim>::quadrant p4est_coarse_cell;
5049  quadrant_set_morton (&p4est_coarse_cell,
5050  /*level=*/0,
5051  /*index=*/0);
5052  p4est_coarse_cell.p.which_tree = c;
5053 
5054  const typename ::internal::p4est::types<dim>::tree *tree =
5055  init_tree(coarse_cell_index);
5056 
5057  get_cell_weights_recursively<dim,spacedim>(*tree,
5058  dealii_coarse_cell,
5059  p4est_coarse_cell,
5060  this->signals,
5061  weights);
5062  }
5063 
5064  return weights;
5065  }
5066 
5067  template <int dim, int spacedim>
5068  typename ::Triangulation<dim,spacedim>::cell_iterator
5069  cell_from_quad
5070  (typename ::parallel::distributed::Triangulation<dim,spacedim> *triangulation,
5071  typename ::internal::p4est::types<dim>::topidx treeidx,
5072  typename ::internal::p4est::types<dim>::quadrant &quad)
5073  {
5074  int i, l = quad.level;
5075  int child_id;
5076  types::global_dof_index dealii_index =
5077  triangulation->get_p4est_tree_to_coarse_cell_permutation()[treeidx];
5078 
5079  for (i = 0; i < l; i++)
5080  {
5081  typename ::Triangulation<dim,spacedim>::cell_iterator cell (triangulation, i, dealii_index);
5083  Assert (cell->has_children (), ExcMessage ("p4est quadrant does not correspond to a cell!"));
5084  dealii_index = cell->child_index(child_id);
5085  }
5086 
5087  typename ::Triangulation<dim,spacedim>::cell_iterator out_cell (triangulation, l, dealii_index);
5088 
5089  return out_cell;
5090  }
5091 
5092 
5093 
5094  template <int spacedim>
5096  :
5097  ::parallel::Triangulation<1,spacedim>(MPI_COMM_WORLD,
5098  typename ::Triangulation<1,spacedim>::MeshSmoothing(),
5099  false)
5100  {
5101  Assert (false, ExcNotImplemented());
5102  }
5103 
5104 
5105  template <int spacedim>
5107  {
5108  Assert (false, ExcNotImplemented());
5109  }
5110 
5111 
5112 
5113  template <int spacedim>
5114  void
5116  (const std::vector<bool> &/*vertex_locally_moved*/)
5117  {
5118  Assert (false, ExcNotImplemented());
5119  }
5120 
5121 
5122  template <int spacedim>
5123  const std::vector<types::global_dof_index> &
5125  {
5126  static std::vector<types::global_dof_index> a;
5127  return a;
5128  }
5129 
5130  template <int spacedim>
5131  void
5134  (std::map<unsigned int, std::set<::types::subdomain_id> >
5135  &/*vertices_with_ghost_neighbors*/)
5136  {
5137  Assert (false, ExcNotImplemented());
5138  }
5139 
5140  template <int spacedim>
5141  void
5144  (const unsigned int /*level*/,
5145  std::map<unsigned int, std::set<::types::subdomain_id> >
5146  &/*vertices_with_ghost_neighbors*/)
5147  {
5148  Assert (false, ExcNotImplemented());
5149  }
5150 
5151  template <int spacedim>
5152  std::vector<bool>
5154  mark_locally_active_vertices_on_level (const unsigned int) const
5155  {
5156  Assert (false, ExcNotImplemented());
5157  return std::vector<bool>();
5158  }
5159  }
5160 }
5161 
5162 
5163 #else // DEAL_II_WITH_P4EST
5164 
5165 namespace parallel
5166 {
5167  namespace distributed
5168  {
5169  template <int dim, int spacedim>
5171  :
5173  {
5174  Assert (false, ExcNotImplemented());
5175  }
5176  }
5177 }
5178 
5179 #endif // DEAL_II_WITH_P4EST
5180 
5181 
5182 
5183 
5184 /*-------------- Explicit Instantiations -------------------------------*/
5185 #include "tria.inst"
5186 
5187 
5188 DEAL_II_NAMESPACE_CLOSE
virtual unsigned int n_global_levels() const
Definition: tria_base.cc:114
void write_mesh_vtk(const char *file_basename) const
Definition: tria.cc:2640
virtual void copy_triangulation(const ::Triangulation< dim, spacedim > &old_tria)
Definition: tria.cc:4904
std::vector< unsigned int > get_cell_weights()
Definition: tria.cc:5019
static unsigned int standard_to_real_face_vertex(const unsigned int vertex, const bool face_orientation=true, const bool face_flip=false, const bool face_rotation=false)
static const unsigned int invalid_unsigned_int
Definition: types.h:164
void fill_vertices_with_ghost_neighbors(std::map< unsigned int, std::set<::types::subdomain_id > > &vertices_with_ghost_neighbors)
Definition: tria.cc:4632
const std::vector< Point< spacedim > > & get_vertices() const
cell_iterator end() const
Definition: tria.cc:10465
types::subdomain_id my_subdomain
Definition: tria_base.h:168
unsigned int register_data_attach(const std::size_t size, const std_cxx11::function< void(const cell_iterator &, const CellStatus, void *)> &pack_callback)
Definition: tria.cc:4123
unsigned int n_active_lines() const
Definition: tria.cc:11188
virtual void copy_triangulation(const ::Triangulation< dim, spacedim > &old_tria)
Definition: tria_base.cc:60
static unsigned int face_to_cell_vertices(const unsigned int face, const unsigned int vertex, const bool face_orientation=true, const bool face_flip=false, const bool face_rotation=false)
std::vector< Point< spacedim > > vertices
Definition: tria.h:3296
::ExceptionBase & ExcMessage(std::string arg1)
boost::signals2::signal< unsigned int(const cell_iterator &, const CellStatus), CellWeightSum< unsigned int > > cell_weight
Definition: tria.h:2072
virtual void copy_triangulation(const Triangulation< dim, spacedim > &old_tria)
Definition: tria.cc:9043
void notify_ready_to_unpack(const unsigned int offset, const std_cxx11::function< void(const cell_iterator &, const CellStatus, const void *)> &unpack_callback)
Definition: tria.cc:4146
const std::vector< bool > & get_used_vertices() const
Definition: tria.cc:11511
virtual std::size_t memory_consumption_p4est() const
Definition: tria.cc:4893
void copy_new_triangulation_to_p4est(::internal::int2type< 2 >)
unsigned int get_checksum() const
Definition: tria.cc:2783
#define AssertThrow(cond, exc)
Definition: exceptions.h:358
void fill_level_vertices_with_ghost_neighbors(const unsigned int level, std::map< unsigned int, std::set<::types::subdomain_id > > &vertices_with_ghost_neighbors)
Definition: tria.cc:4678
virtual bool has_hanging_nodes() const
Definition: tria.cc:2594
void load(const char *filename, const bool autopartition=true)
Definition: tria.cc:2700
Triangulation(MPI_Comm mpi_communicator, const typename::Triangulation< dim, spacedim >::MeshSmoothing smooth_grid=(::Triangulation< dim, spacedim >::none), const Settings settings=default_setting)
Definition: tria.cc:2164
callback_list_t attached_data_pack_callbacks
Definition: tria.h:863
cell_iterator begin(const unsigned int level=0) const
Definition: tria.cc:10377
unsigned int n_active_cells() const
Definition: tria.cc:10973
std::bitset< 3 > orientation
Definition: grid_tools.h:1175
void add_periodicity(const std::vector< GridTools::PeriodicFacePair< cell_iterator > > &)
Definition: tria.cc:4724
typename::internal::p4est::types< dim >::ghost * parallel_ghost
Definition: tria.h:818
unsigned int n_levels() const
typename::internal::p4est::types< dim >::forest * parallel_forest
Definition: tria.h:813
virtual void create_triangulation(const std::vector< Point< spacedim > > &vertices, const std::vector< CellData< dim > > &cells, const SubCellData &subcelldata)
Definition: tria.cc:2210
virtual bool prepare_coarsening_and_refinement()
Definition: tria.cc:3349
void get_vertex_connectivity_of_cells(const Triangulation< dim, spacedim > &triangulation, DynamicSparsityPattern &connectivity)
Definition: grid_tools.cc:1933
virtual void execute_coarsening_and_refinement()
Definition: tria.cc:11602
virtual MPI_Comm get_communicator() const
Definition: tria_base.cc:135
void save_coarsen_flags(std::ostream &out) const
Definition: tria.cc:9435
virtual bool prepare_coarsening_and_refinement()
Definition: tria.cc:12269
Triangulation(const MeshSmoothing smooth_grid=none, const bool check_for_distorted_cells=false)
Definition: tria.cc:8751
unsigned int n_cells() const
Definition: tria.cc:10966
T sum(const T &t, const MPI_Comm &mpi_communicator)
Definition: mpi.h:607
unsigned int global_dof_index
Definition: types.h:88
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)
const std::vector< types::global_dof_index > & get_p4est_tree_to_coarse_cell_permutation() const
Definition: tria.cc:4214
#define Assert(cond, exc)
Definition: exceptions.h:294
virtual void update_number_cache()
Definition: tria_base.cc:143
Signals signals
Definition: tria.h:2078
std::vector< bool > mark_locally_active_vertices_on_level(const unsigned int level) const
Definition: tria.cc:4702
virtual std::size_t memory_consumption() const
Definition: tria_base.cc:76
virtual void create_triangulation(const std::vector< Point< spacedim > > &vertices, const std::vector< CellData< dim > > &cells, const SubCellData &subcelldata)
Definition: tria.cc:9130
virtual std::size_t memory_consumption() const
Definition: tria.cc:4871
unsigned int face_idx[2]
Definition: grid_tools.h:1168
types::subdomain_id locally_owned_subdomain() const
Definition: tria_base.cc:218
void save_refine_flags(std::ostream &out) const
Definition: tria.cc:9370
virtual void execute_coarsening_and_refinement()
Definition: tria.cc:3737
std::vector< unsigned int > invert_permutation(const std::vector< unsigned int > &permutation)
Definition: utilities.cc:483
unsigned int subdomain_id
Definition: types.h:42
active_cell_iterator last_active() const
Definition: tria.cc:10444
unsigned int n_mpi_processes(const MPI_Comm &mpi_communicator)
Definition: mpi.cc:95
::Triangulation< dim, spacedim >::active_cell_iterator active_cell_iterator
Definition: tria.h:368
void reorder_hierarchical(const DynamicSparsityPattern &sparsity, std::vector< DynamicSparsityPattern::size_type > &new_indices)
const types::subdomain_id artificial_subdomain_id
Definition: types.h:255
std_cxx11::enable_if< std_cxx11::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)
void communicate_locally_moved_vertices(const std::vector< bool > &vertex_locally_moved)
Definition: tria.cc:3973
MeshSmoothing smooth_grid
Definition: tria.h:3053
void setup_coarse_cell_to_p4est_tree_permutation()
Definition: tria.cc:2623
::Triangulation< dim, spacedim >::cell_iterator cell_iterator
Definition: tria.h:348
std::vector< bool > get_locally_owned_vertices(const Triangulation< dim, spacedim > &triangulation)
Definition: grid_tools.cc:2075
std::vector< GridTools::PeriodicFacePair< cell_iterator > > periodic_face_pairs_level_0
Definition: tria.h:887
void save(const char *filename) const
Definition: tria.cc:2652
unsigned int n_attached_deserialize
Definition: tria.h:849
active_cell_iterator begin_active(const unsigned int level=0) const
Definition: tria.cc:10397
virtual void update_number_cache()
Definition: tria.cc:2793
std::vector< unsigned int > compute_point_to_point_communication_pattern(const MPI_Comm &mpi_comm, const std::vector< unsigned int > &destinations)
Definition: mpi.cc:122
unsigned int n_vertices() const
typename::internal::p4est::types< dim >::tree * init_tree(const int dealii_coarse_cell_index) const
Definition: tria.cc:2819
const std::vector< types::global_dof_index > & get_coarse_cell_to_p4est_tree_permutation() const
Definition: tria.cc:4223
T max(const T &t, const MPI_Comm &mpi_communicator)
Definition: mpi.h:689
virtual void clear()
Definition: tria.cc:8822
std::vector< types::global_dof_index > coarse_cell_to_p4est_tree_permutation
Definition: tria.h:879