xref: /petsc/src/snes/interface/snesj2.c (revision a8a26c1ef46eece1f692deb11f56bcfac16cda1d)
163dd3a1aSKris Buschelman #define PETSCSNES_DLL
291627157SBarry Smith 
37c4f633dSBarry Smith #include "private/snesimpl.h"    /*I  "petscsnes.h"  I*/
491627157SBarry Smith 
54a2ae208SSatish Balay #undef __FUNCT__
64a2ae208SSatish Balay #define __FUNCT__ "SNESDefaultComputeJacobianColor"
791627157SBarry Smith /*@C
82d0c0e3bSBarry Smith     SNESDefaultComputeJacobianColor - Computes the Jacobian using
9b4fc646aSLois Curfman McInnes     finite differences and coloring to exploit matrix sparsity.
1091627157SBarry Smith 
11fee21e36SBarry Smith     Collective on SNES
12fee21e36SBarry Smith 
13c7afd0dbSLois Curfman McInnes     Input Parameters:
14c7afd0dbSLois Curfman McInnes +   snes - nonlinear solver object
15c7afd0dbSLois Curfman McInnes .   x1 - location at which to evaluate Jacobian
16c7afd0dbSLois Curfman McInnes -   ctx - coloring context, where ctx must have type MatFDColoring,
17c7afd0dbSLois Curfman McInnes           as created via MatFDColoringCreate()
18c7afd0dbSLois Curfman McInnes 
19c7afd0dbSLois Curfman McInnes     Output Parameters:
20c7afd0dbSLois Curfman McInnes +   J - Jacobian matrix (not altered in this routine)
21c7afd0dbSLois Curfman McInnes .   B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
22c7afd0dbSLois Curfman McInnes -   flag - flag indicating whether the matrix sparsity structure has changed
23c7afd0dbSLois Curfman McInnes 
2436851e7fSLois Curfman McInnes     Level: intermediate
2536851e7fSLois Curfman McInnes 
26b4fc646aSLois Curfman McInnes .keywords: SNES, finite differences, Jacobian, coloring, sparse
2791627157SBarry Smith 
28b4fc646aSLois Curfman McInnes .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESDefaultComputeJacobian()
292d0c0e3bSBarry Smith           TSDefaultComputeJacobianColor(), MatFDColoringCreate(),
30cb5b572fSBarry Smith           MatFDColoringSetFunction()
31cb5b572fSBarry Smith 
3291627157SBarry Smith @*/
3395d750ceSBarry Smith 
347087cfbeSBarry Smith PetscErrorCode  SNESDefaultComputeJacobianColor(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx)
3591627157SBarry Smith {
3691627157SBarry Smith   MatFDColoring  color = (MatFDColoring) ctx;
37dfbe8321SBarry Smith   PetscErrorCode ierr;
383a7fca6bSBarry Smith   Vec            f;
394a9d489dSBarry Smith   PetscErrorCode (*ff)(void),(*fd)(void);
40dff777c9SBarry Smith 
413a40ed3dSBarry Smith   PetscFunctionBegin;
42*a8a26c1eSJed Brown   PetscValidHeaderSpecific(color,MAT_FDCOLORING_CLASSID,6);
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