xref: /petsc/src/snes/tutorials/ex14.c (revision 5f80ce2ab25dff0f4601e710601cbbcecf323266)
1c4762a1bSJed Brown 
2c4762a1bSJed Brown static char help[] = "Bratu nonlinear PDE in 3d.\n\
3c4762a1bSJed Brown We solve the  Bratu (SFI - solid fuel ignition) problem in a 3D rectangular\n\
4c4762a1bSJed Brown domain, using distributed arrays (DMDAs) to partition the parallel grid.\n\
5c4762a1bSJed Brown The command line options include:\n\
6c4762a1bSJed Brown   -par <parameter>, where <parameter> indicates the problem's nonlinearity\n\
7c4762a1bSJed Brown      problem SFI:  <parameter> = Bratu parameter (0 <= par <= 6.81)\n\n";
8c4762a1bSJed Brown 
9c4762a1bSJed Brown /*T
10c4762a1bSJed Brown    Concepts: SNES^parallel Bratu example
11c4762a1bSJed Brown    Concepts: DMDA^using distributed arrays;
12c4762a1bSJed Brown    Processors: n
13c4762a1bSJed Brown T*/
14c4762a1bSJed Brown 
15c4762a1bSJed Brown /* ------------------------------------------------------------------------
16c4762a1bSJed Brown 
17c4762a1bSJed Brown     Solid Fuel Ignition (SFI) problem.  This problem is modeled by
18c4762a1bSJed Brown     the partial differential equation
19c4762a1bSJed Brown 
20c4762a1bSJed Brown             -Laplacian u - lambda*exp(u) = 0,  0 < x,y < 1,
21c4762a1bSJed Brown 
22c4762a1bSJed Brown     with boundary conditions
23c4762a1bSJed Brown 
24c4762a1bSJed Brown              u = 0  for  x = 0, x = 1, y = 0, y = 1, z = 0, z = 1
25c4762a1bSJed Brown 
26c4762a1bSJed Brown     A finite difference approximation with the usual 7-point stencil
27c4762a1bSJed Brown     is used to discretize the boundary value problem to obtain a nonlinear
28c4762a1bSJed Brown     system of equations.
29c4762a1bSJed Brown 
30c4762a1bSJed Brown   ------------------------------------------------------------------------- */
31c4762a1bSJed Brown 
32c4762a1bSJed Brown /*
33c4762a1bSJed Brown    Include "petscdmda.h" so that we can use distributed arrays (DMDAs).
34c4762a1bSJed Brown    Include "petscsnes.h" so that we can use SNES solvers.  Note that this
35c4762a1bSJed Brown    file automatically includes:
36c4762a1bSJed Brown      petscsys.h       - base PETSc routines   petscvec.h - vectors
37c4762a1bSJed Brown      petscmat.h - matrices
38c4762a1bSJed Brown      petscis.h     - index sets            petscksp.h - Krylov subspace methods
39c4762a1bSJed Brown      petscviewer.h - viewers               petscpc.h  - preconditioners
40c4762a1bSJed Brown      petscksp.h   - linear solvers
41c4762a1bSJed Brown */
42c4762a1bSJed Brown #include <petscdm.h>
43c4762a1bSJed Brown #include <petscdmda.h>
44c4762a1bSJed Brown #include <petscsnes.h>
45c4762a1bSJed Brown 
46c4762a1bSJed Brown /*
47c4762a1bSJed Brown    User-defined application context - contains data needed by the
48c4762a1bSJed Brown    application-provided call-back routines, FormJacobian() and
49c4762a1bSJed Brown    FormFunction().
50c4762a1bSJed Brown */
51c4762a1bSJed Brown typedef struct {
52c4762a1bSJed Brown   PetscReal param;             /* test problem parameter */
53c4762a1bSJed Brown   DM        da;                /* distributed array data structure */
54c4762a1bSJed Brown } AppCtx;
55c4762a1bSJed Brown 
56c4762a1bSJed Brown /*
57c4762a1bSJed Brown    User-defined routines
58c4762a1bSJed Brown */
59c4762a1bSJed Brown extern PetscErrorCode FormFunctionLocal(SNES,Vec,Vec,void*);
60c4762a1bSJed Brown extern PetscErrorCode FormFunction(SNES,Vec,Vec,void*);
61c4762a1bSJed Brown extern PetscErrorCode FormInitialGuess(AppCtx*,Vec);
62c4762a1bSJed Brown extern PetscErrorCode FormJacobian(SNES,Vec,Mat,Mat,void*);
63c4762a1bSJed Brown 
64c4762a1bSJed Brown int main(int argc,char **argv)
65c4762a1bSJed Brown {
66c4762a1bSJed Brown   SNES           snes;                         /* nonlinear solver */
67c4762a1bSJed Brown   Vec            x,r;                          /* solution, residual vectors */
68c4762a1bSJed Brown   Mat            J = NULL;                            /* Jacobian matrix */
69c4762a1bSJed Brown   AppCtx         user;                         /* user-defined work context */
70c4762a1bSJed Brown   PetscInt       its;                          /* iterations for convergence */
71c4762a1bSJed Brown   MatFDColoring  matfdcoloring = NULL;
72c4762a1bSJed Brown   PetscBool      matrix_free = PETSC_FALSE,coloring = PETSC_FALSE, coloring_ds = PETSC_FALSE,local_coloring = PETSC_FALSE;
73c4762a1bSJed Brown   PetscErrorCode ierr;
74c4762a1bSJed Brown   PetscReal      bratu_lambda_max = 6.81,bratu_lambda_min = 0.,fnorm;
75c4762a1bSJed Brown 
76c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77c4762a1bSJed Brown      Initialize program
78c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
79c4762a1bSJed Brown 
80c4762a1bSJed Brown   ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
81c4762a1bSJed Brown 
82c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
83c4762a1bSJed Brown      Initialize problem parameters
84c4762a1bSJed Brown   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
85c4762a1bSJed Brown   user.param = 6.0;
86*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetReal(NULL,NULL,"-par",&user.param,NULL));
872c71b3e2SJacob Faibussowitsch   PetscCheckFalse(user.param >= bratu_lambda_max || user.param <= bratu_lambda_min,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lambda is out of range");
88c4762a1bSJed Brown 
89c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
90c4762a1bSJed Brown      Create nonlinear solver context
91c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
92*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESCreate(PETSC_COMM_WORLD,&snes));
93c4762a1bSJed Brown 
94c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95c4762a1bSJed Brown      Create distributed array (DMDA) to manage parallel grid and vectors
96c4762a1bSJed Brown   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
97*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDACreate3d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,4,4,4,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,NULL,&user.da));
98*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetFromOptions(user.da));
99*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetUp(user.da));
100c4762a1bSJed Brown   /*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
101c4762a1bSJed Brown      Extract global vectors from DMDA; then duplicate for remaining
102c4762a1bSJed Brown      vectors that are the same types
103c4762a1bSJed Brown    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
104*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreateGlobalVector(user.da,&x));
105*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDuplicate(x,&r));
106c4762a1bSJed Brown 
107c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
108c4762a1bSJed Brown      Set function evaluation routine and vector
109c4762a1bSJed Brown   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
110*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESSetFunction(snes,r,FormFunction,(void*)&user));
111c4762a1bSJed Brown 
112c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
113c4762a1bSJed Brown      Create matrix data structure; set Jacobian evaluation routine
114c4762a1bSJed Brown 
115c4762a1bSJed Brown      Set Jacobian matrix data structure and default Jacobian evaluation
116c4762a1bSJed Brown      routine. User can override with:
117c4762a1bSJed Brown      -snes_mf : matrix-free Newton-Krylov method with no preconditioning
118c4762a1bSJed Brown                 (unless user explicitly sets preconditioner)
119c4762a1bSJed Brown      -snes_mf_operator : form preconditioning matrix as set by the user,
120c4762a1bSJed Brown                          but use matrix-free approx for Jacobian-vector
121c4762a1bSJed Brown                          products within Newton-Krylov method
122c4762a1bSJed Brown      -fdcoloring : using finite differences with coloring to compute the Jacobian
123c4762a1bSJed Brown 
124c4762a1bSJed Brown      Note one can use -matfd_coloring wp or ds the only reason for the -fdcoloring_ds option
125c4762a1bSJed Brown      below is to test the call to MatFDColoringSetType().
126c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
127*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-snes_mf",&matrix_free,NULL));
128*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-fdcoloring",&coloring,NULL));
129*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-fdcoloring_ds",&coloring_ds,NULL));
130*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-fdcoloring_local",&local_coloring,NULL));
131c4762a1bSJed Brown   if (!matrix_free) {
132*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetMatType(user.da,MATAIJ));
133*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateMatrix(user.da,&J));
134c4762a1bSJed Brown     if (coloring) {
135c4762a1bSJed Brown       ISColoring iscoloring;
136c4762a1bSJed Brown       if (!local_coloring) {
137*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMCreateColoring(user.da,IS_COLORING_GLOBAL,&iscoloring));
138*5f80ce2aSJacob Faibussowitsch         CHKERRQ(MatFDColoringCreate(J,iscoloring,&matfdcoloring));
139*5f80ce2aSJacob Faibussowitsch         CHKERRQ(MatFDColoringSetFunction(matfdcoloring,(PetscErrorCode (*)(void))FormFunction,&user));
140c4762a1bSJed Brown       } else {
141*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMCreateColoring(user.da,IS_COLORING_LOCAL,&iscoloring));
142*5f80ce2aSJacob Faibussowitsch         CHKERRQ(MatFDColoringCreate(J,iscoloring,&matfdcoloring));
143*5f80ce2aSJacob Faibussowitsch         CHKERRQ(MatFDColoringUseDM(J,matfdcoloring));
144*5f80ce2aSJacob Faibussowitsch         CHKERRQ(MatFDColoringSetFunction(matfdcoloring,(PetscErrorCode (*)(void))FormFunctionLocal,&user));
145c4762a1bSJed Brown       }
146c4762a1bSJed Brown       if (coloring_ds) {
147*5f80ce2aSJacob Faibussowitsch         CHKERRQ(MatFDColoringSetType(matfdcoloring,MATMFFD_DS));
148c4762a1bSJed Brown       }
149*5f80ce2aSJacob Faibussowitsch       CHKERRQ(MatFDColoringSetFromOptions(matfdcoloring));
150*5f80ce2aSJacob Faibussowitsch       CHKERRQ(MatFDColoringSetUp(J,iscoloring,matfdcoloring));
151*5f80ce2aSJacob Faibussowitsch       CHKERRQ(SNESSetJacobian(snes,J,J,SNESComputeJacobianDefaultColor,matfdcoloring));
152*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISColoringDestroy(&iscoloring));
153c4762a1bSJed Brown     } else {
154*5f80ce2aSJacob Faibussowitsch       CHKERRQ(SNESSetJacobian(snes,J,J,FormJacobian,&user));
155c4762a1bSJed Brown     }
156c4762a1bSJed Brown   }
157c4762a1bSJed Brown 
158c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
159c4762a1bSJed Brown      Customize nonlinear solver; set runtime options
160c4762a1bSJed Brown    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
161*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESSetDM(snes,user.da));
162*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESSetFromOptions(snes));
163c4762a1bSJed Brown 
164c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
165c4762a1bSJed Brown      Evaluate initial guess
166c4762a1bSJed Brown      Note: The user should initialize the vector, x, with the initial guess
167c4762a1bSJed Brown      for the nonlinear solver prior to calling SNESSolve().  In particular,
168c4762a1bSJed Brown      to employ an initial guess of zero, the user should explicitly set
169c4762a1bSJed Brown      this vector to zero by calling VecSet().
170c4762a1bSJed Brown   */
171*5f80ce2aSJacob Faibussowitsch   CHKERRQ(FormInitialGuess(&user,x));
172c4762a1bSJed Brown 
173c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
174c4762a1bSJed Brown      Solve nonlinear system
175c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
176*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESSolve(snes,NULL,x));
177*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESGetIterationNumber(snes,&its));
178c4762a1bSJed Brown 
179c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
180c4762a1bSJed Brown      Explicitly check norm of the residual of the solution
181c4762a1bSJed Brown    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
182*5f80ce2aSJacob Faibussowitsch   CHKERRQ(FormFunction(snes,x,r,(void*)&user));
183*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecNorm(r,NORM_2,&fnorm));
184*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"Number of SNES iterations = %D fnorm %g\n",its,(double)fnorm));
185c4762a1bSJed Brown 
186c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
187c4762a1bSJed Brown      Free work space.  All PETSc objects should be destroyed when they
188c4762a1bSJed Brown      are no longer needed.
189c4762a1bSJed Brown    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
190c4762a1bSJed Brown 
191*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatDestroy(&J));
192*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&x));
193*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&r));
194*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESDestroy(&snes));
195*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&user.da));
196*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatFDColoringDestroy(&matfdcoloring));
197c4762a1bSJed Brown   ierr = PetscFinalize();
198c4762a1bSJed Brown   return ierr;
199c4762a1bSJed Brown }
200c4762a1bSJed Brown /* ------------------------------------------------------------------- */
201c4762a1bSJed Brown /*
202c4762a1bSJed Brown    FormInitialGuess - Forms initial approximation.
203c4762a1bSJed Brown 
204c4762a1bSJed Brown    Input Parameters:
205c4762a1bSJed Brown    user - user-defined application context
206c4762a1bSJed Brown    X - vector
207c4762a1bSJed Brown 
208c4762a1bSJed Brown    Output Parameter:
209c4762a1bSJed Brown    X - vector
210c4762a1bSJed Brown  */
211c4762a1bSJed Brown PetscErrorCode FormInitialGuess(AppCtx *user,Vec X)
212c4762a1bSJed Brown {
213c4762a1bSJed Brown   PetscInt       i,j,k,Mx,My,Mz,xs,ys,zs,xm,ym,zm;
214c4762a1bSJed Brown   PetscReal      lambda,temp1,hx,hy,hz,tempk,tempj;
215c4762a1bSJed Brown   PetscScalar    ***x;
216c4762a1bSJed Brown 
217c4762a1bSJed Brown   PetscFunctionBeginUser;
218*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDAGetInfo(user->da,PETSC_IGNORE,&Mx,&My,&Mz,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE));
219c4762a1bSJed Brown 
220c4762a1bSJed Brown   lambda = user->param;
221c4762a1bSJed Brown   hx     = 1.0/(PetscReal)(Mx-1);
222c4762a1bSJed Brown   hy     = 1.0/(PetscReal)(My-1);
223c4762a1bSJed Brown   hz     = 1.0/(PetscReal)(Mz-1);
224c4762a1bSJed Brown   temp1  = lambda/(lambda + 1.0);
225c4762a1bSJed Brown 
226c4762a1bSJed Brown   /*
227c4762a1bSJed Brown      Get a pointer to vector data.
228c4762a1bSJed Brown        - For default PETSc vectors, VecGetArray() returns a pointer to
229c4762a1bSJed Brown          the data array.  Otherwise, the routine is implementation dependent.
230c4762a1bSJed Brown        - You MUST call VecRestoreArray() when you no longer need access to
231c4762a1bSJed Brown          the array.
232c4762a1bSJed Brown   */
233*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDAVecGetArray(user->da,X,&x));
234c4762a1bSJed Brown 
235c4762a1bSJed Brown   /*
236c4762a1bSJed Brown      Get local grid boundaries (for 3-dimensional DMDA):
237c4762a1bSJed Brown        xs, ys, zs   - starting grid indices (no ghost points)
238c4762a1bSJed Brown        xm, ym, zm   - widths of local grid (no ghost points)
239c4762a1bSJed Brown 
240c4762a1bSJed Brown   */
241*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDAGetCorners(user->da,&xs,&ys,&zs,&xm,&ym,&zm));
242c4762a1bSJed Brown 
243c4762a1bSJed Brown   /*
244c4762a1bSJed Brown      Compute initial guess over the locally owned part of the grid
245c4762a1bSJed Brown   */
246c4762a1bSJed Brown   for (k=zs; k<zs+zm; k++) {
247c4762a1bSJed Brown     tempk = (PetscReal)(PetscMin(k,Mz-k-1))*hz;
248c4762a1bSJed Brown     for (j=ys; j<ys+ym; j++) {
249c4762a1bSJed Brown       tempj = PetscMin((PetscReal)(PetscMin(j,My-j-1))*hy,tempk);
250c4762a1bSJed Brown       for (i=xs; i<xs+xm; i++) {
251c4762a1bSJed Brown         if (i == 0 || j == 0 || k == 0 || i == Mx-1 || j == My-1 || k == Mz-1) {
252c4762a1bSJed Brown           /* boundary conditions are all zero Dirichlet */
253c4762a1bSJed Brown           x[k][j][i] = 0.0;
254c4762a1bSJed Brown         } else {
255c4762a1bSJed Brown           x[k][j][i] = temp1*PetscSqrtReal(PetscMin((PetscReal)(PetscMin(i,Mx-i-1))*hx,tempj));
256c4762a1bSJed Brown         }
257c4762a1bSJed Brown       }
258c4762a1bSJed Brown     }
259c4762a1bSJed Brown   }
260c4762a1bSJed Brown 
261c4762a1bSJed Brown   /*
262c4762a1bSJed Brown      Restore vector
263c4762a1bSJed Brown   */
264*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDAVecRestoreArray(user->da,X,&x));
265c4762a1bSJed Brown   PetscFunctionReturn(0);
266c4762a1bSJed Brown }
267c4762a1bSJed Brown /* ------------------------------------------------------------------- */
268c4762a1bSJed Brown /*
269c4762a1bSJed Brown    FormFunctionLocal - Evaluates nonlinear function, F(x) on a ghosted patch
270c4762a1bSJed Brown 
271c4762a1bSJed Brown    Input Parameters:
272c4762a1bSJed Brown .  snes - the SNES context
273c4762a1bSJed Brown .  localX - input vector, this contains the ghosted region needed
274c4762a1bSJed Brown .  ptr - optional user-defined context, as set by SNESSetFunction()
275c4762a1bSJed Brown 
276c4762a1bSJed Brown    Output Parameter:
277c4762a1bSJed Brown .  F - function vector, this does not contain a ghosted region
278c4762a1bSJed Brown  */
279c4762a1bSJed Brown PetscErrorCode FormFunctionLocal(SNES snes,Vec localX,Vec F,void *ptr)
280c4762a1bSJed Brown {
281c4762a1bSJed Brown   AppCtx         *user = (AppCtx*)ptr;
282c4762a1bSJed Brown   PetscInt       i,j,k,Mx,My,Mz,xs,ys,zs,xm,ym,zm;
283c4762a1bSJed Brown   PetscReal      two = 2.0,lambda,hx,hy,hz,hxhzdhy,hyhzdhx,hxhydhz,sc;
284c4762a1bSJed Brown   PetscScalar    u_north,u_south,u_east,u_west,u_up,u_down,u,u_xx,u_yy,u_zz,***x,***f;
285c4762a1bSJed Brown   DM             da;
286c4762a1bSJed Brown 
287c4762a1bSJed Brown   PetscFunctionBeginUser;
288*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESGetDM(snes,&da));
289*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDAGetInfo(da,PETSC_IGNORE,&Mx,&My,&Mz,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE));
290c4762a1bSJed Brown 
291c4762a1bSJed Brown   lambda  = user->param;
292c4762a1bSJed Brown   hx      = 1.0/(PetscReal)(Mx-1);
293c4762a1bSJed Brown   hy      = 1.0/(PetscReal)(My-1);
294c4762a1bSJed Brown   hz      = 1.0/(PetscReal)(Mz-1);
295c4762a1bSJed Brown   sc      = hx*hy*hz*lambda;
296c4762a1bSJed Brown   hxhzdhy = hx*hz/hy;
297c4762a1bSJed Brown   hyhzdhx = hy*hz/hx;
298c4762a1bSJed Brown   hxhydhz = hx*hy/hz;
299c4762a1bSJed Brown 
300c4762a1bSJed Brown   /*
301c4762a1bSJed Brown      Get pointers to vector data
302c4762a1bSJed Brown   */
303*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDAVecGetArrayRead(da,localX,&x));
304*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDAVecGetArray(da,F,&f));
305c4762a1bSJed Brown 
306c4762a1bSJed Brown   /*
307c4762a1bSJed Brown      Get local grid boundaries
308c4762a1bSJed Brown   */
309*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm));
310c4762a1bSJed Brown 
311c4762a1bSJed Brown   /*
312c4762a1bSJed Brown      Compute function over the locally owned part of the grid
313c4762a1bSJed Brown   */
314c4762a1bSJed Brown   for (k=zs; k<zs+zm; k++) {
315c4762a1bSJed Brown     for (j=ys; j<ys+ym; j++) {
316c4762a1bSJed Brown       for (i=xs; i<xs+xm; i++) {
317c4762a1bSJed Brown         if (i == 0 || j == 0 || k == 0 || i == Mx-1 || j == My-1 || k == Mz-1) {
318c4762a1bSJed Brown           f[k][j][i] = x[k][j][i];
319c4762a1bSJed Brown         } else {
320c4762a1bSJed Brown           u          = x[k][j][i];
321c4762a1bSJed Brown           u_east     = x[k][j][i+1];
322c4762a1bSJed Brown           u_west     = x[k][j][i-1];
323c4762a1bSJed Brown           u_north    = x[k][j+1][i];
324c4762a1bSJed Brown           u_south    = x[k][j-1][i];
325c4762a1bSJed Brown           u_up       = x[k+1][j][i];
326c4762a1bSJed Brown           u_down     = x[k-1][j][i];
327c4762a1bSJed Brown           u_xx       = (-u_east + two*u - u_west)*hyhzdhx;
328c4762a1bSJed Brown           u_yy       = (-u_north + two*u - u_south)*hxhzdhy;
329c4762a1bSJed Brown           u_zz       = (-u_up + two*u - u_down)*hxhydhz;
330c4762a1bSJed Brown           f[k][j][i] = u_xx + u_yy + u_zz - sc*PetscExpScalar(u);
331c4762a1bSJed Brown         }
332c4762a1bSJed Brown       }
333c4762a1bSJed Brown     }
334c4762a1bSJed Brown   }
335c4762a1bSJed Brown 
336c4762a1bSJed Brown   /*
337c4762a1bSJed Brown      Restore vectors
338c4762a1bSJed Brown   */
339*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDAVecRestoreArrayRead(da,localX,&x));
340*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDAVecRestoreArray(da,F,&f));
341*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogFlops(11.0*ym*xm));
342c4762a1bSJed Brown   PetscFunctionReturn(0);
343c4762a1bSJed Brown }
344c4762a1bSJed Brown /* ------------------------------------------------------------------- */
345c4762a1bSJed Brown /*
346c4762a1bSJed Brown    FormFunction - Evaluates nonlinear function, F(x) on the entire domain
347c4762a1bSJed Brown 
348c4762a1bSJed Brown    Input Parameters:
349c4762a1bSJed Brown .  snes - the SNES context
350c4762a1bSJed Brown .  X - input vector
351c4762a1bSJed Brown .  ptr - optional user-defined context, as set by SNESSetFunction()
352c4762a1bSJed Brown 
353c4762a1bSJed Brown    Output Parameter:
354c4762a1bSJed Brown .  F - function vector
355c4762a1bSJed Brown  */
356c4762a1bSJed Brown PetscErrorCode FormFunction(SNES snes,Vec X,Vec F,void *ptr)
357c4762a1bSJed Brown {
358c4762a1bSJed Brown   Vec            localX;
359c4762a1bSJed Brown   DM             da;
360c4762a1bSJed Brown 
361c4762a1bSJed Brown   PetscFunctionBeginUser;
362*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESGetDM(snes,&da));
363*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLocalVector(da,&localX));
364c4762a1bSJed Brown 
365c4762a1bSJed Brown   /*
366c4762a1bSJed Brown      Scatter ghost points to local vector,using the 2-step process
367c4762a1bSJed Brown         DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
368c4762a1bSJed Brown      By placing code between these two statements, computations can be
369c4762a1bSJed Brown      done while messages are in transition.
370c4762a1bSJed Brown   */
371*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGlobalToLocalBegin(da,X,INSERT_VALUES,localX));
372*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGlobalToLocalEnd(da,X,INSERT_VALUES,localX));
373c4762a1bSJed Brown 
374*5f80ce2aSJacob Faibussowitsch   CHKERRQ(FormFunctionLocal(snes,localX,F,ptr));
375*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMRestoreLocalVector(da,&localX));
376c4762a1bSJed Brown   PetscFunctionReturn(0);
377c4762a1bSJed Brown }
378c4762a1bSJed Brown /* ------------------------------------------------------------------- */
379c4762a1bSJed Brown /*
380c4762a1bSJed Brown    FormJacobian - Evaluates Jacobian matrix.
381c4762a1bSJed Brown 
382c4762a1bSJed Brown    Input Parameters:
383c4762a1bSJed Brown .  snes - the SNES context
384c4762a1bSJed Brown .  x - input vector
385c4762a1bSJed Brown .  ptr - optional user-defined context, as set by SNESSetJacobian()
386c4762a1bSJed Brown 
387c4762a1bSJed Brown    Output Parameters:
388c4762a1bSJed Brown .  A - Jacobian matrix
389c4762a1bSJed Brown .  B - optionally different preconditioning matrix
390c4762a1bSJed Brown 
391c4762a1bSJed Brown */
392c4762a1bSJed Brown PetscErrorCode FormJacobian(SNES snes,Vec X,Mat J,Mat jac,void *ptr)
393c4762a1bSJed Brown {
394c4762a1bSJed Brown   AppCtx         *user = (AppCtx*)ptr;  /* user-defined application context */
395c4762a1bSJed Brown   Vec            localX;
396c4762a1bSJed Brown   PetscInt       i,j,k,Mx,My,Mz;
397c4762a1bSJed Brown   MatStencil     col[7],row;
398c4762a1bSJed Brown   PetscInt       xs,ys,zs,xm,ym,zm;
399c4762a1bSJed Brown   PetscScalar    lambda,v[7],hx,hy,hz,hxhzdhy,hyhzdhx,hxhydhz,sc,***x;
400c4762a1bSJed Brown   DM             da;
401c4762a1bSJed Brown 
402c4762a1bSJed Brown   PetscFunctionBeginUser;
403*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESGetDM(snes,&da));
404*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLocalVector(da,&localX));
405*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDAGetInfo(da,PETSC_IGNORE,&Mx,&My,&Mz,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE));
406c4762a1bSJed Brown 
407c4762a1bSJed Brown   lambda  = user->param;
408c4762a1bSJed Brown   hx      = 1.0/(PetscReal)(Mx-1);
409c4762a1bSJed Brown   hy      = 1.0/(PetscReal)(My-1);
410c4762a1bSJed Brown   hz      = 1.0/(PetscReal)(Mz-1);
411c4762a1bSJed Brown   sc      = hx*hy*hz*lambda;
412c4762a1bSJed Brown   hxhzdhy = hx*hz/hy;
413c4762a1bSJed Brown   hyhzdhx = hy*hz/hx;
414c4762a1bSJed Brown   hxhydhz = hx*hy/hz;
415c4762a1bSJed Brown 
416c4762a1bSJed Brown   /*
417c4762a1bSJed Brown      Scatter ghost points to local vector, using the 2-step process
418c4762a1bSJed Brown         DMGlobalToLocalBegin(), DMGlobalToLocalEnd().
419c4762a1bSJed Brown      By placing code between these two statements, computations can be
420c4762a1bSJed Brown      done while messages are in transition.
421c4762a1bSJed Brown   */
422*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGlobalToLocalBegin(da,X,INSERT_VALUES,localX));
423*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGlobalToLocalEnd(da,X,INSERT_VALUES,localX));
424c4762a1bSJed Brown 
425c4762a1bSJed Brown   /*
426c4762a1bSJed Brown      Get pointer to vector data
427c4762a1bSJed Brown   */
428*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDAVecGetArrayRead(da,localX,&x));
429c4762a1bSJed Brown 
430c4762a1bSJed Brown   /*
431c4762a1bSJed Brown      Get local grid boundaries
432c4762a1bSJed Brown   */
433*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm));
434c4762a1bSJed Brown 
435c4762a1bSJed Brown   /*
436c4762a1bSJed Brown      Compute entries for the locally owned part of the Jacobian.
437c4762a1bSJed Brown       - Currently, all PETSc parallel matrix formats are partitioned by
438c4762a1bSJed Brown         contiguous chunks of rows across the processors.
439c4762a1bSJed Brown       - Each processor needs to insert only elements that it owns
440c4762a1bSJed Brown         locally (but any non-local elements will be sent to the
441c4762a1bSJed Brown         appropriate processor during matrix assembly).
442c4762a1bSJed Brown       - Here, we set all entries for a particular row at once.
443c4762a1bSJed Brown       - We can set matrix entries either using either
444c4762a1bSJed Brown         MatSetValuesLocal() or MatSetValues(), as discussed above.
445c4762a1bSJed Brown   */
446c4762a1bSJed Brown   for (k=zs; k<zs+zm; k++) {
447c4762a1bSJed Brown     for (j=ys; j<ys+ym; j++) {
448c4762a1bSJed Brown       for (i=xs; i<xs+xm; i++) {
449c4762a1bSJed Brown         row.k = k; row.j = j; row.i = i;
450c4762a1bSJed Brown         /* boundary points */
451c4762a1bSJed Brown         if (i == 0 || j == 0 || k == 0|| i == Mx-1 || j == My-1 || k == Mz-1) {
452c4762a1bSJed Brown           v[0] = 1.0;
453*5f80ce2aSJacob Faibussowitsch           CHKERRQ(MatSetValuesStencil(jac,1,&row,1,&row,v,INSERT_VALUES));
454c4762a1bSJed Brown         } else {
455c4762a1bSJed Brown           /* interior grid points */
456c4762a1bSJed Brown           v[0] = -hxhydhz; col[0].k=k-1;col[0].j=j;  col[0].i = i;
457c4762a1bSJed Brown           v[1] = -hxhzdhy; col[1].k=k;  col[1].j=j-1;col[1].i = i;
458c4762a1bSJed Brown           v[2] = -hyhzdhx; col[2].k=k;  col[2].j=j;  col[2].i = i-1;
459c4762a1bSJed Brown           v[3] = 2.0*(hyhzdhx+hxhzdhy+hxhydhz)-sc*PetscExpScalar(x[k][j][i]);col[3].k=row.k;col[3].j=row.j;col[3].i = row.i;
460c4762a1bSJed Brown           v[4] = -hyhzdhx; col[4].k=k;  col[4].j=j;  col[4].i = i+1;
461c4762a1bSJed Brown           v[5] = -hxhzdhy; col[5].k=k;  col[5].j=j+1;col[5].i = i;
462c4762a1bSJed Brown           v[6] = -hxhydhz; col[6].k=k+1;col[6].j=j;  col[6].i = i;
463*5f80ce2aSJacob Faibussowitsch           CHKERRQ(MatSetValuesStencil(jac,1,&row,7,col,v,INSERT_VALUES));
464c4762a1bSJed Brown         }
465c4762a1bSJed Brown       }
466c4762a1bSJed Brown     }
467c4762a1bSJed Brown   }
468*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDAVecRestoreArrayRead(da,localX,&x));
469*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMRestoreLocalVector(da,&localX));
470c4762a1bSJed Brown 
471c4762a1bSJed Brown   /*
472c4762a1bSJed Brown      Assemble matrix, using the 2-step process:
473c4762a1bSJed Brown        MatAssemblyBegin(), MatAssemblyEnd().
474c4762a1bSJed Brown   */
475*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY));
476*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY));
477c4762a1bSJed Brown 
478c4762a1bSJed Brown   /*
479c4762a1bSJed Brown      Normally since the matrix has already been assembled above; this
480c4762a1bSJed Brown      would do nothing. But in the matrix free mode -snes_mf_operator
481c4762a1bSJed Brown      this tells the "matrix-free" matrix that a new linear system solve
482c4762a1bSJed Brown      is about to be done.
483c4762a1bSJed Brown   */
484c4762a1bSJed Brown 
485*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY));
486*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY));
487c4762a1bSJed Brown 
488c4762a1bSJed Brown   /*
489c4762a1bSJed Brown      Tell the matrix we will never add a new nonzero location to the
490c4762a1bSJed Brown      matrix. If we do, it will generate an error.
491c4762a1bSJed Brown   */
492*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatSetOption(jac,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE));
493c4762a1bSJed Brown   PetscFunctionReturn(0);
494c4762a1bSJed Brown }
495c4762a1bSJed Brown 
496c4762a1bSJed Brown /*TEST
497c4762a1bSJed Brown 
498c4762a1bSJed Brown    test:
499c4762a1bSJed Brown       nsize: 4
500c4762a1bSJed Brown       args: -snes_monitor_short -ksp_gmres_cgs_refinement_type refine_always
501c4762a1bSJed Brown 
502c4762a1bSJed Brown    test:
503c4762a1bSJed Brown       suffix: 2
504c4762a1bSJed Brown       nsize: 4
505c4762a1bSJed Brown       args: -snes_monitor_short -ksp_gmres_cgs_refinement_type refine_always
506c4762a1bSJed Brown 
507c4762a1bSJed Brown    test:
508c4762a1bSJed Brown       suffix: 3
509c4762a1bSJed Brown       nsize: 4
510c4762a1bSJed Brown       args: -fdcoloring -snes_monitor_short -ksp_gmres_cgs_refinement_type refine_always
511c4762a1bSJed Brown 
512c4762a1bSJed Brown    test:
513c4762a1bSJed Brown       suffix: 3_ds
514c4762a1bSJed Brown       nsize: 4
515c4762a1bSJed Brown       args: -fdcoloring -fdcoloring_ds -snes_monitor_short -ksp_gmres_cgs_refinement_type refine_always
516c4762a1bSJed Brown 
517c4762a1bSJed Brown    test:
518c4762a1bSJed Brown       suffix: 4
519c4762a1bSJed Brown       nsize: 4
520c4762a1bSJed Brown       args: -fdcoloring_local -fdcoloring -ksp_monitor_short -da_refine 1
521c4762a1bSJed Brown       requires: !single
522c4762a1bSJed Brown 
52341ba4c6cSHeeho Park    test:
52441ba4c6cSHeeho Park       suffix: 5
52541ba4c6cSHeeho Park       nsize: 4
52641ba4c6cSHeeho Park       args: -fdcoloring_local -fdcoloring -ksp_monitor_short -da_refine 1 -snes_type newtontrdc
52741ba4c6cSHeeho Park       requires: !single
52841ba4c6cSHeeho Park 
529c4762a1bSJed Brown TEST*/
530