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 PetscErrorCode ierr; 65 PetscBool flg; 66 67 PetscFunctionBegin; 68 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 69 PetscValidPointer(ctx,2); 70 ierr = PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg);CHKERRQ(ierr); 71 if (!flg) *(void**)ctx = NULL; 72 else *(void**)ctx = ((Tao_Shell*)(tao->data))->ctx; 73 PetscFunctionReturn(0); 74 } 75 76 /*@ 77 TaoShellSetContext - sets the context for a shell Tao 78 79 Logically Collective on Tao 80 81 Input Parameters: 82 + tao - the shell Tao 83 - ctx - the context 84 85 Level: advanced 86 87 Fortran Notes: 88 The context can only be an integer or a PetscObject 89 unfortunately it cannot be a Fortran array or derived type. 90 91 .seealso: TaoCreateShell(), TaoShellGetContext() 92 @*/ 93 PetscErrorCode TaoShellSetContext(Tao tao,void *ctx) 94 { 95 Tao_Shell *shell = (Tao_Shell*)tao->data; 96 PetscErrorCode ierr; 97 PetscBool flg; 98 99 PetscFunctionBegin; 100 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 101 ierr = PetscObjectTypeCompare((PetscObject)tao,TAOSHELL,&flg);CHKERRQ(ierr); 102 if (flg) shell->ctx = ctx; 103 PetscFunctionReturn(0); 104 } 105 106 static PetscErrorCode TaoSolve_Shell(Tao tao) 107 { 108 Tao_Shell *shell = (Tao_Shell*)tao->data; 109 PetscErrorCode ierr; 110 111 PetscFunctionBegin; 112 if (!shell->solve) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"Must call TaoShellSetSolve() first"); 113 tao->reason = TAO_CONVERGED_USER; 114 ierr = (*(shell->solve)) (tao);CHKERRQ(ierr); 115 PetscFunctionReturn(0); 116 } 117 118 PetscErrorCode TaoDestroy_Shell(Tao tao) 119 { 120 PetscErrorCode ierr; 121 122 PetscFunctionBegin; 123 ierr = PetscFree(tao->data);CHKERRQ(ierr); 124 PetscFunctionReturn(0); 125 } 126 127 PetscErrorCode TaoSetUp_Shell(Tao tao) 128 { 129 PetscFunctionBegin; 130 PetscFunctionReturn(0); 131 } 132 133 PetscErrorCode TaoSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,Tao tao) 134 { 135 PetscFunctionBegin; 136 PetscFunctionReturn(0); 137 } 138 139 PetscErrorCode TaoView_Shell(Tao tao, PetscViewer viewer) 140 { 141 PetscFunctionBegin; 142 PetscFunctionReturn(0); 143 } 144 145 /*MC 146 TAOSHELL - a user provided nonlinear solver 147 148 Level: advanced 149 150 .seealso: TaoCreate(), Tao, TaoSetType(), TaoType (for list of available types) 151 M*/ 152 PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao) 153 { 154 Tao_Shell *shell; 155 PetscErrorCode ierr; 156 157 PetscFunctionBegin; 158 tao->ops->destroy = TaoDestroy_Shell; 159 tao->ops->setup = TaoSetUp_Shell; 160 tao->ops->setfromoptions = TaoSetFromOptions_Shell; 161 tao->ops->view = TaoView_Shell; 162 tao->ops->solve = TaoSolve_Shell; 163 164 ierr = PetscNewLog(tao,&shell);CHKERRQ(ierr); 165 tao->data = (void*)shell; 166 PetscFunctionReturn(0); 167 } 168 169