Actual source code: ex13.c

slepc-3.17.2 2022-08-09
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[] = "Generalized Symmetric eigenproblem.\n\n"
 12:   "The problem is Ax = lambda Bx, with:\n"
 13:   "   A = Laplacian operator in 2-D\n"
 14:   "   B = diagonal matrix with all values equal to 4 except nulldim zeros\n\n"
 15:   "The command line options are:\n"
 16:   "  -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
 17:   "  -m <m>, where <m> = number of grid subdivisions in y dimension.\n"
 18:   "  -nulldim <k>, where <k> = dimension of the nullspace of B.\n\n";

 20: #include <slepceps.h>

 22: int main(int argc,char **argv)
 23: {
 24:   Mat            A,B;         /* matrices */
 25:   EPS            eps;         /* eigenproblem solver context */
 26:   EPSType        type;
 27:   PetscInt       N,n=10,m,Istart,Iend,II,nev,i,j,nulldim=0;
 28:   PetscBool      flag,terse;

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

 32:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 33:   PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
 34:   if (!flag) m=n;
 35:   N = n*m;
 36:   PetscOptionsGetInt(NULL,NULL,"-nulldim",&nulldim,NULL);
 37:   PetscPrintf(PETSC_COMM_WORLD,"\nGeneralized Symmetric Eigenproblem, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid), null(B)=%" PetscInt_FMT "\n\n",N,n,m,nulldim);

 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:     if (II>=nulldim) MatSetValue(B,II,II,4.0,INSERT_VALUES);
 62:   }

 64:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 65:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
 66:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
 67:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

 69:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 70:                 Create the eigensolver and set various options
 71:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 73:   /*
 74:      Create eigensolver context
 75:   */
 76:   EPSCreate(PETSC_COMM_WORLD,&eps);

 78:   /*
 79:      Set operators. In this case, it is a generalized eigenvalue problem
 80:   */
 81:   EPSSetOperators(eps,A,B);
 82:   EPSSetProblemType(eps,EPS_GHEP);

 84:   /*
 85:      Set solver parameters at runtime
 86:   */
 87:   EPSSetFromOptions(eps);

 89:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 90:                       Solve the eigensystem
 91:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 93:   EPSSolve(eps);

 95:   /*
 96:      Optional: Get some information from the solver and display it
 97:   */
 98:   EPSGetType(eps,&type);
 99:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
100:   EPSGetDimensions(eps,&nev,NULL,NULL);
101:   PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev);

103:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
104:                     Display solution and clean up
105:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

107:   /* show detailed info unless -terse option is given by user */
108:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
109:   if (terse) EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
110:   else {
111:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
112:     EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
113:     EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
114:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
115:   }
116:   EPSDestroy(&eps);
117:   MatDestroy(&A);
118:   MatDestroy(&B);
119:   SlepcFinalize();
120:   return 0;
121: }

123: /*TEST

125:    test:
126:       suffix: 1
127:       args: -eps_nev 4 -eps_ncv 22 -eps_tol 1e-5 -st_type sinvert -terse
128:       filter: grep -v Solution

130:    test:
131:       suffix: 2
132:       args: -n 110 -nulldim 6 -eps_nev 4 -eps_ncv 18 -eps_tol 1e-5 -eps_purify 1 -st_type sinvert -st_matstructure {{different subset}} -terse
133:       requires: !single

135:    test:
136:       suffix: 3
137:       args: -eps_nev 3 -eps_tol 1e-5 -mat_type sbaij -st_type sinvert -terse

139:    test:
140:       suffix: 4
141:       args: -eps_nev 4 -eps_tol 1e-4 -eps_smallest_real -eps_type {{gd lobpcg rqcg}} -terse
142:       output_file: output/ex13_1.out
143:       filter: grep -v Solution

145:    test:
146:       suffix: 5_primme
147:       args: -n 10 -m 12 -eps_nev 4 -eps_target 0.9 -eps_max_it 15000 -eps_type primme -st_pc_type jacobi -terse
148:       requires: primme defined(SLEPC_HAVE_PRIMME3) !single

150:    test:
151:       suffix: 6
152:       nsize: 2
153:       args: -eps_type ciss -rg_type ellipse -rg_ellipse_center 1.4 -rg_ellipse_radius 0.1 -eps_ciss_partitions 2 -terse
154:       requires: !single

156: TEST*/