Actual source code: ex2.c
petsc-3.8.4 2018-03-24
2: static char help[] = "Tests various 1-dimensional DMDA routines.\n\n";
4: #include <petscdm.h>
5: #include <petscdmda.h>
6: #include <petscdraw.h>
8: int main(int argc,char **argv)
9: {
10: PetscMPIInt rank;
11: PetscInt M = 13,s=1,dof=1;
12: DMBoundaryType bx = DM_BOUNDARY_PERIODIC;
13: PetscErrorCode ierr;
14: DM da;
15: PetscViewer viewer;
16: Vec local,global;
17: PetscScalar value;
18: PetscDraw draw;
19: PetscBool flg = PETSC_FALSE;
20: ISLocalToGlobalMapping is;
22: PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
23: PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",280,480,600,200,&viewer);
24: PetscViewerDrawGetDraw(viewer,0,&draw);
25: PetscDrawSetDoubleBuffer(draw);
27: /* Readoptions */
28: PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL);
29: PetscOptionsGetEnum(NULL,NULL,"-wrap",DMBoundaryTypes,(PetscEnum*)&bx,NULL);
30: PetscOptionsGetInt(NULL,NULL,"-dof",&dof,NULL);
31: PetscOptionsGetInt(NULL,NULL,"-s",&s,NULL);
33: /* Create distributed array and get vectors */
34: DMDACreate1d(PETSC_COMM_WORLD,bx,M,dof,s,NULL,&da);
35: DMSetFromOptions(da);
36: DMSetUp(da);
37: DMView(da,viewer);
38: DMCreateGlobalVector(da,&global);
39: DMCreateLocalVector(da,&local);
41: value = 1;
42: VecSet(global,value);
44: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
45: value = rank+1;
46: VecScale(global,value);
48: VecView(global,viewer);
49: PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nGlobal Vector:\n");
50: VecView(global,PETSC_VIEWER_STDOUT_WORLD);
51: PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\n");
53: /* Send ghost points to local vectors */
54: DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
55: DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);
57: PetscOptionsGetBool(NULL,NULL,"-local_print",&flg,NULL);
58: if (flg) {
59: PetscViewer sviewer;
61: PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD);
62: PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nLocal Vector: processor %d\n",rank);
63: PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
64: VecView(local,sviewer);
65: PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
66: PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
67: PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD);
68: }
69: PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nLocal to global mapping\n");
70: DMGetLocalToGlobalMapping(da,&is);
71: ISLocalToGlobalMappingView(is,PETSC_VIEWER_STDOUT_WORLD);
73: /* Free memory */
74: PetscViewerDestroy(&viewer);
75: VecDestroy(&global);
76: VecDestroy(&local);
77: DMDestroy(&da);
78: PetscFinalize();
79: return ierr;
80: }