1 static char help[] = "Tests for point location\n\n"; 2 3 #include <petscsf.h> 4 #include <petscdmplex.h> 5 6 static PetscErrorCode CreateMesh(MPI_Comm comm, DM *dm) 7 { 8 PetscErrorCode ierr; 9 10 PetscFunctionBeginUser; 11 ierr = DMCreate(comm, dm);CHKERRQ(ierr); 12 ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr); 13 ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); 14 ierr = DMViewFromOptions(*dm, NULL, "-dm_view");CHKERRQ(ierr); 15 PetscFunctionReturn(0); 16 } 17 18 static PetscErrorCode TestLocation(DM dm) 19 { 20 Vec points; 21 PetscSF cellSF = NULL; 22 const PetscSFNode *cells; 23 PetscScalar *a; 24 PetscInt cdim, n; 25 PetscInt cStart, cEnd, c; 26 PetscErrorCode ierr; 27 28 PetscFunctionBeginUser; 29 ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr); 30 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 31 /* Locate all centroids */ 32 ierr = VecCreateSeq(PETSC_COMM_SELF, (cEnd - cStart)*cdim, &points);CHKERRQ(ierr); 33 ierr = VecSetBlockSize(points, cdim);CHKERRQ(ierr); 34 ierr = VecGetArray(points, &a);CHKERRQ(ierr); 35 for (c = cStart; c < cEnd; ++c) { 36 PetscReal centroid[3]; 37 PetscInt off = (c - cStart)*cdim, d; 38 39 ierr = DMPlexComputeCellGeometryFVM(dm, c, NULL, centroid, NULL);CHKERRQ(ierr); 40 for (d = 0; d < cdim; ++d) a[off+d] = centroid[d]; 41 } 42 ierr = VecRestoreArray(points, &a);CHKERRQ(ierr); 43 ierr = DMLocatePoints(dm, points, DM_POINTLOCATION_NONE, &cellSF);CHKERRQ(ierr); 44 ierr = VecDestroy(&points);CHKERRQ(ierr); 45 ierr = PetscSFGetGraph(cellSF, NULL, &n, NULL, &cells);CHKERRQ(ierr); 46 if (n != (cEnd - cStart)) { 47 for (c = 0; c < n; ++c) { 48 if (cells[c].index != c+cStart) {ierr = PetscPrintf(PETSC_COMM_SELF, "Could not locate centroid of cell %D, error %D\n", c+cStart, cells[c].index);CHKERRQ(ierr);} 49 } 50 SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Located %D points instead of %D", n, cEnd - cStart); 51 } 52 for (c = cStart; c < cEnd; ++c) { 53 if (cells[c - cStart].index != c) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not locate centroid of cell %D, instead found %D", c, cells[c - cStart].index); 54 } 55 ierr = PetscSFDestroy(&cellSF);CHKERRQ(ierr); 56 PetscFunctionReturn(0); 57 } 58 59 int main(int argc, char **argv) 60 { 61 DM dm; 62 PetscErrorCode ierr; 63 64 ierr = PetscInitialize(&argc, &argv, NULL, help);if (ierr) return ierr; 65 ierr = CreateMesh(PETSC_COMM_WORLD, &dm);CHKERRQ(ierr); 66 ierr = TestLocation(dm);CHKERRQ(ierr); 67 ierr = DMDestroy(&dm);CHKERRQ(ierr); 68 ierr = PetscFinalize(); 69 return ierr; 70 } 71 72 /*TEST 73 74 test: 75 suffix: seg 76 args: -dm_plex_dim 1 -dm_plex_box_faces 10 77 78 testset: 79 args: -dm_plex_box_faces 5,5 80 81 test: 82 suffix: tri 83 requires: triangle 84 85 test: 86 suffix: tri_hash 87 requires: triangle 88 args: -dm_refine 2 -dm_plex_hash_location 89 90 test: 91 suffix: quad 92 args: -dm_plex_simplex 0 93 94 test: 95 suffix: quad_hash 96 args: -dm_plex_simplex 0 -dm_refine 2 -dm_plex_hash_location 97 98 testset: 99 args: -dm_plex_dim 3 -dm_plex_box_faces 3,3,3 100 101 test: 102 suffix: tet 103 requires: ctetgen 104 105 test: 106 suffix: tet_hash 107 requires: ctetgen 108 args: -dm_refine 1 -dm_plex_hash_location 109 110 test: 111 suffix: hex 112 args: -dm_plex_simplex 0 113 114 test: 115 suffix: hex_hash 116 args: -dm_plex_simplex 0 -dm_refine 1 -dm_plex_hash_location 117 118 TEST*/ 119