LCOV - code coverage report
Current view: top level - sys/classes/st/tests - test8.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 85 85 100.0 %
Date: 2024-05-04 00:30:31 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 two matrices and split preconditioner.\n\n";
      12             : 
      13             : #include <slepcst.h>
      14             : 
      15           3 : int main(int argc,char **argv)
      16             : {
      17           3 :   Mat            A,B,Pa,Pb,Pmat,mat[2];
      18           3 :   ST             st;
      19           3 :   KSP            ksp;
      20           3 :   PC             pc;
      21           3 :   Vec            v,w;
      22           3 :   STType         type;
      23           3 :   PetscScalar    sigma;
      24           3 :   PetscInt       n=10,i,Istart,Iend;
      25             : 
      26           3 :   PetscFunctionBeginUser;
      27           3 :   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
      28           3 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      29           3 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n1-D Laplacian plus diagonal, n=%" PetscInt_FMT "\n\n",n));
      30             : 
      31             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      32             :      Compute the operator matrices (1-D Laplacian and diagonal)
      33             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      34             : 
      35           3 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
      36           3 :   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n));
      37           3 :   PetscCall(MatSetFromOptions(A));
      38             : 
      39           3 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
      40           3 :   PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,n,n));
      41           3 :   PetscCall(MatSetFromOptions(B));
      42             : 
      43           3 :   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
      44          33 :   for (i=Istart;i<Iend;i++) {
      45          30 :     PetscCall(MatSetValue(A,i,i,2.0,INSERT_VALUES));
      46          30 :     if (i>0) {
      47          27 :       PetscCall(MatSetValue(A,i,i-1,-1.0,INSERT_VALUES));
      48          27 :       PetscCall(MatSetValue(B,i,i,(PetscScalar)i,INSERT_VALUES));
      49           3 :     } else PetscCall(MatSetValue(B,i,i,-1.0,INSERT_VALUES));
      50          30 :     if (i<n-1) PetscCall(MatSetValue(A,i,i+1,-1.0,INSERT_VALUES));
      51             :   }
      52           3 :   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
      53           3 :   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
      54           3 :   PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
      55           3 :   PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
      56           3 :   PetscCall(MatCreateVecs(A,&v,&w));
      57           3 :   PetscCall(VecSet(v,1.0));
      58             : 
      59             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      60             :      Compute the split preconditioner matrices (two diagonals)
      61             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      62             : 
      63           3 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&Pa));
      64           3 :   PetscCall(MatSetSizes(Pa,PETSC_DECIDE,PETSC_DECIDE,n,n));
      65           3 :   PetscCall(MatSetFromOptions(Pa));
      66             : 
      67           3 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&Pb));
      68           3 :   PetscCall(MatSetSizes(Pb,PETSC_DECIDE,PETSC_DECIDE,n,n));
      69           3 :   PetscCall(MatSetFromOptions(Pb));
      70             : 
      71           3 :   PetscCall(MatGetOwnershipRange(Pa,&Istart,&Iend));
      72          33 :   for (i=Istart;i<Iend;i++) {
      73          30 :     PetscCall(MatSetValue(Pa,i,i,2.0,INSERT_VALUES));
      74          30 :     if (i>0) PetscCall(MatSetValue(Pb,i,i,(PetscScalar)i,INSERT_VALUES));
      75          30 :     else PetscCall(MatSetValue(Pb,i,i,-1.0,INSERT_VALUES));
      76             :   }
      77           3 :   PetscCall(MatAssemblyBegin(Pa,MAT_FINAL_ASSEMBLY));
      78           3 :   PetscCall(MatAssemblyEnd(Pa,MAT_FINAL_ASSEMBLY));
      79           3 :   PetscCall(MatAssemblyBegin(Pb,MAT_FINAL_ASSEMBLY));
      80           3 :   PetscCall(MatAssemblyEnd(Pb,MAT_FINAL_ASSEMBLY));
      81             : 
      82             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      83             :                 Create the spectral transformation object
      84             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      85             : 
      86           3 :   PetscCall(STCreate(PETSC_COMM_WORLD,&st));
      87           3 :   mat[0] = A;
      88           3 :   mat[1] = B;
      89           3 :   PetscCall(STSetMatrices(st,2,mat));
      90           3 :   mat[0] = Pa;
      91           3 :   mat[1] = Pb;
      92           3 :   PetscCall(STSetSplitPreconditioner(st,2,mat,SAME_NONZERO_PATTERN));
      93           3 :   PetscCall(STSetTransform(st,PETSC_TRUE));
      94           3 :   PetscCall(STSetFromOptions(st));
      95           3 :   PetscCall(STCayleySetAntishift(st,-0.2));   /* only relevant for cayley */
      96             : 
      97             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      98             :                Form the preconditioner matrix and print it
      99             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     100             : 
     101           3 :   PetscCall(STGetKSP(st,&ksp));
     102           3 :   PetscCall(KSPGetPC(ksp,&pc));
     103           3 :   PetscCall(STGetOperator(st,NULL));
     104           3 :   PetscCall(PCGetOperators(pc,NULL,&Pmat));
     105           3 :   PetscCall(MatView(Pmat,NULL));
     106             : 
     107             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     108             :                    Apply the operator
     109             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     110             : 
     111             :   /* sigma=0.0 */
     112           3 :   PetscCall(STSetUp(st));
     113           3 :   PetscCall(STGetType(st,&type));
     114           3 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"ST type %s\n",type));
     115           3 :   PetscCall(STApply(st,v,w));
     116           3 :   PetscCall(VecView(w,NULL));
     117             : 
     118             :   /* sigma=0.1 */
     119           3 :   sigma = 0.1;
     120           3 :   PetscCall(STSetShift(st,sigma));
     121           3 :   PetscCall(STGetShift(st,&sigma));
     122           3 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"With shift=%g\n",(double)PetscRealPart(sigma)));
     123           3 :   PetscCall(STGetOperator(st,NULL));
     124           3 :   PetscCall(PCGetOperators(pc,NULL,&Pmat));
     125           3 :   PetscCall(MatView(Pmat,NULL));
     126           3 :   PetscCall(STApply(st,v,w));
     127           3 :   PetscCall(VecView(w,NULL));
     128             : 
     129           3 :   PetscCall(STDestroy(&st));
     130           3 :   PetscCall(MatDestroy(&A));
     131           3 :   PetscCall(MatDestroy(&B));
     132           3 :   PetscCall(MatDestroy(&Pa));
     133           3 :   PetscCall(MatDestroy(&Pb));
     134           3 :   PetscCall(VecDestroy(&v));
     135           3 :   PetscCall(VecDestroy(&w));
     136           3 :   PetscCall(SlepcFinalize());
     137             :   return 0;
     138             : }
     139             : 
     140             : /*TEST
     141             : 
     142             :    test:
     143             :       suffix: 1
     144             :       args: -st_type {{cayley shift sinvert}separate output}
     145             :       requires: !single
     146             : 
     147             : TEST*/

Generated by: LCOV version 1.14