Actual source code: sinvert.c

slepc-3.6.1 2015-09-03
Report Typos and Errors
  1: /*
  2:       Implements the shift-and-invert technique for eigenvalue problems.

  4:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  5:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  6:    Copyright (c) 2002-2015, Universitat Politecnica de Valencia, Spain

  8:    This file is part of SLEPc.

 10:    SLEPc is free software: you can redistribute it and/or modify it under  the
 11:    terms of version 3 of the GNU Lesser General Public License as published by
 12:    the Free Software Foundation.

 14:    SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY
 15:    WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS
 16:    FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for
 17:    more details.

 19:    You  should have received a copy of the GNU Lesser General  Public  License
 20:    along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
 21:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 22: */

 24: #include <slepc/private/stimpl.h>

 28: PetscErrorCode STApply_Sinvert(ST st,Vec x,Vec y)
 29: {

 33:   if (st->nmat>1) {
 34:     /* generalized eigenproblem: y = (A - sB)^-1 B x */
 35:     MatMult(st->T[0],x,st->w);
 36:     STMatSolve(st,st->w,y);
 37:   } else {
 38:     /* standard eigenproblem: y = (A - sI)^-1 x */
 39:     STMatSolve(st,x,y);
 40:   }
 41:   return(0);
 42: }

 46: PetscErrorCode STApplyTranspose_Sinvert(ST st,Vec x,Vec y)
 47: {

 51:   if (st->nmat>1) {
 52:     /* generalized eigenproblem: y = B^T (A - sB)^-T x */
 53:     STMatSolveTranspose(st,x,st->w);
 54:     MatMultTranspose(st->T[0],st->w,y);
 55:   } else {
 56:     /* standard eigenproblem: y = (A - sI)^-T x */
 57:     STMatSolveTranspose(st,x,y);
 58:   }
 59:   return(0);
 60: }

 64: PetscErrorCode STBackTransform_Sinvert(ST st,PetscInt n,PetscScalar *eigr,PetscScalar *eigi)
 65: {
 66:   PetscInt    j;
 67: #if !defined(PETSC_USE_COMPLEX)
 68:   PetscScalar t;
 69: #endif

 72: #if !defined(PETSC_USE_COMPLEX)
 73:   for (j=0;j<n;j++) {
 74:     if (eigi[j] == 0) eigr[j] = 1.0 / eigr[j] + st->sigma;
 75:     else {
 76:       t = eigr[j] * eigr[j] + eigi[j] * eigi[j];
 77:       eigr[j] = eigr[j] / t + st->sigma;
 78:       eigi[j] = - eigi[j] / t;
 79:     }
 80:   }
 81: #else
 82:   for (j=0;j<n;j++) {
 83:     eigr[j] = 1.0 / eigr[j] + st->sigma;
 84:   }
 85: #endif
 86:   return(0);
 87: }

 91: PetscErrorCode STPostSolve_Sinvert(ST st)
 92: {

 96:   if (st->shift_matrix == ST_MATMODE_INPLACE) {
 97:     if (st->nmat>1) {
 98:       MatAXPY(st->A[0],st->sigma,st->A[1],st->str);
 99:     } else {
100:       MatShift(st->A[0],st->sigma);
101:     }
102:     st->Astate[0] = ((PetscObject)st->A[0])->state;
103:     st->setupcalled = 0;
104:   }
105:   return(0);
106: }

110: PetscErrorCode STSetUp_Sinvert(ST st)
111: {
113:   PetscInt       k,nc,nmat=PetscMax(st->nmat,2);
114:   PetscScalar    *coeffs=NULL;

117:   /* if the user did not set the shift, use the target value */
118:   if (!st->sigma_set) st->sigma = st->defsigma;
119:   if (st->transform) {
120:     if (nmat>2) {
121:       nc = (nmat*(nmat+1))/2;
122:       PetscMalloc(nc*sizeof(PetscScalar),&coeffs);
123:       /* Compute coeffs */
124:       STCoeffs_Monomial(st,coeffs);
125:     }
126:     /* T[0] = A_n */
127:     k = nmat-1;
128:     PetscObjectReference((PetscObject)st->A[k]);
129:     st->T[0] = st->A[k];
130:     for (k=1;k<nmat;k++) {
131:       STMatMAXPY_Private(st,nmat>2?st->sigma:-st->sigma,0.0,nmat-k-1,coeffs?coeffs+(k*(k+1))/2:NULL,PETSC_TRUE,&st->T[k]);
132:     }
133:     if (nmat>2) { PetscFree(coeffs); }
134:     st->P = st->T[nmat-1];
135:     PetscObjectReference((PetscObject)st->P);
136:   } else {
137:     for (k=0;k<nmat;k++) {
138:       PetscObjectReference((PetscObject)st->A[k]);
139:       st->T[k] = st->A[k];
140:     }
141:   } 
142:   if (st->P) {
143:     if (!st->ksp) { STGetKSP(st,&st->ksp); }
144:     STCheckFactorPackage(st);
145:     KSPSetOperators(st->ksp,st->P,st->P);
146:     KSPSetErrorIfNotConverged(st->ksp,PETSC_TRUE);
147:     KSPSetUp(st->ksp);
148:   }
149:   return(0);
150: }

154: PetscErrorCode STSetShift_Sinvert(ST st,PetscScalar newshift)
155: {
157:   PetscInt       nmat=PetscMax(st->nmat,2),k,nc;
158:   PetscScalar    *coeffs=NULL;

161:   /* Nothing to be done if STSetUp has not been called yet */
162:   if (!st->setupcalled) return(0);
163:   if (st->transform) {
164:     if (st->shift_matrix == ST_MATMODE_COPY && nmat>2) {
165:       nc = (nmat*(nmat+1))/2;
166:       PetscMalloc(nc*sizeof(PetscScalar),&coeffs);
167:       /* Compute coeffs */
168:       STCoeffs_Monomial(st,coeffs);
169:     }
170:     for (k=1;k<nmat;k++) {
171:       STMatMAXPY_Private(st,nmat>2?newshift:-newshift,nmat>2?st->sigma:-st->sigma,nmat-k-1,coeffs?coeffs+(k*(k+1))/2:NULL,PETSC_FALSE,&st->T[k]);
172:     }
173:     if (st->shift_matrix == ST_MATMODE_COPY && nmat>2) {
174:       PetscFree(coeffs);
175:     }
176:     if (st->P!=st->T[nmat-1]) {
177:       MatDestroy(&st->P);
178:       st->P = st->T[nmat-1];
179:       PetscObjectReference((PetscObject)st->P);
180:     }
181:   }
182:   if (st->P) {
183:     if (!st->ksp) { STGetKSP(st,&st->ksp); }
184:     KSPSetOperators(st->ksp,st->P,st->P);
185:     KSPSetUp(st->ksp);
186:   }
187:   return(0);
188: }

192: PetscErrorCode STSetFromOptions_Sinvert(PetscOptions *PetscOptionsObject,ST st)
193: {
195:   PC             pc;
196:   PCType         pctype;
197:   KSPType        ksptype;

200:   if (!st->ksp) { STGetKSP(st,&st->ksp); }
201:   KSPGetPC(st->ksp,&pc);
202:   KSPGetType(st->ksp,&ksptype);
203:   PCGetType(pc,&pctype);
204:   if (!pctype && !ksptype) {
205:     if (st->shift_matrix == ST_MATMODE_SHELL) {
206:       /* in shell mode use GMRES with Jacobi as the default */
207:       KSPSetType(st->ksp,KSPGMRES);
208:       PCSetType(pc,PCJACOBI);
209:     } else {
210:       /* use direct solver as default */
211:       KSPSetType(st->ksp,KSPPREONLY);
212:       PCSetType(pc,PCLU);
213:     }
214:   }
215:   return(0);
216: }

220: PETSC_EXTERN PetscErrorCode STCreate_Sinvert(ST st)
221: {
223:   st->ops->apply           = STApply_Sinvert;
224:   st->ops->getbilinearform = STGetBilinearForm_Default;
225:   st->ops->applytrans      = STApplyTranspose_Sinvert;
226:   st->ops->postsolve       = STPostSolve_Sinvert;
227:   st->ops->backtransform   = STBackTransform_Sinvert;
228:   st->ops->setup           = STSetUp_Sinvert;
229:   st->ops->setshift        = STSetShift_Sinvert;
230:   st->ops->setfromoptions  = STSetFromOptions_Sinvert;
231:   st->ops->checknullspace  = STCheckNullSpace_Default;
232:   return(0);
233: }