Actual source code: snesj2.c
1: #define PETSCSNES_DLL
3: #include private/matimpl.h
4: #include private/snesimpl.h
8: /*@C
9: SNESDefaultComputeJacobianColor - Computes the Jacobian using
10: finite differences and coloring to exploit matrix sparsity.
11:
12: Collective on SNES
14: Input Parameters:
15: + snes - nonlinear solver object
16: . x1 - location at which to evaluate Jacobian
17: - ctx - coloring context, where ctx must have type MatFDColoring,
18: as created via MatFDColoringCreate()
20: Output Parameters:
21: + J - Jacobian matrix (not altered in this routine)
22: . B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
23: - flag - flag indicating whether the matrix sparsity structure has changed
25: Level: intermediate
27: .keywords: SNES, finite differences, Jacobian, coloring, sparse
29: .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESDefaultComputeJacobian()
30: TSDefaultComputeJacobianColor(), MatFDColoringCreate(),
31: MatFDColoringSetFunction()
33: @*/
35: PetscErrorCode SNESDefaultComputeJacobianColor(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx)
36: {
37: MatFDColoring color = (MatFDColoring) ctx;
39: Vec f;
40: PetscErrorCode (*ff)(void),(*fd)(void);
43: *flag = SAME_NONZERO_PATTERN;
44: SNESGetFunction(snes,&f,(PetscErrorCode (**)(SNES,Vec,Vec,void*))&ff,0);
45: MatFDColoringGetFunction(color,&fd,PETSC_NULL);
46: if (fd == ff) { /* reuse function value computed in SNES */
47: MatFDColoringSetF(color,f);
48: }
49: MatFDColoringApply(*B,color,x1,flag,snes);
50: if (*J != *B) {
51: MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);
52: MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);
53: }
54: return(0);
55: }