LCOV - code coverage report
Current view: top level - mfn/interface - mfnopts.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 129 132 97.7 %
Date: 2024-04-25 00:29:53 Functions: 11 11 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             :    MFN routines related to options that can be set via the command-line
      12             :    or procedurally
      13             : */
      14             : 
      15             : #include <slepc/private/mfnimpl.h>   /*I "slepcmfn.h" I*/
      16             : #include <petscdraw.h>
      17             : 
      18             : /*@C
      19             :    MFNMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type
      20             :    indicated by the user.
      21             : 
      22             :    Collective
      23             : 
      24             :    Input Parameters:
      25             : +  mfn  - the matrix function context
      26             : .  opt  - the command line option for this monitor
      27             : .  name - the monitor type one is seeking
      28             : -  ctx  - an optional user context for the monitor, or NULL
      29             : 
      30             :    Level: developer
      31             : 
      32             : .seealso: MFNMonitorSet()
      33             : @*/
      34          18 : PetscErrorCode MFNMonitorSetFromOptions(MFN mfn,const char opt[],const char name[],void *ctx)
      35             : {
      36          18 :   PetscErrorCode       (*mfunc)(MFN,PetscInt,PetscReal,void*);
      37          18 :   PetscErrorCode       (*cfunc)(PetscViewer,PetscViewerFormat,void*,PetscViewerAndFormat**);
      38          18 :   PetscErrorCode       (*dfunc)(PetscViewerAndFormat**);
      39          18 :   PetscViewerAndFormat *vf;
      40          18 :   PetscViewer          viewer;
      41          18 :   PetscViewerFormat    format;
      42          18 :   PetscViewerType      vtype;
      43          18 :   char                 key[PETSC_MAX_PATH_LEN];
      44          18 :   PetscBool            flg;
      45             : 
      46          18 :   PetscFunctionBegin;
      47          18 :   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)mfn),((PetscObject)mfn)->options,((PetscObject)mfn)->prefix,opt,&viewer,&format,&flg));
      48          18 :   if (!flg) PetscFunctionReturn(PETSC_SUCCESS);
      49             : 
      50           2 :   PetscCall(PetscViewerGetType(viewer,&vtype));
      51           2 :   PetscCall(SlepcMonitorMakeKey_Internal(name,vtype,format,key));
      52           2 :   PetscCall(PetscFunctionListFind(MFNMonitorList,key,&mfunc));
      53           2 :   PetscCheck(mfunc,PetscObjectComm((PetscObject)mfn),PETSC_ERR_SUP,"Specified viewer and format not supported");
      54           2 :   PetscCall(PetscFunctionListFind(MFNMonitorCreateList,key,&cfunc));
      55           2 :   PetscCall(PetscFunctionListFind(MFNMonitorDestroyList,key,&dfunc));
      56           2 :   if (!cfunc) cfunc = PetscViewerAndFormatCreate_Internal;
      57           2 :   if (!dfunc) dfunc = PetscViewerAndFormatDestroy;
      58             : 
      59           2 :   PetscCall((*cfunc)(viewer,format,ctx,&vf));
      60           2 :   PetscCall(PetscOptionsRestoreViewer(&viewer));
      61           2 :   PetscCall(MFNMonitorSet(mfn,mfunc,vf,(PetscErrorCode(*)(void **))dfunc));
      62           2 :   PetscFunctionReturn(PETSC_SUCCESS);
      63             : }
      64             : 
      65             : /*@
      66             :    MFNSetFromOptions - Sets MFN options from the options database.
      67             :    This routine must be called before MFNSetUp() if the user is to be
      68             :    allowed to set the solver type.
      69             : 
      70             :    Collective
      71             : 
      72             :    Input Parameters:
      73             : .  mfn - the matrix function context
      74             : 
      75             :    Notes:
      76             :    To see all options, run your program with the -help option.
      77             : 
      78             :    Level: beginner
      79             : 
      80             : .seealso: MFNSetOptionsPrefix()
      81             : @*/
      82          18 : PetscErrorCode MFNSetFromOptions(MFN mfn)
      83             : {
      84          18 :   char           type[256];
      85          18 :   PetscBool      set,flg,flg1,flg2;
      86          18 :   PetscReal      r;
      87          18 :   PetscInt       i;
      88             : 
      89          18 :   PetscFunctionBegin;
      90          18 :   PetscValidHeaderSpecific(mfn,MFN_CLASSID,1);
      91          18 :   PetscCall(MFNRegisterAll());
      92          54 :   PetscObjectOptionsBegin((PetscObject)mfn);
      93          22 :     PetscCall(PetscOptionsFList("-mfn_type","Matrix Function method","MFNSetType",MFNList,(char*)(((PetscObject)mfn)->type_name?((PetscObject)mfn)->type_name:MFNKRYLOV),type,sizeof(type),&flg));
      94          18 :     if (flg) PetscCall(MFNSetType(mfn,type));
      95          13 :     else if (!((PetscObject)mfn)->type_name) PetscCall(MFNSetType(mfn,MFNKRYLOV));
      96             : 
      97          18 :     i = mfn->max_it;
      98          18 :     PetscCall(PetscOptionsInt("-mfn_max_it","Maximum number of iterations","MFNSetTolerances",mfn->max_it,&i,&flg1));
      99          18 :     if (!flg1) i = PETSC_DEFAULT;
     100          18 :     r = mfn->tol;
     101          31 :     PetscCall(PetscOptionsReal("-mfn_tol","Tolerance","MFNSetTolerances",SlepcDefaultTol(mfn->tol),&r,&flg2));
     102          18 :     if (flg1 || flg2) PetscCall(MFNSetTolerances(mfn,r,i));
     103             : 
     104          18 :     PetscCall(PetscOptionsInt("-mfn_ncv","Number of basis vectors","MFNSetDimensions",mfn->ncv,&i,&flg));
     105          18 :     if (flg) PetscCall(MFNSetDimensions(mfn,i));
     106             : 
     107          18 :     PetscCall(PetscOptionsBool("-mfn_error_if_not_converged","Generate error if solver does not converge","MFNSetErrorIfNotConverged",mfn->errorifnotconverged,&mfn->errorifnotconverged,NULL));
     108             : 
     109             :     /* -----------------------------------------------------------------------*/
     110             :     /*
     111             :       Cancels all monitors hardwired into code before call to MFNSetFromOptions()
     112             :     */
     113          18 :     PetscCall(PetscOptionsBool("-mfn_monitor_cancel","Remove any hardwired monitor routines","MFNMonitorCancel",PETSC_FALSE,&flg,&set));
     114          18 :     if (set && flg) PetscCall(MFNMonitorCancel(mfn));
     115          18 :     PetscCall(MFNMonitorSetFromOptions(mfn,"-mfn_monitor","error_estimate",NULL));
     116             : 
     117             :     /* -----------------------------------------------------------------------*/
     118          18 :     PetscCall(PetscOptionsName("-mfn_view","Print detailed information on solver used","MFNView",&set));
     119             : 
     120          18 :     PetscTryTypeMethod(mfn,setfromoptions,PetscOptionsObject);
     121          18 :     PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)mfn,PetscOptionsObject));
     122          18 :   PetscOptionsEnd();
     123             : 
     124          18 :   if (!mfn->V) PetscCall(MFNGetBV(mfn,&mfn->V));
     125          18 :   PetscCall(BVSetFromOptions(mfn->V));
     126          18 :   if (!mfn->fn) PetscCall(MFNGetFN(mfn,&mfn->fn));
     127          18 :   PetscCall(FNSetFromOptions(mfn->fn));
     128          18 :   PetscFunctionReturn(PETSC_SUCCESS);
     129             : }
     130             : 
     131             : /*@C
     132             :    MFNGetTolerances - Gets the tolerance and maximum iteration count used
     133             :    by the MFN convergence tests.
     134             : 
     135             :    Not Collective
     136             : 
     137             :    Input Parameter:
     138             : .  mfn - the matrix function context
     139             : 
     140             :    Output Parameters:
     141             : +  tol - the convergence tolerance
     142             : -  maxits - maximum number of iterations
     143             : 
     144             :    Notes:
     145             :    The user can specify NULL for any parameter that is not needed.
     146             : 
     147             :    Level: intermediate
     148             : 
     149             : .seealso: MFNSetTolerances()
     150             : @*/
     151           6 : PetscErrorCode MFNGetTolerances(MFN mfn,PetscReal *tol,PetscInt *maxits)
     152             : {
     153           6 :   PetscFunctionBegin;
     154           6 :   PetscValidHeaderSpecific(mfn,MFN_CLASSID,1);
     155           6 :   if (tol)    *tol    = mfn->tol;
     156           6 :   if (maxits) *maxits = mfn->max_it;
     157           6 :   PetscFunctionReturn(PETSC_SUCCESS);
     158             : }
     159             : 
     160             : /*@
     161             :    MFNSetTolerances - Sets the tolerance and maximum iteration count used
     162             :    by the MFN convergence tests.
     163             : 
     164             :    Logically Collective
     165             : 
     166             :    Input Parameters:
     167             : +  mfn - the matrix function context
     168             : .  tol - the convergence tolerance
     169             : -  maxits - maximum number of iterations to use
     170             : 
     171             :    Options Database Keys:
     172             : +  -mfn_tol <tol> - Sets the convergence tolerance
     173             : -  -mfn_max_it <maxits> - Sets the maximum number of iterations allowed
     174             : 
     175             :    Notes:
     176             :    Use PETSC_DEFAULT for either argument to assign a reasonably good value.
     177             : 
     178             :    Level: intermediate
     179             : 
     180             : .seealso: MFNGetTolerances()
     181             : @*/
     182          11 : PetscErrorCode MFNSetTolerances(MFN mfn,PetscReal tol,PetscInt maxits)
     183             : {
     184          11 :   PetscFunctionBegin;
     185          11 :   PetscValidHeaderSpecific(mfn,MFN_CLASSID,1);
     186          44 :   PetscValidLogicalCollectiveReal(mfn,tol,2);
     187          44 :   PetscValidLogicalCollectiveInt(mfn,maxits,3);
     188          11 :   if (tol == (PetscReal)PETSC_DEFAULT) {
     189           0 :     mfn->tol = PETSC_DEFAULT;
     190           0 :     mfn->setupcalled = 0;
     191             :   } else {
     192          11 :     PetscCheck(tol>0.0,PetscObjectComm((PetscObject)mfn),PETSC_ERR_ARG_OUTOFRANGE,"Illegal value of tol. Must be > 0");
     193          11 :     mfn->tol = tol;
     194             :   }
     195          11 :   if (maxits == PETSC_DEFAULT || maxits == PETSC_DECIDE) {
     196           7 :     mfn->max_it = PETSC_DEFAULT;
     197           7 :     mfn->setupcalled = 0;
     198             :   } else {
     199           4 :     PetscCheck(maxits>0,PetscObjectComm((PetscObject)mfn),PETSC_ERR_ARG_OUTOFRANGE,"Illegal value of maxits. Must be > 0");
     200           4 :     mfn->max_it = maxits;
     201             :   }
     202          11 :   PetscFunctionReturn(PETSC_SUCCESS);
     203             : }
     204             : 
     205             : /*@
     206             :    MFNGetDimensions - Gets the dimension of the subspace used by the solver.
     207             : 
     208             :    Not Collective
     209             : 
     210             :    Input Parameter:
     211             : .  mfn - the matrix function context
     212             : 
     213             :    Output Parameter:
     214             : .  ncv - the maximum dimension of the subspace to be used by the solver
     215             : 
     216             :    Level: intermediate
     217             : 
     218             : .seealso: MFNSetDimensions()
     219             : @*/
     220           5 : PetscErrorCode MFNGetDimensions(MFN mfn,PetscInt *ncv)
     221             : {
     222           5 :   PetscFunctionBegin;
     223           5 :   PetscValidHeaderSpecific(mfn,MFN_CLASSID,1);
     224           5 :   PetscAssertPointer(ncv,2);
     225           5 :   *ncv = mfn->ncv;
     226           5 :   PetscFunctionReturn(PETSC_SUCCESS);
     227             : }
     228             : 
     229             : /*@
     230             :    MFNSetDimensions - Sets the dimension of the subspace to be used by the solver.
     231             : 
     232             :    Logically Collective
     233             : 
     234             :    Input Parameters:
     235             : +  mfn - the matrix function context
     236             : -  ncv - the maximum dimension of the subspace to be used by the solver
     237             : 
     238             :    Options Database Keys:
     239             : .  -mfn_ncv <ncv> - Sets the dimension of the subspace
     240             : 
     241             :    Notes:
     242             :    Use PETSC_DEFAULT for ncv to assign a reasonably good value, which is
     243             :    dependent on the solution method.
     244             : 
     245             :    Level: intermediate
     246             : 
     247             : .seealso: MFNGetDimensions()
     248             : @*/
     249           8 : PetscErrorCode MFNSetDimensions(MFN mfn,PetscInt ncv)
     250             : {
     251           8 :   PetscFunctionBegin;
     252           8 :   PetscValidHeaderSpecific(mfn,MFN_CLASSID,1);
     253          32 :   PetscValidLogicalCollectiveInt(mfn,ncv,2);
     254           8 :   if (ncv == PETSC_DECIDE || ncv == PETSC_DEFAULT) {
     255           0 :     mfn->ncv = PETSC_DEFAULT;
     256             :   } else {
     257           8 :     PetscCheck(ncv>0,PetscObjectComm((PetscObject)mfn),PETSC_ERR_ARG_OUTOFRANGE,"Illegal value of ncv. Must be > 0");
     258           8 :     mfn->ncv = ncv;
     259             :   }
     260           8 :   mfn->setupcalled = 0;
     261           8 :   PetscFunctionReturn(PETSC_SUCCESS);
     262             : }
     263             : 
     264             : /*@
     265             :    MFNSetErrorIfNotConverged - Causes MFNSolve() to generate an error if the
     266             :    solver has not converged.
     267             : 
     268             :    Logically Collective
     269             : 
     270             :    Input Parameters:
     271             : +  mfn - the matrix function context
     272             : -  flg - PETSC_TRUE indicates you want the error generated
     273             : 
     274             :    Options Database Keys:
     275             : .  -mfn_error_if_not_converged - this takes an optional truth value (0/1/no/yes/true/false)
     276             : 
     277             :    Level: intermediate
     278             : 
     279             :    Note:
     280             :    Normally SLEPc continues if the solver fails to converge, you can call
     281             :    MFNGetConvergedReason() after a MFNSolve() to determine if it has converged.
     282             : 
     283             : .seealso: MFNGetErrorIfNotConverged()
     284             : @*/
     285          12 : PetscErrorCode MFNSetErrorIfNotConverged(MFN mfn,PetscBool flg)
     286             : {
     287          12 :   PetscFunctionBegin;
     288          12 :   PetscValidHeaderSpecific(mfn,MFN_CLASSID,1);
     289          48 :   PetscValidLogicalCollectiveBool(mfn,flg,2);
     290          12 :   mfn->errorifnotconverged = flg;
     291          12 :   PetscFunctionReturn(PETSC_SUCCESS);
     292             : }
     293             : 
     294             : /*@
     295             :    MFNGetErrorIfNotConverged - Return a flag indicating whether MFNSolve() will
     296             :    generate an error if the solver does not converge.
     297             : 
     298             :    Not Collective
     299             : 
     300             :    Input Parameter:
     301             : .  mfn - the matrix function context
     302             : 
     303             :    Output Parameter:
     304             : .  flag - PETSC_TRUE if it will generate an error, else PETSC_FALSE
     305             : 
     306             :    Level: intermediate
     307             : 
     308             : .seealso: MFNSetErrorIfNotConverged()
     309             : @*/
     310           3 : PetscErrorCode MFNGetErrorIfNotConverged(MFN mfn,PetscBool *flag)
     311             : {
     312           3 :   PetscFunctionBegin;
     313           3 :   PetscValidHeaderSpecific(mfn,MFN_CLASSID,1);
     314           3 :   PetscAssertPointer(flag,2);
     315           3 :   *flag = mfn->errorifnotconverged;
     316           3 :   PetscFunctionReturn(PETSC_SUCCESS);
     317             : }
     318             : 
     319             : /*@C
     320             :    MFNSetOptionsPrefix - Sets the prefix used for searching for all
     321             :    MFN options in the database.
     322             : 
     323             :    Logically Collective
     324             : 
     325             :    Input Parameters:
     326             : +  mfn - the matrix function context
     327             : -  prefix - the prefix string to prepend to all MFN option requests
     328             : 
     329             :    Notes:
     330             :    A hyphen (-) must NOT be given at the beginning of the prefix name.
     331             :    The first character of all runtime options is AUTOMATICALLY the
     332             :    hyphen.
     333             : 
     334             :    For example, to distinguish between the runtime options for two
     335             :    different MFN contexts, one could call
     336             : .vb
     337             :       MFNSetOptionsPrefix(mfn1,"fun1_")
     338             :       MFNSetOptionsPrefix(mfn2,"fun2_")
     339             : .ve
     340             : 
     341             :    Level: advanced
     342             : 
     343             : .seealso: MFNAppendOptionsPrefix(), MFNGetOptionsPrefix()
     344             : @*/
     345           1 : PetscErrorCode MFNSetOptionsPrefix(MFN mfn,const char *prefix)
     346             : {
     347           1 :   PetscFunctionBegin;
     348           1 :   PetscValidHeaderSpecific(mfn,MFN_CLASSID,1);
     349           1 :   if (!mfn->V) PetscCall(MFNGetBV(mfn,&mfn->V));
     350           1 :   PetscCall(BVSetOptionsPrefix(mfn->V,prefix));
     351           1 :   if (!mfn->fn) PetscCall(MFNGetFN(mfn,&mfn->fn));
     352           1 :   PetscCall(FNSetOptionsPrefix(mfn->fn,prefix));
     353           1 :   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mfn,prefix));
     354           1 :   PetscFunctionReturn(PETSC_SUCCESS);
     355             : }
     356             : 
     357             : /*@C
     358             :    MFNAppendOptionsPrefix - Appends to the prefix used for searching for all
     359             :    MFN options in the database.
     360             : 
     361             :    Logically Collective
     362             : 
     363             :    Input Parameters:
     364             : +  mfn - the matrix function context
     365             : -  prefix - the prefix string to prepend to all MFN option requests
     366             : 
     367             :    Notes:
     368             :    A hyphen (-) must NOT be given at the beginning of the prefix name.
     369             :    The first character of all runtime options is AUTOMATICALLY the hyphen.
     370             : 
     371             :    Level: advanced
     372             : 
     373             : .seealso: MFNSetOptionsPrefix(), MFNGetOptionsPrefix()
     374             : @*/
     375           1 : PetscErrorCode MFNAppendOptionsPrefix(MFN mfn,const char *prefix)
     376             : {
     377           1 :   PetscFunctionBegin;
     378           1 :   PetscValidHeaderSpecific(mfn,MFN_CLASSID,1);
     379           1 :   if (!mfn->V) PetscCall(MFNGetBV(mfn,&mfn->V));
     380           1 :   PetscCall(BVAppendOptionsPrefix(mfn->V,prefix));
     381           1 :   if (!mfn->fn) PetscCall(MFNGetFN(mfn,&mfn->fn));
     382           1 :   PetscCall(FNAppendOptionsPrefix(mfn->fn,prefix));
     383           1 :   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)mfn,prefix));
     384           1 :   PetscFunctionReturn(PETSC_SUCCESS);
     385             : }
     386             : 
     387             : /*@C
     388             :    MFNGetOptionsPrefix - Gets the prefix used for searching for all
     389             :    MFN options in the database.
     390             : 
     391             :    Not Collective
     392             : 
     393             :    Input Parameters:
     394             : .  mfn - the matrix function context
     395             : 
     396             :    Output Parameters:
     397             : .  prefix - pointer to the prefix string used is returned
     398             : 
     399             :    Note:
     400             :    On the Fortran side, the user should pass in a string 'prefix' of
     401             :    sufficient length to hold the prefix.
     402             : 
     403             :    Level: advanced
     404             : 
     405             : .seealso: MFNSetOptionsPrefix(), MFNAppendOptionsPrefix()
     406             : @*/
     407           1 : PetscErrorCode MFNGetOptionsPrefix(MFN mfn,const char *prefix[])
     408             : {
     409           1 :   PetscFunctionBegin;
     410           1 :   PetscValidHeaderSpecific(mfn,MFN_CLASSID,1);
     411           1 :   PetscAssertPointer(prefix,2);
     412           1 :   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)mfn,prefix));
     413           1 :   PetscFunctionReturn(PETSC_SUCCESS);
     414             : }

Generated by: LCOV version 1.14