Actual source code: snesj2.c
1: /*$Id: snesj2.c,v 1.31 2001/06/21 21:18:37 bsmith Exp $*/
3: #include src/mat/matimpl.h
4: #include src/snes/snesimpl.h
6: #undef __FUNCT__
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: Options Database Keys:
26: . -mat_fd_coloring_freq <freq> - Activates SNESDefaultComputeJacobianColor()
28: Level: intermediate
30: .keywords: SNES, finite differences, Jacobian, coloring, sparse
32: .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESDefaultComputeJacobian()
33: TSDefaultComputeJacobianColor(), MatFDColoringCreate(),
34: MatFDColoringSetFunction()
36: @*/
37: int SNESDefaultComputeJacobianColor(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx)
38: {
39: MatFDColoring color = (MatFDColoring) ctx;
40: int ierr,freq,it;
41: Vec f;
44: MatFDColoringGetFrequency(color,&freq);
45: SNESGetIterationNumber(snes,&it);
47: if ((freq > 1) && ((it % freq))) {
48: PetscLogInfo(color,"SNESDefaultComputeJacobianColor:Skipping Jacobian recomputation, it %d, freq %dn",it,freq);
49: *flag = SAME_PRECONDITIONER;
50: } else {
51: PetscLogInfo(color,"SNESDefaultComputeJacobianColor:Computing Jacobian, it %d, freq %dn",it,freq);
52: *flag = SAME_NONZERO_PATTERN;
53: ierr = SNESGetFunction(snes,&f,0,0);
54: ierr = MatFDColoringSetF(color,f);
55: ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x1,0,0);
56: PetscStackPush("SNES user function");
57: ierr = MatFDColoringApply(*B,color,x1,flag,snes);
58: PetscStackPop;
59: snes->nfuncs++;
60: PetscLogEventEnd(SNES_FunctionEval,snes,x1,0,0);
61: }
62: if (J != B) {
63: MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);
64: MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);
65: }
66: return(0);
67: }