Actual source code: ex9.c

petsc-master 2016-08-24
Report Typos and Errors
  1: static char help[] = "Demonstrates HDF5 vector input/ouput\n\n";

  3: /*T
  4:    Concepts: viewers
  5:    Concepts: HDF5
  6:    Processors: n
  7: T*/
 8:  #include <petscsys.h>
 9:  #include <petscdm.h>
 10:  #include <petscdmda.h>
 11: #include <petscviewerhdf5.h>

 15: int main(int argc,char **argv)
 16: {
 18:   PetscViewer    viewer;
 19:   DM             da;
 20:   Vec            global,local,global2;
 21:   PetscMPIInt    rank;
 22:   PetscBool      flg;
 23:   PetscInt       ndof;

 25:   /*
 26:     Every PETSc routine should begin with the PetscInitialize() routine.
 27:     argc, argv - These command line arguments are taken to extract the options
 28:                  supplied to PETSc and options supplied to MPI.
 29:     help       - When PETSc executable is invoked with the option -help,
 30:                  it prints the various options that can be applied at
 31:                  runtime.  The user can use the "help" variable place
 32:                  additional help messages in this printout.
 33:   */
 34:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 35:   /* Get number of DOF's from command line */
 36:   PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"DMDA VecView/VecLoad example","");
 37:   {
 38:     ndof = 1;
 39:     PetscOptionsInt("-ndof","Number of DOF's in DMDA","",ndof,&ndof,NULL);
 40:   }
 41:   PetscOptionsEnd();

 43:   /* Create a DMDA and an associated vector */
 44:   DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,100,90,PETSC_DECIDE,PETSC_DECIDE,ndof,1,NULL,NULL,&da);
 45:   DMCreateGlobalVector(da,&global);
 46:   DMCreateLocalVector(da,&local);
 47:   VecSet(global,-1.0);
 48:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 49:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);
 50:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 51:   VecScale(local,rank+1);
 52:   DMLocalToGlobalBegin(da,local,ADD_VALUES,global);
 53:   DMLocalToGlobalEnd(da,local,ADD_VALUES,global);

 55:   /* Create the HDF5 viewer for writing */
 56:   PetscViewerHDF5Open(PETSC_COMM_WORLD,"hdf5output.h5",FILE_MODE_WRITE,&viewer);
 57:   PetscViewerSetFromOptions(viewer);

 59:   /* Write the Vec without one extra dimension for BS */
 60:   PetscViewerHDF5SetBaseDimension2(viewer, PETSC_FALSE);
 61:   PetscObjectSetName((PetscObject) global, "noBsDim");
 62:   VecView(global,viewer);

 64:   /* Write the Vec with one extra, 1-sized, dimension for BS */
 65:   PetscViewerHDF5SetBaseDimension2(viewer, PETSC_TRUE);
 66:   PetscObjectSetName((PetscObject) global, "bsDim");
 67:   VecView(global,viewer);

 69:   PetscViewerDestroy(&viewer);
 70:   MPI_Barrier(PETSC_COMM_WORLD);
 71:   VecDuplicate(global,&global2);

 73:   /* Create the HDF5 viewer for reading */
 74:   PetscViewerHDF5Open(PETSC_COMM_WORLD,"hdf5output.h5",FILE_MODE_READ,&viewer);
 75:   PetscViewerSetFromOptions(viewer);

 77:   /* Load the Vec without the BS dim and compare */
 78:   PetscObjectSetName((PetscObject) global2, "noBsDim");
 79:   VecLoad(global2,viewer);

 81:   VecEqual(global,global2,&flg);
 82:   if (!flg) {
 83:     PetscPrintf(PETSC_COMM_WORLD,"Error: Vectors are not equal\n");
 84:   }

 86:   /* Load the Vec with one extra, 1-sized, BS dim and compare */
 87:   PetscObjectSetName((PetscObject) global2, "bsDim");
 88:   VecLoad(global2,viewer);
 89:   PetscViewerDestroy(&viewer);

 91:   VecEqual(global,global2,&flg);
 92:   if (!flg) {
 93:     PetscPrintf(PETSC_COMM_WORLD,"Error: Vectors are not equal\n");
 94:   }

 96:   /* clean up and exit */
 97:   VecDestroy(&local);
 98:   VecDestroy(&global);
 99:   VecDestroy(&global2);
100:   DMDestroy(&da);

102:   PetscFinalize();
103:   return ierr;
104: }