Date: 2021-09-20T09:44:11+00:00

Actual source code: loaded_string.c
slepc-3.15.2 2021-09-20
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-2021, Universitat Politecnica de Valencia, Spain

      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.

     18:    The loaded_string problem is a rational eigenvalue problem for the
     19:    finite element model of a loaded vibrating string.
     20: */

     22: static char help[] = "Finite element model of a loaded vibrating string.\n\n"
     23:   "The command line options are:\n"
     24:   "  -n <n>, dimension of the matrices.\n"
     25:   "  -kappa <kappa>, stiffness of elastic spring.\n"
     26:   "  -mass <m>, mass of the attached load.\n\n";

     28: #include <slepcnep.h>

     30: #define NMAT 3

     32: int main(int argc,char **argv)
     33: {
     34:   Mat            A[NMAT];         /* problem matrices */
     35:   FN             f[NMAT];         /* functions to define the nonlinear operator */
     36:   NEP            nep;             /* nonlinear eigensolver context */
     37:   PetscInt       n=100,Istart,Iend,i;
     38:   PetscReal      kappa=1.0,m=1.0;
     39:   PetscScalar    sigma,numer[2],denom[2];
     40:   PetscBool      terse;

     43:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;

     45:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
     46:   PetscOptionsGetReal(NULL,NULL,"-kappa",&kappa,NULL);
     47:   PetscOptionsGetReal(NULL,NULL,"-mass",&m,NULL);
     48:   sigma = kappa/m;
     49:   PetscPrintf(PETSC_COMM_WORLD,"Loaded vibrating string, n=%D kappa=%g m=%g\n\n",n,(double)kappa,(double)m);

     51:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     52:                        Build the problem matrices
     53:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     55:   /* initialize matrices */
     56:   for (i=0;i<NMAT;i++) {
     57:     MatCreate(PETSC_COMM_WORLD,&A[i]);
     58:     MatSetSizes(A[i],PETSC_DECIDE,PETSC_DECIDE,n,n);
     59:     MatSetFromOptions(A[i]);
     60:     MatSetUp(A[i]);
     61:   }
     62:   MatGetOwnershipRange(A[0],&Istart,&Iend);

     64:   /* A0 */
     65:   for (i=Istart;i<Iend;i++) {
     66:     MatSetValue(A[0],i,i,(i==n-1)?1.0*n:2.0*n,INSERT_VALUES);
     67:     if (i>0) { MatSetValue(A[0],i,i-1,-1.0*n,INSERT_VALUES); }
     68:     if (i<n-1) { MatSetValue(A[0],i,i+1,-1.0*n,INSERT_VALUES); }
     69:   }

     71:   /* A1 */
     72:   for (i=Istart;i<Iend;i++) {
     73:     MatSetValue(A[1],i,i,(i==n-1)?2.0/(6.0*n):4.0/(6.0*n),INSERT_VALUES);
     74:     if (i>0) { MatSetValue(A[1],i,i-1,1.0/(6.0*n),INSERT_VALUES); }
     75:     if (i<n-1) { MatSetValue(A[1],i,i+1,1.0/(6.0*n),INSERT_VALUES); }
     76:   }

     78:   /* A2 */
     79:   if (Istart<=n-1 && n-1<Iend) {
     80:     MatSetValue(A[2],n-1,n-1,kappa,INSERT_VALUES);
     81:   }

     83:   /* assemble matrices */
     84:   for (i=0;i<NMAT;i++) {
     85:     MatAssemblyBegin(A[i],MAT_FINAL_ASSEMBLY);
     86:   }
     87:   for (i=0;i<NMAT;i++) {
     88:     MatAssemblyEnd(A[i],MAT_FINAL_ASSEMBLY);
     89:   }

     91:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     92:                        Create the problem functions
     93:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     95:   /* f1=1 */
     96:   FNCreate(PETSC_COMM_WORLD,&f[0]);
     97:   FNSetType(f[0],FNRATIONAL);
     98:   numer[0] = 1.0;
     99:   FNRationalSetNumerator(f[0],1,numer);

    101:   /* f2=-lambda */
    102:   FNCreate(PETSC_COMM_WORLD,&f[1]);
    103:   FNSetType(f[1],FNRATIONAL);
    104:   numer[0] = -1.0; numer[1] = 0.0;
    105:   FNRationalSetNumerator(f[1],2,numer);

    107:   /* f3=lambda/(lambda-sigma) */
    108:   FNCreate(PETSC_COMM_WORLD,&f[2]);
    109:   FNSetType(f[2],FNRATIONAL);
    110:   numer[0] = 1.0; numer[1] = 0.0;
    111:   denom[0] = 1.0; denom[1] = -sigma;
    112:   FNRationalSetNumerator(f[2],2,numer);
    113:   FNRationalSetDenominator(f[2],2,denom);

    115:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    116:                 Create the eigensolver and solve the problem
    117:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    119:   NEPCreate(PETSC_COMM_WORLD,&nep);
    120:   NEPSetSplitOperator(nep,3,A,f,SUBSET_NONZERO_PATTERN);
    121:   NEPSetProblemType(nep,NEP_RATIONAL);
    122:   NEPSetFromOptions(nep);
    123:   NEPSolve(nep);

    125:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    126:                     Display solution and clean up
    127:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    129:   /* show detailed info unless -terse option is given by user */
    130:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
    131:   if (terse) {
    132:     NEPErrorView(nep,NEP_ERROR_RELATIVE,NULL);
    133:   } else {
    134:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
    135:     NEPConvergedReasonView(nep,PETSC_VIEWER_STDOUT_WORLD);
    136:     NEPErrorView(nep,NEP_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
    137:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
    138:   }
    139:   NEPDestroy(&nep);
    140:   for (i=0;i<NMAT;i++) {
    141:     MatDestroy(&A[i]);
    142:     FNDestroy(&f[i]);
    143:   }
    144:   SlepcFinalize();
    145:   return ierr;
    146: }

    148: /*TEST

    150:    test:
    151:       suffix: 1
    152:       args: -nep_type rii -nep_target 4 -terse
    153:       requires: !single
    154:       filter: sed -e "s/[+-]0\.0*i//g"

    156:    testset:
    157:       args: -nep_type interpol -rg_type interval -rg_interval_endpoints 5,700,-.1,.1 -nep_nev 7 -nep_target 5 -nep_interpol_interpolation_degree 12 -nep_refine simple -terse
    158:       requires: !single
    159:       output_file: output/loaded_string_2.out
    160:       test:
    161:          suffix: 2
    162:          args: -nep_refine_scheme {{schur explicit}}
    163:       test:
    164:          suffix: 2_mbe
    165:          args: -nep_refine_scheme mbe -nep_refine_ksp_type preonly -nep_refine_pc_type lu

    167:    testset:
    168:       nsize: 2
    169:       args: -nep_type interpol -rg_type interval -rg_interval_endpoints 5,700,-.1,.1 -nep_nev 7 -nep_target 5 -nep_interpol_interpolation_degree 12 -nep_refine simple -nep_refine_partitions 2 -nep_interpol_st_ksp_type bcgs -nep_interpol_st_pc_type bjacobi -terse
    170:       requires: !single
    171:       output_file: output/loaded_string_2.out
    172:       timeoutfactor: 2
    173:       test:
    174:          suffix: 3_explicit
    175:          args: -nep_refine_scheme explicit
    176:       test:
    177:          suffix: 3_mbe
    178:          args: -nep_refine_scheme mbe -nep_refine_ksp_type preonly -nep_refine_pc_type cholesky

    180:    test:
    181:       suffix: 4
    182:       nsize: 4
    183:       args: -nep_type interpol -rg_type interval -rg_interval_endpoints 5,700,-.1,.1 -nep_nev 7 -nep_target 5 -nep_interpol_interpolation_degree 10 -nep_refine simple -nep_refine_partitions 2 -nep_refine_scheme explicit -nep_interpol_st_ksp_type bcgs -nep_interpol_st_pc_type bjacobi -terse -log_exclude nep,pep,fn
    184:       requires: !single
    185:       output_file: output/loaded_string_2.out
    186:       timeoutfactor: 4

    188:    test:
    189:       suffix: 5
    190:       args: -nep_type nleigs -rg_type interval -rg_interval_endpoints 4,700,-.1,.1 -nep_nev 8 -nep_target 5 -terse
    191:       filter: sed -e "s/[+-]0\.0*i//g"
    192:       requires: !single

    194:    test:
    195:       suffix: 6
    196:       args: -nep_type nleigs -rg_type interval -rg_interval_endpoints 100,700 -nep_nev 5 -nep_tol 1e-9 -nep_target 140 -nep_nleigs_interpolation_degree 15 -nep_general -terse
    197:       requires: !complex !single

    199:    test:
    200:       suffix: 6_complex
    201:       args: -nep_type nleigs -rg_type interval -rg_interval_endpoints 100,700,-.1,.1 -nep_nev 5 -nep_tol 1e-9 -nep_target 140 -nep_nleigs_interpolation_degree 15 -nep_general -terse
    202:       filter: sed -e "s/[+-]0\.0*i//g"
    203:       requires: complex !single
    204:       output_file: output/loaded_string_6.out

    206:    test:
    207:       suffix: 7
    208:       args: -nep_type interpol -rg_type interval -rg_interval_endpoints 5,700 -nep_nev 5 -nep_target 100 -nep_interpol_interpolation_degree 20 -nep_ncv 20 -n 20 -nep_refine simple -nep_refine_its 1 -terse
    209:       requires: !complex double

    211:    test:
    212:       suffix: 7_complex
    213:       args: -nep_type interpol -rg_type interval -rg_interval_endpoints 5,700,-.1,.1 -nep_nev 5 -nep_target 100 -nep_interpol_interpolation_degree 20 -nep_ncv 20 -n 20 -nep_refine simple -nep_refine_its 1 -terse
    214:       requires: complex double
    215:       output_file: output/loaded_string_7.out

    217:    testset:
    218:       args: -nep_target 10 -nep_nev 3 -nep_tol 5e-10 -terse
    219:       requires: !single
    220:       output_file: output/loaded_string_8.out
    221:       filter: sed -e "s/[+-]0\.0*i//g"
    222:       test:
    223:          suffix: 8
    224:          args: -nep_type {{rii slp narnoldi}}
    225:       test:
    226:          suffix: 8_rii_thres
    227:          args: -nep_type rii -nep_rii_deflation_threshold 5e-10
    228:       test:
    229:          suffix: 8_slp_thres
    230:          args: -nep_type slp -nep_slp_deflation_threshold 5e-10

    232:       test:
    233:          suffix: 8_slp_two_thres
    234:          args: -nep_type slp -nep_slp_deflation_threshold 5e-10 -nep_two_sided
    235: TEST*/