1e7e93795SLois Curfman McInnes 2af0996ceSBarry Smith #include <petsc/private/snesimpl.h> /*I "petsc/private/snesimpl.h" I*/ 3636fd056SMatthew G. Knepley #include <petscdm.h> 42e7541e6SPeter Brune #include <petscblaslapack.h> 5e7e93795SLois Curfman McInnes 64a2ae208SSatish Balay #undef __FUNCT__ 7a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSolution" 83f1db9ecSBarry Smith /*@C 9a6570f20SBarry Smith SNESMonitorSolution - Monitors progress of the SNES solvers by calling 1036851e7fSLois Curfman McInnes VecView() for the approximate solution at each iteration. 113f1db9ecSBarry Smith 123f1db9ecSBarry Smith Collective on SNES 133f1db9ecSBarry Smith 143f1db9ecSBarry Smith Input Parameters: 153f1db9ecSBarry Smith + snes - the SNES context 163f1db9ecSBarry Smith . its - iteration number 174b27c08aSLois Curfman McInnes . fgnorm - 2-norm of residual 18f55353a2SBarry Smith - dummy - a viewer 193f1db9ecSBarry Smith 2036851e7fSLois Curfman McInnes Level: intermediate 213f1db9ecSBarry Smith 2236851e7fSLois Curfman McInnes .keywords: SNES, nonlinear, vector, monitor, view 233f1db9ecSBarry Smith 24a6570f20SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorDefault(), VecView() 253f1db9ecSBarry Smith @*/ 267087cfbeSBarry Smith PetscErrorCode SNESMonitorSolution(SNES snes,PetscInt its,PetscReal fgnorm,void *dummy) 273f1db9ecSBarry Smith { 28dfbe8321SBarry Smith PetscErrorCode ierr; 293f1db9ecSBarry Smith Vec x; 30b0a32e0cSBarry Smith PetscViewer viewer = (PetscViewer) dummy; 313f1db9ecSBarry Smith 323f1db9ecSBarry Smith PetscFunctionBegin; 334d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 343f1db9ecSBarry Smith ierr = SNESGetSolution(snes,&x);CHKERRQ(ierr); 353f1db9ecSBarry Smith ierr = VecView(x,viewer);CHKERRQ(ierr); 363f1db9ecSBarry Smith PetscFunctionReturn(0); 373f1db9ecSBarry Smith } 383f1db9ecSBarry Smith 394a2ae208SSatish Balay #undef __FUNCT__ 40a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorResidual" 415ed2d596SBarry Smith /*@C 42a6570f20SBarry Smith SNESMonitorResidual - Monitors progress of the SNES solvers by calling 435ed2d596SBarry Smith VecView() for the residual at each iteration. 445ed2d596SBarry Smith 455ed2d596SBarry Smith Collective on SNES 465ed2d596SBarry Smith 475ed2d596SBarry Smith Input Parameters: 485ed2d596SBarry Smith + snes - the SNES context 495ed2d596SBarry Smith . its - iteration number 504b27c08aSLois Curfman McInnes . fgnorm - 2-norm of residual 51f55353a2SBarry Smith - dummy - a viewer 525ed2d596SBarry Smith 535ed2d596SBarry Smith Level: intermediate 545ed2d596SBarry Smith 555ed2d596SBarry Smith .keywords: SNES, nonlinear, vector, monitor, view 565ed2d596SBarry Smith 57a6570f20SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorDefault(), VecView() 585ed2d596SBarry Smith @*/ 597087cfbeSBarry Smith PetscErrorCode SNESMonitorResidual(SNES snes,PetscInt its,PetscReal fgnorm,void *dummy) 605ed2d596SBarry Smith { 61dfbe8321SBarry Smith PetscErrorCode ierr; 625ed2d596SBarry Smith Vec x; 635ed2d596SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 645ed2d596SBarry Smith 655ed2d596SBarry Smith PetscFunctionBegin; 664d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 675ed2d596SBarry Smith ierr = SNESGetFunction(snes,&x,0,0);CHKERRQ(ierr); 685ed2d596SBarry Smith ierr = VecView(x,viewer);CHKERRQ(ierr); 695ed2d596SBarry Smith PetscFunctionReturn(0); 705ed2d596SBarry Smith } 715ed2d596SBarry Smith 725ed2d596SBarry Smith #undef __FUNCT__ 73a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSolutionUpdate" 74d132466eSBarry Smith /*@C 75a6570f20SBarry Smith SNESMonitorSolutionUpdate - Monitors progress of the SNES solvers by calling 76d132466eSBarry Smith VecView() for the UPDATE to the solution at each iteration. 77d132466eSBarry Smith 78d132466eSBarry Smith Collective on SNES 79d132466eSBarry Smith 80d132466eSBarry Smith Input Parameters: 81d132466eSBarry Smith + snes - the SNES context 82d132466eSBarry Smith . its - iteration number 834b27c08aSLois Curfman McInnes . fgnorm - 2-norm of residual 84f55353a2SBarry Smith - dummy - a viewer 85d132466eSBarry Smith 86d132466eSBarry Smith Level: intermediate 87d132466eSBarry Smith 88d132466eSBarry Smith .keywords: SNES, nonlinear, vector, monitor, view 89d132466eSBarry Smith 90a6570f20SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorDefault(), VecView() 91d132466eSBarry Smith @*/ 927087cfbeSBarry Smith PetscErrorCode SNESMonitorSolutionUpdate(SNES snes,PetscInt its,PetscReal fgnorm,void *dummy) 93d132466eSBarry Smith { 94dfbe8321SBarry Smith PetscErrorCode ierr; 95d132466eSBarry Smith Vec x; 96b0a32e0cSBarry Smith PetscViewer viewer = (PetscViewer) dummy; 97d132466eSBarry Smith 98d132466eSBarry Smith PetscFunctionBegin; 994d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 100d132466eSBarry Smith ierr = SNESGetSolutionUpdate(snes,&x);CHKERRQ(ierr); 101d132466eSBarry Smith ierr = VecView(x,viewer);CHKERRQ(ierr); 102d132466eSBarry Smith PetscFunctionReturn(0); 103d132466eSBarry Smith } 104d132466eSBarry Smith 1054a2ae208SSatish Balay #undef __FUNCT__ 106a5c2985bSBarry Smith #define __FUNCT__ "KSPMonitorSNES" 107a5c2985bSBarry Smith /*@C 108a5c2985bSBarry Smith KSPMonitorSNES - Print the residual norm of the nonlinear function at each iteration of the linear iterative solver. 109a5c2985bSBarry Smith 110a5c2985bSBarry Smith Collective on KSP 111a5c2985bSBarry Smith 112a5c2985bSBarry Smith Input Parameters: 113a5c2985bSBarry Smith + ksp - iterative context 114a5c2985bSBarry Smith . n - iteration number 115a5c2985bSBarry Smith . rnorm - 2-norm (preconditioned) residual value (may be estimated). 116a5c2985bSBarry Smith - dummy - unused monitor context 117a5c2985bSBarry Smith 118a5c2985bSBarry Smith Level: intermediate 119a5c2985bSBarry Smith 120a5c2985bSBarry Smith .keywords: KSP, default, monitor, residual 121a5c2985bSBarry Smith 122a5c2985bSBarry Smith .seealso: KSPMonitorSet(), KSPMonitorTrueResidualNorm(), KSPMonitorLGResidualNormCreate() 123a5c2985bSBarry Smith @*/ 124a5c2985bSBarry Smith PetscErrorCode KSPMonitorSNES(KSP ksp,PetscInt n,PetscReal rnorm,void *dummy) 125a5c2985bSBarry Smith { 126a5c2985bSBarry Smith PetscErrorCode ierr; 127a5c2985bSBarry Smith PetscViewer viewer; 128a5c2985bSBarry Smith SNES snes = (SNES) dummy; 129a5c2985bSBarry Smith Vec snes_solution,work1,work2; 130a5c2985bSBarry Smith PetscReal snorm; 131a5c2985bSBarry Smith 132a5c2985bSBarry Smith PetscFunctionBegin; 133a5c2985bSBarry Smith ierr = SNESGetSolution(snes,&snes_solution);CHKERRQ(ierr); 134a5c2985bSBarry Smith ierr = VecDuplicate(snes_solution,&work1);CHKERRQ(ierr); 135a5c2985bSBarry Smith ierr = VecDuplicate(snes_solution,&work2);CHKERRQ(ierr); 136a5c2985bSBarry Smith ierr = KSPBuildSolution(ksp,work1,NULL);CHKERRQ(ierr); 137a5c2985bSBarry Smith ierr = VecAYPX(work1,-1.0,snes_solution);CHKERRQ(ierr); 138a5c2985bSBarry Smith ierr = SNESComputeFunction(snes,work1,work2);CHKERRQ(ierr); 139a5c2985bSBarry Smith ierr = VecNorm(work2,NORM_2,&snorm);CHKERRQ(ierr); 140a5c2985bSBarry Smith ierr = VecDestroy(&work1);CHKERRQ(ierr); 141a5c2985bSBarry Smith ierr = VecDestroy(&work2);CHKERRQ(ierr); 142a5c2985bSBarry Smith 143a5c2985bSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ksp),&viewer);CHKERRQ(ierr); 144a5c2985bSBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ksp)->tablevel);CHKERRQ(ierr); 145a5c2985bSBarry Smith if (n == 0 && ((PetscObject)ksp)->prefix) { 146a5c2985bSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Residual norms for %s solve.\n",((PetscObject)ksp)->prefix);CHKERRQ(ierr); 147a5c2985bSBarry Smith } 148a5c2985bSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Residual norm %5.3e KSP Residual norm %5.3e \n",n,(double)snorm,(double)rnorm);CHKERRQ(ierr); 149a5c2985bSBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ksp)->tablevel);CHKERRQ(ierr); 150a5c2985bSBarry Smith PetscFunctionReturn(0); 151a5c2985bSBarry Smith } 152a5c2985bSBarry Smith 153e5f7ee39SBarry Smith #include <petscdraw.h> 154e5f7ee39SBarry Smith 155e5f7ee39SBarry Smith #undef __FUNCT__ 156e5f7ee39SBarry Smith #define __FUNCT__ "KSPMonitorSNESLGResidualNormCreate" 157e5f7ee39SBarry Smith /*@C 158e5f7ee39SBarry Smith KSPMonitorSNESLGResidualNormCreate - Creates a line graph context for use with 159e5f7ee39SBarry Smith KSP to monitor convergence of preconditioned residual norms. 160e5f7ee39SBarry Smith 161e5f7ee39SBarry Smith Collective on KSP 162e5f7ee39SBarry Smith 163e5f7ee39SBarry Smith Input Parameters: 1648b0b5a47SLisandro Dalcin + comm - communicator context 1658b0b5a47SLisandro Dalcin . host - the X display to open, or null for the local machine 166e5f7ee39SBarry Smith . label - the title to put in the title bar 167e5f7ee39SBarry Smith . x, y - the screen coordinates of the upper left coordinate of 168e5f7ee39SBarry Smith the window 169e5f7ee39SBarry Smith - m, n - the screen width and height in pixels 170e5f7ee39SBarry Smith 171e5f7ee39SBarry Smith Output Parameter: 172e5f7ee39SBarry Smith . draw - the drawing context 173e5f7ee39SBarry Smith 174e5f7ee39SBarry Smith Options Database Key: 175e5f7ee39SBarry Smith . -ksp_monitor_lg_residualnorm - Sets line graph monitor 176e5f7ee39SBarry Smith 177e5f7ee39SBarry Smith Notes: 178e5f7ee39SBarry Smith Use KSPMonitorSNESLGResidualNormDestroy() to destroy this line graph; do not use PetscDrawLGDestroy(). 179e5f7ee39SBarry Smith 180e5f7ee39SBarry Smith Level: intermediate 181e5f7ee39SBarry Smith 182e5f7ee39SBarry Smith .keywords: KSP, monitor, line graph, residual, create 183e5f7ee39SBarry Smith 184e5f7ee39SBarry Smith .seealso: KSPMonitorSNESLGResidualNormDestroy(), KSPMonitorSet(), KSPMonitorSNESLGTrueResidualCreate() 185e5f7ee39SBarry Smith @*/ 1868b0b5a47SLisandro Dalcin PetscErrorCode KSPMonitorSNESLGResidualNormCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscObject **objs) 187e5f7ee39SBarry Smith { 188e5f7ee39SBarry Smith PetscDraw draw; 189e5f7ee39SBarry Smith PetscErrorCode ierr; 190e5f7ee39SBarry Smith PetscDrawAxis axis; 191c36cb520SLisandro Dalcin PetscDrawLG lg; 192e5f7ee39SBarry Smith const char *names[] = {"Linear residual","Nonlinear residual"}; 193e5f7ee39SBarry Smith 194e5f7ee39SBarry Smith PetscFunctionBegin; 1958b0b5a47SLisandro Dalcin ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr); 196e5f7ee39SBarry Smith ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr); 197c36cb520SLisandro Dalcin ierr = PetscDrawLGCreate(draw,2,&lg);CHKERRQ(ierr); 198c36cb520SLisandro Dalcin ierr = PetscDrawLGSetLegend(lg,names);CHKERRQ(ierr); 199c36cb520SLisandro Dalcin ierr = PetscDrawLGSetFromOptions(lg);CHKERRQ(ierr); 200c36cb520SLisandro Dalcin ierr = PetscDrawLGGetAxis(lg,&axis);CHKERRQ(ierr); 201e5f7ee39SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Convergence of Residual Norm","Iteration","Residual Norm");CHKERRQ(ierr); 202c36cb520SLisandro Dalcin ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 203e5f7ee39SBarry Smith 204c36cb520SLisandro Dalcin ierr = PetscMalloc1(2,objs);CHKERRQ(ierr); 205c36cb520SLisandro Dalcin (*objs)[1] = (PetscObject)lg; 206e5f7ee39SBarry Smith PetscFunctionReturn(0); 207e5f7ee39SBarry Smith } 208e5f7ee39SBarry Smith 209e5f7ee39SBarry Smith #undef __FUNCT__ 210e5f7ee39SBarry Smith #define __FUNCT__ "KSPMonitorSNESLGResidualNorm" 211e5f7ee39SBarry Smith PetscErrorCode KSPMonitorSNESLGResidualNorm(KSP ksp,PetscInt n,PetscReal rnorm,PetscObject *objs) 212e5f7ee39SBarry Smith { 213c36cb520SLisandro Dalcin SNES snes = (SNES) objs[0]; 214e5f7ee39SBarry Smith PetscDrawLG lg = (PetscDrawLG) objs[1]; 215e5f7ee39SBarry Smith PetscErrorCode ierr; 216e5f7ee39SBarry Smith PetscReal y[2]; 217e5f7ee39SBarry Smith Vec snes_solution,work1,work2; 218e5f7ee39SBarry Smith 219e5f7ee39SBarry Smith PetscFunctionBegin; 220e5f7ee39SBarry Smith if (rnorm > 0.0) y[0] = PetscLog10Real(rnorm); 221e5f7ee39SBarry Smith else y[0] = -15.0; 222e5f7ee39SBarry Smith 223e5f7ee39SBarry Smith ierr = SNESGetSolution(snes,&snes_solution);CHKERRQ(ierr); 224e5f7ee39SBarry Smith ierr = VecDuplicate(snes_solution,&work1);CHKERRQ(ierr); 225e5f7ee39SBarry Smith ierr = VecDuplicate(snes_solution,&work2);CHKERRQ(ierr); 226e5f7ee39SBarry Smith ierr = KSPBuildSolution(ksp,work1,NULL);CHKERRQ(ierr); 227e5f7ee39SBarry Smith ierr = VecAYPX(work1,-1.0,snes_solution);CHKERRQ(ierr); 228e5f7ee39SBarry Smith ierr = SNESComputeFunction(snes,work1,work2);CHKERRQ(ierr); 229e5f7ee39SBarry Smith ierr = VecNorm(work2,NORM_2,y+1);CHKERRQ(ierr); 230e5f7ee39SBarry Smith if (y[1] > 0.0) y[1] = PetscLog10Real(y[1]); 231e5f7ee39SBarry Smith else y[1] = -15.0; 232e5f7ee39SBarry Smith ierr = VecDestroy(&work1);CHKERRQ(ierr); 233e5f7ee39SBarry Smith ierr = VecDestroy(&work2);CHKERRQ(ierr); 234e5f7ee39SBarry Smith 235e5f7ee39SBarry Smith ierr = PetscDrawLGAddPoint(lg,NULL,y);CHKERRQ(ierr); 236e5f7ee39SBarry Smith if (n < 20 || !(n % 5)) { 237e5f7ee39SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 238e5f7ee39SBarry Smith } 239e5f7ee39SBarry Smith PetscFunctionReturn(0); 240e5f7ee39SBarry Smith } 241e5f7ee39SBarry Smith 242e5f7ee39SBarry Smith #undef __FUNCT__ 243e5f7ee39SBarry Smith #define __FUNCT__ "KSPMonitorSNESLGResidualNormDestroy" 244e5f7ee39SBarry Smith /*@ 245e5f7ee39SBarry Smith KSPMonitorSNESLGResidualNormDestroy - Destroys a line graph context that was created 246e5f7ee39SBarry Smith with KSPMonitorSNESLGResidualNormCreate(). 247e5f7ee39SBarry Smith 248e5f7ee39SBarry Smith Collective on KSP 249e5f7ee39SBarry Smith 250e5f7ee39SBarry Smith Input Parameter: 251e5f7ee39SBarry Smith . draw - the drawing context 252e5f7ee39SBarry Smith 253e5f7ee39SBarry Smith Level: intermediate 254e5f7ee39SBarry Smith 255e5f7ee39SBarry Smith .keywords: KSP, monitor, line graph, destroy 256e5f7ee39SBarry Smith 257e5f7ee39SBarry Smith .seealso: KSPMonitorSNESLGResidualNormCreate(), KSPMonitorSNESLGTrueResidualDestroy(), KSPMonitorSet() 258e5f7ee39SBarry Smith @*/ 259e5f7ee39SBarry Smith PetscErrorCode KSPMonitorSNESLGResidualNormDestroy(PetscObject **objs) 260e5f7ee39SBarry Smith { 261e5f7ee39SBarry Smith PetscErrorCode ierr; 262c36cb520SLisandro Dalcin PetscDrawLG lg = (PetscDrawLG) (*objs)[1]; 263e5f7ee39SBarry Smith 264e5f7ee39SBarry Smith PetscFunctionBegin; 265c36cb520SLisandro Dalcin ierr = PetscDrawLGDestroy(&lg);CHKERRQ(ierr); 266e5f7ee39SBarry Smith ierr = PetscFree(*objs);CHKERRQ(ierr); 267e5f7ee39SBarry Smith PetscFunctionReturn(0); 268e5f7ee39SBarry Smith } 269e5f7ee39SBarry Smith 270a5c2985bSBarry Smith #undef __FUNCT__ 271a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorDefault" 2724b828684SBarry Smith /*@C 273a6570f20SBarry Smith SNESMonitorDefault - Monitors progress of the SNES solvers (default). 274e7e93795SLois Curfman McInnes 275c7afd0dbSLois Curfman McInnes Collective on SNES 276c7afd0dbSLois Curfman McInnes 277e7e93795SLois Curfman McInnes Input Parameters: 278c7afd0dbSLois Curfman McInnes + snes - the SNES context 279e7e93795SLois Curfman McInnes . its - iteration number 2804b27c08aSLois Curfman McInnes . fgnorm - 2-norm of residual 281c7afd0dbSLois Curfman McInnes - dummy - unused context 282fee21e36SBarry Smith 283e7e93795SLois Curfman McInnes Notes: 2844b27c08aSLois Curfman McInnes This routine prints the residual norm at each iteration. 285e7e93795SLois Curfman McInnes 28636851e7fSLois Curfman McInnes Level: intermediate 28736851e7fSLois Curfman McInnes 288e7e93795SLois Curfman McInnes .keywords: SNES, nonlinear, default, monitor, norm 289e7e93795SLois Curfman McInnes 290a6570f20SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorSolution() 291e7e93795SLois Curfman McInnes @*/ 2927087cfbeSBarry Smith PetscErrorCode SNESMonitorDefault(SNES snes,PetscInt its,PetscReal fgnorm,void *dummy) 293e7e93795SLois Curfman McInnes { 294dfbe8321SBarry Smith PetscErrorCode ierr; 2954d4332d5SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 296d132466eSBarry Smith 2973a40ed3dSBarry Smith PetscFunctionBegin; 2984d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 299649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr); 300649052a6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %14.12e \n",its,(double)fgnorm);CHKERRQ(ierr); 301649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr); 3023a40ed3dSBarry Smith PetscFunctionReturn(0); 303e7e93795SLois Curfman McInnes } 3043f1db9ecSBarry Smith 305b271bb04SBarry Smith #undef __FUNCT__ 3062e7541e6SPeter Brune #define __FUNCT__ "SNESMonitorJacUpdateSpectrum" 307a80ad3e0SBarry Smith PetscErrorCode SNESMonitorJacUpdateSpectrum(SNES snes,PetscInt it,PetscReal fnorm,void *ctx) 308a80ad3e0SBarry Smith { 309196da8b6SPeter Brune #if defined(PETSC_MISSING_LAPACK_GEEV) 310ce94432eSBarry Smith SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_SUP,"GEEV - Lapack routine is unavailable\nNot able to provide eigen values."); 311196da8b6SPeter Brune #elif defined(PETSC_HAVE_ESSL) 312ce94432eSBarry Smith SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_SUP,"GEEV - No support for ESSL Lapack Routines"); 313196da8b6SPeter Brune #else 3142e7541e6SPeter Brune Vec X; 3152e7541e6SPeter Brune Mat J,dJ,dJdense; 3162e7541e6SPeter Brune PetscErrorCode ierr; 317d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 3182e7541e6SPeter Brune PetscInt n,i; 3192e7541e6SPeter Brune PetscBLASInt nb,lwork; 3202e7541e6SPeter Brune PetscReal *eigr,*eigi; 3212e7541e6SPeter Brune PetscScalar *work; 3222e7541e6SPeter Brune PetscScalar *a; 3232e7541e6SPeter Brune 3242e7541e6SPeter Brune PetscFunctionBegin; 3252e7541e6SPeter Brune if (it == 0) PetscFunctionReturn(0); 3262e7541e6SPeter Brune /* create the difference between the current update and the current jacobian */ 3272e7541e6SPeter Brune ierr = SNESGetSolution(snes,&X);CHKERRQ(ierr); 328d1e9a80fSBarry Smith ierr = SNESGetJacobian(snes,NULL,&J,&func,NULL);CHKERRQ(ierr); 3292e7541e6SPeter Brune ierr = MatDuplicate(J,MAT_COPY_VALUES,&dJ);CHKERRQ(ierr); 330d1e9a80fSBarry Smith ierr = SNESComputeJacobian(snes,X,dJ,dJ);CHKERRQ(ierr); 3312e7541e6SPeter Brune ierr = MatAXPY(dJ,-1.0,J,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 332f5af7f23SKarl Rupp 3332e7541e6SPeter Brune /* compute the spectrum directly */ 3342e7541e6SPeter Brune ierr = MatConvert(dJ,MATSEQDENSE,MAT_INITIAL_MATRIX,&dJdense);CHKERRQ(ierr); 3350298fd71SBarry Smith ierr = MatGetSize(dJ,&n,NULL);CHKERRQ(ierr); 336c5df96a5SBarry Smith ierr = PetscBLASIntCast(n,&nb);CHKERRQ(ierr); 3372e7541e6SPeter Brune lwork = 3*nb; 338785e854fSJed Brown ierr = PetscMalloc1(n,&eigr);CHKERRQ(ierr); 339785e854fSJed Brown ierr = PetscMalloc1(n,&eigi);CHKERRQ(ierr); 340785e854fSJed Brown ierr = PetscMalloc1(lwork,&work);CHKERRQ(ierr); 3418c778c55SBarry Smith ierr = MatDenseGetArray(dJdense,&a);CHKERRQ(ierr); 3422e7541e6SPeter Brune #if !defined(PETSC_USE_COMPLEX) 3432e7541e6SPeter Brune { 3442e7541e6SPeter Brune PetscBLASInt lierr; 3452e7541e6SPeter Brune ierr = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr); 3468b83055fSJed Brown PetscStackCallBLAS("LAPACKgeev",LAPACKgeev_("N","N",&nb,a,&nb,eigr,eigi,NULL,&nb,NULL,&nb,work,&lwork,&lierr)); 3472e7541e6SPeter Brune if (lierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"geev() error %d",lierr); 3482e7541e6SPeter Brune ierr = PetscFPTrapPop();CHKERRQ(ierr); 3492e7541e6SPeter Brune } 3502e7541e6SPeter Brune #else 3512e7541e6SPeter Brune SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not coded for complex"); 3522e7541e6SPeter Brune #endif 353*fde5950dSBarry Smith ierr = PetscPrintf(PetscObjectComm((PetscObject)snes),"Eigenvalues of J_%d - J_%d:\n",it,it-1);CHKERRQ(ierr); 3542e7541e6SPeter Brune for (i=0;i<n;i++) { 355*fde5950dSBarry Smith ierr = PetscPrintf(PetscObjectComm((PetscObject)snes),"%5d: %20.5g + %20.5gi\n",i,(double)eigr[i],(double)eigi[i]);CHKERRQ(ierr); 3562e7541e6SPeter Brune } 3578c778c55SBarry Smith ierr = MatDenseRestoreArray(dJdense,&a);CHKERRQ(ierr); 3582e7541e6SPeter Brune ierr = MatDestroy(&dJ);CHKERRQ(ierr); 3592e7541e6SPeter Brune ierr = MatDestroy(&dJdense);CHKERRQ(ierr); 3602e7541e6SPeter Brune ierr = PetscFree(eigr);CHKERRQ(ierr); 3612e7541e6SPeter Brune ierr = PetscFree(eigi);CHKERRQ(ierr); 3622e7541e6SPeter Brune ierr = PetscFree(work);CHKERRQ(ierr); 3632e7541e6SPeter Brune PetscFunctionReturn(0); 364196da8b6SPeter Brune #endif 3652e7541e6SPeter Brune } 3662e7541e6SPeter Brune 3672e7541e6SPeter Brune #undef __FUNCT__ 368b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorRange_Private" 3697087cfbeSBarry Smith PetscErrorCode SNESMonitorRange_Private(SNES snes,PetscInt it,PetscReal *per) 370b271bb04SBarry Smith { 371b271bb04SBarry Smith PetscErrorCode ierr; 372b271bb04SBarry Smith Vec resid; 373b271bb04SBarry Smith PetscReal rmax,pwork; 374b271bb04SBarry Smith PetscInt i,n,N; 375b271bb04SBarry Smith PetscScalar *r; 376b271bb04SBarry Smith 377b271bb04SBarry Smith PetscFunctionBegin; 378b271bb04SBarry Smith ierr = SNESGetFunction(snes,&resid,0,0);CHKERRQ(ierr); 379b271bb04SBarry Smith ierr = VecNorm(resid,NORM_INFINITY,&rmax);CHKERRQ(ierr); 380b271bb04SBarry Smith ierr = VecGetLocalSize(resid,&n);CHKERRQ(ierr); 381b271bb04SBarry Smith ierr = VecGetSize(resid,&N);CHKERRQ(ierr); 382b271bb04SBarry Smith ierr = VecGetArray(resid,&r);CHKERRQ(ierr); 383b271bb04SBarry Smith pwork = 0.0; 384b271bb04SBarry Smith for (i=0; i<n; i++) { 385b271bb04SBarry Smith pwork += (PetscAbsScalar(r[i]) > .20*rmax); 386b271bb04SBarry Smith } 387b2566f29SBarry Smith ierr = MPIU_Allreduce(&pwork,per,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)snes));CHKERRQ(ierr); 388b271bb04SBarry Smith ierr = VecRestoreArray(resid,&r);CHKERRQ(ierr); 389b271bb04SBarry Smith *per = *per/N; 390b271bb04SBarry Smith PetscFunctionReturn(0); 391b271bb04SBarry Smith } 392b271bb04SBarry Smith 393b271bb04SBarry Smith #undef __FUNCT__ 394b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorRange" 395b271bb04SBarry Smith /*@C 396b271bb04SBarry Smith SNESMonitorRange - Prints the percentage of residual elements that are more then 10 percent of the maximum value. 397b271bb04SBarry Smith 398b271bb04SBarry Smith Collective on SNES 399b271bb04SBarry Smith 400b271bb04SBarry Smith Input Parameters: 401b271bb04SBarry Smith + snes - iterative context 402b271bb04SBarry Smith . it - iteration number 403b271bb04SBarry Smith . rnorm - 2-norm (preconditioned) residual value (may be estimated). 404b271bb04SBarry Smith - dummy - unused monitor context 405b271bb04SBarry Smith 406b271bb04SBarry Smith Options Database Key: 407b271bb04SBarry Smith . -snes_monitor_range - Activates SNESMonitorRange() 408b271bb04SBarry Smith 409b271bb04SBarry Smith Level: intermediate 410b271bb04SBarry Smith 411b271bb04SBarry Smith .keywords: SNES, default, monitor, residual 412b271bb04SBarry Smith 413b271bb04SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorDefault(), SNESMonitorLGCreate() 414b271bb04SBarry Smith @*/ 4157087cfbeSBarry Smith PetscErrorCode SNESMonitorRange(SNES snes,PetscInt it,PetscReal rnorm,void *dummy) 416b271bb04SBarry Smith { 417b271bb04SBarry Smith PetscErrorCode ierr; 418b271bb04SBarry Smith PetscReal perc,rel; 4194d4332d5SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 420b271bb04SBarry Smith /* should be in a MonitorRangeContext */ 421b271bb04SBarry Smith static PetscReal prev; 422b271bb04SBarry Smith 423b271bb04SBarry Smith PetscFunctionBegin; 4244d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 425b271bb04SBarry Smith if (!it) prev = rnorm; 426b271bb04SBarry Smith ierr = SNESMonitorRange_Private(snes,it,&perc);CHKERRQ(ierr); 427b271bb04SBarry Smith 428b271bb04SBarry Smith rel = (prev - rnorm)/prev; 429b271bb04SBarry Smith prev = rnorm; 430649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr); 4316712e2f1SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES preconditioned resid norm %14.12e Percent values above 20 percent of maximum %5.2f relative decrease %5.2e ratio %5.2e \n",it,(double)rnorm,(double)(100.0*perc),(double)rel,(double)(rel/perc));CHKERRQ(ierr); 432649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr); 433b271bb04SBarry Smith PetscFunctionReturn(0); 434b271bb04SBarry Smith } 435b271bb04SBarry Smith 4363a7fca6bSBarry Smith #undef __FUNCT__ 437a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorRatio" 4383a7fca6bSBarry Smith /*@C 439a6570f20SBarry Smith SNESMonitorRatio - Monitors progress of the SNES solvers by printing the ratio 4404b27c08aSLois Curfman McInnes of residual norm at each iteration to the previous. 4413a7fca6bSBarry Smith 4423a7fca6bSBarry Smith Collective on SNES 4433a7fca6bSBarry Smith 4443a7fca6bSBarry Smith Input Parameters: 4453a7fca6bSBarry Smith + snes - the SNES context 4463a7fca6bSBarry Smith . its - iteration number 4473a7fca6bSBarry Smith . fgnorm - 2-norm of residual (or gradient) 448eabae89aSBarry Smith - dummy - context of monitor 4493a7fca6bSBarry Smith 4503a7fca6bSBarry Smith Level: intermediate 4513a7fca6bSBarry Smith 452*fde5950dSBarry Smith Notes: Insure that SNESMonitorRatio() is called when you set this monitor 4533a7fca6bSBarry Smith .keywords: SNES, nonlinear, monitor, norm 4543a7fca6bSBarry Smith 455*fde5950dSBarry Smith .seealso: SNESMonitorSet(), SNESMonitorSolution(), SNESMonitorRatio() 4563a7fca6bSBarry Smith @*/ 4577087cfbeSBarry Smith PetscErrorCode SNESMonitorRatio(SNES snes,PetscInt its,PetscReal fgnorm,void *dummy) 4583a7fca6bSBarry Smith { 459dfbe8321SBarry Smith PetscErrorCode ierr; 46077431f27SBarry Smith PetscInt len; 46187828ca2SBarry Smith PetscReal *history; 462*fde5950dSBarry Smith PetscViewer viewer = (PetscViewer) dummy; 4633a7fca6bSBarry Smith 4643a7fca6bSBarry Smith PetscFunctionBegin; 4650298fd71SBarry Smith ierr = SNESGetConvergenceHistory(snes,&history,NULL,&len);CHKERRQ(ierr); 466*fde5950dSBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr); 467958c9bccSBarry Smith if (!its || !history || its > len) { 468*fde5950dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %14.12e \n",its,(double)fgnorm);CHKERRQ(ierr); 4693a7fca6bSBarry Smith } else { 47087828ca2SBarry Smith PetscReal ratio = fgnorm/history[its-1]; 471*fde5950dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %14.12e %14.12e \n",its,(double)fgnorm,(double)ratio);CHKERRQ(ierr); 4723a7fca6bSBarry Smith } 473*fde5950dSBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr); 4743a7fca6bSBarry Smith PetscFunctionReturn(0); 4753a7fca6bSBarry Smith } 4763a7fca6bSBarry Smith 4773a7fca6bSBarry Smith #undef __FUNCT__ 478*fde5950dSBarry Smith #define __FUNCT__ "SNESMonitorRatioSetUp" 4793a7fca6bSBarry Smith /*@C 480*fde5950dSBarry Smith SNESMonitorRatioSetUp - Insures the SNES object is saving its history since this monitor needs access to it 4813a7fca6bSBarry Smith 4823a7fca6bSBarry Smith Collective on SNES 4833a7fca6bSBarry Smith 4843a7fca6bSBarry Smith Input Parameters: 485eabae89aSBarry Smith + snes - the SNES context 486*fde5950dSBarry Smith - viewer - the PetscViewer object (ignored) 4873a7fca6bSBarry Smith 4883a7fca6bSBarry Smith Level: intermediate 4893a7fca6bSBarry Smith 4903a7fca6bSBarry Smith .keywords: SNES, nonlinear, monitor, norm 4913a7fca6bSBarry Smith 492*fde5950dSBarry Smith .seealso: SNESMonitorSet(), SNESMonitorSolution(), SNESMonitorDefault(), SNESMonitorRatio() 4933a7fca6bSBarry Smith @*/ 494*fde5950dSBarry Smith PetscErrorCode SNESMonitorRatioSetUp(SNES snes,PetscViewer viewer) 4953a7fca6bSBarry Smith { 496dfbe8321SBarry Smith PetscErrorCode ierr; 49787828ca2SBarry Smith PetscReal *history; 4983a7fca6bSBarry Smith 4993a7fca6bSBarry Smith PetscFunctionBegin; 5000298fd71SBarry Smith ierr = SNESGetConvergenceHistory(snes,&history,NULL,NULL);CHKERRQ(ierr); 5013a7fca6bSBarry Smith if (!history) { 502*fde5950dSBarry Smith ierr = SNESSetConvergenceHistory(snes,NULL,NULL,100,PETSC_TRUE);CHKERRQ(ierr); 5033a7fca6bSBarry Smith } 5043a7fca6bSBarry Smith PetscFunctionReturn(0); 5053a7fca6bSBarry Smith } 5063a7fca6bSBarry Smith 507e7e93795SLois Curfman McInnes /* ---------------------------------------------------------------- */ 5084a2ae208SSatish Balay #undef __FUNCT__ 509a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorDefaultShort" 510be1f7002SBarry Smith /* 511a6570f20SBarry Smith Default (short) SNES Monitor, same as SNESMonitorDefault() except 512be1f7002SBarry Smith it prints fewer digits of the residual as the residual gets smaller. 513be1f7002SBarry Smith This is because the later digits are meaningless and are often 514be1f7002SBarry Smith different on different machines; by using this routine different 515be1f7002SBarry Smith machines will usually generate the same output. 516be1f7002SBarry Smith */ 5177087cfbeSBarry Smith PetscErrorCode SNESMonitorDefaultShort(SNES snes,PetscInt its,PetscReal fgnorm,void *dummy) 518e7e93795SLois Curfman McInnes { 519dfbe8321SBarry Smith PetscErrorCode ierr; 5204d4332d5SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 521d132466eSBarry Smith 5223a40ed3dSBarry Smith PetscFunctionBegin; 5234d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 524649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr); 5258f240d10SBarry Smith if (fgnorm > 1.e-9) { 5268fa295daSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %g \n",its,(double)fgnorm);CHKERRQ(ierr); 5273a40ed3dSBarry Smith } else if (fgnorm > 1.e-11) { 5288fa295daSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %5.3e \n",its,(double)fgnorm);CHKERRQ(ierr); 5293a40ed3dSBarry Smith } else { 530649052a6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm < 1.e-11\n",its);CHKERRQ(ierr); 531a34d58ebSBarry Smith } 532649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr); 5333a40ed3dSBarry Smith PetscFunctionReturn(0); 534e7e93795SLois Curfman McInnes } 5352db13446SMatthew G. Knepley 5362db13446SMatthew G. Knepley #undef __FUNCT__ 5372db13446SMatthew G. Knepley #define __FUNCT__ "SNESMonitorDefaultField" 5382db13446SMatthew G. Knepley /*@C 5392db13446SMatthew G. Knepley SNESMonitorDefaultField - Monitors progress of the SNES solvers, separated into fields. 5402db13446SMatthew G. Knepley 5412db13446SMatthew G. Knepley Collective on SNES 5422db13446SMatthew G. Knepley 5432db13446SMatthew G. Knepley Input Parameters: 5442db13446SMatthew G. Knepley + snes - the SNES context 5452db13446SMatthew G. Knepley . its - iteration number 5462db13446SMatthew G. Knepley . fgnorm - 2-norm of residual 5472db13446SMatthew G. Knepley - ctx - the PetscViewer 5482db13446SMatthew G. Knepley 5492db13446SMatthew G. Knepley Notes: 5502db13446SMatthew G. Knepley This routine uses the DM attached to the residual vector 5512db13446SMatthew G. Knepley 5522db13446SMatthew G. Knepley Level: intermediate 5532db13446SMatthew G. Knepley 5542db13446SMatthew G. Knepley .keywords: SNES, nonlinear, field, monitor, norm 5552db13446SMatthew G. Knepley .seealso: SNESMonitorSet(), SNESMonitorSolution(), SNESMonitorDefault(), SNESMonitorDefaultShort() 5562db13446SMatthew G. Knepley @*/ 5572db13446SMatthew G. Knepley PetscErrorCode SNESMonitorDefaultField(SNES snes, PetscInt its, PetscReal fgnorm, void *ctx) 5582db13446SMatthew G. Knepley { 5594d4332d5SBarry Smith PetscViewer viewer = (PetscViewer) ctx; 5602db13446SMatthew G. Knepley Vec r; 5612db13446SMatthew G. Knepley DM dm; 5622db13446SMatthew G. Knepley PetscReal res[256]; 5632db13446SMatthew G. Knepley PetscInt tablevel; 5642db13446SMatthew G. Knepley PetscErrorCode ierr; 5652db13446SMatthew G. Knepley 5662db13446SMatthew G. Knepley PetscFunctionBegin; 5674d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 5682db13446SMatthew G. Knepley ierr = SNESGetFunction(snes, &r, NULL, NULL);CHKERRQ(ierr); 5692db13446SMatthew G. Knepley ierr = VecGetDM(r, &dm);CHKERRQ(ierr); 5702db13446SMatthew G. Knepley if (!dm) {ierr = SNESMonitorDefault(snes, its, fgnorm, ctx);CHKERRQ(ierr);} 5712db13446SMatthew G. Knepley else { 5722db13446SMatthew G. Knepley PetscSection s, gs; 5732db13446SMatthew G. Knepley PetscInt Nf, f; 5742db13446SMatthew G. Knepley 5752db13446SMatthew G. Knepley ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr); 5762db13446SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr); 5772db13446SMatthew G. Knepley if (!s || !gs) {ierr = SNESMonitorDefault(snes, its, fgnorm, ctx);CHKERRQ(ierr);} 5782db13446SMatthew G. Knepley ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr); 5792db13446SMatthew G. Knepley if (Nf > 256) SETERRQ1(PetscObjectComm((PetscObject) snes), PETSC_ERR_SUP, "Do not support %d fields > 256", Nf); 5802db13446SMatthew G. Knepley ierr = PetscSectionVecNorm(s, gs, r, NORM_2, res);CHKERRQ(ierr); 5812db13446SMatthew G. Knepley ierr = PetscObjectGetTabLevel((PetscObject) snes, &tablevel);CHKERRQ(ierr); 5822db13446SMatthew G. Knepley ierr = PetscViewerASCIIAddTab(viewer, tablevel);CHKERRQ(ierr); 5832db13446SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr); 5842db13446SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 5852db13446SMatthew G. Knepley if (f) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);} 5862db13446SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", res[f]);CHKERRQ(ierr); 5872db13446SMatthew G. Knepley } 5882db13446SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "] \n");CHKERRQ(ierr); 5892db13446SMatthew G. Knepley ierr = PetscViewerASCIISubtractTab(viewer, tablevel);CHKERRQ(ierr); 5902db13446SMatthew G. Knepley } 5912db13446SMatthew G. Knepley PetscFunctionReturn(0); 5922db13446SMatthew G. Knepley } 593e7e93795SLois Curfman McInnes /* ---------------------------------------------------------------- */ 5944a2ae208SSatish Balay #undef __FUNCT__ 5958d359177SBarry Smith #define __FUNCT__ "SNESConvergedDefault" 5964b828684SBarry Smith /*@C 5978d359177SBarry Smith SNESConvergedDefault - Convergence test of the solvers for 598f525115eSLois Curfman McInnes systems of nonlinear equations (default). 599e7e93795SLois Curfman McInnes 600c7afd0dbSLois Curfman McInnes Collective on SNES 601c7afd0dbSLois Curfman McInnes 602e7e93795SLois Curfman McInnes Input Parameters: 603c7afd0dbSLois Curfman McInnes + snes - the SNES context 60406ee9f85SBarry Smith . it - the iteration (0 indicates before any Newton steps) 605e7e93795SLois Curfman McInnes . xnorm - 2-norm of current iterate 606c60f73f4SPeter Brune . snorm - 2-norm of current step 6077f3332b4SBarry Smith . fnorm - 2-norm of function at current iterate 608c7afd0dbSLois Curfman McInnes - dummy - unused context 609e7e93795SLois Curfman McInnes 610184914b5SBarry Smith Output Parameter: 611184914b5SBarry Smith . reason - one of 61270441072SBarry Smith $ SNES_CONVERGED_FNORM_ABS - (fnorm < abstol), 613c60f73f4SPeter Brune $ SNES_CONVERGED_SNORM_RELATIVE - (snorm < stol*xnorm), 614184914b5SBarry Smith $ SNES_CONVERGED_FNORM_RELATIVE - (fnorm < rtol*fnorm0), 615184914b5SBarry Smith $ SNES_DIVERGED_FUNCTION_COUNT - (nfct > maxf), 616184914b5SBarry Smith $ SNES_DIVERGED_FNORM_NAN - (fnorm == NaN), 617184914b5SBarry Smith $ SNES_CONVERGED_ITERATING - (otherwise), 618e7e93795SLois Curfman McInnes 619e7e93795SLois Curfman McInnes where 620c7afd0dbSLois Curfman McInnes + maxf - maximum number of function evaluations, 621c7afd0dbSLois Curfman McInnes set with SNESSetTolerances() 622c7afd0dbSLois Curfman McInnes . nfct - number of function evaluations, 62370441072SBarry Smith . abstol - absolute function norm tolerance, 624c7afd0dbSLois Curfman McInnes set with SNESSetTolerances() 625c7afd0dbSLois Curfman McInnes - rtol - relative function norm tolerance, set with SNESSetTolerances() 626fee21e36SBarry Smith 62736851e7fSLois Curfman McInnes Level: intermediate 62836851e7fSLois Curfman McInnes 629e7e93795SLois Curfman McInnes .keywords: SNES, nonlinear, default, converged, convergence 630e7e93795SLois Curfman McInnes 63171f87433Sdalcinl .seealso: SNESSetConvergenceTest() 632e7e93795SLois Curfman McInnes @*/ 6338d359177SBarry Smith PetscErrorCode SNESConvergedDefault(SNES snes,PetscInt it,PetscReal xnorm,PetscReal snorm,PetscReal fnorm,SNESConvergedReason *reason,void *dummy) 634e7e93795SLois Curfman McInnes { 63563ba0a88SBarry Smith PetscErrorCode ierr; 63663ba0a88SBarry Smith 6373a40ed3dSBarry Smith PetscFunctionBegin; 6380700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 6393f149594SLisandro Dalcin PetscValidPointer(reason,6); 6403f149594SLisandro Dalcin 64106ee9f85SBarry Smith *reason = SNES_CONVERGED_ITERATING; 64206ee9f85SBarry Smith 64306ee9f85SBarry Smith if (!it) { 64406ee9f85SBarry Smith /* set parameter for default relative tolerance convergence test */ 64506ee9f85SBarry Smith snes->ttol = fnorm*snes->rtol; 64606ee9f85SBarry Smith } 6478146f6ebSBarry Smith if (PetscIsInfOrNanReal(fnorm)) { 648ae15b995SBarry Smith ierr = PetscInfo(snes,"Failed to converged, function norm is NaN\n");CHKERRQ(ierr); 649184914b5SBarry Smith *reason = SNES_DIVERGED_FNORM_NAN; 65070441072SBarry Smith } else if (fnorm < snes->abstol) { 6518f1a2a5eSBarry Smith ierr = PetscInfo2(snes,"Converged due to function norm %14.12e < %14.12e\n",(double)fnorm,(double)snes->abstol);CHKERRQ(ierr); 652184914b5SBarry Smith *reason = SNES_CONVERGED_FNORM_ABS; 65343e71028SBarry Smith } else if (snes->nfuncs >= snes->max_funcs) { 654ae15b995SBarry Smith ierr = PetscInfo2(snes,"Exceeded maximum number of function evaluations: %D > %D\n",snes->nfuncs,snes->max_funcs);CHKERRQ(ierr); 655184914b5SBarry Smith *reason = SNES_DIVERGED_FUNCTION_COUNT; 65606ee9f85SBarry Smith } 65706ee9f85SBarry Smith 65806ee9f85SBarry Smith if (it && !*reason) { 65906ee9f85SBarry Smith if (fnorm <= snes->ttol) { 6608f1a2a5eSBarry Smith ierr = PetscInfo2(snes,"Converged due to function norm %14.12e < %14.12e (relative tolerance)\n",(double)fnorm,(double)snes->ttol);CHKERRQ(ierr); 66106ee9f85SBarry Smith *reason = SNES_CONVERGED_FNORM_RELATIVE; 662c60f73f4SPeter Brune } else if (snorm < snes->stol*xnorm) { 663c60f73f4SPeter Brune ierr = PetscInfo3(snes,"Converged due to small update length: %14.12e < %14.12e * %14.12e\n",(double)snorm,(double)snes->stol,(double)xnorm);CHKERRQ(ierr); 664c60f73f4SPeter Brune *reason = SNES_CONVERGED_SNORM_RELATIVE; 66506ee9f85SBarry Smith } 666e7e93795SLois Curfman McInnes } 6673a40ed3dSBarry Smith PetscFunctionReturn(0); 668e7e93795SLois Curfman McInnes } 6693f149594SLisandro Dalcin 6703f149594SLisandro Dalcin #undef __FUNCT__ 671e2a6519dSDmitry Karpeev #define __FUNCT__ "SNESConvergedSkip" 6723f149594SLisandro Dalcin /*@C 673e2a6519dSDmitry Karpeev SNESConvergedSkip - Convergence test for SNES that NEVER returns as 6743f149594SLisandro Dalcin converged, UNLESS the maximum number of iteration have been reached. 6753f149594SLisandro Dalcin 6763f9fe445SBarry Smith Logically Collective on SNES 6773f149594SLisandro Dalcin 6783f149594SLisandro Dalcin Input Parameters: 6793f149594SLisandro Dalcin + snes - the SNES context 6803f149594SLisandro Dalcin . it - the iteration (0 indicates before any Newton steps) 6813f149594SLisandro Dalcin . xnorm - 2-norm of current iterate 682c60f73f4SPeter Brune . snorm - 2-norm of current step 6833f149594SLisandro Dalcin . fnorm - 2-norm of function at current iterate 6843f149594SLisandro Dalcin - dummy - unused context 6853f149594SLisandro Dalcin 6863f149594SLisandro Dalcin Output Parameter: 68785385478SLisandro Dalcin . reason - SNES_CONVERGED_ITERATING, SNES_CONVERGED_ITS, or SNES_DIVERGED_FNORM_NAN 6883f149594SLisandro Dalcin 6893f149594SLisandro Dalcin Notes: 6903f149594SLisandro Dalcin Convergence is then declared after a fixed number of iterations have been used. 6913f149594SLisandro Dalcin 6923f149594SLisandro Dalcin Level: advanced 6933f149594SLisandro Dalcin 6943f149594SLisandro Dalcin .keywords: SNES, nonlinear, skip, converged, convergence 6953f149594SLisandro Dalcin 6963f149594SLisandro Dalcin .seealso: SNESSetConvergenceTest() 6973f149594SLisandro Dalcin @*/ 698e2a6519dSDmitry Karpeev PetscErrorCode SNESConvergedSkip(SNES snes,PetscInt it,PetscReal xnorm,PetscReal snorm,PetscReal fnorm,SNESConvergedReason *reason,void *dummy) 6993f149594SLisandro Dalcin { 7003f149594SLisandro Dalcin PetscErrorCode ierr; 7013f149594SLisandro Dalcin 7023f149594SLisandro Dalcin PetscFunctionBegin; 7030700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7043f149594SLisandro Dalcin PetscValidPointer(reason,6); 7053f149594SLisandro Dalcin 7063f149594SLisandro Dalcin *reason = SNES_CONVERGED_ITERATING; 7073f149594SLisandro Dalcin 7083f149594SLisandro Dalcin if (fnorm != fnorm) { 7093f149594SLisandro Dalcin ierr = PetscInfo(snes,"Failed to converged, function norm is NaN\n");CHKERRQ(ierr); 7103f149594SLisandro Dalcin *reason = SNES_DIVERGED_FNORM_NAN; 7113f149594SLisandro Dalcin } else if (it == snes->max_its) { 7123f149594SLisandro Dalcin *reason = SNES_CONVERGED_ITS; 7133f149594SLisandro Dalcin } 7143f149594SLisandro Dalcin PetscFunctionReturn(0); 7153f149594SLisandro Dalcin } 7163f149594SLisandro Dalcin 71758c9b817SLisandro Dalcin #undef __FUNCT__ 718fa0ddf94SBarry Smith #define __FUNCT__ "SNESSetWorkVecs" 7198d359177SBarry Smith /*@C 720fa0ddf94SBarry Smith SNESSetWorkVecs - Gets a number of work vectors. 72158c9b817SLisandro Dalcin 72258c9b817SLisandro Dalcin Input Parameters: 72358c9b817SLisandro Dalcin . snes - the SNES context 72458c9b817SLisandro Dalcin . nw - number of work vectors to allocate 72558c9b817SLisandro Dalcin 72658c9b817SLisandro Dalcin Level: developer 72758c9b817SLisandro Dalcin 728fa0ddf94SBarry Smith Developers Note: This is PETSC_EXTERN because it may be used by user written plugin SNES implementations 729fa0ddf94SBarry Smith 73098acb6afSMatthew G Knepley @*/ 731fa0ddf94SBarry Smith PetscErrorCode SNESSetWorkVecs(SNES snes,PetscInt nw) 73258c9b817SLisandro Dalcin { 733c5ed8070SMatthew G. Knepley DM dm; 734c5ed8070SMatthew G. Knepley Vec v; 73558c9b817SLisandro Dalcin PetscErrorCode ierr; 73658c9b817SLisandro Dalcin 73758c9b817SLisandro Dalcin PetscFunctionBegin; 73858c9b817SLisandro Dalcin if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);} 73958c9b817SLisandro Dalcin snes->nwork = nw; 740f5af7f23SKarl Rupp 741c5ed8070SMatthew G. Knepley ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 742c5ed8070SMatthew G. Knepley ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr); 743c5ed8070SMatthew G. Knepley ierr = VecDuplicateVecs(v,snes->nwork,&snes->work);CHKERRQ(ierr); 744c5ed8070SMatthew G. Knepley ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr); 74558c9b817SLisandro Dalcin ierr = PetscLogObjectParents(snes,nw,snes->work);CHKERRQ(ierr); 74658c9b817SLisandro Dalcin PetscFunctionReturn(0); 74758c9b817SLisandro Dalcin } 748