LCOV - code coverage report
Current view: top level - nep/tutorials - ex22.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 73 77 94.8 %
Date: 2024-05-04 00:30:31 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             : static char help[] = "Delay differential equation.\n\n"
      12             :   "The command line options are:\n"
      13             :   "  -n <n>, where <n> = number of grid subdivisions.\n"
      14             :   "  -tau <tau>, where <tau> is the delay parameter.\n\n";
      15             : 
      16             : /*
      17             :    Solve parabolic partial differential equation with time delay tau
      18             : 
      19             :             u_t = u_xx + a*u(t) + b*u(t-tau)
      20             :             u(0,t) = u(pi,t) = 0
      21             : 
      22             :    with a = 20 and b(x) = -4.1+x*(1-exp(x-pi)).
      23             : 
      24             :    Discretization leads to a DDE of dimension n
      25             : 
      26             :             -u' = A*u(t) + B*u(t-tau)
      27             : 
      28             :    which results in the nonlinear eigenproblem
      29             : 
      30             :             (-lambda*I + A + exp(-tau*lambda)*B)*u = 0
      31             : */
      32             : 
      33             : #include <slepcnep.h>
      34             : 
      35          18 : int main(int argc,char **argv)
      36             : {
      37          18 :   NEP            nep;             /* nonlinear eigensolver context */
      38          18 :   Mat            Id,A,B;          /* problem matrices */
      39          18 :   FN             f1,f2,f3;        /* functions to define the nonlinear operator */
      40          18 :   Mat            mats[3];
      41          18 :   FN             funs[3];
      42          18 :   PetscScalar    coeffs[2],b;
      43          18 :   PetscInt       n=128,nev,Istart,Iend,i;
      44          18 :   PetscReal      tau=0.001,h,a=20,xi;
      45          18 :   PetscBool      terse;
      46             : 
      47          18 :   PetscFunctionBeginUser;
      48          18 :   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
      49          18 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      50          18 :   PetscCall(PetscOptionsGetReal(NULL,NULL,"-tau",&tau,NULL));
      51          18 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n1-D Delay Eigenproblem, n=%" PetscInt_FMT ", tau=%g\n\n",n,(double)tau));
      52          18 :   h = PETSC_PI/(PetscReal)(n+1);
      53             : 
      54             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      55             :      Create nonlinear eigensolver context
      56             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      57             : 
      58          18 :   PetscCall(NEPCreate(PETSC_COMM_WORLD,&nep));
      59             : 
      60             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      61             :      Create problem matrices and coefficient functions. Pass them to NEP
      62             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      63             : 
      64             :   /*
      65             :      Identity matrix
      66             :   */
      67          18 :   PetscCall(MatCreateConstantDiagonal(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,n,n,1.0,&Id));
      68          18 :   PetscCall(MatSetOption(Id,MAT_HERMITIAN,PETSC_TRUE));
      69             : 
      70             :   /*
      71             :      A = 1/h^2*tridiag(1,-2,1) + a*I
      72             :   */
      73          18 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
      74          18 :   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n));
      75          18 :   PetscCall(MatSetFromOptions(A));
      76          18 :   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
      77        5394 :   for (i=Istart;i<Iend;i++) {
      78        5376 :     if (i>0) PetscCall(MatSetValue(A,i,i-1,1.0/(h*h),INSERT_VALUES));
      79        5376 :     if (i<n-1) PetscCall(MatSetValue(A,i,i+1,1.0/(h*h),INSERT_VALUES));
      80        5376 :     PetscCall(MatSetValue(A,i,i,-2.0/(h*h)+a,INSERT_VALUES));
      81             :   }
      82          18 :   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
      83          18 :   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
      84          18 :   PetscCall(MatSetOption(A,MAT_HERMITIAN,PETSC_TRUE));
      85             : 
      86             :   /*
      87             :      B = diag(b(xi))
      88             :   */
      89          18 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
      90          18 :   PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,n,n));
      91          18 :   PetscCall(MatSetFromOptions(B));
      92          18 :   PetscCall(MatGetOwnershipRange(B,&Istart,&Iend));
      93        5394 :   for (i=Istart;i<Iend;i++) {
      94        5376 :     xi = (i+1)*h;
      95        5376 :     b = -4.1+xi*(1.0-PetscExpReal(xi-PETSC_PI));
      96        5376 :     PetscCall(MatSetValue(B,i,i,b,INSERT_VALUES));
      97             :   }
      98          18 :   PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
      99          18 :   PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
     100          18 :   PetscCall(MatSetOption(B,MAT_HERMITIAN,PETSC_TRUE));
     101             : 
     102             :   /*
     103             :      Functions: f1=-lambda, f2=1.0, f3=exp(-tau*lambda)
     104             :   */
     105          18 :   PetscCall(FNCreate(PETSC_COMM_WORLD,&f1));
     106          18 :   PetscCall(FNSetType(f1,FNRATIONAL));
     107          18 :   coeffs[0] = -1.0; coeffs[1] = 0.0;
     108          18 :   PetscCall(FNRationalSetNumerator(f1,2,coeffs));
     109             : 
     110          18 :   PetscCall(FNCreate(PETSC_COMM_WORLD,&f2));
     111          18 :   PetscCall(FNSetType(f2,FNRATIONAL));
     112          18 :   coeffs[0] = 1.0;
     113          18 :   PetscCall(FNRationalSetNumerator(f2,1,coeffs));
     114             : 
     115          18 :   PetscCall(FNCreate(PETSC_COMM_WORLD,&f3));
     116          18 :   PetscCall(FNSetType(f3,FNEXP));
     117          18 :   PetscCall(FNSetScale(f3,-tau,1.0));
     118             : 
     119             :   /*
     120             :      Set the split operator. Note that A is passed first so that
     121             :      SUBSET_NONZERO_PATTERN can be used
     122             :   */
     123          18 :   mats[0] = A;  funs[0] = f2;
     124          18 :   mats[1] = Id; funs[1] = f1;
     125          18 :   mats[2] = B;  funs[2] = f3;
     126          18 :   PetscCall(NEPSetSplitOperator(nep,3,mats,funs,SUBSET_NONZERO_PATTERN));
     127             : 
     128             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     129             :              Customize nonlinear solver; set runtime options
     130             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     131             : 
     132          18 :   PetscCall(NEPSetTolerances(nep,1e-9,PETSC_DEFAULT));
     133          18 :   PetscCall(NEPSetDimensions(nep,1,PETSC_DEFAULT,PETSC_DEFAULT));
     134          18 :   PetscCall(NEPRIISetLagPreconditioner(nep,0));
     135             : 
     136             :   /*
     137             :      Set solver parameters at runtime
     138             :   */
     139          18 :   PetscCall(NEPSetFromOptions(nep));
     140             : 
     141             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     142             :                       Solve the eigensystem
     143             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     144             : 
     145          18 :   PetscCall(NEPSolve(nep));
     146          18 :   PetscCall(NEPGetDimensions(nep,&nev,NULL,NULL));
     147          18 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev));
     148             : 
     149             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     150             :                     Display solution and clean up
     151             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     152             : 
     153             :   /* show detailed info unless -terse option is given by user */
     154          18 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
     155          18 :   if (terse) PetscCall(NEPErrorView(nep,NEP_ERROR_RELATIVE,NULL));
     156             :   else {
     157           0 :     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
     158           0 :     PetscCall(NEPConvergedReasonView(nep,PETSC_VIEWER_STDOUT_WORLD));
     159           0 :     PetscCall(NEPErrorView(nep,NEP_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
     160           0 :     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
     161             :   }
     162          18 :   PetscCall(NEPDestroy(&nep));
     163          18 :   PetscCall(MatDestroy(&Id));
     164          18 :   PetscCall(MatDestroy(&A));
     165          18 :   PetscCall(MatDestroy(&B));
     166          18 :   PetscCall(FNDestroy(&f1));
     167          18 :   PetscCall(FNDestroy(&f2));
     168          18 :   PetscCall(FNDestroy(&f3));
     169          18 :   PetscCall(SlepcFinalize());
     170             :   return 0;
     171             : }
     172             : 
     173             : /*TEST
     174             : 
     175             :    testset:
     176             :       suffix: 1
     177             :       args: -nep_type {{rii slp narnoldi}} -terse
     178             :       filter: sed -e "s/[+-]0\.0*i//g"
     179             :       requires: !single
     180             : 
     181             :    test:
     182             :       suffix: 1_ciss
     183             :       args: -nep_type ciss -nep_ciss_extraction {{ritz hankel caa}} -rg_type ellipse -rg_ellipse_center 10 -rg_ellipse_radius 9.5 -nep_ncv 24 -terse
     184             :       requires: complex !single
     185             : 
     186             :    test:
     187             :       suffix: 2
     188             :       args: -nep_type interpol -nep_interpol_pep_extract {{none norm residual}} -rg_type interval -rg_interval_endpoints 5,20,-.1,.1 -nep_nev 3 -nep_target 5 -terse
     189             :       filter: sed -e "s/[+-]0\.0*i//g"
     190             :       requires: !single
     191             : 
     192             :    testset:
     193             :       args: -n 512 -nep_target 10 -nep_nev 3 -terse
     194             :       filter: sed -e "s/[+-]0\.0*i//g"
     195             :       requires: !single
     196             :       output_file: output/ex22_3.out
     197             :       test:
     198             :          suffix: 3
     199             :          args: -nep_type {{rii slp narnoldi}}
     200             :       test:
     201             :          suffix: 3_simpleu
     202             :          args: -nep_type {{rii slp narnoldi}} -nep_deflation_simpleu
     203             :       test:
     204             :          suffix: 3_slp_thres
     205             :          args: -nep_type slp -nep_slp_deflation_threshold 1e-8
     206             :       test:
     207             :          suffix: 3_rii_thres
     208             :          args: -nep_type rii -nep_rii_deflation_threshold 1e-8
     209             : 
     210             :    test:
     211             :       suffix: 4
     212             :       args: -nep_type interpol -rg_type interval -rg_interval_endpoints 5,20,-.1,.1 -nep_nev 3 -nep_target 5 -terse -nep_monitor draw::draw_lg
     213             :       filter: sed -e "s/[+-]0\.0*i//g"
     214             :       requires: x !single
     215             :       output_file: output/ex22_2.out
     216             : 
     217             : TEST*/

Generated by: LCOV version 1.14