Actual source code: ex11.c
petsc-3.3-p6 2013-02-11
2: static char help[] = "Tests various 2-dimensional DM routines.\n\n";
4: #include <petscdmda.h>
8: int main(int argc,char **argv)
9: {
10: PetscInt M = 5,N = 4,dof=1,s=1,bx=0,by=0,i,n,j,k,m,cnt;
12: DM da;
13: PetscViewer viewer;
14: Vec local,locala,global,coors;
15: PetscScalar *xy,*alocal;
16: PetscDraw draw;
17: char fname[16];
19: PetscInitialize(&argc,&argv,(char*)0,help);
21: /* Create viewers */
22: PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",PETSC_DECIDE,PETSC_DECIDE,600,200,&viewer);
23: PetscViewerDrawGetDraw(viewer,0,&draw);
24: PetscDrawSetDoubleBuffer(draw);
26: /* Read options */
27: PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);
28: PetscOptionsGetInt(PETSC_NULL,"-N",&N,PETSC_NULL);
29: PetscOptionsGetInt(PETSC_NULL,"-dof",&dof,PETSC_NULL);
30: PetscOptionsGetInt(PETSC_NULL,"-s",&s,PETSC_NULL);
31: PetscOptionsGetInt(PETSC_NULL,"-periodic_x",&wrap,PETSC_NULL);
32: PetscOptionsGetInt(PETSC_NULL,"-periodic_y",&wrap,PETSC_NULL);
34: /* Create distributed array and get vectors */
35: DMDACreate2d(PETSC_COMM_WORLD,(DMDABoundaryType)bx,(DMDABoundaryType)by,DMDA_STENCIL_BOX,M,N,PETSC_DECIDE,
36: PETSC_DECIDE,dof,s,PETSC_NULL,PETSC_NULL,&da);
37: DMDASetUniformCoordinates(da,0.0,1.0,0.0,1.0,0.0,0.0);
38: for (i=0; i<dof; i++) {
39: sprintf(fname,"Field %d",(int)i);
40: DMDASetFieldName(da,i,fname);
41: }
43: DMView(da,viewer);
44: DMCreateGlobalVector(da,&global);
45: DMCreateLocalVector(da,&local);
46: DMCreateLocalVector(da,&locala);
47: DMDAGetCoordinates(da,&coors);
48: VecGetArray(coors,&xy);
50: VecView(coors,PETSC_VIEWER_STDOUT_SELF);
52: /* Set values into local vectors */
53: VecGetArray(local,&alocal);
54: DMDAGetGhostCorners(da,0,0,0,&m,&n,0);
55: n = n/dof;
56: for (k=0; k<dof; k++) {
57: cnt = 0;
58: for (j=0; j<n; j++) {
59: for (i=0; i<m; i++) {
60: alocal[k+dof*cnt] = PetscSinScalar(2.0*PETSC_PI*(k+1)*xy[2*cnt]);
61: cnt++;
62: }
63: }
64: }
65: VecRestoreArray(local,&alocal);
66: VecRestoreArray(coors,&xy);
68: DMLocalToGlobalBegin(da,local,INSERT_VALUES,global);
69: DMLocalToGlobalEnd(da,local,INSERT_VALUES,global);
71: VecView(global,viewer);
73: /* Send ghost points to local vectors */
74: DMGlobalToLocalBegin(da,global,INSERT_VALUES,locala);
75: DMGlobalToLocalEnd(da,global,INSERT_VALUES,locala);
77: /* Free memory */
78: PetscViewerDestroy(&viewer);
79: VecDestroy(&global);
80: VecDestroy(&local);
81: VecDestroy(&locala);
82: DMDestroy(&da);
83: PetscFinalize();
84: return 0;
85: }
86: