1c4762a1bSJed Brown static const char help[] = "Attempts to solve for root of a function with multiple local minima.\n\ 2c4762a1bSJed Brown With the proper initial guess, a backtracking line-search fails because Newton's method gets\n\ 3c4762a1bSJed Brown stuck in a local minimum. However, a critical point line-search or Newton's method without a\n\ 4c4762a1bSJed Brown line search succeeds.\n"; 5c4762a1bSJed Brown 6c4762a1bSJed Brown /* Solve 1D problem f(x) = 8 * exp(-4 * (x - 2)^2) * (x - 2) + 2 * x = 0 7c4762a1bSJed Brown 8c4762a1bSJed Brown This problem is based on the example given here: https://scicomp.stackexchange.com/a/2446/24756 9c4762a1bSJed Brown Originally an optimization problem to find the minimum of the function 10c4762a1bSJed Brown 11c4762a1bSJed Brown g(x) = x^2 - exp(-4 * (x - 2)^2) 12c4762a1bSJed Brown 13c4762a1bSJed Brown it has been reformulated to solve dg(x)/dx = f(x) = 0. The reformulated problem has several local 14c4762a1bSJed Brown minima that can cause problems for some global Newton root-finding methods. In this particular 15c4762a1bSJed Brown example, an initial guess of x0 = 2.5 generates an initial search direction (-df/dx is positive) 16c4762a1bSJed Brown away from the root and towards a local minimum in which a back-tracking line search gets trapped. 17c4762a1bSJed Brown However, omitting a line-search or using a critical point line search, the solve is successful. 18c4762a1bSJed Brown 19c4762a1bSJed Brown The test outputs the final result for x and f(x). 20c4762a1bSJed Brown 21c4762a1bSJed Brown Example usage: 22c4762a1bSJed Brown 23c4762a1bSJed Brown Get help: 24c4762a1bSJed Brown ./ex99 -help 25c4762a1bSJed Brown 26c4762a1bSJed Brown Monitor run (with default back-tracking line search; solve fails): 27c4762a1bSJed Brown ./ex99 -snes_converged_reason -snes_monitor -snes_linesearch_monitor -ksp_converged_reason -ksp_monitor 28c4762a1bSJed Brown 29c4762a1bSJed Brown Run without a line search; solve succeeds: 30c4762a1bSJed Brown ./ex99 -snes_linesearch_type basic 31c4762a1bSJed Brown 32c4762a1bSJed Brown Run with a critical point line search; solve succeeds: 33c4762a1bSJed Brown ./ex99 -snes_linesearch_type cp 34c4762a1bSJed Brown */ 35c4762a1bSJed Brown 36c4762a1bSJed Brown #include <math.h> 37c4762a1bSJed Brown #include <petscsnes.h> 38c4762a1bSJed Brown 39c4762a1bSJed Brown extern PetscErrorCode FormJacobian(SNES,Vec,Mat,Mat,void*); 40c4762a1bSJed Brown extern PetscErrorCode FormFunction(SNES,Vec,Vec,void*); 41c4762a1bSJed Brown 42c4762a1bSJed Brown int main(int argc,char **argv) 43c4762a1bSJed Brown { 44c4762a1bSJed Brown SNES snes; /* nonlinear solver context */ 45c4762a1bSJed Brown KSP ksp; /* linear solver context */ 46c4762a1bSJed Brown PC pc; /* preconditioner context */ 47c4762a1bSJed Brown Vec x,r; /* solution, residual vectors */ 48c4762a1bSJed Brown Mat J; /* Jacobian matrix */ 49c4762a1bSJed Brown PetscMPIInt size; 50c4762a1bSJed Brown 519566063dSJacob Faibussowitsch PetscCall(PetscInitialize(&argc,&argv,(char*)0,help)); 529566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 53*be096a46SBarry Smith PetscCheck(size == 1,PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"Example is only for sequential runs"); 54c4762a1bSJed Brown 55c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 56c4762a1bSJed Brown Create nonlinear solver context 57c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 589566063dSJacob Faibussowitsch PetscCall(SNESCreate(PETSC_COMM_WORLD,&snes)); 59c4762a1bSJed Brown 60c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 61c4762a1bSJed Brown Create matrix and vector data structures; set corresponding routines 62c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 63c4762a1bSJed Brown /* 64c4762a1bSJed Brown Create vectors for solution and nonlinear function 65c4762a1bSJed Brown */ 669566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_WORLD,&x)); 679566063dSJacob Faibussowitsch PetscCall(VecSetSizes(x,PETSC_DECIDE,1)); 689566063dSJacob Faibussowitsch PetscCall(VecSetFromOptions(x)); 699566063dSJacob Faibussowitsch PetscCall(VecDuplicate(x,&r)); 70c4762a1bSJed Brown 71c4762a1bSJed Brown /* 72c4762a1bSJed Brown Create Jacobian matrix data structure 73c4762a1bSJed Brown */ 749566063dSJacob Faibussowitsch PetscCall(MatCreate(PETSC_COMM_WORLD,&J)); 759566063dSJacob Faibussowitsch PetscCall(MatSetSizes(J,PETSC_DECIDE,PETSC_DECIDE,1,1)); 769566063dSJacob Faibussowitsch PetscCall(MatSetFromOptions(J)); 779566063dSJacob Faibussowitsch PetscCall(MatSetUp(J)); 78c4762a1bSJed Brown 799566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes,r,FormFunction,NULL)); 809566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,J,J,FormJacobian,NULL)); 81c4762a1bSJed Brown 82c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 83c4762a1bSJed Brown Customize nonlinear solver; set runtime options 84c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 85c4762a1bSJed Brown /* 86c4762a1bSJed Brown Set linear solver defaults for this problem. By extracting the 87c4762a1bSJed Brown KSP and PC contexts from the SNES context, we can then 88c4762a1bSJed Brown directly call any KSP and PC routines to set various options. 89c4762a1bSJed Brown */ 909566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes,&ksp)); 919566063dSJacob Faibussowitsch PetscCall(KSPGetPC(ksp,&pc)); 929566063dSJacob Faibussowitsch PetscCall(PCSetType(pc,PCNONE)); 939566063dSJacob Faibussowitsch PetscCall(KSPSetTolerances(ksp,1.e-4,PETSC_DEFAULT,PETSC_DEFAULT,20)); 94c4762a1bSJed Brown 95c4762a1bSJed Brown /* 96c4762a1bSJed Brown Set SNES/KSP/KSP/PC runtime options, e.g., 97c4762a1bSJed Brown -snes_view -snes_monitor -ksp_type <ksp> -pc_type <pc> 98c4762a1bSJed Brown These options will override those specified above as long as 99c4762a1bSJed Brown SNESSetFromOptions() is called _after_ any other customization 100c4762a1bSJed Brown routines. 101c4762a1bSJed Brown */ 1029566063dSJacob Faibussowitsch PetscCall(SNESSetFromOptions(snes)); 103c4762a1bSJed Brown 104c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 105c4762a1bSJed Brown Evaluate initial guess; then solve nonlinear system 106c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 1079566063dSJacob Faibussowitsch PetscCall(VecSet(x,2.5)); 108c4762a1bSJed Brown 1099566063dSJacob Faibussowitsch PetscCall(SNESSolve(snes,NULL,x)); 110c4762a1bSJed Brown 111c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 112c4762a1bSJed Brown Output x and f(x) 113c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 1149566063dSJacob Faibussowitsch PetscCall(VecView(x,PETSC_VIEWER_STDOUT_WORLD)); 1159566063dSJacob Faibussowitsch PetscCall(VecView(r,PETSC_VIEWER_STDOUT_WORLD)); 116c4762a1bSJed Brown 117c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 118c4762a1bSJed Brown Free work space. All PETSc objects should be destroyed when they 119c4762a1bSJed Brown are no longer needed. 120c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 121c4762a1bSJed Brown 1229566063dSJacob Faibussowitsch PetscCall(VecDestroy(&x)); PetscCall(VecDestroy(&r)); 1239566063dSJacob Faibussowitsch PetscCall(MatDestroy(&J)); PetscCall(SNESDestroy(&snes)); 1249566063dSJacob Faibussowitsch PetscCall(PetscFinalize()); 125b122ec5aSJacob Faibussowitsch return 0; 126c4762a1bSJed Brown } 127c4762a1bSJed Brown 128c4762a1bSJed Brown PetscErrorCode FormFunction(SNES snes,Vec x,Vec f,void *ctx) 129c4762a1bSJed Brown { 130c4762a1bSJed Brown const PetscScalar *xx; 131c4762a1bSJed Brown PetscScalar *ff; 132c4762a1bSJed Brown 133c4762a1bSJed Brown /* 134c4762a1bSJed Brown Get pointers to vector data. 135c4762a1bSJed Brown - For default PETSc vectors, VecGetArray() returns a pointer to 136c4762a1bSJed Brown the data array. Otherwise, the routine is implementation dependent. 137c4762a1bSJed Brown - You MUST call VecRestoreArray() when you no longer need access to 138c4762a1bSJed Brown the array. 139c4762a1bSJed Brown */ 1409566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(x,&xx)); 1419566063dSJacob Faibussowitsch PetscCall(VecGetArray(f,&ff)); 142c4762a1bSJed Brown 143c4762a1bSJed Brown /* Compute function */ 144c4762a1bSJed Brown ff[0] = 8. * PetscExpScalar(-4. * (xx[0] - 2.) * (xx[0] - 2.)) * (xx[0] - 2.) + 2. * xx[0]; 145c4762a1bSJed Brown 146c4762a1bSJed Brown /* Restore vectors */ 1479566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(x,&xx)); 1489566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(f,&ff)); 149c4762a1bSJed Brown return 0; 150c4762a1bSJed Brown } 151c4762a1bSJed Brown 152c4762a1bSJed Brown PetscErrorCode FormJacobian(SNES snes,Vec x,Mat jac,Mat B,void *dummy) 153c4762a1bSJed Brown { 154c4762a1bSJed Brown const PetscScalar *xx; 155c4762a1bSJed Brown PetscScalar A[1]; 156c4762a1bSJed Brown PetscInt idx[1] = {0}; 157c4762a1bSJed Brown 158c4762a1bSJed Brown /* 159c4762a1bSJed Brown Get pointer to vector data 160c4762a1bSJed Brown */ 1619566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(x,&xx)); 162c4762a1bSJed Brown 163c4762a1bSJed Brown /* 164c4762a1bSJed Brown Compute Jacobian entries and insert into matrix. 165c4762a1bSJed Brown - Since this is such a small problem, we set all entries for 166c4762a1bSJed Brown the matrix at once. 167c4762a1bSJed Brown */ 168c4762a1bSJed Brown A[0] = 8. * ((xx[0] - 2.) * (PetscExpScalar(-4. * (xx[0] - 2.) * (xx[0] - 2.)) * -8. * (xx[0] - 2.)) 169c4762a1bSJed Brown + PetscExpScalar(-4. * (xx[0] - 2.) * (xx[0] - 2.))) 170c4762a1bSJed Brown + 2.; 171c4762a1bSJed Brown 1729566063dSJacob Faibussowitsch PetscCall(MatSetValues(B,1,idx,1,idx,A,INSERT_VALUES)); 173c4762a1bSJed Brown 174c4762a1bSJed Brown /* 175c4762a1bSJed Brown Restore vector 176c4762a1bSJed Brown */ 1779566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(x,&xx)); 178c4762a1bSJed Brown 179c4762a1bSJed Brown /* 180c4762a1bSJed Brown Assemble matrix 181c4762a1bSJed Brown */ 1829566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY)); 1839566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY)); 184c4762a1bSJed Brown if (jac != B) { 1859566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY)); 1869566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY)); 187c4762a1bSJed Brown } 188c4762a1bSJed Brown return 0; 189c4762a1bSJed Brown } 190c4762a1bSJed Brown 191c4762a1bSJed Brown /*TEST 192c4762a1bSJed Brown 193c4762a1bSJed Brown test: 194c4762a1bSJed Brown suffix: 1 195c4762a1bSJed Brown args: -snes_linesearch_type cp 196c4762a1bSJed Brown test: 197c4762a1bSJed Brown suffix: 2 198c4762a1bSJed Brown args: -snes_linesearch_type basic 199c4762a1bSJed Brown test: 200c4762a1bSJed Brown suffix: 3 20141ba4c6cSHeeho Park test: 20241ba4c6cSHeeho Park suffix: 4 20341ba4c6cSHeeho Park args: -snes_type newtontrdc 20441ba4c6cSHeeho Park test: 20541ba4c6cSHeeho Park suffix: 5 20641ba4c6cSHeeho Park args: -snes_type newtontrdc -snes_trdc_use_cauchy false 207c4762a1bSJed Brown 208c4762a1bSJed Brown TEST*/ 209