Actual source code: ex20f90.F90

  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

 13: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 14: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 15: !
 16: !
 17: !     This examples uses Fortran 90 MODULES instead of include files
 18: !
 19: #include <petsc/finclude/petscvec.h>
 20:       use petscvec
 21:       implicit none

 23: !
 24: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 25: !                   Variable declarations
 26: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 27: !
 28: !  Variables:
 29: !     x, y, w - vectors
 30: !     z       - array of vectors
 31: !
 32:       type(tVec)       x,y,w
 33:       type(tVec), pointer :: z(:)

 35:       PetscReal norm,v,v1,v2,tol
 36:       PetscInt  n,ithree
 37:       PetscErrorCode ierr
 38:       PetscMPIInt  rank
 39:       PetscBool       flg
 40:       PetscScalar      one,two,three
 41:       PetscScalar      dots(3),dot
 42:       PetscReal        nfloat

 44: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 45: !                 Beginning of program
 46: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 48:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 49:       if (ierr .ne. 0) then
 50:          print*,'Unable to initialize PETSc'
 51:          stop
 52:       endif

 54:       tol   = 1.e-10_PETSC_REAL_KIND
 55:       one   = 1.0
 56:       two   = 2.0
 57:       three = 3.0
 58:       n     = 20
 59:       ithree = 3

 61:       call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-n',n,flg,ierr);CHKERRA(ierr)
 62:       nfloat = n
 63:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr);CHKERRA(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);CHKERRA(ierr)
 77:       call VecSetSizes(x,PETSC_DECIDE,n,ierr);CHKERRA(ierr)
 78:       call VecSetFromOptions(x,ierr);CHKERRA(ierr)

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

 83:       call VecDuplicate(x,y,ierr);CHKERRA(ierr)
 84:       call VecDuplicate(x,w,ierr);CHKERRA(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);CHKERRA(ierr)

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

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

101: !  Demonstrate various basic vector routines.

103:       call VecDot(x,x,dot,ierr);CHKERRA(ierr)
104:       call VecMDot(x,ithree,z,dots,ierr);CHKERRA(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))),int(PetscRealPart(dots(2))),int(PetscRealPart(dots(3)))
114: #else
115:          write(6,100) int(dot)
116:          write(6,110) int(dots(1)),int(dots(2)),int(dots(3))
117: #endif
118:          write(6,120)
119:       endif
120:  100  format ('Vector length ',i6)
121:  110  format ('Vector length ',3(i6))
122:  120  format ('All other values should be near zero')

124:       call VecScale(x,two,ierr);CHKERRA(ierr)
125:       call VecNorm(x,NORM_2,norm,ierr);CHKERRA(ierr)
126:       v = abs(norm-2.0*sqrt(nfloat))
127:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
128:       if (rank .eq. 0) write(6,130) v
129:  130  format ('VecScale ',1pe9.2)

131:       call VecCopy(x,w,ierr);CHKERRA(ierr)
132:       call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
133:       v = abs(norm-2.0*sqrt(nfloat))
134:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
135:       if (rank .eq. 0) write(6,140) v
136:  140  format ('VecCopy ',1pe9.2)

138:       call VecAXPY(y,three,x,ierr);CHKERRA(ierr)
139:       call VecNorm(y,NORM_2,norm,ierr);CHKERRA(ierr)
140:       v = abs(norm-8.0*sqrt(nfloat))
141:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
142:       if (rank .eq. 0) write(6,150) v
143:  150  format ('VecAXPY ',1pe9.2)

145:       call VecAYPX(y,two,x,ierr);CHKERRA(ierr)
146:       call VecNorm(y,NORM_2,norm,ierr);CHKERRA(ierr)
147:       v = abs(norm-18.0*sqrt(nfloat))
148:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
149:       if (rank .eq. 0) write(6,160) v
150:  160  format ('VecAYXP ',1pe9.2)

152:       call VecSwap(x,y,ierr);CHKERRA(ierr)
153:       call VecNorm(y,NORM_2,norm,ierr);CHKERRA(ierr)
154:       v = abs(norm-2.0*sqrt(nfloat))
155:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
156:       if (rank .eq. 0) write(6,170) v
157:  170  format ('VecSwap ',1pe9.2)

159:       call VecNorm(x,NORM_2,norm,ierr);CHKERRA(ierr)
160:       v = abs(norm-18.0*sqrt(nfloat))
161:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
162:       if (rank .eq. 0) write(6,180) v
163:  180  format ('VecSwap ',1pe9.2)

165:       call VecWAXPY(w,two,x,y,ierr);CHKERRA(ierr)
166:       call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
167:       v = abs(norm-38.0*sqrt(nfloat))
168:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
169:       if (rank .eq. 0) write(6,190) v
170:  190  format ('VecWAXPY ',1pe9.2)

172:       call VecPointwiseMult(w,y,x,ierr);CHKERRA(ierr)
173:       call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
174:       v = abs(norm-36.0*sqrt(nfloat))
175:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
176:       if (rank .eq. 0) write(6,200) v
177:  200  format ('VecPointwiseMult ',1pe9.2)

179:       call VecPointwiseDivide(w,x,y,ierr);CHKERRA(ierr)
180:       call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
181:       v = abs(norm-9.0*sqrt(nfloat))
182:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
183:       if (rank .eq. 0) write(6,210) v
184:  210  format ('VecPointwiseDivide ',1pe9.2)


187:       dots(1) = one
188:       dots(2) = three
189:       dots(3) = two
190:       call VecSet(x,one,ierr);CHKERRA(ierr)
191:       call VecMAXPY(x,ithree,dots,z,ierr);CHKERRA(ierr)
192:       call VecNorm(z(1),NORM_2,norm,ierr);CHKERRA(ierr)
193:       v = abs(norm-sqrt(nfloat))
194:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
195:       call VecNorm(z(2),NORM_2,norm,ierr);CHKERRA(ierr)
196:       v1 = abs(norm-2.0*sqrt(nfloat))
197:       if (v1 .gt. -tol .and. v1 .lt. tol) v1 = 0.0
198:       call VecNorm(z(3),NORM_2,norm,ierr);CHKERRA(ierr)
199:       v2 = abs(norm-3.0*sqrt(nfloat))
200:       if (v2 .gt. -tol .and. v2 .lt. tol) v2 = 0.0
201:       if (rank .eq. 0) write(6,220) v,v1,v2
202:  220  format ('VecMAXPY ',3(1pe9.2))


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

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

214:       end

216: !/*TEST
217: !
218: !     test:
219: !
220: !TEST*/