Date: 2021-09-20T09:44:14+00:00

Actual source code: lmemon.c
slepc-3.15.2 2021-09-20
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:    LME routines related to monitors
     12: */

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

     17: PetscErrorCode LMEMonitorLGCreate(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 LMEMonitor(LME lme,PetscInt it,PetscReal errest)
     41: {
     43:   PetscInt       i,n = lme->numbermonitors;

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

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

     56:    Logically Collective on lme

     58:    Input Parameters:
     59: +  lme     - linear matrix equation solver context obtained from LMECreate()
     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(LME lme,int its,PetscReal errest,void *mctx)

     68: +  lme    - linear matrix equation solver context obtained from LMECreate()
     69: .  its    - iteration number
     70: .  errest - error estimate
     71: -  mctx   - optional monitoring context, as set by LMEMonitorSet()

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

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

     85:    Level: intermediate

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

    100: /*@
    101:    LMEMonitorCancel - Clears all monitors for an LME object.

    103:    Logically Collective on lme

    105:    Input Parameters:
    106: .  lme - linear matrix equation solver context obtained from LMECreate()

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

    113:    Level: intermediate

    115: .seealso: LMEMonitorSet()
    116: @*/
    117: PetscErrorCode LMEMonitorCancel(LME lme)
    118: {
    120:   PetscInt       i;

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

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

    137:    Not Collective

    139:    Input Parameter:
    140: .  lme - linear matrix equation solver context obtained from LMECreate()

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

    145:    Level: intermediate

    147: .seealso: LMEMonitorSet()
    148: @*/
    149: PetscErrorCode LMEGetMonitorContext(LME lme,void **ctx)
    150: {
    153:   *ctx = lme->monitorcontext[0];
    154:   return(0);
    155: }

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

    161:    Collective on lme

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

    169:    Options Database Key:
    170: .  -lme_monitor - activates LMEMonitorDefault()

    172:    Level: intermediate

    174: .seealso: LMEMonitorSet()
    175: @*/
    176: PetscErrorCode LMEMonitorDefault(LME lme,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
    177: {
    179:   PetscViewer    viewer;

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

    197: /*@C
    198:    LMEMonitorDefaultDrawLG - Plots the error estimate of the current approximation at each
    199:    iteration of the linear matrix equation solver.

    201:    Collective on lme

    203:    Input Parameters:
    204: +  lme    - linear matrix equation solver context
    205: .  its    - iteration number
    206: .  errest - error estimate
    207: -  vf     - viewer and format for monitoring

    209:    Options Database Key:
    210: .  -lme_monitor draw::draw_lg - activates LMEMonitorDefaultDrawLG()

    212:    Level: intermediate

    214: .seealso: LMEMonitorSet()
    215: @*/
    216: PetscErrorCode LMEMonitorDefaultDrawLG(LME lme,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
    217: {
    219:   PetscViewer    viewer;
    220:   PetscDrawLG    lg;
    221:   PetscReal      x,y;

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

    248: /*@C
    249:    LMEMonitorDefaultDrawLGCreate - Creates the plotter for the error estimate.

    251:    Collective on viewer

    253:    Input Parameters:
    254: +  viewer - the viewer
    255: .  format - the viewer format
    256: -  ctx    - an optional user context

    258:    Output Parameter:
    259: .  vf     - the viewer and format context

    261:    Level: intermediate

    263: .seealso: LMEMonitorSet()
    264: @*/
    265: PetscErrorCode LMEMonitorDefaultDrawLGCreate(PetscViewer viewer,PetscViewerFormat format,void *ctx,PetscViewerAndFormat **vf)
    266: {

    270:   PetscViewerAndFormatCreate(viewer,format,vf);
    271:   (*vf)->data = ctx;
    272:   LMEMonitorLGCreate(PetscObjectComm((PetscObject)viewer),NULL,"Error Estimate","Log Error Estimate",1,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,&(*vf)->lg);
    273:   return(0);
    274: }