Date: 2022-03-04T17:38:41+00:00

Actual source code: ex9.c

      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>

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

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

     41:   /* Create a DMDA and an associated vector */
     42:   DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,100,90,PETSC_DECIDE,PETSC_DECIDE,ndof,1,NULL,NULL,&da);
     43:   DMSetFromOptions(da);
     44:   DMSetUp(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: }

    106: /*TEST

    108:       build:
    109:          requires: hdf5

    111:       test:
    112:          nsize: 4

    114:       test:
    115:          nsize: 4
    116:          suffix: 2
    117:          args: -ndof 2
    118:          output_file: output/ex9_1.out

    120: TEST*/