Actual source code: ex3f.F

petsc-master 2016-12-04
Report Typos and Errors
  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 <petsc/finclude/petscsys.h>
 25:  #include <petsc/finclude/petscvec.h>
 26:  #include <petsc/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:       if (ierr .ne. 0) then
 41:          print*,'PetscInitialize failed'
 42:          stop
 43:       endif
 44:       n = 50
 45:       call PetscOptionsGetInt(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,      &
 46:      &                        '-n',n,flg,ierr)

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

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

 62: !  Set the vector elements.
 63: !   - Always specify global locations of vector entries.
 64: !   - Each processor needs to insert only elements that it owns locally.
 65:       ione = 1
 66:       do 100 i=istart,iend-1
 67:          v = 1.0*real(i)
 68:          call VecSetValues(x,ione,i,v,INSERT_VALUES,ierr)
 69:  100  continue

 71: !  Assemble vector, using the 2-step process:
 72: !    VecAssemblyBegin(), VecAssemblyEnd()
 73: !  Computations can be done while messages are in transition
 74: !  by placing code between these two statements.
 75:       call VecAssemblyBegin(x,ierr)
 76:       call VecAssemblyEnd(x,ierr)

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

 85:       call PetscViewerDrawOpen(PETSC_COMM_WORLD,PETSC_NULL_CHARACTER,        &
 86:      &                   PETSC_NULL_CHARACTER,0,0,300,300,viewer,ierr)

 88: !  View the vector
 89:       call VecView(x,viewer,ierr)

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

 94:       call PetscViewerDestroy(viewer,ierr)
 95:       call VecDestroy(x,ierr)

 97:       call PetscFinalize(ierr)
 98:       end