xref: /petsc/src/snes/interface/snesj2.c (revision ac2a4f0d24b3b6a4ee93edbcad41f4bb9e923944)
1 /*$Id: snesj2.c,v 1.20 1999/10/01 21:22:24 bsmith Exp bsmith $*/
2 
3 #include "src/mat/matimpl.h"      /*I  "mat.h"  I*/
4 #include "src/snes/snesimpl.h"    /*I  "snes.h"  I*/
5 
6 #undef __FUNC__
7 #define __FUNC__ "SNESDefaultComputeJacobianColor"
8 /*@C
9     SNESDefaultComputeJacobianColor - Computes the Jacobian using
10     finite differences and coloring to exploit matrix sparsity.
11 
12     Collective on SNES
13 
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()
19 
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
24 
25     Options Database Keys:
26 .  -mat_fd_coloring_freq <freq> - Activates SNESDefaultComputeJacobianColor()
27 
28     Level: intermediate
29 
30 .keywords: SNES, finite differences, Jacobian, coloring, sparse
31 
32 .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESDefaultComputeJacobian()
33           TSDefaultComputeJacobianColor(), MatFDColoringCreate(),
34           MatFDColoringSetFunction()
35 
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 
42   PetscFunctionBegin;
43   ierr = MatFDColoringGetFrequency(color,&freq);CHKERRQ(ierr);
44   ierr = SNESGetIterationNumber(snes,&it);CHKERRQ(ierr);
45 
46   if ((freq > 1) && ((it % freq) != 1)) {
47     PLogInfo(color,"SNESDefaultComputeJacobianColor:Skipping Jacobian, it %d, freq %d\n",it,freq);
48     *flag = SAME_PRECONDITIONER;
49     PetscFunctionReturn(0);
50   } else {
51     PLogInfo(color,"SNESDefaultComputeJacobianColor:Computing Jacobian, it %d, freq %d\n",it,freq);
52     *flag = SAME_NONZERO_PATTERN;
53   }
54 
55 
56   PLogEventBegin(SNES_FunctionEval,snes,x1,0,0);
57   PetscStackPush("SNES user function");
58   ierr = MatFDColoringApply(*B,color,x1,flag,snes);CHKERRQ(ierr);
59   PetscStackPop;
60   snes->nfuncs++;
61   PLogEventEnd(SNES_FunctionEval,snes,x1,0,0);
62   PetscFunctionReturn(0);
63 }
64 
65 
66 
67