Actual source code: ex24.c

  1: static char help[] = "Test TSComputeIJacobian()\n\n";

  3: #include <petscdm.h>
  4: #include <petscdmda.h>
  5: #include <petscts.h>

  7: typedef struct {
  8:   PetscScalar u, v;
  9: } Field;

 11: typedef struct {
 12:   PetscReal D1, D2, gamma, kappa;
 13: } AppCtx;

 15: PetscErrorCode RHSJacobian(TS ts, PetscReal t, Vec U, Mat A, Mat BB, PetscCtx ctx)
 16: {
 17:   AppCtx     *appctx = (AppCtx *)ctx; /* user-defined application context */
 18:   DM          da;
 19:   PetscInt    i, j, Mx, My, xs, ys, xm, ym;
 20:   PetscReal   hx, hy, sx, sy;
 21:   PetscScalar uc, vc;
 22:   Field     **u;
 23:   Vec         localU;
 24:   MatStencil  stencil[6], rowstencil;
 25:   PetscScalar entries[6];

 27:   PetscFunctionBeginUser;
 28:   PetscCall(TSGetDM(ts, &da));
 29:   PetscCall(DMGetLocalVector(da, &localU));
 30:   PetscCall(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));

 32:   hx = 2.50 / (PetscReal)Mx;
 33:   sx = 1.0 / (hx * hx);
 34:   hy = 2.50 / (PetscReal)My;
 35:   sy = 1.0 / (hy * hy);

 37:   /*
 38:      Scatter ghost points to local vector,using the 2-step process
 39:         DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
 40:      By placing code between these two statements, computations can be
 41:      done while messages are in transition.
 42:   */
 43:   PetscCall(DMGlobalToLocalBegin(da, U, INSERT_VALUES, localU));
 44:   PetscCall(DMGlobalToLocalEnd(da, U, INSERT_VALUES, localU));

 46:   /*
 47:      Get pointers to vector data
 48:   */
 49:   PetscCall(DMDAVecGetArrayRead(da, localU, &u));

 51:   /*
 52:      Get local grid boundaries
 53:   */
 54:   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &xm, &ym, NULL));

 56:   stencil[0].k = 0;
 57:   stencil[1].k = 0;
 58:   stencil[2].k = 0;
 59:   stencil[3].k = 0;
 60:   stencil[4].k = 0;
 61:   stencil[5].k = 0;
 62:   rowstencil.k = 0;
 63:   /*
 64:      Compute function over the locally owned part of the grid
 65:   */
 66:   for (j = ys; j < ys + ym; j++) {
 67:     stencil[0].j = j - 1;
 68:     stencil[1].j = j + 1;
 69:     stencil[2].j = j;
 70:     stencil[3].j = j;
 71:     stencil[4].j = j;
 72:     stencil[5].j = j;
 73:     rowstencil.k = 0;
 74:     rowstencil.j = j;
 75:     for (i = xs; i < xs + xm; i++) {
 76:       uc = u[j][i].u;
 77:       vc = u[j][i].v;

 79:       /*      uxx       = (-2.0*uc + u[j][i-1].u + u[j][i+1].u)*sx;
 80:       uyy       = (-2.0*uc + u[j-1][i].u + u[j+1][i].u)*sy;

 82:       vxx       = (-2.0*vc + u[j][i-1].v + u[j][i+1].v)*sx;
 83:       vyy       = (-2.0*vc + u[j-1][i].v + u[j+1][i].v)*sy;
 84:        f[j][i].u = appctx->D1*(uxx + uyy) - uc*vc*vc + appctx->gamma*(1.0 - uc);*/

 86:       stencil[0].i = i;
 87:       stencil[0].c = 0;
 88:       entries[0]   = appctx->D1 * sy;
 89:       stencil[1].i = i;
 90:       stencil[1].c = 0;
 91:       entries[1]   = appctx->D1 * sy;
 92:       stencil[2].i = i - 1;
 93:       stencil[2].c = 0;
 94:       entries[2]   = appctx->D1 * sx;
 95:       stencil[3].i = i + 1;
 96:       stencil[3].c = 0;
 97:       entries[3]   = appctx->D1 * sx;
 98:       stencil[4].i = i;
 99:       stencil[4].c = 0;
100:       entries[4]   = -2.0 * appctx->D1 * (sx + sy) - vc * vc - appctx->gamma;
101:       stencil[5].i = i;
102:       stencil[5].c = 1;
103:       entries[5]   = -2.0 * uc * vc;
104:       rowstencil.i = i;
105:       rowstencil.c = 0;

107:       PetscCall(MatSetValuesStencil(A, 1, &rowstencil, 6, stencil, entries, INSERT_VALUES));
108:       stencil[0].c = 1;
109:       entries[0]   = appctx->D2 * sy;
110:       stencil[1].c = 1;
111:       entries[1]   = appctx->D2 * sy;
112:       stencil[2].c = 1;
113:       entries[2]   = appctx->D2 * sx;
114:       stencil[3].c = 1;
115:       entries[3]   = appctx->D2 * sx;
116:       stencil[4].c = 1;
117:       entries[4]   = -2.0 * appctx->D2 * (sx + sy) + 2.0 * uc * vc - appctx->gamma - appctx->kappa;
118:       stencil[5].c = 0;
119:       entries[5]   = vc * vc;
120:       rowstencil.c = 1;

122:       PetscCall(MatSetValuesStencil(A, 1, &rowstencil, 6, stencil, entries, INSERT_VALUES));
123:       /* f[j][i].v = appctx->D2*(vxx + vyy) + uc*vc*vc - (appctx->gamma + appctx->kappa)*vc; */
124:     }
125:   }

127:   /*
128:      Restore vectors
129:   */
130:   PetscCall(PetscLogFlops(19 * xm * ym));
131:   PetscCall(DMDAVecRestoreArrayRead(da, localU, &u));
132:   PetscCall(DMRestoreLocalVector(da, &localU));
133:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
134:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
135:   PetscCall(MatSetOption(A, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
136:   PetscFunctionReturn(PETSC_SUCCESS);
137: }

139: PetscErrorCode InitialConditions(DM da, Vec U)
140: {
141:   PetscInt  i, j, xs, ys, xm, ym, Mx, My;
142:   Field   **u;
143:   PetscReal hx, hy, x, y;

145:   PetscFunctionBeginUser;
146:   PetscCall(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));

148:   hx = 2.5 / (PetscReal)Mx;
149:   hy = 2.5 / (PetscReal)My;

151:   /*
152:      Get pointers to vector data
153:   */
154:   PetscCall(DMDAVecGetArray(da, U, &u));

156:   /*
157:      Get local grid boundaries
158:   */
159:   PetscCall(DMDAGetCorners(da, &xs, &ys, NULL, &xm, &ym, NULL));

161:   /*
162:      Compute function over the locally owned part of the grid
163:   */
164:   for (j = ys; j < ys + ym; j++) {
165:     y = j * hy;
166:     for (i = xs; i < xs + xm; i++) {
167:       x = i * hx;
168:       if ((1.0 <= x) && (x <= 1.5) && (1.0 <= y) && (y <= 1.5)) u[j][i].v = .25 * PetscPowReal(PetscSinReal(4.0 * PETSC_PI * x), 2.0) * PetscPowReal(PetscSinReal(4.0 * PETSC_PI * y), 2.0);
169:       else u[j][i].v = 0.0;

171:       u[j][i].u = 1.0 - 2.0 * u[j][i].v;
172:     }
173:   }

175:   /*
176:      Restore vectors
177:   */
178:   PetscCall(DMDAVecRestoreArray(da, U, &u));
179:   PetscFunctionReturn(PETSC_SUCCESS);
180: }

182: int main(int argc, char **argv)
183: {
184:   TS        ts;
185:   Vec       U, Udot;
186:   Mat       Jac, Jac2;
187:   DM        da;
188:   AppCtx    appctx;
189:   PetscReal t = 0, shift, norm;

191:   PetscFunctionBeginUser;
192:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));

194:   appctx.D1    = 8.0e-5;
195:   appctx.D2    = 4.0e-5;
196:   appctx.gamma = .024;
197:   appctx.kappa = .06;

199:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
200:      Create distributed array (DMDA) to manage parallel grid and vectors
201:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
202:   PetscCall(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_PERIODIC, DM_BOUNDARY_PERIODIC, DMDA_STENCIL_STAR, 64, 64, PETSC_DECIDE, PETSC_DECIDE, 2, 1, NULL, NULL, &da));
203:   PetscCall(DMSetFromOptions(da));
204:   PetscCall(DMSetUp(da));
205:   PetscCall(DMDASetFieldName(da, 0, "u"));
206:   PetscCall(DMDASetFieldName(da, 1, "v"));
207:   PetscCall(DMSetMatType(da, MATAIJ));
208:   PetscCall(DMCreateMatrix(da, &Jac));
209:   PetscCall(DMCreateMatrix(da, &Jac2));

211:   /*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
212:      Extract global vectors from DMDA; then duplicate for remaining
213:      vectors that are the same types
214:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
215:   PetscCall(DMCreateGlobalVector(da, &U));
216:   PetscCall(VecDuplicate(U, &Udot));

218:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
219:      Create timestepping solver context
220:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
221:   PetscCall(TSCreate(PETSC_COMM_WORLD, &ts));
222:   PetscCall(TSSetDM(ts, da));
223:   PetscCall(TSSetProblemType(ts, TS_NONLINEAR));
224:   PetscCall(TSSetRHSJacobian(ts, Jac, Jac, RHSJacobian, &appctx));

226:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
227:      Set initial conditions
228:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
229:   PetscCall(InitialConditions(da, U));
230:   PetscCall(TSSetSolution(ts, U));
231:   PetscCall(TSSetFromOptions(ts));
232:   PetscCall(TSSetUp(ts));

234:   shift = 2.;
235:   PetscCall(TSComputeIJacobian(ts, t, U, Udot, shift, Jac2, Jac2, PETSC_FALSE));
236:   shift = 1.;
237:   PetscCall(TSComputeIJacobian(ts, t, U, Udot, shift, Jac, Jac, PETSC_FALSE));
238:   shift = 2.;
239:   PetscCall(TSComputeIJacobian(ts, t, U, Udot, shift, Jac, Jac, PETSC_FALSE));
240:   PetscCall(MatAXPY(Jac, -1, Jac2, SAME_NONZERO_PATTERN));
241:   PetscCall(MatNorm(Jac, NORM_INFINITY, &norm));
242:   if (norm > 100.0 * PETSC_MACHINE_EPSILON) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Error Norm %g \n Incorrect behaviour of TSComputeIJacobian(). The two matrices should have the same results.\n", (double)norm));
243:   PetscCall(MatDestroy(&Jac));
244:   PetscCall(MatDestroy(&Jac2));
245:   PetscCall(VecDestroy(&U));
246:   PetscCall(VecDestroy(&Udot));
247:   PetscCall(TSDestroy(&ts));
248:   PetscCall(DMDestroy(&da));
249:   PetscCall(PetscFinalize());
250:   return 0;
251: }

253: /*TEST

255:   test:
256:     output_file: output/empty.out

258: TEST*/