Actual source code: trlan.c

slepc-3.21.1 2024-04-26
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain

  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 TRLAN package
 12: */

 14: #include <slepc/private/epsimpl.h>
 15: #include "trlan.h"

 17: /* Nasty global variable to access EPS data from TRLan_ */
 18: static struct {
 19:   EPS eps;
 20:   Vec x,y;
 21: } globaldata;

 23: static PetscErrorCode EPSSetUp_TRLAN(EPS eps)
 24: {
 25:   EPS_TRLAN      *tr = (EPS_TRLAN*)eps->data;

 27:   PetscFunctionBegin;
 28:   EPSCheckHermitian(eps);
 29:   EPSCheckStandard(eps);
 30:   PetscCall(PetscBLASIntCast(PetscMax(7,eps->nev+PetscMin(eps->nev,6)),&tr->maxlan));
 31:   if (eps->ncv!=PETSC_DEFAULT) {
 32:     PetscCheck(eps->ncv>=eps->nev,PetscObjectComm((PetscObject)eps),PETSC_ERR_USER_INPUT,"The value of ncv must be at least nev");
 33:   } else eps->ncv = tr->maxlan;
 34:   if (eps->mpd!=PETSC_DEFAULT) PetscCall(PetscInfo(eps,"Warning: parameter mpd ignored\n"));
 35:   if (eps->max_it==PETSC_DEFAULT) eps->max_it = PetscMax(1000,eps->n);

 37:   if (!eps->which) eps->which = EPS_LARGEST_REAL;
 38:   PetscCheck(eps->which==EPS_SMALLEST_REAL || eps->which==EPS_LARGEST_REAL || eps->which==EPS_TARGET_REAL,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver supports only smallest, largest or target real eigenvalues");
 39:   EPSCheckUnsupported(eps,EPS_FEATURE_ARBITRARY | EPS_FEATURE_REGION | EPS_FEATURE_CONVERGENCE | EPS_FEATURE_STOPPING);
 40:   EPSCheckIgnored(eps,EPS_FEATURE_BALANCE | EPS_FEATURE_EXTRACTION);

 42:   tr->restart = 0;
 43:   if (tr->maxlan+1-eps->ncv<=0) PetscCall(PetscBLASIntCast(tr->maxlan*(tr->maxlan+10),&tr->lwork));
 44:   else PetscCall(PetscBLASIntCast(eps->nloc*(tr->maxlan+1-eps->ncv) + tr->maxlan*(tr->maxlan+10),&tr->lwork));
 45:   if (tr->work) PetscCall(PetscFree(tr->work));
 46:   PetscCall(PetscMalloc1(tr->lwork,&tr->work));

 48:   PetscCall(EPSAllocateSolution(eps,0));
 49:   PetscFunctionReturn(PETSC_SUCCESS);
 50: }

 52: static PetscBLASInt MatMult_TRLAN(PetscBLASInt *n,PetscBLASInt *m,PetscReal *xin,PetscBLASInt *ldx,PetscReal *yout,PetscBLASInt *ldy)
 53: {
 54:   Vec            x=globaldata.x,y=globaldata.y;
 55:   EPS            eps=globaldata.eps;
 56:   PetscBLASInt   i;

 58:   PetscFunctionBegin;
 59:   for (i=0;i<*m;i++) {
 60:     PetscCall(VecPlaceArray(x,(PetscScalar*)xin+i*(*ldx)));
 61:     PetscCall(VecPlaceArray(y,(PetscScalar*)yout+i*(*ldy)));
 62:     PetscCall(STApply(eps->st,x,y));
 63:     PetscCall(BVOrthogonalizeVec(eps->V,y,NULL,NULL,NULL));
 64:     PetscCall(VecResetArray(x));
 65:     PetscCall(VecResetArray(y));
 66:   }
 67:   PetscFunctionReturn(PETSC_SUCCESS);
 68: }

 70: static PetscErrorCode EPSSolve_TRLAN(EPS eps)
 71: {
 72:   PetscInt       i;
 73:   PetscBLASInt   ipar[32],n,lohi,stat,ncv;
 74:   EPS_TRLAN      *tr = (EPS_TRLAN*)eps->data;
 75:   PetscScalar    *pV;
 76:   Vec            v0;
 77:   Mat            A;
 78: #if !defined(PETSC_HAVE_MPIUNI)
 79:   MPI_Fint       fcomm;
 80: #endif

 82:   PetscFunctionBegin;
 83:   PetscCall(PetscBLASIntCast(eps->ncv,&ncv));
 84:   PetscCall(PetscBLASIntCast(eps->nloc,&n));

 86:   PetscCheck(eps->which==EPS_LARGEST_REAL || eps->which==EPS_TARGET_REAL || eps->which==EPS_SMALLEST_REAL,PetscObjectComm((PetscObject)eps),PETSC_ERR_USER_INPUT,"Wrong value of eps->which");
 87:   lohi = (eps->which==EPS_SMALLEST_REAL)? -1: 1;

 89:   globaldata.eps = eps;
 90:   PetscCall(STGetMatrix(eps->st,0,&A));
 91:   PetscCall(MatCreateVecsEmpty(A,&globaldata.x,&globaldata.y));

 93:   ipar[0]  = 0;            /* stat: error flag */
 94:   ipar[1]  = lohi;         /* smallest (lohi<0) or largest eigenvalues (lohi>0) */
 95:   PetscCall(PetscBLASIntCast(eps->nev,&ipar[2])); /* number of desired eigenpairs */
 96:   ipar[3]  = 0;            /* number of eigenpairs already converged */
 97:   ipar[4]  = tr->maxlan;   /* maximum Lanczos basis size */
 98:   ipar[5]  = tr->restart;  /* restarting scheme */
 99:   PetscCall(PetscBLASIntCast(eps->max_it,&ipar[6])); /* maximum number of MATVECs */
100: #if !defined(PETSC_HAVE_MPIUNI)
101:   fcomm    = MPI_Comm_c2f(PetscObjectComm((PetscObject)eps));
102:   ipar[7]  = fcomm;
103: #endif
104:   ipar[8]  = 0;            /* verboseness */
105:   ipar[9]  = 99;           /* Fortran IO unit number used to write log messages */
106:   ipar[10] = 1;            /* use supplied starting vector */
107:   ipar[11] = 0;            /* checkpointing flag */
108:   ipar[12] = 98;           /* Fortran IO unit number used to write checkpoint files */
109:   ipar[13] = 0;            /* number of flops per matvec per PE (not used) */
110:   tr->work[0] = eps->tol;  /* relative tolerance on residual norms */

112:   for (i=0;i<eps->ncv;i++) eps->eigr[i]=0.0;
113:   PetscCall(EPSGetStartVector(eps,0,NULL));
114:   PetscCall(BVSetActiveColumns(eps->V,0,0));  /* just for deflation space */
115:   PetscCall(BVGetColumn(eps->V,0,&v0));
116:   PetscCall(VecGetArray(v0,&pV));

118:   PetscStackCallExternalVoid("TRLan",TRLan_(MatMult_TRLAN,ipar,&n,&ncv,eps->eigr,pV,&n,tr->work,&tr->lwork));

120:   PetscCall(VecRestoreArray(v0,&pV));
121:   PetscCall(BVRestoreColumn(eps->V,0,&v0));

123:   stat        = ipar[0];
124:   eps->nconv  = ipar[3];
125:   eps->its    = ipar[25];
126:   eps->reason = EPS_CONVERGED_TOL;

128:   PetscCall(VecDestroy(&globaldata.x));
129:   PetscCall(VecDestroy(&globaldata.y));
130:   PetscCheck(stat==0,PetscObjectComm((PetscObject)eps),PETSC_ERR_LIB,"Error in TRLAN (code=%" PetscBLASInt_FMT ")",stat);
131:   PetscFunctionReturn(PETSC_SUCCESS);
132: }

134: static PetscErrorCode EPSReset_TRLAN(EPS eps)
135: {
136:   EPS_TRLAN      *tr = (EPS_TRLAN*)eps->data;

138:   PetscFunctionBegin;
139:   PetscCall(PetscFree(tr->work));
140:   PetscFunctionReturn(PETSC_SUCCESS);
141: }

143: static PetscErrorCode EPSDestroy_TRLAN(EPS eps)
144: {
145:   PetscFunctionBegin;
146:   PetscCall(PetscFree(eps->data));
147:   PetscFunctionReturn(PETSC_SUCCESS);
148: }

150: SLEPC_EXTERN PetscErrorCode EPSCreate_TRLAN(EPS eps)
151: {
152:   EPS_TRLAN      *ctx;

154:   PetscFunctionBegin;
155:   PetscCall(PetscNew(&ctx));
156:   eps->data = (void*)ctx;

158:   eps->ops->solve          = EPSSolve_TRLAN;
159:   eps->ops->setup          = EPSSetUp_TRLAN;
160:   eps->ops->setupsort      = EPSSetUpSort_Basic;
161:   eps->ops->destroy        = EPSDestroy_TRLAN;
162:   eps->ops->reset          = EPSReset_TRLAN;
163:   eps->ops->backtransform  = EPSBackTransform_Default;
164:   PetscFunctionReturn(PETSC_SUCCESS);
165: }