Actual source code: snestest.c

petsc-3.4.5 2014-06-29
  2: #include <petsc-private/snesimpl.h>

  4: typedef struct {
  5:   PetscBool complete_print;
  6: } SNES_Test;


 11: PetscErrorCode SNESSolve_Test(SNES snes)
 12: {
 13:   Mat            A = snes->jacobian,B;
 14:   Vec            x = snes->vec_sol,f = snes->vec_func,f1 = snes->vec_sol_update;
 16:   PetscInt       i;
 17:   MatStructure   flg;
 18:   PetscReal      nrm,gnorm;
 19:   SNES_Test      *neP = (SNES_Test*)snes->data;
 20:   PetscErrorCode (*objective)(SNES,Vec,PetscReal*,void*);
 21:   void           *ctx;
 22:   PetscReal      fnorm,f1norm,dnorm;

 25:   if (A != snes->jacobian_pre) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot test with alternative preconditioner");

 27:   PetscPrintf(PetscObjectComm((PetscObject)snes),"Testing hand-coded Jacobian, if the ratio is\n");
 28:   PetscPrintf(PetscObjectComm((PetscObject)snes),"O(1.e-8), the hand-coded Jacobian is probably correct.\n");
 29:   if (!neP->complete_print) {
 30:     PetscPrintf(PetscObjectComm((PetscObject)snes),"Run with -snes_test_display to show difference\n");
 31:     PetscPrintf(PetscObjectComm((PetscObject)snes),"of hand-coded and finite difference Jacobian.\n");
 32:   }

 34:   for (i=0; i<3; i++) {
 35:     void                     *functx;
 36:     static const char *const loc[] = {"user-defined state","constant state -1.0","constant state 1.0"};

 38:     if (i == 1) {
 39:       VecSet(x,-1.0);
 40:     } else if (i == 2) {
 41:       VecSet(x,1.0);
 42:     }

 44:     /* evaluate the function at this point because SNESComputeJacobianDefaultColor() assumes that the function has been evaluated and put into snes->vec_func */
 45:     SNESComputeFunction(snes,x,f);
 46:     if (snes->domainerror) {
 47:       PetscPrintf(PetscObjectComm((PetscObject)snes),"Domain error at %s\n",loc[i]);
 48:       snes->domainerror = PETSC_FALSE;
 49:       continue;
 50:     }

 52:     /* compute both versions of Jacobian */
 53:     SNESComputeJacobian(snes,x,&A,&A,&flg);
 54:     if (!i) {
 55:       PetscInt m,n,M,N;
 56:       MatCreate(PetscObjectComm((PetscObject)A),&B);
 57:       MatGetSize(A,&M,&N);
 58:       MatGetLocalSize(A,&m,&n);
 59:       MatSetSizes(B,m,n,M,N);
 60:       MatSetType(B,((PetscObject)A)->type_name);
 61:       MatSetUp(B);
 62:     }
 63:     SNESGetFunction(snes,NULL,NULL,&functx);
 64:     SNESComputeJacobianDefault(snes,x,&B,&B,&flg,functx);
 65:     if (neP->complete_print) {
 66:       MPI_Comm    comm;
 67:       PetscViewer viewer;
 68:       PetscPrintf(PetscObjectComm((PetscObject)snes),"Finite difference Jacobian (%s)\n",loc[i]);
 69:       PetscObjectGetComm((PetscObject)B,&comm);
 70:       PetscViewerASCIIGetStdout(comm,&viewer);
 71:       MatView(B,viewer);
 72:     }
 73:     /* compare */
 74:     MatAYPX(B,-1.0,A,DIFFERENT_NONZERO_PATTERN);
 75:     MatNorm(B,NORM_FROBENIUS,&nrm);
 76:     MatNorm(A,NORM_FROBENIUS,&gnorm);
 77:     if (neP->complete_print) {
 78:       MPI_Comm    comm;
 79:       PetscViewer viewer;
 80:       PetscPrintf(PetscObjectComm((PetscObject)snes),"Hand-coded Jacobian (%s)\n",loc[i]);
 81:       PetscObjectGetComm((PetscObject)B,&comm);
 82:       PetscViewerASCIIGetStdout(comm,&viewer);
 83:       MatView(A,viewer);
 84:       PetscPrintf(PetscObjectComm((PetscObject)snes),"Hand-coded minus finite difference Jacobian (%s)\n",loc[i]);
 85:       MatView(B,viewer);
 86:     }
 87:     if (!gnorm) gnorm = 1; /* just in case */
 88:     PetscPrintf(PetscObjectComm((PetscObject)snes),"Norm of matrix ratio %g difference %g (%s)\n",(double)(nrm/gnorm),(double)nrm,loc[i]);

 90:     SNESGetObjective(snes,&objective,&ctx);
 91:     if (objective) {
 92:       SNESComputeFunction(snes,x,f);
 93:       VecNorm(f,NORM_2,&fnorm);
 94:       if (neP->complete_print) {
 95:         PetscViewer viewer;
 96:         PetscPrintf(PetscObjectComm((PetscObject)snes),"Hand-coded Function (%s)\n",loc[i]);
 97:         PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes),&viewer);
 98:         VecView(f,viewer);
 99:       }
100:       SNESObjectiveComputeFunctionDefaultFD(snes,x,f1,NULL);
101:       VecNorm(f1,NORM_2,&f1norm);
102:       if (neP->complete_print) {
103:         PetscViewer viewer;
104:         PetscPrintf(PetscObjectComm((PetscObject)snes),"Finite-Difference Function (%s)\n",loc[i]);
105:         PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes),&viewer);
106:         VecView(f1,viewer);
107:       }
108:       /* compare the two */
109:       VecAXPY(f,-1.0,f1);
110:       VecNorm(f,NORM_2,&dnorm);
111:       if (!fnorm) fnorm = 1.;
112:       PetscPrintf(PetscObjectComm((PetscObject)snes),"Norm of function ratio %g difference %g (%s)\n",dnorm/fnorm,dnorm,loc[i]);
113:       if (neP->complete_print) {
114:         PetscViewer viewer;
115:         PetscPrintf(PetscObjectComm((PetscObject)snes),"Difference (%s)\n",loc[i]);
116:         PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes),&viewer);
117:         VecView(f,viewer);
118:       }
119:     }

121:   }
122:   MatDestroy(&B);
123:   /*
124:    Abort after the first iteration due to the jacobian not being valid.
125:   */

127:   SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"SNESTest aborts after Jacobian test");
128:   return(0);
129: }


132: /* ------------------------------------------------------------ */
135: PetscErrorCode SNESDestroy_Test(SNES snes)
136: {

140:   PetscFree(snes->data);
141:   return(0);
142: }

146: static PetscErrorCode SNESSetFromOptions_Test(SNES snes)
147: {
148:   SNES_Test      *ls = (SNES_Test*)snes->data;

152:   PetscOptionsHead("Hand-coded Jacobian tester options");
153:   PetscOptionsBool("-snes_test_display","Display difference between hand-coded and finite difference Jacobians","None",ls->complete_print,&ls->complete_print,NULL);
154:   PetscOptionsTail();
155:   return(0);
156: }

160: PetscErrorCode SNESSetUp_Test(SNES snes)
161: {

165:   SNESSetUpMatrices(snes);
166:   return(0);
167: }

169: /* ------------------------------------------------------------ */
170: /*MC
171:       SNESTEST - Test hand-coded Jacobian against finite difference Jacobian

173:    Options Database:
174: .    -snes_test_display  Display difference between approximate and hand-coded Jacobian

176:    Level: intermediate

178: .seealso:  SNESCreate(), SNES, SNESSetType(), SNESNEWTONLS, SNESNEWTONTR

180: M*/
183: PETSC_EXTERN PetscErrorCode SNESCreate_Test(SNES snes)
184: {
185:   SNES_Test      *neP;

189:   snes->ops->solve          = SNESSolve_Test;
190:   snes->ops->destroy        = SNESDestroy_Test;
191:   snes->ops->setfromoptions = SNESSetFromOptions_Test;
192:   snes->ops->view           = 0;
193:   snes->ops->setup          = SNESSetUp_Test;
194:   snes->ops->reset          = 0;

196:   snes->usesksp = PETSC_FALSE;

198:   PetscNewLog(snes,SNES_Test,&neP);
199:   snes->data          = (void*)neP;
200:   neP->complete_print = PETSC_FALSE;
201:   return(0);
202: }

206: /*@C
207:     SNESUpdateCheckJacobian - Checks each Jacobian computed by the nonlinear solver comparing the users function with a finite difference computation.

209:    Options Database:
210: +    -snes_check_jacobian - use this every time SNESSolve() is called
211: -    -snes_check_jacobian_view -  Display difference between approximate and hand-coded Jacobian

213:    Level: intermediate

215: .seealso:  SNESCreate(), SNES, SNESSetType(), SNESNEWTONLS, SNESNEWTONTR, SNESSolve()

217: @*/
218: PetscErrorCode SNESUpdateCheckJacobian(SNES snes,PetscInt it)
219: {
220:   Mat            A = snes->jacobian,B;
221:   Vec            x = snes->vec_sol,f = snes->vec_func,f1 = snes->vec_sol_update;
223:   MatStructure   flg;
224:   PetscReal      nrm,gnorm;
225:   PetscErrorCode (*objective)(SNES,Vec,PetscReal*,void*);
226:   void           *ctx;
227:   PetscReal      fnorm,f1norm,dnorm;
228:   PetscInt       m,n,M,N;
229:   PetscBool      complete_print = PETSC_FALSE;
230:   void           *functx;
231:   PetscViewer    viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes));

234:   PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_check_jacobian_view",&complete_print);
235:   if (A != snes->jacobian_pre) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot check Jacobian with alternative preconditioner");

237:   PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);
238:   PetscViewerASCIIPrintf(viewer,"      Testing hand-coded Jacobian, if the ratio is O(1.e-8), the hand-coded Jacobian is probably correct.\n");
239:   if (!complete_print) {
240:     PetscViewerASCIIPrintf(viewer,"      Run with -snes_check_jacobian_view [viewer][:filename][:format] to show difference of hand-coded and finite difference Jacobian.\n");
241:   }

243:   /* compute both versions of Jacobian */
244:   SNESComputeJacobian(snes,x,&A,&A,&flg);

246:   MatCreate(PetscObjectComm((PetscObject)A),&B);
247:   MatGetSize(A,&M,&N);
248:   MatGetLocalSize(A,&m,&n);
249:   MatSetSizes(B,m,n,M,N);
250:   MatSetType(B,((PetscObject)A)->type_name);
251:   MatSetUp(B);
252:   SNESGetFunction(snes,NULL,NULL,&functx);
253:   SNESComputeJacobianDefault(snes,x,&B,&B,&flg,functx);

255:   if (complete_print) {
256:     PetscViewerASCIIPrintf(viewer,"    Finite difference Jacobian\n");
257:     MatViewFromOptions(B,((PetscObject)snes)->prefix,"-snes_check_jacobian_view");
258:   }
259:   /* compare */
260:   MatAYPX(B,-1.0,A,DIFFERENT_NONZERO_PATTERN);
261:   MatNorm(B,NORM_FROBENIUS,&nrm);
262:   MatNorm(A,NORM_FROBENIUS,&gnorm);
263:   if (complete_print) {
264:     PetscViewerASCIIPrintf(viewer,"    Hand-coded Jacobian\n");
265:     MatViewFromOptions(A,((PetscObject)snes)->prefix,"-snes_check_jacobian_view");
266:     PetscViewerASCIIPrintf(viewer,"    Hand-coded minus finite difference Jacobian\n");
267:     MatViewFromOptions(B,((PetscObject)snes)->prefix,"-snes_check_jacobian_view");
268:   }
269:   if (!gnorm) gnorm = 1; /* just in case */
270:   PetscViewerASCIIPrintf(viewer,"    %g = ||J - Jfd||//J|| %g  = ||J - Jfd||\n",(double)(nrm/gnorm),(double)nrm);

272:   SNESGetObjective(snes,&objective,&ctx);
273:   if (objective) {
274:     SNESComputeFunction(snes,x,f);
275:     VecNorm(f,NORM_2,&fnorm);
276:     if (complete_print) {
277:       PetscViewerASCIIPrintf(viewer,"    Hand-coded Objective Function \n");
278:       VecView(f,viewer);
279:     }
280:     SNESObjectiveComputeFunctionDefaultFD(snes,x,f1,NULL);
281:     VecNorm(f1,NORM_2,&f1norm);
282:     if (complete_print) {
283:       PetscViewerASCIIPrintf(viewer,"    Finite-Difference Objective Function\n");
284:       VecView(f1,viewer);
285:     }
286:     /* compare the two */
287:     VecAXPY(f,-1.0,f1);
288:     VecNorm(f,NORM_2,&dnorm);
289:     if (!fnorm) fnorm = 1.;
290:     PetscViewerASCIIPrintf(viewer,"    %g = Norm of objective function ratio %g = difference\n",dnorm/fnorm,dnorm);
291:     if (complete_print) {
292:       PetscViewerASCIIPrintf(viewer,"    Difference\n");
293:       VecView(f,viewer);
294:     }
295:   }
296:   PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);

298:   MatDestroy(&B);
299:   return(0);
300: }