Actual source code: elemental.cxx

slepc-3.22.2 2024-12-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 eigensolvers in Elemental.
 12: */

 14: #include <slepc/private/epsimpl.h>
 15: #include <petsc/private/petscelemental.h>

 17: typedef struct {
 18:   Mat Ae,Be;        /* converted matrices */
 19: } EPS_Elemental;

 21: static PetscErrorCode EPSSetUp_Elemental(EPS eps)
 22: {
 23:   EPS_Elemental  *ctx = (EPS_Elemental*)eps->data;
 24:   Mat            A,B;
 25:   PetscInt       nmat;
 26:   PetscBool      isshift;
 27:   PetscScalar    shift;

 29:   PetscFunctionBegin;
 30:   EPSCheckHermitianDefinite(eps);
 31:   EPSCheckNotStructured(eps);
 32:   PetscCall(PetscObjectTypeCompare((PetscObject)eps->st,STSHIFT,&isshift));
 33:   PetscCheck(isshift,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver does not support spectral transformations");
 34:   eps->ncv = eps->n;
 35:   if (eps->mpd!=PETSC_DETERMINE) PetscCall(PetscInfo(eps,"Warning: parameter mpd ignored\n"));
 36:   if (eps->max_it==PETSC_DETERMINE) eps->max_it = 1;
 37:   if (!eps->which) PetscCall(EPSSetWhichEigenpairs_Default(eps));
 38:   PetscCheck(eps->which!=EPS_ALL || eps->inta==eps->intb,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver does not support interval computation");
 39:   EPSCheckUnsupported(eps,EPS_FEATURE_BALANCE | EPS_FEATURE_ARBITRARY | EPS_FEATURE_REGION | EPS_FEATURE_STOPPING);
 40:   EPSCheckIgnored(eps,EPS_FEATURE_EXTRACTION | EPS_FEATURE_CONVERGENCE);
 41:   PetscCall(EPSAllocateSolution(eps,0));

 43:   /* convert matrices */
 44:   PetscCall(MatDestroy(&ctx->Ae));
 45:   PetscCall(MatDestroy(&ctx->Be));
 46:   PetscCall(STGetNumMatrices(eps->st,&nmat));
 47:   PetscCall(STGetMatrix(eps->st,0,&A));
 48:   PetscCall(MatConvert(A,MATELEMENTAL,MAT_INITIAL_MATRIX,&ctx->Ae));
 49:   if (nmat>1) {
 50:     PetscCall(STGetMatrix(eps->st,1,&B));
 51:     PetscCall(MatConvert(B,MATELEMENTAL,MAT_INITIAL_MATRIX,&ctx->Be));
 52:   }
 53:   PetscCall(STGetShift(eps->st,&shift));
 54:   if (shift != 0.0) {
 55:     if (nmat>1) PetscCall(MatAXPY(ctx->Ae,-shift,ctx->Be,SAME_NONZERO_PATTERN));
 56:     else PetscCall(MatShift(ctx->Ae,-shift));
 57:   }
 58:   PetscFunctionReturn(PETSC_SUCCESS);
 59: }

 61: static PetscErrorCode EPSSolve_Elemental(EPS eps)
 62: {
 63:   EPS_Elemental  *ctx = (EPS_Elemental*)eps->data;
 64:   Mat            A = ctx->Ae,B = ctx->Be,Q,V;
 65:   Mat_Elemental  *a = (Mat_Elemental*)A->data,*b,*q;
 66:   PetscInt       i,rrank,ridx,erow;

 68:   PetscFunctionBegin;
 69:   El::DistMatrix<PetscReal,El::VR,El::STAR> w(*a->grid);
 70:   PetscCall(MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&Q));
 71:   q = (Mat_Elemental*)Q->data;

 73:   if (B) {
 74:     b = (Mat_Elemental*)B->data;
 75:     El::HermitianGenDefEig(El::AXBX,El::LOWER,*a->emat,*b->emat,w,*q->emat);
 76:   } else El::HermitianEig(El::LOWER,*a->emat,w,*q->emat);

 78:   for (i=0;i<eps->ncv;i++) {
 79:     P2RO(A,0,i,&rrank,&ridx);
 80:     RO2E(A,0,rrank,ridx,&erow);
 81:     eps->eigr[i] = w.Get(erow,0);
 82:   }
 83:   PetscCall(BVGetMat(eps->V,&V));
 84:   PetscCall(MatConvert(Q,MATDENSE,MAT_REUSE_MATRIX,&V));
 85:   PetscCall(BVRestoreMat(eps->V,&V));
 86:   PetscCall(MatDestroy(&Q));

 88:   eps->nconv  = eps->ncv;
 89:   eps->its    = 1;
 90:   eps->reason = EPS_CONVERGED_TOL;
 91:   PetscFunctionReturn(PETSC_SUCCESS);
 92: }

 94: static PetscErrorCode EPSDestroy_Elemental(EPS eps)
 95: {
 96:   PetscFunctionBegin;
 97:   PetscCall(PetscFree(eps->data));
 98:   PetscFunctionReturn(PETSC_SUCCESS);
 99: }

101: static PetscErrorCode EPSReset_Elemental(EPS eps)
102: {
103:   EPS_Elemental  *ctx = (EPS_Elemental*)eps->data;

105:   PetscFunctionBegin;
106:   PetscCall(MatDestroy(&ctx->Ae));
107:   PetscCall(MatDestroy(&ctx->Be));
108:   PetscFunctionReturn(PETSC_SUCCESS);
109: }

111: SLEPC_EXTERN PetscErrorCode EPSCreate_Elemental(EPS eps)
112: {
113:   EPS_Elemental  *ctx;

115:   PetscFunctionBegin;
116:   PetscCall(PetscNew(&ctx));
117:   eps->data = (void*)ctx;

119:   eps->categ = EPS_CATEGORY_OTHER;

121:   eps->ops->solve          = EPSSolve_Elemental;
122:   eps->ops->setup          = EPSSetUp_Elemental;
123:   eps->ops->setupsort      = EPSSetUpSort_Basic;
124:   eps->ops->destroy        = EPSDestroy_Elemental;
125:   eps->ops->reset          = EPSReset_Elemental;
126:   eps->ops->backtransform  = EPSBackTransform_Default;
127:   eps->ops->setdefaultst   = EPSSetDefaultST_NoFactor;
128:   PetscFunctionReturn(PETSC_SUCCESS);
129: }