Date: 2022-10-01T06:40:06+00:00

Actual source code: ex46.c
slepc-3.18.0 2022-10-01
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;

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

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

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

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

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

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

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

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

     87:   EPSSolve(eps);

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

    105: /*TEST

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

    117: TEST*/