Actual source code: snesj2.c

petsc-3.3-p7 2013-05-11
  2: #include <petsc-private/snesimpl.h>    /*I  "petscsnes.h"  I*/

  6: /*@C
  7:     SNESDefaultComputeJacobianColor - Computes the Jacobian using
  8:     finite differences and coloring to exploit matrix sparsity. 
  9:   
 10:     Collective on SNES

 12:     Input Parameters:
 13: +   snes - nonlinear solver object
 14: .   x1 - location at which to evaluate Jacobian
 15: -   ctx - coloring context, where ctx must have type MatFDColoring, 
 16:           as created via MatFDColoringCreate()

 18:     Output Parameters:
 19: +   J - Jacobian matrix (not altered in this routine)
 20: .   B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
 21: -   flag - flag indicating whether the matrix sparsity structure has changed

 23:     Level: intermediate

 25: .keywords: SNES, finite differences, Jacobian, coloring, sparse

 27: .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESDefaultComputeJacobian()
 28:           MatFDColoringCreate(), MatFDColoringSetFunction()

 30: @*/

 32: PetscErrorCode  SNESDefaultComputeJacobianColor(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx)
 33: {
 34:   MatFDColoring  color = (MatFDColoring) ctx;
 36:   Vec            f;
 37:   PetscErrorCode (*ff)(void),(*fd)(void);

 41:   *flag = SAME_NONZERO_PATTERN;
 42:   SNESGetFunction(snes,&f,(PetscErrorCode (**)(SNES,Vec,Vec,void*))&ff,0);
 43:   MatFDColoringGetFunction(color,&fd,PETSC_NULL);
 44:   if (fd == ff && !snes->vec_rhs) { /* reuse function value computed in SNES */
 45:     MatFDColoringSetF(color,f);
 46:   }
 47:   MatFDColoringApply(*B,color,x1,flag,snes);
 48:   if (*J != *B) {
 49:     MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);
 50:     MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);
 51:   }
 52:   return(0);
 53: }