LCOV - code coverage report
Current view: top level - lme/interface - lmemon.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 33 81 40.7 %
Date: 2024-04-18 01:01:30 Functions: 4 8 50.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       3             :    SLEPc - Scalable Library for Eigenvalue Problem Computations
       4             :    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
       5             : 
       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             : */
      13             : 
      14             : #include <slepc/private/lmeimpl.h>   /*I "slepclme.h" I*/
      15             : #include <petscdraw.h>
      16             : 
      17           0 : 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           0 :   PetscDraw      draw;
      20           0 :   PetscDrawAxis  axis;
      21           0 :   PetscDrawLG    lg;
      22             : 
      23           0 :   PetscFunctionBegin;
      24           0 :   PetscCall(PetscDrawCreate(comm,host,label,x,y,m,n,&draw));
      25           0 :   PetscCall(PetscDrawSetFromOptions(draw));
      26           0 :   PetscCall(PetscDrawLGCreate(draw,l,&lg));
      27           0 :   if (names) PetscCall(PetscDrawLGSetLegend(lg,names));
      28           0 :   PetscCall(PetscDrawLGSetFromOptions(lg));
      29           0 :   PetscCall(PetscDrawLGGetAxis(lg,&axis));
      30           0 :   PetscCall(PetscDrawAxisSetLabels(axis,"Convergence","Iteration",metric));
      31           0 :   PetscCall(PetscDrawDestroy(&draw));
      32           0 :   *lgctx = lg;
      33           0 :   PetscFunctionReturn(PETSC_SUCCESS);
      34             : }
      35             : 
      36             : /*
      37             :    Runs the user provided monitor routines, if any.
      38             : */
      39          55 : PetscErrorCode LMEMonitor(LME lme,PetscInt it,PetscReal errest)
      40             : {
      41          55 :   PetscInt       i,n = lme->numbermonitors;
      42             : 
      43          55 :   PetscFunctionBegin;
      44          63 :   for (i=0;i<n;i++) PetscCall((*lme->monitor[i])(lme,it,errest,lme->monitorcontext[i]));
      45          55 :   PetscFunctionReturn(PETSC_SUCCESS);
      46             : }
      47             : 
      48             : /*@C
      49             :    LMEMonitorSet - Sets an ADDITIONAL function to be called at every
      50             :    iteration to monitor convergence.
      51             : 
      52             :    Logically Collective
      53             : 
      54             :    Input Parameters:
      55             : +  lme     - linear matrix equation solver context obtained from LMECreate()
      56             : .  monitor - pointer to function (if this is NULL, it turns off monitoring)
      57             : .  mctx    - [optional] context for private data for the
      58             :              monitor routine (use NULL if no context is desired)
      59             : -  monitordestroy - [optional] routine that frees monitor context (may be NULL)
      60             : 
      61             :    Calling sequence of monitor:
      62             : $  PetscErrorCode monitor(LME lme,PetscInt its,PetscReal errest,void*mctx)
      63             : +  lme    - linear matrix equation solver context obtained from LMECreate()
      64             : .  its    - iteration number
      65             : .  errest - error estimate
      66             : -  mctx   - optional monitoring context, as set by LMEMonitorSet()
      67             : 
      68             :    Options Database Keys:
      69             : +    -lme_monitor - print the error estimate
      70             : .    -lme_monitor draw::draw_lg - sets line graph monitor for the error estimate
      71             : -    -lme_monitor_cancel - cancels all monitors that have been hardwired into
      72             :       a code by calls to LMEMonitorSet(), but does not cancel those set via
      73             :       the options database.
      74             : 
      75             :    Notes:
      76             :    Several different monitoring routines may be set by calling
      77             :    LMEMonitorSet() multiple times; all will be called in the
      78             :    order in which they were set.
      79             : 
      80             :    Level: intermediate
      81             : 
      82             : .seealso: LMEMonitorCancel()
      83             : @*/
      84           3 : PetscErrorCode LMEMonitorSet(LME lme,PetscErrorCode (*monitor)(LME lme,PetscInt its,PetscReal errest,void*mctx),void *mctx,PetscErrorCode (*monitordestroy)(void**))
      85             : {
      86           3 :   PetscFunctionBegin;
      87           3 :   PetscValidHeaderSpecific(lme,LME_CLASSID,1);
      88           3 :   PetscCheck(lme->numbermonitors<MAXLMEMONITORS,PetscObjectComm((PetscObject)lme),PETSC_ERR_ARG_OUTOFRANGE,"Too many LME monitors set");
      89           3 :   lme->monitor[lme->numbermonitors]           = monitor;
      90           3 :   lme->monitorcontext[lme->numbermonitors]    = (void*)mctx;
      91           3 :   lme->monitordestroy[lme->numbermonitors++]  = monitordestroy;
      92           3 :   PetscFunctionReturn(PETSC_SUCCESS);
      93             : }
      94             : 
      95             : /*@
      96             :    LMEMonitorCancel - Clears all monitors for an LME object.
      97             : 
      98             :    Logically Collective
      99             : 
     100             :    Input Parameters:
     101             : .  lme - linear matrix equation solver context obtained from LMECreate()
     102             : 
     103             :    Options Database Key:
     104             : .    -lme_monitor_cancel - cancels all monitors that have been hardwired
     105             :       into a code by calls to LMEMonitorSet(),
     106             :       but does not cancel those set via the options database.
     107             : 
     108             :    Level: intermediate
     109             : 
     110             : .seealso: LMEMonitorSet()
     111             : @*/
     112           9 : PetscErrorCode LMEMonitorCancel(LME lme)
     113             : {
     114           9 :   PetscInt       i;
     115             : 
     116           9 :   PetscFunctionBegin;
     117           9 :   PetscValidHeaderSpecific(lme,LME_CLASSID,1);
     118          12 :   for (i=0; i<lme->numbermonitors; i++) {
     119           3 :     if (lme->monitordestroy[i]) PetscCall((*lme->monitordestroy[i])(&lme->monitorcontext[i]));
     120             :   }
     121           9 :   lme->numbermonitors = 0;
     122           9 :   PetscFunctionReturn(PETSC_SUCCESS);
     123             : }
     124             : 
     125             : /*@C
     126             :    LMEGetMonitorContext - Gets the monitor context, as set by
     127             :    LMEMonitorSet() for the FIRST monitor only.
     128             : 
     129             :    Not Collective
     130             : 
     131             :    Input Parameter:
     132             : .  lme - linear matrix equation solver context obtained from LMECreate()
     133             : 
     134             :    Output Parameter:
     135             : .  ctx - monitor context
     136             : 
     137             :    Level: intermediate
     138             : 
     139             : .seealso: LMEMonitorSet()
     140             : @*/
     141           0 : PetscErrorCode LMEGetMonitorContext(LME lme,void *ctx)
     142             : {
     143           0 :   PetscFunctionBegin;
     144           0 :   PetscValidHeaderSpecific(lme,LME_CLASSID,1);
     145           0 :   *(void**)ctx = lme->monitorcontext[0];
     146           0 :   PetscFunctionReturn(PETSC_SUCCESS);
     147             : }
     148             : 
     149             : /*@C
     150             :    LMEMonitorDefault - Print the error estimate of the current approximation at each
     151             :    iteration of the linear matrix equation solver.
     152             : 
     153             :    Collective
     154             : 
     155             :    Input Parameters:
     156             : +  lme    - linear matrix equation solver context
     157             : .  its    - iteration number
     158             : .  errest - error estimate
     159             : -  vf     - viewer and format for monitoring
     160             : 
     161             :    Options Database Key:
     162             : .  -lme_monitor - activates LMEMonitorDefault()
     163             : 
     164             :    Level: intermediate
     165             : 
     166             : .seealso: LMEMonitorSet()
     167             : @*/
     168           8 : PetscErrorCode LMEMonitorDefault(LME lme,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
     169             : {
     170           8 :   PetscViewer    viewer = vf->viewer;
     171             : 
     172           8 :   PetscFunctionBegin;
     173           8 :   PetscValidHeaderSpecific(lme,LME_CLASSID,1);
     174           8 :   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
     175           8 :   PetscCall(PetscViewerPushFormat(viewer,vf->format));
     176           8 :   PetscCall(PetscViewerASCIIAddTab(viewer,((PetscObject)lme)->tablevel));
     177           8 :   if (its == 1 && ((PetscObject)lme)->prefix) PetscCall(PetscViewerASCIIPrintf(viewer,"  Error estimates for %s solve.\n",((PetscObject)lme)->prefix));
     178           8 :   PetscCall(PetscViewerASCIIPrintf(viewer,"%3" PetscInt_FMT " LME Error estimate %14.12e\n",its,(double)errest));
     179           8 :   PetscCall(PetscViewerASCIISubtractTab(viewer,((PetscObject)lme)->tablevel));
     180           8 :   PetscCall(PetscViewerPopFormat(viewer));
     181           8 :   PetscFunctionReturn(PETSC_SUCCESS);
     182             : }
     183             : 
     184             : /*@C
     185             :    LMEMonitorDefaultDrawLG - Plots the error estimate of the current approximation at each
     186             :    iteration of the linear matrix equation solver.
     187             : 
     188             :    Collective
     189             : 
     190             :    Input Parameters:
     191             : +  lme    - linear matrix equation solver context
     192             : .  its    - iteration number
     193             : .  errest - error estimate
     194             : -  vf     - viewer and format for monitoring
     195             : 
     196             :    Options Database Key:
     197             : .  -lme_monitor draw::draw_lg - activates LMEMonitorDefaultDrawLG()
     198             : 
     199             :    Level: intermediate
     200             : 
     201             : .seealso: LMEMonitorSet()
     202             : @*/
     203           0 : PetscErrorCode LMEMonitorDefaultDrawLG(LME lme,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
     204             : {
     205           0 :   PetscViewer    viewer = vf->viewer;
     206           0 :   PetscDrawLG    lg = vf->lg;
     207           0 :   PetscReal      x,y;
     208             : 
     209           0 :   PetscFunctionBegin;
     210           0 :   PetscValidHeaderSpecific(lme,LME_CLASSID,1);
     211           0 :   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
     212           0 :   PetscValidHeaderSpecific(lg,PETSC_DRAWLG_CLASSID,4);
     213           0 :   PetscCall(PetscViewerPushFormat(viewer,vf->format));
     214           0 :   if (its==1) {
     215           0 :     PetscCall(PetscDrawLGReset(lg));
     216           0 :     PetscCall(PetscDrawLGSetDimension(lg,1));
     217           0 :     PetscCall(PetscDrawLGSetLimits(lg,1,1.0,PetscLog10Real(lme->tol)-2,0.0));
     218             :   }
     219           0 :   x = (PetscReal)its;
     220           0 :   if (errest > 0.0) y = PetscLog10Real(errest);
     221           0 :   else y = 0.0;
     222           0 :   PetscCall(PetscDrawLGAddPoint(lg,&x,&y));
     223           0 :   if (its <= 20 || !(its % 5) || lme->reason) {
     224           0 :     PetscCall(PetscDrawLGDraw(lg));
     225           0 :     PetscCall(PetscDrawLGSave(lg));
     226             :   }
     227           0 :   PetscCall(PetscViewerPopFormat(viewer));
     228           0 :   PetscFunctionReturn(PETSC_SUCCESS);
     229             : }
     230             : 
     231             : /*@C
     232             :    LMEMonitorDefaultDrawLGCreate - Creates the plotter for the error estimate.
     233             : 
     234             :    Collective
     235             : 
     236             :    Input Parameters:
     237             : +  viewer - the viewer
     238             : .  format - the viewer format
     239             : -  ctx    - an optional user context
     240             : 
     241             :    Output Parameter:
     242             : .  vf     - the viewer and format context
     243             : 
     244             :    Level: intermediate
     245             : 
     246             : .seealso: LMEMonitorSet()
     247             : @*/
     248           0 : PetscErrorCode LMEMonitorDefaultDrawLGCreate(PetscViewer viewer,PetscViewerFormat format,void *ctx,PetscViewerAndFormat **vf)
     249             : {
     250           0 :   PetscFunctionBegin;
     251           0 :   PetscCall(PetscViewerAndFormatCreate(viewer,format,vf));
     252           0 :   (*vf)->data = ctx;
     253           0 :   PetscCall(LMEMonitorLGCreate(PetscObjectComm((PetscObject)viewer),NULL,"Error Estimate","Log Error Estimate",1,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,&(*vf)->lg));
     254           0 :   PetscFunctionReturn(PETSC_SUCCESS);
     255             : }

Generated by: LCOV version 1.14