Actual source code: test8.c

slepc-3.16.1 2021-11-17
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2021, 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 DSSVD with compact storage.\n\n";

 13: #include <slepcds.h>

 15: int main(int argc,char **argv)
 16: {
 18:   DS             ds;
 19:   SlepcSC        sc;
 20:   PetscReal      *T,sigma;
 21:   PetscScalar    *U,*w,d;
 22:   PetscInt       i,n=10,m,l=2,k=5,ld;
 23:   PetscViewer    viewer;
 24:   PetscBool      verbose,extrarow;

 26:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 27:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 28:   m = n;
 29:   PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type SVD with compact storage - dimension %Dx%D.\n",n,m);
 30:   PetscOptionsGetInt(NULL,NULL,"-l",&l,NULL);
 31:   PetscOptionsGetInt(NULL,NULL,"-k",&k,NULL);
 32:   if (l>n || k>n || l>k) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Wrong value of dimensions");
 33:   PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);
 34:   PetscOptionsHasName(NULL,NULL,"-extrarow",&extrarow);

 36:   /* Create DS object */
 37:   DSCreate(PETSC_COMM_WORLD,&ds);
 38:   DSSetType(ds,DSSVD);
 39:   DSSetFromOptions(ds);
 40:   ld = n+2;  /* test leading dimension larger than n */
 41:   DSAllocate(ds,ld);
 42:   DSSetDimensions(ds,n,l,k);
 43:   DSSVDSetDimensions(ds,m);
 44:   DSSetCompact(ds,PETSC_TRUE);
 45:   DSSetExtraRow(ds,extrarow);

 47:   /* Set up viewer */
 48:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
 49:   PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
 50:   DSView(ds,viewer);
 51:   PetscViewerPopFormat(viewer);
 52:   PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);

 54:   /* Fill upper arrow-bidiagonal matrix */
 55:   DSGetArrayReal(ds,DS_MAT_T,&T);
 56:   for (i=0;i<n;i++) T[i] = (PetscReal)(i+1);
 57:   for (i=l;i<n-1;i++) T[i+ld] = 1.0;
 58:   if (extrarow) T[n-1+ld] = 1.0;
 59:   DSRestoreArrayReal(ds,DS_MAT_T,&T);
 60:   if (l==0 && k==0) {
 61:     DSSetState(ds,DS_STATE_INTERMEDIATE);
 62:   } else {
 63:     DSSetState(ds,DS_STATE_RAW);
 64:   }
 65:   PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n");
 66:   DSView(ds,viewer);

 68:   /* Solve */
 69:   PetscMalloc1(n,&w);
 70:   DSGetSlepcSC(ds,&sc);
 71:   sc->comparison    = SlepcCompareLargestReal;
 72:   sc->comparisonctx = NULL;
 73:   sc->map           = NULL;
 74:   sc->mapobj        = NULL;
 75:   DSSolve(ds,w,NULL);
 76:   DSSort(ds,w,NULL,NULL,NULL,NULL);
 77:   if (extrarow) { DSUpdateExtraRow(ds); }
 78:   if (verbose) {
 79:     PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n");
 80:     DSView(ds,viewer);
 81:   }

 83:   /* Print singular values */
 84:   PetscPrintf(PETSC_COMM_WORLD,"Computed singular values =\n");
 85:   for (i=0;i<n;i++) {
 86:     sigma = PetscRealPart(w[i]);
 87:     PetscViewerASCIIPrintf(viewer,"  %.5f\n",(double)sigma);
 88:   }

 90:   if (extrarow) {
 91:     /* Check that extra row is correct */
 92:     DSGetArrayReal(ds,DS_MAT_T,&T);
 93:     DSGetArray(ds,DS_MAT_U,&U);
 94:     d = 0.0;
 95:     for (i=l;i<n;i++) d += T[i+ld]-U[n-1+i*ld];
 96:     if (PetscAbsScalar(d)>10*PETSC_MACHINE_EPSILON) {
 97:       PetscPrintf(PETSC_COMM_WORLD,"Warning: there is a mismatch in the extra row of %g\n",(double)PetscAbsScalar(d));
 98:     }
 99:     DSRestoreArrayReal(ds,DS_MAT_T,&T);
100:     DSRestoreArray(ds,DS_MAT_U,&U);
101:   }
102:   PetscFree(w);
103:   DSDestroy(&ds);
104:   SlepcFinalize();
105:   return ierr;
106: }

108: /*TEST

110:    test:
111:       suffix: 1
112:       requires: !single

114:    test:
115:       args: -l 0 -k 0
116:       suffix: 2
117:       requires: !single

119:    test:
120:       args: -extrarow
121:       suffix: 3
122:       requires: !single

124: TEST*/