LCOV - code coverage report
Current view: top level - pep/tests - test2.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 90 90 100.0 %
Date: 2024-11-23 00:39:48 Functions: 2 2 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             :    Example based on spring problem in NLEVP collection [1]. See the parameters
      12             :    meaning at Example 2 in [2].
      13             : 
      14             :    [1] T. Betcke, N. J. Higham, V. Mehrmann, C. Schroder, and F. Tisseur,
      15             :        NLEVP: A Collection of Nonlinear Eigenvalue Problems, MIMS EPrint
      16             :        2010.98, November 2010.
      17             :    [2] F. Tisseur, Backward error and condition of polynomial eigenvalue
      18             :        problems, Linear Algebra and its Applications, 309 (2000), pp. 339--361,
      19             :        April 2000.
      20             : */
      21             : 
      22             : static char help[] = "Test the solution of a PEP from a finite element model of "
      23             :   "damped mass-spring system (problem from NLEVP collection).\n\n"
      24             :   "The command line options are:\n"
      25             :   "  -n <n> ... number of grid subdivisions.\n"
      26             :   "  -mu <value> ... mass (default 1).\n"
      27             :   "  -tau <value> ... damping constant of the dampers (default 10).\n"
      28             :   "  -kappa <value> ... damping constant of the springs (default 5).\n"
      29             :   "  -initv ... set an initial vector.\n\n";
      30             : 
      31             : #include <slepcpep.h>
      32             : 
      33             : /*
      34             :    Check if computed eigenvectors have unit norm
      35             : */
      36          54 : PetscErrorCode CheckNormalizedVectors(PEP pep)
      37             : {
      38          54 :   PetscInt       i,nconv;
      39          54 :   Mat            A;
      40          54 :   Vec            xr,xi;
      41          54 :   PetscReal      error=0.0,normr;
      42             : #if !defined(PETSC_USE_COMPLEX)
      43             :   PetscReal      normi;
      44             : #endif
      45             : 
      46          54 :   PetscFunctionBeginUser;
      47          54 :   PetscCall(PEPGetConverged(pep,&nconv));
      48          54 :   if (nconv>0) {
      49          54 :     PetscCall(PEPGetOperators(pep,0,&A));
      50          54 :     PetscCall(MatCreateVecs(A,&xr,&xi));
      51         433 :     for (i=0;i<nconv;i++) {
      52         379 :       PetscCall(PEPGetEigenpair(pep,i,NULL,NULL,xr,xi));
      53             : #if defined(PETSC_USE_COMPLEX)
      54         379 :       PetscCall(VecNorm(xr,NORM_2,&normr));
      55         445 :       error = PetscMax(error,PetscAbsReal(normr-PetscRealConstant(1.0)));
      56             : #else
      57             :       PetscCall(VecNormBegin(xr,NORM_2,&normr));
      58             :       PetscCall(VecNormBegin(xi,NORM_2,&normi));
      59             :       PetscCall(VecNormEnd(xr,NORM_2,&normr));
      60             :       PetscCall(VecNormEnd(xi,NORM_2,&normi));
      61             :       error = PetscMax(error,PetscAbsReal(SlepcAbsEigenvalue(normr,normi)-PetscRealConstant(1.0)));
      62             : #endif
      63             :     }
      64          54 :     PetscCall(VecDestroy(&xr));
      65          54 :     PetscCall(VecDestroy(&xi));
      66          54 :     if (error>100*PETSC_MACHINE_EPSILON) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Vectors are not normalized. Error=%g\n",(double)error));
      67             :   }
      68          54 :   PetscFunctionReturn(PETSC_SUCCESS);
      69             : }
      70             : 
      71          54 : int main(int argc,char **argv)
      72             : {
      73          54 :   Mat            M,C,K,A[3];      /* problem matrices */
      74          54 :   PEP            pep;             /* polynomial eigenproblem solver context */
      75          54 :   PetscInt       n=30,Istart,Iend,i,nev;
      76          54 :   PetscReal      mu=1.0,tau=10.0,kappa=5.0;
      77          54 :   PetscBool      initv=PETSC_FALSE,skipnorm=PETSC_FALSE;
      78          54 :   Vec            IV[2];
      79             : 
      80          54 :   PetscFunctionBeginUser;
      81          54 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      82             : 
      83          54 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      84          54 :   PetscCall(PetscOptionsGetReal(NULL,NULL,"-mu",&mu,NULL));
      85          54 :   PetscCall(PetscOptionsGetReal(NULL,NULL,"-tau",&tau,NULL));
      86          54 :   PetscCall(PetscOptionsGetReal(NULL,NULL,"-kappa",&kappa,NULL));
      87          54 :   PetscCall(PetscOptionsGetBool(NULL,NULL,"-initv",&initv,NULL));
      88          54 :   PetscCall(PetscOptionsGetBool(NULL,NULL,"-skipnorm",&skipnorm,NULL));
      89             : 
      90             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      91             :      Compute the matrices that define the eigensystem, (k^2*M+k*C+K)x=0
      92             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      93             : 
      94             :   /* K is a tridiagonal */
      95          54 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&K));
      96          54 :   PetscCall(MatSetSizes(K,PETSC_DECIDE,PETSC_DECIDE,n,n));
      97          54 :   PetscCall(MatSetFromOptions(K));
      98             : 
      99          54 :   PetscCall(MatGetOwnershipRange(K,&Istart,&Iend));
     100        1404 :   for (i=Istart;i<Iend;i++) {
     101        1350 :     if (i>0) PetscCall(MatSetValue(K,i,i-1,-kappa,INSERT_VALUES));
     102        1350 :     PetscCall(MatSetValue(K,i,i,kappa*3.0,INSERT_VALUES));
     103        1350 :     if (i<n-1) PetscCall(MatSetValue(K,i,i+1,-kappa,INSERT_VALUES));
     104             :   }
     105             : 
     106          54 :   PetscCall(MatAssemblyBegin(K,MAT_FINAL_ASSEMBLY));
     107          54 :   PetscCall(MatAssemblyEnd(K,MAT_FINAL_ASSEMBLY));
     108             : 
     109             :   /* C is a tridiagonal */
     110          54 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&C));
     111          54 :   PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,n,n));
     112          54 :   PetscCall(MatSetFromOptions(C));
     113             : 
     114          54 :   PetscCall(MatGetOwnershipRange(C,&Istart,&Iend));
     115        1404 :   for (i=Istart;i<Iend;i++) {
     116        1350 :     if (i>0) PetscCall(MatSetValue(C,i,i-1,-tau,INSERT_VALUES));
     117        1350 :     PetscCall(MatSetValue(C,i,i,tau*3.0,INSERT_VALUES));
     118        1350 :     if (i<n-1) PetscCall(MatSetValue(C,i,i+1,-tau,INSERT_VALUES));
     119             :   }
     120             : 
     121          54 :   PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
     122          54 :   PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));
     123             : 
     124             :   /* M is a diagonal matrix */
     125          54 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&M));
     126          54 :   PetscCall(MatSetSizes(M,PETSC_DECIDE,PETSC_DECIDE,n,n));
     127          54 :   PetscCall(MatSetFromOptions(M));
     128          54 :   PetscCall(MatGetOwnershipRange(M,&Istart,&Iend));
     129        1404 :   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(M,i,i,mu,INSERT_VALUES));
     130          54 :   PetscCall(MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY));
     131          54 :   PetscCall(MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY));
     132             : 
     133             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     134             :                 Create the eigensolver and set various options
     135             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     136             : 
     137          54 :   PetscCall(PEPCreate(PETSC_COMM_WORLD,&pep));
     138          54 :   A[0] = K; A[1] = C; A[2] = M;
     139          54 :   PetscCall(PEPSetOperators(pep,3,A));
     140          54 :   PetscCall(PEPSetProblemType(pep,PEP_GENERAL));
     141          54 :   PetscCall(PEPSetTolerances(pep,PETSC_SMALL,PETSC_CURRENT));
     142          54 :   if (initv) { /* initial vector */
     143           5 :     PetscCall(MatCreateVecs(K,&IV[0],NULL));
     144           5 :     PetscCall(VecSetValue(IV[0],0,-1.0,INSERT_VALUES));
     145           5 :     PetscCall(VecSetValue(IV[0],1,0.5,INSERT_VALUES));
     146           5 :     PetscCall(VecAssemblyBegin(IV[0]));
     147           5 :     PetscCall(VecAssemblyEnd(IV[0]));
     148           5 :     PetscCall(MatCreateVecs(K,&IV[1],NULL));
     149           5 :     PetscCall(VecSetValue(IV[1],0,4.0,INSERT_VALUES));
     150           5 :     PetscCall(VecSetValue(IV[1],2,1.5,INSERT_VALUES));
     151           5 :     PetscCall(VecAssemblyBegin(IV[1]));
     152           5 :     PetscCall(VecAssemblyEnd(IV[1]));
     153           5 :     PetscCall(PEPSetInitialSpace(pep,2,IV));
     154           5 :     PetscCall(VecDestroy(&IV[0]));
     155           5 :     PetscCall(VecDestroy(&IV[1]));
     156             :   }
     157          54 :   PetscCall(PEPSetFromOptions(pep));
     158             : 
     159             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     160             :                       Solve the eigensystem
     161             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     162             : 
     163          54 :   PetscCall(PEPSolve(pep));
     164          54 :   PetscCall(PEPGetDimensions(pep,&nev,NULL,NULL));
     165          54 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev));
     166             : 
     167             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     168             :                     Display solution and clean up
     169             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     170             : 
     171          54 :   PetscCall(PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL));
     172          54 :   if (!skipnorm) PetscCall(CheckNormalizedVectors(pep));
     173          54 :   PetscCall(PEPDestroy(&pep));
     174          54 :   PetscCall(MatDestroy(&M));
     175          54 :   PetscCall(MatDestroy(&C));
     176          54 :   PetscCall(MatDestroy(&K));
     177          54 :   PetscCall(SlepcFinalize());
     178             :   return 0;
     179             : }
     180             : 
     181             : /*TEST
     182             : 
     183             :    testset:
     184             :       args: -pep_nev 4 -initv
     185             :       requires: !single
     186             :       output_file: output/test2_1.out
     187             :       test:
     188             :          suffix: 1
     189             :          args: -pep_type {{toar linear}}
     190             :       test:
     191             :          suffix: 1_toar_mgs
     192             :          args: -pep_type toar -bv_orthog_type mgs
     193             :       test:
     194             :          suffix: 1_qarnoldi
     195             :          args: -pep_type qarnoldi -bv_orthog_refine never
     196             :       test:
     197             :          suffix: 1_linear_gd
     198             :          args: -pep_type linear -pep_linear_eps_type gd -pep_linear_explicitmatrix
     199             : 
     200             :    testset:
     201             :       args: -pep_target -0.43 -pep_nev 4 -pep_ncv 20 -st_type sinvert
     202             :       output_file: output/test2_2.out
     203             :       test:
     204             :          suffix: 2
     205             :          args: -pep_type {{toar linear}}
     206             :       test:
     207             :          suffix: 2_toar_scaleboth
     208             :          args: -pep_type toar -pep_scale both
     209             :       test:
     210             :          suffix: 2_toar_transform
     211             :          args: -pep_type toar -st_transform
     212             :       test:
     213             :          suffix: 2_qarnoldi
     214             :          args: -pep_type qarnoldi -bv_orthog_refine always
     215             :       test:
     216             :          suffix: 2_linear_explicit
     217             :          args: -pep_type linear -pep_linear_explicitmatrix -pep_linear_linearization 0,1
     218             :       test:
     219             :          suffix: 2_linear_explicit_her
     220             :          args: -pep_type linear -pep_linear_explicitmatrix -pep_hermitian -pep_linear_linearization 0,1
     221             :       test:
     222             :          suffix: 2_stoar
     223             :          args: -pep_type stoar -pep_hermitian
     224             :       test:
     225             :          suffix: 2_jd
     226             :          args: -pep_type jd -st_type precond -pep_max_it 200 -pep_ncv 24
     227             :          requires: !single
     228             : 
     229             :    test:
     230             :       suffix: 3
     231             :       args: -pep_nev 12 -pep_extract {{none norm residual structured}} -pep_monitor_cancel
     232             :       requires: !single
     233             : 
     234             :    testset:
     235             :       args: -st_type sinvert -pep_target -0.43 -pep_nev 4
     236             :       output_file: output/test2_2.out
     237             :       test:
     238             :          suffix: 4_schur
     239             :          args: -pep_refine simple -pep_refine_scheme schur
     240             :       test:
     241             :          suffix: 4_mbe
     242             :          args: -pep_refine simple -pep_refine_scheme mbe -pep_refine_ksp_type preonly -pep_refine_pc_type lu
     243             :       test:
     244             :          suffix: 4_explicit
     245             :          args: -pep_refine simple -pep_refine_scheme explicit
     246             :       test:
     247             :          suffix: 4_multiple_schur
     248             :          args: -pep_refine multiple -pep_refine_scheme schur
     249             :          requires: !single
     250             :       test:
     251             :          suffix: 4_multiple_mbe
     252             :          args: -pep_refine multiple -pep_refine_scheme mbe -pep_refine_ksp_type preonly -pep_refine_pc_type lu -pep_refine_pc_factor_shift_type nonzero
     253             :       test:
     254             :          suffix: 4_multiple_explicit
     255             :          args: -pep_refine multiple -pep_refine_scheme explicit
     256             :          requires: !single
     257             : 
     258             :    test:
     259             :       suffix: 5
     260             :       nsize: 2
     261             :       args: -pep_type linear -pep_linear_explicitmatrix -pep_general -pep_target -0.43 -pep_nev 4 -pep_ncv 20 -st_type sinvert -pep_linear_st_ksp_type bcgs -pep_linear_st_pc_type bjacobi
     262             :       output_file: output/test2_2.out
     263             : 
     264             :    test:
     265             :       suffix: 6
     266             :       args: -pep_type linear -pep_nev 12 -pep_extract {{none norm residual}}
     267             :       requires: !single
     268             :       output_file: output/test2_3.out
     269             : 
     270             :    test:
     271             :       suffix: 7
     272             :       args: -pep_nev 12 -pep_extract {{none norm residual structured}} -pep_refine multiple
     273             :       requires: !single
     274             :       output_file: output/test2_3.out
     275             : 
     276             :    testset:
     277             :       args: -st_type sinvert -pep_target -0.43 -pep_nev 4 -st_transform
     278             :       output_file: output/test2_2.out
     279             :       test:
     280             :          suffix: 8_schur
     281             :          args: -pep_refine simple -pep_refine_scheme schur
     282             :       test:
     283             :          suffix: 8_mbe
     284             :          args: -pep_refine simple -pep_refine_scheme mbe -pep_refine_ksp_type preonly -pep_refine_pc_type lu
     285             :       test:
     286             :          suffix: 8_explicit
     287             :          args: -pep_refine simple -pep_refine_scheme explicit
     288             :       test:
     289             :          suffix: 8_multiple_schur
     290             :          args: -pep_refine multiple -pep_refine_scheme schur
     291             :       test:
     292             :          suffix: 8_multiple_mbe
     293             :          args: -pep_refine multiple -pep_refine_scheme mbe -pep_refine_ksp_type preonly -pep_refine_pc_type lu
     294             :       test:
     295             :          suffix: 8_multiple_explicit
     296             :          args: -pep_refine multiple -pep_refine_scheme explicit
     297             : 
     298             :    testset:
     299             :       nsize: 2
     300             :       args: -st_type sinvert -pep_target -0.49 -pep_nev 4 -pep_refine_partitions 2 -st_ksp_type bcgs -st_pc_type bjacobi -pep_scale diagonal -pep_scale_its 4
     301             :       output_file: output/test2_2.out
     302             :       test:
     303             :          suffix: 9_mbe
     304             :          args: -pep_refine simple -pep_refine_scheme mbe -pep_refine_ksp_type preonly -pep_refine_pc_type lu
     305             :       test:
     306             :          suffix: 9_explicit
     307             :          args: -pep_refine simple -pep_refine_scheme explicit
     308             :       test:
     309             :          suffix: 9_multiple_mbe
     310             :          args: -pep_refine multiple -pep_refine_scheme mbe -pep_refine_ksp_type preonly -pep_refine_pc_type lu
     311             :          requires: !single
     312             :       test:
     313             :          suffix: 9_multiple_explicit
     314             :          args: -pep_refine multiple -pep_refine_scheme explicit
     315             :          requires: !single
     316             : 
     317             :    test:
     318             :       suffix: 10
     319             :       nsize: 4
     320             :       args: -st_type sinvert -pep_target -0.43 -pep_nev 4 -pep_refine simple -pep_refine_scheme explicit -pep_refine_partitions 2 -st_ksp_type bcgs -st_pc_type bjacobi -pep_scale diagonal -pep_scale_its 4
     321             :       output_file: output/test2_2.out
     322             : 
     323             :    testset:
     324             :       args: -pep_nev 4 -initv -mat_type aijcusparse
     325             :       output_file: output/test2_1.out
     326             :       requires: cuda !single
     327             :       test:
     328             :          suffix: 11_cuda
     329             :          args: -pep_type {{toar linear}}
     330             :       test:
     331             :          suffix: 11_cuda_qarnoldi
     332             :          args: -pep_type qarnoldi -bv_orthog_refine never
     333             :       test:
     334             :          suffix: 11_cuda_linear_gd
     335             :          args: -pep_type linear -pep_linear_eps_type gd -pep_linear_explicitmatrix
     336             : 
     337             :    test:
     338             :       suffix: 12
     339             :       nsize: 2
     340             :       args: -pep_type jd -ds_parallel synchronized -pep_target -0.43 -pep_nev 4 -pep_ncv 20
     341             :       requires: !single
     342             : 
     343             :    test:
     344             :       suffix: 13
     345             :       args: -pep_nev 12 -pep_view_values draw -pep_monitor draw::draw_lg
     346             :       requires: x !single
     347             :       output_file: output/test2_3.out
     348             : 
     349             :    test:
     350             :       suffix: 14
     351             :       requires: complex double
     352             :       args: -pep_type ciss -rg_type ellipse -rg_ellipse_center -48.5 -rg_ellipse_radius 1.5 -pep_ciss_delta 1e-10
     353             : 
     354             :    testset:
     355             :       args: -pep_nev 4 -initv -mat_type aijhipsparse
     356             :       output_file: output/test2_1.out
     357             :       requires: hip !single
     358             :       test:
     359             :          suffix: 15_hip
     360             :          args: -pep_type {{toar linear}}
     361             :       test:
     362             :          suffix: 15_hip_qarnoldi
     363             :          args: -pep_type qarnoldi -bv_orthog_refine never
     364             :       test:
     365             :          suffix: 15_hip_linear_gd
     366             :          args: -pep_type linear -pep_linear_eps_type gd -pep_linear_explicitmatrix
     367             : 
     368             : TEST*/

Generated by: LCOV version 1.14