LCOV - code coverage report
Current view: top level - pep/tests - test2.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 94 94 100.0 %
Date: 2024-11-21 00:34:55 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          53 : PetscErrorCode CheckNormalizedVectors(PEP pep)
      37             : {
      38          53 :   PetscInt       i,nconv;
      39          53 :   Mat            A;
      40          53 :   Vec            xr,xi;
      41          53 :   PetscReal      error=0.0,normr;
      42             : #if !defined(PETSC_USE_COMPLEX)
      43          53 :   PetscReal      normi;
      44             : #endif
      45             : 
      46          53 :   PetscFunctionBeginUser;
      47          53 :   PetscCall(PEPGetConverged(pep,&nconv));
      48          53 :   if (nconv>0) {
      49          53 :     PetscCall(PEPGetOperators(pep,0,&A));
      50          53 :     PetscCall(MatCreateVecs(A,&xr,&xi));
      51         456 :     for (i=0;i<nconv;i++) {
      52         403 :       PetscCall(PEPGetEigenpair(pep,i,NULL,NULL,xr,xi));
      53             : #if defined(PETSC_USE_COMPLEX)
      54             :       PetscCall(VecNorm(xr,NORM_2,&normr));
      55             :       error = PetscMax(error,PetscAbsReal(normr-PetscRealConstant(1.0)));
      56             : #else
      57         403 :       PetscCall(VecNormBegin(xr,NORM_2,&normr));
      58         403 :       PetscCall(VecNormBegin(xi,NORM_2,&normi));
      59         403 :       PetscCall(VecNormEnd(xr,NORM_2,&normr));
      60         403 :       PetscCall(VecNormEnd(xi,NORM_2,&normi));
      61         403 :       error = PetscMax(error,PetscAbsReal(SlepcAbsEigenvalue(normr,normi)-PetscRealConstant(1.0)));
      62             : #endif
      63             :     }
      64          53 :     PetscCall(VecDestroy(&xr));
      65          53 :     PetscCall(VecDestroy(&xi));
      66          53 :     if (error>100*PETSC_MACHINE_EPSILON) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Vectors are not normalized. Error=%g\n",(double)error));
      67             :   }
      68          53 :   PetscFunctionReturn(PETSC_SUCCESS);
      69             : }
      70             : 
      71          53 : int main(int argc,char **argv)
      72             : {
      73          53 :   Mat            M,C,K,A[3];      /* problem matrices */
      74          53 :   PEP            pep;             /* polynomial eigenproblem solver context */
      75          53 :   PetscInt       n=30,Istart,Iend,i,nev;
      76          53 :   PetscReal      mu=1.0,tau=10.0,kappa=5.0;
      77          53 :   PetscBool      initv=PETSC_FALSE,skipnorm=PETSC_FALSE;
      78          53 :   Vec            IV[2];
      79             : 
      80          53 :   PetscFunctionBeginUser;
      81          53 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      82             : 
      83          53 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      84          53 :   PetscCall(PetscOptionsGetReal(NULL,NULL,"-mu",&mu,NULL));
      85          53 :   PetscCall(PetscOptionsGetReal(NULL,NULL,"-tau",&tau,NULL));
      86          53 :   PetscCall(PetscOptionsGetReal(NULL,NULL,"-kappa",&kappa,NULL));
      87          53 :   PetscCall(PetscOptionsGetBool(NULL,NULL,"-initv",&initv,NULL));
      88          53 :   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          53 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&K));
      96          53 :   PetscCall(MatSetSizes(K,PETSC_DECIDE,PETSC_DECIDE,n,n));
      97          53 :   PetscCall(MatSetFromOptions(K));
      98             : 
      99          53 :   PetscCall(MatGetOwnershipRange(K,&Istart,&Iend));
     100        1373 :   for (i=Istart;i<Iend;i++) {
     101        1320 :     if (i>0) PetscCall(MatSetValue(K,i,i-1,-kappa,INSERT_VALUES));
     102        1320 :     PetscCall(MatSetValue(K,i,i,kappa*3.0,INSERT_VALUES));
     103        1320 :     if (i<n-1) PetscCall(MatSetValue(K,i,i+1,-kappa,INSERT_VALUES));
     104             :   }
     105             : 
     106          53 :   PetscCall(MatAssemblyBegin(K,MAT_FINAL_ASSEMBLY));
     107          53 :   PetscCall(MatAssemblyEnd(K,MAT_FINAL_ASSEMBLY));
     108             : 
     109             :   /* C is a tridiagonal */
     110          53 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&C));
     111          53 :   PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,n,n));
     112          53 :   PetscCall(MatSetFromOptions(C));
     113             : 
     114          53 :   PetscCall(MatGetOwnershipRange(C,&Istart,&Iend));
     115        1373 :   for (i=Istart;i<Iend;i++) {
     116        1320 :     if (i>0) PetscCall(MatSetValue(C,i,i-1,-tau,INSERT_VALUES));
     117        1320 :     PetscCall(MatSetValue(C,i,i,tau*3.0,INSERT_VALUES));
     118        1320 :     if (i<n-1) PetscCall(MatSetValue(C,i,i+1,-tau,INSERT_VALUES));
     119             :   }
     120             : 
     121          53 :   PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
     122          53 :   PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));
     123             : 
     124             :   /* M is a diagonal matrix */
     125          53 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&M));
     126          53 :   PetscCall(MatSetSizes(M,PETSC_DECIDE,PETSC_DECIDE,n,n));
     127          53 :   PetscCall(MatSetFromOptions(M));
     128          53 :   PetscCall(MatGetOwnershipRange(M,&Istart,&Iend));
     129        1373 :   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(M,i,i,mu,INSERT_VALUES));
     130          53 :   PetscCall(MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY));
     131          53 :   PetscCall(MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY));
     132             : 
     133             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     134             :                 Create the eigensolver and set various options
     135             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     136             : 
     137          53 :   PetscCall(PEPCreate(PETSC_COMM_WORLD,&pep));
     138          53 :   A[0] = K; A[1] = C; A[2] = M;
     139          53 :   PetscCall(PEPSetOperators(pep,3,A));
     140          53 :   PetscCall(PEPSetProblemType(pep,PEP_GENERAL));
     141          53 :   PetscCall(PEPSetTolerances(pep,PETSC_SMALL,PETSC_CURRENT));
     142          53 :   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          53 :   PetscCall(PEPSetFromOptions(pep));
     158             : 
     159             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     160             :                       Solve the eigensystem
     161             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     162             : 
     163          53 :   PetscCall(PEPSolve(pep));
     164          53 :   PetscCall(PEPGetDimensions(pep,&nev,NULL,NULL));
     165          53 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev));
     166             : 
     167             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     168             :                     Display solution and clean up
     169             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     170             : 
     171          53 :   PetscCall(PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL));
     172          53 :   if (!skipnorm) PetscCall(CheckNormalizedVectors(pep));
     173          53 :   PetscCall(PEPDestroy(&pep));
     174          53 :   PetscCall(MatDestroy(&M));
     175          53 :   PetscCall(MatDestroy(&C));
     176          53 :   PetscCall(MatDestroy(&K));
     177          53 :   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