Date: 2023-09-05T10:18:42+00:00

Actual source code: test11.c
slepc-3.19.2 2023-09-05
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[] = "Tests a user-defined convergence test (based on ex8.c).\n\n"
     12:   "The command line options are:\n"
     13:   "  -n <n>, where <n> = matrix dimension.\n\n";

     15: #include <slepcsvd.h>

     17: /*
     18:    This example computes the singular values of an nxn Grcar matrix,
     19:    which is a nonsymmetric Toeplitz matrix:

     21:               |  1  1  1  1               |
     22:               | -1  1  1  1  1            |
     23:               |    -1  1  1  1  1         |
     24:               |       .  .  .  .  .       |
     25:           A = |          .  .  .  .  .    |
     26:               |            -1  1  1  1  1 |
     27:               |               -1  1  1  1 |
     28:               |                  -1  1  1 |
     29:               |                     -1  1 |

     31:  */

     33: /*
     34:   MyConvergedRel - Convergence test relative to the norm of A (given in ctx).
     35: */
     36: PetscErrorCode MyConvergedRel(SVD svd,PetscReal sigma,PetscReal res,PetscReal *errest,void *ctx)
     37: {
     38:   PetscReal norm = *(PetscReal*)ctx;

     40:   PetscFunctionBegin;
     41:   *errest = res/norm;
     42:   PetscFunctionReturn(PETSC_SUCCESS);
     43: }

     45: int main(int argc,char **argv)
     46: {
     47:   Mat            A;               /* Grcar matrix */
     48:   SVD            svd;             /* singular value solver context */
     49:   PetscInt       N=30,Istart,Iend,i,col[5],nconv1,nconv2;
     50:   PetscScalar    value[] = { -1, 1, 1, 1, 1 };
     51:   PetscReal      sigma_1,sigma_n;

     53:   PetscFunctionBeginUser;
     54:   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));

     56:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&N,NULL));
     57:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nEstimate the condition number of a Grcar matrix, n=%" PetscInt_FMT "\n\n",N));

     59:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     60:         Generate the matrix
     61:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     63:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
     64:   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N));
     65:   PetscCall(MatSetFromOptions(A));
     66:   PetscCall(MatSetUp(A));
     67:   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
     68:   for (i=Istart;i<Iend;i++) {
     69:     col[0]=i-1; col[1]=i; col[2]=i+1; col[3]=i+2; col[4]=i+3;
     70:     if (i==0) PetscCall(MatSetValues(A,1,&i,PetscMin(4,N-i),col+1,value+1,INSERT_VALUES));
     71:     else PetscCall(MatSetValues(A,1,&i,PetscMin(5,N-i+1),col,value,INSERT_VALUES));
     72:   }
     73:   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
     74:   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));

     76:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     77:              Create the SVD solver and set the solution method
     78:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     80:   PetscCall(SVDCreate(PETSC_COMM_WORLD,&svd));
     81:   PetscCall(SVDSetOperators(svd,A,NULL));
     82:   PetscCall(SVDSetType(svd,SVDTRLANCZOS));
     83:   PetscCall(SVDSetFromOptions(svd));

     85:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     86:                       Solve the singular value problem
     87:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     89:   PetscCall(SVDSetWhichSingularTriplets(svd,SVD_LARGEST));
     90:   PetscCall(SVDSolve(svd));
     91:   PetscCall(SVDGetConverged(svd,&nconv1));
     92:   if (nconv1 > 0) PetscCall(SVDGetSingularTriplet(svd,0,&sigma_1,NULL,NULL));
     93:   else PetscCall(PetscPrintf(PETSC_COMM_WORLD," Unable to compute large singular value!\n\n"));

     95:   /* compute smallest singular value relative to the matrix norm */
     96:   PetscCall(SVDSetConvergenceTestFunction(svd,MyConvergedRel,&sigma_1,NULL));
     97:   PetscCall(SVDSetWhichSingularTriplets(svd,SVD_SMALLEST));
     98:   PetscCall(SVDSolve(svd));
     99:   PetscCall(SVDGetConverged(svd,&nconv2));
    100:   if (nconv2 > 0) PetscCall(SVDGetSingularTriplet(svd,0,&sigma_n,NULL,NULL));
    101:   else PetscCall(PetscPrintf(PETSC_COMM_WORLD," Unable to compute small singular value!\n\n"));

    103:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    104:                     Display solution and clean up
    105:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    106:   if (nconv1 > 0 && nconv2 > 0) {
    107:     PetscCall(PetscPrintf(PETSC_COMM_WORLD," Computed singular values: sigma_1=%.4f, sigma_n=%.4f\n",(double)sigma_1,(double)sigma_n));
    108:     PetscCall(PetscPrintf(PETSC_COMM_WORLD," Estimated condition number: sigma_1/sigma_n=%.4f\n\n",(double)(sigma_1/sigma_n)));
    109:   }

    111:   PetscCall(SVDDestroy(&svd));
    112:   PetscCall(MatDestroy(&A));
    113:   PetscCall(SlepcFinalize());
    114:   return 0;
    115: }

    117: /*TEST

    119:    test:
    120:       suffix: 1

    122: TEST*/