Actual source code: eptorsion2f.F

petsc-master 2016-09-25
Report Typos and Errors
  1: !  Program usage: mpiexec -n <proc> eptorsion2f [all TAO options]
  2: !
  3: !  Description:  This example demonstrates use of the TAO package to solve
  4: !  unconstrained minimization problems in parallel.  This example is based
  5: !  on the Elastic-Plastic Torsion (dept) problem from the MINPACK-2 test suite.
  6: !  The command line options are:
  7: !    -mx <xg>, where <xg> = number of grid points in the 1st coordinate direction
  8: !    -my <yg>, where <yg> = number of grid points in the 2nd coordinate direction
  9: !    -par <param>, where <param> = angle of twist per unit length
 10: !
 11: !/*T
 12: !   Concepts: TAO^Solving an unconstrained minimization problem
 13: !   Routines: TaoCreate(); TaoSetType();
 14: !   Routines: TaoSetInitialVector();
 15: !   Routines: TaoSetObjectiveAndGradientRoutine();
 16: !   Routines: TaoSetHessianRoutine(); TaoSetFromOptions();
 17: !   Routines: TaoSetMonitor(); TaoSetConvergenceTest()
 18: !   Routines: TaoSolve(); TaoGetSolutionStatus()
 19: !   Routines: TaoDestroy();

 21: !   Processors: n
 22: !T*/
 23: !
 24: ! ----------------------------------------------------------------------
 25: !
 26: !  Elastic-plastic torsion problem.
 27: !
 28: !  The elastic plastic torsion problem arises from the deconverged
 29: !  of the stress field on an infinitely long cylindrical bar, which is
 30: !  equivalent to the solution of the following problem:
 31: !     min{ .5 * integral(||gradient(v(x))||^2 dx) - C * integral(v(x) dx)}
 32: !  where C is the torsion angle per unit length.
 33: !
 34: !  The C version of this code is eptorsion2.c
 35: !
 36: ! ----------------------------------------------------------------------

 38:       implicit none
 39: #include "eptorsion2f.h"

 41: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 42: !                   Variable declarations
 43: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 44: !
 45: !  See additional variable declarations in the file eptorsion2f.h
 46: !
 47:       PetscErrorCode   ierr           ! used to check for functions returning nonzeros
 48:       Vec              x              ! solution vector
 49:       Mat              H              ! hessian matrix
 50:       PetscInt         Nx, Ny         ! number of processes in x- and y- directions
 51:       Tao        tao            ! Tao solver context
 52:       PetscBool        flg
 53:       PetscInt         i1
 54:       PetscInt         dummy


 57: !  Note: Any user-defined Fortran routines (such as FormGradient)
 58: !  MUST be declared as external.

 60:       external FormInitialGuess,FormFunctionGradient,ComputeHessian
 61:       external Monitor,ConvergenceTest

 63:       i1 = 1

 65: !     Initialize TAO, PETSc  contexts
 66:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 67:       if (ierr .ne. 0) then
 68:          print*,'Unable to initialize PETSc'
 69:          stop
 70:       endif

 72: !     Specify default parameters
 73:       param = 5.0
 74:       mx = 10
 75:       my = 10
 76:       Nx = PETSC_DECIDE
 77:       Ny = PETSC_DECIDE

 79: !     Check for any command line arguments that might override defaults
 80:       call PetscOptionsGetInt(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,    &
 81:      &                        '-mx',mx,flg,ierr)
 82:       call PetscOptionsGetInt(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,    &
 83:      &                        '-my',my,flg,ierr)
 84:       call PetscOptionsGetReal(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,   &
 85:      &                         '-par',param,flg,ierr)


 88: !     Set up distributed array and vectors
 89:       call DMDACreate2d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,               &
 90:      &     DM_BOUNDARY_NONE,                                             &
 91:      &     DMDA_STENCIL_BOX,mx,my,Nx,Ny,i1,i1,PETSC_NULL_INTEGER,        &
 92:      &     PETSC_NULL_INTEGER,dm,ierr)
 93:       call DMSetFromOptions(dm,ierr)
 94:       call DMSetUp(dm,ierr)

 96: !     Create vectors
 97:       call DMCreateGlobalVector(dm,x,ierr)
 98:       call DMCreateLocalVector(dm,localX,ierr)

100: !     Create Hessian
101:       call DMCreateMatrix(dm,H,ierr)
102:       call MatSetOption(H,MAT_SYMMETRIC,PETSC_TRUE,ierr)

104: !     The TAO code begins here

106: !     Create TAO solver
107:       call TaoCreate(PETSC_COMM_WORLD,tao,ierr)
108:       call TaoSetType(tao,TAOCG,ierr)

110: !     Set routines for function and gradient evaluation

112:       call TaoSetObjectiveAndGradientRoutine(tao,                       &
113:      &     FormFunctionGradient,PETSC_NULL_OBJECT,ierr)
114:       call TaoSetHessianRoutine(tao,H,H,ComputeHessian,                 &
115:      &     PETSC_NULL_OBJECT,ierr)

117: !     Set initial guess
118:       call FormInitialGuess(x,ierr)
119:       call TaoSetInitialVector(tao,x,ierr)

121:       call PetscOptionsHasName(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,   &
122:      &                         '-testmonitor',flg,ierr)
123:       if (flg) then
124:          call TaoSetMonitor(tao,Monitor,dummy,PETSC_NULL_FUNCTION,      &
125:      &        ierr)
126:       endif

128:       call PetscOptionsHasName(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,  &
129:      &                         '-testconvergence',flg, ierr)
130:       if (flg) then
131:          call TaoSetConvergenceTest(tao,ConvergenceTest,dummy,          &
132:      &        ierr)
133:       endif

135: !     Check for any TAO command line options
136:       call TaoSetFromOptions(tao,ierr)


139: !     SOLVE THE APPLICATION
140:       call TaoSolve(tao,ierr)

142: !     Free TAO data structures
143:       call TaoDestroy(tao,ierr)


146: !     Free PETSc data structures
147:       call VecDestroy(x,ierr)
148:       call VecDestroy(localX,ierr)
149:       call MatDestroy(H,ierr)
150:       call DMDestroy(dm,ierr)


153: !     Finalize TAO and PETSc
154:       call PetscFinalize(ierr)
155:       end


158: ! ---------------------------------------------------------------------
159: !
160: !   FormInitialGuess - Computes an initial approximation to the solution.
161: !
162: !   Input Parameters:
163: !   X    - vector
164: !
165: !   Output Parameters:
166: !   X    - vector
167: !   ierr - error code
168: !
169:       subroutine FormInitialGuess(X,ierr)
170:       implicit none

172: ! mx, my are defined in eptorsion2f.h
173: #include "eptorsion2f.h"

175: !  Input/output variables:
176:       Vec              X
177:       PetscErrorCode   ierr

179: !  Local variables:
180:       PetscInt         i, j, k, xe, ye
181:       PetscReal      temp, val, hx, hy
182:       PetscInt         xs, ys, xm, ym
183:       PetscInt         gxm, gym, gxs, gys
184:       PetscInt         i1

186:       i1 = 1
187:       hx = 1.0/(mx + 1)
188:       hy = 1.0/(my + 1)

190: !  Get corner information
191:       call DMDAGetCorners(dm,xs,ys,PETSC_NULL_INTEGER,xm,ym,               &
192:      &                  PETSC_NULL_INTEGER,ierr)
193:       call DMDAGetGhostCorners(dm,gxs,gys,PETSC_NULL_INTEGER,              &
194:      &                   gxm,gym,PETSC_NULL_INTEGER,ierr)



198: !  Compute initial guess over locally owned part of mesh
199:       xe = xs+xm
200:       ye = ys+ym
201:       do j=ys,ye-1
202:          temp = min(j+1,my-j)*hy
203:          do i=xs,xe-1
204:             k   = (j-gys)*gxm + i-gxs
205:             val = min((min(i+1,mx-i))*hx,temp)
206:             call VecSetValuesLocal(X,i1,k,val,ADD_VALUES,ierr)
207:          end do
208:       end do
209:       call VecAssemblyBegin(X,ierr)
210:       call VecAssemblyEnd(X,ierr)
211:       return
212:       end


215: ! ---------------------------------------------------------------------
216: !
217: !  FormFunctionGradient - Evaluates gradient G(X).
218: !
219: !  Input Parameters:
220: !  tao   - the Tao context
221: !  X     - input vector
222: !  dummy - optional user-defined context (not used here)
223: !
224: !  Output Parameters:
225: !  f     - the function value at X
226: !  G     - vector containing the newly evaluated gradient
227: !  ierr  - error code
228: !
229: !  Notes:
230: !  This routine serves as a wrapper for the lower-level routine
231: !  "ApplicationGradient", where the actual computations are
232: !  done using the standard Fortran style of treating the local
233: !  input vector data as an array over the local mesh.
234: !
235:       subroutine FormFunctionGradient(tao,X,f,G,dummy,ierr)
236:       implicit none

238: ! dm, mx, my, param, localX declared in eptorsion2f.h
239: #include "eptorsion2f.h"

241: !  Input/output variables:
242:       Tao        tao
243:       Vec              X, G
244:       PetscReal      f
245:       PetscErrorCode   ierr
246:       PetscInt         dummy

248: !  Declarations for use with local array:


251: ! PETSc's VecGetArray acts differently in Fortran than it does in C.
252: ! Calling VecGetArray((Vec) X, (PetscReal) x_array(0:1), (PetscOffset) x_index, ierr)
253: ! will return an array of doubles referenced by x_array offset by x_index.
254: !  i.e.,  to reference the kth element of X, use x_array(k + x_index).
255: ! Notice that by declaring the arrays with range (0:1), we are using the C 0-indexing practice.
256:       PetscReal      lx_v(0:1)
257:       PetscOffset      lx_i

259: !  Local variables:
260:       PetscReal      zero, p5, area, cdiv3
261:       PetscReal      val, flin, fquad,floc
262:       PetscReal      v, vb, vl, vr, vt, dvdx
263:       PetscReal      dvdy, hx, hy
264:       PetscInt         xe, ye, xsm, ysm
265:       PetscInt         xep, yep, i, j, k, ind
266:       PetscInt         xs, ys, xm, ym
267:       PetscInt         gxs, gys, gxm, gym
268:       PetscInt         i1

270:       i1 = 1
271:       0
272:       cdiv3 = param/3.0
273:       zero = 0.0
274:       p5   = 0.5
275:       hx = 1.0/(mx + 1)
276:       hy = 1.0/(my + 1)
277:       fquad = zero
278:       flin = zero

280: !  Initialize gradient to zero
281:       call VecSet(G,zero,ierr)

283: !  Scatter ghost points to local vector
284:       call DMGlobalToLocalBegin(dm,X,INSERT_VALUES,localX,ierr)
285:       call DMGlobalToLocalEnd(dm,X,INSERT_VALUES,localX,ierr)


288: !  Get corner information
289:       call DMDAGetCorners(dm,xs,ys,PETSC_NULL_INTEGER,xm,ym,               &
290:      &                  PETSC_NULL_INTEGER,ierr)
291:       call DMDAGetGhostCorners(dm,gxs,gys,PETSC_NULL_INTEGER,              &
292:      &                   gxm,gym,PETSC_NULL_INTEGER,ierr)

294: !  Get pointer to vector data.
295:       call VecGetArray(localX,lx_v,lx_i,ierr)


298: !  Set local loop dimensions
299:       xe = xs+xm
300:       ye = ys+ym
301:       if (xs .eq. 0) then
302:          xsm = xs-1
303:       else
304:          xsm = xs
305:       endif
306:       if (ys .eq. 0) then
307:          ysm = ys-1
308:       else
309:          ysm = ys
310:       endif
311:       if (xe .eq. mx) then
312:          xep = xe+1
313:       else
314:          xep = xe
315:       endif
316:       if (ye .eq. my) then
317:          yep = ye+1
318:       else
319:          yep = ye
320:       endif

322: !     Compute local gradient contributions over the lower triangular elements

324:       do j = ysm, ye-1
325:          do i = xsm, xe-1
326:             k  = (j-gys)*gxm + i-gxs
327:             v  = zero
328:             vr = zero
329:             vt = zero
330:             if (i .ge. 0 .and. j .ge. 0)      v = lx_v(lx_i+k)
331:             if (i .lt. mx-1 .and. j .gt. -1) vr = lx_v(lx_i+k+1)
332:             if (i .gt. -1 .and. j .lt. my-1) vt = lx_v(lx_i+k+gxm)
333:             dvdx = (vr-v)/hx
334:             dvdy = (vt-v)/hy
335:             if (i .ne. -1 .and. j .ne. -1) then
336:                ind = k
337:                val = - dvdx/hx - dvdy/hy - cdiv3
338:                call VecSetValuesLocal(G,i1,k,val,ADD_VALUES,ierr)
339:             endif
340:             if (i .ne. mx-1 .and. j .ne. -1) then
341:                ind = k+1
342:                val =  dvdx/hx - cdiv3
343:                call VecSetValuesLocal(G,i1,ind,val,ADD_VALUES,ierr)
344:             endif
345:             if (i .ne. -1 .and. j .ne. my-1) then
346:               ind = k+gxm
347:               val = dvdy/hy - cdiv3
348:               call VecSetValuesLocal(G,i1,ind,val,ADD_VALUES,ierr)
349:             endif
350:             fquad = fquad + dvdx*dvdx + dvdy*dvdy
351:             flin = flin - cdiv3 * (v+vr+vt)
352:          end do
353:       end do

355: !     Compute local gradient contributions over the upper triangular elements

357:       do j = ys, yep-1
358:          do i = xs, xep-1
359:             k  = (j-gys)*gxm + i-gxs
360:             vb = zero
361:             vl = zero
362:             v  = zero
363:             if (i .lt. mx .and. j .gt. 0) vb = lx_v(lx_i+k-gxm)
364:             if (i .gt. 0 .and. j .lt. my) vl = lx_v(lx_i+k-1)
365:             if (i .lt. mx .and. j .lt. my) v = lx_v(lx_i+k)
366:             dvdx = (v-vl)/hx
367:             dvdy = (v-vb)/hy
368:             if (i .ne. mx .and. j .ne. 0) then
369:                ind = k-gxm
370:                val = - dvdy/hy - cdiv3
371:                call VecSetValuesLocal(G,i1,ind,val,ADD_VALUES,ierr)
372:             endif
373:             if (i .ne. 0 .and. j .ne. my) then
374:                ind = k-1
375:                val =  - dvdx/hx - cdiv3
376:                call VecSetValuesLocal(G,i1,ind,val,ADD_VALUES,ierr)
377:             endif
378:             if (i .ne. mx .and. j .ne. my) then
379:                ind = k
380:                val =  dvdx/hx + dvdy/hy - cdiv3
381:                call VecSetValuesLocal(G,i1,ind,val,ADD_VALUES,ierr)
382:             endif
383:             fquad = fquad + dvdx*dvdx + dvdy*dvdy
384:             flin = flin - cdiv3*(vb + vl + v)
385:          end do
386:       end do

388: !  Restore vector
389:       call VecRestoreArray(localX,lx_v,lx_i,ierr)

391: !  Assemble gradient vector
392:       call VecAssemblyBegin(G,ierr)
393:       call VecAssemblyEnd(G,ierr)

395: ! Scale the gradient
396:       area = p5*hx*hy
397:       floc = area *(p5*fquad+flin)
398:       call VecScale(G,area,ierr)


401: !  Sum function contributions from all processes
402:       call MPI_Allreduce(floc,f,1,MPIU_SCALAR,MPIU_SUM,                   &
403:      &                   PETSC_COMM_WORLD,ierr)
404:       call PetscLogFlops(20.0d0*(ye-ysm)*(xe-xsm)+                        &
405:      &                   16.0d0*(xep-xs)*(yep-ys),ierr)
406:       return
407:       end




412:       subroutine ComputeHessian(tao, X, H, Hpre, dummy, ierr)
413:       implicit none
414: #include "eptorsion2f.h"
415:       Tao       tao
416:       Vec             X
417:       Mat             H,Hpre
418:       PetscErrorCode  ierr
419:       PetscInt        dummy


422:       PetscInt i,j,k
423:       PetscInt col(0:4),row
424:       PetscInt xs,xm,gxs,gxm
425:       PetscInt ys,ym,gys,gym
426:       PetscReal v(0:4)
427:       PetscInt i1

429:       i1 = 1

431: !     Get local grid boundaries
432:       call DMDAGetCorners(dm,xs,ys,PETSC_NULL_INTEGER,xm,ym,               &
433:      &                PETSC_NULL_INTEGER,ierr)
434:       call DMDAGetGhostCorners(dm,gxs,gys,PETSC_NULL_INTEGER,gxm,gym,      &
435:      &                PETSC_NULL_INTEGER,ierr)

437:       do j=ys,ys+ym-1
438:          do i=xs,xs+xm-1
439:             row = (j-gys)*gxm + (i-gxs)

441:             k = 0
442:             if (j .gt. gys) then
443:                v(k) = -1.0
444:                col(k) = row-gxm
445:                k = k + 1
446:             endif

448:             if (i .gt. gxs) then
449:                v(k) = -1.0
450:                col(k) = row - 1
451:                k = k +1
452:             endif

454:             v(k) = 4.0
455:             col(k) = row
456:             k = k + 1

458:             if (i+1 .lt. gxs + gxm) then
459:                v(k) = -1.0
460:                col(k) = row + 1
461:                k = k + 1
462:             endif

464:             if (j+1 .lt. gys + gym) then
465:                v(k) = -1.0
466:                col(k) = row + gxm
467:                k = k + 1
468:             endif

470:             call MatSetValuesLocal(H,i1,row,k,col,v,INSERT_VALUES,ierr)
471:          enddo
472:       enddo


475: !     Assemble matrix
476:       call MatAssemblyBegin(H,MAT_FINAL_ASSEMBLY,ierr)
477:       call MatAssemblyEnd(H,MAT_FINAL_ASSEMBLY,ierr)


480: !     Tell the matrix we will never add a new nonzero location to the
481: !     matrix.  If we do it will generate an error.

483:       call MatSetOption(H,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE,ierr)
484:       call MatSetOption(H,MAT_SYMMETRIC,PETSC_TRUE,ierr)


487:       call PetscLogFlops(9.0d0*xm*ym + 49.0d0*xm,ierr)

489:       0
490:       return
491:       end



495:       subroutine Monitor(tao, dummy, ierr)
496:       implicit none
497: #include "eptorsion2f.h"
498:       Tao tao
499:       PetscInt dummy
500:       PetscErrorCode ierr

502:       PetscInt its
503:       PetscReal f,gnorm,cnorm,xdiff
504:       TaoConvergedReason reason

506:       call TaoGetSolutionStatus(tao,its,f,gnorm,cnorm,xdiff,             &
507:      &     reason,ierr)
508:       if (mod(its,5) .ne. 0) then
509:          call PetscPrintf(PETSC_COMM_WORLD,'iteration multiple of 5\n',  &
510:      &        ierr)
511:       endif

513:       0

515:       return
516:       end

518:       subroutine ConvergenceTest(tao, dummy, ierr)
519:       implicit none
520: #include "eptorsion2f.h"
521:       Tao tao
522:       PetscInt dummy
523:       PetscErrorCode ierr

525:       PetscInt its
526:       PetscReal f,gnorm,cnorm,xdiff
527:       TaoConvergedReason reason

529:       call TaoGetSolutionStatus(tao,its,f,gnorm,cnorm,xdiff,            &
530:      &     reason,ierr)
531:       if (its .eq. 7) then
532:        call TaoSetConvergedReason(tao,TAO_DIVERGED_MAXITS,ierr)
533:       endif

535:       0

537:       return
538:       end