LCOV - code coverage report
Current view: top level - pep/tests - test3.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 114 114 100.0 %
Date: 2024-12-18 00:42:09 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 PEP interface functions.\n\n";
      12             : 
      13             : #include <slepcpep.h>
      14             : 
      15           1 : int main(int argc,char **argv)
      16             : {
      17           1 :   Mat                A[3],B;      /* problem matrices */
      18           1 :   PEP                pep;         /* eigenproblem solver context */
      19           1 :   ST                 st;
      20           1 :   KSP                ksp;
      21           1 :   Vec                Dl,Dr;
      22           1 :   DS                 ds;
      23           1 :   PetscReal          tol,alpha;
      24           1 :   PetscScalar        target;
      25           1 :   PetscInt           n=20,i,its,nev,ncv,mpd,Istart,Iend,nmat;
      26           1 :   PEPWhich           which;
      27           1 :   PEPConvergedReason reason;
      28           1 :   PEPType            type;
      29           1 :   PEPExtract         extr;
      30           1 :   PEPBasis           basis;
      31           1 :   PEPScale           scale;
      32           1 :   PEPRefine          refine;
      33           1 :   PEPRefineScheme    rscheme;
      34           1 :   PEPConv            conv;
      35           1 :   PEPStop            stop;
      36           1 :   PEPProblemType     ptype;
      37           1 :   PetscViewerAndFormat *vf;
      38             : 
      39           1 :   PetscFunctionBeginUser;
      40           1 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      41           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nDiagonal Quadratic Eigenproblem, n=%" PetscInt_FMT "\n\n",n));
      42             : 
      43           1 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A[0]));
      44           1 :   PetscCall(MatSetSizes(A[0],PETSC_DECIDE,PETSC_DECIDE,n,n));
      45           1 :   PetscCall(MatSetFromOptions(A[0]));
      46           1 :   PetscCall(MatGetOwnershipRange(A[0],&Istart,&Iend));
      47          21 :   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A[0],i,i,i+1,INSERT_VALUES));
      48           1 :   PetscCall(MatAssemblyBegin(A[0],MAT_FINAL_ASSEMBLY));
      49           1 :   PetscCall(MatAssemblyEnd(A[0],MAT_FINAL_ASSEMBLY));
      50             : 
      51           1 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A[1]));
      52           1 :   PetscCall(MatSetSizes(A[1],PETSC_DECIDE,PETSC_DECIDE,n,n));
      53           1 :   PetscCall(MatSetFromOptions(A[1]));
      54           1 :   PetscCall(MatGetOwnershipRange(A[1],&Istart,&Iend));
      55          21 :   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A[1],i,i,1.0,INSERT_VALUES));
      56           1 :   PetscCall(MatAssemblyBegin(A[1],MAT_FINAL_ASSEMBLY));
      57           1 :   PetscCall(MatAssemblyEnd(A[1],MAT_FINAL_ASSEMBLY));
      58             : 
      59           1 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A[2]));
      60           1 :   PetscCall(MatSetSizes(A[2],PETSC_DECIDE,PETSC_DECIDE,n,n));
      61           1 :   PetscCall(MatSetFromOptions(A[2]));
      62           1 :   PetscCall(MatGetOwnershipRange(A[1],&Istart,&Iend));
      63          21 :   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A[2],i,i,n/(PetscReal)(i+1),INSERT_VALUES));
      64           1 :   PetscCall(MatAssemblyBegin(A[2],MAT_FINAL_ASSEMBLY));
      65           1 :   PetscCall(MatAssemblyEnd(A[2],MAT_FINAL_ASSEMBLY));
      66             : 
      67           1 :   PetscCall(MatCreateVecs(A[0],&Dr,&Dl));
      68           1 :   PetscCall(VecSet(Dl,1.0));
      69           1 :   PetscCall(VecSet(Dr,0.95));
      70             : 
      71             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      72             :              Create eigensolver and test interface functions
      73             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      74           1 :   PetscCall(PEPCreate(PETSC_COMM_WORLD,&pep));
      75           1 :   PetscCall(PEPSetOperators(pep,3,A));
      76           1 :   PetscCall(PEPGetNumMatrices(pep,&nmat));
      77           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Polynomial of degree %" PetscInt_FMT "\n",nmat-1));
      78           1 :   PetscCall(PEPGetOperators(pep,0,&B));
      79           1 :   PetscCall(MatView(B,NULL));
      80             : 
      81           1 :   PetscCall(PEPSetType(pep,PEPTOAR));
      82           1 :   PetscCall(PEPGetType(pep,&type));
      83           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Type set to %s\n",type));
      84             : 
      85           1 :   PetscCall(PEPGetProblemType(pep,&ptype));
      86           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Problem type before changing = %d",(int)ptype));
      87           1 :   PetscCall(PEPSetProblemType(pep,PEP_HERMITIAN));
      88           1 :   PetscCall(PEPGetProblemType(pep,&ptype));
      89           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," ... changed to %d.\n",(int)ptype));
      90             : 
      91           1 :   PetscCall(PEPGetExtract(pep,&extr));
      92           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Extraction before changing = %d",(int)extr));
      93           1 :   PetscCall(PEPSetExtract(pep,PEP_EXTRACT_STRUCTURED));
      94           1 :   PetscCall(PEPGetExtract(pep,&extr));
      95           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," ... changed to %d\n",(int)extr));
      96             : 
      97           1 :   PetscCall(PEPSetScale(pep,PEP_SCALE_BOTH,.1,Dl,Dr,5,1.0));
      98           1 :   PetscCall(PEPGetScale(pep,&scale,&alpha,NULL,NULL,&its,NULL));
      99           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Scaling: %s, alpha=%g, its=%" PetscInt_FMT "\n",PEPScaleTypes[scale],(double)alpha,its));
     100             : 
     101           1 :   PetscCall(PEPSetBasis(pep,PEP_BASIS_CHEBYSHEV1));
     102           1 :   PetscCall(PEPGetBasis(pep,&basis));
     103           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Polynomial basis: %s\n",PEPBasisTypes[basis]));
     104             : 
     105           1 :   PetscCall(PEPSetRefine(pep,PEP_REFINE_SIMPLE,1,1e-9,2,PEP_REFINE_SCHEME_SCHUR));
     106           1 :   PetscCall(PEPGetRefine(pep,&refine,NULL,&tol,&its,&rscheme));
     107           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Refinement: %s, tol=%g, its=%" PetscInt_FMT ", scheme=%s\n",PEPRefineTypes[refine],(double)tol,its,PEPRefineSchemes[rscheme]));
     108             : 
     109           1 :   PetscCall(PEPSetTarget(pep,4.8));
     110           1 :   PetscCall(PEPGetTarget(pep,&target));
     111           1 :   PetscCall(PEPSetWhichEigenpairs(pep,PEP_TARGET_MAGNITUDE));
     112           1 :   PetscCall(PEPGetWhichEigenpairs(pep,&which));
     113           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Which = %d, target = %g\n",(int)which,(double)PetscRealPart(target)));
     114             : 
     115           1 :   PetscCall(PEPSetDimensions(pep,4,PETSC_DETERMINE,PETSC_DETERMINE));
     116           1 :   PetscCall(PEPGetDimensions(pep,&nev,&ncv,&mpd));
     117           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Dimensions: nev=%" PetscInt_FMT ", ncv=%" PetscInt_FMT ", mpd=%" PetscInt_FMT "\n",nev,ncv,mpd));
     118             : 
     119           1 :   PetscCall(PEPSetTolerances(pep,2.2e-4,200));
     120           1 :   PetscCall(PEPGetTolerances(pep,&tol,&its));
     121           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Tolerance = %.5f, max_its = %" PetscInt_FMT "\n",(double)tol,its));
     122             : 
     123           1 :   PetscCall(PEPSetConvergenceTest(pep,PEP_CONV_ABS));
     124           1 :   PetscCall(PEPGetConvergenceTest(pep,&conv));
     125           1 :   PetscCall(PEPSetStoppingTest(pep,PEP_STOP_BASIC));
     126           1 :   PetscCall(PEPGetStoppingTest(pep,&stop));
     127           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Convergence test = %d, stopping test = %d\n",(int)conv,(int)stop));
     128             : 
     129           1 :   PetscCall(PetscViewerAndFormatCreate(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_DEFAULT,&vf));
     130           1 :   PetscCall(PEPMonitorSet(pep,(PetscErrorCode (*)(PEP,PetscInt,PetscInt,PetscScalar*,PetscScalar*,PetscReal*,PetscInt,void*))PEPMonitorFirst,vf,(PetscCtxDestroyFn*)PetscViewerAndFormatDestroy));
     131           1 :   PetscCall(PEPMonitorCancel(pep));
     132             : 
     133           1 :   PetscCall(PEPGetST(pep,&st));
     134           1 :   PetscCall(STGetKSP(st,&ksp));
     135           1 :   PetscCall(KSPSetTolerances(ksp,1e-8,1e-35,PETSC_CURRENT,PETSC_CURRENT));
     136           1 :   PetscCall(STView(st,NULL));
     137           1 :   PetscCall(PEPGetDS(pep,&ds));
     138           1 :   PetscCall(DSView(ds,NULL));
     139             : 
     140           1 :   PetscCall(PEPSetFromOptions(pep));
     141           1 :   PetscCall(PEPSolve(pep));
     142           1 :   PetscCall(PEPGetConvergedReason(pep,&reason));
     143           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Finished - converged reason = %d\n",(int)reason));
     144             : 
     145             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     146             :                     Display solution and clean up
     147             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     148           1 :   PetscCall(PEPErrorView(pep,PEP_ERROR_RELATIVE,NULL));
     149           1 :   PetscCall(PEPDestroy(&pep));
     150           1 :   PetscCall(MatDestroy(&A[0]));
     151           1 :   PetscCall(MatDestroy(&A[1]));
     152           1 :   PetscCall(MatDestroy(&A[2]));
     153           1 :   PetscCall(VecDestroy(&Dl));
     154           1 :   PetscCall(VecDestroy(&Dr));
     155           1 :   PetscCall(SlepcFinalize());
     156             :   return 0;
     157             : }
     158             : 
     159             : /*TEST
     160             : 
     161             :    test:
     162             :       suffix: 1
     163             :       args: -pep_tol 1e-6 -pep_ncv 22
     164             :       filter: sed -e "s/[+-]0\.0*i//g"
     165             : 
     166             : TEST*/

Generated by: LCOV version 1.14