Actual source code: ex1f.F90

petsc-3.9.4 2018-09-11
Report Typos and Errors
  1: !
  2: !
  3: !/*T
  4: !   Concepts: vectors^basic routines
  5: !   Processors: n
  6: !T*/
  7: !
  8: ! -----------------------------------------------------------------------

 10:       program main
 11:  #include <petsc/finclude/petscvec.h>
 12:       use petscvec
 13:       implicit none

 15: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 16: !                   Variable declarations
 17: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 18: !
 19: !  Variables:
 20: !     x, y, w - vectors
 21: !     z       - array of vectors

 23:       Vec              x,y,w,z(5)
 24:       PetscReal        norm,v,v1,v2
 25:       PetscInt           n,ithree
 26:       PetscBool  flg
 27:       PetscErrorCode ierr
 28:       PetscMPIInt  rank
 29:       PetscScalar  one,two,three
 30:       PetscScalar  dots(3),dot
 31:       character*(40)   name
 32:       PetscReal    nfloat

 34: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 35: !                 Beginning of program
 36: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 38:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 39:       if (ierr .ne. 0) then
 40:         print*,'Unable to initialize PETSc'
 41:         stop
 42:       endif
 43:       one   = 1.0
 44:       two   = 2.0
 45:       three = 3.0
 46:       n     = 20
 47:       ithree = 3
 48:       call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-n',n,flg,ierr)
 49:       nfloat = n
 50:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)

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

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

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

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

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

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

 90:       call VecSet(x,one,ierr)

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

 97: !  Demonstrate various basic vector routines.

 99:       call VecDot(x,x,dot,ierr)
100:       call VecMDot(x,ithree,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))),int(PetscRealPart(dots(2))),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 = abs(norm-2.0*sqrt(nfloat))
123:       if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
124:       if (rank .eq. 0) write(6,130) v
125:  130  format ('VecScale ',1pe9.2)

127:       call VecCopy(x,w,ierr)
128:       call VecNorm(w,NORM_2,norm,ierr)
129:       v = abs(norm-2.0*sqrt(nfloat))
130:       if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
131:       if (rank .eq. 0) write(6,140) v
132:  140  format ('VecCopy ',1pe9.2)

134:       call VecAXPY(y,three,x,ierr)
135:       call VecNorm(y,NORM_2,norm,ierr)
136:       v = abs(norm-8.0*sqrt(nfloat))
137:       if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
138:       if (rank .eq. 0) write(6,150) v
139:  150  format ('VecAXPY ',1pe9.2)

141:       call VecAYPX(y,two,x,ierr)
142:       call VecNorm(y,NORM_2,norm,ierr)
143:       v = abs(norm-18.0*sqrt(nfloat))
144:       if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
145:       if (rank .eq. 0) write(6,160) v
146:  160  format ('VecAYXP ',1pe9.2)

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

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

161:       call VecWAXPY(w,two,x,y,ierr)
162:       call VecNorm(w,NORM_2,norm,ierr)
163:       v = abs(norm-38.0*sqrt(nfloat))
164:       if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
165:       if (rank .eq. 0) write(6,190) v
166:  190  format ('VecWAXPY ',1pe9.2)

168:       call VecPointwiseMult(w,y,x,ierr)
169:       call VecNorm(w,NORM_2,norm,ierr)
170:       v = abs(norm-36.0*sqrt(nfloat))
171:       if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
172:       if (rank .eq. 0) write(6,200) v
173:  200  format ('VecPointwiseMult ',1pe9.2)

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


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

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

203:       call VecDestroy(x,ierr)
204:       call VecDestroy(y,ierr)
205:       call VecDestroy(w,ierr)
206:       call VecDestroyVecs(ithree,z,ierr)
207:       call PetscFinalize(ierr)

209:       end

211: !
212: !/*TEST
213: !
214: !     test:
215: !       suffix: 1
216: !
217: !     test:
218: !       suffix: 2
219: !       nsize: 2
220: !
221: !TEST*/