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
1272 slepc 1
/*
2
     SVD routines for setting solver options.
3
*/
4
#include "src/svd/svdimpl.h"      /*I "slepcsvd.h" I*/
5
 
6
#undef __FUNCT__  
7
#define __FUNCT__ "SVDSetTransposeMode"
8
/*@C
9
   SVDSetTransposeMode - Sets how to handle the transpose of the matrix
10
   associated with the singular value problem.
11
 
12
   Collective on SVD and Mat
13
 
14
   Input Parameters:
15
+  svd  - the singular value solver context
16
-  mode - how to compute the transpose, one of SVD_TRANSPOSE_EXPLICIT
17
          or SVD_TRANSPOSE_MATMULT (see notes below)
18
 
19
   Options Database Key:
20
.  -svd_transpose_mode <mode> - Indicates the mode flag, where <mode>
21
    is one of 'explicit' or 'matmult'.
22
 
23
   Notes:
24
   In the SVD_TRANSPOSE_EXPLICIT mode, the transpose of the matrix is
25
   explicitly built.
26
 
27
   The option SVD_TRANSPOSE_MATMULT does not build the transpose, but
28
   handles it implicitly via MatMultTranspose() operations. This is
29
   likely to be more inefficient than SVD_TRANSPOSE_EXPLICIT, both in
30
   sequential and in parallel, but requires less storage.
31
 
32
   The default is SVD_TRANSPOSE_EXPLICIT if the matrix has defined the
33
   MatTranspose operation, and SVD_TRANSPOSE_MATMULT otherwise.
34
 
35
   Level: advanced
36
 
37
   .seealso: SVDGetTransposeMode(), SVDSolve(), SVDSetOperator(), SVDGetOperator()
38
@*/
39
PetscErrorCode SVDSetTransposeMode(SVD svd,SVDTransposeMode mode)
40
{
41
  PetscFunctionBegin;
42
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
43
  switch (mode) {
1283 slepc 44
    case PETSC_DEFAULT:
45
      mode = PETSC_DECIDE;
1272 slepc 46
    case SVD_TRANSPOSE_EXPLICIT:
1283 slepc 47
    case SVD_TRANSPOSE_IMPLICIT:
48
    case PETSC_DECIDE:
1272 slepc 49
      svd->transmode = mode;
50
      svd->setupcalled = 0;
51
      break;
52
    default:
53
      SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Invalid transpose mode");
54
  }
55
  PetscFunctionReturn(0);
56
}
57
 
58
#undef __FUNCT__  
59
#define __FUNCT__ "SVDGetTransposeMode"
60
/*@
61
   SVDGetTransposeMode - Gets the mode use to compute the  transpose
62
   of the matrix associated with the singular value problem.
63
 
64
   Not collective
65
 
66
   Input Parameter:
67
+  svd  - the singular value solver context
68
 
69
   Output paramter:
70
+  mode - how to compute the transpose, one of SVD_TRANSPOSE_EXPLICIT
71
          or SVD_TRANSPOSE_MATMULT
72
 
73
   Level: advanced
74
 
75
   .seealso: SVDSetTransposeMode(), SVDSolve(), SVDSetOperator(), SVDGetOperator()
76
@*/
77
PetscErrorCode SVDGetTransposeMode(SVD svd,SVDTransposeMode *mode)
78
{
79
  PetscFunctionBegin;
80
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
81
  PetscValidPointer(mode,2);
82
  *mode = svd->transmode;
83
  PetscFunctionReturn(0);
84
}
85
 
86
#undef __FUNCT__  
87
#define __FUNCT__ "SVDSetTolerances"
88
/*@
89
   SVDSetTolerances - Sets the tolerance and maximum
90
   iteration count used by the default SVD convergence testers.
91
 
92
   Collective on SVD
93
 
94
   Input Parameters:
95
+  svd - the singluar value solver context
96
.  tol - the convergence tolerance
97
-  maxits - maximum number of iterations to use
98
 
99
   Options Database Keys:
100
+  -svd_tol <tol> - Sets the convergence tolerance
101
-  -svd_max_it <maxits> - Sets the maximum number of iterations allowed
1283 slepc 102
   (use PETSC_DECIDE to compute an educated guess based on basis and matrix sizes)
1272 slepc 103
 
104
   Notes:
105
   Use PETSC_IGNORE to retain the previous value of any parameter.
106
 
107
   Level: intermediate
108
 
109
.seealso: SVDGetTolerances()
110
@*/
111
PetscErrorCode SVDSetTolerances(SVD svd,PetscReal tol,int maxits)
112
{
113
  PetscFunctionBegin;
114
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
115
  if (tol != PETSC_IGNORE) {
1283 slepc 116
    if (tol == PETSC_DEFAULT) {
117
      tol = 1e-7;
118
    } else {
119
      if (tol < 0.0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Illegal value of tol. Must be > 0");
120
      svd->tol = tol;
121
    }
1272 slepc 122
  }
123
  if (maxits != PETSC_IGNORE) {
1283 slepc 124
    if (maxits == PETSC_DEFAULT || maxits == PETSC_DECIDE) {
125
      svd->max_it = PETSC_DECIDE;
1272 slepc 126
      svd->setupcalled = 0;
127
    } else {
1277 slepc 128
      if (maxits < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Illegal value of maxits. Must be > 0");
1283 slepc 129
      svd->max_it = maxits;
1272 slepc 130
    }
131
  }
132
  PetscFunctionReturn(0);
133
}
134
 
135
#undef __FUNCT__  
136
#define __FUNCT__ "SVDGetTolerances"
137
/*@
138
   SVDGetTolerances - Gets the tolerance and maximum
139
   iteration count used by the default SVD convergence tests.
140
 
141
   Not Collective
142
 
143
   Input Parameter:
144
.  svd - the singular value solver context
145
 
146
   Output Parameters:
147
+  tol - the convergence tolerance
148
-  maxits - maximum number of iterations
149
 
150
   Notes:
151
   The user can specify PETSC_NULL for any parameter that is not needed.
152
 
153
   Level: intermediate
154
 
155
.seealso: SVDSetTolerances()
156
@*/
157
PetscErrorCode SVDGetTolerances(SVD svd,PetscReal *tol,int *maxits)
158
{
159
  PetscFunctionBegin;
160
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
161
  if (tol)    *tol    = svd->tol;
162
  if (maxits) *maxits = svd->max_it;
163
  PetscFunctionReturn(0);
164
}
165
 
166
#undef __FUNCT__  
167
#define __FUNCT__ "SVDSetDimensions"
168
/*@
169
   SVDSetDimensions - Sets the number of singular values to compute
170
   and the dimension of the subspace.
171
 
172
   Collective on SVD
173
 
174
   Input Parameters:
175
+  svd - the singular solver context
176
.  nsv - number of singular values to compute
177
-  ncv - the maximum dimension of the subspace to be used by the solver
178
 
179
   Options Database Keys:
180
+  -svd_nsv <nsv> - Sets the number of singular values
181
-  -svd_ncv <ncv> - Sets the dimension of the subspace
182
 
183
   Notes:
184
   Use PETSC_IGNORE to retain the previous value of any parameter.
185
 
1283 slepc 186
   Use PETSC_DECIDE for ncv to assign a reasonably good value, which is
187
   dependent on the solution method and the number of singular values required.
1272 slepc 188
 
189
   Level: intermediate
190
 
191
.seealso: SVDGetDimensions()
192
@*/
193
PetscErrorCode SVDSetDimensions(SVD svd,int nsv,int ncv)
194
{
195
  PetscFunctionBegin;
196
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
197
 
198
  if (nsv != PETSC_IGNORE) {
199
    if (nsv<1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Illegal value of nsv. Must be > 0");
200
    svd->nsv = nsv;
201
  }
202
  if (ncv != PETSC_IGNORE) {
1283 slepc 203
    if (ncv == PETSC_DEFAULT || ncv == PETSC_DECIDE) {
204
      svd->ncv = PETSC_DECIDE;
205
    } else {
206
      if (ncv<1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Illegal value of ncv. Must be > 0");
207
      svd->ncv = ncv;
208
    }
1272 slepc 209
    svd->setupcalled = 0;
210
  }
211
  PetscFunctionReturn(0);
212
}
213
 
214
#undef __FUNCT__  
215
#define __FUNCT__ "SVDGetDimensions"
216
/*@
217
   SVDGetDimensions - Gets the number of singular values to compute
218
   and the dimension of the subspace.
219
 
220
   Not Collective
221
 
222
   Input Parameter:
223
.  svd - the singular value context
224
 
225
   Output Parameters:
226
+  nsv - number of singular values to compute
227
-  ncv - the maximum dimension of the subspace to be used by the solver
228
 
229
   Notes:
230
   The user can specify PETSC_NULL for any parameter that is not needed.
231
 
232
   Level: intermediate
233
 
234
.seealso: SVDSetDimensions()
235
@*/
236
PetscErrorCode SVDGetDimensions(SVD svd,int *nsv,int *ncv)
237
{
238
  PetscFunctionBegin;
239
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
240
  if (nsv) *nsv = svd->nsv;
241
  if (ncv) *ncv = svd->ncv;
242
  PetscFunctionReturn(0);
243
}
244
 
245
#undef __FUNCT__  
246
#define __FUNCT__ "SVDSetWhichSingularTriplets"
247
/*@
248
    SVDSetWhichSingularTriplets - Specifies which singular triplets are
249
    to be sought.
250
 
251
    Collective on SVD
252
 
253
    Input Parameter:
254
.   svd - singular value solver context obtained from SVDCreate()
255
 
256
    Output Parameter:
257
.   which - which singular triplets are to be sought
258
 
259
    Possible values:
260
    The parameter 'which' can have one of these values:
261
 
262
+     SVD_LARGEST  - largest singular values
263
-     SVD_SMALLEST - smallest singular values
264
 
265
    Options Database Keys:
1277 slepc 266
+   -svd_largest  - Sets largest singular values
267
-   -svd_smallest - Sets smallest singular values
1272 slepc 268
 
269
    Level: intermediate
270
 
271
.seealso: SVDGetWhichSingularTriplets()
272
@*/
273
PetscErrorCode SVDSetWhichSingularTriplets(SVD svd,SVDWhich which)
274
{
275
  PetscFunctionBegin;
276
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
277
  switch (which) {
278
    case SVD_LARGEST:
279
    case SVD_SMALLEST:
280
      svd->which = which;
281
      break;
282
  default:
283
    SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Invalid 'which' parameter");    
284
  }
285
  PetscFunctionReturn(0);
286
}
287
 
288
#undef __FUNCT__  
289
#define __FUNCT__ "SVDGetWhichSingularTriplets"
290
/*@C
291
    SVDGetWhichSingularTriplet - Returns which singular triplets are
292
    to be sought.
293
 
294
    Not Collective
295
 
296
    Input Parameter:
297
.   svd - singular value solver context obtained from SVDCreate()
298
 
299
    Output Parameter:
300
.   which - which singular triplets are to be sought
301
 
302
    Notes:
303
    See SVDSetWhichSingularTriplets() for possible values of which
304
 
305
    Level: intermediate
306
 
307
.seealso: SVDSetWhichSingularTriplets()
308
@*/
309
PetscErrorCode SVDGetWhichSingularTriplets(SVD svd,SVDWhich *which)
310
{
311
  PetscFunctionBegin;
312
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
313
  PetscValidPointer(which,2);
314
  *which = svd->which;
315
  PetscFunctionReturn(0);
316
}
317
 
318
#undef __FUNCT__  
319
#define __FUNCT__ "SVDSetFromOptions"
320
/*@
321
   SVDSetFromOptions - Sets SVD options from the options database.
322
   This routine must be called before SVDSetUp() if the user is to be
323
   allowed to set the solver type.
324
 
325
   Collective on SVD
326
 
327
   Input Parameters:
328
.  svd - the singular value solver context
329
 
330
   Notes:  
331
   To see all options, run your program with the -help option.
332
 
333
   Level: beginner
334
 
335
.seealso:
336
@*/
337
PetscErrorCode SVDSetFromOptions(SVD svd)
338
{
339
  PetscErrorCode ierr;
1291 slepc 340
  char           type[256],monfilename[PETSC_MAX_PATH_LEN];
1283 slepc 341
  PetscTruth     flg;
342
  const char     *mode_list[2] = { "explicit", "implicit" };
1272 slepc 343
  PetscInt       i,j;
344
  PetscReal      r;
1288 slepc 345
  PetscViewer    monviewer;
1272 slepc 346
 
347
  PetscFunctionBegin;
348
  PetscValidHeaderSpecific(svd,SVD_COOKIE,1);
349
  svd->setupcalled = 0;
350
  ierr = PetscOptionsBegin(svd->comm,svd->prefix,"Singular Value Solver (SVD) Options","SVD");CHKERRQ(ierr);
351
 
352
  ierr = PetscOptionsList("-svd_type","Singular Value Solver method","SVDSetType",SVDList,(char*)(svd->type_name?svd->type_name:SVDEIGENSOLVER),type,256,&flg);CHKERRQ(ierr);
353
  if (flg) {
354
    ierr = SVDSetType(svd,type);CHKERRQ(ierr);
355
  } else if (!svd->type_name) {
356
    ierr = SVDSetType(svd,SVDEIGENSOLVER);CHKERRQ(ierr);
357
  }
358
 
359
  ierr = PetscOptionsName("-svd_view","Print detailed information on solver used","SVDView",0);CHKERRQ(ierr);
360
 
1283 slepc 361
  ierr = PetscOptionsEList("-svd_transpose_mode","Transpose SVD mode","SVDSetTransposeMode",mode_list,2,svd->transmode == PETSC_DECIDE ? "decide" : mode_list[svd->transmode],&i,&flg);CHKERRQ(ierr);
1272 slepc 362
  if (flg) {
363
    ierr = SVDSetTransposeMode(svd,(SVDTransposeMode)i);CHKERRQ(ierr);
364
  }  
365
 
366
  r = i = PETSC_IGNORE;
1283 slepc 367
  ierr = PetscOptionsInt("-svd_max_it","Maximum number of iterations","SVDSetTolerances",svd->max_it,&i,PETSC_NULL);CHKERRQ(ierr);
368
  ierr = PetscOptionsReal("-svd_tol","Tolerance","SVDSetTolerances",svd->tol,&r,PETSC_NULL);CHKERRQ(ierr);
369
  ierr = SVDSetTolerances(svd,r,i);CHKERRQ(ierr);
1272 slepc 370
 
371
  i = j = PETSC_IGNORE;
1283 slepc 372
  ierr = PetscOptionsInt("-svd_nsv","Number of singular values to compute","SVDSetDimensions",svd->ncv,&i,PETSC_NULL);CHKERRQ(ierr);
373
  ierr = PetscOptionsInt("-svd_ncv","Number of basis vectors","SVDSetDimensions",svd->ncv,&j,PETSC_NULL);CHKERRQ(ierr);
374
  ierr = SVDSetDimensions(svd,i,j);CHKERRQ(ierr);
1272 slepc 375
 
1277 slepc 376
  ierr = PetscOptionsTruthGroupBegin("-svd_largest","compute largest singular values","SVDSetWhichSingularTriplets",&flg);CHKERRQ(ierr);
377
  if (flg) { ierr = SVDSetWhichSingularTriplets(svd,SVD_LARGEST);CHKERRQ(ierr); }
378
  ierr = PetscOptionsTruthGroupEnd("-svd_smallest","compute smallest singular values","SVDSetWhichSingularTriplets",&flg);CHKERRQ(ierr);
379
  if (flg) { ierr = SVDSetWhichSingularTriplets(svd,SVD_SMALLEST);CHKERRQ(ierr); }
380
 
1288 slepc 381
  ierr = PetscOptionsName("-svd_cancelmonitors","Remove any hardwired monitor routines","SVDClearMonitor",&flg);CHKERRQ(ierr);
382
  if (flg) {
1291 slepc 383
    ierr = SVDClearMonitor(svd);CHKERRQ(ierr);
1288 slepc 384
  }
385
 
386
  ierr = PetscOptionsString("-svd_monitor","Monitor approximate singular values and error estimates","SVDSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
387
  if (flg) {
388
    ierr = PetscViewerASCIIOpen(svd->comm,monfilename,&monviewer);CHKERRQ(ierr);
389
    ierr = SVDSetMonitor(svd,SVDDefaultMonitor,monviewer,(PetscErrorCode (*)(void*))PetscViewerDestroy);CHKERRQ(ierr);
390
  }
391
  ierr = PetscOptionsName("-svd_xmonitor","Monitor error estimates graphically","SVDSetMonitor",&flg);CHKERRQ(ierr);
392
  if (flg) {
393
    ierr = SVDSetMonitor(svd,SVDLGMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
394
  }
395
 
1272 slepc 396
  ierr = PetscOptionsEnd();CHKERRQ(ierr);
1302 slepc 397
  ierr = IPSetFromOptions(svd->ip);CHKERRQ(ierr);
1272 slepc 398
  if (svd->ops->setfromoptions) {
399
    ierr = (*svd->ops->setfromoptions)(svd);CHKERRQ(ierr);
400
  }
401
  PetscFunctionReturn(0);
402
}