1 #include <petsc/private/taoimpl.h> /*I "petsctao.h" I*/ 2 3 typedef struct _n_TaoShell Tao_Shell; 4 5 struct _n_TaoShell 6 { 7 PetscErrorCode (*solve)(Tao); 8 void *ctx; 9 }; 10 11 /*@C 12 TaoShellSetSolve - Sets routine to apply as solver 13 14 Logically Collective on Tao 15 16 Input Parameters: 17 + tao - the nonlinear solver context 18 - solve - the application-provided solver routine 19 20 Calling sequence of solve: 21 .vb 22 PetscErrorCode solve (Tao tao) 23 .ve 24 25 . tao - the optimizer, get the application context with TaoShellGetContext() 26 27 Notes: 28 the function MUST return an error code of 0 on success and nonzero on failure. 29 30 Level: advanced 31 32 .seealso: `TAOSHELL`, `TaoShellSetContext()`, `TaoShellGetContext()` 33 @*/ 34 PetscErrorCode TaoShellSetSolve(Tao tao, PetscErrorCode (*solve) (Tao)) 35 { 36 Tao_Shell *shell = (Tao_Shell*)tao->data; 37 38 PetscFunctionBegin; 39 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 40 shell->solve = solve; 41 PetscFunctionReturn(0); 42 } 43 44 /*@ 45 TaoShellGetContext - Returns the user-provided context associated with a shell Tao 46 47 Not Collective 48 49 Input Parameter: 50 . tao - should have been created with TaoSetType(tao,TAOSHELL); 51 52 Output Parameter: 53 . ctx - the user provided context 54 55 Level: advanced 56 57 Notes: 58 This routine is intended for use within various shell routines 59 60 .seealso: `TaoCreateShell()`, `TaoShellSetContext()` 61 @*/ 62 PetscErrorCode TaoShellGetContext(Tao tao,void *ctx) 63 { 64 PetscBool flg; 65 66 PetscFunctionBegin; 67 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 68 PetscValidPointer(ctx,2); 69 PetscCall(PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg)); 70 if (!flg) *(void**)ctx = NULL; 71 else *(void**)ctx = ((Tao_Shell*)(tao->data))->ctx; 72 PetscFunctionReturn(0); 73 } 74 75 /*@ 76 TaoShellSetContext - sets the context for a shell Tao 77 78 Logically Collective on Tao 79 80 Input Parameters: 81 + tao - the shell Tao 82 - ctx - the context 83 84 Level: advanced 85 86 Fortran Notes: 87 The context can only be an integer or a PetscObject 88 unfortunately it cannot be a Fortran array or derived type. 89 90 .seealso: `TaoCreateShell()`, `TaoShellGetContext()` 91 @*/ 92 PetscErrorCode TaoShellSetContext(Tao tao,void *ctx) 93 { 94 Tao_Shell *shell = (Tao_Shell*)tao->data; 95 PetscBool flg; 96 97 PetscFunctionBegin; 98 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 99 PetscCall(PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg)); 100 if (flg) shell->ctx = ctx; 101 PetscFunctionReturn(0); 102 } 103 104 static PetscErrorCode TaoSolve_Shell(Tao tao) 105 { 106 Tao_Shell *shell = (Tao_Shell*)tao->data; 107 108 PetscFunctionBegin; 109 PetscCheck(shell->solve,PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"Must call TaoShellSetSolve() first"); 110 tao->reason = TAO_CONVERGED_USER; 111 PetscCall((*(shell->solve)) (tao)); 112 PetscFunctionReturn(0); 113 } 114 115 PetscErrorCode TaoDestroy_Shell(Tao tao) 116 { 117 PetscFunctionBegin; 118 PetscCall(PetscFree(tao->data)); 119 PetscFunctionReturn(0); 120 } 121 122 PetscErrorCode TaoSetUp_Shell(Tao tao) 123 { 124 PetscFunctionBegin; 125 PetscFunctionReturn(0); 126 } 127 128 PetscErrorCode TaoSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,Tao tao) 129 { 130 PetscFunctionBegin; 131 PetscFunctionReturn(0); 132 } 133 134 PetscErrorCode TaoView_Shell(Tao tao, PetscViewer viewer) 135 { 136 PetscFunctionBegin; 137 PetscFunctionReturn(0); 138 } 139 140 /*MC 141 TAOSHELL - a user provided nonlinear solver 142 143 Level: advanced 144 145 .seealso: `TaoCreate()`, `Tao`, `TaoSetType()`, `TaoType` 146 M*/ 147 PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao) 148 { 149 Tao_Shell *shell; 150 151 PetscFunctionBegin; 152 tao->ops->destroy = TaoDestroy_Shell; 153 tao->ops->setup = TaoSetUp_Shell; 154 tao->ops->setfromoptions = TaoSetFromOptions_Shell; 155 tao->ops->view = TaoView_Shell; 156 tao->ops->solve = TaoSolve_Shell; 157 158 PetscCall(PetscNewLog(tao,&shell)); 159 tao->data = (void*)shell; 160 PetscFunctionReturn(0); 161 } 162