Date: 2026-03-09T09:12:28+00:00

Actual source code: test2.c

      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[] = "Tests multiple calls to EPSSolve with the same matrix.\n\n";

     13: #include <slepceps.h>

     15: int main(int argc,char **argv)
     16: {
     17:   Mat            A;           /* problem matrix */
     18:   EPS            eps;         /* eigenproblem solver context */
     19:   ST             st;
     20:   PetscReal      tol=PetscMax(1000*PETSC_MACHINE_EPSILON,1e-9);
     21:   PetscInt       n=30,i,Istart,Iend;
     22:   PetscBool      flg;
     23:   EPSLanczosReorthogType reorth;

     25:   PetscFunctionBeginUser;
     26:   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));

     28:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
     29:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n1-D Laplacian Eigenproblem, n=%" PetscInt_FMT "\n\n",n));

     31:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     32:      Compute the operator matrix that defines the eigensystem, Ax=kx
     33:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     35:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
     36:   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n));
     37:   PetscCall(MatSetFromOptions(A));

     39:   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
     40:   for (i=Istart;i<Iend;i++) {
     41:     if (i>0) PetscCall(MatSetValue(A,i,i-1,-1.0,INSERT_VALUES));
     42:     if (i<n-1) PetscCall(MatSetValue(A,i,i+1,-1.0,INSERT_VALUES));
     43:     PetscCall(MatSetValue(A,i,i,2.0,INSERT_VALUES));
     44:   }
     45:   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
     46:   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));

     48:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     49:                         Create the eigensolver
     50:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     51:   PetscCall(EPSCreate(PETSC_COMM_WORLD,&eps));
     52:   PetscCall(EPSSetOperators(eps,A,NULL));
     53:   PetscCall(EPSSetProblemType(eps,EPS_HEP));
     54:   PetscCall(EPSSetTolerances(eps,tol,PETSC_CURRENT));
     55:   PetscCall(EPSSetFromOptions(eps));

     57:   /* illustrate how to extract parameters from specific solver types */
     58:   PetscCall(PetscObjectTypeCompare((PetscObject)eps,EPSLANCZOS,&flg));
     59:   if (flg) {
     60:     PetscCall(EPSLanczosGetReorthog(eps,&reorth));
     61:     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Reorthogonalization type used in Lanczos: %s\n",EPSLanczosReorthogTypes[reorth]));
     62:   }

     64:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     65:                     Solve for largest eigenvalues
     66:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     67:   PetscCall(EPSSetWhichEigenpairs(eps,EPS_LARGEST_REAL));
     68:   PetscCall(EPSSolve(eps));
     69:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," - - - Largest eigenvalues - - -\n"));
     70:   PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));

     72:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     73:                     Solve for smallest eigenvalues
     74:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     75:   PetscCall(EPSSetWhichEigenpairs(eps,EPS_SMALLEST_REAL));
     76:   PetscCall(EPSSolve(eps));
     77:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," - - - Smallest eigenvalues - - -\n"));
     78:   PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));

     80:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     81:                     Solve for interior eigenvalues (target=2.1)
     82:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     83:   PetscCall(EPSSetWhichEigenpairs(eps,EPS_TARGET_MAGNITUDE));
     84:   PetscCall(EPSSetTarget(eps,2.1));
     85:   PetscCall(PetscObjectTypeCompare((PetscObject)eps,EPSLANCZOS,&flg));
     86:   if (flg) {
     87:     PetscCall(EPSGetST(eps,&st));
     88:     PetscCall(STSetType(st,STSINVERT));
     89:   } else {
     90:     PetscCall(PetscObjectTypeCompare((PetscObject)eps,EPSKRYLOVSCHUR,&flg));
     91:     if (!flg) PetscCall(PetscObjectTypeCompare((PetscObject)eps,EPSARNOLDI,&flg));
     92:     if (flg) PetscCall(EPSSetExtraction(eps,EPS_HARMONIC));
     93:   }
     94:   PetscCall(EPSSolve(eps));
     95:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," - - - Interior eigenvalues - - -\n"));
     96:   PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));

     98:   PetscCall(EPSDestroy(&eps));
     99:   PetscCall(MatDestroy(&A));
    100:   PetscCall(SlepcFinalize());
    101:   return 0;
    102: }

    104: /*TEST

    106:    testset:
    107:       args: -eps_nev 4
    108:       requires: !single
    109:       output_file: output/test2_1.out
    110:       test:
    111:          suffix: 1
    112:          args: -eps_type {{arnoldi gd jd lapack}}
    113:       test:
    114:          suffix: 1_gd2
    115:          args: -eps_type gd -eps_gd_double_expansion
    116:          timeoutfactor: 2
    117:       test:
    118:          suffix: 1_krylovschur
    119:          args: -eps_type krylovschur -eps_krylovschur_locking {{0 1}}
    120:       test:
    121:          suffix: 1_scalapack
    122:          requires: scalapack
    123:          args: -eps_type scalapack
    124:       test:
    125:          suffix: 1_elpa
    126:          requires: elpa
    127:          args: -eps_type elpa
    128:       test:
    129:          suffix: 1_elemental
    130:          requires: elemental
    131:          args: -eps_type elemental

    133:    testset:
    134:       args: -eps_type lanczos -eps_nev 4
    135:       requires: !single
    136:       filter: grep -v "Lanczos"
    137:       output_file: output/test2_1.out
    138:       test:
    139:          suffix: 2
    140:          args: -eps_lanczos_reorthog {{local full periodic partial}}
    141:       test:
    142:          suffix: 2_selective
    143:          args: -eps_lanczos_reorthog selective
    144:          requires: !defined(PETSCTEST_VALGRIND)

    146:    testset:
    147:       args: -n 32 -eps_nev 4
    148:       requires: !single
    149:       output_file: output/test2_3.out
    150:       test:
    151:          nsize: 2
    152:          suffix: 3
    153:          args: -eps_type {{krylovschur lapack}}
    154:       test:
    155:          nsize: 2
    156:          suffix: 3_gd
    157:          args: -eps_type gd -eps_gd_krylov_start
    158:          timeoutfactor: 2
    159:       test:
    160:          suffix: 3_jd
    161:          args: -eps_type jd -eps_jd_krylov_start -eps_ncv 18

    163:    testset:
    164:       args: -eps_nev 4 -eps_type {{krylovschur arnoldi gd jd}}
    165:       requires: !single
    166:       output_file: output/test2_1.out
    167:       test:
    168:          suffix: 4_cuda
    169:          requires: cuda
    170:          args: -mat_type aijcusparse
    171:       test:
    172:          suffix: 4_hip
    173:          requires: hip
    174:          args: -mat_type aijhipsparse

    176: TEST*/