Actual source code: ex2.c
petsc-3.5.4 2015-05-23
2: static char help[] = "Tests various 1-dimensional DMDA routines.\n\n";
4: #include <petscdm.h>
5: #include <petscdmda.h>
6: #include <petscdraw.h>
10: int main(int argc,char **argv)
11: {
12: PetscMPIInt rank;
13: PetscInt M = 13,s=1,dof=1;
14: DMBoundaryType bx = DM_BOUNDARY_PERIODIC;
15: PetscErrorCode ierr;
16: DM da;
17: PetscViewer viewer;
18: Vec local,global;
19: PetscScalar value;
20: PetscDraw draw;
21: PetscBool flg = PETSC_FALSE;
23: PetscInitialize(&argc,&argv,(char*)0,help);
25: /* Create viewers */
26: PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",280,480,600,200,&viewer);
27: PetscViewerDrawGetDraw(viewer,0,&draw);
28: PetscDrawSetDoubleBuffer(draw);
30: /* Readoptions */
31: PetscOptionsGetInt(NULL,"-M",&M,NULL);
32: PetscOptionsGetEnum(NULL,"-wrap",DMBoundaryTypes,(PetscEnum*)&bx,NULL);
33: PetscOptionsGetInt(NULL,"-dof",&dof,NULL);
34: PetscOptionsGetInt(NULL,"-s",&s,NULL);
36: /* Create distributed array and get vectors */
37: DMDACreate1d(PETSC_COMM_WORLD,bx,M,dof,s,NULL,&da);
38: DMView(da,viewer);
39: DMCreateGlobalVector(da,&global);
40: DMCreateLocalVector(da,&local);
42: /* Set global vector; send ghost points to local vectors */
43: value = 1;
44: VecSet(global,value);
45: DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
46: DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);
48: /* Scale local vectors according to processor rank; pass to global vector */
49: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
50: value = rank+1;
51: VecScale(local,value);
52: DMLocalToGlobalBegin(da,local,INSERT_VALUES,global);
53: DMLocalToGlobalEnd(da,local,INSERT_VALUES,global);
55: VecView(global,viewer);
56: PetscPrintf(PETSC_COMM_WORLD,"\nGlobal Vector:\n");
57: VecView(global,PETSC_VIEWER_STDOUT_WORLD);
58: PetscPrintf(PETSC_COMM_WORLD,"\n");
60: /* Send ghost points to local vectors */
61: DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
62: DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);
64: PetscOptionsGetBool(NULL,"-local_print",&flg,NULL);
65: if (flg) {
66: PetscViewer sviewer;
67: ISLocalToGlobalMapping is;
69: PetscViewerASCIISynchronizedAllow(PETSC_VIEWER_STDOUT_WORLD,PETSC_TRUE);
70: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"\nLocal Vector: processor %d\n",rank);
71: PetscViewerGetSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
72: VecView(local,sviewer);
73: PetscViewerRestoreSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
74: PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);
76: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"\nLocal to global mapping: processor %d\n",rank);
77: PetscViewerGetSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
78: DMGetLocalToGlobalMapping(da,&is);
79: ISLocalToGlobalMappingView(is,sviewer);
80: PetscViewerRestoreSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
81: PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);
82: }
84: /* Free memory */
85: PetscViewerDestroy(&viewer);
86: VecDestroy(&global);
87: VecDestroy(&local);
88: DMDestroy(&da);
89: PetscFinalize();
90: return 0;
91: }