xref: /petsc/src/snes/interface/snesj2.c (revision 7c4f633dc6bb6149cca88d301ead35a99e103cbb)
163dd3a1aSKris Buschelman #define PETSCSNES_DLL
291627157SBarry Smith 
3*7c4f633dSBarry Smith #include "private/matimpl.h"      /*I  "petscmat.h"  I*/
4*7c4f633dSBarry Smith #include "private/snesimpl.h"    /*I  "petscsnes.h"  I*/
591627157SBarry Smith 
64a2ae208SSatish Balay #undef __FUNCT__
74a2ae208SSatish Balay #define __FUNCT__ "SNESDefaultComputeJacobianColor"
891627157SBarry Smith /*@C
92d0c0e3bSBarry Smith     SNESDefaultComputeJacobianColor - Computes the Jacobian using
10b4fc646aSLois Curfman McInnes     finite differences and coloring to exploit matrix sparsity.
1191627157SBarry Smith 
12fee21e36SBarry Smith     Collective on SNES
13fee21e36SBarry Smith 
14c7afd0dbSLois Curfman McInnes     Input Parameters:
15c7afd0dbSLois Curfman McInnes +   snes - nonlinear solver object
16c7afd0dbSLois Curfman McInnes .   x1 - location at which to evaluate Jacobian
17c7afd0dbSLois Curfman McInnes -   ctx - coloring context, where ctx must have type MatFDColoring,
18c7afd0dbSLois Curfman McInnes           as created via MatFDColoringCreate()
19c7afd0dbSLois Curfman McInnes 
20c7afd0dbSLois Curfman McInnes     Output Parameters:
21c7afd0dbSLois Curfman McInnes +   J - Jacobian matrix (not altered in this routine)
22c7afd0dbSLois Curfman McInnes .   B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
23c7afd0dbSLois Curfman McInnes -   flag - flag indicating whether the matrix sparsity structure has changed
24c7afd0dbSLois Curfman McInnes 
2536851e7fSLois Curfman McInnes     Level: intermediate
2636851e7fSLois Curfman McInnes 
27b4fc646aSLois Curfman McInnes .keywords: SNES, finite differences, Jacobian, coloring, sparse
2891627157SBarry Smith 
29b4fc646aSLois Curfman McInnes .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESDefaultComputeJacobian()
302d0c0e3bSBarry Smith           TSDefaultComputeJacobianColor(), MatFDColoringCreate(),
31cb5b572fSBarry Smith           MatFDColoringSetFunction()
32cb5b572fSBarry Smith 
3391627157SBarry Smith @*/
3495d750ceSBarry Smith 
3563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultComputeJacobianColor(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx)
3691627157SBarry Smith {
3791627157SBarry Smith   MatFDColoring  color = (MatFDColoring) ctx;
38dfbe8321SBarry Smith   PetscErrorCode ierr;
393a7fca6bSBarry Smith   Vec            f;
404a9d489dSBarry Smith   PetscErrorCode (*ff)(void),(*fd)(void);
41dff777c9SBarry Smith 
423a40ed3dSBarry Smith   PetscFunctionBegin;
4392abec31SBarry Smith   *flag = SAME_NONZERO_PATTERN;
444a9d489dSBarry Smith   ierr  = SNESGetFunction(snes,&f,(PetscErrorCode (**)(SNES,Vec,Vec,void*))&ff,0);CHKERRQ(ierr);
454a9d489dSBarry Smith   ierr  = MatFDColoringGetFunction(color,&fd,PETSC_NULL);CHKERRQ(ierr);
464a9d489dSBarry Smith   if (fd == ff) { /* reuse function value computed in SNES */
473a7fca6bSBarry Smith     ierr  = MatFDColoringSetF(color,f);CHKERRQ(ierr);
484a9d489dSBarry Smith   }
49005c665bSBarry Smith   ierr  = MatFDColoringApply(*B,color,x1,flag,snes);CHKERRQ(ierr);
5032dfb669SBarry Smith   if (*J != *B) {
51194405b3SBarry Smith     ierr = MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
52194405b3SBarry Smith     ierr = MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
53194405b3SBarry Smith   }
543a40ed3dSBarry Smith   PetscFunctionReturn(0);
5591627157SBarry Smith }
5691627157SBarry Smith 
5791627157SBarry Smith 
5891627157SBarry Smith 
59