LCOV - code coverage report
Current view: top level - pep/tests - test5.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 65 65 100.0 %
Date: 2024-05-04 00:51:07 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[] = "Test PEP view and monitor functionality.\n\n";
      12             : 
      13             : #include <slepcpep.h>
      14             : 
      15           4 : int main(int argc,char **argv)
      16             : {
      17           4 :   Mat            A[3];
      18           4 :   PEP            pep;
      19           4 :   Vec            xr,xi;
      20           4 :   PetscScalar    kr,ki;
      21           4 :   PetscInt       n=6,Istart,Iend,i,nconv,its;
      22           4 :   PetscReal      errest;
      23           4 :   PetscBool      checkfile;
      24           4 :   char           filename[PETSC_MAX_PATH_LEN];
      25           4 :   PetscViewer    viewer;
      26             : 
      27           4 :   PetscFunctionBeginUser;
      28           4 :   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
      29           4 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      30           4 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nPEP of diagonal problem, n=%" PetscInt_FMT "\n\n",n));
      31             : 
      32             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      33             :         Generate the matrices
      34             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      35           4 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A[0]));
      36           4 :   PetscCall(MatSetSizes(A[0],PETSC_DECIDE,PETSC_DECIDE,n,n));
      37           4 :   PetscCall(MatSetFromOptions(A[0]));
      38           4 :   PetscCall(MatGetOwnershipRange(A[0],&Istart,&Iend));
      39          34 :   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A[0],i,i,i+1,INSERT_VALUES));
      40           4 :   PetscCall(MatAssemblyBegin(A[0],MAT_FINAL_ASSEMBLY));
      41           4 :   PetscCall(MatAssemblyEnd(A[0],MAT_FINAL_ASSEMBLY));
      42             : 
      43           4 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A[1]));
      44           4 :   PetscCall(MatSetSizes(A[1],PETSC_DECIDE,PETSC_DECIDE,n,n));
      45           4 :   PetscCall(MatSetFromOptions(A[1]));
      46          34 :   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A[1],i,i,-1.5,INSERT_VALUES));
      47           4 :   PetscCall(MatAssemblyBegin(A[1],MAT_FINAL_ASSEMBLY));
      48           4 :   PetscCall(MatAssemblyEnd(A[1],MAT_FINAL_ASSEMBLY));
      49             : 
      50           4 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A[2]));
      51           4 :   PetscCall(MatSetSizes(A[2],PETSC_DECIDE,PETSC_DECIDE,n,n));
      52           4 :   PetscCall(MatSetFromOptions(A[2]));
      53          34 :   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A[2],i,i,-1.0/(i+1),INSERT_VALUES));
      54           4 :   PetscCall(MatAssemblyBegin(A[2],MAT_FINAL_ASSEMBLY));
      55           4 :   PetscCall(MatAssemblyEnd(A[2],MAT_FINAL_ASSEMBLY));
      56             : 
      57             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      58             :                      Create the PEP solver
      59             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      60           4 :   PetscCall(PEPCreate(PETSC_COMM_WORLD,&pep));
      61           4 :   PetscCall(PetscObjectSetName((PetscObject)pep,"pep"));
      62           4 :   PetscCall(PEPSetOperators(pep,3,A));
      63           4 :   PetscCall(PEPSetFromOptions(pep));
      64             : 
      65             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      66             :                 Solve the eigensystem and display solution
      67             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      68           4 :   PetscCall(PEPSolve(pep));
      69           4 :   PetscCall(PEPGetConverged(pep,&nconv));
      70           4 :   PetscCall(PEPGetIterationNumber(pep,&its));
      71           4 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," %" PetscInt_FMT " converged eigenpairs after %" PetscInt_FMT " iterations\n",nconv,its));
      72           4 :   if (nconv>0) {
      73           4 :     PetscCall(MatCreateVecs(A[0],&xr,&xi));
      74           4 :     PetscCall(PEPGetEigenpair(pep,0,&kr,&ki,xr,xi));
      75           4 :     PetscCall(VecDestroy(&xr));
      76           4 :     PetscCall(VecDestroy(&xi));
      77           4 :     PetscCall(PEPGetErrorEstimate(pep,0,&errest));
      78             :   }
      79           4 :   PetscCall(PEPErrorView(pep,PEP_ERROR_RELATIVE,NULL));
      80             : 
      81             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      82             :                    Check file containing the eigenvalues
      83             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      84           4 :   PetscCall(PetscOptionsGetString(NULL,NULL,"-checkfile",filename,sizeof(filename),&checkfile));
      85           4 :   if (checkfile) {
      86             : #if defined(PETSC_HAVE_COMPLEX)
      87           1 :     PetscComplex *eigs,eval;
      88           1 :     PetscCall(PetscMalloc1(nconv,&eigs));
      89           1 :     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer));
      90           1 :     PetscCall(PetscViewerBinaryRead(viewer,eigs,nconv,NULL,PETSC_COMPLEX));
      91           1 :     PetscCall(PetscViewerDestroy(&viewer));
      92          13 :     for (i=0;i<nconv;i++) {
      93          12 :       PetscCall(PEPGetEigenpair(pep,i,&kr,&ki,NULL,NULL));
      94             : #if defined(PETSC_USE_COMPLEX)
      95             :       eval = kr;
      96             : #else
      97          12 :       eval = PetscCMPLX(kr,ki);
      98             : #endif
      99          12 :       PetscCheck(eval==eigs[i],PETSC_COMM_WORLD,PETSC_ERR_FILE_UNEXPECTED,"Eigenvalues in the file do not match");
     100             :     }
     101           1 :     PetscCall(PetscFree(eigs));
     102             : #else
     103             :     SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"The -checkfile option requires C99 complex numbers");
     104             : #endif
     105             :   }
     106             : 
     107           4 :   PetscCall(PEPDestroy(&pep));
     108           4 :   PetscCall(MatDestroy(&A[0]));
     109           4 :   PetscCall(MatDestroy(&A[1]));
     110           4 :   PetscCall(MatDestroy(&A[2]));
     111           4 :   PetscCall(SlepcFinalize());
     112             :   return 0;
     113             : }
     114             : 
     115             : /*TEST
     116             : 
     117             :    test:
     118             :       suffix: 1
     119             :       args: -pep_error_backward ::ascii_info_detail -pep_largest_real -pep_view_values -pep_monitor_conv -pep_error_absolute ::ascii_matlab -pep_monitor_all -pep_converged_reason -pep_view
     120             :       requires: !single
     121             :       filter: grep -v "tolerance" | grep -v "problem type" | sed -e "s/[+-]0\.0*i//g" -e "s/\([0-9]\.[5]*\)[+-][0-9]\.[0-9]*e-[0-9]*i/\\1/g" -e "s/[0-9]\.[0-9]*e-\([0-9]*\)/removed/g"
     122             : 
     123             :    test:
     124             :       suffix: 2
     125             :       args: -n 12 -pep_largest_real -pep_monitor -pep_view_values ::ascii_matlab
     126             :       requires: double
     127             :       filter: sed -e "s/[+-][0-9]\.[0-9]*e-[0-9]*i//" -e "s/[0-9]\.[0-9]*e-\([0-9]*\)/removed/g" -e "s/5\.\([49]\)999999[0-9]*e+00/5.\\1999999999999999e+00/"
     128             : 
     129             :    test:
     130             :       suffix: 3
     131             :       args: -pep_nev 4 -pep_view_values binary:myvalues.bin -checkfile myvalues.bin
     132             :       requires: double c99_complex
     133             : 
     134             :    test:
     135             :       suffix: 4
     136             :       args: -pep_nev 4 -pep_ncv 10 -pep_refine -pep_conv_norm -pep_extract none -pep_scale scalar -pep_view -pep_monitor -pep_error_relative ::ascii_info_detail
     137             :       requires: double !complex
     138             :       filter: grep -v "tolerance" | sed -e "s/[0-9]\.[0-9]*e-\([0-9]*\)/removed/g"
     139             : 
     140             :    test:
     141             :       suffix: 5
     142             :       args: -n 12 -pep_largest_real -pep_monitor draw::draw_lg -pep_monitor_all draw::draw_lg -pep_view_values draw -draw_save myeigen.ppm -draw_virtual
     143             :       requires: x double
     144             : 
     145             : TEST*/

Generated by: LCOV version 1.14