Actual source code: ex1f.F

petsc-3.6.1 2015-08-06
Report Typos and Errors
  1: !
  2: !
  3: !/*T
  4: !   Concepts: vectors^basic routines
  5: !   Processors: n
  6: !T*/
  7: !
  8: ! -----------------------------------------------------------------------

 10:       program main
 11:       implicit none

 13: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 14: !                    Include files
 15: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 16: !
 17: !  The following include statements are required for Fortran programs
 18: !  that use PETSc vectors:
 19: !     petscsys.h       - base PETSc routines
 20: !     petscvec.h    - vectors
 21: !  Additional include statements may be needed if using additional
 22: !  PETSc routines in a Fortran program, e.g.,
 23: !     petscviewer.h - viewers
 24: !     petscis.h     - index sets
 25: !
 26: #include <petsc/finclude/petscsys.h>
 27: #include <petsc/finclude/petscvec.h>

 29: !
 30: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 31: !                   Variable declarations
 32: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 33: !
 34: !  Variables:
 35: !     x, y, w - vectors
 36: !     z       - array of vectors

 38:       Vec              x,y,w,z(5)
 39:       PetscReal        norm,v,v1,v2
 40:       PetscInt           n,ithree
 41:       PetscBool  flg
 42:       PetscErrorCode ierr
 43:       PetscMPIInt  rank
 44:       PetscScalar  one,two,three
 45:       PetscScalar  dots(3),dot
 46:       character*(40)   name

 48: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 49: !                 Beginning of program
 50: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 52:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 53:       one   = 1.0
 54:       two   = 2.0
 55:       three = 3.0
 56:       n     = 20
 57:       ithree = 3
 58:       call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',n,flg,ierr)
 59:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)

 61: !  Create a vector, specifying only its global dimension.
 62: !  When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
 63: !  the vector format (currently parallel
 64: !  or sequential) is determined at runtime.  Also, the parallel
 65: !  partitioning of the vector is determined by PETSc at runtime.
 66: !
 67: !  Routines for creating particular vector types directly are:
 68: !     VecCreateSeq() - uniprocessor vector
 69: !     VecCreateMPI() - distributed vector, where the user can
 70: !                      determine the parallel partitioning
 71: !     VecCreateShared() - parallel vector that uses shared memory
 72: !                         (available only on the SGI); otherwise,
 73: !                         is the same as VecCreateMPI()
 74: !
 75: !     VecCreate(), VecSetSizes() and VecSetFromOptions() allows one
 76: !                 to determine at runtime which version to use
 77: !                 with the options -vec_type mpi or -vec_type shared
 78: !
 79:       call VecCreate(PETSC_COMM_WORLD,x,ierr)
 80:       call VecSetSizes(x,PETSC_DECIDE,n,ierr)
 81:       call VecSetFromOptions(x,ierr)
 82:       call VecGetType(x,name,ierr)

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

 87:       call VecDuplicate(x,y,ierr)
 88:       call VecDuplicate(x,w,ierr)

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

 95:       call VecDuplicateVecs(x,ithree,z,ierr)

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

 99:       call VecSet(x,one,ierr)

101:       call VecSet(y,two,ierr)
102:       call VecSet(z(1),one,ierr)
103:       call VecSet(z(2),two,ierr)
104:       call VecSet(z(3),three,ierr)

106: !  Demonstrate various basic vector routines.

108:       call VecDot(x,x,dot,ierr)
109:       call VecMDot(x,ithree,z,dots,ierr)

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

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

131:       call VecScale(x,two,ierr)
132:       call VecNorm(x,NORM_2,norm,ierr)
133:       v = norm-2.0*sqrt(dble(n))
134:       if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
135:       if (rank .eq. 0) write(6,130) v
136:  130  format ('VecScale ',1pe8.2)

138:       call VecCopy(x,w,ierr)
139:       call VecNorm(w,NORM_2,norm,ierr)
140:       v = norm-2.0*sqrt(dble(n))
141:       if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
142:       if (rank .eq. 0) write(6,140) v
143:  140  format ('VecCopy ',1pe8.2)

145:       call VecAXPY(y,three,x,ierr)
146:       call VecNorm(y,NORM_2,norm,ierr)
147:       v = norm-8.0*sqrt(dble(n))
148:       if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
149:       if (rank .eq. 0) write(6,150) v
150:  150  format ('VecAXPY ',1pe8.2)

152:       call VecAYPX(y,two,x,ierr)
153:       call VecNorm(y,NORM_2,norm,ierr)
154:       v = norm-18.0*sqrt(dble(n))
155:       if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
156:       if (rank .eq. 0) write(6,160) v
157:  160  format ('VecAYXP ',1pe8.2)

159:       call VecSwap(x,y,ierr)
160:       call VecNorm(y,NORM_2,norm,ierr)
161:       v = norm-2.0*sqrt(dble(n))
162:       if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
163:       if (rank .eq. 0) write(6,170) v
164:  170  format ('VecSwap ',1pe8.2)

166:       call VecNorm(x,NORM_2,norm,ierr)
167:       v = norm-18.0*sqrt(dble(n))
168:       if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
169:       if (rank .eq. 0) write(6,180) v
170:  180  format ('VecSwap ',1pe8.2)

172:       call VecWAXPY(w,two,x,y,ierr)
173:       call VecNorm(w,NORM_2,norm,ierr)
174:       v = norm-38.0*sqrt(dble(n))
175:       if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
176:       if (rank .eq. 0) write(6,190) v
177:  190  format ('VecWAXPY ',1pe8.2)

179:       call VecPointwiseMult(w,y,x,ierr)
180:       call VecNorm(w,NORM_2,norm,ierr)
181:       v = norm-36.0*sqrt(dble(n))
182:       if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
183:       if (rank .eq. 0) write(6,200) v
184:  200  format ('VecPointwiseMult ',1pe8.2)

186:       call VecPointwiseDivide(w,x,y,ierr)
187:       call VecNorm(w,NORM_2,norm,ierr)
188:       v = norm-9.0*sqrt(dble(n))
189:       if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
190:       if (rank .eq. 0) write(6,210) v
191:  210  format ('VecPointwiseDivide ',1pe8.2)


194:       dots(1) = one
195:       dots(2) = three
196:       dots(3) = two
197:       call VecSet(x,one,ierr)
198:       call VecMAXPY(x,ithree,dots,z,ierr)
199:       call VecNorm(z(1),NORM_2,norm,ierr)
200:       v = norm-sqrt(dble(n))
201:       if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
202:       call VecNorm(z(2),NORM_2,norm,ierr)
203:       v1 = norm-2.0*sqrt(dble(n))
204:       if (v1 .gt. -PETSC_SMALL .and. v1 .lt. PETSC_SMALL) v1 = 0.0
205:       call VecNorm(z(3),NORM_2,norm,ierr)
206:       v2 = norm-3.0*sqrt(dble(n))
207:       if (v2 .gt. -PETSC_SMALL .and. v2 .lt. PETSC_SMALL) v2 = 0.0
208:       if (rank .eq. 0) write(6,220) v,v1,v2
209:  220  format ('VecMAXPY ',3(1pe8.2))

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

214:       call VecDestroy(x,ierr)
215:       call VecDestroy(y,ierr)
216:       call VecDestroy(w,ierr)
217:       call VecDestroyVecs(ithree,z,ierr)
218:       call PetscFinalize(ierr)

220:       end