static void check_all (sc_MPI_Comm mpicomm, p4est_connectivity_t * conn, const char *vtkname, unsigned crc_expected, unsigned gcrc_expected) { int mpiret; unsigned crc_computed, gcrc_computed; long long lsize[3], gsize[3]; size_t size_conn, size_p4est, size_ghost; p4est_t *p4est; p4est_nodes_t *nodes; p4est_ghost_t *ghost; P4EST_GLOBAL_STATISTICSF ("Testing configuration %s\n", vtkname); p4est = p4est_new_ext (mpicomm, conn, 0, 0, 0, 0, NULL, NULL); p4est_refine (p4est, 1, refine_fn, NULL); p4est_coarsen (p4est, 1, coarsen_fn, NULL); p4est_balance (p4est, P4EST_CONNECT_FULL, NULL); p4est_partition (p4est, 0, NULL); p4est_vtk_write_file (p4est, NULL, vtkname); crc_computed = p4est_checksum (p4est); P4EST_GLOBAL_STATISTICSF ("Forest checksum 0x%08x\n", crc_computed); if (p4est->mpisize == 2 && p4est->mpirank == 0) { SC_CHECK_ABORT (crc_computed == crc_expected, "Forest checksum mismatch"); } ghost = p4est_ghost_new (p4est, P4EST_CONNECT_FULL); /* compute total size of forest storage */ size_conn = p4est_connectivity_memory_used (conn); size_p4est = p4est_memory_used (p4est); size_ghost = p4est_ghost_memory_used (ghost); lsize[0] = (long long) size_conn; lsize[1] = (long long) size_p4est; lsize[2] = (long long) size_ghost; mpiret = sc_MPI_Reduce (lsize, gsize, 3, sc_MPI_LONG_LONG_INT, sc_MPI_SUM, 0, mpicomm); SC_CHECK_MPI (mpiret); P4EST_GLOBAL_INFOF ("Global byte sizes: %lld %lld %lld\n", gsize[0], gsize[1], gsize[2]); gcrc_computed = p4est_ghost_checksum (p4est, ghost); P4EST_GLOBAL_STATISTICSF ("Ghost checksum 0x%08x\n", gcrc_computed); if (p4est->mpisize == 2 && p4est->mpirank == 0) { SC_CHECK_ABORT (gcrc_computed == gcrc_expected, "Ghost checksum mismatch"); } nodes = p4est_nodes_new (p4est, ghost); p4est_nodes_destroy (nodes); p4est_ghost_destroy (ghost); p4est_destroy (p4est); p4est_connectivity_destroy (conn); }
int main (int argc, char **argv) { int mpiret; mpi_context_t mpi_context, *mpi = &mpi_context; p4est_t *p4est; p4est_connectivity_t *connectivity; p4est_ghost_t *ghost; /* initialize MPI and p4est internals */ mpiret = MPI_Init (&argc, &argv); SC_CHECK_MPI (mpiret); mpi->mpicomm = MPI_COMM_WORLD; /* your favourite comm here */ mpiret = MPI_Comm_size (mpi->mpicomm, &mpi->mpisize); SC_CHECK_MPI (mpiret); mpiret = MPI_Comm_rank (mpi->mpicomm, &mpi->mpirank); SC_CHECK_MPI (mpiret); /* this should alwaps be MPI_COMM_WORLD (no effect on p4est) */ sc_init (MPI_COMM_WORLD, 0, 0, NULL, SC_LP_DEFAULT); p4est_init (NULL, SC_LP_DEFAULT); /* create 2D connectivity and forest structures */ connectivity = p4est_connectivity_new_unitsquare (); p4est = p4est_new_ext (mpi->mpicomm, connectivity, 0, 0, 1, 0, NULL, NULL); /* refine and partition */ p4est_refine (p4est, 1, refine_fn, NULL); p4est_partition (p4est, NULL); /* write vtk output */ p4est_vtk_write_file (p4est, NULL, "p4est_ptest2"); /* create and destroy ghost layer */ ghost = p4est_ghost_new (p4est, P4EST_CONNECT_FULL); p4est_ghost_destroy (ghost); /* destroy the p4est and its connectivity structure */ p4est_destroy (p4est); p4est_connectivity_destroy (connectivity); /* clean up and exit */ sc_finalize (); mpiret = MPI_Finalize (); SC_CHECK_MPI (mpiret); return 0; }
int main (int argc, char **argv) { const p4est_qcoord_t qone = 1; int mpiret; int k; int level, mid, cid; int id0, id1, id2, id3; int64_t index1, index2; size_t iz, jz, incount; p4est_qcoord_t mh = P4EST_QUADRANT_LEN (P4EST_QMAXLEVEL); p4est_connectivity_t *connectivity; p4est_t *p4est1; p4est_t *p4est2; p4est_tree_t *t1, *t2, tree; p4est_quadrant_t *p, *q1, *q2; p4est_quadrant_t r, s; p4est_quadrant_t c0, c1, c2, c3; p4est_quadrant_t cv[P4EST_CHILDREN], *cp[P4EST_CHILDREN]; p4est_quadrant_t A, B, C, D, E, F, G, H, I, P, Q; p4est_quadrant_t a, f, g, h; uint64_t Aid, Fid; /* initialize MPI */ mpiret = sc_MPI_Init (&argc, &argv); SC_CHECK_MPI (mpiret); /* create connectivity and forest structures */ connectivity = p4est_connectivity_new_unitsquare (); p4est1 = p4est_new_ext (sc_MPI_COMM_SELF, connectivity, 15, 0, 0, 0, NULL, NULL); p4est2 = p4est_new_ext (sc_MPI_COMM_SELF, connectivity, 15, 0, 0, 8, NULL, NULL); /* refine the second tree to a uniform level */ p4est_refine (p4est1, 1, refine_none, NULL); p4est_refine (p4est2, 1, refine_some, NULL); t1 = p4est_tree_array_index (p4est1->trees, 0); t2 = p4est_tree_array_index (p4est2->trees, 0); SC_CHECK_ABORT (p4est_tree_is_sorted (t1), "is_sorted"); SC_CHECK_ABORT (p4est_tree_is_sorted (t2), "is_sorted"); /* run a bunch of cross-tests */ p = NULL; for (iz = 0; iz < t1->quadrants.elem_count; ++iz) { q1 = p4est_quadrant_array_index (&t1->quadrants, iz); /* test the index conversion */ index1 = p4est_quadrant_linear_id (q1, (int) q1->level); p4est_quadrant_set_morton (&r, (int) q1->level, index1); index2 = p4est_quadrant_linear_id (&r, (int) r.level); SC_CHECK_ABORT (index1 == index2, "index conversion"); level = (int) q1->level - 1; if (level >= 0) { index1 = p4est_quadrant_linear_id (q1, level); p4est_quadrant_set_morton (&r, level, index1); index2 = p4est_quadrant_linear_id (&r, level); SC_CHECK_ABORT (index1 == index2, "index conversion"); } /* test the is_next function */ if (p != NULL) { SC_CHECK_ABORT (p4est_quadrant_is_next (p, q1), "is_next"); } p = q1; /* test the is_family function */ p4est_quadrant_children (q1, &c0, &c1, &c2, &c3); SC_CHECK_ABORT (p4est_quadrant_is_family (&c0, &c1, &c2, &c3), "is_family"); SC_CHECK_ABORT (!p4est_quadrant_is_family (&c1, &c0, &c2, &c3), "is_family"); SC_CHECK_ABORT (!p4est_quadrant_is_family (&c0, &c0, &c1, &c2), "is_family"); p4est_quadrant_childrenv (q1, cv); SC_CHECK_ABORT (p4est_quadrant_is_equal (&c0, &cv[0]), "is_family"); SC_CHECK_ABORT (p4est_quadrant_is_equal (&c1, &cv[1]), "is_family"); SC_CHECK_ABORT (p4est_quadrant_is_equal (&c2, &cv[2]), "is_family"); SC_CHECK_ABORT (p4est_quadrant_is_equal (&c3, &cv[3]), "is_family"); SC_CHECK_ABORT (p4est_quadrant_is_family (&cv[0], &cv[1], &cv[2], &cv[3]), "is_family"); cp[0] = &cv[0]; cp[1] = &cv[1]; cp[2] = &cv[2]; cp[3] = &cv[3]; SC_CHECK_ABORT (p4est_quadrant_is_familypv (cp), "is_family"); cv[1] = cv[0]; SC_CHECK_ABORT (!p4est_quadrant_is_familyv (cv), "is_family"); cp[1] = &c1; SC_CHECK_ABORT (p4est_quadrant_is_familypv (cp), "is_family"); cp[2] = &c3; SC_CHECK_ABORT (!p4est_quadrant_is_familypv (cp), "is_family"); /* test the sibling function */ mid = p4est_quadrant_child_id (q1); for (cid = 0; cid < 4; ++cid) { p4est_quadrant_sibling (q1, &r, cid); if (cid != mid) { SC_CHECK_ABORT (p4est_quadrant_is_sibling (q1, &r), "sibling"); } else { SC_CHECK_ABORT (p4est_quadrant_is_equal (q1, &r), "sibling"); } } /* test t1 against itself */ for (jz = 0; jz < t1->quadrants.elem_count; ++jz) { q2 = p4est_quadrant_array_index (&t1->quadrants, jz); /* test the comparison function */ SC_CHECK_ABORT (p4est_quadrant_compare (q1, q2) == -p4est_quadrant_compare (q2, q1), "compare"); SC_CHECK_ABORT ((p4est_quadrant_compare (q1, q2) == 0) == p4est_quadrant_is_equal (q1, q2), "is_equal"); /* test the descriptive versions of functions */ SC_CHECK_ABORT (p4est_quadrant_is_sibling_D (q1, q2) == p4est_quadrant_is_sibling (q1, q2), "is_sibling"); SC_CHECK_ABORT (p4est_quadrant_is_parent_D (q1, q2) == p4est_quadrant_is_parent (q1, q2), "is_parent"); SC_CHECK_ABORT (p4est_quadrant_is_parent_D (q2, q1) == p4est_quadrant_is_parent (q2, q1), "is_parent"); SC_CHECK_ABORT (p4est_quadrant_is_ancestor_D (q1, q2) == p4est_quadrant_is_ancestor (q1, q2), "is_ancestor"); SC_CHECK_ABORT (p4est_quadrant_is_ancestor_D (q2, q1) == p4est_quadrant_is_ancestor (q2, q1), "is_ancestor"); SC_CHECK_ABORT (p4est_quadrant_is_next_D (q1, q2) == p4est_quadrant_is_next (q1, q2), "is_next"); SC_CHECK_ABORT (p4est_quadrant_is_next_D (q2, q1) == p4est_quadrant_is_next (q2, q1), "is_next"); p4est_nearest_common_ancestor_D (q1, q2, &r); p4est_nearest_common_ancestor (q1, q2, &s); SC_CHECK_ABORT (p4est_quadrant_is_equal (&r, &s), "common_ancestor"); p4est_nearest_common_ancestor_D (q2, q1, &r); p4est_nearest_common_ancestor (q2, q1, &s); SC_CHECK_ABORT (p4est_quadrant_is_equal (&r, &s), "common_ancestor"); } /* test t1 against t2 */ for (jz = 0; jz < t2->quadrants.elem_count; ++jz) { q2 = p4est_quadrant_array_index (&t2->quadrants, jz); /* test the comparison function */ SC_CHECK_ABORT (p4est_quadrant_compare (q1, q2) == -p4est_quadrant_compare (q2, q1), "compare"); SC_CHECK_ABORT ((p4est_quadrant_compare (q1, q2) == 0) == p4est_quadrant_is_equal (q1, q2), "is_equal"); /* test the descriptive versions of functions */ SC_CHECK_ABORT (p4est_quadrant_is_sibling_D (q1, q2) == p4est_quadrant_is_sibling (q1, q2), "is_sibling"); SC_CHECK_ABORT (p4est_quadrant_is_parent_D (q1, q2) == p4est_quadrant_is_parent (q1, q2), "is_parent"); SC_CHECK_ABORT (p4est_quadrant_is_parent_D (q2, q1) == p4est_quadrant_is_parent (q2, q1), "is_parent"); SC_CHECK_ABORT (p4est_quadrant_is_ancestor_D (q1, q2) == p4est_quadrant_is_ancestor (q1, q2), "is_ancestor"); SC_CHECK_ABORT (p4est_quadrant_is_ancestor_D (q2, q1) == p4est_quadrant_is_ancestor (q2, q1), "is_ancestor"); SC_CHECK_ABORT (p4est_quadrant_is_next_D (q1, q2) == p4est_quadrant_is_next (q1, q2), "is_next"); SC_CHECK_ABORT (p4est_quadrant_is_next_D (q2, q1) == p4est_quadrant_is_next (q2, q1), "is_next"); p4est_nearest_common_ancestor_D (q1, q2, &r); p4est_nearest_common_ancestor (q1, q2, &s); SC_CHECK_ABORT (p4est_quadrant_is_equal (&r, &s), "common_ancestor"); p4est_nearest_common_ancestor_D (q2, q1, &r); p4est_nearest_common_ancestor (q2, q1, &s); SC_CHECK_ABORT (p4est_quadrant_is_equal (&r, &s), "common_ancestor"); } } p = NULL; for (iz = 0; iz < t2->quadrants.elem_count; ++iz) { q1 = p4est_quadrant_array_index (&t2->quadrants, iz); /* test the is_next function */ if (p != NULL) { SC_CHECK_ABORT (p4est_quadrant_is_next (p, q1), "is_next"); } p = q1; } /* test the coarsen function */ p4est_coarsen (p4est1, 1, coarsen_none, NULL); p4est_coarsen (p4est1, 1, coarsen_all, NULL); p4est_coarsen (p4est2, 1, coarsen_some, NULL); /* test the linearize algorithm */ incount = t2->quadrants.elem_count; (void) p4est_linearize_tree (p4est2, t2); SC_CHECK_ABORT (incount == t2->quadrants.elem_count, "linearize"); /* this is user_data neutral only when p4est1->data_size == 0 */ sc_array_init (&tree.quadrants, sizeof (p4est_quadrant_t)); sc_array_resize (&tree.quadrants, 18); q1 = p4est_quadrant_array_index (&tree.quadrants, 0); q2 = p4est_quadrant_array_index (&t2->quadrants, 0); *q1 = *q2; q2 = p4est_quadrant_array_index (&t2->quadrants, 1); for (k = 0; k < 3; ++k) { q1 = p4est_quadrant_array_index (&tree.quadrants, (size_t) (k + 1)); *q1 = *q2; q1->level = (int8_t) (q1->level + k); } for (k = 0; k < 10; ++k) { q1 = p4est_quadrant_array_index (&tree.quadrants, (size_t) (k + 4)); q2 = p4est_quadrant_array_index (&t2->quadrants, (size_t) (k + 3)); *q1 = *q2; q1->level = (int8_t) (q1->level + k); } for (k = 0; k < 4; ++k) { q1 = p4est_quadrant_array_index (&tree.quadrants, (size_t) (k + 14)); q2 = p4est_quadrant_array_index (&t2->quadrants, (size_t) (k + 12)); *q1 = *q2; q1->level = (int8_t) (q1->level + 10 + k); } tree.maxlevel = 0; for (k = 0; k <= P4EST_QMAXLEVEL; ++k) { tree.quadrants_per_level[k] = 0; } for (; k <= P4EST_MAXLEVEL; ++k) { tree.quadrants_per_level[k] = -1; } incount = tree.quadrants.elem_count; for (iz = 0; iz < incount; ++iz) { q1 = p4est_quadrant_array_index (&tree.quadrants, iz); ++tree.quadrants_per_level[q1->level]; tree.maxlevel = (int8_t) SC_MAX (tree.maxlevel, q1->level); } SC_CHECK_ABORT (!p4est_tree_is_linear (&tree), "is_linear"); (void) p4est_linearize_tree (p4est1, &tree); SC_CHECK_ABORT (incount - 3 == tree.quadrants.elem_count, "linearize"); sc_array_reset (&tree.quadrants); /* create a partial tree and check overlap */ sc_array_resize (&tree.quadrants, 3); q1 = p4est_quadrant_array_index (&tree.quadrants, 0); p4est_quadrant_set_morton (q1, 1, 1); q1 = p4est_quadrant_array_index (&tree.quadrants, 1); p4est_quadrant_set_morton (q1, 2, 8); q1 = p4est_quadrant_array_index (&tree.quadrants, 2); p4est_quadrant_set_morton (q1, 2, 9); for (k = 0; k <= P4EST_QMAXLEVEL; ++k) { tree.quadrants_per_level[k] = 0; } for (; k <= P4EST_MAXLEVEL; ++k) { tree.quadrants_per_level[k] = -1; } tree.quadrants_per_level[1] = 1; tree.quadrants_per_level[2] = 2; tree.maxlevel = 2; p4est_quadrant_first_descendant (p4est_quadrant_array_index (&tree.quadrants, 0), &tree.first_desc, P4EST_QMAXLEVEL); p4est_quadrant_last_descendant (p4est_quadrant_array_index (&tree.quadrants, tree.quadrants.elem_count - 1), &tree.last_desc, P4EST_QMAXLEVEL); SC_CHECK_ABORT (p4est_tree_is_complete (&tree), "is_complete"); p4est_quadrant_set_morton (&D, 0, 0); SC_CHECK_ABORT (p4est_quadrant_overlaps_tree (&tree, &D), "overlaps 0"); p4est_quadrant_set_morton (&A, 1, 0); SC_CHECK_ABORT (!p4est_quadrant_overlaps_tree (&tree, &A), "overlaps 1"); p4est_quadrant_set_morton (&A, 1, 1); SC_CHECK_ABORT (p4est_quadrant_overlaps_tree (&tree, &A), "overlaps 2"); p4est_quadrant_set_morton (&A, 1, 2); SC_CHECK_ABORT (p4est_quadrant_overlaps_tree (&tree, &A), "overlaps 3"); p4est_quadrant_set_morton (&A, 1, 3); SC_CHECK_ABORT (!p4est_quadrant_overlaps_tree (&tree, &A), "overlaps 4"); p4est_quadrant_set_morton (&B, 3, 13); SC_CHECK_ABORT (!p4est_quadrant_overlaps_tree (&tree, &B), "overlaps 5"); p4est_quadrant_set_morton (&B, 3, 25); SC_CHECK_ABORT (p4est_quadrant_overlaps_tree (&tree, &B), "overlaps 6"); p4est_quadrant_set_morton (&B, 3, 39); SC_CHECK_ABORT (p4est_quadrant_overlaps_tree (&tree, &B), "overlaps 7"); p4est_quadrant_set_morton (&B, 3, 40); SC_CHECK_ABORT (!p4est_quadrant_overlaps_tree (&tree, &B), "overlaps 8"); p4est_quadrant_set_morton (&C, 4, 219); SC_CHECK_ABORT (!p4est_quadrant_overlaps_tree (&tree, &C), "overlaps 9"); sc_array_reset (&tree.quadrants); /* destroy the p4est and its connectivity structure */ p4est_destroy (p4est1); p4est_destroy (p4est2); p4est_connectivity_destroy (connectivity); /* This will test the ability to address negative quadrants */ P4EST_QUADRANT_INIT (&A); P4EST_QUADRANT_INIT (&B); P4EST_QUADRANT_INIT (&C); P4EST_QUADRANT_INIT (&D); P4EST_QUADRANT_INIT (&E); P4EST_QUADRANT_INIT (&F); P4EST_QUADRANT_INIT (&G); P4EST_QUADRANT_INIT (&H); P4EST_QUADRANT_INIT (&I); P4EST_QUADRANT_INIT (&P); P4EST_QUADRANT_INIT (&Q); A.x = -qone << P4EST_MAXLEVEL; A.y = -qone << P4EST_MAXLEVEL; A.level = 0; B.x = qone << P4EST_MAXLEVEL; B.y = -qone << P4EST_MAXLEVEL; B.level = 0; C.x = -qone << P4EST_MAXLEVEL; C.y = qone << P4EST_MAXLEVEL; C.level = 0; D.x = qone << P4EST_MAXLEVEL; D.y = qone << P4EST_MAXLEVEL; D.level = 0; /* this one is outside the 3x3 box */ E.x = -qone << (P4EST_MAXLEVEL + 1); E.y = -qone; E.level = 0; F.x = P4EST_ROOT_LEN + (P4EST_ROOT_LEN - mh); F.y = P4EST_ROOT_LEN + (P4EST_ROOT_LEN - mh); F.level = P4EST_QMAXLEVEL; G.x = -mh; G.y = -mh; G.level = P4EST_QMAXLEVEL; H.x = -qone << (P4EST_MAXLEVEL - 1); H.y = -qone << (P4EST_MAXLEVEL - 1); H.level = 1; I.x = -qone << P4EST_MAXLEVEL; I.y = -qone << (P4EST_MAXLEVEL - 1); I.level = 1; check_linear_id (&A, &A); check_linear_id (&A, &B); check_linear_id (&A, &C); check_linear_id (&A, &D); /* check_linear_id (&A, &E); */ check_linear_id (&A, &F); check_linear_id (&A, &G); check_linear_id (&A, &H); check_linear_id (&A, &I); check_linear_id (&B, &A); check_linear_id (&B, &B); check_linear_id (&B, &C); check_linear_id (&B, &D); /* check_linear_id (&B, &E); */ check_linear_id (&B, &F); check_linear_id (&B, &G); check_linear_id (&B, &H); check_linear_id (&B, &I); check_linear_id (&D, &A); check_linear_id (&D, &B); check_linear_id (&D, &C); check_linear_id (&D, &D); /* check_linear_id (&D, &E); */ check_linear_id (&D, &F); check_linear_id (&D, &G); check_linear_id (&D, &H); check_linear_id (&D, &I); check_linear_id (&G, &A); check_linear_id (&G, &B); check_linear_id (&G, &C); check_linear_id (&G, &D); /* check_linear_id (&G, &E); */ check_linear_id (&G, &F); check_linear_id (&G, &G); check_linear_id (&G, &H); check_linear_id (&G, &I); check_linear_id (&I, &A); check_linear_id (&I, &B); check_linear_id (&I, &C); check_linear_id (&I, &D); /* check_linear_id (&I, &E); */ check_linear_id (&I, &F); check_linear_id (&I, &G); check_linear_id (&I, &H); check_linear_id (&I, &I); SC_CHECK_ABORT (p4est_quadrant_is_extended (&A) == 1, "is_extended A"); SC_CHECK_ABORT (p4est_quadrant_is_extended (&B) == 1, "is_extended B"); SC_CHECK_ABORT (p4est_quadrant_is_extended (&C) == 1, "is_extended C"); SC_CHECK_ABORT (p4est_quadrant_is_extended (&D) == 1, "is_extended D"); SC_CHECK_ABORT (!p4est_quadrant_is_extended (&E) == 1, "!is_extended E"); SC_CHECK_ABORT (p4est_quadrant_is_extended (&F) == 1, "is_extended F"); SC_CHECK_ABORT (p4est_quadrant_is_extended (&G) == 1, "is_extended G"); SC_CHECK_ABORT (p4est_quadrant_compare (&A, &A) == 0, "compare"); SC_CHECK_ABORT (p4est_quadrant_compare (&A, &B) > 0, "compare"); SC_CHECK_ABORT (p4est_quadrant_compare (&B, &A) < 0, "compare"); SC_CHECK_ABORT (p4est_quadrant_compare (&F, &F) == 0, "compare"); SC_CHECK_ABORT (p4est_quadrant_compare (&G, &F) > 0, "compare"); SC_CHECK_ABORT (p4est_quadrant_compare (&F, &G) < 0, "compare"); A.p.which_tree = 0; B.p.piggy1.which_tree = 0; SC_CHECK_ABORT (p4est_quadrant_compare_piggy (&A, &A) == 0, "compare_piggy"); SC_CHECK_ABORT (p4est_quadrant_compare_piggy (&A, &B) > 0, "compare_piggy"); SC_CHECK_ABORT (p4est_quadrant_compare_piggy (&B, &A) < 0, "compare_piggy"); F.p.piggy2.which_tree = 0; G.p.which_tree = 0; SC_CHECK_ABORT (p4est_quadrant_compare_piggy (&F, &F) == 0, "compare_piggy"); SC_CHECK_ABORT (p4est_quadrant_compare_piggy (&G, &F) > 0, "compare_piggy"); SC_CHECK_ABORT (p4est_quadrant_compare_piggy (&F, &G) < 0, "compare_piggy"); F.p.piggy1.which_tree = (p4est_topidx_t) P4EST_TOPIDX_MAX - 3; G.p.piggy2.which_tree = (p4est_topidx_t) P4EST_TOPIDX_MAX / 2; SC_CHECK_ABORT (p4est_quadrant_compare_piggy (&F, &F) == 0, "compare_piggy"); SC_CHECK_ABORT (p4est_quadrant_compare_piggy (&G, &F) < 0, "compare_piggy"); SC_CHECK_ABORT (p4est_quadrant_compare_piggy (&F, &G) > 0, "compare_piggy"); SC_CHECK_ABORT (p4est_quadrant_is_equal (&A, &A) == 1, "is_equal"); SC_CHECK_ABORT (p4est_quadrant_is_equal (&F, &F) == 1, "is_equal"); SC_CHECK_ABORT (p4est_quadrant_is_equal (&G, &G) == 1, "is_equal"); /* Not sure if these make sense because D, O and A are all level 0 */ #if 0 SC_CHECK_ABORT (p4est_quadrant_is_sibling (&D, &O) == 1, "is_sibling"); SC_CHECK_ABORT (p4est_quadrant_is_sibling (&D, &A) == 0, "is_sibling"); SC_CHECK_ABORT (p4est_quadrant_is_sibling_D (&D, &O) == 1, "is_sibling_D"); SC_CHECK_ABORT (p4est_quadrant_is_sibling_D (&D, &A) == 0, "is_sibling_D"); #endif SC_CHECK_ABORT (p4est_quadrant_is_sibling (&I, &H) == 1, "is_sibling"); SC_CHECK_ABORT (p4est_quadrant_is_sibling (&I, &G) == 0, "is_sibling"); SC_CHECK_ABORT (p4est_quadrant_is_sibling_D (&I, &H) == 1, "is_sibling_D"); SC_CHECK_ABORT (p4est_quadrant_is_sibling_D (&I, &G) == 0, "is_sibling_D"); SC_CHECK_ABORT (p4est_quadrant_is_parent (&A, &H) == 1, "is_parent"); SC_CHECK_ABORT (p4est_quadrant_is_parent (&H, &A) == 0, "is_parent"); SC_CHECK_ABORT (p4est_quadrant_is_parent (&A, &D) == 0, "is_parent"); SC_CHECK_ABORT (p4est_quadrant_is_parent_D (&A, &H) == 1, "is_parent_D"); SC_CHECK_ABORT (p4est_quadrant_is_ancestor (&A, &G) == 1, "is_ancestor"); SC_CHECK_ABORT (p4est_quadrant_is_ancestor (&G, &A) == 0, "is_ancestor"); SC_CHECK_ABORT (p4est_quadrant_is_ancestor_D (&A, &G) == 1, "is_ancestor_D"); SC_CHECK_ABORT (p4est_quadrant_is_ancestor_D (&G, &A) == 0, "is_ancestor_D"); /* SC_CHECK_ABORT (p4est_quadrant_is_next (&F, &E) == 1, "is_next"); */ SC_CHECK_ABORT (p4est_quadrant_is_next (&A, &H) == 0, "is_next"); /* SC_CHECK_ABORT (p4est_quadrant_is_next_D (&F, &E) == 1, "is_next_D"); */ SC_CHECK_ABORT (p4est_quadrant_is_next_D (&A, &H) == 0, "is_next_D"); p4est_quadrant_parent (&H, &a); SC_CHECK_ABORT (p4est_quadrant_is_equal (&A, &a) == 1, "parent"); p4est_quadrant_sibling (&I, &h, 3); SC_CHECK_ABORT (p4est_quadrant_is_equal (&H, &h) == 1, "sibling"); p4est_quadrant_children (&A, &c0, &c1, &c2, &c3); SC_CHECK_ABORT (p4est_quadrant_is_equal (&c2, &I) == 1, "children"); SC_CHECK_ABORT (p4est_quadrant_is_equal (&c3, &H) == 1, "children"); SC_CHECK_ABORT (p4est_quadrant_is_equal (&c3, &G) == 0, "children"); SC_CHECK_ABORT (p4est_quadrant_is_family (&c0, &c1, &c2, &c3) == 1, "is_family"); id0 = p4est_quadrant_child_id (&c0); id1 = p4est_quadrant_child_id (&c1); id2 = p4est_quadrant_child_id (&c2); id3 = p4est_quadrant_child_id (&c3); SC_CHECK_ABORT (id0 == 0 && id1 == 1 && id2 == 2 && id3 == 3, "child_id"); SC_CHECK_ABORT (p4est_quadrant_child_id (&G) == 3, "child_id"); p4est_quadrant_first_descendant (&A, &c1, 1); SC_CHECK_ABORT (p4est_quadrant_is_equal (&c0, &c1) == 1, "first_descendant"); p4est_quadrant_last_descendant (&A, &g, P4EST_QMAXLEVEL); SC_CHECK_ABORT (p4est_quadrant_is_equal (&G, &g) == 1, "last_descendant"); Fid = p4est_quadrant_linear_id (&F, P4EST_QMAXLEVEL); p4est_quadrant_set_morton (&f, P4EST_QMAXLEVEL, Fid); SC_CHECK_ABORT (p4est_quadrant_is_equal (&F, &f) == 1, "set_morton/linear_id"); Aid = p4est_quadrant_linear_id (&A, 0); p4est_quadrant_set_morton (&a, 0, Aid); SC_CHECK_ABORT (Aid == 15, "linear_id"); SC_CHECK_ABORT (p4est_quadrant_is_equal (&A, &a) == 1, "set_morton/linear_id"); p4est_nearest_common_ancestor (&I, &H, &a); SC_CHECK_ABORT (p4est_quadrant_is_equal (&A, &a) == 1, "ancestor"); p4est_nearest_common_ancestor_D (&I, &H, &a); SC_CHECK_ABORT (p4est_quadrant_is_equal (&A, &a) == 1, "ancestor_D"); for (k = 0; k < 16; ++k) { if (k != 4 && k != 6 && k != 8 && k != 9 && k != 12 && k != 13 && k != 14) { p4est_quadrant_set_morton (&E, 0, (uint64_t) k); } } p4est_quadrant_set_morton (&P, 0, 10); p4est_quadrant_set_morton (&Q, 0, 11); SC_CHECK_ABORT (p4est_quadrant_is_next (&P, &Q), "is_next"); SC_CHECK_ABORT (!p4est_quadrant_is_next (&A, &Q), "is_next"); sc_finalize (); mpiret = sc_MPI_Finalize (); SC_CHECK_MPI (mpiret); return 0; }
int main (int argc, char **argv) { int rank; int mpiret; sc_MPI_Comm mpicomm; p4est_t *p4est; p4est_connectivity_t *connectivity; mpiret = sc_MPI_Init (&argc, &argv); SC_CHECK_MPI (mpiret); mpicomm = sc_MPI_COMM_WORLD; mpiret = sc_MPI_Comm_rank (mpicomm, &rank); SC_CHECK_MPI (mpiret); sc_init (mpicomm, 1, 1, NULL, SC_LP_DEFAULT); p4est_init (NULL, SC_LP_DEFAULT); /* create connectivity and forest structures */ connectivity = p4est_connectivity_new_star (); p4est = p4est_new_ext (mpicomm, connectivity, 15, 0, 0, sizeof (user_data_t), init_fn, NULL); /* refine to make the number of elements interesting */ p4est_refine (p4est, 1, refine_fn, init_fn); /* balance the forest */ p4est_balance (p4est, P4EST_CONNECT_FULL, init_fn); /* do a uniform partition, include the weight function for testing */ p4est_partition (p4est, 0, weight_one); p4est_check_local_order (p4est, connectivity); /* do a weighted partition with many zero weights */ weight_counter = 0; weight_index = (rank == 1) ? 1342 : 0; p4est_partition (p4est, 0, weight_once); p4est_check_local_order (p4est, connectivity); /* clean up */ p4est_destroy (p4est); p4est_connectivity_destroy (connectivity); /* create connectivity and forest structures */ connectivity = p4est_connectivity_new_periodic (); p4est = p4est_new_ext (mpicomm, connectivity, 15, 0, 0, sizeof (user_data_t), init_fn, NULL); /* refine to make the number of elements interesting */ p4est_refine (p4est, 1, refine_fn, init_fn); /* balance the forest */ p4est_balance (p4est, P4EST_CONNECT_FULL, init_fn); /* do a uniform partition, include the weight function for testing */ p4est_partition (p4est, 0, weight_one); p4est_check_local_order (p4est, connectivity); /* clean up and exit */ p4est_destroy (p4est); p4est_connectivity_destroy (connectivity); sc_finalize (); mpiret = sc_MPI_Finalize (); SC_CHECK_MPI (mpiret); return 0; }
int main (int argc, char **argv) { sc_MPI_Comm mpicomm; int mpiret; int found_total; p4est_locidx_t jt, Al, Bl; p4est_locidx_t local_count; p4est_connectivity_t *conn; p4est_quadrant_t *A, *B; p4est_geometry_t *geom; p4est_t *p4est; sc_array_t *points; test_point_t *p; const char *vtkname; /* Initialize MPI */ mpiret = sc_MPI_Init (&argc, &argv); SC_CHECK_MPI (mpiret); mpicomm = sc_MPI_COMM_WORLD; /* Initialize packages */ sc_init (mpicomm, 1, 1, NULL, SC_LP_DEFAULT); p4est_init (NULL, SC_LP_DEFAULT); /* Create forest */ #ifndef P4_TO_P8 conn = p4est_connectivity_new_star (); geom = NULL; vtkname = "test_search2"; #else conn = p8est_connectivity_new_sphere (); geom = p8est_geometry_new_sphere (conn, 1., 0.191728, 0.039856); vtkname = "test_search3"; #endif p4est = p4est_new_ext (mpicomm, conn, 0, 0, 0, 0, NULL, &local_count); p4est_refine (p4est, 1, refine_fn, NULL); p4est_partition (p4est, 0, NULL); p4est_vtk_write_file (p4est, geom, vtkname); /* The following code should really be in a separate function. */ /* Prepare a point search -- fix size so the memory is not relocated */ points = sc_array_new_size (sizeof (test_point_t), 2); /* A */ p = (test_point_t *) sc_array_index (points, 0); p->name = "A"; A = &p->quad; P4EST_QUADRANT_INIT (A); p4est_quadrant_set_morton (A, 3, 23); A->p.piggy3.which_tree = 0; A->p.piggy3.local_num = -1; Al = -1; /* B */ p = (test_point_t *) sc_array_index (points, 1); p->name = "B"; B = &p->quad; P4EST_QUADRANT_INIT (B); p4est_quadrant_set_morton (B, 2, 13); B->p.piggy3.which_tree = conn->num_trees / 2; B->p.piggy3.local_num = -1; Bl = -1; /* Find quadrant numbers if existing */ for (jt = p4est->first_local_tree; jt <= p4est->last_local_tree; ++jt) { size_t zz; p4est_tree_t *tree = p4est_tree_array_index (p4est->trees, jt); p4est_quadrant_t *quad; sc_array_t *tquadrants = &tree->quadrants; for (zz = 0; zz < tquadrants->elem_count; ++zz) { quad = p4est_quadrant_array_index (tquadrants, zz); if (A->p.piggy3.which_tree == jt && !p4est_quadrant_compare (quad, A)) { Al = tree->quadrants_offset + (p4est_locidx_t) zz; P4EST_VERBOSEF ("Searching for A at %lld\n", (long long) Al); } if (B->p.piggy3.which_tree == jt && !p4est_quadrant_compare (quad, B)) { Bl = tree->quadrants_offset + (p4est_locidx_t) zz; P4EST_VERBOSEF ("Searching for B at %lld\n", (long long) Bl); } } } /* Go */ found_count = 0; p4est_search_local (p4est, 0, NULL, search_callback, points); mpiret = sc_MPI_Allreduce (&found_count, &found_total, 1, sc_MPI_INT, sc_MPI_SUM, mpicomm); SC_CHECK_MPI (mpiret); SC_CHECK_ABORT (found_total == (int) points->elem_count, "Point search"); SC_CHECK_ABORT (A->p.piggy3.local_num == Al, "Search A"); SC_CHECK_ABORT (B->p.piggy3.local_num == Bl, "Search B"); /* Use another search to count local quadrants */ local_count = 0; p4est_search_local (p4est, 0, count_callback, NULL, NULL); SC_CHECK_ABORT (local_count == p4est->local_num_quadrants, "Count search"); /* Clear memory */ sc_array_destroy (points); p4est_destroy (p4est); if (geom != NULL) { p4est_geometry_destroy (geom); } p4est_connectivity_destroy (conn); /* Test the build_local function and friends */ test_build_local (mpicomm); /* Finalize */ sc_finalize (); mpiret = sc_MPI_Finalize (); SC_CHECK_MPI (mpiret); return 0; }
static void test_build_local (sc_MPI_Comm mpicomm) { sc_array_t *points; p4est_connectivity_t *conn; p4est_t *p4est, *built, *copy; test_build_t stb, *tb = &stb; /* 0. prepare data that we will reuse */ tb->maxlevel = 7 - P4EST_DIM; tb->counter = -1; tb->wrapper = 3; tb->init_default = -1; tb->init_add = -1; tb->count_add = -1; tb->last_tree = -1; tb->build = NULL; #ifndef P4_TO_P8 conn = p4est_connectivity_new_moebius (); #else conn = p8est_connectivity_new_rotcubes (); #endif /* P4_TO_P8 */ p4est = p4est_new_ext (mpicomm, conn, 0, 0, 2, 0, NULL, tb); p4est_refine (p4est, 1, test_build_refine, NULL); p4est_partition (p4est, 0, NULL); /* TODO: enrich tests with quadrant data */ /* 1. Create a p4est that shall be identical to the old one. */ tb->build = p4est_build_new (p4est, 0, NULL, NULL); p4est_search_local (p4est, 0, test_search_local_1, NULL, NULL); built = p4est_build_complete (tb->build); SC_CHECK_ABORT (p4est_is_equal (p4est, built, 0), "Mismatch build_local 1"); p4est_destroy (built); /* 2. Create a p4est that is as coarse as possible. * Coarsen recursively, compare. */ tb->build = p4est_build_new (p4est, 4, NULL, NULL); p4est_search_local (p4est, 0, test_search_local_2, NULL, NULL); built = p4est_build_complete (tb->build); copy = p4est_copy (p4est, 0); p4est_coarsen (copy, 1, test_build_coarsen, NULL); SC_CHECK_ABORT (p4est_is_equal (copy, built, 0), "Mismatch build_local 2"); p4est_destroy (copy); p4est_destroy (built); /* 3. Create a p4est with some random pattern for demonstration */ tb->init_default = 0; tb->init_add = 0; tb->count_add = 0; tb->build = p4est_build_new (p4est, 0, test_search_init_3, tb); p4est_build_init_add (tb->build, test_search_init_add_3); p4est_search_local (p4est, 1, test_search_local_3, NULL, NULL); built = p4est_build_complete (tb->build); p4est_build_verify_3 (built); SC_CHECK_ABORT (p4est_is_valid (built), "Invalid build_local 3"); p4est_destroy (built); /* 4. Create a p4est from a search with one quadrant per tree */ tb->init_default = 0; tb->init_add = 0; tb->count_add = 0; tb->last_tree = -1; tb->build = p4est_build_new (p4est, sizeof (long), test_search_init_4, tb); p4est_build_init_add (tb->build, test_search_init_add_4); p4est_search_local (p4est, 0, test_search_local_4, NULL, NULL); built = p4est_build_complete (tb->build); p4est_build_verify_4 (built); SC_CHECK_ABORT (p4est_is_valid (built), "Invalid build_local 4"); p4est_destroy (built); /* 5. Create a p4est from a multiple-item search */ points = sc_array_new_size (sizeof (int8_t), 2); *(int8_t *) sc_array_index (points, 0) = 0; *(int8_t *) sc_array_index (points, 1) = 1; tb->wrapper = 5; tb->init_default = 0; tb->init_add = 0; tb->build = p4est_build_new (p4est, 0, NULL, tb); p4est_search_local (p4est, 0, NULL, test_search_point_5, points); built = p4est_build_complete (tb->build); #if 0 p4est_build_verify_5 (built); #endif SC_CHECK_ABORT (p4est_is_valid (built), "Invalid build_local 5"); p4est_destroy (built); sc_array_destroy (points); /* clean up */ p4est_destroy (p4est); p4est_connectivity_destroy (conn); }
static void mesh_run (mpi_context_t * mpi, p4est_connectivity_t * connectivity, int uniform, int compute_tree_index, int compute_level_lists, p4est_connect_type_t mesh_btype) { int mpiret; unsigned crc; long local_used[4], global_used[4]; p4est_t *p4est; p4est_ghost_t *ghost; p4est_mesh_t *mesh; user_data_t *ghost_data; p4est = p4est_new (mpi->mpicomm, connectivity, sizeof (user_data_t), init_fn, NULL); if (!uniform) p4est_vtk_write_file (p4est, NULL, P4EST_STRING "_mesh_new"); /* refinement */ if (uniform) { p4est_refine (p4est, 1, refine_uniform, init_fn); } else { p4est_refine (p4est, 1, refine_normal, init_fn); p4est_vtk_write_file (p4est, NULL, P4EST_STRING "_mesh_refined"); } /* balance */ p4est_balance (p4est, P4EST_CONNECT_FULL, init_fn); if (!uniform) p4est_vtk_write_file (p4est, NULL, P4EST_STRING "_mesh_balanced"); /* partition */ p4est_partition (p4est, 0, NULL); if (!uniform) { p4est_vtk_write_file (p4est, NULL, P4EST_STRING "_mesh_partition"); } crc = p4est_checksum (p4est); /* print and verify forest checksum */ P4EST_GLOBAL_STATISTICSF ("Tree %s checksum 0x%08x\n", uniform ? "uniform" : "adapted", crc); /* create ghost layer and mesh */ ghost = p4est_ghost_new (p4est, P4EST_CONNECT_FULL); ghost_data = P4EST_ALLOC (user_data_t, ghost->ghosts.elem_count); p4est_ghost_exchange_data (p4est, ghost, ghost_data); mesh = p4est_mesh_new_ext (p4est, ghost, compute_tree_index, compute_level_lists, mesh_btype); test_mesh (p4est, ghost, mesh, compute_tree_index, compute_level_lists, mesh_btype, ghost_data, uniform); /* compute memory used */ local_used[0] = (long) p4est_connectivity_memory_used (p4est->connectivity); local_used[1] = (long) p4est_memory_used (p4est); local_used[2] = (long) p4est_ghost_memory_used (ghost); local_used[3] = (long) p4est_mesh_memory_used (mesh); mpiret = sc_MPI_Allreduce (local_used, global_used, 4, sc_MPI_LONG, sc_MPI_SUM, mpi->mpicomm); SC_CHECK_MPI (mpiret); P4EST_GLOBAL_PRODUCTIONF ("Total %s memory used %ld %ld %ld %ld\n", uniform ? "uniform" : "adapted", global_used[0], global_used[1], global_used[2], global_used[3]); /* destroy ghost layer and mesh */ P4EST_FREE (ghost_data); p4est_mesh_destroy (mesh); p4est_ghost_destroy (ghost); /* destroy the p4est structure */ p4est_destroy (p4est); }
int main (int argc, char *argv[]) { MPI_Comm comm = MPI_COMM_WORLD; p4est_t *p4est; p4est_connectivity_t *conn; p4est_ghost_t *ghost_layer; p4est_lnodes_t *lnodes; int rank; const int degree = 1; BFAM_MPI_CHECK(MPI_Init(&argc,&argv)); BFAM_MPI_CHECK(MPI_Comm_rank(comm, &rank)); bfam_log_init(rank, stdout, BFAM_LL_DEFAULT); bfam_signal_handler_set(); sc_init(comm, 0, 0, NULL, SC_LP_DEFAULT); p4est_init(NULL, SC_LP_DEFAULT); conn = p4est_connectivity_new_corner(); p4est = p4est_new_ext(comm, conn, 0, 0, 0, 0, NULL, NULL); refine_level = 1; p4est_refine(p4est, 1, refine_fn, NULL); p4est_balance(p4est, P4EST_CONNECT_FACE, NULL); p4est_partition(p4est, 1, NULL); p4est_vtk_write_file(p4est, NULL, "mesh"); ghost_layer = p4est_ghost_new(p4est, P4EST_CONNECT_FULL); lnodes = p4est_lnodes_new(p4est, ghost_layer, degree); /* * Output the mesh. It can be read using something like following command: * * mpirun -np 3 ./bfam_exam_p4est | grep MESH | sort -n -k 2 | sort -n -k 5 | gvim - */ fflush(stdout); BFAM_MPI_CHECK(MPI_Barrier(comm)); BFAM_ROOT_INFO("MESH 0 ------------ Mesh Begin ------------"); BFAM_ROOT_INFO("MESH 1 degree = %d", lnodes->degree); BFAM_ROOT_INFO("MESH 2 vnodes = %d", lnodes->vnodes); BFAM_INFO("MESH 3 num_local_elements = %jd", (intmax_t)lnodes->num_local_elements); BFAM_INFO("MESH 4 num_local_nodes = %jd", (intmax_t)lnodes->num_local_nodes); BFAM_INFO("MESH 5 owned_count = %jd", (intmax_t)lnodes->owned_count); BFAM_INFO("MESH 6 global_offset = %jd", (intmax_t)lnodes->global_offset); sc_array_t *global_nodes = sc_array_new(sizeof (p4est_gloidx_t)); sc_array_resize(global_nodes, lnodes->num_local_nodes); for(size_t zz = 0; zz < global_nodes->elem_count; ++zz) { *((p4est_gloidx_t *) sc_array_index(global_nodes, zz)) = p4est_lnodes_global_index(lnodes, zz); } p4est_lnodes_share_owned(global_nodes, lnodes); for(size_t zz = 0; zz < global_nodes->elem_count; ++zz) { const p4est_gloidx_t gn = *((p4est_gloidx_t *)sc_array_index(global_nodes, zz)); SC_CHECK_ABORT (gn == p4est_lnodes_global_index(lnodes, zz), "Lnodes: bad global index across procesors"); BFAM_INFO("MESH 7 global_nodes[%zu] = %jd", zz, (intmax_t)gn); } sc_array_destroy(global_nodes); p4est_topidx_t flt = p4est->first_local_tree; p4est_topidx_t llt = p4est->last_local_tree; p4est_locidx_t elid, elnid; p4est_topidx_t t; const double *v = conn->vertices; const p4est_topidx_t *tree_to_vertex = conn->tree_to_vertex; for(elid = 0, elnid = 0, t = flt; t <= llt; ++t) { p4est_tree_t *tree = p4est_tree_array_index(p4est->trees, t); const size_t count = tree->quadrants.elem_count; p4est_topidx_t vt[P4EST_CHILDREN]; for (int c = 0; c < P4EST_CHILDREN; ++c) { vt[c] = tree_to_vertex[t * P4EST_CHILDREN + c]; } for (size_t zz = 0; zz < count; ++zz, ++elid) { p4est_quadrant_t *q = p4est_quadrant_array_index(&tree->quadrants, zz); for(int jind = 0; jind < degree + 1; ++jind) { for(int iind = 0; iind < degree + 1; ++iind, ++elnid) { double xyz[3]; for (int j = 0; j < 3; ++j) { const p4est_qcoord_t len = P4EST_QUADRANT_LEN(q->level); const double rlen = (double) P4EST_ROOT_LEN; const double deg = (double) degree; const double qlen = ((double) len) / rlen; const double eta_x = ((double) q->x) / rlen + (((double) iind) / deg) * qlen; const double eta_y = ((double) q->y) / rlen + (((double) jind) / deg) * qlen; xyz[j] = ((1. - eta_y) * ((1. - eta_x) * v[3 * vt[0] + j] + eta_x * v[3 * vt[1] + j]) + eta_y * ((1. - eta_x) * v[3 * vt[2] + j] + eta_x * v[3 * vt[3] + j])); } const p4est_locidx_t nid = lnodes->element_nodes[elnid]; BFAM_INFO( "MESH 8 local_node[%03jd] = %03jd ( %25.16e %25.16e %25.16e )", (intmax_t)elnid, (intmax_t)nid, xyz[0], xyz[1], xyz[2]); } } } } BFAM_ROOT_INFO("MESH 9 ------------ Mesh End ------------"); p4est_lnodes_destroy(lnodes); p4est_ghost_destroy(ghost_layer); p4est_destroy(p4est); p4est_connectivity_destroy(conn); sc_finalize(); BFAM_MPI_CHECK(MPI_Finalize()); return EXIT_SUCCESS; }
int main (int argc, char **argv) { int mpiret; sc_MPI_Comm mpicomm; p4est_t *p4est; p4est_connectivity_t *connectivity; p4est_locidx_t save_local_count; p4est_geometry_t *geom; mpiret = sc_MPI_Init (&argc, &argv); SC_CHECK_MPI (mpiret); mpicomm = sc_MPI_COMM_WORLD; sc_init (mpicomm, 1, 1, NULL, SC_LP_DEFAULT); p4est_init (NULL, SC_LP_DEFAULT); /* create connectivity and forest structures */ #ifdef P4_TO_P8 connectivity = p8est_connectivity_new_rotcubes (); geom = NULL; #else connectivity = p4est_connectivity_new_star (); geom = p4est_geometry_new_connectivity (connectivity); #endif p4est = p4est_new_ext (mpicomm, connectivity, 15, 0, 0, 0, NULL, NULL); save_local_count = p4est->local_num_quadrants; refine_callback_count = 0; p4est_refine_ext (p4est, 0, 2, test_refine, NULL, NULL); SC_CHECK_ABORT (refine_callback_count == save_local_count, "Refine count"); refine_callback_count = 0; p4est_refine (p4est, 1, test_refine, NULL); p4est_balance (p4est, P4EST_CONNECT_FULL, NULL); coarsen_all = 1; p4est_coarsen_both (p4est, 0, test_coarsen, NULL); coarsen_all = 0; p4est_coarsen_both (p4est, 1, test_coarsen, NULL); p4est_balance (p4est, P4EST_CONNECT_FULL, NULL); coarsen_all = 1; p4est_coarsen_both (p4est, 1, test_coarsen, NULL); p4est_vtk_write_file (p4est, geom, P4EST_STRING "_endcoarsen"); if (p4est->mpisize == 1) { SC_CHECK_ABORT (p4est->global_num_quadrants == (p4est_gloidx_t) connectivity->num_trees, "Coarsen all"); } p4est_destroy (p4est); if (geom != NULL) { p4est_geometry_destroy (geom); } p4est_connectivity_destroy (connectivity); sc_finalize (); mpiret = sc_MPI_Finalize (); SC_CHECK_MPI (mpiret); return 0; }
static void run_bricks (MPI_Comm mpicomm, int per, int l, int rlevel) { int mpiret; int tcount; double elapsed_create, elapsed_partition, elapsed_balance; #ifdef BRICKS_VTK char filename[BUFSIZ]; #endif p4est_connectivity_t *conn; p4est_t *p4est; P4EST_GLOBAL_PRODUCTIONF ("Run bricks on level %d/%d\n", l, rlevel); P4EST_ASSERT (l <= rlevel); /* create and refine the forest */ mpiret = MPI_Barrier (mpicomm); SC_CHECK_MPI (mpiret); elapsed_create = -MPI_Wtime (); tcount = 1 << l; #ifndef P4_TO_P8 conn = p4est_connectivity_new_brick (tcount, tcount, per, per); #else conn = p8est_connectivity_new_brick (tcount, tcount, tcount, per, per, per); #endif p4est = p4est_new_ext (mpicomm, conn, 0, rlevel - l, 1, 0, NULL, NULL); level_shift = 4; refine_level = rlevel - l + level_shift; p4est_refine (p4est, 1, refine_fractal, NULL); elapsed_create += MPI_Wtime (); /* partition the forest */ mpiret = MPI_Barrier (mpicomm); SC_CHECK_MPI (mpiret); elapsed_partition = -MPI_Wtime (); p4est_partition (p4est, NULL); elapsed_partition += MPI_Wtime (); /* balance the forest */ mpiret = MPI_Barrier (mpicomm); SC_CHECK_MPI (mpiret); elapsed_balance = -MPI_Wtime (); p4est_balance (p4est, P4EST_CONNECT_FULL, NULL); elapsed_balance += MPI_Wtime (); /* postprocessing */ P4EST_GLOBAL_PRODUCTIONF ("Timings %g %g %g\n", elapsed_create, elapsed_partition, elapsed_balance); #ifdef BRICKS_VTK snprintf (filename, BUFSIZ, "brick_%02d_%02d_B", rlevel, l); p4est_vtk_write_file (p4est, NULL, filename); #endif p4est_destroy (p4est); p4est_connectivity_destroy (conn); }
/** The main step 3 program. * * Setup of the example parameters; create the forest, with the state variable * stored in the quadrant data; refine, balance, and partition the forest; * timestep; clean up, and exit. */ int main (int argc, char **argv) { int mpiret; int recursive, partforcoarsen; sc_MPI_Comm mpicomm; p4est_t *p4est; p4est_connectivity_t *conn; step3_ctx_t ctx; /* Initialize MPI; see sc_mpi.h. * If configure --enable-mpi is given these are true MPI calls. * Else these are dummy functions that simulate a single-processor run. */ mpiret = sc_MPI_Init (&argc, &argv); SC_CHECK_MPI (mpiret); mpicomm = sc_MPI_COMM_WORLD; /* These functions are optional. If called they store the MPI rank as a * static variable so subsequent global p4est log messages are only issued * from processor zero. Here we turn off most of the logging; see sc.h. */ sc_init (mpicomm, 1, 1, NULL, SC_LP_ESSENTIAL); p4est_init (NULL, SC_LP_PRODUCTION); P4EST_GLOBAL_PRODUCTIONF ("This is the p4est %dD demo example/steps/%s_step3\n", P4EST_DIM, P4EST_STRING); ctx.bump_width = 0.1; ctx.max_err = 2.e-2; ctx.center[0] = 0.5; ctx.center[1] = 0.5; #ifdef P4_TO_P8 ctx.center[2] = 0.5; #endif #ifndef P4_TO_P8 /* randomly chosen advection direction */ ctx.v[0] = -0.445868402501118; ctx.v[1] = -0.895098523991131; #else ctx.v[0] = 0.485191768970225; ctx.v[1] = -0.427996381877778; ctx.v[2] = 0.762501176669961; #endif ctx.refine_period = 2; ctx.repartition_period = 4; ctx.write_period = 8; /* Create a forest that consists of just one periodic quadtree/octree. */ #ifndef P4_TO_P8 conn = p4est_connectivity_new_periodic (); #else conn = p8est_connectivity_new_periodic (); #endif /* *INDENT-OFF* */ p4est = p4est_new_ext (mpicomm, /* communicator */ conn, /* connectivity */ 0, /* minimum quadrants per MPI process */ 4, /* minimum level of refinement */ 1, /* fill uniform */ sizeof (step3_data_t), /* data size */ step3_init_initial_condition, /* initializes data */ (void *) (&ctx)); /* context */ /* *INDENT-ON* */ /* refine and coarsen based on an interpolation error estimate */ recursive = 1; p4est_refine (p4est, recursive, step3_refine_err_estimate, step3_init_initial_condition); p4est_coarsen (p4est, recursive, step3_coarsen_initial_condition, step3_init_initial_condition); /* Partition: The quadrants are redistributed for equal element count. The * partition can optionally be modified such that a family of octants, which * are possibly ready for coarsening, are never split between processors. */ partforcoarsen = 1; /* If we call the 2:1 balance we ensure that neighbors do not differ in size * by more than a factor of 2. This can optionally include diagonal * neighbors across edges or corners as well; see p4est.h. */ p4est_balance (p4est, P4EST_CONNECT_FACE, step3_init_initial_condition); p4est_partition (p4est, partforcoarsen, NULL); /* time step */ step3_timestep (p4est, 0.1); /* Destroy the p4est and the connectivity structure. */ p4est_destroy (p4est); p4est_connectivity_destroy (conn); /* Verify that allocations internal to p4est and sc do not leak memory. * This should be called if sc_init () has been called earlier. */ sc_finalize (); /* This is standard MPI programs. Without --enable-mpi, this is a dummy. */ mpiret = sc_MPI_Finalize (); SC_CHECK_MPI (mpiret); return 0; }
static void test_loadsave (p4est_connectivity_t * connectivity, const char *prefix, sc_MPI_Comm mpicomm, int mpirank) { int mpiret, retval; unsigned csum, csum2; double elapsed, wtime; p4est_connectivity_t *conn2; p4est_t *p4est, *p4est2; sc_statinfo_t stats[STATS_COUNT]; char conn_name[BUFSIZ]; char p4est_name[BUFSIZ]; snprintf (conn_name, BUFSIZ, "%s.%s", prefix, P4EST_CONN_SUFFIX); snprintf (p4est_name, BUFSIZ, "%s.%s", prefix, P4EST_FOREST_SUFFIX); P4EST_GLOBAL_INFOF ("Using file names %s and %s\n", conn_name, p4est_name); p4est = p4est_new_ext (mpicomm, connectivity, 0, 0, 0, sizeof (int), init_fn, NULL); p4est_refine (p4est, 1, refine_fn, init_fn); test_deflate (p4est); /* save, synchronize, load connectivity and compare */ if (mpirank == 0) { retval = p4est_connectivity_save (conn_name, connectivity); SC_CHECK_ABORT (retval == 0, "connectivity_save failed"); } mpiret = sc_MPI_Barrier (mpicomm); SC_CHECK_MPI (mpiret); wtime = sc_MPI_Wtime (); conn2 = p4est_connectivity_load (conn_name, NULL); elapsed = sc_MPI_Wtime () - wtime; sc_stats_set1 (stats + STATS_CONN_LOAD, elapsed, "conn load"); SC_CHECK_ABORT (p4est_connectivity_is_equal (connectivity, conn2), "load/save connectivity mismatch A"); p4est_connectivity_destroy (conn2); /* save, synchronize, load p4est and compare */ wtime = sc_MPI_Wtime (); p4est_save (p4est_name, p4est, 1); elapsed = sc_MPI_Wtime () - wtime; sc_stats_set1 (stats + STATS_P4EST_SAVE1, elapsed, "p4est save 1"); wtime = sc_MPI_Wtime (); p4est2 = p4est_load (p4est_name, mpicomm, sizeof (int), 1, NULL, &conn2); elapsed = sc_MPI_Wtime () - wtime; sc_stats_set1 (stats + STATS_P4EST_LOAD1a, elapsed, "p4est load 1a"); SC_CHECK_ABORT (p4est_connectivity_is_equal (connectivity, conn2), "load/save connectivity mismatch Ba"); SC_CHECK_ABORT (p4est_is_equal (p4est, p4est2, 1), "load/save p4est mismatch Ba"); p4est_destroy (p4est2); p4est_connectivity_destroy (conn2); wtime = sc_MPI_Wtime (); p4est2 = p4est_load (p4est_name, mpicomm, 0, 0, NULL, &conn2); elapsed = sc_MPI_Wtime () - wtime; sc_stats_set1 (stats + STATS_P4EST_LOAD1b, elapsed, "p4est load 1b"); SC_CHECK_ABORT (p4est_connectivity_is_equal (connectivity, conn2), "load/save connectivity mismatch Bb"); SC_CHECK_ABORT (p4est_is_equal (p4est, p4est2, 0), "load/save p4est mismatch Bb"); test_deflate (p4est2); p4est_destroy (p4est2); p4est_connectivity_destroy (conn2); /* partition and balance */ p4est_partition (p4est, 0, NULL); p4est_balance (p4est, P4EST_CONNECT_FULL, init_fn); csum = p4est_checksum (p4est); sc_stats_set1 (stats + STATS_P4EST_ELEMS, (double) p4est->local_num_quadrants, "p4est elements"); /* save, synchronize, load p4est and compare */ wtime = sc_MPI_Wtime (); p4est_save (p4est_name, p4est, 0); elapsed = sc_MPI_Wtime () - wtime; sc_stats_set1 (stats + STATS_P4EST_SAVE2, elapsed, "p4est save 2"); wtime = sc_MPI_Wtime (); p4est2 = p4est_load (p4est_name, mpicomm, sizeof (int), 0, NULL, &conn2); elapsed = sc_MPI_Wtime () - wtime; sc_stats_set1 (stats + STATS_P4EST_LOAD2, elapsed, "p4est load 2"); SC_CHECK_ABORT (p4est_connectivity_is_equal (connectivity, conn2), "load/save connectivity mismatch C"); SC_CHECK_ABORT (p4est_is_equal (p4est, p4est2, 0), "load/save p4est mismatch C"); p4est_destroy (p4est2); p4est_connectivity_destroy (conn2); /* save, synchronize, load p4est and compare */ wtime = sc_MPI_Wtime (); p4est_save (p4est_name, p4est, 1); elapsed = sc_MPI_Wtime () - wtime; sc_stats_set1 (stats + STATS_P4EST_SAVE3, elapsed, "p4est save 3"); wtime = sc_MPI_Wtime (); p4est2 = p4est_load (p4est_name, mpicomm, sizeof (int), 0, NULL, &conn2); elapsed = sc_MPI_Wtime () - wtime; sc_stats_set1 (stats + STATS_P4EST_LOAD3, elapsed, "p4est load 3"); SC_CHECK_ABORT (p4est_connectivity_is_equal (connectivity, conn2), "load/save connectivity mismatch D"); SC_CHECK_ABORT (p4est_is_equal (p4est, p4est2, 0), "load/save p4est mismatch D"); p4est_destroy (p4est2); p4est_connectivity_destroy (conn2); /* Test autopartition load feature */ wtime = sc_MPI_Wtime (); p4est2 = p4est_load_ext (p4est_name, mpicomm, sizeof (int), 0, 1, 0, NULL, &conn2); elapsed = sc_MPI_Wtime () - wtime; csum2 = p4est_checksum (p4est2); sc_stats_set1 (stats + STATS_P4EST_LOAD4, elapsed, "p4est load 4"); SC_CHECK_ABORT (p4est_connectivity_is_equal (connectivity, conn2), "load/save connectivity mismatch E"); SC_CHECK_ABORT (mpirank != 0 || csum == csum2, "load/save p4est mismatch E"); p4est_destroy (p4est2); p4est_connectivity_destroy (conn2); /* destroy data structures */ p4est_destroy (p4est); /* compute and print timings */ sc_stats_compute (mpicomm, STATS_COUNT, stats); sc_stats_print (p4est_package_id, SC_LP_STATISTICS, STATS_COUNT, stats, 0, 1); }
int main (int argc, char **argv) { int rank; int num_procs; int mpiret; sc_MPI_Comm mpicomm; p4est_t *p4est, *copy; p4est_connectivity_t *connectivity; int i; p4est_topidx_t t; size_t qz; p4est_locidx_t num_quadrants_on_last; p4est_locidx_t *num_quadrants_in_proc; p4est_gloidx_t *pertree1, *pertree2; p4est_quadrant_t *quad; p4est_tree_t *tree; user_data_t *user_data; int64_t sum; unsigned crc; mpiret = sc_MPI_Init (&argc, &argv); SC_CHECK_MPI (mpiret); mpicomm = sc_MPI_COMM_WORLD; mpiret = sc_MPI_Comm_rank (mpicomm, &rank); SC_CHECK_MPI (mpiret); sc_init (mpicomm, 1, 1, NULL, SC_LP_DEFAULT); /* create connectivity and forest structures */ #ifdef P4_TO_P8 connectivity = p8est_connectivity_new_twocubes (); #else connectivity = p4est_connectivity_new_corner (); #endif p4est = p4est_new_ext (mpicomm, connectivity, 15, 0, 0, sizeof (user_data_t), init_fn, NULL); pertree1 = P4EST_ALLOC (p4est_gloidx_t, p4est->connectivity->num_trees + 1); pertree2 = P4EST_ALLOC (p4est_gloidx_t, p4est->connectivity->num_trees + 1); num_procs = p4est->mpisize; num_quadrants_in_proc = P4EST_ALLOC (p4est_locidx_t, num_procs); /* refine and balance to make the number of elements interesting */ test_pertree (p4est, NULL, pertree1); p4est_refine (p4est, 1, refine_fn, init_fn); test_pertree (p4est, NULL, pertree1); /* Set an arbitrary partition. * * Since this is just a test we assume the global number of * quadrants will fit in an int32_t */ num_quadrants_on_last = (p4est_locidx_t) p4est->global_num_quadrants; for (i = 0; i < num_procs - 1; ++i) { num_quadrants_in_proc[i] = (p4est_locidx_t) i + 1; /* type ok */ num_quadrants_on_last -= (p4est_locidx_t) i + 1; /* type ok */ } num_quadrants_in_proc[num_procs - 1] = num_quadrants_on_last; SC_CHECK_ABORT (num_quadrants_on_last > 0, "Negative number of quadrants on the last processor"); /* Save a checksum of the original forest */ crc = p4est_checksum (p4est); /* partition the forest */ (void) p4est_partition_given (p4est, num_quadrants_in_proc); test_pertree (p4est, pertree1, pertree2); /* Double check that we didn't loose any quads */ SC_CHECK_ABORT (crc == p4est_checksum (p4est), "bad checksum, missing a quad"); /* count the actual number of quadrants per proc */ SC_CHECK_ABORT (num_quadrants_in_proc[rank] == p4est->local_num_quadrants, "partition failed, wrong number of quadrants"); /* check user data content */ for (t = p4est->first_local_tree; t <= p4est->last_local_tree; ++t) { tree = p4est_tree_array_index (p4est->trees, t); for (qz = 0; qz < tree->quadrants.elem_count; ++qz) { quad = p4est_quadrant_array_index (&tree->quadrants, qz); user_data = (user_data_t *) quad->p.user_data; sum = quad->x + quad->y + quad->level; SC_CHECK_ABORT (user_data->a == t, "bad user_data, a"); SC_CHECK_ABORT (user_data->sum == sum, "bad user_data, sum"); } } /* do a weighted partition with uniform weights */ p4est_partition (p4est, 0, weight_one); test_pertree (p4est, pertree1, pertree2); SC_CHECK_ABORT (crc == p4est_checksum (p4est), "bad checksum after uniformly weighted partition"); /* copy the p4est */ copy = p4est_copy (p4est, 1); SC_CHECK_ABORT (crc == p4est_checksum (copy), "bad checksum after copy"); /* do a weighted partition with many zero weights */ weight_counter = 0; weight_index = (rank == 1) ? 1342 : 0; p4est_partition (copy, 0, weight_once); test_pertree (copy, pertree1, pertree2); SC_CHECK_ABORT (crc == p4est_checksum (copy), "bad checksum after unevenly weighted partition 1"); /* do a weighted partition with many zero weights */ weight_counter = 0; weight_index = 0; p4est_partition (copy, 0, weight_once); test_pertree (copy, pertree1, pertree2); SC_CHECK_ABORT (crc == p4est_checksum (copy), "bad checksum after unevenly weighted partition 2"); /* do a weighted partition with many zero weights * * Since this is just a test we assume the local number of * quadrants will fit in an int */ weight_counter = 0; weight_index = (rank == num_procs - 1) ? ((int) copy->local_num_quadrants - 1) : 0; p4est_partition (copy, 0, weight_once); test_pertree (copy, pertree1, pertree2); SC_CHECK_ABORT (crc == p4est_checksum (copy), "bad checksum after unevenly weighted partition 3"); /* check user data content */ for (t = copy->first_local_tree; t <= copy->last_local_tree; ++t) { tree = p4est_tree_array_index (copy->trees, t); for (qz = 0; qz < tree->quadrants.elem_count; ++qz) { quad = p4est_quadrant_array_index (&tree->quadrants, qz); user_data = (user_data_t *) quad->p.user_data; sum = quad->x + quad->y + quad->level; SC_CHECK_ABORT (user_data->a == t, "bad user_data, a"); SC_CHECK_ABORT (user_data->sum == sum, "bad user_data, sum"); } } /* Add another test. Overwrites pertree1, pertree2 */ test_partition_circle (mpicomm, connectivity, pertree1, pertree2); /* clean up and exit */ P4EST_FREE (pertree1); P4EST_FREE (pertree2); P4EST_FREE (num_quadrants_in_proc); p4est_destroy (p4est); p4est_destroy (copy); p4est_connectivity_destroy (connectivity); sc_finalize (); mpiret = sc_MPI_Finalize (); SC_CHECK_MPI (mpiret); return 0; }
int main (int argc, char **argv) { sc_MPI_Comm mpicomm; int mpiret; int size, rank; unsigned crcF, crcC; p4est_connectivity_t *connectivity; p4est_t *p4est; p4est_t *p4estF, *p4estC; #ifdef P4_TO_P8 unsigned crcE; p4est_t *p4estE; #endif /* initialize */ mpiret = sc_MPI_Init (&argc, &argv); SC_CHECK_MPI (mpiret); mpicomm = sc_MPI_COMM_WORLD; mpiret = sc_MPI_Comm_size (mpicomm, &size); SC_CHECK_MPI (mpiret); mpiret = sc_MPI_Comm_rank (mpicomm, &rank); SC_CHECK_MPI (mpiret); sc_init (mpicomm, 1, 1, NULL, SC_LP_DEFAULT); p4est_init (NULL, SC_LP_DEFAULT); /* create forest and refine */ #ifndef P4_TO_P8 connectivity = p4est_connectivity_new_star (); #else connectivity = p8est_connectivity_new_rotcubes (); #endif p4est = p4est_new_ext (mpicomm, connectivity, 0, 0, 0, 0, NULL, NULL); p4est_refine (p4est, 1, refine_fn, NULL); /* test face balance */ p4estF = p4est_copy (p4est, 0); #ifndef P4_TO_P8 p4est_balance (p4estF, P4EST_CONNECT_FACE, NULL); #else p4est_balance (p4estF, P8EST_CONNECT_FACE, NULL); #endif crcF = p4est_checksum (p4estF); P4EST_GLOBAL_INFOF ("Face balance with %lld quadrants and crc 0x%08x\n", (long long) p4estF->global_num_quadrants, crcF); #ifdef P4_TO_P8 /* test edge balance */ p4estE = p4est_copy (p4est, 1); p4est_balance (p4estF, P8EST_CONNECT_EDGE, NULL); p4est_balance (p4estE, P8EST_CONNECT_EDGE, NULL); crcE = p4est_checksum (p4estE); SC_CHECK_ABORT (crcE == p4est_checksum (p4estF), "mismatch A"); P4EST_GLOBAL_INFOF ("Edge balance with %lld quadrants and crc 0x%08x\n", (long long) p4estE->global_num_quadrants, crcE); #endif /* test corner balance */ p4estC = p4est_copy (p4est, 1); #ifndef P4_TO_P8 p4est_balance (p4estF, P4EST_CONNECT_CORNER, NULL); p4est_balance (p4estC, P4EST_CONNECT_CORNER, NULL); #else p4est_balance (p4estF, P8EST_CONNECT_CORNER, NULL); p4est_balance (p4estC, P8EST_CONNECT_CORNER, NULL); #endif crcC = p4est_checksum (p4estC); SC_CHECK_ABORT (crcC == p4est_checksum (p4estF), "mismatch B"); P4EST_GLOBAL_INFOF ("Corner balance with %lld quadrants and crc 0x%08x\n", (long long) p4estC->global_num_quadrants, crcC); /* destroy forests and connectivity */ p4est_destroy (p4est); p4est_destroy (p4estF); #ifdef P4_TO_P8 p4est_destroy (p4estE); #endif p4est_destroy (p4estC); p4est_connectivity_destroy (connectivity); /* clean up and exit */ sc_finalize (); mpiret = sc_MPI_Finalize (); SC_CHECK_MPI (mpiret); return 0; }
int main (int argc, char **argv) { sc_MPI_Comm mpicomm; int mpiret; int mpisize, mpirank; p4est_t *p4est; p4est_connectivity_t *conn; sc_array_t *points_per_dim, *cone_sizes, *cones, *cone_orientations, *coords, *children, *parents, *childids, *leaves, *remotes; p4est_locidx_t first_local_quad = -1; /* initialize MPI */ mpiret = sc_MPI_Init (&argc, &argv); SC_CHECK_MPI (mpiret); mpicomm = sc_MPI_COMM_WORLD; mpiret = sc_MPI_Comm_size (mpicomm, &mpisize); SC_CHECK_MPI (mpiret); mpiret = sc_MPI_Comm_rank (mpicomm, &mpirank); SC_CHECK_MPI (mpiret); sc_init (mpicomm, 1, 1, NULL, SC_LP_DEFAULT); p4est_init (NULL, SC_LP_DEFAULT); #ifndef P4_TO_P8 conn = p4est_connectivity_new_moebius (); #else conn = p8est_connectivity_new_rotcubes (); #endif p4est = p4est_new_ext (mpicomm, conn, 0, 1, 1, 0, NULL, NULL); p4est_refine (p4est, 1, refine_fn, NULL); p4est_balance (p4est, P4EST_CONNECT_FULL, NULL); p4est_partition (p4est, 0, NULL); points_per_dim = sc_array_new (sizeof (p4est_locidx_t)); cone_sizes = sc_array_new (sizeof (p4est_locidx_t)); cones = sc_array_new (sizeof (p4est_locidx_t)); cone_orientations = sc_array_new (sizeof (p4est_locidx_t)); coords = sc_array_new (3 * sizeof (double)); children = sc_array_new (sizeof (p4est_locidx_t)); parents = sc_array_new (sizeof (p4est_locidx_t)); childids = sc_array_new (sizeof (p4est_locidx_t)); leaves = sc_array_new (sizeof (p4est_locidx_t)); remotes = sc_array_new (2 * sizeof (p4est_locidx_t)); p4est_get_plex_data (p4est, P4EST_CONNECT_FULL, (mpisize > 1) ? 2 : 0, &first_local_quad, points_per_dim, cone_sizes, cones, cone_orientations, coords, children, parents, childids, leaves, remotes); #ifdef P4EST_WITH_PETSC { PetscErrorCode ierr; DM plex, refTree; PetscInt pStart, pEnd; PetscSection parentSection; PetscSF pointSF; size_t zz, count; locidx_to_PetscInt (points_per_dim); locidx_to_PetscInt (cone_sizes); locidx_to_PetscInt (cones); locidx_to_PetscInt (cone_orientations); coords_double_to_PetscScalar (coords); locidx_to_PetscInt (children); locidx_to_PetscInt (parents); locidx_to_PetscInt (childids); locidx_to_PetscInt (leaves); locidx_pair_to_PetscSFNode (remotes); P4EST_GLOBAL_PRODUCTION ("Begin PETSc routines\n"); ierr = PetscInitialize (&argc, &argv, 0, help); CHKERRQ (ierr); ierr = DMPlexCreate (mpicomm, &plex); CHKERRQ (ierr); ierr = DMSetDimension (plex, P4EST_DIM); CHKERRQ (ierr); ierr = DMSetCoordinateDim (plex, 3); CHKERRQ (ierr); ierr = DMPlexCreateFromDAG (plex, P4EST_DIM, (PetscInt *) points_per_dim->array, (PetscInt *) cone_sizes->array, (PetscInt *) cones->array, (PetscInt *) cone_orientations->array, (PetscScalar *) coords->array); CHKERRQ (ierr); ierr = PetscSFCreate (mpicomm, &pointSF); CHKERRQ (ierr); ierr = DMPlexCreateDefaultReferenceTree (mpicomm, P4EST_DIM, PETSC_FALSE, &refTree); CHKERRQ (ierr); ierr = DMPlexSetReferenceTree (plex, refTree); CHKERRQ (ierr); ierr = DMDestroy (&refTree); CHKERRQ (ierr); ierr = PetscSectionCreate (mpicomm, &parentSection); CHKERRQ (ierr); ierr = DMPlexGetChart (plex, &pStart, &pEnd); CHKERRQ (ierr); ierr = PetscSectionSetChart (parentSection, pStart, pEnd); CHKERRQ (ierr); count = children->elem_count; for (zz = 0; zz < count; zz++) { PetscInt child = *((PetscInt *) sc_array_index (children, zz)); ierr = PetscSectionSetDof (parentSection, child, 1); CHKERRQ (ierr); } ierr = PetscSectionSetUp (parentSection); CHKERRQ (ierr); ierr = DMPlexSetTree (plex, parentSection, (PetscInt *) parents->array, (PetscInt *) childids->array); CHKERRQ (ierr); ierr = PetscSectionDestroy (&parentSection); CHKERRQ (ierr); ierr = PetscSFSetGraph (pointSF, pEnd - pStart, (PetscInt) leaves->elem_count, (PetscInt *) leaves->array, PETSC_COPY_VALUES, (PetscSFNode *) remotes->array, PETSC_COPY_VALUES); CHKERRQ (ierr); ierr = DMViewFromOptions (plex, NULL, "-dm_view"); CHKERRQ (ierr); /* TODO: test with rigid body modes as in plex ex3 */ ierr = DMDestroy (&plex); CHKERRQ (ierr); ierr = PetscFinalize (); P4EST_GLOBAL_PRODUCTION ("End PETSc routines\n"); } #endif sc_array_destroy (points_per_dim); sc_array_destroy (cone_sizes); sc_array_destroy (cones); sc_array_destroy (cone_orientations); sc_array_destroy (coords); sc_array_destroy (children); sc_array_destroy (parents); sc_array_destroy (childids); sc_array_destroy (leaves); sc_array_destroy (remotes); p4est_destroy (p4est); p4est_connectivity_destroy (conn); sc_finalize (); mpiret = sc_MPI_Finalize (); SC_CHECK_MPI (mpiret); return 0; }
static void run_load (sc_MPI_Comm mpicomm, p4est_connectivity_t * conn, int level) { int mpiret; double elapsed_create, elapsed_partition, elapsed_balance; #ifdef LOADCONN_VTK char filename[BUFSIZ]; #endif p4est_t *p4est; P4EST_GLOBAL_PRODUCTIONF ("Run load on level %d\n", level); /* create and refine the forest */ mpiret = sc_MPI_Barrier (mpicomm); SC_CHECK_MPI (mpiret); elapsed_create = -sc_MPI_Wtime (); p4est = p4est_new_ext (mpicomm, conn, 0, level, 1, 0, NULL, NULL); level_shift = 4; refine_level = level + level_shift; p4est_refine (p4est, 1, refine_fractal, NULL); elapsed_create += sc_MPI_Wtime (); #ifdef LOADCONN_VTK snprintf (filename, BUFSIZ, "loadconn%d_%02d_C", P4EST_DIM, level); p4est_vtk_write_file (p4est, NULL, filename); #endif /* partition the forest */ mpiret = sc_MPI_Barrier (mpicomm); SC_CHECK_MPI (mpiret); elapsed_partition = -sc_MPI_Wtime (); p4est_partition (p4est, 0, NULL); elapsed_partition += sc_MPI_Wtime (); /* balance the forest */ mpiret = sc_MPI_Barrier (mpicomm); SC_CHECK_MPI (mpiret); elapsed_balance = -sc_MPI_Wtime (); p4est_balance (p4est, P4EST_CONNECT_FULL, NULL); elapsed_balance += sc_MPI_Wtime (); #ifdef LOADCONN_VTK snprintf (filename, BUFSIZ, "loadconn%d_%02d_B", P4EST_DIM, level); p4est_vtk_write_file (p4est, NULL, filename); #endif /* report timings */ P4EST_GLOBAL_PRODUCTIONF ("Timings %d: %g %g %g\n", level, elapsed_create, elapsed_partition, elapsed_balance); p4est_destroy (p4est); }
int main (int argc, char **argv) { sc_MPI_Comm mpicomm; int mpiret; int mpisize, mpirank; p4est_t *p4est; p4est_connectivity_t *connectivity; sc_dmatrix_t *vtkvec; p4est_tree_t *tree; sc_array_t *quadrants; size_t zz, count; p4est_quadrant_t *q; int i; #ifndef P4_TO_P8 char filename[] = "p4est_balance_face"; #else char filename[] = "p8est_balance_edge"; #endif p4est_vtk_context_t *context; sc_array_t *level; int retval; /* initialize MPI */ mpiret = sc_MPI_Init (&argc, &argv); SC_CHECK_MPI (mpiret); mpicomm = sc_MPI_COMM_WORLD; mpiret = sc_MPI_Comm_size (mpicomm, &mpisize); SC_CHECK_MPI (mpiret); mpiret = sc_MPI_Comm_rank (mpicomm, &mpirank); SC_CHECK_MPI (mpiret); sc_init (mpicomm, 1, 1, NULL, SC_LP_DEFAULT); p4est_init (NULL, SC_LP_DEFAULT); #ifndef P4_TO_P8 connectivity = p4est_connectivity_new_unitsquare (); #else connectivity = p8est_connectivity_new_unitcube (); #endif p4est = p4est_new_ext (mpicomm, connectivity, 0, 2, 1, sizeof (balance_seeds_elem_t), init_fn, NULL); p4est_refine (p4est, 1, refine_fn, init_fn); context = p4est_vtk_context_new (p4est, filename); p4est_vtk_context_set_scale (context, 1. - 2. * SC_EPS); context = p4est_vtk_write_header (context); SC_CHECK_ABORT (context != NULL, P4EST_STRING "_vtk: Error writing header"); vtkvec = sc_dmatrix_new (p4est->local_num_quadrants, P4EST_CHILDREN); tree = p4est_tree_array_index (p4est->trees, 0); quadrants = &(tree->quadrants); count = quadrants->elem_count; for (zz = 0; zz < count; zz++) { q = p4est_quadrant_array_index (quadrants, zz); for (i = 0; i < P4EST_CHILDREN; i++) { vtkvec->e[zz][i] = (double) ((balance_seeds_elem_t *) (q->p.user_data))->flag; } } level = sc_array_new_data ((void *) vtkvec->e[0], sizeof (double), count * P4EST_CHILDREN); context = p4est_vtk_write_point_dataf (context, 1, 0, "level", level, context); SC_CHECK_ABORT (context != NULL, P4EST_STRING "_vtk: Error writing point data"); sc_array_destroy (level); retval = p4est_vtk_write_footer (context); SC_CHECK_ABORT (!retval, P4EST_STRING "_vtk: Error writing footer"); sc_dmatrix_destroy (vtkvec); p4est_destroy (p4est); p4est_connectivity_destroy (connectivity); sc_finalize (); mpiret = sc_MPI_Finalize (); SC_CHECK_MPI (mpiret); return 0; }
int main (int argc, char **argv) { sc_MPI_Comm mpicomm; int mpiret; int mpisize, mpirank; unsigned crc; #ifndef P4_TO_P8 size_t kz; int8_t l; p4est_quadrant_t *q; p4est_tree_t stree, *tree = &stree; #endif p4est_t *p4est; p4est_connectivity_t *connectivity; /* initialize MPI */ mpiret = sc_MPI_Init (&argc, &argv); SC_CHECK_MPI (mpiret); mpicomm = sc_MPI_COMM_WORLD; mpiret = sc_MPI_Comm_size (mpicomm, &mpisize); SC_CHECK_MPI (mpiret); mpiret = sc_MPI_Comm_rank (mpicomm, &mpirank); SC_CHECK_MPI (mpiret); sc_init (mpicomm, 1, 1, NULL, SC_LP_DEFAULT); p4est_init (NULL, SC_LP_DEFAULT); #ifndef P4_TO_P8 connectivity = p4est_connectivity_new_star (); #else connectivity = p8est_connectivity_new_rotcubes (); #endif p4est = p4est_new_ext (mpicomm, connectivity, 0, 0, 0, 4, NULL, NULL); #ifndef P4_TO_P8 /* build empty tree */ sc_array_init (&tree->quadrants, sizeof (p4est_quadrant_t)); for (l = 0; l <= P4EST_MAXLEVEL; ++l) { tree->quadrants_per_level[l] = 0; } tree->maxlevel = 0; /* insert two quadrants */ sc_array_resize (&tree->quadrants, 4); q = p4est_quadrant_array_index (&tree->quadrants, 0); p4est_quadrant_set_morton (q, 3, 13); q = p4est_quadrant_array_index (&tree->quadrants, 1); p4est_quadrant_set_morton (q, 1, 1); q = p4est_quadrant_array_index (&tree->quadrants, 2); p4est_quadrant_set_morton (q, 1, 2); q = p4est_quadrant_array_index (&tree->quadrants, 3); p4est_quadrant_set_morton (q, 1, 3); for (kz = 0; kz < tree->quadrants.elem_count; ++kz) { q = p4est_quadrant_array_index (&tree->quadrants, kz); q->p.user_data = sc_mempool_alloc (p4est->user_data_pool); ++tree->quadrants_per_level[q->level]; tree->maxlevel = (int8_t) SC_MAX (tree->maxlevel, q->level); } /* balance the tree, print and destroy */ #if 0 p4est_balance_subtree (p4est, P4EST_CONNECT_FULL, 0, NULL); p4est_tree_print (SC_LP_INFO, tree); #endif for (kz = 0; kz < tree->quadrants.elem_count; ++kz) { q = p4est_quadrant_array_index (&tree->quadrants, kz); sc_mempool_free (p4est->user_data_pool, q->p.user_data); } sc_array_reset (&tree->quadrants); #endif /* !P4_TO_P8 */ /* check reset data function */ p4est_reset_data (p4est, 0, init_fn, NULL); p4est_reset_data (p4est, 0, NULL, NULL); /* refine and balance the forest */ SC_CHECK_ABORT (p4est_is_balanced (p4est, P4EST_CONNECT_FULL), "Balance 1"); p4est_refine (p4est, 1, refine_fn, NULL); SC_CHECK_ABORT (!p4est_is_balanced (p4est, P4EST_CONNECT_FULL), "Balance 2"); p4est_balance (p4est, P4EST_CONNECT_FULL, NULL); SC_CHECK_ABORT (p4est_is_balanced (p4est, P4EST_CONNECT_FULL), "Balance 3"); /* check reset data function */ p4est_reset_data (p4est, 17, NULL, NULL); p4est_reset_data (p4est, 8, init_fn, NULL); /* checksum and partition */ crc = p4est_checksum (p4est); p4est_partition (p4est, 0, NULL); SC_CHECK_ABORT (p4est_checksum (p4est) == crc, "Partition"); SC_CHECK_ABORT (p4est_is_balanced (p4est, P4EST_CONNECT_FULL), "Balance 4"); /* check reset data function */ p4est_reset_data (p4est, 3, NULL, NULL); p4est_reset_data (p4est, 3, NULL, NULL); /* checksum and rebalance */ crc = p4est_checksum (p4est); p4est_balance (p4est, P4EST_CONNECT_FULL, NULL); SC_CHECK_ABORT (p4est_checksum (p4est) == crc, "Rebalance"); /* clean up and exit */ P4EST_ASSERT (p4est->user_data_pool->elem_count == (size_t) p4est->local_num_quadrants); p4est_destroy (p4est); p4est_connectivity_destroy (connectivity); sc_finalize (); mpiret = sc_MPI_Finalize (); SC_CHECK_MPI (mpiret); return 0; }
/* main */ int main (int argc, char **argv) { int rank, num_procs, mpiret, i; sc_MPI_Comm mpicomm = sc_MPI_COMM_WORLD; p4est_t *p4est_1tree, *p4est_ntrees; p4est_connectivity_t *connectivity_1tree, *connectivity_ntrees; /* initialize MPI and p4est internals */ mpiret = sc_MPI_Init (&argc, &argv); SC_CHECK_MPI (mpiret); mpiret = sc_MPI_Comm_size (mpicomm, &num_procs); SC_CHECK_MPI (mpiret); mpiret = sc_MPI_Comm_rank (mpicomm, &rank); SC_CHECK_MPI (mpiret); sc_init (mpicomm, 1, 1, NULL, SC_LP_DEFAULT); p4est_init (NULL, SC_LP_DEFAULT); /* create connectivity */ #ifdef P4_TO_P8 connectivity_1tree = p8est_connectivity_new_unitcube (); connectivity_ntrees = p8est_connectivity_new_twocubes (); #else connectivity_1tree = p4est_connectivity_new_unitsquare (); connectivity_ntrees = p4est_connectivity_new_corner (); #endif /* create p4est structure */ p4est_1tree = p4est_new_ext (mpicomm, connectivity_1tree, 15, 0, 0, sizeof (user_data_t), init_fn, NULL); p4est_ntrees = p4est_new_ext (mpicomm, connectivity_ntrees, 15, 0, 0, sizeof (user_data_t), init_fn, NULL); /* write output: new */ p4est_vtk_write_file (p4est_1tree, NULL, P4EST_STRING "_partition_corr_1tree_new"); p4est_vtk_write_file (p4est_ntrees, NULL, P4EST_STRING "_partition_corr_ntrees_new"); /* refine */ p4est_refine (p4est_1tree, 1, refine_fn, init_fn); p4est_refine (p4est_ntrees, 1, refine_fn, init_fn); /* write output: refined */ p4est_vtk_write_file (p4est_1tree, NULL, P4EST_STRING "_partition_corr_1tree_refined"); p4est_vtk_write_file (p4est_ntrees, NULL, P4EST_STRING "_partition_corr_ntrees_refined"); /* run partition and coarsen till one quadrant per tree remains */ i = 0; while (p4est_1tree->global_num_quadrants > 1 && i <= P4EST_MAXLEVEL) { (void) p4est_partition_ext (p4est_1tree, 1, NULL); p4est_coarsen (p4est_1tree, 0, coarsen_fn, init_fn); i++; } SC_CHECK_ABORT (p4est_1tree->global_num_quadrants == 1, "coarsest forest with one tree was not achieved"); i = 0; while (p4est_ntrees->global_num_quadrants > connectivity_ntrees->num_trees && i <= P4EST_MAXLEVEL) { (void) p4est_partition_ext (p4est_ntrees, 1, NULL); p4est_coarsen (p4est_ntrees, 0, coarsen_fn, init_fn); i++; } SC_CHECK_ABORT (p4est_ntrees->global_num_quadrants == connectivity_ntrees->num_trees, "coarsest forest with multiple trees was not achieved"); /* run partition on coarse forest (one quadrant per tree) once again */ (void) p4est_partition_ext (p4est_1tree, 1, NULL); (void) p4est_partition_ext (p4est_ntrees, 1, NULL); /* write output: coarsened */ p4est_vtk_write_file (p4est_1tree, NULL, P4EST_STRING "_partition_corr_1tree_coarsened"); p4est_vtk_write_file (p4est_ntrees, NULL, P4EST_STRING "_partition_corr_ntrees_coarsened"); /* destroy the p4est and its connectivity structure */ p4est_destroy (p4est_1tree); p4est_destroy (p4est_ntrees); p4est_connectivity_destroy (connectivity_1tree); p4est_connectivity_destroy (connectivity_ntrees); /* clean up and exit */ sc_finalize (); mpiret = sc_MPI_Finalize (); SC_CHECK_MPI (mpiret); return 0; }