Date: 2015-10-02T22:20:42+00:00

Actual source code: xmon.c
petsc-3.6.2 2015-10-02
Report Typos and Errors

      2: #include <petsc/private/kspimpl.h>              /*I  "petscksp.h"   I*/
      3: #include <petscdraw.h>

      7: /*@C
      8:    KSPMonitorLGResidualNormCreate - Creates a line graph context for use with
      9:    KSP to monitor convergence of preconditioned residual norms.

     11:    Collective on KSP

     13:    Input Parameters:
     14: +  host - the X display to open, or null for the local machine
     15: .  label - the title to put in the title bar
     16: .  x, y - the screen coordinates of the upper left coordinate of
     17:           the window
     18: -  m, n - the screen width and height in pixels

     20:    Output Parameter:
     21: .  draw - the drawing context

     23:    Options Database Key:
     24: .  -ksp_monitor_lg_residualnorm - Sets line graph monitor

     26:    Notes:
     27:    Use KSPMonitorLGResidualNormDestroy() to destroy this line graph; do not use PetscDrawLGDestroy().

     29:    Level: intermediate

     31: .keywords: KSP, monitor, line graph, residual, create

     33: .seealso: KSPMonitorLGResidualNormDestroy(), KSPMonitorSet(), KSPMonitorLGTrueResidualCreate()
     34: @*/
     35: PetscErrorCode  KSPMonitorLGResidualNormCreate(const char host[],const char label[],int x,int y,int m,int n,PetscObject **objs)
     36: {
     37:   PetscDraw      win;
     39:   PetscDrawAxis  axis;
     40:   PetscDrawLG    draw;

     43:   PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);
     44:   PetscDrawSetFromOptions(win);
     45:   PetscDrawLGCreate(win,1,&draw);
     46:   PetscDrawLGSetFromOptions(draw);
     47:   PetscDrawLGGetAxis(draw,&axis);
     48:   PetscDrawAxisSetLabels(axis,"Convergence","Iteration","Residual Norm");
     49:   PetscLogObjectParent((PetscObject)draw,(PetscObject)win);
     50:   PetscMalloc1(2,objs);
     51:   (*objs)[0] = (PetscObject)draw;
     52:   (*objs)[1] = (PetscObject)win;
     53:   return(0);
     54: }

     58: PetscErrorCode  KSPMonitorLGResidualNorm(KSP ksp,PetscInt n,PetscReal rnorm,PetscObject *objs)
     59: {
     61:   PetscReal      x,y;
     62:   PetscDrawLG    lg = (PetscDrawLG) objs[0];

     65:   if (!n) {PetscDrawLGReset(lg);}
     66:   x = (PetscReal) n;
     67:   if (rnorm > 0.0) y = PetscLog10Real(rnorm);
     68:   else y = -15.0;
     69:   PetscDrawLGAddPoint(lg,&x,&y);
     70:   if (n < 20 || !(n % 5)) {
     71:     PetscDrawLGDraw(lg);
     72:   }
     73:   return(0);
     74: }

     78: /*@
     79:    KSPMonitorLGResidualNormDestroy - Destroys a line graph context that was created
     80:    with KSPMonitorLGResidualNormCreate().

     82:    Collective on KSP

     84:    Input Parameter:
     85: .  draw - the drawing context

     87:    Level: intermediate

     89: .keywords: KSP, monitor, line graph, destroy

     91: .seealso: KSPMonitorLGResidualNormCreate(), KSPMonitorLGTrueResidualDestroy(), KSPMonitorSet()
     92: @*/
     93: PetscErrorCode  KSPMonitorLGResidualNormDestroy(PetscObject **objs)
     94: {
     96:   PetscDrawLG    drawlg = (PetscDrawLG) (*objs)[0];
     97:   PetscDraw      draw = (PetscDraw) (*objs)[1];

    100:   PetscDrawDestroy(&draw);
    101:   PetscDrawLGDestroy(&drawlg);
    102:   PetscFree(*objs);
    103:   return(0);
    104: }

    106: extern PetscErrorCode  KSPMonitorRange_Private(KSP,PetscInt,PetscReal*);
    109: PetscErrorCode  KSPMonitorLGRange(KSP ksp,PetscInt n,PetscReal rnorm,void *monctx)
    110: {
    111:   PetscDrawLG      lg;
    112:   PetscErrorCode   ierr;
    113:   PetscReal        x,y,per;
    114:   PetscViewer      v = (PetscViewer)monctx;
    115:   static PetscReal prev; /* should be in the context */
    116:   PetscDraw        draw;

    119:   PetscViewerDrawGetDrawLG(v,0,&lg);
    120:   if (!n) {PetscDrawLGReset(lg);}
    121:   PetscDrawLGGetDraw(lg,&draw);
    122:   PetscDrawSetTitle(draw,"Residual norm");
    123:   x    = (PetscReal) n;
    124:   if (rnorm > 0.0) y = PetscLog10Real(rnorm);
    125:   else y = -15.0;
    126:   PetscDrawLGAddPoint(lg,&x,&y);
    127:   if (n < 20 || !(n % 5)) {
    128:     PetscDrawLGDraw(lg);
    129:   }

    131:   PetscViewerDrawGetDrawLG(v,1,&lg);
    132:    KSPMonitorRange_Private(ksp,n,&per);
    133:   if (!n) {PetscDrawLGReset(lg);}
    134:   PetscDrawLGGetDraw(lg,&draw);
    135:   PetscDrawSetTitle(draw,"% elemts > .2*max elemt");
    136:   x    = (PetscReal) n;
    137:   y    = 100.0*per;
    138:   PetscDrawLGAddPoint(lg,&x,&y);
    139:   if (n < 20 || !(n % 5)) {
    140:     PetscDrawLGDraw(lg);
    141:   }

    143:   PetscViewerDrawGetDrawLG(v,2,&lg);
    144:   if (!n) {prev = rnorm;PetscDrawLGReset(lg);}
    145:   PetscDrawLGGetDraw(lg,&draw);
    146:   PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");
    147:   PetscDrawLGGetDraw(lg,&draw);
    148:   PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");
    149:   x    = (PetscReal) n;
    150:   y    = (prev - rnorm)/prev;
    151:   PetscDrawLGAddPoint(lg,&x,&y);
    152:   if (n < 20 || !(n % 5)) {
    153:     PetscDrawLGDraw(lg);
    154:   }

    156:   PetscViewerDrawGetDrawLG(v,3,&lg);
    157:   if (!n) {PetscDrawLGReset(lg);}
    158:   PetscDrawLGGetDraw(lg,&draw);
    159:   PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");
    160:   x    = (PetscReal) n;
    161:   y    = (prev - rnorm)/(prev*per);
    162:   if (n > 5) {
    163:     PetscDrawLGAddPoint(lg,&x,&y);
    164:   }
    165:   if (n < 20 || !(n % 5)) {
    166:     PetscDrawLGDraw(lg);
    167:   }
    168:   prev = rnorm;
    169:   return(0);
    170: }

    174: /*@C
    175:    KSPMonitorLGTrueResidualNormCreate - Creates a line graph context for use with
    176:    KSP to monitor convergence of true residual norms (as opposed to
    177:    preconditioned residual norms).

    179:    Collective on KSP

    181:    Input Parameters:
    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: .  draw - the drawing context

    191:    Options Database Key:
    192: .  -ksp_monitor_lg_true_residualnorm - Sets true line graph monitor

    194:    Notes:
    195:    Use KSPMonitorLGTrueResidualNormDestroy() to destroy this line graph, not
    196:    PetscDrawLGDestroy().

    198:    Level: intermediate

    200: .keywords: KSP, monitor, line graph, residual, create, true

    202: .seealso: KSPMonitorLGResidualNormDestroy(), KSPMonitorSet(), KSPMonitorDefault()
    203: @*/
    204: PetscErrorCode  KSPMonitorLGTrueResidualNormCreate(const char host[],const char label[],int x,int y,int m,int n,PetscObject **objs)
    205: {
    206:   PetscDraw      win;
    208:   PetscDrawAxis  axis;
    209:   PetscDrawLG    draw;

    212:   PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);
    213:   PetscDrawSetFromOptions(win);
    214:   PetscDrawLGCreate(win,2,&draw);
    215:   PetscDrawLGSetFromOptions(draw);
    216:   PetscDrawLGGetAxis(draw,&axis);
    217:   PetscDrawAxisSetLabels(axis,"Convergence","Iteration","Residual Norms");
    218:   PetscLogObjectParent((PetscObject)draw,(PetscObject)win);
    219:   PetscMalloc1(2,objs);
    220:   (*objs)[0] = (PetscObject)draw;
    221:   (*objs)[1] = (PetscObject)win;
    222:   return(0);
    223: }

    227: PetscErrorCode  KSPMonitorLGTrueResidualNorm(KSP ksp,PetscInt n,PetscReal rnorm,PetscObject *objs)
    228: {
    229:   PetscDrawLG    lg = (PetscDrawLG) objs[0];
    230:   PetscReal      x[2],y[2],scnorm;
    232:   PetscMPIInt    rank;
    233:   Vec            resid,work;

    236:   MPI_Comm_rank(PetscObjectComm((PetscObject)ksp),&rank);
    237:   if (!rank) {
    238:     if (!n) {PetscDrawLGReset(lg);}
    239:     x[0] = x[1] = (PetscReal) n;
    240:     if (rnorm > 0.0) y[0] = PetscLog10Real(rnorm);
    241:     else y[0] = -15.0;
    242:   }

    244:   VecDuplicate(ksp->vec_rhs,&work);
    245:   KSPBuildResidual(ksp,0,work,&resid);
    246:   VecNorm(resid,NORM_2,&scnorm);
    247:   VecDestroy(&work);

    249:   if (!rank) {
    250:     if (scnorm > 0.0) y[1] = PetscLog10Real(scnorm);
    251:     else y[1] = -15.0;
    252:     PetscDrawLGAddPoint(lg,x,y);
    253:     if (n <= 20 || (n % 3)) {
    254:       PetscDrawLGDraw(lg);
    255:     }
    256:   }
    257:   return(0);
    258: }

    262: /*@C
    263:    KSPMonitorLGTrueResidualNormDestroy - Destroys a line graph context that was created
    264:    with KSPMonitorLGTrueResidualNormCreate().

    266:    Collective on KSP

    268:    Input Parameter:
    269: .  draw - the drawing context

    271:    Level: intermediate

    273: .keywords: KSP, monitor, line graph, destroy, true

    275: .seealso: KSPMonitorLGTrueResidualNormCreate(), KSPMonitorSet()
    276: @*/
    277: PetscErrorCode  KSPMonitorLGTrueResidualNormDestroy(PetscObject **objs)
    278: {
    280:   PetscDrawLG    drawlg = (PetscDrawLG) (*objs)[0];
    281:   PetscDraw      draw = (PetscDraw) (*objs)[1];

    284:   PetscDrawDestroy(&draw);
    285:   PetscDrawLGDestroy(&drawlg);
    286:   PetscFree(*objs);
    287:   return(0);
    288: }