Actual source code: ex1f.F

petsc-3.7.3 2016-08-01
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
 47:       PetscReal    nfloat

 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_OBJECT,PETSC_NULL_CHARACTER,         &
 60:      &                        '-n',n,flg,ierr)
 61:       nfloat = n
 62:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)

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

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

 90:       call VecDuplicate(x,y,ierr)
 91:       call VecDuplicate(x,w,ierr)

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

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

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

102:       call VecSet(x,one,ierr)

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

109: !  Demonstrate various basic vector routines.

111:       call VecDot(x,x,dot,ierr)
112:       call VecMDot(x,ithree,z,dots,ierr)

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

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

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

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

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

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

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

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

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

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

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


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

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

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

223:       end