Actual source code: ex12.F

  1: !
  2: !    "$Id: ex12.F,v 1.60 2001/08/07 03:04:13 balay Exp $";
  3: !
  4: !  This example demonstrates basic use of the SNES Fortran interface.
  5: !
  6: !  Note:  The program ex10.f is the same as this example, except that it
  7: !         uses the Fortran .f suffix rather than the .F suffix.
  8: !
  9: !  In this example the application context is a Fortran integer array:
 10: !      ctx(1) = da    - distributed array
 11: !          2  = F     - global vector where the function is stored
 12: !          3  = xl    - local work vector
 13: !          4  = rank  - processor rank
 14: !          5  = size  - number of processors
 15: !          6  = N     - system size
 16: !
 17: !  Note: Any user-defined Fortran routines (such as FormJacobian)
 18: !  MUST be declared as external.
 19: !
 20: !
 21: ! Macros to make setting/getting  values into vector clearer.
 22: ! The element xx(ib) is the ibth element in the vector indicated by ctx(3)
 23: #define xx(ib)  vxx(ixx + (ib))
 24: #define ff(ib)  vff(iff + (ib))
 25: #define F2(ib)  vF2(iF2 + (ib))
 26:       program main
 27:       implicit none

 29:  #include include/finclude/petsc.h
 30:  #include include/finclude/petscvec.h
 31:  #include include/finclude/petscda.h
 32:  #include include/finclude/petscmat.h
 33:  #include include/finclude/petscsnes.h

 35:       PetscFortranAddr ctx(6)
 36:       integer          rank,size,ierr,N,start,end,nn,i,ii,its,flg
 37:       SNES             snes
 38:       Mat              J
 39:       Vec              x,r,u
 40:       PetscScalar      xp,FF,UU,h
 41:       character*(10)   matrixname
 42:       external         FormJacobian,FormFunction

 44:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 45:       N = 10
 46:       call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',N,flg,ierr)
 47:       h = 1.d0/(N-1.d0)
 48:       ctx(6) = N

 50:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
 51:       call MPI_Comm_size(PETSC_COMM_WORLD,size,ierr)
 52:       ctx(4) = rank
 53:       ctx(5) = size

 55: ! Set up data structures
 56:       call DACreate1d(PETSC_COMM_WORLD,DA_NONPERIODIC,N,1,1,            &
 57:      &     PETSC_NULL_INTEGER,ctx(1),ierr)

 59:       call DACreateGlobalVector(ctx(1),x,ierr)
 60:       call DACreateLocalVector(ctx(1),ctx(3),ierr)

 62:       call PetscObjectSetName(x,'Approximate Solution',ierr)
 63:       call VecDuplicate(x,r,ierr)
 64:       call VecDuplicate(x,ctx(2),ierr)
 65:       call VecDuplicate(x,U,ierr)
 66:       call PetscObjectSetName(U,'Exact Solution',ierr)

 68:       call MatCreateMPIAIJ(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,N, &
 69:      &     N,3,PETSC_NULL_INTEGER,0,PETSC_NULL_INTEGER,J,ierr)

 71:       call MatGetType(J,matrixname,ierr)

 73: ! Store right-hand-side of PDE and exact solution
 74:       call VecGetOwnershipRange(x,start,end,ierr)
 75:       xp = h*start
 76:       nn = end - start
 77:       ii = start
 78:       do 10, i=0,nn-1
 79:         FF = 6.0*xp + (xp+1.e-12)**6.e0
 80:         UU = xp*xp*xp
 81:         call VecSetValues(ctx(2),1,ii,FF,INSERT_VALUES,ierr)
 82:         call VecSetValues(U,1,ii,UU,INSERT_VALUES,ierr)
 83:         xp = xp + h
 84:         ii = ii + 1
 85:  10   continue
 86:       call VecAssemblyBegin(ctx(2),ierr)
 87:       call VecAssemblyEnd(ctx(2),ierr)
 88:       call VecAssemblyBegin(U,ierr)
 89:       call VecAssemblyEnd(U,ierr)

 91: ! Create nonlinear solver
 92:       call SNESCreate(PETSC_COMM_WORLD,snes,ierr)

 94: ! Set various routines and options
 95:       call SNESSetFunction(snes,r,FormFunction,ctx,ierr)
 96:       call SNESSetJacobian(snes,J,J,FormJacobian,ctx,ierr)
 97:       call SNESSetFromOptions(snes,ierr)

 99: ! Solve nonlinear system
100:       call FormInitialGuess(snes,x,ierr)
101:       call SNESSolve(snes,x,its,ierr)

103: ! Write results if first processor
104:       if (ctx(4) .eq. 0) then
105:         write(6,100) its
106:       endif
107:   100 format('Number of Newton iterations = ',i5)

109: !  Free work space.  All PETSc objects should be destroyed when they
110: !  are no longer needed.
111:       call VecDestroy(x,ierr)
112:       call VecDestroy(ctx(3),ierr)
113:       call VecDestroy(r,ierr)
114:       call VecDestroy(U,ierr)
115:       call VecDestroy(ctx(2),ierr)
116:       call MatDestroy(J,ierr)
117:       call SNESDestroy(snes,ierr)
118:       call DADestroy(ctx(1),ierr)
119:       call PetscFinalize(ierr)
120:       end


123: ! --------------------  Evaluate Function F(x) ---------------------

125:       subroutine FormFunction(snes,x,f,ctx,ierr)
126:       implicit none
127:       SNES             snes
128:       Vec              x,f
129:       PetscFortranAddr ctx(*)
130:       integer          rank,size,i,s,n,ierr
131:       PetscOffset      ixx,iff,iF2
132:       PetscScalar      h,d,vf2(1),vxx(1),vff(1)
133:  #include include/finclude/petsc.h
134:  #include include/finclude/petscvec.h
135:  #include include/finclude/petscda.h
136:  #include include/finclude/petscmat.h
137:  #include include/finclude/petscsnes.h


140:       rank  = ctx(4)
141:       size  = ctx(5)
142:       h     = 1.d0/(ctx(6) - 1.d0)
143:       call DAGlobalToLocalBegin(ctx(1),x,INSERT_VALUES,ctx(3),ierr)
144:       call DAGlobalToLocalEnd(ctx(1),x,INSERT_VALUES,ctx(3),ierr)

146:       call VecGetLocalSize(ctx(3),n,ierr)
147:       if (n .gt. 1000) then
148:         print*, 'Local work array not big enough'
149:         call MPI_Abort(PETSC_COMM_WORLD,0,ierr)
150:       endif

152: !
153: ! This sets the index ixx so that vxx(ixx+1) is the first local
154: ! element in the vector indicated by ctx(3).
155: !
156:       call VecGetArray(ctx(3),vxx,ixx,ierr)
157:       call VecGetArray(f,vff,iff,ierr)
158:       call VecGetArray(ctx(2),vF2,iF2,ierr)

160:       d = h*h

162: !
163: !  Note that the array vxx() was obtained from a ghosted local vector
164: !  ctx(3) while the array vff() was obtained from the non-ghosted parallel
165: !  vector F. This is why there is a need for shift variable s. Since vff()
166: !  does not have locations for the ghost variables we need to index in it
167: !  slightly different then indexing into vxx(). For example on processor
168: !  1 (the second processor)
169: !
170: !        xx(1)        xx(2)             xx(3)             .....
171: !      ^^^^^^^        ^^^^^             ^^^^^
172: !      ghost value   1st local value   2nd local value
173: !
174: !                      ff(1)             ff(2)
175: !                     ^^^^^^^           ^^^^^^^
176: !                    1st local value   2nd local value
177: !
178:        if (rank .eq. 0) then
179:         s = 0
180:         ff(1) = xx(1)
181:       else
182:         s = 1
183:       endif

185:       do 10 i=1,n-2
186:        ff(i-s+1) = d*(xx(i) - 2.d0*xx(i+1)                              &
187:      &      + xx(i+2)) + xx(i+1)*xx(i+1)                                &
188:      &      - F2(i-s+1)
189:  10   continue

191:       if (rank .eq. size-1) then
192:         ff(n-s) = xx(n) - 1.d0
193:       endif

195:       call VecRestoreArray(f,vff,iff,ierr)
196:       call VecRestoreArray(ctx(3),vxx,ixx,ierr)
197:       call VecRestoreArray(ctx(2),vF2,iF2,ierr)
198:       return
199:       end

201: ! --------------------  Form initial approximation -----------------

203:       subroutine FormInitialGuess(snes,x,ierr)
204:       implicit none
205:  #include include/finclude/petsc.h
206:  #include include/finclude/petscvec.h
207:  #include include/finclude/petscsnes.h
208:       integer          ierr
209:       Vec              x
210:       SNES             snes
211:       PetscScalar      five

213:       five = 5.d-1
214:       call VecSet(five,x,ierr)
215:       return
216:       end

218: ! --------------------  Evaluate Jacobian --------------------

220:       subroutine FormJacobian(snes,x,jac,B,flag,ctx,ierr)
221:       implicit none
222:  #include include/finclude/petsc.h
223:  #include include/finclude/petscvec.h
224:  #include include/finclude/petscda.h
225:  #include include/finclude/petscmat.h
226:  #include include/finclude/petscsnes.h
227:       SNES             snes
228:       Vec              x
229:       Mat              jac,B
230:       PetscFortranAddr ctx(*)
231:       integer          flag,ii,istart
232:       PetscOffset      ixx
233:       integer          iend,i,j,n,rank,size,end,start,ierr
234:       PetscScalar      d,A,h,vxx(1)


237:       h = 1.d0/(ctx(6) - 1.d0)
238:       d = h*h
239:       rank = ctx(4)
240:       size = ctx(5)

242:       call VecGetArray(x,vxx,ixx,ierr)
243:       call VecGetOwnershipRange(x,start,end,ierr)
244:       n = end - start

246:       if (rank .eq. 0) then
247:         A = 1.0
248:         call MatSetValues(jac,1,start,1,start,A,INSERT_VALUES,ierr)
249:         istart = 1
250:       else
251:         istart = 0
252:       endif
253:       if (rank .eq. size-1) then
254:         i = ctx(6)-1
255:         A = 1.0
256:         call MatSetValues(jac,1,i,1,i,A,INSERT_VALUES,ierr)
257:         iend = n-1
258:       else
259:         iend = n
260:       endif
261:       do 10 i=istart,iend-1
262:         ii = i + start
263:         j = start + i - 1
264:         call MatSetValues(jac,1,ii,1,j,d,INSERT_VALUES,ierr)
265:         j = start + i + 1
266:         call MatSetValues(jac,1,ii,1,j,d,INSERT_VALUES,ierr)
267:         A = -2.0*d + 2.0*xx(i+1)
268:         call MatSetValues(jac,1,ii,1,ii,A,INSERT_VALUES,ierr)
269:  10   continue
270:       call VecRestoreArray(x,vxx,ixx,ierr)
271:       call MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY,ierr)
272:       call MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY,ierr)
273:       return
274:       end