LCOV - code coverage report
Current view: top level - nep/tests - test16.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 85 122 69.7 %
Date: 2024-12-18 00:42:09 Functions: 3 4 75.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[] = "Illustrates use of NEPSetEigenvalueComparison().\n\n"
      12             :   "This is a simplified version of ex20.\n"
      13             :   "The command line options are:\n"
      14             :   "  -n <n>, where <n> = number of grid subdivisions.\n";
      15             : 
      16             : /*
      17             :    Solve 1-D PDE
      18             :             -u'' = lambda*u
      19             :    on [0,1] subject to
      20             :             u(0)=0, u'(1)=u(1)*lambda*kappa/(kappa-lambda)
      21             : */
      22             : 
      23             : #include <slepcnep.h>
      24             : 
      25             : /*
      26             :    User-defined routines
      27             : */
      28             : PetscErrorCode FormFunction(NEP,PetscScalar,Mat,Mat,void*);
      29             : PetscErrorCode FormJacobian(NEP,PetscScalar,Mat,void*);
      30             : PetscErrorCode MyEigenSort(PetscScalar,PetscScalar,PetscScalar,PetscScalar,PetscInt*,void*);
      31             : 
      32             : /*
      33             :    User-defined application context
      34             : */
      35             : typedef struct {
      36             :   PetscScalar kappa;   /* ratio between stiffness of spring and attached mass */
      37             :   PetscReal   h;       /* mesh spacing */
      38             : } ApplicationCtx;
      39             : 
      40           1 : int main(int argc,char **argv)
      41             : {
      42           1 :   NEP            nep;             /* nonlinear eigensolver context */
      43           1 :   Mat            F,J;             /* Function and Jacobian matrices */
      44           1 :   ApplicationCtx ctx;             /* user-defined context */
      45           1 :   PetscScalar    target;
      46           1 :   RG             rg;
      47           1 :   PetscInt       n=128;
      48           1 :   PetscBool      terse;
      49             : 
      50           1 :   PetscFunctionBeginUser;
      51           1 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      52           1 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      53           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n1-D Nonlinear Eigenproblem, n=%" PetscInt_FMT "\n\n",n));
      54           1 :   ctx.h = 1.0/(PetscReal)n;
      55           1 :   ctx.kappa = 1.0;
      56             : 
      57             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      58             :                Prepare nonlinear eigensolver context
      59             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      60             : 
      61           1 :   PetscCall(NEPCreate(PETSC_COMM_WORLD,&nep));
      62             : 
      63           1 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&F));
      64           1 :   PetscCall(MatSetSizes(F,PETSC_DECIDE,PETSC_DECIDE,n,n));
      65           1 :   PetscCall(MatSetFromOptions(F));
      66           1 :   PetscCall(MatSeqAIJSetPreallocation(F,3,NULL));
      67           1 :   PetscCall(MatMPIAIJSetPreallocation(F,3,NULL,1,NULL));
      68           1 :   PetscCall(NEPSetFunction(nep,F,F,FormFunction,&ctx));
      69             : 
      70           1 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&J));
      71           1 :   PetscCall(MatSetSizes(J,PETSC_DECIDE,PETSC_DECIDE,n,n));
      72           1 :   PetscCall(MatSetFromOptions(J));
      73           1 :   PetscCall(MatSeqAIJSetPreallocation(J,3,NULL));
      74           1 :   PetscCall(MatMPIAIJSetPreallocation(F,3,NULL,1,NULL));
      75           1 :   PetscCall(NEPSetJacobian(nep,J,FormJacobian,&ctx));
      76             : 
      77           1 :   PetscCall(NEPSetType(nep,NEPNLEIGS));
      78           1 :   PetscCall(NEPGetRG(nep,&rg));
      79           1 :   PetscCall(RGSetType(rg,RGINTERVAL));
      80             : #if defined(PETSC_USE_COMPLEX)
      81             :   PetscCall(RGIntervalSetEndpoints(rg,2.0,400.0,-0.001,0.001));
      82             : #else
      83           1 :   PetscCall(RGIntervalSetEndpoints(rg,2.0,400.0,0,0));
      84             : #endif
      85           1 :   PetscCall(NEPSetTarget(nep,25.0));
      86           1 :   PetscCall(NEPSetEigenvalueComparison(nep,MyEigenSort,&target));
      87           1 :   PetscCall(NEPSetTolerances(nep,PETSC_SMALL,PETSC_CURRENT));
      88           1 :   PetscCall(NEPSetFromOptions(nep));
      89           1 :   PetscCall(NEPGetTarget(nep,&target));
      90             : 
      91             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      92             :               Solve the eigensystem and display the solution
      93             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      94             : 
      95           1 :   PetscCall(NEPSolve(nep));
      96             : 
      97             :   /* show detailed info unless -terse option is given by user */
      98           1 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
      99           1 :   if (terse) PetscCall(NEPErrorView(nep,NEP_ERROR_RELATIVE,NULL));
     100             :   else {
     101           0 :     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
     102           0 :     PetscCall(NEPConvergedReasonView(nep,PETSC_VIEWER_STDOUT_WORLD));
     103           0 :     PetscCall(NEPErrorView(nep,NEP_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
     104           0 :     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
     105             :   }
     106             : 
     107           1 :   PetscCall(NEPDestroy(&nep));
     108           1 :   PetscCall(MatDestroy(&F));
     109           1 :   PetscCall(MatDestroy(&J));
     110           1 :   PetscCall(SlepcFinalize());
     111             :   return 0;
     112             : }
     113             : 
     114             : /* ------------------------------------------------------------------- */
     115             : /*
     116             :    FormFunction - Computes Function matrix  T(lambda)
     117             : 
     118             :    Input Parameters:
     119             : .  nep    - the NEP context
     120             : .  lambda - the scalar argument
     121             : .  ctx    - optional user-defined context, as set by NEPSetFunction()
     122             : 
     123             :    Output Parameters:
     124             : .  fun - Function matrix
     125             : .  B   - optionally different preconditioning matrix
     126             : */
     127         108 : PetscErrorCode FormFunction(NEP nep,PetscScalar lambda,Mat fun,Mat B,void *ctx)
     128             : {
     129         108 :   ApplicationCtx *user = (ApplicationCtx*)ctx;
     130         108 :   PetscScalar    A[3],c,d;
     131         108 :   PetscReal      h;
     132         108 :   PetscInt       i,n,j[3],Istart,Iend;
     133         108 :   PetscBool      FirstBlock=PETSC_FALSE,LastBlock=PETSC_FALSE;
     134             : 
     135         108 :   PetscFunctionBeginUser;
     136             :   /*
     137             :      Compute Function entries and insert into matrix
     138             :   */
     139         108 :   PetscCall(MatGetSize(fun,&n,NULL));
     140         108 :   PetscCall(MatGetOwnershipRange(fun,&Istart,&Iend));
     141         108 :   if (Istart==0) FirstBlock=PETSC_TRUE;
     142         108 :   if (Iend==n) LastBlock=PETSC_TRUE;
     143         108 :   h = user->h;
     144         108 :   c = user->kappa/(lambda-user->kappa);
     145         108 :   d = n;
     146             : 
     147             :   /*
     148             :      Interior grid points
     149             :   */
     150       13716 :   for (i=(FirstBlock? Istart+1: Istart);i<(LastBlock? Iend-1: Iend);i++) {
     151       13608 :     j[0] = i-1; j[1] = i; j[2] = i+1;
     152       13608 :     A[0] = A[2] = -d-lambda*h/6.0; A[1] = 2.0*(d-lambda*h/3.0);
     153       13608 :     PetscCall(MatSetValues(fun,1,&i,3,j,A,INSERT_VALUES));
     154             :   }
     155             : 
     156             :   /*
     157             :      Boundary points
     158             :   */
     159         108 :   if (FirstBlock) {
     160         108 :     i = 0;
     161         108 :     j[0] = 0; j[1] = 1;
     162         108 :     A[0] = 2.0*(d-lambda*h/3.0); A[1] = -d-lambda*h/6.0;
     163         108 :     PetscCall(MatSetValues(fun,1,&i,2,j,A,INSERT_VALUES));
     164             :   }
     165             : 
     166         108 :   if (LastBlock) {
     167         108 :     i = n-1;
     168         108 :     j[0] = n-2; j[1] = n-1;
     169         108 :     A[0] = -d-lambda*h/6.0; A[1] = d-lambda*h/3.0+lambda*c;
     170         108 :     PetscCall(MatSetValues(fun,1,&i,2,j,A,INSERT_VALUES));
     171             :   }
     172             : 
     173             :   /*
     174             :      Assemble matrix
     175             :   */
     176         108 :   PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
     177         108 :   PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
     178         108 :   if (fun != B) {
     179           0 :     PetscCall(MatAssemblyBegin(fun,MAT_FINAL_ASSEMBLY));
     180           0 :     PetscCall(MatAssemblyEnd(fun,MAT_FINAL_ASSEMBLY));
     181             :   }
     182         108 :   PetscFunctionReturn(PETSC_SUCCESS);
     183             : }
     184             : 
     185             : /* ------------------------------------------------------------------- */
     186             : /*
     187             :    FormJacobian - Computes Jacobian matrix  T'(lambda)
     188             : 
     189             :    Input Parameters:
     190             : .  nep    - the NEP context
     191             : .  lambda - the scalar argument
     192             : .  ctx    - optional user-defined context, as set by NEPSetJacobian()
     193             : 
     194             :    Output Parameters:
     195             : .  jac - Jacobian matrix
     196             : .  B   - optionally different preconditioning matrix
     197             : */
     198           0 : PetscErrorCode FormJacobian(NEP nep,PetscScalar lambda,Mat jac,void *ctx)
     199             : {
     200           0 :   ApplicationCtx *user = (ApplicationCtx*)ctx;
     201           0 :   PetscScalar    A[3],c;
     202           0 :   PetscReal      h;
     203           0 :   PetscInt       i,n,j[3],Istart,Iend;
     204           0 :   PetscBool      FirstBlock=PETSC_FALSE,LastBlock=PETSC_FALSE;
     205             : 
     206           0 :   PetscFunctionBeginUser;
     207             :   /*
     208             :      Compute Jacobian entries and insert into matrix
     209             :   */
     210           0 :   PetscCall(MatGetSize(jac,&n,NULL));
     211           0 :   PetscCall(MatGetOwnershipRange(jac,&Istart,&Iend));
     212           0 :   if (Istart==0) FirstBlock=PETSC_TRUE;
     213           0 :   if (Iend==n) LastBlock=PETSC_TRUE;
     214           0 :   h = user->h;
     215           0 :   c = user->kappa/(lambda-user->kappa);
     216             : 
     217             :   /*
     218             :      Interior grid points
     219             :   */
     220           0 :   for (i=(FirstBlock? Istart+1: Istart);i<(LastBlock? Iend-1: Iend);i++) {
     221           0 :     j[0] = i-1; j[1] = i; j[2] = i+1;
     222           0 :     A[0] = A[2] = -h/6.0; A[1] = -2.0*h/3.0;
     223           0 :     PetscCall(MatSetValues(jac,1,&i,3,j,A,INSERT_VALUES));
     224             :   }
     225             : 
     226             :   /*
     227             :      Boundary points
     228             :   */
     229           0 :   if (FirstBlock) {
     230           0 :     i = 0;
     231           0 :     j[0] = 0; j[1] = 1;
     232           0 :     A[0] = -2.0*h/3.0; A[1] = -h/6.0;
     233           0 :     PetscCall(MatSetValues(jac,1,&i,2,j,A,INSERT_VALUES));
     234             :   }
     235             : 
     236           0 :   if (LastBlock) {
     237           0 :     i = n-1;
     238           0 :     j[0] = n-2; j[1] = n-1;
     239           0 :     A[0] = -h/6.0; A[1] = -h/3.0-c*c;
     240           0 :     PetscCall(MatSetValues(jac,1,&i,2,j,A,INSERT_VALUES));
     241             :   }
     242             : 
     243             :   /*
     244             :      Assemble matrix
     245             :   */
     246           0 :   PetscCall(MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY));
     247           0 :   PetscCall(MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY));
     248           0 :   PetscFunctionReturn(PETSC_SUCCESS);
     249             : }
     250             : 
     251             : /*
     252             :     Function for user-defined eigenvalue ordering criterion.
     253             : 
     254             :     Given two eigenvalues ar+i*ai and br+i*bi, the subroutine must choose
     255             :     one of them as the preferred one according to the criterion.
     256             :     In this example, eigenvalues are sorted with respect to the target,
     257             :     but those on the right of the target are preferred.
     258             : */
     259         196 : PetscErrorCode MyEigenSort(PetscScalar ar,PetscScalar ai,PetscScalar br,PetscScalar bi,PetscInt *r,void *ctx)
     260             : {
     261         196 :   PetscReal   a,b;
     262         196 :   PetscScalar target = *(PetscScalar*)ctx;
     263             : 
     264         196 :   PetscFunctionBeginUser;
     265         196 :   if (PetscRealPart(ar-target)<0.0 && PetscRealPart(br-target)>0.0) *r = 1;
     266             :   else {
     267         109 :     a = SlepcAbsEigenvalue(ar-target,ai);
     268         109 :     b = SlepcAbsEigenvalue(br-target,bi);
     269         109 :     if (a>b) *r = 1;
     270          85 :     else if (a<b) *r = -1;
     271           0 :     else *r = 0;
     272             :   }
     273         196 :   PetscFunctionReturn(PETSC_SUCCESS);
     274             : }
     275             : 
     276             : /*TEST
     277             : 
     278             :    test:
     279             :       suffix: 1
     280             :       args: -nep_nev 4 -nep_ncv 8 -terse
     281             :       requires: double !complex
     282             : 
     283             : TEST*/

Generated by: LCOV version 1.14