Date: 2022-10-01T06:40:33+00:00

Actual source code: nepbasic.c
slepc-3.18.0 2022-10-01
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: */
     10: /*
     11:    Basic NEP routines
     12: */

     14: #include <slepc/private/nepimpl.h>

     16: /* Logging support */
     17: PetscClassId      NEP_CLASSID = 0;
     18: PetscLogEvent     NEP_SetUp = 0,NEP_Solve = 0,NEP_Refine = 0,NEP_FunctionEval = 0,NEP_JacobianEval = 0,NEP_Resolvent = 0,NEP_CISS_SVD = 0;

     20: /* List of registered NEP routines */
     21: PetscFunctionList NEPList = NULL;
     22: PetscBool         NEPRegisterAllCalled = PETSC_FALSE;

     24: /* List of registered NEP monitors */
     25: PetscFunctionList NEPMonitorList              = NULL;
     26: PetscFunctionList NEPMonitorCreateList        = NULL;
     27: PetscFunctionList NEPMonitorDestroyList       = NULL;
     28: PetscBool         NEPMonitorRegisterAllCalled = PETSC_FALSE;

     30: /*@
     31:    NEPCreate - Creates the default NEP context.

     33:    Collective

     35:    Input Parameter:
     36: .  comm - MPI communicator

     38:    Output Parameter:
     39: .  outnep - location to put the NEP context

     41:    Level: beginner

     43: .seealso: NEPSetUp(), NEPSolve(), NEPDestroy(), NEP
     44: @*/
     45: PetscErrorCode NEPCreate(MPI_Comm comm,NEP *outnep)
     46: {
     47:   NEP            nep;

     50:   *outnep = NULL;
     51:   NEPInitializePackage();
     52:   SlepcHeaderCreate(nep,NEP_CLASSID,"NEP","Nonlinear Eigenvalue Problem","NEP",comm,NEPDestroy,NEPView);

     54:   nep->max_it          = PETSC_DEFAULT;
     55:   nep->nev             = 1;
     56:   nep->ncv             = PETSC_DEFAULT;
     57:   nep->mpd             = PETSC_DEFAULT;
     58:   nep->nini            = 0;
     59:   nep->target          = 0.0;
     60:   nep->tol             = PETSC_DEFAULT;
     61:   nep->conv            = NEP_CONV_REL;
     62:   nep->stop            = NEP_STOP_BASIC;
     63:   nep->which           = (NEPWhich)0;
     64:   nep->problem_type    = (NEPProblemType)0;
     65:   nep->refine          = NEP_REFINE_NONE;
     66:   nep->npart           = 1;
     67:   nep->rtol            = PETSC_DEFAULT;
     68:   nep->rits            = PETSC_DEFAULT;
     69:   nep->scheme          = (NEPRefineScheme)0;
     70:   nep->trackall        = PETSC_FALSE;
     71:   nep->twosided        = PETSC_FALSE;

     73:   nep->computefunction = NULL;
     74:   nep->computejacobian = NULL;
     75:   nep->functionctx     = NULL;
     76:   nep->jacobianctx     = NULL;
     77:   nep->converged       = NEPConvergedRelative;
     78:   nep->convergeduser   = NULL;
     79:   nep->convergeddestroy= NULL;
     80:   nep->stopping        = NEPStoppingBasic;
     81:   nep->stoppinguser    = NULL;
     82:   nep->stoppingdestroy = NULL;
     83:   nep->convergedctx    = NULL;
     84:   nep->stoppingctx     = NULL;
     85:   nep->numbermonitors  = 0;

     87:   nep->ds              = NULL;
     88:   nep->V               = NULL;
     89:   nep->W               = NULL;
     90:   nep->rg              = NULL;
     91:   nep->function        = NULL;
     92:   nep->function_pre    = NULL;
     93:   nep->jacobian        = NULL;
     94:   nep->A               = NULL;
     95:   nep->f               = NULL;
     96:   nep->nt              = 0;
     97:   nep->mstr            = UNKNOWN_NONZERO_PATTERN;
     98:   nep->P               = NULL;
     99:   nep->mstrp           = UNKNOWN_NONZERO_PATTERN;
    100:   nep->IS              = NULL;
    101:   nep->eigr            = NULL;
    102:   nep->eigi            = NULL;
    103:   nep->errest          = NULL;
    104:   nep->perm            = NULL;
    105:   nep->nwork           = 0;
    106:   nep->work            = NULL;
    107:   nep->data            = NULL;

    109:   nep->state           = NEP_STATE_INITIAL;
    110:   nep->nconv           = 0;
    111:   nep->its             = 0;
    112:   nep->n               = 0;
    113:   nep->nloc            = 0;
    114:   nep->nrma            = NULL;
    115:   nep->fui             = (NEPUserInterface)0;
    116:   nep->useds           = PETSC_FALSE;
    117:   nep->resolvent       = NULL;
    118:   nep->reason          = NEP_CONVERGED_ITERATING;

    120:   PetscNew(&nep->sc);
    121:   *outnep = nep;
    122:   return 0;
    123: }

    125: /*@C
    126:    NEPSetType - Selects the particular solver to be used in the NEP object.

    128:    Logically Collective on nep

    130:    Input Parameters:
    131: +  nep      - the nonlinear eigensolver context
    132: -  type     - a known method

    134:    Options Database Key:
    135: .  -nep_type <method> - Sets the method; use -help for a list
    136:     of available methods

    138:    Notes:
    139:    See "slepc/include/slepcnep.h" for available methods.

    141:    Normally, it is best to use the NEPSetFromOptions() command and
    142:    then set the NEP type from the options database rather than by using
    143:    this routine.  Using the options database provides the user with
    144:    maximum flexibility in evaluating the different available methods.
    145:    The NEPSetType() routine is provided for those situations where it
    146:    is necessary to set the iterative solver independently of the command
    147:    line or options database.

    149:    Level: intermediate

    151: .seealso: NEPType
    152: @*/
    153: PetscErrorCode NEPSetType(NEP nep,NEPType type)
    154: {
    155:   PetscErrorCode (*r)(NEP);
    156:   PetscBool      match;

    161:   PetscObjectTypeCompare((PetscObject)nep,type,&match);
    162:   if (match) return 0;

    164:   PetscFunctionListFind(NEPList,type,&r);

    167:   PetscTryTypeMethod(nep,destroy);
    168:   PetscMemzero(nep->ops,sizeof(struct _NEPOps));

    170:   nep->state = NEP_STATE_INITIAL;
    171:   PetscObjectChangeTypeName((PetscObject)nep,type);
    172:   (*r)(nep);
    173:   return 0;
    174: }

    176: /*@C
    177:    NEPGetType - Gets the NEP type as a string from the NEP object.

    179:    Not Collective

    181:    Input Parameter:
    182: .  nep - the eigensolver context

    184:    Output Parameter:
    185: .  type - name of NEP method

    187:    Level: intermediate

    189: .seealso: NEPSetType()
    190: @*/
    191: PetscErrorCode NEPGetType(NEP nep,NEPType *type)
    192: {
    195:   *type = ((PetscObject)nep)->type_name;
    196:   return 0;
    197: }

    199: /*@C
    200:    NEPRegister - Adds a method to the nonlinear eigenproblem solver package.

    202:    Not Collective

    204:    Input Parameters:
    205: +  name - name of a new user-defined solver
    206: -  function - routine to create the solver context

    208:    Notes:
    209:    NEPRegister() may be called multiple times to add several user-defined solvers.

    211:    Sample usage:
    212: .vb
    213:     NEPRegister("my_solver",MySolverCreate);
    214: .ve

    216:    Then, your solver can be chosen with the procedural interface via
    217: $     NEPSetType(nep,"my_solver")
    218:    or at runtime via the option
    219: $     -nep_type my_solver

    221:    Level: advanced

    223: .seealso: NEPRegisterAll()
    224: @*/
    225: PetscErrorCode NEPRegister(const char *name,PetscErrorCode (*function)(NEP))
    226: {
    227:   NEPInitializePackage();
    228:   PetscFunctionListAdd(&NEPList,name,function);
    229:   return 0;
    230: }

    232: /*@C
    233:    NEPMonitorRegister - Adds NEP monitor routine.

    235:    Not Collective

    237:    Input Parameters:
    238: +  name    - name of a new monitor routine
    239: .  vtype   - a PetscViewerType for the output
    240: .  format  - a PetscViewerFormat for the output
    241: .  monitor - monitor routine
    242: .  create  - creation routine, or NULL
    243: -  destroy - destruction routine, or NULL

    245:    Notes:
    246:    NEPMonitorRegister() may be called multiple times to add several user-defined monitors.

    248:    Sample usage:
    249: .vb
    250:    NEPMonitorRegister("my_monitor",PETSCVIEWERASCII,PETSC_VIEWER_ASCII_INFO_DETAIL,MyMonitor,NULL,NULL);
    251: .ve

    253:    Then, your monitor can be chosen with the procedural interface via
    254: $      NEPMonitorSetFromOptions(nep,"-nep_monitor_my_monitor","my_monitor",NULL)
    255:    or at runtime via the option
    256: $      -nep_monitor_my_monitor

    258:    Level: advanced

    260: .seealso: NEPMonitorRegisterAll()
    261: @*/
    262: PetscErrorCode NEPMonitorRegister(const char name[],PetscViewerType vtype,PetscViewerFormat format,PetscErrorCode (*monitor)(NEP,PetscInt,PetscInt,PetscScalar*,PetscScalar*,PetscReal*,PetscInt,PetscViewerAndFormat*),PetscErrorCode (*create)(PetscViewer,PetscViewerFormat,void*,PetscViewerAndFormat**),PetscErrorCode (*destroy)(PetscViewerAndFormat**))
    263: {
    264:   char           key[PETSC_MAX_PATH_LEN];

    266:   NEPInitializePackage();
    267:   SlepcMonitorMakeKey_Internal(name,vtype,format,key);
    268:   PetscFunctionListAdd(&NEPMonitorList,key,monitor);
    269:   if (create)  PetscFunctionListAdd(&NEPMonitorCreateList,key,create);
    270:   if (destroy) PetscFunctionListAdd(&NEPMonitorDestroyList,key,destroy);
    271:   return 0;
    272: }

    274: /*
    275:    NEPReset_Problem - Destroys the problem matrices.
    276: */
    277: PetscErrorCode NEPReset_Problem(NEP nep)
    278: {
    279:   PetscInt       i;

    282:   MatDestroy(&nep->function);
    283:   MatDestroy(&nep->function_pre);
    284:   MatDestroy(&nep->jacobian);
    285:   if (nep->fui==NEP_USER_INTERFACE_SPLIT) {
    286:     MatDestroyMatrices(nep->nt,&nep->A);
    287:     for (i=0;i<nep->nt;i++) FNDestroy(&nep->f[i]);
    288:     PetscFree(nep->f);
    289:     PetscFree(nep->nrma);
    290:     if (nep->P) MatDestroyMatrices(nep->nt,&nep->P);
    291:     nep->nt = 0;
    292:   }
    293:   return 0;
    294: }
    295: /*@
    296:    NEPReset - Resets the NEP context to the initial state (prior to setup)
    297:    and destroys any allocated Vecs and Mats.

    299:    Collective on nep

    301:    Input Parameter:
    302: .  nep - eigensolver context obtained from NEPCreate()

    304:    Level: advanced

    306: .seealso: NEPDestroy()
    307: @*/
    308: PetscErrorCode NEPReset(NEP nep)
    309: {
    311:   if (!nep) return 0;
    312:   PetscTryTypeMethod(nep,reset);
    313:   if (nep->refineksp) KSPReset(nep->refineksp);
    314:   NEPReset_Problem(nep);
    315:   BVDestroy(&nep->V);
    316:   BVDestroy(&nep->W);
    317:   VecDestroyVecs(nep->nwork,&nep->work);
    318:   MatDestroy(&nep->resolvent);
    319:   nep->nwork = 0;
    320:   nep->state = NEP_STATE_INITIAL;
    321:   return 0;
    322: }

    324: /*@C
    325:    NEPDestroy - Destroys the NEP context.

    327:    Collective on nep

    329:    Input Parameter:
    330: .  nep - eigensolver context obtained from NEPCreate()

    332:    Level: beginner

    334: .seealso: NEPCreate(), NEPSetUp(), NEPSolve()
    335: @*/
    336: PetscErrorCode NEPDestroy(NEP *nep)
    337: {
    338:   if (!*nep) return 0;
    340:   if (--((PetscObject)(*nep))->refct > 0) { *nep = NULL; return 0; }
    341:   NEPReset(*nep);
    342:   PetscTryTypeMethod(*nep,destroy);
    343:   if ((*nep)->eigr) PetscFree4((*nep)->eigr,(*nep)->eigi,(*nep)->errest,(*nep)->perm);
    344:   RGDestroy(&(*nep)->rg);
    345:   DSDestroy(&(*nep)->ds);
    346:   KSPDestroy(&(*nep)->refineksp);
    347:   PetscSubcommDestroy(&(*nep)->refinesubc);
    348:   PetscFree((*nep)->sc);
    349:   /* just in case the initial vectors have not been used */
    350:   SlepcBasisDestroy_Private(&(*nep)->nini,&(*nep)->IS);
    351:   if ((*nep)->convergeddestroy) (*(*nep)->convergeddestroy)((*nep)->convergedctx);
    352:   NEPMonitorCancel(*nep);
    353:   PetscHeaderDestroy(nep);
    354:   return 0;
    355: }

    357: /*@
    358:    NEPSetBV - Associates a basis vectors object to the nonlinear eigensolver.

    360:    Collective on nep

    362:    Input Parameters:
    363: +  nep - eigensolver context obtained from NEPCreate()
    364: -  bv  - the basis vectors object

    366:    Note:
    367:    Use NEPGetBV() to retrieve the basis vectors context (for example,
    368:    to free it at the end of the computations).

    370:    Level: advanced

    372: .seealso: NEPGetBV()
    373: @*/
    374: PetscErrorCode NEPSetBV(NEP nep,BV bv)
    375: {
    379:   PetscObjectReference((PetscObject)bv);
    380:   BVDestroy(&nep->V);
    381:   nep->V = bv;
    382:   return 0;
    383: }

    385: /*@
    386:    NEPGetBV - Obtain the basis vectors object associated to the nonlinear
    387:    eigensolver object.

    389:    Not Collective

    391:    Input Parameters:
    392: .  nep - eigensolver context obtained from NEPCreate()

    394:    Output Parameter:
    395: .  bv - basis vectors context

    397:    Level: advanced

    399: .seealso: NEPSetBV()
    400: @*/
    401: PetscErrorCode NEPGetBV(NEP nep,BV *bv)
    402: {
    405:   if (!nep->V) {
    406:     BVCreate(PetscObjectComm((PetscObject)nep),&nep->V);
    407:     PetscObjectIncrementTabLevel((PetscObject)nep->V,(PetscObject)nep,0);
    408:     PetscObjectSetOptions((PetscObject)nep->V,((PetscObject)nep)->options);
    409:   }
    410:   *bv = nep->V;
    411:   return 0;
    412: }

    414: /*@
    415:    NEPSetRG - Associates a region object to the nonlinear eigensolver.

    417:    Collective on nep

    419:    Input Parameters:
    420: +  nep - eigensolver context obtained from NEPCreate()
    421: -  rg  - the region object

    423:    Note:
    424:    Use NEPGetRG() to retrieve the region context (for example,
    425:    to free it at the end of the computations).

    427:    Level: advanced

    429: .seealso: NEPGetRG()
    430: @*/
    431: PetscErrorCode NEPSetRG(NEP nep,RG rg)
    432: {
    434:   if (rg) {
    437:   }
    438:   PetscObjectReference((PetscObject)rg);
    439:   RGDestroy(&nep->rg);
    440:   nep->rg = rg;
    441:   return 0;
    442: }

    444: /*@
    445:    NEPGetRG - Obtain the region object associated to the
    446:    nonlinear eigensolver object.

    448:    Not Collective

    450:    Input Parameters:
    451: .  nep - eigensolver context obtained from NEPCreate()

    453:    Output Parameter:
    454: .  rg - region context

    456:    Level: advanced

    458: .seealso: NEPSetRG()
    459: @*/
    460: PetscErrorCode NEPGetRG(NEP nep,RG *rg)
    461: {
    464:   if (!nep->rg) {
    465:     RGCreate(PetscObjectComm((PetscObject)nep),&nep->rg);
    466:     PetscObjectIncrementTabLevel((PetscObject)nep->rg,(PetscObject)nep,0);
    467:     PetscObjectSetOptions((PetscObject)nep->rg,((PetscObject)nep)->options);
    468:   }
    469:   *rg = nep->rg;
    470:   return 0;
    471: }

    473: /*@
    474:    NEPSetDS - Associates a direct solver object to the nonlinear eigensolver.

    476:    Collective on nep

    478:    Input Parameters:
    479: +  nep - eigensolver context obtained from NEPCreate()
    480: -  ds  - the direct solver object

    482:    Note:
    483:    Use NEPGetDS() to retrieve the direct solver context (for example,
    484:    to free it at the end of the computations).

    486:    Level: advanced

    488: .seealso: NEPGetDS()
    489: @*/
    490: PetscErrorCode NEPSetDS(NEP nep,DS ds)
    491: {
    495:   PetscObjectReference((PetscObject)ds);
    496:   DSDestroy(&nep->ds);
    497:   nep->ds = ds;
    498:   return 0;
    499: }

    501: /*@
    502:    NEPGetDS - Obtain the direct solver object associated to the
    503:    nonlinear eigensolver object.

    505:    Not Collective

    507:    Input Parameters:
    508: .  nep - eigensolver context obtained from NEPCreate()

    510:    Output Parameter:
    511: .  ds - direct solver context

    513:    Level: advanced

    515: .seealso: NEPSetDS()
    516: @*/
    517: PetscErrorCode NEPGetDS(NEP nep,DS *ds)
    518: {
    521:   if (!nep->ds) {
    522:     DSCreate(PetscObjectComm((PetscObject)nep),&nep->ds);
    523:     PetscObjectIncrementTabLevel((PetscObject)nep->ds,(PetscObject)nep,0);
    524:     PetscObjectSetOptions((PetscObject)nep->ds,((PetscObject)nep)->options);
    525:   }
    526:   *ds = nep->ds;
    527:   return 0;
    528: }

    530: /*@
    531:    NEPRefineGetKSP - Obtain the ksp object used by the eigensolver
    532:    object in the refinement phase.

    534:    Not Collective

    536:    Input Parameters:
    537: .  nep - eigensolver context obtained from NEPCreate()

    539:    Output Parameter:
    540: .  ksp - ksp context

    542:    Level: advanced

    544: .seealso: NEPSetRefine()
    545: @*/
    546: PetscErrorCode NEPRefineGetKSP(NEP nep,KSP *ksp)
    547: {
    548:   MPI_Comm       comm;

    552:   if (!nep->refineksp) {
    553:     if (nep->npart>1) {
    554:       /* Split in subcomunicators */
    555:       PetscSubcommCreate(PetscObjectComm((PetscObject)nep),&nep->refinesubc);
    556:       PetscSubcommSetNumber(nep->refinesubc,nep->npart);
    557:       PetscSubcommSetType(nep->refinesubc,PETSC_SUBCOMM_CONTIGUOUS);
    558:       PetscSubcommGetChild(nep->refinesubc,&comm);
    559:     } else PetscObjectGetComm((PetscObject)nep,&comm);
    560:     KSPCreate(comm,&nep->refineksp);
    561:     PetscObjectIncrementTabLevel((PetscObject)nep->refineksp,(PetscObject)nep,0);
    562:     PetscObjectSetOptions((PetscObject)nep->refineksp,((PetscObject)nep)->options);
    563:     KSPSetOptionsPrefix(*ksp,((PetscObject)nep)->prefix);
    564:     KSPAppendOptionsPrefix(*ksp,"nep_refine_");
    565:     KSPSetTolerances(nep->refineksp,SlepcDefaultTol(nep->rtol),PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);
    566:   }
    567:   *ksp = nep->refineksp;
    568:   return 0;
    569: }

    571: /*@
    572:    NEPSetTarget - Sets the value of the target.

    574:    Logically Collective on nep

    576:    Input Parameters:
    577: +  nep    - eigensolver context
    578: -  target - the value of the target

    580:    Options Database Key:
    581: .  -nep_target <scalar> - the value of the target

    583:    Notes:
    584:    The target is a scalar value used to determine the portion of the spectrum
    585:    of interest. It is used in combination with NEPSetWhichEigenpairs().

    587:    In the case of complex scalars, a complex value can be provided in the
    588:    command line with [+/-][realnumber][+/-]realnumberi with no spaces, e.g.
    589:    -nep_target 1.0+2.0i

    591:    Level: intermediate

    593: .seealso: NEPGetTarget(), NEPSetWhichEigenpairs()
    594: @*/
    595: PetscErrorCode NEPSetTarget(NEP nep,PetscScalar target)
    596: {
    599:   nep->target = target;
    600:   return 0;
    601: }

    603: /*@
    604:    NEPGetTarget - Gets the value of the target.

    606:    Not Collective

    608:    Input Parameter:
    609: .  nep - eigensolver context

    611:    Output Parameter:
    612: .  target - the value of the target

    614:    Note:
    615:    If the target was not set by the user, then zero is returned.

    617:    Level: intermediate

    619: .seealso: NEPSetTarget()
    620: @*/
    621: PetscErrorCode NEPGetTarget(NEP nep,PetscScalar* target)
    622: {
    625:   *target = nep->target;
    626:   return 0;
    627: }

    629: /*@C
    630:    NEPSetFunction - Sets the function to compute the nonlinear Function T(lambda)
    631:    as well as the location to store the matrix.

    633:    Logically Collective on nep

    635:    Input Parameters:
    636: +  nep - the NEP context
    637: .  A   - Function matrix
    638: .  B   - preconditioner matrix (usually same as A)
    639: .  fun - Function evaluation routine (if NULL then NEP retains any
    640:          previously set value)
    641: -  ctx - [optional] user-defined context for private data for the Function
    642:          evaluation routine (may be NULL) (if NULL then NEP retains any
    643:          previously set value)

    645:    Calling Sequence of fun:
    646: $   fun(NEP nep,PetscScalar lambda,Mat T,Mat P,void *ctx)

    648: +  nep    - the NEP context
    649: .  lambda - the scalar argument where T(.) must be evaluated
    650: .  T      - matrix that will contain T(lambda)
    651: .  P      - (optional) different matrix to build the preconditioner
    652: -  ctx    - (optional) user-defined context, as set by NEPSetFunction()

    654:    Level: beginner

    656: .seealso: NEPGetFunction(), NEPSetJacobian()
    657: @*/
    658: PetscErrorCode NEPSetFunction(NEP nep,Mat A,Mat B,PetscErrorCode (*fun)(NEP,PetscScalar,Mat,Mat,void*),void *ctx)
    659: {

    666:   if (nep->state) NEPReset(nep);
    667:   else if (nep->fui && nep->fui!=NEP_USER_INTERFACE_CALLBACK) NEPReset_Problem(nep);

    669:   if (fun) nep->computefunction = fun;
    670:   if (ctx) nep->functionctx     = ctx;
    671:   if (A) {
    672:     PetscObjectReference((PetscObject)A);
    673:     MatDestroy(&nep->function);
    674:     nep->function = A;
    675:   }
    676:   if (B) {
    677:     PetscObjectReference((PetscObject)B);
    678:     MatDestroy(&nep->function_pre);
    679:     nep->function_pre = B;
    680:   }
    681:   nep->fui   = NEP_USER_INTERFACE_CALLBACK;
    682:   nep->state = NEP_STATE_INITIAL;
    683:   return 0;
    684: }

    686: /*@C
    687:    NEPGetFunction - Returns the Function matrix and optionally the user
    688:    provided context for evaluating the Function.

    690:    Not Collective, but Mat object will be parallel if NEP object is

    692:    Input Parameter:
    693: .  nep - the nonlinear eigensolver context

    695:    Output Parameters:
    696: +  A   - location to stash Function matrix (or NULL)
    697: .  B   - location to stash preconditioner matrix (or NULL)
    698: .  fun - location to put Function function (or NULL)
    699: -  ctx - location to stash Function context (or NULL)

    701:    Level: advanced

    703: .seealso: NEPSetFunction()
    704: @*/
    705: PetscErrorCode NEPGetFunction(NEP nep,Mat *A,Mat *B,PetscErrorCode (**fun)(NEP,PetscScalar,Mat,Mat,void*),void **ctx)
    706: {
    708:   NEPCheckCallback(nep,1);
    709:   if (A)   *A   = nep->function;
    710:   if (B)   *B   = nep->function_pre;
    711:   if (fun) *fun = nep->computefunction;
    712:   if (ctx) *ctx = nep->functionctx;
    713:   return 0;
    714: }

    716: /*@C
    717:    NEPSetJacobian - Sets the function to compute the Jacobian T'(lambda) as well
    718:    as the location to store the matrix.

    720:    Logically Collective on nep

    722:    Input Parameters:
    723: +  nep - the NEP context
    724: .  A   - Jacobian matrix
    725: .  jac - Jacobian evaluation routine (if NULL then NEP retains any
    726:          previously set value)
    727: -  ctx - [optional] user-defined context for private data for the Jacobian
    728:          evaluation routine (may be NULL) (if NULL then NEP retains any
    729:          previously set value)

    731:    Calling Sequence of jac:
    732: $   jac(NEP nep,PetscScalar lambda,Mat J,void *ctx)

    734: +  nep    - the NEP context
    735: .  lambda - the scalar argument where T'(.) must be evaluated
    736: .  J      - matrix that will contain T'(lambda)
    737: -  ctx    - (optional) user-defined context, as set by NEPSetJacobian()

    739:    Level: beginner

    741: .seealso: NEPSetFunction(), NEPGetJacobian()
    742: @*/
    743: PetscErrorCode NEPSetJacobian(NEP nep,Mat A,PetscErrorCode (*jac)(NEP,PetscScalar,Mat,void*),void *ctx)
    744: {

    749:   if (nep->state) NEPReset(nep);
    750:   else if (nep->fui && nep->fui!=NEP_USER_INTERFACE_CALLBACK) NEPReset_Problem(nep);

    752:   if (jac) nep->computejacobian = jac;
    753:   if (ctx) nep->jacobianctx     = ctx;
    754:   if (A) {
    755:     PetscObjectReference((PetscObject)A);
    756:     MatDestroy(&nep->jacobian);
    757:     nep->jacobian = A;
    758:   }
    759:   nep->fui   = NEP_USER_INTERFACE_CALLBACK;
    760:   nep->state = NEP_STATE_INITIAL;
    761:   return 0;
    762: }

    764: /*@C
    765:    NEPGetJacobian - Returns the Jacobian matrix and optionally the user
    766:    provided routine and context for evaluating the Jacobian.

    768:    Not Collective, but Mat object will be parallel if NEP object is

    770:    Input Parameter:
    771: .  nep - the nonlinear eigensolver context

    773:    Output Parameters:
    774: +  A   - location to stash Jacobian matrix (or NULL)
    775: .  jac - location to put Jacobian function (or NULL)
    776: -  ctx - location to stash Jacobian context (or NULL)

    778:    Level: advanced

    780: .seealso: NEPSetJacobian()
    781: @*/
    782: PetscErrorCode NEPGetJacobian(NEP nep,Mat *A,PetscErrorCode (**jac)(NEP,PetscScalar,Mat,void*),void **ctx)
    783: {
    785:   NEPCheckCallback(nep,1);
    786:   if (A)   *A   = nep->jacobian;
    787:   if (jac) *jac = nep->computejacobian;
    788:   if (ctx) *ctx = nep->jacobianctx;
    789:   return 0;
    790: }

    792: /*@
    793:    NEPSetSplitOperator - Sets the operator of the nonlinear eigenvalue problem
    794:    in split form.

    796:    Collective on nep

    798:    Input Parameters:
    799: +  nep - the nonlinear eigensolver context
    800: .  nt  - number of terms in the split form
    801: .  A   - array of matrices
    802: .  f   - array of functions
    803: -  str - structure flag for matrices

    805:    Notes:
    806:    The nonlinear operator is written as T(lambda) = sum_i A_i*f_i(lambda),
    807:    for i=1,...,n. The derivative T'(lambda) can be obtained using the
    808:    derivatives of f_i.

    810:    The structure flag provides information about A_i's nonzero pattern
    811:    (see MatStructure enum). If all matrices have the same pattern, then
    812:    use SAME_NONZERO_PATTERN. If the patterns are different but contained
    813:    in the pattern of the first one, then use SUBSET_NONZERO_PATTERN. If
    814:    patterns are known to be different, use DIFFERENT_NONZERO_PATTERN.
    815:    If set to UNKNOWN_NONZERO_PATTERN, the patterns will be compared to
    816:    determine if they are equal.

    818:    This function must be called before NEPSetUp(). If it is called again
    819:    after NEPSetUp() then the NEP object is reset.

    821:    Level: beginner

    823: .seealso: NEPGetSplitOperatorTerm(), NEPGetSplitOperatorInfo(), NEPSetSplitPreconditioner()
    824: @*/
    825: PetscErrorCode NEPSetSplitOperator(NEP nep,PetscInt nt,Mat A[],FN f[],MatStructure str)
    826: {
    827:   PetscInt       i,n=0,m,m0=0,mloc,nloc,mloc0=0;

    836:   for (i=0;i<nt;i++) {
    841:     MatGetSize(A[i],&m,&n);
    842:     MatGetLocalSize(A[i],&mloc,&nloc);
    845:     if (!i) { m0 = m; mloc0 = mloc; }
    848:     PetscObjectReference((PetscObject)A[i]);
    849:     PetscObjectReference((PetscObject)f[i]);
    850:   }

    852:   if (nep->state && (n!=nep->n || nloc!=nep->nloc)) NEPReset(nep);
    853:   else NEPReset_Problem(nep);

    855:   /* allocate space and copy matrices and functions */
    856:   PetscMalloc1(nt,&nep->A);
    857:   for (i=0;i<nt;i++) nep->A[i] = A[i];
    858:   PetscMalloc1(nt,&nep->f);
    859:   for (i=0;i<nt;i++) nep->f[i] = f[i];
    860:   PetscCalloc1(nt,&nep->nrma);
    861:   nep->nt    = nt;
    862:   nep->mstr  = str;
    863:   nep->fui   = NEP_USER_INTERFACE_SPLIT;
    864:   nep->state = NEP_STATE_INITIAL;
    865:   return 0;
    866: }

    868: /*@
    869:    NEPGetSplitOperatorTerm - Gets the matrices and functions associated with
    870:    the nonlinear operator in split form.

    872:    Not collective, though parallel Mats and FNs are returned if the NEP is parallel

    874:    Input Parameters:
    875: +  nep - the nonlinear eigensolver context
    876: -  k   - the index of the requested term (starting in 0)

    878:    Output Parameters:
    879: +  A - the matrix of the requested term
    880: -  f - the function of the requested term

    882:    Level: intermediate

    884: .seealso: NEPSetSplitOperator(), NEPGetSplitOperatorInfo()
    885: @*/
    886: PetscErrorCode NEPGetSplitOperatorTerm(NEP nep,PetscInt k,Mat *A,FN *f)
    887: {
    890:   NEPCheckSplit(nep,1);
    892:   if (A) *A = nep->A[k];
    893:   if (f) *f = nep->f[k];
    894:   return 0;
    895: }

    897: /*@
    898:    NEPGetSplitOperatorInfo - Returns the number of terms of the split form of
    899:    the nonlinear operator, as well as the structure flag for matrices.

    901:    Not collective

    903:    Input Parameter:
    904: .  nep - the nonlinear eigensolver context

    906:    Output Parameters:
    907: +  n   - the number of terms passed in NEPSetSplitOperator()
    908: -  str - the matrix structure flag passed in NEPSetSplitOperator()

    910:    Level: intermediate

    912: .seealso: NEPSetSplitOperator(), NEPGetSplitOperatorTerm()
    913: @*/
    914: PetscErrorCode NEPGetSplitOperatorInfo(NEP nep,PetscInt *n,MatStructure *str)
    915: {
    917:   NEPCheckSplit(nep,1);
    918:   if (n)   *n = nep->nt;
    919:   if (str) *str = nep->mstr;
    920:   return 0;
    921: }

    923: /*@
    924:    NEPSetSplitPreconditioner - Sets an operator in split form from which
    925:    to build the preconditioner to be used when solving the nonlinear
    926:    eigenvalue problem in split form.

    928:    Collective on nep

    930:    Input Parameters:
    931: +  nep  - the nonlinear eigensolver context
    932: .  ntp  - number of terms in the split preconditioner
    933: .  P    - array of matrices
    934: -  strp - structure flag for matrices

    936:    Notes:
    937:    The matrix for the preconditioner is expressed as P(lambda) =
    938:    sum_i P_i*f_i(lambda), for i=1,...,n, where the f_i functions
    939:    are the same as in NEPSetSplitOperator(). It is not necessary to call
    940:    this function. If it is not invoked, then the preconditioner is
    941:    built from T(lambda), i.e., both matrices and functions passed in
    942:    NEPSetSplitOperator().

    944:    The structure flag provides information about P_i's nonzero pattern
    945:    in the same way as in NEPSetSplitOperator().

    947:    If the functions defining the preconditioner operator were different
    948:    from the ones given in NEPSetSplitOperator(), then the split form
    949:    cannot be used. Use the callback interface instead.

    951:    Use ntp=0 to reset a previously set split preconditioner.

    953:    Level: advanced

    955: .seealso: NEPGetSplitPreconditionerTerm(), NEPGetSplitPreconditionerInfo(), NEPSetSplitOperator()
    956: @*/
    957: PetscErrorCode NEPSetSplitPreconditioner(NEP nep,PetscInt ntp,Mat P[],MatStructure strp)
    958: {
    959:   PetscInt       i,n=0,m,m0=0,mloc,nloc,mloc0=0;

    969:   for (i=0;i<ntp;i++) {
    972:     MatGetSize(P[i],&m,&n);
    973:     MatGetLocalSize(P[i],&mloc,&nloc);
    976:     if (!i) { m0 = m; mloc0 = mloc; }
    979:     PetscObjectReference((PetscObject)P[i]);
    980:   }

    983:   if (nep->P) MatDestroyMatrices(nep->nt,&nep->P);

    985:   /* allocate space and copy matrices */
    986:   if (ntp) {
    987:     PetscMalloc1(ntp,&nep->P);
    988:     for (i=0;i<ntp;i++) nep->P[i] = P[i];
    989:   }
    990:   nep->mstrp = strp;
    991:   nep->state = NEP_STATE_INITIAL;
    992:   return 0;
    993: }

    995: /*@
    996:    NEPGetSplitPreconditionerTerm - Gets the matrices associated with
    997:    the split preconditioner.

    999:    Not collective, though parallel Mats are returned if the NEP is parallel

    1001:    Input Parameters:
    1002: +  nep - the nonlinear eigensolver context
    1003: -  k   - the index of the requested term (starting in 0)

    1005:    Output Parameter:
    1006: .  P  - the matrix of the requested term

    1008:    Level: advanced

    1010: .seealso: NEPSetSplitPreconditioner(), NEPGetSplitPreconditionerInfo()
    1011: @*/
    1012: PetscErrorCode NEPGetSplitPreconditionerTerm(NEP nep,PetscInt k,Mat *P)
    1013: {
    1017:   NEPCheckSplit(nep,1);
    1020:   *P = nep->P[k];
    1021:   return 0;
    1022: }

    1024: /*@
    1025:    NEPGetSplitPreconditionerInfo - Returns the number of terms of the split
    1026:    preconditioner, as well as the structure flag for matrices.

    1028:    Not collective

    1030:    Input Parameter:
    1031: .  nep - the nonlinear eigensolver context

    1033:    Output Parameters:
    1034: +  n    - the number of terms passed in NEPSetSplitPreconditioner()
    1035: -  strp - the matrix structure flag passed in NEPSetSplitPreconditioner()

    1037:    Level: advanced

    1039: .seealso: NEPSetSplitPreconditioner(), NEPGetSplitPreconditionerTerm()
    1040: @*/
    1041: PetscErrorCode NEPGetSplitPreconditionerInfo(NEP nep,PetscInt *n,MatStructure *strp)
    1042: {
    1044:   NEPCheckSplit(nep,1);
    1045:   if (n)    *n    = nep->P? nep->nt: 0;
    1046:   if (strp) *strp = nep->mstrp;
    1047:   return 0;
    1048: }