xref: /petsc/src/dm/impls/plex/tests/ex17.c (revision 8fb5bd83c3955fefcf33a54e3bb66920a9fa884b)
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));
27*8fb5bd83SMatthew 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 
619566063dSJacob Faibussowitsch   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
629566063dSJacob Faibussowitsch   PetscCall(CreateMesh(PETSC_COMM_WORLD, &dm));
639566063dSJacob Faibussowitsch   PetscCall(TestLocation(dm));
649566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
659566063dSJacob Faibussowitsch   PetscCall(PetscFinalize());
66b122ec5aSJacob Faibussowitsch   return 0;
67c4762a1bSJed Brown }
68c4762a1bSJed Brown 
69c4762a1bSJed Brown /*TEST
70c4762a1bSJed Brown 
71b26b5bf9SMatthew G. Knepley   testset:
72b26b5bf9SMatthew G. Knepley     args: -dm_plex_dim 1 -dm_plex_box_faces 10
73b26b5bf9SMatthew G. Knepley 
74c4762a1bSJed Brown     test:
751af33867SMatthew G. Knepley       suffix: seg
76b26b5bf9SMatthew G. Knepley 
77b26b5bf9SMatthew G. Knepley     test:
78b26b5bf9SMatthew G. Knepley       suffix: seg_hash
79b26b5bf9SMatthew G. Knepley       args: -dm_refine 2 -dm_plex_hash_location
801af33867SMatthew G. Knepley 
813285882aSMatthew G. Knepley   testset:
823285882aSMatthew G. Knepley     args: -dm_plex_box_faces 5,5
833285882aSMatthew G. Knepley 
841af33867SMatthew G. Knepley     test:
851af33867SMatthew G. Knepley       suffix: tri
86c4762a1bSJed Brown       requires: triangle
873285882aSMatthew G. Knepley 
883285882aSMatthew G. Knepley     test:
893285882aSMatthew G. Knepley       suffix: tri_hash
903285882aSMatthew G. Knepley       requires: triangle
913285882aSMatthew G. Knepley       args: -dm_refine 2 -dm_plex_hash_location
921af33867SMatthew G. Knepley 
931af33867SMatthew G. Knepley     test:
941af33867SMatthew G. Knepley       suffix: quad
953285882aSMatthew G. Knepley       args: -dm_plex_simplex 0
963285882aSMatthew G. Knepley 
973285882aSMatthew G. Knepley     test:
983285882aSMatthew G. Knepley       suffix: quad_hash
993285882aSMatthew G. Knepley       args: -dm_plex_simplex 0 -dm_refine 2 -dm_plex_hash_location
1003285882aSMatthew G. Knepley 
1013285882aSMatthew G. Knepley   testset:
1023285882aSMatthew G. Knepley     args: -dm_plex_dim 3 -dm_plex_box_faces 3,3,3
1031af33867SMatthew G. Knepley 
1041af33867SMatthew G. Knepley     test:
1051af33867SMatthew G. Knepley       suffix: tet
1061af33867SMatthew G. Knepley       requires: ctetgen
1073285882aSMatthew G. Knepley 
1083285882aSMatthew G. Knepley     test:
1093285882aSMatthew G. Knepley       suffix: tet_hash
1103285882aSMatthew G. Knepley       requires: ctetgen
1113285882aSMatthew G. Knepley       args: -dm_refine 1 -dm_plex_hash_location
1121af33867SMatthew G. Knepley 
1131af33867SMatthew G. Knepley     test:
1141af33867SMatthew G. Knepley       suffix: hex
1153285882aSMatthew G. Knepley       args: -dm_plex_simplex 0
1163285882aSMatthew G. Knepley 
1173285882aSMatthew G. Knepley     test:
1183285882aSMatthew G. Knepley       suffix: hex_hash
1193285882aSMatthew G. Knepley       args: -dm_plex_simplex 0 -dm_refine 1 -dm_plex_hash_location
120c4762a1bSJed Brown 
121c4762a1bSJed Brown TEST*/
122