xref: /petsc/src/dm/impls/plex/tests/ex17.c (revision 327415f76d85372a4417cf1aaa14db707d4d6c04)
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));
278fb5bd83SMatthew G. Knepley   PetscCall(DMGetCoordinatesLocalSetUp(dm));
289566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
29c4762a1bSJed Brown   /* Locate all centroids */
309566063dSJacob Faibussowitsch   PetscCall(VecCreateSeq(PETSC_COMM_SELF, (cEnd - cStart)*cdim, &points));
319566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(points, cdim));
329566063dSJacob Faibussowitsch   PetscCall(VecGetArray(points, &a));
33c4762a1bSJed Brown   for (c = cStart; c < cEnd; ++c) {
34c4762a1bSJed Brown     PetscReal          centroid[3];
353285882aSMatthew G. Knepley     PetscInt           off = (c - cStart)*cdim, d;
36c4762a1bSJed Brown 
379566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dm, c, NULL, centroid, NULL));
383285882aSMatthew G. Knepley     for (d = 0; d < cdim; ++d) a[off+d] = centroid[d];
39c4762a1bSJed Brown   }
409566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(points, &a));
419566063dSJacob Faibussowitsch   PetscCall(DMLocatePoints(dm, points, DM_POINTLOCATION_NONE, &cellSF));
429566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&points));
439566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(cellSF, NULL, &n, NULL, &cells));
443285882aSMatthew G. Knepley   if (n != (cEnd - cStart)) {
453285882aSMatthew G. Knepley     for (c = 0; c < n; ++c) {
4663a3b9bcSJacob 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));
473285882aSMatthew G. Knepley     }
4863a3b9bcSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Located %" PetscInt_FMT " points instead of %" PetscInt_FMT, n, cEnd - cStart);
493285882aSMatthew G. Knepley   }
503285882aSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
5163a3b9bcSJacob 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);
523285882aSMatthew G. Knepley   }
539566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&cellSF));
54c4762a1bSJed Brown   PetscFunctionReturn(0);
55c4762a1bSJed Brown }
56c4762a1bSJed Brown 
57c4762a1bSJed Brown int main(int argc, char **argv)
58c4762a1bSJed Brown {
59c4762a1bSJed Brown   DM             dm;
60c4762a1bSJed Brown 
61*327415f7SBarry Smith   PetscFunctionBeginUser;
629566063dSJacob Faibussowitsch   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
639566063dSJacob Faibussowitsch   PetscCall(CreateMesh(PETSC_COMM_WORLD, &dm));
649566063dSJacob Faibussowitsch   PetscCall(TestLocation(dm));
659566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
669566063dSJacob Faibussowitsch   PetscCall(PetscFinalize());
67b122ec5aSJacob Faibussowitsch   return 0;
68c4762a1bSJed Brown }
69c4762a1bSJed Brown 
70c4762a1bSJed Brown /*TEST
71c4762a1bSJed Brown 
72b26b5bf9SMatthew G. Knepley   testset:
73b26b5bf9SMatthew G. Knepley     args: -dm_plex_dim 1 -dm_plex_box_faces 10
74b26b5bf9SMatthew G. Knepley 
75c4762a1bSJed Brown     test:
761af33867SMatthew G. Knepley       suffix: seg
77b26b5bf9SMatthew G. Knepley 
78b26b5bf9SMatthew G. Knepley     test:
79b26b5bf9SMatthew G. Knepley       suffix: seg_hash
80b26b5bf9SMatthew G. Knepley       args: -dm_refine 2 -dm_plex_hash_location
811af33867SMatthew G. Knepley 
823285882aSMatthew G. Knepley   testset:
833285882aSMatthew G. Knepley     args: -dm_plex_box_faces 5,5
843285882aSMatthew G. Knepley 
851af33867SMatthew G. Knepley     test:
861af33867SMatthew G. Knepley       suffix: tri
87c4762a1bSJed Brown       requires: triangle
883285882aSMatthew G. Knepley 
893285882aSMatthew G. Knepley     test:
903285882aSMatthew G. Knepley       suffix: tri_hash
913285882aSMatthew G. Knepley       requires: triangle
923285882aSMatthew G. Knepley       args: -dm_refine 2 -dm_plex_hash_location
931af33867SMatthew G. Knepley 
941af33867SMatthew G. Knepley     test:
951af33867SMatthew G. Knepley       suffix: quad
963285882aSMatthew G. Knepley       args: -dm_plex_simplex 0
973285882aSMatthew G. Knepley 
983285882aSMatthew G. Knepley     test:
993285882aSMatthew G. Knepley       suffix: quad_hash
1003285882aSMatthew G. Knepley       args: -dm_plex_simplex 0 -dm_refine 2 -dm_plex_hash_location
1013285882aSMatthew G. Knepley 
1023285882aSMatthew G. Knepley   testset:
1033285882aSMatthew G. Knepley     args: -dm_plex_dim 3 -dm_plex_box_faces 3,3,3
1041af33867SMatthew G. Knepley 
1051af33867SMatthew G. Knepley     test:
1061af33867SMatthew G. Knepley       suffix: tet
1071af33867SMatthew G. Knepley       requires: ctetgen
1083285882aSMatthew G. Knepley 
1093285882aSMatthew G. Knepley     test:
1103285882aSMatthew G. Knepley       suffix: tet_hash
1113285882aSMatthew G. Knepley       requires: ctetgen
1123285882aSMatthew G. Knepley       args: -dm_refine 1 -dm_plex_hash_location
1131af33867SMatthew G. Knepley 
1141af33867SMatthew G. Knepley     test:
1151af33867SMatthew G. Knepley       suffix: hex
1163285882aSMatthew G. Knepley       args: -dm_plex_simplex 0
1173285882aSMatthew G. Knepley 
1183285882aSMatthew G. Knepley     test:
1193285882aSMatthew G. Knepley       suffix: hex_hash
1203285882aSMatthew G. Knepley       args: -dm_plex_simplex 0 -dm_refine 1 -dm_plex_hash_location
121c4762a1bSJed Brown 
122c4762a1bSJed Brown TEST*/
123