Actual source code: ex1f90.F

  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: !     petsc.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 include/finclude/petsc.h
 30:  #include include/finclude/petscvec.h
 31: #include "include/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:       double precision norm,v,v1,v2
 44:       integer          n,ierr,flg,rank
 45:       PetscScalar      one,two,three,dots(3),dot

 47: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 48: !                 Beginning of program
 49: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

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

 59: !  Create a vector, specifying only its global dimension.
 60: !  When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
 61: !  the vector format (currently parallel
 62: !  or sequential) is determined at runtime.  Also, the parallel
 63: !  partitioning of the vector is determined by PETSc at runtime.
 64: !
 65: !  Routines for creating particular vector types directly are:
 66: !     VecCreateSeq() - uniprocessor vector
 67: !     VecCreateMPI() - distributed vector, where the user can
 68: !                      determine the parallel partitioning

 70:       call VecCreate(PETSC_COMM_WORLD,x,ierr)
 71:       call VecSetSizes(x,PETSC_DECIDE,n,ierr)
 72:       call VecSetFromOptions(x,ierr)

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

 77:       call VecDuplicate(x,y,ierr)
 78:       call VecDuplicate(x,w,ierr)

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

 85:       call VecDuplicateVecsF90(x,3,z,ierr)

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

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

 95: !  Demonstrate various basic vector routines.

 97:       call VecDot(x,x,dot,ierr)
 98:       call VecMDot(x,3,z,dots,ierr)

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

221:       end
222: