Subversion Repositories slepc-dev

Rev

Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1288 slepc 1
/*
2
      SVD routines related to monitors.
1376 slepc 3
 
4
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1672 slepc 5
   SLEPc - Scalable Library for Eigenvalue Problem Computations
2575 eromero 6
   Copyright (c) 2002-2011, Universitat Politecnica de Valencia, Spain
1376 slepc 7
 
1672 slepc 8
   This file is part of SLEPc.
9
 
10
   SLEPc is free software: you can redistribute it and/or modify it under  the
11
   terms of version 3 of the GNU Lesser General Public License as published by
12
   the Free Software Foundation.
13
 
14
   SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY
15
   WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS
16
   FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for
17
   more details.
18
 
19
   You  should have received a copy of the GNU Lesser General  Public  License
20
   along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
1376 slepc 21
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1288 slepc 22
*/
1376 slepc 23
 
2729 jroman 24
#include <slepc-private/svdimpl.h>   /*I "slepcsvd.h" I*/
1288 slepc 25
 
26
#undef __FUNCT__  
2313 jroman 27
#define __FUNCT__ "SVDMonitor"
28
/*
29
   Runs the user provided monitor routines, if any.
30
*/
31
PetscErrorCode SVDMonitor(SVD svd,PetscInt it,PetscInt nconv,PetscReal *sigma,PetscReal *errest,PetscInt nest)
32
{
33
  PetscErrorCode ierr;
34
  PetscInt       i,n = svd->numbermonitors;
35
 
36
  PetscFunctionBegin;
37
  for (i=0;i<n;i++) {
38
    ierr = (*svd->monitor[i])(svd,it,nconv,sigma,errest,nest,svd->monitorcontext[i]);CHKERRQ(ierr);
39
  }
40
  PetscFunctionReturn(0);
41
}
42
 
43
#undef __FUNCT__  
1331 slepc 44
#define __FUNCT__ "SVDMonitorSet"
1288 slepc 45
/*@C
1331 slepc 46
   SVDMonitorSet - Sets an ADDITIONAL function to be called at every
1288 slepc 47
   iteration to monitor the error estimates for each requested singular triplet.
48
 
49
   Collective on SVD
50
 
51
   Input Parameters:
52
+  svd     - singular value solver context obtained from SVDCreate()
53
.  monitor - pointer to function (if this is PETSC_NULL, it turns off monitoring)
54
-  mctx    - [optional] context for private data for the
55
             monitor routine (use PETSC_NULL if no context is desired)
56
 
57
   Calling Sequence of monitor:
1504 slepc 58
$     monitor (SVD svd, PetscInt its, PetscInt nconv, PetscReal *sigma, PetscReal* errest, PetscInt nest, void *mctx)
1288 slepc 59
 
60
+  svd    - singular value solver context obtained from SVDCreate()
61
.  its    - iteration number
62
.  nconv  - number of converged singular triplets
63
.  sigma  - singular values
64
.  errest - relative error estimates for each singular triplet
65
.  nest   - number of error estimates
1331 slepc 66
-  mctx   - optional monitoring context, as set by SVDMonitorSet()
1288 slepc 67
 
68
   Options Database Keys:
2054 eromero 69
+    -svd_monitor          - print only the first error estimate
70
.    -svd_monitor_all      - print error estimates at each iteration
71
.    -svd_monitor_conv     - print the singular value approximations only when
1897 jroman 72
      convergence has been reached
2054 eromero 73
.    -svd_monitor_draw     - sets line graph monitor for the first unconverged
74
      approximate singular value
75
.    -svd_monitor_draw_all - sets line graph monitor for all unconverged
76
      approximate singular value
77
-    -svd_monitor_cancel   - cancels all monitors that have been hardwired into
1331 slepc 78
      a code by calls to SVDMonitorSet(), but does not cancel those set via
1288 slepc 79
      the options database.
80
 
81
   Notes:  
82
   Several different monitoring routines may be set by calling
1331 slepc 83
   SVDMonitorSet() multiple times; all will be called in the
1288 slepc 84
   order in which they were set.
85
 
86
   Level: intermediate
87
 
2644 jroman 88
.seealso: SVDMonitorFirst(), SVDMonitorAll(), SVDMonitorCancel()
1288 slepc 89
@*/
2351 jroman 90
PetscErrorCode SVDMonitorSet(SVD svd,PetscErrorCode (*monitor)(SVD,PetscInt,PetscInt,PetscReal*,PetscReal*,PetscInt,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
1288 slepc 91
{
92
  PetscFunctionBegin;
2213 jroman 93
  PetscValidHeaderSpecific(svd,SVD_CLASSID,1);
2762 jroman 94
  if (svd->numbermonitors >= MAXSVDMONITORS) SETERRQ(((PetscObject)svd)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Too many SVD monitors set");
1288 slepc 95
  svd->monitor[svd->numbermonitors]           = monitor;
96
  svd->monitorcontext[svd->numbermonitors]    = (void*)mctx;
97
  svd->monitordestroy[svd->numbermonitors++]  = monitordestroy;
98
  PetscFunctionReturn(0);
99
}
100
 
101
#undef __FUNCT__  
1331 slepc 102
#define __FUNCT__ "SVDMonitorCancel"
1288 slepc 103
/*@
1331 slepc 104
   SVDMonitorCancel - Clears all monitors for an SVD object.
1288 slepc 105
 
106
   Collective on SVD
107
 
108
   Input Parameters:
109
.  svd - singular value solver context obtained from SVDCreate()
110
 
111
   Options Database Key:
1331 slepc 112
.    -svd_monitor_cancel - Cancels all monitors that have been hardwired
113
      into a code by calls to SVDMonitorSet(),
1288 slepc 114
      but does not cancel those set via the options database.
115
 
116
   Level: intermediate
117
 
2134 eromero 118
.seealso: SVDMonitorSet()
1288 slepc 119
@*/
1331 slepc 120
PetscErrorCode SVDMonitorCancel(SVD svd)
1288 slepc 121
{
122
  PetscErrorCode ierr;
123
  PetscInt       i;
124
 
125
  PetscFunctionBegin;
2213 jroman 126
  PetscValidHeaderSpecific(svd,SVD_CLASSID,1);
1288 slepc 127
  for (i=0; i<svd->numbermonitors; i++) {
128
    if (svd->monitordestroy[i]) {
2311 jroman 129
      ierr = (*svd->monitordestroy[i])(&svd->monitorcontext[i]);CHKERRQ(ierr);
1288 slepc 130
    }
131
  }
132
  svd->numbermonitors = 0;
133
  PetscFunctionReturn(0);
134
}
135
 
136
#undef __FUNCT__  
137
#define __FUNCT__ "SVDGetMonitorContext"
138
/*@C
139
   SVDGetMonitorContext - Gets the monitor context, as set by
1331 slepc 140
   SVDMonitorSet() for the FIRST monitor only.
1288 slepc 141
 
142
   Not Collective
143
 
144
   Input Parameter:
145
.  svd - singular value solver context obtained from SVDCreate()
146
 
147
   Output Parameter:
148
.  ctx - monitor context
149
 
150
   Level: intermediate
151
 
2134 eromero 152
.seealso: SVDMonitorSet()
1288 slepc 153
@*/
2331 jroman 154
PetscErrorCode SVDGetMonitorContext(SVD svd,void **ctx)
1288 slepc 155
{
156
  PetscFunctionBegin;
2213 jroman 157
  PetscValidHeaderSpecific(svd,SVD_CLASSID,1);
2317 jroman 158
  *ctx = (svd->monitorcontext[0]);
1288 slepc 159
  PetscFunctionReturn(0);
160
}
161
 
162
#undef __FUNCT__  
2054 eromero 163
#define __FUNCT__ "SVDMonitorAll"
1288 slepc 164
/*@C
2054 eromero 165
   SVDMonitorAll - Print the current approximate values and
1288 slepc 166
   error estimates at each iteration of the singular value solver.
167
 
168
   Collective on SVD
169
 
170
   Input Parameters:
171
+  svd    - singular value solver context
172
.  its    - iteration number
173
.  nconv  - number of converged singular triplets so far
174
.  sigma  - singular values
175
.  errest - error estimates
176
.  nest   - number of error estimates to display
2671 jroman 177
-  monctx - monitor context (contains viewer, can be PETSC_NULL)
1288 slepc 178
 
179
   Level: intermediate
180
 
2328 jroman 181
.seealso: SVDMonitorSet(), SVDMonitorFirst(), SVDMonitorConverged()
1288 slepc 182
@*/
2671 jroman 183
PetscErrorCode SVDMonitorAll(SVD svd,PetscInt its,PetscInt nconv,PetscReal *sigma,PetscReal *errest,PetscInt nest,void *monctx)
1288 slepc 184
{
2419 jroman 185
  PetscErrorCode ierr;
186
  PetscInt       i;
2671 jroman 187
  PetscViewer    viewer = monctx? (PetscViewer)monctx: PETSC_VIEWER_STDOUT_(((PetscObject)svd)->comm);
1288 slepc 188
 
189
  PetscFunctionBegin;
190
  if (its) {
2419 jroman 191
    ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)svd)->tablevel);CHKERRQ(ierr);
192
    ierr = PetscViewerASCIIPrintf(viewer,"%3D SVD nconv=%D Values (Errors)",its,nconv);CHKERRQ(ierr);
1288 slepc 193
    for (i=0;i<nest;i++) {
2419 jroman 194
      ierr = PetscViewerASCIIPrintf(viewer," %G (%10.8e)",sigma[i],(double)errest[i]);CHKERRQ(ierr);
1288 slepc 195
    }
2419 jroman 196
    ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
197
    ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)svd)->tablevel);CHKERRQ(ierr);
1288 slepc 198
  }
199
  PetscFunctionReturn(0);
200
}
201
 
202
#undef __FUNCT__  
1721 antodo 203
#define __FUNCT__ "SVDMonitorFirst"
204
/*@C
205
   SVDMonitorFirst - Print the first unconverged approximate values and
206
   error estimates at each iteration of the singular value solver.
207
 
208
   Collective on SVD
209
 
210
   Input Parameters:
211
+  svd    - singular value solver context
212
.  its    - iteration number
213
.  nconv  - number of converged singular triplets so far
214
.  sigma  - singular values
215
.  errest - error estimates
216
.  nest   - number of error estimates to display
2671 jroman 217
-  monctx - monitor context (contains viewer, can be PETSC_NULL)
1721 antodo 218
 
219
   Level: intermediate
220
 
2328 jroman 221
.seealso: SVDMonitorSet(), SVDMonitorAll(), SVDMonitorConverged()
1721 antodo 222
@*/
2671 jroman 223
PetscErrorCode SVDMonitorFirst(SVD svd,PetscInt its,PetscInt nconv,PetscReal *sigma,PetscReal *errest,PetscInt nest,void *monctx)
1721 antodo 224
{
2419 jroman 225
  PetscErrorCode ierr;
2671 jroman 226
  PetscViewer    viewer = monctx? (PetscViewer)monctx: PETSC_VIEWER_STDOUT_(((PetscObject)svd)->comm);
1721 antodo 227
 
228
  PetscFunctionBegin;
229
  if (its && nconv<nest) {
2419 jroman 230
    ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)svd)->tablevel);CHKERRQ(ierr);
231
    ierr = PetscViewerASCIIPrintf(viewer,"%3D SVD nconv=%D first unconverged value (error)",its,nconv);CHKERRQ(ierr);
232
    ierr = PetscViewerASCIIPrintf(viewer," %G (%10.8e)\n",sigma[nconv],(double)errest[nconv]);CHKERRQ(ierr);
233
    ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)svd)->tablevel);CHKERRQ(ierr);
1721 antodo 234
  }
235
  PetscFunctionReturn(0);
236
}
237
 
238
#undef __FUNCT__  
239
#define __FUNCT__ "SVDMonitorConverged"
240
/*@C
241
   SVDMonitorConverged - Print the approximate values and error estimates as they converge.
242
 
243
   Collective on SVD
244
 
245
   Input Parameters:
246
+  svd    - singular value solver context
247
.  its    - iteration number
248
.  nconv  - number of converged singular triplets so far
249
.  sigma  - singular values
250
.  errest - error estimates
251
.  nest   - number of error estimates to display
2671 jroman 252
-  monctx - monitor context
1721 antodo 253
 
2671 jroman 254
   Note:
255
   The monitor context must contain a struct with a PetscViewer and a
256
   PetscInt. In Fortran, pass a PETSC_NULL_OBJECT.
257
 
1721 antodo 258
   Level: intermediate
259
 
2328 jroman 260
.seealso: SVDMonitorSet(), SVDMonitorFirst(), SVDMonitorAll()
1721 antodo 261
@*/
2671 jroman 262
PetscErrorCode SVDMonitorConverged(SVD svd,PetscInt its,PetscInt nconv,PetscReal *sigma,PetscReal *errest,PetscInt nest,void *monctx)
1721 antodo 263
{
2311 jroman 264
  PetscErrorCode   ierr;
265
  PetscInt         i;
2671 jroman 266
  PetscViewer      viewer;
267
  SlepcConvMonitor ctx = (SlepcConvMonitor) monctx;
1721 antodo 268
 
269
  PetscFunctionBegin;
2671 jroman 270
  if (!monctx) SETERRQ(((PetscObject)svd)->comm,PETSC_ERR_ARG_WRONG,"Must provide a context for SVDMonitorConverged");
1721 antodo 271
  if (!its) {
1951 jroman 272
    ctx->oldnconv = 0;
1721 antodo 273
  } else {
2671 jroman 274
    viewer = ctx->viewer? ctx->viewer: PETSC_VIEWER_STDOUT_(((PetscObject)svd)->comm);
1951 jroman 275
    for (i=ctx->oldnconv;i<nconv;i++) {
2671 jroman 276
      ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)svd)->tablevel);CHKERRQ(ierr);
277
      ierr = PetscViewerASCIIPrintf(viewer,"%3D SVD converged value (error) #%D",its,i);CHKERRQ(ierr);
278
      ierr = PetscViewerASCIIPrintf(viewer," %G (%10.8e)\n",sigma[i],(double)errest[i]);CHKERRQ(ierr);
279
      ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)svd)->tablevel);CHKERRQ(ierr);
1721 antodo 280
    }
1951 jroman 281
    ctx->oldnconv = nconv;
1721 antodo 282
  }
283
  PetscFunctionReturn(0);
284
}
285
 
2311 jroman 286
#undef __FUNCT__  
1331 slepc 287
#define __FUNCT__ "SVDMonitorLG"
1504 slepc 288
PetscErrorCode SVDMonitorLG(SVD svd,PetscInt its,PetscInt nconv,PetscReal *sigma,PetscReal *errest,PetscInt nest,void *monctx)
1288 slepc 289
{
290
  PetscViewer    viewer = (PetscViewer) monctx;
2317 jroman 291
  PetscDraw      draw,draw1;
292
  PetscDrawLG    lg,lg1;
1288 slepc 293
  PetscErrorCode ierr;
2054 eromero 294
  PetscReal      x,y,p;
295
 
296
  PetscFunctionBegin;
297
  if (!viewer) { viewer = PETSC_VIEWER_DRAW_(((PetscObject)svd)->comm); }
298
  ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
299
  ierr = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
300
  ierr = PetscViewerDrawGetDraw(viewer,1,&draw1);CHKERRQ(ierr);
301
  ierr = PetscViewerDrawGetDrawLG(viewer,1,&lg1);CHKERRQ(ierr);
302
 
303
  if (!its) {
304
    ierr = PetscDrawSetTitle(draw,"Error estimates");CHKERRQ(ierr);
305
    ierr = PetscDrawSetDoubleBuffer(draw);CHKERRQ(ierr);
306
    ierr = PetscDrawLGSetDimension(lg,1);CHKERRQ(ierr);
307
    ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);
308
    ierr = PetscDrawLGSetLimits(lg,0,1.0,log10(svd->tol)-2,0.0);CHKERRQ(ierr);
309
 
310
    ierr = PetscDrawSetTitle(draw1,"Approximate singular values");CHKERRQ(ierr);
311
    ierr = PetscDrawSetDoubleBuffer(draw1);CHKERRQ(ierr);
312
    ierr = PetscDrawLGSetDimension(lg1,1);CHKERRQ(ierr);
313
    ierr = PetscDrawLGReset(lg1);CHKERRQ(ierr);
314
    ierr = PetscDrawLGSetLimits(lg1,0,1.0,1.e20,-1.e20);CHKERRQ(ierr);
315
  }
316
 
317
  x = (PetscReal) its;
318
  if (errest[nconv] > 0.0) y = log10(errest[nconv]); else y = 0.0;
319
  ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
320
 
321
  ierr = PetscDrawLGAddPoint(lg1,&x,svd->sigma);CHKERRQ(ierr);
322
  ierr = PetscDrawGetPause(draw1,&p);CHKERRQ(ierr);
323
  ierr = PetscDrawSetPause(draw1,0);CHKERRQ(ierr);
324
  ierr = PetscDrawLGDraw(lg1);CHKERRQ(ierr);
325
  ierr = PetscDrawSetPause(draw1,p);CHKERRQ(ierr);
326
 
327
  ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
328
  PetscFunctionReturn(0);
329
}
330
 
331
#undef __FUNCT__  
332
#define __FUNCT__ "SVDMonitorLGAll"
333
PetscErrorCode SVDMonitorLGAll(SVD svd,PetscInt its,PetscInt nconv,PetscReal *sigma,PetscReal *errest,PetscInt nest,void *monctx)
334
{
335
  PetscViewer    viewer = (PetscViewer) monctx;
2317 jroman 336
  PetscDraw      draw,draw1;
337
  PetscDrawLG    lg,lg1;
2054 eromero 338
  PetscErrorCode ierr;
1852 antodo 339
  PetscReal      *x,*y,p;
2317 jroman 340
  PetscInt       i,n = PetscMin(svd->nsv,255);
1288 slepc 341
 
342
  PetscFunctionBegin;
1422 slepc 343
  if (!viewer) { viewer = PETSC_VIEWER_DRAW_(((PetscObject)svd)->comm); }
1288 slepc 344
  ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
345
  ierr = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
346
  ierr = PetscViewerDrawGetDraw(viewer,1,&draw1);CHKERRQ(ierr);
347
  ierr = PetscViewerDrawGetDrawLG(viewer,1,&lg1);CHKERRQ(ierr);
348
 
349
  if (!its) {
350
    ierr = PetscDrawSetTitle(draw,"Error estimates");CHKERRQ(ierr);
351
    ierr = PetscDrawSetDoubleBuffer(draw);CHKERRQ(ierr);
352
    ierr = PetscDrawLGSetDimension(lg,n);CHKERRQ(ierr);
353
    ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);
354
    ierr = PetscDrawLGSetLimits(lg,0,1.0,log10(svd->tol)-2,0.0);CHKERRQ(ierr);
355
 
356
    ierr = PetscDrawSetTitle(draw1,"Approximate singular values");CHKERRQ(ierr);
357
    ierr = PetscDrawSetDoubleBuffer(draw1);CHKERRQ(ierr);
358
    ierr = PetscDrawLGSetDimension(lg1,n);CHKERRQ(ierr);
359
    ierr = PetscDrawLGReset(lg1);CHKERRQ(ierr);
360
    ierr = PetscDrawLGSetLimits(lg1,0,1.0,1.e20,-1.e20);CHKERRQ(ierr);
361
  }
362
 
363
  ierr = PetscMalloc(sizeof(PetscReal)*n,&x);CHKERRQ(ierr);
364
  ierr = PetscMalloc(sizeof(PetscReal)*n,&y);CHKERRQ(ierr);
365
  for (i=0;i<n;i++) {
366
    x[i] = (PetscReal) its;
2054 eromero 367
    if (i < nest && errest[i] > 0.0) y[i] = log10(errest[i]);
368
    else y[i] = 0.0;
1288 slepc 369
  }
370
  ierr = PetscDrawLGAddPoint(lg,x,y);CHKERRQ(ierr);
371
 
372
  ierr = PetscDrawLGAddPoint(lg1,x,svd->sigma);CHKERRQ(ierr);
373
  ierr = PetscDrawGetPause(draw1,&p);CHKERRQ(ierr);
374
  ierr = PetscDrawSetPause(draw1,0);CHKERRQ(ierr);
375
  ierr = PetscDrawLGDraw(lg1);CHKERRQ(ierr);
376
  ierr = PetscDrawSetPause(draw1,p);CHKERRQ(ierr);
377
 
378
  ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
379
  ierr = PetscFree(x);CHKERRQ(ierr);
380
  ierr = PetscFree(y);CHKERRQ(ierr);
381
  PetscFunctionReturn(0);
382
}