Actual source code: snesj2.c
petsc-3.4.0 2013-05-13
2: #include <petsc-private/snesimpl.h> /*I "petscsnes.h" I*/
3: #include <petscdm.h> /*I "petscdm.h" I*/
7: /*@C
8: SNESComputeJacobianDefaultColor - Computes the Jacobian using
9: finite differences and coloring to exploit matrix sparsity.
11: Collective on SNES
13: Input Parameters:
14: + snes - nonlinear solver object
15: . x1 - location at which to evaluate Jacobian
16: - ctx - ignored context parameter
18: Output Parameters:
19: + J - Jacobian matrix (not altered in this routine)
20: . B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
21: - flag - flag indicating whether the matrix sparsity structure has changed
23: Level: intermediate
25: .notes: This will first try to get the coloring from the DM. If the DM type
26: has no coloring routine, then it will try to get the coloring from the matrix. This
27: requires that the matrix have nonzero entries precomputed, such as in
28: snes/examples/tutorials/ex45.c.
30: .keywords: SNES, finite differences, Jacobian, coloring, sparse
32: .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESComputeJacobianDefault()
33: MatFDColoringCreate(), MatFDColoringSetFunction()
35: @*/
37: PetscErrorCode SNESComputeJacobianDefaultColor(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx)
38: {
39: MatFDColoring color = NULL;
41: DM dm;
42: PetscErrorCode (*func)(SNES,Vec,Vec,void*);
43: Vec F;
44: void *funcctx;
45: ISColoring iscoloring;
46: PetscBool hascolor;
49: PetscObjectQuery((PetscObject)*B,"SNESMatFDColoring",(PetscObject*)&color);
50: *flag = SAME_NONZERO_PATTERN;
51: SNESGetFunction(snes,&F,&func,&funcctx);
52: if (!color) {
53: SNESGetDM(snes,&dm);
54: DMHasColoring(dm,&hascolor);
55: if (hascolor) {
56: DMCreateColoring(dm,IS_COLORING_GLOBAL,MATAIJ,&iscoloring);
57: MatFDColoringCreate(*B,iscoloring,&color);
58: ISColoringDestroy(&iscoloring);
59: MatFDColoringSetFunction(color,(PetscErrorCode (*)(void))func,funcctx);
60: MatFDColoringSetFromOptions(color);
61: } else {
62: MatGetColoring(*B,MATCOLORINGSL,&iscoloring);
63: MatFDColoringCreate(*B,iscoloring,&color);
64: ISColoringDestroy(&iscoloring);
65: MatFDColoringSetFunction(color,(PetscErrorCode (*)(void))func,(void*)funcctx);
66: MatFDColoringSetFromOptions(color);
67: }
68: PetscObjectCompose((PetscObject)*B,"SNESMatFDColoring",(PetscObject)color);
69: PetscObjectDereference((PetscObject)color);
70: }
72: /* F is only usable if there is no RHS on the SNES */
73: if (!snes->vec_rhs) {
74: MatFDColoringSetF(color,F);
75: }
76: MatFDColoringApply(*B,color,x1,flag,snes);
77: if (*J != *B) {
78: MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);
79: MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);
80: }
81: return(0);
82: }