Actual source code: ex1f.F

  1: !
  2: !    "$Id: ex1f.F,v 1.30 2001/01/17 22:21:32 bsmith Exp $"
  3: !
  4: !   Program usage:  mpirun ex1f [-help] [all PETSc options]
  5: !
  6: !/*T
  7: !   Concepts: vectors^basic routines
  8: !   Processors: n
  9: !T*/
 10: !
 11: ! -----------------------------------------------------------------------

 13:       program main
 14:       implicit none

 16: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 17: !                    Include files
 18: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 19: !
 20: !  The following include statements are required for Fortran programs
 21: !  that use PETSc vectors:
 22: !     petsc.h       - base PETSc routines
 23: !     petscvec.h    - vectors
 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 include/finclude/petsc.h
 30:  #include include/finclude/petscvec.h
 31: !
 32: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 33: !                   Variable declarations
 34: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 35: !
 36: !  Variables:
 37: !     x, y, w - vectors
 38: !     z       - array of vectors

 40:       Vec              x,y,w,z(5)
 41:       double precision norm,v,v1,v2
 42:       integer          n,ierr,flg,rank
 43:       Scalar           one,two,three,dots(3),dot

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

 49:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 50:       one   = 1.0
 51:       two   = 2.0
 52:       three = 3.0
 53:       n     = 20
 54:       call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',n,flg,ierr)
 55:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)

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

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

 79:       call VecDuplicate(x,y,ierr)
 80:       call VecDuplicate(x,w,ierr)

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

 87:       call VecDuplicateVecs(x,3,z,ierr)

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

 91:       call VecSet(one,x,ierr)
 92:       call VecSet(two,y,ierr)
 93:       call VecSet(one,z(1),ierr)
 94:       call VecSet(two,z(2),ierr)
 95:       call VecSet(three,z(3),ierr)

 97: !  Demonstrate various basic vector routines.

 99:       call VecDot(x,x,dot,ierr)
100:       call VecMDot(3,x,z,dots,ierr)

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

106:       if (rank .eq. 0) then
107: #if defined(PETSC_USE_COMPLEX)
108:          write(6,100) int(PetscRealPart(dot))
109:          write(6,110) int(PetscRealPart(dots(1))),                               &
110:      &                int(PetscRealPart(dots(2))),                               &
111:      &                int(PetscRealPart(dots(3)))
112: #else
113:          write(6,100) int(dot)
114:          write(6,110) int(dots(1)),int(dots(2)),int(dots(3))
115: #endif
116:          write(6,120)
117:       endif
118:  100  format ("Vector length ",i6)
119:  110  format ("Vector length ",3(i6))
120:  120  format ("All other values should be near zero")

122:       call VecScale(two,x,ierr)
123:       call VecNorm(x,NORM_2,norm,ierr)
124:       v = norm-2.0*sqrt(dble(n))
125:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
126:       if (rank .eq. 0) write(6,130) v
127:  130  format ("VecScale ",1pe8.2)

129:       call VecCopy(x,w,ierr)
130:       call VecNorm(w,NORM_2,norm,ierr)
131:       v = norm-2.0*sqrt(dble(n))
132:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
133:       if (rank .eq. 0) write(6,140) v
134:  140  format ("VecCopy ",1pe8.2)

136:       call VecAXPY(three,x,y,ierr)
137:       call VecNorm(y,NORM_2,norm,ierr)
138:       v = norm-8.0*sqrt(dble(n))
139:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
140:       if (rank .eq. 0) write(6,150) v
141:  150  format ("VecAXPY ",1pe8.2)

143:       call VecAYPX(two,x,y,ierr)
144:       call VecNorm(y,NORM_2,norm,ierr)
145:       v = norm-18.0*sqrt(dble(n))
146:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
147:       if (rank .eq. 0) write(6,160) v
148:  160  format ("VecAYXP ",1pe8.2)

150:       call VecSwap(x,y,ierr)
151:       call VecNorm(y,NORM_2,norm,ierr)
152:       v = norm-2.0*sqrt(dble(n))
153:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
154:       if (rank .eq. 0) write(6,170) v
155:  170  format ("VecSwap ",1pe8.2)

157:       call VecNorm(x,NORM_2,norm,ierr)
158:       v = norm-18.0*sqrt(dble(n))
159:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
160:       if (rank .eq. 0) write(6,180) v
161:  180  format ("VecSwap ",1pe8.2)

163:       call VecWAXPY(two,x,y,w,ierr)
164:       call VecNorm(w,NORM_2,norm,ierr)
165:       v = norm-38.0*sqrt(dble(n))
166:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
167:       if (rank .eq. 0) write(6,190) v
168:  190  format ("VecWAXPY ",1pe8.2)

170:       call VecPointwiseMult(y,x,w,ierr)
171:       call VecNorm(w,NORM_2,norm,ierr)
172:       v = norm-36.0*sqrt(dble(n))
173:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
174:       if (rank .eq. 0) write(6,200) v
175:  200  format ("VecPointwiseMult ",1pe8.2)

177:       call VecPointwiseDivide(x,y,w,ierr)
178:       call VecNorm(w,NORM_2,norm,ierr)
179:       v = norm-9.0*sqrt(dble(n))
180:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
181:       if (rank .eq. 0) write(6,210) v
182:  210  format ("VecPointwiseDivide ",1pe8.2)

184: 
185:       dots(1) = one
186:       dots(2) = three
187:       dots(3) = two
188:       call VecSet(one,x,ierr)
189:       call VecMAXPY(3,dots,x,z,ierr)
190:       call VecNorm(z(1),NORM_2,norm,ierr)
191:       v = norm-sqrt(dble(n))
192:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
193:       call VecNorm(z(2),NORM_2,norm,ierr)
194:       v1 = norm-2.0*sqrt(dble(n))
195:       if (v1 .gt. -1.d-10 .and. v1 .lt. 1.d-10) v1 = 0.0
196:       call VecNorm(z(3),NORM_2,norm,ierr)
197:       v2 = norm-3.0*sqrt(dble(n))
198:       if (v2 .gt. -1.d-10 .and. v2 .lt. 1.d-10) v2 = 0.0
199:       if (rank .eq. 0) write(6,220) v,v1,v2
200:  220  format ("VecMAXPY ",3(1pe8.2))


203: !  Test whether vector has been corrupted (just to demonstrate this
204: !  routine) not needed in most application codes.

206:       call VecValid(x,flg,ierr)
207:       if (flg .ne. PETSC_TRUE) then
208:          if (rank .eq. 0) then
209:             write(6,*) 'Corrupted vector!'
210:          endif
211:          SETERRQ(1,' ',ierr)
212:       endif

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(z,3,ierr)
221:       call PetscFinalize(ierr)

223:       end
224: