Actual source code: ex20f90.F90

petsc-master 2016-12-03
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

 13: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 14: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 15: !
 16: !
 17: !     This examples uses Fortran 90 MODULES instead of include files
 18: !   see the manual page UsingFortran
 19: !
 20: #define PETSC_USE_FORTRAN_MODULES
 21:  #include <petsc/finclude/petscsysdef.h>
 22:  #include <petsc/finclude/petscvecdef.h>
 23: #if defined(PETSC_USE_FORTRAN_MODULES)
 24:       use petscvec
 25: #endif
 26:       implicit none
 27: #if !defined(PETSC_USE_FORTRAN_MODULES)
 28:  #include <petsc/finclude/petscsys.h>
 29:  #include <petsc/finclude/petscvec.h>
 30: #include <petsc/finclude/petscvec.h90>

 32: #endif
 33: !
 34: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 35: !                   Variable declarations
 36: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 37: !
 38: !  Variables:
 39: !     x, y, w - vectors
 40: !     z       - array of vectors
 41: !
 42: #if defined(PETSC_USE_FORTRAN_DATATYPES)
 43:       type(Vec)       x,y,w
 44:       type(Vec), pointer :: z(:)
 45: #else
 46:       Vec              x,y,w
 47:       Vec, pointer :: z(:)
 48: #endif
 49:       PetscReal norm,v,v1,v2,tol
 50:       PetscInt  n,ithree
 51:       PetscErrorCode ierr
 52:       PetscMPIInt  rank
 53:       PetscBool       flg
 54:       PetscScalar      one,two,three
 55:       PetscScalar      dots(3),dot
 56:       PetscReal        nfloat

 58: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 59: !                 Beginning of program
 60: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 62:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 63:       if (ierr .ne. 0) then
 64:          print*,'Unable to initialize PETSc'
 65:          stop
 66:       endif

 68:       tol   = 1.e-10_PETSC_REAL_KIND
 69:       one   = 1.0
 70:       two   = 2.0
 71:       three = 3.0
 72:       n     = 20
 73:       ithree = 3

 75:       call PetscOptionsGetInt(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,'-n',n,flg,ierr);CHKERRQ(ierr)
 76:       nfloat = n
 77:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr);CHKERRQ(ierr)

 79: !  Create a vector, specifying only its global dimension.
 80: !  When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
 81: !  the vector format (currently parallel
 82: !  or sequential) is determined at runtime.  Also, the parallel
 83: !  partitioning of the vector is determined by PETSc at runtime.
 84: !
 85: !  Routines for creating particular vector types directly are:
 86: !     VecCreateSeq() - uniprocessor vector
 87: !     VecCreateMPI() - distributed vector, where the user can
 88: !                      determine the parallel partitioning

 90:       call VecCreate(PETSC_COMM_WORLD,x,ierr);CHKERRQ(ierr)
 91:       call VecSetSizes(x,PETSC_DECIDE,n,ierr);CHKERRQ(ierr)
 92:       call VecSetFromOptions(x,ierr);CHKERRQ(ierr)

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

 97:       call VecDuplicate(x,y,ierr);CHKERRQ(ierr)
 98:       call VecDuplicate(x,w,ierr);CHKERRQ(ierr)

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

105:       call VecDuplicateVecsF90(x,ithree,z,ierr);CHKERRQ(ierr)

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

109:       call VecSet(x,one,ierr);CHKERRQ(ierr)
110:       call VecSet(y,two,ierr);CHKERRQ(ierr)
111:       call VecSet(z(1),one,ierr);CHKERRQ(ierr)
112:       call VecSet(z(2),two,ierr);CHKERRQ(ierr)
113:       call VecSet(z(3),three,ierr);CHKERRQ(ierr)

115: !  Demonstrate various basic vector routines.

117:       call VecDot(x,x,dot,ierr);CHKERRQ(ierr)
118:       call VecMDot(x,ithree,z,dots,ierr);CHKERRQ(ierr)

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

124:       if (rank .eq. 0) then
125: #if defined(PETSC_USE_COMPLEX)
126:          write(6,100) int(PetscRealPart(dot))
127:          write(6,110) int(PetscRealPart(dots(1))),int(PetscRealPart(dots(2))),int(PetscRealPart(dots(3)))
128: #else
129:          write(6,100) int(dot)
130:          write(6,110) int(dots(1)),int(dots(2)),int(dots(3))
131: #endif
132:          write(6,120)
133:       endif
134:  100  format ('Vector length ',i6)
135:  110  format ('Vector length ',3(i6))
136:  120  format ('All other values should be near zero')

138:       call VecScale(x,two,ierr);CHKERRQ(ierr)
139:       call VecNorm(x,NORM_2,norm,ierr);CHKERRQ(ierr)
140:       v = abs(norm-2.0*sqrt(nfloat))
141:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
142:       if (rank .eq. 0) write(6,130) v
143:  130  format ('VecScale ',1pe8.2)

145:       call VecCopy(x,w,ierr);CHKERRQ(ierr)
146:       call VecNorm(w,NORM_2,norm,ierr);CHKERRQ(ierr)
147:       v = abs(norm-2.0*sqrt(nfloat))
148:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
149:       if (rank .eq. 0) write(6,140) v
150:  140  format ('VecCopy ',1pe8.2)

152:       call VecAXPY(y,three,x,ierr);CHKERRQ(ierr)
153:       call VecNorm(y,NORM_2,norm,ierr);CHKERRQ(ierr)
154:       v = abs(norm-8.0*sqrt(nfloat))
155:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
156:       if (rank .eq. 0) write(6,150) v
157:  150  format ('VecAXPY ',1pe8.2)

159:       call VecAYPX(y,two,x,ierr);CHKERRQ(ierr)
160:       call VecNorm(y,NORM_2,norm,ierr);CHKERRQ(ierr)
161:       v = abs(norm-18.0*sqrt(nfloat))
162:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
163:       if (rank .eq. 0) write(6,160) v
164:  160  format ('VecAYXP ',1pe8.2)

166:       call VecSwap(x,y,ierr);CHKERRQ(ierr)
167:       call VecNorm(y,NORM_2,norm,ierr);CHKERRQ(ierr)
168:       v = abs(norm-2.0*sqrt(nfloat))
169:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
170:       if (rank .eq. 0) write(6,170) v
171:  170  format ('VecSwap ',1pe8.2)

173:       call VecNorm(x,NORM_2,norm,ierr);CHKERRQ(ierr)
174:       v = abs(norm-18.0*sqrt(nfloat))
175:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
176:       if (rank .eq. 0) write(6,180) v
177:  180  format ('VecSwap ',1pe8.2)

179:       call VecWAXPY(w,two,x,y,ierr);CHKERRQ(ierr)
180:       call VecNorm(w,NORM_2,norm,ierr);CHKERRQ(ierr)
181:       v = abs(norm-38.0*sqrt(nfloat))
182:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
183:       if (rank .eq. 0) write(6,190) v
184:  190  format ('VecWAXPY ',1pe8.2)

186:       call VecPointwiseMult(w,y,x,ierr);CHKERRQ(ierr)
187:       call VecNorm(w,NORM_2,norm,ierr);CHKERRQ(ierr)
188:       v = abs(norm-36.0*sqrt(nfloat))
189:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
190:       if (rank .eq. 0) write(6,200) v
191:  200  format ('VecPointwiseMult ',1pe8.2)

193:       call VecPointwiseDivide(w,x,y,ierr);CHKERRQ(ierr)
194:       call VecNorm(w,NORM_2,norm,ierr);CHKERRQ(ierr)
195:       v = abs(norm-9.0*sqrt(nfloat))
196:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
197:       if (rank .eq. 0) write(6,210) v
198:  210  format ('VecPointwiseDivide ',1pe8.2)


201:       dots(1) = one
202:       dots(2) = three
203:       dots(3) = two
204:       call VecSet(x,one,ierr);CHKERRQ(ierr)
205:       call VecMAXPY(x,ithree,dots,z,ierr);CHKERRQ(ierr)
206:       call VecNorm(z(1),NORM_2,norm,ierr);CHKERRQ(ierr)
207:       v = abs(norm-sqrt(nfloat))
208:       if (v .gt. -tol .and. v .lt. tol) v = 0.0
209:       call VecNorm(z(2),NORM_2,norm,ierr);CHKERRQ(ierr)
210:       v1 = abs(norm-2.0*sqrt(nfloat))
211:       if (v1 .gt. -tol .and. v1 .lt. tol) v1 = 0.0
212:       call VecNorm(z(3),NORM_2,norm,ierr);CHKERRQ(ierr)
213:       v2 = abs(norm-3.0*sqrt(nfloat))
214:       if (v2 .gt. -tol .and. v2 .lt. tol) v2 = 0.0
215:       if (rank .eq. 0) write(6,220) v,v1,v2
216:  220  format ('VecMAXPY ',3(1pe8.2))


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

222:       call VecDestroy(x,ierr);CHKERRQ(ierr)
223:       call VecDestroy(y,ierr);CHKERRQ(ierr)
224:       call VecDestroy(w,ierr);CHKERRQ(ierr)
225:       call VecDestroyVecsF90(ithree,z,ierr);CHKERRQ(ierr)
226:       call PetscFinalize(ierr)

228:       end