Actual source code: eptorsion2f.F
petsc-3.8.4 2018-03-24
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: #include "eptorsion2f.h"
39: #if defined(PETSC_USING_F90) && !defined(PETSC_USE_FORTRANKIND)
40: external PETSC_NULL_FUNCTION
41: #endif
42: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43: ! Variable declarations
44: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
45: !
46: ! See additional variable declarations in the file eptorsion2f.h
47: !
48: PetscErrorCode ierr ! used to check for functions returning nonzeros
49: Vec x ! solution vector
50: Mat H ! hessian matrix
51: PetscInt Nx, Ny ! number of processes in x- and y- directions
52: Tao tao ! Tao solver context
53: PetscBool flg
54: PetscInt i1
55: PetscInt dummy
58: ! Note: Any user-defined Fortran routines (such as FormGradient)
59: ! MUST be declared as external.
61: external FormInitialGuess,FormFunctionGradient,ComputeHessian
62: external Monitor,ConvergenceTest
64: i1 = 1
66: ! Initialize TAO, PETSc contexts
67: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
68: if (ierr .ne. 0) then
69: print*,'Unable to initialize PETSc'
70: stop
71: endif
73: ! Specify default parameters
74: param = 5.0
75: mx = 10
76: my = 10
77: Nx = PETSC_DECIDE
78: Ny = PETSC_DECIDE
80: ! Check for any command line arguments that might override defaults
81: call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER, &
82: & '-mx',mx,flg,ierr)
83: call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER, &
84: & '-my',my,flg,ierr)
85: call PetscOptionsGetReal(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER, &
86: & '-par',param,flg,ierr)
89: ! Set up distributed array and vectors
90: call DMDACreate2d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE, &
91: & DM_BOUNDARY_NONE, &
92: & DMDA_STENCIL_BOX,mx,my,Nx,Ny,i1,i1,PETSC_NULL_INTEGER, &
93: & PETSC_NULL_INTEGER,dm,ierr)
94: call DMSetFromOptions(dm,ierr)
95: call DMSetUp(dm,ierr)
97: ! Create vectors
98: call DMCreateGlobalVector(dm,x,ierr)
99: call DMCreateLocalVector(dm,localX,ierr)
101: ! Create Hessian
102: call DMCreateMatrix(dm,H,ierr)
103: call MatSetOption(H,MAT_SYMMETRIC,PETSC_TRUE,ierr)
105: ! The TAO code begins here
107: ! Create TAO solver
108: call TaoCreate(PETSC_COMM_WORLD,tao,ierr)
109: call TaoSetType(tao,TAOCG,ierr)
111: ! Set routines for function and gradient evaluation
113: call TaoSetObjectiveAndGradientRoutine(tao, &
114: & FormFunctionGradient,0,ierr)
115: call TaoSetHessianRoutine(tao,H,H,ComputeHessian, &
116: & 0,ierr)
118: ! Set initial guess
119: call FormInitialGuess(x,ierr)
120: call TaoSetInitialVector(tao,x,ierr)
122: call PetscOptionsHasName(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER, &
123: & '-testmonitor',flg,ierr)
124: if (flg) then
125: call TaoSetMonitor(tao,Monitor,dummy,PETSC_NULL_FUNCTION, &
126: & ierr)
127: endif
129: call PetscOptionsHasName(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER, &
130: & '-testconvergence',flg, ierr)
131: if (flg) then
132: call TaoSetConvergenceTest(tao,ConvergenceTest,dummy, &
133: & ierr)
134: endif
136: ! Check for any TAO command line options
137: call TaoSetFromOptions(tao,ierr)
140: ! SOLVE THE APPLICATION
141: call TaoSolve(tao,ierr)
143: ! Free TAO data structures
144: call TaoDestroy(tao,ierr)
147: ! Free PETSc data structures
148: call VecDestroy(x,ierr)
149: call VecDestroy(localX,ierr)
150: call MatDestroy(H,ierr)
151: call DMDestroy(dm,ierr)
154: ! Finalize TAO and PETSc
155: call PetscFinalize(ierr)
156: end
159: ! ---------------------------------------------------------------------
160: !
161: ! FormInitialGuess - Computes an initial approximation to the solution.
162: !
163: ! Input Parameters:
164: ! X - vector
165: !
166: ! Output Parameters:
167: ! X - vector
168: ! ierr - error code
169: !
170: subroutine FormInitialGuess(X,ierr)
171: #include "eptorsion2f.h"
173: ! Input/output variables:
174: Vec X
175: PetscErrorCode ierr
177: ! Local variables:
178: PetscInt i, j, k, xe, ye
179: PetscReal temp, val, hx, hy
180: PetscInt xs, ys, xm, ym
181: PetscInt gxm, gym, gxs, gys
182: PetscInt i1
184: i1 = 1
185: hx = 1.0/real(mx + 1)
186: hy = 1.0/real(my + 1)
188: ! Get corner information
189: call DMDAGetCorners(dm,xs,ys,PETSC_NULL_INTEGER,xm,ym, &
190: & PETSC_NULL_INTEGER,ierr)
191: call DMDAGetGhostCorners(dm,gxs,gys,PETSC_NULL_INTEGER, &
192: & gxm,gym,PETSC_NULL_INTEGER,ierr)
196: ! Compute initial guess over locally owned part of mesh
197: xe = xs+xm
198: ye = ys+ym
199: do j=ys,ye-1
200: temp = min(j+1,my-j)*hy
201: do i=xs,xe-1
202: k = (j-gys)*gxm + i-gxs
203: val = min((min(i+1,mx-i))*hx,temp)
204: call VecSetValuesLocal(X,i1,k,val,ADD_VALUES,ierr)
205: end do
206: end do
207: call VecAssemblyBegin(X,ierr)
208: call VecAssemblyEnd(X,ierr)
209: return
210: end
213: ! ---------------------------------------------------------------------
214: !
215: ! FormFunctionGradient - Evaluates gradient G(X).
216: !
217: ! Input Parameters:
218: ! tao - the Tao context
219: ! X - input vector
220: ! dummy - optional user-defined context (not used here)
221: !
222: ! Output Parameters:
223: ! f - the function value at X
224: ! G - vector containing the newly evaluated gradient
225: ! ierr - error code
226: !
227: ! Notes:
228: ! This routine serves as a wrapper for the lower-level routine
229: ! "ApplicationGradient", where the actual computations are
230: ! done using the standard Fortran style of treating the local
231: ! input vector data as an array over the local mesh.
232: !
233: subroutine FormFunctionGradient(tao,X,f,G,dummy,ierr)
234: #include "eptorsion2f.h"
236: ! Input/output variables:
237: Tao tao
238: Vec X, G
239: PetscReal f
240: PetscErrorCode ierr
241: PetscInt dummy
243: ! Declarations for use with local array:
246: ! PETSc's VecGetArray acts differently in Fortran than it does in C.
247: ! Calling VecGetArray((Vec) X, (PetscReal) x_array(0:1), (PetscOffset) x_index, ierr)
248: ! will return an array of doubles referenced by x_array offset by x_index.
249: ! i.e., to reference the kth element of X, use x_array(k + x_index).
250: ! Notice that by declaring the arrays with range (0:1), we are using the C 0-indexing practice.
251: PetscReal lx_v(0:1)
252: PetscOffset lx_i
254: ! Local variables:
255: PetscReal zero, p5, area, cdiv3
256: PetscReal val, flin, fquad,floc
257: PetscReal v, vb, vl, vr, vt, dvdx
258: PetscReal dvdy, hx, hy
259: PetscInt xe, ye, xsm, ysm
260: PetscInt xep, yep, i, j, k, ind
261: PetscInt xs, ys, xm, ym
262: PetscInt gxs, gys, gxm, gym
263: PetscInt i1
265: i1 = 1
266: 0
267: cdiv3 = param/3.0
268: zero = 0.0
269: p5 = 0.5
270: hx = 1.0/real(mx + 1)
271: hy = 1.0/real(my + 1)
272: fquad = zero
273: flin = zero
275: ! Initialize gradient to zero
276: call VecSet(G,zero,ierr)
278: ! Scatter ghost points to local vector
279: call DMGlobalToLocalBegin(dm,X,INSERT_VALUES,localX,ierr)
280: call DMGlobalToLocalEnd(dm,X,INSERT_VALUES,localX,ierr)
283: ! Get corner information
284: call DMDAGetCorners(dm,xs,ys,PETSC_NULL_INTEGER,xm,ym, &
285: & PETSC_NULL_INTEGER,ierr)
286: call DMDAGetGhostCorners(dm,gxs,gys,PETSC_NULL_INTEGER, &
287: & gxm,gym,PETSC_NULL_INTEGER,ierr)
289: ! Get pointer to vector data.
290: call VecGetArray(localX,lx_v,lx_i,ierr)
293: ! Set local loop dimensions
294: xe = xs+xm
295: ye = ys+ym
296: if (xs .eq. 0) then
297: xsm = xs-1
298: else
299: xsm = xs
300: endif
301: if (ys .eq. 0) then
302: ysm = ys-1
303: else
304: ysm = ys
305: endif
306: if (xe .eq. mx) then
307: xep = xe+1
308: else
309: xep = xe
310: endif
311: if (ye .eq. my) then
312: yep = ye+1
313: else
314: yep = ye
315: endif
317: ! Compute local gradient contributions over the lower triangular elements
319: do j = ysm, ye-1
320: do i = xsm, xe-1
321: k = (j-gys)*gxm + i-gxs
322: v = zero
323: vr = zero
324: vt = zero
325: if (i .ge. 0 .and. j .ge. 0) v = lx_v(lx_i+k)
326: if (i .lt. mx-1 .and. j .gt. -1) vr = lx_v(lx_i+k+1)
327: if (i .gt. -1 .and. j .lt. my-1) vt = lx_v(lx_i+k+gxm)
328: dvdx = (vr-v)/hx
329: dvdy = (vt-v)/hy
330: if (i .ne. -1 .and. j .ne. -1) then
331: ind = k
332: val = - dvdx/hx - dvdy/hy - cdiv3
333: call VecSetValuesLocal(G,i1,k,val,ADD_VALUES,ierr)
334: endif
335: if (i .ne. mx-1 .and. j .ne. -1) then
336: ind = k+1
337: val = dvdx/hx - cdiv3
338: call VecSetValuesLocal(G,i1,ind,val,ADD_VALUES,ierr)
339: endif
340: if (i .ne. -1 .and. j .ne. my-1) then
341: ind = k+gxm
342: val = dvdy/hy - cdiv3
343: call VecSetValuesLocal(G,i1,ind,val,ADD_VALUES,ierr)
344: endif
345: fquad = fquad + dvdx*dvdx + dvdy*dvdy
346: flin = flin - cdiv3 * (v+vr+vt)
347: end do
348: end do
350: ! Compute local gradient contributions over the upper triangular elements
352: do j = ys, yep-1
353: do i = xs, xep-1
354: k = (j-gys)*gxm + i-gxs
355: vb = zero
356: vl = zero
357: v = zero
358: if (i .lt. mx .and. j .gt. 0) vb = lx_v(lx_i+k-gxm)
359: if (i .gt. 0 .and. j .lt. my) vl = lx_v(lx_i+k-1)
360: if (i .lt. mx .and. j .lt. my) v = lx_v(lx_i+k)
361: dvdx = (v-vl)/hx
362: dvdy = (v-vb)/hy
363: if (i .ne. mx .and. j .ne. 0) then
364: ind = k-gxm
365: val = - dvdy/hy - cdiv3
366: call VecSetValuesLocal(G,i1,ind,val,ADD_VALUES,ierr)
367: endif
368: if (i .ne. 0 .and. j .ne. my) then
369: ind = k-1
370: val = - dvdx/hx - cdiv3
371: call VecSetValuesLocal(G,i1,ind,val,ADD_VALUES,ierr)
372: endif
373: if (i .ne. mx .and. j .ne. my) then
374: ind = k
375: val = dvdx/hx + dvdy/hy - cdiv3
376: call VecSetValuesLocal(G,i1,ind,val,ADD_VALUES,ierr)
377: endif
378: fquad = fquad + dvdx*dvdx + dvdy*dvdy
379: flin = flin - cdiv3*(vb + vl + v)
380: end do
381: end do
383: ! Restore vector
384: call VecRestoreArray(localX,lx_v,lx_i,ierr)
386: ! Assemble gradient vector
387: call VecAssemblyBegin(G,ierr)
388: call VecAssemblyEnd(G,ierr)
390: ! Scale the gradient
391: area = p5*hx*hy
392: floc = area *(p5*fquad+flin)
393: call VecScale(G,area,ierr)
396: ! Sum function contributions from all processes
397: call MPI_Allreduce(floc,f,1,MPIU_SCALAR,MPIU_SUM, &
398: & PETSC_COMM_WORLD,ierr)
399: call PetscLogFlops(20.0d0*(ye-ysm)*(xe-xsm)+ &
400: & 16.0d0*(xep-xs)*(yep-ys),ierr)
401: return
402: end
404: subroutine ComputeHessian(tao, X, H, Hpre, dummy, ierr)
405: #include "eptorsion2f.h"
407: Tao tao
408: Vec X
409: Mat H,Hpre
410: PetscErrorCode ierr
411: PetscInt dummy
414: PetscInt i,j,k
415: PetscInt col(0:4),row
416: PetscInt xs,xm,gxs,gxm
417: PetscInt ys,ym,gys,gym
418: PetscReal v(0:4)
419: PetscInt i1
421: i1 = 1
423: ! Get local grid boundaries
424: call DMDAGetCorners(dm,xs,ys,PETSC_NULL_INTEGER,xm,ym, &
425: & PETSC_NULL_INTEGER,ierr)
426: call DMDAGetGhostCorners(dm,gxs,gys,PETSC_NULL_INTEGER,gxm,gym, &
427: & PETSC_NULL_INTEGER,ierr)
429: do j=ys,ys+ym-1
430: do i=xs,xs+xm-1
431: row = (j-gys)*gxm + (i-gxs)
433: k = 0
434: if (j .gt. gys) then
435: v(k) = -1.0
436: col(k) = row-gxm
437: k = k + 1
438: endif
440: if (i .gt. gxs) then
441: v(k) = -1.0
442: col(k) = row - 1
443: k = k +1
444: endif
446: v(k) = 4.0
447: col(k) = row
448: k = k + 1
450: if (i+1 .lt. gxs + gxm) then
451: v(k) = -1.0
452: col(k) = row + 1
453: k = k + 1
454: endif
456: if (j+1 .lt. gys + gym) then
457: v(k) = -1.0
458: col(k) = row + gxm
459: k = k + 1
460: endif
462: call MatSetValuesLocal(H,i1,row,k,col,v,INSERT_VALUES,ierr)
463: enddo
464: enddo
467: ! Assemble matrix
468: call MatAssemblyBegin(H,MAT_FINAL_ASSEMBLY,ierr)
469: call MatAssemblyEnd(H,MAT_FINAL_ASSEMBLY,ierr)
472: ! Tell the matrix we will never add a new nonzero location to the
473: ! matrix. If we do it will generate an error.
475: call MatSetOption(H,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE,ierr)
476: call MatSetOption(H,MAT_SYMMETRIC,PETSC_TRUE,ierr)
479: call PetscLogFlops(9.0d0*xm*ym + 49.0d0*xm,ierr)
481: 0
482: return
483: end
487: subroutine Monitor(tao, dummy, ierr)
488: #include "eptorsion2f.h"
490: Tao tao
491: PetscInt dummy
492: PetscErrorCode ierr
494: PetscInt its
495: PetscReal f,gnorm,cnorm,xdiff
496: TaoConvergedReason reason
498: call TaoGetSolutionStatus(tao,its,f,gnorm,cnorm,xdiff, &
499: & reason,ierr)
500: if (mod(its,5) .ne. 0) then
501: call PetscPrintf(PETSC_COMM_WORLD,'iteration multiple of 5\n', &
502: & ierr)
503: endif
505: 0
507: return
508: end
510: subroutine ConvergenceTest(tao, dummy, ierr)
511: #include "eptorsion2f.h"
513: Tao tao
514: PetscInt dummy
515: PetscErrorCode ierr
517: PetscInt its
518: PetscReal f,gnorm,cnorm,xdiff
519: TaoConvergedReason reason
521: call TaoGetSolutionStatus(tao,its,f,gnorm,cnorm,xdiff, &
522: & reason,ierr)
523: if (its .eq. 7) then
524: call TaoSetConvergedReason(tao,TAO_DIVERGED_MAXITS,ierr)
525: endif
527: 0
529: return
530: end