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