Actual source code: ex1f90.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
 12: #include <petsc/finclude/petscvec.h>
 13:       use petscvec
 14:       implicit none

 16: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 17: !                   Variable declarations
 18: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 19: !
 20: !  Variables:
 21: !     x, y, w - vectors
 22: !     z       - array of vectors
 23: !
 24:       Vec              x,y,w
 25:       Vec, pointer :: z(:)
 26:       PetscReal norm,v,v1,v2
 27:       PetscInt         n,ithree
 28:       PetscErrorCode   ierr
 29:       PetscMPIInt      rank
 30:       PetscBool        flg
 31:       PetscScalar      one,two,three
 32:       PetscScalar      dots(3),dot
 33:       PetscReal        nfloat

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

 39:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 40:       if (ierr .ne. 0) then
 41:         print*,'Unable to initialize PETSc'
 42:         stop
 43:       endif
 44:       one   = 1.0
 45:       two   = 2.0
 46:       three = 3.0
 47:       n     = 20
 48:       ithree = 3

 50:       call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-n',n,flg,ierr);CHKERRA(ierr)
 51:       nfloat = n
 52:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr);CHKERRA(ierr)

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

 65:       call VecCreate(PETSC_COMM_WORLD,x,ierr);CHKERRA(ierr)
 66:       call VecSetSizes(x,PETSC_DECIDE,n,ierr);CHKERRA(ierr)
 67:       call VecSetFromOptions(x,ierr);CHKERRA(ierr)

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

 72:       call VecDuplicate(x,y,ierr);CHKERRA(ierr)
 73:       call VecDuplicate(x,w,ierr);CHKERRA(ierr)

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

 80:       call VecDuplicateVecsF90(x,ithree,z,ierr);CHKERRA(ierr)

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

 84:       call VecSet(x,one,ierr);CHKERRA(ierr)
 85:       call VecSet(y,two,ierr);CHKERRA(ierr)
 86:       call VecSet(z(1),one,ierr);CHKERRA(ierr)
 87:       call VecSet(z(2),two,ierr);CHKERRA(ierr)
 88:       call VecSet(z(3),three,ierr);CHKERRA(ierr)

 90: !  Demonstrate various basic vector routines.

 92:       call VecDot(x,x,dot,ierr);CHKERRA(ierr)
 93:       call VecMDot(x,ithree,z,dots,ierr);CHKERRA(ierr)

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

 99:       if (rank .eq. 0) then
100: #if defined(PETSC_USE_COMPLEX)
101:          write(6,100) int(PetscRealPart(dot))
102:          write(6,110) int(PetscRealPart(dots(1))),int(PetscRealPart(dots(2))),int(PetscRealPart(dots(3)))
103: #else
104:          write(6,100) int(dot)
105:          write(6,110) int(dots(1)),int(dots(2)),int(dots(3))
106: #endif
107:          write(6,120)
108:       endif
109:  100  format ('Vector length ',i6)
110:  110  format ('Vector length ',3(i6))
111:  120  format ('All other values should be near zero')

113:       call VecScale(x,two,ierr);CHKERRA(ierr)
114:       call VecNorm(x,NORM_2,norm,ierr);CHKERRA(ierr)
115:       v = abs(norm-2.0*sqrt(nfloat))
116:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
117:       if (rank .eq. 0) write(6,130) v
118:  130  format ('VecScale ',1pe9.2)

120:       call VecCopy(x,w,ierr);CHKERRA(ierr)
121:       call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
122:       v = abs(norm-2.0*sqrt(nfloat))
123:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
124:       if (rank .eq. 0) write(6,140) v
125:  140  format ('VecCopy ',1pe9.2)

127:       call VecAXPY(y,three,x,ierr);CHKERRA(ierr)
128:       call VecNorm(y,NORM_2,norm,ierr);CHKERRA(ierr)
129:       v = abs(norm-8.0*sqrt(nfloat))
130:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
131:       if (rank .eq. 0) write(6,150) v
132:  150  format ('VecAXPY ',1pe9.2)

134:       call VecAYPX(y,two,x,ierr);CHKERRA(ierr)
135:       call VecNorm(y,NORM_2,norm,ierr);CHKERRA(ierr)
136:       v = abs(norm-18.0*sqrt(nfloat))
137:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
138:       if (rank .eq. 0) write(6,160) v
139:  160  format ('VecAYXP ',1pe9.2)

141:       call VecSwap(x,y,ierr);CHKERRA(ierr)
142:       call VecNorm(y,NORM_2,norm,ierr);CHKERRA(ierr)
143:       v = abs(norm-2.0*sqrt(nfloat))
144:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
145:       if (rank .eq. 0) write(6,170) v
146:  170  format ('VecSwap ',1pe9.2)

148:       call VecNorm(x,NORM_2,norm,ierr);CHKERRA(ierr)
149:       v = abs(norm-18.0*sqrt(nfloat))
150:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
151:       if (rank .eq. 0) write(6,180) v
152:  180  format ('VecSwap ',1pe9.2)

154:       call VecWAXPY(w,two,x,y,ierr);CHKERRA(ierr)
155:       call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
156:       v = abs(norm-38.0*sqrt(nfloat))
157:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
158:       if (rank .eq. 0) write(6,190) v
159:  190  format ('VecWAXPY ',1pe9.2)

161:       call VecPointwiseMult(w,y,x,ierr);CHKERRA(ierr)
162:       call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
163:       v = abs(norm-36.0*sqrt(nfloat))
164:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
165:       if (rank .eq. 0) write(6,200) v
166:  200  format ('VecPointwiseMult ',1pe9.2)

168:       call VecPointwiseDivide(w,x,y,ierr);CHKERRA(ierr)
169:       call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
170:       v = abs(norm-9.0*sqrt(nfloat))
171:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
172:       if (rank .eq. 0) write(6,210) v
173:  210  format ('VecPointwiseDivide ',1pe9.2)


176:       dots(1) = one
177:       dots(2) = three
178:       dots(3) = two
179:       call VecSet(x,one,ierr);CHKERRA(ierr)
180:       call VecMAXPY(x,ithree,dots,z,ierr);CHKERRA(ierr)
181:       call VecNorm(z(1),NORM_2,norm,ierr);CHKERRA(ierr)
182:       v = abs(norm-sqrt(nfloat))
183:       if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
184:       call VecNorm(z(2),NORM_2,norm,ierr);CHKERRA(ierr)
185:       v1 = abs(norm-2.0*sqrt(nfloat))
186:       if (v1 .gt. -1.d-10 .and. v1 .lt. 1.d-10) v1 = 0.0
187:       call VecNorm(z(3),NORM_2,norm,ierr);CHKERRA(ierr)
188:       v2 = abs(norm-3.0*sqrt(nfloat))
189:       if (v2 .gt. -1.d-10 .and. v2 .lt. 1.d-10) v2 = 0.0
190:       if (rank .eq. 0) write(6,220) v,v1,v2
191:  220  format ('VecMAXPY ',3(1pe9.2))

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

196:       call VecDestroy(x,ierr);CHKERRA(ierr)
197:       call VecDestroy(y,ierr);CHKERRA(ierr)
198:       call VecDestroy(w,ierr);CHKERRA(ierr)
199:       call VecDestroyVecsF90(ithree,z,ierr);CHKERRA(ierr)
200:       call PetscFinalize(ierr)

202:       end

204: !
205: !/*TEST
206: !
207: !     test:
208: !       nsize: 2
209: !
210: !TEST*/