xref: /petsc/src/dm/impls/plex/tests/ex17.c (revision 63a3b9bc7a1f24f247904ccba9383635fe6abade)
1c4762a1bSJed Brown static char help[] = "Tests for point location\n\n";
2c4762a1bSJed Brown 
3c4762a1bSJed Brown #include <petscsf.h>
4c4762a1bSJed Brown #include <petscdmplex.h>
5c4762a1bSJed Brown 
630602db0SMatthew G. Knepley static PetscErrorCode CreateMesh(MPI_Comm comm, DM *dm)
7c4762a1bSJed Brown {
8c4762a1bSJed Brown   PetscFunctionBeginUser;
99566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
109566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
119566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(*dm));
129566063dSJacob Faibussowitsch   PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view"));
13c4762a1bSJed Brown   PetscFunctionReturn(0);
14c4762a1bSJed Brown }
15c4762a1bSJed Brown 
1630602db0SMatthew G. Knepley static PetscErrorCode TestLocation(DM dm)
17c4762a1bSJed Brown {
183285882aSMatthew G. Knepley   Vec                points;
193285882aSMatthew G. Knepley   PetscSF            cellSF = NULL;
203285882aSMatthew G. Knepley   const PetscSFNode *cells;
213285882aSMatthew G. Knepley   PetscScalar       *a;
223285882aSMatthew G. Knepley   PetscInt           cdim, n;
23c4762a1bSJed Brown   PetscInt           cStart, cEnd, c;
24c4762a1bSJed Brown 
25c4762a1bSJed Brown   PetscFunctionBeginUser;
269566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
279566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
28c4762a1bSJed Brown   /* Locate all centroids */
299566063dSJacob Faibussowitsch   PetscCall(VecCreateSeq(PETSC_COMM_SELF, (cEnd - cStart)*cdim, &points));
309566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(points, cdim));
319566063dSJacob Faibussowitsch   PetscCall(VecGetArray(points, &a));
32c4762a1bSJed Brown   for (c = cStart; c < cEnd; ++c) {
33c4762a1bSJed Brown     PetscReal          centroid[3];
343285882aSMatthew G. Knepley     PetscInt           off = (c - cStart)*cdim, d;
35c4762a1bSJed Brown 
369566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dm, c, NULL, centroid, NULL));
373285882aSMatthew G. Knepley     for (d = 0; d < cdim; ++d) a[off+d] = centroid[d];
38c4762a1bSJed Brown   }
399566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(points, &a));
409566063dSJacob Faibussowitsch   PetscCall(DMLocatePoints(dm, points, DM_POINTLOCATION_NONE, &cellSF));
419566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&points));
429566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(cellSF, NULL, &n, NULL, &cells));
433285882aSMatthew G. Knepley   if (n != (cEnd - cStart)) {
443285882aSMatthew G. Knepley     for (c = 0; c < n; ++c) {
45*63a3b9bcSJacob Faibussowitsch       if (cells[c].index != c+cStart) PetscCall(PetscPrintf(PETSC_COMM_SELF, "Could not locate centroid of cell %" PetscInt_FMT ", error %" PetscInt_FMT "\n", c+cStart, cells[c].index));
463285882aSMatthew G. Knepley     }
47*63a3b9bcSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Located %" PetscInt_FMT " points instead of %" PetscInt_FMT, n, cEnd - cStart);
483285882aSMatthew G. Knepley   }
493285882aSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
50*63a3b9bcSJacob Faibussowitsch     PetscCheck(cells[c - cStart].index == c,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not locate centroid of cell %" PetscInt_FMT ", instead found %" PetscInt_FMT, c, cells[c - cStart].index);
513285882aSMatthew G. Knepley   }
529566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&cellSF));
53c4762a1bSJed Brown   PetscFunctionReturn(0);
54c4762a1bSJed Brown }
55c4762a1bSJed Brown 
56c4762a1bSJed Brown int main(int argc, char **argv)
57c4762a1bSJed Brown {
58c4762a1bSJed Brown   DM             dm;
59c4762a1bSJed Brown 
609566063dSJacob Faibussowitsch   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
619566063dSJacob Faibussowitsch   PetscCall(CreateMesh(PETSC_COMM_WORLD, &dm));
629566063dSJacob Faibussowitsch   PetscCall(TestLocation(dm));
639566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
649566063dSJacob Faibussowitsch   PetscCall(PetscFinalize());
65b122ec5aSJacob Faibussowitsch   return 0;
66c4762a1bSJed Brown }
67c4762a1bSJed Brown 
68c4762a1bSJed Brown /*TEST
69c4762a1bSJed Brown 
70b26b5bf9SMatthew G. Knepley   testset:
71b26b5bf9SMatthew G. Knepley     args: -dm_plex_dim 1 -dm_plex_box_faces 10
72b26b5bf9SMatthew G. Knepley 
73c4762a1bSJed Brown     test:
741af33867SMatthew G. Knepley       suffix: seg
75b26b5bf9SMatthew G. Knepley 
76b26b5bf9SMatthew G. Knepley     test:
77b26b5bf9SMatthew G. Knepley       suffix: seg_hash
78b26b5bf9SMatthew G. Knepley       args: -dm_refine 2 -dm_plex_hash_location
791af33867SMatthew G. Knepley 
803285882aSMatthew G. Knepley   testset:
813285882aSMatthew G. Knepley     args: -dm_plex_box_faces 5,5
823285882aSMatthew G. Knepley 
831af33867SMatthew G. Knepley     test:
841af33867SMatthew G. Knepley       suffix: tri
85c4762a1bSJed Brown       requires: triangle
863285882aSMatthew G. Knepley 
873285882aSMatthew G. Knepley     test:
883285882aSMatthew G. Knepley       suffix: tri_hash
893285882aSMatthew G. Knepley       requires: triangle
903285882aSMatthew G. Knepley       args: -dm_refine 2 -dm_plex_hash_location
911af33867SMatthew G. Knepley 
921af33867SMatthew G. Knepley     test:
931af33867SMatthew G. Knepley       suffix: quad
943285882aSMatthew G. Knepley       args: -dm_plex_simplex 0
953285882aSMatthew G. Knepley 
963285882aSMatthew G. Knepley     test:
973285882aSMatthew G. Knepley       suffix: quad_hash
983285882aSMatthew G. Knepley       args: -dm_plex_simplex 0 -dm_refine 2 -dm_plex_hash_location
993285882aSMatthew G. Knepley 
1003285882aSMatthew G. Knepley   testset:
1013285882aSMatthew G. Knepley     args: -dm_plex_dim 3 -dm_plex_box_faces 3,3,3
1021af33867SMatthew G. Knepley 
1031af33867SMatthew G. Knepley     test:
1041af33867SMatthew G. Knepley       suffix: tet
1051af33867SMatthew G. Knepley       requires: ctetgen
1063285882aSMatthew G. Knepley 
1073285882aSMatthew G. Knepley     test:
1083285882aSMatthew G. Knepley       suffix: tet_hash
1093285882aSMatthew G. Knepley       requires: ctetgen
1103285882aSMatthew G. Knepley       args: -dm_refine 1 -dm_plex_hash_location
1111af33867SMatthew G. Knepley 
1121af33867SMatthew G. Knepley     test:
1131af33867SMatthew G. Knepley       suffix: hex
1143285882aSMatthew G. Knepley       args: -dm_plex_simplex 0
1153285882aSMatthew G. Knepley 
1163285882aSMatthew G. Knepley     test:
1173285882aSMatthew G. Knepley       suffix: hex_hash
1183285882aSMatthew G. Knepley       args: -dm_plex_simplex 0 -dm_refine 1 -dm_plex_hash_location
119c4762a1bSJed Brown 
120c4762a1bSJed Brown TEST*/
121