Guest User

Untitled

a guest
Dec 14th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1.  
  2. static char help[] = "Tests I/O of vectors for different data formats (binary,HDF5) and illustrates the use of user-defined event logging\n\n";
  3.  
  4. #include <petscvec.h>
  5. #include <petscviewerhdf5.h>
  6.  
  7. /* Note:  Most applications would not read and write a vector within
  8.   the same program.  This example is intended only to demonstrate
  9.   both input and output and is written for use with either 1,2,or 4 processors. */
  10.  
  11. int main(int argc,char **args)
  12. {
  13.   PetscErrorCode    ierr;
  14.   PetscMPIInt       rank,size;
  15.   PetscInt          i,m = 20,low,high,ldim,iglobal,lsize;
  16.   PetscScalar       v;
  17.   Vec               u;
  18.   PetscViewer       viewer;
  19.   ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
  20.   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
  21.   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
  22.   ierr = PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);CHKERRQ(ierr);
  23.  
  24.   /* PART 1:  Generate vector, then write it in the given data format */
  25.  
  26.  // Generate vector
  27.   ierr = VecCreate(PETSC_COMM_WORLD,&u);CHKERRQ(ierr);
  28.   ierr = VecSetSizes(u,PETSC_DECIDE,m);CHKERRQ(ierr);
  29.   ierr = VecSetFromOptions(u);CHKERRQ(ierr);
  30.   ierr = VecGetOwnershipRange(u,&low,&high);CHKERRQ(ierr);
  31.   ierr = VecGetLocalSize(u,&ldim);CHKERRQ(ierr);
  32.   for (i=0; i<ldim; i++) {
  33.     iglobal = i + low;
  34.     v       = (PetscScalar)(i + low);
  35.     ierr    = VecSetValues(u,1,&iglobal,&v,INSERT_VALUES);CHKERRQ(ierr);
  36.   }
  37.   ierr = VecAssemblyBegin(u);CHKERRQ(ierr);
  38.   ierr = VecAssemblyEnd(u);CHKERRQ(ierr);
  39.   ierr = VecView(u,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
  40.  
  41.   ierr = PetscPrintf(PETSC_COMM_WORLD,"writing vector in hdf5 to vector.dat ...\n");CHKERRQ(ierr);
  42.   ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
  43.  
  44.   ierr = PetscViewerHDF5SetTimestep(viewer, 0);CHKERRQ(ierr);
  45.   ierr = VecView(u,viewer);CHKERRQ(ierr);
  46.  
  47.   ierr = PetscViewerHDF5SetTimestep(viewer, 1);CHKERRQ(ierr);
  48.   ierr = VecView(u,viewer);CHKERRQ(ierr);
  49.  
  50.   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
  51.  
  52.   /* Free data structures */
  53.   ierr = VecDestroy(&u);CHKERRQ(ierr);
  54.   ierr = PetscFinalize();
  55.   return ierr;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment