Actual source code: ex12f.F

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

 28:  #include finclude/petscsys.h
 29:  #include finclude/petscvec.h
 30:  #include finclude/petscda.h
 31:  #include finclude/petscmat.h
 32:  #include finclude/petscsnes.h

 34:       PetscFortranAddr ctx(6)
 35:       PetscMPIInt rank,size
 36:       PetscErrorCode ierr
 37:       PetscInt N,start,end,nn,i
 38:       PetscInt ii,its,i1,i0,i3
 39:       PetscTruth flg
 40:       SNES             snes
 41:       Mat              J
 42:       Vec              x,r,u
 43:       PetscScalar      xp,FF,UU,h
 44:       character*(10)   matrixname
 45:       external         FormJacobian,FormFunction

 47:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 48:       i1 = 1
 49:       i0 = 0
 50:       i3 = 3
 51:       N  = 10
 52:       call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',N,flg,ierr)
 53:       h = 1.d0/(N-1.d0)
 54:       ctx(6) = N

 56:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
 57:       call MPI_Comm_size(PETSC_COMM_WORLD,size,ierr)
 58:       ctx(4) = rank
 59:       ctx(5) = size

 61: ! Set up data structures
 62:       call DACreate1d(PETSC_COMM_WORLD,DA_NONPERIODIC,N,i1,i1,            &
 63:      &     PETSC_NULL_INTEGER,ctx(1),ierr)

 65:       call DACreateGlobalVector(ctx(1),x,ierr)
 66:       call DACreateLocalVector(ctx(1),ctx(3),ierr)

 68:       call PetscObjectSetName(x,'Approximate Solution',ierr)
 69:       call VecDuplicate(x,r,ierr)
 70:       call VecDuplicate(x,ctx(2),ierr)
 71:       call VecDuplicate(x,U,ierr)
 72:       call PetscObjectSetName(U,'Exact Solution',ierr)

 74:       call MatCreateMPIAIJ(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,N, &
 75:      &     N,i3,PETSC_NULL_INTEGER,i0,PETSC_NULL_INTEGER,J,ierr)

 77:       call MatGetType(J,matrixname,ierr)

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

 97: ! Create nonlinear solver
 98:       call SNESCreate(PETSC_COMM_WORLD,snes,ierr)

100: ! Set various routines and options
101:       call SNESSetFunction(snes,r,FormFunction,ctx,ierr)
102:       call SNESSetJacobian(snes,J,J,FormJacobian,ctx,ierr)
103:       call SNESSetFromOptions(snes,ierr)

105: ! Solve nonlinear system
106:       call FormInitialGuess(snes,x,ierr)
107:       call SNESSolve(snes,PETSC_NULL_OBJECT,x,ierr)
108:       call SNESGetIterationNumber(snes,its,ierr);

110: ! Write results if first processor
111:       if (ctx(4) .eq. 0) then
112:         write(6,100) its
113:       endif
114:   100 format('Number of Newton iterations = ',i5)

116: !  Free work space.  All PETSc objects should be destroyed when they
117: !  are no longer needed.
118:       call VecDestroy(x,ierr)
119:       call VecDestroy(ctx(3),ierr)
120:       call VecDestroy(r,ierr)
121:       call VecDestroy(U,ierr)
122:       call VecDestroy(ctx(2),ierr)
123:       call MatDestroy(J,ierr)
124:       call SNESDestroy(snes,ierr)
125:       call DADestroy(ctx(1),ierr)
126:       call PetscFinalize(ierr)
127:       end


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

132:       subroutine FormFunction(snes,x,f,ctx,ierr)
133:       implicit none
134:       SNES             snes
135:       Vec              x,f
136:       PetscFortranAddr ctx(*)
137:       PetscMPIInt  rank,size
138:       PetscInt i,s,n
139:       PetscErrorCode ierr
140:       PetscOffset      ixx,iff,iF2
141:       PetscScalar      h,d,vf2(1),vxx(1),vff(1)
142:  #include finclude/petscsys.h
143:  #include finclude/petscvec.h
144:  #include finclude/petscda.h
145:  #include finclude/petscmat.h
146:  #include finclude/petscsnes.h


149:       rank  = ctx(4)
150:       size  = ctx(5)
151:       h     = 1.d0/(ctx(6) - 1.d0)
152:       call DAGlobalToLocalBegin(ctx(1),x,INSERT_VALUES,ctx(3),ierr)
153:       call DAGlobalToLocalEnd(ctx(1),x,INSERT_VALUES,ctx(3),ierr)

155:       call VecGetLocalSize(ctx(3),n,ierr)
156:       if (n .gt. 1000) then
157:         print*, 'Local work array not big enough'
158:         call MPI_Abort(PETSC_COMM_WORLD,0,ierr)
159:       endif

161: !
162: ! This sets the index ixx so that vxx(ixx+1) is the first local
163: ! element in the vector indicated by ctx(3).
164: !
165:       call VecGetArray(ctx(3),vxx,ixx,ierr)
166:       call VecGetArray(f,vff,iff,ierr)
167:       call VecGetArray(ctx(2),vF2,iF2,ierr)

169:       d = h*h

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

194:       do 10 i=1,n-2
195:        ff(i-s+1) = d*(xx(i) - 2.d0*xx(i+1)                              &
196:      &      + xx(i+2)) + xx(i+1)*xx(i+1)                                &
197:      &      - F2(i-s+1)
198:  10   continue

200:       if (rank .eq. size-1) then
201:         ff(n-s) = xx(n) - 1.d0
202:       endif

204:       call VecRestoreArray(f,vff,iff,ierr)
205:       call VecRestoreArray(ctx(3),vxx,ixx,ierr)
206:       call VecRestoreArray(ctx(2),vF2,iF2,ierr)
207:       return
208:       end

210: ! --------------------  Form initial approximation -----------------

212:       subroutine FormInitialGuess(snes,x,ierr)
213:       implicit none
214:  #include finclude/petscsys.h
215:  #include finclude/petscvec.h
216:  #include finclude/petscsnes.h
217:       PetscErrorCode   ierr
218:       Vec              x
219:       SNES             snes
220:       PetscScalar      five

222:       five = 5.d-1
223:       call VecSet(x,five,ierr)
224:       return
225:       end

227: ! --------------------  Evaluate Jacobian --------------------

229:       subroutine FormJacobian(snes,x,jac,B,flag,ctx,ierr)
230:       implicit none
231:  #include finclude/petscsys.h
232:  #include finclude/petscvec.h
233:  #include finclude/petscda.h
234:  #include finclude/petscmat.h
235:  #include finclude/petscsnes.h
236:       SNES             snes
237:       Vec              x
238:       Mat              jac,B
239:       PetscFortranAddr ctx(*)
240:       PetscTruth       flag
241:       PetscInt  ii,istart,iend
242:       PetscInt  i,j,n,end,start,i1
243:       PetscErrorCode ierr
244:       PetscMPIInt rank,size
245:       PetscOffset      ixx
246:       PetscScalar      d,A,h,vxx(1)

248:       i1 = 1
249:       h = 1.d0/(ctx(6) - 1.d0)
250:       d = h*h
251:       rank = ctx(4)
252:       size = ctx(5)

254:       call VecGetArray(x,vxx,ixx,ierr)
255:       call VecGetOwnershipRange(x,start,end,ierr)
256:       n = end - start

258:       if (rank .eq. 0) then
259:         A = 1.0
260:         call MatSetValues(jac,i1,start,i1,start,A,INSERT_VALUES,ierr)
261:         istart = 1
262:       else
263:         istart = 0
264:       endif
265:       if (rank .eq. size-1) then
266:         i = ctx(6)-1
267:         A = 1.0
268:         call MatSetValues(jac,i1,i,i1,i,A,INSERT_VALUES,ierr)
269:         iend = n-1
270:       else
271:         iend = n
272:       endif
273:       do 10 i=istart,iend-1
274:         ii = i + start
275:         j = start + i - 1
276:         call MatSetValues(jac,i1,ii,i1,j,d,INSERT_VALUES,ierr)
277:         j = start + i + 1
278:         call MatSetValues(jac,i1,ii,i1,j,d,INSERT_VALUES,ierr)
279:         A = -2.0*d + 2.0*xx(i+1)
280:         call MatSetValues(jac,i1,ii,i1,ii,A,INSERT_VALUES,ierr)
281:  10   continue
282:       call VecRestoreArray(x,vxx,ixx,ierr)
283:       call MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY,ierr)
284:       call MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY,ierr)
285:       return
286:       end