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