LCOV - code coverage report
Current view: top level - eps/impls/external/arpack - arpack.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 144 155 92.9 %
Date: 2024-11-21 00:40:22 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14