static const char help[] = "Tries to solve u`` + u^{2} = f for an easy case and an impossible case.\n\n"; /* This example was contributed by Peter Graf to show how SNES fails when given a nonlinear problem with no solution. Run with -n 14 to see fail to converge and -n 15 to see convergence The option -second_order uses a different discretization of the Neumann boundary condition and always converges */ #include PetscBool second_order = PETSC_FALSE; #define X0DOT -2.0 #define X1 5.0 #define KPOW 2.0 const PetscScalar sperturb = 1.1; /* User-defined routines */ PetscErrorCode FormJacobian(SNES,Vec,Mat,Mat,void*); PetscErrorCode FormFunction(SNES,Vec,Vec,void*); int main(int argc,char **argv) { SNES snes; /* SNES context */ Vec x,r,F; /* vectors */ Mat J; /* Jacobian */ PetscInt it,n = 11,i; PetscReal h,xp = 0.0; PetscScalar v; const PetscScalar a = X0DOT; const PetscScalar b = X1; const PetscScalar k = KPOW; PetscScalar v2; PetscScalar *xx; PetscFunctionBeginUser; PetscCall(PetscInitialize(&argc,&argv,(char*)0,help)); PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL)); PetscCall(PetscOptionsGetBool(NULL,NULL,"-second_order",&second_order,NULL)); h = 1.0/(n-1); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create nonlinear solver context - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ PetscCall(SNESCreate(PETSC_COMM_WORLD,&snes)); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create vector data structures; set function evaluation routine - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ PetscCall(VecCreate(PETSC_COMM_SELF,&x)); PetscCall(VecSetSizes(x,PETSC_DECIDE,n)); PetscCall(VecSetFromOptions(x)); PetscCall(VecDuplicate(x,&r)); PetscCall(VecDuplicate(x,&F)); PetscCall(SNESSetFunction(snes,r,FormFunction,(void*)F)); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create matrix data structures; set Jacobian evaluation routine - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ PetscCall(MatCreateSeqAIJ(PETSC_COMM_SELF,n,n,3,NULL,&J)); /* Note that in this case we create separate matrices for the Jacobian and preconditioner matrix. Both of these are computed in the routine FormJacobian() */ /* PetscCall(SNESSetJacobian(snes,NULL,JPrec,FormJacobian,0)); */ PetscCall(SNESSetJacobian(snes,J,J,FormJacobian,0)); /* PetscCall(SNESSetJacobian(snes,J,JPrec,FormJacobian,0)); */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Customize nonlinear solver; set runtime options - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ PetscCall(SNESSetFromOptions(snes)); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Initialize application: Store right-hand-side of PDE and exact solution - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* set right hand side and initial guess to be exact solution of continuim problem */ #define SQR(x) ((x)*(x)) xp = 0.0; for (i=0; i