LCOV - code coverage report
Current view: top level - pep/tutorials/nlevp - spring.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 52 56 92.9 %
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 spring problem is a QEP from the finite element model of a damped
      19             :    mass-spring system. This implementation supports only scalar parameters,
      20             :    that is all masses, dampers and springs have the same constants.
      21             :    Furthermore, this implementation does not consider different constants
      22             :    for dampers and springs connecting adjacent masses or masses to the ground.
      23             : */
      24             : 
      25             : static char help[] = "FEM model of a damped mass-spring system.\n\n"
      26             :   "The command line options are:\n"
      27             :   "  -n <n> ... dimension of the matrices.\n"
      28             :   "  -mu <value> ... mass (default 1).\n"
      29             :   "  -tau <value> ... damping constant of the dampers (default 10).\n"
      30             :   "  -kappa <value> ... damping constant of the springs (default 5).\n\n";
      31             : 
      32             : #include <slepcpep.h>
      33             : 
      34           9 : int main(int argc,char **argv)
      35             : {
      36           9 :   Mat            M,C,K,A[3];      /* problem matrices */
      37           9 :   PEP            pep;             /* polynomial eigenproblem solver context */
      38           9 :   PetscInt       n=5,Istart,Iend,i;
      39           9 :   PetscReal      mu=1.0,tau=10.0,kappa=5.0;
      40           9 :   PetscBool      terse;
      41             : 
      42           9 :   PetscFunctionBeginUser;
      43           9 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      44             : 
      45           9 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      46           9 :   PetscCall(PetscOptionsGetReal(NULL,NULL,"-mu",&mu,NULL));
      47           9 :   PetscCall(PetscOptionsGetReal(NULL,NULL,"-tau",&tau,NULL));
      48           9 :   PetscCall(PetscOptionsGetReal(NULL,NULL,"-kappa",&kappa,NULL));
      49           9 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nDamped mass-spring system, n=%" PetscInt_FMT " mu=%g tau=%g kappa=%g\n\n",n,(double)mu,(double)tau,(double)kappa));
      50             : 
      51             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      52             :      Compute the matrices that define the eigensystem, (k^2*M+k*C+K)x=0
      53             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      54             : 
      55             :   /* K is a tridiagonal */
      56           9 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&K));
      57           9 :   PetscCall(MatSetSizes(K,PETSC_DECIDE,PETSC_DECIDE,n,n));
      58           9 :   PetscCall(MatSetFromOptions(K));
      59             : 
      60           9 :   PetscCall(MatGetOwnershipRange(K,&Istart,&Iend));
      61        1053 :   for (i=Istart;i<Iend;i++) {
      62        1044 :     if (i>0) PetscCall(MatSetValue(K,i,i-1,-kappa,INSERT_VALUES));
      63        1044 :     PetscCall(MatSetValue(K,i,i,kappa*3.0,INSERT_VALUES));
      64        1044 :     if (i<n-1) PetscCall(MatSetValue(K,i,i+1,-kappa,INSERT_VALUES));
      65             :   }
      66             : 
      67           9 :   PetscCall(MatAssemblyBegin(K,MAT_FINAL_ASSEMBLY));
      68           9 :   PetscCall(MatAssemblyEnd(K,MAT_FINAL_ASSEMBLY));
      69             : 
      70             :   /* C is a tridiagonal */
      71           9 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&C));
      72           9 :   PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,n,n));
      73           9 :   PetscCall(MatSetFromOptions(C));
      74             : 
      75           9 :   PetscCall(MatGetOwnershipRange(C,&Istart,&Iend));
      76        1053 :   for (i=Istart;i<Iend;i++) {
      77        1044 :     if (i>0) PetscCall(MatSetValue(C,i,i-1,-tau,INSERT_VALUES));
      78        1044 :     PetscCall(MatSetValue(C,i,i,tau*3.0,INSERT_VALUES));
      79        1044 :     if (i<n-1) PetscCall(MatSetValue(C,i,i+1,-tau,INSERT_VALUES));
      80             :   }
      81             : 
      82           9 :   PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
      83           9 :   PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));
      84             : 
      85             :   /* M is a diagonal matrix */
      86           9 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&M));
      87           9 :   PetscCall(MatSetSizes(M,PETSC_DECIDE,PETSC_DECIDE,n,n));
      88           9 :   PetscCall(MatSetFromOptions(M));
      89           9 :   PetscCall(MatGetOwnershipRange(M,&Istart,&Iend));
      90        1053 :   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(M,i,i,mu,INSERT_VALUES));
      91           9 :   PetscCall(MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY));
      92           9 :   PetscCall(MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY));
      93             : 
      94             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      95             :                 Create the eigensolver and solve the problem
      96             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      97             : 
      98           9 :   PetscCall(PEPCreate(PETSC_COMM_WORLD,&pep));
      99           9 :   A[0] = K; A[1] = C; A[2] = M;
     100           9 :   PetscCall(PEPSetOperators(pep,3,A));
     101           9 :   PetscCall(PEPSetFromOptions(pep));
     102           9 :   PetscCall(PEPSolve(pep));
     103             : 
     104             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     105             :                     Display solution and clean up
     106             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     107             : 
     108             :   /* show detailed info unless -terse option is given by user */
     109           9 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
     110           9 :   if (terse) PetscCall(PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL));
     111             :   else {
     112           0 :     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
     113           0 :     PetscCall(PEPConvergedReasonView(pep,PETSC_VIEWER_STDOUT_WORLD));
     114           0 :     PetscCall(PEPErrorView(pep,PEP_ERROR_BACKWARD,PETSC_VIEWER_STDOUT_WORLD));
     115           0 :     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
     116             :   }
     117           9 :   PetscCall(PEPDestroy(&pep));
     118           9 :   PetscCall(MatDestroy(&M));
     119           9 :   PetscCall(MatDestroy(&C));
     120           9 :   PetscCall(MatDestroy(&K));
     121           9 :   PetscCall(SlepcFinalize());
     122             :   return 0;
     123             : }
     124             : 
     125             : /*TEST
     126             : 
     127             :    testset:
     128             :       args: -pep_nev 4 -n 24 -pep_ncv 18 -pep_target -.5 -st_type sinvert -pep_scale diagonal -terse
     129             :       output_file: output/spring_1.out
     130             :       filter: sed -e "s/[+-]0\.0*i//g"
     131             :       test:
     132             :          suffix: 1
     133             :          args: -pep_type {{toar linear}} -pep_conv_norm
     134             :       test:
     135             :          suffix: 1_stoar
     136             :          args: -pep_type stoar -pep_hermitian -pep_conv_rel
     137             :       test:
     138             :          suffix: 1_qarnoldi
     139             :          args: -pep_type qarnoldi -pep_conv_rel
     140             :       test:
     141             :          suffix: 1_cuda
     142             :          args: -mat_type aijcusparse
     143             :          requires: cuda
     144             :       test:
     145             :          suffix: 1_hip
     146             :          args: -mat_type aijhipsparse
     147             :          requires: hip
     148             : 
     149             :    test:
     150             :       suffix: 2
     151             :       args: -pep_type jd -pep_jd_minimality_index 1 -pep_nev 4 -n 24 -pep_ncv 18 -pep_target -50 -terse
     152             :       requires: !single
     153             :       filter: sed -e "s/[+-]0\.0*i//g"
     154             : 
     155             :    test:
     156             :       suffix: 3
     157             :       args: -n 300 -pep_hermitian -pep_interval -10.1,-9.5 -pep_type stoar -st_type sinvert -st_pc_type cholesky -terse
     158             :       filter: sed -e "s/52565/52566/" | sed -e "s/90758/90759/"
     159             :       requires: !single
     160             : 
     161             :    test:
     162             :       suffix: 4
     163             :       args: -n 300 -pep_hyperbolic -pep_interval -9.6,-.527 -pep_type stoar -st_type sinvert -st_pc_type cholesky -terse
     164             :       requires: !single
     165             :       timeoutfactor: 2
     166             : 
     167             :    test:
     168             :       suffix: 5
     169             :       args: -n 300 -pep_hyperbolic -pep_interval -.506,-.3 -pep_type stoar -st_type sinvert -st_pc_type cholesky -pep_stoar_nev 11 -terse
     170             :       requires: !single
     171             : 
     172             :    test:
     173             :       suffix: 6
     174             :       args: -n 24 -pep_ncv 18 -pep_target -.5 -terse -pep_type jd -pep_jd_restart .6 -pep_jd_fix .001
     175             :       requires: !single
     176             : 
     177             : TEST*/

Generated by: LCOV version 1.14