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 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;
784 dsic.upv.es!antodo 38
  PetscLogInfo((st,"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;
784 dsic.upv.es!antodo 71
  PetscLogInfo((st,"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__  
138
#define __FUNCT__ "STGetNumberLinearIterations"
139
/*@
140
   STGetNumberLinearIterations - Gets the total number of linear iterations
141
   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:
149
.  lits - number of linear iterations
150
 
151
   Level: intermediate
152
 
153
.seealso: STResetNumberLinearIterations()
154
@*/
476 dsic.upv.es!antodo 155
PetscErrorCode STGetNumberLinearIterations(ST st,int* lits)
163 dsic.upv.es!antodo 156
{
157
  PetscFunctionBegin;
158
  PetscValidHeaderSpecific(st,ST_COOKIE,1);
159
  PetscValidIntPointer(lits,2);
160
  *lits = st->lineariterations;
161
  PetscFunctionReturn(0);
162
}
163
 
164
#undef __FUNCT__  
165
#define __FUNCT__ "STResetNumberLinearIterations"
166
/*@
167
   STResetNumberLinearIterations - Resets the counter for total number of
168
   linear iterations used by the ST object.
169
 
170
   Collective on ST
171
 
172
   Input Parameter:
172 dsic.upv.es!jroman 173
.  st - the spectral transformation context
163 dsic.upv.es!antodo 174
 
175
   Level: intermediate
176
 
177
.seealso: STGetNumberLinearIterations()
178
@*/
476 dsic.upv.es!antodo 179
PetscErrorCode STResetNumberLinearIterations(ST st)
163 dsic.upv.es!antodo 180
{
181
  PetscFunctionBegin;
182
  PetscValidHeaderSpecific(st,ST_COOKIE,1);
183
  st->lineariterations = 0;
184
  PetscFunctionReturn(0);
185
}
186
 
310 dsic.upv.es!antodo 187
#undef __FUNCT__
188
#define __FUNCT__ "STCheckNullSpace_Default"
476 dsic.upv.es!antodo 189
PetscErrorCode STCheckNullSpace_Default(ST st,int n,Vec* V)
310 dsic.upv.es!antodo 190
{
476 dsic.upv.es!antodo 191
  PetscErrorCode ierr;
192
  int            i,c;
193
  PetscReal      norm;
194
  Vec            *T,w;
195
  Mat            A;
196
  PC             pc;
197
  MatNullSpace   nullsp;
310 dsic.upv.es!antodo 198
 
199
  PetscFunctionBegin;
200
  ierr = PetscMalloc(n*sizeof(Vec),&T);CHKERRQ(ierr);
201
  ierr = KSPGetPC(st->ksp,&pc);CHKERRQ(ierr);
202
  ierr = PCGetOperators(pc,&A,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
203
  ierr = MatGetVecs(A,PETSC_NULL,&w);CHKERRQ(ierr);
204
  c = 0;
205
  for (i=0;i<n;i++) {
206
    ierr = MatMult(A,V[i],w);CHKERRQ(ierr);
207
    ierr = VecNorm(w,NORM_2,&norm);CHKERRQ(ierr);
208
    if (norm < 1e-8) {
784 dsic.upv.es!antodo 209
      PetscLogInfo((st,"STCheckNullSpace: vector %i norm=%g\n",i,norm));
310 dsic.upv.es!antodo 210
      T[c] = V[i];
211
      c++;
212
    }
213
  }
214
  ierr = VecDestroy(w);CHKERRQ(ierr);
215
  if (c>0) {
216
    ierr = MatNullSpaceCreate(st->comm,PETSC_FALSE,c,T,&nullsp);CHKERRQ(ierr);
217
    ierr = KSPSetNullSpace(st->ksp,nullsp);CHKERRQ(ierr);
218
    ierr = MatNullSpaceDestroy(nullsp);CHKERRQ(ierr);
219
  }
220
  ierr = PetscFree(T);CHKERRQ(ierr);
221
  PetscFunctionReturn(0);
222
}
163 dsic.upv.es!antodo 223
 
310 dsic.upv.es!antodo 224
#undef __FUNCT__
225
#define __FUNCT__ "STCheckNullSpace"
313 dsic.upv.es!jroman 226
/*@C
227
   STCheckNullSpace - Given a set of vectors, this function test each of
228
   them to be a nullspace vector of the coefficient matrix of the associated
229
   KSP object. All these nullspace vectors are passed to the KSP object.
230
 
231
   Collective on ST
232
 
233
   Input Parameters:
234
+  st - the spectral transformation context
235
.  n  - number of vectors
236
-  V  - vectors to be checked
237
 
238
   Note:
239
   This function allows to handle singular pencils and to solve some problems
240
   in which the nullspace is important (see the users guide for details).
241
 
242
   Level: developer
243
 
244
.seealso: EPSAttachDeflationSpace()
245
@*/
476 dsic.upv.es!antodo 246
PetscErrorCode STCheckNullSpace(ST st,int n,Vec* V)
310 dsic.upv.es!antodo 247
{
476 dsic.upv.es!antodo 248
  PetscErrorCode ierr;
310 dsic.upv.es!antodo 249
 
250
  PetscFunctionBegin;
251
  if (n>0 && st->checknullspace) {
252
    ierr = (*st->checknullspace)(st,n,V);CHKERRQ(ierr);
253
  }
254
  PetscFunctionReturn(0);
255
}
256
 
257