LCOV - code coverage report
Current view: top level - eps/impls/external/arpack - arpack.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 143 156 91.7 %
Date: 2020-05-28 03:14:05 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       3             :    SLEPc - Scalable Library for Eigenvalue Problem Computations
       4             :    Copyright (c) 2002-2020, Universitat Politecnica de Valencia, Spain
       5             : 
       6             :    This file is part of SLEPc.
       7             :    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
       8             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       9             : */
      10             : /*
      11             :    This file implements a wrapper to the ARPACK package
      12             : */
      13             : 
      14             : #include <slepc/private/epsimpl.h>
      15             : #include "arpack.h"
      16             : 
      17           8 : PetscErrorCode EPSSetUp_ARPACK(EPS eps)
      18             : {
      19             :   PetscErrorCode ierr;
      20             :   PetscInt       ncv;
      21           8 :   EPS_ARPACK     *ar = (EPS_ARPACK*)eps->data;
      22             : 
      23           8 :   PetscFunctionBegin;
      24           8 :   EPSCheckDefinite(eps);
      25           8 :   if (eps->ncv!=PETSC_DEFAULT) {
      26           3 :     if (eps->ncv<eps->nev+2) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The value of ncv must be at least nev+2");
      27           5 :   } else eps->ncv = PetscMin(PetscMax(20,2*eps->nev+1),eps->n); /* set default value of ncv */
      28           8 :   if (eps->mpd!=PETSC_DEFAULT) { ierr = PetscInfo(eps,"Warning: parameter mpd ignored\n");CHKERRQ(ierr); }
      29           8 :   if (eps->max_it==PETSC_DEFAULT) eps->max_it = PetscMax(300,(PetscInt)(2*eps->n/eps->ncv));
      30           8 :   if (!eps->which) { ierr = EPSSetWhichEigenpairs_Default(eps);CHKERRQ(ierr); }
      31           8 :   if (eps->which==EPS_ALL) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver does not support computing all eigenvalues");
      32           8 :   if (eps->which==EPS_WHICH_USER) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver does not support user-defined ordering of eigenvalues");
      33           8 :   EPSCheckUnsupported(eps,EPS_FEATURE_BALANCE | EPS_FEATURE_ARBITRARY | EPS_FEATURE_REGION | EPS_FEATURE_CONVERGENCE | EPS_FEATURE_STOPPING | EPS_FEATURE_TWOSIDED);
      34           8 :   EPSCheckIgnored(eps,EPS_FEATURE_EXTRACTION);
      35             : 
      36           8 :   ncv = eps->ncv;
      37             : #if defined(PETSC_USE_COMPLEX)
      38             :   ierr = PetscFree(ar->rwork);CHKERRQ(ierr);
      39             :   ierr = PetscMalloc1(ncv,&ar->rwork);CHKERRQ(ierr);
      40             :   ierr = PetscLogObjectMemory((PetscObject)eps,ncv*sizeof(PetscReal));CHKERRQ(ierr);
      41             :   ar->lworkl = 3*ncv*ncv+5*ncv;
      42             :   ierr = PetscFree(ar->workev);CHKERRQ(ierr);
      43             :   ierr = PetscMalloc1(3*ncv,&ar->workev);CHKERRQ(ierr);
      44             :   ierr = PetscLogObjectMemory((PetscObject)eps,3*ncv*sizeof(PetscScalar));CHKERRQ(ierr);
      45             : #else
      46           8 :   if (eps->ishermitian) {
      47           6 :     ar->lworkl = ncv*(ncv+8);
      48             :   } else {
      49           2 :     ar->lworkl = 3*ncv*ncv+6*ncv;
      50           2 :     ierr = PetscFree(ar->workev);CHKERRQ(ierr);
      51           2 :     ierr = PetscMalloc1(3*ncv,&ar->workev);CHKERRQ(ierr);
      52           2 :     ierr = PetscLogObjectMemory((PetscObject)eps,3*ncv*sizeof(PetscScalar));CHKERRQ(ierr);
      53             :   }
      54             : #endif
      55           8 :   ierr = PetscFree(ar->workl);CHKERRQ(ierr);
      56           8 :   ierr = PetscMalloc1(ar->lworkl,&ar->workl);CHKERRQ(ierr);
      57           8 :   ierr = PetscLogObjectMemory((PetscObject)eps,ar->lworkl*sizeof(PetscScalar));CHKERRQ(ierr);
      58           8 :   ierr = PetscFree(ar->select);CHKERRQ(ierr);
      59           8 :   ierr = PetscMalloc1(ncv,&ar->select);CHKERRQ(ierr);
      60           8 :   ierr = PetscLogObjectMemory((PetscObject)eps,ncv*sizeof(PetscInt));CHKERRQ(ierr);
      61           8 :   ierr = PetscFree(ar->workd);CHKERRQ(ierr);
      62           8 :   ierr = PetscMalloc1(3*eps->nloc,&ar->workd);CHKERRQ(ierr);
      63           8 :   ierr = PetscLogObjectMemory((PetscObject)eps,3*eps->nloc*sizeof(PetscScalar));CHKERRQ(ierr);
      64             : 
      65           8 :   ierr = EPSAllocateSolution(eps,0);CHKERRQ(ierr);
      66           8 :   ierr = EPS_SetInnerProduct(eps);CHKERRQ(ierr);
      67           8 :   ierr = EPSSetWorkVecs(eps,2);CHKERRQ(ierr);
      68           8 :   PetscFunctionReturn(0);
      69             : }
      70             : 
      71           8 : PetscErrorCode EPSSolve_ARPACK(EPS eps)
      72             : {
      73             :   PetscErrorCode ierr;
      74           8 :   EPS_ARPACK     *ar = (EPS_ARPACK*)eps->data;
      75           8 :   char           bmat[1],howmny[] = "A";
      76             :   const char     *which;
      77             :   PetscInt       n,iparam[11],ipntr[14],ido,info,nev,ncv,rvec;
      78             : #if !defined(PETSC_HAVE_MPIUNI)
      79             :   MPI_Fint       fcomm;
      80             : #endif
      81             :   PetscScalar    sigmar,*pV,*resid;
      82           8 :   Vec            x,y,w = eps->work[0];
      83             :   Mat            A;
      84             :   PetscBool      isSinv,isShift;
      85             : #if !defined(PETSC_USE_COMPLEX)
      86           8 :   PetscScalar    sigmai = 0.0;
      87             : #endif
      88             : 
      89           8 :   PetscFunctionBegin;
      90           8 :   nev = eps->nev;
      91           8 :   ncv = eps->ncv;
      92             : #if !defined(PETSC_HAVE_MPIUNI)
      93           8 :   fcomm = MPI_Comm_c2f(PetscObjectComm((PetscObject)eps));
      94             : #endif
      95           8 :   n = eps->nloc;
      96           8 :   ierr = EPSGetStartVector(eps,0,NULL);CHKERRQ(ierr);
      97           8 :   ierr = BVSetActiveColumns(eps->V,0,0);CHKERRQ(ierr);  /* just for deflation space */
      98           8 :   ierr = BVCopyVec(eps->V,0,eps->work[1]);CHKERRQ(ierr);
      99           8 :   ierr = BVGetArray(eps->V,&pV);CHKERRQ(ierr);
     100           8 :   ierr = VecGetArray(eps->work[1],&resid);CHKERRQ(ierr);
     101             : 
     102           8 :   ido  = 0;            /* first call to reverse communication interface */
     103           8 :   info = 1;            /* indicates an initial vector is provided */
     104           8 :   iparam[0] = 1;       /* use exact shifts */
     105           8 :   iparam[2] = eps->max_it;  /* max Arnoldi iterations */
     106           8 :   iparam[3] = 1;       /* blocksize */
     107           8 :   iparam[4] = 0;       /* number of converged Ritz values */
     108             : 
     109             :   /*
     110             :      Computational modes ([]=not supported):
     111             :             symmetric    non-symmetric    complex
     112             :         1     1  'I'        1  'I'         1  'I'
     113             :         2     3  'I'        3  'I'         3  'I'
     114             :         3     2  'G'        2  'G'         2  'G'
     115             :         4     3  'G'        3  'G'         3  'G'
     116             :         5   [ 4  'G' ]    [ 3  'G' ]
     117             :         6   [ 5  'G' ]    [ 4  'G' ]
     118             :    */
     119           8 :   ierr = PetscObjectTypeCompare((PetscObject)eps->st,STSINVERT,&isSinv);CHKERRQ(ierr);
     120           8 :   ierr = PetscObjectTypeCompare((PetscObject)eps->st,STSHIFT,&isShift);CHKERRQ(ierr);
     121           8 :   ierr = STGetShift(eps->st,&sigmar);CHKERRQ(ierr);
     122           8 :   ierr = STGetMatrix(eps->st,0,&A);CHKERRQ(ierr);
     123           8 :   ierr = MatCreateVecsEmpty(A,&x,&y);CHKERRQ(ierr);
     124             : 
     125           8 :   if (isSinv) {
     126             :     /* shift-and-invert mode */
     127           1 :     iparam[6] = 3;
     128           1 :     if (eps->ispositive) bmat[0] = 'G';
     129           1 :     else bmat[0] = 'I';
     130           7 :   } else if (isShift && eps->ispositive) {
     131             :     /* generalized shift mode with B positive definite */
     132           1 :     iparam[6] = 2;
     133           1 :     bmat[0] = 'G';
     134             :   } else {
     135             :     /* regular mode */
     136           6 :     if (eps->ishermitian && eps->isgeneralized) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Spectral transformation not supported by ARPACK hermitian solver");
     137           6 :     iparam[6] = 1;
     138           6 :     bmat[0] = 'I';
     139             :   }
     140             : 
     141             : #if !defined(PETSC_USE_COMPLEX)
     142           8 :   if (eps->ishermitian) {
     143           6 :     switch (eps->which) {
     144             :       case EPS_TARGET_MAGNITUDE:
     145             :       case EPS_LARGEST_MAGNITUDE:  which = "LM"; break;
     146           0 :       case EPS_SMALLEST_MAGNITUDE: which = "SM"; break;
     147             :       case EPS_TARGET_REAL:
     148           0 :       case EPS_LARGEST_REAL:       which = "LA"; break;
     149           2 :       case EPS_SMALLEST_REAL:      which = "SA"; break;
     150           0 :       default: SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_WRONG,"Wrong value of eps->which");
     151             :     }
     152             :   } else {
     153             : #endif
     154           2 :     switch (eps->which) {
     155             :       case EPS_TARGET_MAGNITUDE:
     156             :       case EPS_LARGEST_MAGNITUDE:  which = "LM"; break;
     157           0 :       case EPS_SMALLEST_MAGNITUDE: which = "SM"; break;
     158             :       case EPS_TARGET_REAL:
     159           1 :       case EPS_LARGEST_REAL:       which = "LR"; break;
     160           0 :       case EPS_SMALLEST_REAL:      which = "SR"; break;
     161             :       case EPS_TARGET_IMAGINARY:
     162           0 :       case EPS_LARGEST_IMAGINARY:  which = "LI"; break;
     163           0 :       case EPS_SMALLEST_IMAGINARY: which = "SI"; break;
     164           0 :       default: SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_WRONG,"Wrong value of eps->which");
     165             :     }
     166             : #if !defined(PETSC_USE_COMPLEX)
     167             :   }
     168             : #endif
     169             : 
     170             :   do {
     171             : 
     172             : #if !defined(PETSC_USE_COMPLEX)
     173        1493 :     if (eps->ishermitian) {
     174         695 :       PetscStackCall("ARPACKsaupd",ARPACKsaupd_(&fcomm,&ido,bmat,&n,which,&nev,&eps->tol,resid,&ncv,pV,&n,iparam,ipntr,ar->workd,ar->workl,&ar->lworkl,&info));
     175             :     } else {
     176         798 :       PetscStackCall("ARPACKnaupd",ARPACKnaupd_(&fcomm,&ido,bmat,&n,which,&nev,&eps->tol,resid,&ncv,pV,&n,iparam,ipntr,ar->workd,ar->workl,&ar->lworkl,&info));
     177             :     }
     178             : #else
     179             :     PetscStackCall("ARPACKnaupd",ARPACKnaupd_(&fcomm,&ido,bmat,&n,which,&nev,&eps->tol,resid,&ncv,pV,&n,iparam,ipntr,ar->workd,ar->workl,&ar->lworkl,ar->rwork,&info));
     180             : #endif
     181             : 
     182        1493 :     if (ido == -1 || ido == 1 || ido == 2) {
     183        1485 :       if (ido == 1 && iparam[6] == 3 && bmat[0] == 'G') {
     184             :         /* special case for shift-and-invert with B semi-positive definite*/
     185           0 :         ierr = VecPlaceArray(x,&ar->workd[ipntr[2]-1]);CHKERRQ(ierr);
     186             :       } else {
     187        1485 :         ierr = VecPlaceArray(x,&ar->workd[ipntr[0]-1]);CHKERRQ(ierr);
     188             :       }
     189        1485 :       ierr = VecPlaceArray(y,&ar->workd[ipntr[1]-1]);CHKERRQ(ierr);
     190             : 
     191        1485 :       if (ido == -1) {
     192             :         /* Y = OP * X for for the initialization phase to
     193             :            force the starting vector into the range of OP */
     194           1 :         ierr = STApply(eps->st,x,y);CHKERRQ(ierr);
     195        1484 :       } else if (ido == 2) {
     196             :         /* Y = B * X */
     197         579 :         ierr = BVApplyMatrix(eps->V,x,y);CHKERRQ(ierr);
     198             :       } else { /* ido == 1 */
     199         905 :         if (iparam[6] == 3 && bmat[0] == 'G') {
     200             :           /* Y = OP * X for shift-and-invert with B semi-positive definite */
     201           0 :           ierr = STMatSolve(eps->st,x,y);CHKERRQ(ierr);
     202         905 :         } else if (iparam[6] == 2) {
     203             :           /* X=A*X Y=B^-1*X for shift with B positive definite */
     204         189 :           ierr = MatMult(A,x,y);CHKERRQ(ierr);
     205         189 :           if (sigmar != 0.0) {
     206           0 :             ierr = BVApplyMatrix(eps->V,x,w);CHKERRQ(ierr);
     207           0 :             ierr = VecAXPY(y,sigmar,w);CHKERRQ(ierr);
     208             :           }
     209         189 :           ierr = VecCopy(y,x);CHKERRQ(ierr);
     210         189 :           ierr = STMatSolve(eps->st,x,y);CHKERRQ(ierr);
     211             :         } else {
     212             :           /* Y = OP * X */
     213         716 :           ierr = STApply(eps->st,x,y);CHKERRQ(ierr);
     214             :         }
     215         905 :         ierr = BVOrthogonalizeVec(eps->V,y,NULL,NULL,NULL);CHKERRQ(ierr);
     216             :       }
     217             : 
     218        1485 :       ierr = VecResetArray(x);CHKERRQ(ierr);
     219        1485 :       ierr = VecResetArray(y);CHKERRQ(ierr);
     220           8 :     } else if (ido != 99) SETERRQ1(PetscObjectComm((PetscObject)eps),PETSC_ERR_LIB,"Internal error in ARPACK reverse comunication interface (ido=%d)",ido);
     221             : 
     222        1493 :   } while (ido != 99);
     223             : 
     224           8 :   eps->nconv = iparam[4];
     225           8 :   eps->its = iparam[2];
     226             : 
     227           8 :   if (info==3) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_LIB,"No shift could be applied in xxAUPD.\nTry increasing the size of NCV relative to NEV");
     228           8 :   else if (info!=0 && info!=1) SETERRQ1(PetscObjectComm((PetscObject)eps),PETSC_ERR_LIB,"Error reported by ARPACK subroutine xxAUPD (%d)",(int)info);
     229             : 
     230           8 :   rvec = PETSC_TRUE;
     231             : 
     232           8 :   if (eps->nconv > 0) {
     233             : #if !defined(PETSC_USE_COMPLEX)
     234           8 :     if (eps->ishermitian) {
     235           6 :       PetscStackCall("ARPACKseupd",ARPACKseupd_(&fcomm,&rvec,howmny,ar->select,eps->eigr,pV,&n,&sigmar,bmat,&n,which,&nev,&eps->tol,resid,&ncv,pV,&n,iparam,ipntr,ar->workd,ar->workl,&ar->lworkl,&info));
     236             :     } else {
     237           2 :       PetscStackCall("ARPACKneupd",ARPACKneupd_(&fcomm,&rvec,howmny,ar->select,eps->eigr,eps->eigi,pV,&n,&sigmar,&sigmai,ar->workev,bmat,&n,which,&nev,&eps->tol,resid,&ncv,pV,&n,iparam,ipntr,ar->workd,ar->workl,&ar->lworkl,&info));
     238             :     }
     239             : #else
     240             :     PetscStackCall("ARPACKneupd",ARPACKneupd_(&fcomm,&rvec,howmny,ar->select,eps->eigr,pV,&n,&sigmar,ar->workev,bmat,&n,which,&nev,&eps->tol,resid,&ncv,pV,&n,iparam,ipntr,ar->workd,ar->workl,&ar->lworkl,ar->rwork,&info));
     241             : #endif
     242           8 :     if (info!=0) SETERRQ1(PetscObjectComm((PetscObject)eps),PETSC_ERR_LIB,"Error reported by ARPACK subroutine xxEUPD (%d)",(int)info);
     243             :   }
     244             : 
     245           8 :   ierr = BVRestoreArray(eps->V,&pV);CHKERRQ(ierr);
     246           8 :   ierr = VecRestoreArray(eps->work[1],&resid);CHKERRQ(ierr);
     247           8 :   if (eps->nconv >= eps->nev) eps->reason = EPS_CONVERGED_TOL;
     248           0 :   else eps->reason = EPS_DIVERGED_ITS;
     249             : 
     250           8 :   ierr = VecDestroy(&x);CHKERRQ(ierr);
     251           8 :   ierr = VecDestroy(&y);CHKERRQ(ierr);
     252           8 :   PetscFunctionReturn(0);
     253             : }
     254             : 
     255           8 : PetscErrorCode EPSBackTransform_ARPACK(EPS eps)
     256             : {
     257             :   PetscErrorCode ierr;
     258             :   PetscBool      isSinv;
     259             : 
     260           8 :   PetscFunctionBegin;
     261           8 :   ierr = PetscObjectTypeCompare((PetscObject)eps->st,STSINVERT,&isSinv);CHKERRQ(ierr);
     262           8 :   if (!isSinv) {
     263           7 :     ierr = EPSBackTransform_Default(eps);CHKERRQ(ierr);
     264             :   }
     265           8 :   PetscFunctionReturn(0);
     266             : }
     267             : 
     268           7 : PetscErrorCode EPSReset_ARPACK(EPS eps)
     269             : {
     270             :   PetscErrorCode ierr;
     271           7 :   EPS_ARPACK     *ar = (EPS_ARPACK*)eps->data;
     272             : 
     273           7 :   PetscFunctionBegin;
     274           7 :   ierr = PetscFree(ar->workev);CHKERRQ(ierr);
     275           7 :   ierr = PetscFree(ar->workl);CHKERRQ(ierr);
     276           7 :   ierr = PetscFree(ar->select);CHKERRQ(ierr);
     277           7 :   ierr = PetscFree(ar->workd);CHKERRQ(ierr);
     278             : #if defined(PETSC_USE_COMPLEX)
     279             :   ierr = PetscFree(ar->rwork);CHKERRQ(ierr);
     280             : #endif
     281           7 :   PetscFunctionReturn(0);
     282             : }
     283             : 
     284           7 : PetscErrorCode EPSDestroy_ARPACK(EPS eps)
     285             : {
     286             :   PetscErrorCode ierr;
     287             : 
     288           7 :   PetscFunctionBegin;
     289           7 :   ierr = PetscFree(eps->data);CHKERRQ(ierr);
     290           7 :   PetscFunctionReturn(0);
     291             : }
     292             : 
     293           7 : SLEPC_EXTERN PetscErrorCode EPSCreate_ARPACK(EPS eps)
     294             : {
     295             :   EPS_ARPACK     *ctx;
     296             :   PetscErrorCode ierr;
     297             : 
     298           7 :   PetscFunctionBegin;
     299           7 :   ierr = PetscNewLog(eps,&ctx);CHKERRQ(ierr);
     300           7 :   eps->data = (void*)ctx;
     301             : 
     302           7 :   eps->ops->solve          = EPSSolve_ARPACK;
     303           7 :   eps->ops->setup          = EPSSetUp_ARPACK;
     304           7 :   eps->ops->setupsort      = EPSSetUpSort_Basic;
     305           7 :   eps->ops->destroy        = EPSDestroy_ARPACK;
     306           7 :   eps->ops->reset          = EPSReset_ARPACK;
     307           7 :   eps->ops->backtransform  = EPSBackTransform_ARPACK;
     308           7 :   PetscFunctionReturn(0);
     309             : }
     310             : 

Generated by: LCOV version 1.13