Subversion Repositories slepc-dev

Rev

Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
525 dsic.upv.es!antodo 1
/*
545 dsic.upv.es!jroman 2
      EPS routines related to monitors.
1376 slepc 3
 
4
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5
      SLEPc - Scalable Library for Eigenvalue Problem Computations
6
      Copyright (c) 2002-2007, Universidad Politecnica de Valencia, Spain
7
 
8
      This file is part of SLEPc. See the README file for conditions of use
9
      and additional information.
10
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
525 dsic.upv.es!antodo 11
*/
1376 slepc 12
 
1521 slepc 13
#include "private/epsimpl.h"   /*I "slepceps.h" I*/
525 dsic.upv.es!antodo 14
 
15
#undef __FUNCT__  
1331 slepc 16
#define __FUNCT__ "EPSMonitorSet"
525 dsic.upv.es!antodo 17
/*@C
1331 slepc 18
   EPSMonitorSet - Sets an ADDITIONAL function to be called at every
525 dsic.upv.es!antodo 19
   iteration to monitor the error estimates for each requested eigenpair.
20
 
21
   Collective on EPS
22
 
23
   Input Parameters:
24
+  eps     - eigensolver context obtained from EPSCreate()
1021 slepc 25
.  monitor - pointer to function (if this is PETSC_NULL, it turns off monitoring)
1287 slepc 26
.  mctx    - [optional] context for private data for the
525 dsic.upv.es!antodo 27
             monitor routine (use PETSC_NULL if no context is desired)
1287 slepc 28
-  monitordestroy - [optional] routine that frees monitor context
29
          (may be PETSC_NULL)
525 dsic.upv.es!antodo 30
 
31
   Calling Sequence of monitor:
617 dsic.upv.es!antodo 32
$     monitor (EPS eps, int its, int nconv, PetscScalar *eigr, PetscScalar *eigi, PetscReal* errest, int nest, void *mctx)
525 dsic.upv.es!antodo 33
 
34
+  eps    - eigensolver context obtained from EPSCreate()
35
.  its    - iteration number
36
.  nconv  - number of converged eigenpairs
617 dsic.upv.es!antodo 37
.  eigr   - real part of the eigenvalues
38
.  eigi   - imaginary part of the eigenvalues
663 dsic.upv.es!antodo 39
.  errest - relative error estimates for each eigenpair
525 dsic.upv.es!antodo 40
.  nest   - number of error estimates
1331 slepc 41
-  mctx   - optional monitoring context, as set by EPSMonitorSet()
525 dsic.upv.es!antodo 42
 
43
   Options Database Keys:
44
+    -eps_monitor        - print error estimates at each iteration
1331 slepc 45
.    -eps_monitor_draw   - sets line graph monitor
46
-    -eps_monitor_cancel - cancels all monitors that have been hardwired into
47
      a code by calls to EPSMonitorSet(), but does not cancel those set via
525 dsic.upv.es!antodo 48
      the options database.
49
 
50
   Notes:  
51
   Several different monitoring routines may be set by calling
1331 slepc 52
   EPSMonitorSet() multiple times; all will be called in the
525 dsic.upv.es!antodo 53
   order in which they were set.
54
 
55
   Level: intermediate
56
 
1331 slepc 57
.seealso: EPSMonitorDefault(), EPSMonitorCancel()
525 dsic.upv.es!antodo 58
@*/
1509 slepc 59
PetscErrorCode EPSMonitorSet(EPS eps,PetscErrorCode (*monitor)(EPS,PetscInt,PetscInt,PetscScalar*,PetscScalar*,PetscReal*,PetscInt,void*),
1288 slepc 60
                             void *mctx,PetscErrorCode (*monitordestroy)(void*))
525 dsic.upv.es!antodo 61
{
62
  PetscFunctionBegin;
63
  PetscValidHeaderSpecific(eps,EPS_COOKIE,1);
64
  if (eps->numbermonitors >= MAXEPSMONITORS) {
65
    SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many EPS monitors set");
66
  }
67
  eps->monitor[eps->numbermonitors]           = monitor;
1287 slepc 68
  eps->monitorcontext[eps->numbermonitors]    = (void*)mctx;
69
  eps->monitordestroy[eps->numbermonitors++]  = monitordestroy;
525 dsic.upv.es!antodo 70
  PetscFunctionReturn(0);
71
}
72
 
73
#undef __FUNCT__  
1331 slepc 74
#define __FUNCT__ "EPSMonitorCancel"
1021 slepc 75
/*@
1331 slepc 76
   EPSMonitorCancel - Clears all monitors for an EPS object.
525 dsic.upv.es!antodo 77
 
78
   Collective on EPS
79
 
80
   Input Parameters:
81
.  eps - eigensolver context obtained from EPSCreate()
82
 
83
   Options Database Key:
1331 slepc 84
.    -eps_monitor_cancel - Cancels all monitors that have been hardwired
85
      into a code by calls to EPSMonitorSet(),
525 dsic.upv.es!antodo 86
      but does not cancel those set via the options database.
87
 
88
   Level: intermediate
89
 
1331 slepc 90
.seealso: EPSMonitorSet()
525 dsic.upv.es!antodo 91
@*/
1331 slepc 92
PetscErrorCode EPSMonitorCancel(EPS eps)
525 dsic.upv.es!antodo 93
{
1287 slepc 94
  PetscErrorCode ierr;
95
  PetscInt       i;
96
 
525 dsic.upv.es!antodo 97
  PetscFunctionBegin;
98
  PetscValidHeaderSpecific(eps,EPS_COOKIE,1);
1287 slepc 99
  for (i=0; i<eps->numbermonitors; i++) {
100
    if (eps->monitordestroy[i]) {
101
      ierr = (*eps->monitordestroy[i])(eps->monitorcontext[i]);CHKERRQ(ierr);
102
    }
103
  }
525 dsic.upv.es!antodo 104
  eps->numbermonitors = 0;
105
  PetscFunctionReturn(0);
106
}
107
 
108
#undef __FUNCT__  
109
#define __FUNCT__ "EPSGetMonitorContext"
110
/*@C
1021 slepc 111
   EPSGetMonitorContext - Gets the monitor context, as set by
525 dsic.upv.es!antodo 112
   EPSSetMonitor() for the FIRST monitor only.
113
 
114
   Not Collective
115
 
116
   Input Parameter:
117
.  eps - eigensolver context obtained from EPSCreate()
118
 
119
   Output Parameter:
120
.  ctx - monitor context
121
 
122
   Level: intermediate
123
 
1021 slepc 124
.seealso: EPSSetMonitor(), EPSDefaultMonitor()
525 dsic.upv.es!antodo 125
@*/
126
PetscErrorCode EPSGetMonitorContext(EPS eps, void **ctx)
127
{
128
  PetscFunctionBegin;
129
  PetscValidHeaderSpecific(eps,EPS_COOKIE,1);
130
  *ctx =      (eps->monitorcontext[0]);
131
  PetscFunctionReturn(0);
132
}
133
 
134
#undef __FUNCT__  
1331 slepc 135
#define __FUNCT__ "EPSMonitorDefault"
525 dsic.upv.es!antodo 136
/*@C
1331 slepc 137
   EPSMonitorDefault - Print the current approximate values and
525 dsic.upv.es!antodo 138
   error estimates at each iteration of the eigensolver.
139
 
140
   Collective on EPS
141
 
142
   Input Parameters:
143
+  eps    - eigensolver context
144
.  its    - iteration number
145
.  nconv  - number of converged eigenpairs so far
146
.  eigr   - real part of the eigenvalues
147
.  eigi   - imaginary part of the eigenvalues
148
.  errest - error estimates
149
.  nest   - number of error estimates to display
150
-  dummy  - unused monitor context
151
 
152
   Level: intermediate
153
 
1331 slepc 154
.seealso: EPSMonitorSet()
525 dsic.upv.es!antodo 155
@*/
1509 slepc 156
PetscErrorCode EPSMonitorDefault(EPS eps,PetscInt its,PetscInt nconv,PetscScalar *eigr,PetscScalar *eigi,PetscReal *errest,PetscInt nest,void *dummy)
525 dsic.upv.es!antodo 157
{
158
  PetscErrorCode ierr;
1509 slepc 159
  PetscInt       i;
525 dsic.upv.es!antodo 160
  PetscViewer    viewer = (PetscViewer) dummy;
161
 
162
  PetscFunctionBegin;
1220 slepc 163
  if (its) {
1422 slepc 164
    if (!viewer) viewer = PETSC_VIEWER_STDOUT_(((PetscObject)eps)->comm);
1220 slepc 165
    ierr = PetscViewerASCIIPrintf(viewer,"%3d EPS nconv=%d Values (Errors)",its,nconv);CHKERRQ(ierr);
166
    for (i=0;i<nest;i++) {
525 dsic.upv.es!antodo 167
#if defined(PETSC_USE_COMPLEX)
1220 slepc 168
      ierr = PetscViewerASCIIPrintf(viewer," %g%+gi",PetscRealPart(eigr[i]),PetscImaginaryPart(eigr[i]));CHKERRQ(ierr);
525 dsic.upv.es!antodo 169
#else
1220 slepc 170
      ierr = PetscViewerASCIIPrintf(viewer," %g",eigr[i]);CHKERRQ(ierr);
171
      if (eigi[i]!=0.0) { ierr = PetscViewerASCIIPrintf(viewer,"%+gi",eigi[i]);CHKERRQ(ierr); }
525 dsic.upv.es!antodo 172
#endif
1220 slepc 173
      ierr = PetscViewerASCIIPrintf(viewer," (%10.8e)",errest[i]);CHKERRQ(ierr);
174
    }
175
    ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
525 dsic.upv.es!antodo 176
  }
177
  PetscFunctionReturn(0);
178
}
623 dsic.upv.es!antodo 179
 
180
#undef __FUNCT__  
1331 slepc 181
#define __FUNCT__ "EPSMonitorLG"
1509 slepc 182
PetscErrorCode EPSMonitorLG(EPS eps,PetscInt its,PetscInt nconv,PetscScalar *eigr,PetscScalar *eigi,PetscReal *errest,PetscInt nest,void *monctx)
623 dsic.upv.es!antodo 183
{
626 dsic.upv.es!antodo 184
  PetscViewer    viewer = (PetscViewer) monctx;
185
  PetscDraw      draw;
186
  PetscDrawLG    lg;
623 dsic.upv.es!antodo 187
  PetscErrorCode ierr;
188
  PetscReal      *x,*y;
1509 slepc 189
  PetscInt       i;
190
  int            n = eps->nev;
626 dsic.upv.es!antodo 191
#if !defined(PETSC_USE_COMPLEX)
1004 slepc 192
  int            p;
626 dsic.upv.es!antodo 193
  PetscDraw      draw1;
194
  PetscDrawLG    lg1;
195
#endif
623 dsic.upv.es!antodo 196
 
197
  PetscFunctionBegin;
198
 
1422 slepc 199
  if (!viewer) { viewer = PETSC_VIEWER_DRAW_(((PetscObject)eps)->comm); }
626 dsic.upv.es!antodo 200
 
201
  ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
202
  ierr = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
203
  if (!its) {
628 dsic.upv.es!antodo 204
    ierr = PetscDrawSetTitle(draw,"Error estimates");CHKERRQ(ierr);
626 dsic.upv.es!antodo 205
    ierr = PetscDrawSetDoubleBuffer(draw);CHKERRQ(ierr);
206
    ierr = PetscDrawLGSetDimension(lg,n);CHKERRQ(ierr);
207
    ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);
208
    ierr = PetscDrawLGSetLimits(lg,0,1.0,log10(eps->tol)-2,0.0);CHKERRQ(ierr);
209
  }
210
 
211
#if !defined(PETSC_USE_COMPLEX)
212
  if (eps->ishermitian) {
213
    ierr = PetscViewerDrawGetDraw(viewer,1,&draw1);CHKERRQ(ierr);
214
    ierr = PetscViewerDrawGetDrawLG(viewer,1,&lg1);CHKERRQ(ierr);
623 dsic.upv.es!antodo 215
    if (!its) {
628 dsic.upv.es!antodo 216
      ierr = PetscDrawSetTitle(draw1,"Approximate eigenvalues");CHKERRQ(ierr);
626 dsic.upv.es!antodo 217
      ierr = PetscDrawSetDoubleBuffer(draw1);CHKERRQ(ierr);
218
      ierr = PetscDrawLGSetDimension(lg1,n);CHKERRQ(ierr);
219
      ierr = PetscDrawLGReset(lg1);CHKERRQ(ierr);
220
      ierr = PetscDrawLGSetLimits(lg1,0,1.0,1.e20,-1.e20);CHKERRQ(ierr);
623 dsic.upv.es!antodo 221
    }
222
  }
626 dsic.upv.es!antodo 223
#endif
623 dsic.upv.es!antodo 224
 
225
  ierr = PetscMalloc(sizeof(PetscReal)*n,&x);CHKERRQ(ierr);
226
  ierr = PetscMalloc(sizeof(PetscReal)*n,&y);CHKERRQ(ierr);
227
  for (i=0;i<n;i++) {
228
    x[i] = (PetscReal) its;
229
    if (errest[i] > 0.0) y[i] = log10(errest[i]); else y[i] = 0.0;
230
  }
231
  ierr = PetscDrawLGAddPoint(lg,x,y);CHKERRQ(ierr);
626 dsic.upv.es!antodo 232
#if !defined(PETSC_USE_COMPLEX)
233
  if (eps->ishermitian) {
234
    ierr = PetscDrawLGAddPoint(lg1,x,eps->eigr);CHKERRQ(ierr);
1004 slepc 235
    ierr = PetscDrawGetPause(draw1,&p);CHKERRQ(ierr);
626 dsic.upv.es!antodo 236
    ierr = PetscDrawSetPause(draw1,0);CHKERRQ(ierr);    
237
    ierr = PetscDrawLGDraw(lg1);CHKERRQ(ierr);
1004 slepc 238
    ierr = PetscDrawSetPause(draw1,p);CHKERRQ(ierr);    
626 dsic.upv.es!antodo 239
  }
240
#endif  
705 dsic.upv.es!antodo 241
  ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
623 dsic.upv.es!antodo 242
  ierr = PetscFree(x);CHKERRQ(ierr);
243
  ierr = PetscFree(y);CHKERRQ(ierr);  
244
  PetscFunctionReturn(0);
245
}
1021 slepc 246