LCOV - code coverage report
Current view: top level - sys/classes/ds/tests - test4.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 69 75 92.0 %
Date: 2024-11-21 00:40:22 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       3             :    SLEPc - Scalable Library for Eigenvalue Problem Computations
       4             :    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
       5             : 
       6             :    This file is part of SLEPc.
       7             :    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
       8             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       9             : */
      10             : 
      11             : static char help[] = "Test DSGNHEP.\n\n";
      12             : 
      13             : #include <slepcds.h>
      14             : 
      15           1 : int main(int argc,char **argv)
      16             : {
      17           1 :   DS             ds;
      18           1 :   SlepcSC        sc;
      19           1 :   PetscScalar    *A,*B,*X,*wr,*wi;
      20           1 :   PetscReal      re,im,rnorm,aux;
      21           1 :   PetscInt       i,j,n=10,ld;
      22           1 :   PetscViewer    viewer;
      23           1 :   PetscBool      verbose;
      24             : 
      25           1 :   PetscFunctionBeginUser;
      26           1 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      27           1 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      28           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type GNHEP - dimension %" PetscInt_FMT ".\n",n));
      29           1 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-verbose",&verbose));
      30             : 
      31             :   /* Create DS object */
      32           1 :   PetscCall(DSCreate(PETSC_COMM_WORLD,&ds));
      33           1 :   PetscCall(DSSetType(ds,DSGNHEP));
      34           1 :   PetscCall(DSSetFromOptions(ds));
      35           1 :   ld = n+2;  /* test leading dimension larger than n */
      36           1 :   PetscCall(DSAllocate(ds,ld));
      37           1 :   PetscCall(DSSetDimensions(ds,n,0,0));
      38             : 
      39             :   /* Set up viewer */
      40           1 :   PetscCall(PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer));
      41           1 :   PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL));
      42           1 :   PetscCall(DSView(ds,viewer));
      43           1 :   PetscCall(PetscViewerPopFormat(viewer));
      44           1 :   if (verbose) PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB));
      45             : 
      46             :   /* Fill A with Grcar matrix */
      47           1 :   PetscCall(DSGetArray(ds,DS_MAT_A,&A));
      48           1 :   PetscCall(PetscArrayzero(A,ld*n));
      49          10 :   for (i=1;i<n;i++) A[i+(i-1)*ld]=-1.0;
      50           5 :   for (j=0;j<4;j++) {
      51          38 :     for (i=0;i<n-j;i++) A[i+(i+j)*ld]=1.0;
      52             :   }
      53           1 :   PetscCall(DSRestoreArray(ds,DS_MAT_A,&A));
      54             :   /* Fill B with an upper triangular matrix */
      55           1 :   PetscCall(DSGetArray(ds,DS_MAT_B,&B));
      56           1 :   PetscCall(PetscArrayzero(B,ld*n));
      57           1 :   B[0+0*ld]=-1.0;
      58           1 :   B[0+1*ld]=2.0;
      59          10 :   for (i=1;i<n;i++) B[i+i*ld]=1.0;
      60           1 :   PetscCall(DSRestoreArray(ds,DS_MAT_B,&B));
      61             : 
      62           1 :   if (verbose) {
      63           0 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n"));
      64           0 :     PetscCall(DSView(ds,viewer));
      65             :   }
      66             : 
      67             :   /* Solve */
      68           1 :   PetscCall(PetscMalloc2(n,&wr,n,&wi));
      69           1 :   PetscCall(DSGetSlepcSC(ds,&sc));
      70           1 :   sc->comparison    = SlepcCompareLargestMagnitude;
      71           1 :   sc->comparisonctx = NULL;
      72           1 :   sc->map           = NULL;
      73           1 :   sc->mapobj        = NULL;
      74           1 :   PetscCall(DSSolve(ds,wr,wi));
      75           1 :   PetscCall(DSSort(ds,wr,wi,NULL,NULL,NULL));
      76           1 :   if (verbose) {
      77           0 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n"));
      78           0 :     PetscCall(DSView(ds,viewer));
      79             :   }
      80             : 
      81             :   /* Print eigenvalues */
      82           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Computed eigenvalues =\n"));
      83          11 :   for (i=0;i<n;i++) {
      84             : #if defined(PETSC_USE_COMPLEX)
      85          10 :     re = PetscRealPart(wr[i]);
      86          10 :     im = PetscImaginaryPart(wr[i]);
      87             : #else
      88             :     re = wr[i];
      89             :     im = wi[i];
      90             : #endif
      91          10 :     if (PetscAbs(im)<1e-10) PetscCall(PetscViewerASCIIPrintf(viewer,"  %.5f\n",(double)re));
      92          10 :     else PetscCall(PetscViewerASCIIPrintf(viewer,"  %.5f%+.5fi\n",(double)re,(double)im));
      93             :   }
      94             : 
      95             :   /* Eigenvectors */
      96           1 :   j = 1;
      97           1 :   PetscCall(DSVectors(ds,DS_MAT_X,&j,&rnorm));  /* second eigenvector */
      98           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Value of rnorm for 2nd vector = %.3f\n",(double)rnorm));
      99           1 :   PetscCall(DSVectors(ds,DS_MAT_X,NULL,NULL));  /* all eigenvectors */
     100           1 :   j = 0;
     101           1 :   rnorm = 0.0;
     102           1 :   PetscCall(DSGetArray(ds,DS_MAT_X,&X));
     103          11 :   for (i=0;i<n;i++) {
     104             : #if defined(PETSC_USE_COMPLEX)
     105          10 :     aux = PetscAbsScalar(X[i+j*ld]);
     106             : #else
     107             :     if (PetscAbs(wi[j])==0.0) aux = PetscAbsScalar(X[i+j*ld]);
     108             :     else aux = SlepcAbsEigenvalue(X[i+j*ld],X[i+(j+1)*ld]);
     109             : #endif
     110          10 :     rnorm += aux*aux;
     111             :   }
     112           1 :   PetscCall(DSRestoreArray(ds,DS_MAT_X,&X));
     113           1 :   rnorm = PetscSqrtReal(rnorm);
     114           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Norm of 1st vector = %.3f\n",(double)rnorm));
     115           1 :   if (verbose) {
     116           0 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After vectors - - - - - - - - -\n"));
     117           0 :     PetscCall(DSView(ds,viewer));
     118             :   }
     119             : 
     120           1 :   PetscCall(PetscFree2(wr,wi));
     121           1 :   PetscCall(DSDestroy(&ds));
     122           1 :   PetscCall(SlepcFinalize());
     123             :   return 0;
     124             : }
     125             : 
     126             : /*TEST
     127             : 
     128             :    test:
     129             :       suffix: 1
     130             :       filter: sed -e "s/[+-]\([0-9]\.[0-9]*i\)/+-\\1/" | sed -e "s/+-0\.0*i//" | sed -e "s/1.29552/1.29551/"
     131             : 
     132             : TEST*/

Generated by: LCOV version 1.14