1 static char help[] = "Tests various 1-dimensional DMDA routines.\n\n"; 2 3 #include <petscdm.h> 4 #include <petscdmda.h> 5 #include <petscdraw.h> 6 7 int main(int argc, char **argv) 8 { 9 PetscMPIInt rank; 10 PetscInt M = 13, s = 1, dof = 1, n; 11 DMBoundaryType bx = DM_BOUNDARY_PERIODIC; 12 DM da; 13 PetscViewer viewer; 14 Vec local, global; 15 PetscScalar value, *array; 16 PetscDraw draw; 17 PetscBool flg = PETSC_FALSE; 18 ISLocalToGlobalMapping is; 19 20 PetscFunctionBeginUser; 21 PetscCall(PetscInitialize(&argc, &argv, (char *)0, help)); 22 PetscCall(PetscViewerDrawOpen(PETSC_COMM_WORLD, 0, "", 280, 480, 600, 200, &viewer)); 23 PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw)); 24 PetscCall(PetscDrawSetDoubleBuffer(draw)); 25 26 /* Readoptions */ 27 PetscCall(PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL)); 28 PetscCall(PetscOptionsGetEnum(NULL, NULL, "-wrap", DMBoundaryTypes, (PetscEnum *)&bx, NULL)); 29 PetscCall(PetscOptionsGetInt(NULL, NULL, "-dof", &dof, NULL)); 30 PetscCall(PetscOptionsGetInt(NULL, NULL, "-s", &s, NULL)); 31 32 /* Create distributed array and get vectors */ 33 PetscCall(DMDACreate1d(PETSC_COMM_WORLD, bx, M, dof, s, NULL, &da)); 34 PetscCall(DMSetFromOptions(da)); 35 PetscCall(DMSetUp(da)); 36 PetscCall(DMView(da, viewer)); 37 PetscCall(DMCreateGlobalVector(da, &global)); 38 PetscCall(DMCreateLocalVector(da, &local)); 39 40 value = 1; 41 PetscCall(VecSet(global, value)); 42 43 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank)); 44 value = rank + 1; 45 PetscCall(VecGetLocalSize(global, &n)); 46 PetscCall(VecGetArray(global, &array)); 47 for (PetscInt i = 0; i < n; i++) array[i] *= value; 48 PetscCall(VecRestoreArray(global, &array)); 49 50 PetscCall(VecView(global, viewer)); 51 PetscCall(PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD, "\nGlobal Vector:\n")); 52 PetscCall(VecView(global, PETSC_VIEWER_STDOUT_WORLD)); 53 PetscCall(PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD, "\n")); 54 55 /* Send ghost points to local vectors */ 56 PetscCall(DMGlobalToLocalBegin(da, global, INSERT_VALUES, local)); 57 PetscCall(DMGlobalToLocalEnd(da, global, INSERT_VALUES, local)); 58 59 PetscCall(PetscOptionsGetBool(NULL, NULL, "-local_print", &flg, NULL)); 60 if (flg) { 61 PetscViewer sviewer; 62 63 PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD)); 64 PetscCall(PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, &sviewer)); 65 PetscCall(PetscViewerASCIIPrintf(sviewer, "\nLocal Vector: processor %d\n", rank)); 66 PetscCall(VecView(local, sviewer)); 67 PetscCall(PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, &sviewer)); 68 PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD)); 69 } 70 PetscCall(PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD, "\nLocal to global mapping\n")); 71 PetscCall(DMGetLocalToGlobalMapping(da, &is)); 72 PetscCall(ISLocalToGlobalMappingView(is, PETSC_VIEWER_STDOUT_WORLD)); 73 74 /* Free memory */ 75 PetscCall(PetscViewerDestroy(&viewer)); 76 PetscCall(VecDestroy(&global)); 77 PetscCall(VecDestroy(&local)); 78 PetscCall(DMDestroy(&da)); 79 PetscCall(PetscFinalize()); 80 return 0; 81 } 82 83 /*TEST 84 85 test: 86 nsize: 2 87 args: -nox 88 filter: grep -v " MPI process" 89 output_file: output/ex2_1.out 90 requires: x 91 92 test: 93 suffix: 2 94 nsize: 3 95 args: -wrap none -local_print -nox 96 filter: grep -v "Vec Object: Vec" 97 requires: x 98 99 test: 100 suffix: 3 101 nsize: 3 102 args: -wrap ghosted -local_print -nox 103 filter: grep -v "Vec Object: Vec" 104 requires: x 105 106 TEST*/ 107