LCOV - code coverage report
Current view: top level - svd/tests - test5.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 39 39 100.0 %
Date: 2024-12-18 00:51:33 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 SVD view and monitor functionality.\n\n";
      12             : 
      13             : #include <slepcsvd.h>
      14             : 
      15           3 : int main(int argc,char **argv)
      16             : {
      17           3 :   Mat            A;
      18           3 :   SVD            svd;
      19           3 :   PetscReal      *sigma,sval;
      20           3 :   PetscInt       n=6,Istart,Iend,i,nconv;
      21           3 :   PetscBool      checkfile;
      22           3 :   char           filename[PETSC_MAX_PATH_LEN];
      23           3 :   PetscViewer    viewer;
      24             : 
      25           3 :   PetscFunctionBeginUser;
      26           3 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      27           3 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      28           3 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nSVD of diagonal matrix, n=%" PetscInt_FMT "\n\n",n));
      29             : 
      30             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      31             :         Generate the matrix
      32             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      33           3 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
      34           3 :   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n));
      35           3 :   PetscCall(MatSetFromOptions(A));
      36           3 :   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
      37          21 :   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A,i,i,i+1,INSERT_VALUES));
      38           3 :   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
      39           3 :   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
      40             : 
      41             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      42             :                      Create the SVD solver
      43             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      44           3 :   PetscCall(SVDCreate(PETSC_COMM_WORLD,&svd));
      45           3 :   PetscCall(PetscObjectSetName((PetscObject)svd,"svd"));
      46           3 :   PetscCall(SVDSetOperators(svd,A,NULL));
      47           3 :   PetscCall(SVDSetFromOptions(svd));
      48             : 
      49             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      50             :                 Compute the singular triplets and display solution
      51             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      52           3 :   PetscCall(SVDSolve(svd));
      53           3 :   PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL));
      54             : 
      55             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      56             :                    Check file containing the singular values
      57             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      58           3 :   PetscCall(PetscOptionsGetString(NULL,NULL,"-checkfile",filename,sizeof(filename),&checkfile));
      59           3 :   if (checkfile) {
      60           1 :     PetscCall(SVDGetConverged(svd,&nconv));
      61           1 :     PetscCall(PetscMalloc1(nconv,&sigma));
      62           1 :     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer));
      63           1 :     PetscCall(PetscViewerBinaryRead(viewer,sigma,nconv,NULL,PETSC_REAL));
      64           1 :     PetscCall(PetscViewerDestroy(&viewer));
      65           7 :     for (i=0;i<nconv;i++) {
      66           6 :       PetscCall(SVDGetSingularTriplet(svd,i,&sval,NULL,NULL));
      67           6 :       PetscCheck(sval==sigma[i],PETSC_COMM_WORLD,PETSC_ERR_FILE_UNEXPECTED,"Singular values in the file do not match");
      68             :     }
      69           1 :     PetscCall(PetscFree(sigma));
      70             :   }
      71             : 
      72           3 :   PetscCall(SVDDestroy(&svd));
      73           3 :   PetscCall(MatDestroy(&A));
      74           3 :   PetscCall(SlepcFinalize());
      75             :   return 0;
      76             : }
      77             : 
      78             : /*TEST
      79             : 
      80             :    test:
      81             :       suffix: 1
      82             :       args: -svd_error_relative ::ascii_info_detail -svd_view_values -svd_monitor_conv -svd_error_absolute ::ascii_matlab -svd_monitor_all -svd_error_norm ::ascii_info_detail -svd_converged_reason -svd_view
      83             :       filter: grep -v "tolerance" | grep -v "problem type" | sed -e "s/1.999999/2.000000/" | sed -e "s/2.000001/2.000000/" | sed -e "s/[0-9]\.[0-9]*e-\([0-9]*\)/removed/g"
      84             :       requires: !single
      85             : 
      86             :    test:
      87             :       suffix: 2
      88             :       args: -svd_nsv 4 -svd_view_values binary:myvalues.bin -checkfile myvalues.bin
      89             :       requires: double
      90             : 
      91             :    test:
      92             :       suffix: 3
      93             :       args: -svd_type trlanczos -svd_error_relative ::ascii_info_detail -svd_view_values -svd_monitor_conv -svd_error_absolute ::ascii_matlab -svd_monitor_all -svd_converged_reason -svd_view
      94             :       filter: grep -v "tolerance" | grep -v "problem type" | sed -e "s/1.999999/2.000000/" | sed -e "s/2.000001/2.000000/" | sed -e "s/[0-9]\.[0-9]*e-\([0-9]*\)/removed/g"
      95             :       requires: !single
      96             : 
      97             :    test:
      98             :       suffix: 4
      99             :       args: -svd_monitor draw::draw_lg -svd_monitor_all draw::draw_lg -svd_view_values draw -draw_save mysingu.ppm -draw_virtual
     100             :       requires: x !single
     101             : 
     102             : TEST*/

Generated by: LCOV version 1.14