xref: /petsc/src/snes/interface/snesj2.c (revision 3a40ed3dce77c081171d005ae1a6ff4bb9d13b6f)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: snesj2.c,v 1.11 1997/10/12 21:45:52 bsmith Exp bsmith $";
3 #endif
4 
5 #include "src/mat/matimpl.h"      /*I  "mat.h"  I*/
6 #include "src/snes/snesimpl.h"    /*I  "snes.h"  I*/
7 
8 #undef __FUNC__
9 #define __FUNC__ "SNESDefaultComputeJacobianWithColoring"
10 /*@C
11     SNESDefaultComputeJacobianWithColoring - Computes the Jacobian using
12     finite differences and coloring to exploit matrix sparsity.
13 
14     Input Parameters:
15 .   snes - nonlinear solver object
16 .   x1 - location at which to evaluate Jacobian
17 .   ctx - coloring context, where
18 $      ctx must have type MatFDColoring,
19 $      as created via MatFDColoringCreate()
20 
21     Output Parameters:
22 .   J - Jacobian matrix (not altered in this routine)
23 .   B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
24 .   flag - flag indicating whether the matrix sparsity structure has changed
25 
26    Options Database Keys:
27 $  -mat_fd_coloring_freq <freq>
28 
29 .keywords: SNES, finite differences, Jacobian, coloring, sparse
30 
31 .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESDefaultComputeJacobian()
32 @*/
33 int SNESDefaultComputeJacobianWithColoring(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx)
34 {
35   MatFDColoring color = (MatFDColoring) ctx;
36   int           ierr,freq,it;
37 
38   PetscFunctionBegin;
39   ierr = MatFDColoringGetFrequency(color,&freq);CHKERRQ(ierr);
40   ierr = SNESGetIterationNumber(snes,&it); CHKERRQ(ierr);
41 
42   if ((freq > 1) && ((it % freq) != 1)) {
43     PLogInfo(color,"SNESDefaultComputeJacobianWithColoring:Skipping Jacobian, it %d, freq %d\n",it,freq);
44     *flag = SAME_PRECONDITIONER;
45     PetscFunctionReturn(0);
46   } else {
47     PLogInfo(color,"SNESDefaultComputeJacobianWithColoring:Computing Jacobian, it %d, freq %d\n",it,freq);
48     *flag = SAME_NONZERO_PATTERN;
49   }
50 
51   ierr = MatFDColoringApply(*B,color,x1,flag,snes); CHKERRQ(ierr);
52   PetscFunctionReturn(0);
53 }
54 
55 
56 
57