Date: 2023-09-05T10:17:47+00:00

Actual source code: elemental.cxx
slepc-3.19.2 2023-09-05
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: 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:   PetscCall(PetscObjectTypeCompare((PetscObject)eps->st,STSHIFT,&isshift));
     32:   PetscCheck(isshift,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver does not support spectral transformations");
     33:   eps->ncv = eps->n;
     34:   if (eps->mpd!=PETSC_DEFAULT) PetscCall(PetscInfo(eps,"Warning: parameter mpd ignored\n"));
     35:   if (eps->max_it==PETSC_DEFAULT) eps->max_it = 1;
     36:   if (!eps->which) PetscCall(EPSSetWhichEigenpairs_Default(eps));
     37:   PetscCheck(eps->which!=EPS_ALL || eps->inta==eps->intb,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver does not support interval computation");
     38:   EPSCheckUnsupported(eps,EPS_FEATURE_BALANCE | EPS_FEATURE_ARBITRARY | EPS_FEATURE_REGION | EPS_FEATURE_STOPPING);
     39:   EPSCheckIgnored(eps,EPS_FEATURE_EXTRACTION | EPS_FEATURE_CONVERGENCE);
     40:   PetscCall(EPSAllocateSolution(eps,0));

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

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

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

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

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

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

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

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

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

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

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

    118:   eps->categ = EPS_CATEGORY_OTHER;

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