xref: /petsc/src/snes/interface/snesj2.c (revision 8bc8193efbc389280f83b3d41dffa9e2d23e2ace)
1 
2 #include "src/mat/matimpl.h"      /*I  "petscmat.h"  I*/
3 #include "src/snes/snesimpl.h"    /*I  "petscsnes.h"  I*/
4 
5 #undef __FUNCT__
6 #define __FUNCT__ "SNESDefaultComputeJacobianColor"
7 /*@C
8     SNESDefaultComputeJacobianColor - Computes the Jacobian using
9     finite differences and coloring to exploit matrix sparsity.
10 
11     Collective on SNES
12 
13     Input Parameters:
14 +   snes - nonlinear solver object
15 .   x1 - location at which to evaluate Jacobian
16 -   ctx - coloring context, where ctx must have type MatFDColoring,
17           as created via MatFDColoringCreate()
18 
19     Output Parameters:
20 +   J - Jacobian matrix (not altered in this routine)
21 .   B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
22 -   flag - flag indicating whether the matrix sparsity structure has changed
23 
24     Options Database Keys:
25 .  -mat_fd_coloring_freq <freq> - Activates SNESDefaultComputeJacobianColor()
26 
27     Level: intermediate
28 
29 .keywords: SNES, finite differences, Jacobian, coloring, sparse
30 
31 .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESDefaultComputeJacobian()
32           TSDefaultComputeJacobianColor(), MatFDColoringCreate(),
33           MatFDColoringSetFunction()
34 
35 @*/
36 PetscErrorCode SNESDefaultComputeJacobianColor(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx)
37 {
38   MatFDColoring  color = (MatFDColoring) ctx;
39   PetscErrorCode ierr;
40   PetscInt       freq,it;
41   Vec            f;
42 
43   PetscFunctionBegin;
44   ierr = MatFDColoringGetFrequency(color,&freq);CHKERRQ(ierr);
45   ierr = SNESGetIterationNumber(snes,&it);CHKERRQ(ierr);
46 
47   if ((freq > 1) && ((it % freq))) {
48     PetscLogInfo(color,"SNESDefaultComputeJacobianColor:Skipping Jacobian recomputation, it %D, freq %D\n",it,freq);
49     *flag = SAME_PRECONDITIONER;
50   } else {
51     PetscLogInfo(color,"SNESDefaultComputeJacobianColor:Computing Jacobian, it %D, freq %D\n",it,freq);
52     *flag = SAME_NONZERO_PATTERN;
53     ierr  = SNESGetFunction(snes,&f,0,0);CHKERRQ(ierr);
54     ierr  = MatFDColoringSetF(color,f);CHKERRQ(ierr);
55     ierr  = MatFDColoringApply(*B,color,x1,flag,snes);CHKERRQ(ierr);
56   }
57   if (*J != *B) {
58     ierr = MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
59     ierr = MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
60   }
61   PetscFunctionReturn(0);
62 }
63 
64 
65 
66