LCOV - code coverage report
Current view: top level - nep/interface - nepdefault.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 67 85 78.8 %
Date: 2021-08-02 00:35:43 Functions: 7 7 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-2021, 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             :    Simple default routines for common NEP operations
      12             : */
      13             : 
      14             : #include <slepc/private/nepimpl.h>     /*I "slepcnep.h" I*/
      15             : 
      16             : /*@
      17             :    NEPSetWorkVecs - Sets a number of work vectors into a NEP object
      18             : 
      19             :    Collective on nep
      20             : 
      21             :    Input Parameters:
      22             : +  nep - nonlinear eigensolver context
      23             : -  nw  - number of work vectors to allocate
      24             : 
      25             :    Developers Note:
      26             :    This is SLEPC_EXTERN because it may be required by user plugin NEP
      27             :    implementations.
      28             : 
      29             :    Level: developer
      30             : @*/
      31         677 : PetscErrorCode NEPSetWorkVecs(NEP nep,PetscInt nw)
      32             : {
      33         677 :   PetscErrorCode ierr;
      34         677 :   Vec            t;
      35             : 
      36         677 :   PetscFunctionBegin;
      37         677 :   if (nep->nwork < nw) {
      38         162 :     ierr = VecDestroyVecs(nep->nwork,&nep->work);CHKERRQ(ierr);
      39         162 :     nep->nwork = nw;
      40         162 :     ierr = BVGetColumn(nep->V,0,&t);CHKERRQ(ierr);
      41         162 :     ierr = VecDuplicateVecs(t,nw,&nep->work);CHKERRQ(ierr);
      42         162 :     ierr = BVRestoreColumn(nep->V,0,&t);CHKERRQ(ierr);
      43         613 :     ierr = PetscLogObjectParents(nep,nw,nep->work);CHKERRQ(ierr);
      44             :   }
      45         677 :   PetscFunctionReturn(0);
      46             : }
      47             : 
      48             : /*
      49             :   NEPGetDefaultShift - Return the value of sigma to start the nonlinear iteration.
      50             :  */
      51          55 : PetscErrorCode NEPGetDefaultShift(NEP nep,PetscScalar *sigma)
      52             : {
      53          55 :   PetscFunctionBegin;
      54          55 :   PetscValidScalarPointer(sigma,2);
      55          55 :   switch (nep->which) {
      56           0 :     case NEP_LARGEST_MAGNITUDE:
      57             :     case NEP_LARGEST_IMAGINARY:
      58             :     case NEP_ALL:
      59             :     case NEP_WHICH_USER:
      60           0 :       *sigma = 1.0;   /* arbitrary value */
      61           0 :       break;
      62           0 :     case NEP_SMALLEST_MAGNITUDE:
      63             :     case NEP_SMALLEST_IMAGINARY:
      64           0 :       *sigma = 0.0;
      65           0 :       break;
      66           0 :     case NEP_LARGEST_REAL:
      67           0 :       *sigma = PETSC_MAX_REAL;
      68           0 :       break;
      69           0 :     case NEP_SMALLEST_REAL:
      70           0 :       *sigma = PETSC_MIN_REAL;
      71           0 :       break;
      72          55 :     case NEP_TARGET_MAGNITUDE:
      73             :     case NEP_TARGET_REAL:
      74             :     case NEP_TARGET_IMAGINARY:
      75          55 :       *sigma = nep->target;
      76          55 :       break;
      77             :   }
      78          55 :   PetscFunctionReturn(0);
      79             : }
      80             : 
      81             : /*
      82             :   NEPConvergedRelative - Checks convergence relative to the eigenvalue.
      83             : */
      84         875 : PetscErrorCode NEPConvergedRelative(NEP nep,PetscScalar eigr,PetscScalar eigi,PetscReal res,PetscReal *errest,void *ctx)
      85             : {
      86         875 :   PetscReal w;
      87             : 
      88         875 :   PetscFunctionBegin;
      89         875 :   w = SlepcAbsEigenvalue(eigr,eigi);
      90         875 :   *errest = res/w;
      91         875 :   PetscFunctionReturn(0);
      92             : }
      93             : 
      94             : /*
      95             :   NEPConvergedAbsolute - Checks convergence absolutely.
      96             : */
      97           9 : PetscErrorCode NEPConvergedAbsolute(NEP nep,PetscScalar eigr,PetscScalar eigi,PetscReal res,PetscReal *errest,void *ctx)
      98             : {
      99           9 :   PetscFunctionBegin;
     100           9 :   *errest = res;
     101           9 :   PetscFunctionReturn(0);
     102             : }
     103             : 
     104             : /*
     105             :   NEPConvergedNorm - Checks convergence relative to the matrix norms.
     106             : */
     107           8 : PetscErrorCode NEPConvergedNorm(NEP nep,PetscScalar eigr,PetscScalar eigi,PetscReal res,PetscReal *errest,void *ctx)
     108             : {
     109           8 :   PetscScalar    s;
     110           8 :   PetscReal      w=0.0;
     111           8 :   PetscInt       j;
     112           8 :   PetscBool      flg;
     113           8 :   PetscErrorCode ierr;
     114             : 
     115           8 :   PetscFunctionBegin;
     116           8 :   if (nep->fui!=NEP_USER_INTERFACE_SPLIT) {
     117           0 :     ierr = NEPComputeFunction(nep,eigr,nep->function,nep->function);CHKERRQ(ierr);
     118           0 :     ierr = MatHasOperation(nep->function,MATOP_NORM,&flg);CHKERRQ(ierr);
     119           0 :     if (!flg) SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_WRONG,"The computation of backward errors requires a matrix norm operation");
     120           0 :     ierr = MatNorm(nep->function,NORM_INFINITY,&w);CHKERRQ(ierr);
     121             :   } else {
     122             :     /* initialization of matrix norms */
     123           8 :     if (!nep->nrma[0]) {
     124           3 :       for (j=0;j<nep->nt;j++) {
     125           2 :         ierr = MatHasOperation(nep->A[j],MATOP_NORM,&flg);CHKERRQ(ierr);
     126           2 :         if (!flg) SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_WRONG,"The convergence test related to the matrix norms requires a matrix norm operation");
     127           2 :         ierr = MatNorm(nep->A[j],NORM_INFINITY,&nep->nrma[j]);CHKERRQ(ierr);
     128             :       }
     129             :     }
     130          24 :     for (j=0;j<nep->nt;j++) {
     131          16 :       ierr = FNEvaluateFunction(nep->f[j],eigr,&s);CHKERRQ(ierr);
     132          16 :       w = w + nep->nrma[j]*PetscAbsScalar(s);
     133             :     }
     134             :   }
     135           8 :   *errest = res/w;
     136           8 :   PetscFunctionReturn(0);
     137             : }
     138             : 
     139             : /*@C
     140             :    NEPStoppingBasic - Default routine to determine whether the outer eigensolver
     141             :    iteration must be stopped.
     142             : 
     143             :    Collective on nep
     144             : 
     145             :    Input Parameters:
     146             : +  nep    - nonlinear eigensolver context obtained from NEPCreate()
     147             : .  its    - current number of iterations
     148             : .  max_it - maximum number of iterations
     149             : .  nconv  - number of currently converged eigenpairs
     150             : .  nev    - number of requested eigenpairs
     151             : -  ctx    - context (not used here)
     152             : 
     153             :    Output Parameter:
     154             : .  reason - result of the stopping test
     155             : 
     156             :    Notes:
     157             :    A positive value of reason indicates that the iteration has finished successfully
     158             :    (converged), and a negative value indicates an error condition (diverged). If
     159             :    the iteration needs to be continued, reason must be set to NEP_CONVERGED_ITERATING
     160             :    (zero).
     161             : 
     162             :    NEPStoppingBasic() will stop if all requested eigenvalues are converged, or if
     163             :    the maximum number of iterations has been reached.
     164             : 
     165             :    Use NEPSetStoppingTest() to provide your own test instead of using this one.
     166             : 
     167             :    Level: advanced
     168             : 
     169             : .seealso: NEPSetStoppingTest(), NEPConvergedReason, NEPGetConvergedReason()
     170             : @*/
     171         677 : PetscErrorCode NEPStoppingBasic(NEP nep,PetscInt its,PetscInt max_it,PetscInt nconv,PetscInt nev,NEPConvergedReason *reason,void *ctx)
     172             : {
     173         677 :   PetscErrorCode ierr;
     174             : 
     175         677 :   PetscFunctionBegin;
     176         677 :   *reason = NEP_CONVERGED_ITERATING;
     177         677 :   if (nconv >= nev) {
     178          70 :     ierr = PetscInfo2(nep,"Nonlinear eigensolver finished successfully: %D eigenpairs converged at iteration %D\n",nconv,its);CHKERRQ(ierr);
     179          70 :     *reason = NEP_CONVERGED_TOL;
     180         607 :   } else if (its >= max_it) {
     181           0 :     *reason = NEP_DIVERGED_ITS;
     182           0 :     ierr = PetscInfo1(nep,"Nonlinear eigensolver iteration reached maximum number of iterations (%D)\n",its);CHKERRQ(ierr);
     183             :   }
     184         677 :   PetscFunctionReturn(0);
     185             : }
     186             : 
     187          67 : PetscErrorCode NEPComputeVectors_Schur(NEP nep)
     188             : {
     189          67 :   PetscErrorCode ierr;
     190          67 :   Mat            Z;
     191             : 
     192          67 :   PetscFunctionBegin;
     193          67 :   ierr = DSVectors(nep->ds,DS_MAT_X,NULL,NULL);CHKERRQ(ierr);
     194          67 :   ierr = DSGetMat(nep->ds,DS_MAT_X,&Z);CHKERRQ(ierr);
     195          67 :   ierr = BVMultInPlace(nep->V,Z,0,nep->nconv);CHKERRQ(ierr);
     196          67 :   ierr = MatDestroy(&Z);CHKERRQ(ierr);
     197          67 :   ierr = BVNormalize(nep->V,nep->eigi);CHKERRQ(ierr);
     198          67 :   PetscFunctionReturn(0);
     199             : }
     200             : 

Generated by: LCOV version 1.14