Actual source code: ex46.c

slepc-3.17.1 2022-04-11
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[] = "Illustrates passing a sparser matrix to build the preconditioner.\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,A0;            /* operator matrix */
 21:   EPS            eps;             /* eigenproblem solver context */
 22:   ST             st;
 23:   PetscInt       N,n=24,m,Istart,Iend,II,i,j;
 24:   PetscBool      flag,terse;

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

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

 34:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 35:        Compute the operator matrix A and a sparser approximation A0
 36:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 38:   MatCreate(PETSC_COMM_WORLD,&A);
 39:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
 40:   MatSetFromOptions(A);
 41:   MatSetUp(A);
 42:   MatGetOwnershipRange(A,&Istart,&Iend);
 43:   for (II=Istart;II<Iend;II++) {
 44:     i = II/n; j = II-i*n;
 45:     if (i>0) MatSetValue(A,II,II-n,-0.2,INSERT_VALUES);
 46:     if (i<m-1) MatSetValue(A,II,II+n,-0.2,INSERT_VALUES);
 47:     if (j>0) MatSetValue(A,II,II-1,-3.0,INSERT_VALUES);
 48:     if (j<n-1) MatSetValue(A,II,II+1,-3.0,INSERT_VALUES);
 49:     MatSetValue(A,II,II,7.0,INSERT_VALUES);
 50:   }
 51:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 52:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 54:   MatCreate(PETSC_COMM_WORLD,&A0);
 55:   MatSetSizes(A0,PETSC_DECIDE,PETSC_DECIDE,N,N);
 56:   MatSetFromOptions(A0);
 57:   MatSetUp(A0);
 58:   MatGetOwnershipRange(A0,&Istart,&Iend);
 59:   for (II=Istart;II<Iend;II++) {
 60:     i = II/n; j = II-i*n;
 61:     if (j>0) MatSetValue(A0,II,II-1,-3.0,INSERT_VALUES);
 62:     if (j<n-1) MatSetValue(A0,II,II+1,-3.0,INSERT_VALUES);
 63:     MatSetValue(A0,II,II,7.0,INSERT_VALUES);
 64:   }
 65:   MatAssemblyBegin(A0,MAT_FINAL_ASSEMBLY);
 66:   MatAssemblyEnd(A0,MAT_FINAL_ASSEMBLY);

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

 72:   EPSCreate(PETSC_COMM_WORLD,&eps);
 73:   EPSSetOperators(eps,A,NULL);
 74:   EPSSetProblemType(eps,EPS_HEP);
 75:   EPSGetST(eps,&st);
 76:   STSetType(st,STSINVERT);
 77:   STSetPreconditionerMat(st,A0);
 78:   EPSSetTarget(eps,0.0);
 79:   EPSSetWhichEigenpairs(eps,EPS_TARGET_MAGNITUDE);
 80:   EPSSetFromOptions(eps);

 82:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 83:                  Solve the eigensystem and display solution
 84:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 86:   EPSSolve(eps);

 88:   /* show detailed info unless -terse option is given by user */
 89:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
 90:   if (terse) EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
 91:   else {
 92:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
 93:     EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
 94:     EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
 95:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
 96:   }
 97:   EPSDestroy(&eps);
 98:   MatDestroy(&A);
 99:   MatDestroy(&A0);
100:   SlepcFinalize();
101:   return 0;
102: }

104: /*TEST

106:    testset:
107:       args: -eps_nev 4 -terse
108:       output_file: output/ex46_1.out
109:       requires: !single
110:       test:
111:          suffix: 1
112:       test:
113:          suffix: 2
114:          args: -st_ksp_type bcgs -st_pc_type bjacobi

116: TEST*/