Date: 2022-10-01T06:40:44+00:00

Actual source code: mfnmon.c
slepc-3.18.0 2022-10-01
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-, 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;

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

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

     42:   for (i=0;i<n;i++) (*mfn->monitor[i])(mfn,it,errest,mfn->monitorcontext[i]);
     43:   return 0;
     44: }

     46: /*@C
     47:    MFNMonitorSet - Sets an ADDITIONAL function to be called at every
     48:    iteration to monitor convergence.

     50:    Logically Collective on mfn

     52:    Input Parameters:
     53: +  mfn     - matrix function context obtained from MFNCreate()
     54: .  monitor - pointer to function (if this is NULL, it turns off monitoring)
     55: .  mctx    - [optional] context for private data for the
     56:              monitor routine (use NULL if no context is desired)
     57: -  monitordestroy - [optional] routine that frees monitor context (may be NULL)

     59:    Calling Sequence of monitor:
     60: $   monitor(MFN mfn,PetscInt its,PetscReal errest,void *mctx)

     62: +  mfn    - matrix function context obtained from MFNCreate()
     63: .  its    - iteration number
     64: .  errest - error estimate
     65: -  mctx   - optional monitoring context, as set by MFNMonitorSet()

     67:    Options Database Keys:
     68: +    -mfn_monitor - print the error estimate
     69: .    -mfn_monitor draw::draw_lg - sets line graph monitor for the error estimate
     70: -    -mfn_monitor_cancel - cancels all monitors that have been hardwired into
     71:       a code by calls to MFNMonitorSet(), but does not cancel those set via
     72:       the options database.

     74:    Notes:
     75:    Several different monitoring routines may be set by calling
     76:    MFNMonitorSet() multiple times; all will be called in the
     77:    order in which they were set.

     79:    Level: intermediate

     81: .seealso: MFNMonitorCancel()
     82: @*/
     83: PetscErrorCode MFNMonitorSet(MFN mfn,PetscErrorCode (*monitor)(MFN,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
     84: {
     87:   mfn->monitor[mfn->numbermonitors]           = monitor;
     88:   mfn->monitorcontext[mfn->numbermonitors]    = (void*)mctx;
     89:   mfn->monitordestroy[mfn->numbermonitors++]  = monitordestroy;
     90:   return 0;
     91: }

     93: /*@
     94:    MFNMonitorCancel - Clears all monitors for an MFN object.

     96:    Logically Collective on mfn

     98:    Input Parameters:
     99: .  mfn - matrix function context obtained from MFNCreate()

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

    106:    Level: intermediate

    108: .seealso: MFNMonitorSet()
    109: @*/
    110: PetscErrorCode MFNMonitorCancel(MFN mfn)
    111: {
    112:   PetscInt       i;

    115:   for (i=0; i<mfn->numbermonitors; i++) {
    116:     if (mfn->monitordestroy[i]) (*mfn->monitordestroy[i])(&mfn->monitorcontext[i]);
    117:   }
    118:   mfn->numbermonitors = 0;
    119:   return 0;
    120: }

    122: /*@C
    123:    MFNGetMonitorContext - Gets the monitor context, as set by
    124:    MFNMonitorSet() for the FIRST monitor only.

    126:    Not Collective

    128:    Input Parameter:
    129: .  mfn - matrix function context obtained from MFNCreate()

    131:    Output Parameter:
    132: .  ctx - monitor context

    134:    Level: intermediate

    136: .seealso: MFNMonitorSet()
    137: @*/
    138: PetscErrorCode MFNGetMonitorContext(MFN mfn,void *ctx)
    139: {
    141:   *(void**)ctx = mfn->monitorcontext[0];
    142:   return 0;
    143: }

    145: /*@C
    146:    MFNMonitorDefault - Print the error estimate of the current approximation at each
    147:    iteration of the matrix function solver.

    149:    Collective on mfn

    151:    Input Parameters:
    152: +  mfn    - matrix function context
    153: .  its    - iteration number
    154: .  errest - error estimate
    155: -  vf     - viewer and format for monitoring

    157:    Options Database Key:
    158: .  -mfn_monitor - activates MFNMonitorDefault()

    160:    Level: intermediate

    162: .seealso: MFNMonitorSet()
    163: @*/
    164: PetscErrorCode MFNMonitorDefault(MFN mfn,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
    165: {
    166:   PetscViewer    viewer = vf->viewer;

    170:   PetscViewerPushFormat(viewer,vf->format);
    171:   PetscViewerASCIIAddTab(viewer,((PetscObject)mfn)->tablevel);
    172:   if (its == 1 && ((PetscObject)mfn)->prefix) PetscViewerASCIIPrintf(viewer,"  Error estimates for %s solve.\n",((PetscObject)mfn)->prefix);
    173:   PetscViewerASCIIPrintf(viewer,"%3" PetscInt_FMT " MFN Error estimate %14.12e\n",its,(double)errest);
    174:   PetscViewerASCIISubtractTab(viewer,((PetscObject)mfn)->tablevel);
    175:   PetscViewerPopFormat(viewer);
    176:   return 0;
    177: }

    179: /*@C
    180:    MFNMonitorDefaultDrawLG - Plots the error estimate of the current approximation at each
    181:    iteration of the matrix function solver.

    183:    Collective on mfn

    185:    Input Parameters:
    186: +  mfn    - matrix function context
    187: .  its    - iteration number
    188: .  errest - error estimate
    189: -  vf     - viewer and format for monitoring

    191:    Options Database Key:
    192: .  -mfn_monitor draw::draw_lg - activates MFNMonitorDefaultDrawLG()

    194:    Level: intermediate

    196: .seealso: MFNMonitorSet()
    197: @*/
    198: PetscErrorCode MFNMonitorDefaultDrawLG(MFN mfn,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
    199: {
    200:   PetscViewer    viewer = vf->viewer;
    201:   PetscDrawLG    lg = vf->lg;
    202:   PetscReal      x,y;

    207:   PetscViewerPushFormat(viewer,vf->format);
    208:   if (its==1) {
    209:     PetscDrawLGReset(lg);
    210:     PetscDrawLGSetDimension(lg,1);
    211:     PetscDrawLGSetLimits(lg,1,1.0,PetscLog10Real(mfn->tol)-2,0.0);
    212:   }
    213:   x = (PetscReal)its;
    214:   if (errest > 0.0) y = PetscLog10Real(errest);
    215:   else y = 0.0;
    216:   PetscDrawLGAddPoint(lg,&x,&y);
    217:   if (its <= 20 || !(its % 5) || mfn->reason) {
    218:     PetscDrawLGDraw(lg);
    219:     PetscDrawLGSave(lg);
    220:   }
    221:   PetscViewerPopFormat(viewer);
    222:   return 0;
    223: }

    225: /*@C
    226:    MFNMonitorDefaultDrawLGCreate - Creates the plotter for the error estimate.

    228:    Collective on viewer

    230:    Input Parameters:
    231: +  viewer - the viewer
    232: .  format - the viewer format
    233: -  ctx    - an optional user context

    235:    Output Parameter:
    236: .  vf     - the viewer and format context

    238:    Level: intermediate

    240: .seealso: MFNMonitorSet()
    241: @*/
    242: PetscErrorCode MFNMonitorDefaultDrawLGCreate(PetscViewer viewer,PetscViewerFormat format,void *ctx,PetscViewerAndFormat **vf)
    243: {
    244:   PetscViewerAndFormatCreate(viewer,format,vf);
    245:   (*vf)->data = ctx;
    246:   MFNMonitorLGCreate(PetscObjectComm((PetscObject)viewer),NULL,"Error Estimate","Log Error Estimate",1,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,&(*vf)->lg);
    247:   return 0;
    248: }