1af0996ceSBarry Smith #include <petsc/private/snesimpl.h> 22a4ee8f2SPeter Brune 3075cc632SBarry Smith /*MC 4075cc632SBarry Smith SNESObjectiveFunction - functional form used to convey the objective function to the nonlinear solver 52a4ee8f2SPeter Brune 6075cc632SBarry Smith Synopsis: 7aaa7dc30SBarry Smith #include <petscsnes.h> 8075cc632SBarry Smith SNESObjectiveFunction(SNES snes,Vec x,PetscReal *obj,void *ctx); 92a4ee8f2SPeter Brune 102a4ee8f2SPeter Brune Input Parameters: 112a4ee8f2SPeter Brune + snes - the SNES context 122a4ee8f2SPeter Brune . X - solution 132a4ee8f2SPeter Brune . F - current function/gradient 142a4ee8f2SPeter Brune . obj - real to hold the objective value 152a4ee8f2SPeter Brune - ctx - optional user-defined objective context 162a4ee8f2SPeter Brune 17878cb397SSatish Balay Level: advanced 18878cb397SSatish Balay 19f8b49ee9SBarry Smith .seealso: SNESSetFunction(), SNESGetFunction(), SNESSetObjective(), SNESGetObjective(), SNESJacobianFunction, SNESFunction 20075cc632SBarry Smith M*/ 21075cc632SBarry Smith 22075cc632SBarry Smith 23075cc632SBarry Smith #undef __FUNCT__ 24075cc632SBarry Smith #define __FUNCT__ "SNESSetObjective" 25075cc632SBarry Smith /*@C 26eb23ba39SBarry Smith SNESSetObjective - Sets the objective function minimized by some of the SNES linesearch methods. 27075cc632SBarry Smith 28075cc632SBarry Smith Logically Collective on SNES 29075cc632SBarry Smith 30075cc632SBarry Smith Input Parameters: 31075cc632SBarry Smith + snes - the SNES context 32f8b49ee9SBarry Smith . obj - objective evaluation routine; see SNESObjectiveFunction for details 33075cc632SBarry Smith - ctx - [optional] user-defined context for private data for the 340298fd71SBarry Smith function evaluation routine (may be NULL) 35075cc632SBarry Smith 362b26979fSBarry Smith Level: intermediate 372a4ee8f2SPeter Brune 38eb23ba39SBarry Smith Note: This is not used in the SNESLINESEARCHCP line search. 39eb23ba39SBarry Smith 40eb23ba39SBarry Smith If not provided then this defaults to the two norm of the function evaluation (set with SNESSetFunction()) 41075cc632SBarry Smith 422a4ee8f2SPeter Brune .keywords: SNES, nonlinear, set, objective 432a4ee8f2SPeter Brune 44075cc632SBarry Smith .seealso: SNESGetObjective(), SNESComputeObjective(), SNESSetFunction(), SNESSetJacobian(), SNESObjectiveFunction 452a4ee8f2SPeter Brune @*/ 46f8b49ee9SBarry Smith PetscErrorCode SNESSetObjective(SNES snes,PetscErrorCode (*obj)(SNES,Vec,PetscReal*,void*),void *ctx) 472a4ee8f2SPeter Brune { 482a4ee8f2SPeter Brune PetscErrorCode ierr; 492a4ee8f2SPeter Brune DM dm; 502a4ee8f2SPeter Brune 512a4ee8f2SPeter Brune PetscFunctionBegin; 522a4ee8f2SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 532a4ee8f2SPeter Brune ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 54f8b49ee9SBarry Smith ierr = DMSNESSetObjective(dm,obj,ctx);CHKERRQ(ierr); 552a4ee8f2SPeter Brune PetscFunctionReturn(0); 562a4ee8f2SPeter Brune } 572a4ee8f2SPeter Brune 582a4ee8f2SPeter Brune #undef __FUNCT__ 592a4ee8f2SPeter Brune #define __FUNCT__ "SNESGetObjective" 602a4ee8f2SPeter Brune /*@C 612a4ee8f2SPeter Brune SNESGetObjective - Returns the objective function. 622a4ee8f2SPeter Brune 632a4ee8f2SPeter Brune Not Collective 642a4ee8f2SPeter Brune 652a4ee8f2SPeter Brune Input Parameter: 662a4ee8f2SPeter Brune . snes - the SNES context 672a4ee8f2SPeter Brune 682a4ee8f2SPeter Brune Output Parameter: 69f8b49ee9SBarry Smith + obj - objective evaluation routine (or NULL); see SNESObjectFunction for details 700298fd71SBarry Smith - ctx - the function context (or NULL) 712a4ee8f2SPeter Brune 722a4ee8f2SPeter Brune Level: advanced 732a4ee8f2SPeter Brune 742a4ee8f2SPeter Brune .keywords: SNES, nonlinear, get, objective 752a4ee8f2SPeter Brune 762a4ee8f2SPeter Brune .seealso: SNESSetObjective(), SNESGetSolution() 772a4ee8f2SPeter Brune @*/ 78f8b49ee9SBarry Smith PetscErrorCode SNESGetObjective(SNES snes,PetscErrorCode (**obj)(SNES,Vec,PetscReal*,void*),void **ctx) 792a4ee8f2SPeter Brune { 802a4ee8f2SPeter Brune PetscErrorCode ierr; 812a4ee8f2SPeter Brune DM dm; 825fd66863SKarl Rupp 832a4ee8f2SPeter Brune PetscFunctionBegin; 842a4ee8f2SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 852a4ee8f2SPeter Brune ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 86f8b49ee9SBarry Smith ierr = DMSNESGetObjective(dm,obj,ctx);CHKERRQ(ierr); 872a4ee8f2SPeter Brune PetscFunctionReturn(0); 882a4ee8f2SPeter Brune } 892a4ee8f2SPeter Brune 902a4ee8f2SPeter Brune #undef __FUNCT__ 912a4ee8f2SPeter Brune #define __FUNCT__ "SNESComputeObjective" 922a4ee8f2SPeter Brune /*@C 932a4ee8f2SPeter Brune SNESComputeObjective - Computes the objective. 942a4ee8f2SPeter Brune 952a4ee8f2SPeter Brune Collective on SNES 962a4ee8f2SPeter Brune 972a4ee8f2SPeter Brune Input Parameter: 982a4ee8f2SPeter Brune + snes - the SNES context 992a4ee8f2SPeter Brune - X - the state vector 1002a4ee8f2SPeter Brune 1012a4ee8f2SPeter Brune Output Parameter: 1022a4ee8f2SPeter Brune . ob - the objective value 1032a4ee8f2SPeter Brune 1042a4ee8f2SPeter Brune Level: advanced 1052a4ee8f2SPeter Brune 1062a4ee8f2SPeter Brune .keywords: SNES, nonlinear, compute, objective 1072a4ee8f2SPeter Brune 1082a4ee8f2SPeter Brune .seealso: SNESSetObjective(), SNESGetSolution() 1092a4ee8f2SPeter Brune @*/ 1102a4ee8f2SPeter Brune PetscErrorCode SNESComputeObjective(SNES snes,Vec X,PetscReal *ob) 1112a4ee8f2SPeter Brune { 1122a4ee8f2SPeter Brune PetscErrorCode ierr; 1132a4ee8f2SPeter Brune DM dm; 114942e3340SBarry Smith DMSNES sdm; 115f23aa3ddSBarry Smith 1162a4ee8f2SPeter Brune PetscFunctionBegin; 1172a4ee8f2SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1182a4ee8f2SPeter Brune PetscValidHeaderSpecific(X,VEC_CLASSID,2); 1192a4ee8f2SPeter Brune PetscValidPointer(ob,3); 1202a4ee8f2SPeter Brune ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 121942e3340SBarry Smith ierr = DMGetDMSNES(dm,&sdm);CHKERRQ(ierr); 12222c6f798SBarry Smith if (sdm->ops->computeobjective) { 123*94db00ebSBarry Smith ierr = PetscLogEventBegin(SNES_ObjectiveEval,snes,X,0,0);CHKERRQ(ierr); 12422c6f798SBarry Smith ierr = (sdm->ops->computeobjective)(snes,X,ob,sdm->objectivectx);CHKERRQ(ierr); 125*94db00ebSBarry Smith ierr = PetscLogEventEnd(SNES_ObjectiveEval,snes,X,0,0);CHKERRQ(ierr); 126ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetObjective() before SNESComputeObjective()."); 1272a4ee8f2SPeter Brune PetscFunctionReturn(0); 1282a4ee8f2SPeter Brune } 12997584545SPeter Brune 13097584545SPeter Brune 13197584545SPeter Brune #undef __FUNCT__ 1328d359177SBarry Smith #define __FUNCT__ "SNESObjectiveComputeFunctionDefaultFD" 1335c3e6ab7SPeter Brune /*@C 1345c3e6ab7SPeter Brune SNESObjectiveComputeFunctionDefaultFD - Computes the gradient of a user provided objective 1355c3e6ab7SPeter Brune 1365c3e6ab7SPeter Brune Collective on SNES 1375c3e6ab7SPeter Brune 1385c3e6ab7SPeter Brune Input Parameter: 1395c3e6ab7SPeter Brune + snes - the SNES context 1405c3e6ab7SPeter Brune . X - the state vector 1415c3e6ab7SPeter Brune - ctx - the (ignored) function context 1425c3e6ab7SPeter Brune 1435c3e6ab7SPeter Brune Output Parameter: 1445c3e6ab7SPeter Brune . F - the function value 1455c3e6ab7SPeter Brune 1465c3e6ab7SPeter Brune Options Database Key: 1475c3e6ab7SPeter Brune + -snes_fd_function_eps - The differencing parameter 1485c3e6ab7SPeter Brune - -snes_fd_function - Compute function from user provided objective with finite difference 1495c3e6ab7SPeter Brune 1505c3e6ab7SPeter Brune Notes: 1515c3e6ab7SPeter Brune SNESObjectiveComputeFunctionDefaultFD is similar in character to SNESComputeJacobianDefault. 1525c3e6ab7SPeter Brune Therefore, it should be used for debugging purposes only. Using it in conjunction with 1535c3e6ab7SPeter Brune SNESComputeJacobianDefault is excessively costly and produces a Jacobian that is quite 1545c3e6ab7SPeter Brune noisy. This is often necessary, but should be done with a grain of salt, even when debugging 1555c3e6ab7SPeter Brune small problems. 1565c3e6ab7SPeter Brune 1575c3e6ab7SPeter Brune Note that this uses quadratic interpolation of the objective to form each value in the function. 1585c3e6ab7SPeter Brune 1595c3e6ab7SPeter Brune Level: advanced 1605c3e6ab7SPeter Brune 1615c3e6ab7SPeter Brune .keywords: SNES, objective, debugging, finite differences, function 1625c3e6ab7SPeter Brune 1635c3e6ab7SPeter Brune .seealso: SNESSetFunction(), SNESComputeObjective(), SNESComputeJacobianDefault() 1645c3e6ab7SPeter Brune @*/ 1658d359177SBarry Smith PetscErrorCode SNESObjectiveComputeFunctionDefaultFD(SNES snes,Vec X,Vec F,void *ctx) 1660adebc6cSBarry Smith { 16797584545SPeter Brune Vec Xh; 16897584545SPeter Brune PetscErrorCode ierr; 16997584545SPeter Brune PetscInt i,N,start,end; 17097584545SPeter Brune PetscReal ob,ob1,ob2,ob3,fob,dx,eps=1e-6; 17197584545SPeter Brune PetscScalar fv,xv; 17297584545SPeter Brune 17397584545SPeter Brune PetscFunctionBegin; 17497584545SPeter Brune ierr = VecDuplicate(X,&Xh);CHKERRQ(ierr); 175302440fdSBarry Smith ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->prefix,"Differencing parameters","SNES");CHKERRQ(ierr); 1760298fd71SBarry Smith ierr = PetscOptionsReal("-snes_fd_function_eps","Tolerance for nonzero entries in fd function","None",eps,&eps,NULL);CHKERRQ(ierr); 177302440fdSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 17897584545SPeter Brune ierr = VecSet(F,0.);CHKERRQ(ierr); 17997584545SPeter Brune 18097584545SPeter Brune ierr = VecNorm(X,NORM_2,&fob);CHKERRQ(ierr); 18197584545SPeter Brune 18297584545SPeter Brune ierr = VecGetSize(X,&N);CHKERRQ(ierr); 18397584545SPeter Brune ierr = VecGetOwnershipRange(X,&start,&end);CHKERRQ(ierr); 18497584545SPeter Brune ierr = SNESComputeObjective(snes,X,&ob);CHKERRQ(ierr); 18597584545SPeter Brune 186f5af7f23SKarl Rupp if (fob > 0.) dx =1e-6*fob; 187f5af7f23SKarl Rupp else dx = 1e-6; 18897584545SPeter Brune 18997584545SPeter Brune for (i=0; i<N; i++) { 19097584545SPeter Brune /* compute the 1st value */ 19197584545SPeter Brune ierr = VecCopy(X,Xh);CHKERRQ(ierr); 19297584545SPeter Brune if (i>= start && i<end) { 19397584545SPeter Brune xv = dx; 19497584545SPeter Brune ierr = VecSetValues(Xh,1,&i,&xv,ADD_VALUES);CHKERRQ(ierr); 19597584545SPeter Brune } 19697584545SPeter Brune ierr = VecAssemblyBegin(Xh);CHKERRQ(ierr); 19797584545SPeter Brune ierr = VecAssemblyEnd(Xh);CHKERRQ(ierr); 19897584545SPeter Brune ierr = SNESComputeObjective(snes,Xh,&ob1);CHKERRQ(ierr); 19997584545SPeter Brune 20097584545SPeter Brune /* compute the 2nd value */ 20197584545SPeter Brune ierr = VecCopy(X,Xh);CHKERRQ(ierr); 20297584545SPeter Brune if (i>= start && i<end) { 20397584545SPeter Brune xv = 2.*dx; 20497584545SPeter Brune ierr = VecSetValues(Xh,1,&i,&xv,ADD_VALUES);CHKERRQ(ierr); 20597584545SPeter Brune } 20697584545SPeter Brune ierr = VecAssemblyBegin(Xh);CHKERRQ(ierr); 20797584545SPeter Brune ierr = VecAssemblyEnd(Xh);CHKERRQ(ierr); 20897584545SPeter Brune ierr = SNESComputeObjective(snes,Xh,&ob2);CHKERRQ(ierr); 20997584545SPeter Brune 21097584545SPeter Brune /* compute the 3rd value */ 21197584545SPeter Brune ierr = VecCopy(X,Xh);CHKERRQ(ierr); 21297584545SPeter Brune if (i>= start && i<end) { 21397584545SPeter Brune xv = -dx; 21497584545SPeter Brune ierr = VecSetValues(Xh,1,&i,&xv,ADD_VALUES);CHKERRQ(ierr); 21597584545SPeter Brune } 21697584545SPeter Brune ierr = VecAssemblyBegin(Xh);CHKERRQ(ierr); 21797584545SPeter Brune ierr = VecAssemblyEnd(Xh);CHKERRQ(ierr); 21897584545SPeter Brune ierr = SNESComputeObjective(snes,Xh,&ob3);CHKERRQ(ierr); 21997584545SPeter Brune 22097584545SPeter Brune if (i >= start && i<end) { 22197584545SPeter Brune /* set this entry to be the gradient of the objective */ 22297584545SPeter Brune fv = (-ob2 + 6.*ob1 - 3.*ob -2.*ob3) / (6.*dx); 22397584545SPeter Brune if (PetscAbsScalar(fv) > eps) { 22497584545SPeter Brune ierr = VecSetValues(F,1,&i,&fv,INSERT_VALUES);CHKERRQ(ierr); 22597584545SPeter Brune } else { 22697584545SPeter Brune fv = 0.; 22797584545SPeter Brune ierr = VecSetValues(F,1,&i,&fv,INSERT_VALUES);CHKERRQ(ierr); 22897584545SPeter Brune } 22997584545SPeter Brune } 23097584545SPeter Brune } 23197584545SPeter Brune 23297584545SPeter Brune ierr = VecDestroy(&Xh);CHKERRQ(ierr); 23397584545SPeter Brune 23497584545SPeter Brune ierr = VecAssemblyBegin(F);CHKERRQ(ierr); 23597584545SPeter Brune ierr = VecAssemblyEnd(F);CHKERRQ(ierr); 23697584545SPeter Brune PetscFunctionReturn(0); 23797584545SPeter Brune } 238