xref: /petsc/src/snes/interface/snesut.c (revision a80ad3e0b1d13640ca709fadabf46bb33024dbd8)
1e7e93795SLois Curfman McInnes 
2b45d2f2cSJed Brown #include <petsc-private/snesimpl.h>       /*I   "petscsnes.h"   I*/
32e7541e6SPeter Brune #include <petscblaslapack.h>
4e7e93795SLois Curfman McInnes 
54a2ae208SSatish Balay #undef __FUNCT__
6a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSolution"
73f1db9ecSBarry Smith /*@C
8a6570f20SBarry Smith    SNESMonitorSolution - Monitors progress of the SNES solvers by calling
936851e7fSLois Curfman McInnes    VecView() for the approximate solution at each iteration.
103f1db9ecSBarry Smith 
113f1db9ecSBarry Smith    Collective on SNES
123f1db9ecSBarry Smith 
133f1db9ecSBarry Smith    Input Parameters:
143f1db9ecSBarry Smith +  snes - the SNES context
153f1db9ecSBarry Smith .  its - iteration number
164b27c08aSLois Curfman McInnes .  fgnorm - 2-norm of residual
173f1db9ecSBarry Smith -  dummy - either a viewer or PETSC_NULL
183f1db9ecSBarry Smith 
1936851e7fSLois Curfman McInnes    Level: intermediate
203f1db9ecSBarry Smith 
2136851e7fSLois Curfman McInnes .keywords: SNES, nonlinear, vector, monitor, view
223f1db9ecSBarry Smith 
23a6570f20SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorDefault(), VecView()
243f1db9ecSBarry Smith @*/
257087cfbeSBarry Smith PetscErrorCode  SNESMonitorSolution(SNES snes,PetscInt its,PetscReal fgnorm,void *dummy)
263f1db9ecSBarry Smith {
27dfbe8321SBarry Smith   PetscErrorCode ierr;
283f1db9ecSBarry Smith   Vec            x;
29b0a32e0cSBarry Smith   PetscViewer    viewer = (PetscViewer) dummy;
303f1db9ecSBarry Smith 
313f1db9ecSBarry Smith   PetscFunctionBegin;
323f1db9ecSBarry Smith   ierr = SNESGetSolution(snes,&x);CHKERRQ(ierr);
333f1db9ecSBarry Smith   if (!viewer) {
343f1db9ecSBarry Smith     MPI_Comm comm;
353f1db9ecSBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
36b0a32e0cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
373f1db9ecSBarry Smith   }
383f1db9ecSBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
393f1db9ecSBarry Smith 
403f1db9ecSBarry Smith   PetscFunctionReturn(0);
413f1db9ecSBarry Smith }
423f1db9ecSBarry Smith 
434a2ae208SSatish Balay #undef __FUNCT__
44a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorResidual"
455ed2d596SBarry Smith /*@C
46a6570f20SBarry Smith    SNESMonitorResidual - Monitors progress of the SNES solvers by calling
475ed2d596SBarry Smith    VecView() for the residual at each iteration.
485ed2d596SBarry Smith 
495ed2d596SBarry Smith    Collective on SNES
505ed2d596SBarry Smith 
515ed2d596SBarry Smith    Input Parameters:
525ed2d596SBarry Smith +  snes - the SNES context
535ed2d596SBarry Smith .  its - iteration number
544b27c08aSLois Curfman McInnes .  fgnorm - 2-norm of residual
555ed2d596SBarry Smith -  dummy - either a viewer or PETSC_NULL
565ed2d596SBarry Smith 
575ed2d596SBarry Smith    Level: intermediate
585ed2d596SBarry Smith 
595ed2d596SBarry Smith .keywords: SNES, nonlinear, vector, monitor, view
605ed2d596SBarry Smith 
61a6570f20SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorDefault(), VecView()
625ed2d596SBarry Smith @*/
637087cfbeSBarry Smith PetscErrorCode  SNESMonitorResidual(SNES snes,PetscInt its,PetscReal fgnorm,void *dummy)
645ed2d596SBarry Smith {
65dfbe8321SBarry Smith   PetscErrorCode ierr;
665ed2d596SBarry Smith   Vec            x;
675ed2d596SBarry Smith   PetscViewer    viewer = (PetscViewer) dummy;
685ed2d596SBarry Smith 
695ed2d596SBarry Smith   PetscFunctionBegin;
705ed2d596SBarry Smith   ierr = SNESGetFunction(snes,&x,0,0);CHKERRQ(ierr);
715ed2d596SBarry Smith   if (!viewer) {
725ed2d596SBarry Smith     MPI_Comm comm;
735ed2d596SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
745ed2d596SBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
755ed2d596SBarry Smith   }
765ed2d596SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
775ed2d596SBarry Smith 
785ed2d596SBarry Smith   PetscFunctionReturn(0);
795ed2d596SBarry Smith }
805ed2d596SBarry Smith 
815ed2d596SBarry Smith #undef __FUNCT__
82a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSolutionUpdate"
83d132466eSBarry Smith /*@C
84a6570f20SBarry Smith    SNESMonitorSolutionUpdate - Monitors progress of the SNES solvers by calling
85d132466eSBarry Smith    VecView() for the UPDATE to the solution at each iteration.
86d132466eSBarry Smith 
87d132466eSBarry Smith    Collective on SNES
88d132466eSBarry Smith 
89d132466eSBarry Smith    Input Parameters:
90d132466eSBarry Smith +  snes - the SNES context
91d132466eSBarry Smith .  its - iteration number
924b27c08aSLois Curfman McInnes .  fgnorm - 2-norm of residual
93d132466eSBarry Smith -  dummy - either a viewer or PETSC_NULL
94d132466eSBarry Smith 
95d132466eSBarry Smith    Level: intermediate
96d132466eSBarry Smith 
97d132466eSBarry Smith .keywords: SNES, nonlinear, vector, monitor, view
98d132466eSBarry Smith 
99a6570f20SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorDefault(), VecView()
100d132466eSBarry Smith @*/
1017087cfbeSBarry Smith PetscErrorCode  SNESMonitorSolutionUpdate(SNES snes,PetscInt its,PetscReal fgnorm,void *dummy)
102d132466eSBarry Smith {
103dfbe8321SBarry Smith   PetscErrorCode ierr;
104d132466eSBarry Smith   Vec            x;
105b0a32e0cSBarry Smith   PetscViewer    viewer = (PetscViewer) dummy;
106d132466eSBarry Smith 
107d132466eSBarry Smith   PetscFunctionBegin;
108d132466eSBarry Smith   ierr = SNESGetSolutionUpdate(snes,&x);CHKERRQ(ierr);
109d132466eSBarry Smith   if (!viewer) {
110d132466eSBarry Smith     MPI_Comm comm;
111d132466eSBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
112b0a32e0cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
113d132466eSBarry Smith   }
114d132466eSBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
115d132466eSBarry Smith 
116d132466eSBarry Smith   PetscFunctionReturn(0);
117d132466eSBarry Smith }
118d132466eSBarry Smith 
1194a2ae208SSatish Balay #undef __FUNCT__
120a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorDefault"
1214b828684SBarry Smith /*@C
122a6570f20SBarry Smith    SNESMonitorDefault - Monitors progress of the SNES solvers (default).
123e7e93795SLois Curfman McInnes 
124c7afd0dbSLois Curfman McInnes    Collective on SNES
125c7afd0dbSLois Curfman McInnes 
126e7e93795SLois Curfman McInnes    Input Parameters:
127c7afd0dbSLois Curfman McInnes +  snes - the SNES context
128e7e93795SLois Curfman McInnes .  its - iteration number
1294b27c08aSLois Curfman McInnes .  fgnorm - 2-norm of residual
130c7afd0dbSLois Curfman McInnes -  dummy - unused context
131fee21e36SBarry Smith 
132e7e93795SLois Curfman McInnes    Notes:
1334b27c08aSLois Curfman McInnes    This routine prints the residual norm at each iteration.
134e7e93795SLois Curfman McInnes 
13536851e7fSLois Curfman McInnes    Level: intermediate
13636851e7fSLois Curfman McInnes 
137e7e93795SLois Curfman McInnes .keywords: SNES, nonlinear, default, monitor, norm
138e7e93795SLois Curfman McInnes 
139a6570f20SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorSolution()
140e7e93795SLois Curfman McInnes @*/
1417087cfbeSBarry Smith PetscErrorCode  SNESMonitorDefault(SNES snes,PetscInt its,PetscReal fgnorm,void *dummy)
142e7e93795SLois Curfman McInnes {
143dfbe8321SBarry Smith   PetscErrorCode ierr;
144649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm);
145d132466eSBarry Smith 
1463a40ed3dSBarry Smith   PetscFunctionBegin;
147649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
148649052a6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %14.12e \n",its,(double)fgnorm);CHKERRQ(ierr);
149649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
1503a40ed3dSBarry Smith   PetscFunctionReturn(0);
151e7e93795SLois Curfman McInnes }
1523f1db9ecSBarry Smith 
153b271bb04SBarry Smith #undef __FUNCT__
1542e7541e6SPeter Brune #define __FUNCT__ "SNESMonitorJacUpdateSpectrum"
155*a80ad3e0SBarry Smith PetscErrorCode SNESMonitorJacUpdateSpectrum(SNES snes,PetscInt it,PetscReal fnorm,void *ctx)
156*a80ad3e0SBarry Smith {
157196da8b6SPeter Brune #if defined(PETSC_MISSING_LAPACK_GEEV)
15850c1c956SJed Brown   SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_SUP,"GEEV - Lapack routine is unavailable\nNot able to provide eigen values.");
159196da8b6SPeter Brune #elif defined(PETSC_HAVE_ESSL)
16050c1c956SJed Brown   SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_SUP,"GEEV - No support for ESSL Lapack Routines");
161196da8b6SPeter Brune #else
1622e7541e6SPeter Brune   Vec            X;
1632e7541e6SPeter Brune   Mat            J,dJ,dJdense;
1642e7541e6SPeter Brune   PetscErrorCode ierr;
1652e7541e6SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
1662e7541e6SPeter Brune   PetscInt       n,i;
1672e7541e6SPeter Brune   PetscBLASInt   nb,lwork;
1682e7541e6SPeter Brune   PetscReal      *eigr,*eigi;
1692e7541e6SPeter Brune   MatStructure   flg = DIFFERENT_NONZERO_PATTERN;
1702e7541e6SPeter Brune   PetscScalar    *work;
1712e7541e6SPeter Brune   PetscScalar    *a;
1722e7541e6SPeter Brune 
1732e7541e6SPeter Brune   PetscFunctionBegin;
1742e7541e6SPeter Brune   if (it == 0) PetscFunctionReturn(0);
1752e7541e6SPeter Brune   /* create the difference between the current update and the current jacobian */
1762e7541e6SPeter Brune   ierr = SNESGetSolution(snes,&X);CHKERRQ(ierr);
1772e7541e6SPeter Brune   ierr = SNESGetJacobian(snes,&J,PETSC_NULL,&func,PETSC_NULL);CHKERRQ(ierr);
1782e7541e6SPeter Brune   ierr = MatDuplicate(J,MAT_COPY_VALUES,&dJ);CHKERRQ(ierr);
1792e7541e6SPeter Brune   ierr = SNESComputeJacobian(snes,X,&dJ,&dJ,&flg);CHKERRQ(ierr);
1802e7541e6SPeter Brune   ierr = MatAXPY(dJ,-1.0,J,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
1812e7541e6SPeter Brune   /* compute the spectrum directly */
1822e7541e6SPeter Brune   ierr = MatConvert(dJ,MATSEQDENSE,MAT_INITIAL_MATRIX,&dJdense);CHKERRQ(ierr);
1832e7541e6SPeter Brune   ierr = MatGetSize(dJ,&n,PETSC_NULL);CHKERRQ(ierr);
1842e7541e6SPeter Brune   nb    = PetscBLASIntCast(n);
1852e7541e6SPeter Brune   lwork = 3*nb;
1862e7541e6SPeter Brune   ierr = PetscMalloc(n*sizeof(PetscReal),&eigr);CHKERRQ(ierr);
1872e7541e6SPeter Brune   ierr = PetscMalloc(n*sizeof(PetscReal),&eigi);CHKERRQ(ierr);
1882e7541e6SPeter Brune   ierr = PetscMalloc(lwork*sizeof(PetscScalar),&work);CHKERRQ(ierr);
1898c778c55SBarry Smith   ierr = MatDenseGetArray(dJdense,&a);CHKERRQ(ierr);
1902e7541e6SPeter Brune #if !defined(PETSC_USE_COMPLEX)
1912e7541e6SPeter Brune   {
1922e7541e6SPeter Brune     PetscBLASInt lierr;
1932e7541e6SPeter Brune     ierr = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr);
1942e7541e6SPeter Brune     LAPACKgeev_("N","N",&nb,a,&nb,eigr,eigi,PETSC_NULL,&nb,PETSC_NULL,&nb,work,&lwork,&lierr);
1952e7541e6SPeter Brune     if (lierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"geev() error %d",lierr);
1962e7541e6SPeter Brune     ierr = PetscFPTrapPop();CHKERRQ(ierr);
1972e7541e6SPeter Brune   }
1982e7541e6SPeter Brune #else
1992e7541e6SPeter Brune   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not coded for complex");
2002e7541e6SPeter Brune #endif
2012e7541e6SPeter Brune   PetscPrintf(((PetscObject)snes)->comm,"Eigenvalues of J_%d - J_%d:\n",it,it-1);CHKERRQ(ierr);
2022e7541e6SPeter Brune   for (i=0;i<n;i++) {
2032e7541e6SPeter Brune     PetscPrintf(((PetscObject)snes)->comm,"%5d: %20.5g + %20.5gi\n",i,eigr[i],eigi[i]);CHKERRQ(ierr);
2042e7541e6SPeter Brune   }
2058c778c55SBarry Smith   ierr = MatDenseRestoreArray(dJdense,&a);CHKERRQ(ierr);
2062e7541e6SPeter Brune   ierr = MatDestroy(&dJ);CHKERRQ(ierr);
2072e7541e6SPeter Brune   ierr = MatDestroy(&dJdense);CHKERRQ(ierr);
2082e7541e6SPeter Brune   ierr = PetscFree(eigr);CHKERRQ(ierr);
2092e7541e6SPeter Brune   ierr = PetscFree(eigi);CHKERRQ(ierr);
2102e7541e6SPeter Brune   ierr = PetscFree(work);CHKERRQ(ierr);
2112e7541e6SPeter Brune   PetscFunctionReturn(0);
212196da8b6SPeter Brune #endif
2132e7541e6SPeter Brune }
2142e7541e6SPeter Brune 
2152e7541e6SPeter Brune #undef __FUNCT__
216b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorRange_Private"
2177087cfbeSBarry Smith PetscErrorCode  SNESMonitorRange_Private(SNES snes,PetscInt it,PetscReal *per)
218b271bb04SBarry Smith {
219b271bb04SBarry Smith   PetscErrorCode          ierr;
220b271bb04SBarry Smith   Vec                     resid;
221b271bb04SBarry Smith   PetscReal               rmax,pwork;
222b271bb04SBarry Smith   PetscInt                i,n,N;
223b271bb04SBarry Smith   PetscScalar             *r;
224b271bb04SBarry Smith 
225b271bb04SBarry Smith   PetscFunctionBegin;
226b271bb04SBarry Smith   ierr = SNESGetFunction(snes,&resid,0,0);CHKERRQ(ierr);
227b271bb04SBarry Smith   ierr = VecNorm(resid,NORM_INFINITY,&rmax);CHKERRQ(ierr);
228b271bb04SBarry Smith   ierr = VecGetLocalSize(resid,&n);CHKERRQ(ierr);
229b271bb04SBarry Smith   ierr = VecGetSize(resid,&N);CHKERRQ(ierr);
230b271bb04SBarry Smith   ierr = VecGetArray(resid,&r);CHKERRQ(ierr);
231b271bb04SBarry Smith   pwork = 0.0;
232b271bb04SBarry Smith   for (i=0; i<n; i++) {
233b271bb04SBarry Smith     pwork += (PetscAbsScalar(r[i]) > .20*rmax);
234b271bb04SBarry Smith   }
23506a205a8SBarry Smith   ierr = MPI_Allreduce(&pwork,per,1,MPIU_REAL,MPIU_SUM,((PetscObject)snes)->comm);CHKERRQ(ierr);
236b271bb04SBarry Smith   ierr = VecRestoreArray(resid,&r);CHKERRQ(ierr);
237b271bb04SBarry Smith   *per  = *per/N;
238b271bb04SBarry Smith   PetscFunctionReturn(0);
239b271bb04SBarry Smith }
240b271bb04SBarry Smith 
241b271bb04SBarry Smith #undef __FUNCT__
242b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorRange"
243b271bb04SBarry Smith /*@C
244b271bb04SBarry Smith    SNESMonitorRange - Prints the percentage of residual elements that are more then 10 percent of the maximum value.
245b271bb04SBarry Smith 
246b271bb04SBarry Smith    Collective on SNES
247b271bb04SBarry Smith 
248b271bb04SBarry Smith    Input Parameters:
249b271bb04SBarry Smith +  snes   - iterative context
250b271bb04SBarry Smith .  it    - iteration number
251b271bb04SBarry Smith .  rnorm - 2-norm (preconditioned) residual value (may be estimated).
252b271bb04SBarry Smith -  dummy - unused monitor context
253b271bb04SBarry Smith 
254b271bb04SBarry Smith    Options Database Key:
255b271bb04SBarry Smith .  -snes_monitor_range - Activates SNESMonitorRange()
256b271bb04SBarry Smith 
257b271bb04SBarry Smith    Level: intermediate
258b271bb04SBarry Smith 
259b271bb04SBarry Smith .keywords: SNES, default, monitor, residual
260b271bb04SBarry Smith 
261b271bb04SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorDefault(), SNESMonitorLGCreate()
262b271bb04SBarry Smith @*/
2637087cfbeSBarry Smith PetscErrorCode  SNESMonitorRange(SNES snes,PetscInt it,PetscReal rnorm,void *dummy)
264b271bb04SBarry Smith {
265b271bb04SBarry Smith   PetscErrorCode   ierr;
266b271bb04SBarry Smith   PetscReal        perc,rel;
267649052a6SBarry Smith   PetscViewer      viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm);
268b271bb04SBarry Smith   /* should be in a MonitorRangeContext */
269b271bb04SBarry Smith   static PetscReal prev;
270b271bb04SBarry Smith 
271b271bb04SBarry Smith   PetscFunctionBegin;
272b271bb04SBarry Smith   if (!it) prev = rnorm;
273b271bb04SBarry Smith   ierr = SNESMonitorRange_Private(snes,it,&perc);CHKERRQ(ierr);
274b271bb04SBarry Smith 
275b271bb04SBarry Smith   rel  = (prev - rnorm)/prev;
276b271bb04SBarry Smith   prev = rnorm;
277649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
278649052a6SBarry 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);
279649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
280b271bb04SBarry Smith   PetscFunctionReturn(0);
281b271bb04SBarry Smith }
282b271bb04SBarry Smith 
283eabae89aSBarry Smith typedef struct {
284649052a6SBarry Smith   PetscViewer viewer;
285eabae89aSBarry Smith   PetscReal   *history;
286a6570f20SBarry Smith } SNESMonitorRatioContext;
287eabae89aSBarry Smith 
2883a7fca6bSBarry Smith #undef __FUNCT__
289a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorRatio"
2903a7fca6bSBarry Smith /*@C
291a6570f20SBarry Smith    SNESMonitorRatio - Monitors progress of the SNES solvers by printing the ratio
2924b27c08aSLois Curfman McInnes    of residual norm at each iteration to the previous.
2933a7fca6bSBarry Smith 
2943a7fca6bSBarry Smith    Collective on SNES
2953a7fca6bSBarry Smith 
2963a7fca6bSBarry Smith    Input Parameters:
2973a7fca6bSBarry Smith +  snes - the SNES context
2983a7fca6bSBarry Smith .  its - iteration number
2993a7fca6bSBarry Smith .  fgnorm - 2-norm of residual (or gradient)
300eabae89aSBarry Smith -  dummy -  context of monitor
3013a7fca6bSBarry Smith 
3023a7fca6bSBarry Smith    Level: intermediate
3033a7fca6bSBarry Smith 
3043a7fca6bSBarry Smith .keywords: SNES, nonlinear, monitor, norm
3053a7fca6bSBarry Smith 
306a6570f20SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorSolution()
3073a7fca6bSBarry Smith @*/
3087087cfbeSBarry Smith PetscErrorCode  SNESMonitorRatio(SNES snes,PetscInt its,PetscReal fgnorm,void *dummy)
3093a7fca6bSBarry Smith {
310dfbe8321SBarry Smith   PetscErrorCode          ierr;
31177431f27SBarry Smith   PetscInt                len;
31287828ca2SBarry Smith   PetscReal               *history;
313a6570f20SBarry Smith   SNESMonitorRatioContext *ctx = (SNESMonitorRatioContext*)dummy;
3143a7fca6bSBarry Smith 
3153a7fca6bSBarry Smith   PetscFunctionBegin;
3163a7fca6bSBarry Smith   ierr = SNESGetConvergenceHistory(snes,&history,PETSC_NULL,&len);CHKERRQ(ierr);
317649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(ctx->viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
318958c9bccSBarry Smith   if (!its || !history || its > len) {
319649052a6SBarry Smith     ierr = PetscViewerASCIIPrintf(ctx->viewer,"%3D SNES Function norm %14.12e \n",its,(double)fgnorm);CHKERRQ(ierr);
3203a7fca6bSBarry Smith   } else {
32187828ca2SBarry Smith     PetscReal ratio = fgnorm/history[its-1];
3228f1a2a5eSBarry Smith     ierr = PetscViewerASCIIPrintf(ctx->viewer,"%3D SNES Function norm %14.12e %14.12e \n",its,(double)fgnorm,(double)ratio);CHKERRQ(ierr);
3233a7fca6bSBarry Smith   }
324649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(ctx->viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
3253a7fca6bSBarry Smith   PetscFunctionReturn(0);
3263a7fca6bSBarry Smith }
3273a7fca6bSBarry Smith 
3283a7fca6bSBarry Smith /*
3293a7fca6bSBarry Smith    If the we set the history monitor space then we must destroy it
3303a7fca6bSBarry Smith */
3313a7fca6bSBarry Smith #undef __FUNCT__
332a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorRatioDestroy"
333c2efdce3SBarry Smith PetscErrorCode SNESMonitorRatioDestroy(void **ct)
3343a7fca6bSBarry Smith {
335dfbe8321SBarry Smith   PetscErrorCode          ierr;
336c2efdce3SBarry Smith   SNESMonitorRatioContext *ctx = *(SNESMonitorRatioContext**)ct;
3373a7fca6bSBarry Smith 
3383a7fca6bSBarry Smith   PetscFunctionBegin;
33905b42c5fSBarry Smith   ierr = PetscFree(ctx->history);CHKERRQ(ierr);
340649052a6SBarry Smith   ierr = PetscViewerDestroy(&ctx->viewer);CHKERRQ(ierr);
341eabae89aSBarry Smith   ierr = PetscFree(ctx);CHKERRQ(ierr);
3423a7fca6bSBarry Smith   PetscFunctionReturn(0);
3433a7fca6bSBarry Smith }
3443a7fca6bSBarry Smith 
3453a7fca6bSBarry Smith #undef __FUNCT__
346a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSetRatio"
3473a7fca6bSBarry Smith /*@C
348a6570f20SBarry Smith    SNESMonitorSetRatio - Sets SNES to use a monitor that prints the
3494b27c08aSLois Curfman McInnes    ratio of the function norm at each iteration.
3503a7fca6bSBarry Smith 
3513a7fca6bSBarry Smith    Collective on SNES
3523a7fca6bSBarry Smith 
3533a7fca6bSBarry Smith    Input Parameters:
354eabae89aSBarry Smith +   snes - the SNES context
355eabae89aSBarry Smith -   viewer - ASCII viewer to print output
3563a7fca6bSBarry Smith 
3573a7fca6bSBarry Smith    Level: intermediate
3583a7fca6bSBarry Smith 
3593a7fca6bSBarry Smith .keywords: SNES, nonlinear, monitor, norm
3603a7fca6bSBarry Smith 
361a6570f20SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorSolution(), SNESMonitorDefault()
3623a7fca6bSBarry Smith @*/
363649052a6SBarry Smith PetscErrorCode  SNESMonitorSetRatio(SNES snes,PetscViewer viewer)
3643a7fca6bSBarry Smith {
365dfbe8321SBarry Smith   PetscErrorCode          ierr;
366a6570f20SBarry Smith   SNESMonitorRatioContext *ctx;
36787828ca2SBarry Smith   PetscReal               *history;
3683a7fca6bSBarry Smith 
3693a7fca6bSBarry Smith   PetscFunctionBegin;
370eabae89aSBarry Smith   if (!viewer) {
371649052a6SBarry Smith     ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,"stdout",&viewer);CHKERRQ(ierr);
372eabae89aSBarry Smith     ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr);
373eabae89aSBarry Smith   }
37438f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESMonitorRatioContext,&ctx);
3753a7fca6bSBarry Smith   ierr = SNESGetConvergenceHistory(snes,&history,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
3763a7fca6bSBarry Smith   if (!history) {
377eabae89aSBarry Smith     ierr = PetscMalloc(100*sizeof(PetscReal),&ctx->history);CHKERRQ(ierr);
378eabae89aSBarry Smith     ierr = SNESSetConvergenceHistory(snes,ctx->history,0,100,PETSC_TRUE);CHKERRQ(ierr);
3793a7fca6bSBarry Smith   }
380eabae89aSBarry Smith   ctx->viewer = viewer;
381a6570f20SBarry Smith   ierr = SNESMonitorSet(snes,SNESMonitorRatio,ctx,SNESMonitorRatioDestroy);CHKERRQ(ierr);
3823a7fca6bSBarry Smith   PetscFunctionReturn(0);
3833a7fca6bSBarry Smith }
3843a7fca6bSBarry Smith 
385e7e93795SLois Curfman McInnes /* ---------------------------------------------------------------- */
3864a2ae208SSatish Balay #undef __FUNCT__
387a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorDefaultShort"
388be1f7002SBarry Smith /*
389a6570f20SBarry Smith      Default (short) SNES Monitor, same as SNESMonitorDefault() except
390be1f7002SBarry Smith   it prints fewer digits of the residual as the residual gets smaller.
391be1f7002SBarry Smith   This is because the later digits are meaningless and are often
392be1f7002SBarry Smith   different on different machines; by using this routine different
393be1f7002SBarry Smith   machines will usually generate the same output.
394be1f7002SBarry Smith */
3957087cfbeSBarry Smith PetscErrorCode  SNESMonitorDefaultShort(SNES snes,PetscInt its,PetscReal fgnorm,void *dummy)
396e7e93795SLois Curfman McInnes {
397dfbe8321SBarry Smith   PetscErrorCode ierr;
398649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm);
399d132466eSBarry Smith 
4003a40ed3dSBarry Smith   PetscFunctionBegin;
401649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
4028f240d10SBarry Smith   if (fgnorm > 1.e-9) {
403649052a6SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %G \n",its,fgnorm);CHKERRQ(ierr);
4043a40ed3dSBarry Smith   } else if (fgnorm > 1.e-11){
405649052a6SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %5.3e \n",its,fgnorm);CHKERRQ(ierr);
4063a40ed3dSBarry Smith   } else {
407649052a6SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm < 1.e-11\n",its);CHKERRQ(ierr);
408a34d58ebSBarry Smith   }
409649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
4103a40ed3dSBarry Smith   PetscFunctionReturn(0);
411e7e93795SLois Curfman McInnes }
412e7e93795SLois Curfman McInnes /* ---------------------------------------------------------------- */
4134a2ae208SSatish Balay #undef __FUNCT__
4143f149594SLisandro Dalcin #define __FUNCT__ "SNESDefaultConverged"
4154b828684SBarry Smith /*@C
4163f149594SLisandro Dalcin    SNESDefaultConverged - Convergence test of the solvers for
417f525115eSLois Curfman McInnes    systems of nonlinear equations (default).
418e7e93795SLois Curfman McInnes 
419c7afd0dbSLois Curfman McInnes    Collective on SNES
420c7afd0dbSLois Curfman McInnes 
421e7e93795SLois Curfman McInnes    Input Parameters:
422c7afd0dbSLois Curfman McInnes +  snes - the SNES context
42306ee9f85SBarry Smith .  it - the iteration (0 indicates before any Newton steps)
424e7e93795SLois Curfman McInnes .  xnorm - 2-norm of current iterate
425c60f73f4SPeter Brune .  snorm - 2-norm of current step
4267f3332b4SBarry Smith .  fnorm - 2-norm of function at current iterate
427c7afd0dbSLois Curfman McInnes -  dummy - unused context
428e7e93795SLois Curfman McInnes 
429184914b5SBarry Smith    Output Parameter:
430184914b5SBarry Smith .   reason  - one of
43170441072SBarry Smith $  SNES_CONVERGED_FNORM_ABS       - (fnorm < abstol),
432c60f73f4SPeter Brune $  SNES_CONVERGED_SNORM_RELATIVE  - (snorm < stol*xnorm),
433184914b5SBarry Smith $  SNES_CONVERGED_FNORM_RELATIVE  - (fnorm < rtol*fnorm0),
434184914b5SBarry Smith $  SNES_DIVERGED_FUNCTION_COUNT   - (nfct > maxf),
435184914b5SBarry Smith $  SNES_DIVERGED_FNORM_NAN        - (fnorm == NaN),
436184914b5SBarry Smith $  SNES_CONVERGED_ITERATING       - (otherwise),
437e7e93795SLois Curfman McInnes 
438e7e93795SLois Curfman McInnes    where
439c7afd0dbSLois Curfman McInnes +    maxf - maximum number of function evaluations,
440c7afd0dbSLois Curfman McInnes             set with SNESSetTolerances()
441c7afd0dbSLois Curfman McInnes .    nfct - number of function evaluations,
44270441072SBarry Smith .    abstol - absolute function norm tolerance,
443c7afd0dbSLois Curfman McInnes             set with SNESSetTolerances()
444c7afd0dbSLois Curfman McInnes -    rtol - relative function norm tolerance, set with SNESSetTolerances()
445fee21e36SBarry Smith 
44636851e7fSLois Curfman McInnes    Level: intermediate
44736851e7fSLois Curfman McInnes 
448e7e93795SLois Curfman McInnes .keywords: SNES, nonlinear, default, converged, convergence
449e7e93795SLois Curfman McInnes 
45071f87433Sdalcinl .seealso: SNESSetConvergenceTest()
451e7e93795SLois Curfman McInnes @*/
452c60f73f4SPeter Brune PetscErrorCode  SNESDefaultConverged(SNES snes,PetscInt it,PetscReal xnorm,PetscReal snorm,PetscReal fnorm,SNESConvergedReason *reason,void *dummy)
453e7e93795SLois Curfman McInnes {
45463ba0a88SBarry Smith   PetscErrorCode ierr;
45563ba0a88SBarry Smith 
4563a40ed3dSBarry Smith   PetscFunctionBegin;
4570700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
4583f149594SLisandro Dalcin   PetscValidPointer(reason,6);
4593f149594SLisandro Dalcin 
46006ee9f85SBarry Smith   *reason = SNES_CONVERGED_ITERATING;
46106ee9f85SBarry Smith 
46206ee9f85SBarry Smith   if (!it) {
46306ee9f85SBarry Smith     /* set parameter for default relative tolerance convergence test */
46406ee9f85SBarry Smith     snes->ttol = fnorm*snes->rtol;
46506ee9f85SBarry Smith   }
4668146f6ebSBarry Smith   if (PetscIsInfOrNanReal(fnorm)) {
467ae15b995SBarry Smith     ierr = PetscInfo(snes,"Failed to converged, function norm is NaN\n");CHKERRQ(ierr);
468184914b5SBarry Smith     *reason = SNES_DIVERGED_FNORM_NAN;
46970441072SBarry Smith   } else if (fnorm < snes->abstol) {
4708f1a2a5eSBarry Smith     ierr = PetscInfo2(snes,"Converged due to function norm %14.12e < %14.12e\n",(double)fnorm,(double)snes->abstol);CHKERRQ(ierr);
471184914b5SBarry Smith     *reason = SNES_CONVERGED_FNORM_ABS;
47243e71028SBarry Smith   } else if (snes->nfuncs >= snes->max_funcs) {
473ae15b995SBarry Smith     ierr = PetscInfo2(snes,"Exceeded maximum number of function evaluations: %D > %D\n",snes->nfuncs,snes->max_funcs);CHKERRQ(ierr);
474184914b5SBarry Smith     *reason = SNES_DIVERGED_FUNCTION_COUNT;
47506ee9f85SBarry Smith   }
47606ee9f85SBarry Smith 
47706ee9f85SBarry Smith   if (it && !*reason) {
47806ee9f85SBarry Smith     if (fnorm <= snes->ttol) {
4798f1a2a5eSBarry Smith       ierr = PetscInfo2(snes,"Converged due to function norm %14.12e < %14.12e (relative tolerance)\n",(double)fnorm,(double)snes->ttol);CHKERRQ(ierr);
48006ee9f85SBarry Smith       *reason = SNES_CONVERGED_FNORM_RELATIVE;
481c60f73f4SPeter Brune     } else if (snorm < snes->stol*xnorm) {
482c60f73f4SPeter 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);
483c60f73f4SPeter Brune       *reason = SNES_CONVERGED_SNORM_RELATIVE;
48406ee9f85SBarry Smith     }
485e7e93795SLois Curfman McInnes   }
4863a40ed3dSBarry Smith   PetscFunctionReturn(0);
487e7e93795SLois Curfman McInnes }
4883f149594SLisandro Dalcin 
4893f149594SLisandro Dalcin #undef __FUNCT__
4903f149594SLisandro Dalcin #define __FUNCT__ "SNESSkipConverged"
4913f149594SLisandro Dalcin /*@C
4923f149594SLisandro Dalcin    SNESSkipConverged - Convergence test for SNES that NEVER returns as
4933f149594SLisandro Dalcin    converged, UNLESS the maximum number of iteration have been reached.
4943f149594SLisandro Dalcin 
4953f9fe445SBarry Smith    Logically Collective on SNES
4963f149594SLisandro Dalcin 
4973f149594SLisandro Dalcin    Input Parameters:
4983f149594SLisandro Dalcin +  snes - the SNES context
4993f149594SLisandro Dalcin .  it - the iteration (0 indicates before any Newton steps)
5003f149594SLisandro Dalcin .  xnorm - 2-norm of current iterate
501c60f73f4SPeter Brune .  snorm - 2-norm of current step
5023f149594SLisandro Dalcin .  fnorm - 2-norm of function at current iterate
5033f149594SLisandro Dalcin -  dummy - unused context
5043f149594SLisandro Dalcin 
5053f149594SLisandro Dalcin    Output Parameter:
50685385478SLisandro Dalcin .   reason  - SNES_CONVERGED_ITERATING, SNES_CONVERGED_ITS, or SNES_DIVERGED_FNORM_NAN
5073f149594SLisandro Dalcin 
5083f149594SLisandro Dalcin    Notes:
5093f149594SLisandro Dalcin    Convergence is then declared after a fixed number of iterations have been used.
5103f149594SLisandro Dalcin 
5113f149594SLisandro Dalcin    Level: advanced
5123f149594SLisandro Dalcin 
5133f149594SLisandro Dalcin .keywords: SNES, nonlinear, skip, converged, convergence
5143f149594SLisandro Dalcin 
5153f149594SLisandro Dalcin .seealso: SNESSetConvergenceTest()
5163f149594SLisandro Dalcin @*/
517c60f73f4SPeter Brune PetscErrorCode  SNESSkipConverged(SNES snes,PetscInt it,PetscReal xnorm,PetscReal snorm,PetscReal fnorm,SNESConvergedReason *reason,void *dummy)
5183f149594SLisandro Dalcin {
5193f149594SLisandro Dalcin   PetscErrorCode ierr;
5203f149594SLisandro Dalcin 
5213f149594SLisandro Dalcin   PetscFunctionBegin;
5220700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5233f149594SLisandro Dalcin   PetscValidPointer(reason,6);
5243f149594SLisandro Dalcin 
5253f149594SLisandro Dalcin   *reason = SNES_CONVERGED_ITERATING;
5263f149594SLisandro Dalcin 
5273f149594SLisandro Dalcin   if (fnorm != fnorm) {
5283f149594SLisandro Dalcin     ierr = PetscInfo(snes,"Failed to converged, function norm is NaN\n");CHKERRQ(ierr);
5293f149594SLisandro Dalcin     *reason = SNES_DIVERGED_FNORM_NAN;
5303f149594SLisandro Dalcin   } else if (it == snes->max_its) {
5313f149594SLisandro Dalcin     *reason = SNES_CONVERGED_ITS;
5323f149594SLisandro Dalcin   }
5333f149594SLisandro Dalcin   PetscFunctionReturn(0);
5343f149594SLisandro Dalcin }
5353f149594SLisandro Dalcin 
53658c9b817SLisandro Dalcin #undef __FUNCT__
53758c9b817SLisandro Dalcin #define __FUNCT__ "SNESDefaultGetWork"
53898acb6afSMatthew G Knepley /*@
53958c9b817SLisandro Dalcin   SNESDefaultGetWork - Gets a number of work vectors.
54058c9b817SLisandro Dalcin 
54158c9b817SLisandro Dalcin   Input Parameters:
54258c9b817SLisandro Dalcin . snes  - the SNES context
54358c9b817SLisandro Dalcin . nw - number of work vectors to allocate
54458c9b817SLisandro Dalcin 
54558c9b817SLisandro Dalcin    Level: developer
54658c9b817SLisandro Dalcin 
54758c9b817SLisandro Dalcin   Notes:
54858c9b817SLisandro Dalcin   Call this only if no work vectors have been allocated
54998acb6afSMatthew G Knepley @*/
55058c9b817SLisandro Dalcin PetscErrorCode SNESDefaultGetWork(SNES snes,PetscInt nw)
55158c9b817SLisandro Dalcin {
55258c9b817SLisandro Dalcin   PetscErrorCode ierr;
55358c9b817SLisandro Dalcin 
55458c9b817SLisandro Dalcin   PetscFunctionBegin;
55558c9b817SLisandro Dalcin   if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);}
55658c9b817SLisandro Dalcin   snes->nwork = nw;
55758c9b817SLisandro Dalcin   ierr = VecDuplicateVecs(snes->vec_sol,snes->nwork,&snes->work);CHKERRQ(ierr);
55858c9b817SLisandro Dalcin   ierr = PetscLogObjectParents(snes,nw,snes->work);CHKERRQ(ierr);
55958c9b817SLisandro Dalcin   PetscFunctionReturn(0);
56058c9b817SLisandro Dalcin }
561