LCOV - code coverage report
Current view: top level - nep/tests - test2.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 110 110 100.0 %
Date: 2024-11-21 00:34:55 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 NEP interface functions.\n\n";
      12             : 
      13             : #include <slepcnep.h>
      14             : 
      15           1 : int main(int argc,char **argv)
      16             : {
      17           1 :   Mat                  A[3],B;      /* problem matrices */
      18           1 :   FN                   f[3],g;      /* problem functions */
      19           1 :   NEP                  nep;         /* eigenproblem solver context */
      20           1 :   DS                   ds;
      21           1 :   RG                   rg;
      22           1 :   PetscReal            tol;
      23           1 :   PetscScalar          coeffs[2],target;
      24           1 :   PetscInt             n=20,i,its,nev,ncv,mpd,Istart,Iend,nterm;
      25           1 :   PetscBool            twoside;
      26           1 :   NEPWhich             which;
      27           1 :   NEPConvergedReason   reason;
      28           1 :   NEPType              type;
      29           1 :   NEPRefine            refine;
      30           1 :   NEPRefineScheme      rscheme;
      31           1 :   NEPConv              conv;
      32           1 :   NEPStop              stop;
      33           1 :   NEPProblemType       ptype;
      34           1 :   MatStructure         mstr;
      35           1 :   PetscViewerAndFormat *vf;
      36             : 
      37           1 :   PetscFunctionBeginUser;
      38           1 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      39           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nDiagonal Nonlinear Eigenproblem, n=%" PetscInt_FMT "\n\n",n));
      40             : 
      41             :   /*
      42             :      Matrices
      43             :   */
      44           1 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A[0]));
      45           1 :   PetscCall(MatSetSizes(A[0],PETSC_DECIDE,PETSC_DECIDE,n,n));
      46           1 :   PetscCall(MatSetFromOptions(A[0]));
      47           1 :   PetscCall(MatGetOwnershipRange(A[0],&Istart,&Iend));
      48          21 :   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A[0],i,i,i+1,INSERT_VALUES));
      49           1 :   PetscCall(MatAssemblyBegin(A[0],MAT_FINAL_ASSEMBLY));
      50           1 :   PetscCall(MatAssemblyEnd(A[0],MAT_FINAL_ASSEMBLY));
      51             : 
      52           1 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A[1]));
      53           1 :   PetscCall(MatSetSizes(A[1],PETSC_DECIDE,PETSC_DECIDE,n,n));
      54           1 :   PetscCall(MatSetFromOptions(A[1]));
      55           1 :   PetscCall(MatGetOwnershipRange(A[1],&Istart,&Iend));
      56          21 :   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A[1],i,i,1.0,INSERT_VALUES));
      57           1 :   PetscCall(MatAssemblyBegin(A[1],MAT_FINAL_ASSEMBLY));
      58           1 :   PetscCall(MatAssemblyEnd(A[1],MAT_FINAL_ASSEMBLY));
      59             : 
      60           1 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A[2]));
      61           1 :   PetscCall(MatSetSizes(A[2],PETSC_DECIDE,PETSC_DECIDE,n,n));
      62           1 :   PetscCall(MatSetFromOptions(A[2]));
      63           1 :   PetscCall(MatGetOwnershipRange(A[1],&Istart,&Iend));
      64          21 :   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(A[2],i,i,n/(PetscReal)(i+1),INSERT_VALUES));
      65           1 :   PetscCall(MatAssemblyBegin(A[2],MAT_FINAL_ASSEMBLY));
      66           1 :   PetscCall(MatAssemblyEnd(A[2],MAT_FINAL_ASSEMBLY));
      67             : 
      68             :   /*
      69             :      Functions: f0=-lambda, f1=1.0, f2=sqrt(lambda)
      70             :   */
      71           1 :   PetscCall(FNCreate(PETSC_COMM_WORLD,&f[0]));
      72           1 :   PetscCall(FNSetType(f[0],FNRATIONAL));
      73           1 :   coeffs[0] = -1.0; coeffs[1] = 0.0;
      74           1 :   PetscCall(FNRationalSetNumerator(f[0],2,coeffs));
      75             : 
      76           1 :   PetscCall(FNCreate(PETSC_COMM_WORLD,&f[1]));
      77           1 :   PetscCall(FNSetType(f[1],FNRATIONAL));
      78           1 :   coeffs[0] = 1.0;
      79           1 :   PetscCall(FNRationalSetNumerator(f[1],1,coeffs));
      80             : 
      81           1 :   PetscCall(FNCreate(PETSC_COMM_WORLD,&f[2]));
      82           1 :   PetscCall(FNSetType(f[2],FNSQRT));
      83             : 
      84             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      85             :              Create eigensolver and test interface functions
      86             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      87           1 :   PetscCall(NEPCreate(PETSC_COMM_WORLD,&nep));
      88           1 :   PetscCall(NEPSetSplitOperator(nep,3,A,f,SAME_NONZERO_PATTERN));
      89           1 :   PetscCall(NEPGetSplitOperatorInfo(nep,&nterm,&mstr));
      90           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Nonlinear function with %" PetscInt_FMT " terms, with %s nonzero pattern\n",nterm,MatStructures[mstr]));
      91           1 :   PetscCall(NEPGetSplitOperatorTerm(nep,0,&B,&g));
      92           1 :   PetscCall(MatView(B,NULL));
      93           1 :   PetscCall(FNView(g,NULL));
      94             : 
      95           1 :   PetscCall(NEPSetType(nep,NEPRII));
      96           1 :   PetscCall(NEPGetType(nep,&type));
      97           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Type set to %s\n",type));
      98           1 :   PetscCall(NEPGetTwoSided(nep,&twoside));
      99           2 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Two-sided flag = %s\n",twoside?"true":"false"));
     100             : 
     101           1 :   PetscCall(NEPGetProblemType(nep,&ptype));
     102           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Problem type before changing = %d",(int)ptype));
     103           1 :   PetscCall(NEPSetProblemType(nep,NEP_RATIONAL));
     104           1 :   PetscCall(NEPGetProblemType(nep,&ptype));
     105           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," ... changed to %d.\n",(int)ptype));
     106             : 
     107           1 :   PetscCall(NEPSetRefine(nep,NEP_REFINE_SIMPLE,1,1e-9,2,NEP_REFINE_SCHEME_EXPLICIT));
     108           1 :   PetscCall(NEPGetRefine(nep,&refine,NULL,&tol,&its,&rscheme));
     109           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Refinement: %s, tol=%g, its=%" PetscInt_FMT ", scheme=%s\n",NEPRefineTypes[refine],(double)tol,its,NEPRefineSchemes[rscheme]));
     110             : 
     111           1 :   PetscCall(NEPSetTarget(nep,1.1));
     112           1 :   PetscCall(NEPGetTarget(nep,&target));
     113           1 :   PetscCall(NEPSetWhichEigenpairs(nep,NEP_TARGET_MAGNITUDE));
     114           1 :   PetscCall(NEPGetWhichEigenpairs(nep,&which));
     115           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Which = %d, target = %g\n",(int)which,(double)PetscRealPart(target)));
     116             : 
     117           1 :   PetscCall(NEPSetDimensions(nep,1,12,PETSC_CURRENT));
     118           1 :   PetscCall(NEPGetDimensions(nep,&nev,&ncv,&mpd));
     119           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Dimensions: nev=%" PetscInt_FMT ", ncv=%" PetscInt_FMT ", mpd=%" PetscInt_FMT "\n",nev,ncv,mpd));
     120             : 
     121           1 :   PetscCall(NEPSetTolerances(nep,1.0e-6,200));
     122           1 :   PetscCall(NEPGetTolerances(nep,&tol,&its));
     123           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Tolerance = %.6f, max_its = %" PetscInt_FMT "\n",(double)tol,its));
     124             : 
     125           1 :   PetscCall(NEPSetConvergenceTest(nep,NEP_CONV_ABS));
     126           1 :   PetscCall(NEPGetConvergenceTest(nep,&conv));
     127           1 :   PetscCall(NEPSetStoppingTest(nep,NEP_STOP_BASIC));
     128           1 :   PetscCall(NEPGetStoppingTest(nep,&stop));
     129           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Convergence test = %d, stopping test = %d\n",(int)conv,(int)stop));
     130             : 
     131           1 :   PetscCall(PetscViewerAndFormatCreate(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_DEFAULT,&vf));
     132           1 :   PetscCall(NEPMonitorSet(nep,(PetscErrorCode (*)(NEP,PetscInt,PetscInt,PetscScalar*,PetscScalar*,PetscReal*,PetscInt,void*))NEPMonitorFirst,vf,(PetscCtxDestroyFn*)PetscViewerAndFormatDestroy));
     133           1 :   PetscCall(NEPMonitorCancel(nep));
     134             : 
     135           1 :   PetscCall(NEPGetDS(nep,&ds));
     136           1 :   PetscCall(DSView(ds,NULL));
     137           1 :   PetscCall(NEPSetFromOptions(nep));
     138             : 
     139           1 :   PetscCall(NEPGetRG(nep,&rg));
     140           1 :   PetscCall(RGView(rg,NULL));
     141             : 
     142           1 :   PetscCall(NEPSolve(nep));
     143           1 :   PetscCall(NEPGetConvergedReason(nep,&reason));
     144           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Finished - converged reason = %d\n",(int)reason));
     145             : 
     146             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     147             :                     Display solution and clean up
     148             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     149           1 :   PetscCall(NEPErrorView(nep,NEP_ERROR_RELATIVE,NULL));
     150           1 :   PetscCall(NEPDestroy(&nep));
     151           1 :   PetscCall(MatDestroy(&A[0]));
     152           1 :   PetscCall(MatDestroy(&A[1]));
     153           1 :   PetscCall(MatDestroy(&A[2]));
     154           1 :   PetscCall(FNDestroy(&f[0]));
     155           1 :   PetscCall(FNDestroy(&f[1]));
     156           1 :   PetscCall(FNDestroy(&f[2]));
     157           1 :   PetscCall(SlepcFinalize());
     158             :   return 0;
     159             : }
     160             : 
     161             : /*TEST
     162             : 
     163             :    test:
     164             :       suffix: 1
     165             :       args: -nep_view
     166             :       filter: grep -v tolerance
     167             : 
     168             : TEST*/

Generated by: LCOV version 1.14