Date: 2023-09-05T10:18:05+00:00

Actual source code: ex43.c
slepc-3.19.2 2023-09-05
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 eigenproblem, illustrates setting MUMPS options.\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\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\n";

     19: #include <slepceps.h>

     21: int main(int argc,char **argv)
     22: {
     23:   Mat            A,B;
     24: #if defined(PETSC_HAVE_MUMPS)
     25:   Mat            K;
     26: #endif
     27:   EPS            eps;
     28:   EPSType        type;
     29:   ST             st;
     30:   KSP            ksp;
     31:   PC             pc;
     32:   PetscInt       N,n=10,m=12,Istart,Iend,II,nev,i,j;
     33:   PetscBool      flag,terse;

     35:   PetscFunctionBeginUser;
     36:   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));

     38:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
     39:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag));
     40:   N = n*m;
     41:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nGeneralized Eigenproblem, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m));

     43:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     44:      Compute the matrices that define the eigensystem, Ax=kBx
     45:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     47:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
     48:   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N));
     49:   PetscCall(MatSetFromOptions(A));
     50:   PetscCall(MatSetUp(A));

     52:   PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
     53:   PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,N,N));
     54:   PetscCall(MatSetFromOptions(B));
     55:   PetscCall(MatSetUp(B));

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

     68:   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
     69:   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
     70:   PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
     71:   PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));

     73:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     74:                 Create the eigensolver and set various options
     75:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     77:   /*
     78:      Create eigensolver context
     79:   */
     80:   PetscCall(EPSCreate(PETSC_COMM_WORLD,&eps));

     82:   /*
     83:      Set operators. In this case, it is a generalized eigenvalue problem
     84:   */
     85:   PetscCall(EPSSetOperators(eps,A,B));
     86:   PetscCall(EPSSetProblemType(eps,EPS_GNHEP));

     88:   /*
     89:      Set some solver options
     90:   */
     91:   PetscCall(EPSSetTarget(eps,1.3));
     92:   PetscCall(EPSSetDimensions(eps,2,PETSC_DEFAULT,PETSC_DEFAULT));
     93:   PetscCall(EPSGetST(eps,&st));
     94:   PetscCall(STSetType(st,STSINVERT));

     96:   PetscCall(STGetKSP(st,&ksp));
     97:   PetscCall(KSPSetType(ksp,KSPPREONLY));
     98:   PetscCall(KSPGetPC(ksp,&pc));
     99:   PetscCall(PCSetType(pc,PCLU));

    101:   /*
    102:      Set MUMPS options if available
    103:   */
    104: #if defined(PETSC_HAVE_MUMPS)
    105:   PetscCall(PCFactorSetMatSolverType(pc,MATSOLVERMUMPS));
    106:   /* the next line is required to force the creation of the ST operator and its passing to KSP */
    107:   PetscCall(STGetOperator(st,NULL));
    108:   PetscCall(PCFactorSetUpMatSolverType(pc));
    109:   PetscCall(PCFactorGetMatrix(pc,&K));
    110:   PetscCall(MatMumpsSetIcntl(K,14,50));
    111:   PetscCall(MatMumpsSetCntl(K,3,1e-12));
    112: #endif

    114:   /*
    115:      Let the user change settings at runtime
    116:   */
    117:   PetscCall(EPSSetFromOptions(eps));

    119:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    120:                       Solve the eigensystem
    121:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    123:   PetscCall(EPSSolve(eps));

    125:   /*
    126:      Optional: Get some information from the solver and display it
    127:   */
    128:   PetscCall(EPSGetType(eps,&type));
    129:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type));
    130:   PetscCall(EPSGetDimensions(eps,&nev,NULL,NULL));
    131:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev));

    133:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    134:                     Display solution and clean up
    135:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    137:   /* show detailed info unless -terse option is given by user */
    138:   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
    139:   if (terse) PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
    140:   else {
    141:     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
    142:     PetscCall(EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD));
    143:     PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
    144:     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
    145:   }
    146:   PetscCall(EPSDestroy(&eps));
    147:   PetscCall(MatDestroy(&A));
    148:   PetscCall(MatDestroy(&B));
    149:   PetscCall(SlepcFinalize());
    150:   return 0;
    151: }

    153: /*TEST

    155:    testset:
    156:       args: -terse
    157:       output_file: output/ex43_1.out
    158:       test:
    159:          suffix: 1
    160:       test:
    161:          suffix: 2
    162:          nsize: 2
    163:          args: -st_pc_factor_mat_solver_type mumps
    164:          requires: mumps

    166: TEST*/