Actual source code: snesj2.c

  1: /*$Id: snesj2.c,v 1.31 2001/06/21 21:18:37 bsmith Exp $*/

 3:  #include src/mat/matimpl.h
 4:  #include src/snes/snesimpl.h

  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:     Options Database Keys:
 24: .  -mat_fd_coloring_freq <freq> - Activates SNESDefaultComputeJacobianColor()

 26:     Level: intermediate

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

 30: .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESDefaultComputeJacobian()
 31:           TSDefaultComputeJacobianColor(), MatFDColoringCreate(),
 32:           MatFDColoringSetFunction()

 34: @*/
 35: int SNESDefaultComputeJacobianColor(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx)
 36: {
 37:   MatFDColoring color = (MatFDColoring) ctx;
 38:   int           ierr,freq,it;
 39:   Vec           f;

 42:   MatFDColoringGetFrequency(color,&freq);
 43:   SNESGetIterationNumber(snes,&it);

 45:   if ((freq > 1) && ((it % freq))) {
 46:     PetscLogInfo(color,"SNESDefaultComputeJacobianColor:Skippingsnesj2.c and  Jacobian, it %d, freq %dn",it,freq);
 47:     *flag = SAME_PRECONDITIONER;
 48:   } else {
 49:     PetscLogInfo(color,"SNESDefaultComputeJacobianColor:Computing Jacobian, it %d, freq %dn",it,freq);
 50:     *flag = SAME_NONZERO_PATTERN;
 51:     ierr  = SNESGetFunction(snes,&f,0,0);
 52:     ierr  = MatFDColoringSetF(color,f);
 53:     ierr  = PetscLogEventBegin(SNES_FunctionEval,snes,x1,0,0);
 54:     PetscStackPush("SNES user function");
 55:     ierr  = MatFDColoringApply(*B,color,x1,flag,snes);
 56:     PetscStackPop;
 57:     snes->nfuncs++;
 58:     PetscLogEventEnd(SNES_FunctionEval,snes,x1,0,0);
 59:   }
 60:   if (J != B) {
 61:     MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);
 62:     MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);
 63:   }
 64:   return(0);
 65: }