Date: 2023-09-05T10:18:21+00:00

Actual source code: test14.c
slepc-3.19.2 2023-09-05
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-, 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: */

     11: static char help[] = "Tests a user-defined convergence test in NEP.\n\n"
     12:   "The command line options are:\n"
     13:   "  -n <n>, where <n> = matrix dimension.\n";

     15: /*
     16:    Solve T(lambda)x=0 with T(lambda) = -D+sqrt(lambda)*I
     17:       where D is the Laplacian operator in 1 dimension
     18: */

     20: #include <slepcnep.h>

     22: /*
     23:   MyConvergedRel - Convergence test relative to the norm of D (given in ctx).
     24: */
     25: PetscErrorCode MyConvergedRel(NEP nep,PetscScalar eigr,PetscScalar eigi,PetscReal res,PetscReal *errest,void *ctx)
     26: {
     27:   PetscReal norm = *(PetscReal*)ctx;

     29:   PetscFunctionBegin;
     30:   *errest = res/norm;
     31:   PetscFunctionReturn(PETSC_SUCCESS);
     32: }

     34: int main(int argc,char **argv)
     35: {
     36:   NEP            nep;             /* nonlinear eigensolver context */
     37:   Mat            A[2];
     38:   PetscInt       n=100,Istart,Iend,i;
     39:   PetscBool      terse;
     40:   PetscReal      norm;
     41:   FN             f[2];
     42:   PetscScalar    coeffs;

     44:   PetscFunctionBeginUser;
     45:   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
     46:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
     47:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nSquare root eigenproblem, n=%" PetscInt_FMT "\n\n",n));

     49:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     50:      Create nonlinear eigensolver, define problem in split form
     51:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     53:   PetscCall(NEPCreate(PETSC_COMM_WORLD,&nep));

     55:   /* Create matrices */
     56:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A[0]));
     57:   PetscCall(MatSetSizes(A[0],PETSC_DECIDE,PETSC_DECIDE,n,n));
     58:   PetscCall(MatSetFromOptions(A[0]));
     59:   PetscCall(MatSetUp(A[0]));
     60:   PetscCall(MatGetOwnershipRange(A[0],&Istart,&Iend));
     61:   for (i=Istart;i<Iend;i++) {
     62:     if (i>0) PetscCall(MatSetValue(A[0],i,i-1,1.0,INSERT_VALUES));
     63:     if (i<n-1) PetscCall(MatSetValue(A[0],i,i+1,1.0,INSERT_VALUES));
     64:     PetscCall(MatSetValue(A[0],i,i,-2.0,INSERT_VALUES));
     65:   }
     66:   PetscCall(MatAssemblyBegin(A[0],MAT_FINAL_ASSEMBLY));
     67:   PetscCall(MatAssemblyEnd(A[0],MAT_FINAL_ASSEMBLY));

     69:   PetscCall(MatCreateConstantDiagonal(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,n,n,1.0,&A[1]));

     71:   /* Define functions */
     72:   PetscCall(FNCreate(PETSC_COMM_WORLD,&f[0]));
     73:   PetscCall(FNSetType(f[0],FNRATIONAL));
     74:   coeffs = 1.0;
     75:   PetscCall(FNRationalSetNumerator(f[0],1,&coeffs));
     76:   PetscCall(FNCreate(PETSC_COMM_WORLD,&f[1]));
     77:   PetscCall(FNSetType(f[1],FNSQRT));
     78:   PetscCall(NEPSetSplitOperator(nep,2,A,f,SUBSET_NONZERO_PATTERN));

     80:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     81:                    Set some options and solve
     82:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     84:   PetscCall(NEPSetTarget(nep,1.1));

     86:   /* setup convergence test relative to the norm of D */
     87:   PetscCall(MatNorm(A[0],NORM_1,&norm));
     88:   PetscCall(NEPSetConvergenceTestFunction(nep,MyConvergedRel,&norm,NULL));

     90:   PetscCall(NEPSetFromOptions(nep));
     91:   PetscCall(NEPSolve(nep));

     93:   /* show detailed info unless -terse option is given by user */
     94:   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
     95:   if (terse) PetscCall(NEPErrorView(nep,NEP_ERROR_BACKWARD,NULL));
     96:   else {
     97:     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
     98:     PetscCall(NEPConvergedReasonView(nep,PETSC_VIEWER_STDOUT_WORLD));
     99:     PetscCall(NEPErrorView(nep,NEP_ERROR_BACKWARD,PETSC_VIEWER_STDOUT_WORLD));
    100:     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
    101:   }
    102:   PetscCall(NEPDestroy(&nep));
    103:   PetscCall(MatDestroy(&A[0]));
    104:   PetscCall(MatDestroy(&A[1]));
    105:   PetscCall(FNDestroy(&f[0]));
    106:   PetscCall(FNDestroy(&f[1]));
    107:   PetscCall(SlepcFinalize());
    108:   return 0;
    109: }

    111: /*TEST

    113:    test:
    114:       suffix: 1
    115:       args: -nep_type slp -nep_nev 2 -terse

    117: TEST*/