Actual source code: mfnmon.c

slepc-3.11.2 2019-07-30
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2019, 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>   /*I "slepcmfn.h" I*/
 15: #include <petscdraw.h>

 17: /*
 18:    Runs the user provided monitor routines, if any.
 19: */
 20: PetscErrorCode MFNMonitor(MFN mfn,PetscInt it,PetscReal errest)
 21: {
 23:   PetscInt       i,n = mfn->numbermonitors;

 26:   for (i=0;i<n;i++) {
 27:     (*mfn->monitor[i])(mfn,it,errest,mfn->monitorcontext[i]);
 28:   }
 29:   return(0);
 30: }

 32: /*@C
 33:    MFNMonitorSet - Sets an ADDITIONAL function to be called at every
 34:    iteration to monitor convergence.

 36:    Logically Collective on MFN

 38:    Input Parameters:
 39: +  mfn     - matrix function context obtained from MFNCreate()
 40: .  monitor - pointer to function (if this is NULL, it turns off monitoring)
 41: .  mctx    - [optional] context for private data for the
 42:              monitor routine (use NULL if no context is desired)
 43: -  monitordestroy - [optional] routine that frees monitor context (may be NULL)

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

 48: +  mfn    - matrix function context obtained from MFNCreate()
 49: .  its    - iteration number
 50: .  errest - error estimate
 51: -  mctx   - optional monitoring context, as set by MFNMonitorSet()

 53:    Options Database Keys:
 54: +    -mfn_monitor        - print the error estimate
 55: .    -mfn_monitor_lg     - sets line graph monitor for the error estimate
 56: -    -mfn_monitor_cancel - cancels all monitors that have been hardwired into
 57:       a code by calls to MFNMonitorSet(), but does not cancel those set via
 58:       the options database.

 60:    Notes:
 61:    Several different monitoring routines may be set by calling
 62:    MFNMonitorSet() multiple times; all will be called in the
 63:    order in which they were set.

 65:    Level: intermediate

 67: .seealso: MFNMonitorCancel()
 68: @*/
 69: PetscErrorCode MFNMonitorSet(MFN mfn,PetscErrorCode (*monitor)(MFN,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
 70: {
 73:   if (mfn->numbermonitors >= MAXMFNMONITORS) SETERRQ(PetscObjectComm((PetscObject)mfn),PETSC_ERR_ARG_OUTOFRANGE,"Too many MFN monitors set");
 74:   mfn->monitor[mfn->numbermonitors]           = monitor;
 75:   mfn->monitorcontext[mfn->numbermonitors]    = (void*)mctx;
 76:   mfn->monitordestroy[mfn->numbermonitors++]  = monitordestroy;
 77:   return(0);
 78: }

 80: /*@
 81:    MFNMonitorCancel - Clears all monitors for an MFN object.

 83:    Logically Collective on MFN

 85:    Input Parameters:
 86: .  mfn - matrix function context obtained from MFNCreate()

 88:    Options Database Key:
 89: .    -mfn_monitor_cancel - Cancels all monitors that have been hardwired
 90:       into a code by calls to MFNMonitorSet(),
 91:       but does not cancel those set via the options database.

 93:    Level: intermediate

 95: .seealso: MFNMonitorSet()
 96: @*/
 97: PetscErrorCode MFNMonitorCancel(MFN mfn)
 98: {
100:   PetscInt       i;

104:   for (i=0; i<mfn->numbermonitors; i++) {
105:     if (mfn->monitordestroy[i]) {
106:       (*mfn->monitordestroy[i])(&mfn->monitorcontext[i]);
107:     }
108:   }
109:   mfn->numbermonitors = 0;
110:   return(0);
111: }

113: /*@C
114:    MFNGetMonitorContext - Gets the monitor context, as set by
115:    MFNMonitorSet() for the FIRST monitor only.

117:    Not Collective

119:    Input Parameter:
120: .  mfn - matrix function context obtained from MFNCreate()

122:    Output Parameter:
123: .  ctx - monitor context

125:    Level: intermediate

127: .seealso: MFNMonitorSet()
128: @*/
129: PetscErrorCode MFNGetMonitorContext(MFN mfn,void **ctx)
130: {
133:   *ctx = mfn->monitorcontext[0];
134:   return(0);
135: }

137: /*@C
138:    MFNMonitorDefault - Print the error estimate of the current approximation at each
139:    iteration of the matrix function solver.

141:    Collective on MFN

143:    Input Parameters:
144: +  mfn    - matrix function context
145: .  its    - iteration number
146: .  errest - error estimate
147: -  vf     - viewer and format for monitoring

149:    Level: intermediate

151: .seealso: MFNMonitorSet()
152: @*/
153: PetscErrorCode MFNMonitorDefault(MFN mfn,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
154: {
156:   PetscViewer    viewer;

161:   viewer = vf->viewer;
163:   PetscViewerPushFormat(viewer,vf->format);
164:   PetscViewerASCIIAddTab(viewer,((PetscObject)mfn)->tablevel);
165:   if (its == 1 && ((PetscObject)mfn)->prefix) {
166:     PetscViewerASCIIPrintf(viewer,"  Error estimates for %s solve.\n",((PetscObject)mfn)->prefix);
167:   }
168:   PetscViewerASCIIPrintf(viewer,"%3D MFN Error estimate %14.12e\n",its,(double)errest);
169:   PetscViewerASCIISubtractTab(viewer,((PetscObject)mfn)->tablevel);
170:   PetscViewerPopFormat(viewer);
171:   return(0);
172: }

174: /*@C
175:    MFNMonitorLGCreate - Creates a line graph context for use with
176:    MFN to monitor convergence.

178:    Collective on MPI_Comm

180:    Input Parameters:
181: +  comm - communicator context
182: .  host - the X display to open, or null for the local machine
183: .  label - the title to put in the title bar
184: .  x, y - the screen coordinates of the upper left coordinate of
185:           the window
186: -  m, n - the screen width and height in pixels

188:    Output Parameter:
189: .  lgctx - the drawing context

191:    Options Database Keys:
192: .  -mfn_monitor_lg - Sets line graph monitor

194:    Notes:
195:    Use PetscDrawLGDestroy() to destroy this line graph.

197:    Level: intermediate

199: .seealso: MFNMonitorSet()
200: @*/
201: PetscErrorCode MFNMonitorLGCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *lgctx)
202: {
203:   PetscDraw      draw;
204:   PetscDrawLG    lg;

208:   PetscDrawCreate(comm,host,label,x,y,m,n,&draw);
209:   PetscDrawSetFromOptions(draw);
210:   PetscDrawLGCreate(draw,1,&lg);
211:   PetscDrawLGSetFromOptions(lg);
212:   PetscDrawDestroy(&draw);
213:   *lgctx = lg;
214:   return(0);
215: }

217: PetscErrorCode MFNMonitorLG(MFN mfn,PetscInt its,PetscReal errest,void *ctx)
218: {
219:   PetscDrawLG    lg = (PetscDrawLG)ctx;
220:   PetscReal      x,y;

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