Actual source code: ex1f.F
petsc-3.6.1 2015-08-06
1: !
2: ! Solves the time dependent Bratu problem using pseudo-timestepping
3: !
4: ! Concepts: TS^pseudo-timestepping
5: ! Concepts: pseudo-timestepping
6: ! Concepts: TS^nonlinear problems
7: ! Processors: 1
8: !
9: ! This code demonstrates how one may solve a nonlinear problem
10: ! with pseudo-timestepping. In this simple example, the pseudo-timestep
11: ! is the same for all grid points, i.e., this is equivalent to using
12: ! the backward Euler method with a variable timestep.
13: !
14: ! Note: This example does not require pseudo-timestepping since it
15: ! is an easy nonlinear problem, but it is included to demonstrate how
16: ! the pseudo-timestepping may be done.
17: !
18: ! See snes/examples/tutorials/ex4.c[ex4f.F] and
19: ! snes/examples/tutorials/ex5.c[ex5f.F] where the problem is described
20: ! and solved using the method of Newton alone.
21: !
22: ! Include "petscts.h" to use the PETSc timestepping routines,
23: ! "petscsys.h" for basic PETSc operation,
24: ! "petscmat.h" for matrix operations,
25: ! "petscpc.h" for preconditions, and
26: ! "petscvec.h" for vector operations.
27: !
28: !23456789012345678901234567890123456789012345678901234567890123456789012
29: program main
30: implicit none
31: #include <petsc/finclude/petscsys.h>
32: #include <petsc/finclude/petscvec.h>
33: #include <petsc/finclude/petscmat.h>
34: #include <petsc/finclude/petscpc.h>
35: #include <petsc/finclude/petscts.h>
36: !
37: ! Create an application context to contain data needed by the
38: ! application-provided call-back routines, FormJacobian() and
39: ! FormFunction(). We use a double precision array with three
40: ! entries indexed by param, lmx, lmy.
41: !
42: PetscReal user(3)
43: PetscInt param,lmx,lmy,i5
44: parameter (param = 1,lmx = 2,lmy = 3)
45: !
46: ! User-defined routines
47: !
48: external FormJacobian,FormFunction
49: !
50: ! Data for problem
51: !
52: TS ts
53: Vec x,r
54: Mat J
55: PetscInt its,N,i1000
56: PetscBool flg
57: PetscErrorCode ierr
58: PetscReal param_max,param_min,dt
59: PetscReal tmax,zero
60: PetscReal ftime
61: TSConvergedReason reason
63: i5 = 5
64: param_max = 6.81
65: param_min = 0
67: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
68: user(lmx) = 4
69: user(lmy) = 4
70: user(param) = 6.0
72: !
73: ! Allow user to set the grid dimensions and nonlinearity parameter at run-time
74: !
75: call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-mx',user(lmx), &
76: & flg,ierr)
77: call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-my',user(lmy), &
78: & flg,ierr)
79: call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-param', &
80: & user(param),flg,ierr)
81: if (user(param) .ge. param_max .or. &
82: & user(param) .le. param_min) then
83: print*,'Parameter is out of range'
84: endif
85: if (user(lmx) .gt. user(lmy)) then
86: dt = .5/user(lmx)
87: else
88: dt = .5/user(lmy)
89: endif
90: call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-dt',dt,flg,ierr)
91: N = int(user(lmx)*user(lmy))
93: !
94: ! Create vectors to hold the solution and function value
95: !
96: call VecCreateSeq(PETSC_COMM_SELF,N,x,ierr)
97: call VecDuplicate(x,r,ierr)
99: !
100: ! Create matrix to hold Jacobian. Preallocate 5 nonzeros per row
101: ! in the sparse matrix. Note that this is not the optimal strategy see
102: ! the Performance chapter of the users manual for information on
103: ! preallocating memory in sparse matrices.
104: !
105: i5 = 5
106: call MatCreateSeqAIJ(PETSC_COMM_SELF,N,N,i5,PETSC_NULL_INTEGER, &
107: & J,ierr)
109: !
110: ! Create timestepper context
111: !
113: call TSCreate(PETSC_COMM_WORLD,ts,ierr)
114: call TSSetProblemType(ts,TS_NONLINEAR,ierr)
116: !
117: ! Tell the timestepper context where to compute solutions
118: !
120: call TSSetSolution(ts,x,ierr)
122: !
123: ! Provide the call-back for the nonlinear function we are
124: ! evaluating. Thus whenever the timestepping routines need the
125: ! function they will call this routine. Note the final argument
126: ! is the application context used by the call-back functions.
127: !
129: call TSSetRHSFunction(ts,PETSC_NULL_OBJECT,FormFunction,user,ierr)
131: !
132: ! Set the Jacobian matrix and the function used to compute
133: ! Jacobians.
134: !
136: call TSSetRHSJacobian(ts,J,J,FormJacobian,user,ierr)
138: !
139: ! For the initial guess for the problem
140: !
142: call FormInitialGuess(x,user,ierr)
144: !
145: ! This indicates that we are using pseudo timestepping to
146: ! find a steady state solution to the nonlinear problem.
147: !
149: call TSSetType(ts,TSPSEUDO,ierr)
151: !
152: ! Set the initial time to start at (this is arbitrary for
153: ! steady state problems and the initial timestep given above
154: !
156: zero = 0.0
157: call TSSetInitialTimeStep(ts,zero,dt,ierr)
159: !
160: ! Set a large number of timesteps and final duration time
161: ! to insure convergence to steady state.
162: !
163: i1000 = 1000
164: tmax = 1.e12
165: call TSSetDuration(ts,i1000,tmax,ierr)
167: !
168: ! Set any additional options from the options database. This
169: ! includes all options for the nonlinear and linear solvers used
170: ! internally the timestepping routines.
171: !
173: call TSSetFromOptions(ts,ierr)
175: call TSSetUp(ts,ierr)
177: !
178: ! Perform the solve. This is where the timestepping takes place.
179: !
180: call TSSolve(ts,x,ierr)
181: call TSGetSolveTime(ts,ftime,ierr)
182: call TSGetTimeStepNumber(ts,its,ierr)
183: call TSGetConvergedReason(ts,reason,ierr)
185: write(6,100) its,ftime,reason
186: 100 format('Number of pseudo time-steps ',i5,' final time ',1pe8.2 &
187: & ,' reason ',i3)
189: !
190: ! Free the data structures constructed above
191: !
193: call VecDestroy(x,ierr)
194: call VecDestroy(r,ierr)
195: call MatDestroy(J,ierr)
196: call TSDestroy(ts,ierr)
197: call PetscFinalize(ierr)
198: end
200: !
201: ! -------------------- Form initial approximation -----------------
202: !
203: subroutine FormInitialGuess(X,user,ierr)
204: implicit none
205: #include <petsc/finclude/petscsys.h>
206: #include <petsc/finclude/petscvec.h>
207: #include <petsc/finclude/petscmat.h>
208: #include <petsc/finclude/petscpc.h>
209: #include <petsc/finclude/petscts.h>
210: Vec X
211: PetscReal user(3)
212: PetscInt i,j,row,mx,my
213: PetscErrorCode ierr
214: PetscOffset xidx
215: PetscReal one,lambda
216: PetscReal temp1,temp,hx,hy
217: PetscScalar xx(1)
218: PetscInt param,lmx,lmy
219: parameter (param = 1,lmx = 2,lmy = 3)
221: one = 1.0
223: mx = int(user(lmx))
224: my = int(user(lmy))
225: lambda = user(param)
227: hy = one / (my-1)
228: hx = one / (mx-1)
230: call VecGetArray(X,xx,xidx,ierr)
231: temp1 = lambda/(lambda + one)
232: do 10, j=1,my
233: temp = min(j-1,my-j)*hy
234: do 20 i=1,mx
235: row = i + (j-1)*mx
236: if (i .eq. 1 .or. j .eq. 1 .or. &
237: & i .eq. mx .or. j .eq. my) then
238: xx(row+xidx) = 0.0
239: else
240: xx(row+xidx) = &
241: & temp1*sqrt(min(min(i-1,mx-i)*hx,temp))
242: endif
243: 20 continue
244: 10 continue
245: call VecRestoreArray(X,xx,xidx,ierr)
246: return
247: end
248: !
249: ! -------------------- Evaluate Function F(x) ---------------------
250: !
251: subroutine FormFunction(ts,t,X,F,user,ierr)
252: implicit none
253: #include <petsc/finclude/petscsys.h>
254: #include <petsc/finclude/petscvec.h>
255: #include <petsc/finclude/petscmat.h>
256: #include <petsc/finclude/petscpc.h>
257: #include <petsc/finclude/petscts.h>
258: TS ts
259: PetscReal t
260: Vec X,F
261: PetscReal user(3)
262: PetscErrorCode ierr
263: PetscInt i,j,row,mx,my
264: PetscOffset xidx,fidx
265: PetscReal two,lambda
266: PetscReal hx,hy,hxdhy,hydhx
267: PetscScalar ut,ub,ul,ur,u
268: PetscScalar uxx,uyy,sc
269: PetscScalar xx(1),ff(1)
270: PetscInt param,lmx,lmy
271: parameter (param = 1,lmx = 2,lmy = 3)
273: two = 2.0
275: mx = int(user(lmx))
276: my = int(user(lmy))
277: lambda = user(param)
279: hx = 1.0 / (mx-1)
280: hy = 1.0 / (my-1)
281: sc = hx*hy
282: hxdhy = hx/hy
283: hydhx = hy/hx
285: call VecGetArrayRead(X,xx,xidx,ierr)
286: call VecGetArray(F,ff,fidx,ierr)
287: do 10 j=1,my
288: do 20 i=1,mx
289: row = i + (j-1)*mx
290: if (i .eq. 1 .or. j .eq. 1 .or. &
291: & i .eq. mx .or. j .eq. my) then
292: ff(row+fidx) = xx(row+xidx)
293: else
294: u = xx(row + xidx)
295: ub = xx(row - mx + xidx)
296: ul = xx(row - 1 + xidx)
297: ut = xx(row + mx + xidx)
298: ur = xx(row + 1 + xidx)
299: uxx = (-ur + two*u - ul)*hydhx
300: uyy = (-ut + two*u - ub)*hxdhy
301: ff(row+fidx) = -uxx - uyy + sc*lambda*exp(u)
302: u = -uxx - uyy + sc*lambda*exp(u)
303: endif
304: 20 continue
305: 10 continue
307: call VecRestoreArrayRead(X,xx,xidx,ierr)
308: call VecRestoreArray(F,ff,fidx,ierr)
309: return
310: end
311: !
312: ! -------------------- Evaluate Jacobian of F(x) --------------------
313: !
314: subroutine FormJacobian(ts,ctime,X,JJ,B,user,ierr)
315: implicit none
316: #include <petsc/finclude/petscsys.h>
317: #include <petsc/finclude/petscvec.h>
318: #include <petsc/finclude/petscmat.h>
319: #include <petsc/finclude/petscpc.h>
320: #include <petsc/finclude/petscts.h>
321: TS ts
322: Vec X
323: Mat JJ,B
324: PetscReal user(3),ctime
325: PetscErrorCode ierr
326: Mat jac
327: PetscOffset xidx
328: PetscInt i,j,row(1),mx,my
329: PetscInt col(5),i1,i5
330: PetscScalar two,one,lambda
331: PetscScalar v(5),sc,xx(1)
332: PetscReal hx,hy,hxdhy,hydhx
334: PetscInt param,lmx,lmy
335: parameter (param = 1,lmx = 2,lmy = 3)
337: i1 = 1
338: i5 = 5
339: jac = B
340: two = 2.0
341: one = 1.0
343: mx = int(user(lmx))
344: my = int(user(lmy))
345: lambda = user(param)
347: hx = 1.0 / (mx-1)
348: hy = 1.0 / (my-1)
349: sc = hx*hy
350: hxdhy = hx/hy
351: hydhx = hy/hx
353: call VecGetArrayRead(X,xx,xidx,ierr)
354: do 10 j=1,my
355: do 20 i=1,mx
356: !
357: ! When inserting into PETSc matrices, indices start at 0
358: !
359: row(1) = i - 1 + (j-1)*mx
360: if (i .eq. 1 .or. j .eq. 1 .or. &
361: & i .eq. mx .or. j .eq. my) then
362: call MatSetValues(jac,i1,row,i1,row,one,INSERT_VALUES,ierr)
363: else
364: v(1) = hxdhy
365: col(1) = row(1) - mx
366: v(2) = hydhx
367: col(2) = row(1) - 1
368: v(3) = -two*(hydhx+hxdhy)+sc*lambda*exp(xx(row(1)+1+xidx))
369: col(3) = row(1)
370: v(4) = hydhx
371: col(4) = row(1) + 1
372: v(5) = hxdhy
373: col(5) = row(1) + mx
374: call MatSetValues(jac,i1,row,i5,col,v,INSERT_VALUES,ierr)
375: endif
376: 20 continue
377: 10 continue
378: call MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY,ierr)
379: call MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY,ierr)
380: call VecRestoreArray(X,xx,xidx,ierr)
381: return
382: end