LCOV - code coverage report
Current view: top level - pep/tutorials/nlevp - loaded_string.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 46 50 92.0 %
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             :    This example solves the loaded_string problem by first transforming
      21             :    it to a quadratic eigenvalue problem.
      22             : */
      23             : 
      24             : static char help[] = "Finite element model of a loaded vibrating string.\n\n"
      25             :   "The command line options are:\n"
      26             :   "  -n <n>, dimension of the matrices.\n"
      27             :   "  -kappa <kappa>, stiffness of elastic spring.\n"
      28             :   "  -mass <m>, mass of the attached load.\n\n";
      29             : 
      30             : #include <slepcpep.h>
      31             : 
      32             : #define NMAT 3
      33             : 
      34           1 : int main(int argc,char **argv)
      35             : {
      36           1 :   Mat            A[3],M;      /* problem matrices */
      37           1 :   PEP            pep;         /* polynomial eigenproblem solver context */
      38           1 :   PetscInt       n=100,Istart,Iend,i;
      39           1 :   PetscBool      terse;
      40           1 :   PetscReal      kappa=1.0,m=1.0;
      41           1 :   PetscScalar    sigma;
      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 (QEP), n=%" PetscInt_FMT " kappa=%g m=%g\n\n",n,(double)kappa,(double)m));
      51             : 
      52             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      53             :      Compute the matrices that define the eigensystem, (k^2*M+k*C+K)x=0
      54             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      55             :   /* initialize matrices */
      56           4 :   for (i=0;i<NMAT;i++) {
      57           3 :     PetscCall(MatCreate(PETSC_COMM_WORLD,&A[i]));
      58           3 :     PetscCall(MatSetSizes(A[i],PETSC_DECIDE,PETSC_DECIDE,n,n));
      59           3 :     PetscCall(MatSetFromOptions(A[i]));
      60             :   }
      61           1 :   PetscCall(MatGetOwnershipRange(A[0],&Istart,&Iend));
      62             : 
      63             :   /* A0 */
      64         101 :   for (i=Istart;i<Iend;i++) {
      65         100 :     PetscCall(MatSetValue(A[0],i,i,(i==n-1)?1.0*n:2.0*n,INSERT_VALUES));
      66         100 :     if (i>0) PetscCall(MatSetValue(A[0],i,i-1,-1.0*n,INSERT_VALUES));
      67         100 :     if (i<n-1) PetscCall(MatSetValue(A[0],i,i+1,-1.0*n,INSERT_VALUES));
      68             :   }
      69             : 
      70             :   /* A1 */
      71         101 :   for (i=Istart;i<Iend;i++) {
      72         100 :     PetscCall(MatSetValue(A[1],i,i,(i==n-1)?2.0/(6.0*n):4.0/(6.0*n),INSERT_VALUES));
      73         100 :     if (i>0) PetscCall(MatSetValue(A[1],i,i-1,1.0/(6.0*n),INSERT_VALUES));
      74         100 :     if (i<n-1) PetscCall(MatSetValue(A[1],i,i+1,1.0/(6.0*n),INSERT_VALUES));
      75             :   }
      76             : 
      77             :   /* A2 */
      78           1 :   if (Istart<=n-1 && n-1<Iend) PetscCall(MatSetValue(A[2],n-1,n-1,kappa,INSERT_VALUES));
      79             : 
      80             :   /* assemble matrices */
      81           4 :   for (i=0;i<NMAT;i++) PetscCall(MatAssemblyBegin(A[i],MAT_FINAL_ASSEMBLY));
      82           4 :   for (i=0;i<NMAT;i++) PetscCall(MatAssemblyEnd(A[i],MAT_FINAL_ASSEMBLY));
      83             : 
      84             :   /* build matrices for the QEP */
      85           1 :   PetscCall(MatAXPY(A[2],1.0,A[0],DIFFERENT_NONZERO_PATTERN));
      86           1 :   PetscCall(MatAXPY(A[2],sigma,A[1],SAME_NONZERO_PATTERN));
      87           1 :   PetscCall(MatScale(A[2],-1.0));
      88           1 :   PetscCall(MatScale(A[0],sigma));
      89           1 :   M = A[1];
      90           1 :   A[1] = A[2];
      91           1 :   A[2] = M;
      92             : 
      93             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      94             :                 Create the eigensolver and solve the problem
      95             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      96             : 
      97           1 :   PetscCall(PEPCreate(PETSC_COMM_WORLD,&pep));
      98           1 :   PetscCall(PEPSetOperators(pep,3,A));
      99           1 :   PetscCall(PEPSetFromOptions(pep));
     100           1 :   PetscCall(PEPSolve(pep));
     101             : 
     102             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     103             :                     Display solution and clean up
     104             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     105             : 
     106             :   /* show detailed info unless -terse option is given by user */
     107           1 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
     108           1 :   if (terse) PetscCall(PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL));
     109             :   else {
     110           0 :     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
     111           0 :     PetscCall(PEPConvergedReasonView(pep,PETSC_VIEWER_STDOUT_WORLD));
     112           0 :     PetscCall(PEPErrorView(pep,PEP_ERROR_BACKWARD,PETSC_VIEWER_STDOUT_WORLD));
     113           0 :     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
     114             :   }
     115           1 :   PetscCall(PEPDestroy(&pep));
     116           4 :   for (i=0;i<NMAT;i++) PetscCall(MatDestroy(&A[i]));
     117           1 :   PetscCall(SlepcFinalize());
     118             :   return 0;
     119             : }
     120             : 
     121             : /*TEST
     122             : 
     123             :    test:
     124             :       suffix: 1
     125             :       args: -pep_hyperbolic -pep_interval 4,900 -pep_type stoar -st_type sinvert -st_pc_type cholesky -terse
     126             :       requires: !single
     127             : 
     128             : TEST*/

Generated by: LCOV version 1.14