Actual source code: ex13.c

slepc-3.20.2 2024-03-15
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:   PetscFunctionBeginUser;
 31:   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));

 33:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
 34:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag));
 35:   if (!flag) m=n;
 36:   N = n*m;
 37:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-nulldim",&nulldim,NULL));
 38:   PetscCall(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));

 40:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 41:      Compute the matrices that define the eigensystem, Ax=kBx
 42:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 44:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
 45:   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N));
 46:   PetscCall(MatSetFromOptions(A));
 47:   PetscCall(MatSetUp(A));

 49:   PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
 50:   PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,N,N));
 51:   PetscCall(MatSetFromOptions(B));
 52:   PetscCall(MatSetUp(B));

 54:   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
 55:   for (II=Istart;II<Iend;II++) {
 56:     i = II/n; j = II-i*n;
 57:     if (i>0) PetscCall(MatSetValue(A,II,II-n,-1.0,INSERT_VALUES));
 58:     if (i<m-1) PetscCall(MatSetValue(A,II,II+n,-1.0,INSERT_VALUES));
 59:     if (j>0) PetscCall(MatSetValue(A,II,II-1,-1.0,INSERT_VALUES));
 60:     if (j<n-1) PetscCall(MatSetValue(A,II,II+1,-1.0,INSERT_VALUES));
 61:     PetscCall(MatSetValue(A,II,II,4.0,INSERT_VALUES));
 62:     if (II>=nulldim) PetscCall(MatSetValue(B,II,II,4.0,INSERT_VALUES));
 63:   }

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

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

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

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

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

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

 94:   PetscCall(EPSSolve(eps));

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

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

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

124: /*TEST

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

131:    test:
132:       suffix: 2
133:       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
134:       requires: !single

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

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

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

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

157: TEST*/