Date: 2022-08-09T09:05:50+00:00

Actual source code: test4.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[] = "Solve a quadratic problem with PEPLINEAR with a user-provided EPS.\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 <slepcpep.h>

     18: int main(int argc,char **argv)
     19: {
     20:   Mat            M,C,K,A[3];
     21:   PEP            pep;
     22:   PetscInt       N,n=10,m,Istart,Iend,II,i,j;
     23:   PetscBool      flag,expmat;
     24:   PetscReal      alpha,beta;
     25:   EPS            eps;
     26:   ST             st;
     27:   KSP            ksp;
     28:   PC             pc;

     30:   SlepcInitialize(&argc,&argv,(char*)0,help);
     31:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
     32:   PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
     33:   if (!flag) m=n;
     34:   N = n*m;
     35:   PetscPrintf(PETSC_COMM_WORLD,"\nQuadratic Eigenproblem, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m);

     37:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     38:      Compute the matrices that define the eigensystem, (k^2*M+k*C+K)x=0
     39:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     41:   /* K is the 2-D Laplacian */
     42:   MatCreate(PETSC_COMM_WORLD,&K);
     43:   MatSetSizes(K,PETSC_DECIDE,PETSC_DECIDE,N,N);
     44:   MatSetFromOptions(K);
     45:   MatSetUp(K);
     46:   MatGetOwnershipRange(K,&Istart,&Iend);
     47:   for (II=Istart;II<Iend;II++) {
     48:     i = II/n; j = II-i*n;
     49:     if (i>0) MatSetValue(K,II,II-n,-1.0,INSERT_VALUES);
     50:     if (i<m-1) MatSetValue(K,II,II+n,-1.0,INSERT_VALUES);
     51:     if (j>0) MatSetValue(K,II,II-1,-1.0,INSERT_VALUES);
     52:     if (j<n-1) MatSetValue(K,II,II+1,-1.0,INSERT_VALUES);
     53:     MatSetValue(K,II,II,4.0,INSERT_VALUES);
     54:   }
     55:   MatAssemblyBegin(K,MAT_FINAL_ASSEMBLY);
     56:   MatAssemblyEnd(K,MAT_FINAL_ASSEMBLY);

     58:   /* C is the 1-D Laplacian on horizontal lines */
     59:   MatCreate(PETSC_COMM_WORLD,&C);
     60:   MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,N,N);
     61:   MatSetFromOptions(C);
     62:   MatSetUp(C);
     63:   MatGetOwnershipRange(C,&Istart,&Iend);
     64:   for (II=Istart;II<Iend;II++) {
     65:     i = II/n; j = II-i*n;
     66:     if (j>0) MatSetValue(C,II,II-1,-1.0,INSERT_VALUES);
     67:     if (j<n-1) MatSetValue(C,II,II+1,-1.0,INSERT_VALUES);
     68:     MatSetValue(C,II,II,2.0,INSERT_VALUES);
     69:   }
     70:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
     71:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

     73:   /* M is a diagonal matrix */
     74:   MatCreate(PETSC_COMM_WORLD,&M);
     75:   MatSetSizes(M,PETSC_DECIDE,PETSC_DECIDE,N,N);
     76:   MatSetFromOptions(M);
     77:   MatSetUp(M);
     78:   MatGetOwnershipRange(M,&Istart,&Iend);
     79:   for (II=Istart;II<Iend;II++) MatSetValue(M,II,II,(PetscReal)(II+1),INSERT_VALUES);
     80:   MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
     81:   MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);

     83:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     84:              Create a standalone EPS with appropriate settings
     85:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     87:   EPSCreate(PETSC_COMM_WORLD,&eps);
     88:   EPSSetWhichEigenpairs(eps,EPS_TARGET_MAGNITUDE);
     89: #if defined(PETSC_USE_COMPLEX)
     90:   EPSSetTarget(eps,0.01*PETSC_i);
     91: #endif
     92:   EPSGetST(eps,&st);
     93:   STSetType(st,STSINVERT);
     94:   STGetKSP(st,&ksp);
     95:   KSPSetType(ksp,KSPBCGS);
     96:   KSPGetPC(ksp,&pc);
     97:   PCSetType(pc,PCJACOBI);
     98:   EPSSetFromOptions(eps);

    100:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    101:              Create the eigensolver and solve the eigensystem
    102:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    104:   PEPCreate(PETSC_COMM_WORLD,&pep);
    105:   PetscObjectSetName((PetscObject)pep,"PEP_solver");
    106:   A[0] = K; A[1] = C; A[2] = M;
    107:   PEPSetOperators(pep,3,A);
    108:   PEPSetType(pep,PEPLINEAR);
    109:   PEPSetProblemType(pep,PEP_GENERAL);
    110:   PEPLinearSetEPS(pep,eps);
    111:   PEPSetFromOptions(pep);
    112:   PEPSolve(pep);
    113:   PEPLinearGetLinearization(pep,&alpha,&beta);
    114:   PetscPrintf(PETSC_COMM_WORLD," Linearization with alpha=%g, beta=%g",(double)alpha,(double)beta);
    115:   PEPLinearGetExplicitMatrix(pep,&expmat);
    116:   if (expmat) PetscPrintf(PETSC_COMM_WORLD," with explicit matrix");
    117:   PetscPrintf(PETSC_COMM_WORLD,"\n");

    119:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    120:                     Display solution and clean up
    121:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    123:   PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL);
    124:   PEPDestroy(&pep);
    125:   EPSDestroy(&eps);
    126:   MatDestroy(&M);
    127:   MatDestroy(&C);
    128:   MatDestroy(&K);
    129:   SlepcFinalize();
    130:   return 0;
    131: }

    133: /*TEST

    135:    testset:
    136:       args: -pep_linear_explicitmatrix -pep_view_vectors ::ascii_info
    137:       test:
    138:          suffix: 1_real
    139:          requires: !single !complex
    140:       test:
    141:          suffix: 1
    142:          requires: !single complex

    144: TEST*/