Date: 2021-09-20T09:43:42+00:00

Actual source code: test9.c
slepc-3.15.2 2021-09-20
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 DSGHEP.\n\n";

     13: #include <slepcds.h>

     15: /*
     16:    Compute the norm of the j-th column of matrix mat in ds
     17:  */
     18: PetscErrorCode ComputeNorm(DS ds,DSMatType mat,PetscInt j,PetscReal *onrm)
     19: {
     21:   PetscScalar    *X;
     22:   PetscReal      aux,nrm=0.0;
     23:   PetscInt       i,n,ld;

     26:   DSGetLeadingDimension(ds,&ld);
     27:   DSGetDimensions(ds,&n,NULL,NULL,NULL,NULL);
     28:   DSGetArray(ds,mat,&X);
     29:   for (i=0;i<n;i++) {
     30:     aux = PetscAbsScalar(X[i+j*ld]);
     31:     nrm += aux*aux;
     32:   }
     33:   DSRestoreArray(ds,mat,&X);
     34:   *onrm = PetscSqrtReal(nrm);
     35:   return(0);
     36: }

     38: int main(int argc,char **argv)
     39: {
     41:   DS             ds;
     42:   SlepcSC        sc;
     43:   PetscReal      re;
     44:   PetscScalar    *A,*B,*eig;
     45:   PetscReal      nrm;
     46:   PetscInt       i,j,n=10,ld;
     47:   PetscViewer    viewer;
     48:   PetscBool      verbose;

     50:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
     51:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
     52:   PetscPrintf(PETSC_COMM_WORLD,"Solve a System of type GHEP - dimension %D.\n",n);
     53:   PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);

     55:   /* Create DS object */
     56:   DSCreate(PETSC_COMM_WORLD,&ds);
     57:   DSSetType(ds,DSGHEP);
     58:   DSSetFromOptions(ds);
     59:   ld = n+2;  /* test leading dimension larger than n */
     60:   DSAllocate(ds,ld);
     61:   DSSetDimensions(ds,n,0,0,0);

     63:   /* Set up viewer */
     64:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
     65:   PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
     66:   DSView(ds,viewer);
     67:   PetscViewerPopFormat(viewer);
     68:   if (verbose) {
     69:     PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
     70:   }

     72:   /* Fill with a symmetric Toeplitz matrix */
     73:   DSGetArray(ds,DS_MAT_A,&A);
     74:   DSGetArray(ds,DS_MAT_B,&B);
     75:   for (i=0;i<n;i++) A[i+i*ld]=2.0;
     76:   for (j=1;j<3;j++) {
     77:     for (i=0;i<n-j;i++) { A[i+(i+j)*ld]=1.0; A[(i+j)+i*ld]=1.0; }
     78:   }
     79:   for (j=1;j<3;j++) { A[0+j*ld]=-1.0*(j+2); A[j+0*ld]=-1.0*(j+2); }
     80:   /* Diagonal matrix */
     81:   for (i=0;i<n;i++) B[i+i*ld]=0.1*(i+1);
     82:   DSRestoreArray(ds,DS_MAT_A,&A);
     83:   DSRestoreArray(ds,DS_MAT_B,&B);
     84:   DSSetState(ds,DS_STATE_RAW);
     85:   if (verbose) {
     86:     PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n");
     87:     DSView(ds,viewer);
     88:   }

     90:   /* Solve */
     91:   PetscMalloc1(n,&eig);
     92:   PetscNew(&sc);
     93:   sc->comparison    = SlepcCompareLargestMagnitude;
     94:   sc->comparisonctx = NULL;
     95:   sc->map           = NULL;
     96:   sc->mapobj        = NULL;
     97:   DSSetSlepcSC(ds,sc);
     98:   DSSolve(ds,eig,NULL);
     99:   DSSort(ds,eig,NULL,NULL,NULL,NULL);
    100:   if (verbose) {
    101:     PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n");
    102:     DSView(ds,viewer);
    103:   }

    105:   /* Print eigenvalues */
    106:   PetscPrintf(PETSC_COMM_WORLD,"Computed eigenvalues =\n");
    107:   for (i=0;i<n;i++) {
    108:     re = PetscRealPart(eig[i]);
    109:     PetscViewerASCIIPrintf(viewer,"  %.5f\n",(double)re);
    110:   }

    112:   /* Eigenvectors */
    113:   j = 0;
    114:   DSVectors(ds,DS_MAT_X,&j,NULL);  /* all eigenvectors */
    115:   ComputeNorm(ds,DS_MAT_X,0,&nrm);
    116:   PetscPrintf(PETSC_COMM_WORLD,"Norm of 1st vector = %.3f\n",(double)nrm);
    117:   DSVectors(ds,DS_MAT_X,NULL,NULL);  /* all eigenvectors */
    118:   if (verbose) {
    119:     PetscPrintf(PETSC_COMM_WORLD,"After vectors - - - - - - - - -\n");
    120:     DSView(ds,viewer);
    121:   }

    123:   PetscFree(eig);
    124:   PetscFree(sc);
    125:   DSDestroy(&ds);
    126:   SlepcFinalize();
    127:   return ierr;
    128: }

    130: /*TEST

    132:    test:
    133:       suffix: 1
    134:       requires: !single

    136: TEST*/