Subversion Repositories slepc-dev

Rev

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

Rev Author Line No. Line
6 dsic.upv.es!jroman 1
/*
2
    The ST (spectral transformation) interface routines, callable by users.
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
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
6 dsic.upv.es!jroman 22
*/
23
 
2283 jroman 24
#include <private/stimpl.h>            /*I "slepcst.h" I*/
6 dsic.upv.es!jroman 25
 
26
#undef __FUNCT__  
27
#define __FUNCT__ "STApply"
28
/*@
29
   STApply - Applies the spectral transformation operator to a vector, for
30
   instance (A - sB)^-1 B in the case of the shift-and-invert tranformation
31
   and generalized eigenproblem.
32
 
33
   Collective on ST and Vec
34
 
35
   Input Parameters:
36
+  st - the spectral transformation context
37
-  x  - input vector
38
 
39
   Output Parameter:
40
.  y - output vector
41
 
42
   Level: developer
43
 
1358 slepc 44
.seealso: STApplyTranspose()
6 dsic.upv.es!jroman 45
@*/
476 dsic.upv.es!antodo 46
PetscErrorCode STApply(ST st,Vec x,Vec y)
6 dsic.upv.es!jroman 47
{
476 dsic.upv.es!antodo 48
  PetscErrorCode ierr;
6 dsic.upv.es!jroman 49
 
50
  PetscFunctionBegin;
2213 jroman 51
  PetscValidHeaderSpecific(st,ST_CLASSID,1);
52
  PetscValidHeaderSpecific(x,VEC_CLASSID,2);
53
  PetscValidHeaderSpecific(y,VEC_CLASSID,3);
2214 jroman 54
  if (x == y) SETERRQ(((PetscObject)st)->comm,PETSC_ERR_ARG_IDN,"x and y must be different vectors");
6 dsic.upv.es!jroman 55
 
2330 jroman 56
  if (!st->setupcalled) { ierr = STSetUp(st);CHKERRQ(ierr); }
6 dsic.upv.es!jroman 57
 
2318 jroman 58
  if (!st->ops->apply) SETERRQ(((PetscObject)st)->comm,PETSC_ERR_SUP,"ST does not have apply");
6 dsic.upv.es!jroman 59
  ierr = PetscLogEventBegin(ST_Apply,st,x,y,0);CHKERRQ(ierr);
1209 slepc 60
  st->applys++;
1804 jroman 61
  if (st->D) { /* with balancing */
62
    ierr = VecPointwiseDivide(st->wb,x,st->D);CHKERRQ(ierr);
63
    ierr = (*st->ops->apply)(st,st->wb,y);CHKERRQ(ierr);
64
    ierr = VecPointwiseMult(y,y,st->D);CHKERRQ(ierr);
65
  }
66
  else {
67
    ierr = (*st->ops->apply)(st,x,y);CHKERRQ(ierr);
68
  }
6 dsic.upv.es!jroman 69
  ierr = PetscLogEventEnd(ST_Apply,st,x,y,0);CHKERRQ(ierr);
70
  PetscFunctionReturn(0);
71
}
72
 
73
#undef __FUNCT__  
1358 slepc 74
#define __FUNCT__ "STGetBilinearForm"
6 dsic.upv.es!jroman 75
/*@
1804 jroman 76
   STGetBilinearForm - Returns the matrix used in the bilinear form with a
77
   generalized problem with semi-definite B.
6 dsic.upv.es!jroman 78
 
2328 jroman 79
   Not collective, though a parallel Mat may be returned
6 dsic.upv.es!jroman 80
 
81
   Input Parameters:
1358 slepc 82
.  st - the spectral transformation context
6 dsic.upv.es!jroman 83
 
84
   Output Parameter:
1358 slepc 85
.  B - output matrix
6 dsic.upv.es!jroman 86
 
2328 jroman 87
   Notes:
88
   The output matrix B must be destroyed after use. It will be PETSC_NULL in
89
   case of standard eigenproblems.
1449 slepc 90
 
6 dsic.upv.es!jroman 91
   Level: developer
92
@*/
1358 slepc 93
PetscErrorCode STGetBilinearForm(ST st,Mat *B)
6 dsic.upv.es!jroman 94
{
476 dsic.upv.es!antodo 95
  PetscErrorCode ierr;
6 dsic.upv.es!jroman 96
 
97
  PetscFunctionBegin;
2213 jroman 98
  PetscValidHeaderSpecific(st,ST_CLASSID,1);
1358 slepc 99
  PetscValidPointer(B,2);
100
  ierr = (*st->ops->getbilinearform)(st,B);CHKERRQ(ierr);
6 dsic.upv.es!jroman 101
  PetscFunctionReturn(0);
102
}
103
 
104
#undef __FUNCT__  
1358 slepc 105
#define __FUNCT__ "STGetBilinearForm_Default"
106
PetscErrorCode STGetBilinearForm_Default(ST st,Mat *B)
6 dsic.upv.es!jroman 107
{
1450 slepc 108
  PetscErrorCode ierr;
109
 
6 dsic.upv.es!jroman 110
  PetscFunctionBegin;
1358 slepc 111
  *B = st->B;
1449 slepc 112
  if (*B) {
113
    ierr =  PetscObjectReference((PetscObject)*B);CHKERRQ(ierr);
114
  }
6 dsic.upv.es!jroman 115
  PetscFunctionReturn(0);
116
}
117
 
118
#undef __FUNCT__  
780 dsic.upv.es!jroman 119
#define __FUNCT__ "STApplyTranspose"
120
/*@
121
   STApplyTranspose - Applies the transpose of the operator to a vector, for
122
   instance B^T(A - sB)^-T in the case of the shift-and-invert tranformation
123
   and generalized eigenproblem.
124
 
125
   Collective on ST and Vec
126
 
127
   Input Parameters:
128
+  st - the spectral transformation context
129
-  x  - input vector
130
 
131
   Output Parameter:
132
.  y - output vector
133
 
134
   Level: developer
135
 
136
.seealso: STApply()
137
@*/
138
PetscErrorCode STApplyTranspose(ST st,Vec x,Vec y)
139
{
140
  PetscErrorCode ierr;
141
 
142
  PetscFunctionBegin;
2213 jroman 143
  PetscValidHeaderSpecific(st,ST_CLASSID,1);
144
  PetscValidHeaderSpecific(x,VEC_CLASSID,2);
145
  PetscValidHeaderSpecific(y,VEC_CLASSID,3);
2214 jroman 146
  if (x == y) SETERRQ(((PetscObject)st)->comm,PETSC_ERR_ARG_IDN,"x and y must be different vectors");
780 dsic.upv.es!jroman 147
 
2330 jroman 148
  if (!st->setupcalled) { ierr = STSetUp(st);CHKERRQ(ierr); }
780 dsic.upv.es!jroman 149
 
2318 jroman 150
  if (!st->ops->applytrans) SETERRQ(((PetscObject)st)->comm,PETSC_ERR_SUP,"ST does not have applytrans");
780 dsic.upv.es!jroman 151
  ierr = PetscLogEventBegin(ST_ApplyTranspose,st,x,y,0);CHKERRQ(ierr);
1802 jroman 152
  st->applys++;
1804 jroman 153
  if (st->D) { /* with balancing */
154
    ierr = VecPointwiseMult(st->wb,x,st->D);CHKERRQ(ierr);
155
    ierr = (*st->ops->applytrans)(st,st->wb,y);CHKERRQ(ierr);
156
    ierr = VecPointwiseDivide(y,y,st->D);CHKERRQ(ierr);
157
  }
158
  else {
159
    ierr = (*st->ops->applytrans)(st,x,y);CHKERRQ(ierr);
160
  }
780 dsic.upv.es!jroman 161
  ierr = PetscLogEventEnd(ST_ApplyTranspose,st,x,y,0);CHKERRQ(ierr);
162
  PetscFunctionReturn(0);
163
}
164
 
165
#undef __FUNCT__  
677 dsic.upv.es!antodo 166
#define __FUNCT__ "STComputeExplicitOperator"
167
/*@
2317 jroman 168
   STComputeExplicitOperator - Computes the explicit operator associated
169
   to the eigenvalue problem with the specified spectral transformation.  
677 dsic.upv.es!antodo 170
 
2317 jroman 171
   Collective on ST
677 dsic.upv.es!antodo 172
 
2317 jroman 173
   Input Parameter:
174
.  st - the spectral transform context
677 dsic.upv.es!antodo 175
 
2317 jroman 176
   Output Parameter:
177
.  mat - the explicit operator
677 dsic.upv.es!antodo 178
 
2317 jroman 179
   Notes:
180
   This routine builds a matrix containing the explicit operator. For
181
   example, in generalized problems with shift-and-invert spectral
182
   transformation the result would be matrix (A - s B)^-1 B.
677 dsic.upv.es!antodo 183
 
2317 jroman 184
   This computation is done by applying the operator to columns of the
185
   identity matrix. Note that the result is a dense matrix.
677 dsic.upv.es!antodo 186
 
2317 jroman 187
   Level: advanced
188
 
677 dsic.upv.es!antodo 189
.seealso: STApply()  
190
@*/
191
PetscErrorCode STComputeExplicitOperator(ST st,Mat *mat)
192
{
2334 jroman 193
  PetscErrorCode    ierr;
194
  Vec               in,out;
195
  PetscInt          i,M,m,*rows,start,end;
196
  const PetscScalar *array;
197
  PetscScalar       one = 1.0;
677 dsic.upv.es!antodo 198
 
199
  PetscFunctionBegin;
2213 jroman 200
  PetscValidHeaderSpecific(st,ST_CLASSID,1);
677 dsic.upv.es!antodo 201
  PetscValidPointer(mat,2);
202
 
203
  ierr = MatGetVecs(st->A,&in,&out);CHKERRQ(ierr);
204
  ierr = VecGetSize(out,&M);CHKERRQ(ierr);
205
  ierr = VecGetLocalSize(out,&m);CHKERRQ(ierr);
206
  ierr = VecGetOwnershipRange(out,&start,&end);CHKERRQ(ierr);
1508 slepc 207
  ierr = PetscMalloc(m*sizeof(PetscInt),&rows);CHKERRQ(ierr);
677 dsic.upv.es!antodo 208
  for (i=0; i<m; i++) rows[i] = start + i;
209
 
1422 slepc 210
  ierr = MatCreateMPIDense(((PetscObject)st)->comm,m,m,M,M,PETSC_NULL,mat);CHKERRQ(ierr);
677 dsic.upv.es!antodo 211
 
212
  for (i=0; i<M; i++) {
828 dsic.upv.es!antodo 213
    ierr = VecSet(in,0.0);CHKERRQ(ierr);
677 dsic.upv.es!antodo 214
    ierr = VecSetValues(in,1,&i,&one,INSERT_VALUES);CHKERRQ(ierr);
215
    ierr = VecAssemblyBegin(in);CHKERRQ(ierr);
216
    ierr = VecAssemblyEnd(in);CHKERRQ(ierr);
217
 
2330 jroman 218
    ierr = STApply(st,in,out);CHKERRQ(ierr);
677 dsic.upv.es!antodo 219
 
2334 jroman 220
    ierr = VecGetArrayRead(out,&array);CHKERRQ(ierr);
677 dsic.upv.es!antodo 221
    ierr = MatSetValues(*mat,m,rows,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
2334 jroman 222
    ierr = VecRestoreArrayRead(out,&array);CHKERRQ(ierr);
677 dsic.upv.es!antodo 223
  }
224
  ierr = PetscFree(rows);CHKERRQ(ierr);
2305 jroman 225
  ierr = VecDestroy(&in);CHKERRQ(ierr);
226
  ierr = VecDestroy(&out);CHKERRQ(ierr);
677 dsic.upv.es!antodo 227
  ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
228
  ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
229
  PetscFunctionReturn(0);
230
}
231
 
232
#undef __FUNCT__  
6 dsic.upv.es!jroman 233
#define __FUNCT__ "STSetUp"
234
/*@
235
   STSetUp - Prepares for the use of a spectral transformation.
236
 
237
   Collective on ST
238
 
239
   Input Parameter:
240
.  st - the spectral transformation context
241
 
242
   Level: advanced
243
 
244
.seealso: STCreate(), STApply(), STDestroy()
245
@*/
476 dsic.upv.es!antodo 246
PetscErrorCode STSetUp(ST st)
6 dsic.upv.es!jroman 247
{
2348 jroman 248
  PetscInt       n,k;
476 dsic.upv.es!antodo 249
  PetscErrorCode ierr;
6 dsic.upv.es!jroman 250
 
251
  PetscFunctionBegin;
2213 jroman 252
  PetscValidHeaderSpecific(st,ST_CLASSID,1);
2348 jroman 253
  if (!st->A) {SETERRQ(((PetscObject)st)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be set first");}
254
  if (st->setupcalled) PetscFunctionReturn(0);
1038 slepc 255
  PetscInfo(st,"Setting up new ST\n");
6 dsic.upv.es!jroman 256
  ierr = PetscLogEventBegin(ST_SetUp,st,0,0,0);CHKERRQ(ierr);
1422 slepc 257
  if (!((PetscObject)st)->type_name) {
106 dsic.upv.es!antodo 258
    ierr = STSetType(st,STSHIFT);CHKERRQ(ierr);
6 dsic.upv.es!jroman 259
  }
2305 jroman 260
  ierr = VecDestroy(&st->w);CHKERRQ(ierr);
1358 slepc 261
  ierr = MatGetVecs(st->A,&st->w,PETSC_NULL);CHKERRQ(ierr);
2348 jroman 262
  if (st->D) {
263
    ierr = MatGetLocalSize(st->A,PETSC_NULL,&n);CHKERRQ(ierr);
264
    ierr = VecGetLocalSize(st->D,&k);CHKERRQ(ierr);
265
    if (n != k) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Balance matrix has wrong dimension %D (should be %D)",k,n);
266
    ierr = VecDestroy(&st->wb);CHKERRQ(ierr);
267
    ierr = VecDuplicate(st->D,&st->wb);CHKERRQ(ierr);
6 dsic.upv.es!jroman 268
  }
2348 jroman 269
  if (st->ops->setup) { ierr = (*st->ops->setup)(st);CHKERRQ(ierr); }
6 dsic.upv.es!jroman 270
  st->setupcalled = 1;
271
  ierr = PetscLogEventEnd(ST_SetUp,st,0,0,0);CHKERRQ(ierr);
272
  PetscFunctionReturn(0);
273
}
274
 
275
#undef __FUNCT__  
276
#define __FUNCT__ "STPostSolve"
1029 slepc 277
/*@
6 dsic.upv.es!jroman 278
   STPostSolve - Optional post-solve phase, intended for any actions that must
279
   be performed on the ST object after the eigensolver has finished.
280
 
281
   Collective on ST
282
 
283
   Input Parameters:
1029 slepc 284
.  st  - the spectral transformation context
6 dsic.upv.es!jroman 285
 
1029 slepc 286
   Level: developer
6 dsic.upv.es!jroman 287
 
1029 slepc 288
.seealso: EPSSolve()
289
@*/
290
PetscErrorCode STPostSolve(ST st)
6 dsic.upv.es!jroman 291
{
476 dsic.upv.es!antodo 292
  PetscErrorCode ierr;
6 dsic.upv.es!jroman 293
 
294
  PetscFunctionBegin;
2213 jroman 295
  PetscValidHeaderSpecific(st,ST_CLASSID,1);
6 dsic.upv.es!jroman 296
  if (st->ops->postsolve) {
297
    ierr = (*st->ops->postsolve)(st);CHKERRQ(ierr);
298
  }
299
  PetscFunctionReturn(0);
300
}
301
 
302
#undef __FUNCT__  
303
#define __FUNCT__ "STBackTransform"
531 dsic.upv.es!jroman 304
/*@
305
   STBackTransform - Back-transformation phase, intended for
306
   spectral transformations which require to transform the computed
6 dsic.upv.es!jroman 307
   eigenvalues back to the original eigenvalue problem.
308
 
2328 jroman 309
   Not Collective
6 dsic.upv.es!jroman 310
 
311
   Input Parameters:
312
   st   - the spectral transformation context
313
   eigr - real part of a computed eigenvalue
314
   eigi - imaginary part of a computed eigenvalue
315
 
316
   Level: developer
540 dsic.upv.es!jroman 317
@*/
1779 antodo 318
PetscErrorCode STBackTransform(ST st,PetscInt n,PetscScalar* eigr,PetscScalar* eigi)
6 dsic.upv.es!jroman 319
{
476 dsic.upv.es!antodo 320
  PetscErrorCode ierr;
6 dsic.upv.es!jroman 321
 
322
  PetscFunctionBegin;
2213 jroman 323
  PetscValidHeaderSpecific(st,ST_CLASSID,1);
6 dsic.upv.es!jroman 324
  if (st->ops->backtr) {
1778 antodo 325
    ierr = (*st->ops->backtr)(st,n,eigr,eigi);CHKERRQ(ierr);
6 dsic.upv.es!jroman 326
  }
327
  PetscFunctionReturn(0);
328
}