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, n
 19:   PetscErrorCode ierr
 20:   PetscBool flg
 21:   integer4, parameter :: xl = 0, yl = 0, w = 300, h = 300

 23:   PetscCallA(PetscInitialize(ierr))
 24:   n = 50
 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, 1_PETSC_INT_KIND, [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).
 62:   PetscCallA(PetscViewerDrawOpen(PETSC_COMM_WORLD, PETSC_NULL_CHARACTER, PETSC_NULL_CHARACTER, xl, yl, w, h, viewer, ierr))

 64: !  View the vector
 65:   PetscCallA(VecView(x, viewer, ierr))

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

 70:   PetscCallA(PetscViewerDestroy(viewer, ierr))
 71:   PetscCallA(VecDestroy(x, ierr))

 73:   PetscCallA(PetscFinalize(ierr))
 74: end

 76: !/*TEST
 77: !
 78: !     test:
 79: !       nsize: 2
 80: !       output_file: output/empty.out
 81: !
 82: !TEST*/