/*@C SNESMonitorSetRatio - Sets SNES to use a monitor that prints the ratio of the function norm at each iteration. Collective on SNES Input Parameters: + snes - the SNES context - viewer - ASCII viewer to print output Level: intermediate .keywords: SNES, nonlinear, monitor, norm .seealso: SNESMonitorSet(), SNESMonitorSolution(), SNESMonitorDefault() @*/ PetscErrorCode SNESMonitorSetRatio(SNES snes,PetscViewer viewer) { PetscErrorCode ierr; SNESMonitorRatioContext *ctx; PetscReal *history; PetscFunctionBegin; ierr = PetscNewLog(snes,&ctx);CHKERRQ(ierr); ierr = SNESGetConvergenceHistory(snes,&history,NULL,NULL);CHKERRQ(ierr); if (!history) { ierr = PetscMalloc1(100,&ctx->history);CHKERRQ(ierr); ierr = SNESSetConvergenceHistory(snes,ctx->history,0,100,PETSC_TRUE);CHKERRQ(ierr); } ctx->viewer = viewer; ierr = SNESMonitorSet(snes,SNESMonitorRatio,ctx,SNESMonitorRatioDestroy);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C SNESMonitorRatio - Monitors progress of the SNES solvers by printing the ratio of residual norm at each iteration to the previous. Collective on SNES Input Parameters: + snes - the SNES context . its - iteration number . fgnorm - 2-norm of residual (or gradient) - dummy - context of monitor Level: intermediate .keywords: SNES, nonlinear, monitor, norm .seealso: SNESMonitorSet(), SNESMonitorSolution() @*/ PetscErrorCode SNESMonitorRatio(SNES snes,PetscInt its,PetscReal fgnorm,void *dummy) { PetscErrorCode ierr; PetscInt len; PetscReal *history; SNESMonitorRatioContext *ctx = (SNESMonitorRatioContext*)dummy; PetscFunctionBegin; ierr = SNESGetConvergenceHistory(snes,&history,PETSC_NULL,&len);CHKERRQ(ierr); ierr = PetscViewerASCIIAddTab(ctx->viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr); if (!its || !history || its > len) { ierr = PetscViewerASCIIPrintf(ctx->viewer,"%3D SNES Function norm %14.12e \n",its,(double)fgnorm);CHKERRQ(ierr); } else { PetscReal ratio = fgnorm/history[its-1]; ierr = PetscViewerASCIIPrintf(ctx->viewer,"%3D SNES Function norm %14.12e %14.12e \n",its,(double)fgnorm,(double)ratio);CHKERRQ(ierr); } ierr = PetscViewerASCIISubtractTab(ctx->viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C SNESMonitorSetRatio - Sets SNES to use a monitor that prints the ratio of the function norm at each iteration. Collective on SNES Input Parameters: + snes - the SNES context - viewer - ASCII viewer to print output Level: intermediate .keywords: SNES, nonlinear, monitor, norm .seealso: SNESMonitorSet(), SNESMonitorSolution(), SNESMonitorDefault() @*/ PetscErrorCode SNESMonitorSetRatio(SNES snes,PetscViewer viewer) { PetscErrorCode ierr; SNESMonitorRatioContext *ctx; PetscReal *history; PetscFunctionBegin; if (!viewer) { ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,"stdout",&viewer);CHKERRQ(ierr); ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr); } ierr = PetscNewLog(snes,SNESMonitorRatioContext,&ctx);CHKERRQ(ierr); ierr = SNESGetConvergenceHistory(snes,&history,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); if (!history) { ierr = PetscMalloc(100*sizeof(PetscReal),&ctx->history);CHKERRQ(ierr); ierr = SNESSetConvergenceHistory(snes,ctx->history,0,100,PETSC_TRUE);CHKERRQ(ierr); } ctx->viewer = viewer; ierr = SNESMonitorSet(snes,SNESMonitorRatio,ctx,SNESMonitorRatioDestroy);CHKERRQ(ierr); PetscFunctionReturn(0); }