LCOV - code coverage report
Current view: top level - sys/classes/st/tests - test6.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 72 72 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 STPRECOND operations.\n\n";
      12             : 
      13             : #include <slepcst.h>
      14             : 
      15           3 : int main(int argc,char **argv)
      16             : {
      17           3 :   Mat            A,P,mat[1];
      18           3 :   ST             st;
      19           3 :   KSP            ksp;
      20           3 :   Vec            v,w;
      21           3 :   PetscScalar    sigma;
      22           3 :   PetscInt       n=10,i,Istart,Iend;
      23           3 :   STMatMode      matmode;
      24             : 
      25           3 :   PetscFunctionBeginUser;
      26           3 :   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
      27           3 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      28           3 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nPreconditioner for 1-D Laplacian, n=%" PetscInt_FMT "\n\n",n));
      29             : 
      30             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      31             :              Compute the operator matrix for the 1-D Laplacian
      32             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      33             : 
      34           3 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
      35           3 :   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n));
      36           3 :   PetscCall(MatSetFromOptions(A));
      37             : 
      38           3 :   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
      39          33 :   for (i=Istart;i<Iend;i++) {
      40          30 :     if (i>0) PetscCall(MatSetValue(A,i,i-1,-1.0,INSERT_VALUES));
      41          30 :     if (i<n-1) PetscCall(MatSetValue(A,i,i+1,-1.0,INSERT_VALUES));
      42          30 :     PetscCall(MatSetValue(A,i,i,2.0,INSERT_VALUES));
      43             :   }
      44           3 :   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
      45           3 :   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
      46           3 :   PetscCall(MatCreateVecs(A,&v,&w));
      47           3 :   PetscCall(VecSet(v,1.0));
      48             : 
      49             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      50             :                 Create the spectral transformation object
      51             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      52             : 
      53           3 :   PetscCall(STCreate(PETSC_COMM_WORLD,&st));
      54           3 :   mat[0] = A;
      55           3 :   PetscCall(STSetMatrices(st,1,mat));
      56           3 :   PetscCall(STSetType(st,STPRECOND));
      57           3 :   PetscCall(STGetKSP(st,&ksp));
      58           3 :   PetscCall(KSPSetType(ksp,KSPPREONLY));
      59           3 :   PetscCall(STSetFromOptions(st));
      60             : 
      61             :   /* set up */
      62             :   /* - the transform flag is necessary so that A-sigma*I is built explicitly */
      63           3 :   PetscCall(STSetTransform(st,PETSC_TRUE));
      64             :   /* - the ksphasmat flag is necessary when using STApply(), otherwise can only use PCApply() */
      65           3 :   PetscCall(STPrecondSetKSPHasMat(st,PETSC_TRUE));
      66             :   /* no need to call STSetUp() explicitly */
      67           3 :   PetscCall(STSetUp(st));
      68             : 
      69             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      70             :                        Apply the preconditioner
      71             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      72             : 
      73             :   /* default shift */
      74           3 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"With default shift\n"));
      75           3 :   PetscCall(STApply(st,v,w));
      76           3 :   PetscCall(VecView(w,NULL));
      77             : 
      78             :   /* change shift */
      79           3 :   sigma = 0.1;
      80           3 :   PetscCall(STSetShift(st,sigma));
      81           3 :   PetscCall(STGetShift(st,&sigma));
      82           3 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"With shift=%g\n",(double)PetscRealPart(sigma)));
      83           3 :   PetscCall(STApply(st,v,w));
      84           3 :   PetscCall(VecView(w,NULL));
      85           3 :   PetscCall(STPostSolve(st));   /* undo changes if inplace */
      86             : 
      87             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      88             :                  Test a user-provided preconditioner matrix
      89             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      90             : 
      91           3 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&P));
      92           3 :   PetscCall(MatSetSizes(P,PETSC_DECIDE,PETSC_DECIDE,n,n));
      93           3 :   PetscCall(MatSetFromOptions(P));
      94             : 
      95           3 :   PetscCall(MatGetOwnershipRange(P,&Istart,&Iend));
      96          33 :   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(P,i,i,2.0,INSERT_VALUES));
      97           3 :   if (Istart==0) {
      98           3 :     PetscCall(MatSetValue(P,1,0,-1.0,INSERT_VALUES));
      99           3 :     PetscCall(MatSetValue(P,0,1,-1.0,INSERT_VALUES));
     100             :   }
     101           3 :   PetscCall(MatAssemblyBegin(P,MAT_FINAL_ASSEMBLY));
     102           3 :   PetscCall(MatAssemblyEnd(P,MAT_FINAL_ASSEMBLY));
     103             : 
     104             :   /* apply new preconditioner */
     105           3 :   PetscCall(STSetPreconditionerMat(st,P));
     106           3 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"With user-provided matrix\n"));
     107           3 :   PetscCall(STApply(st,v,w));
     108           3 :   PetscCall(VecView(w,NULL));
     109             : 
     110             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     111             :             Test a user-provided preconditioner in split form
     112             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     113             : 
     114           3 :   PetscCall(STGetMatMode(st,&matmode));
     115           3 :   if (matmode==ST_MATMODE_COPY) {
     116           1 :     PetscCall(STSetPreconditionerMat(st,NULL));
     117           1 :     mat[0] = P;
     118           1 :     PetscCall(STSetSplitPreconditioner(st,1,mat,SAME_NONZERO_PATTERN));
     119             : 
     120             :     /* apply new preconditioner */
     121           1 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"With split preconditioner\n"));
     122           1 :     PetscCall(STApply(st,v,w));
     123           1 :     PetscCall(VecView(w,NULL));
     124             :   }
     125             : 
     126             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     127             :                              Clean up
     128             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     129           3 :   PetscCall(STDestroy(&st));
     130           3 :   PetscCall(MatDestroy(&A));
     131           3 :   PetscCall(MatDestroy(&P));
     132           3 :   PetscCall(VecDestroy(&v));
     133           3 :   PetscCall(VecDestroy(&w));
     134           3 :   PetscCall(SlepcFinalize());
     135             :   return 0;
     136             : }
     137             : 
     138             : /*TEST
     139             : 
     140             :    test:
     141             :       suffix: 1
     142             :       args: -st_matmode {{copy inplace shell}separate output}
     143             :       requires: !single
     144             : 
     145             : TEST*/

Generated by: LCOV version 1.14