xref: /petsc/src/snes/interface/snesj2.c (revision d5d37b614d6ea02bb947345c08c45a4da2fd754a)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: snesj2.c,v 1.16 1998/09/25 03:16:01 bsmith Exp curfman $";
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     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 SNESDefaultComputeJacobianWithColoring()
29 
30     Level: intermediate
31 
32 .keywords: SNES, finite differences, Jacobian, coloring, sparse
33 
34 .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESDefaultComputeJacobian()
35           TSDefaultComputeJacobianWithColoring(), MatFDColoringCreate(),
36           MatFDColoringSetFunction()
37 
38 @*/
39 int SNESDefaultComputeJacobianWithColoring(SNES snes,Vec x1,Mat *J,Mat *B,
40                                            MatStructure *flag,void *ctx)
41 {
42   MatFDColoring color = (MatFDColoring) ctx;
43   int           ierr,freq,it;
44 
45   PetscFunctionBegin;
46   ierr = MatFDColoringGetFrequency(color,&freq);CHKERRQ(ierr);
47   ierr = SNESGetIterationNumber(snes,&it); CHKERRQ(ierr);
48 
49   if ((freq > 1) && ((it % freq) != 1)) {
50     PLogInfo(color,"SNESDefaultComputeJacobianWithColoring:Skipping Jacobian, it %d, freq %d\n",it,freq);
51     *flag = SAME_PRECONDITIONER;
52     PetscFunctionReturn(0);
53   } else {
54     PLogInfo(color,"SNESDefaultComputeJacobianWithColoring:Computing Jacobian, it %d, freq %d\n",it,freq);
55     *flag = SAME_NONZERO_PATTERN;
56   }
57 
58 
59   PLogEventBegin(SNES_FunctionEval,snes,x1,0,0);
60   PetscStackPush("SNES user function");
61   ierr = MatFDColoringApply(*B,color,x1,flag,snes); CHKERRQ(ierr);
62   PetscStackPop;
63   snes->nfuncs++;
64   PLogEventEnd(SNES_FunctionEval,snes,x1,0,0);
65   PetscFunctionReturn(0);
66 }
67 
68 
69 
70