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