LCOV - code coverage report
Current view: top level - pep/tutorials/nlevp - damped_beam.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 91 95 95.8 %
Date: 2024-11-23 00:34:26 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 damped_beam problem is a QEP from the vibrarion analysis of a beam
      19             :    simply supported at both ends and damped in the middle.
      20             : */
      21             : 
      22             : static char help[] = "Quadratic eigenproblem from the vibrarion analysis of a beam.\n\n"
      23             :   "The command line options are:\n"
      24             :   "  -n <n> ... dimension of the matrices.\n\n";
      25             : 
      26             : #include <slepcpep.h>
      27             : 
      28           4 : int main(int argc,char **argv)
      29             : {
      30           4 :   Mat            M,Mo,C,K,Ko,A[3]; /* problem matrices */
      31           4 :   PEP            pep;              /* polynomial eigenproblem solver context */
      32           4 :   IS             isf,isbc,is;
      33           4 :   PetscInt       n=200,nele,Istart,Iend,i,j,mloc,nloc,bc[2];
      34           4 :   PetscReal      width=0.05,height=0.005,glength=1.0,dlen,EI,area,rho;
      35           4 :   PetscScalar    K1[4],K2[4],K2t[4],K3[4],M1[4],M2[4],M2t[4],M3[4],damp=5.0;
      36           4 :   PetscBool      terse;
      37             : 
      38           4 :   PetscFunctionBeginUser;
      39           4 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      40             : 
      41           4 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      42           4 :   nele = n/2;
      43           4 :   n    = 2*nele;
      44           4 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nSimply supported beam damped in the middle, n=%" PetscInt_FMT " (nele=%" PetscInt_FMT ")\n\n",n,nele));
      45             : 
      46           4 :   dlen = glength/nele;
      47           4 :   EI   = 7e10*width*height*height*height/12.0;
      48           4 :   area = width*height;
      49           4 :   rho  = 0.674/(area*glength);
      50             : 
      51           4 :   K1[0]  =  12;  K1[1]  =   6*dlen;  K1[2]  =   6*dlen;  K1[3]  =  4*dlen*dlen;
      52           4 :   K2[0]  = -12;  K2[1]  =   6*dlen;  K2[2]  =  -6*dlen;  K2[3]  =  2*dlen*dlen;
      53           4 :   K2t[0] = -12;  K2t[1] =  -6*dlen;  K2t[2] =   6*dlen;  K2t[3] =  2*dlen*dlen;
      54           4 :   K3[0]  =  12;  K3[1]  =  -6*dlen;  K3[2]  =  -6*dlen;  K3[3]  =  4*dlen*dlen;
      55           4 :   M1[0]  = 156;  M1[1]  =  22*dlen;  M1[2]  =  22*dlen;  M1[3]  =  4*dlen*dlen;
      56           4 :   M2[0]  =  54;  M2[1]  = -13*dlen;  M2[2]  =  13*dlen;  M2[3]  = -3*dlen*dlen;
      57           4 :   M2t[0] =  54;  M2t[1] =  13*dlen;  M2t[2] = -13*dlen;  M2t[3] = -3*dlen*dlen;
      58           4 :   M3[0]  = 156;  M3[1]  = -22*dlen;  M3[2]  = -22*dlen;  M3[3]  =  4*dlen*dlen;
      59             : 
      60             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      61             :      Compute the matrices that define the eigensystem, (k^2*M+k*C+K)x=0
      62             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      63             : 
      64             :   /* K is block-tridiagonal */
      65           4 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&Ko));
      66           4 :   PetscCall(MatSetSizes(Ko,PETSC_DECIDE,PETSC_DECIDE,n+2,n+2));
      67           4 :   PetscCall(MatSetBlockSize(Ko,2));
      68           4 :   PetscCall(MatSetFromOptions(Ko));
      69             : 
      70           4 :   PetscCall(MatGetOwnershipRange(Ko,&Istart,&Iend));
      71         408 :   for (i=Istart/2;i<Iend/2;i++) {
      72         404 :     if (i>0) {
      73         400 :       j = i-1;
      74         400 :       PetscCall(MatSetValuesBlocked(Ko,1,&i,1,&j,K2t,ADD_VALUES));
      75         400 :       PetscCall(MatSetValuesBlocked(Ko,1,&i,1,&i,K3,ADD_VALUES));
      76             :     }
      77         404 :     if (i<nele) {
      78         400 :       j = i+1;
      79         400 :       PetscCall(MatSetValuesBlocked(Ko,1,&i,1,&j,K2,ADD_VALUES));
      80         404 :       PetscCall(MatSetValuesBlocked(Ko,1,&i,1,&i,K1,ADD_VALUES));
      81             :     }
      82             :   }
      83           4 :   PetscCall(MatAssemblyBegin(Ko,MAT_FINAL_ASSEMBLY));
      84           4 :   PetscCall(MatAssemblyEnd(Ko,MAT_FINAL_ASSEMBLY));
      85           4 :   PetscCall(MatScale(Ko,EI/(dlen*dlen*dlen)));
      86             : 
      87             :   /* M is block-tridiagonal */
      88           4 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&Mo));
      89           4 :   PetscCall(MatSetSizes(Mo,PETSC_DECIDE,PETSC_DECIDE,n+2,n+2));
      90           4 :   PetscCall(MatSetBlockSize(Mo,2));
      91           4 :   PetscCall(MatSetFromOptions(Mo));
      92             : 
      93           4 :   PetscCall(MatGetOwnershipRange(Mo,&Istart,&Iend));
      94         408 :   for (i=Istart/2;i<Iend/2;i++) {
      95         404 :     if (i>0) {
      96         400 :       j = i-1;
      97         400 :       PetscCall(MatSetValuesBlocked(Mo,1,&i,1,&j,M2t,ADD_VALUES));
      98         400 :       PetscCall(MatSetValuesBlocked(Mo,1,&i,1,&i,M3,ADD_VALUES));
      99             :     }
     100         404 :     if (i<nele) {
     101         400 :       j = i+1;
     102         400 :       PetscCall(MatSetValuesBlocked(Mo,1,&i,1,&j,M2,ADD_VALUES));
     103         404 :       PetscCall(MatSetValuesBlocked(Mo,1,&i,1,&i,M1,ADD_VALUES));
     104             :     }
     105             :   }
     106           4 :   PetscCall(MatAssemblyBegin(Mo,MAT_FINAL_ASSEMBLY));
     107           4 :   PetscCall(MatAssemblyEnd(Mo,MAT_FINAL_ASSEMBLY));
     108           4 :   PetscCall(MatScale(Mo,rho*area*dlen/420));
     109             : 
     110             :   /* remove rows/columns from K and M corresponding to boundary conditions */
     111           4 :   PetscCall(ISCreateStride(PETSC_COMM_WORLD,Iend-Istart,Istart,1,&isf));
     112           4 :   bc[0] = 0; bc[1] = n;
     113           4 :   PetscCall(ISCreateGeneral(PETSC_COMM_SELF,2,bc,PETSC_USE_POINTER,&isbc));
     114           4 :   PetscCall(ISDifference(isf,isbc,&is));
     115           4 :   PetscCall(MatCreateSubMatrix(Ko,is,is,MAT_INITIAL_MATRIX,&K));
     116           4 :   PetscCall(MatCreateSubMatrix(Mo,is,is,MAT_INITIAL_MATRIX,&M));
     117           4 :   PetscCall(MatGetLocalSize(M,&mloc,&nloc));
     118             : 
     119             :   /* C is zero except for the (nele,nele)-entry */
     120           4 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&C));
     121           4 :   PetscCall(MatSetSizes(C,mloc,nloc,PETSC_DECIDE,PETSC_DECIDE));
     122           4 :   PetscCall(MatSetFromOptions(C));
     123             : 
     124           4 :   PetscCall(MatGetOwnershipRange(C,&Istart,&Iend));
     125           4 :   if (nele-1>=Istart && nele-1<Iend) PetscCall(MatSetValue(C,nele-1,nele-1,damp,INSERT_VALUES));
     126           4 :   PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
     127           4 :   PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));
     128             : 
     129             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     130             :                 Create the eigensolver and solve the problem
     131             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     132             : 
     133           4 :   PetscCall(PEPCreate(PETSC_COMM_WORLD,&pep));
     134           4 :   A[0] = K; A[1] = C; A[2] = M;
     135           4 :   PetscCall(PEPSetOperators(pep,3,A));
     136           4 :   PetscCall(PEPSetFromOptions(pep));
     137           4 :   PetscCall(PEPSolve(pep));
     138             : 
     139             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     140             :                     Display solution and clean up
     141             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     142             : 
     143             :   /* show detailed info unless -terse option is given by user */
     144           4 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
     145           4 :   if (terse) PetscCall(PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL));
     146             :   else {
     147           0 :     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
     148           0 :     PetscCall(PEPConvergedReasonView(pep,PETSC_VIEWER_STDOUT_WORLD));
     149           0 :     PetscCall(PEPErrorView(pep,PEP_ERROR_BACKWARD,PETSC_VIEWER_STDOUT_WORLD));
     150           0 :     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
     151             :   }
     152           4 :   PetscCall(PEPDestroy(&pep));
     153           4 :   PetscCall(ISDestroy(&isf));
     154           4 :   PetscCall(ISDestroy(&isbc));
     155           4 :   PetscCall(ISDestroy(&is));
     156           4 :   PetscCall(MatDestroy(&M));
     157           4 :   PetscCall(MatDestroy(&C));
     158           4 :   PetscCall(MatDestroy(&K));
     159           4 :   PetscCall(MatDestroy(&Ko));
     160           4 :   PetscCall(MatDestroy(&Mo));
     161           4 :   PetscCall(SlepcFinalize());
     162             :   return 0;
     163             : }
     164             : 
     165             : /*TEST
     166             : 
     167             :    testset:
     168             :       args: -pep_nev 2 -pep_ncv 12 -pep_target 0 -terse
     169             :       requires: !single
     170             :       output_file: output/damped_beam_1.out
     171             :       test:
     172             :          suffix: 1
     173             :          args: -pep_type {{toar linear}} -st_type sinvert
     174             :       test:
     175             :          suffix: 1_qarnoldi
     176             :          args: -pep_type qarnoldi -pep_qarnoldi_locking 0 -st_type sinvert
     177             :       test:
     178             :          suffix: 1_jd
     179             :          args: -pep_type jd
     180             :          filter: sed -e "s/23066i/23065i/"
     181             : 
     182             :    testset:
     183             :       args: -pep_nev 2 -pep_ncv 12 -pep_target 1i -terse
     184             :       requires: complex !single
     185             :       output_file: output/damped_beam_1.out
     186             :       test:
     187             :          suffix: 1_complex
     188             :          args: -pep_type {{toar linear}} -st_type sinvert
     189             :       test:
     190             :          suffix: 1_qarnoldi_complex
     191             :          args: -pep_type qarnoldi -pep_qarnoldi_locking 0 -st_type sinvert
     192             :       test:
     193             :          suffix: 1_jd_complex
     194             :          args: -pep_type jd
     195             : 
     196             : TEST*/

Generated by: LCOV version 1.14