Actual source code: ex25.c
1: static const char help[] ="Minimum surface problem in 2D.\n\
2: Uses 2-dimensional distributed arrays.\n\
3: \n\
4: Solves the linear systems via multilevel methods \n\
5: \n\n";
7: /*T
8: Concepts: SNES^solving a system of nonlinear equations
9: Concepts: DMDA^using distributed arrays
10: Concepts: multigrid;
11: Processors: n
12: T*/
14: /*
16: This example models the partial differential equation
18: - Div((1 + ||GRAD T||^2)^(1/2) (GRAD T)) = 0.
20: in the unit square, which is uniformly discretized in each of x and
21: y in this simple encoding. The degrees of freedom are vertex centered
23: A finite difference approximation with the usual 5-point stencil
24: is used to discretize the boundary value problem to obtain a
25: nonlinear system of equations.
27: */
29: #include <petscsnes.h>
30: #include <petscdm.h>
31: #include <petscdmda.h>
33: extern PetscErrorCode FormFunctionLocal(DMDALocalInfo*,PetscScalar**,PetscScalar**,void*);
35: int main(int argc,char **argv)
36: {
37: SNES snes;
39: PetscInt its,lits;
40: PetscReal litspit;
41: DM da;
43: PetscInitialize(&argc,&argv,NULL,help);if (ierr) return ierr;
44: /*
45: Set the DMDA (grid structure) for the grids.
46: */
47: DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,5,5,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0,&da);
48: DMSetFromOptions(da);
49: DMSetUp(da);
50: DMDASNESSetFunctionLocal(da,INSERT_VALUES,(PetscErrorCode (*)(DMDALocalInfo*,void*,void*,void*))FormFunctionLocal,NULL);
51: SNESCreate(PETSC_COMM_WORLD,&snes);
52: SNESSetDM(snes,da);
53: DMDestroy(&da);
55: SNESSetFromOptions(snes);
57: SNESSolve(snes,0,0);
58: SNESGetIterationNumber(snes,&its);
59: SNESGetLinearSolveIterations(snes,&lits);
60: litspit = ((PetscReal)lits)/((PetscReal)its);
61: PetscPrintf(PETSC_COMM_WORLD,"Number of SNES iterations = %D\n",its);
62: PetscPrintf(PETSC_COMM_WORLD,"Number of Linear iterations = %D\n",lits);
63: PetscPrintf(PETSC_COMM_WORLD,"Average Linear its / SNES = %e\n",(double)litspit);
65: SNESDestroy(&snes);
66: PetscFinalize();
67: return ierr;
68: }
70: PetscErrorCode FormFunctionLocal(DMDALocalInfo *info,PetscScalar **t,PetscScalar **f,void *ptr)
71: {
72: PetscInt i,j;
73: PetscScalar hx,hy;
74: PetscScalar gradup,graddown,gradleft,gradright,gradx,grady;
75: PetscScalar coeffup,coeffdown,coeffleft,coeffright;
78: hx = 1.0/(PetscReal)(info->mx-1); hy = 1.0/(PetscReal)(info->my-1);
80: /* Evaluate function */
81: for (j=info->ys; j<info->ys+info->ym; j++) {
82: for (i=info->xs; i<info->xs+info->xm; i++) {
84: if (i == 0 || i == info->mx-1 || j == 0 || j == info->my-1) {
85: f[j][i] = t[j][i] - (1.0 - (2.0*hx*(PetscReal)i - 1.0)*(2.0*hx*(PetscReal)i - 1.0));
86: } else {
88: gradup = (t[j+1][i] - t[j][i])/hy;
89: graddown = (t[j][i] - t[j-1][i])/hy;
90: gradright = (t[j][i+1] - t[j][i])/hx;
91: gradleft = (t[j][i] - t[j][i-1])/hx;
93: gradx = .5*(t[j][i+1] - t[j][i-1])/hx;
94: grady = .5*(t[j+1][i] - t[j-1][i])/hy;
96: coeffup = 1.0/PetscSqrtScalar(1.0 + gradup*gradup + gradx*gradx);
97: coeffdown = 1.0/PetscSqrtScalar(1.0 + graddown*graddown + gradx*gradx);
99: coeffleft = 1.0/PetscSqrtScalar(1.0 + gradleft*gradleft + grady*grady);
100: coeffright = 1.0/PetscSqrtScalar(1.0 + gradright*gradright + grady*grady);
102: f[j][i] = (coeffup*gradup - coeffdown*graddown)*hx + (coeffright*gradright - coeffleft*gradleft)*hy;
103: }
105: }
106: }
107: return(0);
108: }
110: /*TEST
112: test:
113: args: -pc_type mg -da_refine 1 -ksp_type fgmres
115: test:
116: suffix: 2
117: nsize: 2
118: args: -pc_type mg -da_refine 1 -ksp_type fgmres
120: TEST*/