xref: /petsc/src/snes/interface/snesut.c (revision a2b725a8db0d6bf6cc2a1c6df7dd8029aadfff6e)
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 
63f1db9ecSBarry Smith /*@C
7a6570f20SBarry Smith    SNESMonitorSolution - Monitors progress of the SNES solvers by calling
836851e7fSLois Curfman McInnes    VecView() for the approximate solution at each iteration.
93f1db9ecSBarry Smith 
103f1db9ecSBarry Smith    Collective on SNES
113f1db9ecSBarry Smith 
123f1db9ecSBarry Smith    Input Parameters:
133f1db9ecSBarry Smith +  snes - the SNES context
143f1db9ecSBarry Smith .  its - iteration number
154b27c08aSLois Curfman McInnes .  fgnorm - 2-norm of residual
16f55353a2SBarry Smith -  dummy -  a viewer
173f1db9ecSBarry Smith 
18ee32d87aSMatthew G. Knepley    Options Database Keys:
19ee32d87aSMatthew G. Knepley .  -snes_monitor_solution [ascii binary draw][:filename][:viewer format] - plots solution at each iteration
20ee32d87aSMatthew G. Knepley 
2136851e7fSLois Curfman McInnes    Level: intermediate
223f1db9ecSBarry Smith 
2336851e7fSLois Curfman McInnes .keywords: SNES, nonlinear, vector, monitor, view
243f1db9ecSBarry Smith 
25a6570f20SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorDefault(), VecView()
263f1db9ecSBarry Smith @*/
27d43b4f6eSBarry Smith PetscErrorCode  SNESMonitorSolution(SNES snes,PetscInt its,PetscReal fgnorm,PetscViewerAndFormat *vf)
283f1db9ecSBarry Smith {
29dfbe8321SBarry Smith   PetscErrorCode ierr;
303f1db9ecSBarry Smith   Vec            x;
31d43b4f6eSBarry Smith   PetscViewer    viewer = vf->viewer;
323f1db9ecSBarry Smith 
333f1db9ecSBarry Smith   PetscFunctionBegin;
344d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
353f1db9ecSBarry Smith   ierr = SNESGetSolution(snes,&x);CHKERRQ(ierr);
36d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
373f1db9ecSBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
38d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
393f1db9ecSBarry Smith   PetscFunctionReturn(0);
403f1db9ecSBarry Smith }
413f1db9ecSBarry Smith 
425ed2d596SBarry Smith /*@C
43a6570f20SBarry Smith    SNESMonitorResidual - Monitors progress of the SNES solvers by calling
445ed2d596SBarry Smith    VecView() for the residual at each iteration.
455ed2d596SBarry Smith 
465ed2d596SBarry Smith    Collective on SNES
475ed2d596SBarry Smith 
485ed2d596SBarry Smith    Input Parameters:
495ed2d596SBarry Smith +  snes - the SNES context
505ed2d596SBarry Smith .  its - iteration number
514b27c08aSLois Curfman McInnes .  fgnorm - 2-norm of residual
52f55353a2SBarry Smith -  dummy -  a viewer
535ed2d596SBarry Smith 
54ee32d87aSMatthew G. Knepley    Options Database Keys:
55ee32d87aSMatthew G. Knepley .  -snes_monitor_residual [ascii binary draw][:filename][:viewer format] - plots residual (not its norm) at each iteration
56ee32d87aSMatthew G. Knepley 
575ed2d596SBarry Smith    Level: intermediate
585ed2d596SBarry Smith 
595ed2d596SBarry Smith .keywords: SNES, nonlinear, vector, monitor, view
605ed2d596SBarry Smith 
61a6570f20SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorDefault(), VecView()
625ed2d596SBarry Smith @*/
63d43b4f6eSBarry Smith PetscErrorCode  SNESMonitorResidual(SNES snes,PetscInt its,PetscReal fgnorm,PetscViewerAndFormat *vf)
645ed2d596SBarry Smith {
65dfbe8321SBarry Smith   PetscErrorCode ierr;
665ed2d596SBarry Smith   Vec            x;
67d43b4f6eSBarry Smith   PetscViewer    viewer = vf->viewer;
685ed2d596SBarry Smith 
695ed2d596SBarry Smith   PetscFunctionBegin;
704d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
715ed2d596SBarry Smith   ierr = SNESGetFunction(snes,&x,0,0);CHKERRQ(ierr);
72d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
735ed2d596SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
74d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
755ed2d596SBarry Smith   PetscFunctionReturn(0);
765ed2d596SBarry Smith }
775ed2d596SBarry Smith 
78d132466eSBarry Smith /*@C
79a6570f20SBarry Smith    SNESMonitorSolutionUpdate - Monitors progress of the SNES solvers by calling
80d132466eSBarry Smith    VecView() for the UPDATE to the solution at each iteration.
81d132466eSBarry Smith 
82d132466eSBarry Smith    Collective on SNES
83d132466eSBarry Smith 
84d132466eSBarry Smith    Input Parameters:
85d132466eSBarry Smith +  snes - the SNES context
86d132466eSBarry Smith .  its - iteration number
874b27c08aSLois Curfman McInnes .  fgnorm - 2-norm of residual
88f55353a2SBarry Smith -  dummy - a viewer
89d132466eSBarry Smith 
90ee32d87aSMatthew G. Knepley    Options Database Keys:
91ee32d87aSMatthew G. Knepley .  -snes_monitor_solution_update [ascii binary draw][:filename][:viewer format] - plots update to solution at each iteration
92ee32d87aSMatthew G. Knepley 
93d132466eSBarry Smith    Level: intermediate
94d132466eSBarry Smith 
95d132466eSBarry Smith .keywords: SNES, nonlinear, vector, monitor, view
96d132466eSBarry Smith 
97a6570f20SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorDefault(), VecView()
98d132466eSBarry Smith @*/
99d43b4f6eSBarry Smith PetscErrorCode  SNESMonitorSolutionUpdate(SNES snes,PetscInt its,PetscReal fgnorm,PetscViewerAndFormat *vf)
100d132466eSBarry Smith {
101dfbe8321SBarry Smith   PetscErrorCode ierr;
102d132466eSBarry Smith   Vec            x;
103d43b4f6eSBarry Smith   PetscViewer    viewer = vf->viewer;
104d132466eSBarry Smith 
105d132466eSBarry Smith   PetscFunctionBegin;
1064d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
107d132466eSBarry Smith   ierr = SNESGetSolutionUpdate(snes,&x);CHKERRQ(ierr);
108d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
109d132466eSBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
110d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
111d132466eSBarry Smith   PetscFunctionReturn(0);
112d132466eSBarry Smith }
113d132466eSBarry Smith 
114a5c2985bSBarry Smith /*@C
115a5c2985bSBarry Smith    KSPMonitorSNES - Print the residual norm of the nonlinear function at each iteration of the linear iterative solver.
116a5c2985bSBarry Smith 
117a5c2985bSBarry Smith    Collective on KSP
118a5c2985bSBarry Smith 
119a5c2985bSBarry Smith    Input Parameters:
120a5c2985bSBarry Smith +  ksp   - iterative context
121a5c2985bSBarry Smith .  n     - iteration number
122a5c2985bSBarry Smith .  rnorm - 2-norm (preconditioned) residual value (may be estimated).
123a5c2985bSBarry Smith -  dummy - unused monitor context
124a5c2985bSBarry Smith 
125a5c2985bSBarry Smith    Level: intermediate
126a5c2985bSBarry Smith 
127a5c2985bSBarry Smith .keywords: KSP, default, monitor, residual
128a5c2985bSBarry Smith 
129a5c2985bSBarry Smith .seealso: KSPMonitorSet(), KSPMonitorTrueResidualNorm(), KSPMonitorLGResidualNormCreate()
130a5c2985bSBarry Smith @*/
131a5c2985bSBarry Smith PetscErrorCode  KSPMonitorSNES(KSP ksp,PetscInt n,PetscReal rnorm,void *dummy)
132a5c2985bSBarry Smith {
133a5c2985bSBarry Smith   PetscErrorCode ierr;
134a5c2985bSBarry Smith   PetscViewer    viewer;
135a5c2985bSBarry Smith   SNES           snes = (SNES) dummy;
136a5c2985bSBarry Smith   Vec            snes_solution,work1,work2;
137a5c2985bSBarry Smith   PetscReal      snorm;
138a5c2985bSBarry Smith 
139a5c2985bSBarry Smith   PetscFunctionBegin;
140a5c2985bSBarry Smith   ierr = SNESGetSolution(snes,&snes_solution);CHKERRQ(ierr);
141a5c2985bSBarry Smith   ierr = VecDuplicate(snes_solution,&work1);CHKERRQ(ierr);
142a5c2985bSBarry Smith   ierr = VecDuplicate(snes_solution,&work2);CHKERRQ(ierr);
143a5c2985bSBarry Smith   ierr = KSPBuildSolution(ksp,work1,NULL);CHKERRQ(ierr);
144a5c2985bSBarry Smith   ierr = VecAYPX(work1,-1.0,snes_solution);CHKERRQ(ierr);
145a5c2985bSBarry Smith   ierr = SNESComputeFunction(snes,work1,work2);CHKERRQ(ierr);
146a5c2985bSBarry Smith   ierr = VecNorm(work2,NORM_2,&snorm);CHKERRQ(ierr);
147a5c2985bSBarry Smith   ierr = VecDestroy(&work1);CHKERRQ(ierr);
148a5c2985bSBarry Smith   ierr = VecDestroy(&work2);CHKERRQ(ierr);
149a5c2985bSBarry Smith 
150a5c2985bSBarry Smith   ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ksp),&viewer);CHKERRQ(ierr);
151a5c2985bSBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ksp)->tablevel);CHKERRQ(ierr);
152a5c2985bSBarry Smith   if (n == 0 && ((PetscObject)ksp)->prefix) {
153a5c2985bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  Residual norms for %s solve.\n",((PetscObject)ksp)->prefix);CHKERRQ(ierr);
154a5c2985bSBarry Smith   }
155a5c2985bSBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Residual norm %5.3e KSP Residual norm %5.3e \n",n,(double)snorm,(double)rnorm);CHKERRQ(ierr);
156a5c2985bSBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ksp)->tablevel);CHKERRQ(ierr);
157a5c2985bSBarry Smith   PetscFunctionReturn(0);
158a5c2985bSBarry Smith }
159a5c2985bSBarry Smith 
160e5f7ee39SBarry Smith #include <petscdraw.h>
161e5f7ee39SBarry Smith 
162e5f7ee39SBarry Smith /*@C
163e5f7ee39SBarry Smith    KSPMonitorSNESLGResidualNormCreate - Creates a line graph context for use with
164e5f7ee39SBarry Smith    KSP to monitor convergence of preconditioned residual norms.
165e5f7ee39SBarry Smith 
166e5f7ee39SBarry Smith    Collective on KSP
167e5f7ee39SBarry Smith 
168e5f7ee39SBarry Smith    Input Parameters:
1698b0b5a47SLisandro Dalcin +  comm - communicator context
1708b0b5a47SLisandro Dalcin .  host - the X display to open, or null for the local machine
171e5f7ee39SBarry Smith .  label - the title to put in the title bar
172e5f7ee39SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of
173e5f7ee39SBarry Smith           the window
174e5f7ee39SBarry Smith -  m, n - the screen width and height in pixels
175e5f7ee39SBarry Smith 
176e5f7ee39SBarry Smith    Output Parameter:
177e5f7ee39SBarry Smith .  draw - the drawing context
178e5f7ee39SBarry Smith 
179e5f7ee39SBarry Smith    Options Database Key:
180e5f7ee39SBarry Smith .  -ksp_monitor_lg_residualnorm - Sets line graph monitor
181e5f7ee39SBarry Smith 
182e5f7ee39SBarry Smith    Notes:
183e5f7ee39SBarry Smith    Use KSPMonitorSNESLGResidualNormDestroy() to destroy this line graph; do not use PetscDrawLGDestroy().
184e5f7ee39SBarry Smith 
185e5f7ee39SBarry Smith    Level: intermediate
186e5f7ee39SBarry Smith 
187e5f7ee39SBarry Smith .keywords: KSP, monitor, line graph, residual, create
188e5f7ee39SBarry Smith 
189e5f7ee39SBarry Smith .seealso: KSPMonitorSNESLGResidualNormDestroy(), KSPMonitorSet(), KSPMonitorSNESLGTrueResidualCreate()
190e5f7ee39SBarry Smith @*/
1918b0b5a47SLisandro Dalcin PetscErrorCode  KSPMonitorSNESLGResidualNormCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscObject **objs)
192e5f7ee39SBarry Smith {
193e5f7ee39SBarry Smith   PetscDraw      draw;
194e5f7ee39SBarry Smith   PetscErrorCode ierr;
195e5f7ee39SBarry Smith   PetscDrawAxis  axis;
196c36cb520SLisandro Dalcin   PetscDrawLG    lg;
197e5f7ee39SBarry Smith   const char     *names[] = {"Linear residual","Nonlinear residual"};
198e5f7ee39SBarry Smith 
199e5f7ee39SBarry Smith   PetscFunctionBegin;
2008b0b5a47SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
201e5f7ee39SBarry Smith   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
202c36cb520SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,2,&lg);CHKERRQ(ierr);
203c36cb520SLisandro Dalcin   ierr = PetscDrawLGSetLegend(lg,names);CHKERRQ(ierr);
204c36cb520SLisandro Dalcin   ierr = PetscDrawLGSetFromOptions(lg);CHKERRQ(ierr);
205c36cb520SLisandro Dalcin   ierr = PetscDrawLGGetAxis(lg,&axis);CHKERRQ(ierr);
206e5f7ee39SBarry Smith   ierr = PetscDrawAxisSetLabels(axis,"Convergence of Residual Norm","Iteration","Residual Norm");CHKERRQ(ierr);
207c36cb520SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
208e5f7ee39SBarry Smith 
209c36cb520SLisandro Dalcin   ierr = PetscMalloc1(2,objs);CHKERRQ(ierr);
210c36cb520SLisandro Dalcin   (*objs)[1] = (PetscObject)lg;
211e5f7ee39SBarry Smith   PetscFunctionReturn(0);
212e5f7ee39SBarry Smith }
213e5f7ee39SBarry Smith 
214e5f7ee39SBarry Smith PetscErrorCode  KSPMonitorSNESLGResidualNorm(KSP ksp,PetscInt n,PetscReal rnorm,PetscObject *objs)
215e5f7ee39SBarry Smith {
216c36cb520SLisandro Dalcin   SNES           snes = (SNES) objs[0];
217e5f7ee39SBarry Smith   PetscDrawLG    lg   = (PetscDrawLG) objs[1];
218e5f7ee39SBarry Smith   PetscErrorCode ierr;
219e5f7ee39SBarry Smith   PetscReal      y[2];
220e5f7ee39SBarry Smith   Vec            snes_solution,work1,work2;
221e5f7ee39SBarry Smith 
222e5f7ee39SBarry Smith   PetscFunctionBegin;
223e5f7ee39SBarry Smith   if (rnorm > 0.0) y[0] = PetscLog10Real(rnorm);
224e5f7ee39SBarry Smith   else y[0] = -15.0;
225e5f7ee39SBarry Smith 
226e5f7ee39SBarry Smith   ierr = SNESGetSolution(snes,&snes_solution);CHKERRQ(ierr);
227e5f7ee39SBarry Smith   ierr = VecDuplicate(snes_solution,&work1);CHKERRQ(ierr);
228e5f7ee39SBarry Smith   ierr = VecDuplicate(snes_solution,&work2);CHKERRQ(ierr);
229e5f7ee39SBarry Smith   ierr = KSPBuildSolution(ksp,work1,NULL);CHKERRQ(ierr);
230e5f7ee39SBarry Smith   ierr = VecAYPX(work1,-1.0,snes_solution);CHKERRQ(ierr);
231e5f7ee39SBarry Smith   ierr = SNESComputeFunction(snes,work1,work2);CHKERRQ(ierr);
232e5f7ee39SBarry Smith   ierr = VecNorm(work2,NORM_2,y+1);CHKERRQ(ierr);
233e5f7ee39SBarry Smith   if (y[1] > 0.0) y[1] = PetscLog10Real(y[1]);
234e5f7ee39SBarry Smith   else y[1] = -15.0;
235e5f7ee39SBarry Smith   ierr = VecDestroy(&work1);CHKERRQ(ierr);
236e5f7ee39SBarry Smith   ierr = VecDestroy(&work2);CHKERRQ(ierr);
237e5f7ee39SBarry Smith 
238e5f7ee39SBarry Smith   ierr = PetscDrawLGAddPoint(lg,NULL,y);CHKERRQ(ierr);
2396934998bSLisandro Dalcin   if (n < 20 || !(n % 5) || snes->reason) {
240e5f7ee39SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2416934998bSLisandro Dalcin     ierr = PetscDrawLGSave(lg);CHKERRQ(ierr);
242e5f7ee39SBarry Smith   }
243e5f7ee39SBarry Smith   PetscFunctionReturn(0);
244e5f7ee39SBarry Smith }
245e5f7ee39SBarry Smith 
246e5f7ee39SBarry Smith /*@
247e5f7ee39SBarry Smith    KSPMonitorSNESLGResidualNormDestroy - Destroys a line graph context that was created
248e5f7ee39SBarry Smith    with KSPMonitorSNESLGResidualNormCreate().
249e5f7ee39SBarry Smith 
250e5f7ee39SBarry Smith    Collective on KSP
251e5f7ee39SBarry Smith 
252e5f7ee39SBarry Smith    Input Parameter:
253e5f7ee39SBarry Smith .  draw - the drawing context
254e5f7ee39SBarry Smith 
255e5f7ee39SBarry Smith    Level: intermediate
256e5f7ee39SBarry Smith 
257e5f7ee39SBarry Smith .keywords: KSP, monitor, line graph, destroy
258e5f7ee39SBarry Smith 
259e5f7ee39SBarry Smith .seealso: KSPMonitorSNESLGResidualNormCreate(), KSPMonitorSNESLGTrueResidualDestroy(), KSPMonitorSet()
260e5f7ee39SBarry Smith @*/
261e5f7ee39SBarry Smith PetscErrorCode  KSPMonitorSNESLGResidualNormDestroy(PetscObject **objs)
262e5f7ee39SBarry Smith {
263e5f7ee39SBarry Smith   PetscErrorCode ierr;
264c36cb520SLisandro Dalcin   PetscDrawLG    lg = (PetscDrawLG) (*objs)[1];
265e5f7ee39SBarry Smith 
266e5f7ee39SBarry Smith   PetscFunctionBegin;
267c36cb520SLisandro Dalcin   ierr = PetscDrawLGDestroy(&lg);CHKERRQ(ierr);
268e5f7ee39SBarry Smith   ierr = PetscFree(*objs);CHKERRQ(ierr);
269e5f7ee39SBarry Smith   PetscFunctionReturn(0);
270e5f7ee39SBarry Smith }
271e5f7ee39SBarry Smith 
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
281d43b4f6eSBarry Smith -  vf - viewer and format structure
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 @*/
292d43b4f6eSBarry Smith PetscErrorCode  SNESMonitorDefault(SNES snes,PetscInt its,PetscReal fgnorm,PetscViewerAndFormat *vf)
293e7e93795SLois Curfman McInnes {
294dfbe8321SBarry Smith   PetscErrorCode ierr;
295d43b4f6eSBarry Smith   PetscViewer    viewer = vf->viewer;
296d132466eSBarry Smith 
2973a40ed3dSBarry Smith   PetscFunctionBegin;
2984d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
299d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
300649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
301649052a6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %14.12e \n",its,(double)fgnorm);CHKERRQ(ierr);
302649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
303d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr) ;
3043a40ed3dSBarry Smith   PetscFunctionReturn(0);
305e7e93795SLois Curfman McInnes }
3063f1db9ecSBarry Smith 
3071f60017eSBarry Smith /*@C
3081f60017eSBarry Smith    SNESMonitorScaling - Monitors the largest value in each row of the Jacobian.
3091f60017eSBarry Smith 
3101f60017eSBarry Smith    Collective on SNES
3111f60017eSBarry Smith 
3121f60017eSBarry Smith    Input Parameters:
3131f60017eSBarry Smith +  snes - the SNES context
3141f60017eSBarry Smith .  its - iteration number
3151f60017eSBarry Smith .  fgnorm - 2-norm of residual
3161f60017eSBarry Smith -  vf - viewer and format structure
3171f60017eSBarry Smith 
3181f60017eSBarry Smith    Notes:
3191f60017eSBarry Smith    This routine prints the largest value in each row of the Jacobian
3201f60017eSBarry Smith 
3211f60017eSBarry Smith    Level: intermediate
3221f60017eSBarry Smith 
3231f60017eSBarry Smith .keywords: SNES, nonlinear, default, monitor, norm
3241f60017eSBarry Smith 
3251f60017eSBarry Smith .seealso: SNESMonitorSet(), SNESMonitorSolution()
3261f60017eSBarry Smith @*/
3271f60017eSBarry Smith PetscErrorCode  SNESMonitorScaling(SNES snes,PetscInt its,PetscReal fgnorm,PetscViewerAndFormat *vf)
3281f60017eSBarry Smith {
3291f60017eSBarry Smith   PetscErrorCode ierr;
3301f60017eSBarry Smith   PetscViewer    viewer = vf->viewer;
3311f60017eSBarry Smith   KSP            ksp;
3321f60017eSBarry Smith   Mat            J;
3331f60017eSBarry Smith   Vec            v;
3341f60017eSBarry Smith 
3351f60017eSBarry Smith   PetscFunctionBegin;
3361f60017eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3371f60017eSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
3381f60017eSBarry Smith   ierr = KSPGetOperators(ksp,&J,NULL);CHKERRQ(ierr);
3391f60017eSBarry Smith   ierr = MatCreateVecs(J,&v,NULL);CHKERRQ(ierr);
3401f60017eSBarry Smith   ierr = MatGetRowMaxAbs(J,v,NULL);CHKERRQ(ierr);
3411f60017eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3421f60017eSBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
3431f60017eSBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Jacobian maximum row entries \n");CHKERRQ(ierr);
3441f60017eSBarry Smith   ierr = VecView(v,viewer);CHKERRQ(ierr);
3451f60017eSBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
3461f60017eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr) ;
3471f60017eSBarry Smith   ierr = VecDestroy(&v);CHKERRQ(ierr);
3481f60017eSBarry Smith   PetscFunctionReturn(0);
3491f60017eSBarry Smith }
3501f60017eSBarry Smith 
351d43b4f6eSBarry Smith PetscErrorCode SNESMonitorJacUpdateSpectrum(SNES snes,PetscInt it,PetscReal fnorm,PetscViewerAndFormat *vf)
352a80ad3e0SBarry Smith {
353196da8b6SPeter Brune #if defined(PETSC_MISSING_LAPACK_GEEV)
354ce94432eSBarry Smith   SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_SUP,"GEEV - Lapack routine is unavailable\nNot able to provide eigen values.");
355196da8b6SPeter Brune #elif defined(PETSC_HAVE_ESSL)
356ce94432eSBarry Smith   SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_SUP,"GEEV - No support for ESSL Lapack Routines");
357196da8b6SPeter Brune #else
3582e7541e6SPeter Brune   Vec            X;
3592e7541e6SPeter Brune   Mat            J,dJ,dJdense;
3602e7541e6SPeter Brune   PetscErrorCode ierr;
361d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
3622e7541e6SPeter Brune   PetscInt       n,i;
3632e7541e6SPeter Brune   PetscBLASInt   nb,lwork;
3642e7541e6SPeter Brune   PetscReal      *eigr,*eigi;
3652e7541e6SPeter Brune   PetscScalar    *work;
3662e7541e6SPeter Brune   PetscScalar    *a;
3672e7541e6SPeter Brune 
3682e7541e6SPeter Brune   PetscFunctionBegin;
3692e7541e6SPeter Brune   if (it == 0) PetscFunctionReturn(0);
3702e7541e6SPeter Brune   /* create the difference between the current update and the current jacobian */
3712e7541e6SPeter Brune   ierr = SNESGetSolution(snes,&X);CHKERRQ(ierr);
372d1e9a80fSBarry Smith   ierr = SNESGetJacobian(snes,NULL,&J,&func,NULL);CHKERRQ(ierr);
3732e7541e6SPeter Brune   ierr = MatDuplicate(J,MAT_COPY_VALUES,&dJ);CHKERRQ(ierr);
374d1e9a80fSBarry Smith   ierr = SNESComputeJacobian(snes,X,dJ,dJ);CHKERRQ(ierr);
3752e7541e6SPeter Brune   ierr = MatAXPY(dJ,-1.0,J,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
376f5af7f23SKarl Rupp 
3772e7541e6SPeter Brune   /* compute the spectrum directly */
3782e7541e6SPeter Brune   ierr  = MatConvert(dJ,MATSEQDENSE,MAT_INITIAL_MATRIX,&dJdense);CHKERRQ(ierr);
3790298fd71SBarry Smith   ierr  = MatGetSize(dJ,&n,NULL);CHKERRQ(ierr);
380c5df96a5SBarry Smith   ierr  = PetscBLASIntCast(n,&nb);CHKERRQ(ierr);
3812e7541e6SPeter Brune   lwork = 3*nb;
382785e854fSJed Brown   ierr  = PetscMalloc1(n,&eigr);CHKERRQ(ierr);
383785e854fSJed Brown   ierr  = PetscMalloc1(n,&eigi);CHKERRQ(ierr);
384785e854fSJed Brown   ierr  = PetscMalloc1(lwork,&work);CHKERRQ(ierr);
3858c778c55SBarry Smith   ierr  = MatDenseGetArray(dJdense,&a);CHKERRQ(ierr);
3862e7541e6SPeter Brune #if !defined(PETSC_USE_COMPLEX)
3872e7541e6SPeter Brune   {
3882e7541e6SPeter Brune     PetscBLASInt lierr;
3892e7541e6SPeter Brune     ierr = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr);
3908b83055fSJed Brown     PetscStackCallBLAS("LAPACKgeev",LAPACKgeev_("N","N",&nb,a,&nb,eigr,eigi,NULL,&nb,NULL,&nb,work,&lwork,&lierr));
3912e7541e6SPeter Brune     if (lierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"geev() error %d",lierr);
3922e7541e6SPeter Brune     ierr = PetscFPTrapPop();CHKERRQ(ierr);
3932e7541e6SPeter Brune   }
3942e7541e6SPeter Brune #else
3952e7541e6SPeter Brune   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not coded for complex");
3962e7541e6SPeter Brune #endif
397fde5950dSBarry Smith   ierr = PetscPrintf(PetscObjectComm((PetscObject)snes),"Eigenvalues of J_%d - J_%d:\n",it,it-1);CHKERRQ(ierr);
3982e7541e6SPeter Brune   for (i=0;i<n;i++) {
399fde5950dSBarry Smith     ierr = PetscPrintf(PetscObjectComm((PetscObject)snes),"%5d: %20.5g + %20.5gi\n",i,(double)eigr[i],(double)eigi[i]);CHKERRQ(ierr);
4002e7541e6SPeter Brune   }
4018c778c55SBarry Smith   ierr = MatDenseRestoreArray(dJdense,&a);CHKERRQ(ierr);
4022e7541e6SPeter Brune   ierr = MatDestroy(&dJ);CHKERRQ(ierr);
4032e7541e6SPeter Brune   ierr = MatDestroy(&dJdense);CHKERRQ(ierr);
4042e7541e6SPeter Brune   ierr = PetscFree(eigr);CHKERRQ(ierr);
4052e7541e6SPeter Brune   ierr = PetscFree(eigi);CHKERRQ(ierr);
4062e7541e6SPeter Brune   ierr = PetscFree(work);CHKERRQ(ierr);
4072e7541e6SPeter Brune   PetscFunctionReturn(0);
408196da8b6SPeter Brune #endif
4092e7541e6SPeter Brune }
4102e7541e6SPeter Brune 
4116ba87a44SLisandro Dalcin PETSC_INTERN PetscErrorCode  SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
4126ba87a44SLisandro Dalcin 
4137087cfbeSBarry Smith PetscErrorCode  SNESMonitorRange_Private(SNES snes,PetscInt it,PetscReal *per)
414b271bb04SBarry Smith {
415b271bb04SBarry Smith   PetscErrorCode ierr;
416b271bb04SBarry Smith   Vec            resid;
417b271bb04SBarry Smith   PetscReal      rmax,pwork;
418b271bb04SBarry Smith   PetscInt       i,n,N;
419b271bb04SBarry Smith   PetscScalar    *r;
420b271bb04SBarry Smith 
421b271bb04SBarry Smith   PetscFunctionBegin;
422b271bb04SBarry Smith   ierr  = SNESGetFunction(snes,&resid,0,0);CHKERRQ(ierr);
423b271bb04SBarry Smith   ierr  = VecNorm(resid,NORM_INFINITY,&rmax);CHKERRQ(ierr);
424b271bb04SBarry Smith   ierr  = VecGetLocalSize(resid,&n);CHKERRQ(ierr);
425b271bb04SBarry Smith   ierr  = VecGetSize(resid,&N);CHKERRQ(ierr);
426b271bb04SBarry Smith   ierr  = VecGetArray(resid,&r);CHKERRQ(ierr);
427b271bb04SBarry Smith   pwork = 0.0;
428b271bb04SBarry Smith   for (i=0; i<n; i++) {
429b271bb04SBarry Smith     pwork += (PetscAbsScalar(r[i]) > .20*rmax);
430b271bb04SBarry Smith   }
431b2566f29SBarry Smith   ierr = MPIU_Allreduce(&pwork,per,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)snes));CHKERRQ(ierr);
432b271bb04SBarry Smith   ierr = VecRestoreArray(resid,&r);CHKERRQ(ierr);
433b271bb04SBarry Smith   *per = *per/N;
434b271bb04SBarry Smith   PetscFunctionReturn(0);
435b271bb04SBarry Smith }
436b271bb04SBarry Smith 
437b271bb04SBarry Smith /*@C
438b271bb04SBarry Smith    SNESMonitorRange - Prints the percentage of residual elements that are more then 10 percent of the maximum value.
439b271bb04SBarry Smith 
440b271bb04SBarry Smith    Collective on SNES
441b271bb04SBarry Smith 
442b271bb04SBarry Smith    Input Parameters:
443b271bb04SBarry Smith +  snes   - iterative context
444b271bb04SBarry Smith .  it    - iteration number
445b271bb04SBarry Smith .  rnorm - 2-norm (preconditioned) residual value (may be estimated).
446b271bb04SBarry Smith -  dummy - unused monitor context
447b271bb04SBarry Smith 
448b271bb04SBarry Smith    Options Database Key:
449b271bb04SBarry Smith .  -snes_monitor_range - Activates SNESMonitorRange()
450b271bb04SBarry Smith 
451b271bb04SBarry Smith    Level: intermediate
452b271bb04SBarry Smith 
453b271bb04SBarry Smith .keywords: SNES, default, monitor, residual
454b271bb04SBarry Smith 
455b271bb04SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorDefault(), SNESMonitorLGCreate()
456b271bb04SBarry Smith @*/
457d43b4f6eSBarry Smith PetscErrorCode  SNESMonitorRange(SNES snes,PetscInt it,PetscReal rnorm,PetscViewerAndFormat *vf)
458b271bb04SBarry Smith {
459b271bb04SBarry Smith   PetscErrorCode ierr;
460b271bb04SBarry Smith   PetscReal      perc,rel;
461d43b4f6eSBarry Smith   PetscViewer    viewer = vf->viewer;
462b271bb04SBarry Smith   /* should be in a MonitorRangeContext */
463b271bb04SBarry Smith   static PetscReal prev;
464b271bb04SBarry Smith 
465b271bb04SBarry Smith   PetscFunctionBegin;
4664d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
467b271bb04SBarry Smith   if (!it) prev = rnorm;
468b271bb04SBarry Smith   ierr = SNESMonitorRange_Private(snes,it,&perc);CHKERRQ(ierr);
469b271bb04SBarry Smith 
470b271bb04SBarry Smith   rel  = (prev - rnorm)/prev;
471b271bb04SBarry Smith   prev = rnorm;
472d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
473649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
4746712e2f1SBarry 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);
475649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
476d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
477b271bb04SBarry Smith   PetscFunctionReturn(0);
478b271bb04SBarry Smith }
479b271bb04SBarry Smith 
4803a7fca6bSBarry Smith /*@C
481a6570f20SBarry Smith    SNESMonitorRatio - Monitors progress of the SNES solvers by printing the ratio
4824b27c08aSLois Curfman McInnes    of residual norm at each iteration to the previous.
4833a7fca6bSBarry Smith 
4843a7fca6bSBarry Smith    Collective on SNES
4853a7fca6bSBarry Smith 
4863a7fca6bSBarry Smith    Input Parameters:
4873a7fca6bSBarry Smith +  snes - the SNES context
4883a7fca6bSBarry Smith .  its - iteration number
4893a7fca6bSBarry Smith .  fgnorm - 2-norm of residual (or gradient)
490eabae89aSBarry Smith -  dummy -  context of monitor
4913a7fca6bSBarry Smith 
4923a7fca6bSBarry Smith    Level: intermediate
4933a7fca6bSBarry Smith 
49495452b02SPatrick Sanan    Notes:
49595452b02SPatrick Sanan     Insure that SNESMonitorRatio() is called when you set this monitor
4963a7fca6bSBarry Smith .keywords: SNES, nonlinear, monitor, norm
4973a7fca6bSBarry Smith 
498fde5950dSBarry Smith .seealso: SNESMonitorSet(), SNESMonitorSolution(), SNESMonitorRatio()
4993a7fca6bSBarry Smith @*/
500d43b4f6eSBarry Smith PetscErrorCode  SNESMonitorRatio(SNES snes,PetscInt its,PetscReal fgnorm,PetscViewerAndFormat *vf)
5013a7fca6bSBarry Smith {
502dfbe8321SBarry Smith   PetscErrorCode          ierr;
50377431f27SBarry Smith   PetscInt                len;
50487828ca2SBarry Smith   PetscReal               *history;
505d43b4f6eSBarry Smith   PetscViewer             viewer = vf->viewer;
5063a7fca6bSBarry Smith 
5073a7fca6bSBarry Smith   PetscFunctionBegin;
5080298fd71SBarry Smith   ierr = SNESGetConvergenceHistory(snes,&history,NULL,&len);CHKERRQ(ierr);
509d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
510fde5950dSBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
511958c9bccSBarry Smith   if (!its || !history || its > len) {
512fde5950dSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %14.12e \n",its,(double)fgnorm);CHKERRQ(ierr);
5133a7fca6bSBarry Smith   } else {
51487828ca2SBarry Smith     PetscReal ratio = fgnorm/history[its-1];
515fde5950dSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %14.12e %14.12e \n",its,(double)fgnorm,(double)ratio);CHKERRQ(ierr);
5163a7fca6bSBarry Smith   }
517fde5950dSBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
518d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
5193a7fca6bSBarry Smith   PetscFunctionReturn(0);
5203a7fca6bSBarry Smith }
5213a7fca6bSBarry Smith 
5223a7fca6bSBarry Smith /*@C
523fde5950dSBarry Smith    SNESMonitorRatioSetUp - Insures the SNES object is saving its history since this monitor needs access to it
5243a7fca6bSBarry Smith 
5253a7fca6bSBarry Smith    Collective on SNES
5263a7fca6bSBarry Smith 
5273a7fca6bSBarry Smith    Input Parameters:
528eabae89aSBarry Smith +   snes - the SNES context
529fde5950dSBarry Smith -   viewer - the PetscViewer object (ignored)
5303a7fca6bSBarry Smith 
5313a7fca6bSBarry Smith    Level: intermediate
5323a7fca6bSBarry Smith 
5333a7fca6bSBarry Smith .keywords: SNES, nonlinear, monitor, norm
5343a7fca6bSBarry Smith 
535fde5950dSBarry Smith .seealso: SNESMonitorSet(), SNESMonitorSolution(), SNESMonitorDefault(), SNESMonitorRatio()
5363a7fca6bSBarry Smith @*/
537d43b4f6eSBarry Smith PetscErrorCode  SNESMonitorRatioSetUp(SNES snes,PetscViewerAndFormat *vf)
5383a7fca6bSBarry Smith {
539dfbe8321SBarry Smith   PetscErrorCode          ierr;
54087828ca2SBarry Smith   PetscReal               *history;
5413a7fca6bSBarry Smith 
5423a7fca6bSBarry Smith   PetscFunctionBegin;
5430298fd71SBarry Smith   ierr = SNESGetConvergenceHistory(snes,&history,NULL,NULL);CHKERRQ(ierr);
5443a7fca6bSBarry Smith   if (!history) {
545fde5950dSBarry Smith     ierr = SNESSetConvergenceHistory(snes,NULL,NULL,100,PETSC_TRUE);CHKERRQ(ierr);
5463a7fca6bSBarry Smith   }
5473a7fca6bSBarry Smith   PetscFunctionReturn(0);
5483a7fca6bSBarry Smith }
5493a7fca6bSBarry Smith 
550e7e93795SLois Curfman McInnes /* ---------------------------------------------------------------- */
551be1f7002SBarry Smith /*
552a6570f20SBarry Smith      Default (short) SNES Monitor, same as SNESMonitorDefault() except
553be1f7002SBarry Smith   it prints fewer digits of the residual as the residual gets smaller.
554be1f7002SBarry Smith   This is because the later digits are meaningless and are often
555be1f7002SBarry Smith   different on different machines; by using this routine different
556be1f7002SBarry Smith   machines will usually generate the same output.
557dec21524SBarry Smith 
558dec21524SBarry Smith   Deprecated: Intentionally has no manual page
559be1f7002SBarry Smith */
560d43b4f6eSBarry Smith PetscErrorCode  SNESMonitorDefaultShort(SNES snes,PetscInt its,PetscReal fgnorm,PetscViewerAndFormat *vf)
561e7e93795SLois Curfman McInnes {
562dfbe8321SBarry Smith   PetscErrorCode ierr;
563d43b4f6eSBarry Smith   PetscViewer    viewer = vf->viewer;
564d132466eSBarry Smith 
5653a40ed3dSBarry Smith   PetscFunctionBegin;
5664d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
567d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
568649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
5698f240d10SBarry Smith   if (fgnorm > 1.e-9) {
5708fa295daSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %g \n",its,(double)fgnorm);CHKERRQ(ierr);
5713a40ed3dSBarry Smith   } else if (fgnorm > 1.e-11) {
5728fa295daSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %5.3e \n",its,(double)fgnorm);CHKERRQ(ierr);
5733a40ed3dSBarry Smith   } else {
574649052a6SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm < 1.e-11\n",its);CHKERRQ(ierr);
575a34d58ebSBarry Smith   }
576649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
577d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
5783a40ed3dSBarry Smith   PetscFunctionReturn(0);
579e7e93795SLois Curfman McInnes }
5802db13446SMatthew G. Knepley 
5812db13446SMatthew G. Knepley /*@C
5822db13446SMatthew G. Knepley   SNESMonitorDefaultField - Monitors progress of the SNES solvers, separated into fields.
5832db13446SMatthew G. Knepley 
5842db13446SMatthew G. Knepley   Collective on SNES
5852db13446SMatthew G. Knepley 
5862db13446SMatthew G. Knepley   Input Parameters:
5872db13446SMatthew G. Knepley + snes   - the SNES context
5882db13446SMatthew G. Knepley . its    - iteration number
5892db13446SMatthew G. Knepley . fgnorm - 2-norm of residual
5902db13446SMatthew G. Knepley - ctx    - the PetscViewer
5912db13446SMatthew G. Knepley 
5922db13446SMatthew G. Knepley   Notes:
5932db13446SMatthew G. Knepley   This routine uses the DM attached to the residual vector
5942db13446SMatthew G. Knepley 
5952db13446SMatthew G. Knepley   Level: intermediate
5962db13446SMatthew G. Knepley 
5972db13446SMatthew G. Knepley .keywords: SNES, nonlinear, field, monitor, norm
598dec21524SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorSolution(), SNESMonitorDefault()
5992db13446SMatthew G. Knepley @*/
600d43b4f6eSBarry Smith PetscErrorCode SNESMonitorDefaultField(SNES snes, PetscInt its, PetscReal fgnorm, PetscViewerAndFormat *vf)
6012db13446SMatthew G. Knepley {
602d43b4f6eSBarry Smith   PetscViewer    viewer = vf->viewer;
6032db13446SMatthew G. Knepley   Vec            r;
6042db13446SMatthew G. Knepley   DM             dm;
6052db13446SMatthew G. Knepley   PetscReal      res[256];
6062db13446SMatthew G. Knepley   PetscInt       tablevel;
6072db13446SMatthew G. Knepley   PetscErrorCode ierr;
6082db13446SMatthew G. Knepley 
6092db13446SMatthew G. Knepley   PetscFunctionBegin;
6104d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
6112db13446SMatthew G. Knepley   ierr = SNESGetFunction(snes, &r, NULL, NULL);CHKERRQ(ierr);
6122db13446SMatthew G. Knepley   ierr = VecGetDM(r, &dm);CHKERRQ(ierr);
613d43b4f6eSBarry Smith   if (!dm) {ierr = SNESMonitorDefault(snes, its, fgnorm, vf);CHKERRQ(ierr);}
6142db13446SMatthew G. Knepley   else {
6152db13446SMatthew G. Knepley     PetscSection s, gs;
6162db13446SMatthew G. Knepley     PetscInt     Nf, f;
6172db13446SMatthew G. Knepley 
618e87a4003SBarry Smith     ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
619e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
620d43b4f6eSBarry Smith     if (!s || !gs) {ierr = SNESMonitorDefault(snes, its, fgnorm, vf);CHKERRQ(ierr);}
6212db13446SMatthew G. Knepley     ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
6222db13446SMatthew G. Knepley     if (Nf > 256) SETERRQ1(PetscObjectComm((PetscObject) snes), PETSC_ERR_SUP, "Do not support %d fields > 256", Nf);
6232db13446SMatthew G. Knepley     ierr = PetscSectionVecNorm(s, gs, r, NORM_2, res);CHKERRQ(ierr);
6242db13446SMatthew G. Knepley     ierr = PetscObjectGetTabLevel((PetscObject) snes, &tablevel);CHKERRQ(ierr);
625d43b4f6eSBarry Smith     ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
6262db13446SMatthew G. Knepley     ierr = PetscViewerASCIIAddTab(viewer, tablevel);CHKERRQ(ierr);
6272db13446SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr);
6282db13446SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
6292db13446SMatthew G. Knepley       if (f) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
6302db13446SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", res[f]);CHKERRQ(ierr);
6312db13446SMatthew G. Knepley     }
6322db13446SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "] \n");CHKERRQ(ierr);
6332db13446SMatthew G. Knepley     ierr = PetscViewerASCIISubtractTab(viewer, tablevel);CHKERRQ(ierr);
634d43b4f6eSBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
6352db13446SMatthew G. Knepley   }
6362db13446SMatthew G. Knepley   PetscFunctionReturn(0);
6372db13446SMatthew G. Knepley }
638e7e93795SLois Curfman McInnes /* ---------------------------------------------------------------- */
6394b828684SBarry Smith /*@C
6408d359177SBarry Smith    SNESConvergedDefault - Convergence test of the solvers for
641f525115eSLois Curfman McInnes    systems of nonlinear equations (default).
642e7e93795SLois Curfman McInnes 
643c7afd0dbSLois Curfman McInnes    Collective on SNES
644c7afd0dbSLois Curfman McInnes 
645e7e93795SLois Curfman McInnes    Input Parameters:
646c7afd0dbSLois Curfman McInnes +  snes - the SNES context
64706ee9f85SBarry Smith .  it - the iteration (0 indicates before any Newton steps)
648e7e93795SLois Curfman McInnes .  xnorm - 2-norm of current iterate
649c60f73f4SPeter Brune .  snorm - 2-norm of current step
6507f3332b4SBarry Smith .  fnorm - 2-norm of function at current iterate
651c7afd0dbSLois Curfman McInnes -  dummy - unused context
652e7e93795SLois Curfman McInnes 
653184914b5SBarry Smith    Output Parameter:
654184914b5SBarry Smith .   reason  - one of
65570441072SBarry Smith $  SNES_CONVERGED_FNORM_ABS       - (fnorm < abstol),
656c60f73f4SPeter Brune $  SNES_CONVERGED_SNORM_RELATIVE  - (snorm < stol*xnorm),
657184914b5SBarry Smith $  SNES_CONVERGED_FNORM_RELATIVE  - (fnorm < rtol*fnorm0),
658184914b5SBarry Smith $  SNES_DIVERGED_FUNCTION_COUNT   - (nfct > maxf),
659184914b5SBarry Smith $  SNES_DIVERGED_FNORM_NAN        - (fnorm == NaN),
660184914b5SBarry Smith $  SNES_CONVERGED_ITERATING       - (otherwise),
661e7e93795SLois Curfman McInnes 
662e7e93795SLois Curfman McInnes    where
663c7afd0dbSLois Curfman McInnes +    maxf - maximum number of function evaluations,
664c7afd0dbSLois Curfman McInnes             set with SNESSetTolerances()
665c7afd0dbSLois Curfman McInnes .    nfct - number of function evaluations,
66670441072SBarry Smith .    abstol - absolute function norm tolerance,
667c7afd0dbSLois Curfman McInnes             set with SNESSetTolerances()
668c7afd0dbSLois Curfman McInnes -    rtol - relative function norm tolerance, set with SNESSetTolerances()
669fee21e36SBarry Smith 
67036851e7fSLois Curfman McInnes    Level: intermediate
67136851e7fSLois Curfman McInnes 
672e7e93795SLois Curfman McInnes .keywords: SNES, nonlinear, default, converged, convergence
673e7e93795SLois Curfman McInnes 
67471f87433Sdalcinl .seealso: SNESSetConvergenceTest()
675e7e93795SLois Curfman McInnes @*/
6768d359177SBarry Smith PetscErrorCode  SNESConvergedDefault(SNES snes,PetscInt it,PetscReal xnorm,PetscReal snorm,PetscReal fnorm,SNESConvergedReason *reason,void *dummy)
677e7e93795SLois Curfman McInnes {
67863ba0a88SBarry Smith   PetscErrorCode ierr;
67963ba0a88SBarry Smith 
6803a40ed3dSBarry Smith   PetscFunctionBegin;
6810700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6823f149594SLisandro Dalcin   PetscValidPointer(reason,6);
6833f149594SLisandro Dalcin 
68406ee9f85SBarry Smith   *reason = SNES_CONVERGED_ITERATING;
68506ee9f85SBarry Smith 
68606ee9f85SBarry Smith   if (!it) {
68706ee9f85SBarry Smith     /* set parameter for default relative tolerance convergence test */
68806ee9f85SBarry Smith     snes->ttol   = fnorm*snes->rtol;
689e37c518bSBarry Smith     snes->rnorm0 = fnorm;
69006ee9f85SBarry Smith   }
6918146f6ebSBarry Smith   if (PetscIsInfOrNanReal(fnorm)) {
692ae15b995SBarry Smith     ierr    = PetscInfo(snes,"Failed to converged, function norm is NaN\n");CHKERRQ(ierr);
693184914b5SBarry Smith     *reason = SNES_DIVERGED_FNORM_NAN;
694be5caee7SBarry Smith   } else if (fnorm < snes->abstol && (it || !snes->forceiteration)) {
6958f1a2a5eSBarry Smith     ierr    = PetscInfo2(snes,"Converged due to function norm %14.12e < %14.12e\n",(double)fnorm,(double)snes->abstol);CHKERRQ(ierr);
696184914b5SBarry Smith     *reason = SNES_CONVERGED_FNORM_ABS;
697e71169deSBarry Smith   } else if (snes->nfuncs >= snes->max_funcs && snes->max_funcs >= 0) {
698ae15b995SBarry Smith     ierr    = PetscInfo2(snes,"Exceeded maximum number of function evaluations: %D > %D\n",snes->nfuncs,snes->max_funcs);CHKERRQ(ierr);
699184914b5SBarry Smith     *reason = SNES_DIVERGED_FUNCTION_COUNT;
70006ee9f85SBarry Smith   }
70106ee9f85SBarry Smith 
70206ee9f85SBarry Smith   if (it && !*reason) {
70306ee9f85SBarry Smith     if (fnorm <= snes->ttol) {
7048f1a2a5eSBarry Smith       ierr    = PetscInfo2(snes,"Converged due to function norm %14.12e < %14.12e (relative tolerance)\n",(double)fnorm,(double)snes->ttol);CHKERRQ(ierr);
70506ee9f85SBarry Smith       *reason = SNES_CONVERGED_FNORM_RELATIVE;
706c60f73f4SPeter Brune     } else if (snorm < snes->stol*xnorm) {
707c60f73f4SPeter 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);
708c60f73f4SPeter Brune       *reason = SNES_CONVERGED_SNORM_RELATIVE;
709e4d06f11SPatrick Farrell     } else if (snes->divtol > 0 && (fnorm > snes->divtol*snes->rnorm0)) {
710e37c518bSBarry Smith       ierr    = PetscInfo3(snes,"Diverged due to increase in function norm: %14.12e > %14.12e * %14.12e\n",(double)fnorm,(double)snes->divtol,(double)snes->rnorm0);CHKERRQ(ierr);
711e37c518bSBarry Smith       *reason = SNES_DIVERGED_DTOL;
71206ee9f85SBarry Smith     }
713e37c518bSBarry Smith 
714e7e93795SLois Curfman McInnes   }
7153a40ed3dSBarry Smith   PetscFunctionReturn(0);
716e7e93795SLois Curfman McInnes }
7173f149594SLisandro Dalcin 
7183f149594SLisandro Dalcin /*@C
719e2a6519dSDmitry Karpeev    SNESConvergedSkip - Convergence test for SNES that NEVER returns as
7203f149594SLisandro Dalcin    converged, UNLESS the maximum number of iteration have been reached.
7213f149594SLisandro Dalcin 
7223f9fe445SBarry Smith    Logically Collective on SNES
7233f149594SLisandro Dalcin 
7243f149594SLisandro Dalcin    Input Parameters:
7253f149594SLisandro Dalcin +  snes - the SNES context
7263f149594SLisandro Dalcin .  it - the iteration (0 indicates before any Newton steps)
7273f149594SLisandro Dalcin .  xnorm - 2-norm of current iterate
728c60f73f4SPeter Brune .  snorm - 2-norm of current step
7293f149594SLisandro Dalcin .  fnorm - 2-norm of function at current iterate
7303f149594SLisandro Dalcin -  dummy - unused context
7313f149594SLisandro Dalcin 
7323f149594SLisandro Dalcin    Output Parameter:
73385385478SLisandro Dalcin .   reason  - SNES_CONVERGED_ITERATING, SNES_CONVERGED_ITS, or SNES_DIVERGED_FNORM_NAN
7343f149594SLisandro Dalcin 
7353f149594SLisandro Dalcin    Notes:
7363f149594SLisandro Dalcin    Convergence is then declared after a fixed number of iterations have been used.
7373f149594SLisandro Dalcin 
7383f149594SLisandro Dalcin    Level: advanced
7393f149594SLisandro Dalcin 
7403f149594SLisandro Dalcin .keywords: SNES, nonlinear, skip, converged, convergence
7413f149594SLisandro Dalcin 
7423f149594SLisandro Dalcin .seealso: SNESSetConvergenceTest()
7433f149594SLisandro Dalcin @*/
744e2a6519dSDmitry Karpeev PetscErrorCode  SNESConvergedSkip(SNES snes,PetscInt it,PetscReal xnorm,PetscReal snorm,PetscReal fnorm,SNESConvergedReason *reason,void *dummy)
7453f149594SLisandro Dalcin {
7463f149594SLisandro Dalcin   PetscErrorCode ierr;
7473f149594SLisandro Dalcin 
7483f149594SLisandro Dalcin   PetscFunctionBegin;
7490700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7503f149594SLisandro Dalcin   PetscValidPointer(reason,6);
7513f149594SLisandro Dalcin 
7523f149594SLisandro Dalcin   *reason = SNES_CONVERGED_ITERATING;
7533f149594SLisandro Dalcin 
7543f149594SLisandro Dalcin   if (fnorm != fnorm) {
7553f149594SLisandro Dalcin     ierr    = PetscInfo(snes,"Failed to converged, function norm is NaN\n");CHKERRQ(ierr);
7563f149594SLisandro Dalcin     *reason = SNES_DIVERGED_FNORM_NAN;
7573f149594SLisandro Dalcin   } else if (it == snes->max_its) {
7583f149594SLisandro Dalcin     *reason = SNES_CONVERGED_ITS;
7593f149594SLisandro Dalcin   }
7603f149594SLisandro Dalcin   PetscFunctionReturn(0);
7613f149594SLisandro Dalcin }
7623f149594SLisandro Dalcin 
7638d359177SBarry Smith /*@C
764fa0ddf94SBarry Smith   SNESSetWorkVecs - Gets a number of work vectors.
76558c9b817SLisandro Dalcin 
76658c9b817SLisandro Dalcin   Input Parameters:
767*a2b725a8SWilliam Gropp + snes  - the SNES context
768*a2b725a8SWilliam Gropp - nw - number of work vectors to allocate
76958c9b817SLisandro Dalcin 
77058c9b817SLisandro Dalcin   Level: developer
771fa0ddf94SBarry Smith 
77298acb6afSMatthew G Knepley @*/
773fa0ddf94SBarry Smith PetscErrorCode SNESSetWorkVecs(SNES snes,PetscInt nw)
77458c9b817SLisandro Dalcin {
775c5ed8070SMatthew G. Knepley   DM             dm;
776c5ed8070SMatthew G. Knepley   Vec            v;
77758c9b817SLisandro Dalcin   PetscErrorCode ierr;
77858c9b817SLisandro Dalcin 
77958c9b817SLisandro Dalcin   PetscFunctionBegin;
78058c9b817SLisandro Dalcin   if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);}
78158c9b817SLisandro Dalcin   snes->nwork = nw;
782f5af7f23SKarl Rupp 
783c5ed8070SMatthew G. Knepley   ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr);
784c5ed8070SMatthew G. Knepley   ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
785c5ed8070SMatthew G. Knepley   ierr = VecDuplicateVecs(v,snes->nwork,&snes->work);CHKERRQ(ierr);
786c5ed8070SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
78758c9b817SLisandro Dalcin   ierr = PetscLogObjectParents(snes,nw,snes->work);CHKERRQ(ierr);
78858c9b817SLisandro Dalcin   PetscFunctionReturn(0);
78958c9b817SLisandro Dalcin }
790