Actual source code: trlan.c

slepc-3.18.1 2022-11-02
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: PetscErrorCode EPSSetUp_TRLAN(EPS eps)
 24: {
 25:   EPS_TRLAN      *tr = (EPS_TRLAN*)eps->data;

 27:   EPSCheckHermitian(eps);
 28:   EPSCheckStandard(eps);
 29:   PetscBLASIntCast(PetscMax(7,eps->nev+PetscMin(eps->nev,6)),&tr->maxlan);
 30:   if (eps->ncv!=PETSC_DEFAULT) {
 32:   } else eps->ncv = tr->maxlan;
 33:   if (eps->mpd!=PETSC_DEFAULT) PetscInfo(eps,"Warning: parameter mpd ignored\n");
 34:   if (eps->max_it==PETSC_DEFAULT) eps->max_it = PetscMax(1000,eps->n);

 36:   if (!eps->which) eps->which = EPS_LARGEST_REAL;
 38:   EPSCheckUnsupported(eps,EPS_FEATURE_ARBITRARY | EPS_FEATURE_REGION | EPS_FEATURE_CONVERGENCE | EPS_FEATURE_STOPPING);
 39:   EPSCheckIgnored(eps,EPS_FEATURE_BALANCE | EPS_FEATURE_EXTRACTION);

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

 47:   EPSAllocateSolution(eps,0);
 48:   return 0;
 49: }

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

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

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

 80:   PetscBLASIntCast(eps->ncv,&ncv);
 81:   PetscBLASIntCast(eps->nloc,&n);

 84:   lohi = (eps->which==EPS_SMALLEST_REAL)? -1: 1;

 86:   globaldata.eps = eps;
 87:   STGetMatrix(eps->st,0,&A);
 88:   MatCreateVecsEmpty(A,&globaldata.x,&globaldata.y);

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

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

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

117:   VecRestoreArray(v0,&pV);
118:   BVRestoreColumn(eps->V,0,&v0);

120:   stat        = ipar[0];
121:   eps->nconv  = ipar[3];
122:   eps->its    = ipar[25];
123:   eps->reason = EPS_CONVERGED_TOL;

125:   VecDestroy(&globaldata.x);
126:   VecDestroy(&globaldata.y);
128:   return 0;
129: }

131: PetscErrorCode EPSReset_TRLAN(EPS eps)
132: {
133:   EPS_TRLAN      *tr = (EPS_TRLAN*)eps->data;

135:   PetscFree(tr->work);
136:   return 0;
137: }

139: PetscErrorCode EPSDestroy_TRLAN(EPS eps)
140: {
141:   PetscFree(eps->data);
142:   return 0;
143: }

145: SLEPC_EXTERN PetscErrorCode EPSCreate_TRLAN(EPS eps)
146: {
147:   EPS_TRLAN      *ctx;

149:   PetscNew(&ctx);
150:   eps->data = (void*)ctx;

152:   eps->ops->solve          = EPSSolve_TRLAN;
153:   eps->ops->setup          = EPSSetUp_TRLAN;
154:   eps->ops->setupsort      = EPSSetUpSort_Basic;
155:   eps->ops->destroy        = EPSDestroy_TRLAN;
156:   eps->ops->reset          = EPSReset_TRLAN;
157:   eps->ops->backtransform  = EPSBackTransform_Default;
158:   return 0;
159: }