Actual source code: ex1f90.F

petsc-3.7.5 2017-01-01
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 <petsc/finclude/petscsys.h>
 30: #include <petsc/finclude/petscvec.h>
 31: #include <petsc/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
 50:       PetscReal        nfloat

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

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

 63:       call PetscOptionsGetInt(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,           &
 64:      &                        '-n',n,flg,ierr)
 65:       nfloat = n
 66:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)

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

 79:       call VecCreate(PETSC_COMM_WORLD,x,ierr)
 80:       call VecSetSizes(x,PETSC_DECIDE,n,ierr)
 81:       call VecSetFromOptions(x,ierr)

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

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

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

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

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

 98:       call VecSet(x,one,ierr)
 99:       call VecSet(y,two,ierr)
100:       call VecSet(z(1),one,ierr)
101:       call VecSet(z(2),two,ierr)
102:       call VecSet(z(3),three,ierr)

104: !  Demonstrate various basic vector routines.

106:       call VecDot(x,x,dot,ierr)
107:       call VecMDot(x,ithree,z,dots,ierr)

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

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

129:       call VecScale(x,two,ierr)
130:       call VecNorm(x,NORM_2,norm,ierr)
131:       v = abs(norm-2.0*sqrt(nfloat))
132:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
133:       if (rank .eq. 0) write(6,130) v
134:  130  format ('VecScale ',1pe8.2)

136:       call VecCopy(x,w,ierr)
137:       call VecNorm(w,NORM_2,norm,ierr)
138:       v = abs(norm-2.0*sqrt(nfloat))
139:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
140:       if (rank .eq. 0) write(6,140) v
141:  140  format ('VecCopy ',1pe8.2)

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

150:       call VecAYPX(y,two,x,ierr)
151:       call VecNorm(y,NORM_2,norm,ierr)
152:       v = abs(norm-18.0*sqrt(nfloat))
153:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
154:       if (rank .eq. 0) write(6,160) v
155:  160  format ('VecAYXP ',1pe8.2)

157:       call VecSwap(x,y,ierr)
158:       call VecNorm(y,NORM_2,norm,ierr)
159:       v = abs(norm-2.0*sqrt(nfloat))
160:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
161:       if (rank .eq. 0) write(6,170) v
162:  170  format ('VecSwap ',1pe8.2)

164:       call VecNorm(x,NORM_2,norm,ierr)
165:       v = abs(norm-18.0*sqrt(nfloat))
166:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
167:       if (rank .eq. 0) write(6,180) v
168:  180  format ('VecSwap ',1pe8.2)

170:       call VecWAXPY(w,two,x,y,ierr)
171:       call VecNorm(w,NORM_2,norm,ierr)
172:       v = abs(norm-38.0*sqrt(nfloat))
173:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
174:       if (rank .eq. 0) write(6,190) v
175:  190  format ('VecWAXPY ',1pe8.2)

177:       call VecPointwiseMult(w,y,x,ierr)
178:       call VecNorm(w,NORM_2,norm,ierr)
179:       v = abs(norm-36.0*sqrt(nfloat))
180:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
181:       if (rank .eq. 0) write(6,200) v
182:  200  format ('VecPointwiseMult ',1pe8.2)

184:       call VecPointwiseDivide(w,x,y,ierr)
185:       call VecNorm(w,NORM_2,norm,ierr)
186:       v = abs(norm-9.0*sqrt(nfloat))
187:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
188:       if (rank .eq. 0) write(6,210) v
189:  210  format ('VecPointwiseDivide ',1pe8.2)


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

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

212:       call VecDestroy(x,ierr)
213:       call VecDestroy(y,ierr)
214:       call VecDestroy(w,ierr)
215:       call VecDestroyVecsF90(ithree,z,ierr)
216:       call PetscFinalize(ierr)

218:       end