1 2 #ifndef lint 3 static char vcid[] = "$Id: snesj.c,v 1.24 1995/11/01 19:12:03 bsmith Exp bsmith $"; 4 #endif 5 6 #include "draw.h" /*I "draw.h" I*/ 7 #include "snes.h" /*I "snes.h" I*/ 8 9 /*@C 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,MatStructure *flag,void *ctx) 37 { 38 Vec j1,j2,x2; 39 int i,ierr,N,start,end,j; 40 Scalar dx, mone = -1.0,*y,scale,*xx,wscale; 41 double amax, epsilon = 1.e-8; /* assumes double precision */ 42 MPI_Comm comm; 43 44 PetscObjectGetComm((PetscObject)x1,&comm); 45 MatZeroEntries(*J); 46 ierr = VecDuplicate(x1,&j1); CHKERRQ(ierr); 47 ierr = VecDuplicate(x1,&j2); CHKERRQ(ierr); 48 ierr = VecDuplicate(x1,&x2); CHKERRQ(ierr); 49 PLogObjectParent(snes,j1); PLogObjectParent(snes,j2); 50 PLogObjectParent(snes,x2); 51 52 ierr = VecGetSize(x1,&N); CHKERRQ(ierr); 53 ierr = VecGetOwnershipRange(x1,&start,&end); CHKERRQ(ierr); 54 VecGetArray(x1,&xx); 55 ierr = SNESComputeFunction(snes,x1,j1); CHKERRQ(ierr); 56 for ( i=0; i<N; i++ ) { 57 ierr = VecCopy(x1,x2); CHKERRQ(ierr); 58 if ( i>= start && i<end) { 59 dx = xx[i-start]; 60 #if !defined(PETSC_COMPLEX) 61 if (dx < 1.e-16 && dx >= 0.0) dx = 1.e-1; 62 else if (dx < 0.0 && dx > -1.e-16) dx = -1.e-1; 63 #else 64 if (abs(dx) < 1.e-16 && real(dx) >= 0.0) dx = 1.e-1; 65 else if (real(dx) < 0.0 && abs(dx) < 1.e-16) dx = -1.e-1; 66 #endif 67 dx *= epsilon; 68 wscale = -1.0/dx; 69 VecSetValues(x2,1,&i,&dx,ADD_VALUES); 70 } 71 else { 72 wscale = 0.0; 73 } 74 ierr = SNESComputeFunction(snes,x2,j2); CHKERRQ(ierr); 75 ierr = VecAXPY(&mone,j1,j2); CHKERRQ(ierr); 76 /* communicate scale to all processors */ 77 #if !defined(PETSC_COMPLEX) 78 MPI_Allreduce(&wscale,&scale,1,MPI_DOUBLE,MPI_SUM,comm); 79 #else 80 MPI_Allreduce(&wscale,&scale,2,MPI_DOUBLE,MPI_SUM,comm); 81 #endif 82 scale = -scale; 83 VecScale(&scale,j2); 84 VecGetArray(j2,&y); 85 VecNorm(j2,NORM_INFINITY,&amax); amax *= 1.e-14; 86 for ( j=start; j<end; j++ ) { 87 if (PetscAbsScalar(y[j-start]) > amax) { 88 ierr = MatSetValues(*J,1,&j,1,&i,y+j-start,INSERT_VALUES); CHKERRQ(ierr); 89 } 90 } 91 VecRestoreArray(j2,&y); 92 } 93 MatAssemblyBegin(*J,FINAL_ASSEMBLY); 94 VecDestroy(x2); VecDestroy(j1); VecDestroy(j2); 95 MatAssemblyEnd(*J,FINAL_ASSEMBLY); 96 return 0; 97 } 98 99