Actual source code: ex1f90.F

petsc-dev 2014-08-19
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
 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: !     petscvec.h90  - to allow access to Fortran90 features of vectors
 23: !
 24: !  Additional include statements may be needed if using additional
 25: !  PETSc routines in a Fortran program, e.g.,
 26: !     petscviewer.h - viewers
 27: !     petscis.h     - index sets
 28: !
 29: #include <finclude/petscsys.h>
 30: #include <finclude/petscvec.h>
 31: #include <finclude/petscvec.h90>
 32: !
 33: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 34: !                   Variable declarations
 35: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 36: !
 37: !  Variables:
 38: !     x, y, w - vectors
 39: !     z       - array of vectors
 40: !
 41:       Vec              x,y,w
 42:       Vec, pointer :: z(:)
 43:       PetscReal norm,v,v1,v2
 44:       PetscInt         n,ithree
 45:       PetscErrorCode   ierr
 46:       PetscMPIInt      rank
 47:       PetscBool        flg
 48:       PetscScalar      one,two,three
 49:       PetscScalar      dots(3),dot

 51: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 52: !                 Beginning of program
 53: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 55:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 56:       one   = 1.0
 57:       two   = 2.0
 58:       three = 3.0
 59:       n     = 20
 60:       ithree = 3

 62:       call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',n,flg,ierr)
 63:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)

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

 76:       call VecCreate(PETSC_COMM_WORLD,x,ierr)
 77:       call VecSetSizes(x,PETSC_DECIDE,n,ierr)
 78:       call VecSetFromOptions(x,ierr)

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

 83:       call VecDuplicate(x,y,ierr)
 84:       call VecDuplicate(x,w,ierr)

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

 91:       call VecDuplicateVecsF90(x,ithree,z,ierr)

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

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

101: !  Demonstrate various basic vector routines.

103:       call VecDot(x,x,dot,ierr)
104:       call VecMDot(x,ithree,z,dots,ierr)

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

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

126:       call VecScale(x,two,ierr)
127:       call VecNorm(x,NORM_2,norm,ierr)
128:       v = norm-2.0*sqrt(dble(n))
129:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
130:       if (rank .eq. 0) write(6,130) v
131:  130  format ("VecScale ",1pe8.2)

133:       call VecCopy(x,w,ierr)
134:       call VecNorm(w,NORM_2,norm,ierr)
135:       v = norm-2.0*sqrt(dble(n))
136:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
137:       if (rank .eq. 0) write(6,140) v
138:  140  format ("VecCopy ",1pe8.2)

140:       call VecAXPY(y,three,x,ierr)
141:       call VecNorm(y,NORM_2,norm,ierr)
142:       v = norm-8.0*sqrt(dble(n))
143:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
144:       if (rank .eq. 0) write(6,150) v
145:  150  format ("VecAXPY ",1pe8.2)

147:       call VecAYPX(y,two,x,ierr)
148:       call VecNorm(y,NORM_2,norm,ierr)
149:       v = norm-18.0*sqrt(dble(n))
150:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
151:       if (rank .eq. 0) write(6,160) v
152:  160  format ("VecAYXP ",1pe8.2)

154:       call VecSwap(x,y,ierr)
155:       call VecNorm(y,NORM_2,norm,ierr)
156:       v = norm-2.0*sqrt(dble(n))
157:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
158:       if (rank .eq. 0) write(6,170) v
159:  170  format ("VecSwap ",1pe8.2)

161:       call VecNorm(x,NORM_2,norm,ierr)
162:       v = norm-18.0*sqrt(dble(n))
163:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
164:       if (rank .eq. 0) write(6,180) v
165:  180  format ("VecSwap ",1pe8.2)

167:       call VecWAXPY(w,two,x,y,ierr)
168:       call VecNorm(w,NORM_2,norm,ierr)
169:       v = norm-38.0*sqrt(dble(n))
170:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
171:       if (rank .eq. 0) write(6,190) v
172:  190  format ("VecWAXPY ",1pe8.2)

174:       call VecPointwiseMult(w,y,x,ierr)
175:       call VecNorm(w,NORM_2,norm,ierr)
176:       v = norm-36.0*sqrt(dble(n))
177:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
178:       if (rank .eq. 0) write(6,200) v
179:  200  format ("VecPointwiseMult ",1pe8.2)

181:       call VecPointwiseDivide(w,x,y,ierr)
182:       call VecNorm(w,NORM_2,norm,ierr)
183:       v = norm-9.0*sqrt(dble(n))
184:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
185:       if (rank .eq. 0) write(6,210) v
186:  210  format ("VecPointwiseDivide ",1pe8.2)


189:       dots(1) = one
190:       dots(2) = three
191:       dots(3) = two
192:       call VecSet(x,one,ierr)
193:       call VecMAXPY(x,ithree,dots,z,ierr)
194:       call VecNorm(z(1),NORM_2,norm,ierr)
195:       v = norm-sqrt(dble(n))
196:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
197:       call VecNorm(z(2),NORM_2,norm,ierr)
198:       v1 = norm-2.0*sqrt(dble(n))
199:       if (v1 .gt. -1.d-10 .and. v1 .lt. 1.d-10) v1 = 0.0
200:       call VecNorm(z(3),NORM_2,norm,ierr)
201:       v2 = norm-3.0*sqrt(dble(n))
202:       if (v2 .gt. -1.d-10 .and. v2 .lt. 1.d-10) v2 = 0.0
203:       if (rank .eq. 0) write(6,220) v,v1,v2
204:  220  format ("VecMAXPY ",3(1pe8.2))

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

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

215:       end