Actual source code: test9.c

slepc-3.18.2 2023-01-26
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 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: {
 20:   PetscScalar    *X;
 21:   PetscReal      aux,nrm=0.0;
 22:   PetscInt       i,n,ld;

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

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

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

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

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

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

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

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

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

120:   PetscFree(eig);
121:   PetscFree(sc);
122:   DSDestroy(&ds);
123:   SlepcFinalize();
124:   return 0;
125: }

127: /*TEST

129:    test:
130:       suffix: 1
131:       requires: !single

133: TEST*/