Actual source code: ex14.c

petsc-3.6.4 2016-04-12
Report Typos and Errors
  2: static char help[] = "Tests saving DMDA vectors to files.\n\n";

  4: /*
  5:     ex13.c reads in the DMDA and vector written by this program.
  6: */

  8: #include <petscdm.h>
  9: #include <petscdmda.h>

 13: int main(int argc,char **argv)
 14: {
 15:   PetscMPIInt    rank;
 16:   PetscInt       M = 10,N = 8,m = PETSC_DECIDE,n = PETSC_DECIDE, dof = 1;
 18:   DM             da;
 19:   Vec            local,global,natural;
 20:   PetscScalar    value;
 21:   PetscViewer    bviewer;

 23:   PetscInitialize(&argc,&argv,(char*)0,help);

 25:   /* Read options */
 26:   PetscOptionsGetInt(NULL,"-M",&M,NULL);
 27:   PetscOptionsGetInt(NULL,"-N",&N,NULL);
 28:   PetscOptionsGetInt(NULL,"-m",&m,NULL);
 29:   PetscOptionsGetInt(NULL,"-n",&n,NULL);
 30:   PetscOptionsGetInt(NULL,"-dof",&dof,NULL);

 32:   /* Create distributed array and get vectors */
 33:   DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,
 34:                       M,N,m,n,dof,1,NULL,NULL,&da);
 35:   DMCreateGlobalVector(da,&global);
 36:   DMCreateLocalVector(da,&local);

 38:   value = -3.0;
 39:   VecSet(global,value);
 40:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 41:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);

 43:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 44:   value = rank+1;
 45:   VecScale(local,value);
 46:   DMLocalToGlobalBegin(da,local,ADD_VALUES,global);
 47:   DMLocalToGlobalEnd(da,local,ADD_VALUES,global);

 49:   DMDACreateNaturalVector(da,&natural);
 50:   DMDAGlobalToNaturalBegin(da,global,INSERT_VALUES,natural);
 51:   DMDAGlobalToNaturalEnd(da,global,INSERT_VALUES,natural);

 53:   DMDASetFieldName(da,0,"First field");
 54:   /*  VecView(global,PETSC_VIEWER_DRAW_WORLD); */

 56:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"daoutput",FILE_MODE_WRITE,&bviewer);
 57:   DMView(da,bviewer);
 58:   VecView(global,bviewer);
 59:   PetscViewerDestroy(&bviewer);

 61:   /* Free memory */
 62:   VecDestroy(&local);
 63:   VecDestroy(&global);
 64:   VecDestroy(&natural);
 65:   DMDestroy(&da);
 66:   PetscFinalize();
 67:   return 0;
 68: }