Actual source code: test5.c

slepc-3.11.2 2019-07-30
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2019, 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 DSGHIEP.\n\n";

 13: #include <slepcds.h>

 15: int main(int argc,char **argv)
 16: {
 18:   DS             ds;
 19:   SlepcSC        sc;
 20:   PetscReal      re,im;
 21:   PetscScalar    *A,*B,*eigr,*eigi;
 22:   PetscInt       i,j,n=10,ld;
 23:   PetscViewer    viewer;
 24:   PetscBool      verbose;

 26:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 27:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 28:   PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type GHIEP - dimension %D.\n",n);
 29:   PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);

 31:   /* Create DS object */
 32:   DSCreate(PETSC_COMM_WORLD,&ds);
 33:   DSSetType(ds,DSGHIEP);
 34:   DSSetFromOptions(ds);
 35:   ld = n+2;  /* test leading dimension larger than n */
 36:   DSAllocate(ds,ld);
 37:   DSSetDimensions(ds,n,0,0,0);

 39:   /* Set up viewer */
 40:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
 41:   PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
 42:   DSView(ds,viewer);
 43:   PetscViewerPopFormat(viewer);
 44:   if (verbose) {
 45:     PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
 46:   }

 48:   /* Fill with a symmetric Toeplitz matrix */
 49:   DSGetArray(ds,DS_MAT_A,&A);
 50:   DSGetArray(ds,DS_MAT_B,&B);
 51:   for (i=0;i<n;i++) A[i+i*ld]=2.0;
 52:   for (j=1;j<3;j++) {
 53:     for (i=0;i<n-j;i++) { A[i+(i+j)*ld]=1.0; A[(i+j)+i*ld]=1.0; }
 54:   }
 55:   for (j=1;j<3;j++) { A[0+j*ld]=-1.0*(j+2); A[j+0*ld]=-1.0*(j+2); }
 56:   /* Signature matrix */
 57:   for (i=0;i<n;i++) B[i+i*ld]=1.0;
 58:   B[0] = -1.0;
 59:   B[n-1+(n-1)*ld] = -1.0;
 60:   DSRestoreArray(ds,DS_MAT_A,&A);
 61:   DSRestoreArray(ds,DS_MAT_B,&B);
 62:   DSSetState(ds,DS_STATE_RAW);
 63:   if (verbose) {
 64:     PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n");
 65:     DSView(ds,viewer);
 66:   }

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

 82:   /* Print eigenvalues */
 83:   PetscPrintf(PETSC_COMM_WORLD,"Computed eigenvalues =\n");
 84:   for (i=0;i<n;i++) {
 85: #if defined(PETSC_USE_COMPLEX)
 86:     re = PetscRealPart(eigr[i]);
 87:     im = PetscImaginaryPart(eigr[i]);
 88: #else
 89:     re = eigr[i];
 90:     im = eigi[i];
 91: #endif
 92:     if (PetscAbs(im)<1e-10) {
 93:       PetscViewerASCIIPrintf(viewer,"  %.5f\n",(double)re);
 94:     } else {
 95:       PetscViewerASCIIPrintf(viewer,"  %.5f%+.5fi\n",(double)re,(double)im);
 96:     }
 97:   }
 98:   PetscFree2(eigr,eigi);
 99:   DSDestroy(&ds);
100:   SlepcFinalize();
101:   return ierr;
102: }

104: /*TEST

106:    test:
107:       suffix: 1
108:       requires: !single
109:       args: -ds_method {{0 1 2}}
110:       filter: grep -v "solving the problem"

112: TEST*/