Actual source code: ex10.c
2: static char help[] = "Tests various 1-dimensional DA routines.\n\n";
4: #include petscda.h
5: #include petscsys.h
9: int main(int argc,char **argv)
10: {
11: PetscInt M = 13,dof=1,s=1,wrap=0,i,n,j;
13: DA da;
14: PetscViewer viewer;
15: Vec local,locala,global,coors;
16: PetscScalar *x,*alocal;
17: PetscDraw draw;
18: char fname[16];
20: PetscInitialize(&argc,&argv,(char*)0,help);
22: /* Create viewers */
23: PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",PETSC_DECIDE,PETSC_DECIDE,600,200,&viewer);
24: PetscViewerDrawGetDraw(viewer,0,&draw);
25: PetscDrawSetDoubleBuffer(draw);
27: /* Read options */
28: PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);
29: PetscOptionsGetInt(PETSC_NULL,"-dof",&dof,PETSC_NULL);
30: PetscOptionsGetInt(PETSC_NULL,"-s",&s,PETSC_NULL);
31: PetscOptionsGetInt(PETSC_NULL,"-periodic",&wrap,PETSC_NULL);
33: /* Create distributed array and get vectors */
34: DACreate1d(PETSC_COMM_WORLD,(DAPeriodicType)wrap,M,dof,s,PETSC_NULL,&da);
35: DASetUniformCoordinates(da,0.0,1.0,0.0,0.0,0.0,0.0);
36: for (i=0; i<dof; i++) {
37: sprintf(fname,"Field %d",(int)i);
38: DASetFieldName(da,i,fname);
39: }
41: DAView(da,viewer);
42: DACreateGlobalVector(da,&global);
43: DACreateLocalVector(da,&local);
44: DACreateLocalVector(da,&locala);
45: DAGetCoordinates(da,&coors);
46: VecGetArray(coors,&x);
48: /* Set values into global vectors */
49: VecGetArray(global,&alocal);
50: VecGetLocalSize(global,&n);
51: n = n/dof;
52: for (j=0; j<dof; j++) {
53: for (i=0; i<n; i++) {
54: alocal[j+dof*i] = PetscSinScalar(2*PETSC_PI*(j+1)*x[i]);
55: }
56: }
57: VecRestoreArray(global,&alocal);
58: VecRestoreArray(coors,&x);
59: VecDestroy(coords);
61: VecView(global,viewer);
63: /* Send ghost points to local vectors */
64: DAGlobalToLocalBegin(da,global,INSERT_VALUES,locala);
65: DAGlobalToLocalEnd(da,global,INSERT_VALUES,locala);
67: /* Free memory */
68: PetscViewerDestroy(viewer);
69: VecDestroy(global);
70: VecDestroy(local);
71: VecDestroy(locala);
72: DADestroy(da);
73: PetscFinalize();
74: return 0;
75: }
76: