1 #include <petsc-private/snesimpl.h> 2 3 #undef __FUNCT__ 4 #define __FUNCT__ "SNESSetObjective" 5 /*@C 6 SNESSetObjective - Sets the objective function minimized by 7 the SNES methods. 8 9 Logically Collective on SNES 10 11 Input Parameters: 12 + snes - the SNES context 13 . func - objective evaluation routine 14 - ctx - [optional] user-defined context for private data for the 15 function evaluation routine (may be PETSC_NULL) 16 17 Calling sequence of func: 18 $ func (SNES snes,Vec x,PetscReal *obj,void *ctx); 19 20 + snes - the SNES context 21 . X - solution 22 . F - current function/gradient 23 . obj - real to hold the objective value 24 - ctx - optional user-defined objective context 25 26 Level: beginner 27 28 .keywords: SNES, nonlinear, set, objective 29 30 .seealso: SNESGetObjective(), SNESComputeObjective(), SNESSetFunction(), SNESSetJacobian() 31 @*/ 32 PetscErrorCode SNESSetObjective(SNES snes,SNESObjective func,void *ctx) 33 { 34 PetscErrorCode ierr; 35 DM dm; 36 37 PetscFunctionBegin; 38 PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 39 ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 40 ierr = DMSNESSetObjective(dm,func,ctx);CHKERRQ(ierr); 41 PetscFunctionReturn(0); 42 } 43 44 #undef __FUNCT__ 45 #define __FUNCT__ "SNESGetObjective" 46 /*@C 47 SNESGetObjective - Returns the objective function. 48 49 Not Collective 50 51 Input Parameter: 52 . snes - the SNES context 53 54 Output Parameter: 55 + func - the function (or PETSC_NULL) 56 - ctx - the function context (or PETSC_NULL) 57 58 Level: advanced 59 60 .keywords: SNES, nonlinear, get, objective 61 62 .seealso: SNESSetObjective(), SNESGetSolution() 63 @*/ 64 PetscErrorCode SNESGetObjective(SNES snes,SNESObjective *func,void **ctx) 65 { 66 PetscErrorCode ierr; 67 DM dm; 68 PetscFunctionBegin; 69 PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 70 ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 71 ierr = DMSNESGetObjective(dm,func,ctx);CHKERRQ(ierr); 72 PetscFunctionReturn(0); 73 } 74 75 #undef __FUNCT__ 76 #define __FUNCT__ "SNESComputeObjective" 77 /*@C 78 SNESComputeObjective - Computes the objective. 79 80 Collective on SNES 81 82 Input Parameter: 83 + snes - the SNES context 84 - X - the state vector 85 86 Output Parameter: 87 . ob - the objective value 88 89 Level: advanced 90 91 .keywords: SNES, nonlinear, compute, objective 92 93 .seealso: SNESSetObjective(), SNESGetSolution() 94 @*/ 95 PetscErrorCode SNESComputeObjective(SNES snes,Vec X,PetscReal *ob) 96 { 97 PetscErrorCode ierr; 98 DM dm; 99 SNESDM sdm; 100 PetscFunctionBegin; 101 PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 102 PetscValidHeaderSpecific(X,VEC_CLASSID,2); 103 PetscValidPointer(ob,3); 104 ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 105 ierr = DMSNESGetContext(dm,&sdm);CHKERRQ(ierr); 106 if (sdm->computeobjective) { 107 ierr = (sdm->computeobjective)(snes,X,ob,sdm->objectivectx);CHKERRQ(ierr); 108 } else { 109 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetObjective() before SNESComputeObjective()."); 110 } 111 PetscFunctionReturn(0); 112 } 113