LCOV - code coverage report
Current view: top level - sys/classes/ds/tests - test3.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 62 64 96.9 %
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 DSHEP with compact storage.\n\n";
      12             : 
      13             : #include <slepcds.h>
      14             : 
      15           6 : int main(int argc,char **argv)
      16             : {
      17           6 :   DS             ds;
      18           6 :   SlepcSC        sc;
      19           6 :   PetscReal      *T;
      20           6 :   PetscScalar    *Q,*eig,d;
      21           6 :   PetscInt       i,n=10,l=2,k=5,ld;
      22           6 :   PetscViewer    viewer;
      23           6 :   PetscBool      verbose,extrarow;
      24             : 
      25           6 :   PetscFunctionBeginUser;
      26           6 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      27           6 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      28           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type HEP with compact storage - dimension %" PetscInt_FMT ".\n",n));
      29           6 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-l",&l,NULL));
      30           6 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-k",&k,NULL));
      31           6 :   PetscCheck(l<=n && k<=n && l<=k,PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Wrong value of dimensions");
      32           6 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-verbose",&verbose));
      33           6 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-extrarow",&extrarow));
      34             : 
      35             :   /* Create DS object */
      36           6 :   PetscCall(DSCreate(PETSC_COMM_WORLD,&ds));
      37           6 :   PetscCall(DSSetType(ds,DSHEP));
      38           6 :   PetscCall(DSSetFromOptions(ds));
      39           6 :   ld = n+2;  /* test leading dimension larger than n */
      40           6 :   PetscCall(DSAllocate(ds,ld));
      41           6 :   PetscCall(DSSetDimensions(ds,n,l,k));
      42           6 :   PetscCall(DSSetCompact(ds,PETSC_TRUE));
      43           6 :   PetscCall(DSSetExtraRow(ds,extrarow));
      44             : 
      45             :   /* Set up viewer */
      46           6 :   PetscCall(PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer));
      47           6 :   PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL));
      48           6 :   PetscCall(DSView(ds,viewer));
      49           6 :   PetscCall(PetscViewerPopFormat(viewer));
      50           6 :   PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB));
      51             : 
      52             :   /* Fill arrow-tridiagonal matrix */
      53           6 :   PetscCall(DSGetArrayReal(ds,DS_MAT_T,&T));
      54          60 :   for (i=0;i<n;i++) T[i] = (PetscReal)(i+1);
      55          42 :   for (i=l;i<n-1;i++) T[i+ld] = 1.0;
      56           6 :   if (extrarow) T[n-1+ld] = 1.0;
      57           6 :   PetscCall(DSRestoreArrayReal(ds,DS_MAT_T,&T));
      58           6 :   if (l==0 && k==0) PetscCall(DSSetState(ds,DS_STATE_INTERMEDIATE));
      59           6 :   else PetscCall(DSSetState(ds,DS_STATE_RAW));
      60           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n"));
      61           6 :   PetscCall(DSView(ds,viewer));
      62             : 
      63             :   /* Solve */
      64           6 :   PetscCall(PetscMalloc1(n,&eig));
      65           6 :   PetscCall(DSGetSlepcSC(ds,&sc));
      66           6 :   sc->comparison    = SlepcCompareLargestMagnitude;
      67           6 :   sc->comparisonctx = NULL;
      68           6 :   sc->map           = NULL;
      69           6 :   sc->mapobj        = NULL;
      70           6 :   PetscCall(DSSolve(ds,eig,NULL));
      71           6 :   PetscCall(DSSort(ds,eig,NULL,NULL,NULL,NULL));
      72           6 :   if (extrarow) PetscCall(DSUpdateExtraRow(ds));
      73           6 :   if (verbose) {
      74           0 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n"));
      75           0 :     PetscCall(DSView(ds,viewer));
      76             :   }
      77             : 
      78             :   /* Print eigenvalues */
      79           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Computed eigenvalues =\n"));
      80          60 :   for (i=0;i<n;i++) PetscCall(PetscViewerASCIIPrintf(viewer,"  %.5f\n",(double)PetscRealPart(eig[i])));
      81             : 
      82           6 :   if (extrarow) {
      83             :     /* Check that extra row is correct */
      84           3 :     PetscCall(DSGetArrayReal(ds,DS_MAT_T,&T));
      85           3 :     PetscCall(DSGetArray(ds,DS_MAT_Q,&Q));
      86           3 :     d = 0.0;
      87          24 :     for (i=l;i<n;i++) d += T[i+ld]-Q[n-1+i*ld];
      88           3 :     if (PetscAbsScalar(d)>10*PETSC_MACHINE_EPSILON) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Warning: there is a mismatch in the extra row of %g\n",(double)PetscAbsScalar(d)));
      89           3 :     PetscCall(DSRestoreArrayReal(ds,DS_MAT_T,&T));
      90           3 :     PetscCall(DSRestoreArray(ds,DS_MAT_Q,&Q));
      91             :   }
      92           6 :   PetscCall(PetscFree(eig));
      93           6 :   PetscCall(DSDestroy(&ds));
      94           6 :   PetscCall(SlepcFinalize());
      95             :   return 0;
      96             : }
      97             : 
      98             : /*TEST
      99             : 
     100             :    testset:
     101             :       args: -n 9 -ds_method {{0 1 2}}
     102             :       filter: grep -v "solving the problem" | sed -e "s/extrarow//"
     103             :       requires: !single
     104             :       test:
     105             :          suffix: 1
     106             :       test:
     107             :          suffix: 2
     108             :          args: -extrarow
     109             : 
     110             : TEST*/

Generated by: LCOV version 1.14