Actual source code: snesj2.c

  1: #define PETSCSNES_DLL

 3:  #include include/private/matimpl.h
 4:  #include include/private/snesimpl.h

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

 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()

 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

 25:     Options Database Keys:
 26: .  -mat_fd_coloring_freq <freq> - Activates SNESDefaultComputeJacobianColor()

 28:     Level: intermediate

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

 32: .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESDefaultComputeJacobian()
 33:           TSDefaultComputeJacobianColor(), MatFDColoringCreate(),
 34:           MatFDColoringSetFunction()

 36: @*/

 38: PetscErrorCode  SNESDefaultComputeJacobianColor(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx)
 39: {
 40:   MatFDColoring  color = (MatFDColoring) ctx;
 42:   PetscInt       freq,it;
 43:   Vec            f;
 44:   PetscErrorCode (*ff)(void),(*fd)(void);

 47:   MatFDColoringGetFrequency(color,&freq);
 48:   SNESGetIterationNumber(snes,&it);

 50:   if ((freq > 1) && ((it % freq))) {
 51:     PetscInfo2(color,"Skipping Jacobian recomputation, it %D, freq %D\n",it,freq);
 52:     *flag = SAME_PRECONDITIONER;
 53:   } else {
 54:     PetscInfo2(color,"Computing Jacobian, it %D, freq %D\n",it,freq);
 55:     *flag = SAME_NONZERO_PATTERN;
 56:     SNESGetFunction(snes,&f,(PetscErrorCode (**)(SNES,Vec,Vec,void*))&ff,0);
 57:     MatFDColoringGetFunction(color,&fd,PETSC_NULL);
 58:     if (fd == ff) { /* reuse function value computed in SNES */
 59:       MatFDColoringSetF(color,f);
 60:     }
 61:     MatFDColoringApply(*B,color,x1,flag,snes);
 62:   }
 63:   if (*J != *B) {
 64:     MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);
 65:     MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);
 66:   }
 67:   return(0);
 68: }