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
1288 slepc 1
/*
2
      SVD routines related to monitors.
3
*/
4
#include "src/svd/svdimpl.h"   /*I "slepcsvd.h" I*/
5
 
6
#undef __FUNCT__  
1331 slepc 7
#define __FUNCT__ "SVDMonitorSet"
1288 slepc 8
/*@C
1331 slepc 9
   SVDMonitorSet - Sets an ADDITIONAL function to be called at every
1288 slepc 10
   iteration to monitor the error estimates for each requested singular triplet.
11
 
12
   Collective on SVD
13
 
14
   Input Parameters:
15
+  svd     - singular value solver context obtained from SVDCreate()
16
.  monitor - pointer to function (if this is PETSC_NULL, it turns off monitoring)
17
-  mctx    - [optional] context for private data for the
18
             monitor routine (use PETSC_NULL if no context is desired)
19
 
20
   Calling Sequence of monitor:
21
$     monitor (SVD svd, int its, int nconv, PetscReal *sigma, PetscReal* errest, int nest, void *mctx)
22
 
23
+  svd    - singular value solver context obtained from SVDCreate()
24
.  its    - iteration number
25
.  nconv  - number of converged singular triplets
26
.  sigma  - singular values
27
.  errest - relative error estimates for each singular triplet
28
.  nest   - number of error estimates
1331 slepc 29
-  mctx   - optional monitoring context, as set by SVDMonitorSet()
1288 slepc 30
 
31
   Options Database Keys:
32
+    -svd_monitor        - print error estimates at each iteration
1331 slepc 33
-    -svd_monitor_cancel - cancels all monitors that have been hardwired into
34
      a code by calls to SVDMonitorSet(), but does not cancel those set via
1288 slepc 35
      the options database.
36
 
37
   Notes:  
38
   Several different monitoring routines may be set by calling
1331 slepc 39
   SVDMonitorSet() multiple times; all will be called in the
1288 slepc 40
   order in which they were set.
41
 
42
   Level: intermediate
43
 
1331 slepc 44
.seealso: SVDMonitorDefault(), SVDMonitorCancel()
1288 slepc 45
@*/
1331 slepc 46
PetscErrorCode SVDMonitorSet(SVD svd,PetscErrorCode (*monitor)(SVD,int,int,PetscReal*,PetscReal*,int,void*),
1288 slepc 47
                             void *mctx,PetscErrorCode (*monitordestroy)(void*))
48
{
49
  PetscFunctionBegin;
50
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
51
  if (svd->numbermonitors >= MAXSVDMONITORS) {
52
    SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many SVD monitors set");
53
  }
54
  svd->monitor[svd->numbermonitors]           = monitor;
55
  svd->monitorcontext[svd->numbermonitors]    = (void*)mctx;
56
  svd->monitordestroy[svd->numbermonitors++]  = monitordestroy;
57
  PetscFunctionReturn(0);
58
}
59
 
60
#undef __FUNCT__  
1331 slepc 61
#define __FUNCT__ "SVDMonitorCancel"
1288 slepc 62
/*@
1331 slepc 63
   SVDMonitorCancel - Clears all monitors for an SVD object.
1288 slepc 64
 
65
   Collective on SVD
66
 
67
   Input Parameters:
68
.  svd - singular value solver context obtained from SVDCreate()
69
 
70
   Options Database Key:
1331 slepc 71
.    -svd_monitor_cancel - Cancels all monitors that have been hardwired
72
      into a code by calls to SVDMonitorSet(),
1288 slepc 73
      but does not cancel those set via the options database.
74
 
75
   Level: intermediate
76
 
1331 slepc 77
.seealso: SVDMonitorCancel()
1288 slepc 78
@*/
1331 slepc 79
PetscErrorCode SVDMonitorCancel(SVD svd)
1288 slepc 80
{
81
  PetscErrorCode ierr;
82
  PetscInt       i;
83
 
84
  PetscFunctionBegin;
85
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
86
  for (i=0; i<svd->numbermonitors; i++) {
87
    if (svd->monitordestroy[i]) {
88
      ierr = (*svd->monitordestroy[i])(svd->monitorcontext[i]);CHKERRQ(ierr);
89
    }
90
  }
91
  svd->numbermonitors = 0;
92
  PetscFunctionReturn(0);
93
}
94
 
95
#undef __FUNCT__  
96
#define __FUNCT__ "SVDGetMonitorContext"
97
/*@C
98
   SVDGetMonitorContext - Gets the monitor context, as set by
1331 slepc 99
   SVDMonitorSet() for the FIRST monitor only.
1288 slepc 100
 
101
   Not Collective
102
 
103
   Input Parameter:
104
.  svd - singular value solver context obtained from SVDCreate()
105
 
106
   Output Parameter:
107
.  ctx - monitor context
108
 
109
   Level: intermediate
110
 
1331 slepc 111
.seealso: SVDMonitorSet(), SVDMonitorDefault()
1288 slepc 112
@*/
113
PetscErrorCode SVDGetMonitorContext(SVD svd, void **ctx)
114
{
115
  PetscFunctionBegin;
116
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
117
  *ctx =      (svd->monitorcontext[0]);
118
  PetscFunctionReturn(0);
119
}
120
 
121
#undef __FUNCT__  
1331 slepc 122
#define __FUNCT__ "SVDMonitorDefault"
1288 slepc 123
/*@C
124
   SVDDefaultMonitor - Print the current approximate values and
125
   error estimates at each iteration of the singular value solver.
126
 
127
   Collective on SVD
128
 
129
   Input Parameters:
130
+  svd    - singular value solver context
131
.  its    - iteration number
132
.  nconv  - number of converged singular triplets so far
133
.  sigma  - singular values
134
.  errest - error estimates
135
.  nest   - number of error estimates to display
136
-  dummy  - unused monitor context
137
 
138
   Level: intermediate
139
 
1331 slepc 140
.seealso: SVDMonitorSet()
1288 slepc 141
@*/
1331 slepc 142
PetscErrorCode SVDMonitorDefault(SVD svd,int its,int nconv,PetscReal *sigma,PetscReal *errest,int nest,void *dummy)
1288 slepc 143
{
144
  PetscErrorCode ierr;
145
  int            i;
146
  PetscViewer    viewer = (PetscViewer) dummy;
147
 
148
  PetscFunctionBegin;
149
  if (its) {
150
    if (!viewer) viewer = PETSC_VIEWER_STDOUT_(svd->comm);
151
    ierr = PetscViewerASCIIPrintf(viewer,"%3d SVD nconv=%d Values (Errors)",its,nconv);CHKERRQ(ierr);
152
    for (i=0;i<nest;i++) {
153
      ierr = PetscViewerASCIIPrintf(viewer," %g (%10.8e)",sigma[i],errest[i]);CHKERRQ(ierr);
154
    }
155
    ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
156
  }
157
  PetscFunctionReturn(0);
158
}
159
 
160
#undef __FUNCT__  
1331 slepc 161
#define __FUNCT__ "SVDMonitorLG"
162
PetscErrorCode SVDMonitorLG(SVD svd,int its,int nconv,PetscReal *sigma,PetscReal *errest,int nest,void *monctx)
1288 slepc 163
{
164
  PetscViewer    viewer = (PetscViewer) monctx;
165
  PetscDraw      draw;
166
  PetscDrawLG    lg;
167
  PetscErrorCode ierr;
168
  PetscReal      *x,*y;
169
  int            i,n = svd->nsv;
170
  int            p;
171
  PetscDraw      draw1;
172
  PetscDrawLG    lg1;
173
 
174
  PetscFunctionBegin;
175
 
176
  if (!viewer) { viewer = PETSC_VIEWER_DRAW_(svd->comm); }
177
 
178
  ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
179
  ierr = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
180
  ierr = PetscViewerDrawGetDraw(viewer,1,&draw1);CHKERRQ(ierr);
181
  ierr = PetscViewerDrawGetDrawLG(viewer,1,&lg1);CHKERRQ(ierr);
182
 
183
  if (!its) {
184
    ierr = PetscDrawSetTitle(draw,"Error estimates");CHKERRQ(ierr);
185
    ierr = PetscDrawSetDoubleBuffer(draw);CHKERRQ(ierr);
186
    ierr = PetscDrawLGSetDimension(lg,n);CHKERRQ(ierr);
187
    ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);
188
    ierr = PetscDrawLGSetLimits(lg,0,1.0,log10(svd->tol)-2,0.0);CHKERRQ(ierr);
189
 
190
    ierr = PetscDrawSetTitle(draw1,"Approximate singular values");CHKERRQ(ierr);
191
    ierr = PetscDrawSetDoubleBuffer(draw1);CHKERRQ(ierr);
192
    ierr = PetscDrawLGSetDimension(lg1,n);CHKERRQ(ierr);
193
    ierr = PetscDrawLGReset(lg1);CHKERRQ(ierr);
194
    ierr = PetscDrawLGSetLimits(lg1,0,1.0,1.e20,-1.e20);CHKERRQ(ierr);
195
  }
196
 
197
  ierr = PetscMalloc(sizeof(PetscReal)*n,&x);CHKERRQ(ierr);
198
  ierr = PetscMalloc(sizeof(PetscReal)*n,&y);CHKERRQ(ierr);
199
  for (i=0;i<n;i++) {
200
    x[i] = (PetscReal) its;
201
    if (errest[i] > 0.0) y[i] = log10(errest[i]); else y[i] = 0.0;
202
  }
203
  ierr = PetscDrawLGAddPoint(lg,x,y);CHKERRQ(ierr);
204
 
205
  ierr = PetscDrawLGAddPoint(lg1,x,svd->sigma);CHKERRQ(ierr);
206
  ierr = PetscDrawGetPause(draw1,&p);CHKERRQ(ierr);
207
  ierr = PetscDrawSetPause(draw1,0);CHKERRQ(ierr);
208
  ierr = PetscDrawLGDraw(lg1);CHKERRQ(ierr);
209
  ierr = PetscDrawSetPause(draw1,p);CHKERRQ(ierr);
210
 
211
  ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
212
  ierr = PetscFree(x);CHKERRQ(ierr);
213
  ierr = PetscFree(y);CHKERRQ(ierr);
214
  PetscFunctionReturn(0);
215
}
216