Actual source code: ex17.c

  1: static const char help[] = "Newton's method to solve a two-variable system, sequentially.\n"
  2:                            "The same problem is solved twice - i) fully assembled system + ii) block system\n\n";

  4: /*
  5: Include "petscsnes.h" so that we can use SNES solvers.  Note that this
  6: file automatically includes:
  7: petscsys.h - base PETSc routines
  8: petscvec.h - vectors
  9: petscmat.h - matrices
 10: petscis.h  - index sets
 11: petscpc.h  - preconditioners
 12: petscksp.h - linear solvers
 13: */
 14: #include <petscsnes.h>

 16: /*
 17: This example is block version of the test found at
 18:   ${PETSC_DIR}/src/snes/tutorials/ex1.c
 19: In this test we replace the Jacobian systems
 20:   [J]{x} = {F}
 21: where

 23: [J] = (j_00, j_01),  {x} = (x_0, x_1)^T,   {F} = (f_0, f_1)^T
 24:       (j_10, j_11)
 25: where [J] \in \mathbb^{2 \times 2}, {x},{F} \in \mathbb^{2 \times 1},

 27: with a block system in which each block is of length 1.
 28: i.e. The block system is thus

 30: [J] = ([j00], [j01]),  {x} = ({x0}, {x1})^T, {F} = ({f0}, {f1})^T
 31:       ([j10], [j11])
 32: where
 33: [j00], [j01], [j10], [j11] \in \mathbb^{1 \times 1}
 34: {x0}, {x1}, {f0}, {f1} \in \mathbb^{1 \times 1}

 36: In practice we would not bother defing blocks of size one, and would instead assemble the
 37: full system. This is just a simple test to illustrate how to manipulate the blocks and
 38: to confirm the implementation is correct.
 39: */

 41: /*
 42: User-defined routines
 43: */
 44: static PetscErrorCode FormJacobian1(SNES, Vec, Mat, Mat, void *);
 45: static PetscErrorCode FormFunction1(SNES, Vec, Vec, void *);
 46: static PetscErrorCode FormJacobian2(SNES, Vec, Mat, Mat, void *);
 47: static PetscErrorCode FormFunction2(SNES, Vec, Vec, void *);
 48: static PetscErrorCode FormJacobian1_block(SNES, Vec, Mat, Mat, void *);
 49: static PetscErrorCode FormFunction1_block(SNES, Vec, Vec, void *);
 50: static PetscErrorCode FormJacobian2_block(SNES, Vec, Mat, Mat, void *);
 51: static PetscErrorCode FormFunction2_block(SNES, Vec, Vec, void *);

 53: static PetscErrorCode assembled_system(void)
 54: {
 55:   SNES        snes; /* nonlinear solver context */
 56:   KSP         ksp;  /* linear solver context */
 57:   PC          pc;   /* preconditioner context */
 58:   Vec         x, r; /* solution, residual vectors */
 59:   Mat         J;    /* Jacobian matrix */
 60:   PetscInt    its;
 61:   PetscScalar pfive = .5, *xx;
 62:   PetscBool   flg;

 64:   PetscFunctionBeginUser;
 65:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "\n\n========================= Assembled system =========================\n\n"));

 67:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 68:   Create nonlinear solver context
 69:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 71:   PetscCall(SNESCreate(PETSC_COMM_WORLD, &snes));

 73:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 74:   Create matrix and vector data structures; set corresponding routines
 75:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 77:   /*
 78:   Create vectors for solution and nonlinear function
 79:   */
 80:   PetscCall(VecCreateSeq(PETSC_COMM_SELF, 2, &x));
 81:   PetscCall(VecDuplicate(x, &r));

 83:   /*
 84:   Create Jacobian matrix data structure
 85:   */
 86:   PetscCall(MatCreate(PETSC_COMM_SELF, &J));
 87:   PetscCall(MatSetSizes(J, PETSC_DECIDE, PETSC_DECIDE, 2, 2));
 88:   PetscCall(MatSetFromOptions(J));
 89:   PetscCall(MatSetUp(J));

 91:   PetscCall(PetscOptionsHasName(NULL, NULL, "-hard", &flg));
 92:   if (!flg) {
 93:     /*
 94:     Set function evaluation routine and vector.
 95:     */
 96:     PetscCall(SNESSetFunction(snes, r, FormFunction1, NULL));

 98:     /*
 99:     Set Jacobian matrix data structure and Jacobian evaluation routine
100:     */
101:     PetscCall(SNESSetJacobian(snes, J, J, FormJacobian1, NULL));
102:   } else {
103:     PetscCall(SNESSetFunction(snes, r, FormFunction2, NULL));
104:     PetscCall(SNESSetJacobian(snes, J, J, FormJacobian2, NULL));
105:   }

107:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
108:   Customize nonlinear solver; set runtime options
109:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

111:   /*
112:   Set linear solver defaults for this problem. By extracting the
113:   KSP, KSP, and PC contexts from the SNES context, we can then
114:   directly call any KSP, KSP, and PC routines to set various options.
115:   */
116:   PetscCall(SNESGetKSP(snes, &ksp));
117:   PetscCall(KSPGetPC(ksp, &pc));
118:   PetscCall(PCSetType(pc, PCNONE));
119:   PetscCall(KSPSetTolerances(ksp, 1.e-4, PETSC_CURRENT, PETSC_CURRENT, 20));

121:   /*
122:   Set SNES/KSP/KSP/PC runtime options, e.g.,
123:   -snes_view -snes_monitor -ksp_type <ksp> -pc_type <pc>
124:   These options will override those specified above as long as
125:   SNESSetFromOptions() is called _after_ any other customization
126:   routines.
127:   */
128:   PetscCall(SNESSetFromOptions(snes));

130:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
131:   Evaluate initial guess; then solve nonlinear system
132:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
133:   if (!flg) PetscCall(VecSet(x, pfive));
134:   else {
135:     PetscCall(VecGetArray(x, &xx));
136:     xx[0] = 2.0;
137:     xx[1] = 3.0;
138:     PetscCall(VecRestoreArray(x, &xx));
139:   }
140:   /*
141:   Note: The user should initialize the vector, x, with the initial guess
142:   for the nonlinear solver prior to calling SNESSolve().  In particular,
143:   to employ an initial guess of zero, the user should explicitly set
144:   this vector to zero by calling VecSet().
145:   */

147:   PetscCall(SNESSolve(snes, NULL, x));
148:   PetscCall(SNESGetIterationNumber(snes, &its));
149:   if (flg) {
150:     Vec f;
151:     PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
152:     PetscCall(SNESGetFunction(snes, &f, NULL, NULL));
153:     PetscCall(VecView(r, PETSC_VIEWER_STDOUT_WORLD));
154:   }
155:   PetscCall(PetscPrintf(PETSC_COMM_SELF, "number of SNES iterations = %" PetscInt_FMT "\n\n", its));

157:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
158:   Free work space.  All PETSc objects should be destroyed when they
159:   are no longer needed.
160:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

162:   PetscCall(VecDestroy(&x));
163:   PetscCall(VecDestroy(&r));
164:   PetscCall(MatDestroy(&J));
165:   PetscCall(SNESDestroy(&snes));
166:   PetscFunctionReturn(PETSC_SUCCESS);
167: }

169: /*
170: FormFunction1 - Evaluates nonlinear function, F(x).

172: Input Parameters:
173: .  snes - the SNES context
174: .  x - input vector
175: .  dummy - optional user-defined context (not used here)

177: Output Parameter:
178: .  f - function vector
179: */
180: static PetscErrorCode FormFunction1(SNES snes, Vec x, Vec f, void *dummy)
181: {
182:   const PetscScalar *xx;
183:   PetscScalar       *ff;

185:   PetscFunctionBeginUser;
186:   /*
187:   Get pointers to vector data.
188:   - For default PETSc vectors, VecGetArray() returns a pointer to
189:   the data array.  Otherwise, the routine is implementation dependent.
190:   - You MUST call VecRestoreArray() when you no longer need access to
191:   the array.
192:   */
193:   PetscCall(VecGetArrayRead(x, &xx));
194:   PetscCall(VecGetArray(f, &ff));

196:   /*
197:   Compute function
198:   */
199:   ff[0] = xx[0] * xx[0] + xx[0] * xx[1] - 3.0;
200:   ff[1] = xx[0] * xx[1] + xx[1] * xx[1] - 6.0;

202:   /*
203:   Restore vectors
204:   */
205:   PetscCall(VecRestoreArrayRead(x, &xx));
206:   PetscCall(VecRestoreArray(f, &ff));
207:   PetscFunctionReturn(PETSC_SUCCESS);
208: }

210: /*
211: FormJacobian1 - Evaluates Jacobian matrix.

213: Input Parameters:
214: .  snes - the SNES context
215: .  x - input vector
216: .  dummy - optional user-defined context (not used here)

218: Output Parameters:
219: .  jac - Jacobian matrix
220: .  B - optionally different matrix used to construct the preconditioner

222: */
223: static PetscErrorCode FormJacobian1(SNES snes, Vec x, Mat jac, Mat B, void *dummy)
224: {
225:   const PetscScalar *xx;
226:   PetscScalar        A[4];
227:   PetscInt           idx[2] = {0, 1};

229:   PetscFunctionBeginUser;
230:   /*
231:   Get pointer to vector data
232:   */
233:   PetscCall(VecGetArrayRead(x, &xx));

235:   /*
236:   Compute Jacobian entries and insert into matrix.
237:   - Since this is such a small problem, we set all entries for
238:   the matrix at once.
239:   */
240:   A[0] = 2.0 * xx[0] + xx[1];
241:   A[1] = xx[0];
242:   A[2] = xx[1];
243:   A[3] = xx[0] + 2.0 * xx[1];
244:   PetscCall(MatSetValues(jac, 2, idx, 2, idx, A, INSERT_VALUES));

246:   /*
247:   Restore vector
248:   */
249:   PetscCall(VecRestoreArrayRead(x, &xx));

251:   /*
252:   Assemble matrix
253:   */
254:   PetscCall(MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY));
255:   PetscCall(MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY));
256:   PetscFunctionReturn(PETSC_SUCCESS);
257: }

259: static PetscErrorCode FormFunction2(SNES snes, Vec x, Vec f, void *dummy)
260: {
261:   const PetscScalar *xx;
262:   PetscScalar       *ff;

264:   PetscFunctionBeginUser;
265:   /*
266:   Get pointers to vector data.
267:   - For default PETSc vectors, VecGetArray() returns a pointer to
268:   the data array.  Otherwise, the routine is implementation dependent.
269:   - You MUST call VecRestoreArray() when you no longer need access to
270:   the array.
271:   */
272:   PetscCall(VecGetArrayRead(x, &xx));
273:   PetscCall(VecGetArray(f, &ff));

275:   /*
276:   Compute function
277:   */
278:   ff[0] = PetscSinScalar(3.0 * xx[0]) + xx[0];
279:   ff[1] = xx[1];

281:   /*
282:   Restore vectors
283:   */
284:   PetscCall(VecRestoreArrayRead(x, &xx));
285:   PetscCall(VecRestoreArray(f, &ff));
286:   PetscFunctionReturn(PETSC_SUCCESS);
287: }

289: static PetscErrorCode FormJacobian2(SNES snes, Vec x, Mat jac, Mat B, void *dummy)
290: {
291:   const PetscScalar *xx;
292:   PetscScalar        A[4];
293:   PetscInt           idx[2] = {0, 1};

295:   PetscFunctionBeginUser;
296:   /*
297:   Get pointer to vector data
298:   */
299:   PetscCall(VecGetArrayRead(x, &xx));

301:   /*
302:   Compute Jacobian entries and insert into matrix.
303:   - Since this is such a small problem, we set all entries for
304:   the matrix at once.
305:   */
306:   A[0] = 3.0 * PetscCosScalar(3.0 * xx[0]) + 1.0;
307:   A[1] = 0.0;
308:   A[2] = 0.0;
309:   A[3] = 1.0;
310:   PetscCall(MatSetValues(jac, 2, idx, 2, idx, A, INSERT_VALUES));

312:   /*
313:   Restore vector
314:   */
315:   PetscCall(VecRestoreArrayRead(x, &xx));

317:   /*
318:   Assemble matrix
319:   */
320:   PetscCall(MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY));
321:   PetscCall(MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY));
322:   PetscFunctionReturn(PETSC_SUCCESS);
323: }

325: static PetscErrorCode block_system(void)
326: {
327:   SNES        snes; /* nonlinear solver context */
328:   KSP         ksp;  /* linear solver context */
329:   PC          pc;   /* preconditioner context */
330:   Vec         x, r; /* solution, residual vectors */
331:   Mat         J;    /* Jacobian matrix */
332:   PetscInt    its;
333:   PetscScalar pfive = .5;
334:   PetscBool   flg;

336:   Mat j11, j12, j21, j22;
337:   Vec x1, x2, r1, r2;
338:   Vec bv;
339:   Vec bx[2];
340:   Mat bA[2][2];

342:   PetscFunctionBeginUser;
343:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "\n\n========================= Block system =========================\n\n"));

345:   PetscCall(SNESCreate(PETSC_COMM_WORLD, &snes));

347:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
348:   Create matrix and vector data structures; set corresponding routines
349:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

351:   /*
352:   Create sub vectors for solution and nonlinear function
353:   */
354:   PetscCall(VecCreateSeq(PETSC_COMM_SELF, 1, &x1));
355:   PetscCall(VecDuplicate(x1, &r1));

357:   PetscCall(VecCreateSeq(PETSC_COMM_SELF, 1, &x2));
358:   PetscCall(VecDuplicate(x2, &r2));

360:   /*
361:   Create the block vectors
362:   */
363:   bx[0] = x1;
364:   bx[1] = x2;
365:   PetscCall(VecCreateNest(PETSC_COMM_WORLD, 2, NULL, bx, &x));
366:   PetscCall(VecAssemblyBegin(x));
367:   PetscCall(VecAssemblyEnd(x));
368:   PetscCall(VecDestroy(&x1));
369:   PetscCall(VecDestroy(&x2));

371:   bx[0] = r1;
372:   bx[1] = r2;
373:   PetscCall(VecCreateNest(PETSC_COMM_WORLD, 2, NULL, bx, &r));
374:   PetscCall(VecDestroy(&r1));
375:   PetscCall(VecDestroy(&r2));
376:   PetscCall(VecAssemblyBegin(r));
377:   PetscCall(VecAssemblyEnd(r));

379:   /*
380:   Create sub Jacobian matrix data structure
381:   */
382:   PetscCall(MatCreate(PETSC_COMM_WORLD, &j11));
383:   PetscCall(MatSetSizes(j11, 1, 1, 1, 1));
384:   PetscCall(MatSetType(j11, MATSEQAIJ));
385:   PetscCall(MatSetUp(j11));

387:   PetscCall(MatCreate(PETSC_COMM_WORLD, &j12));
388:   PetscCall(MatSetSizes(j12, 1, 1, 1, 1));
389:   PetscCall(MatSetType(j12, MATSEQAIJ));
390:   PetscCall(MatSetUp(j12));

392:   PetscCall(MatCreate(PETSC_COMM_WORLD, &j21));
393:   PetscCall(MatSetSizes(j21, 1, 1, 1, 1));
394:   PetscCall(MatSetType(j21, MATSEQAIJ));
395:   PetscCall(MatSetUp(j21));

397:   PetscCall(MatCreate(PETSC_COMM_WORLD, &j22));
398:   PetscCall(MatSetSizes(j22, PETSC_DECIDE, PETSC_DECIDE, 1, 1));
399:   PetscCall(MatSetType(j22, MATSEQAIJ));
400:   PetscCall(MatSetUp(j22));
401:   /*
402:   Create block Jacobian matrix data structure
403:   */
404:   bA[0][0] = j11;
405:   bA[0][1] = j12;
406:   bA[1][0] = j21;
407:   bA[1][1] = j22;

409:   PetscCall(MatCreateNest(PETSC_COMM_WORLD, 2, NULL, 2, NULL, &bA[0][0], &J));
410:   PetscCall(MatSetUp(J));
411:   PetscCall(MatNestSetVecType(J, VECNEST));
412:   PetscCall(MatDestroy(&j11));
413:   PetscCall(MatDestroy(&j12));
414:   PetscCall(MatDestroy(&j21));
415:   PetscCall(MatDestroy(&j22));

417:   PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
418:   PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));

420:   PetscCall(PetscOptionsHasName(NULL, NULL, "-hard", &flg));
421:   if (!flg) {
422:     /*
423:     Set function evaluation routine and vector.
424:     */
425:     PetscCall(SNESSetFunction(snes, r, FormFunction1_block, NULL));

427:     /*
428:     Set Jacobian matrix data structure and Jacobian evaluation routine
429:     */
430:     PetscCall(SNESSetJacobian(snes, J, J, FormJacobian1_block, NULL));
431:   } else {
432:     PetscCall(SNESSetFunction(snes, r, FormFunction2_block, NULL));
433:     PetscCall(SNESSetJacobian(snes, J, J, FormJacobian2_block, NULL));
434:   }

436:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
437:   Customize nonlinear solver; set runtime options
438:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

440:   /*
441:   Set linear solver defaults for this problem. By extracting the
442:   KSP, KSP, and PC contexts from the SNES context, we can then
443:   directly call any KSP, KSP, and PC routines to set various options.
444:   */
445:   PetscCall(SNESGetKSP(snes, &ksp));
446:   PetscCall(KSPGetPC(ksp, &pc));
447:   PetscCall(PCSetType(pc, PCNONE));
448:   PetscCall(KSPSetTolerances(ksp, 1.e-4, PETSC_CURRENT, PETSC_CURRENT, 20));

450:   /*
451:   Set SNES/KSP/KSP/PC runtime options, e.g.,
452:   -snes_view -snes_monitor -ksp_type <ksp> -pc_type <pc>
453:   These options will override those specified above as long as
454:   SNESSetFromOptions() is called _after_ any other customization
455:   routines.
456:   */
457:   PetscCall(SNESSetFromOptions(snes));

459:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
460:   Evaluate initial guess; then solve nonlinear system
461:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
462:   if (!flg) PetscCall(VecSet(x, pfive));
463:   else {
464:     Vec *vecs;
465:     PetscCall(VecNestGetSubVecs(x, NULL, &vecs));
466:     bv = vecs[0];
467:     /*    PetscCall(VecBlockGetSubVec(x, 0, &bv)); */
468:     PetscCall(VecSetValue(bv, 0, 2.0, INSERT_VALUES)); /* xx[0] = 2.0; */
469:     PetscCall(VecAssemblyBegin(bv));
470:     PetscCall(VecAssemblyEnd(bv));

472:     /*    PetscCall(VecBlockGetSubVec(x, 1, &bv)); */
473:     bv = vecs[1];
474:     PetscCall(VecSetValue(bv, 0, 3.0, INSERT_VALUES)); /* xx[1] = 3.0; */
475:     PetscCall(VecAssemblyBegin(bv));
476:     PetscCall(VecAssemblyEnd(bv));
477:   }
478:   /*
479:   Note: The user should initialize the vector, x, with the initial guess
480:   for the nonlinear solver prior to calling SNESSolve().  In particular,
481:   to employ an initial guess of zero, the user should explicitly set
482:   this vector to zero by calling VecSet().
483:   */
484:   PetscCall(SNESSolve(snes, NULL, x));
485:   PetscCall(SNESGetIterationNumber(snes, &its));
486:   if (flg) {
487:     Vec f;
488:     PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
489:     PetscCall(SNESGetFunction(snes, &f, NULL, NULL));
490:     PetscCall(VecView(r, PETSC_VIEWER_STDOUT_WORLD));
491:   }

493:   PetscCall(PetscPrintf(PETSC_COMM_SELF, "number of SNES iterations = %" PetscInt_FMT "\n\n", its));

495:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
496:   Free work space.  All PETSc objects should be destroyed when they
497:   are no longer needed.
498:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
499:   PetscCall(VecDestroy(&x));
500:   PetscCall(VecDestroy(&r));
501:   PetscCall(MatDestroy(&J));
502:   PetscCall(SNESDestroy(&snes));
503:   PetscFunctionReturn(PETSC_SUCCESS);
504: }

506: static PetscErrorCode FormFunction1_block(SNES snes, Vec x, Vec f, void *dummy)
507: {
508:   Vec        *xx, *ff, x1, x2, f1, f2;
509:   PetscScalar ff_0, ff_1;
510:   PetscScalar xx_0, xx_1;
511:   PetscInt    index, nb;

513:   PetscFunctionBeginUser;
514:   /* get blocks for function */
515:   PetscCall(VecNestGetSubVecs(f, &nb, &ff));
516:   f1 = ff[0];
517:   f2 = ff[1];

519:   /* get blocks for solution */
520:   PetscCall(VecNestGetSubVecs(x, &nb, &xx));
521:   x1 = xx[0];
522:   x2 = xx[1];

524:   /* get solution values */
525:   index = 0;
526:   PetscCall(VecGetValues(x1, 1, &index, &xx_0));
527:   PetscCall(VecGetValues(x2, 1, &index, &xx_1));

529:   /* Compute function */
530:   ff_0 = xx_0 * xx_0 + xx_0 * xx_1 - 3.0;
531:   ff_1 = xx_0 * xx_1 + xx_1 * xx_1 - 6.0;

533:   /* set function values */
534:   PetscCall(VecSetValue(f1, index, ff_0, INSERT_VALUES));

536:   PetscCall(VecSetValue(f2, index, ff_1, INSERT_VALUES));

538:   PetscCall(VecAssemblyBegin(f));
539:   PetscCall(VecAssemblyEnd(f));
540:   PetscFunctionReturn(PETSC_SUCCESS);
541: }

543: static PetscErrorCode FormJacobian1_block(SNES snes, Vec x, Mat jac, Mat B, void *dummy)
544: {
545:   Vec        *xx, x1, x2;
546:   PetscScalar xx_0, xx_1;
547:   PetscInt    index, nb;
548:   PetscScalar A_00, A_01, A_10, A_11;
549:   Mat         j11, j12, j21, j22;
550:   Mat       **mats;

552:   PetscFunctionBeginUser;
553:   /* get blocks for solution */
554:   PetscCall(VecNestGetSubVecs(x, &nb, &xx));
555:   x1 = xx[0];
556:   x2 = xx[1];

558:   /* get solution values */
559:   index = 0;
560:   PetscCall(VecGetValues(x1, 1, &index, &xx_0));
561:   PetscCall(VecGetValues(x2, 1, &index, &xx_1));

563:   /* get block matrices */
564:   PetscCall(MatNestGetSubMats(jac, NULL, NULL, &mats));
565:   j11 = mats[0][0];
566:   j12 = mats[0][1];
567:   j21 = mats[1][0];
568:   j22 = mats[1][1];

570:   /* compute jacobian entries */
571:   A_00 = 2.0 * xx_0 + xx_1;
572:   A_01 = xx_0;
573:   A_10 = xx_1;
574:   A_11 = xx_0 + 2.0 * xx_1;

576:   /* set jacobian values */
577:   PetscCall(MatSetValue(j11, 0, 0, A_00, INSERT_VALUES));
578:   PetscCall(MatSetValue(j12, 0, 0, A_01, INSERT_VALUES));
579:   PetscCall(MatSetValue(j21, 0, 0, A_10, INSERT_VALUES));
580:   PetscCall(MatSetValue(j22, 0, 0, A_11, INSERT_VALUES));

582:   /* Assemble sub matrix */
583:   PetscCall(MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY));
584:   PetscCall(MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY));
585:   PetscFunctionReturn(PETSC_SUCCESS);
586: }

588: static PetscErrorCode FormFunction2_block(SNES snes, Vec x, Vec f, void *dummy)
589: {
590:   PetscScalar       *ff;
591:   const PetscScalar *xx;

593:   PetscFunctionBeginUser;
594:   /*
595:   Get pointers to vector data.
596:   - For default PETSc vectors, VecGetArray() returns a pointer to
597:   the data array.  Otherwise, the routine is implementation dependent.
598:   - You MUST call VecRestoreArray() when you no longer need access to
599:   the array.
600:   */
601:   PetscCall(VecGetArrayRead(x, &xx));
602:   PetscCall(VecGetArray(f, &ff));

604:   /*
605:   Compute function
606:   */
607:   ff[0] = PetscSinScalar(3.0 * xx[0]) + xx[0];
608:   ff[1] = xx[1];

610:   /*
611:   Restore vectors
612:   */
613:   PetscCall(VecRestoreArrayRead(x, &xx));
614:   PetscCall(VecRestoreArray(f, &ff));
615:   PetscFunctionReturn(PETSC_SUCCESS);
616: }

618: static PetscErrorCode FormJacobian2_block(SNES snes, Vec x, Mat jac, Mat B, void *dummy)
619: {
620:   const PetscScalar *xx;
621:   PetscScalar        A[4];
622:   PetscInt           idx[2] = {0, 1};

624:   PetscFunctionBeginUser;
625:   /*
626:   Get pointer to vector data
627:   */
628:   PetscCall(VecGetArrayRead(x, &xx));

630:   /*
631:   Compute Jacobian entries and insert into matrix.
632:   - Since this is such a small problem, we set all entries for
633:   the matrix at once.
634:   */
635:   A[0] = 3.0 * PetscCosScalar(3.0 * xx[0]) + 1.0;
636:   A[1] = 0.0;
637:   A[2] = 0.0;
638:   A[3] = 1.0;
639:   PetscCall(MatSetValues(jac, 2, idx, 2, idx, A, INSERT_VALUES));

641:   /*
642:   Restore vector
643:   */
644:   PetscCall(VecRestoreArrayRead(x, &xx));

646:   /*
647:   Assemble matrix
648:   */
649:   PetscCall(MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY));
650:   PetscCall(MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY));
651:   PetscFunctionReturn(PETSC_SUCCESS);
652: }

654: int main(int argc, char **argv)
655: {
656:   PetscMPIInt size;

658:   PetscFunctionBeginUser;
659:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
660:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
661:   PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "This is a uniprocessor example only!");
662:   PetscCall(assembled_system());
663:   PetscCall(block_system());
664:   PetscCall(PetscFinalize());
665:   return 0;
666: }

668: /*TEST

670:    test:
671:       args: -snes_monitor
672:       requires: !single

674: TEST*/