LCOV - code coverage report
Current view: top level - sys/classes/fn/tests - test5.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 74 78 94.9 %
Date: 2024-12-18 00:51:33 Functions: 2 2 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 matrix rational function.\n\n";
      12             : 
      13             : #include <slepcfn.h>
      14             : 
      15             : /*
      16             :    Compute matrix rational function B = q(A)\p(A)
      17             :  */
      18           4 : PetscErrorCode TestMatRational(FN fn,Mat A,PetscViewer viewer,PetscBool verbose,PetscBool inplace)
      19             : {
      20           4 :   PetscBool      set,flg;
      21           4 :   PetscInt       n;
      22           4 :   Mat            F,Acopy;
      23           4 :   Vec            v,f0;
      24           4 :   PetscReal      nrm;
      25             : 
      26           4 :   PetscFunctionBeginUser;
      27           4 :   PetscCall(MatGetSize(A,&n,NULL));
      28           4 :   PetscCall(MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&F));
      29           4 :   PetscCall(PetscObjectSetName((PetscObject)F,"F"));
      30             :   /* compute matrix function */
      31           4 :   if (inplace) {
      32           2 :     PetscCall(MatCopy(A,F,SAME_NONZERO_PATTERN));
      33           2 :     PetscCall(MatIsHermitianKnown(A,&set,&flg));
      34           2 :     if (set && flg) PetscCall(MatSetOption(F,MAT_HERMITIAN,PETSC_TRUE));
      35           2 :     PetscCall(FNEvaluateFunctionMat(fn,F,NULL));
      36             :   } else {
      37           2 :     PetscCall(MatDuplicate(A,MAT_COPY_VALUES,&Acopy));
      38           2 :     PetscCall(FNEvaluateFunctionMat(fn,A,F));
      39             :     /* check that A has not been modified */
      40           2 :     PetscCall(MatAXPY(Acopy,-1.0,A,SAME_NONZERO_PATTERN));
      41           2 :     PetscCall(MatNorm(Acopy,NORM_1,&nrm));
      42           2 :     if (nrm>100*PETSC_MACHINE_EPSILON) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Warning: the input matrix has changed by %g\n",(double)nrm));
      43           2 :     PetscCall(MatDestroy(&Acopy));
      44             :   }
      45           4 :   if (verbose) {
      46           0 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Matrix A - - - - - - - -\n"));
      47           0 :     PetscCall(MatView(A,viewer));
      48           0 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Computed f(A) - - - - - - -\n"));
      49           0 :     PetscCall(MatView(F,viewer));
      50             :   }
      51             :   /* print matrix norm for checking */
      52           4 :   PetscCall(MatNorm(F,NORM_1,&nrm));
      53           4 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"The 1-norm of f(A) is %g\n",(double)nrm));
      54             :   /* check FNEvaluateFunctionMatVec() */
      55           4 :   PetscCall(MatCreateVecs(A,&v,&f0));
      56           4 :   PetscCall(MatGetColumnVector(F,f0,0));
      57           4 :   PetscCall(FNEvaluateFunctionMatVec(fn,A,v));
      58           4 :   PetscCall(VecAXPY(v,-1.0,f0));
      59           4 :   PetscCall(VecNorm(v,NORM_2,&nrm));
      60           4 :   if (nrm>100*PETSC_MACHINE_EPSILON) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Warning: the norm of f(A)*e_1-v is %g\n",(double)nrm));
      61           4 :   PetscCall(MatDestroy(&F));
      62           4 :   PetscCall(VecDestroy(&v));
      63           4 :   PetscCall(VecDestroy(&f0));
      64           4 :   PetscFunctionReturn(PETSC_SUCCESS);
      65             : }
      66             : 
      67           2 : int main(int argc,char **argv)
      68             : {
      69           2 :   FN             fn;
      70           2 :   Mat            A=NULL;
      71           2 :   PetscInt       i,j,n=10,np,nq;
      72           2 :   PetscScalar    *As,p[10],q[10];
      73           2 :   PetscViewer    viewer;
      74           2 :   PetscBool      verbose,inplace,matcuda;
      75             : 
      76           2 :   PetscFunctionBeginUser;
      77           2 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      78           2 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      79           2 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-verbose",&verbose));
      80           2 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-inplace",&inplace));
      81           2 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-matcuda",&matcuda));
      82           2 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Matrix rational function, n=%" PetscInt_FMT ".\n",n));
      83             : 
      84             :   /* Create rational function r(x)=p(x)/q(x) */
      85           2 :   PetscCall(FNCreate(PETSC_COMM_WORLD,&fn));
      86           2 :   PetscCall(FNSetType(fn,FNRATIONAL));
      87           2 :   np = 2; nq = 3;
      88           2 :   p[0] = -3.1; p[1] = 1.1;
      89           2 :   q[0] = 1.0; q[1] = -2.0; q[2] = 3.5;
      90           2 :   PetscCall(FNRationalSetNumerator(fn,np,p));
      91           2 :   PetscCall(FNRationalSetDenominator(fn,nq,q));
      92           2 :   PetscCall(FNSetFromOptions(fn));
      93             : 
      94             :   /* Set up viewer */
      95           2 :   PetscCall(PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer));
      96           2 :   PetscCall(FNView(fn,viewer));
      97           2 :   if (verbose) PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB));
      98             : 
      99             :   /* Create matrices */
     100           2 :   if (matcuda) {
     101             : #if defined(PETSC_HAVE_CUDA)
     102             :     PetscCall(MatCreateSeqDenseCUDA(PETSC_COMM_SELF,n,n,NULL,&A));
     103             : #endif
     104           2 :   } else PetscCall(MatCreateSeqDense(PETSC_COMM_SELF,n,n,NULL,&A));
     105           2 :   PetscCall(PetscObjectSetName((PetscObject)A,"A"));
     106             : 
     107             :   /* Fill A with a symmetric Toeplitz matrix */
     108           2 :   PetscCall(MatDenseGetArray(A,&As));
     109          22 :   for (i=0;i<n;i++) As[i+i*n]=2.0;
     110           6 :   for (j=1;j<3;j++) {
     111          38 :     for (i=0;i<n-j;i++) { As[i+(i+j)*n]=1.0; As[(i+j)+i*n]=1.0; }
     112             :   }
     113           2 :   PetscCall(MatDenseRestoreArray(A,&As));
     114           2 :   PetscCall(MatSetOption(A,MAT_HERMITIAN,PETSC_TRUE));
     115           2 :   PetscCall(TestMatRational(fn,A,viewer,verbose,inplace));
     116             : 
     117             :   /* Repeat with same matrix as non-symmetric */
     118           2 :   PetscCall(MatSetOption(A,MAT_HERMITIAN,PETSC_FALSE));
     119           2 :   PetscCall(TestMatRational(fn,A,viewer,verbose,inplace));
     120             : 
     121           2 :   PetscCall(MatDestroy(&A));
     122           2 :   PetscCall(FNDestroy(&fn));
     123           2 :   PetscCall(SlepcFinalize());
     124             :   return 0;
     125             : }
     126             : 
     127             : /*TEST
     128             : 
     129             :    testset:
     130             :       output_file: output/test5_1.out
     131             :       requires: !single
     132             :       test:
     133             :          suffix: 1
     134             :       test:
     135             :          suffix: 1_cuda
     136             :          args: -matcuda
     137             :          requires: cuda
     138             :       test:
     139             :          suffix: 2
     140             :          args: -inplace
     141             :       test:
     142             :          suffix: 2_cuda
     143             :          args: -inplace -matcuda
     144             :          requires: cuda
     145             : 
     146             : TEST*/

Generated by: LCOV version 1.14