Actual source code: ex3f.F90

      1: !
      2: !
      3: !  Description: Displays a vector visually.
      4: !
      5: ! -----------------------------------------------------------------------

      7: program main
      8: #include <petsc/finclude/petscvec.h>
      9:   use petscvec
     10:   implicit none

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

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

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

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

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

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

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

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

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

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

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

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

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

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