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