Actual source code: arpack.c

  1: /*
  2:        This file implements a wrapper to the ARPACK package

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

  8:    This file is part of SLEPc.
  9:       
 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 src/eps/impls/external/arpack/arpackp.h
 25:  #include private/stimpl.h

 27: PetscErrorCode EPSSolve_ARPACK(EPS);

 31: PetscErrorCode EPSSetUp_ARPACK(EPS eps)
 32: {
 34:   PetscInt       ncv;
 35:   EPS_ARPACK     *ar = (EPS_ARPACK *)eps->data;

 38:   if (eps->ncv) {
 39:     if (eps->ncv<eps->nev+2) SETERRQ(1,"The value of ncv must be at least nev+2");
 40:   } else /* set default value of ncv */
 41:     eps->ncv = PetscMin(PetscMax(20,2*eps->nev+1),eps->n);
 42:   if (eps->mpd) PetscInfo(eps,"Warning: parameter mpd ignored\n");
 43:   if (!eps->max_it) eps->max_it = PetscMax(300,(PetscInt)(2*eps->n/eps->ncv));
 44:   if (!eps->which) eps->which = EPS_LARGEST_MAGNITUDE;

 46:   ncv = eps->ncv;
 47: #if defined(PETSC_USE_COMPLEX)
 48:   PetscFree(ar->rwork);
 49:   PetscMalloc(ncv*sizeof(PetscReal),&ar->rwork);
 50:   ar->lworkl = PetscBLASIntCast(3*ncv*ncv+5*ncv);
 51:   PetscFree(ar->workev);
 52:   PetscMalloc(3*ncv*sizeof(PetscScalar),&ar->workev);
 53: #else
 54:   if( eps->ishermitian ) {
 55:     ar->lworkl = PetscBLASIntCast(ncv*(ncv+8));
 56:   } else {
 57:     ar->lworkl = PetscBLASIntCast(3*ncv*ncv+6*ncv);
 58:     PetscFree(ar->workev);
 59:     PetscMalloc(3*ncv*sizeof(PetscScalar),&ar->workev);
 60:   }
 61: #endif
 62:   PetscFree(ar->workl);
 63:   PetscMalloc(ar->lworkl*sizeof(PetscScalar),&ar->workl);
 64:   PetscFree(ar->select);
 65:   PetscMalloc(ncv*sizeof(PetscTruth),&ar->select);
 66:   PetscFree(ar->workd);
 67:   PetscMalloc(3*eps->nloc*sizeof(PetscScalar),&ar->workd);

 69:   if (eps->extraction) {
 70:      PetscInfo(eps,"Warning: extraction type ignored\n");
 71:   }

 73:   if (eps->balance!=EPS_BALANCE_NONE)
 74:     SETERRQ(PETSC_ERR_SUP,"Balancing not supported in the Arpack interface");

 76:   EPSAllocateSolution(eps);
 77:   EPSDefaultGetWork(eps,2);

 79:   /* dispatch solve method */
 80:   if (eps->leftvecs) SETERRQ(PETSC_ERR_SUP,"Left vectors not supported in this solver");
 81:   eps->ops->solve = EPSSolve_ARPACK;
 82:   return(0);
 83: }

 87: PetscErrorCode EPSSolve_ARPACK(EPS eps)
 88: {
 90:   EPS_ARPACK           *ar = (EPS_ARPACK *)eps->data;
 91:   char                 bmat[1], howmny[] = "A";
 92:   const char           *which;
 93:   PetscBLASInt   n, iparam[11], ipntr[14], ido, info,
 94:                  nev, ncv;
 95:   PetscScalar          sigmar, *pV, *resid;
 96:   Vec                  x, y, w = eps->work[0];
 97:   Mat                  A;
 98:   PetscTruth           isSinv, isShift, rvec;
 99:   PetscBLASInt   fcomm;
100: #if !defined(PETSC_USE_COMPLEX)
101:   PetscScalar    sigmai = 0.0;
102: #endif
104: 
105:   nev = PetscBLASIntCast(eps->nev);
106:   ncv = PetscBLASIntCast(eps->ncv);
107:   fcomm = PetscBLASIntCast(MPI_Comm_c2f(((PetscObject)eps)->comm));
108:   n = PetscBLASIntCast(eps->nloc);
109:   VecCreateMPIWithArray(((PetscObject)eps)->comm,eps->nloc,PETSC_DECIDE,PETSC_NULL,&x);
110:   VecCreateMPIWithArray(((PetscObject)eps)->comm,eps->nloc,PETSC_DECIDE,PETSC_NULL,&y);
111:   VecGetArray(eps->V[0],&pV);
112:   EPSGetStartVector(eps,0,eps->work[1],PETSC_NULL);
113:   VecGetArray(eps->work[1],&resid);
114: 
115:   ido  = 0;            /* first call to reverse communication interface */
116:   info = 1;            /* indicates a initial vector is provided */
117:   iparam[0] = 1;       /* use exact shifts */
118:   iparam[2] = PetscBLASIntCast(eps->max_it);  /* maximum number of Arnoldi update iterations */
119:   iparam[3] = 1;       /* blocksize */
120:   iparam[4] = 0;       /* number of converged Ritz values */
121: 
122:   /*
123:      Computational modes ([]=not supported):
124:             symmetric    non-symmetric    complex
125:         1     1  'I'        1  'I'         1  'I'
126:         2     3  'I'        3  'I'         3  'I'
127:         3     2  'G'        2  'G'         2  'G'
128:         4     3  'G'        3  'G'         3  'G'
129:         5   [ 4  'G' ]    [ 3  'G' ]
130:         6   [ 5  'G' ]    [ 4  'G' ]
131:    */
132:   PetscTypeCompare((PetscObject)eps->OP,STSINVERT,&isSinv);
133:   PetscTypeCompare((PetscObject)eps->OP,STSHIFT,&isShift);
134:   STGetShift(eps->OP,&sigmar);
135:   STGetOperators(eps->OP,&A,PETSC_NULL);

137:   if (isSinv) {
138:     /* shift-and-invert mode */
139:     iparam[6] = 3;
140:     if (eps->ispositive) bmat[0] = 'G';
141:     else bmat[0] = 'I';
142:   } else if (isShift && eps->ispositive) {
143:     /* generalized shift mode with B positive definite */
144:     iparam[6] = 2;
145:     bmat[0] = 'G';
146:   } else {
147:     /* regular mode */
148:     if (eps->ishermitian && eps->isgeneralized)
149:       SETERRQ(PETSC_ERR_SUP,"Spectral transformation not supported by ARPACK hermitian solver");
150:     iparam[6] = 1;
151:     bmat[0] = 'I';
152:   }
153: 
154: #if !defined(PETSC_USE_COMPLEX)
155:     if (eps->ishermitian) {
156:       switch(eps->which) {
157:         case EPS_LARGEST_MAGNITUDE:  which = "LM"; break;
158:         case EPS_SMALLEST_MAGNITUDE: which = "SM"; break;
159:         case EPS_LARGEST_REAL:       which = "LA"; break;
160:         case EPS_SMALLEST_REAL:      which = "SA"; break;
161:         default: SETERRQ(1,"Wrong value of eps->which");
162:       }
163:     } else {
164: #endif
165:       switch(eps->which) {
166:         case EPS_LARGEST_MAGNITUDE:  which = "LM"; break;
167:         case EPS_SMALLEST_MAGNITUDE: which = "SM"; break;
168:         case EPS_LARGEST_REAL:       which = "LR"; break;
169:         case EPS_SMALLEST_REAL:      which = "SR"; break;
170:         case EPS_LARGEST_IMAGINARY:  which = "LI"; break;
171:         case EPS_SMALLEST_IMAGINARY: which = "SI"; break;
172:         default: SETERRQ(1,"Wrong value of eps->which");
173:       }
174: #if !defined(PETSC_USE_COMPLEX)
175:     }
176: #endif

178:   do {

180: #if !defined(PETSC_USE_COMPLEX)
181:     if (eps->ishermitian) {
182:       ARsaupd_( &fcomm, &ido, bmat, &n, which, &nev, &eps->tol,
183:                 resid, &ncv, pV, &n, iparam, ipntr, ar->workd,
184:                 ar->workl, &ar->lworkl, &info, 1, 2 );
185:     }
186:     else {
187:       ARnaupd_( &fcomm, &ido, bmat, &n, which, &nev, &eps->tol,
188:                 resid, &ncv, pV, &n, iparam, ipntr, ar->workd,
189:                 ar->workl, &ar->lworkl, &info, 1, 2 );
190:     }
191: #else
192:     ARnaupd_( &fcomm, &ido, bmat, &n, which, &nev, &eps->tol,
193:               resid, &ncv, pV, &n, iparam, ipntr, ar->workd,
194:               ar->workl, &ar->lworkl, ar->rwork, &info, 1, 2 );
195: #endif
196: 
197:     if (ido == -1 || ido == 1 || ido == 2) {
198:       if (ido == 1 && iparam[6] == 3 && bmat[0] == 'G') {
199:         /* special case for shift-and-invert with B semi-positive definite*/
200:         VecPlaceArray(x,&ar->workd[ipntr[2]-1]);
201:       } else {
202:         VecPlaceArray(x,&ar->workd[ipntr[0]-1]);
203:       }
204:       VecPlaceArray(y,&ar->workd[ipntr[1]-1]);
205: 
206:       if (ido == -1) {
207:         /* Y = OP * X for for the initialization phase to 
208:            force the starting vector into the range of OP */
209:         STApply(eps->OP,x,y);
210:       } else if (ido == 2) {
211:         /* Y = B * X */
212:         IPApplyMatrix(eps->ip,x,y);
213:       } else { /* ido == 1 */
214:         if (iparam[6] == 3 && bmat[0] == 'G') {
215:           /* Y = OP * X for shift-and-invert with B semi-positive definite */
216:           STAssociatedKSPSolve(eps->OP,x,y);
217:         } else if (iparam[6] == 2) {
218:           /* X=A*X Y=B^-1*X for shift with B positive definite */
219:           MatMult(A,x,y);
220:           if (sigmar != 0.0) {
221:             IPApplyMatrix(eps->ip,x,w);
222:             VecAXPY(y,sigmar,w);
223:           }
224:           VecCopy(y,x);
225:           STAssociatedKSPSolve(eps->OP,x,y);
226:         } else  {
227:           /* Y = OP * X */
228:           STApply(eps->OP,x,y);
229:         }
230:         IPOrthogonalize(eps->ip,0,PETSC_NULL,eps->nds,PETSC_NULL,eps->DS,y,PETSC_NULL,PETSC_NULL,PETSC_NULL);
231:       }
232: 
233:       VecResetArray(x);
234:       VecResetArray(y);
235:     } else if (ido != 99) {
236:       SETERRQ1(1,"Internal error in ARPACK reverse comunication interface (ido=%i)\n",ido);
237:     }
238: 
239:   } while (ido != 99);

241:   eps->nconv = iparam[4];
242:   eps->its = iparam[2];
243: 
244:   if (info==3) { SETERRQ(1,"No shift could be applied in xxAUPD.\n"
245:                            "Try increasing the size of NCV relative to NEV."); }
246:   else if (info!=0 && info!=1) { SETERRQ1(PETSC_ERR_LIB,"Error reported by ARPACK subroutine xxAUPD (%d)",info);}

248:   rvec = PETSC_TRUE;

250:   if (eps->nconv > 0) {
251: #if !defined(PETSC_USE_COMPLEX)
252:     if (eps->ishermitian) {
253:       EPSMonitor(eps,iparam[2],iparam[4],&ar->workl[ipntr[5]-1],eps->eigi,&ar->workl[ipntr[6]-1],eps->ncv);
254:       ARseupd_ ( &fcomm, &rvec, howmny, ar->select, eps->eigr,
255:                  pV, &n, &sigmar,
256:                  bmat, &n, which, &nev, &eps->tol,
257:                  resid, &ncv, pV, &n, iparam, ipntr, ar->workd,
258:                  ar->workl, &ar->lworkl, &info, 1, 1, 2 );
259:     }
260:     else {
261:       EPSMonitor(eps,iparam[2],iparam[4],&ar->workl[ipntr[5]-1],&ar->workl[ipntr[6]-1],&ar->workl[ipntr[7]-1],eps->ncv);
262:       ARneupd_ ( &fcomm, &rvec, howmny, ar->select, eps->eigr, eps->eigi,
263:                  pV, &n, &sigmar, &sigmai, ar->workev,
264:                  bmat, &n, which, &nev, &eps->tol,
265:                  resid, &ncv, pV, &n, iparam, ipntr, ar->workd,
266:                  ar->workl, &ar->lworkl, &info, 1, 1, 2 );
267:     }
268: #else
269:     EPSMonitor(eps,eps->its,iparam[4],&ar->workl[ipntr[5]-1],eps->eigi,(PetscReal*)&ar->workl[ipntr[7]-1],eps->ncv);
270:     ARneupd_ ( &fcomm, &rvec, howmny, ar->select, eps->eigr,
271:                pV, &n, &sigmar, ar->workev,
272:                bmat, &n, which, &nev, &eps->tol,
273:                resid, &ncv, pV, &n, iparam, ipntr, ar->workd,
274:                ar->workl, &ar->lworkl, ar->rwork, &info, 1, 1, 2 );
275: #endif
276:     if (info!=0) { SETERRQ1(PETSC_ERR_LIB,"Error reported by ARPACK subroutine xxEUPD (%d)",info); }
277:   }

279:   VecRestoreArray( eps->V[0], &pV );
280:   VecRestoreArray( eps->work[1], &resid );
281:   if( eps->nconv >= eps->nev ) eps->reason = EPS_CONVERGED_TOL;
282:   else eps->reason = EPS_DIVERGED_ITS;

284:   if (eps->ishermitian) {
285:     PetscMemcpy(eps->errest,&ar->workl[ipntr[8]-1],eps->nconv);
286:   } else {
287:     PetscMemcpy(eps->errest,&ar->workl[ipntr[10]-1],eps->nconv);
288:   }

290:   VecDestroy(x);
291:   VecDestroy(y);

293:   return(0);
294: }

298: PetscErrorCode EPSBackTransform_ARPACK(EPS eps)
299: {
301:   PetscTruth     isSinv;

304:   PetscTypeCompare((PetscObject)eps->OP,STSINVERT,&isSinv);
305:   if (!isSinv) {
306:     EPSBackTransform_Default(eps);
307:   }
308:   return(0);
309: }

313: PetscErrorCode EPSDestroy_ARPACK(EPS eps)
314: {
316:   EPS_ARPACK     *ar = (EPS_ARPACK *)eps->data;

320:   PetscFree(ar->workev);
321:   PetscFree(ar->workl);
322:   PetscFree(ar->select);
323:   PetscFree(ar->workd);
324: #if defined(PETSC_USE_COMPLEX)
325:   PetscFree(ar->rwork);
326: #endif
327:   PetscFree(eps->data);
328:   EPSDefaultFreeWork(eps);
329:   EPSFreeSolution(eps);
330:   return(0);
331: }

336: PetscErrorCode EPSCreate_ARPACK(EPS eps)
337: {
339:   EPS_ARPACK     *arpack;

342:   PetscNew(EPS_ARPACK,&arpack);
343:   PetscLogObjectMemory(eps,sizeof(EPS_ARPACK));
344:   eps->data                      = (void *) arpack;
345:   eps->ops->setup                = EPSSetUp_ARPACK;
346:   eps->ops->destroy              = EPSDestroy_ARPACK;
347:   eps->ops->backtransform        = EPSBackTransform_ARPACK;
348:   eps->ops->computevectors       = EPSComputeVectors_Default;
349:   return(0);
350: }