xref: /petsc/src/dm/impls/plex/tests/ex17.c (revision 5f80ce2ab25dff0f4601e710601cbbcecf323266)
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;
9*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreate(comm, dm));
10*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetType(*dm, DMPLEX));
11*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetFromOptions(*dm));
12*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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;
26*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDim(dm, &cdim));
27*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
28c4762a1bSJed Brown   /* Locate all centroids */
29*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecCreateSeq(PETSC_COMM_SELF, (cEnd - cStart)*cdim, &points));
30*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecSetBlockSize(points, cdim));
31*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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 
36*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexComputeCellGeometryFVM(dm, c, NULL, centroid, NULL));
373285882aSMatthew G. Knepley     for (d = 0; d < cdim; ++d) a[off+d] = centroid[d];
38c4762a1bSJed Brown   }
39*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArray(points, &a));
40*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLocatePoints(dm, points, DM_POINTLOCATION_NONE, &cellSF));
41*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&points));
42*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFGetGraph(cellSF, NULL, &n, NULL, &cells));
433285882aSMatthew G. Knepley   if (n != (cEnd - cStart)) {
443285882aSMatthew G. Knepley     for (c = 0; c < n; ++c) {
45*5f80ce2aSJacob Faibussowitsch       if (cells[c].index != c+cStart) CHKERRQ(PetscPrintf(PETSC_COMM_SELF, "Could not locate centroid of cell %D, error %D\n", c+cStart, cells[c].index));
463285882aSMatthew G. Knepley     }
4798921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Located %D points instead of %D", n, cEnd - cStart);
483285882aSMatthew G. Knepley   }
493285882aSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
502c71b3e2SJacob Faibussowitsch     PetscCheckFalse(cells[c - cStart].index != c,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not locate centroid of cell %D, instead found %D", c, cells[c - cStart].index);
513285882aSMatthew G. Knepley   }
52*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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   PetscErrorCode ierr;
60c4762a1bSJed Brown 
61c4762a1bSJed Brown   ierr = PetscInitialize(&argc, &argv, NULL, help);if (ierr) return ierr;
62*5f80ce2aSJacob Faibussowitsch   CHKERRQ(CreateMesh(PETSC_COMM_WORLD, &dm));
63*5f80ce2aSJacob Faibussowitsch   CHKERRQ(TestLocation(dm));
64*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&dm));
65c4762a1bSJed Brown   ierr = PetscFinalize();
66c4762a1bSJed Brown   return ierr;
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