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 CHKERRQ(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,8,8,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&da)); 45 CHKERRQ(DMSetFromOptions(da)); 46 CHKERRQ(DMSetUp(da)); 47 48 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 49 Extract global vectors from DMDA; 50 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 51 CHKERRQ(DMCreateGlobalVector(da,&u)); 52 CHKERRQ(VecDuplicate(u,&r)); 53 54 /* Initialize user application context */ 55 user.c = -30.0; 56 57 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 58 Create timestepping solver context 59 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 60 CHKERRQ(TSCreate(PETSC_COMM_WORLD,&ts)); 61 CHKERRQ(TSSetDM(ts,da)); 62 CHKERRQ(TSSetType(ts,TSBEULER)); 63 CHKERRQ(TSSetRHSFunction(ts,r,RHSFunction,&user)); 64 65 /* Set Jacobian */ 66 CHKERRQ(DMSetMatType(da,MATAIJ)); 67 CHKERRQ(DMCreateMatrix(da,&J)); 68 CHKERRQ(TSSetRHSJacobian(ts,J,J,RHSJacobian,NULL)); 69 70 ftime = 1.0; 71 CHKERRQ(TSSetMaxTime(ts,ftime)); 72 CHKERRQ(TSSetExactFinalTime(ts,TS_EXACTFINALTIME_STEPOVER)); 73 74 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 75 Set initial conditions 76 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 77 CHKERRQ(FormInitialSolution(da,u,&user)); 78 dt = .01; 79 CHKERRQ(TSSetTimeStep(ts,dt)); 80 81 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 82 Set runtime options 83 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 84 CHKERRQ(TSSetFromOptions(ts)); 85 86 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 87 Solve nonlinear system 88 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 89 CHKERRQ(TSSolve(ts,u)); 90 CHKERRQ(TSGetSolveTime(ts,&ftime)); 91 CHKERRQ(TSGetStepNumber(ts,&steps)); 92 93 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 94 Free work space. 95 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 96 CHKERRQ(MatDestroy(&J)); 97 CHKERRQ(VecDestroy(&u)); 98 CHKERRQ(VecDestroy(&r)); 99 CHKERRQ(TSDestroy(&ts)); 100 CHKERRQ(DMDestroy(&da)); 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 PetscInt i,j,Mx,My,xs,ys,xm,ym; 122 PetscReal two = 2.0,hx,hy,sx,sy; 123 PetscScalar u,uxx,uyy,**uarray,**f; 124 Vec localU; 125 126 PetscFunctionBeginUser; 127 CHKERRQ(TSGetDM(ts,&da)); 128 CHKERRQ(DMGetLocalVector(da,&localU)); 129 CHKERRQ(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)); 130 131 hx = 1.0/(PetscReal)(Mx-1); sx = 1.0/(hx*hx); 132 hy = 1.0/(PetscReal)(My-1); sy = 1.0/(hy*hy); 133 134 /* 135 Scatter ghost points to local vector,using the 2-step process 136 DMGlobalToLocalBegin(),DMGlobalToLocalEnd(). 137 By placing code between these two statements, computations can be 138 done while messages are in transition. 139 */ 140 CHKERRQ(DMGlobalToLocalBegin(da,U,INSERT_VALUES,localU)); 141 CHKERRQ(DMGlobalToLocalEnd(da,U,INSERT_VALUES,localU)); 142 143 /* Get pointers to vector data */ 144 CHKERRQ(DMDAVecGetArrayRead(da,localU,&uarray)); 145 CHKERRQ(DMDAVecGetArray(da,F,&f)); 146 147 /* Get local grid boundaries */ 148 CHKERRQ(DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL)); 149 150 /* Compute function over the locally owned part of the grid */ 151 for (j=ys; j<ys+ym; j++) { 152 for (i=xs; i<xs+xm; i++) { 153 if (i == 0 || j == 0 || i == Mx-1 || j == My-1) { 154 f[j][i] = uarray[j][i]; 155 continue; 156 } 157 u = uarray[j][i]; 158 uxx = (-two*u + uarray[j][i-1] + uarray[j][i+1])*sx; 159 uyy = (-two*u + uarray[j-1][i] + uarray[j+1][i])*sy; 160 f[j][i] = uxx + uyy; 161 } 162 } 163 164 /* Restore vectors */ 165 CHKERRQ(DMDAVecRestoreArrayRead(da,localU,&uarray)); 166 CHKERRQ(DMDAVecRestoreArray(da,F,&f)); 167 CHKERRQ(DMRestoreLocalVector(da,&localU)); 168 CHKERRQ(PetscLogFlops(11.0*ym*xm)); 169 PetscFunctionReturn(0); 170 } 171 172 /* --------------------------------------------------------------------- */ 173 /* 174 RHSJacobian - User-provided routine to compute the Jacobian of 175 the nonlinear right-hand-side function of the ODE. 176 177 Input Parameters: 178 ts - the TS context 179 t - current time 180 U - global input vector 181 dummy - optional user-defined context, as set by TSetRHSJacobian() 182 183 Output Parameters: 184 J - Jacobian matrix 185 Jpre - optionally different preconditioning matrix 186 str - flag indicating matrix structure 187 */ 188 PetscErrorCode RHSJacobian(TS ts,PetscReal t,Vec U,Mat J,Mat Jpre,void *ctx) 189 { 190 DM da; 191 DMDALocalInfo info; 192 PetscInt i,j; 193 PetscReal hx,hy,sx,sy; 194 195 PetscFunctionBeginUser; 196 CHKERRQ(TSGetDM(ts,&da)); 197 CHKERRQ(DMDAGetLocalInfo(da,&info)); 198 hx = 1.0/(PetscReal)(info.mx-1); sx = 1.0/(hx*hx); 199 hy = 1.0/(PetscReal)(info.my-1); sy = 1.0/(hy*hy); 200 for (j=info.ys; j<info.ys+info.ym; j++) { 201 for (i=info.xs; i<info.xs+info.xm; i++) { 202 PetscInt nc = 0; 203 MatStencil row,col[5]; 204 PetscScalar val[5]; 205 row.i = i; row.j = j; 206 if (i == 0 || j == 0 || i == info.mx-1 || j == info.my-1) { 207 col[nc].i = i; col[nc].j = j; val[nc++] = 1.0; 208 } else { 209 col[nc].i = i-1; col[nc].j = j; val[nc++] = sx; 210 col[nc].i = i+1; col[nc].j = j; val[nc++] = sx; 211 col[nc].i = i; col[nc].j = j-1; val[nc++] = sy; 212 col[nc].i = i; col[nc].j = j+1; val[nc++] = sy; 213 col[nc].i = i; col[nc].j = j; val[nc++] = -2*sx - 2*sy; 214 } 215 CHKERRQ(MatSetValuesStencil(Jpre,1,&row,nc,col,val,INSERT_VALUES)); 216 } 217 } 218 CHKERRQ(MatAssemblyBegin(Jpre,MAT_FINAL_ASSEMBLY)); 219 CHKERRQ(MatAssemblyEnd(Jpre,MAT_FINAL_ASSEMBLY)); 220 if (J != Jpre) { 221 CHKERRQ(MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY)); 222 CHKERRQ(MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY)); 223 } 224 PetscFunctionReturn(0); 225 } 226 227 /* ------------------------------------------------------------------- */ 228 PetscErrorCode FormInitialSolution(DM da,Vec U,void* ptr) 229 { 230 AppCtx *user=(AppCtx*)ptr; 231 PetscReal c=user->c; 232 PetscInt i,j,xs,ys,xm,ym,Mx,My; 233 PetscScalar **u; 234 PetscReal hx,hy,x,y,r; 235 236 PetscFunctionBeginUser; 237 CHKERRQ(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)); 238 239 hx = 1.0/(PetscReal)(Mx-1); 240 hy = 1.0/(PetscReal)(My-1); 241 242 /* Get pointers to vector data */ 243 CHKERRQ(DMDAVecGetArray(da,U,&u)); 244 245 /* Get local grid boundaries */ 246 CHKERRQ(DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL)); 247 248 /* Compute function over the locally owned part of the grid */ 249 for (j=ys; j<ys+ym; j++) { 250 y = j*hy; 251 for (i=xs; i<xs+xm; i++) { 252 x = i*hx; 253 r = PetscSqrtReal((x-.5)*(x-.5) + (y-.5)*(y-.5)); 254 if (r < .125) u[j][i] = PetscExpReal(c*r*r*r); 255 else u[j][i] = 0.0; 256 } 257 } 258 259 /* Restore vectors */ 260 CHKERRQ(DMDAVecRestoreArray(da,U,&u)); 261 PetscFunctionReturn(0); 262 } 263 264 /*TEST 265 266 test: 267 args: -ts_max_steps 5 -ts_monitor 268 269 test: 270 suffix: 2 271 args: -ts_max_steps 5 -ts_monitor 272 273 test: 274 suffix: 3 275 args: -ts_max_steps 5 -snes_fd_color -ts_monitor 276 277 TEST*/ 278