Date: 2021-02-01T14:42:03+00:00

Actual source code: ex25.c
slepc-3.14.2 2021-02-01
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-2020, 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: */

     11: static char help[] = "Spectrum slicing on generalized symmetric eigenproblem.\n\n"
     12:   "The problem is similar to ex13.c.\n\n"
     13:   "The command line options are:\n"
     14:   "  -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
     15:   "  -m <m>, where <m> = number of grid subdivisions in y dimension.\n";

     17: #include <slepceps.h>

     19: int main(int argc,char **argv)
     20: {
     21:   Mat            A,B;         /* matrices */
     22:   EPS            eps;         /* eigenproblem solver context */
     23:   ST             st;          /* spectral transformation context */
     24:   KSP            ksp;
     25:   PC             pc;
     26:   EPSType        type;
     27:   PetscInt       N,n=10,m,Istart,Iend,II,nev,i,j,*inertias,ns;
     28:   PetscReal      inta,intb,*shifts;
     29:   PetscBool      flag,show=PETSC_FALSE,terse;

     32:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;

     34:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
     35:   PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
     36:   PetscOptionsGetBool(NULL,NULL,"-show_inertias",&show,NULL);
     37:   if (!flag) m=n;
     38:   N = n*m;
     39:   PetscPrintf(PETSC_COMM_WORLD,"\nSpectrum slicing on GHEP, N=%D (%Dx%D grid)\n\n",N,n,m);

     41:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     42:      Compute the matrices that define the eigensystem, Ax=kBx
     43:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     45:   MatCreate(PETSC_COMM_WORLD,&A);
     46:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
     47:   MatSetFromOptions(A);
     48:   MatSetUp(A);

     50:   MatCreate(PETSC_COMM_WORLD,&B);
     51:   MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,N,N);
     52:   MatSetFromOptions(B);
     53:   MatSetUp(B);

     55:   MatGetOwnershipRange(A,&Istart,&Iend);
     56:   for (II=Istart;II<Iend;II++) {
     57:     i = II/n; j = II-i*n;
     58:     if (i>0) { MatSetValue(A,II,II-n,-1.0,INSERT_VALUES); }
     59:     if (i<m-1) { MatSetValue(A,II,II+n,-1.0,INSERT_VALUES); }
     60:     if (j>0) { MatSetValue(A,II,II-1,-1.0,INSERT_VALUES); }
     61:     if (j<n-1) { MatSetValue(A,II,II+1,-1.0,INSERT_VALUES); }
     62:     MatSetValue(A,II,II,4.0,INSERT_VALUES);
     63:     MatSetValue(B,II,II,4.0,INSERT_VALUES);
     64:   }

     66:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
     67:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
     68:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
     69:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

     71:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     72:                 Create the eigensolver and set various options
     73:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     75:   /*
     76:      Create eigensolver context
     77:   */
     78:   EPSCreate(PETSC_COMM_WORLD,&eps);

     80:   /*
     81:      Set operators and set problem type
     82:   */
     83:   EPSSetOperators(eps,A,B);
     84:   EPSSetProblemType(eps,EPS_GHEP);

     86:   /*
     87:      Set interval for spectrum slicing
     88:   */
     89:   inta = 0.1;
     90:   intb = 0.2;
     91:   EPSSetInterval(eps,inta,intb);
     92:   EPSSetWhichEigenpairs(eps,EPS_ALL);

     94:   /*
     95:      Spectrum slicing requires Krylov-Schur
     96:   */
     97:   EPSSetType(eps,EPSKRYLOVSCHUR);

     99:   /*
    100:      Set shift-and-invert with Cholesky; select MUMPS if available
    101:   */

    103:   EPSGetST(eps,&st);
    104:   STSetType(st,STSINVERT);

    106:   STGetKSP(st,&ksp);
    107:   KSPSetType(ksp,KSPPREONLY);
    108:   KSPGetPC(ksp,&pc);
    109:   PCSetType(pc,PCCHOLESKY);

    111:   /*
    112:      Use MUMPS if available.
    113:      Note that in complex scalars we cannot use MUMPS for spectrum slicing,
    114:      because MatGetInertia() is not available in that case.
    115:   */
    116: #if defined(PETSC_HAVE_MUMPS) && !defined(PETSC_USE_COMPLEX)
    117:   EPSKrylovSchurSetDetectZeros(eps,PETSC_TRUE);  /* enforce zero detection */
    118:   PCFactorSetMatSolverType(pc,MATSOLVERMUMPS);
    119:   /*
    120:      Add several MUMPS options (see ex43.c for a better way of setting them in program):
    121:      '-mat_mumps_icntl_13 1': turn off ScaLAPACK for matrix inertia
    122:      '-mat_mumps_icntl_24 1': detect null pivots in factorization (for the case that a shift is equal to an eigenvalue)
    123:      '-mat_mumps_cntl_3 <tol>': a tolerance used for null pivot detection (must be larger than machine epsilon)

    125:      Note: depending on the interval, it may be necessary also to increase the workspace:
    126:      '-mat_mumps_icntl_14 <percentage>': increase workspace with a percentage (50, 100 or more)
    127:   */
    128:   PetscOptionsInsertString(NULL,"-mat_mumps_icntl_13 1 -mat_mumps_icntl_24 1 -mat_mumps_cntl_3 1e-12");
    129: #endif

    131:   /*
    132:      Set solver parameters at runtime
    133:   */
    134:   EPSSetFromOptions(eps);

    136:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    137:                       Solve the eigensystem
    138:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    139:   EPSSetUp(eps);
    140:   if (show) {
    141:     EPSKrylovSchurGetInertias(eps,&ns,&shifts,&inertias);
    142:     PetscPrintf(PETSC_COMM_WORLD,"Subintervals (after setup):\n");
    143:     for (i=0;i<ns;i++) { PetscPrintf(PETSC_COMM_WORLD,"Shift %g  Inertia %D \n",(double)shifts[i],inertias[i]); }
    144:     PetscPrintf(PETSC_COMM_WORLD,"\n");
    145:     PetscFree(shifts);
    146:     PetscFree(inertias);
    147:   }
    148:   EPSSolve(eps);
    149:   if (show) {
    150:     EPSKrylovSchurGetInertias(eps,&ns,&shifts,&inertias);
    151:     PetscPrintf(PETSC_COMM_WORLD,"All shifts (after solve):\n");
    152:     for (i=0;i<ns;i++) { PetscPrintf(PETSC_COMM_WORLD,"Shift %g  Inertia %D \n",(double)shifts[i],inertias[i]); }
    153:     PetscPrintf(PETSC_COMM_WORLD,"\n");
    154:     PetscFree(shifts);
    155:     PetscFree(inertias);
    156:   }

    158:   /*
    159:      Show eigenvalues in interval and print solution
    160:   */
    161:   EPSGetType(eps,&type);
    162:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
    163:   EPSGetDimensions(eps,&nev,NULL,NULL);
    164:   EPSGetInterval(eps,&inta,&intb);
    165:   PetscPrintf(PETSC_COMM_WORLD," %D eigenvalues found in [%g, %g]\n",nev,(double)inta,(double)intb);

    167:   /*
    168:      Show detailed info unless -terse option is given by user
    169:    */
    170:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
    171:   if (terse) {
    172:     EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
    173:   } else {
    174:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
    175:     EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
    176:     EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
    177:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
    178:   }

    180:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    181:                     Clean up
    182:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    183:   EPSDestroy(&eps);
    184:   MatDestroy(&A);
    185:   MatDestroy(&B);
    186:   SlepcFinalize();
    187:   return ierr;
    188: }

    190: /*TEST

    192:    testset:
    193:       args: -terse
    194:       test:
    195:          requires: !mumps
    196:       test:
    197:          requires: mumps !complex

    199: TEST*/