LCOV - code coverage report
Current view: top level - sys/classes/st/tests - test2.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 108 108 100.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 ST with one matrix.\n\n";
      12             : 
      13             : #include <slepcst.h>
      14             : 
      15           6 : int main(int argc,char **argv)
      16             : {
      17           6 :   Mat            A,B,mat[1];
      18           6 :   ST             st;
      19           6 :   Vec            v,w;
      20           6 :   STType         type;
      21           6 :   PetscScalar    sigma,tau,val;
      22           6 :   PetscInt       n=10,i,Istart,Iend;
      23           6 :   PetscBool      test_compl=PETSC_FALSE;
      24             : 
      25           6 :   PetscFunctionBeginUser;
      26           6 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      27           6 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      28           6 :   PetscCall(PetscOptionsGetBool(NULL,NULL,"-complex",&test_compl,NULL));
      29           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n1-D Laplacian, n=%" PetscInt_FMT "\n\n",n));
      30             : 
      31             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      32             :      Compute the operator matrix for the 1-D Laplacian
      33             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      34             : 
      35           6 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
      36           6 :   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n));
      37           6 :   PetscCall(MatSetFromOptions(A));
      38             : 
      39           6 :   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
      40             : #if defined(PETSC_USE_COMPLEX)
      41           6 :   val = test_compl? PetscCMPLX(-1.0,0.4): -1.0;
      42             : #else
      43             :   val = -1.0;
      44             : #endif
      45          66 :   for (i=Istart;i<Iend;i++) {
      46          60 :     if (i>0) PetscCall(MatSetValue(A,i,i-1,val,INSERT_VALUES));
      47          60 :     if (i<n-1) PetscCall(MatSetValue(A,i,i+1,PetscConj(val),INSERT_VALUES));
      48          60 :     PetscCall(MatSetValue(A,i,i,2.0,INSERT_VALUES));
      49             :   }
      50           6 :   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
      51           6 :   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
      52           6 :   PetscCall(MatCreateVecs(A,&v,&w));
      53             : #if defined(PETSC_USE_COMPLEX)
      54           6 :   val = test_compl? PetscCMPLX(1.0,-0.01): 1.0;
      55             : #else
      56             :   val = 1.0;
      57             : #endif
      58           6 :   PetscCall(VecSet(v,val));
      59             : 
      60             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      61             :                 Create the spectral transformation object
      62             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      63             : 
      64           6 :   PetscCall(STCreate(PETSC_COMM_WORLD,&st));
      65           6 :   mat[0] = A;
      66           6 :   PetscCall(STSetMatrices(st,1,mat));
      67           6 :   PetscCall(STSetTransform(st,PETSC_TRUE));
      68           6 :   PetscCall(STSetFromOptions(st));
      69             : 
      70             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      71             :               Apply the transformed operator for several ST's
      72             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      73             : 
      74             :   /* shift, sigma=0.0 */
      75           6 :   PetscCall(STSetUp(st));
      76           6 :   PetscCall(STGetType(st,&type));
      77           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"ST type %s\n",type));
      78           6 :   PetscCall(STApply(st,v,w));
      79           6 :   PetscCall(VecView(w,NULL));
      80             : 
      81             :   /* shift, sigma=0.1 */
      82           6 :   sigma = 0.1;
      83           6 :   PetscCall(STSetShift(st,sigma));
      84           6 :   PetscCall(STGetShift(st,&sigma));
      85           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"With shift=%g\n",(double)PetscRealPart(sigma)));
      86           6 :   PetscCall(STApply(st,v,w));
      87           6 :   PetscCall(VecView(w,NULL));
      88           6 :   PetscCall(STApplyTranspose(st,v,w));
      89           6 :   PetscCall(VecView(w,NULL));
      90           6 :   if (test_compl) {
      91           3 :     PetscCall(STApplyHermitianTranspose(st,v,w));
      92           3 :     PetscCall(VecView(w,NULL));
      93             :   }
      94             : 
      95             :   /* sinvert, sigma=0.1 */
      96           6 :   PetscCall(STPostSolve(st));   /* undo changes if inplace */
      97           6 :   PetscCall(STSetType(st,STSINVERT));
      98           6 :   PetscCall(STGetType(st,&type));
      99           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"ST type %s\n",type));
     100           6 :   PetscCall(STGetShift(st,&sigma));
     101           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"With shift=%g\n",(double)PetscRealPart(sigma)));
     102           6 :   PetscCall(STApply(st,v,w));
     103           6 :   PetscCall(VecView(w,NULL));
     104             : 
     105             :   /* sinvert, sigma=-0.5 */
     106           6 :   sigma = -0.5;
     107           6 :   PetscCall(STSetShift(st,sigma));
     108           6 :   PetscCall(STGetShift(st,&sigma));
     109           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"With shift=%g\n",(double)PetscRealPart(sigma)));
     110           6 :   PetscCall(STApply(st,v,w));
     111           6 :   PetscCall(VecView(w,NULL));
     112           6 :   if (test_compl) {
     113           3 :     PetscCall(STApplyHermitianTranspose(st,v,w));
     114           3 :     PetscCall(VecView(w,NULL));
     115             :   }
     116             : 
     117             :   /* cayley, sigma=-0.5, tau=-0.5 (equal to sigma by default) */
     118           6 :   PetscCall(STPostSolve(st));   /* undo changes if inplace */
     119           6 :   PetscCall(STSetType(st,STCAYLEY));
     120           6 :   PetscCall(STSetUp(st));
     121           6 :   PetscCall(STGetType(st,&type));
     122           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"ST type %s\n",type));
     123           6 :   PetscCall(STGetShift(st,&sigma));
     124           6 :   PetscCall(STCayleyGetAntishift(st,&tau));
     125           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"With shift=%g, antishift=%g\n",(double)PetscRealPart(sigma),(double)PetscRealPart(tau)));
     126           6 :   PetscCall(STApply(st,v,w));
     127           6 :   PetscCall(VecView(w,NULL));
     128             : 
     129             :   /* cayley, sigma=1.1, tau=1.1 (still equal to sigma) */
     130           6 :   sigma = 1.1;
     131           6 :   PetscCall(STSetShift(st,sigma));
     132           6 :   PetscCall(STGetShift(st,&sigma));
     133           6 :   PetscCall(STCayleyGetAntishift(st,&tau));
     134           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"With shift=%g, antishift=%g\n",(double)PetscRealPart(sigma),(double)PetscRealPart(tau)));
     135           6 :   PetscCall(STApply(st,v,w));
     136           6 :   PetscCall(VecView(w,NULL));
     137             : 
     138             :   /* cayley, sigma=1.1, tau=-1.0 */
     139           6 :   tau = -1.0;
     140           6 :   PetscCall(STCayleySetAntishift(st,tau));
     141           6 :   PetscCall(STGetShift(st,&sigma));
     142           6 :   PetscCall(STCayleyGetAntishift(st,&tau));
     143           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"With shift=%g, antishift=%g\n",(double)PetscRealPart(sigma),(double)PetscRealPart(tau)));
     144           6 :   PetscCall(STApply(st,v,w));
     145           6 :   PetscCall(VecView(w,NULL));
     146             : 
     147             :   /* check inner product matrix in Cayley */
     148           6 :   PetscCall(STGetBilinearForm(st,&B));
     149           6 :   PetscCall(MatMult(B,v,w));
     150           6 :   PetscCall(VecView(w,NULL));
     151             : 
     152             :   /* check again sinvert, sigma=0.1 */
     153           6 :   PetscCall(STPostSolve(st));   /* undo changes if inplace */
     154           6 :   PetscCall(STSetType(st,STSINVERT));
     155           6 :   PetscCall(STGetType(st,&type));
     156           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"ST type %s\n",type));
     157           6 :   sigma = 0.1;
     158           6 :   PetscCall(STSetShift(st,sigma));
     159           6 :   PetscCall(STGetShift(st,&sigma));
     160           6 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"With shift=%g\n",(double)PetscRealPart(sigma)));
     161           6 :   PetscCall(STApply(st,v,w));
     162           6 :   PetscCall(VecView(w,NULL));
     163             : 
     164           6 :   PetscCall(STDestroy(&st));
     165           6 :   PetscCall(MatDestroy(&A));
     166           6 :   PetscCall(MatDestroy(&B));
     167           6 :   PetscCall(VecDestroy(&v));
     168           6 :   PetscCall(VecDestroy(&w));
     169           6 :   PetscCall(SlepcFinalize());
     170             :   return 0;
     171             : }
     172             : 
     173             : /*TEST
     174             : 
     175             :    test:
     176             :       suffix: 1
     177             :       args: -st_matmode {{copy inplace shell}}
     178             :       requires: !single
     179             : 
     180             :    test:
     181             :       suffix: 2
     182             :       args: -complex -st_matmode {{copy inplace shell}}
     183             :       requires: complex !single
     184             :       filter: sed -e "s/3.46945e-18 - 8.67362e-18 i/0./"
     185             : 
     186             : TEST*/

Generated by: LCOV version 1.14