Actual source code: ex20f90.F90
petsc-3.13.6 2020-09-29
Report Typos and Errors

      1: !
      2: !
      3: !/*T
      4: !   Concepts: vectors^using basic vector routines;
      5: !   Concepts: Fortran90^using basic vector routines;
      6: !   Processors: n
      7: !T*/
      8: !
      9: ! -----------------------------------------------------------------------

     11:       program main

     13: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     14: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     15: !
     16: !
     17: !     This examples uses Fortran 90 MODULES instead of include files
     18: !   see the manual page UsingFortran
     19: !
     20:  #include <petsc/finclude/petscvec.h>
     21:       use petscvec
     22:       implicit none

     24: !
     25: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     26: !                   Variable declarations
     27: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     28: !
     29: !  Variables:
     30: !     x, y, w - vectors
     31: !     z       - array of vectors
     32: !
     33:       type(tVec)       x,y,w
     34:       type(tVec), pointer :: z(:)

     36:       PetscReal norm,v,v1,v2,tol
     37:       PetscInt  n,ithree
     38:       PetscErrorCode ierr
     39:       PetscMPIInt  rank
     40:       PetscBool       flg
     41:       PetscScalar      one,two,three
     42:       PetscScalar      dots(3),dot
     43:       PetscReal        nfloat

     45: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     46: !                 Beginning of program
     47: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

     49:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
     50:       if (ierr .ne. 0) then
     51:          print*,'Unable to initialize PETSc'
     52:          stop
     53:       endif

     55:       tol   = 1.e-10_PETSC_REAL_KIND
     56:       one   = 1.0
     57:       two   = 2.0
     58:       three = 3.0
     59:       n     = 20
     60:       ithree = 3

     62:       call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-n',n,flg,ierr);CHKERRA(ierr)
     63:       nfloat = n
     64:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr);CHKERRA(ierr)

     66: !  Create a vector, specifying only its global dimension.
     67: !  When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
     68: !  the vector format (currently parallel
     69: !  or sequential) is determined at runtime.  Also, the parallel
     70: !  partitioning of the vector is determined by PETSc at runtime.
     71: !
     72: !  Routines for creating particular vector types directly are:
     73: !     VecCreateSeq() - uniprocessor vector
     74: !     VecCreateMPI() - distributed vector, where the user can
     75: !                      determine the parallel partitioning

     77:       call VecCreate(PETSC_COMM_WORLD,x,ierr);CHKERRA(ierr)
     78:       call VecSetSizes(x,PETSC_DECIDE,n,ierr);CHKERRA(ierr)
     79:       call VecSetFromOptions(x,ierr);CHKERRA(ierr)

     81: !  Duplicate some work vectors (of the same format and
     82: !  partitioning as the initial vector).

     84:       call VecDuplicate(x,y,ierr);CHKERRA(ierr)
     85:       call VecDuplicate(x,w,ierr);CHKERRA(ierr)

     87: !  Duplicate more work vectors (of the same format and
     88: !  partitioning as the initial vector).  Here we duplicate
     89: !  an array of vectors, which is often more convenient than
     90: !  duplicating individual ones.

     92:       call VecDuplicateVecsF90(x,ithree,z,ierr);CHKERRA(ierr)

     94: !  Set the vectors to entries to a constant value.

     96:       call VecSet(x,one,ierr);CHKERRA(ierr)
     97:       call VecSet(y,two,ierr);CHKERRA(ierr)
     98:       call VecSet(z(1),one,ierr);CHKERRA(ierr)
     99:       call VecSet(z(2),two,ierr);CHKERRA(ierr)
    100:       call VecSet(z(3),three,ierr);CHKERRA(ierr)

    102: !  Demonstrate various basic vector routines.

    104:       call VecDot(x,x,dot,ierr);CHKERRA(ierr)
    105:       call VecMDot(x,ithree,z,dots,ierr);CHKERRA(ierr)

    107: !  Note: If using a complex numbers version of PETSc, then
    108: !  PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
    109: !  (when using real numbers) it is undefined.

    111:       if (rank .eq. 0) then
    112: #if defined(PETSC_USE_COMPLEX)
    113:          write(6,100) int(PetscRealPart(dot))
    114:          write(6,110) int(PetscRealPart(dots(1))),int(PetscRealPart(dots(2))),int(PetscRealPart(dots(3)))
    115: #else
    116:          write(6,100) int(dot)
    117:          write(6,110) int(dots(1)),int(dots(2)),int(dots(3))
    118: #endif
    119:          write(6,120)
    120:       endif
    121:  100  format ('Vector length ',i6)
    122:  110  format ('Vector length ',3(i6))
    123:  120  format ('All other values should be near zero')

    125:       call VecScale(x,two,ierr);CHKERRA(ierr)
    126:       call VecNorm(x,NORM_2,norm,ierr);CHKERRA(ierr)
    127:       v = abs(norm-2.0*sqrt(nfloat))
    128:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
    129:       if (rank .eq. 0) write(6,130) v
    130:  130  format ('VecScale ',1pe9.2)

    132:       call VecCopy(x,w,ierr);CHKERRA(ierr)
    133:       call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
    134:       v = abs(norm-2.0*sqrt(nfloat))
    135:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
    136:       if (rank .eq. 0) write(6,140) v
    137:  140  format ('VecCopy ',1pe9.2)

    139:       call VecAXPY(y,three,x,ierr);CHKERRA(ierr)
    140:       call VecNorm(y,NORM_2,norm,ierr);CHKERRA(ierr)
    141:       v = abs(norm-8.0*sqrt(nfloat))
    142:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
    143:       if (rank .eq. 0) write(6,150) v
    144:  150  format ('VecAXPY ',1pe9.2)

    146:       call VecAYPX(y,two,x,ierr);CHKERRA(ierr)
    147:       call VecNorm(y,NORM_2,norm,ierr);CHKERRA(ierr)
    148:       v = abs(norm-18.0*sqrt(nfloat))
    149:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
    150:       if (rank .eq. 0) write(6,160) v
    151:  160  format ('VecAYXP ',1pe9.2)

    153:       call VecSwap(x,y,ierr);CHKERRA(ierr)
    154:       call VecNorm(y,NORM_2,norm,ierr);CHKERRA(ierr)
    155:       v = abs(norm-2.0*sqrt(nfloat))
    156:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
    157:       if (rank .eq. 0) write(6,170) v
    158:  170  format ('VecSwap ',1pe9.2)

    160:       call VecNorm(x,NORM_2,norm,ierr);CHKERRA(ierr)
    161:       v = abs(norm-18.0*sqrt(nfloat))
    162:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
    163:       if (rank .eq. 0) write(6,180) v
    164:  180  format ('VecSwap ',1pe9.2)

    166:       call VecWAXPY(w,two,x,y,ierr);CHKERRA(ierr)
    167:       call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
    168:       v = abs(norm-38.0*sqrt(nfloat))
    169:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
    170:       if (rank .eq. 0) write(6,190) v
    171:  190  format ('VecWAXPY ',1pe9.2)

    173:       call VecPointwiseMult(w,y,x,ierr);CHKERRA(ierr)
    174:       call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
    175:       v = abs(norm-36.0*sqrt(nfloat))
    176:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
    177:       if (rank .eq. 0) write(6,200) v
    178:  200  format ('VecPointwiseMult ',1pe9.2)

    180:       call VecPointwiseDivide(w,x,y,ierr);CHKERRA(ierr)
    181:       call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
    182:       v = abs(norm-9.0*sqrt(nfloat))
    183:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
    184:       if (rank .eq. 0) write(6,210) v
    185:  210  format ('VecPointwiseDivide ',1pe9.2)

    188:       dots(1) = one
    189:       dots(2) = three
    190:       dots(3) = two
    191:       call VecSet(x,one,ierr);CHKERRA(ierr)
    192:       call VecMAXPY(x,ithree,dots,z,ierr);CHKERRA(ierr)
    193:       call VecNorm(z(1),NORM_2,norm,ierr);CHKERRA(ierr)
    194:       v = abs(norm-sqrt(nfloat))
    195:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
    196:       call VecNorm(z(2),NORM_2,norm,ierr);CHKERRA(ierr)
    197:       v1 = abs(norm-2.0*sqrt(nfloat))
    198:       if (v1 .gt. -tol .and. v1 .lt. tol) v1 = 0.0
    199:       call VecNorm(z(3),NORM_2,norm,ierr);CHKERRA(ierr)
    200:       v2 = abs(norm-3.0*sqrt(nfloat))
    201:       if (v2 .gt. -tol .and. v2 .lt. tol) v2 = 0.0
    202:       if (rank .eq. 0) write(6,220) v,v1,v2
    203:  220  format ('VecMAXPY ',3(1pe9.2))

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

    209:       call VecDestroy(x,ierr);CHKERRA(ierr)
    210:       call VecDestroy(y,ierr);CHKERRA(ierr)
    211:       call VecDestroy(w,ierr);CHKERRA(ierr)
    212:       call VecDestroyVecsF90(ithree,z,ierr);CHKERRA(ierr)
    213:       call PetscFinalize(ierr)

    215:       end

    217: !/*TEST
    218: !
    219: !     test:
    220: !
    221: !TEST*/