Actual source code: ex1f.F

petsc-3.3-p7 2013-05-11
  1: !
  2: !   Program usage:  mpiexec ex1f [-help] [all PETSc options]
  3: !
  4: !/*T
  5: !   Concepts: vectors^basic 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: !  Additional include statements may be needed if using additional
 23: !  PETSc routines in a Fortran program, e.g.,
 24: !     petscviewer.h - viewers
 25: !     petscis.h     - index sets
 26: !
 27: #include <finclude/petscsys.h>
 28: #include <finclude/petscvec.h>

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

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

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

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

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

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

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

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

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

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

100:       call VecSet(x,one,ierr)

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

107: !  Demonstrate various basic vector routines.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

221:       end
222: