Actual source code: test20.c

slepc-3.17.0 2022-03-31
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 DSGNHEP with upper quasi-triangular matrix pair.\n\n";

 13: #include <slepcds.h>

 15: int main(int argc,char **argv)
 16: {
 17:   DS             ds;
 18:   PetscScalar    *A,*B,*X;
 19:   PetscReal      rnorm,aux;
 20:   PetscInt       i,j,n=10,ld;
 21:   PetscViewer    viewer;
 22:   PetscBool      verbose;

 24:   SlepcInitialize(&argc,&argv,(char*)0,help);
 25:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 26:   PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type GNHEP - dimension %" PetscInt_FMT ".\n",n);
 27:   PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);

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

 37:   /* Set up viewer */
 38:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
 39:   PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
 40:   DSView(ds,viewer);
 41:   PetscViewerPopFormat(viewer);
 42:   if (verbose) PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);

 44:   /* Fill A,B with upper quasi-triangular matrices */
 45:   DSGetArray(ds,DS_MAT_A,&A);
 46:   DSGetArray(ds,DS_MAT_B,&B);
 47:   PetscArrayzero(A,ld*n);
 48:   for (i=0;i<n;i++) A[i+i*ld]=2.0;
 49:   for (j=1;j<3;j++) {
 50:     for (i=0;i<n-j;i++) A[i+(i+j)*ld]=0.001;
 51:   }
 52:   PetscArrayzero(B,ld*n);
 53:   for (i=0;i<n;i++) B[i+i*ld]=1.0;
 54:   B[1+0*ld]=B[0+1*ld]=PETSC_MACHINE_EPSILON;
 55:   for (i=1;i<n;i+=3) {
 56:     A[i+(i-1)*ld]=-A[(i-1)+i*ld];
 57:   }
 58:   DSRestoreArray(ds,DS_MAT_A,&A);
 59:   DSRestoreArray(ds,DS_MAT_B,&B);
 60:   DSSetState(ds,DS_STATE_INTERMEDIATE);

 62:   if (verbose) {
 63:     PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n");
 64:     DSView(ds,viewer);
 65:   }

 67:   /* Eigenvectors */
 68:   j = 0;
 69:   DSVectors(ds,DS_MAT_X,&j,&rnorm);  /* first eigenvector */
 70:   PetscPrintf(PETSC_COMM_WORLD,"Value of rnorm for 2nd vector = %.3f\n",(double)rnorm);
 71:   DSVectors(ds,DS_MAT_X,NULL,NULL);  /* all eigenvectors */
 72:   j = 0;
 73:   rnorm = 0.0;
 74:   DSGetArray(ds,DS_MAT_X,&X);
 75:   for (i=0;i<n;i++) {
 76: #if defined(PETSC_USE_COMPLEX)
 77:     aux = PetscAbsScalar(X[i+j*ld]);
 78: #else
 79:     aux = SlepcAbsEigenvalue(X[i+j*ld],X[i+(j+1)*ld]);
 80: #endif
 81:     rnorm += aux*aux;
 82:   }
 83:   DSRestoreArray(ds,DS_MAT_X,&X);
 84:   rnorm = PetscSqrtReal(rnorm);
 85:   PetscPrintf(PETSC_COMM_WORLD,"Norm of 1st columns = %.3f\n",(double)rnorm);
 86:   if (verbose) {
 87:     PetscPrintf(PETSC_COMM_WORLD,"After vectors - - - - - - - - -\n");
 88:     DSView(ds,viewer);
 89:   }

 91:   DSDestroy(&ds);
 92:   SlepcFinalize();
 93:   return 0;
 94: }

 96: /*TEST

 98:    test:
 99:       suffix: 1

101: TEST*/