Actual source code: ex3.c

  2: static char help[] = "Solves the 1-dimensional wave equation.\n\n";

  4: #include <petscdmda.h>

  8: int main(int argc,char **argv)
  9: {
 10:   PetscMPIInt    rank,size;
 12:   PetscInt       M = 60,time_steps = 100, localsize,j,i,mybase,myend,width,xbase,*localnodes = PETSC_NULL;
 13:   DM             da;
 14:   PetscViewer    viewer,viewer_private;
 15:   PetscDraw      draw;
 16:   Vec            local,global,copy;
 17:   PetscScalar    *localptr,*copyptr;
 18:   PetscReal      a,h,k;
 19:   PetscBool      flg = PETSC_FALSE;
 20: 
 21:   PetscInitialize(&argc,&argv,(char*)0,help);
 22:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 23:   MPI_Comm_size(PETSC_COMM_WORLD,&size);

 25:   PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);
 26:   PetscOptionsGetInt(PETSC_NULL,"-time",&time_steps,PETSC_NULL);
 27:   /*
 28:       Test putting two nodes on each processor, exact last processor gets the rest
 29:   */
 30:   PetscOptionsGetBool(PETSC_NULL,"-distribute",&flg,PETSC_NULL);
 31:   if (flg) {
 32:     PetscMalloc(size*sizeof(PetscInt),&localnodes);
 33:     for (i=0; i<size-1; i++) { localnodes[i] = 2;}
 34:     localnodes[size-1] = M - 2*(size-1);
 35:   }
 36: 
 37:   /* Set up the array */
 38:   DMDACreate1d(PETSC_COMM_WORLD,DMDA_BOUNDARY_PERIODIC,M,1,1,localnodes,&da);
 39:   PetscFree(localnodes);
 40:   DMCreateGlobalVector(da,&global);
 41:   DMCreateLocalVector(da,&local);

 43:   /* Set up display to show combined wave graph */
 44:   PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"Entire Solution",20,480,800,200,&viewer);
 45:   PetscViewerDrawGetDraw(viewer,0,&draw);
 46:   PetscDrawSetDoubleBuffer(draw);

 48:   /* determine starting point of each processor */
 49:   VecGetOwnershipRange(global,&mybase,&myend);

 51:   /* set up display to show my portion of the wave */
 52:   xbase = (int)((mybase)*((800.0 - 4.0*size)/M) + 4.0*rank);
 53:   width = (int)((myend-mybase)*800./M);
 54:   PetscViewerDrawOpen(PETSC_COMM_SELF,0,"Local Portion of Solution",xbase,200,
 55:                          width,200,&viewer_private);
 56:   PetscViewerDrawGetDraw(viewer_private,0,&draw);
 57:   PetscDrawSetDoubleBuffer(draw);



 61:   /* Initialize the array */
 62:   VecGetLocalSize(local,&localsize);
 63:   VecGetArray(local,&localptr);
 64:   localptr[0] = 0.0;
 65:   localptr[localsize-1] = 0.0;
 66:   for (i=1; i<localsize-1; i++) {
 67:     j=(i-1)+mybase;
 68:     localptr[i] = sin((PETSC_PI*j*6)/((PetscReal)M)
 69:                         + 1.2 * sin((PETSC_PI*j*2)/((PetscReal)M))) * 2;
 70:   }

 72:   VecRestoreArray(local,&localptr);
 73:   DMLocalToGlobalBegin(da,local,INSERT_VALUES,global);
 74:   DMLocalToGlobalEnd(da,local,INSERT_VALUES,global);

 76:   /* Make copy of local array for doing updates */
 77:   VecDuplicate(local,&copy);

 79:   /* Assign Parameters */
 80:   a= 1.0;
 81:   h= 1.0/M;
 82:   k= h;

 84:   for (j=0; j<time_steps; j++) {

 86:     /* Global to Local */
 87:     DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 88:     DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);

 90:     /*Extract local array */
 91:     VecGetArray(local,&localptr);
 92:     VecGetArray(copy,&copyptr);

 94:     /* Update Locally - Make array of new values */
 95:     /* Note: I don't do anything for the first and last entry */
 96:     for (i=1; i< localsize-1; i++) {
 97:       copyptr[i] = .5*(localptr[i+1]+localptr[i-1]) -
 98:                     (k / (2.0*a*h)) * (localptr[i+1] - localptr[i-1]);
 99:     }
100:     VecRestoreArray(copy,&copyptr);
101:     VecRestoreArray(local,&localptr);

103:     /* Local to Global */
104:     DMLocalToGlobalBegin(da,copy,INSERT_VALUES,global);
105:     DMLocalToGlobalEnd(da,copy,INSERT_VALUES,global);
106: 
107:     /* View my part of Wave */
108:     VecView(copy,viewer_private);

110:     /* View global Wave */
111:     VecView(global,viewer);
112:   }

114:   DMDestroy(&da);
115:   PetscViewerDestroy(&viewer);
116:   PetscViewerDestroy(&viewer_private);
117:   VecDestroy(&copy);
118:   VecDestroy(&local);
119:   VecDestroy(&global);

121:   PetscFinalize();
122:   return 0;
123: }
124: