LCOV - code coverage report
Current view: top level - nep/tests - test9.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 61 65 93.8 %
Date: 2024-12-18 00:51:33 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             :    This example implements one of the problems found at
      12             :        NLEVP: A Collection of Nonlinear Eigenvalue Problems,
      13             :        The University of Manchester.
      14             :    The details of the collection can be found at:
      15             :        [1] T. Betcke et al., "NLEVP: A Collection of Nonlinear Eigenvalue
      16             :            Problems", ACM Trans. Math. Software 39(2), Article 7, 2013.
      17             : 
      18             :    The loaded_string problem is a rational eigenvalue problem for the
      19             :    finite element model of a loaded vibrating string.
      20             : */
      21             : 
      22             : static char help[] = "Test the NLEIGS solver with FNCOMBINE.\n\n"
      23             :   "This is based on loaded_string from the NLEVP collection.\n"
      24             :   "The command line options are:\n"
      25             :   "  -n <n>, dimension of the matrices.\n"
      26             :   "  -kappa <kappa>, stiffness of elastic spring.\n"
      27             :   "  -mass <m>, mass of the attached load.\n\n";
      28             : 
      29             : #include <slepcnep.h>
      30             : 
      31             : #define NMAT 3
      32             : 
      33           1 : int main(int argc,char **argv)
      34             : {
      35           1 :   Mat            A[NMAT];         /* problem matrices */
      36           1 :   FN             f[NMAT],g;       /* functions to define the nonlinear operator */
      37           1 :   NEP            nep;             /* nonlinear eigensolver context */
      38           1 :   PetscInt       n=100,Istart,Iend,i;
      39           1 :   PetscReal      kappa=1.0,m=1.0;
      40           1 :   PetscScalar    sigma,numer[2],denom[2];
      41           1 :   PetscBool      terse;
      42             : 
      43           1 :   PetscFunctionBeginUser;
      44           1 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      45             : 
      46           1 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      47           1 :   PetscCall(PetscOptionsGetReal(NULL,NULL,"-kappa",&kappa,NULL));
      48           1 :   PetscCall(PetscOptionsGetReal(NULL,NULL,"-mass",&m,NULL));
      49           1 :   sigma = kappa/m;
      50           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Loaded vibrating string, n=%" PetscInt_FMT " kappa=%g m=%g\n\n",n,(double)kappa,(double)m));
      51             : 
      52             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      53             :                        Build the problem matrices
      54             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      55             : 
      56             :   /* initialize matrices */
      57           4 :   for (i=0;i<NMAT;i++) {
      58           3 :     PetscCall(MatCreate(PETSC_COMM_WORLD,&A[i]));
      59           3 :     PetscCall(MatSetSizes(A[i],PETSC_DECIDE,PETSC_DECIDE,n,n));
      60           3 :     PetscCall(MatSetFromOptions(A[i]));
      61             :   }
      62           1 :   PetscCall(MatGetOwnershipRange(A[0],&Istart,&Iend));
      63             : 
      64             :   /* A0 */
      65         101 :   for (i=Istart;i<Iend;i++) {
      66         100 :     PetscCall(MatSetValue(A[0],i,i,(i==n-1)?1.0*n:2.0*n,INSERT_VALUES));
      67         100 :     if (i>0) PetscCall(MatSetValue(A[0],i,i-1,-1.0*n,INSERT_VALUES));
      68         100 :     if (i<n-1) PetscCall(MatSetValue(A[0],i,i+1,-1.0*n,INSERT_VALUES));
      69             :   }
      70             : 
      71             :   /* A1 */
      72         101 :   for (i=Istart;i<Iend;i++) {
      73         100 :     PetscCall(MatSetValue(A[1],i,i,(i==n-1)?2.0/(6.0*n):4.0/(6.0*n),INSERT_VALUES));
      74         100 :     if (i>0) PetscCall(MatSetValue(A[1],i,i-1,1.0/(6.0*n),INSERT_VALUES));
      75         100 :     if (i<n-1) PetscCall(MatSetValue(A[1],i,i+1,1.0/(6.0*n),INSERT_VALUES));
      76             :   }
      77             : 
      78             :   /* A2 */
      79           1 :   if (Istart<=n-1 && n-1<Iend) PetscCall(MatSetValue(A[2],n-1,n-1,kappa,INSERT_VALUES));
      80             : 
      81             :   /* assemble matrices */
      82           4 :   for (i=0;i<NMAT;i++) PetscCall(MatAssemblyBegin(A[i],MAT_FINAL_ASSEMBLY));
      83           4 :   for (i=0;i<NMAT;i++) PetscCall(MatAssemblyEnd(A[i],MAT_FINAL_ASSEMBLY));
      84             : 
      85             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      86             :                        Create the problem functions
      87             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      88             : 
      89             :   /* f1=1 */
      90           1 :   PetscCall(FNCreate(PETSC_COMM_WORLD,&f[0]));
      91           1 :   PetscCall(FNSetType(f[0],FNRATIONAL));
      92           1 :   numer[0] = 1.0;
      93           1 :   PetscCall(FNRationalSetNumerator(f[0],1,numer));
      94             : 
      95             :   /* f2=-lambda */
      96           1 :   PetscCall(FNCreate(PETSC_COMM_WORLD,&f[1]));
      97           1 :   PetscCall(FNSetType(f[1],FNRATIONAL));
      98           1 :   numer[0] = -1.0; numer[1] = 0.0;
      99           1 :   PetscCall(FNRationalSetNumerator(f[1],2,numer));
     100             : 
     101             :   /* f3=lambda/(lambda-sigma)=1+sigma/(lambda-sigma) */
     102           1 :   PetscCall(FNCreate(PETSC_COMM_WORLD,&g));
     103           1 :   PetscCall(FNSetType(g,FNRATIONAL));
     104           1 :   numer[0] = sigma;
     105           1 :   denom[0] = 1.0; denom[1] = -sigma;
     106           1 :   PetscCall(FNRationalSetNumerator(g,1,numer));
     107           1 :   PetscCall(FNRationalSetDenominator(g,2,denom));
     108           1 :   PetscCall(FNCreate(PETSC_COMM_WORLD,&f[2]));
     109           1 :   PetscCall(FNSetType(f[2],FNCOMBINE));
     110           1 :   PetscCall(FNCombineSetChildren(f[2],FN_COMBINE_ADD,f[0],g));
     111             : 
     112             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     113             :                 Create the eigensolver and solve the problem
     114             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     115             : 
     116           1 :   PetscCall(NEPCreate(PETSC_COMM_WORLD,&nep));
     117           1 :   PetscCall(NEPSetSplitOperator(nep,3,A,f,SUBSET_NONZERO_PATTERN));
     118           1 :   PetscCall(NEPSetProblemType(nep,NEP_RATIONAL));
     119           1 :   PetscCall(NEPSetFromOptions(nep));
     120           1 :   PetscCall(NEPSolve(nep));
     121             : 
     122             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     123             :                     Display solution and clean up
     124             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     125             : 
     126             :   /* show detailed info unless -terse option is given by user */
     127           1 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
     128           1 :   if (terse) PetscCall(NEPErrorView(nep,NEP_ERROR_RELATIVE,NULL));
     129             :   else {
     130           0 :     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
     131           0 :     PetscCall(NEPConvergedReasonView(nep,PETSC_VIEWER_STDOUT_WORLD));
     132           0 :     PetscCall(NEPErrorView(nep,NEP_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
     133           0 :     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
     134             :   }
     135           1 :   PetscCall(NEPDestroy(&nep));
     136           4 :   for (i=0;i<NMAT;i++) {
     137           3 :     PetscCall(MatDestroy(&A[i]));
     138           3 :     PetscCall(FNDestroy(&f[i]));
     139             :   }
     140           1 :   PetscCall(FNDestroy(&g));
     141           1 :   PetscCall(SlepcFinalize());
     142             :   return 0;
     143             : }
     144             : 
     145             : /*TEST
     146             : 
     147             :    test:
     148             :       suffix: 1
     149             :       args: -nep_type nleigs -rg_type interval -rg_interval_endpoints 4,700,-.1,.1 -nep_nev 8 -nep_target 5 -terse
     150             :       filter: sed -e "s/[+-]0\.0*i//g"
     151             :       requires: !single
     152             : 
     153             : TEST*/

Generated by: LCOV version 1.14