xref: /petsc/src/snes/interface/snesut.c (revision e37c518b3c178882b922d1d3faeb3ee252cb498a)
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 @*/
26d43b4f6eSBarry Smith PetscErrorCode  SNESMonitorSolution(SNES snes,PetscInt its,PetscReal fgnorm,PetscViewerAndFormat *vf)
273f1db9ecSBarry Smith {
28dfbe8321SBarry Smith   PetscErrorCode ierr;
293f1db9ecSBarry Smith   Vec            x;
30d43b4f6eSBarry Smith   PetscViewer    viewer = vf->viewer;
313f1db9ecSBarry Smith 
323f1db9ecSBarry Smith   PetscFunctionBegin;
334d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
343f1db9ecSBarry Smith   ierr = SNESGetSolution(snes,&x);CHKERRQ(ierr);
35d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
363f1db9ecSBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
37d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
383f1db9ecSBarry Smith   PetscFunctionReturn(0);
393f1db9ecSBarry Smith }
403f1db9ecSBarry Smith 
414a2ae208SSatish Balay #undef __FUNCT__
42a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorResidual"
435ed2d596SBarry Smith /*@C
44a6570f20SBarry Smith    SNESMonitorResidual - Monitors progress of the SNES solvers by calling
455ed2d596SBarry Smith    VecView() for the residual at each iteration.
465ed2d596SBarry Smith 
475ed2d596SBarry Smith    Collective on SNES
485ed2d596SBarry Smith 
495ed2d596SBarry Smith    Input Parameters:
505ed2d596SBarry Smith +  snes - the SNES context
515ed2d596SBarry Smith .  its - iteration number
524b27c08aSLois Curfman McInnes .  fgnorm - 2-norm of residual
53f55353a2SBarry Smith -  dummy -  a viewer
545ed2d596SBarry Smith 
555ed2d596SBarry Smith    Level: intermediate
565ed2d596SBarry Smith 
575ed2d596SBarry Smith .keywords: SNES, nonlinear, vector, monitor, view
585ed2d596SBarry Smith 
59a6570f20SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorDefault(), VecView()
605ed2d596SBarry Smith @*/
61d43b4f6eSBarry Smith PetscErrorCode  SNESMonitorResidual(SNES snes,PetscInt its,PetscReal fgnorm,PetscViewerAndFormat *vf)
625ed2d596SBarry Smith {
63dfbe8321SBarry Smith   PetscErrorCode ierr;
645ed2d596SBarry Smith   Vec            x;
65d43b4f6eSBarry Smith   PetscViewer    viewer = vf->viewer;
665ed2d596SBarry Smith 
675ed2d596SBarry Smith   PetscFunctionBegin;
684d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
695ed2d596SBarry Smith   ierr = SNESGetFunction(snes,&x,0,0);CHKERRQ(ierr);
70d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
715ed2d596SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
72d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
735ed2d596SBarry Smith   PetscFunctionReturn(0);
745ed2d596SBarry Smith }
755ed2d596SBarry Smith 
765ed2d596SBarry Smith #undef __FUNCT__
77a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSolutionUpdate"
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 
90d132466eSBarry Smith    Level: intermediate
91d132466eSBarry Smith 
92d132466eSBarry Smith .keywords: SNES, nonlinear, vector, monitor, view
93d132466eSBarry Smith 
94a6570f20SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorDefault(), VecView()
95d132466eSBarry Smith @*/
96d43b4f6eSBarry Smith PetscErrorCode  SNESMonitorSolutionUpdate(SNES snes,PetscInt its,PetscReal fgnorm,PetscViewerAndFormat *vf)
97d132466eSBarry Smith {
98dfbe8321SBarry Smith   PetscErrorCode ierr;
99d132466eSBarry Smith   Vec            x;
100d43b4f6eSBarry Smith   PetscViewer    viewer = vf->viewer;
101d132466eSBarry Smith 
102d132466eSBarry Smith   PetscFunctionBegin;
1034d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
104d132466eSBarry Smith   ierr = SNESGetSolutionUpdate(snes,&x);CHKERRQ(ierr);
105d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
106d132466eSBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
107d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
108d132466eSBarry Smith   PetscFunctionReturn(0);
109d132466eSBarry Smith }
110d132466eSBarry Smith 
1114a2ae208SSatish Balay #undef __FUNCT__
112a5c2985bSBarry Smith #define __FUNCT__ "KSPMonitorSNES"
113a5c2985bSBarry Smith /*@C
114a5c2985bSBarry Smith    KSPMonitorSNES - Print the residual norm of the nonlinear function at each iteration of the linear iterative solver.
115a5c2985bSBarry Smith 
116a5c2985bSBarry Smith    Collective on KSP
117a5c2985bSBarry Smith 
118a5c2985bSBarry Smith    Input Parameters:
119a5c2985bSBarry Smith +  ksp   - iterative context
120a5c2985bSBarry Smith .  n     - iteration number
121a5c2985bSBarry Smith .  rnorm - 2-norm (preconditioned) residual value (may be estimated).
122a5c2985bSBarry Smith -  dummy - unused monitor context
123a5c2985bSBarry Smith 
124a5c2985bSBarry Smith    Level: intermediate
125a5c2985bSBarry Smith 
126a5c2985bSBarry Smith .keywords: KSP, default, monitor, residual
127a5c2985bSBarry Smith 
128a5c2985bSBarry Smith .seealso: KSPMonitorSet(), KSPMonitorTrueResidualNorm(), KSPMonitorLGResidualNormCreate()
129a5c2985bSBarry Smith @*/
130a5c2985bSBarry Smith PetscErrorCode  KSPMonitorSNES(KSP ksp,PetscInt n,PetscReal rnorm,void *dummy)
131a5c2985bSBarry Smith {
132a5c2985bSBarry Smith   PetscErrorCode ierr;
133a5c2985bSBarry Smith   PetscViewer    viewer;
134a5c2985bSBarry Smith   SNES           snes = (SNES) dummy;
135a5c2985bSBarry Smith   Vec            snes_solution,work1,work2;
136a5c2985bSBarry Smith   PetscReal      snorm;
137a5c2985bSBarry Smith 
138a5c2985bSBarry Smith   PetscFunctionBegin;
139a5c2985bSBarry Smith   ierr = SNESGetSolution(snes,&snes_solution);CHKERRQ(ierr);
140a5c2985bSBarry Smith   ierr = VecDuplicate(snes_solution,&work1);CHKERRQ(ierr);
141a5c2985bSBarry Smith   ierr = VecDuplicate(snes_solution,&work2);CHKERRQ(ierr);
142a5c2985bSBarry Smith   ierr = KSPBuildSolution(ksp,work1,NULL);CHKERRQ(ierr);
143a5c2985bSBarry Smith   ierr = VecAYPX(work1,-1.0,snes_solution);CHKERRQ(ierr);
144a5c2985bSBarry Smith   ierr = SNESComputeFunction(snes,work1,work2);CHKERRQ(ierr);
145a5c2985bSBarry Smith   ierr = VecNorm(work2,NORM_2,&snorm);CHKERRQ(ierr);
146a5c2985bSBarry Smith   ierr = VecDestroy(&work1);CHKERRQ(ierr);
147a5c2985bSBarry Smith   ierr = VecDestroy(&work2);CHKERRQ(ierr);
148a5c2985bSBarry Smith 
149a5c2985bSBarry Smith   ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ksp),&viewer);CHKERRQ(ierr);
150a5c2985bSBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ksp)->tablevel);CHKERRQ(ierr);
151a5c2985bSBarry Smith   if (n == 0 && ((PetscObject)ksp)->prefix) {
152a5c2985bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  Residual norms for %s solve.\n",((PetscObject)ksp)->prefix);CHKERRQ(ierr);
153a5c2985bSBarry Smith   }
154a5c2985bSBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Residual norm %5.3e KSP Residual norm %5.3e \n",n,(double)snorm,(double)rnorm);CHKERRQ(ierr);
155a5c2985bSBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ksp)->tablevel);CHKERRQ(ierr);
156a5c2985bSBarry Smith   PetscFunctionReturn(0);
157a5c2985bSBarry Smith }
158a5c2985bSBarry Smith 
159e5f7ee39SBarry Smith #include <petscdraw.h>
160e5f7ee39SBarry Smith 
161e5f7ee39SBarry Smith #undef __FUNCT__
162e5f7ee39SBarry Smith #define __FUNCT__ "KSPMonitorSNESLGResidualNormCreate"
163e5f7ee39SBarry Smith /*@C
164e5f7ee39SBarry Smith    KSPMonitorSNESLGResidualNormCreate - Creates a line graph context for use with
165e5f7ee39SBarry Smith    KSP to monitor convergence of preconditioned residual norms.
166e5f7ee39SBarry Smith 
167e5f7ee39SBarry Smith    Collective on KSP
168e5f7ee39SBarry Smith 
169e5f7ee39SBarry Smith    Input Parameters:
1708b0b5a47SLisandro Dalcin +  comm - communicator context
1718b0b5a47SLisandro Dalcin .  host - the X display to open, or null for the local machine
172e5f7ee39SBarry Smith .  label - the title to put in the title bar
173e5f7ee39SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of
174e5f7ee39SBarry Smith           the window
175e5f7ee39SBarry Smith -  m, n - the screen width and height in pixels
176e5f7ee39SBarry Smith 
177e5f7ee39SBarry Smith    Output Parameter:
178e5f7ee39SBarry Smith .  draw - the drawing context
179e5f7ee39SBarry Smith 
180e5f7ee39SBarry Smith    Options Database Key:
181e5f7ee39SBarry Smith .  -ksp_monitor_lg_residualnorm - Sets line graph monitor
182e5f7ee39SBarry Smith 
183e5f7ee39SBarry Smith    Notes:
184e5f7ee39SBarry Smith    Use KSPMonitorSNESLGResidualNormDestroy() to destroy this line graph; do not use PetscDrawLGDestroy().
185e5f7ee39SBarry Smith 
186e5f7ee39SBarry Smith    Level: intermediate
187e5f7ee39SBarry Smith 
188e5f7ee39SBarry Smith .keywords: KSP, monitor, line graph, residual, create
189e5f7ee39SBarry Smith 
190e5f7ee39SBarry Smith .seealso: KSPMonitorSNESLGResidualNormDestroy(), KSPMonitorSet(), KSPMonitorSNESLGTrueResidualCreate()
191e5f7ee39SBarry Smith @*/
1928b0b5a47SLisandro Dalcin PetscErrorCode  KSPMonitorSNESLGResidualNormCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscObject **objs)
193e5f7ee39SBarry Smith {
194e5f7ee39SBarry Smith   PetscDraw      draw;
195e5f7ee39SBarry Smith   PetscErrorCode ierr;
196e5f7ee39SBarry Smith   PetscDrawAxis  axis;
197c36cb520SLisandro Dalcin   PetscDrawLG    lg;
198e5f7ee39SBarry Smith   const char     *names[] = {"Linear residual","Nonlinear residual"};
199e5f7ee39SBarry Smith 
200e5f7ee39SBarry Smith   PetscFunctionBegin;
2018b0b5a47SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
202e5f7ee39SBarry Smith   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
203c36cb520SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,2,&lg);CHKERRQ(ierr);
204c36cb520SLisandro Dalcin   ierr = PetscDrawLGSetLegend(lg,names);CHKERRQ(ierr);
205c36cb520SLisandro Dalcin   ierr = PetscDrawLGSetFromOptions(lg);CHKERRQ(ierr);
206c36cb520SLisandro Dalcin   ierr = PetscDrawLGGetAxis(lg,&axis);CHKERRQ(ierr);
207e5f7ee39SBarry Smith   ierr = PetscDrawAxisSetLabels(axis,"Convergence of Residual Norm","Iteration","Residual Norm");CHKERRQ(ierr);
208c36cb520SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
209e5f7ee39SBarry Smith 
210c36cb520SLisandro Dalcin   ierr = PetscMalloc1(2,objs);CHKERRQ(ierr);
211c36cb520SLisandro Dalcin   (*objs)[1] = (PetscObject)lg;
212e5f7ee39SBarry Smith   PetscFunctionReturn(0);
213e5f7ee39SBarry Smith }
214e5f7ee39SBarry Smith 
215e5f7ee39SBarry Smith #undef __FUNCT__
216e5f7ee39SBarry Smith #define __FUNCT__ "KSPMonitorSNESLGResidualNorm"
217e5f7ee39SBarry Smith PetscErrorCode  KSPMonitorSNESLGResidualNorm(KSP ksp,PetscInt n,PetscReal rnorm,PetscObject *objs)
218e5f7ee39SBarry Smith {
219c36cb520SLisandro Dalcin   SNES           snes = (SNES) objs[0];
220e5f7ee39SBarry Smith   PetscDrawLG    lg   = (PetscDrawLG) objs[1];
221e5f7ee39SBarry Smith   PetscErrorCode ierr;
222e5f7ee39SBarry Smith   PetscReal      y[2];
223e5f7ee39SBarry Smith   Vec            snes_solution,work1,work2;
224e5f7ee39SBarry Smith 
225e5f7ee39SBarry Smith   PetscFunctionBegin;
226e5f7ee39SBarry Smith   if (rnorm > 0.0) y[0] = PetscLog10Real(rnorm);
227e5f7ee39SBarry Smith   else y[0] = -15.0;
228e5f7ee39SBarry Smith 
229e5f7ee39SBarry Smith   ierr = SNESGetSolution(snes,&snes_solution);CHKERRQ(ierr);
230e5f7ee39SBarry Smith   ierr = VecDuplicate(snes_solution,&work1);CHKERRQ(ierr);
231e5f7ee39SBarry Smith   ierr = VecDuplicate(snes_solution,&work2);CHKERRQ(ierr);
232e5f7ee39SBarry Smith   ierr = KSPBuildSolution(ksp,work1,NULL);CHKERRQ(ierr);
233e5f7ee39SBarry Smith   ierr = VecAYPX(work1,-1.0,snes_solution);CHKERRQ(ierr);
234e5f7ee39SBarry Smith   ierr = SNESComputeFunction(snes,work1,work2);CHKERRQ(ierr);
235e5f7ee39SBarry Smith   ierr = VecNorm(work2,NORM_2,y+1);CHKERRQ(ierr);
236e5f7ee39SBarry Smith   if (y[1] > 0.0) y[1] = PetscLog10Real(y[1]);
237e5f7ee39SBarry Smith   else y[1] = -15.0;
238e5f7ee39SBarry Smith   ierr = VecDestroy(&work1);CHKERRQ(ierr);
239e5f7ee39SBarry Smith   ierr = VecDestroy(&work2);CHKERRQ(ierr);
240e5f7ee39SBarry Smith 
241e5f7ee39SBarry Smith   ierr = PetscDrawLGAddPoint(lg,NULL,y);CHKERRQ(ierr);
2426934998bSLisandro Dalcin   if (n < 20 || !(n % 5) || snes->reason) {
243e5f7ee39SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2446934998bSLisandro Dalcin     ierr = PetscDrawLGSave(lg);CHKERRQ(ierr);
245e5f7ee39SBarry Smith   }
246e5f7ee39SBarry Smith   PetscFunctionReturn(0);
247e5f7ee39SBarry Smith }
248e5f7ee39SBarry Smith 
249e5f7ee39SBarry Smith #undef __FUNCT__
250e5f7ee39SBarry Smith #define __FUNCT__ "KSPMonitorSNESLGResidualNormDestroy"
251e5f7ee39SBarry Smith /*@
252e5f7ee39SBarry Smith    KSPMonitorSNESLGResidualNormDestroy - Destroys a line graph context that was created
253e5f7ee39SBarry Smith    with KSPMonitorSNESLGResidualNormCreate().
254e5f7ee39SBarry Smith 
255e5f7ee39SBarry Smith    Collective on KSP
256e5f7ee39SBarry Smith 
257e5f7ee39SBarry Smith    Input Parameter:
258e5f7ee39SBarry Smith .  draw - the drawing context
259e5f7ee39SBarry Smith 
260e5f7ee39SBarry Smith    Level: intermediate
261e5f7ee39SBarry Smith 
262e5f7ee39SBarry Smith .keywords: KSP, monitor, line graph, destroy
263e5f7ee39SBarry Smith 
264e5f7ee39SBarry Smith .seealso: KSPMonitorSNESLGResidualNormCreate(), KSPMonitorSNESLGTrueResidualDestroy(), KSPMonitorSet()
265e5f7ee39SBarry Smith @*/
266e5f7ee39SBarry Smith PetscErrorCode  KSPMonitorSNESLGResidualNormDestroy(PetscObject **objs)
267e5f7ee39SBarry Smith {
268e5f7ee39SBarry Smith   PetscErrorCode ierr;
269c36cb520SLisandro Dalcin   PetscDrawLG    lg = (PetscDrawLG) (*objs)[1];
270e5f7ee39SBarry Smith 
271e5f7ee39SBarry Smith   PetscFunctionBegin;
272c36cb520SLisandro Dalcin   ierr = PetscDrawLGDestroy(&lg);CHKERRQ(ierr);
273e5f7ee39SBarry Smith   ierr = PetscFree(*objs);CHKERRQ(ierr);
274e5f7ee39SBarry Smith   PetscFunctionReturn(0);
275e5f7ee39SBarry Smith }
276e5f7ee39SBarry Smith 
277a5c2985bSBarry Smith #undef __FUNCT__
278a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorDefault"
2794b828684SBarry Smith /*@C
280a6570f20SBarry Smith    SNESMonitorDefault - Monitors progress of the SNES solvers (default).
281e7e93795SLois Curfman McInnes 
282c7afd0dbSLois Curfman McInnes    Collective on SNES
283c7afd0dbSLois Curfman McInnes 
284e7e93795SLois Curfman McInnes    Input Parameters:
285c7afd0dbSLois Curfman McInnes +  snes - the SNES context
286e7e93795SLois Curfman McInnes .  its - iteration number
2874b27c08aSLois Curfman McInnes .  fgnorm - 2-norm of residual
288d43b4f6eSBarry Smith -  vf - viewer and format structure
289fee21e36SBarry Smith 
290e7e93795SLois Curfman McInnes    Notes:
2914b27c08aSLois Curfman McInnes    This routine prints the residual norm at each iteration.
292e7e93795SLois Curfman McInnes 
29336851e7fSLois Curfman McInnes    Level: intermediate
29436851e7fSLois Curfman McInnes 
295e7e93795SLois Curfman McInnes .keywords: SNES, nonlinear, default, monitor, norm
296e7e93795SLois Curfman McInnes 
297a6570f20SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorSolution()
298e7e93795SLois Curfman McInnes @*/
299d43b4f6eSBarry Smith PetscErrorCode  SNESMonitorDefault(SNES snes,PetscInt its,PetscReal fgnorm,PetscViewerAndFormat *vf)
300e7e93795SLois Curfman McInnes {
301dfbe8321SBarry Smith   PetscErrorCode ierr;
302d43b4f6eSBarry Smith   PetscViewer    viewer = vf->viewer;
303d132466eSBarry Smith 
3043a40ed3dSBarry Smith   PetscFunctionBegin;
3054d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
306d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
307649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
308649052a6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %14.12e \n",its,(double)fgnorm);CHKERRQ(ierr);
309649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
310d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr) ;
3113a40ed3dSBarry Smith   PetscFunctionReturn(0);
312e7e93795SLois Curfman McInnes }
3133f1db9ecSBarry Smith 
314b271bb04SBarry Smith #undef __FUNCT__
3152e7541e6SPeter Brune #define __FUNCT__ "SNESMonitorJacUpdateSpectrum"
316d43b4f6eSBarry Smith PetscErrorCode SNESMonitorJacUpdateSpectrum(SNES snes,PetscInt it,PetscReal fnorm,PetscViewerAndFormat *vf)
317a80ad3e0SBarry Smith {
318196da8b6SPeter Brune #if defined(PETSC_MISSING_LAPACK_GEEV)
319ce94432eSBarry Smith   SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_SUP,"GEEV - Lapack routine is unavailable\nNot able to provide eigen values.");
320196da8b6SPeter Brune #elif defined(PETSC_HAVE_ESSL)
321ce94432eSBarry Smith   SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_SUP,"GEEV - No support for ESSL Lapack Routines");
322196da8b6SPeter Brune #else
3232e7541e6SPeter Brune   Vec            X;
3242e7541e6SPeter Brune   Mat            J,dJ,dJdense;
3252e7541e6SPeter Brune   PetscErrorCode ierr;
326d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
3272e7541e6SPeter Brune   PetscInt       n,i;
3282e7541e6SPeter Brune   PetscBLASInt   nb,lwork;
3292e7541e6SPeter Brune   PetscReal      *eigr,*eigi;
3302e7541e6SPeter Brune   PetscScalar    *work;
3312e7541e6SPeter Brune   PetscScalar    *a;
3322e7541e6SPeter Brune 
3332e7541e6SPeter Brune   PetscFunctionBegin;
3342e7541e6SPeter Brune   if (it == 0) PetscFunctionReturn(0);
3352e7541e6SPeter Brune   /* create the difference between the current update and the current jacobian */
3362e7541e6SPeter Brune   ierr = SNESGetSolution(snes,&X);CHKERRQ(ierr);
337d1e9a80fSBarry Smith   ierr = SNESGetJacobian(snes,NULL,&J,&func,NULL);CHKERRQ(ierr);
3382e7541e6SPeter Brune   ierr = MatDuplicate(J,MAT_COPY_VALUES,&dJ);CHKERRQ(ierr);
339d1e9a80fSBarry Smith   ierr = SNESComputeJacobian(snes,X,dJ,dJ);CHKERRQ(ierr);
3402e7541e6SPeter Brune   ierr = MatAXPY(dJ,-1.0,J,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
341f5af7f23SKarl Rupp 
3422e7541e6SPeter Brune   /* compute the spectrum directly */
3432e7541e6SPeter Brune   ierr  = MatConvert(dJ,MATSEQDENSE,MAT_INITIAL_MATRIX,&dJdense);CHKERRQ(ierr);
3440298fd71SBarry Smith   ierr  = MatGetSize(dJ,&n,NULL);CHKERRQ(ierr);
345c5df96a5SBarry Smith   ierr  = PetscBLASIntCast(n,&nb);CHKERRQ(ierr);
3462e7541e6SPeter Brune   lwork = 3*nb;
347785e854fSJed Brown   ierr  = PetscMalloc1(n,&eigr);CHKERRQ(ierr);
348785e854fSJed Brown   ierr  = PetscMalloc1(n,&eigi);CHKERRQ(ierr);
349785e854fSJed Brown   ierr  = PetscMalloc1(lwork,&work);CHKERRQ(ierr);
3508c778c55SBarry Smith   ierr  = MatDenseGetArray(dJdense,&a);CHKERRQ(ierr);
3512e7541e6SPeter Brune #if !defined(PETSC_USE_COMPLEX)
3522e7541e6SPeter Brune   {
3532e7541e6SPeter Brune     PetscBLASInt lierr;
3542e7541e6SPeter Brune     ierr = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr);
3558b83055fSJed Brown     PetscStackCallBLAS("LAPACKgeev",LAPACKgeev_("N","N",&nb,a,&nb,eigr,eigi,NULL,&nb,NULL,&nb,work,&lwork,&lierr));
3562e7541e6SPeter Brune     if (lierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"geev() error %d",lierr);
3572e7541e6SPeter Brune     ierr = PetscFPTrapPop();CHKERRQ(ierr);
3582e7541e6SPeter Brune   }
3592e7541e6SPeter Brune #else
3602e7541e6SPeter Brune   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not coded for complex");
3612e7541e6SPeter Brune #endif
362fde5950dSBarry Smith   ierr = PetscPrintf(PetscObjectComm((PetscObject)snes),"Eigenvalues of J_%d - J_%d:\n",it,it-1);CHKERRQ(ierr);
3632e7541e6SPeter Brune   for (i=0;i<n;i++) {
364fde5950dSBarry Smith     ierr = PetscPrintf(PetscObjectComm((PetscObject)snes),"%5d: %20.5g + %20.5gi\n",i,(double)eigr[i],(double)eigi[i]);CHKERRQ(ierr);
3652e7541e6SPeter Brune   }
3668c778c55SBarry Smith   ierr = MatDenseRestoreArray(dJdense,&a);CHKERRQ(ierr);
3672e7541e6SPeter Brune   ierr = MatDestroy(&dJ);CHKERRQ(ierr);
3682e7541e6SPeter Brune   ierr = MatDestroy(&dJdense);CHKERRQ(ierr);
3692e7541e6SPeter Brune   ierr = PetscFree(eigr);CHKERRQ(ierr);
3702e7541e6SPeter Brune   ierr = PetscFree(eigi);CHKERRQ(ierr);
3712e7541e6SPeter Brune   ierr = PetscFree(work);CHKERRQ(ierr);
3722e7541e6SPeter Brune   PetscFunctionReturn(0);
373196da8b6SPeter Brune #endif
3742e7541e6SPeter Brune }
3752e7541e6SPeter Brune 
3766ba87a44SLisandro Dalcin PETSC_INTERN PetscErrorCode  SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
3776ba87a44SLisandro Dalcin 
3782e7541e6SPeter Brune #undef __FUNCT__
379b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorRange_Private"
3807087cfbeSBarry Smith PetscErrorCode  SNESMonitorRange_Private(SNES snes,PetscInt it,PetscReal *per)
381b271bb04SBarry Smith {
382b271bb04SBarry Smith   PetscErrorCode ierr;
383b271bb04SBarry Smith   Vec            resid;
384b271bb04SBarry Smith   PetscReal      rmax,pwork;
385b271bb04SBarry Smith   PetscInt       i,n,N;
386b271bb04SBarry Smith   PetscScalar    *r;
387b271bb04SBarry Smith 
388b271bb04SBarry Smith   PetscFunctionBegin;
389b271bb04SBarry Smith   ierr  = SNESGetFunction(snes,&resid,0,0);CHKERRQ(ierr);
390b271bb04SBarry Smith   ierr  = VecNorm(resid,NORM_INFINITY,&rmax);CHKERRQ(ierr);
391b271bb04SBarry Smith   ierr  = VecGetLocalSize(resid,&n);CHKERRQ(ierr);
392b271bb04SBarry Smith   ierr  = VecGetSize(resid,&N);CHKERRQ(ierr);
393b271bb04SBarry Smith   ierr  = VecGetArray(resid,&r);CHKERRQ(ierr);
394b271bb04SBarry Smith   pwork = 0.0;
395b271bb04SBarry Smith   for (i=0; i<n; i++) {
396b271bb04SBarry Smith     pwork += (PetscAbsScalar(r[i]) > .20*rmax);
397b271bb04SBarry Smith   }
398b2566f29SBarry Smith   ierr = MPIU_Allreduce(&pwork,per,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)snes));CHKERRQ(ierr);
399b271bb04SBarry Smith   ierr = VecRestoreArray(resid,&r);CHKERRQ(ierr);
400b271bb04SBarry Smith   *per = *per/N;
401b271bb04SBarry Smith   PetscFunctionReturn(0);
402b271bb04SBarry Smith }
403b271bb04SBarry Smith 
404b271bb04SBarry Smith #undef __FUNCT__
405b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorRange"
406b271bb04SBarry Smith /*@C
407b271bb04SBarry Smith    SNESMonitorRange - Prints the percentage of residual elements that are more then 10 percent of the maximum value.
408b271bb04SBarry Smith 
409b271bb04SBarry Smith    Collective on SNES
410b271bb04SBarry Smith 
411b271bb04SBarry Smith    Input Parameters:
412b271bb04SBarry Smith +  snes   - iterative context
413b271bb04SBarry Smith .  it    - iteration number
414b271bb04SBarry Smith .  rnorm - 2-norm (preconditioned) residual value (may be estimated).
415b271bb04SBarry Smith -  dummy - unused monitor context
416b271bb04SBarry Smith 
417b271bb04SBarry Smith    Options Database Key:
418b271bb04SBarry Smith .  -snes_monitor_range - Activates SNESMonitorRange()
419b271bb04SBarry Smith 
420b271bb04SBarry Smith    Level: intermediate
421b271bb04SBarry Smith 
422b271bb04SBarry Smith .keywords: SNES, default, monitor, residual
423b271bb04SBarry Smith 
424b271bb04SBarry Smith .seealso: SNESMonitorSet(), SNESMonitorDefault(), SNESMonitorLGCreate()
425b271bb04SBarry Smith @*/
426d43b4f6eSBarry Smith PetscErrorCode  SNESMonitorRange(SNES snes,PetscInt it,PetscReal rnorm,PetscViewerAndFormat *vf)
427b271bb04SBarry Smith {
428b271bb04SBarry Smith   PetscErrorCode ierr;
429b271bb04SBarry Smith   PetscReal      perc,rel;
430d43b4f6eSBarry Smith   PetscViewer    viewer = vf->viewer;
431b271bb04SBarry Smith   /* should be in a MonitorRangeContext */
432b271bb04SBarry Smith   static PetscReal prev;
433b271bb04SBarry Smith 
434b271bb04SBarry Smith   PetscFunctionBegin;
4354d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
436b271bb04SBarry Smith   if (!it) prev = rnorm;
437b271bb04SBarry Smith   ierr = SNESMonitorRange_Private(snes,it,&perc);CHKERRQ(ierr);
438b271bb04SBarry Smith 
439b271bb04SBarry Smith   rel  = (prev - rnorm)/prev;
440b271bb04SBarry Smith   prev = rnorm;
441d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
442649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
4436712e2f1SBarry 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);
444649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
445d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
446b271bb04SBarry Smith   PetscFunctionReturn(0);
447b271bb04SBarry Smith }
448b271bb04SBarry Smith 
4493a7fca6bSBarry Smith #undef __FUNCT__
450a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorRatio"
4513a7fca6bSBarry Smith /*@C
452a6570f20SBarry Smith    SNESMonitorRatio - Monitors progress of the SNES solvers by printing the ratio
4534b27c08aSLois Curfman McInnes    of residual norm at each iteration to the previous.
4543a7fca6bSBarry Smith 
4553a7fca6bSBarry Smith    Collective on SNES
4563a7fca6bSBarry Smith 
4573a7fca6bSBarry Smith    Input Parameters:
4583a7fca6bSBarry Smith +  snes - the SNES context
4593a7fca6bSBarry Smith .  its - iteration number
4603a7fca6bSBarry Smith .  fgnorm - 2-norm of residual (or gradient)
461eabae89aSBarry Smith -  dummy -  context of monitor
4623a7fca6bSBarry Smith 
4633a7fca6bSBarry Smith    Level: intermediate
4643a7fca6bSBarry Smith 
465fde5950dSBarry Smith    Notes: Insure that SNESMonitorRatio() is called when you set this monitor
4663a7fca6bSBarry Smith .keywords: SNES, nonlinear, monitor, norm
4673a7fca6bSBarry Smith 
468fde5950dSBarry Smith .seealso: SNESMonitorSet(), SNESMonitorSolution(), SNESMonitorRatio()
4693a7fca6bSBarry Smith @*/
470d43b4f6eSBarry Smith PetscErrorCode  SNESMonitorRatio(SNES snes,PetscInt its,PetscReal fgnorm,PetscViewerAndFormat *vf)
4713a7fca6bSBarry Smith {
472dfbe8321SBarry Smith   PetscErrorCode          ierr;
47377431f27SBarry Smith   PetscInt                len;
47487828ca2SBarry Smith   PetscReal               *history;
475d43b4f6eSBarry Smith   PetscViewer             viewer = vf->viewer;
4763a7fca6bSBarry Smith 
4773a7fca6bSBarry Smith   PetscFunctionBegin;
4780298fd71SBarry Smith   ierr = SNESGetConvergenceHistory(snes,&history,NULL,&len);CHKERRQ(ierr);
479d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
480fde5950dSBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
481958c9bccSBarry Smith   if (!its || !history || its > len) {
482fde5950dSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %14.12e \n",its,(double)fgnorm);CHKERRQ(ierr);
4833a7fca6bSBarry Smith   } else {
48487828ca2SBarry Smith     PetscReal ratio = fgnorm/history[its-1];
485fde5950dSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %14.12e %14.12e \n",its,(double)fgnorm,(double)ratio);CHKERRQ(ierr);
4863a7fca6bSBarry Smith   }
487fde5950dSBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
488d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
4893a7fca6bSBarry Smith   PetscFunctionReturn(0);
4903a7fca6bSBarry Smith }
4913a7fca6bSBarry Smith 
4923a7fca6bSBarry Smith #undef __FUNCT__
493fde5950dSBarry Smith #define __FUNCT__ "SNESMonitorRatioSetUp"
4943a7fca6bSBarry Smith /*@C
495fde5950dSBarry Smith    SNESMonitorRatioSetUp - Insures the SNES object is saving its history since this monitor needs access to it
4963a7fca6bSBarry Smith 
4973a7fca6bSBarry Smith    Collective on SNES
4983a7fca6bSBarry Smith 
4993a7fca6bSBarry Smith    Input Parameters:
500eabae89aSBarry Smith +   snes - the SNES context
501fde5950dSBarry Smith -   viewer - the PetscViewer object (ignored)
5023a7fca6bSBarry Smith 
5033a7fca6bSBarry Smith    Level: intermediate
5043a7fca6bSBarry Smith 
5053a7fca6bSBarry Smith .keywords: SNES, nonlinear, monitor, norm
5063a7fca6bSBarry Smith 
507fde5950dSBarry Smith .seealso: SNESMonitorSet(), SNESMonitorSolution(), SNESMonitorDefault(), SNESMonitorRatio()
5083a7fca6bSBarry Smith @*/
509d43b4f6eSBarry Smith PetscErrorCode  SNESMonitorRatioSetUp(SNES snes,PetscViewerAndFormat *vf)
5103a7fca6bSBarry Smith {
511dfbe8321SBarry Smith   PetscErrorCode          ierr;
51287828ca2SBarry Smith   PetscReal               *history;
5133a7fca6bSBarry Smith 
5143a7fca6bSBarry Smith   PetscFunctionBegin;
5150298fd71SBarry Smith   ierr = SNESGetConvergenceHistory(snes,&history,NULL,NULL);CHKERRQ(ierr);
5163a7fca6bSBarry Smith   if (!history) {
517fde5950dSBarry Smith     ierr = SNESSetConvergenceHistory(snes,NULL,NULL,100,PETSC_TRUE);CHKERRQ(ierr);
5183a7fca6bSBarry Smith   }
5193a7fca6bSBarry Smith   PetscFunctionReturn(0);
5203a7fca6bSBarry Smith }
5213a7fca6bSBarry Smith 
522e7e93795SLois Curfman McInnes /* ---------------------------------------------------------------- */
5234a2ae208SSatish Balay #undef __FUNCT__
524a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorDefaultShort"
525be1f7002SBarry Smith /*
526a6570f20SBarry Smith      Default (short) SNES Monitor, same as SNESMonitorDefault() except
527be1f7002SBarry Smith   it prints fewer digits of the residual as the residual gets smaller.
528be1f7002SBarry Smith   This is because the later digits are meaningless and are often
529be1f7002SBarry Smith   different on different machines; by using this routine different
530be1f7002SBarry Smith   machines will usually generate the same output.
531be1f7002SBarry Smith */
532d43b4f6eSBarry Smith PetscErrorCode  SNESMonitorDefaultShort(SNES snes,PetscInt its,PetscReal fgnorm,PetscViewerAndFormat *vf)
533e7e93795SLois Curfman McInnes {
534dfbe8321SBarry Smith   PetscErrorCode ierr;
535d43b4f6eSBarry Smith   PetscViewer    viewer = vf->viewer;
536d132466eSBarry Smith 
5373a40ed3dSBarry Smith   PetscFunctionBegin;
5384d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
539d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
540649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
5418f240d10SBarry Smith   if (fgnorm > 1.e-9) {
5428fa295daSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %g \n",its,(double)fgnorm);CHKERRQ(ierr);
5433a40ed3dSBarry Smith   } else if (fgnorm > 1.e-11) {
5448fa295daSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm %5.3e \n",its,(double)fgnorm);CHKERRQ(ierr);
5453a40ed3dSBarry Smith   } else {
546649052a6SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%3D SNES Function norm < 1.e-11\n",its);CHKERRQ(ierr);
547a34d58ebSBarry Smith   }
548649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr);
549d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
5503a40ed3dSBarry Smith   PetscFunctionReturn(0);
551e7e93795SLois Curfman McInnes }
5522db13446SMatthew G. Knepley 
5532db13446SMatthew G. Knepley #undef __FUNCT__
5542db13446SMatthew G. Knepley #define __FUNCT__ "SNESMonitorDefaultField"
5552db13446SMatthew G. Knepley /*@C
5562db13446SMatthew G. Knepley   SNESMonitorDefaultField - Monitors progress of the SNES solvers, separated into fields.
5572db13446SMatthew G. Knepley 
5582db13446SMatthew G. Knepley   Collective on SNES
5592db13446SMatthew G. Knepley 
5602db13446SMatthew G. Knepley   Input Parameters:
5612db13446SMatthew G. Knepley + snes   - the SNES context
5622db13446SMatthew G. Knepley . its    - iteration number
5632db13446SMatthew G. Knepley . fgnorm - 2-norm of residual
5642db13446SMatthew G. Knepley - ctx    - the PetscViewer
5652db13446SMatthew G. Knepley 
5662db13446SMatthew G. Knepley   Notes:
5672db13446SMatthew G. Knepley   This routine uses the DM attached to the residual vector
5682db13446SMatthew G. Knepley 
5692db13446SMatthew G. Knepley   Level: intermediate
5702db13446SMatthew G. Knepley 
5712db13446SMatthew G. Knepley .keywords: SNES, nonlinear, field, monitor, norm
5722db13446SMatthew G. Knepley .seealso: SNESMonitorSet(), SNESMonitorSolution(), SNESMonitorDefault(), SNESMonitorDefaultShort()
5732db13446SMatthew G. Knepley @*/
574d43b4f6eSBarry Smith PetscErrorCode SNESMonitorDefaultField(SNES snes, PetscInt its, PetscReal fgnorm, PetscViewerAndFormat *vf)
5752db13446SMatthew G. Knepley {
576d43b4f6eSBarry Smith   PetscViewer    viewer = vf->viewer;
5772db13446SMatthew G. Knepley   Vec            r;
5782db13446SMatthew G. Knepley   DM             dm;
5792db13446SMatthew G. Knepley   PetscReal      res[256];
5802db13446SMatthew G. Knepley   PetscInt       tablevel;
5812db13446SMatthew G. Knepley   PetscErrorCode ierr;
5822db13446SMatthew G. Knepley 
5832db13446SMatthew G. Knepley   PetscFunctionBegin;
5844d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
5852db13446SMatthew G. Knepley   ierr = SNESGetFunction(snes, &r, NULL, NULL);CHKERRQ(ierr);
5862db13446SMatthew G. Knepley   ierr = VecGetDM(r, &dm);CHKERRQ(ierr);
587d43b4f6eSBarry Smith   if (!dm) {ierr = SNESMonitorDefault(snes, its, fgnorm, vf);CHKERRQ(ierr);}
5882db13446SMatthew G. Knepley   else {
5892db13446SMatthew G. Knepley     PetscSection s, gs;
5902db13446SMatthew G. Knepley     PetscInt     Nf, f;
5912db13446SMatthew G. Knepley 
5922db13446SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
5932db13446SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
594d43b4f6eSBarry Smith     if (!s || !gs) {ierr = SNESMonitorDefault(snes, its, fgnorm, vf);CHKERRQ(ierr);}
5952db13446SMatthew G. Knepley     ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
5962db13446SMatthew G. Knepley     if (Nf > 256) SETERRQ1(PetscObjectComm((PetscObject) snes), PETSC_ERR_SUP, "Do not support %d fields > 256", Nf);
5972db13446SMatthew G. Knepley     ierr = PetscSectionVecNorm(s, gs, r, NORM_2, res);CHKERRQ(ierr);
5982db13446SMatthew G. Knepley     ierr = PetscObjectGetTabLevel((PetscObject) snes, &tablevel);CHKERRQ(ierr);
599d43b4f6eSBarry Smith     ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
6002db13446SMatthew G. Knepley     ierr = PetscViewerASCIIAddTab(viewer, tablevel);CHKERRQ(ierr);
6012db13446SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr);
6022db13446SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
6032db13446SMatthew G. Knepley       if (f) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
6042db13446SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", res[f]);CHKERRQ(ierr);
6052db13446SMatthew G. Knepley     }
6062db13446SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "] \n");CHKERRQ(ierr);
6072db13446SMatthew G. Knepley     ierr = PetscViewerASCIISubtractTab(viewer, tablevel);CHKERRQ(ierr);
608d43b4f6eSBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
6092db13446SMatthew G. Knepley   }
6102db13446SMatthew G. Knepley   PetscFunctionReturn(0);
6112db13446SMatthew G. Knepley }
612e7e93795SLois Curfman McInnes /* ---------------------------------------------------------------- */
6134a2ae208SSatish Balay #undef __FUNCT__
6148d359177SBarry Smith #define __FUNCT__ "SNESConvergedDefault"
6154b828684SBarry Smith /*@C
6168d359177SBarry Smith    SNESConvergedDefault - Convergence test of the solvers for
617f525115eSLois Curfman McInnes    systems of nonlinear equations (default).
618e7e93795SLois Curfman McInnes 
619c7afd0dbSLois Curfman McInnes    Collective on SNES
620c7afd0dbSLois Curfman McInnes 
621e7e93795SLois Curfman McInnes    Input Parameters:
622c7afd0dbSLois Curfman McInnes +  snes - the SNES context
62306ee9f85SBarry Smith .  it - the iteration (0 indicates before any Newton steps)
624e7e93795SLois Curfman McInnes .  xnorm - 2-norm of current iterate
625c60f73f4SPeter Brune .  snorm - 2-norm of current step
6267f3332b4SBarry Smith .  fnorm - 2-norm of function at current iterate
627c7afd0dbSLois Curfman McInnes -  dummy - unused context
628e7e93795SLois Curfman McInnes 
629184914b5SBarry Smith    Output Parameter:
630184914b5SBarry Smith .   reason  - one of
63170441072SBarry Smith $  SNES_CONVERGED_FNORM_ABS       - (fnorm < abstol),
632c60f73f4SPeter Brune $  SNES_CONVERGED_SNORM_RELATIVE  - (snorm < stol*xnorm),
633184914b5SBarry Smith $  SNES_CONVERGED_FNORM_RELATIVE  - (fnorm < rtol*fnorm0),
634184914b5SBarry Smith $  SNES_DIVERGED_FUNCTION_COUNT   - (nfct > maxf),
635184914b5SBarry Smith $  SNES_DIVERGED_FNORM_NAN        - (fnorm == NaN),
636184914b5SBarry Smith $  SNES_CONVERGED_ITERATING       - (otherwise),
637e7e93795SLois Curfman McInnes 
638e7e93795SLois Curfman McInnes    where
639c7afd0dbSLois Curfman McInnes +    maxf - maximum number of function evaluations,
640c7afd0dbSLois Curfman McInnes             set with SNESSetTolerances()
641c7afd0dbSLois Curfman McInnes .    nfct - number of function evaluations,
64270441072SBarry Smith .    abstol - absolute function norm tolerance,
643c7afd0dbSLois Curfman McInnes             set with SNESSetTolerances()
644c7afd0dbSLois Curfman McInnes -    rtol - relative function norm tolerance, set with SNESSetTolerances()
645fee21e36SBarry Smith 
64636851e7fSLois Curfman McInnes    Level: intermediate
64736851e7fSLois Curfman McInnes 
648e7e93795SLois Curfman McInnes .keywords: SNES, nonlinear, default, converged, convergence
649e7e93795SLois Curfman McInnes 
65071f87433Sdalcinl .seealso: SNESSetConvergenceTest()
651e7e93795SLois Curfman McInnes @*/
6528d359177SBarry Smith PetscErrorCode  SNESConvergedDefault(SNES snes,PetscInt it,PetscReal xnorm,PetscReal snorm,PetscReal fnorm,SNESConvergedReason *reason,void *dummy)
653e7e93795SLois Curfman McInnes {
65463ba0a88SBarry Smith   PetscErrorCode ierr;
65563ba0a88SBarry Smith 
6563a40ed3dSBarry Smith   PetscFunctionBegin;
6570700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6583f149594SLisandro Dalcin   PetscValidPointer(reason,6);
6593f149594SLisandro Dalcin 
66006ee9f85SBarry Smith   *reason = SNES_CONVERGED_ITERATING;
66106ee9f85SBarry Smith 
66206ee9f85SBarry Smith   if (!it) {
66306ee9f85SBarry Smith     /* set parameter for default relative tolerance convergence test */
66406ee9f85SBarry Smith     snes->ttol   = fnorm*snes->rtol;
665*e37c518bSBarry Smith     snes->rnorm0 = fnorm;
66606ee9f85SBarry Smith   }
6678146f6ebSBarry Smith   if (PetscIsInfOrNanReal(fnorm)) {
668ae15b995SBarry Smith     ierr    = PetscInfo(snes,"Failed to converged, function norm is NaN\n");CHKERRQ(ierr);
669184914b5SBarry Smith     *reason = SNES_DIVERGED_FNORM_NAN;
67070441072SBarry Smith   } else if (fnorm < snes->abstol) {
6718f1a2a5eSBarry Smith     ierr    = PetscInfo2(snes,"Converged due to function norm %14.12e < %14.12e\n",(double)fnorm,(double)snes->abstol);CHKERRQ(ierr);
672184914b5SBarry Smith     *reason = SNES_CONVERGED_FNORM_ABS;
67343e71028SBarry Smith   } else if (snes->nfuncs >= snes->max_funcs) {
674ae15b995SBarry Smith     ierr    = PetscInfo2(snes,"Exceeded maximum number of function evaluations: %D > %D\n",snes->nfuncs,snes->max_funcs);CHKERRQ(ierr);
675184914b5SBarry Smith     *reason = SNES_DIVERGED_FUNCTION_COUNT;
67606ee9f85SBarry Smith   }
67706ee9f85SBarry Smith 
67806ee9f85SBarry Smith   if (it && !*reason) {
67906ee9f85SBarry Smith     if (fnorm <= snes->ttol) {
6808f1a2a5eSBarry Smith       ierr    = PetscInfo2(snes,"Converged due to function norm %14.12e < %14.12e (relative tolerance)\n",(double)fnorm,(double)snes->ttol);CHKERRQ(ierr);
68106ee9f85SBarry Smith       *reason = SNES_CONVERGED_FNORM_RELATIVE;
682c60f73f4SPeter Brune     } else if (snorm < snes->stol*xnorm) {
683c60f73f4SPeter 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);
684c60f73f4SPeter Brune       *reason = SNES_CONVERGED_SNORM_RELATIVE;
685*e37c518bSBarry Smith     } else if (fnorm > snes->divtol*snes->rnorm0) {
686*e37c518bSBarry 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);
687*e37c518bSBarry Smith       *reason = SNES_DIVERGED_DTOL;
68806ee9f85SBarry Smith     }
689*e37c518bSBarry Smith 
690e7e93795SLois Curfman McInnes   }
6913a40ed3dSBarry Smith   PetscFunctionReturn(0);
692e7e93795SLois Curfman McInnes }
6933f149594SLisandro Dalcin 
6943f149594SLisandro Dalcin #undef __FUNCT__
695e2a6519dSDmitry Karpeev #define __FUNCT__ "SNESConvergedSkip"
6963f149594SLisandro Dalcin /*@C
697e2a6519dSDmitry Karpeev    SNESConvergedSkip - Convergence test for SNES that NEVER returns as
6983f149594SLisandro Dalcin    converged, UNLESS the maximum number of iteration have been reached.
6993f149594SLisandro Dalcin 
7003f9fe445SBarry Smith    Logically Collective on SNES
7013f149594SLisandro Dalcin 
7023f149594SLisandro Dalcin    Input Parameters:
7033f149594SLisandro Dalcin +  snes - the SNES context
7043f149594SLisandro Dalcin .  it - the iteration (0 indicates before any Newton steps)
7053f149594SLisandro Dalcin .  xnorm - 2-norm of current iterate
706c60f73f4SPeter Brune .  snorm - 2-norm of current step
7073f149594SLisandro Dalcin .  fnorm - 2-norm of function at current iterate
7083f149594SLisandro Dalcin -  dummy - unused context
7093f149594SLisandro Dalcin 
7103f149594SLisandro Dalcin    Output Parameter:
71185385478SLisandro Dalcin .   reason  - SNES_CONVERGED_ITERATING, SNES_CONVERGED_ITS, or SNES_DIVERGED_FNORM_NAN
7123f149594SLisandro Dalcin 
7133f149594SLisandro Dalcin    Notes:
7143f149594SLisandro Dalcin    Convergence is then declared after a fixed number of iterations have been used.
7153f149594SLisandro Dalcin 
7163f149594SLisandro Dalcin    Level: advanced
7173f149594SLisandro Dalcin 
7183f149594SLisandro Dalcin .keywords: SNES, nonlinear, skip, converged, convergence
7193f149594SLisandro Dalcin 
7203f149594SLisandro Dalcin .seealso: SNESSetConvergenceTest()
7213f149594SLisandro Dalcin @*/
722e2a6519dSDmitry Karpeev PetscErrorCode  SNESConvergedSkip(SNES snes,PetscInt it,PetscReal xnorm,PetscReal snorm,PetscReal fnorm,SNESConvergedReason *reason,void *dummy)
7233f149594SLisandro Dalcin {
7243f149594SLisandro Dalcin   PetscErrorCode ierr;
7253f149594SLisandro Dalcin 
7263f149594SLisandro Dalcin   PetscFunctionBegin;
7270700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7283f149594SLisandro Dalcin   PetscValidPointer(reason,6);
7293f149594SLisandro Dalcin 
7303f149594SLisandro Dalcin   *reason = SNES_CONVERGED_ITERATING;
7313f149594SLisandro Dalcin 
7323f149594SLisandro Dalcin   if (fnorm != fnorm) {
7333f149594SLisandro Dalcin     ierr    = PetscInfo(snes,"Failed to converged, function norm is NaN\n");CHKERRQ(ierr);
7343f149594SLisandro Dalcin     *reason = SNES_DIVERGED_FNORM_NAN;
7353f149594SLisandro Dalcin   } else if (it == snes->max_its) {
7363f149594SLisandro Dalcin     *reason = SNES_CONVERGED_ITS;
7373f149594SLisandro Dalcin   }
7383f149594SLisandro Dalcin   PetscFunctionReturn(0);
7393f149594SLisandro Dalcin }
7403f149594SLisandro Dalcin 
74158c9b817SLisandro Dalcin #undef __FUNCT__
742fa0ddf94SBarry Smith #define __FUNCT__ "SNESSetWorkVecs"
7438d359177SBarry Smith /*@C
744fa0ddf94SBarry Smith   SNESSetWorkVecs - Gets a number of work vectors.
74558c9b817SLisandro Dalcin 
74658c9b817SLisandro Dalcin   Input Parameters:
74758c9b817SLisandro Dalcin . snes  - the SNES context
74858c9b817SLisandro Dalcin . nw - number of work vectors to allocate
74958c9b817SLisandro Dalcin 
75058c9b817SLisandro Dalcin    Level: developer
75158c9b817SLisandro Dalcin 
752fa0ddf94SBarry Smith    Developers Note: This is PETSC_EXTERN because it may be used by user written plugin SNES implementations
753fa0ddf94SBarry Smith 
75498acb6afSMatthew G Knepley @*/
755fa0ddf94SBarry Smith PetscErrorCode SNESSetWorkVecs(SNES snes,PetscInt nw)
75658c9b817SLisandro Dalcin {
757c5ed8070SMatthew G. Knepley   DM             dm;
758c5ed8070SMatthew G. Knepley   Vec            v;
75958c9b817SLisandro Dalcin   PetscErrorCode ierr;
76058c9b817SLisandro Dalcin 
76158c9b817SLisandro Dalcin   PetscFunctionBegin;
76258c9b817SLisandro Dalcin   if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);}
76358c9b817SLisandro Dalcin   snes->nwork = nw;
764f5af7f23SKarl Rupp 
765c5ed8070SMatthew G. Knepley   ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr);
766c5ed8070SMatthew G. Knepley   ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
767c5ed8070SMatthew G. Knepley   ierr = VecDuplicateVecs(v,snes->nwork,&snes->work);CHKERRQ(ierr);
768c5ed8070SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
76958c9b817SLisandro Dalcin   ierr = PetscLogObjectParents(snes,nw,snes->work);CHKERRQ(ierr);
77058c9b817SLisandro Dalcin   PetscFunctionReturn(0);
77158c9b817SLisandro Dalcin }
772