xref: /petsc/src/snes/interface/snesj2.c (revision 454a90a3eb7bca6958262e5eca1eb393ad97e108)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: snesj2.c,v 1.19 1999/05/04 20:35:43 balay 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__ "SNESDefaultComputeJacobianColor"
10 /*@C
11     SNESDefaultComputeJacobianColor - Computes the Jacobian using
12     finite differences and coloring to exploit matrix sparsity.
13 
14     Collective on SNES
15 
16     Input Parameters:
17 +   snes - nonlinear solver object
18 .   x1 - location at which to evaluate Jacobian
19 -   ctx - coloring context, where ctx must have type MatFDColoring,
20           as created via MatFDColoringCreate()
21 
22     Output Parameters:
23 +   J - Jacobian matrix (not altered in this routine)
24 .   B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
25 -   flag - flag indicating whether the matrix sparsity structure has changed
26 
27     Options Database Keys:
28 .  -mat_fd_coloring_freq <freq> - Activates SNESDefaultComputeJacobianColor()
29 
30     Level: intermediate
31 
32 .keywords: SNES, finite differences, Jacobian, coloring, sparse
33 
34 .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESDefaultComputeJacobian()
35           TSDefaultComputeJacobianColor(), MatFDColoringCreate(),
36           MatFDColoringSetFunction()
37 
38 @*/
39 int SNESDefaultComputeJacobianColor(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx)
40 {
41   MatFDColoring color = (MatFDColoring) ctx;
42   int           ierr,freq,it;
43 
44   PetscFunctionBegin;
45   ierr = MatFDColoringGetFrequency(color,&freq);CHKERRQ(ierr);
46   ierr = SNESGetIterationNumber(snes,&it);CHKERRQ(ierr);
47 
48   if ((freq > 1) && ((it % freq) != 1)) {
49     PLogInfo(color,"SNESDefaultComputeJacobianColor:Skipping Jacobian, it %d, freq %d\n",it,freq);
50     *flag = SAME_PRECONDITIONER;
51     PetscFunctionReturn(0);
52   } else {
53     PLogInfo(color,"SNESDefaultComputeJacobianColor:Computing Jacobian, it %d, freq %d\n",it,freq);
54     *flag = SAME_NONZERO_PATTERN;
55   }
56 
57 
58   PLogEventBegin(SNES_FunctionEval,snes,x1,0,0);
59   PetscStackPush("SNES user function");
60   ierr = MatFDColoringApply(*B,color,x1,flag,snes);CHKERRQ(ierr);
61   PetscStackPop;
62   snes->nfuncs++;
63   PLogEventEnd(SNES_FunctionEval,snes,x1,0,0);
64   PetscFunctionReturn(0);
65 }
66 
67 
68 
69