xref: /petsc/src/snes/interface/snesj.c (revision bbb6d6a8ce168c53119d255f697846c361b726e4)
111320018SBarry Smith 
211320018SBarry Smith #ifndef lint
3*bbb6d6a8SBarry Smith static char vcid[] = "$Id: snesj.c,v 1.17 1995/06/29 23:54:14 bsmith Exp bsmith $";
411320018SBarry Smith #endif
511320018SBarry Smith 
6b1f0a012SBarry Smith #include "draw.h"    /*I  "draw.h"  I*/
7b1f0a012SBarry Smith #include "snes.h"    /*I  "snes.h"  I*/
811320018SBarry Smith 
911320018SBarry Smith /*@
105f3c43d9SLois Curfman McInnes    SNESDefaultComputeJacobian - Computes the Jacobian using finite
115f3c43d9SLois Curfman McInnes    differences.
1211320018SBarry Smith 
1311320018SBarry Smith    Input Parameters:
14da88328cSLois Curfman McInnes .  x1 - compute Jacobian at this point
15da88328cSLois Curfman McInnes .  ctx - application's function context, as set with SNESSetFunction()
1611320018SBarry Smith 
1711320018SBarry Smith    Output Parameters:
1823242f5aSBarry Smith .  J - Jacobian
1923242f5aSBarry Smith .  B - preconditioner, same as Jacobian
20da88328cSLois Curfman McInnes .  flag - matrix flag
2111320018SBarry Smith 
22ad960d00SLois Curfman McInnes    Options Database Key:
23ad960d00SLois Curfman McInnes $  -snes_fd
24ad960d00SLois Curfman McInnes 
255f3c43d9SLois Curfman McInnes    Notes:
265f3c43d9SLois Curfman McInnes    This routine is slow and expensive, and is not currently optimized
275f3c43d9SLois Curfman McInnes    to take advantage of sparsity in the problem.  Although
285f3c43d9SLois Curfman McInnes    SNESDefaultComputeJacobian() is not recommended for general use
295f3c43d9SLois Curfman McInnes    in large-scale applications, It can be useful in checking the
305f3c43d9SLois Curfman McInnes    correctness of a user-provided Jacobian.
3111320018SBarry Smith 
325f3c43d9SLois Curfman McInnes .keywords: SNES, finite differences, Jacobian
335f3c43d9SLois Curfman McInnes 
345f3c43d9SLois Curfman McInnes .seealso: SNESSetJacobian(), SNESTestJacobian()
3511320018SBarry Smith @*/
36eccfb7ebSLois Curfman McInnes int SNESDefaultComputeJacobian(SNES snes,Vec x1,Mat *J,Mat *B,
37eccfb7ebSLois Curfman McInnes                                MatStructure *flag,void *ctx)
3811320018SBarry Smith {
3923242f5aSBarry Smith   Vec      j1,j2,x2;
4023242f5aSBarry Smith   int      i,ierr,N,start,end,j;
41*bbb6d6a8SBarry Smith   Scalar   dx, mone = -1.0,*y,scale,*xx,wscale;
42336a5e98SBarry Smith   double   epsilon = 1.e-8,amax; /* assumes double precision */
43*bbb6d6a8SBarry Smith   MPI_Comm comm;
4423242f5aSBarry Smith 
45*bbb6d6a8SBarry Smith   PetscObjectGetComm((PetscObject)x1,&comm);
46336a5e98SBarry Smith   MatZeroEntries(*J);
4778b31e54SBarry Smith   ierr = VecDuplicate(x1,&j1); CHKERRQ(ierr);
4878b31e54SBarry Smith   ierr = VecDuplicate(x1,&j2); CHKERRQ(ierr);
4978b31e54SBarry Smith   ierr = VecDuplicate(x1,&x2); CHKERRQ(ierr);
5023242f5aSBarry Smith 
5178b31e54SBarry Smith   ierr = VecGetSize(x1,&N); CHKERRQ(ierr);
5278b31e54SBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end); CHKERRQ(ierr);
5339e2f89bSBarry Smith   VecGetArray(x1,&xx);
5478b31e54SBarry Smith   ierr = SNESComputeFunction(snes,x1,j1); CHKERRQ(ierr);
5539e2f89bSBarry Smith   for ( i=0; i<N; i++ ) {
5678b31e54SBarry Smith     ierr = VecCopy(x1,x2); CHKERRQ(ierr);
5723242f5aSBarry Smith     if ( i>= start && i<end) {
5839e2f89bSBarry Smith       dx = xx[i-start];
5919a167f6SBarry Smith #if !defined(PETSC_COMPLEX)
60336a5e98SBarry Smith       if (dx < 1.e-16 && dx >= 0.0) dx = 1.e-1;
61336a5e98SBarry Smith       else if (dx < 0.0 && dx > -1.e-16) dx = -1.e-1;
6219a167f6SBarry Smith #else
6319a167f6SBarry Smith       if (abs(dx) < 1.e-16 && real(dx) >= 0.0) dx = 1.e-1;
6419a167f6SBarry Smith       else if (real(dx) < 0.0 && abs(dx) > -1.e-16) dx = -1.e-1;
6519a167f6SBarry Smith #endif
6639e2f89bSBarry Smith       dx *= epsilon;
67*bbb6d6a8SBarry Smith       wscale = -1.0/dx;
689d00d63dSBarry Smith       VecSetValues(x2,1,&i,&dx,ADDVALUES);
6911320018SBarry Smith     }
70*bbb6d6a8SBarry Smith     else {
71*bbb6d6a8SBarry Smith       wscale = 0.0;
72*bbb6d6a8SBarry Smith     }
7378b31e54SBarry Smith     ierr = SNESComputeFunction(snes,x2,j2); CHKERRQ(ierr);
7478b31e54SBarry Smith     ierr = VecAXPY(&mone,j1,j2); CHKERRQ(ierr);
75*bbb6d6a8SBarry Smith /* communicate scale to all processors */
76*bbb6d6a8SBarry Smith #if !defined(PETSC_COMPLEX)
77*bbb6d6a8SBarry Smith     MPI_Allreduce(&wscale,&scale,1,MPI_DOUBLE,MPI_SUM,comm);
78*bbb6d6a8SBarry Smith #else
79*bbb6d6a8SBarry Smith     MPI_Allreduce(&wscale,&scale,2,MPI_DOUBLE,MPI_SUM,comm);
80*bbb6d6a8SBarry Smith #endif
8123242f5aSBarry Smith     VecScale(&scale,j2);
8223242f5aSBarry Smith     VecGetArray(j2,&y);
83336a5e98SBarry Smith     VecAMax(j2,0,&amax); amax *= 1.e-14;
8423242f5aSBarry Smith     for ( j=start; j<end; j++ ) {
8519a167f6SBarry Smith #if defined(PETSC_COMPLEX)
8619a167f6SBarry Smith       if (abs(y[j-start]) > amax) {
8719a167f6SBarry Smith #else
88336a5e98SBarry Smith       if (y[j-start] > amax || y[j-start] < -amax) {
8919a167f6SBarry Smith #endif
9078b31e54SBarry Smith         ierr = MatSetValues(*J,1,&j,1,&i,y+j-start,INSERTVALUES); CHKERRQ(ierr);
9123242f5aSBarry Smith       }
9223242f5aSBarry Smith     }
9323242f5aSBarry Smith     VecRestoreArray(j2,&y);
9423242f5aSBarry Smith   }
95ee50ffe9SBarry Smith   MatAssemblyBegin(*J,FINAL_ASSEMBLY);
9623242f5aSBarry Smith   VecDestroy(x2); VecDestroy(j1); VecDestroy(j2);
97ee50ffe9SBarry Smith   MatAssemblyEnd(*J,FINAL_ASSEMBLY);
9811320018SBarry Smith   return 0;
9911320018SBarry Smith }
10011320018SBarry Smith 
101