PetscErrorCode KSPMonitorSNESLGResidualNorm(KSP ksp,PetscInt n,PetscReal rnorm,PetscObject *objs) { SNES snes = (SNES) objs[0]; PetscDrawLG lg = (PetscDrawLG) objs[1]; PetscErrorCode ierr; PetscReal y[2]; Vec snes_solution,work1,work2; PetscFunctionBegin; if (rnorm > 0.0) y[0] = PetscLog10Real(rnorm); else y[0] = -15.0; ierr = SNESGetSolution(snes,&snes_solution);CHKERRQ(ierr); ierr = VecDuplicate(snes_solution,&work1);CHKERRQ(ierr); ierr = VecDuplicate(snes_solution,&work2);CHKERRQ(ierr); ierr = KSPBuildSolution(ksp,work1,NULL);CHKERRQ(ierr); ierr = VecAYPX(work1,-1.0,snes_solution);CHKERRQ(ierr); ierr = SNESComputeFunction(snes,work1,work2);CHKERRQ(ierr); ierr = VecNorm(work2,NORM_2,y+1);CHKERRQ(ierr); if (y[1] > 0.0) y[1] = PetscLog10Real(y[1]); else y[1] = -15.0; ierr = VecDestroy(&work1);CHKERRQ(ierr); ierr = VecDestroy(&work2);CHKERRQ(ierr); ierr = PetscDrawLGAddPoint(lg,NULL,y);CHKERRQ(ierr); if (n < 20 || !(n % 5)) { ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@C KSPMonitorSNES - Print the residual norm of the nonlinear function at each iteration of the linear iterative solver. Collective on KSP Input Parameters: + ksp - iterative context . n - iteration number . rnorm - 2-norm (preconditioned) residual value (may be estimated). - dummy - unused monitor context Level: intermediate .keywords: KSP, default, monitor, residual .seealso: KSPMonitorSet(), KSPMonitorTrueResidualNorm(), KSPMonitorLGResidualNormCreate() @*/ PetscErrorCode KSPMonitorSNES(KSP ksp,PetscInt n,PetscReal rnorm,void *dummy) { PetscErrorCode ierr; PetscViewer viewer; SNES snes = (SNES) dummy; Vec snes_solution,work1,work2; PetscReal snorm; PetscFunctionBegin; ierr = SNESGetSolution(snes,&snes_solution);CHKERRQ(ierr); ierr = VecDuplicate(snes_solution,&work1);CHKERRQ(ierr); ierr = VecDuplicate(snes_solution,&work2);CHKERRQ(ierr); ierr = KSPBuildSolution(ksp,work1,NULL);CHKERRQ(ierr); ierr = VecAYPX(work1,-1.0,snes_solution);CHKERRQ(ierr); ierr = SNESComputeFunction(snes,work1,work2);CHKERRQ(ierr); ierr = VecNorm(work2,NORM_2,&snorm);CHKERRQ(ierr); ierr = VecDestroy(&work1);CHKERRQ(ierr); ierr = VecDestroy(&work2);CHKERRQ(ierr); ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ksp),&viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ksp)->tablevel);CHKERRQ(ierr); if (n == 0 && ((PetscObject)ksp)->prefix) { ierr = PetscViewerASCIIPrintf(viewer," Residual norms for %s solve.\n",((PetscObject)ksp)->prefix);CHKERRQ(ierr); } ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Residual norm %5.3e KSP Residual norm %5.3e \n",n,(double)snorm,(double)rnorm);CHKERRQ(ierr); ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ksp)->tablevel);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C KSPMonitorSolution - Monitors progress of the KSP solvers by calling VecView() for the approximate solution at each iteration. Collective on KSP Input Parameters: + ksp - the KSP context . its - iteration number . fgnorm - 2-norm of residual (or gradient) - dummy - a viewer Level: intermediate Notes: For some Krylov methods such as GMRES constructing the solution at each iteration is expensive, hence using this will slow the code. .keywords: KSP, nonlinear, vector, monitor, view .seealso: KSPMonitorSet(), KSPMonitorDefault(), VecView() @*/ PetscErrorCode KSPMonitorSolution(KSP ksp,PetscInt its,PetscReal fgnorm,void *dummy) { PetscErrorCode ierr; Vec x; PetscViewer viewer = (PetscViewer) dummy; PetscFunctionBegin; PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); ierr = KSPBuildSolution(ksp,NULL,&x);CHKERRQ(ierr); ierr = VecView(x,viewer);CHKERRQ(ierr); PetscFunctionReturn(0); }
/* KSPBuildResidualDefault - Default code to compute the residual. Input Parameters: . ksp - iterative context . t - pointer to temporary vector . v - pointer to user vector Output Parameter: . V - pointer to a vector containing the residual Level: advanced Developers Note: This is PETSC_EXTERN because it may be used by user written plugin KSP implementations .keywords: KSP, build, residual, default .seealso: KSPBuildSolutionDefault() */ PetscErrorCode KSPBuildResidualDefault(KSP ksp,Vec t,Vec v,Vec *V) { PetscErrorCode ierr; Mat Amat,Pmat; PetscFunctionBegin; if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} ierr = PCGetOperators(ksp->pc,&Amat,&Pmat);CHKERRQ(ierr); ierr = KSPBuildSolution(ksp,t,NULL);CHKERRQ(ierr); ierr = KSP_MatMult(ksp,Amat,t,v);CHKERRQ(ierr); ierr = VecAYPX(v,-1.0,ksp->vec_rhs);CHKERRQ(ierr); *V = v; PetscFunctionReturn(0); }
/*@C KSPMonitorSolution - Monitors progress of the KSP solvers by calling VecView() for the approximate solution at each iteration. Collective on KSP Input Parameters: + ksp - the KSP context . its - iteration number . fgnorm - 2-norm of residual (or gradient) - dummy - either a viewer or NULL Level: intermediate Notes: For some Krylov methods such as GMRES constructing the solution at each iteration is expensive, hence using this will slow the code. .keywords: KSP, nonlinear, vector, monitor, view .seealso: KSPMonitorSet(), KSPMonitorDefault(), VecView() @*/ PetscErrorCode KSPMonitorSolution(KSP ksp,PetscInt its,PetscReal fgnorm,void *dummy) { PetscErrorCode ierr; Vec x; PetscViewer viewer = (PetscViewer) dummy; PetscFunctionBegin; ierr = KSPBuildSolution(ksp,NULL,&x);CHKERRQ(ierr); if (!viewer) { MPI_Comm comm; ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr); viewer = PETSC_VIEWER_DRAW_(comm); } ierr = VecView(x,viewer);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C KSPMonitorSolution - Monitors progress of the KSP solvers by calling VecView() for the approximate solution at each iteration. Collective on KSP Input Parameters: + ksp - the KSP context . its - iteration number . fgnorm - 2-norm of residual (or gradient) - dummy - a viewer Level: intermediate Notes: For some Krylov methods such as GMRES constructing the solution at each iteration is expensive, hence using this will slow the code. .keywords: KSP, nonlinear, vector, monitor, view .seealso: KSPMonitorSet(), KSPMonitorDefault(), VecView() @*/ PetscErrorCode KSPMonitorSolution(KSP ksp,PetscInt its,PetscReal fgnorm,PetscViewerAndFormat *dummy) { PetscErrorCode ierr; Vec x; PetscViewer viewer = dummy->viewer; PetscFunctionBegin; PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); ierr = KSPBuildSolution(ksp,NULL,&x); CHKERRQ(ierr); ierr = PetscViewerPushFormat(viewer,dummy->format); CHKERRQ(ierr); ierr = VecView(x,viewer); CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer); CHKERRQ(ierr); PetscFunctionReturn(0); }
/* MyKSPMonitor - This is a user-defined routine for monitoring the KSP iterative solvers. Input Parameters: ksp - iterative context n - iteration number rnorm - 2-norm (preconditioned) residual value (may be estimated) dummy - optional user-defined monitor context (unused here) */ PetscErrorCode MyKSPMonitor(KSP ksp,PetscInt n,PetscReal rnorm,void *dummy) { Vec x; PetscErrorCode ierr; /* Build the solution vector */ ierr = KSPBuildSolution(ksp,PETSC_NULL,&x);CHKERRQ(ierr); /* Write the solution vector and residual norm to stdout. - PetscPrintf() handles output for multiprocessor jobs by printing from only one processor in the communicator. - The parallel viewer PETSC_VIEWER_STDOUT_WORLD handles data from multiple processors so that the output is not jumbled. */ ierr = PetscPrintf(PETSC_COMM_WORLD,"iteration %D solution vector:\n",n);CHKERRQ(ierr); ierr = VecView(x,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"iteration %D KSP Residual norm %14.12e \n",n,rnorm);CHKERRQ(ierr); return 0; }
/* This convergence test determines if the two norm of the solution lies outside the trust region, if so it halts. */ static PetscErrorCode SNESTR_KSPConverged_Private(KSP ksp,PetscInt n,PetscReal rnorm,KSPConvergedReason *reason,void *cctx) { SNES_TR_KSPConverged_Ctx *ctx = (SNES_TR_KSPConverged_Ctx*)cctx; SNES snes = ctx->snes; SNES_NEWTONTR *neP = (SNES_NEWTONTR*)snes->data; Vec x; PetscReal nrm; PetscErrorCode ierr; PetscFunctionBegin; ierr = KSPConvergedDefault(ksp,n,rnorm,reason,ctx->ctx);CHKERRQ(ierr); if (*reason) { ierr = PetscInfo2(snes,"default convergence test KSP iterations=%D, rnorm=%g\n",n,(double)rnorm);CHKERRQ(ierr); } /* Determine norm of solution */ ierr = KSPBuildSolution(ksp,0,&x);CHKERRQ(ierr); ierr = VecNorm(x,NORM_2,&nrm);CHKERRQ(ierr); if (nrm >= neP->delta) { ierr = PetscInfo2(snes,"Ending linear iteration early, delta=%g, length=%g\n",(double)neP->delta,(double)nrm);CHKERRQ(ierr); *reason = KSP_CONVERGED_STEP_LENGTH; } PetscFunctionReturn(0); }