Date: 2026-05-01T10:02:37+00:00

Actual source code: ex2.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[] = "Standard symmetric eigenproblem corresponding to the Laplacian operator in 2 dimensions.\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;               /* operator matrix */
     21:   EPS            eps;             /* eigenproblem solver context */
     22:   EPSType        type;
     23:   EPSStop        stop;
     24:   PetscReal      thres;
     25:   PetscInt       N,n=10,m,Istart,Iend,II,nev,i,j;
     26:   PetscBool      flag,terse;

     28:   PetscFunctionBeginUser;
     29:   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));

     31:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
     32:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag));
     33:   if (!flag) m=n;
     34:   N = n*m;
     35:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n2-D Laplacian Eigenproblem, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m));

     37:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     38:      Compute the operator matrix that defines the eigensystem, Ax=kx
     39:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     41:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
     42:   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N));
     43:   PetscCall(MatSetFromOptions(A));

     45:   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
     46:   for (II=Istart;II<Iend;II++) {
     47:     i = II/n; j = II-i*n;
     48:     if (i>0) PetscCall(MatSetValue(A,II,II-n,-1.0,INSERT_VALUES));
     49:     if (i<m-1) PetscCall(MatSetValue(A,II,II+n,-1.0,INSERT_VALUES));
     50:     if (j>0) PetscCall(MatSetValue(A,II,II-1,-1.0,INSERT_VALUES));
     51:     if (j<n-1) PetscCall(MatSetValue(A,II,II+1,-1.0,INSERT_VALUES));
     52:     PetscCall(MatSetValue(A,II,II,4.0,INSERT_VALUES));
     53:   }

     55:   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
     56:   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));

     58:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     59:                 Create the eigensolver and set various options
     60:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     62:   /*
     63:      Create eigensolver context
     64:   */
     65:   PetscCall(EPSCreate(PETSC_COMM_WORLD,&eps));

     67:   /*
     68:      Set operators. In this case, it is a standard eigenvalue problem
     69:   */
     70:   PetscCall(EPSSetOperators(eps,A,NULL));
     71:   PetscCall(EPSSetProblemType(eps,EPS_HEP));

     73:   /*
     74:      Set solver parameters at runtime
     75:   */
     76:   PetscCall(EPSSetFromOptions(eps));

     78:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     79:                       Solve the eigensystem
     80:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     82:   PetscCall(EPSSolve(eps));

     84:   /*
     85:      Optional: Get some information from the solver and display it
     86:   */
     87:   PetscCall(EPSGetType(eps,&type));
     88:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type));
     89:   PetscCall(EPSGetStoppingTest(eps,&stop));
     90:   if (stop!=EPS_STOP_THRESHOLD) {
     91:     PetscCall(EPSGetDimensions(eps,&nev,NULL,NULL));
     92:     PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev));
     93:   } else {
     94:     PetscCall(EPSGetThreshold(eps,&thres,NULL));
     95:     PetscCall(PetscPrintf(PETSC_COMM_WORLD," Using threshold: %.4g\n",(double)thres));
     96:   }

     98:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     99:                     Display solution and clean up
    100:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    102:   /* show detailed info unless -terse option is given by user */
    103:   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
    104:   if (terse) PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
    105:   else {
    106:     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
    107:     PetscCall(EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD));
    108:     PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
    109:     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
    110:   }
    111:   PetscCall(EPSDestroy(&eps));
    112:   PetscCall(MatDestroy(&A));
    113:   PetscCall(SlepcFinalize());
    114:   return 0;
    115: }

    117: /*TEST

    119:    testset:
    120:       args: -n 72 -eps_nev 4 -eps_ncv 20 -terse
    121:       output_file: output/ex2_1.out
    122:       requires: !single
    123:       test:
    124:          suffix: 1
    125:       test:
    126:          suffix: 2
    127:          requires: defined(PETSC_HAVE_DYNAMIC_LIBRARIES) defined(PETSC_USE_SHARED_LIBRARIES)
    128:          args: -library_preload

    130:    testset:
    131:       args: -n 30 -eps_type ciss -eps_ciss_realmats -terse
    132:       requires: !single
    133:       output_file: output/ex2_ciss.out
    134:       filter: grep -v method
    135:       test:
    136:          suffix: ciss_1
    137:          nsize: 1
    138:          args: -rg_type interval -rg_interval_endpoints 1.1,1.25,-.1,.1
    139:          requires: complex
    140:       test:
    141:          suffix: ciss_1_hpddm
    142:          nsize: 1
    143:          args: -rg_type interval -rg_interval_endpoints 1.1,1.25 -st_ksp_type hpddm
    144:          requires: hpddm
    145:       test:
    146:          suffix: ciss_2
    147:          nsize: 2
    148:          args: -rg_type ellipse -rg_ellipse_center 1.175 -rg_ellipse_radius 0.075 -eps_ciss_partitions 2
    149:       test:
    150:          suffix: ciss_2_block
    151:          args: -rg_type ellipse -rg_ellipse_center 1.175 -rg_ellipse_radius 0.075 -eps_ciss_blocksize 3 -eps_ciss_moments 2
    152:          requires: complex !__float128
    153:       test:
    154:          suffix: ciss_2_hpddm
    155:          nsize: 2
    156:          args: -rg_type ellipse -rg_ellipse_center 1.175 -rg_ellipse_radius 0.075 -eps_ciss_partitions 2 -eps_ciss_ksp_type hpddm
    157:          requires: hpddm
    158:       test:
    159:          suffix: feast
    160:          args: -eps_type feast -eps_interval 1.1,1.25 -eps_ncv 64 -options_left 0
    161:          requires: feast

    163:    testset:
    164:       args: -n 30 -m 30 -eps_interval 3.9,4.15 -terse
    165:       output_file: output/ex2_3.out
    166:       filter: grep -v Solution
    167:       requires: !single
    168:       test:
    169:          suffix: 3
    170:          args: -st_type sinvert -st_pc_type cholesky
    171:       test:
    172:          suffix: 3_evsl
    173:          args: -eps_type evsl -eps_evsl_slices 6
    174:          requires: evsl

    176:    testset:
    177:       args: -n 45 -m 46 -eps_interval 4.54,4.57 -eps_ncv 24 -terse
    178:       output_file: output/ex2_4.out
    179:       filter: grep -v Solution | grep -v "Number of requested" | grep -v "Using threshold"
    180:       requires: !single
    181:       timeoutfactor: 2
    182:       test:
    183:          suffix: 4
    184:          args: -st_type sinvert -st_pc_type cholesky
    185:       test:
    186:          suffix: 4_filter
    187:          args: -eps_type {{krylovschur subspace}} -st_type filter -st_filter_degree 200 -st_filter_type {{filtlan chebyshev}}
    188:          requires: !__float128
    189:       test:
    190:          suffix: 4_filter_cuda
    191:          args: -eps_type {{krylovschur subspace}} -st_type filter -st_filter_degree 200 -mat_type aijcusparse -st_filter_type {{filtlan chebyshev}}
    192:          requires: cuda
    193:       test:
    194:          suffix: 4_filter_hip
    195:          args: -eps_type {{krylovschur subspace}} -st_type filter -st_filter_degree 200 -mat_type aijhipsparse -st_filter_type {{filtlan chebyshev}}
    196:          requires: hip
    197:       test:
    198:          suffix: 4_evsl
    199:          args: -eps_type evsl
    200:          requires: evsl

    202:    test:
    203:       args: -n 25 -m 24 -eps_threshold_absolute .25 -eps_smallest_magnitude -eps_ncv 10 -terse
    204:       suffix: 5
    205:       requires: !single

    207:    testset:
    208:       args: -n 25 -m 24 -st_type sinvert -terse
    209:       requires: double
    210:       test:
    211:          suffix: 6
    212:          args: -eps_threshold_absolute .15 -eps_target 0.01 -eps_type {{krylovschur subspace}}
    213:          filter: grep -v "Solution method"
    214:       test:
    215:          suffix: 6_rel_large
    216:          args: -eps_threshold_relative .98 -eps_target 8
    217:       test:
    218:          suffix: 6_rel_small
    219:          args: -eps_threshold_relative 3

    221: TEST*/