LCOV - code coverage report
Current view: top level - nep/tests - test17.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 160 160 100.0 %
Date: 2024-12-18 00:42:09 Functions: 5 5 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[] = "Tests a user-provided preconditioner.\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"
      15             :   "  -a <a>, where <a> is the coefficient that multiplies u in the equation.\n"
      16             :   "  -split <0/1>, to select the split form in the problem definition (enabled by default).\n";
      17             : 
      18             : /* Based on ex22.c (delay) */
      19             : 
      20             : #include <slepcnep.h>
      21             : 
      22             : /*
      23             :    User-defined application context
      24             : */
      25             : typedef struct {
      26             :   PetscScalar tau;
      27             :   PetscReal   a;
      28             : } ApplicationCtx;
      29             : 
      30             : /*
      31             :    Create problem matrices in split form
      32             : */
      33           4 : PetscErrorCode BuildSplitMatrices(PetscInt n,PetscReal a,Mat *Id,Mat *A,Mat *B)
      34             : {
      35           4 :   PetscInt       i,Istart,Iend;
      36           4 :   PetscReal      h,xi;
      37           4 :   PetscScalar    b;
      38             : 
      39           4 :   PetscFunctionBeginUser;
      40           4 :   h = PETSC_PI/(PetscReal)(n+1);
      41             : 
      42             :   /* Identity matrix */
      43           4 :   PetscCall(MatCreateConstantDiagonal(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,n,n,1.0,Id));
      44           4 :   PetscCall(MatSetOption(*Id,MAT_HERMITIAN,PETSC_TRUE));
      45             : 
      46             :   /* A = 1/h^2*tridiag(1,-2,1) + a*I */
      47           4 :   PetscCall(MatCreate(PETSC_COMM_WORLD,A));
      48           4 :   PetscCall(MatSetSizes(*A,PETSC_DECIDE,PETSC_DECIDE,n,n));
      49           4 :   PetscCall(MatSetFromOptions(*A));
      50           4 :   PetscCall(MatGetOwnershipRange(*A,&Istart,&Iend));
      51         516 :   for (i=Istart;i<Iend;i++) {
      52         512 :     if (i>0) PetscCall(MatSetValue(*A,i,i-1,1.0/(h*h),INSERT_VALUES));
      53         512 :     if (i<n-1) PetscCall(MatSetValue(*A,i,i+1,1.0/(h*h),INSERT_VALUES));
      54         512 :     PetscCall(MatSetValue(*A,i,i,-2.0/(h*h)+a,INSERT_VALUES));
      55             :   }
      56           4 :   PetscCall(MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY));
      57           4 :   PetscCall(MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY));
      58           4 :   PetscCall(MatSetOption(*A,MAT_HERMITIAN,PETSC_TRUE));
      59             : 
      60             :   /* B = diag(b(xi)) */
      61           4 :   PetscCall(MatCreate(PETSC_COMM_WORLD,B));
      62           4 :   PetscCall(MatSetSizes(*B,PETSC_DECIDE,PETSC_DECIDE,n,n));
      63           4 :   PetscCall(MatSetFromOptions(*B));
      64           4 :   PetscCall(MatGetOwnershipRange(*B,&Istart,&Iend));
      65         516 :   for (i=Istart;i<Iend;i++) {
      66         512 :     xi = (i+1)*h;
      67         512 :     b = -4.1+xi*(1.0-PetscExpReal(xi-PETSC_PI));
      68         512 :     PetscCall(MatSetValue(*B,i,i,b,INSERT_VALUES));
      69             :   }
      70           4 :   PetscCall(MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY));
      71           4 :   PetscCall(MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY));
      72           4 :   PetscCall(MatSetOption(*B,MAT_HERMITIAN,PETSC_TRUE));
      73           4 :   PetscFunctionReturn(PETSC_SUCCESS);
      74             : }
      75             : 
      76             : /*
      77             :    Create preconditioner matrices (only Ap=diag(A))
      78             : */
      79           4 : PetscErrorCode BuildSplitPreconditioner(PetscInt n,PetscReal a,Mat *Ap)
      80             : {
      81           4 :   PetscInt       i,Istart,Iend;
      82           4 :   PetscReal      h;
      83             : 
      84           4 :   PetscFunctionBeginUser;
      85           4 :   h = PETSC_PI/(PetscReal)(n+1);
      86             : 
      87             :   /* Ap = diag(A) */
      88           4 :   PetscCall(MatCreate(PETSC_COMM_WORLD,Ap));
      89           4 :   PetscCall(MatSetSizes(*Ap,PETSC_DECIDE,PETSC_DECIDE,n,n));
      90           4 :   PetscCall(MatSetFromOptions(*Ap));
      91           4 :   PetscCall(MatGetOwnershipRange(*Ap,&Istart,&Iend));
      92         516 :   for (i=Istart;i<Iend;i++) PetscCall(MatSetValue(*Ap,i,i,-2.0/(h*h)+a,INSERT_VALUES));
      93           4 :   PetscCall(MatAssemblyBegin(*Ap,MAT_FINAL_ASSEMBLY));
      94           4 :   PetscCall(MatAssemblyEnd(*Ap,MAT_FINAL_ASSEMBLY));
      95           4 :   PetscCall(MatSetOption(*Ap,MAT_HERMITIAN,PETSC_TRUE));
      96           4 :   PetscFunctionReturn(PETSC_SUCCESS);
      97             : }
      98             : 
      99             : /*
     100             :    Compute Function matrix  T(lambda)
     101             : */
     102         125 : PetscErrorCode FormFunction(NEP nep,PetscScalar lambda,Mat fun,Mat B,void *ctx)
     103             : {
     104         125 :   ApplicationCtx *user = (ApplicationCtx*)ctx;
     105         125 :   PetscInt       i,n,Istart,Iend;
     106         125 :   PetscReal      h,xi;
     107         125 :   PetscScalar    b;
     108             : 
     109         125 :   PetscFunctionBeginUser;
     110         125 :   PetscCall(MatGetSize(fun,&n,NULL));
     111         125 :   h = PETSC_PI/(PetscReal)(n+1);
     112         125 :   PetscCall(MatGetOwnershipRange(fun,&Istart,&Iend));
     113       16125 :   for (i=Istart;i<Iend;i++) {
     114       16000 :     if (i>0) PetscCall(MatSetValue(fun,i,i-1,1.0/(h*h),INSERT_VALUES));
     115       16000 :     if (i<n-1) PetscCall(MatSetValue(fun,i,i+1,1.0/(h*h),INSERT_VALUES));
     116       16000 :     xi = (i+1)*h;
     117       16000 :     b = -4.1+xi*(1.0-PetscExpReal(xi-PETSC_PI));
     118       16000 :     PetscCall(MatSetValue(fun,i,i,-lambda-2.0/(h*h)+user->a+PetscExpScalar(-user->tau*lambda)*b,INSERT_VALUES));
     119       16000 :     if (B!=fun) PetscCall(MatSetValue(B,i,i,-lambda-2.0/(h*h)+user->a+PetscExpScalar(-user->tau*lambda)*b,INSERT_VALUES));
     120             :   }
     121         125 :   PetscCall(MatAssemblyBegin(fun,MAT_FINAL_ASSEMBLY));
     122         125 :   PetscCall(MatAssemblyEnd(fun,MAT_FINAL_ASSEMBLY));
     123         125 :   if (fun != B) {
     124          14 :     PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
     125          14 :     PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
     126             :   }
     127         125 :   PetscFunctionReturn(PETSC_SUCCESS);
     128             : }
     129             : 
     130             : /*
     131             :    Compute Jacobian matrix  T'(lambda)
     132             : */
     133           6 : PetscErrorCode FormJacobian(NEP nep,PetscScalar lambda,Mat jac,void *ctx)
     134             : {
     135           6 :   ApplicationCtx *user = (ApplicationCtx*)ctx;
     136           6 :   PetscInt       i,n,Istart,Iend;
     137           6 :   PetscReal      h,xi;
     138           6 :   PetscScalar    b;
     139             : 
     140           6 :   PetscFunctionBeginUser;
     141           6 :   PetscCall(MatGetSize(jac,&n,NULL));
     142           6 :   h = PETSC_PI/(PetscReal)(n+1);
     143           6 :   PetscCall(MatGetOwnershipRange(jac,&Istart,&Iend));
     144         774 :   for (i=Istart;i<Iend;i++) {
     145         768 :     xi = (i+1)*h;
     146         768 :     b = -4.1+xi*(1.0-PetscExpReal(xi-PETSC_PI));
     147         768 :     PetscCall(MatSetValue(jac,i,i,-1.0-user->tau*PetscExpScalar(-user->tau*lambda)*b,INSERT_VALUES));
     148             :   }
     149           6 :   PetscCall(MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY));
     150           6 :   PetscCall(MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY));
     151           6 :   PetscFunctionReturn(PETSC_SUCCESS);
     152             : }
     153             : 
     154           7 : int main(int argc,char **argv)
     155             : {
     156           7 :   NEP            nep;             /* nonlinear eigensolver context */
     157           7 :   Mat            Id,A,B,Ap,J,F,P; /* problem matrices */
     158           7 :   FN             f1,f2,f3;        /* functions to define the nonlinear operator */
     159           7 :   ApplicationCtx ctx;             /* user-defined context */
     160           7 :   Mat            mats[3],M;
     161           7 :   FN             funs[3];
     162           7 :   PetscScalar    coeffs[2];
     163           7 :   PetscInt       n=128,nterm;
     164           7 :   PetscReal      tau=0.001,a=20;
     165           7 :   PetscBool      split=PETSC_TRUE;
     166           7 :   MatStructure   mstr;
     167             : 
     168           7 :   PetscFunctionBeginUser;
     169           7 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
     170           7 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
     171           7 :   PetscCall(PetscOptionsGetReal(NULL,NULL,"-tau",&tau,NULL));
     172           7 :   PetscCall(PetscOptionsGetReal(NULL,NULL,"-a",&a,NULL));
     173           7 :   PetscCall(PetscOptionsGetBool(NULL,NULL,"-split",&split,NULL));
     174           7 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n1-D Delay Eigenproblem, n=%" PetscInt_FMT ", tau=%g, a=%g\n\n",n,(double)tau,(double)a));
     175             : 
     176             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     177             :               Create nonlinear eigensolver and solve the problem
     178             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     179             : 
     180           7 :   PetscCall(NEPCreate(PETSC_COMM_WORLD,&nep));
     181           7 :   if (split) {
     182           4 :     PetscCall(BuildSplitMatrices(n,a,&Id,&A,&B));
     183             :     /* f1=-lambda */
     184           4 :     PetscCall(FNCreate(PETSC_COMM_WORLD,&f1));
     185           4 :     PetscCall(FNSetType(f1,FNRATIONAL));
     186           4 :     coeffs[0] = -1.0; coeffs[1] = 0.0;
     187           4 :     PetscCall(FNRationalSetNumerator(f1,2,coeffs));
     188             :     /* f2=1.0 */
     189           4 :     PetscCall(FNCreate(PETSC_COMM_WORLD,&f2));
     190           4 :     PetscCall(FNSetType(f2,FNRATIONAL));
     191           4 :     coeffs[0] = 1.0;
     192           4 :     PetscCall(FNRationalSetNumerator(f2,1,coeffs));
     193             :     /* f3=exp(-tau*lambda) */
     194           4 :     PetscCall(FNCreate(PETSC_COMM_WORLD,&f3));
     195           4 :     PetscCall(FNSetType(f3,FNEXP));
     196           4 :     PetscCall(FNSetScale(f3,-tau,1.0));
     197           4 :     mats[0] = A;  funs[0] = f2;
     198           4 :     mats[1] = Id; funs[1] = f1;
     199           4 :     mats[2] = B;  funs[2] = f3;
     200           4 :     PetscCall(NEPSetSplitOperator(nep,3,mats,funs,SUBSET_NONZERO_PATTERN));
     201           4 :     PetscCall(BuildSplitPreconditioner(n,a,&Ap));
     202           4 :     mats[0] = Ap;
     203           4 :     mats[1] = Id;
     204           4 :     mats[2] = B;
     205           4 :     PetscCall(NEPSetSplitPreconditioner(nep,3,mats,SAME_NONZERO_PATTERN));
     206             :   } else {
     207             :     /* callback form  */
     208           3 :     ctx.tau = tau;
     209           3 :     ctx.a   = a;
     210           3 :     PetscCall(MatCreate(PETSC_COMM_WORLD,&F));
     211           3 :     PetscCall(MatSetSizes(F,PETSC_DECIDE,PETSC_DECIDE,n,n));
     212           3 :     PetscCall(MatSetFromOptions(F));
     213           3 :     PetscCall(MatSeqAIJSetPreallocation(F,3,NULL));
     214           3 :     PetscCall(MatMPIAIJSetPreallocation(F,3,NULL,1,NULL));
     215           3 :     PetscCall(MatDuplicate(F,MAT_DO_NOT_COPY_VALUES,&P));
     216           3 :     PetscCall(NEPSetFunction(nep,F,P,FormFunction,&ctx));
     217           3 :     PetscCall(MatCreate(PETSC_COMM_WORLD,&J));
     218           3 :     PetscCall(MatSetSizes(J,PETSC_DECIDE,PETSC_DECIDE,n,n));
     219           3 :     PetscCall(MatSetFromOptions(J));
     220           3 :     PetscCall(MatSeqAIJSetPreallocation(J,3,NULL));
     221           3 :     PetscCall(MatMPIAIJSetPreallocation(F,3,NULL,1,NULL));
     222           3 :     PetscCall(NEPSetJacobian(nep,J,FormJacobian,&ctx));
     223             :   }
     224             : 
     225             :   /* Set solver parameters at runtime */
     226           7 :   PetscCall(NEPSetFromOptions(nep));
     227             : 
     228           7 :   if (split) {
     229           4 :     PetscCall(NEPGetSplitPreconditionerInfo(nep,&nterm,&mstr));
     230           4 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD," Nonlinear preconditioner with %" PetscInt_FMT " terms, with %s nonzero pattern\n",nterm,MatStructures[mstr]));
     231           4 :     PetscCall(NEPGetSplitPreconditionerTerm(nep,0,&M));
     232             :   }
     233             : 
     234             :   /* Solve the eigensystem */
     235           7 :   PetscCall(NEPSolve(nep));
     236           7 :   PetscCall(NEPErrorView(nep,NEP_ERROR_RELATIVE,NULL));
     237             : 
     238           7 :   PetscCall(NEPDestroy(&nep));
     239           7 :   if (split) {
     240           4 :     PetscCall(MatDestroy(&Id));
     241           4 :     PetscCall(MatDestroy(&A));
     242           4 :     PetscCall(MatDestroy(&B));
     243           4 :     PetscCall(MatDestroy(&Ap));
     244           4 :     PetscCall(FNDestroy(&f1));
     245           4 :     PetscCall(FNDestroy(&f2));
     246           4 :     PetscCall(FNDestroy(&f3));
     247             :   } else {
     248           3 :     PetscCall(MatDestroy(&F));
     249           3 :     PetscCall(MatDestroy(&P));
     250           3 :     PetscCall(MatDestroy(&J));
     251             :   }
     252           7 :   PetscCall(SlepcFinalize());
     253             :   return 0;
     254             : }
     255             : 
     256             : /*TEST
     257             : 
     258             :    testset:
     259             :       args: -a 90000 -nep_nev 2
     260             :       requires: double !defined(PETSCTEST_VALGRIND)
     261             :       output_file: output/test17_1.out
     262             :       timeoutfactor: 2
     263             :       filter: grep -v "with 3 terms, with SAME"
     264             :       test:
     265             :          suffix: 1
     266             :          args: -nep_type slp -nep_two_sided {{0 1}} -split {{0 1}}
     267             : 
     268             :    testset:
     269             :       args: -nep_nev 2 -rg_type interval -rg_interval_endpoints .5,15,-.1,.1 -nep_target .7
     270             :       requires: !single
     271             :       output_file: output/test17_2.out
     272             :       filter: sed -e "s/[+-]0\.0*i//g" | grep -v "with 3 terms, with SAME"
     273             :       test:
     274             :          suffix: 2_interpol
     275             :          args: -nep_type interpol -nep_interpol_st_ksp_type bcgs -nep_interpol_st_pc_type sor -nep_tol 1e-6 -nep_interpol_st_ksp_rtol 1e-7
     276             :       test:
     277             :          suffix: 2_nleigs
     278             :          args: -nep_type nleigs -split {{0 1}}
     279             :          requires: complex
     280             :       test:
     281             :          suffix: 2_nleigs_real
     282             :          args: -nep_type nleigs -rg_interval_endpoints .5,11 -split {{0 1}} -nep_nleigs_ksp_type tfqmr
     283             :          requires: !complex !__float128
     284             : 
     285             :    testset:
     286             :       args: -nep_type ciss -rg_type ellipse -rg_ellipse_center 10 -rg_ellipse_radius 9.5 -rg_ellipse_vscale 0.1 -nep_ciss_ksp_type bcgs -nep_ciss_pc_type sor
     287             :       output_file: output/test17_3.out
     288             :       requires: complex !single !defined(PETSCTEST_VALGRIND)
     289             :       filter: grep -v "with 3 terms, with SAME"
     290             :       test:
     291             :          suffix: 3
     292             :          args: -split {{0 1}}
     293             :       test:
     294             :          suffix: 3_par
     295             :          nsize: 2
     296             :          args: -nep_ciss_partitions 2
     297             : 
     298             : TEST*/

Generated by: LCOV version 1.14