Debian Codegraph @

include/deal.II/distributed/p4est_wrappers.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2016 - 2025 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_p4est_wrappers_h
16 #define dealii_p4est_wrappers_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/geometry_info.h>
21
22 #ifdef DEAL_II_WITH_P4EST
23
24 # include <deal.II/base/mpi_stub.h>
25
26 # include <p4est_bits.h>
27 # include <p4est_communication.h>
28 # include <p4est_extended.h>
29 # include <p4est_ghost.h>
30 # include <p4est_iterate.h>
31 # include <p4est_search.h>
32 # include <p4est_vtk.h>
33 # include <p8est_bits.h>
34 # include <p8est_communication.h>
35 # include <p8est_extended.h>
36 # include <p8est_ghost.h>
37 # include <p8est_iterate.h>
38 # include <p8est_search.h>
39 # include <p8est_vtk.h>
40
41 # include <limits>
42
43 DEAL_II_NAMESPACE_OPEN
44
45 // Forward declaration
46 # ifndef DOXYGEN
47 namespace parallel
48 {
49 namespace distributed
50 {
51 template <int dim, int spacedim>
52 DEAL_II_CXX20_REQUIRES((concepts::is_valid_dim_spacedim<dim, spacedim>))
53 class Triangulation;
54 }
55 } // namespace parallel
56 # endif
57
58 namespace internal
59 {
60 namespace p4est
61 {
62 /**
63 * A structure whose explicit specializations contain alias to the
64 * relevant p4est_* and p8est_* types. Using this structure, for example
65 * by saying <tt>types<dim>::connectivity</tt> we can write code in a
66 * dimension independent way, either referring to p4est_connectivity_t or
67 * p8est_connectivity_t, depending on template argument.
68 */
69 template <int>
70 struct types;
71
72 // these struct mimics p4est for 1d
73 template <>
74 struct types<1>
75 {
76 // id of a quadrant is an integeger
77 using quadrant = int;
78
79 // maximum number of children
80 static const int max_n_child_indices_bits = 27;
81
82 // number of bits the data type of id has
83 static const int n_bits = std::numeric_limits<quadrant>::digits;
84 };
85
86 template <>
87 struct types<2>
88 {
89 using connectivity = p4est_connectivity_t;
90 using forest = p4est_t;
91 using tree = p4est_tree_t;
92 using quadrant = p4est_quadrant_t;
93 using quadrant_coord = p4est_qcoord_t;
94 using topidx = p4est_topidx_t;
95 using locidx = p4est_locidx_t;
96 using gloidx = p4est_gloidx_t;
97 using balance_type = p4est_connect_type_t;
98 using ghost = p4est_ghost_t;
99 using transfer_context = p4est_transfer_context_t;
100 using search_partition_callback = p4est_search_partition_t;
101 };
102
103 template <>
104 struct types<3>
105 {
106 using connectivity = p8est_connectivity_t;
107 using forest = p8est_t;
108 using tree = p8est_tree_t;
109 using quadrant = p8est_quadrant_t;
110 using quadrant_coord = p4est_qcoord_t;
111 using topidx = p4est_topidx_t;
112 using locidx = p4est_locidx_t;
113 using gloidx = p4est_gloidx_t;
114 using balance_type = p8est_connect_type_t;
115 using ghost = p8est_ghost_t;
116 using transfer_context = p8est_transfer_context_t;
117 using search_partition_callback = p8est_search_partition_t;
118 };
119
120
121
122 /**
123 * A structure whose explicit specializations represent the
124 * relevant p4est_* and p8est_* functions. Using this structure, for
125 * example by saying functions<dim>::quadrant_compare(...), we can write
126 * code in a dimension independent way, either calling
127 * p4est_quadrant_compare or p8est_quadrant_compare, depending on template
128 * argument.
129 *
130 * In most cases, the members of this class are simply pointers to
131 * p4est_* or p8est_* functions. In one case, it's simply a static
132 * member function that dispatches to things p4est chooses to
133 * implement via a macro.
134 */
135 template <int dim>
136 struct functions;
137
138 template <>
139 struct functions<2>
140 {
141 static int (&quadrant_compare)(const void *v1, const void *v2);
142
143 static void (&quadrant_childrenv)(const types<2>::quadrant *q,
144 types<2>::quadrant c[]);
145
146 static int (&quadrant_overlaps_tree)(types<2>::tree *tree,
147 const types<2>::quadrant *q);
148
149 static void (&quadrant_set_morton)(types<2>::quadrant *quadrant,
150 int level,
151 std::uint64_t id);
152
153 static void
154 quadrant_init(types<2>::quadrant &q);
155
156 static int (&quadrant_is_equal)(const types<2>::quadrant *q1,
157 const types<2>::quadrant *q2);
158
159 static int (&quadrant_is_sibling)(const types<2>::quadrant *q1,
160 const types<2>::quadrant *q2);
161
162 static int (&quadrant_is_ancestor)(const types<2>::quadrant *q1,
163 const types<2>::quadrant *q2);
164
165 static int (&quadrant_ancestor_id)(const types<2>::quadrant *q,
166 int level);
167
168 static int (&comm_find_owner)(types<2>::forest *p4est,
169 const types<2>::locidx which_tree,
170 const types<2>::quadrant *q,
171 const int guess);
172
173 static types<2>::connectivity *(&connectivity_new)(
174 types<2>::topidx num_vertices,
175 types<2>::topidx num_trees,
176 types<2>::topidx num_corners,
177 types<2>::topidx num_vtt);
178
179 static types<2>::connectivity *(&connectivity_new_copy)(
180 types<2>::topidx num_vertices,
181 types<2>::topidx num_trees,
182 types<2>::topidx num_corners,
183 const double *vertices,
184 const types<2>::topidx *ttv,
185 const types<2>::topidx *ttt,
186 const int8_t *ttf,
187 const types<2>::topidx *ttc,
188 const types<2>::topidx *coff,
189 const types<2>::topidx *ctt,
190 const int8_t *ctc);
191
192 static void (&connectivity_join_faces)(types<2>::connectivity *conn,
193 types<2>::topidx tree_left,
194 types<2>::topidx tree_right,
195 int face_left,
196 int face_right,
197 int orientation);
198
199
200
201 static void (&connectivity_destroy)(p4est_connectivity_t *connectivity);
202
203 static types<2>::forest *(&new_forest)(
204 MPI_Comm mpicomm,
205 types<2>::connectivity *connectivity,
206 types<2>::locidx min_quadrants,
207 int min_level,
208 int fill_uniform,
209 std::size_t data_size,
210 p4est_init_t init_fn,
211 void *user_pointer);
212
213 static types<2>::forest *(&copy_forest)(types<2>::forest *input,
214 int copy_data);
215
216 static void (&destroy)(types<2>::forest *p4est);
217
218 static void (&refine)(types<2>::forest *p4est,
219 int refine_recursive,
220 p4est_refine_t refine_fn,
221 p4est_init_t init_fn);
222
223 static void (&coarsen)(types<2>::forest *p4est,
224 int coarsen_recursive,
225 p4est_coarsen_t coarsen_fn,
226 p4est_init_t init_fn);
227
228 static void (&balance)(types<2>::forest *p4est,
229 types<2>::balance_type btype,
230 p4est_init_t init_fn);
231
232 static types<2>::gloidx (&partition)(types<2>::forest *p4est,
233 int partition_for_coarsening,
234 p4est_weight_t weight_fn);
235
236 static void (&save)(const char *filename,
237 types<2>::forest *p4est,
238 int save_data);
239
240 static types<2>::forest *(&load_ext)(const char *filename,
241 MPI_Comm mpicomm,
242 std::size_t data_size,
243 int load_data,
244 int autopartition,
245 int broadcasthead,
246 void *user_pointer,
247 types<2>::connectivity **p4est);
248
249 static int (&connectivity_save)(const char *filename,
250 types<2>::connectivity *connectivity);
251
252 static int (&connectivity_is_valid)(types<2>::connectivity *connectivity);
253
254 static types<2>::connectivity *(&connectivity_load)(const char *filename,
255 std::size_t *length);
256
257 static unsigned int (&checksum)(types<2>::forest *p4est);
258
259 static void (&vtk_write_file)(types<2>::forest *p4est,
260 p4est_geometry_t *,
261 const char *baseName);
262
263 static types<2>::ghost *(&ghost_new)(types<2>::forest *p4est,
264 types<2>::balance_type btype);
265
266 static void (&ghost_destroy)(types<2>::ghost *ghost);
267
268 static void (&reset_data)(types<2>::forest *p4est,
269 std::size_t data_size,
270 p4est_init_t init_fn,
271 void *user_pointer);
272
273 static std::size_t (&forest_memory_used)(types<2>::forest *p4est);
274
275 static std::size_t (&connectivity_memory_used)(
276 types<2>::connectivity *p4est);
277
278 template <int spacedim>
279 static void
280 iterate(dealii::internal::p4est::types<2>::forest *parallel_forest,
281 dealii::internal::p4est::types<2>::ghost *parallel_ghost,
282 void *user_data);
283
284 static constexpr unsigned int max_level = P4EST_MAXLEVEL;
285
286 static void (&transfer_fixed)(const types<2>::gloidx *dest_gfq,
287 const types<2>::gloidx *src_gfq,
288 MPI_Comm mpicomm,
289 int tag,
290 void *dest_data,
291 const void *src_data,
292 std::size_t data_size);
293
294 static types<2>::transfer_context *(&transfer_fixed_begin)(
295 const types<2>::gloidx *dest_gfq,
296 const types<2>::gloidx *src_gfq,
297 MPI_Comm mpicomm,
298 int tag,
299 void *dest_data,
300 const void *src_data,
301 std::size_t data_size);
302
303 static void (&transfer_fixed_end)(types<2>::transfer_context *tc);
304
305 static void (&transfer_custom)(const types<2>::gloidx *dest_gfq,
306 const types<2>::gloidx *src_gfq,
307 MPI_Comm mpicomm,
308 int tag,
309 void *dest_data,
310 const int *dest_sizes,
311 const void *src_data,
312 const int *src_sizes);
313
314 static types<2>::transfer_context *(&transfer_custom_begin)(
315 const types<2>::gloidx *dest_gfq,
316 const types<2>::gloidx *src_gfq,
317 MPI_Comm mpicomm,
318 int tag,
319 void *dest_data,
320 const int *dest_sizes,
321 const void *src_data,
322 const int *src_sizes);
323
324 static void (&transfer_custom_end)(types<2>::transfer_context *tc);
325
326 static void (&search_partition)(
327 types<2>::forest *forest,
328 int call_post,
329 types<2>::search_partition_callback quadrant_fn,
330 types<2>::search_partition_callback point_fn,
331 sc_array_t *points);
332
333 static void (&quadrant_coord_to_vertex)(
334 types<2>::connectivity *connectivity,
335 types<2>::topidx treeid,
336 types<2>::quadrant_coord x,
337 types<2>::quadrant_coord y,
338 double vxyz[3]);
339 };
340
341
342 template <>
343 struct functions<3>
344 {
345 static int (&quadrant_compare)(const void *v1, const void *v2);
346
347 static void (&quadrant_childrenv)(const types<3>::quadrant *q,
348 types<3>::quadrant c[]);
349
350 static int (&quadrant_overlaps_tree)(types<3>::tree *tree,
351 const types<3>::quadrant *q);
352
353 static void (&quadrant_set_morton)(types<3>::quadrant *quadrant,
354 int level,
355 std::uint64_t id);
356
357 static void
358 quadrant_init(types<3>::quadrant &q);
359
360 static int (&quadrant_is_equal)(const types<3>::quadrant *q1,
361 const types<3>::quadrant *q2);
362
363 static int (&quadrant_is_sibling)(const types<3>::quadrant *q1,
364 const types<3>::quadrant *q2);
365
366 static int (&quadrant_is_ancestor)(const types<3>::quadrant *q1,
367 const types<3>::quadrant *q2);
368 static int (&quadrant_ancestor_id)(const types<3>::quadrant *q,
369 int level);
370
371 static int (&comm_find_owner)(types<3>::forest *p4est,
372 const types<3>::locidx which_tree,
373 const types<3>::quadrant *q,
374 const int guess);
375
376 static types<3>::connectivity *(&connectivity_new)(
377 types<3>::topidx num_vertices,
378 types<3>::topidx num_trees,
379 types<3>::topidx num_edges,
380 types<3>::topidx num_ett,
381 types<3>::topidx num_corners,
382 types<3>::topidx num_ctt);
383
384 static types<3>::connectivity *(&connectivity_new_copy)(
385 types<3>::topidx num_vertices,
386 types<3>::topidx num_trees,
387 types<3>::topidx num_edges,
388 types<3>::topidx num_corners,
389 const double *vertices,
390 const types<3>::topidx *ttv,
391 const types<3>::topidx *ttt,
392 const int8_t *ttf,
393 const types<3>::topidx *tte,
394 const types<3>::topidx *eoff,
395 const types<3>::topidx *ett,
396 const int8_t *ete,
397 const types<3>::topidx *ttc,
398 const types<3>::topidx *coff,
399 const types<3>::topidx *ctt,
400 const int8_t *ctc);
401
402 static void (&connectivity_join_faces)(types<3>::connectivity *conn,
403 types<3>::topidx tree_left,
404 types<3>::topidx tree_right,
405 int face_left,
406 int face_right,
407 int orientation);
408
409 static void (&connectivity_destroy)(p8est_connectivity_t *connectivity);
410
411 static types<3>::forest *(&new_forest)(
412 MPI_Comm mpicomm,
413 types<3>::connectivity *connectivity,
414 types<3>::locidx min_quadrants,
415 int min_level,
416 int fill_uniform,
417 std::size_t data_size,
418 p8est_init_t init_fn,
419 void *user_pointer);
420
421 static types<3>::forest *(&copy_forest)(types<3>::forest *input,
422 int copy_data);
423
424 static void (&destroy)(types<3>::forest *p8est);
425
426 static void (&refine)(types<3>::forest *p8est,
427 int refine_recursive,
428 p8est_refine_t refine_fn,
429 p8est_init_t init_fn);
430
431 static void (&coarsen)(types<3>::forest *p8est,
432 int coarsen_recursive,
433 p8est_coarsen_t coarsen_fn,
434 p8est_init_t init_fn);
435
436 static void (&balance)(types<3>::forest *p8est,
437 types<3>::balance_type btype,
438 p8est_init_t init_fn);
439
440 static types<3>::gloidx (&partition)(types<3>::forest *p8est,
441 int partition_for_coarsening,
442 p8est_weight_t weight_fn);
443
444 static void (&save)(const char *filename,
445 types<3>::forest *p4est,
446 int save_data);
447
448 static types<3>::forest *(&load_ext)(const char *filename,
449 MPI_Comm mpicomm,
450 std::size_t data_size,
451 int load_data,
452 int autopartition,
453 int broadcasthead,
454 void *user_pointer,
455 types<3>::connectivity **p4est);
456
457 static int (&connectivity_save)(const char *filename,
458 types<3>::connectivity *connectivity);
459
460 static int (&connectivity_is_valid)(types<3>::connectivity *connectivity);
461
462 static types<3>::connectivity *(&connectivity_load)(const char *filename,
463 std::size_t *length);
464
465 static unsigned int (&checksum)(types<3>::forest *p8est);
466
467 static void (&vtk_write_file)(types<3>::forest *p8est,
468 p8est_geometry_t *,
469 const char *baseName);
470 static types<3>::ghost *(&ghost_new)(types<3>::forest *p4est,
471 types<3>::balance_type btype);
472
473 static void (&ghost_destroy)(types<3>::ghost *ghost);
474
475 static void (&reset_data)(types<3>::forest *p4est,
476 std::size_t data_size,
477 p8est_init_t init_fn,
478 void *user_pointer);
479
480 static std::size_t (&forest_memory_used)(types<3>::forest *p4est);
481
482 static std::size_t (&connectivity_memory_used)(
483 types<3>::connectivity *p4est);
484
485 static constexpr unsigned int max_level = P8EST_MAXLEVEL;
486
487 static void (&transfer_fixed)(const types<3>::gloidx *dest_gfq,
488 const types<3>::gloidx *src_gfq,
489 MPI_Comm mpicomm,
490 int tag,
491 void *dest_data,
492 const void *src_data,
493 std::size_t data_size);
494
495 static types<3>::transfer_context *(&transfer_fixed_begin)(
496 const types<3>::gloidx *dest_gfq,
497 const types<3>::gloidx *src_gfq,
498 MPI_Comm mpicomm,
499 int tag,
500 void *dest_data,
501 const void *src_data,
502 std::size_t data_size);
503
504 static void (&transfer_fixed_end)(types<3>::transfer_context *tc);
505
506 static void (&transfer_custom)(const types<3>::gloidx *dest_gfq,
507 const types<3>::gloidx *src_gfq,
508 MPI_Comm mpicomm,
509 int tag,
510 void *dest_data,
511 const int *dest_sizes,
512 const void *src_data,
513 const int *src_sizes);
514
515 static types<3>::transfer_context *(&transfer_custom_begin)(
516 const types<3>::gloidx *dest_gfq,
517 const types<3>::gloidx *src_gfq,
518 MPI_Comm mpicomm,
519 int tag,
520 void *dest_data,
521 const int *dest_sizes,
522 const void *src_data,
523 const int *src_sizes);
524
525 static void (&transfer_custom_end)(types<3>::transfer_context *tc);
526
527 static void (&search_partition)(
528 types<3>::forest *forest,
529 int call_post,
530 types<3>::search_partition_callback quadrant_fn,
531 types<3>::search_partition_callback point_fn,
532 sc_array_t *points);
533
534 static void (&quadrant_coord_to_vertex)(
535 types<3>::connectivity *connectivity,
536 types<3>::topidx treeid,
537 types<3>::quadrant_coord x,
538 types<3>::quadrant_coord y,
539 types<3>::quadrant_coord z,
540 double vxyz[3]);
541 };
542
543
544
545 /**
546 * This struct templatizes the p4est iterate structs and function
547 * prototypes, which are used to execute callback functions for faces,
548 * edges, and corners that require local neighborhood information, i.e.
549 * the neighboring cells
550 */
551 template <int dim>
552 struct iter;
553
554 template <>
555 struct iter<2>
556 {
557 using corner_info = p4est_iter_corner_info_t;
558 using corner_side = p4est_iter_corner_side_t;
559 using corner_iter = p4est_iter_corner_t;
560 using face_info = p4est_iter_face_info_t;
561 using face_side = p4est_iter_face_side_t;
562 using face_iter = p4est_iter_face_t;
563 };
564
565 template <>
566 struct iter<3>
567 {
568 using corner_info = p8est_iter_corner_info_t;
569 using corner_side = p8est_iter_corner_side_t;
570 using corner_iter = p8est_iter_corner_t;
571 using edge_info = p8est_iter_edge_info_t;
572 using edge_side = p8est_iter_edge_side_t;
573 using edge_iter = p8est_iter_edge_t;
574 using face_info = p8est_iter_face_info_t;
575 using face_side = p8est_iter_face_side_t;
576 using face_iter = p8est_iter_face_t;
577 };
578
579
580
581 /**
582 * Initialize the GeometryInfo<dim>::max_children_per_cell children of the
583 * cell p4est_cell.
584 */
585 template <int dim>
586 void
587 init_quadrant_children(
588 const typename types<dim>::quadrant &p4est_cell,
589 typename types<dim>::quadrant (
590 &p4est_children)[dealii::GeometryInfo<dim>::max_children_per_cell]);
591
592
593
594 /**
595 * Initialize quadrant to represent a coarse cell.
596 */
597 template <int dim>
598 void
599 init_coarse_quadrant(typename types<dim>::quadrant &quad);
600
601
602
603 /**
604 * Return whether q1 and q2 are equal
605 */
606 template <int dim>
607 bool
608 quadrant_is_equal(const typename types<dim>::quadrant &q1,
609 const typename types<dim>::quadrant &q2);
610
611
612
613 /**
614 * Return whether q1 is an ancestor of q2
615 */
616 template <int dim>
617 bool
618 quadrant_is_ancestor(const typename types<dim>::quadrant &q1,
619 const typename types<dim>::quadrant &q2);
620
621
622
623 /**
624 * Return whether the children of a coarse cell are stored locally
625 */
626 template <int dim>
627 bool
628 tree_exists_locally(const typename types<dim>::forest *parallel_forest,
629 const typename types<dim>::topidx coarse_grid_cell);
630
631
632 /**
633 * Deep copy a p4est connectivity object.
634 */
635 template <int dim>
636 typename types<dim>::connectivity *
637 copy_connectivity(const typename types<dim>::connectivity *connectivity);
638
639 # ifndef DOXYGEN
640 template <>
641 typename types<2>::connectivity *
642 copy_connectivity<2>(const typename types<2>::connectivity *connectivity);
643
644 template <>
645 typename types<3>::connectivity *
646 copy_connectivity<3>(const typename types<3>::connectivity *connectivity);
647 # endif
648 } // namespace p4est
649 } // namespace internal
650
651 DEAL_II_NAMESPACE_CLOSE
652
653 #else
654
655 // Make sure the scripts that create the C++20 module input files have
656 // something to latch on if the preprocessor #ifdef above would
657 // otherwise lead to an empty content of the file.
658 DEAL_II_NAMESPACE_OPEN
659 DEAL_II_NAMESPACE_CLOSE
660
661 #endif // DEAL_II_WITH_P4EST
662
663 #endif // dealii_p4est_wrappers_h