Actual source code: trlan.c

  2: /*                       
  3:        This file implements a wrapper to the TRLAN package
  4: */
 5:  #include src/eps/impls/trlan/trlanp.h

  7: /* Nasty global variable to access EPS data from TRLan_ */
  8: static EPS globaleps;

 12: PetscErrorCode EPSSetUp_TRLAN(EPS eps)
 13: {
 15:   PetscInt       n;
 16:   EPS_TRLAN      *tr = (EPS_TRLAN *)eps->data;

 19:   VecGetSize(eps->vec_initial,&n);
 20:   if (eps->ncv) {
 21:     if (eps->ncv<eps->nev) SETERRQ(1,"The value of ncv must be at least nev");
 22:   }
 23:   else eps->ncv = eps->nev;
 24:   if (!eps->max_it) eps->max_it = PetscMax(1000,n);
 25: 
 26:   if (!eps->ishermitian)
 27:     SETERRQ(PETSC_ERR_SUP,"Requested method is only available for Hermitian problems");

 29:   if (eps->isgeneralized)
 30:     SETERRQ(PETSC_ERR_SUP,"Requested method is not available for generalized problems");

 32:   tr->restart = 0;
 33:   VecGetLocalSize(eps->vec_initial,&n);
 34:   tr->maxlan = eps->nev+PetscMin(eps->nev,6);
 35:   if (tr->maxlan+1-eps->ncv<=0) tr->lwork = tr->maxlan*(tr->maxlan+10);
 36:   else tr->lwork = n*(tr->maxlan+1-eps->ncv) + tr->maxlan*(tr->maxlan+10);
 37:   PetscMalloc(tr->lwork*sizeof(PetscReal),&tr->work);

 39:   EPSAllocateSolutionContiguous(eps);
 40:   return(0);
 41: }

 45: static int MatMult_TRLAN(int *n,int *m,PetscReal *xin,int *ldx,PetscReal *yout,int *ldy)
 46: {
 48:   Vec            x,y;
 49:   int            i;

 52:   VecCreateMPIWithArray(globaleps->comm,*n,PETSC_DECIDE,PETSC_NULL,&x);
 53:   VecCreateMPIWithArray(globaleps->comm,*n,PETSC_DECIDE,PETSC_NULL,&y);
 54:   for (i=0;i<*m;i++) {
 55:     VecPlaceArray(x,(PetscScalar*)xin+i*(*ldx));
 56:     VecPlaceArray(y,(PetscScalar*)yout+i*(*ldy));
 57:     STApply(globaleps->OP,x,y);
 58:     EPSOrthogonalize(globaleps,globaleps->nds,PETSC_NULL,globaleps->DS,y,PETSC_NULL,PETSC_NULL,PETSC_NULL);
 59:     VecResetArray(x);
 60:     VecResetArray(y);
 61:   }
 62:   VecDestroy(x);
 63:   VecDestroy(y);
 64:   return(0);
 65: }

 69: PetscErrorCode EPSSolve_TRLAN(EPS eps)
 70: {
 72:   PetscInt       nn;
 73:   int            ipar[32], i, n, lohi, stat;
 74:   EPS_TRLAN      *tr = (EPS_TRLAN *)eps->data;
 75:   PetscScalar    *pV;
 76: 

 79:   VecGetLocalSize(eps->vec_initial,&nn);
 80:   n = nn;
 81: 
 82:   if (eps->which==EPS_LARGEST_REAL) lohi = 1;
 83:   else if (eps->which==EPS_SMALLEST_REAL) lohi = -1;
 84:   else SETERRQ(1,"Wrong value of eps->which");

 86:   globaleps = eps;

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

104:   for (i=0;i<eps->ncv;i++) eps->eigr[i]=0.0;
105:   EPSGetStartVector(eps,0,eps->V[0],PETSC_NULL);
106:   VecGetArray(eps->V[0],&pV);

108:   TRLan_ ( MatMult_TRLAN, ipar, &n, &eps->ncv, eps->eigr, pV, &n, tr->work, &tr->lwork );

110:   VecRestoreArray( eps->V[0], &pV );

112:   stat        = ipar[0];
113:   eps->nconv  = ipar[3];
114:   eps->its    = ipar[25];
115:   eps->reason = EPS_CONVERGED_TOL;
116: 
117:   if (stat!=0) { SETERRQ1(PETSC_ERR_LIB,"Error in TRLAN (code=%d)",stat);}

119:   return(0);
120: }

124: PetscErrorCode EPSDestroy_TRLAN(EPS eps)
125: {
127:   EPS_TRLAN      *tr = (EPS_TRLAN *)eps->data;

131:   PetscFree(tr->work);
132:   PetscFree(eps->data);
133:   EPSFreeSolutionContiguous(eps);
134:   return(0);
135: }

140: PetscErrorCode EPSCreate_TRLAN(EPS eps)
141: {
143:   EPS_TRLAN      *trlan;

146:   PetscNew(EPS_TRLAN,&trlan);
147:   PetscLogObjectMemory(eps,sizeof(EPS_TRLAN));
148:   eps->data                      = (void *) trlan;
149:   eps->ops->solve                = EPSSolve_TRLAN;
150:   eps->ops->setup                = EPSSetUp_TRLAN;
151:   eps->ops->destroy              = EPSDestroy_TRLAN;
152:   eps->ops->backtransform        = EPSBackTransform_Default;
153:   eps->ops->computevectors       = EPSComputeVectors_Default;
154:   eps->which = EPS_LARGEST_REAL;
155:   return(0);
156: }