LCOV - code coverage report
Current view: top level - nep/tests - test13.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 88 88 100.0 %
Date: 2024-05-03 00:51:52 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[] = "Test the NEPProjectOperator() function.\n\n"
      12             :   "This is based on ex22.\n"
      13             :   "The command line options are:\n"
      14             :   "  -n <n>, where <n> = number of grid subdivisions.\n"
      15             :   "  -tau <tau>, where <tau> is the delay parameter.\n";
      16             : 
      17             : /*
      18             :    Solve parabolic partial differential equation with time delay tau
      19             : 
      20             :             u_t = u_xx + a*u(t) + b*u(t-tau)
      21             :             u(0,t) = u(pi,t) = 0
      22             : 
      23             :    with a = 20 and b(x) = -4.1+x*(1-exp(x-pi)).
      24             : 
      25             :    Discretization leads to a DDE of dimension n
      26             : 
      27             :             -u' = A*u(t) + B*u(t-tau)
      28             : 
      29             :    which results in the nonlinear eigenproblem
      30             : 
      31             :             (-lambda*I + A + exp(-tau*lambda)*B)*u = 0
      32             : */
      33             : 
      34             : #include <slepcnep.h>
      35             : 
      36           1 : int main(int argc,char **argv)
      37             : {
      38           1 :   NEP            nep;
      39           1 :   Mat            Id,A,B,mats[3];
      40           1 :   FN             f1,f2,f3,funs[3];
      41           1 :   BV             V;
      42           1 :   DS             ds;
      43           1 :   Vec            v;
      44           1 :   PetscScalar    coeffs[2],b,*M;
      45           1 :   PetscInt       n=32,Istart,Iend,i,j,k,nc;
      46           1 :   PetscReal      tau=0.001,h,a=20,xi;
      47             : 
      48           1 :   PetscFunctionBeginUser;
      49           1 :   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
      50           1 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      51           1 :   PetscCall(PetscOptionsGetReal(NULL,NULL,"-tau",&tau,NULL));
      52           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n1-D Delay Eigenproblem, n=%" PetscInt_FMT ", tau=%g\n",n,(double)tau));
      53           1 :   h = PETSC_PI/(PetscReal)(n+1);
      54             : 
      55             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      56             :      Create nonlinear eigensolver context
      57             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      58             : 
      59           1 :   PetscCall(NEPCreate(PETSC_COMM_WORLD,&nep));
      60             : 
      61             :   /* Identity matrix */
      62           1 :   PetscCall(MatCreateConstantDiagonal(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,n,n,1.0,&Id));
      63           1 :   PetscCall(MatSetOption(Id,MAT_HERMITIAN,PETSC_TRUE));
      64             : 
      65             :   /* A = 1/h^2*tridiag(1,-2,1) + a*I */
      66           1 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
      67           1 :   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n));
      68           1 :   PetscCall(MatSetFromOptions(A));
      69           1 :   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
      70          33 :   for (i=Istart;i<Iend;i++) {
      71          32 :     if (i>0) PetscCall(MatSetValue(A,i,i-1,1.0/(h*h),INSERT_VALUES));
      72          32 :     if (i<n-1) PetscCall(MatSetValue(A,i,i+1,1.0/(h*h),INSERT_VALUES));
      73          32 :     PetscCall(MatSetValue(A,i,i,-2.0/(h*h)+a,INSERT_VALUES));
      74             :   }
      75           1 :   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
      76           1 :   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
      77           1 :   PetscCall(MatSetOption(A,MAT_HERMITIAN,PETSC_TRUE));
      78             : 
      79             :   /* B = diag(b(xi)) */
      80           1 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
      81           1 :   PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,n,n));
      82           1 :   PetscCall(MatSetFromOptions(B));
      83           1 :   PetscCall(MatGetOwnershipRange(B,&Istart,&Iend));
      84          33 :   for (i=Istart;i<Iend;i++) {
      85          32 :     xi = (i+1)*h;
      86          32 :     b = -4.1+xi*(1.0-PetscExpReal(xi-PETSC_PI));
      87          32 :     PetscCall(MatSetValues(B,1,&i,1,&i,&b,INSERT_VALUES));
      88             :   }
      89           1 :   PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
      90           1 :   PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
      91           1 :   PetscCall(MatSetOption(B,MAT_HERMITIAN,PETSC_TRUE));
      92             : 
      93             :   /* Functions: f1=-lambda, f2=1.0, f3=exp(-tau*lambda) */
      94           1 :   PetscCall(FNCreate(PETSC_COMM_WORLD,&f1));
      95           1 :   PetscCall(FNSetType(f1,FNRATIONAL));
      96           1 :   coeffs[0] = -1.0; coeffs[1] = 0.0;
      97           1 :   PetscCall(FNRationalSetNumerator(f1,2,coeffs));
      98             : 
      99           1 :   PetscCall(FNCreate(PETSC_COMM_WORLD,&f2));
     100           1 :   PetscCall(FNSetType(f2,FNRATIONAL));
     101           1 :   coeffs[0] = 1.0;
     102           1 :   PetscCall(FNRationalSetNumerator(f2,1,coeffs));
     103             : 
     104           1 :   PetscCall(FNCreate(PETSC_COMM_WORLD,&f3));
     105           1 :   PetscCall(FNSetType(f3,FNEXP));
     106           1 :   PetscCall(FNSetScale(f3,-tau,1.0));
     107             : 
     108             :   /* Set the split operator */
     109           1 :   mats[0] = A;  funs[0] = f2;
     110           1 :   mats[1] = Id; funs[1] = f1;
     111           1 :   mats[2] = B;  funs[2] = f3;
     112           1 :   PetscCall(NEPSetSplitOperator(nep,3,mats,funs,SUBSET_NONZERO_PATTERN));
     113           1 :   PetscCall(NEPSetType(nep,NEPNARNOLDI));
     114           1 :   PetscCall(NEPSetFromOptions(nep));
     115             : 
     116             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     117             :                       Project the NEP
     118             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     119             : 
     120           1 :   PetscCall(NEPSetUp(nep));
     121           1 :   PetscCall(NEPGetBV(nep,&V));
     122           1 :   PetscCall(BVGetSizes(V,NULL,NULL,&nc));
     123           6 :   for (i=0;i<nc;i++) {
     124           5 :     PetscCall(BVGetColumn(V,i,&v));
     125           5 :     PetscCall(VecSetValue(v,i,1.0,INSERT_VALUES));
     126           5 :     PetscCall(VecAssemblyBegin(v));
     127           5 :     PetscCall(VecAssemblyEnd(v));
     128           5 :     PetscCall(BVRestoreColumn(V,i,&v));
     129             :   }
     130           1 :   PetscCall(NEPGetDS(nep,&ds));
     131           1 :   PetscCall(DSSetType(ds,DSNEP));
     132           1 :   PetscCall(DSNEPSetFN(ds,3,funs));
     133           1 :   PetscCall(DSAllocate(ds,nc));
     134           1 :   PetscCall(DSSetDimensions(ds,nc,0,0));
     135           1 :   PetscCall(NEPProjectOperator(nep,0,nc));
     136             : 
     137             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     138             :                 Display projected matrices and clean up
     139             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     140             : 
     141           4 :   for (k=0;k<3;k++) {
     142           3 :     PetscCall(DSGetArray(ds,DSMatExtra[k],&M));
     143           3 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nMatrix E%" PetscInt_FMT " = \n",k));
     144          18 :     for (i=0;i<nc;i++) {
     145          90 :       for (j=0;j<nc;j++) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"  %.5g",(double)PetscRealPart(M[i+j*nc])));
     146          15 :       PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n"));
     147             :     }
     148           3 :     PetscCall(DSRestoreArray(ds,DSMatExtra[k],&M));
     149             :   }
     150             : 
     151           1 :   PetscCall(NEPDestroy(&nep));
     152           1 :   PetscCall(MatDestroy(&Id));
     153           1 :   PetscCall(MatDestroy(&A));
     154           1 :   PetscCall(MatDestroy(&B));
     155           1 :   PetscCall(FNDestroy(&f1));
     156           1 :   PetscCall(FNDestroy(&f2));
     157           1 :   PetscCall(FNDestroy(&f3));
     158           1 :   PetscCall(SlepcFinalize());
     159             :   return 0;
     160             : }
     161             : 
     162             : /*TEST
     163             : 
     164             :    test:
     165             :       suffix: 1
     166             :       args: -nep_ncv 5
     167             : 
     168             : TEST*/

Generated by: LCOV version 1.14