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 related to the
18 dsic.upv.es!jroman 3
    KSP object associated to it.
1376 slepc 4
 
5
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
6
      SLEPc - Scalable Library for Eigenvalue Problem Computations
7
      Copyright (c) 2002-2007, Universidad Politecnica de Valencia, Spain
8
 
9
      This file is part of SLEPc. See the README file for conditions of use
10
      and additional information.
11
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
6 dsic.upv.es!jroman 12
*/
13
 
14
#include "src/st/stimpl.h"            /*I "slepcst.h" I*/
15
 
16
#undef __FUNCT__  
18 dsic.upv.es!jroman 17
#define __FUNCT__ "STAssociatedKSPSolve"
313 dsic.upv.es!jroman 18
/*
172 dsic.upv.es!jroman 19
   STAssociatedKSPSolve - Solves the linear system of equations associated
6 dsic.upv.es!jroman 20
   to the spectral transformation.
21
 
22
   Input Parameters:
23
.  st - the spectral transformation context
24
.  b  - right hand side vector
25
 
26
   Output  Parameter:
27
.  x - computed solution
313 dsic.upv.es!jroman 28
*/
476 dsic.upv.es!antodo 29
PetscErrorCode STAssociatedKSPSolve(ST st,Vec b,Vec x)
6 dsic.upv.es!jroman 30
{
476 dsic.upv.es!antodo 31
  PetscErrorCode ierr;
982 slepc 32
  PetscInt       its;
6 dsic.upv.es!jroman 33
  KSPConvergedReason reason;
34
 
35
  PetscFunctionBegin;
25 dsic.upv.es!jroman 36
  PetscValidHeaderSpecific(st,ST_COOKIE,1);
37
  PetscValidHeaderSpecific(b,VEC_COOKIE,2);
38
  PetscValidHeaderSpecific(x,VEC_COOKIE,3);
18 dsic.upv.es!jroman 39
  if (!st->ksp) { SETERRQ(PETSC_ERR_SUP,"ST has no associated KSP"); }
239 dsic.upv.es!antodo 40
  ierr = KSPSolve(st->ksp,b,x);CHKERRQ(ierr);
18 dsic.upv.es!jroman 41
  ierr = KSPGetConvergedReason(st->ksp,&reason);CHKERRQ(ierr);
42
  if (reason<0) { SETERRQ1(0,"Warning: KSP did not converge (%d)",reason); }
163 dsic.upv.es!antodo 43
  ierr = KSPGetIterationNumber(st->ksp,&its);CHKERRQ(ierr);  
44
  st->lineariterations += its;
1038 slepc 45
  PetscInfo1(st,"Linear solve iterations=%d\n",its);
6 dsic.upv.es!jroman 46
  PetscFunctionReturn(0);
47
}
48
 
49
#undef __FUNCT__  
780 dsic.upv.es!jroman 50
#define __FUNCT__ "STAssociatedKSPSolveTranspose"
51
/*
52
   STAssociatedKSPSolveTranspose - Solves the transpose of the linear
53
   system of equations associated to the spectral transformation.
54
 
55
   Input Parameters:
56
.  st - the spectral transformation context
57
.  b  - right hand side vector
58
 
59
   Output  Parameter:
60
.  x - computed solution
61
*/
62
PetscErrorCode STAssociatedKSPSolveTranspose(ST st,Vec b,Vec x)
63
{
64
  PetscErrorCode ierr;
982 slepc 65
  PetscInt       its;
780 dsic.upv.es!jroman 66
  KSPConvergedReason reason;
67
 
68
  PetscFunctionBegin;
69
  PetscValidHeaderSpecific(st,ST_COOKIE,1);
70
  PetscValidHeaderSpecific(b,VEC_COOKIE,2);
71
  PetscValidHeaderSpecific(x,VEC_COOKIE,3);
72
  if (!st->ksp) { SETERRQ(PETSC_ERR_SUP,"ST has no associated KSP"); }
73
  ierr = KSPSolveTranspose(st->ksp,b,x);CHKERRQ(ierr);
74
  ierr = KSPGetConvergedReason(st->ksp,&reason);CHKERRQ(ierr);
75
  if (reason<0) { SETERRQ1(0,"Warning: KSP did not converge (%d)",reason); }
76
  ierr = KSPGetIterationNumber(st->ksp,&its);CHKERRQ(ierr);  
77
  st->lineariterations += its;
1038 slepc 78
  PetscInfo1(st,"Linear solve iterations=%d\n",its);
780 dsic.upv.es!jroman 79
  PetscFunctionReturn(0);
80
}
81
 
82
#undef __FUNCT__  
18 dsic.upv.es!jroman 83
#define __FUNCT__ "STSetKSP"
6 dsic.upv.es!jroman 84
/*@
18 dsic.upv.es!jroman 85
   STSetKSP - Sets the KSP object associated with the spectral
6 dsic.upv.es!jroman 86
   transformation.
87
 
88
   Not collective
89
 
90
   Input Parameters:
91
+  st   - the spectral transformation context
18 dsic.upv.es!jroman 92
-  ksp  - the linear system context
6 dsic.upv.es!jroman 93
 
94
   Level: advanced
95
 
96
@*/
476 dsic.upv.es!antodo 97
PetscErrorCode STSetKSP(ST st,KSP ksp)
6 dsic.upv.es!jroman 98
{
476 dsic.upv.es!antodo 99
  PetscErrorCode ierr;
100
 
6 dsic.upv.es!jroman 101
  PetscFunctionBegin;
25 dsic.upv.es!jroman 102
  PetscValidHeaderSpecific(st,ST_COOKIE,1);
103
  PetscValidHeaderSpecific(ksp,KSP_COOKIE,2);
104
  PetscCheckSameComm(st,1,ksp,2);
1295 slepc 105
  ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
246 dsic.upv.es!antodo 106
  if (st->ksp) {
107
    ierr = KSPDestroy(st->ksp);CHKERRQ(ierr);
108
  }
18 dsic.upv.es!jroman 109
  st->ksp = ksp;
6 dsic.upv.es!jroman 110
  PetscFunctionReturn(0);
111
}
112
 
113
#undef __FUNCT__  
18 dsic.upv.es!jroman 114
#define __FUNCT__ "STGetKSP"
6 dsic.upv.es!jroman 115
/*@
18 dsic.upv.es!jroman 116
   STGetKSP - Gets the KSP object associated with the spectral
6 dsic.upv.es!jroman 117
   transformation.
118
 
119
   Not collective
120
 
121
   Input Parameter:
122
.  st - the spectral transformation context
123
 
124
   Output Parameter:
18 dsic.upv.es!jroman 125
.  ksp  - the linear system context
6 dsic.upv.es!jroman 126
 
127
   Notes:
18 dsic.upv.es!jroman 128
   On output, the value of ksp can be PETSC_NULL if the combination of
6 dsic.upv.es!jroman 129
   eigenproblem type and selected transformation does not require to
130
   solve a linear system of equations.
131
 
132
   Level: intermediate
133
 
134
@*/
476 dsic.upv.es!antodo 135
PetscErrorCode STGetKSP(ST st,KSP* ksp)
6 dsic.upv.es!jroman 136
{
137
  PetscFunctionBegin;
25 dsic.upv.es!jroman 138
  PetscValidHeaderSpecific(st,ST_COOKIE,1);
6 dsic.upv.es!jroman 139
  if (!st->type_name) { SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call STSetType first"); }
18 dsic.upv.es!jroman 140
  if (ksp)  *ksp = st->ksp;
6 dsic.upv.es!jroman 141
  PetscFunctionReturn(0);
142
}
143
 
163 dsic.upv.es!antodo 144
#undef __FUNCT__  
1209 slepc 145
#define __FUNCT__ "STGetOperationCounters"
163 dsic.upv.es!antodo 146
/*@
1345 slepc 147
   STGetOperationCounters - Gets the total number of operator applications
148
   and linear solver iterations used by the ST object.
6 dsic.upv.es!jroman 149
 
163 dsic.upv.es!antodo 150
   Not Collective
151
 
152
   Input Parameter:
172 dsic.upv.es!jroman 153
.  st - the spectral transformation context
163 dsic.upv.es!antodo 154
 
155
   Output Parameter:
1209 slepc 156
+  ops  - number of operator applications
1345 slepc 157
-  lits - number of linear solver iterations
163 dsic.upv.es!antodo 158
 
1209 slepc 159
   Notes:
1214 slepc 160
   Any output parameter may be PETSC_NULL on input if not needed.
1209 slepc 161
 
163 dsic.upv.es!antodo 162
   Level: intermediate
163
 
1209 slepc 164
.seealso: STResetOperationCounters()
163 dsic.upv.es!antodo 165
@*/
1345 slepc 166
PetscErrorCode STGetOperationCounters(ST st,int* ops,int* lits)
163 dsic.upv.es!antodo 167
{
168
  PetscFunctionBegin;
169
  PetscValidHeaderSpecific(st,ST_COOKIE,1);
1209 slepc 170
  if (ops) *ops = st->applys;
171
  if (lits) *lits = st->lineariterations;
163 dsic.upv.es!antodo 172
  PetscFunctionReturn(0);
173
}
174
 
175
#undef __FUNCT__  
1209 slepc 176
#define __FUNCT__ "STResetOperationCounters"
163 dsic.upv.es!antodo 177
/*@
1214 slepc 178
   STResetOperationCounters - Resets the counters for operator applications,
179
   inner product operations and total number of linear iterations used by
180
   the ST object.
163 dsic.upv.es!antodo 181
 
182
   Collective on ST
183
 
184
   Input Parameter:
172 dsic.upv.es!jroman 185
.  st - the spectral transformation context
163 dsic.upv.es!antodo 186
 
187
   Level: intermediate
188
 
1209 slepc 189
.seealso: STGetOperationCounters()
163 dsic.upv.es!antodo 190
@*/
1209 slepc 191
PetscErrorCode STResetOperationCounters(ST st)
163 dsic.upv.es!antodo 192
{
193
  PetscFunctionBegin;
194
  PetscValidHeaderSpecific(st,ST_COOKIE,1);
195
  st->lineariterations = 0;
1209 slepc 196
  st->applys = 0;
163 dsic.upv.es!antodo 197
  PetscFunctionReturn(0);
198
}
199
 
310 dsic.upv.es!antodo 200
#undef __FUNCT__
201
#define __FUNCT__ "STCheckNullSpace_Default"
1020 slepc 202
PetscErrorCode STCheckNullSpace_Default(ST st,int n,const Vec V[])
310 dsic.upv.es!antodo 203
{
476 dsic.upv.es!antodo 204
  PetscErrorCode ierr;
205
  int            i,c;
206
  PetscReal      norm;
207
  Vec            *T,w;
208
  Mat            A;
209
  PC             pc;
210
  MatNullSpace   nullsp;
310 dsic.upv.es!antodo 211
 
212
  PetscFunctionBegin;
213
  ierr = PetscMalloc(n*sizeof(Vec),&T);CHKERRQ(ierr);
214
  ierr = KSPGetPC(st->ksp,&pc);CHKERRQ(ierr);
215
  ierr = PCGetOperators(pc,&A,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
216
  ierr = MatGetVecs(A,PETSC_NULL,&w);CHKERRQ(ierr);
217
  c = 0;
218
  for (i=0;i<n;i++) {
219
    ierr = MatMult(A,V[i],w);CHKERRQ(ierr);
220
    ierr = VecNorm(w,NORM_2,&norm);CHKERRQ(ierr);
221
    if (norm < 1e-8) {
1038 slepc 222
      PetscInfo2(st,"Vector %i norm=%g\n",i,norm);
310 dsic.upv.es!antodo 223
      T[c] = V[i];
224
      c++;
225
    }
226
  }
227
  ierr = VecDestroy(w);CHKERRQ(ierr);
228
  if (c>0) {
229
    ierr = MatNullSpaceCreate(st->comm,PETSC_FALSE,c,T,&nullsp);CHKERRQ(ierr);
230
    ierr = KSPSetNullSpace(st->ksp,nullsp);CHKERRQ(ierr);
231
    ierr = MatNullSpaceDestroy(nullsp);CHKERRQ(ierr);
232
  }
233
  ierr = PetscFree(T);CHKERRQ(ierr);
234
  PetscFunctionReturn(0);
235
}
163 dsic.upv.es!antodo 236
 
310 dsic.upv.es!antodo 237
#undef __FUNCT__
238
#define __FUNCT__ "STCheckNullSpace"
1028 slepc 239
/*@
1020 slepc 240
   STCheckNullSpace - Given a set of vectors, this function tests each of
313 dsic.upv.es!jroman 241
   them to be a nullspace vector of the coefficient matrix of the associated
242
   KSP object. All these nullspace vectors are passed to the KSP object.
243
 
244
   Collective on ST
245
 
246
   Input Parameters:
247
+  st - the spectral transformation context
248
.  n  - number of vectors
249
-  V  - vectors to be checked
250
 
251
   Note:
252
   This function allows to handle singular pencils and to solve some problems
253
   in which the nullspace is important (see the users guide for details).
254
 
255
   Level: developer
256
 
257
.seealso: EPSAttachDeflationSpace()
258
@*/
1020 slepc 259
PetscErrorCode STCheckNullSpace(ST st,int n,const Vec V[])
310 dsic.upv.es!antodo 260
{
476 dsic.upv.es!antodo 261
  PetscErrorCode ierr;
310 dsic.upv.es!antodo 262
 
263
  PetscFunctionBegin;
264
  if (n>0 && st->checknullspace) {
265
    ierr = (*st->checknullspace)(st,n,V);CHKERRQ(ierr);
266
  }
267
  PetscFunctionReturn(0);
268
}
269
 
270