Date: 2024-12-02T08:57:52+00:00

Actual source code: test5.c
slepc-3.22.2 2024-12-02
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain

      6:    This file is part of SLEPc.
      7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
      8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      9: */

     11: static char help[] = "Test SVD view and monitor functionality.\n\n";

     13: #include <slepcsvd.h>

     15: int main(int argc,char **argv)
     16: {
     17:   Mat            A;
     18:   SVD            svd;
     19:   PetscReal      *sigma,sval;
     20:   PetscInt       n=6,Istart,Iend,i,nconv;
     21:   PetscBool      checkfile;
     22:   char           filename[PETSC_MAX_PATH_LEN];
     23:   PetscViewer    viewer;

     25:   PetscFunctionBeginUser;
     26:   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
     27:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
     28:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nSVD of diagonal matrix, n=%" PetscInt_FMT "\n\n",n));

     30:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     31:         Generate the matrix
     32:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     33:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
     34:   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n));
     35:   PetscCall(MatSetFromOptions(A));
     36:   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
     37:   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A,i,i,i+1,INSERT_VALUES));
     38:   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
     39:   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));

     41:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     42:                      Create the SVD solver
     43:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     44:   PetscCall(SVDCreate(PETSC_COMM_WORLD,&svd));
     45:   PetscCall(PetscObjectSetName((PetscObject)svd,"svd"));
     46:   PetscCall(SVDSetOperators(svd,A,NULL));
     47:   PetscCall(SVDSetFromOptions(svd));

     49:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     50:                 Compute the singular triplets and display solution
     51:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     52:   PetscCall(SVDSolve(svd));
     53:   PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL));

     55:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     56:                    Check file containing the singular values
     57:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     58:   PetscCall(PetscOptionsGetString(NULL,NULL,"-checkfile",filename,sizeof(filename),&checkfile));
     59:   if (checkfile) {
     60:     PetscCall(SVDGetConverged(svd,&nconv));
     61:     PetscCall(PetscMalloc1(nconv,&sigma));
     62:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer));
     63:     PetscCall(PetscViewerBinaryRead(viewer,sigma,nconv,NULL,PETSC_REAL));
     64:     PetscCall(PetscViewerDestroy(&viewer));
     65:     for (i=0;i<nconv;i++) {
     66:       PetscCall(SVDGetSingularTriplet(svd,i,&sval,NULL,NULL));
     67:       PetscCheck(sval==sigma[i],PETSC_COMM_WORLD,PETSC_ERR_FILE_UNEXPECTED,"Singular values in the file do not match");
     68:     }
     69:     PetscCall(PetscFree(sigma));
     70:   }

     72:   PetscCall(SVDDestroy(&svd));
     73:   PetscCall(MatDestroy(&A));
     74:   PetscCall(SlepcFinalize());
     75:   return 0;
     76: }

     78: /*TEST

     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

     86:    test:
     87:       suffix: 2
     88:       args: -svd_nsv 4 -svd_view_values binary:myvalues.bin -checkfile myvalues.bin
     89:       requires: double

     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

     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

    102: TEST*/