Actual source code: ex3f.F90

  1: !
  2: !
  3: !  Description: Displays a vector visually.
  4: !
  5: ! -----------------------------------------------------------------------
  6: #include <petsc/finclude/petscvec.h>
  7: program main
  8:   use petscvec
  9:   implicit none

 11: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 12: !                 Beginning of program
 13: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 15:   Vec x
 16:   PetscViewer viewer
 17:   PetscScalar v
 18:   PetscInt :: i, istart, iend
 19:   PetscInt, parameter :: ione = 1, n = 50
 20:   PetscErrorCode ierr
 21:   PetscBool flg
 22:   integer4 xl, yl, w, h

 24:   PetscCallA(PetscInitialize(ierr))
 25:   PetscCallA(PetscOptionsGetInt(PETSC_NULL_OPTIONS, PETSC_NULL_CHARACTER, '-n', n, flg, ierr))

 27: !  Create a vector, specifying only its global dimension.
 28: !  When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
 29: !  the vector format (currently parallel
 30: !  or sequential) is determined at runtime.  Also, the parallel
 31: !  partitioning of the vector is determined by PETSc at runtime.
 32:   PetscCallA(VecCreate(PETSC_COMM_WORLD, x, ierr))
 33:   PetscCallA(VecSetSizes(x, PETSC_DECIDE, n, ierr))
 34:   PetscCallA(VecSetFromOptions(x, ierr))

 36: !  Currently, all PETSc parallel vectors are partitioned by
 37: !  contiguous chunks of rows across the processors.  Determine
 38: !  which vector are locally owned.
 39:   PetscCallA(VecGetOwnershipRange(x, istart, iend, ierr))

 41: !  Set the vector elements.
 42: !   - Always specify global locations of vector entries.
 43: !   - Each processor needs to insert only elements that it owns locally.
 44:   do i = istart, iend - 1
 45:     v = 1.0*real(i)
 46:     PetscCallA(VecSetValues(x, ione, [i], [v], INSERT_VALUES, ierr))
 47:   end do

 49: !  Assemble vector, using the 2-step process:
 50: !    VecAssemblyBegin(), VecAssemblyEnd()
 51: !  Computations can be done while messages are in transition
 52: !  by placing code between these two statements.
 53:   PetscCallA(VecAssemblyBegin(x, ierr))
 54:   PetscCallA(VecAssemblyEnd(x, ierr))

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

 63:   xl = 0
 64:   yl = 0
 65:   w = 300
 66:   h = 300
 67:   PetscCallA(PetscViewerDrawOpen(PETSC_COMM_WORLD, PETSC_NULL_CHARACTER, PETSC_NULL_CHARACTER, xl, yl, w, h, viewer, ierr))

 69: !  View the vector
 70:   PetscCallA(VecView(x, viewer, ierr))

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

 75:   PetscCallA(PetscViewerDestroy(viewer, ierr))
 76:   PetscCallA(VecDestroy(x, ierr))

 78:   PetscCallA(PetscFinalize(ierr))
 79: end

 81: !/*TEST
 82: !
 83: !     test:
 84: !       nsize: 2
 85: !       output_file: output/empty.out
 86: !
 87: !TEST*/