Actual source code: ex2.c


  2: static char help[] = "Tests various 1-dimensional DMDA routines.\n\n";

  4: #include <petscdm.h>
  5: #include <petscdmda.h>
  6: #include <petscdraw.h>

  8: int main(int argc,char **argv)
  9: {
 10:   PetscMPIInt            rank;
 11:   PetscInt               M  = 13,s=1,dof=1;
 12:   DMBoundaryType         bx = DM_BOUNDARY_PERIODIC;
 13:   DM                     da;
 14:   PetscViewer            viewer;
 15:   Vec                    local,global;
 16:   PetscScalar            value;
 17:   PetscDraw              draw;
 18:   PetscBool              flg = PETSC_FALSE;
 19:   ISLocalToGlobalMapping is;

 21:   PetscInitialize(&argc,&argv,(char*)0,help);
 22:   PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",280,480,600,200,&viewer);
 23:   PetscViewerDrawGetDraw(viewer,0,&draw);
 24:   PetscDrawSetDoubleBuffer(draw);

 26:   /* Readoptions */
 27:   PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL);
 28:   PetscOptionsGetEnum(NULL,NULL,"-wrap",DMBoundaryTypes,(PetscEnum*)&bx,NULL);
 29:   PetscOptionsGetInt(NULL,NULL,"-dof",&dof,NULL);
 30:   PetscOptionsGetInt(NULL,NULL,"-s",&s,NULL);

 32:   /* Create distributed array and get vectors */
 33:   DMDACreate1d(PETSC_COMM_WORLD,bx,M,dof,s,NULL,&da);
 34:   DMSetFromOptions(da);
 35:   DMSetUp(da);
 36:   DMView(da,viewer);
 37:   DMCreateGlobalVector(da,&global);
 38:   DMCreateLocalVector(da,&local);

 40:   value = 1;
 41:   VecSet(global,value);

 43:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 44:   value = rank+1;
 45:   VecScale(global,value);

 47:   VecView(global,viewer);
 48:   PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nGlobal Vector:\n");
 49:   VecView(global,PETSC_VIEWER_STDOUT_WORLD);
 50:   PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\n");

 52:   /* Send ghost points to local vectors */
 53:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 54:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);

 56:   PetscOptionsGetBool(NULL,NULL,"-local_print",&flg,NULL);
 57:   if (flg) {
 58:     PetscViewer            sviewer;

 60:     PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD);
 61:     PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nLocal Vector: processor %d\n",rank);
 62:     PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
 63:     VecView(local,sviewer);
 64:     PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
 65:     PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
 66:     PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD);
 67:   }
 68:   PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nLocal to global mapping\n");
 69:   DMGetLocalToGlobalMapping(da,&is);
 70:   ISLocalToGlobalMappingView(is,PETSC_VIEWER_STDOUT_WORLD);

 72:   /* Free memory */
 73:   PetscViewerDestroy(&viewer);
 74:   VecDestroy(&global);
 75:   VecDestroy(&local);
 76:   DMDestroy(&da);
 77:   PetscFinalize();
 78:   return 0;
 79: }

 81: /*TEST

 83:    test:
 84:       nsize: 2
 85:       args: -nox
 86:       filter: grep -v "MPI processes"
 87:       output_file: output/ex2_1.out
 88:       requires: x

 90:    test:
 91:       suffix: 2
 92:       nsize: 3
 93:       args: -wrap none -local_print -nox
 94:       filter: grep -v "Vec Object: Vec"
 95:       requires: x

 97:    test:
 98:       suffix: 3
 99:       nsize: 3
100:       args: -wrap ghosted -local_print -nox
101:       filter: grep -v "Vec Object: Vec"
102:       requires: x

104: TEST*/