Actual source code: ex2.c

slepc-3.20.1 2023-11-27
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));
 42:   PetscCall(MatSetUp(A));

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

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

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

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

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

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

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

 81:   PetscCall(EPSSolve(eps));

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

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

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

110: /*TEST

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

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

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

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

190: TEST*/