LCOV - code coverage report
Current view: top level - pep/tutorials/nlevp - acoustic_wave_1d.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 53 57 93.0 %
Date: 2024-11-21 00:40:22 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 acoustic_wave_1d problem is a QEP from an acoustics application.
      19             :    Here we solve it with the eigenvalue scaled by the imaginary unit, to be
      20             :    able to use real arithmetic, so the computed eigenvalues should be scaled
      21             :    back.
      22             : */
      23             : 
      24             : static char help[] = "Quadratic eigenproblem from an acoustics application (1-D).\n\n"
      25             :   "The command line options are:\n"
      26             :   "  -n <n>, where <n> = dimension of the matrices.\n"
      27             :   "  -z <z>, where <z> = impedance (default 1.0).\n\n";
      28             : 
      29             : #include <slepcpep.h>
      30             : 
      31          11 : int main(int argc,char **argv)
      32             : {
      33          11 :   Mat            M,C,K,A[3];      /* problem matrices */
      34          11 :   PEP            pep;             /* polynomial eigenproblem solver context */
      35          11 :   PetscInt       n=10,Istart,Iend,i;
      36          11 :   PetscScalar    z=1.0;
      37          11 :   char           str[50];
      38          11 :   PetscBool      terse;
      39             : 
      40          11 :   PetscFunctionBeginUser;
      41          11 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      42             : 
      43          11 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      44          11 :   PetscCall(PetscOptionsGetScalar(NULL,NULL,"-z",&z,NULL));
      45          11 :   PetscCall(SlepcSNPrintfScalar(str,sizeof(str),z,PETSC_FALSE));
      46          11 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nAcoustic wave 1-D, n=%" PetscInt_FMT " z=%s\n\n",n,str));
      47             : 
      48             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      49             :      Compute the matrices that define the eigensystem, (k^2*M+k*C+K)x=0
      50             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      51             : 
      52             :   /* K is a tridiagonal */
      53          11 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&K));
      54          11 :   PetscCall(MatSetSizes(K,PETSC_DECIDE,PETSC_DECIDE,n,n));
      55          11 :   PetscCall(MatSetFromOptions(K));
      56             : 
      57          11 :   PetscCall(MatGetOwnershipRange(K,&Istart,&Iend));
      58         275 :   for (i=Istart;i<Iend;i++) {
      59         264 :     if (i>0) PetscCall(MatSetValue(K,i,i-1,-1.0*n,INSERT_VALUES));
      60         264 :     if (i<n-1) {
      61         253 :       PetscCall(MatSetValue(K,i,i,2.0*n,INSERT_VALUES));
      62         253 :       PetscCall(MatSetValue(K,i,i+1,-1.0*n,INSERT_VALUES));
      63         264 :     } else PetscCall(MatSetValue(K,i,i,1.0*n,INSERT_VALUES));
      64             :   }
      65             : 
      66          11 :   PetscCall(MatAssemblyBegin(K,MAT_FINAL_ASSEMBLY));
      67          11 :   PetscCall(MatAssemblyEnd(K,MAT_FINAL_ASSEMBLY));
      68             : 
      69             :   /* C is the zero matrix but one element*/
      70          11 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&C));
      71          11 :   PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,n,n));
      72          11 :   PetscCall(MatSetFromOptions(C));
      73             : 
      74          11 :   PetscCall(MatGetOwnershipRange(C,&Istart,&Iend));
      75          11 :   if (n-1>=Istart && n-1<Iend) PetscCall(MatSetValue(C,n-1,n-1,-2*PETSC_PI/z,INSERT_VALUES));
      76          11 :   PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
      77          11 :   PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));
      78             : 
      79             :   /* M is a diagonal matrix */
      80          11 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&M));
      81          11 :   PetscCall(MatSetSizes(M,PETSC_DECIDE,PETSC_DECIDE,n,n));
      82          11 :   PetscCall(MatSetFromOptions(M));
      83             : 
      84          11 :   PetscCall(MatGetOwnershipRange(M,&Istart,&Iend));
      85         275 :   for (i=Istart;i<Iend;i++) {
      86         264 :     if (i<n-1) PetscCall(MatSetValue(M,i,i,4*PETSC_PI*PETSC_PI/n,INSERT_VALUES));
      87         264 :     else PetscCall(MatSetValue(M,i,i,2*PETSC_PI*PETSC_PI/n,INSERT_VALUES));
      88             :   }
      89          11 :   PetscCall(MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY));
      90          11 :   PetscCall(MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY));
      91             : 
      92             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      93             :                 Create the eigensolver and solve the problem
      94             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      95             : 
      96          11 :   PetscCall(PEPCreate(PETSC_COMM_WORLD,&pep));
      97          11 :   A[0] = K; A[1] = C; A[2] = M;
      98          11 :   PetscCall(PEPSetOperators(pep,3,A));
      99          11 :   PetscCall(PEPSetFromOptions(pep));
     100          11 :   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          11 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
     108          11 :   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          11 :   PetscCall(PEPDestroy(&pep));
     116          11 :   PetscCall(MatDestroy(&M));
     117          11 :   PetscCall(MatDestroy(&C));
     118          11 :   PetscCall(MatDestroy(&K));
     119          11 :   PetscCall(SlepcFinalize());
     120             :   return 0;
     121             : }
     122             : 
     123             : /*TEST
     124             : 
     125             :    testset:
     126             :       args: -pep_nev 4 -pep_tol 1e-7 -n 24 -terse
     127             :       output_file: output/acoustic_wave_1d_1.out
     128             :       requires: !single
     129             :       test:
     130             :          suffix: 1
     131             :          args: -st_type sinvert -st_transform -pep_type {{toar qarnoldi linear}}
     132             :       test:
     133             :          suffix: 1_stoar
     134             :          args: -st_type sinvert -st_transform -pep_type stoar -pep_hermitian -pep_stoar_locking 0 -pep_stoar_nev 11 -pep_ncv 10
     135             :       test:
     136             :          suffix: 2
     137             :          args: -st_type sinvert -st_transform -pep_type toar -pep_extract {{none norm residual}}
     138             :       test:
     139             :          suffix: 3
     140             :          args: -st_type sinvert -pep_type linear -pep_extract {{none norm residual}}
     141             :       test:
     142             :          suffix: 4
     143             :          args: -pep_type jd
     144             : 
     145             : TEST*/

Generated by: LCOV version 1.14