xref: /petsc/src/ts/tutorials/ex13.c (revision 7d5fd1e4d9337468ad3f05b65b7facdcd2dfd2a4)
1 
2 static char help[] = "Time-dependent PDE in 2d. Simplified from ex7.c for illustrating how to use TS on a structured domain. \n";
3 /*
4    u_t = uxx + uyy
5    0 < x < 1, 0 < y < 1;
6    At t=0: u(x,y) = exp(c*r*r*r), if r=PetscSqrtReal((x-.5)*(x-.5) + (y-.5)*(y-.5)) < .125
7            u(x,y) = 0.0           if r >= .125
8 
9     mpiexec -n 2 ./ex13 -da_grid_x 40 -da_grid_y 40 -ts_max_steps 2 -snes_monitor -ksp_monitor
10     mpiexec -n 1 ./ex13 -snes_fd_color -ts_monitor_draw_solution
11     mpiexec -n 2 ./ex13 -ts_type sundials -ts_monitor
12 */
13 
14 #include <petscdm.h>
15 #include <petscdmda.h>
16 #include <petscts.h>
17 
18 /*
19    User-defined data structures and routines
20 */
21 typedef struct {
22   PetscReal c;
23 } AppCtx;
24 
25 extern PetscErrorCode RHSFunction(TS,PetscReal,Vec,Vec,void*);
26 extern PetscErrorCode RHSJacobian(TS,PetscReal,Vec,Mat,Mat,void*);
27 extern PetscErrorCode FormInitialSolution(DM,Vec,void*);
28 
29 int main(int argc,char **argv)
30 {
31   TS             ts;                   /* nonlinear solver */
32   Vec            u,r;                  /* solution, residual vector */
33   Mat            J;                    /* Jacobian matrix */
34   PetscInt       steps;                /* iterations for convergence */
35   PetscErrorCode ierr;
36   DM             da;
37   PetscReal      ftime,dt;
38   AppCtx         user;              /* user-defined work context */
39 
40   ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
41   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
42      Create distributed array (DMDA) to manage parallel grid and vectors
43   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
44   ierr = DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,8,8,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&da);CHKERRQ(ierr);
45   ierr = DMSetFromOptions(da);CHKERRQ(ierr);
46   ierr = DMSetUp(da);CHKERRQ(ierr);
47 
48   /*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49      Extract global vectors from DMDA;
50    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
51   ierr = DMCreateGlobalVector(da,&u);CHKERRQ(ierr);
52   ierr = VecDuplicate(u,&r);CHKERRQ(ierr);
53 
54   /* Initialize user application context */
55   user.c = -30.0;
56 
57   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
58      Create timestepping solver context
59      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
60   ierr = TSCreate(PETSC_COMM_WORLD,&ts);CHKERRQ(ierr);
61   ierr = TSSetDM(ts,da);CHKERRQ(ierr);
62   ierr = TSSetType(ts,TSBEULER);CHKERRQ(ierr);
63   ierr = TSSetRHSFunction(ts,r,RHSFunction,&user);CHKERRQ(ierr);
64 
65   /* Set Jacobian */
66   ierr = DMSetMatType(da,MATAIJ);CHKERRQ(ierr);
67   ierr = DMCreateMatrix(da,&J);CHKERRQ(ierr);
68   ierr = TSSetRHSJacobian(ts,J,J,RHSJacobian,NULL);CHKERRQ(ierr);
69 
70   ftime = 1.0;
71   ierr = TSSetMaxTime(ts,ftime);CHKERRQ(ierr);
72   ierr = TSSetExactFinalTime(ts,TS_EXACTFINALTIME_STEPOVER);CHKERRQ(ierr);
73 
74   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
75      Set initial conditions
76    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
77   ierr = FormInitialSolution(da,u,&user);CHKERRQ(ierr);
78   dt   = .01;
79   ierr = TSSetTimeStep(ts,dt);CHKERRQ(ierr);
80 
81   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
82      Set runtime options
83    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
84   ierr = TSSetFromOptions(ts);CHKERRQ(ierr);
85 
86   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
87      Solve nonlinear system
88      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
89   ierr = TSSolve(ts,u);CHKERRQ(ierr);
90   ierr = TSGetSolveTime(ts,&ftime);CHKERRQ(ierr);
91   ierr = TSGetStepNumber(ts,&steps);CHKERRQ(ierr);
92 
93   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
94      Free work space.
95    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
96   ierr = MatDestroy(&J);CHKERRQ(ierr);
97   ierr = VecDestroy(&u);CHKERRQ(ierr);
98   ierr = VecDestroy(&r);CHKERRQ(ierr);
99   ierr = TSDestroy(&ts);CHKERRQ(ierr);
100   ierr = DMDestroy(&da);CHKERRQ(ierr);
101 
102   ierr = PetscFinalize();
103   return ierr;
104 }
105 /* ------------------------------------------------------------------- */
106 /*
107    RHSFunction - Evaluates nonlinear function, F(u).
108 
109    Input Parameters:
110 .  ts - the TS context
111 .  U - input vector
112 .  ptr - optional user-defined context, as set by TSSetFunction()
113 
114    Output Parameter:
115 .  F - function vector
116  */
117 PetscErrorCode RHSFunction(TS ts,PetscReal ftime,Vec U,Vec F,void *ptr)
118 {
119   /* PETSC_UNUSED AppCtx *user=(AppCtx*)ptr; */
120   DM             da;
121   PetscErrorCode ierr;
122   PetscInt       i,j,Mx,My,xs,ys,xm,ym;
123   PetscReal      two = 2.0,hx,hy,sx,sy;
124   PetscScalar    u,uxx,uyy,**uarray,**f;
125   Vec            localU;
126 
127   PetscFunctionBeginUser;
128   ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
129   ierr = DMGetLocalVector(da,&localU);CHKERRQ(ierr);
130   ierr = DMDAGetInfo(da,PETSC_IGNORE,&Mx,&My,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);CHKERRQ(ierr);
131 
132   hx = 1.0/(PetscReal)(Mx-1); sx = 1.0/(hx*hx);
133   hy = 1.0/(PetscReal)(My-1); sy = 1.0/(hy*hy);
134 
135   /*
136      Scatter ghost points to local vector,using the 2-step process
137         DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
138      By placing code between these two statements, computations can be
139      done while messages are in transition.
140   */
141   ierr = DMGlobalToLocalBegin(da,U,INSERT_VALUES,localU);CHKERRQ(ierr);
142   ierr = DMGlobalToLocalEnd(da,U,INSERT_VALUES,localU);CHKERRQ(ierr);
143 
144   /* Get pointers to vector data */
145   ierr = DMDAVecGetArrayRead(da,localU,&uarray);CHKERRQ(ierr);
146   ierr = DMDAVecGetArray(da,F,&f);CHKERRQ(ierr);
147 
148   /* Get local grid boundaries */
149   ierr = DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);CHKERRQ(ierr);
150 
151   /* Compute function over the locally owned part of the grid */
152   for (j=ys; j<ys+ym; j++) {
153     for (i=xs; i<xs+xm; i++) {
154       if (i == 0 || j == 0 || i == Mx-1 || j == My-1) {
155         f[j][i] = uarray[j][i];
156         continue;
157       }
158       u       = uarray[j][i];
159       uxx     = (-two*u + uarray[j][i-1] + uarray[j][i+1])*sx;
160       uyy     = (-two*u + uarray[j-1][i] + uarray[j+1][i])*sy;
161       f[j][i] = uxx + uyy;
162     }
163   }
164 
165   /* Restore vectors */
166   ierr = DMDAVecRestoreArrayRead(da,localU,&uarray);CHKERRQ(ierr);
167   ierr = DMDAVecRestoreArray(da,F,&f);CHKERRQ(ierr);
168   ierr = DMRestoreLocalVector(da,&localU);CHKERRQ(ierr);
169   ierr = PetscLogFlops(11.0*ym*xm);CHKERRQ(ierr);
170   PetscFunctionReturn(0);
171 }
172 
173 /* --------------------------------------------------------------------- */
174 /*
175    RHSJacobian - User-provided routine to compute the Jacobian of
176    the nonlinear right-hand-side function of the ODE.
177 
178    Input Parameters:
179    ts - the TS context
180    t - current time
181    U - global input vector
182    dummy - optional user-defined context, as set by TSetRHSJacobian()
183 
184    Output Parameters:
185    J - Jacobian matrix
186    Jpre - optionally different preconditioning matrix
187    str - flag indicating matrix structure
188 */
189 PetscErrorCode RHSJacobian(TS ts,PetscReal t,Vec U,Mat J,Mat Jpre,void *ctx)
190 {
191   PetscErrorCode ierr;
192   DM             da;
193   DMDALocalInfo  info;
194   PetscInt       i,j;
195   PetscReal      hx,hy,sx,sy;
196 
197   PetscFunctionBeginUser;
198   ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
199   ierr = DMDAGetLocalInfo(da,&info);CHKERRQ(ierr);
200   hx   = 1.0/(PetscReal)(info.mx-1); sx = 1.0/(hx*hx);
201   hy   = 1.0/(PetscReal)(info.my-1); sy = 1.0/(hy*hy);
202   for (j=info.ys; j<info.ys+info.ym; j++) {
203     for (i=info.xs; i<info.xs+info.xm; i++) {
204       PetscInt    nc = 0;
205       MatStencil  row,col[5];
206       PetscScalar val[5];
207       row.i = i; row.j = j;
208       if (i == 0 || j == 0 || i == info.mx-1 || j == info.my-1) {
209         col[nc].i = i; col[nc].j = j; val[nc++] = 1.0;
210       } else {
211         col[nc].i = i-1; col[nc].j = j;   val[nc++] = sx;
212         col[nc].i = i+1; col[nc].j = j;   val[nc++] = sx;
213         col[nc].i = i;   col[nc].j = j-1; val[nc++] = sy;
214         col[nc].i = i;   col[nc].j = j+1; val[nc++] = sy;
215         col[nc].i = i;   col[nc].j = j;   val[nc++] = -2*sx - 2*sy;
216       }
217       ierr = MatSetValuesStencil(Jpre,1,&row,nc,col,val,INSERT_VALUES);CHKERRQ(ierr);
218     }
219   }
220   ierr = MatAssemblyBegin(Jpre,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
221   ierr = MatAssemblyEnd(Jpre,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
222   if (J != Jpre) {
223     ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
224     ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
225   }
226   PetscFunctionReturn(0);
227 }
228 
229 /* ------------------------------------------------------------------- */
230 PetscErrorCode FormInitialSolution(DM da,Vec U,void* ptr)
231 {
232   AppCtx         *user=(AppCtx*)ptr;
233   PetscReal      c=user->c;
234   PetscErrorCode ierr;
235   PetscInt       i,j,xs,ys,xm,ym,Mx,My;
236   PetscScalar    **u;
237   PetscReal      hx,hy,x,y,r;
238 
239   PetscFunctionBeginUser;
240   ierr = DMDAGetInfo(da,PETSC_IGNORE,&Mx,&My,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);CHKERRQ(ierr);
241 
242   hx = 1.0/(PetscReal)(Mx-1);
243   hy = 1.0/(PetscReal)(My-1);
244 
245   /* Get pointers to vector data */
246   ierr = DMDAVecGetArray(da,U,&u);CHKERRQ(ierr);
247 
248   /* Get local grid boundaries */
249   ierr = DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);CHKERRQ(ierr);
250 
251   /* Compute function over the locally owned part of the grid */
252   for (j=ys; j<ys+ym; j++) {
253     y = j*hy;
254     for (i=xs; i<xs+xm; i++) {
255       x = i*hx;
256       r = PetscSqrtReal((x-.5)*(x-.5) + (y-.5)*(y-.5));
257       if (r < .125) u[j][i] = PetscExpReal(c*r*r*r);
258       else u[j][i] = 0.0;
259     }
260   }
261 
262   /* Restore vectors */
263   ierr = DMDAVecRestoreArray(da,U,&u);CHKERRQ(ierr);
264   PetscFunctionReturn(0);
265 }
266 
267 /*TEST
268 
269     test:
270       args: -ts_max_steps 5 -ts_monitor
271 
272     test:
273       suffix: 2
274       args: -ts_max_steps 5 -ts_monitor
275 
276     test:
277       suffix: 3
278       args: -ts_max_steps 5 -snes_fd_color -ts_monitor
279 
280 TEST*/
281