LCOV - code coverage report
Current view: top level - sys/classes/ds/tests - test2.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 70 76 92.1 %
Date: 2024-11-23 00:39:48 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.\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 :   PetscScalar    *A,*X,*Q,*eig,d;
      20           6 :   PetscReal      rnorm,aux;
      21           6 :   PetscInt       i,j,n=10,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 - dimension %" PetscInt_FMT ".\n",n));
      29           6 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-verbose",&verbose));
      30           6 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-extrarow",&extrarow));
      31             : 
      32             :   /* Create DS object */
      33           6 :   PetscCall(DSCreate(PETSC_COMM_WORLD,&ds));
      34           6 :   PetscCall(DSSetType(ds,DSHEP));
      35           6 :   PetscCall(DSSetFromOptions(ds));
      36           6 :   ld = n+2;  /* test leading dimension larger than n */
      37           6 :   PetscCall(DSAllocate(ds,ld));
      38           6 :   PetscCall(DSSetDimensions(ds,n,0,0));
      39           6 :   PetscCall(DSSetExtraRow(ds,extrarow));
      40             : 
      41             :   /* Set up viewer */
      42           6 :   PetscCall(PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer));
      43           6 :   PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL));
      44           6 :   PetscCall(DSView(ds,viewer));
      45           6 :   PetscCall(PetscViewerPopFormat(viewer));
      46           6 :   if (verbose) PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB));
      47             : 
      48             :   /* Fill with a symmetric Toeplitz matrix */
      49           6 :   PetscCall(DSGetArray(ds,DS_MAT_A,&A));
      50          78 :   for (i=0;i<n;i++) A[i+i*ld]=2.0;
      51          18 :   for (j=1;j<3;j++) {
      52         138 :     for (i=0;i<n-j;i++) { A[i+(i+j)*ld]=1.0; A[(i+j)+i*ld]=1.0; }
      53             :   }
      54           6 :   if (extrarow) { A[n+(n-2)*ld]=1.0; A[n+(n-1)*ld]=1.0; }
      55           6 :   PetscCall(DSRestoreArray(ds,DS_MAT_A,&A));
      56           6 :   PetscCall(DSSetState(ds,DS_STATE_RAW));
      57           6 :   if (verbose) {
      58           0 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n"));
      59           0 :     PetscCall(DSView(ds,viewer));
      60             :   }
      61             : 
      62             :   /* Solve */
      63           6 :   PetscCall(PetscMalloc1(n,&eig));
      64           6 :   PetscCall(DSGetSlepcSC(ds,&sc));
      65           6 :   sc->comparison    = SlepcCompareLargestMagnitude;
      66           6 :   sc->comparisonctx = NULL;
      67           6 :   sc->map           = NULL;
      68           6 :   sc->mapobj        = NULL;
      69           6 :   PetscCall(DSSolve(ds,eig,NULL));
      70           6 :   PetscCall(DSSort(ds,eig,NULL,NULL,NULL,NULL));
      71           6 :   if (extrarow) PetscCall(DSUpdateExtraRow(ds));
      72           6 :   if (verbose) {
      73           0 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n"));
      74           0 :     PetscCall(DSView(ds,viewer));
      75             :   }
      76             : 
      77             :   /* Print eigenvalues */
      78           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Computed eigenvalues =\n"));
      79          78 :   for (i=0;i<n;i++) PetscCall(PetscViewerASCIIPrintf(viewer,"  %.5f\n",(double)PetscRealPart(eig[i])));
      80             : 
      81           6 :   if (extrarow) {
      82             :     /* Check that extra row is correct */
      83           3 :     PetscCall(DSGetArray(ds,DS_MAT_A,&A));
      84           3 :     PetscCall(DSGetArray(ds,DS_MAT_Q,&Q));
      85             :     d = 0.0;
      86          39 :     for (i=0;i<n;i++) d += A[n+i*ld]-Q[n-2+i*ld]-Q[n-1+i*ld];
      87           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)));
      88           3 :     PetscCall(DSRestoreArray(ds,DS_MAT_A,&A));
      89           3 :     PetscCall(DSRestoreArray(ds,DS_MAT_Q,&Q));
      90             :   }
      91             : 
      92             :   /* Eigenvectors */
      93           6 :   j = 2;
      94           6 :   PetscCall(DSVectors(ds,DS_MAT_X,&j,&rnorm));  /* third eigenvector */
      95           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Value of rnorm for 3rd vector = %.3f\n",(double)rnorm));
      96           6 :   PetscCall(DSVectors(ds,DS_MAT_X,NULL,NULL));  /* all eigenvectors */
      97           6 :   j = 0;
      98           6 :   rnorm = 0.0;
      99           6 :   PetscCall(DSGetArray(ds,DS_MAT_X,&X));
     100          78 :   for (i=0;i<n;i++) {
     101          72 :     aux = PetscAbsScalar(X[i+j*ld]);
     102          72 :     rnorm += aux*aux;
     103             :   }
     104           6 :   PetscCall(DSRestoreArray(ds,DS_MAT_X,&X));
     105           6 :   rnorm = PetscSqrtReal(rnorm);
     106           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Norm of 1st vector = %.3f\n",(double)rnorm));
     107           6 :   if (verbose) {
     108           0 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After vectors - - - - - - - - -\n"));
     109           0 :     PetscCall(DSView(ds,viewer));
     110             :   }
     111             : 
     112           6 :   PetscCall(PetscFree(eig));
     113           6 :   PetscCall(DSDestroy(&ds));
     114           6 :   PetscCall(SlepcFinalize());
     115             :   return 0;
     116             : }
     117             : 
     118             : /*TEST
     119             : 
     120             :    testset:
     121             :       args: -n 12 -ds_method {{0 1 2}}
     122             :       filter: grep -v "solving the problem" | sed -e "s/extrarow//"
     123             :       output_file: output/test2_1.out
     124             :       requires: !single
     125             :       test:
     126             :          suffix: 1
     127             :       test:
     128             :          suffix: 2
     129             :          args: -extrarow
     130             : 
     131             : TEST*/

Generated by: LCOV version 1.14