Actual source code: mfnmon.c

slepc-3.16.1 2021-11-17
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2021, Universitat Politecnica de Valencia, Spain

  6:    This file is part of SLEPc.
  7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
  8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  9: */
 10: /*
 11:    MFN routines related to monitors
 12: */

 14: #include <slepc/private/mfnimpl.h>
 15: #include <petscdraw.h>

 17: PetscErrorCode MFNMonitorLGCreate(MPI_Comm comm,const char host[],const char label[],const char metric[],PetscInt l,const char *names[],int x,int y,int m,int n,PetscDrawLG *lgctx)
 18: {
 19:   PetscDraw      draw;
 20:   PetscDrawAxis  axis;
 21:   PetscDrawLG    lg;

 25:   PetscDrawCreate(comm,host,label,x,y,m,n,&draw);
 26:   PetscDrawSetFromOptions(draw);
 27:   PetscDrawLGCreate(draw,l,&lg);
 28:   if (names) { PetscDrawLGSetLegend(lg,names); }
 29:   PetscDrawLGSetFromOptions(lg);
 30:   PetscDrawLGGetAxis(lg,&axis);
 31:   PetscDrawAxisSetLabels(axis,"Convergence","Iteration",metric);
 32:   PetscDrawDestroy(&draw);
 33:   *lgctx = lg;
 34:   return(0);
 35: }

 37: /*
 38:    Runs the user provided monitor routines, if any.
 39: */
 40: PetscErrorCode MFNMonitor(MFN mfn,PetscInt it,PetscReal errest)
 41: {
 43:   PetscInt       i,n = mfn->numbermonitors;

 46:   for (i=0;i<n;i++) {
 47:     (*mfn->monitor[i])(mfn,it,errest,mfn->monitorcontext[i]);
 48:   }
 49:   return(0);
 50: }

 52: /*@C
 53:    MFNMonitorSet - Sets an ADDITIONAL function to be called at every
 54:    iteration to monitor convergence.

 56:    Logically Collective on mfn

 58:    Input Parameters:
 59: +  mfn     - matrix function context obtained from MFNCreate()
 60: .  monitor - pointer to function (if this is NULL, it turns off monitoring)
 61: .  mctx    - [optional] context for private data for the
 62:              monitor routine (use NULL if no context is desired)
 63: -  monitordestroy - [optional] routine that frees monitor context (may be NULL)

 65:    Calling Sequence of monitor:
 66: $   monitor(MFN mfn,int its,PetscReal errest,void *mctx)

 68: +  mfn    - matrix function context obtained from MFNCreate()
 69: .  its    - iteration number
 70: .  errest - error estimate
 71: -  mctx   - optional monitoring context, as set by MFNMonitorSet()

 73:    Options Database Keys:
 74: +    -mfn_monitor - print the error estimate
 75: .    -mfn_monitor draw::draw_lg - sets line graph monitor for the error estimate
 76: -    -mfn_monitor_cancel - cancels all monitors that have been hardwired into
 77:       a code by calls to MFNMonitorSet(), but does not cancel those set via
 78:       the options database.

 80:    Notes:
 81:    Several different monitoring routines may be set by calling
 82:    MFNMonitorSet() multiple times; all will be called in the
 83:    order in which they were set.

 85:    Level: intermediate

 87: .seealso: MFNMonitorCancel()
 88: @*/
 89: PetscErrorCode MFNMonitorSet(MFN mfn,PetscErrorCode (*monitor)(MFN,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
 90: {
 93:   if (mfn->numbermonitors >= MAXMFNMONITORS) SETERRQ(PetscObjectComm((PetscObject)mfn),PETSC_ERR_ARG_OUTOFRANGE,"Too many MFN monitors set");
 94:   mfn->monitor[mfn->numbermonitors]           = monitor;
 95:   mfn->monitorcontext[mfn->numbermonitors]    = (void*)mctx;
 96:   mfn->monitordestroy[mfn->numbermonitors++]  = monitordestroy;
 97:   return(0);
 98: }

100: /*@
101:    MFNMonitorCancel - Clears all monitors for an MFN object.

103:    Logically Collective on mfn

105:    Input Parameters:
106: .  mfn - matrix function context obtained from MFNCreate()

108:    Options Database Key:
109: .    -mfn_monitor_cancel - cancels all monitors that have been hardwired
110:       into a code by calls to MFNMonitorSet(),
111:       but does not cancel those set via the options database.

113:    Level: intermediate

115: .seealso: MFNMonitorSet()
116: @*/
117: PetscErrorCode MFNMonitorCancel(MFN mfn)
118: {
120:   PetscInt       i;

124:   for (i=0; i<mfn->numbermonitors; i++) {
125:     if (mfn->monitordestroy[i]) {
126:       (*mfn->monitordestroy[i])(&mfn->monitorcontext[i]);
127:     }
128:   }
129:   mfn->numbermonitors = 0;
130:   return(0);
131: }

133: /*@C
134:    MFNGetMonitorContext - Gets the monitor context, as set by
135:    MFNMonitorSet() for the FIRST monitor only.

137:    Not Collective

139:    Input Parameter:
140: .  mfn - matrix function context obtained from MFNCreate()

142:    Output Parameter:
143: .  ctx - monitor context

145:    Level: intermediate

147: .seealso: MFNMonitorSet()
148: @*/
149: PetscErrorCode MFNGetMonitorContext(MFN mfn,void *ctx)
150: {
153:   *(void**)ctx = mfn->monitorcontext[0];
154:   return(0);
155: }

157: /*@C
158:    MFNMonitorDefault - Print the error estimate of the current approximation at each
159:    iteration of the matrix function solver.

161:    Collective on mfn

163:    Input Parameters:
164: +  mfn    - matrix function context
165: .  its    - iteration number
166: .  errest - error estimate
167: -  vf     - viewer and format for monitoring

169:    Options Database Key:
170: .  -mfn_monitor - activates MFNMonitorDefault()

172:    Level: intermediate

174: .seealso: MFNMonitorSet()
175: @*/
176: PetscErrorCode MFNMonitorDefault(MFN mfn,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
177: {
179:   PetscViewer    viewer = vf->viewer;

184:   PetscViewerPushFormat(viewer,vf->format);
185:   PetscViewerASCIIAddTab(viewer,((PetscObject)mfn)->tablevel);
186:   if (its == 1 && ((PetscObject)mfn)->prefix) {
187:     PetscViewerASCIIPrintf(viewer,"  Error estimates for %s solve.\n",((PetscObject)mfn)->prefix);
188:   }
189:   PetscViewerASCIIPrintf(viewer,"%3D MFN Error estimate %14.12e\n",its,(double)errest);
190:   PetscViewerASCIISubtractTab(viewer,((PetscObject)mfn)->tablevel);
191:   PetscViewerPopFormat(viewer);
192:   return(0);
193: }

195: /*@C
196:    MFNMonitorDefaultDrawLG - Plots the error estimate of the current approximation at each
197:    iteration of the matrix function solver.

199:    Collective on mfn

201:    Input Parameters:
202: +  mfn    - matrix function context
203: .  its    - iteration number
204: .  errest - error estimate
205: -  vf     - viewer and format for monitoring

207:    Options Database Key:
208: .  -mfn_monitor draw::draw_lg - activates MFNMonitorDefaultDrawLG()

210:    Level: intermediate

212: .seealso: MFNMonitorSet()
213: @*/
214: PetscErrorCode MFNMonitorDefaultDrawLG(MFN mfn,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
215: {
217:   PetscViewer    viewer = vf->viewer;
218:   PetscDrawLG    lg = vf->lg;
219:   PetscReal      x,y;

225:   PetscViewerPushFormat(viewer,vf->format);
226:   if (its==1) {
227:     PetscDrawLGReset(lg);
228:     PetscDrawLGSetDimension(lg,1);
229:     PetscDrawLGSetLimits(lg,1,1.0,PetscLog10Real(mfn->tol)-2,0.0);
230:   }
231:   x = (PetscReal)its;
232:   if (errest > 0.0) y = PetscLog10Real(errest);
233:   else y = 0.0;
234:   PetscDrawLGAddPoint(lg,&x,&y);
235:   if (its <= 20 || !(its % 5) || mfn->reason) {
236:     PetscDrawLGDraw(lg);
237:     PetscDrawLGSave(lg);
238:   }
239:   PetscViewerPopFormat(viewer);
240:   return(0);
241: }

243: /*@C
244:    MFNMonitorDefaultDrawLGCreate - Creates the plotter for the error estimate.

246:    Collective on viewer

248:    Input Parameters:
249: +  viewer - the viewer
250: .  format - the viewer format
251: -  ctx    - an optional user context

253:    Output Parameter:
254: .  vf     - the viewer and format context

256:    Level: intermediate

258: .seealso: MFNMonitorSet()
259: @*/
260: PetscErrorCode MFNMonitorDefaultDrawLGCreate(PetscViewer viewer,PetscViewerFormat format,void *ctx,PetscViewerAndFormat **vf)
261: {

265:   PetscViewerAndFormatCreate(viewer,format,vf);
266:   (*vf)->data = ctx;
267:   MFNMonitorLGCreate(PetscObjectComm((PetscObject)viewer),NULL,"Error Estimate","Log Error Estimate",1,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,&(*vf)->lg);
268:   return(0);
269: }