Actual source code: ex3f.F

petsc-3.4.5 2014-06-29
  1: !
  2: !
  3: !  Description: Displays a vector visually.
  4: !
  5: !/*T
  6: !   Concepts: vectors^drawing vectors;
  7: !   Processors: n
  8: !T*/
  9: ! -----------------------------------------------------------------------

 11:       program main
 12:       implicit none

 14: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 15: !                    Include files
 16: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 17: !
 18: !  The following include statements are required for Fortran programs
 19: !  that use PETSc vectors:
 20: !     petscsys.h       - base PETSc routines
 21: !     petscvec.h    - vectors
 22: !  Include petscviewer.h so that we can use the PETSc viewers.
 23: !
 24: #include <finclude/petscsys.h>
 25: #include <finclude/petscvec.h>
 26: #include <finclude/petscviewer.h>

 28: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 29: !                 Beginning of program
 30: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 32:       Vec     x
 33:       PetscViewer  viewer
 34:       PetscScalar  v
 35:       PetscInt i,istart,iend,n,ione
 36:       PetscErrorCode ierr
 37:       PetscBool  flg

 39:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 40:       n = 50
 41:       call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',n,flg,ierr)

 43: !  Create a vector, specifying only its global dimension.
 44: !  When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
 45: !  the vector format (currently parallel
 46: !  or sequential) is determined at runtime.  Also, the parallel
 47: !  partitioning of the vector is determined by PETSc at runtime.
 48:       call VecCreate(PETSC_COMM_WORLD,x,ierr)
 49:       call VecSetSizes(x,PETSC_DECIDE,n,ierr)
 50:       call VecSetFromOptions(x,ierr)

 52: !  Currently, all PETSc parallel vectors are partitioned by
 53: !  contiguous chunks of rows across the processors.  Determine
 54: !  which vector are locally owned.
 55:       call VecGetOwnershipRange(x,istart,iend,ierr)

 57: !  Set the vector elements.
 58: !   - Always specify global locations of vector entries.
 59: !   - Each processor needs to insert only elements that it owns locally.
 60:       ione = 1
 61:       do 100 i=istart,iend-1
 62:          v = dble(i)
 63:          call VecSetValues(x,ione,i,v,INSERT_VALUES,ierr)
 64:  100  continue

 66: !  Assemble vector, using the 2-step process:
 67: !    VecAssemblyBegin(), VecAssemblyEnd()
 68: !  Computations can be done while messages are in transition
 69: !  by placing code between these two statements.
 70:       call VecAssemblyBegin(x,ierr)
 71:       call VecAssemblyEnd(x,ierr)

 73: !  Open an X-window viewer.  Note that we specify the same communicator
 74: !  for the viewer as we used for the distributed vector (PETSC_COMM_WORLD).
 75: !    - Helpful runtime option:
 76: !         -draw_pause <pause> : sets time (in seconds) that the
 77: !               program pauses after PetscDrawPause() has been called
 78: !              (0 is default, -1 implies until user input).

 80:       call PetscViewerDrawOpen(PETSC_COMM_WORLD,PETSC_NULL_CHARACTER,        &
 81:      &                   PETSC_NULL_CHARACTER,0,0,300,300,viewer,ierr)

 83: !  View the vector
 84:       call VecView(x,viewer,ierr)

 86: !  Free work space.  All PETSc objects should be destroyed when they
 87: !  are no longer needed.

 89:       call PetscViewerDestroy(viewer,ierr)
 90:       call VecDestroy(x,ierr)

 92:       call PetscFinalize(ierr)
 93:       end