Actual source code: ex19.c

petsc-3.3-p7 2013-05-11
  1: static char help[] = "Parallel HDF5 Vec Viewing.\n\n";

  3: /*T
  4:    Concepts: vectors^viewing
  5:    Concepts: viewers^hdf5
  6:    Processors: n
  7: T*/

  9: #include <petscvec.h>

 13: int main(int argc,char **argv)
 14: {
 15:   Vec            x1, x2, y1, y2, y3, y4;
 16:   PetscViewer    viewer;
 17:   PetscMPIInt    rank;
 18:   PetscInt       i, nlocal, n = 6;
 19:   PetscScalar   *array;
 20:   PetscBool      equal;

 24:   PetscInitialize(&argc, &argv, (char *) 0, help);
 25:   MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
 26:   PetscOptionsGetInt(PETSC_NULL, "-n", &n, PETSC_NULL);

 28:   VecCreate(PETSC_COMM_WORLD, &x1);
 29:   PetscObjectSetName((PetscObject) x1, "TestVec");
 30:   VecSetSizes(x1, PETSC_DECIDE, n);
 31:   VecSetFromOptions(x1);

 33:   VecGetLocalSize(x1, &nlocal);
 34:   VecGetArray(x1, &array);
 35:   for(i = 0; i < nlocal; i++) {
 36:     array[i] = rank + 1;
 37:   }
 38:   VecRestoreArray(x1, &array);
 39:   VecAssemblyBegin(x1);
 40:   VecAssemblyEnd(x1);

 42:   VecCreate(PETSC_COMM_WORLD, &x2);
 43:   PetscObjectSetName((PetscObject) x2, "TestVec2");
 44:   VecSetSizes(x2, PETSC_DECIDE, n);
 45:   VecSetBlockSize(x2, 2);
 46:   VecSetFromOptions(x2);
 47:   VecCopy(x1, x2);

 49:   PetscViewerHDF5Open(PETSC_COMM_WORLD, "ex19.h5", FILE_MODE_WRITE, &viewer);
 50:   PetscViewerHDF5PushGroup(viewer, "/");
 51:   VecView(x1, viewer);
 52:   PetscViewerHDF5PushGroup(viewer, "/testBlockSize");
 53:   VecView(x2, viewer);
 54:   PetscViewerHDF5PushGroup(viewer, "/testTimestep");
 55:   PetscViewerHDF5SetTimestep(viewer, 0);
 56:   VecView(x2, viewer);
 57:   PetscViewerHDF5SetTimestep(viewer, 1);
 58:   VecView(x2, viewer);
 59:   PetscViewerHDF5PopGroup(viewer);
 60:   PetscViewerHDF5PopGroup(viewer);
 61:   PetscViewerHDF5PopGroup(viewer);
 62:   PetscViewerDestroy(&viewer);

 64:   VecCreate(PETSC_COMM_WORLD, &y1);
 65:   PetscObjectSetName((PetscObject) y1, "TestVec");
 66:   VecSetSizes(y1, PETSC_DECIDE, n);
 67:   VecSetFromOptions(y1);

 69:   VecCreate(PETSC_COMM_WORLD,&y2);
 70:   VecSetBlockSize(y2, 2);
 71:   PetscObjectSetName((PetscObject) y2, "TestVec2");

 73:   VecCreate(PETSC_COMM_WORLD,&y3);
 74:   VecSetBlockSize(y3, 2);
 75:   PetscObjectSetName((PetscObject) y3, "TestVec2");

 77:   VecCreate(PETSC_COMM_WORLD,&y4);
 78:   VecSetBlockSize(y4, 2);
 79:   PetscObjectSetName((PetscObject) y4, "TestVec2");

 81:   PetscViewerHDF5Open(PETSC_COMM_WORLD, "ex19.h5", FILE_MODE_READ, &viewer);
 82:   PetscViewerHDF5PushGroup(viewer, "/");
 83:   VecLoad(y1, viewer);
 84: 
 85:   PetscViewerHDF5PushGroup(viewer, "/testBlockSize");
 86:   VecLoad(y2, viewer);
 87:   PetscViewerHDF5PushGroup(viewer, "/testTimestep");
 88:   PetscViewerHDF5SetTimestep(viewer, 0);
 89:   VecLoad(y3, viewer);
 90:   PetscViewerHDF5SetTimestep(viewer, 1);
 91:   VecLoad(y4, viewer);
 92:   PetscViewerHDF5PopGroup(viewer);
 93:   PetscViewerHDF5PopGroup(viewer);
 94:   PetscViewerHDF5PopGroup(viewer);
 95:   PetscViewerDestroy(&viewer);

 97:   VecEqual(x1, y1, &equal);
 98:   if (!equal) {
 99:     VecView(x1, PETSC_VIEWER_STDOUT_WORLD);
100:     VecView(y1, PETSC_VIEWER_STDOUT_WORLD);
101:     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Error in HDF5 viewer");
102:   }
103:   VecEqual(x2, y2, &equal);
104:   if (!equal) {
105:     VecView(x2, PETSC_VIEWER_STDOUT_WORLD);
106:     VecView(y2, PETSC_VIEWER_STDOUT_WORLD);
107:     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Error in HDF5 viewer");
108:   }

110:   VecDestroy(&x1);
111:   VecDestroy(&x2);
112:   VecDestroy(&y1);
113:   VecDestroy(&y2);
114:   VecDestroy(&y3);
115:   VecDestroy(&y4);
116:   PetscFinalize();
117:   return(0);
118: }
119: