183a0a5c3SToby Isaac #include <petsc/private/taoimpl.h> /*I "petsctao.h" I*/ 283a0a5c3SToby Isaac 383a0a5c3SToby Isaac typedef struct _n_TaoShell Tao_Shell; 483a0a5c3SToby Isaac 59371c9d4SSatish Balay struct _n_TaoShell { 683a0a5c3SToby Isaac PetscErrorCode (*solve)(Tao); 783a0a5c3SToby Isaac void *ctx; 883a0a5c3SToby Isaac }; 983a0a5c3SToby Isaac 1083a0a5c3SToby Isaac /*@C 1183a0a5c3SToby Isaac TaoShellSetSolve - Sets routine to apply as solver 1283a0a5c3SToby Isaac 1383a0a5c3SToby Isaac Logically Collective on Tao 1483a0a5c3SToby Isaac 1583a0a5c3SToby Isaac Input Parameters: 1683a0a5c3SToby Isaac + tao - the nonlinear solver context 1783a0a5c3SToby Isaac - solve - the application-provided solver routine 1883a0a5c3SToby Isaac 1983a0a5c3SToby Isaac Calling sequence of solve: 2083a0a5c3SToby Isaac .vb 2183a0a5c3SToby Isaac PetscErrorCode solve (Tao tao) 2283a0a5c3SToby Isaac .ve 2383a0a5c3SToby Isaac 2483a0a5c3SToby Isaac . tao - the optimizer, get the application context with TaoShellGetContext() 2583a0a5c3SToby Isaac 2683a0a5c3SToby Isaac Notes: 2783a0a5c3SToby Isaac the function MUST return an error code of 0 on success and nonzero on failure. 2883a0a5c3SToby Isaac 2983a0a5c3SToby Isaac Level: advanced 3083a0a5c3SToby Isaac 31db781477SPatrick Sanan .seealso: `TAOSHELL`, `TaoShellSetContext()`, `TaoShellGetContext()` 3283a0a5c3SToby Isaac @*/ 339371c9d4SSatish Balay PetscErrorCode TaoShellSetSolve(Tao tao, PetscErrorCode (*solve)(Tao)) { 3483a0a5c3SToby Isaac Tao_Shell *shell = (Tao_Shell *)tao->data; 3583a0a5c3SToby Isaac 3683a0a5c3SToby Isaac PetscFunctionBegin; 3783a0a5c3SToby Isaac PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 3883a0a5c3SToby Isaac shell->solve = solve; 3983a0a5c3SToby Isaac PetscFunctionReturn(0); 4083a0a5c3SToby Isaac } 4183a0a5c3SToby Isaac 4283a0a5c3SToby Isaac /*@ 4383a0a5c3SToby Isaac TaoShellGetContext - Returns the user-provided context associated with a shell Tao 4483a0a5c3SToby Isaac 4583a0a5c3SToby Isaac Not Collective 4683a0a5c3SToby Isaac 4783a0a5c3SToby Isaac Input Parameter: 4883a0a5c3SToby Isaac . tao - should have been created with TaoSetType(tao,TAOSHELL); 4983a0a5c3SToby Isaac 5083a0a5c3SToby Isaac Output Parameter: 5183a0a5c3SToby Isaac . ctx - the user provided context 5283a0a5c3SToby Isaac 5383a0a5c3SToby Isaac Level: advanced 5483a0a5c3SToby Isaac 5583a0a5c3SToby Isaac Notes: 5683a0a5c3SToby Isaac This routine is intended for use within various shell routines 5783a0a5c3SToby Isaac 58db781477SPatrick Sanan .seealso: `TaoCreateShell()`, `TaoShellSetContext()` 5983a0a5c3SToby Isaac @*/ 609371c9d4SSatish Balay PetscErrorCode TaoShellGetContext(Tao tao, void *ctx) { 6183a0a5c3SToby Isaac PetscBool flg; 6283a0a5c3SToby Isaac 6383a0a5c3SToby Isaac PetscFunctionBegin; 6483a0a5c3SToby Isaac PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 6583a0a5c3SToby Isaac PetscValidPointer(ctx, 2); 669566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)tao, TAOSHELL, &flg)); 673ec1f749SStefano Zampini if (!flg) *(void **)ctx = NULL; 683ec1f749SStefano Zampini else *(void **)ctx = ((Tao_Shell *)(tao->data))->ctx; 6983a0a5c3SToby Isaac PetscFunctionReturn(0); 7083a0a5c3SToby Isaac } 7183a0a5c3SToby Isaac 7283a0a5c3SToby Isaac /*@ 7383a0a5c3SToby Isaac TaoShellSetContext - sets the context for a shell Tao 7483a0a5c3SToby Isaac 7583a0a5c3SToby Isaac Logically Collective on Tao 7683a0a5c3SToby Isaac 7783a0a5c3SToby Isaac Input Parameters: 7883a0a5c3SToby Isaac + tao - the shell Tao 7983a0a5c3SToby Isaac - ctx - the context 8083a0a5c3SToby Isaac 8183a0a5c3SToby Isaac Level: advanced 8283a0a5c3SToby Isaac 8383a0a5c3SToby Isaac Fortran Notes: 8483a0a5c3SToby Isaac The context can only be an integer or a PetscObject 8583a0a5c3SToby Isaac unfortunately it cannot be a Fortran array or derived type. 8683a0a5c3SToby Isaac 87db781477SPatrick Sanan .seealso: `TaoCreateShell()`, `TaoShellGetContext()` 8883a0a5c3SToby Isaac @*/ 899371c9d4SSatish Balay PetscErrorCode TaoShellSetContext(Tao tao, void *ctx) { 9083a0a5c3SToby Isaac Tao_Shell *shell = (Tao_Shell *)tao->data; 9183a0a5c3SToby Isaac PetscBool flg; 9283a0a5c3SToby Isaac 9383a0a5c3SToby Isaac PetscFunctionBegin; 9483a0a5c3SToby Isaac PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 959566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)tao, TAOSHELL, &flg)); 9683a0a5c3SToby Isaac if (flg) shell->ctx = ctx; 9783a0a5c3SToby Isaac PetscFunctionReturn(0); 9883a0a5c3SToby Isaac } 9983a0a5c3SToby Isaac 1009371c9d4SSatish Balay static PetscErrorCode TaoSolve_Shell(Tao tao) { 10183a0a5c3SToby Isaac Tao_Shell *shell = (Tao_Shell *)tao->data; 10283a0a5c3SToby Isaac 10383a0a5c3SToby Isaac PetscFunctionBegin; 1043c859ba3SBarry Smith PetscCheck(shell->solve, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "Must call TaoShellSetSolve() first"); 10583a0a5c3SToby Isaac tao->reason = TAO_CONVERGED_USER; 1069566063dSJacob Faibussowitsch PetscCall((*(shell->solve))(tao)); 10783a0a5c3SToby Isaac PetscFunctionReturn(0); 10883a0a5c3SToby Isaac } 10983a0a5c3SToby Isaac 1109371c9d4SSatish Balay PetscErrorCode TaoDestroy_Shell(Tao tao) { 11183a0a5c3SToby Isaac PetscFunctionBegin; 1129566063dSJacob Faibussowitsch PetscCall(PetscFree(tao->data)); 11383a0a5c3SToby Isaac PetscFunctionReturn(0); 11483a0a5c3SToby Isaac } 11583a0a5c3SToby Isaac 1169371c9d4SSatish Balay PetscErrorCode TaoSetUp_Shell(Tao tao) { 11783a0a5c3SToby Isaac PetscFunctionBegin; 11883a0a5c3SToby Isaac PetscFunctionReturn(0); 11983a0a5c3SToby Isaac } 12083a0a5c3SToby Isaac 1219371c9d4SSatish Balay PetscErrorCode TaoSetFromOptions_Shell(Tao tao, PetscOptionItems *PetscOptionsObject) { 12283a0a5c3SToby Isaac PetscFunctionBegin; 12383a0a5c3SToby Isaac PetscFunctionReturn(0); 12483a0a5c3SToby Isaac } 12583a0a5c3SToby Isaac 1269371c9d4SSatish Balay PetscErrorCode TaoView_Shell(Tao tao, PetscViewer viewer) { 12783a0a5c3SToby Isaac PetscFunctionBegin; 12883a0a5c3SToby Isaac PetscFunctionReturn(0); 12983a0a5c3SToby Isaac } 13083a0a5c3SToby Isaac 13183a0a5c3SToby Isaac /*MC 13283a0a5c3SToby Isaac TAOSHELL - a user provided nonlinear solver 13383a0a5c3SToby Isaac 13483a0a5c3SToby Isaac Level: advanced 13583a0a5c3SToby Isaac 136db781477SPatrick Sanan .seealso: `TaoCreate()`, `Tao`, `TaoSetType()`, `TaoType` 13783a0a5c3SToby Isaac M*/ 1389371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode TaoCreate_Shell(Tao tao) { 13983a0a5c3SToby Isaac Tao_Shell *shell; 14083a0a5c3SToby Isaac 14183a0a5c3SToby Isaac PetscFunctionBegin; 14283a0a5c3SToby Isaac tao->ops->destroy = TaoDestroy_Shell; 14383a0a5c3SToby Isaac tao->ops->setup = TaoSetUp_Shell; 14483a0a5c3SToby Isaac tao->ops->setfromoptions = TaoSetFromOptions_Shell; 14583a0a5c3SToby Isaac tao->ops->view = TaoView_Shell; 14683a0a5c3SToby Isaac tao->ops->solve = TaoSolve_Shell; 14783a0a5c3SToby Isaac 148*4dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&shell)); 14983a0a5c3SToby Isaac tao->data = (void *)shell; 15083a0a5c3SToby Isaac PetscFunctionReturn(0); 15183a0a5c3SToby Isaac } 152