Date: 2022-03-04T17:39:23+00:00

Actual source code: snesshell.c

      1: #include <petsc/private/snesimpl.h>

      3: typedef struct {PetscErrorCode (*solve)(SNES,Vec);void *ctx;} SNES_Shell;

      5: /*@C
      6:    SNESShellSetSolve - Sets routine to apply as solver

      8:    Logically Collective on SNES

     10:    Input Parameters:
     11: +  snes - the nonlinear solver context
     12: -  apply - the application-provided solver routine

     14:    Calling sequence of solve:
     15: .vb
     16:    PetscErrorCode apply (SNES snes,Vec xout)
     17: .ve

     19: +  snes - the preconditioner, get the application context with SNESShellGetContext()
     20: -  xout - solution vector

     22:    Notes:
     23:     the function MUST return an error code of 0 on success and nonzero on failure.

     25:    Level: advanced

     27: .seealso: SNESSHELL, SNESShellSetContext(), SNESShellGetContext()
     28: @*/
     29: PetscErrorCode  SNESShellSetSolve(SNES snes,PetscErrorCode (*solve)(SNES,Vec))
     30: {

     35:   PetscTryMethod(snes,"SNESShellSetSolve_C",(SNES,PetscErrorCode (*)(SNES,Vec)),(snes,solve));
     36:   return(0);
     37: }

     39: PetscErrorCode SNESReset_Shell(SNES snes)
     40: {
     42:   return(0);
     43: }

     45: PetscErrorCode SNESDestroy_Shell(SNES snes)
     46: {

     50:   SNESReset_Shell(snes);
     51:   PetscFree(snes->data);
     52:   return(0);
     53: }

     55: PetscErrorCode SNESSetUp_Shell(SNES snes)
     56: {
     58:   return(0);
     59: }

     61: PetscErrorCode SNESSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,SNES snes)
     62: {

     66:   PetscOptionsHead(PetscOptionsObject,"SNES Shell options");
     67:   return(0);
     68: }

     70: PetscErrorCode SNESView_Shell(SNES snes, PetscViewer viewer)
     71: {
     73:   return(0);
     74: }

     76: /*@
     77:     SNESShellGetContext - Returns the user-provided context associated with a shell SNES

     79:     Not Collective

     81:     Input Parameter:
     82: .   snes - should have been created with SNESSetType(snes,SNESSHELL);

     84:     Output Parameter:
     85: .   ctx - the user provided context

     87:     Level: advanced

     89:     Notes:
     90:     This routine is intended for use within various shell routines

     92: .seealso: SNESCreateShell(), SNESShellSetContext()
     93: @*/
     94: PetscErrorCode  SNESShellGetContext(SNES snes,void *ctx)
     95: {
     97:   PetscBool      flg;

    102:   PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg);
    103:   if (!flg) *(void**)ctx = NULL;
    104:   else      *(void**)ctx = ((SNES_Shell*)(snes->data))->ctx;
    105:   return(0);
    106: }

    108: /*@
    109:     SNESShellSetContext - sets the context for a shell SNES

    111:    Logically Collective on SNES

    113:     Input Parameters:
    114: +   snes - the shell SNES
    115: -   ctx - the context

    117:    Level: advanced

    119:    Fortran Notes:
    120:     The context can only be an integer or a PetscObject
    121:       unfortunately it cannot be a Fortran array or derived type.

    123: .seealso: SNESCreateShell(), SNESShellGetContext()
    124: @*/
    125: PetscErrorCode  SNESShellSetContext(SNES snes,void *ctx)
    126: {
    127:   SNES_Shell     *shell = (SNES_Shell*)snes->data;
    129:   PetscBool      flg;

    133:   PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg);
    134:   if (flg) shell->ctx = ctx;
    135:   return(0);
    136: }

    138: PetscErrorCode SNESSolve_Shell(SNES snes)
    139: {
    140:   SNES_Shell     *shell = (SNES_Shell*) snes->data;

    144:   if (!shell->solve) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Must call SNESShellSetSolve() first");
    145:   snes->reason = SNES_CONVERGED_ITS;
    146:   (*shell->solve)(snes,snes->vec_sol);
    147:   return(0);
    148: }

    150: PetscErrorCode  SNESShellSetSolve_Shell(SNES snes,PetscErrorCode (*solve)(SNES,Vec))
    151: {
    152:   SNES_Shell *shell = (SNES_Shell*)snes->data;

    155:   shell->solve = solve;
    156:   return(0);
    157: }

    159: /*MC
    160:   SNESSHELL - a user provided nonlinear solver

    162:    Level: advanced

    164: .seealso: SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types)
    165: M*/

    167: PETSC_EXTERN PetscErrorCode SNESCreate_Shell(SNES snes)
    168: {
    169:   SNES_Shell     *shell;

    173:   snes->ops->destroy        = SNESDestroy_Shell;
    174:   snes->ops->setup          = SNESSetUp_Shell;
    175:   snes->ops->setfromoptions = SNESSetFromOptions_Shell;
    176:   snes->ops->view           = SNESView_Shell;
    177:   snes->ops->solve          = SNESSolve_Shell;
    178:   snes->ops->reset          = SNESReset_Shell;

    180:   snes->usesksp = PETSC_FALSE;
    181:   snes->usesnpc = PETSC_FALSE;

    183:   snes->alwayscomputesfinalresidual = PETSC_FALSE;

    185:   PetscNewLog(snes,&shell);
    186:   snes->data = (void*) shell;
    187:   PetscObjectComposeFunction((PetscObject)snes,"SNESShellSetSolve_C",SNESShellSetSolve_Shell);
    188:   return(0);
    189: }