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