xref: /petsc/src/dm/impls/plex/tests/ex17.c (revision ad540459ab38c4a232710a68077e487eb6627fe2)
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 
69371c9d4SSatish Balay static PetscErrorCode CreateMesh(MPI_Comm comm, DM *dm) {
7c4762a1bSJed Brown   PetscFunctionBeginUser;
89566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
99566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
109566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(*dm));
119566063dSJacob Faibussowitsch   PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view"));
12c4762a1bSJed Brown   PetscFunctionReturn(0);
13c4762a1bSJed Brown }
14c4762a1bSJed Brown 
159371c9d4SSatish Balay static PetscErrorCode TestLocation(DM dm) {
163285882aSMatthew G. Knepley   Vec                points;
173285882aSMatthew G. Knepley   PetscSF            cellSF = NULL;
183285882aSMatthew G. Knepley   const PetscSFNode *cells;
193285882aSMatthew G. Knepley   PetscScalar       *a;
203285882aSMatthew G. Knepley   PetscInt           cdim, n;
21c4762a1bSJed Brown   PetscInt           cStart, cEnd, c;
22c4762a1bSJed Brown 
23c4762a1bSJed Brown   PetscFunctionBeginUser;
249566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
258fb5bd83SMatthew G. Knepley   PetscCall(DMGetCoordinatesLocalSetUp(dm));
269566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
27c4762a1bSJed Brown   /* Locate all centroids */
289566063dSJacob Faibussowitsch   PetscCall(VecCreateSeq(PETSC_COMM_SELF, (cEnd - cStart) * cdim, &points));
299566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(points, cdim));
309566063dSJacob Faibussowitsch   PetscCall(VecGetArray(points, &a));
31c4762a1bSJed Brown   for (c = cStart; c < cEnd; ++c) {
32c4762a1bSJed Brown     PetscReal centroid[3];
333285882aSMatthew G. Knepley     PetscInt  off = (c - cStart) * cdim, d;
34c4762a1bSJed Brown 
359566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeCellGeometryFVM(dm, c, NULL, centroid, NULL));
363285882aSMatthew G. Knepley     for (d = 0; d < cdim; ++d) a[off + d] = centroid[d];
37c4762a1bSJed Brown   }
389566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(points, &a));
399566063dSJacob Faibussowitsch   PetscCall(DMLocatePoints(dm, points, DM_POINTLOCATION_NONE, &cellSF));
409566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&points));
419566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(cellSF, NULL, &n, NULL, &cells));
423285882aSMatthew G. Knepley   if (n != (cEnd - cStart)) {
433285882aSMatthew G. Knepley     for (c = 0; c < n; ++c) {
4463a3b9bcSJacob 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));
453285882aSMatthew G. Knepley     }
4663a3b9bcSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Located %" PetscInt_FMT " points instead of %" PetscInt_FMT, n, cEnd - cStart);
473285882aSMatthew G. Knepley   }
48*ad540459SPierre Jolivet   for (c = cStart; c < cEnd; ++c) 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);
499566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&cellSF));
50c4762a1bSJed Brown   PetscFunctionReturn(0);
51c4762a1bSJed Brown }
52c4762a1bSJed Brown 
539371c9d4SSatish Balay int main(int argc, char **argv) {
54c4762a1bSJed Brown   DM dm;
55c4762a1bSJed Brown 
56327415f7SBarry Smith   PetscFunctionBeginUser;
579566063dSJacob Faibussowitsch   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
589566063dSJacob Faibussowitsch   PetscCall(CreateMesh(PETSC_COMM_WORLD, &dm));
599566063dSJacob Faibussowitsch   PetscCall(TestLocation(dm));
609566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
619566063dSJacob Faibussowitsch   PetscCall(PetscFinalize());
62b122ec5aSJacob Faibussowitsch   return 0;
63c4762a1bSJed Brown }
64c4762a1bSJed Brown 
65c4762a1bSJed Brown /*TEST
66c4762a1bSJed Brown 
67b26b5bf9SMatthew G. Knepley   testset:
68b26b5bf9SMatthew G. Knepley     args: -dm_plex_dim 1 -dm_plex_box_faces 10
69b26b5bf9SMatthew G. Knepley 
70c4762a1bSJed Brown     test:
711af33867SMatthew G. Knepley       suffix: seg
72b26b5bf9SMatthew G. Knepley 
73b26b5bf9SMatthew G. Knepley     test:
74b26b5bf9SMatthew G. Knepley       suffix: seg_hash
75b26b5bf9SMatthew G. Knepley       args: -dm_refine 2 -dm_plex_hash_location
761af33867SMatthew G. Knepley 
773285882aSMatthew G. Knepley   testset:
783285882aSMatthew G. Knepley     args: -dm_plex_box_faces 5,5
793285882aSMatthew G. Knepley 
801af33867SMatthew G. Knepley     test:
811af33867SMatthew G. Knepley       suffix: tri
82c4762a1bSJed Brown       requires: triangle
833285882aSMatthew G. Knepley 
843285882aSMatthew G. Knepley     test:
853285882aSMatthew G. Knepley       suffix: tri_hash
863285882aSMatthew G. Knepley       requires: triangle
873285882aSMatthew G. Knepley       args: -dm_refine 2 -dm_plex_hash_location
881af33867SMatthew G. Knepley 
891af33867SMatthew G. Knepley     test:
901af33867SMatthew G. Knepley       suffix: quad
913285882aSMatthew G. Knepley       args: -dm_plex_simplex 0
923285882aSMatthew G. Knepley 
933285882aSMatthew G. Knepley     test:
943285882aSMatthew G. Knepley       suffix: quad_hash
953285882aSMatthew G. Knepley       args: -dm_plex_simplex 0 -dm_refine 2 -dm_plex_hash_location
963285882aSMatthew G. Knepley 
973285882aSMatthew G. Knepley   testset:
983285882aSMatthew G. Knepley     args: -dm_plex_dim 3 -dm_plex_box_faces 3,3,3
991af33867SMatthew G. Knepley 
1001af33867SMatthew G. Knepley     test:
1011af33867SMatthew G. Knepley       suffix: tet
1021af33867SMatthew G. Knepley       requires: ctetgen
1033285882aSMatthew G. Knepley 
1043285882aSMatthew G. Knepley     test:
1053285882aSMatthew G. Knepley       suffix: tet_hash
1063285882aSMatthew G. Knepley       requires: ctetgen
1073285882aSMatthew G. Knepley       args: -dm_refine 1 -dm_plex_hash_location
1081af33867SMatthew G. Knepley 
1091af33867SMatthew G. Knepley     test:
1101af33867SMatthew G. Knepley       suffix: hex
1113285882aSMatthew G. Knepley       args: -dm_plex_simplex 0
1123285882aSMatthew G. Knepley 
1133285882aSMatthew G. Knepley     test:
1143285882aSMatthew G. Knepley       suffix: hex_hash
1153285882aSMatthew G. Knepley       args: -dm_plex_simplex 0 -dm_refine 1 -dm_plex_hash_location
116c4762a1bSJed Brown 
117c4762a1bSJed Brown TEST*/
118