Actual source code: ex5f90.F90
1: !
2: ! Description: Solves a nonlinear system in parallel with SNES.
3: ! We solve the Bratu (SFI - solid fuel ignition) problem in a 2D rectangular
4: ! domain, using distributed arrays (DMDAs) to partition the parallel grid.
5: ! The command line options include:
6: ! -par <parameter>, where <parameter> indicates the nonlinearity of the problem
7: ! problem SFI: <parameter> = Bratu parameter (0 <= par <= 6.81)
8: !
10: !
11: ! --------------------------------------------------------------------------
12: !
13: ! Solid Fuel Ignition (SFI) problem. This problem is modeled by
14: ! the partial differential equation
15: !
16: ! -Laplacian u - lambda*exp(u) = 0, 0 < x,y < 1,
17: !
18: ! with boundary conditions
19: !
20: ! u = 0 for x = 0, x = 1, y = 0, y = 1.
21: !
22: ! A finite difference approximation with the usual 5-point stencil
23: ! is used to discretize the boundary value problem to obtain a nonlinear
24: ! system of equations.
25: !
26: ! The uniprocessor version of this code is snes/tutorials/ex4f.F
27: !
28: ! --------------------------------------------------------------------------
29: ! The following define must be used before including any PETSc include files
30: ! into a module or interface. This is because they can't handle declarations
31: ! in them
32: !
33: #include <petsc/finclude/petscsnes.h>
34: #include <petsc/finclude/petscdmda.h>
35: module ex5module
36: use petscsnes
37: use petscdmda
38: implicit none
39: type AppCtx
40: PetscInt xs, xe, xm, gxs, gxe, gxm
41: PetscInt ys, ye, ym, gys, gye, gym
42: PetscInt mx, my
43: PetscMPIInt rank
44: PetscReal lambda
45: end type AppCtx
47: contains
48: ! ---------------------------------------------------------------------
49: !
50: ! FormFunction - Evaluates nonlinear function, F(x).
51: !
52: ! Input Parameters:
53: ! snes - the SNES context
54: ! X - input vector
55: ! dummy - optional user-defined context, as set by SNESSetFunction()
56: ! (not used here)
57: !
58: ! Output Parameter:
59: ! F - function vector
60: !
61: ! Notes:
62: ! This routine serves as a wrapper for the lower-level routine
63: ! "FormFunctionLocal", where the actual computations are
64: ! done using the standard Fortran style of treating the local
65: ! vector data as a multidimensional array over the local mesh.
66: ! This routine merely handles ghost point scatters and accesses
67: ! the local vector data via VecGetArray() and VecRestoreArray().
68: !
69: subroutine FormFunction(snes, X, F, ctx, ierr)
70: ! Input/output variables:
71: SNES snes
72: Vec X, F
73: PetscErrorCode, intent(out) :: ierr
74: type(AppCtx) ctx
75: DM da
77: ! Declarations for use with local arrays:
78: PetscScalar, pointer :: lx_v(:), lf_v(:)
79: Vec localX
81: ! Scatter ghost points to local vector, using the 2-step process
82: ! DMGlobalToLocalBegin(), DMGlobalToLocalEnd().
83: ! By placing code between these two statements, computations can
84: ! be done while messages are in transition.
85: PetscCall(SNESGetDM(snes, da, ierr))
86: PetscCall(DMGetLocalVector(da, localX, ierr))
87: PetscCall(DMGlobalToLocalBegin(da, X, INSERT_VALUES, localX, ierr))
88: PetscCall(DMGlobalToLocalEnd(da, X, INSERT_VALUES, localX, ierr))
90: ! Get a pointer to vector data.
91: ! - For default PETSc vectors, VecGetArray() returns a pointer to
92: ! the data array. Otherwise, the routine is implementation dependent.
93: ! - You MUST call VecRestoreArray() when you no longer need access to
94: ! the array.
95: PetscCall(VecGetArray(localX, lx_v, ierr))
96: PetscCall(VecGetArray(F, lf_v, ierr))
98: ! Compute function over the locally owned part of the grid
99: PetscCall(FormFunctionLocal(lx_v, lf_v, ctx, ierr))
101: ! Restore vectors
102: PetscCall(VecRestoreArray(localX, lx_v, ierr))
103: PetscCall(VecRestoreArray(F, lf_v, ierr))
105: ! Insert values into global vector
107: PetscCall(DMRestoreLocalVector(da, localX, ierr))
108: PetscCall(PetscLogFlops(11.0d0*ctx%ym*ctx%xm, ierr))
110: ! PetscCallA(VecView(X,PETSC_VIEWER_STDOUT_WORLD,ierr))
111: ! PetscCallA(VecView(F,PETSC_VIEWER_STDOUT_WORLD,ierr))
112: end subroutine formfunction
114: ! ---------------------------------------------------------------------
115: !
116: ! FormInitialGuess - Forms initial approximation.
117: !
118: ! Input Parameters:
119: ! X - vector
120: !
121: ! Output Parameter:
122: ! X - vector
123: !
124: ! Notes:
125: ! This routine serves as a wrapper for the lower-level routine
126: ! "InitialGuessLocal", where the actual computations are
127: ! done using the standard Fortran style of treating the local
128: ! vector data as a multidimensional array over the local mesh.
129: ! This routine merely handles ghost point scatters and accesses
130: ! the local vector data via VecGetArray() and VecRestoreArray().
131: !
132: subroutine FormInitialGuess(snes, X, ierr)
133: ! Input/output variables:
134: SNES snes
135: type(AppCtx), pointer:: ctx
136: Vec X
137: PetscErrorCode, intent(out) :: ierr
138: DM da
140: ! Declarations for use with local arrays:
141: PetscScalar, pointer :: lx_v(:)
143: PetscCallA(SNESGetDM(snes, da, ierr))
144: PetscCallA(SNESGetApplicationContext(snes, ctx, ierr))
145: ! Get a pointer to vector data.
146: ! - For default PETSc vectors, VecGetArray() returns a pointer to
147: ! the data array. Otherwise, the routine is implementation dependent.
148: ! - You MUST call VecRestoreArray() when you no longer need access to
149: ! the array.
150: PetscCallA(VecGetArray(X, lx_v, ierr))
152: ! Compute initial guess over the locally owned part of the grid
153: PetscCallA(InitialGuessLocal(ctx, lx_v, ierr))
155: ! Restore vector
156: PetscCallA(VecRestoreArray(X, lx_v, ierr))
158: ! Insert values into global vector
160: end
162: ! ---------------------------------------------------------------------
163: !
164: ! InitialGuessLocal - Computes initial approximation, called by
165: ! the higher level routine FormInitialGuess().
166: !
167: ! Input Parameter:
168: ! x - local vector data
169: !
170: ! Output Parameters:
171: ! x - local vector data
172: ! ierr - error code
173: !
174: ! Notes:
175: ! This routine uses standard Fortran-style computations over a 2-dim array.
176: !
177: subroutine InitialGuessLocal(ctx, x, ierr)
178: ! Input/output variables:
179: type(AppCtx) ctx
180: PetscScalar x(ctx%xs:ctx%xe, ctx%ys:ctx%ye)
181: PetscErrorCode, intent(out) :: ierr
182: ! Local variables:
183: PetscInt i, j
184: PetscReal temp1, temp, hx, hy
186: hx = 1._PETSC_REAL_KIND/(ctx%mx - 1)
187: hy = 1._PETSC_REAL_KIND/(ctx%my - 1)
188: temp1 = ctx%lambda/(ctx%lambda + 1._PETSC_REAL_KIND)
190: do j = ctx%ys, ctx%ye
191: temp = min(j - 1, ctx%my - j)*hy
192: do i = ctx%xs, ctx%xe
193: if (i == 1 .or. j == 1 .or. i == ctx%mx .or. j == ctx%my) then
194: x(i, j) = 0.0
195: else
196: x(i, j) = temp1*sqrt(min(hx*min(i - 1, ctx%mx - i), temp))
197: end if
198: end do
199: end do
200: ierr = 0
201: end
203: ! ---------------------------------------------------------------------
204: !
205: ! FormFunctionLocal - Computes nonlinear function, called by
206: ! the higher level routine FormFunction().
207: !
208: ! Input Parameter:
209: ! x - local vector data
210: !
211: ! Output Parameters:
212: ! f - local vector data, f(x)
213: ! ierr - error code
214: !
215: ! Notes:
216: ! This routine uses standard Fortran-style computations over a 2-dim array.
217: !
218: subroutine FormFunctionLocal(x, f, ctx, ierr)
219: ! Input/output variables:
220: type(AppCtx), intent(in) :: ctx
221: PetscScalar x(ctx%gxs:ctx%gxe, ctx%gys:ctx%gye)
222: PetscScalar f(ctx%xs:ctx%xe, ctx%ys:ctx%ye)
223: PetscErrorCode, intent(out) :: ierr
224: ! Local variables:
225: PetscScalar, parameter :: two = 2.0, one = 1.0
226: PetscScalar hx, hy, hxdhy, hydhx, sc
227: PetscScalar u, uxx, uyy
228: PetscInt i, j
230: hx = one/(ctx%mx - 1)
231: hy = one/(ctx%my - 1)
232: sc = hx*hy*ctx%lambda
233: hxdhy = hx/hy
234: hydhx = hy/hx
236: ! Compute function over the locally owned part of the grid
238: do j = ctx%ys, ctx%ye
239: do i = ctx%xs, ctx%xe
240: if (i == 1 .or. j == 1 .or. i == ctx%mx .or. j == ctx%my) then
241: f(i, j) = x(i, j)
242: else
243: u = x(i, j)
244: uxx = hydhx*(two*u - x(i - 1, j) - x(i + 1, j))
245: uyy = hxdhy*(two*u - x(i, j - 1) - x(i, j + 1))
246: f(i, j) = uxx + uyy - sc*exp(u)
247: end if
248: end do
249: end do
250: ierr = 0
251: end
253: ! ---------------------------------------------------------------------
254: !
255: ! FormJacobian - Evaluates Jacobian matrix.
256: !
257: ! Input Parameters:
258: ! snes - the SNES context
259: ! x - input vector
260: ! dummy - optional user-defined context, as set by SNESSetJacobian()
261: ! (not used here)
262: !
263: ! Output Parameters:
264: ! jac - Jacobian matrix
265: ! jac_prec - optionally different matrix used to construct the preconditioner (not used here)
266: !
267: ! Notes:
268: ! This routine serves as a wrapper for the lower-level routine
269: ! "FormJacobianLocal", where the actual computations are
270: ! done using the standard Fortran style of treating the local
271: ! vector data as a multidimensional array over the local mesh.
272: ! This routine merely accesses the local vector data via
273: ! VecGetArray() and VecRestoreArray().
274: !
275: ! Notes:
276: ! Due to grid point reordering with DMDAs, we must always work
277: ! with the local grid points, and then transform them to the new
278: ! global numbering with the "ltog" mapping
279: ! We cannot work directly with the global numbers for the original
280: ! uniprocessor grid!
281: !
282: ! Two methods are available for imposing this transformation
283: ! when setting matrix entries:
284: ! (A) MatSetValuesLocal(), using the local ordering (including
285: ! ghost points!)
286: ! - Set matrix entries using the local ordering
287: ! by calling MatSetValuesLocal()
288: ! (B) MatSetValues(), using the global ordering
290: ! - Set matrix entries using the global ordering by calling
291: ! MatSetValues()
292: ! Option (A) seems cleaner/easier in many cases, and is the procedure
293: ! used in this example.
294: !
295: subroutine FormJacobian(snes, X, jac, jac_prec, ctx, ierr)
296: ! Input/output variables:
297: SNES snes
298: Vec X
299: Mat jac, jac_prec
300: type(AppCtx) ctx
301: PetscErrorCode, intent(out) :: ierr
302: DM da
303: ! Declarations for use with local arrays:
304: PetscScalar, pointer :: lx_v(:)
305: Vec localX
307: ! Scatter ghost points to local vector, using the 2-step process
308: ! DMGlobalToLocalBegin(), DMGlobalToLocalEnd()
309: ! Computations can be done while messages are in transition,
310: ! by placing code between these two statements.
312: PetscCallA(SNESGetDM(snes, da, ierr))
313: PetscCallA(DMGetLocalVector(da, localX, ierr))
314: PetscCallA(DMGlobalToLocalBegin(da, X, INSERT_VALUES, localX, ierr))
315: PetscCallA(DMGlobalToLocalEnd(da, X, INSERT_VALUES, localX, ierr))
317: ! Get a pointer to vector data
318: PetscCallA(VecGetArray(localX, lx_v, ierr))
320: ! Compute entries for the locally owned part of the Jacobian preconditioner.
321: PetscCallA(FormJacobianLocal(lx_v, jac_prec, ctx, ierr))
323: ! Assemble matrix, using the 2-step process:
324: ! MatAssemblyBegin(), MatAssemblyEnd()
325: ! Computations can be done while messages are in transition,
326: ! by placing code between these two statements.
328: PetscCallA(MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY, ierr))
329: if (jac /= jac_prec) then
330: PetscCallA(MatAssemblyBegin(jac_prec, MAT_FINAL_ASSEMBLY, ierr))
331: end if
332: PetscCallA(VecRestoreArray(localX, lx_v, ierr))
333: PetscCallA(DMRestoreLocalVector(da, localX, ierr))
334: PetscCallA(MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY, ierr))
335: if (jac /= jac_prec) then
336: PetscCallA(MatAssemblyEnd(jac_prec, MAT_FINAL_ASSEMBLY, ierr))
337: end if
339: ! Tell the matrix we will never add a new nonzero location to the
340: ! matrix. If we do it will generate an error.
342: PetscCallA(MatSetOption(jac, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE, ierr))
344: end
346: ! ---------------------------------------------------------------------
347: !
348: ! FormJacobianLocal - Computes Jacobian matrix used to compute the preconditioner,
349: ! called by the higher level routine FormJacobian().
350: !
351: ! Input Parameters:
352: ! x - local vector data
353: !
354: ! Output Parameters:
355: ! jac_prec - Jacobian matrix used to compute the preconditioner
356: ! ierr - error code
357: !
358: ! Notes:
359: ! This routine uses standard Fortran-style computations over a 2-dim array.
360: !
361: ! Notes:
362: ! Due to grid point reordering with DMDAs, we must always work
363: ! with the local grid points, and then transform them to the new
364: ! global numbering with the "ltog" mapping
365: ! We cannot work directly with the global numbers for the original
366: ! uniprocessor grid!
367: !
368: ! Two methods are available for imposing this transformation
369: ! when setting matrix entries:
370: ! (A) MatSetValuesLocal(), using the local ordering (including
371: ! ghost points!)
372: ! - Set matrix entries using the local ordering
373: ! by calling MatSetValuesLocal()
374: ! (B) MatSetValues(), using the global ordering
375: ! - Then apply this map explicitly yourself
376: ! - Set matrix entries using the global ordering by calling
377: ! MatSetValues()
378: ! Option (A) seems cleaner/easier in many cases, and is the procedure
379: ! used in this example.
380: !
381: subroutine FormJacobianLocal(x, jac_prec, ctx, ierr)
382: ! Input/output variables:
383: type(AppCtx) ctx
384: PetscScalar x(ctx%gxs:ctx%gxe, ctx%gys:ctx%gye)
385: Mat jac_prec
386: PetscErrorCode ierr
388: ! Local variables:
389: PetscInt row, col(5), i, j
390: PetscScalar, parameter :: two = 2.0, one = 1.0
391: PetscScalar hx, hy, hxdhy, hydhx, sc, v(5)
393: ! Set parameters
394: hx = one/(ctx%mx - 1)
395: hy = one/(ctx%my - 1)
396: sc = hx*hy
397: hxdhy = hx/hy
398: hydhx = hy/hx
400: ! Compute entries for the locally owned part of the Jacobian.
401: ! - Currently, all PETSc parallel matrix formats are partitioned by
402: ! contiguous chunks of rows across the processors.
403: ! - Each processor needs to insert only elements that it owns
404: ! locally (but any non-local elements will be sent to the
405: ! appropriate processor during matrix assembly).
406: ! - Here, we set all entries for a particular row at once.
407: ! - We can set matrix entries either using either
408: ! MatSetValuesLocal() or MatSetValues(), as discussed above.
409: ! - Note that MatSetValues() uses 0-based row and column numbers
410: ! in Fortran as well as in C.
412: do j = ctx%ys, ctx%ye
413: row = (j - ctx%gys)*ctx%gxm + ctx%xs - ctx%gxs - 1
414: do i = ctx%xs, ctx%xe
415: row = row + 1
416: ! boundary points
417: if (i == 1 .or. j == 1 .or. i == ctx%mx .or. j == ctx%my) then
418: col(1) = row
419: v(1) = one
420: PetscCallA(MatSetValuesLocal(jac_prec, 1_PETSC_INT_KIND, [row], 1_PETSC_INT_KIND, col, v, INSERT_VALUES, ierr))
421: ! interior grid points
422: else
423: v(1) = -hxdhy
424: v(2) = -hydhx
425: v(3) = two*(hydhx + hxdhy) - sc*ctx%lambda*exp(x(i, j))
426: v(4) = -hydhx
427: v(5) = -hxdhy
428: col(1) = row - ctx%gxm
429: col(2) = row - 1
430: col(3) = row
431: col(4) = row + 1
432: col(5) = row + ctx%gxm
433: PetscCallA(MatSetValuesLocal(jac_prec, 1_PETSC_INT_KIND, [row], 5_PETSC_INT_KIND, col, v, INSERT_VALUES, ierr))
434: end if
435: end do
436: end do
438: end
440: end module ex5module
442: program main
443: use ex5module
444: implicit none
445: !
447: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
448: ! Variable declarations
449: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
450: !
451: ! Variables:
452: ! snes - nonlinear solver
453: ! x, r - solution, residual vectors
454: ! J - Jacobian matrix
455: ! its - iterations for convergence
456: ! Nx, Ny - number of preocessors in x- and y- directions
457: ! matrix_free - flag - 1 indicates matrix-free version
458: !
459: SNES snes
460: Vec x, r
461: Mat J
462: PetscErrorCode ierr
463: PetscInt its
464: PetscBool flg, matrix_free
465: PetscReal, parameter :: lambda_min = 0.0_PETSC_REAL_KIND, lambda_max = 6.81_PETSC_REAL_KIND
466: type(AppCtx) ctx
467: DM da
469: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
470: ! Initialize program
471: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
472: PetscCallA(PetscInitialize(ierr))
473: PetscCallMPIA(MPI_Comm_rank(PETSC_COMM_WORLD, ctx%rank, ierr))
475: ! Initialize problem parameters
476: ctx%lambda = 6.0
477: PetscCallA(PetscOptionsGetReal(PETSC_NULL_OPTIONS, PETSC_NULL_CHARACTER, '-par', ctx%lambda, flg, ierr))
478: PetscCheckA(ctx%lambda < lambda_max .and. ctx%lambda > lambda_min, PETSC_COMM_SELF, PETSC_ERR_USER, 'Lambda provided with -par is out of range')
480: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
481: ! Create nonlinear solver context
482: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
483: PetscCallA(SNESCreate(PETSC_COMM_WORLD, snes, ierr))
485: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
486: ! Create vector data structures; set function evaluation routine
487: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
489: ! Create distributed array (DMDA) to manage parallel grid and vectors
491: ! This really needs only the star-type stencil, but we use the box
492: ! stencil temporarily.
493: PetscCallA(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_BOX, 4_PETSC_INT_KIND, 4_PETSC_INT_KIND, PETSC_DECIDE, PETSC_DECIDE, 1_PETSC_INT_KIND, 1_PETSC_INT_KIND, PETSC_NULL_INTEGER_ARRAY, PETSC_NULL_INTEGER_ARRAY, da, ierr))
494: PetscCallA(DMSetFromOptions(da, ierr))
495: PetscCallA(DMSetUp(da, ierr))
497: PetscCallA(DMDAGetInfo(da, PETSC_NULL_INTEGER, ctx%mx, ctx%my, PETSC_NULL_INTEGER, PETSC_NULL_INTEGER, PETSC_NULL_INTEGER, PETSC_NULL_INTEGER, PETSC_NULL_INTEGER, PETSC_NULL_INTEGER, PETSC_NULL_DMBOUNDARYTYPE, PETSC_NULL_DMBOUNDARYTYPE, PETSC_NULL_DMBOUNDARYTYPE, PETSC_NULL_DMDASTENCILTYPE, ierr))
499: !
500: ! Visualize the distribution of the array across the processors
501: !
502: ! PetscCallA(DMView(da,PETSC_VIEWER_DRAW_WORLD,ierr))
504: ! Extract global and local vectors from DMDA; then duplicate for remaining
505: ! vectors that are the same types
506: PetscCallA(DMCreateGlobalVector(da, x, ierr))
507: PetscCallA(VecDuplicate(x, r, ierr))
509: ! Get local grid boundaries (for 2-dimensional DMDA)
510: PetscCallA(DMDAGetCorners(da, ctx%xs, ctx%ys, PETSC_NULL_INTEGER, ctx%xm, ctx%ym, PETSC_NULL_INTEGER, ierr))
511: PetscCallA(DMDAGetGhostCorners(da, ctx%gxs, ctx%gys, PETSC_NULL_INTEGER, ctx%gxm, ctx%gym, PETSC_NULL_INTEGER, ierr))
513: ! Here we shift the starting indices up by one so that we can easily
514: ! use the Fortran convention of 1-based indices (rather 0-based indices).
515: ctx%xs = ctx%xs + 1
516: ctx%ys = ctx%ys + 1
517: ctx%gxs = ctx%gxs + 1
518: ctx%gys = ctx%gys + 1
520: ctx%ye = ctx%ys + ctx%ym - 1
521: ctx%xe = ctx%xs + ctx%xm - 1
522: ctx%gye = ctx%gys + ctx%gym - 1
523: ctx%gxe = ctx%gxs + ctx%gxm - 1
525: PetscCallA(SNESSetApplicationContext(snes, ctx, ierr))
527: ! Set function evaluation routine and vector
528: PetscCallA(SNESSetFunction(snes, r, FormFunction, ctx, ierr))
530: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
531: ! Create matrix data structure; set Jacobian evaluation routine
532: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
534: ! Set Jacobian matrix data structure and default Jacobian evaluation
535: ! routine. User can override with:
536: ! -snes_fd : default finite differencing approximation of Jacobian
537: ! -snes_mf : matrix-free Newton-Krylov method with no preconditioning
538: ! (unless user explicitly sets preconditioner)
539: ! -snes_mf_operator : form matrix used to construct the preconditioner as set by the user,
540: ! but use matrix-free approx for Jacobian-vector
541: ! products within Newton-Krylov method
542: !
543: ! Note: For the parallel case, vectors and matrices MUST be partitioned
544: ! accordingly. When using distributed arrays (DMDAs) to create vectors,
545: ! the DMDAs determine the problem partitioning. We must explicitly
546: ! specify the local matrix dimensions upon its creation for compatibility
547: ! with the vector distribution. Thus, the generic MatCreate() routine
548: ! is NOT sufficient when working with distributed arrays.
549: !
550: ! Note: Here we only approximately preallocate storage space for the
551: ! Jacobian. See the users manual for a discussion of better techniques
552: ! for preallocating matrix memory.
554: PetscCallA(PetscOptionsHasName(PETSC_NULL_OPTIONS, PETSC_NULL_CHARACTER, '-snes_mf', matrix_free, ierr))
555: if (.not. matrix_free) then
556: PetscCallA(DMSetMatType(da, MATAIJ, ierr))
557: PetscCallA(DMCreateMatrix(da, J, ierr))
558: PetscCallA(SNESSetJacobian(snes, J, J, FormJacobian, ctx, ierr))
559: end if
561: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
562: ! Customize nonlinear solver; set runtime options
563: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
564: ! Set runtime options (e.g., -snes_monitor -snes_rtol <rtol> -ksp_type <type>)
565: PetscCallA(SNESSetDM(snes, da, ierr))
566: PetscCallA(SNESSetFromOptions(snes, ierr))
568: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
569: ! Evaluate initial guess; then solve nonlinear system.
570: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
571: ! Note: The user should initialize the vector, x, with the initial guess
572: ! for the nonlinear solver prior to calling SNESSolve(). In particular,
573: ! to employ an initial guess of zero, the user should explicitly set
574: ! this vector to zero by calling VecSet().
576: PetscCallA(FormInitialGuess(snes, x, ierr))
577: PetscCallA(SNESSolve(snes, PETSC_NULL_VEC, x, ierr))
578: PetscCallA(SNESGetIterationNumber(snes, its, ierr))
579: if (ctx%rank == 0) then
580: write (6, 100) its
581: end if
582: 100 format('Number of SNES iterations = ', i5)
584: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
585: ! Free work space. All PETSc objects should be destroyed when they
586: ! are no longer needed.
587: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
588: if (.not. matrix_free) PetscCallA(MatDestroy(J, ierr))
589: PetscCallA(VecDestroy(x, ierr))
590: PetscCallA(VecDestroy(r, ierr))
591: PetscCallA(SNESDestroy(snes, ierr))
592: PetscCallA(DMDestroy(da, ierr))
594: PetscCallA(PetscFinalize(ierr))
595: end
596: !
597: !/*TEST
598: !
599: ! test:
600: ! nsize: 4
601: ! args: -snes_mf -pc_type none -da_processors_x 4 -da_processors_y 1 -snes_monitor_short -ksp_gmres_cgs_refinement_type refine_always
602: ! requires: !single
603: !
604: ! test:
605: ! suffix: 2
606: ! nsize: 4
607: ! args: -da_processors_x 2 -da_processors_y 2 -snes_monitor_short -ksp_gmres_cgs_refinement_type refine_always
608: ! requires: !single
609: !
610: ! test:
611: ! suffix: 3
612: ! nsize: 3
613: ! args: -snes_fd -snes_monitor_short -ksp_gmres_cgs_refinement_type refine_always
614: ! requires: !single
615: !
616: ! test:
617: ! suffix: 4
618: ! nsize: 3
619: ! args: -snes_mf_operator -snes_monitor_short -ksp_gmres_cgs_refinement_type refine_always
620: ! requires: !single
621: !
622: ! test:
623: ! suffix: 5
624: ! requires: !single
625: !
626: !TEST*/