LCOV - code coverage report
Current view: top level - pep/tutorials - ex16.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 66 80 82.5 %
Date: 2024-11-21 00:40:22 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       3             :    SLEPc - Scalable Library for Eigenvalue Problem Computations
       4             :    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
       5             : 
       6             :    This file is part of SLEPc.
       7             :    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
       8             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       9             : */
      10             : 
      11             : static char help[] = "Simple quadratic eigenvalue problem.\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";
      15             : 
      16             : #include <slepcpep.h>
      17             : 
      18           6 : int main(int argc,char **argv)
      19             : {
      20           6 :   Mat            M,C,K,A[3];      /* problem matrices */
      21           6 :   PEP            pep;             /* polynomial eigenproblem solver context */
      22           6 :   PetscInt       N,n=10,m,Istart,Iend,II,nev,i,j,nconv;
      23           6 :   PetscBool      flag,terse;
      24           6 :   PetscReal      error,re,im;
      25           6 :   PetscScalar    kr,ki;
      26           6 :   Vec            xr,xi;
      27           6 :   BV             V;
      28           6 :   PetscRandom    rand;
      29             : 
      30           6 :   PetscFunctionBeginUser;
      31           6 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      32             : 
      33           6 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      34           6 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag));
      35           6 :   if (!flag) m=n;
      36           6 :   N = n*m;
      37           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nQuadratic Eigenproblem, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m));
      38             : 
      39             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      40             :      Compute the matrices that define the eigensystem, (k^2*M+k*C+K)x=0
      41             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      42             : 
      43             :   /* K is the 2-D Laplacian */
      44           6 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&K));
      45           6 :   PetscCall(MatSetSizes(K,PETSC_DECIDE,PETSC_DECIDE,N,N));
      46           6 :   PetscCall(MatSetFromOptions(K));
      47           6 :   PetscCall(MatGetOwnershipRange(K,&Istart,&Iend));
      48         870 :   for (II=Istart;II<Iend;II++) {
      49         864 :     i = II/n; j = II-i*n;
      50         864 :     if (i>0) PetscCall(MatSetValue(K,II,II-n,-1.0,INSERT_VALUES));
      51         864 :     if (i<m-1) PetscCall(MatSetValue(K,II,II+n,-1.0,INSERT_VALUES));
      52         864 :     if (j>0) PetscCall(MatSetValue(K,II,II-1,-1.0,INSERT_VALUES));
      53         864 :     if (j<n-1) PetscCall(MatSetValue(K,II,II+1,-1.0,INSERT_VALUES));
      54         864 :     PetscCall(MatSetValue(K,II,II,4.0,INSERT_VALUES));
      55             :   }
      56           6 :   PetscCall(MatAssemblyBegin(K,MAT_FINAL_ASSEMBLY));
      57           6 :   PetscCall(MatAssemblyEnd(K,MAT_FINAL_ASSEMBLY));
      58             : 
      59             :   /* C is the 1-D Laplacian on horizontal lines */
      60           6 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&C));
      61           6 :   PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,N,N));
      62           6 :   PetscCall(MatSetFromOptions(C));
      63           6 :   PetscCall(MatGetOwnershipRange(C,&Istart,&Iend));
      64         870 :   for (II=Istart;II<Iend;II++) {
      65         864 :     i = II/n; j = II-i*n;
      66         864 :     if (j>0) PetscCall(MatSetValue(C,II,II-1,-1.0,INSERT_VALUES));
      67         864 :     if (j<n-1) PetscCall(MatSetValue(C,II,II+1,-1.0,INSERT_VALUES));
      68         864 :     PetscCall(MatSetValue(C,II,II,2.0,INSERT_VALUES));
      69             :   }
      70           6 :   PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
      71           6 :   PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));
      72             : 
      73             :   /* M is a diagonal matrix */
      74           6 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&M));
      75           6 :   PetscCall(MatSetSizes(M,PETSC_DECIDE,PETSC_DECIDE,N,N));
      76           6 :   PetscCall(MatSetFromOptions(M));
      77           6 :   PetscCall(MatGetOwnershipRange(M,&Istart,&Iend));
      78         870 :   for (II=Istart;II<Iend;II++) PetscCall(MatSetValue(M,II,II,(PetscReal)(II+1),INSERT_VALUES));
      79           6 :   PetscCall(MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY));
      80           6 :   PetscCall(MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY));
      81             : 
      82             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      83             :                 Create the eigensolver and set various options
      84             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      85             : 
      86             :   /*
      87             :      Create eigensolver context
      88             :   */
      89           6 :   PetscCall(PEPCreate(PETSC_COMM_WORLD,&pep));
      90             : 
      91             :   /*
      92             :      Set matrices and problem type
      93             :   */
      94           6 :   A[0] = K; A[1] = C; A[2] = M;
      95           6 :   PetscCall(PEPSetOperators(pep,3,A));
      96           6 :   PetscCall(PEPSetProblemType(pep,PEP_HERMITIAN));
      97             : 
      98             :   /*
      99             :      In complex scalars, use a real initial vector since in this example
     100             :      the matrices are all real, then all vectors generated by the solver
     101             :      will have a zero imaginary part. This is not really necessary.
     102             :   */
     103           6 :   PetscCall(PEPGetBV(pep,&V));
     104           6 :   PetscCall(BVGetRandomContext(V,&rand));
     105           6 :   PetscCall(PetscRandomSetInterval(rand,-1,1));
     106             : 
     107             :   /*
     108             :      Set solver parameters at runtime
     109             :   */
     110           6 :   PetscCall(PEPSetFromOptions(pep));
     111             : 
     112             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     113             :                       Solve the eigensystem
     114             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     115             : 
     116           6 :   PetscCall(PEPSolve(pep));
     117             : 
     118             :   /*
     119             :      Optional: Get some information from the solver and display it
     120             :   */
     121           6 :   PetscCall(PEPGetDimensions(pep,&nev,NULL,NULL));
     122           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev));
     123             : 
     124             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     125             :                     Display solution and clean up
     126             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     127             : 
     128             :   /* show detailed info unless -terse option is given by user */
     129           6 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
     130           6 :   if (terse) PetscCall(PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL));
     131             :   else {
     132           0 :     PetscCall(PEPGetConverged(pep,&nconv));
     133           0 :     if (nconv>0) {
     134           0 :       PetscCall(MatCreateVecs(M,&xr,&xi));
     135             :       /* display eigenvalues and relative errors */
     136           0 :       PetscCall(PetscPrintf(PETSC_COMM_WORLD,
     137             :            "\n           k          ||P(k)x||/||kx||\n"
     138             :            "   ----------------- ------------------\n"));
     139           0 :       for (i=0;i<nconv;i++) {
     140             :         /* get converged eigenpairs */
     141           0 :         PetscCall(PEPGetEigenpair(pep,i,&kr,&ki,xr,xi));
     142             :         /* compute the relative error associated to each eigenpair */
     143           0 :         PetscCall(PEPComputeError(pep,i,PEP_ERROR_BACKWARD,&error));
     144             : #if defined(PETSC_USE_COMPLEX)
     145           0 :         re = PetscRealPart(kr);
     146           0 :         im = PetscImaginaryPart(kr);
     147             : #else
     148             :         re = kr;
     149             :         im = ki;
     150             : #endif
     151           0 :         if (im!=0.0) PetscCall(PetscPrintf(PETSC_COMM_WORLD," %9f%+9fi   %12g\n",(double)re,(double)im,(double)error));
     152           0 :         else PetscCall(PetscPrintf(PETSC_COMM_WORLD,"   %12f       %12g\n",(double)re,(double)error));
     153             :       }
     154           0 :       PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n"));
     155           0 :       PetscCall(VecDestroy(&xr));
     156           0 :       PetscCall(VecDestroy(&xi));
     157             :     }
     158             :   }
     159           6 :   PetscCall(PEPDestroy(&pep));
     160           6 :   PetscCall(MatDestroy(&M));
     161           6 :   PetscCall(MatDestroy(&C));
     162           6 :   PetscCall(MatDestroy(&K));
     163           6 :   PetscCall(SlepcFinalize());
     164             :   return 0;
     165             : }
     166             : 
     167             : /*TEST
     168             : 
     169             :    testset:
     170             :       args: -pep_nev 4 -pep_ncv 21 -n 12 -terse
     171             :       output_file: output/ex16_1.out
     172             :       test:
     173             :          suffix: 1
     174             :          args: -pep_type {{toar qarnoldi}}
     175             :       test:
     176             :          suffix: 1_linear
     177             :          args: -pep_type linear -pep_linear_explicitmatrix
     178             :          requires: !single
     179             :       test:
     180             :          suffix: 1_linear_symm
     181             :          args: -pep_type linear -pep_linear_explicitmatrix -pep_linear_eps_gen_indefinite -pep_scale scalar -pep_linear_bv_definite_tol 1e-12
     182             :          requires: !single
     183             :       test:
     184             :          suffix: 1_stoar
     185             :          args: -pep_type stoar -pep_scale scalar
     186             :          requires: double
     187             :       test:
     188             :          suffix: 1_stoar_t
     189             :          args: -pep_type stoar -pep_scale scalar -st_transform
     190             :          requires: double
     191             : 
     192             : TEST*/

Generated by: LCOV version 1.14