Actual source code: sinvert.c

slepc-3.7.1 2016-05-27
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-2016, 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->state = ST_STATE_INITIAL;
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:     MatDestroy(&st->T[0]);
130:     st->T[0] = st->A[k];
131:     for (k=1;k<nmat;k++) {
132:       STMatMAXPY_Private(st,nmat>2?st->sigma:-st->sigma,0.0,nmat-k-1,coeffs?coeffs+(k*(k+1))/2:NULL,PetscNot(st->state==ST_STATE_UPDATED),&st->T[k]);
133:     }
134:     if (nmat>2) { PetscFree(coeffs); }
135:     PetscObjectReference((PetscObject)st->T[nmat-1]);
136:     MatDestroy(&st->P);
137:     st->P = st->T[nmat-1];
138:   } else {
139:     for (k=0;k<nmat;k++) {
140:       PetscObjectReference((PetscObject)st->A[k]);
141:       MatDestroy(&st->T[k]);
142:       st->T[k] = st->A[k];
143:     }
144:   } 
145:   if (st->P) {
146:     if (!st->ksp) { STGetKSP(st,&st->ksp); }
147:     STCheckFactorPackage(st);
148:     KSPSetOperators(st->ksp,st->P,st->P);
149:     KSPSetErrorIfNotConverged(st->ksp,PETSC_TRUE);
150:     KSPSetUp(st->ksp);
151:   }
152:   return(0);
153: }

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

164:   if (st->transform) {
165:     if (st->shift_matrix == ST_MATMODE_COPY && nmat>2) {
166:       nc = (nmat*(nmat+1))/2;
167:       PetscMalloc(nc*sizeof(PetscScalar),&coeffs);
168:       /* Compute coeffs */
169:       STCoeffs_Monomial(st,coeffs);
170:     }
171:     for (k=1;k<nmat;k++) {
172:       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]);
173:     }
174:     if (st->shift_matrix == ST_MATMODE_COPY && nmat>2) {
175:       PetscFree(coeffs);
176:     }
177:     if (st->P!=st->T[nmat-1]) {
178:       MatDestroy(&st->P);
179:       st->P = st->T[nmat-1];
180:       PetscObjectReference((PetscObject)st->P);
181:     }
182:   }
183:   if (st->P) {
184:     if (!st->ksp) { STGetKSP(st,&st->ksp); }
185:     KSPSetOperators(st->ksp,st->P,st->P);
186:     KSPSetUp(st->ksp);
187:   }
188:   return(0);
189: }

193: PetscErrorCode STSetFromOptions_Sinvert(PetscOptionItems *PetscOptionsObject,ST st)
194: {
196:   PC             pc;
197:   PCType         pctype;
198:   KSPType        ksptype;

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

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