Actual source code: ex22f.F
1: ! Time-dependent advection-reaction PDE in 1d. Demonstrates IMEX methods
2: !
3: ! u_t + a1*u_x = -k1*u + k2*v + s1
4: ! v_t + a2*v_x = k1*u - k2*v + s2
5: ! 0 < x < 1;
6: ! a1 = 1, k1 = 10^6, s1 = 0,
7: ! a2 = 0, k2 = 2*k1, s2 = 1
8: !
9: ! Initial conditions:
10: ! u(x,0) = 1 * s2*x
11: ! v(x,0) = k0/k1*u(x,0) + s1/k1
12: !
13: ! Upstream boundary conditions:
14: ! u(0,t) = 1-sin(12*t)^4
15: !
17: program main
18: implicit none
19: #include <finclude/petscsys.h>
20: #include <finclude/petscvec.h>
21: #include <finclude/petscmat.h>
22: #include <finclude/petscsnes.h>
23: #include <finclude/petscts.h>
24: #include <finclude/petscdmda.h>
25: !
26: ! Create an application context to contain data needed by the
27: ! application-provided call-back routines, FormJacobian() and
28: ! FormFunction(). We use a double precision array with six
29: ! entries, two for each problem parameter a, k, s.
30: !
31: PetscReal user(6)
32: integer user_a,user_k,user_s
33: parameter (user_a = 0,user_k = 2,user_s = 4)
35: external FormRHSFunction,FormIFunction,FormIJacobian
36: external FormInitialSolution
38: TS ts
39: Vec X
40: Mat J
41: PetscInt steps,maxsteps,mx
42: PetscErrorCode ierr
43: DM da
44: PetscReal ftime,dt
45: PetscReal zero,one,pone
46: PetscInt im11,i2
47: PetscBool flg
49: im11 = -11
50: i2 = 2
51: zero = 0.0
52: one = 1.0
53: pone = one / 10
55: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
57: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
58: ! Create distributed array (DMDA) to manage parallel grid and vectors
59: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
60: call DMDACreate1d(PETSC_COMM_WORLD,DMDA_BOUNDARY_NONE,im11,i2,i2, &
61: & PETSC_NULL_INTEGER,da,ierr)
63: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
64: ! Extract global vectors from DMDA;
65: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
66: call DMCreateGlobalVector(da,X,ierr)
68: ! Initialize user application context
69: ! Use zero-based indexing for command line parameters to match ex22.c
70: user(user_a+1) = 1.0
71: call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-a0', &
72: & user(user_a+1),flg,ierr)
73: user(user_a+2) = 0.0
74: call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-a1', &
75: & user(user_a+2),flg,ierr)
76: user(user_k+1) = 1000000.0
77: call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-k0', &
78: & user(user_k+1),flg,ierr)
79: user(user_k+2) = 2*user(user_k+1)
80: call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-k1', &
81: & user(user_k+2),flg,ierr)
82: user(user_s+1) = 0.0
83: call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-s0', &
84: & user(user_s+1),flg,ierr)
85: user(user_s+2) = 1.0
86: call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-s1', &
87: & user(user_s+2),flg,ierr)
89: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
90: ! Create timestepping solver context
91: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
92: call TSCreate(PETSC_COMM_WORLD,ts,ierr)
93: call TSSetDM(ts,da,ierr)
94: call TSSetType(ts,TSARKIMEX,ierr)
95: call TSSetRHSFunction(ts,PETSC_NULL_OBJECT,FormRHSFunction, &
96: & user,ierr)
97: call TSSetIFunction(ts,PETSC_NULL_OBJECT,FormIFunction,user,ierr)
98: call DMGetMatrix(da,MATAIJ,J,ierr)
99: call TSSetIJacobian(ts,J,J,FormIJacobian,user,ierr)
101: ftime = 1.0
102: maxsteps = 10000
103: call TSSetDuration(ts,maxsteps,ftime,ierr)
105: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
106: ! Set initial conditions
107: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
108: call FormInitialSolution(ts,X,user,ierr)
109: call TSSetSolution(ts,X,ierr)
110: call VecGetSize(X,mx,ierr)
111: ! Advective CFL, I don't know why it needs so much safety factor.
112: dt = pone * max(user(user_a+1),user(user_a+2)) / mx;
113: call TSSetInitialTimeStep(ts,zero,dt,ierr)
115: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
116: ! Set runtime options
117: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
118: call TSSetFromOptions(ts,ierr)
120: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
121: ! Solve nonlinear system
122: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
123: call TSSolve(ts,X,ftime,ierr)
124: call TSGetTimeStepNumber(ts,steps,ierr)
126: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
127: ! Free work space.
128: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
129: call MatDestroy(J,ierr)
130: call VecDestroy(X,ierr)
131: call TSDestroy(ts,ierr)
132: call DMDestroy(da,ierr)
133: call PetscFinalize(ierr)
134: end program
136: ! Small helper to extract the layout, result uses 1-based indexing.
137: subroutine GetLayout(da,mx,xs,xe,gxs,gxe,ierr)
138: implicit none
139: #include <finclude/petscsys.h>
140: #include <finclude/petscdmda.h>
141: DM da
142: PetscInt mx,xs,xe,gxs,gxe
143: PetscErrorCode ierr
144: PetscInt xm,gxm
145: call DMDAGetInfo(da,PETSC_NULL_INTEGER, &
146: & mx,PETSC_NULL_INTEGER,PETSC_NULL_INTEGER, &
147: & PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,PETSC_NULL_INTEGER, &
148: & PETSC_NULL_INTEGER,PETSC_NULL_INTEGER, &
149: & PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,PETSC_NULL_INTEGER, &
150: & PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,ierr)
151: call DMDAGetCorners(da,xs,PETSC_NULL_INTEGER,PETSC_NULL_INTEGER, &
152: & xm,PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,ierr)
153: call DMDAGetGhostCorners(da, &
154: & gxs,PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,
155: & gxm,PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,ierr)
156: xs = xs + 1
157: gxs = gxs + 1
158: xe = xs + xm - 1
159: gxe = gxs + gxm - 1
160: end subroutine
162: subroutine FormIFunctionLocal(mx,xs,xe,gxs,gxe,x,xdot,f, &
163: & a,k,s,ierr)
164: implicit none
165: PetscInt mx,xs,xe,gxs,gxe
166: PetscScalar x(2,xs:xe)
167: PetscScalar xdot(2,xs:xe)
168: PetscScalar f(2,xs:xe)
169: PetscReal a(2),k(2),s(2)
170: PetscErrorCode ierr
171: PetscInt i
172: do 10 i = xs,xe
173: f(1,i) = xdot(1,i) + k(1)*x(1,i) - k(2)*x(2,i) - s(1)
174: f(2,i) = xdot(2,i) - k(1)*x(1,i) + k(2)*x(2,i) - s(2)
175: 10 continue
176: end subroutine
178: subroutine FormIFunction(ts,t,X,Xdot,F,user,ierr)
179: implicit none
180: #include <finclude/petscsys.h>
181: #include <finclude/petscvec.h>
182: #include <finclude/petscmat.h>
183: #include <finclude/petscsnes.h>
184: #include <finclude/petscts.h>
185: #include <finclude/petscdmda.h>
186: TS ts
187: PetscReal t
188: Vec X,Xdot,F
189: PetscReal user(6)
190: PetscErrorCode ierr
191: integer user_a,user_k,user_s
192: parameter (user_a = 1,user_k = 3,user_s = 5)
194: DM da
195: PetscInt mx,xs,xe,gxs,gxe
196: Vec Xloc
197: PetscOffset ixx,ixxdot,iff
198: PetscScalar xx(0:1),xxdot(0:1),ff(0:1)
200: call TSGetDM(ts,da,ierr)
201: call GetLayout(da,mx,xs,xe,gxs,gxe,ierr)
203: ! Get access to vector data
204: call VecGetArray(X,xx,ixx,ierr)
205: call VecGetArray(Xdot,xxdot,ixxdot,ierr)
206: call VecGetArray(F,ff,iff,ierr)
208: call FormIFunctionLocal(mx,xs,xe,gxs,gxe, &
209: & xx(ixx),xxdot(ixxdot),ff(iff), &
210: & user(user_a),user(user_k),user(user_s),ierr)
212: call VecRestoreArray(X,xx,ixx,ierr)
213: call VecRestoreArray(Xdot,xxdot,ixxdot,ierr)
214: call VecRestoreArray(F,ff,iff,ierr)
215: end subroutine
217: subroutine FormRHSFunctionLocal(mx,xs,xe,gxs,gxe,t,x,f, &
218: & a,k,s,ierr)
219: implicit none
220: PetscInt mx,xs,xe,gxs,gxe
221: PetscReal t
222: PetscScalar x(2,gxs:gxe),f(2,xs:xe)
223: PetscReal a(2),k(2),s(2)
224: PetscErrorCode ierr
225: PetscInt i,j
226: PetscReal hx,u0t(2)
227: PetscReal one,two,three,four,six,twelve
228: PetscReal half,third,twothird,sixth
229: PetscReal twelfth
231: one = 1.0
232: two = 2.0
233: three = 3.0
234: four = 4.0
235: six = 6.0
236: twelve = 12.0
237: hx = one / mx
238: u0t(1) = one - sin(twelve*t)**four
239: u0t(2) = 0.0
240: half = one/two
241: third = one / three
242: twothird = two / three
243: sixth = one / six
244: twelfth = one / twelve
245: do 20 i = xs,xe
246: do 10 j = 1,2
247: if (i .eq. 1) then
248: f(j,i) = a(j)/hx*(third*u0t(j) + half*x(j,i) - x(j,i+1) &
249: & + sixth*x(j,i+2))
250: else if (i .eq. 2) then
251: f(j,i) = a(j)/hx*(-twelfth*u0t(j) + twothird*x(j,i-1) &
252: & - twothird*x(j,i+1) + twelfth*x(j,i+2))
253: else if (i .eq. mx-1) then
254: f(j,i) = a(j)/hx*(-sixth*x(j,i-2) + x(j,i-1) &
255: & - half*x(j,i) -third*x(j,i+1))
256: else if (i .eq. mx) then
257: f(j,i) = a(j)/hx*(-x(j,i) + x(j,i-1))
258: else
259: f(j,i) = a(j)/hx*(-twelfth*x(j,i-2) &
260: & + twothird*x(j,i-1) &
261: & - twothird*x(j,i+1) + twelfth*x(j,i+2))
262: end if
263: 10 continue
264: 20 continue
265: end subroutine
267: subroutine FormRHSFunction(ts,t,X,F,user,ierr)
268: implicit none
269: #include <finclude/petscsys.h>
270: #include <finclude/petscvec.h>
271: #include <finclude/petscmat.h>
272: #include <finclude/petscsnes.h>
273: #include <finclude/petscts.h>
274: #include <finclude/petscdmda.h>
275: TS ts
276: PetscReal t
277: Vec X,F
278: PetscReal user(6)
279: PetscErrorCode ierr
280: integer user_a,user_k,user_s
281: parameter (user_a = 1,user_k = 3,user_s = 5)
282: DM da
283: Vec Xloc
284: PetscInt mx,xs,xe,gxs,gxe
285: PetscOffset ixx,iff
286: PetscScalar xx(0:1),ff(0:1)
288: call TSGetDM(ts,da,ierr)
289: call GetLayout(da,mx,xs,xe,gxs,gxe,ierr)
291: ! Scatter ghost points to local vector,using the 2-step process
292: ! DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
293: ! By placing code between these two statements, computations can be
294: ! done while messages are in transition.
295: call DMGetLocalVector(da,Xloc,ierr)
296: call DMGlobalToLocalBegin(da,X,INSERT_VALUES,Xloc,ierr)
297: call DMGlobalToLocalEnd(da,X,INSERT_VALUES,Xloc,ierr)
299: ! Get access to vector data
300: call VecGetArray(Xloc,xx,ixx,ierr)
301: call VecGetArray(F,ff,iff,ierr)
303: call FormRHSFunctionLocal(mx,xs,xe,gxs,gxe, &
304: & t,xx(ixx),ff(iff), &
305: & user(user_a),user(user_k),user(user_s),ierr)
307: call VecRestoreArray(Xloc,xx,ixx,ierr)
308: call VecRestoreArray(F,ff,iff,ierr)
309: call DMRestoreLocalVector(da,Xloc,ierr)
310: end subroutine
312: ! ---------------------------------------------------------------------
313: !
314: ! IJacobian - Compute IJacobian = dF/dU + shift*dF/dUdot
315: !
316: subroutine FormIJacobian(ts,t,X,Xdot,shift,J,Jpre,mstr,user,ierr)
317: implicit none
318: #include <finclude/petscsys.h>
319: #include <finclude/petscvec.h>
320: #include <finclude/petscmat.h>
321: #include <finclude/petscsnes.h>
322: #include <finclude/petscts.h>
323: #include <finclude/petscdmda.h>
324: TS ts
325: PetscReal t,shift
326: Vec X,Xdot
327: Mat J,Jpre
328: MatStructure mstr
329: PetscReal user(6)
330: PetscErrorCode ierr
331: integer user_a,user_k,user_s
332: parameter (user_a = 0,user_k = 2,user_s = 4)
334: DM da
335: PetscInt mx,xs,xe,gxs,gxe
336: PetscInt i,i1,row,col
337: PetscReal k1,k2;
338: PetscScalar val(4)
340: call TSGetDM(ts,da,ierr)
341: call GetLayout(da,mx,xs,xe,gxs,gxe,ierr)
343: i1 = 1
344: k1 = user(user_k+1)
345: k2 = user(user_k+2)
346: do 10 i = xs,xe
347: row = i-gxs
348: col = i-gxs
349: val(1) = shift + k1
350: val(2) = -k2
351: val(3) = -k1
352: val(4) = shift + k2
353: call MatSetValuesBlockedLocal(Jpre,i1,row,i1,col,val, &
354: & INSERT_VALUES,ierr)
355: 10 continue
356: call MatAssemblyBegin(Jpre,MAT_FINAL_ASSEMBLY,ierr)
357: call MatAssemblyEnd(Jpre,MAT_FINAL_ASSEMBLY,ierr)
358: if (J /= Jpre) then
359: call MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY,ierr)
360: call MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY,ierr)
361: end if
362: mstr = SAME_NONZERO_PATTERN
363: end subroutine
365: subroutine FormInitialSolutionLocal(mx,xs,xe,gxs,gxe,x,a,k,s,ierr)
366: implicit none
367: PetscInt mx,xs,xe,gxs,gxe
368: PetscScalar x(2,xs:xe)
369: PetscReal a(2),k(2),s(2)
370: PetscErrorCode ierr
372: PetscInt i
373: PetscReal one,hx,r,ik
374: one = 1.0
375: hx = one / mx
376: do 10 i=xs,xe
377: r = i*hx
378: if (k(2) .ne. 0.0) then
379: ik = one/k(2)
380: else
381: ik = one
382: end if
383: x(1,i) = one + s(2)*r
384: x(2,i) = k(1)*ik*x(1,i) + s(2)*ik
385: 10 continue
386: end subroutine
388: subroutine FormInitialSolution(ts,X,user,ierr)
389: implicit none
390: #include <finclude/petscsys.h>
391: #include <finclude/petscvec.h>
392: #include <finclude/petscmat.h>
393: #include <finclude/petscsnes.h>
394: #include <finclude/petscts.h>
395: #include <finclude/petscdmda.h>
396: TS ts
397: PetscReal t
398: Vec X
399: PetscReal user(6)
400: PetscErrorCode ierr
401: integer user_a,user_k,user_s
402: parameter (user_a = 1,user_k = 3,user_s = 5)
404: DM da
405: PetscInt mx,xs,xe,gxs,gxe
406: PetscOffset ixx
407: PetscScalar xx(0:1)
409: call TSGetDM(ts,da,ierr)
410: call GetLayout(da,mx,xs,xe,gxs,gxe,ierr)
412: ! Get access to vector data
413: call VecGetArray(X,xx,ixx,ierr)
415: call FormInitialSolutionLocal(mx,xs,xe,gxs,gxe, &
416: & xx(ixx),user(user_a),user(user_k),user(user_s),ierr)
418: call VecRestoreArray(X,xx,ixx,ierr)
419: end subroutine