LCOV - code coverage report
Current view: top level - sys/classes/ds/tests - test19.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 47 47 100.0 %
Date: 2024-11-21 00:34:55 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 DSSortWithPermutation() on a NHEP.\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,*wr,*wi;
      20           1 :   PetscReal      re,im;
      21           1 :   PetscInt       i,j,n=12,*perm;
      22             : 
      23           1 :   PetscFunctionBeginUser;
      24           1 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      25           1 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      26           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type NHEP - dimension %" PetscInt_FMT ".\n",n));
      27             : 
      28             :   /* Create DS object */
      29           1 :   PetscCall(DSCreate(PETSC_COMM_WORLD,&ds));
      30           1 :   PetscCall(DSSetType(ds,DSNHEP));
      31           1 :   PetscCall(DSSetFromOptions(ds));
      32           1 :   PetscCall(DSAllocate(ds,n));
      33           1 :   PetscCall(DSSetDimensions(ds,n,0,0));
      34             : 
      35             :   /* Fill with Grcar matrix */
      36           1 :   PetscCall(DSGetArray(ds,DS_MAT_A,&A));
      37          12 :   for (i=1;i<n;i++) A[i+(i-1)*n]=-1.0;
      38           5 :   for (j=0;j<4;j++) {
      39          46 :     for (i=0;i<n-j;i++) A[i+(i+j)*n]=1.0;
      40             :   }
      41           1 :   PetscCall(DSRestoreArray(ds,DS_MAT_A,&A));
      42           1 :   PetscCall(DSSetState(ds,DS_STATE_INTERMEDIATE));
      43             : 
      44             :   /* Solve */
      45           1 :   PetscCall(PetscMalloc3(n,&wr,n,&wi,n,&perm));
      46           1 :   PetscCall(DSGetSlepcSC(ds,&sc));
      47           1 :   sc->comparison    = SlepcCompareLargestMagnitude;
      48           1 :   sc->comparisonctx = NULL;
      49           1 :   sc->map           = NULL;
      50           1 :   sc->mapobj        = NULL;
      51           1 :   PetscCall(DSSolve(ds,wr,wi));
      52           1 :   PetscCall(DSSort(ds,wr,wi,NULL,NULL,NULL));
      53             : 
      54             :   /* Print eigenvalues */
      55           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Computed eigenvalues =\n"));
      56          13 :   for (i=0;i<n;i++) {
      57             : #if defined(PETSC_USE_COMPLEX)
      58             :     re = PetscRealPart(wr[i]);
      59             :     im = PetscImaginaryPart(wr[i]);
      60             : #else
      61          12 :     re = wr[i];
      62          12 :     im = wi[i];
      63             : #endif
      64          12 :     if (PetscAbs(im)<1e-10) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"  %.5f\n",(double)re));
      65          12 :     else PetscCall(PetscPrintf(PETSC_COMM_WORLD,"  %.5f%+.5fi\n",(double)re,(double)im));
      66             :   }
      67             : 
      68             :   /* Reorder eigenvalues */
      69           7 :   for (i=0;i<n/2;i++) perm[i] = n/2+i;
      70           7 :   for (i=0;i<n/2;i++) perm[i+n/2] = i;
      71           1 :   PetscCall(DSSortWithPermutation(ds,perm,wr,wi));
      72           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Reordered eigenvalues =\n"));
      73          13 :   for (i=0;i<n;i++) {
      74             : #if defined(PETSC_USE_COMPLEX)
      75             :     re = PetscRealPart(wr[i]);
      76             :     im = PetscImaginaryPart(wr[i]);
      77             : #else
      78          12 :     re = wr[i];
      79          12 :     im = wi[i];
      80             : #endif
      81          12 :     if (PetscAbs(im)<1e-10) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"  %.5f\n",(double)re));
      82          12 :     else PetscCall(PetscPrintf(PETSC_COMM_WORLD,"  %.5f%+.5fi\n",(double)re,(double)im));
      83             :   }
      84             : 
      85           1 :   PetscCall(PetscFree3(wr,wi,perm));
      86           1 :   PetscCall(DSDestroy(&ds));
      87           1 :   PetscCall(SlepcFinalize());
      88             :   return 0;
      89             : }
      90             : 
      91             : /*TEST
      92             : 
      93             :    test:
      94             :       suffix: 1
      95             :       filter: sed -e "s/[+-]\([0-9]\.[0-9]*i\)/+-\\1/" | sed -e "s/2.16612/2.16613/"
      96             : 
      97             : TEST*/

Generated by: LCOV version 1.14