Date: 2022-10-01T06:40:05+00:00

Actual source code: ex12.c
slepc-3.18.0 2022-10-01
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: */

     11: static char help[] = "Compute all eigenvalues in an interval of a symmetric-definite problem.\n\n"
     12:   "The command line options are:\n"
     13:   "  -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
     14:   "  -m <m>, where <m> = number of grid subdivisions in y dimension.\n\n";

     16: #include <slepceps.h>

     18: int main(int argc,char **argv)
     19: {
     20:   Mat            A,B;         /* matrices */
     21:   EPS            eps;         /* eigenproblem solver context */
     22:   ST             st;          /* spectral transformation context */
     23:   KSP            ksp;
     24:   PC             pc;
     25:   PetscInt       N,n=35,m,Istart,Iend,II,nev,i,j,k,*inertias;
     26:   PetscBool      flag,showinertia=PETSC_TRUE;
     27:   PetscReal      int0,int1,*shifts;

     30:   SlepcInitialize(&argc,&argv,(char*)0,help);

     32:   PetscOptionsGetBool(NULL,NULL,"-showinertia",&showinertia,NULL);
     33:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
     34:   PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
     35:   if (!flag) m=n;
     36:   N = n*m;
     37:   PetscPrintf(PETSC_COMM_WORLD,"\nSymmetric-definite problem with two intervals, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m);

     39:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     40:      Compute the matrices that define the eigensystem, Ax=kBx
     41:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

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

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

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

     70:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
     71:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
     72:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
     73:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

     75:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     76:                 Create the eigensolver and set various options
     77:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     79:   EPSCreate(PETSC_COMM_WORLD,&eps);
     80:   EPSSetOperators(eps,A,B);
     81:   EPSSetProblemType(eps,EPS_GHEP);

     83:   /*
     84:      Set first interval and other settings for spectrum slicing
     85:   */
     86:   EPSSetType(eps,EPSKRYLOVSCHUR);
     87:   EPSSetWhichEigenpairs(eps,EPS_ALL);
     88:   EPSSetInterval(eps,1.1,1.3);
     89:   EPSGetST(eps,&st);
     90:   STSetType(st,STSINVERT);
     91:   EPSKrylovSchurGetKSP(eps,&ksp);
     92:   KSPGetPC(ksp,&pc);
     93:   KSPSetType(ksp,KSPPREONLY);
     94:   PCSetType(pc,PCCHOLESKY);
     95:   EPSSetFromOptions(eps);

     97:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     98:                  Solve for first interval and display info
     99:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    101:   EPSSolve(eps);
    102:   EPSGetDimensions(eps,&nev,NULL,NULL);
    103:   EPSGetInterval(eps,&int0,&int1);
    104:   PetscPrintf(PETSC_COMM_WORLD," Found %" PetscInt_FMT " eigenvalues in interval [%g,%g]\n",nev,(double)int0,(double)int1);
    105:   if (showinertia) {
    106:     EPSKrylovSchurGetInertias(eps,&k,&shifts,&inertias);
    107:     PetscPrintf(PETSC_COMM_WORLD," Used %" PetscInt_FMT " shifts (inertia):\n",k);
    108:     for (i=0;i<k;i++) PetscPrintf(PETSC_COMM_WORLD," .. %g (%" PetscInt_FMT ")\n",(double)shifts[i],inertias[i]);
    109:     PetscFree(shifts);
    110:     PetscFree(inertias);
    111:   }

    113:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    114:                  Solve for second interval and display info
    115:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    116:   EPSSetInterval(eps,1.499,1.6);
    117:   EPSSolve(eps);
    118:   EPSGetDimensions(eps,&nev,NULL,NULL);
    119:   EPSGetInterval(eps,&int0,&int1);
    120:   PetscPrintf(PETSC_COMM_WORLD," Found %" PetscInt_FMT " eigenvalues in interval [%g,%g]\n",nev,(double)int0,(double)int1);
    121:   if (showinertia) {
    122:     EPSKrylovSchurGetInertias(eps,&k,&shifts,&inertias);
    123:     PetscPrintf(PETSC_COMM_WORLD," Used %" PetscInt_FMT " shifts (inertia):\n",k);
    124:     for (i=0;i<k;i++) PetscPrintf(PETSC_COMM_WORLD," .. %g (%" PetscInt_FMT ")\n",(double)shifts[i],inertias[i]);
    125:     PetscFree(shifts);
    126:     PetscFree(inertias);
    127:   }

    129:   EPSDestroy(&eps);
    130:   MatDestroy(&A);
    131:   MatDestroy(&B);
    132:   SlepcFinalize();
    133:   return 0;
    134: }

    136: /*TEST

    138:    test:
    139:       suffix: 1
    140:       args: -showinertia 0 -eps_error_relative
    141:       requires: !single

    143: TEST*/