xref: /petsc/src/snes/interface/snesj.c (revision 4c04a9de2e624de0fa2e8b46feb619f94d1e86ab)
1 
2 #ifndef lint
3 static char vcid[] = "$Id: snesj.c,v 1.17 1995/06/29 23:54:14 bsmith Exp bsmith $";
4 #endif
5 
6 #include "draw.h"    /*I  "draw.h"  I*/
7 #include "snes.h"    /*I  "snes.h"  I*/
8 
9 /*@
10    SNESDefaultComputeJacobian - Computes the Jacobian using finite
11    differences.
12 
13    Input Parameters:
14 .  x1 - compute Jacobian at this point
15 .  ctx - application's function context, as set with SNESSetFunction()
16 
17    Output Parameters:
18 .  J - Jacobian
19 .  B - preconditioner, same as Jacobian
20 .  flag - matrix flag
21 
22    Options Database Key:
23 $  -snes_fd
24 
25    Notes:
26    This routine is slow and expensive, and is not currently optimized
27    to take advantage of sparsity in the problem.  Although
28    SNESDefaultComputeJacobian() is not recommended for general use
29    in large-scale applications, It can be useful in checking the
30    correctness of a user-provided Jacobian.
31 
32 .keywords: SNES, finite differences, Jacobian
33 
34 .seealso: SNESSetJacobian(), SNESTestJacobian()
35 @*/
36 int SNESDefaultComputeJacobian(SNES snes,Vec x1,Mat *J,Mat *B,
37                                MatStructure *flag,void *ctx)
38 {
39   Vec      j1,j2,x2;
40   int      i,ierr,N,start,end,j;
41   Scalar   dx, mone = -1.0,*y,scale,*xx,wscale;
42   double   epsilon = 1.e-8,amax; /* assumes double precision */
43   MPI_Comm comm;
44 
45   PetscObjectGetComm((PetscObject)x1,&comm);
46   MatZeroEntries(*J);
47   ierr = VecDuplicate(x1,&j1); CHKERRQ(ierr);
48   ierr = VecDuplicate(x1,&j2); CHKERRQ(ierr);
49   ierr = VecDuplicate(x1,&x2); CHKERRQ(ierr);
50 
51   ierr = VecGetSize(x1,&N); CHKERRQ(ierr);
52   ierr = VecGetOwnershipRange(x1,&start,&end); CHKERRQ(ierr);
53   VecGetArray(x1,&xx);
54   ierr = SNESComputeFunction(snes,x1,j1); CHKERRQ(ierr);
55   for ( i=0; i<N; i++ ) {
56     ierr = VecCopy(x1,x2); CHKERRQ(ierr);
57     if ( i>= start && i<end) {
58       dx = xx[i-start];
59 #if !defined(PETSC_COMPLEX)
60       if (dx < 1.e-16 && dx >= 0.0) dx = 1.e-1;
61       else if (dx < 0.0 && dx > -1.e-16) dx = -1.e-1;
62 #else
63       if (abs(dx) < 1.e-16 && real(dx) >= 0.0) dx = 1.e-1;
64       else if (real(dx) < 0.0 && abs(dx) > -1.e-16) dx = -1.e-1;
65 #endif
66       dx *= epsilon;
67       wscale = -1.0/dx;
68       VecSetValues(x2,1,&i,&dx,ADDVALUES);
69     }
70     else {
71       wscale = 0.0;
72     }
73     ierr = SNESComputeFunction(snes,x2,j2); CHKERRQ(ierr);
74     ierr = VecAXPY(&mone,j1,j2); CHKERRQ(ierr);
75 /* communicate scale to all processors */
76 #if !defined(PETSC_COMPLEX)
77     MPI_Allreduce(&wscale,&scale,1,MPI_DOUBLE,MPI_SUM,comm);
78 #else
79     MPI_Allreduce(&wscale,&scale,2,MPI_DOUBLE,MPI_SUM,comm);
80 #endif
81     VecScale(&scale,j2);
82     VecGetArray(j2,&y);
83     VecAMax(j2,0,&amax); amax *= 1.e-14;
84     for ( j=start; j<end; j++ ) {
85 #if defined(PETSC_COMPLEX)
86       if (abs(y[j-start]) > amax) {
87 #else
88       if (y[j-start] > amax || y[j-start] < -amax) {
89 #endif
90         ierr = MatSetValues(*J,1,&j,1,&i,y+j-start,INSERTVALUES); CHKERRQ(ierr);
91       }
92     }
93     VecRestoreArray(j2,&y);
94   }
95   MatAssemblyBegin(*J,FINAL_ASSEMBLY);
96   VecDestroy(x2); VecDestroy(j1); VecDestroy(j2);
97   MatAssemblyEnd(*J,FINAL_ASSEMBLY);
98   return 0;
99 }
100 
101