Actual source code: ex2.c
slepc-3.21.1 2024-04-26
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[] = "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:   PetscInt       N,n=10,m,Istart,Iend,II,nev,i,j;
     24:   PetscBool      flag,terse;

     26:   PetscFunctionBeginUser;
     27:   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));

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

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

     39:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
     40:   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N));
     41:   PetscCall(MatSetFromOptions(A));

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

     53:   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
     54:   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));

     56:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     57:                 Create the eigensolver and set various options
     58:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

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

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

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

     76:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     77:                       Solve the eigensystem
     78:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     80:   PetscCall(EPSSolve(eps));

     82:   /*
     83:      Optional: Get some information from the solver and display it
     84:   */
     85:   PetscCall(EPSGetType(eps,&type));
     86:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type));
     87:   PetscCall(EPSGetDimensions(eps,&nev,NULL,NULL));
     88:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev));

     90:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     91:                     Display solution and clean up
     92:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     94:   /* show detailed info unless -terse option is given by user */
     95:   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
     96:   if (terse) PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
     97:   else {
     98:     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
     99:     PetscCall(EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD));
    100:     PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
    101:     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
    102:   }
    103:   PetscCall(EPSDestroy(&eps));
    104:   PetscCall(MatDestroy(&A));
    105:   PetscCall(SlepcFinalize());
    106:   return 0;
    107: }

    109: /*TEST

    111:    testset:
    112:       args: -n 72 -eps_nev 4 -eps_ncv 20 -terse
    113:       output_file: output/ex2_1.out
    114:       requires: !single
    115:       test:
    116:          suffix: 1
    117:       test:
    118:          suffix: 2
    119:          args: -library_preload

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

    154:    testset:
    155:       args: -n 30 -m 30 -eps_interval 3.9,4.15 -terse
    156:       output_file: output/ex2_3.out
    157:       filter: grep -v Solution
    158:       requires: !single
    159:       test:
    160:          suffix: 3
    161:          args: -st_type sinvert -st_pc_type cholesky
    162:       test:
    163:          suffix: 3_evsl
    164:          args: -eps_type evsl -eps_evsl_slices 6
    165:          requires: evsl

    167:    testset:
    168:       args: -n 45 -m 46 -eps_interval 4.54,4.57 -eps_ncv 24 -terse
    169:       output_file: output/ex2_4.out
    170:       filter: grep -v Solution
    171:       requires: !single
    172:       timeoutfactor: 2
    173:       test:
    174:          suffix: 4
    175:          args: -st_type sinvert -st_pc_type cholesky
    176:       test:
    177:          suffix: 4_filter
    178:          args: -eps_type {{krylovschur subspace}} -st_type filter -st_filter_degree 200
    179:          requires: !__float128
    180:       test:
    181:          suffix: 4_filter_cuda
    182:          args: -eps_type {{krylovschur subspace}} -st_type filter -st_filter_degree 200 -mat_type aijcusparse
    183:          requires: cuda
    184:       test:
    185:          suffix: 4_evsl
    186:          args: -eps_type evsl
    187:          requires: evsl

    189: TEST*/