1 #include <private/linesearchimpl.h> 2 #include <private/snesimpl.h> 3 4 5 typedef struct { 6 SNESLineSearchUserFunc func; 7 void *ctx; 8 } SNESLineSearch_Shell; 9 10 #undef __FUNCT__ 11 #define __FUNCT__ "SNESLineSearchShellSetUserFunc" 12 /*@C 13 SNESLineSearchShellSetUserFunc - Sets the user function for the SNESLineSearch Shell implementation. 14 15 Not Collective 16 17 Level: advanced 18 19 .keywords: SNESLineSearch, SNESLineSearchShell, Shell 20 21 .seealso: SNESLineSearchShellGetUserFunc() 22 @*/ 23 PetscErrorCode SNESLineSearchShellSetUserFunc(SNESLineSearch linesearch, SNESLineSearchUserFunc func, void *ctx) { 24 25 PetscErrorCode ierr; 26 PetscBool flg; 27 SNESLineSearch_Shell *shell = (SNESLineSearch_Shell *)linesearch->data; 28 PetscFunctionBegin; 29 PetscValidHeaderSpecific(linesearch, SNESLINESEARCH_CLASSID, 1); 30 ierr = PetscTypeCompare((PetscObject)linesearch,SNES_LINESEARCH_SHELL,&flg);CHKERRQ(ierr); 31 if (flg) { 32 shell->ctx = ctx; 33 shell->func = func; 34 } 35 PetscFunctionReturn(0); 36 } 37 38 39 #undef __FUNCT__ 40 #define __FUNCT__ "SNESLineSearchShellGetUserFunc" 41 /*@C 42 SNESLineSearchShellGetUserFunc - Gets the user function and context for the shell implementation. 43 44 Not Collective 45 46 Level: advanced 47 48 .keywords: SNESLineSearch, SNESLineSearchShell, Shell 49 50 .seealso: SNESLineSearchShellSetUserFunc() 51 @*/ 52 PetscErrorCode SNESLineSearchShellGetUserFunc(SNESLineSearch linesearch, SNESLineSearchUserFunc *func, void **ctx) { 53 54 PetscErrorCode ierr; 55 PetscBool flg; 56 SNESLineSearch_Shell *shell = (SNESLineSearch_Shell *)linesearch->data; 57 PetscFunctionBegin; 58 PetscValidHeaderSpecific(linesearch, SNESLINESEARCH_CLASSID, 1); 59 if (func) PetscValidPointer(func,2); 60 if (ctx) PetscValidPointer(ctx,3); 61 ierr = PetscTypeCompare((PetscObject)linesearch,SNES_LINESEARCH_SHELL,&flg);CHKERRQ(ierr); 62 if (flg) { 63 *ctx = shell->ctx; 64 *func = shell->func; 65 } 66 PetscFunctionReturn(0); 67 } 68 69 70 #undef __FUNCT__ 71 #define __FUNCT__ "SNESLineSearchApply_Shell" 72 static PetscErrorCode SNESLineSearchApply_Shell(SNESLineSearch linesearch) 73 { 74 SNESLineSearch_Shell *shell = (SNESLineSearch_Shell *)linesearch->data; 75 PetscErrorCode ierr; 76 77 PetscFunctionBegin; 78 79 /* apply the user function */ 80 if (shell->func) { 81 ierr = (*shell->func)(linesearch, shell->ctx);CHKERRQ(ierr); 82 } else { 83 SETERRQ(((PetscObject)linesearch)->comm, PETSC_ERR_USER, "SNESLineSearchShell needs to have a shell function set with SNESLineSearchShellSetUserFunc"); 84 } 85 PetscFunctionReturn(0); 86 } 87 88 #undef __FUNCT__ 89 #define __FUNCT__ "SNESLineSearchDestroy_Shell" 90 static PetscErrorCode SNESLineSearchDestroy_Shell(SNESLineSearch linesearch) 91 { 92 SNESLineSearch_Shell *shell = (SNESLineSearch_Shell *)linesearch->data; 93 PetscErrorCode ierr; 94 95 PetscFunctionBegin; 96 ierr = PetscFree(shell);CHKERRQ(ierr); 97 PetscFunctionReturn(0); 98 } 99 100 #undef __FUNCT__ 101 #define __FUNCT__ "SNESLineSearchCreate_Shell" 102 /*MC 103 104 SNES_LINESEARCH_SHELL - Provides context for a user-provided line search routine. 105 106 The user routine has one argument, the SNESLineSearch context. The user uses the interface to 107 extract line search parameters and set them accordingly when the computation is finished. 108 109 Any of the other line searches may serve as a guide to how this is to be done. 110 111 Level: advanced 112 113 M*/ 114 PETSC_EXTERN_C PetscErrorCode SNESLineSearchCreate_Shell(SNESLineSearch linesearch) 115 { 116 117 SNESLineSearch_Shell *shell; 118 PetscErrorCode ierr; 119 120 PetscFunctionBegin; 121 122 linesearch->ops->apply = SNESLineSearchApply_Shell; 123 linesearch->ops->destroy = SNESLineSearchDestroy_Shell; 124 linesearch->ops->setfromoptions = PETSC_NULL; 125 linesearch->ops->reset = PETSC_NULL; 126 linesearch->ops->view = PETSC_NULL; 127 linesearch->ops->setup = PETSC_NULL; 128 129 ierr = PetscNewLog(linesearch, SNESLineSearch_Shell, &shell);CHKERRQ(ierr); 130 linesearch->data = (void*) shell; 131 PetscFunctionReturn(0); 132 } 133