1c4762a1bSJed Brown 2c4762a1bSJed Brown static char help[] = "Newton method to solve u'' + u^{2} = f, sequentially.\n\ 3c4762a1bSJed Brown This example tests PCVPBJacobiSetBlocks().\n\n"; 4c4762a1bSJed Brown 5c4762a1bSJed Brown /*T 6c4762a1bSJed Brown Concepts: SNES^basic uniprocessor example 7c4762a1bSJed Brown Processors: 1 8c4762a1bSJed Brown T*/ 9c4762a1bSJed Brown 10c4762a1bSJed Brown /* 11c4762a1bSJed Brown Include "petscsnes.h" so that we can use SNES solvers. Note that this 12c4762a1bSJed Brown file automatically includes: 13c4762a1bSJed Brown petscsys.h - base PETSc routines petscvec.h - vectors 14c4762a1bSJed Brown petscmat.h - matrices 15c4762a1bSJed Brown petscis.h - index sets petscksp.h - Krylov subspace methods 16c4762a1bSJed Brown petscviewer.h - viewers petscpc.h - preconditioners 17c4762a1bSJed Brown petscksp.h - linear solvers 18c4762a1bSJed Brown */ 19c4762a1bSJed Brown 20c4762a1bSJed Brown #include <petscsnes.h> 21c4762a1bSJed Brown 22c4762a1bSJed Brown /* 23c4762a1bSJed Brown User-defined routines 24c4762a1bSJed Brown */ 25c4762a1bSJed Brown extern PetscErrorCode FormJacobian(SNES,Vec,Mat,Mat,void*); 26c4762a1bSJed Brown extern PetscErrorCode FormFunction(SNES,Vec,Vec,void*); 27c4762a1bSJed Brown extern PetscErrorCode FormInitialGuess(Vec); 28c4762a1bSJed Brown 29c4762a1bSJed Brown int main(int argc,char **argv) 30c4762a1bSJed Brown { 31c4762a1bSJed Brown SNES snes; /* SNES context */ 32c4762a1bSJed Brown Vec x,r,F,U; /* vectors */ 33c4762a1bSJed Brown Mat J; /* Jacobian matrix */ 34c4762a1bSJed Brown PetscErrorCode ierr; 35c4762a1bSJed Brown PetscInt its,n = 5,i,maxit,maxf,lens[3] = {1,2,2}; 36c4762a1bSJed Brown PetscMPIInt size; 37c4762a1bSJed Brown PetscScalar h,xp,v,none = -1.0; 38c4762a1bSJed Brown PetscReal abstol,rtol,stol,norm; 39c4762a1bSJed Brown KSP ksp; 40c4762a1bSJed Brown PC pc; 41c4762a1bSJed Brown 42c4762a1bSJed Brown ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; 43*5f80ce2aSJacob Faibussowitsch CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 442c71b3e2SJacob Faibussowitsch PetscCheckFalse(size != 1,PETSC_COMM_SELF,PETSC_ERR_SUP,"This is a uniprocessor example only!"); 45*5f80ce2aSJacob Faibussowitsch CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL)); 46c4762a1bSJed Brown h = 1.0/(n-1); 47c4762a1bSJed Brown 48c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 49c4762a1bSJed Brown Create nonlinear solver context 50c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 51c4762a1bSJed Brown 52*5f80ce2aSJacob Faibussowitsch CHKERRQ(SNESCreate(PETSC_COMM_WORLD,&snes)); 53*5f80ce2aSJacob Faibussowitsch CHKERRQ(SNESGetKSP(snes,&ksp)); 54*5f80ce2aSJacob Faibussowitsch CHKERRQ(KSPGetPC(ksp,&pc)); 55*5f80ce2aSJacob Faibussowitsch CHKERRQ(PCSetType(pc,PCVPBJACOBI)); 56c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 57c4762a1bSJed Brown Create vector data structures; set function evaluation routine 58c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 59c4762a1bSJed Brown /* 60c4762a1bSJed Brown Note that we form 1 vector from scratch and then duplicate as needed. 61c4762a1bSJed Brown */ 62*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecCreate(PETSC_COMM_WORLD,&x)); 63*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecSetSizes(x,PETSC_DECIDE,n)); 64*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecSetFromOptions(x)); 65*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecDuplicate(x,&r)); 66*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecDuplicate(x,&F)); 67*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecDuplicate(x,&U)); 68c4762a1bSJed Brown 69c4762a1bSJed Brown /* 70c4762a1bSJed Brown Set function evaluation routine and vector 71c4762a1bSJed Brown */ 72*5f80ce2aSJacob Faibussowitsch CHKERRQ(SNESSetFunction(snes,r,FormFunction,(void*)F)); 73c4762a1bSJed Brown 74c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 75c4762a1bSJed Brown Create matrix data structure; set Jacobian evaluation routine 76c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 77c4762a1bSJed Brown 78*5f80ce2aSJacob Faibussowitsch CHKERRQ(MatCreate(PETSC_COMM_WORLD,&J)); 79*5f80ce2aSJacob Faibussowitsch CHKERRQ(MatSetSizes(J,PETSC_DECIDE,PETSC_DECIDE,n,n)); 80*5f80ce2aSJacob Faibussowitsch CHKERRQ(MatSetFromOptions(J)); 81*5f80ce2aSJacob Faibussowitsch CHKERRQ(MatSeqAIJSetPreallocation(J,3,NULL)); 82*5f80ce2aSJacob Faibussowitsch CHKERRQ(MatSetVariableBlockSizes(J,3,lens)); 83c4762a1bSJed Brown 84c4762a1bSJed Brown /* 85c4762a1bSJed Brown Set Jacobian matrix data structure and default Jacobian evaluation 86c4762a1bSJed Brown routine. User can override with: 87c4762a1bSJed Brown -snes_fd : default finite differencing approximation of Jacobian 88c4762a1bSJed Brown -snes_mf : matrix-free Newton-Krylov method with no preconditioning 89c4762a1bSJed Brown (unless user explicitly sets preconditioner) 90c4762a1bSJed Brown -snes_mf_operator : form preconditioning matrix as set by the user, 91c4762a1bSJed Brown but use matrix-free approx for Jacobian-vector 92c4762a1bSJed Brown products within Newton-Krylov method 93c4762a1bSJed Brown */ 94c4762a1bSJed Brown 95*5f80ce2aSJacob Faibussowitsch CHKERRQ(SNESSetJacobian(snes,J,J,FormJacobian,NULL)); 96c4762a1bSJed Brown 97c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 98c4762a1bSJed Brown Customize nonlinear solver; set runtime options 99c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 100c4762a1bSJed Brown 101c4762a1bSJed Brown /* 102c4762a1bSJed Brown Set names for some vectors to facilitate monitoring (optional) 103c4762a1bSJed Brown */ 104*5f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectSetName((PetscObject)x,"Approximate Solution")); 105*5f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectSetName((PetscObject)U,"Exact Solution")); 106c4762a1bSJed Brown 107c4762a1bSJed Brown /* 108c4762a1bSJed Brown Set SNES/KSP/KSP/PC runtime options, e.g., 109c4762a1bSJed Brown -snes_view -snes_monitor -ksp_type <ksp> -pc_type <pc> 110c4762a1bSJed Brown */ 111*5f80ce2aSJacob Faibussowitsch CHKERRQ(SNESSetFromOptions(snes)); 112c4762a1bSJed Brown 113c4762a1bSJed Brown /* 114c4762a1bSJed Brown Print parameters used for convergence testing (optional) ... just 115c4762a1bSJed Brown to demonstrate this routine; this information is also printed with 116c4762a1bSJed Brown the option -snes_view 117c4762a1bSJed Brown */ 118*5f80ce2aSJacob Faibussowitsch CHKERRQ(SNESGetTolerances(snes,&abstol,&rtol,&stol,&maxit,&maxf)); 119*5f80ce2aSJacob Faibussowitsch CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"atol=%g, rtol=%g, stol=%g, maxit=%D, maxf=%D\n",(double)abstol,(double)rtol,(double)stol,maxit,maxf)); 120c4762a1bSJed Brown 121c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 122c4762a1bSJed Brown Initialize application: 123c4762a1bSJed Brown Store right-hand-side of PDE and exact solution 124c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 125c4762a1bSJed Brown 126c4762a1bSJed Brown xp = 0.0; 127c4762a1bSJed Brown for (i=0; i<n; i++) { 128c4762a1bSJed Brown v = 6.0*xp + PetscPowScalar(xp+1.e-12,6.0); /* +1.e-12 is to prevent 0^6 */ 129*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecSetValues(F,1,&i,&v,INSERT_VALUES)); 130c4762a1bSJed Brown v = xp*xp*xp; 131*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecSetValues(U,1,&i,&v,INSERT_VALUES)); 132c4762a1bSJed Brown xp += h; 133c4762a1bSJed Brown } 134c4762a1bSJed Brown 135c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 136c4762a1bSJed Brown Evaluate initial guess; then solve nonlinear system 137c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 138c4762a1bSJed Brown /* 139c4762a1bSJed Brown Note: The user should initialize the vector, x, with the initial guess 140c4762a1bSJed Brown for the nonlinear solver prior to calling SNESSolve(). In particular, 141c4762a1bSJed Brown to employ an initial guess of zero, the user should explicitly set 142c4762a1bSJed Brown this vector to zero by calling VecSet(). 143c4762a1bSJed Brown */ 144*5f80ce2aSJacob Faibussowitsch CHKERRQ(FormInitialGuess(x)); 145*5f80ce2aSJacob Faibussowitsch CHKERRQ(SNESSolve(snes,NULL,x)); 146*5f80ce2aSJacob Faibussowitsch CHKERRQ(SNESGetIterationNumber(snes,&its)); 147*5f80ce2aSJacob Faibussowitsch CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"number of SNES iterations = %D\n\n",its)); 148c4762a1bSJed Brown 149c4762a1bSJed Brown /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 150c4762a1bSJed Brown Check solution and clean up 151c4762a1bSJed Brown - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 152c4762a1bSJed Brown 153c4762a1bSJed Brown /* 154c4762a1bSJed Brown Check the error 155c4762a1bSJed Brown */ 156*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecAXPY(x,none,U)); 157*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecNorm(x,NORM_2,&norm)); 158*5f80ce2aSJacob Faibussowitsch CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"Norm of error %g, Iterations %D\n",(double)norm,its)); 159c4762a1bSJed Brown 160c4762a1bSJed Brown /* 161c4762a1bSJed Brown Free work space. All PETSc objects should be destroyed when they 162c4762a1bSJed Brown are no longer needed. 163c4762a1bSJed Brown */ 164*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecDestroy(&x)); CHKERRQ(VecDestroy(&r)); 165*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecDestroy(&U)); CHKERRQ(VecDestroy(&F)); 166*5f80ce2aSJacob Faibussowitsch CHKERRQ(MatDestroy(&J)); CHKERRQ(SNESDestroy(&snes)); 167c4762a1bSJed Brown ierr = PetscFinalize(); 168c4762a1bSJed Brown return ierr; 169c4762a1bSJed Brown } 170c4762a1bSJed Brown /* ------------------------------------------------------------------- */ 171c4762a1bSJed Brown /* 172c4762a1bSJed Brown FormInitialGuess - Computes initial guess. 173c4762a1bSJed Brown 174c4762a1bSJed Brown Input/Output Parameter: 175c4762a1bSJed Brown . x - the solution vector 176c4762a1bSJed Brown */ 177c4762a1bSJed Brown PetscErrorCode FormInitialGuess(Vec x) 178c4762a1bSJed Brown { 179c4762a1bSJed Brown PetscScalar pfive = .50; 180*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecSet(x,pfive)); 181c4762a1bSJed Brown return 0; 182c4762a1bSJed Brown } 183c4762a1bSJed Brown /* ------------------------------------------------------------------- */ 184c4762a1bSJed Brown /* 185c4762a1bSJed Brown FormFunction - Evaluates nonlinear function, F(x). 186c4762a1bSJed Brown 187c4762a1bSJed Brown Input Parameters: 188c4762a1bSJed Brown . snes - the SNES context 189c4762a1bSJed Brown . x - input vector 190c4762a1bSJed Brown . ctx - optional user-defined context, as set by SNESSetFunction() 191c4762a1bSJed Brown 192c4762a1bSJed Brown Output Parameter: 193c4762a1bSJed Brown . f - function vector 194c4762a1bSJed Brown 195c4762a1bSJed Brown Note: 196c4762a1bSJed Brown The user-defined context can contain any application-specific data 197c4762a1bSJed Brown needed for the function evaluation (such as various parameters, work 198c4762a1bSJed Brown vectors, and grid information). In this program the context is just 199c4762a1bSJed Brown a vector containing the right-hand-side of the discretized PDE. 200c4762a1bSJed Brown */ 201c4762a1bSJed Brown 202c4762a1bSJed Brown PetscErrorCode FormFunction(SNES snes,Vec x,Vec f,void *ctx) 203c4762a1bSJed Brown { 204c4762a1bSJed Brown Vec g = (Vec)ctx; 205c4762a1bSJed Brown const PetscScalar *xx,*gg; 206c4762a1bSJed Brown PetscScalar *ff,d; 207c4762a1bSJed Brown PetscInt i,n; 208c4762a1bSJed Brown 209c4762a1bSJed Brown /* 210c4762a1bSJed Brown Get pointers to vector data. 211c4762a1bSJed Brown - For default PETSc vectors, VecGetArray() returns a pointer to 212c4762a1bSJed Brown the data array. Otherwise, the routine is implementation dependent. 213c4762a1bSJed Brown - You MUST call VecRestoreArray() when you no longer need access to 214c4762a1bSJed Brown the array. 215c4762a1bSJed Brown */ 216*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecGetArrayRead(x,&xx)); 217*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecGetArray(f,&ff)); 218*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecGetArrayRead(g,&gg)); 219c4762a1bSJed Brown 220c4762a1bSJed Brown /* 221c4762a1bSJed Brown Compute function 222c4762a1bSJed Brown */ 223*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecGetSize(x,&n)); 224c4762a1bSJed Brown d = (PetscReal)(n - 1); d = d*d; 225c4762a1bSJed Brown ff[0] = xx[0]; 226c4762a1bSJed Brown for (i=1; i<n-1; i++) ff[i] = d*(xx[i-1] - 2.0*xx[i] + xx[i+1]) + xx[i]*xx[i] - gg[i]; 227c4762a1bSJed Brown ff[n-1] = xx[n-1] - 1.0; 228c4762a1bSJed Brown 229c4762a1bSJed Brown /* 230c4762a1bSJed Brown Restore vectors 231c4762a1bSJed Brown */ 232*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecRestoreArrayRead(x,&xx)); 233*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecRestoreArray(f,&ff)); 234*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecRestoreArrayRead(g,&gg)); 235c4762a1bSJed Brown return 0; 236c4762a1bSJed Brown } 237c4762a1bSJed Brown /* ------------------------------------------------------------------- */ 238c4762a1bSJed Brown /* 239c4762a1bSJed Brown FormJacobian - Evaluates Jacobian matrix. 240c4762a1bSJed Brown 241c4762a1bSJed Brown Input Parameters: 242c4762a1bSJed Brown . snes - the SNES context 243c4762a1bSJed Brown . x - input vector 244c4762a1bSJed Brown . dummy - optional user-defined context (not used here) 245c4762a1bSJed Brown 246c4762a1bSJed Brown Output Parameters: 247c4762a1bSJed Brown . jac - Jacobian matrix 248c4762a1bSJed Brown . B - optionally different preconditioning matrix 249c4762a1bSJed Brown 250c4762a1bSJed Brown */ 251c4762a1bSJed Brown 252c4762a1bSJed Brown PetscErrorCode FormJacobian(SNES snes,Vec x,Mat jac,Mat B,void *dummy) 253c4762a1bSJed Brown { 254c4762a1bSJed Brown const PetscScalar *xx; 255c4762a1bSJed Brown PetscScalar A[3],d; 256c4762a1bSJed Brown PetscInt i,n,j[3]; 257c4762a1bSJed Brown 258c4762a1bSJed Brown /* 259c4762a1bSJed Brown Get pointer to vector data 260c4762a1bSJed Brown */ 261*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecGetArrayRead(x,&xx)); 262c4762a1bSJed Brown 263c4762a1bSJed Brown /* 264c4762a1bSJed Brown Compute Jacobian entries and insert into matrix. 265c4762a1bSJed Brown - Note that in this case we set all elements for a particular 266c4762a1bSJed Brown row at once. 267c4762a1bSJed Brown */ 268*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecGetSize(x,&n)); 269c4762a1bSJed Brown d = (PetscReal)(n - 1); d = d*d; 270c4762a1bSJed Brown 271c4762a1bSJed Brown /* 272c4762a1bSJed Brown Interior grid points 273c4762a1bSJed Brown */ 274c4762a1bSJed Brown for (i=1; i<n-1; i++) { 275c4762a1bSJed Brown j[0] = i - 1; j[1] = i; j[2] = i + 1; 276c4762a1bSJed Brown A[0] = A[2] = d; A[1] = -2.0*d + 2.0*xx[i]; 277*5f80ce2aSJacob Faibussowitsch CHKERRQ(MatSetValues(B,1,&i,3,j,A,INSERT_VALUES)); 278c4762a1bSJed Brown } 279c4762a1bSJed Brown 280c4762a1bSJed Brown /* 281c4762a1bSJed Brown Boundary points 282c4762a1bSJed Brown */ 283c4762a1bSJed Brown i = 0; A[0] = 1.0; 284c4762a1bSJed Brown 285*5f80ce2aSJacob Faibussowitsch CHKERRQ(MatSetValues(B,1,&i,1,&i,A,INSERT_VALUES)); 286c4762a1bSJed Brown 287c4762a1bSJed Brown i = n-1; A[0] = 1.0; 288c4762a1bSJed Brown 289*5f80ce2aSJacob Faibussowitsch CHKERRQ(MatSetValues(B,1,&i,1,&i,A,INSERT_VALUES)); 290c4762a1bSJed Brown 291c4762a1bSJed Brown /* 292c4762a1bSJed Brown Restore vector 293c4762a1bSJed Brown */ 294*5f80ce2aSJacob Faibussowitsch CHKERRQ(VecRestoreArrayRead(x,&xx)); 295c4762a1bSJed Brown 296c4762a1bSJed Brown /* 297c4762a1bSJed Brown Assemble matrix 298c4762a1bSJed Brown */ 299*5f80ce2aSJacob Faibussowitsch CHKERRQ(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY)); 300*5f80ce2aSJacob Faibussowitsch CHKERRQ(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY)); 301c4762a1bSJed Brown if (jac != B) { 302*5f80ce2aSJacob Faibussowitsch CHKERRQ(MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY)); 303*5f80ce2aSJacob Faibussowitsch CHKERRQ(MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY)); 304c4762a1bSJed Brown } 305c4762a1bSJed Brown return 0; 306c4762a1bSJed Brown } 307c4762a1bSJed Brown 308c4762a1bSJed Brown /*TEST 309c4762a1bSJed Brown 310c4762a1bSJed Brown test: 311c4762a1bSJed Brown args: -snes_monitor_short -snes_view -ksp_monitor 312c4762a1bSJed Brown 313c4762a1bSJed Brown # this is just a test for SNESKSPTRASPOSEONLY and KSPSolveTranspose to behave properly 314c4762a1bSJed Brown # the solution is wrong on purpose 315c4762a1bSJed Brown test: 316c4762a1bSJed Brown requires: !single !complex 317c4762a1bSJed Brown suffix: transpose_only 318c4762a1bSJed Brown args: -snes_monitor_short -snes_view -ksp_monitor -snes_type ksptransposeonly -pc_type ilu -snes_test_jacobian -snes_test_jacobian_view -ksp_view_rhs -ksp_view_solution -ksp_view_mat_explicit -ksp_view_preconditioned_operator_explicit 319c4762a1bSJed Brown 320c4762a1bSJed Brown TEST*/ 321